diff options
author | Michaël Zasso <targos@protonmail.com> | 2019-04-05 11:11:26 +0200 |
---|---|---|
committer | Michaël Zasso <targos@protonmail.com> | 2019-04-08 11:23:09 +0200 |
commit | 112cc7c27551254aa2b17098fb774867f05ed0d9 (patch) | |
tree | 0f48ef3ac9e8949f0bf49af38fb6dc7c6e2f64af /lib/internal/bootstrap | |
parent | 969bd1eb7b56fda3573ad3d41745a491f2b06dde (diff) | |
download | android-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')
-rw-r--r-- | lib/internal/bootstrap/loaders.js | 7 | ||||
-rw-r--r-- | lib/internal/bootstrap/primordials.js | 33 |
2 files changed, 35 insertions, 5 deletions
diff --git a/lib/internal/bootstrap/loaders.js b/lib/internal/bootstrap/loaders.js index ffcc931334..e4d1a09587 100644 --- a/lib/internal/bootstrap/loaders.js +++ b/lib/internal/bootstrap/loaders.js @@ -225,7 +225,7 @@ NativeModule.prototype.compileForPublicLoader = function(needToProxify) { }; const getOwn = (target, property, receiver) => { - return Reflect.apply(ObjectPrototype.hasOwnProperty, target, [property]) ? + return ObjectPrototype.hasOwnProperty(target, property) ? Reflect.get(target, property, receiver) : undefined; }; @@ -239,8 +239,7 @@ NativeModule.prototype.proxifyExports = function() { const update = (property, value) => { if (this.reflect !== undefined && - Reflect.apply(ObjectPrototype.hasOwnProperty, - this.reflect.exports, [property])) + ObjectPrototype.hasOwnProperty(this.reflect.exports, property)) this.reflect.exports[property].set(value); }; @@ -254,7 +253,7 @@ NativeModule.prototype.proxifyExports = function() { !Reflect.has(handler, 'get')) { handler.get = (target, prop, receiver) => { const value = Reflect.get(target, prop, receiver); - if (Reflect.apply(ObjectPrototype.hasOwnProperty, target, [prop])) + if (ObjectPrototype.hasOwnProperty(target, prop)) update(prop, value); return value; }; 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); |