MIRROR: javascript for ๐Ÿœ's, a tiny runtime with big ambitions
1
fork

Configure Feed

Select the types of activity you want to include in your feed.

add support for coroutine stack size environment variable

+51 -26
+1 -1
src/modules/server.c
··· 110 110 111 111 static void on_js_timer(uv_timer_t *handle) { 112 112 struct js *js = (struct js*)handle->data; 113 - if (js) if (has_pending_immediates()) process_immediates(js); 113 + if (js && has_pending_immediates()) process_immediates(js); 114 114 } 115 115 116 116 static int parse_accept_encoding(const char *buffer, size_t len) {
+31 -19
src/modules/timer.c
··· 47 47 int active_timer_count; 48 48 } timer_state = {NULL, NULL, NULL, NULL, NULL, NULL, 1, 1, 0}; 49 49 50 + static void add_timer_entry(timer_entry_t *entry) { 51 + entry->next = timer_state.timers; 52 + entry->prev = NULL; 53 + if (timer_state.timers) { 54 + timer_state.timers->prev = entry; 55 + } 56 + timer_state.timers = entry; 57 + } 58 + 59 + static void remove_timer_entry(timer_entry_t *entry) { 60 + if (entry->prev) entry->prev->next = entry->next; 61 + else timer_state.timers = entry->next; 62 + if (entry->next) entry->next->prev = entry->prev; 63 + } 64 + 65 + static void timer_close_cb(uv_handle_t *h) { 66 + timer_entry_t *entry = (timer_entry_t *)h->data; 67 + if (!entry) return; 68 + 69 + remove_timer_entry(entry); 70 + entry->callback = 0; 71 + entry->next = NULL; 72 + entry->prev = NULL; 73 + h->data = NULL; 74 + 75 + free(entry); 76 + } 77 + 50 78 static void timer_callback(uv_timer_t *handle) { 51 79 timer_entry_t *entry = (timer_entry_t *)handle->data; 52 80 if (!entry || !entry->active) return; ··· 58 86 59 87 if (!entry->is_interval) { 60 88 entry->active = 0; 61 - uv_timer_stop(&entry->handle); 62 89 timer_state.active_timer_count--; 63 - } 64 - } 65 - 66 - static void add_timer_entry(timer_entry_t *entry) { 67 - entry->next = timer_state.timers; 68 - entry->prev = NULL; 69 - if (timer_state.timers) { 70 - timer_state.timers->prev = entry; 90 + uv_close((uv_handle_t *)&entry->handle, timer_close_cb); 71 91 } 72 - timer_state.timers = entry; 73 - } 74 - 75 - static void remove_timer_entry(timer_entry_t *entry) { 76 - if (entry->prev) entry->prev->next = entry->next; 77 - else timer_state.timers = entry->next; 78 - if (entry->next) entry->next->prev = entry->prev; 79 92 } 80 93 81 94 // setTimeout(callback, delay) ··· 139 152 140 153 for (timer_entry_t *entry = timer_state.timers; entry != NULL; entry = entry->next) { 141 154 if (entry->timer_id == timer_id && entry->active) { 142 - entry->active = 0; 143 - uv_timer_stop(&entry->handle); 144 - timer_state.active_timer_count--; break; 155 + entry->active = 0; timer_state.active_timer_count--; 156 + uv_close((uv_handle_t *)&entry->handle, timer_close_cb); break; 145 157 } 146 158 } 147 159
+1
src/reactor.c
··· 79 79 80 80 if (work & WORK_BLOCKING) uv_run(uv_default_loop(), UV_RUN_NOWAIT); 81 81 else if (work & WORK_ASYNC) { maybe_gc(js); uv_run(uv_default_loop(), UV_RUN_ONCE); } 82 + else if (work & WORK_COROUTINES) break; 82 83 } 83 84 84 85 js_poll_events(js);
+1 -2
src/snapshot.c
··· 1 1 #include <string.h> 2 2 3 3 #include "ant.h" 4 - #include "errors.h" 5 4 #include "snapshot.h" 6 5 #include "internal.h" 7 6 #include "snapshot_data.h" 8 7 9 - jsval_t ant_load_snapshot(struct js *js) { 8 + jsval_t ant_load_snapshot(ant_t *js) { 10 9 if (!js) return js_mkundef(); 11 10 jsval_t result = js_eval(js, (const char *)ant_snapshot_source, ant_snapshot_source_len); 12 11
+17 -4
src/sugar.c
··· 102 102 } 103 103 104 104 static size_t calculate_coro_stack_size(void) { 105 - if (coro_stack_size_initialized) return 0; 105 + static size_t cached_size = 0; 106 + if (coro_stack_size_initialized) return cached_size; 106 107 coro_stack_size_initialized = true; 107 108 const char *env_stack = getenv("ANT_CORO_STACK_SIZE"); 108 109 if (env_stack) { 109 110 size_t size = (size_t)atoi(env_stack) * 1024; 110 - if (size >= 32 * 1024 && size <= 8 * 1024 * 1024) return size; 111 + if (size >= 32 * 1024 && size <= 8 * 1024 * 1024) { 112 + cached_size = size; return cached_size; 113 + } 111 114 } 112 - return 0; 115 + return cached_size; 113 116 } 114 117 115 118 static void mco_async_entry(mco_coro* mco) { ··· 212 215 213 216 if (nargs > 0) { 214 217 coro->args = (jsval_t *)CORO_MALLOC(sizeof(jsval_t) * nargs); 215 - if (coro->args) memcpy(coro->args, args, sizeof(jsval_t) * nargs); 218 + if (!coro->args) { 219 + mco_destroy(mco); CORO_FREE(coro); CORO_FREE(ctx); 220 + return js_mkerr(js, "out of memory for coroutine args"); 221 + } 222 + memcpy(coro->args, args, sizeof(jsval_t) * nargs); 216 223 } 217 224 218 225 extern UT_array *global_scope_stack; 219 226 utarray_new(coro->scope_stack, &jsoff_icd); 227 + 228 + if (!coro->scope_stack) { 229 + mco_destroy(mco); CORO_FREE(coro); CORO_FREE(ctx); 230 + return js_mkerr(js, "out of memory for coroutine scope stack"); 231 + } 232 + 220 233 jsoff_t glob_off = (jsoff_t)vdata(js_glob(js)); 221 234 utarray_push_back(coro->scope_stack, &glob_off); 222 235