diff --git a/lib/internal/modules/package_json_reader.js b/lib/internal/modules/package_json_reader.js index 07d61427fe1394..066047b55eb9d8 100644 --- a/lib/internal/modules/package_json_reader.js +++ b/lib/internal/modules/package_json_reader.js @@ -14,7 +14,8 @@ function read(path) { return cache.get(path); } - const result = internalModuleReadJSON(path); + const [string, containsKeys] = internalModuleReadJSON(path); + const result = { string, containsKeys }; cache.set(path, result); return result; } diff --git a/src/node_file.cc b/src/node_file.cc index 740477844a3345..41a5eb608532ca 100644 --- a/src/node_file.cc +++ b/src/node_file.cc @@ -832,19 +832,17 @@ void Close(const FunctionCallbackInfo& args) { } -// Used to speed up module loading. Returns object -// {string: string | undefined, containsKeys: undefined | boolean} +// Used to speed up module loading. Returns an array [string, boolean] static void InternalModuleReadJSON(const FunctionCallbackInfo& args) { Environment* env = Environment::GetCurrent(args); Isolate* isolate = env->isolate(); uv_loop_t* loop = env->event_loop(); - Local return_value = Object::New(isolate); CHECK(args[0]->IsString()); node::Utf8Value path(isolate, args[0]); if (strlen(*path) != path.length()) { - args.GetReturnValue().Set(return_value); + args.GetReturnValue().Set(Array::New(isolate)); return; // Contains a nul byte. } uv_fs_t open_req; @@ -852,7 +850,7 @@ static void InternalModuleReadJSON(const FunctionCallbackInfo& args) { uv_fs_req_cleanup(&open_req); if (fd < 0) { - args.GetReturnValue().Set(return_value); + args.GetReturnValue().Set(Array::New(isolate)); return; } @@ -879,7 +877,7 @@ static void InternalModuleReadJSON(const FunctionCallbackInfo& args) { uv_fs_req_cleanup(&read_req); if (numchars < 0) { - args.GetReturnValue().Set(return_value); + args.GetReturnValue().Set(Array::New(isolate)); return; } offset += numchars; @@ -916,19 +914,17 @@ static void InternalModuleReadJSON(const FunctionCallbackInfo& args) { if (0 == memcmp(s, "exports", 7)) break; } } - return_value->Set( - isolate->GetCurrentContext(), - FIXED_ONE_BYTE_STRING(isolate, "string"), + + + Local return_value[] = { String::NewFromUtf8(isolate, - &chars[start], - v8::NewStringType::kNormal, - size).ToLocalChecked()).Check(); - - return_value->Set( - isolate->GetCurrentContext(), - FIXED_ONE_BYTE_STRING(isolate, "containsKeys"), - Boolean::New(isolate, p < pe ? true : false)).Check(); - args.GetReturnValue().Set(return_value); + &chars[start], + v8::NewStringType::kNormal, + size).ToLocalChecked(), + Boolean::New(isolate, p < pe ? true : false) + }; + args.GetReturnValue().Set( + Array::New(isolate, return_value, arraysize(return_value))); } // Used to speed up module loading. Returns 0 if the path refers to diff --git a/test/parallel/test-module-binding.js b/test/parallel/test-module-binding.js index ad587da460d7d6..a0fa0a038990f0 100644 --- a/test/parallel/test-module-binding.js +++ b/test/parallel/test-module-binding.js @@ -7,28 +7,31 @@ const { internalModuleReadJSON } = internalBinding('fs'); const { readFileSync } = require('fs'); const { strictEqual } = require('assert'); { - const result = internalModuleReadJSON('nosuchfile'); - strictEqual(result.string, undefined); - strictEqual(result.containsKeys, undefined); + const [string, containsKeys] = internalModuleReadJSON('nosuchfile'); + strictEqual(string, undefined); + strictEqual(containsKeys, undefined); } { - const result = internalModuleReadJSON(fixtures.path('empty.txt')); - strictEqual(result.string, ''); - strictEqual(result.containsKeys, false); + const [string, containsKeys] = + internalModuleReadJSON(fixtures.path('empty.txt')); + strictEqual(string, ''); + strictEqual(containsKeys, false); } { - const result = internalModuleReadJSON(fixtures.path('empty.txt')); - strictEqual(result.string, ''); - strictEqual(result.containsKeys, false); + const [string, containsKeys] = + internalModuleReadJSON(fixtures.path('empty.txt')); + strictEqual(string, ''); + strictEqual(containsKeys, false); } { - const result = internalModuleReadJSON(fixtures.path('empty-with-bom.txt')); - strictEqual(result.string, ''); - strictEqual(result.containsKeys, false); + const [string, containsKeys] = + internalModuleReadJSON(fixtures.path('empty-with-bom.txt')); + strictEqual(string, ''); + strictEqual(containsKeys, false); } { const filename = fixtures.path('require-bin/package.json'); - const result = internalModuleReadJSON(filename); - strictEqual(result.string, readFileSync(filename, 'utf8')); - strictEqual(result.containsKeys, true); + const [string, containsKeys] = internalModuleReadJSON(filename); + strictEqual(string, readFileSync(filename, 'utf8')); + strictEqual(containsKeys, true); }