diff --git a/src/module_wrap.cc b/src/module_wrap.cc index cf9425f4786144..eae79b0fc765db 100644 --- a/src/module_wrap.cc +++ b/src/module_wrap.cc @@ -250,10 +250,14 @@ void ModuleWrap::New(const FunctionCallbackInfo& args) { } static Local createImportAttributesContainer( - Environment* env, Isolate* isolate, Local raw_attributes) { + Environment* env, + Isolate* isolate, + Local raw_attributes, + const int elements_per_attribute) { + CHECK_EQ(raw_attributes->Length() % elements_per_attribute, 0); Local attributes = Object::New(isolate, v8::Null(env->isolate()), nullptr, nullptr, 0); - for (int i = 0; i < raw_attributes->Length(); i += 3) { + for (int i = 0; i < raw_attributes->Length(); i += elements_per_attribute) { attributes ->Set(env->context(), raw_attributes->Get(env->context(), i).As(), @@ -299,7 +303,7 @@ void ModuleWrap::Link(const FunctionCallbackInfo& args) { Local raw_attributes = module_request->GetImportAssertions(); Local attributes = - createImportAttributesContainer(env, isolate, raw_attributes); + createImportAttributesContainer(env, isolate, raw_attributes, 3); Local argv[] = { specifier, @@ -583,7 +587,7 @@ static MaybeLocal ImportModuleDynamically( options->Get(context, HostDefinedOptions::kID).As(); Local attributes = - createImportAttributesContainer(env, isolate, import_attributes); + createImportAttributesContainer(env, isolate, import_attributes, 2); Local import_args[] = { id, diff --git a/test/es-module/test-esm-import-attributes-errors.js b/test/es-module/test-esm-import-attributes-errors.js index 4521248db176e5..976d4a3f67f778 100644 --- a/test/es-module/test-esm-import-attributes-errors.js +++ b/test/es-module/test-esm-import-attributes-errors.js @@ -26,6 +26,11 @@ async function test() { { code: 'ERR_IMPORT_ASSERTION_TYPE_FAILED' } ); + await rejects( + import(jsModuleDataUrl, { with: { type: 'json', other: 'unsupported' } }), + { code: 'ERR_IMPORT_ASSERTION_TYPE_FAILED' } + ); + await rejects( import(jsModuleDataUrl, { with: { type: 'unsupported' } }), { code: 'ERR_IMPORT_ASSERTION_TYPE_UNSUPPORTED' } diff --git a/test/es-module/test-esm-import-attributes-errors.mjs b/test/es-module/test-esm-import-attributes-errors.mjs index ff932636e39a5f..072b94b1b0fc55 100644 --- a/test/es-module/test-esm-import-attributes-errors.mjs +++ b/test/es-module/test-esm-import-attributes-errors.mjs @@ -21,6 +21,11 @@ await rejects( { code: 'ERR_IMPORT_ASSERTION_TYPE_FAILED' } ); +await rejects( + import(jsModuleDataUrl, { with: { type: 'json', other: 'unsupported' } }), + { code: 'ERR_IMPORT_ASSERTION_TYPE_FAILED' } +); + await rejects( import(import.meta.url, { with: { type: 'unsupported' } }), { code: 'ERR_IMPORT_ASSERTION_TYPE_UNSUPPORTED' }