mirror of
https://github.com/nodejs/node.git
synced 2024-12-01 16:10:02 +01:00
28dc848e70
Using a more "direct" method of function calling yields better performance. PR-URL: https://github.com/nodejs/node/pull/10789 Reviewed-By: James M Snell <jasnell@gmail.com> Reviewed-By: Michaël Zasso <targos@protonmail.com> Reviewed-By: Michael Dawson <michael_dawson@ca.ibm.com> Reviewed-By: Matteo Collina <matteo.collina@gmail.com> Reviewed-By: Benjamin Gruenbaum <benjamingr@gmail.com>
98 lines
2.5 KiB
JavaScript
98 lines
2.5 KiB
JavaScript
'use strict';
|
|
|
|
exports = module.exports = {
|
|
makeRequireFunction,
|
|
stripBOM,
|
|
addBuiltinLibsToObject
|
|
};
|
|
|
|
exports.requireDepth = 0;
|
|
|
|
// Invoke with makeRequireFunction(module) where |module| is the Module object
|
|
// to use as the context for the require() function.
|
|
function makeRequireFunction(mod) {
|
|
const Module = mod.constructor;
|
|
|
|
function require(path) {
|
|
try {
|
|
exports.requireDepth += 1;
|
|
return mod.require(path);
|
|
} finally {
|
|
exports.requireDepth -= 1;
|
|
}
|
|
}
|
|
|
|
function resolve(request) {
|
|
return Module._resolveFilename(request, mod);
|
|
}
|
|
|
|
require.resolve = resolve;
|
|
|
|
require.main = process.mainModule;
|
|
|
|
// Enable support to add extra extension types.
|
|
require.extensions = Module._extensions;
|
|
|
|
require.cache = Module._cache;
|
|
|
|
return require;
|
|
}
|
|
|
|
/**
|
|
* Remove byte order marker. This catches EF BB BF (the UTF-8 BOM)
|
|
* because the buffer-to-string conversion in `fs.readFileSync()`
|
|
* translates it to FEFF, the UTF-16 BOM.
|
|
*/
|
|
function stripBOM(content) {
|
|
if (content.charCodeAt(0) === 0xFEFF) {
|
|
content = content.slice(1);
|
|
}
|
|
return content;
|
|
}
|
|
|
|
exports.builtinLibs = [
|
|
'assert', 'buffer', 'child_process', 'cluster', 'crypto', 'dgram', 'dns',
|
|
'domain', 'events', 'fs', 'http', 'https', 'net', 'os', 'path', 'punycode',
|
|
'querystring', 'readline', 'repl', 'stream', 'string_decoder', 'tls', 'tty',
|
|
'url', 'util', 'v8', 'vm', 'zlib'
|
|
];
|
|
|
|
function addBuiltinLibsToObject(object) {
|
|
// Make built-in modules available directly (loaded lazily).
|
|
exports.builtinLibs.forEach((name) => {
|
|
// 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 setReal = (val) => {
|
|
// Deleting the property before re-assigning it disables the
|
|
// getter/setter mechanism.
|
|
delete object[name];
|
|
object[name] = val;
|
|
};
|
|
|
|
Object.defineProperty(object, name, {
|
|
get: () => {
|
|
const lib = require(name);
|
|
|
|
// Disable the current getter/setter and set up a new
|
|
// non-enumerable property.
|
|
delete object[name];
|
|
Object.defineProperty(object, name, {
|
|
get: () => lib,
|
|
set: setReal,
|
|
configurable: true,
|
|
enumerable: false
|
|
});
|
|
|
|
return lib;
|
|
},
|
|
set: setReal,
|
|
configurable: true,
|
|
enumerable: false
|
|
});
|
|
});
|
|
}
|