From 5bedf4db0a3a8cee3cc97a9a8daf4fbf7d6082e1 Mon Sep 17 00:00:00 2001 From: Joyee Cheung Date: Sat, 25 Apr 2020 05:21:53 +0800 Subject: [PATCH 1/2] lib: make the builtInObjects set deterministic during bootstrap Make this list explicit instead of filtering over globalThis so that this list is deterministic during the bootstrap regardless of: a) Wether this file is required when the context is going to be serialized, or what the V8 flags are. V8 does not add SharedArrayBuffer, Atomics, WebAssembly, and other flaggable globals e.g. harmony ones to a context that will be serialized. b) Wether this file is required before or after bootstrap (Node.js adds things like Buffer, URL, etc.) --- lib/internal/bootstrap/pre_execution.js | 8 ++++ lib/internal/util/inspect.js | 58 +++++++++++++++++++++++-- 2 files changed, 62 insertions(+), 4 deletions(-) diff --git a/lib/internal/bootstrap/pre_execution.js b/lib/internal/bootstrap/pre_execution.js index 3d5e0061daa8d1..a7bc1eb0381cf2 100644 --- a/lib/internal/bootstrap/pre_execution.js +++ b/lib/internal/bootstrap/pre_execution.js @@ -3,6 +3,7 @@ const { Map, ObjectDefineProperty, + ObjectGetOwnPropertyNames, SafeWeakMap, } = primordials; @@ -178,6 +179,13 @@ function setupDebugEnv() { if (getOptionValue('--expose-internals')) { require('internal/bootstrap/loaders').NativeModule.exposeInternals(); } + + const { builtInObjects } = require('internal/util/inspect'); + // The content of this list depends on e.g. v8 flags, as well as + // what gets added by Node.js to the global object. + for (const key of ObjectGetOwnPropertyNames(globalThis)) { + builtInObjects.add(key); + } } // This has to be called after initializeReport() is called diff --git a/lib/internal/util/inspect.js b/lib/internal/util/inspect.js index fe21854e16aa7f..f2dccdd12747c2 100644 --- a/lib/internal/util/inspect.js +++ b/lib/internal/util/inspect.js @@ -130,9 +130,58 @@ const typedArraySizeGetter = uncurryThis( let hexSlice; -const builtInObjects = new Set( - ObjectGetOwnPropertyNames(global).filter((e) => /^[A-Z][a-zA-Z0-9]+$/.test(e)) -); +// Make this list explicit instead of filtering over globalThis so that +// this list is deterministic during the bootstrap regardless of: +// a) Wether this file is required when the context is going to be +// serialized, or what the V8 flags are. V8 does not add +// SharedArrayBuffer, Atomics, WebAssembly, and other flaggable +// globals e.g. harmony ones to a context that will be serialized. +// b) Wether this file is required before or after bootstrap +// (Node.js adds things like Buffer, URL, etc.) +const builtInObjects = new Set([ + 'Object', + 'Function', + 'Array', + 'Number', + 'Infinity', + 'NaN', + 'Boolean', + 'String', + 'Symbol', + 'Date', + 'Promise', + 'RegExp', + 'Error', + 'EvalError', + 'RangeError', + 'ReferenceError', + 'SyntaxError', + 'TypeError', + 'URIError', + 'JSON', + 'Math', + 'Intl', + 'ArrayBuffer', + 'Uint8Array', + 'Int8Array', + 'Uint16Array', + 'Int16Array', + 'Uint32Array', + 'Int32Array', + 'Float32Array', + 'Float64Array', + 'Uint8ClampedArray', + 'BigUint64Array', + 'BigInt64Array', + 'DataView', + 'Map', + 'BigInt', + 'Set', + 'WeakMap', + 'WeakSet', + 'Proxy', + 'Reflect' +]); // https://tc39.es/ecma262/#sec-IsHTMLDDA-internal-slot const isUndetectableObject = (v) => typeof v === 'undefined' && v !== undefined; @@ -2033,5 +2082,6 @@ module.exports = { formatWithOptions, getStringWidth, inspectDefaultOptions, - stripVTControlCharacters + stripVTControlCharacters, + builtInObjects, }; From 5f01a4f52e1cda83c19864b0c6cbfc00b120fc83 Mon Sep 17 00:00:00 2001 From: Joyee Cheung Date: Thu, 30 Apr 2020 10:22:31 +0800 Subject: [PATCH 2/2] fixup! lib: make the builtInObjects set deterministic during bootstrap --- lib/internal/bootstrap/pre_execution.js | 2 +- lib/internal/util/inspect.js | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/lib/internal/bootstrap/pre_execution.js b/lib/internal/bootstrap/pre_execution.js index a7bc1eb0381cf2..299e26247ca25e 100644 --- a/lib/internal/bootstrap/pre_execution.js +++ b/lib/internal/bootstrap/pre_execution.js @@ -181,7 +181,7 @@ function setupDebugEnv() { } const { builtInObjects } = require('internal/util/inspect'); - // The content of this list depends on e.g. v8 flags, as well as + // The content of this list depends on e.g. V8 flags, as well as // what gets added by Node.js to the global object. for (const key of ObjectGetOwnPropertyNames(globalThis)) { builtInObjects.add(key); diff --git a/lib/internal/util/inspect.js b/lib/internal/util/inspect.js index f2dccdd12747c2..ed976dc7aea1d9 100644 --- a/lib/internal/util/inspect.js +++ b/lib/internal/util/inspect.js @@ -132,11 +132,11 @@ let hexSlice; // Make this list explicit instead of filtering over globalThis so that // this list is deterministic during the bootstrap regardless of: -// a) Wether this file is required when the context is going to be +// a) Whether this file is required when the context is going to be // serialized, or what the V8 flags are. V8 does not add // SharedArrayBuffer, Atomics, WebAssembly, and other flaggable // globals e.g. harmony ones to a context that will be serialized. -// b) Wether this file is required before or after bootstrap +// b) Whether this file is required before or after bootstrap // (Node.js adds things like Buffer, URL, etc.) const builtInObjects = new Set([ 'Object',