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.

improve dryness of loader.c

+78 -73
+78 -73
src/esm/loader.c
··· 133 133 return esm_try_resolve_with_exts(dir, idx, false); 134 134 } 135 135 136 + static ant_value_t esm_default_export_or_namespace(ant_t *js, ant_value_t ns) { 137 + ant_value_t default_val = js_get_slot(js, ns, SLOT_DEFAULT); 138 + return vtype(default_val) != T_UNDEF ? default_val : ns; 139 + } 140 + 141 + static ant_value_t esm_complete_value_module(esm_module_t *mod, ant_value_t value) { 142 + if (is_err(value)) { 143 + mod->is_loading = false; 144 + return value; 145 + } 146 + mod->namespace_obj = value; 147 + mod->default_export = value; 148 + mod->is_loaded = true; 149 + mod->is_loading = false; 150 + return value; 151 + } 152 + 153 + static ant_value_t esm_complete_namespace_module(ant_t *js, esm_module_t *mod, ant_value_t ns) { 154 + mod->namespace_obj = ns; 155 + mod->default_export = esm_default_export_or_namespace(js, ns); 156 + mod->is_loaded = true; 157 + mod->is_loading = false; 158 + return ns; 159 + } 160 + 161 + static ant_value_t esm_prepare_eval_ctx( 162 + ant_t *js, 163 + const char *resolved_path, 164 + ant_value_t ns, 165 + ant_module_format_t format, 166 + bool is_main, 167 + const char *fallback_parent_path, 168 + ant_module_t *out_ctx 169 + ) { 170 + ant_module_t *parent_ctx = js->module; 171 + ant_value_t import_meta = js_create_import_meta(js, resolved_path, is_main); 172 + if (is_err(import_meta)) return import_meta; 173 + 174 + *out_ctx = (ant_module_t){ 175 + .module_ns = ns, 176 + .import_meta = import_meta, 177 + .prev_import_meta_prop = js_mkundef(), 178 + .filename = resolved_path, 179 + .parent_path = parent_ctx ? parent_ctx->filename : fallback_parent_path, 180 + .format = format, 181 + .prev = NULL, 182 + }; 183 + return js_mkundef(); 184 + } 185 + 136 186 static char *esm_try_resolve_from_extension_list( 137 187 const char *dir, 138 188 const char *spec, ··· 713 763 size_t js_len, ant_value_t ns 714 764 ) { 715 765 ant_module_format_t format = esm_decide_module_format(resolved_path); 716 - ant_module_t *parent_ctx = js->module; 717 - ant_value_t import_meta = js_create_import_meta(js, resolved_path, parent_ctx == NULL); 718 - if (is_err(import_meta)) return import_meta; 719 - 720 - ant_module_t eval_ctx = { 721 - .module_ns = ns, 722 - .import_meta = import_meta, 723 - .prev_import_meta_prop = js_mkundef(), 724 - .filename = resolved_path, 725 - .parent_path = parent_ctx ? parent_ctx->filename : NULL, 726 - .format = format, 727 - .prev = NULL, 728 - }; 729 - 766 + ant_module_t eval_ctx; 767 + 768 + ant_value_t prep_res = esm_prepare_eval_ctx( 769 + js, resolved_path, ns, format, 770 + js->module == NULL, NULL, &eval_ctx 771 + ); 772 + 773 + if (is_err(prep_res)) return prep_res; 730 774 js_module_eval_ctx_push(js, &eval_ctx); 775 + 731 776 ant_value_t result = esm_eval_module_with_format( 732 - js, resolved_path, js_code, js_len, ns, format 777 + js, resolved_path, js_code, 778 + js_len, ns, format 733 779 ); 734 780 735 781 js_module_eval_ctx_pop(js, &eval_ctx); ··· 926 972 switch (mod->kind) { 927 973 case ESM_MODULE_KIND_JSON: { 928 974 ant_value_t json_val = esm_load_json(js, mod->resolved_path); 929 - if (is_err(json_val)) { 930 - mod->is_loading = false; 931 - return json_val; 932 - } 933 - mod->namespace_obj = json_val; 934 - mod->default_export = json_val; 935 - mod->is_loaded = true; 936 - mod->is_loading = false; 937 - return json_val; 975 + return esm_complete_value_module(mod, json_val); 938 976 } 939 977 case ESM_MODULE_KIND_TEXT: { 940 978 ant_value_t text_val = esm_load_text(js, mod->resolved_path); 941 - if (is_err(text_val)) { 942 - mod->is_loading = false; 943 - return text_val; 944 - } 945 - mod->namespace_obj = text_val; 946 - mod->default_export = text_val; 947 - mod->is_loaded = true; 948 - mod->is_loading = false; 949 - return text_val; 979 + return esm_complete_value_module(mod, text_val); 950 980 } 951 981 case ESM_MODULE_KIND_IMAGE: { 952 982 ant_value_t img_val = esm_load_image(js, mod->resolved_path); 953 - if (is_err(img_val)) { 954 - mod->is_loading = false; 955 - return img_val; 956 - } 957 - mod->namespace_obj = img_val; 958 - mod->default_export = img_val; 959 - mod->is_loaded = true; 960 - mod->is_loading = false; 961 - return img_val; 983 + return esm_complete_value_module(mod, img_val); 962 984 } 963 985 case ESM_MODULE_KIND_NATIVE: { 964 986 ant_value_t ns = js_mkobj(js); ··· 969 991 mod->is_loading = false; 970 992 return native_exports; 971 993 } 972 - 973 - ant_value_t default_val = js_get_slot(js, ns, SLOT_DEFAULT); 974 - mod->default_export = vtype(default_val) != T_UNDEF ? default_val : ns; 975 - mod->is_loaded = true; 976 - mod->is_loading = false; 977 - return ns; 994 + return esm_complete_namespace_module(js, mod, ns); 978 995 } 979 996 case ESM_MODULE_KIND_CODE: 980 - case ESM_MODULE_KIND_URL: 981 - break; 997 + case ESM_MODULE_KIND_URL: break; 982 998 } 983 999 984 1000 char *content = NULL; ··· 1042 1058 mod->namespace_obj = ns; 1043 1059 1044 1060 const char *prev_filename = js->filename; 1045 - ant_module_t *parent_ctx = js->module; 1046 - 1047 - ant_value_t import_meta = js_create_import_meta(js, mod->resolved_path, false); 1048 - if (is_err(import_meta)) { 1061 + ant_module_t eval_ctx; 1062 + 1063 + ant_value_t prep_res = esm_prepare_eval_ctx( 1064 + js, mod->resolved_path, 1065 + ns, mod->format, 1066 + false, prev_filename, 1067 + &eval_ctx 1068 + ); 1069 + 1070 + if (is_err(prep_res)) { 1049 1071 free(content); 1050 1072 mod->is_loading = false; 1051 - return import_meta; 1073 + return prep_res; 1052 1074 } 1053 1075 1054 - ant_module_t eval_ctx = { 1055 - .module_ns = ns, 1056 - .import_meta = import_meta, 1057 - .prev_import_meta_prop = js_mkundef(), 1058 - .filename = mod->resolved_path, 1059 - .parent_path = parent_ctx ? parent_ctx->filename : prev_filename, 1060 - .format = mod->format, 1061 - .prev = NULL, 1062 - }; 1063 - 1064 1076 js_set_filename(js, mod->resolved_path); 1065 1077 js_module_eval_ctx_push(js, &eval_ctx); 1066 1078 ··· 1083 1095 mod->is_loading = false; 1084 1096 return result; 1085 1097 } 1086 - 1087 - ant_value_t default_val = js_get_slot(js, ns, SLOT_DEFAULT); 1088 - mod->default_export = vtype(default_val) != T_UNDEF ? default_val : ns; 1089 - 1090 - mod->is_loaded = true; 1091 - mod->is_loading = false; 1092 - 1093 - return ns; 1098 + return esm_complete_namespace_module(js, mod, ns); 1094 1099 } 1095 1100 1096 1101 static ant_value_t esm_get_or_load(ant_t *js, const char *specifier, const char *resolved_path) {