Skip to content

Commit

Permalink
src: iterate on import attributes array correctly
Browse files Browse the repository at this point in the history
The array's length is supposed to be a multiple of two for dynamic
import callbacks.

Fixes: #50700
PR-URL: #50703
Backport-PR-URL: #51136
Reviewed-By: Antoine du Hamel <duhamelantoine1995@gmail.com>
Reviewed-By: Shelley Vohr <shelley.vohr@gmail.com>
Reviewed-By: Joyee Cheung <joyeec9h3@gmail.com>
Reviewed-By: James M Snell <jasnell@gmail.com>
  • Loading branch information
targos authored and richardlau committed Mar 18, 2024
1 parent 06646e1 commit 96514a8
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 4 deletions.
12 changes: 8 additions & 4 deletions src/module_wrap.cc
Expand Up @@ -250,10 +250,14 @@ void ModuleWrap::New(const FunctionCallbackInfo<Value>& args) {
}

static Local<Object> createImportAttributesContainer(
Environment* env, Isolate* isolate, Local<FixedArray> raw_attributes) {
Environment* env,
Isolate* isolate,
Local<FixedArray> raw_attributes,
const int elements_per_attribute) {
CHECK_EQ(raw_attributes->Length() % elements_per_attribute, 0);
Local<Object> 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<String>(),
Expand Down Expand Up @@ -299,7 +303,7 @@ void ModuleWrap::Link(const FunctionCallbackInfo<Value>& args) {

Local<FixedArray> raw_attributes = module_request->GetImportAssertions();
Local<Object> attributes =
createImportAttributesContainer(env, isolate, raw_attributes);
createImportAttributesContainer(env, isolate, raw_attributes, 3);

Local<Value> argv[] = {
specifier,
Expand Down Expand Up @@ -583,7 +587,7 @@ static MaybeLocal<Promise> ImportModuleDynamically(
options->Get(context, HostDefinedOptions::kID).As<Symbol>();

Local<Object> attributes =
createImportAttributesContainer(env, isolate, import_attributes);
createImportAttributesContainer(env, isolate, import_attributes, 2);

Local<Value> import_args[] = {
id,
Expand Down
5 changes: 5 additions & 0 deletions test/es-module/test-esm-import-attributes-errors.js
Expand Up @@ -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' }
Expand Down
5 changes: 5 additions & 0 deletions test/es-module/test-esm-import-attributes-errors.mjs
Expand Up @@ -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' }
Expand Down

0 comments on commit 96514a8

Please sign in to comment.