summaryrefslogtreecommitdiff
path: root/lib/internal/bootstrap/primordials.js
diff options
context:
space:
mode:
authorMichaël Zasso <targos@protonmail.com>2019-04-05 11:11:26 +0200
committerMichaël Zasso <targos@protonmail.com>2019-04-08 11:23:09 +0200
commit112cc7c27551254aa2b17098fb774867f05ed0d9 (patch)
tree0f48ef3ac9e8949f0bf49af38fb6dc7c6e2f64af /lib/internal/bootstrap/primordials.js
parent969bd1eb7b56fda3573ad3d41745a491f2b06dde (diff)
downloadandroid-node-v8-112cc7c27551254aa2b17098fb774867f05ed0d9.tar.gz
android-node-v8-112cc7c27551254aa2b17098fb774867f05ed0d9.tar.bz2
android-node-v8-112cc7c27551254aa2b17098fb774867f05ed0d9.zip
lib: use safe methods from primordials
This changes the primordials to expose built-in prototypes with their methods already uncurried. The uncurryThis function is therefore moved to the primordials. All uses of uncurryThis on built-ins are changed to import the relevant prototypes from primordials. All uses of Function.call.bind are also changed to use primordials. PR-URL: https://github.com/nodejs/node/pull/27096 Reviewed-By: Ruben Bridgewater <ruben@bridgewater.de> Reviewed-By: James M Snell <jasnell@gmail.com> Reviewed-By: Joyee Cheung <joyeec9h3@gmail.com>
Diffstat (limited to 'lib/internal/bootstrap/primordials.js')
-rw-r--r--lib/internal/bootstrap/primordials.js33
1 files changed, 32 insertions, 1 deletions
diff --git a/lib/internal/bootstrap/primordials.js b/lib/internal/bootstrap/primordials.js
index e1fef7f886..8d2150b7b7 100644
--- a/lib/internal/bootstrap/primordials.js
+++ b/lib/internal/bootstrap/primordials.js
@@ -12,6 +12,19 @@
// `primordials.Object` where `primordials` is a lexical variable passed
// by the native module compiler.
+const ReflectApply = Reflect.apply;
+
+// This function is borrowed from the function with the same name on V8 Extras'
+// `utils` object. V8 implements Reflect.apply very efficiently in conjunction
+// with the spread syntax, such that no additional special case is needed for
+// function calls w/o arguments.
+// Refs: https://github.com/v8/v8/blob/d6ead37d265d7215cf9c5f768f279e21bd170212/src/js/prologue.js#L152-L156
+function uncurryThis(func) {
+ return (thisArg, ...args) => ReflectApply(func, thisArg, args);
+}
+
+primordials.uncurryThis = uncurryThis;
+
function copyProps(src, dest) {
for (const key of Reflect.ownKeys(src)) {
if (!Reflect.getOwnPropertyDescriptor(dest, key)) {
@@ -23,6 +36,18 @@ function copyProps(src, dest) {
}
}
+function copyPrototype(src, dest) {
+ for (const key of Reflect.ownKeys(src)) {
+ if (!Reflect.getOwnPropertyDescriptor(dest, key)) {
+ const desc = Reflect.getOwnPropertyDescriptor(src, key);
+ if (typeof desc.value === 'function') {
+ desc.value = uncurryThis(desc.value);
+ }
+ Reflect.defineProperty(dest, key, desc);
+ }
+ }
+}
+
function makeSafe(unsafe, safe) {
copyProps(unsafe.prototype, safe.prototype);
copyProps(unsafe, safe);
@@ -64,17 +89,23 @@ primordials.SafePromise = makeSafe(
// Create copies of intrinsic objects
[
'Array',
+ 'BigInt',
+ 'Boolean',
'Date',
+ 'Error',
'Function',
+ 'Map',
+ 'Number',
'Object',
'RegExp',
+ 'Set',
'String',
'Symbol',
].forEach((name) => {
const target = primordials[name] = Object.create(null);
copyProps(global[name], target);
const proto = primordials[name + 'Prototype'] = Object.create(null);
- copyProps(global[name].prototype, proto);
+ copyPrototype(global[name].prototype, proto);
});
Object.setPrototypeOf(primordials, null);