-
-
Notifications
You must be signed in to change notification settings - Fork 31.9k
module: addBuiltinLibsToObject refactoring #14085
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -83,43 +83,50 @@ const builtinLibs = [ | |
'string_decoder', 'tls', 'tty', 'url', 'util', 'v8', 'vm', 'zlib' | ||
]; | ||
|
||
// Make built-in modules available directly (loaded lazily). | ||
function addBuiltinLibsToObject(object) { | ||
// Make built-in modules available directly (loaded lazily). | ||
builtinLibs.forEach((name) => { | ||
const configurable = true; | ||
const enumerable = false; | ||
|
||
// Setter function that will be bound to some lib name | ||
function setReal(libname, value) { | ||
// Deleting the property before re-assigning it disables the | ||
// getter/setter mechanism. | ||
delete object[libname]; | ||
object[libname] = value; | ||
} | ||
|
||
for (var n = 0, len = builtinLibs.length; n < len; n++) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. No need to cache the length, V8 can work fine with using |
||
// Goals of this mechanism are: | ||
// - Lazy loading of built-in modules | ||
// - Having all built-in modules available as non-enumerable properties | ||
// - Allowing the user to re-assign these variables as if there were no | ||
// pre-existing globals with the same name. | ||
const libname = builtinLibs[n]; | ||
const set = setReal.bind(null, libname); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why |
||
|
||
const setReal = (val) => { | ||
// Deleting the property before re-assigning it disables the | ||
// getter/setter mechanism. | ||
delete object[name]; | ||
object[name] = val; | ||
}; | ||
|
||
Object.defineProperty(object, name, { | ||
Object.defineProperty(object, libname, { | ||
get: () => { | ||
const lib = require(name); | ||
const lib = require(libname); | ||
|
||
// Disable the current getter/setter and set up a new | ||
// non-enumerable property. | ||
delete object[name]; | ||
Object.defineProperty(object, name, { | ||
// Disable the current getter/setter | ||
delete object[libname]; | ||
|
||
// Set up a new non-emumerable propery | ||
Object.defineProperty(object, libname, { | ||
get: () => lib, | ||
set: setReal, | ||
configurable: true, | ||
enumerable: false | ||
set, | ||
configurable, | ||
enumerable | ||
}); | ||
|
||
return lib; | ||
}, | ||
set: setReal, | ||
configurable: true, | ||
enumerable: false | ||
set, | ||
configurable, | ||
enumerable | ||
}); | ||
}); | ||
} | ||
} | ||
|
||
module.exports = exports = { | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't think these are necessary. I would just keep these values inline to ensure no variable lookup has to be done by V8.