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.

clean up esm_fetch_url and esm_resolve_url functions

+42 -118
+4
include/esm/remote.h
··· 5 5 #include <stddef.h> 6 6 7 7 bool esm_is_url(const char *spec); 8 + 8 9 char *esm_fetch_url(const char *url, size_t *out_len, char **out_error); 9 10 char *esm_resolve_url(const char *specifier, const char *base_url); 11 + 12 + #define FILE_RESOLVER *(*file_resolver)(const char *, const char *) 13 + char *esm_resolve(const char *specifier, const char *base_path, char FILE_RESOLVER); 10 14 11 15 #endif
+33 -118
src/ant.c
··· 889 889 js->flags &= (uint8_t)~F_THROW; 890 890 js->thrown_value = js_mkundef(); 891 891 js_reject_promise(js, ctx->promise, reject_value); 892 - } else { 893 - js_resolve_promise(js, ctx->promise, result); 894 - } 892 + } else js_resolve_promise(js, ctx->promise, result); 895 893 } 896 894 897 895 static void enqueue_coroutine(coroutine_t *coro) { ··· 901 899 902 900 if (pending_coroutines.tail) { 903 901 pending_coroutines.tail->next = coro; 904 - } else { 905 - pending_coroutines.head = coro; 906 - } 902 + } else pending_coroutines.head = coro; 907 903 pending_coroutines.tail = coro; 908 904 } 909 905 ··· 912 908 913 909 if (coro->prev) { 914 910 coro->prev->next = coro->next; 915 - } else { 916 - pending_coroutines.head = coro->next; 917 - } 911 + } else pending_coroutines.head = coro->next; 918 912 919 913 if (coro->next) { 920 914 coro->next->prev = coro->prev; 921 - } else { 922 - pending_coroutines.tail = coro->prev; 923 - } 915 + } else pending_coroutines.tail = coro->prev; 924 916 925 917 coro->prev = NULL; 926 918 coro->next = NULL; ··· 953 945 process_immediates(js); 954 946 process_microtasks(js); 955 947 956 - coroutine_t *temp = pending_coroutines.head; 957 - 958 - while (temp) { 959 - coroutine_t *next = temp->next; 948 + for (coroutine_t *temp = pending_coroutines.head, *next; temp; temp = next) { 949 + next = temp->next; 950 + if (!temp->is_ready || !temp->mco || mco_status(temp->mco) != MCO_SUSPENDED) continue; 960 951 961 - if (temp->is_ready && temp->mco && mco_status(temp->mco) == MCO_SUSPENDED) { 962 - remove_coroutine(temp); 963 - mco_result res = mco_resume(temp->mco); 964 - 965 - if (res == MCO_SUCCESS && mco_status(temp->mco) == MCO_DEAD) { 966 - free_coroutine(temp); 967 - } else if (res == MCO_SUCCESS) { 968 - temp->is_ready = false; 969 - enqueue_coroutine(temp); 970 - } else free_coroutine(temp); 971 - } 952 + remove_coroutine(temp); 953 + mco_result res = mco_resume(temp->mco); 972 954 973 - temp = next; 955 + if (res == MCO_SUCCESS && mco_status(temp->mco) != MCO_DEAD) { 956 + temp->is_ready = false; 957 + enqueue_coroutine(temp); 958 + } else free_coroutine(temp); 974 959 } 975 960 } 976 961 ··· 19270 19255 return ns; 19271 19256 } 19272 19257 19258 + static jsval_t esm_get_or_load(struct js *js, const char *specifier, const char *resolved_path) { 19259 + esm_module_t *mod = esm_find_module(resolved_path); 19260 + if (!mod) { 19261 + mod = esm_create_module(specifier, resolved_path); 19262 + if (!mod) return js_mkerr(js, "Cannot create module"); 19263 + } 19264 + return esm_load_module(js, mod); 19265 + } 19266 + 19273 19267 static jsval_t builtin_import(struct js *js, jsval_t *args, int nargs) { 19274 19268 if (nargs < 1 || vtype(args[0]) != T_STR) { 19275 19269 return js_mkerr(js, "import() requires a string specifier"); ··· 19289 19283 } 19290 19284 19291 19285 const char *base_path = js->filename ? js->filename : "."; 19292 - char *resolved_path = NULL; 19293 - 19294 - if (esm_is_url(specifier) || esm_is_url(base_path)) { 19295 - resolved_path = esm_resolve_url(specifier, base_path); 19296 - } else resolved_path = esm_resolve_path(specifier, base_path); 19297 - 19286 + char *resolved_path = esm_resolve(specifier, base_path, esm_resolve_path); 19298 19287 if (!resolved_path) { 19299 19288 jsval_t err = js_mkerr(js, "Cannot resolve module: %s", specifier); 19300 19289 free(specifier); 19301 19290 return err; 19302 19291 } 19303 19292 19304 - esm_module_t *mod = esm_find_module(resolved_path); 19305 - if (!mod) { 19306 - mod = esm_create_module(specifier, resolved_path); 19307 - if (!mod) { 19308 - free(resolved_path); 19309 - free(specifier); return js_mkerr(js, "Cannot create module"); 19310 - } 19311 - } 19312 - 19313 - jsval_t ns = esm_load_module(js, mod); 19293 + jsval_t ns = esm_get_or_load(js, specifier, resolved_path); 19314 19294 free(resolved_path); 19315 19295 free(specifier); 19316 19296 ··· 19330 19310 char *specifier = strndup((char *)&js->mem[spec_off], spec_len); 19331 19311 19332 19312 const char *base_path = js->filename ? js->filename : "."; 19333 - char *resolved_path = NULL; 19334 - 19335 - if (esm_is_url(specifier) || esm_is_url(base_path)) { 19336 - resolved_path = esm_resolve_url(specifier, base_path); 19337 - } else resolved_path = esm_resolve_path(specifier, base_path); 19338 - 19313 + char *resolved_path = esm_resolve(specifier, base_path, esm_resolve_path); 19339 19314 if (!resolved_path) { 19340 19315 jsval_t err = js_mkerr(js, "Cannot resolve module: %s", specifier); 19341 19316 free(specifier); return err; ··· 19466 19441 ns = lib->init_fn(js); 19467 19442 } else { 19468 19443 const char *base_path = js->filename ? js->filename : "."; 19469 - char *resolved_path = NULL; 19470 - 19471 - if (esm_is_url(spec_str) || esm_is_url(base_path)) { 19472 - resolved_path = esm_resolve_url(spec_str, base_path); 19473 - } else resolved_path = esm_resolve_path(spec_str, base_path); 19474 - 19444 + char *resolved_path = esm_resolve(spec_str, base_path, esm_resolve_path); 19475 19445 if (!resolved_path) { 19476 19446 free(spec_str); 19477 19447 return js_mkerr(js, "Cannot resolve module: %.*s", (int)spec_len, spec_str); 19478 19448 } 19479 19449 19480 - esm_module_t *mod = esm_find_module(resolved_path); 19481 - if (!mod) { 19482 - mod = esm_create_module(spec_str, resolved_path); 19483 - if (!mod) { 19484 - free(resolved_path); 19485 - free(spec_str); return js_mkerr(js, "Cannot create module"); 19486 - } 19487 - } 19488 - 19489 19450 js_parse_state_t saved; 19490 19451 JS_SAVE_STATE(js, saved); 19491 - 19492 - ns = esm_load_module(js, mod); 19452 + ns = esm_get_or_load(js, spec_str, resolved_path); 19493 19453 JS_RESTORE_STATE(js, saved); 19494 19454 19495 19455 free(resolved_path); ··· 19581 19541 ns = lib->init_fn(js); 19582 19542 } else { 19583 19543 const char *base_path = js->filename ? js->filename : "."; 19584 - char *resolved_path = NULL; 19585 - 19586 - if (esm_is_url(spec_str) || esm_is_url(base_path)) { 19587 - resolved_path = esm_resolve_url(spec_str, base_path); 19588 - } else resolved_path = esm_resolve_path(spec_str, base_path); 19589 - 19544 + char *resolved_path = esm_resolve(spec_str, base_path, esm_resolve_path); 19590 19545 if (!resolved_path) { 19591 19546 free(spec_str); 19592 19547 return js_mkerr(js, "Cannot resolve module: %s", spec_str); 19593 19548 } 19594 19549 19595 - esm_module_t *mod = esm_find_module(resolved_path); 19596 - if (!mod) { 19597 - mod = esm_create_module(spec_str, resolved_path); 19598 - if (!mod) { 19599 - free(resolved_path); 19600 - free(spec_str); return js_mkerr(js, "Cannot create module"); 19601 - } 19602 - } 19603 - 19604 19550 js_parse_state_t saved; 19605 19551 JS_SAVE_STATE(js, saved); 19606 - 19607 - ns = esm_load_module(js, mod); 19552 + ns = esm_get_or_load(js, spec_str, resolved_path); 19608 19553 JS_RESTORE_STATE(js, saved); 19609 19554 19610 19555 free(resolved_path); ··· 19693 19638 ns = lib->init_fn(js); 19694 19639 } else { 19695 19640 const char *base_path = js->filename ? js->filename : "."; 19696 - char *resolved_path = NULL; 19697 - 19698 - if (esm_is_url(spec_str) || esm_is_url(base_path)) { 19699 - resolved_path = esm_resolve_url(spec_str, base_path); 19700 - } else resolved_path = esm_resolve_path(spec_str, base_path); 19701 - 19641 + char *resolved_path = esm_resolve(spec_str, base_path, esm_resolve_path); 19702 19642 if (!resolved_path) { 19703 19643 free(spec_str); 19704 19644 return js_mkerr(js, "Cannot resolve module: %s", spec_str); 19705 19645 } 19706 19646 19707 - esm_module_t *mod = esm_find_module(resolved_path); 19708 - if (!mod) { 19709 - mod = esm_create_module(spec_str, resolved_path); 19710 - if (!mod) { 19711 - free(resolved_path); 19712 - free(spec_str); return js_mkerr(js, "Cannot create module"); 19713 - } 19714 - } 19715 - 19716 19647 js_parse_state_t saved; 19717 19648 JS_SAVE_STATE(js, saved); 19718 19649 jsoff_t saved_toff = js->toff, saved_tlen = js->tlen; 19719 19650 jsval_t saved_scope = js->scope; 19720 19651 19721 - ns = esm_load_module(js, mod); 19652 + ns = esm_get_or_load(js, spec_str, resolved_path); 19722 19653 19723 19654 JS_RESTORE_STATE(js, saved); 19724 19655 js->toff = saved_toff; ··· 19853 19784 ns = lib->init_fn(js); 19854 19785 } else { 19855 19786 const char *base_path = js->filename ? js->filename : "."; 19856 - char *resolved_path = NULL; 19857 - 19858 - if (esm_is_url(spec_str) || esm_is_url(base_path)) { 19859 - resolved_path = esm_resolve_url(spec_str, base_path); 19860 - } else resolved_path = esm_resolve_path(spec_str, base_path); 19861 - 19787 + char *resolved_path = esm_resolve(spec_str, base_path, esm_resolve_path); 19862 19788 if (!resolved_path) { 19863 19789 free(spec_str); 19864 19790 return js_mkerr(js, "Cannot resolve module: %s", spec_str); 19865 19791 } 19866 19792 19867 - esm_module_t *mod = esm_find_module(resolved_path); 19868 - if (!mod) { 19869 - mod = esm_create_module(spec_str, resolved_path); 19870 - if (!mod) { 19871 - free(resolved_path); 19872 - free(spec_str); 19873 - return js_mkerr(js, "Cannot create module"); 19874 - } 19875 - } 19876 - 19877 19793 js_parse_state_t saved; 19878 19794 JS_SAVE_STATE(js, saved); 19879 - 19880 - ns = esm_load_module(js, mod); 19795 + ns = esm_get_or_load(js, spec_str, resolved_path); 19881 19796 JS_RESTORE_STATE(js, saved); 19882 19797 19883 19798 free(resolved_path);
+5
src/esm/remote.c
··· 370 370 371 371 return strdup(specifier); 372 372 } 373 + 374 + char *esm_resolve(const char *specifier, const char *base_path, char FILE_RESOLVER) { 375 + if (esm_is_url(specifier) || esm_is_url(base_path)) return esm_resolve_url(specifier, base_path); 376 + return file_resolver(specifier, base_path); 377 + }