diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 218f719..43276d4 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -61,11 +61,11 @@ jobs: - { os: ubuntu-latest, arch: x86, runTest262: true } - { os: ubuntu-latest, arch: riscv64 } - - { os: macos-14, configType: Debug } - - { os: macos-14, configType: Release } - - { os: macos-14, configType: examples } - - { os: macos-14, configType: shared } - - { os: macos-14, configType: asan+ubsan, runTest262: true } + - { os: macos-15, configType: Debug } + - { os: macos-15, configType: Release } + - { os: macos-15, configType: examples } + - { os: macos-15, configType: shared } + - { os: macos-15, configType: asan+ubsan, runTest262: true } steps: - uses: actions/checkout@v6 with: diff --git a/api-test.c b/api-test.c index d17c49a..68471f6 100644 --- a/api-test.c +++ b/api-test.c @@ -382,6 +382,91 @@ static void module_serde(void) JS_FreeRuntime(rt); } +static int typed_requests, plain_requests; + +static char *attribute_normalize(JSContext *ctx, const char *base, + const char *name, JSValueConst attrs, void *opaque) +{ + JSValue type = JS_IsUndefined(attrs) ? JS_UNDEFINED : JS_GetPropertyStr(ctx, attrs, "type"); + const char *resolved; + if (JS_IsUndefined(type)) { + plain_requests++; + resolved = "plain"; + } else { + typed_requests++; + resolved = "typed"; + } + JS_FreeValue(ctx, type); + return js_strdup(ctx, resolved); +} + +static JSModuleDef *attribute_loader(JSContext *ctx, const char *name, void *opaque, + JSValueConst attrs) +{ + JSValue type = JS_IsUndefined(attrs) ? JS_UNDEFINED : JS_GetPropertyStr(ctx, attrs, "type"); + if (!JS_IsUndefined(type)) { + const char *value = JS_ToCString(ctx, type); + if (!value) { + JS_FreeValue(ctx, type); + return NULL; + } + bool invalid = !strcmp(value, "unsupported"); + JS_FreeCString(ctx, value); + if (invalid) { + JS_FreeValue(ctx, type); + JS_ThrowTypeError(ctx, "unsupported module type"); + return NULL; + } + } + JS_FreeValue(ctx, type); + const char *code = "export default 1;"; + JSValue value = JS_Eval(ctx, code, strlen(code), name, + JS_EVAL_TYPE_MODULE | JS_EVAL_FLAG_COMPILE_ONLY); + assert(!JS_IsException(value)); + JSModuleDef *module = JS_VALUE_GET_PTR(value); + JS_FreeValue(ctx, value); + return module; +} + +static void distinct_import_requests(void) +{ + for (int order = 0; order < 2; order++) { + JSRuntime *rt = JS_NewRuntime(); + JS_SetModuleLoaderFunc2(rt, NULL, attribute_loader, NULL, NULL); + JS_SetModuleNormalizeFunc2(rt, attribute_normalize); + JSContext *ctx = JS_NewContext(rt); + const char *code = order == 0 + ? "import a from 'asset' with {type:'image'}; import b from 'asset';" + : "import a from 'asset'; import b from 'asset' with {type:'image'};"; + typed_requests = plain_requests = 0; + JSValue mod = JS_Eval(ctx, code, strlen(code), "entry", + JS_EVAL_TYPE_MODULE | JS_EVAL_FLAG_COMPILE_ONLY); + assert(!JS_IsException(mod)); + assert(typed_requests == 1 && plain_requests == 1); + JS_FreeValue(ctx, mod); + JS_FreeContext(ctx); + JS_FreeRuntime(rt); + } + + /* A legacy normalizer cannot key modules by attributes. Keep its existing + resolution behavior, including rejection of unsupported types. */ + for (int null_normalizer = 0; null_normalizer < 2; null_normalizer++) { + JSRuntime *rt = JS_NewRuntime(); + JS_SetModuleLoaderFunc2(rt, NULL, attribute_loader, NULL, NULL); + if (null_normalizer) + JS_SetModuleNormalizeFunc2(rt, NULL); + JSContext *ctx = JS_NewContext(rt); + const char *code = "import a from 'asset'; import b from 'asset' with {type:'unsupported'};"; + JSValue mod = JS_Eval(ctx, code, strlen(code), "entry", + JS_EVAL_TYPE_MODULE | JS_EVAL_FLAG_COMPILE_ONLY); + assert(JS_IsException(mod)); + JSValue error = JS_GetException(ctx); + JS_FreeValue(ctx, error); + JS_FreeContext(ctx); + JS_FreeRuntime(rt); + } +} + static void runtime_cstring_free(void) { JSRuntime *rt = JS_NewRuntime(); @@ -1027,6 +1112,7 @@ int main(void) raw_context_global_var(); is_array(); module_serde(); + distinct_import_requests(); runtime_cstring_free(); utf16_string(); weak_map_gc_check(); diff --git a/quickjs.c b/quickjs.c index cc0c731..63fb968 100644 --- a/quickjs.c +++ b/quickjs.c @@ -29120,11 +29120,14 @@ static int add_req_module_entry(JSContext *ctx, JSModuleDef *m, JSReqModuleEntry *rme; int i; - /* no need to add the module request if it is already present */ - for(i = 0; i < m->req_module_entries_count; i++) { - rme = &m->req_module_entries[i]; - if (rme->module_name == module_name) - return i; + /* Attribute-aware hosts must resolve each request with its own attributes. + Legacy normalizers cannot distinguish them, so retain their behavior. */ + if (!ctx->rt->module_normalize_has_attr || !ctx->rt->normalize_u.module_normalize_func2) { + for(i = 0; i < m->req_module_entries_count; i++) { + rme = &m->req_module_entries[i]; + if (rme->module_name == module_name) + return i; + } } if (js_resize_array(ctx, (void **)&m->req_module_entries, @@ -29136,7 +29139,7 @@ static int add_req_module_entry(JSContext *ctx, JSModuleDef *m, rme->module_name = JS_DupAtom(ctx, module_name); rme->module = NULL; rme->attributes = JS_UNDEFINED; - return i; + return m->req_module_entries_count - 1; } #endif // QJS_DISABLE_PARSER