Skip to content

Commit 0d947ec

Browse files
committed
esm: internalModuleReadJSON returns an array
Refs: #30674
1 parent e5f3182 commit 0d947ec

File tree

3 files changed

+34
-34
lines changed

3 files changed

+34
-34
lines changed

lib/internal/modules/package_json_reader.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,8 @@ function read(path) {
1414
return cache.get(path);
1515
}
1616

17-
const result = internalModuleReadJSON(path);
17+
const [string, containsKeys] = internalModuleReadJSON(path);
18+
const result = { string, containsKeys };
1819
cache.set(path, result);
1920
return result;
2021
}

src/node_file.cc

Lines changed: 14 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -832,27 +832,25 @@ void Close(const FunctionCallbackInfo<Value>& args) {
832832
}
833833

834834

835-
// Used to speed up module loading. Returns object
836-
// {string: string | undefined, containsKeys: undefined | boolean}
835+
// Used to speed up module loading. Returns an array [string, boolean]
837836
static void InternalModuleReadJSON(const FunctionCallbackInfo<Value>& args) {
838837
Environment* env = Environment::GetCurrent(args);
839838
Isolate* isolate = env->isolate();
840839
uv_loop_t* loop = env->event_loop();
841-
Local<Object> return_value = Object::New(isolate);
842840

843841
CHECK(args[0]->IsString());
844842
node::Utf8Value path(isolate, args[0]);
845843

846844
if (strlen(*path) != path.length()) {
847-
args.GetReturnValue().Set(return_value);
845+
args.GetReturnValue().Set(Array::New(isolate));
848846
return; // Contains a nul byte.
849847
}
850848
uv_fs_t open_req;
851849
const int fd = uv_fs_open(loop, &open_req, *path, O_RDONLY, 0, nullptr);
852850
uv_fs_req_cleanup(&open_req);
853851

854852
if (fd < 0) {
855-
args.GetReturnValue().Set(return_value);
853+
args.GetReturnValue().Set(Array::New(isolate));
856854
return;
857855
}
858856

@@ -879,7 +877,7 @@ static void InternalModuleReadJSON(const FunctionCallbackInfo<Value>& args) {
879877
uv_fs_req_cleanup(&read_req);
880878

881879
if (numchars < 0) {
882-
args.GetReturnValue().Set(return_value);
880+
args.GetReturnValue().Set(Array::New(isolate));
883881
return;
884882
}
885883
offset += numchars;
@@ -916,19 +914,17 @@ static void InternalModuleReadJSON(const FunctionCallbackInfo<Value>& args) {
916914
if (0 == memcmp(s, "exports", 7)) break;
917915
}
918916
}
919-
return_value->Set(
920-
isolate->GetCurrentContext(),
921-
FIXED_ONE_BYTE_STRING(isolate, "string"),
917+
918+
919+
Local<Value> return_value[] = {
922920
String::NewFromUtf8(isolate,
923-
&chars[start],
924-
v8::NewStringType::kNormal,
925-
size).ToLocalChecked()).Check();
926-
927-
return_value->Set(
928-
isolate->GetCurrentContext(),
929-
FIXED_ONE_BYTE_STRING(isolate, "containsKeys"),
930-
Boolean::New(isolate, p < pe ? true : false)).Check();
931-
args.GetReturnValue().Set(return_value);
921+
&chars[start],
922+
v8::NewStringType::kNormal,
923+
size).ToLocalChecked(),
924+
Boolean::New(isolate, p < pe ? true : false)
925+
};
926+
args.GetReturnValue().Set(
927+
Array::New(isolate, return_value, arraysize(return_value)));
932928
}
933929

934930
// Used to speed up module loading. Returns 0 if the path refers to

test/parallel/test-module-binding.js

Lines changed: 18 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -7,28 +7,31 @@ const { internalModuleReadJSON } = internalBinding('fs');
77
const { readFileSync } = require('fs');
88
const { strictEqual } = require('assert');
99
{
10-
const result = internalModuleReadJSON('nosuchfile');
11-
strictEqual(result.string, undefined);
12-
strictEqual(result.containsKeys, undefined);
10+
const [string, containsKeys] = internalModuleReadJSON('nosuchfile');
11+
strictEqual(string, undefined);
12+
strictEqual(containsKeys, undefined);
1313
}
1414
{
15-
const result = internalModuleReadJSON(fixtures.path('empty.txt'));
16-
strictEqual(result.string, '');
17-
strictEqual(result.containsKeys, false);
15+
const [string, containsKeys] =
16+
internalModuleReadJSON(fixtures.path('empty.txt'));
17+
strictEqual(string, '');
18+
strictEqual(containsKeys, false);
1819
}
1920
{
20-
const result = internalModuleReadJSON(fixtures.path('empty.txt'));
21-
strictEqual(result.string, '');
22-
strictEqual(result.containsKeys, false);
21+
const [string, containsKeys] =
22+
internalModuleReadJSON(fixtures.path('empty.txt'));
23+
strictEqual(string, '');
24+
strictEqual(containsKeys, false);
2325
}
2426
{
25-
const result = internalModuleReadJSON(fixtures.path('empty-with-bom.txt'));
26-
strictEqual(result.string, '');
27-
strictEqual(result.containsKeys, false);
27+
const [string, containsKeys] =
28+
internalModuleReadJSON(fixtures.path('empty-with-bom.txt'));
29+
strictEqual(string, '');
30+
strictEqual(containsKeys, false);
2831
}
2932
{
3033
const filename = fixtures.path('require-bin/package.json');
31-
const result = internalModuleReadJSON(filename);
32-
strictEqual(result.string, readFileSync(filename, 'utf8'));
33-
strictEqual(result.containsKeys, true);
34+
const [string, containsKeys] = internalModuleReadJSON(filename);
35+
strictEqual(string, readFileSync(filename, 'utf8'));
36+
strictEqual(containsKeys, true);
3437
}

0 commit comments

Comments
 (0)