Created
February 15, 2018 04:40
-
-
Save devsnek/76aab1dcd169c96b952fbe8c74404475 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
diff --git a/lib/internal/bootstrap_node.js b/lib/internal/bootstrap_node.js | |
index 0c2109fab3..f882145434 100644 | |
--- a/lib/internal/bootstrap_node.js | |
+++ b/lib/internal/bootstrap_node.js | |
@@ -8,6 +8,7 @@ | |
'use strict'; | |
(function(process) { | |
+ const primordials = makePrimordials(); | |
let internalBinding; | |
const exceptionHandlerState = { captureFn: null }; | |
@@ -594,6 +595,8 @@ | |
NativeModule.require = function(id) { | |
if (id === 'native_module') { | |
return NativeModule; | |
+ } else if (id === 'primordials') { | |
+ return primordials; | |
} | |
const cached = NativeModule.getCached(id); | |
@@ -636,7 +639,7 @@ | |
}; | |
NativeModule.exists = function(id) { | |
- return NativeModule._source.hasOwnProperty(id); | |
+ return id === 'primordials' || NativeModule._source.hasOwnProperty(id); | |
}; | |
if (config.exposeInternals) { | |
@@ -697,4 +700,47 @@ | |
}; | |
startup(); | |
+ | |
+ function makePrimordials() { | |
+ const TARGETS = Object.getOwnPropertyNames(global) | |
+ .filter((n) => !/DTRACE|global|console/.test(n) && | |
+ ['object', 'function'].includes(typeof global[n])) | |
+ .map((n) => [n, global[n]]); | |
+ | |
+ const ReflectApply = Reflect.apply; | |
+ function uncurryThis(fn) { | |
+ return (thisArg, ...args) => ReflectApply(fn, thisArg, args); | |
+ } | |
+ | |
+ const primordials = {}; | |
+ | |
+ const exportName = (Tname, name) => `${Tname}${name[0].toUpperCase()}${name.slice(1)}`; | |
+ | |
+ for (const [targetName, Target] of TARGETS) { | |
+ const topNames = Object.getOwnPropertyNames(Target) | |
+ .filter((n) => filter(Target, n)); | |
+ | |
+ let prototypeNames = Target.prototype !== undefined ? | |
+ Object.getOwnPropertyNames(Target.prototype) | |
+ .filter((n) => filter(Target.prototype, n)) : []; | |
+ | |
+ for (const name of topNames) | |
+ primordials[exportName(targetName, name)] = Target[name]; | |
+ | |
+ for (const name of prototypeNames) | |
+ primordials[exportName(targetName, name)] = uncurryThis(Target.prototype[name]); | |
+ } | |
+ | |
+ function filter(T, name) { | |
+ // some things like Function.arguments will throw on access | |
+ try { | |
+ return typeof T[name] === 'function'; | |
+ } catch (err) { | |
+ return false; | |
+ } | |
+ } | |
+ | |
+ Object.freeze(primordials); | |
+ return primordials; | |
+ } | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment