diff options
author | Michaël Zasso <targos@protonmail.com> | 2019-11-22 20:04:46 +0300 |
---|---|---|
committer | Michaël Zasso <targos@protonmail.com> | 2019-11-25 12:28:15 +0300 |
commit | 0646eda4fc0affb98e13c30acb522e63b7fd6dde (patch) | |
tree | 078209f50b044e24ea2c72cbbe7dca6e34bb7e25 /lib/internal/per_context | |
parent | 35c6e0cc2b56a5380e6808ef5603ecc2b167e032 (diff) |
lib: flatten access to primordials
Store all primordials as properties of the primordials object.
Static functions are prefixed by the constructor's name and prototype
methods are prefixed by the constructor's name followed by "Prototype".
For example: primordials.Object.keys becomes primordials.ObjectKeys.
PR-URL: https://github.com/nodejs/node/pull/30610
Refs: https://github.com/nodejs/node/issues/29766
Reviewed-By: Anna Henningsen <anna@addaleax.net>
Reviewed-By: Matteo Collina <matteo.collina@gmail.com>
Reviewed-By: Colin Ihrig <cjihrig@gmail.com>
Reviewed-By: Trivikram Kamat <trivikr.dev@gmail.com>
Diffstat (limited to 'lib/internal/per_context')
-rw-r--r-- | lib/internal/per_context/domexception.js | 13 | ||||
-rw-r--r-- | lib/internal/per_context/primordials.js | 33 |
2 files changed, 31 insertions, 15 deletions
diff --git a/lib/internal/per_context/domexception.js b/lib/internal/per_context/domexception.js index 3852567ac6b..9b266319042 100644 --- a/lib/internal/per_context/domexception.js +++ b/lib/internal/per_context/domexception.js @@ -1,10 +1,11 @@ 'use strict'; const { + ObjectDefineProperties, + ObjectDefineProperty, SafeWeakMap, SafeMap, - Object, - Symbol + SymbolToStringTag, } = primordials; class ERR_INVALID_THIS extends TypeError { @@ -73,8 +74,8 @@ class DOMException extends Error { } } -Object.defineProperties(DOMException.prototype, { - [Symbol.toStringTag]: { configurable: true, value: 'DOMException' }, +ObjectDefineProperties(DOMException.prototype, { + [SymbolToStringTag]: { configurable: true, value: 'DOMException' }, name: { enumerable: true, configurable: true }, message: { enumerable: true, configurable: true }, code: { enumerable: true, configurable: true } @@ -112,8 +113,8 @@ function forEachCode(fn) { forEachCode((name, codeName, value) => { const desc = { enumerable: true, value }; - Object.defineProperty(DOMException, codeName, desc); - Object.defineProperty(DOMException.prototype, codeName, desc); + ObjectDefineProperty(DOMException, codeName, desc); + ObjectDefineProperty(DOMException.prototype, codeName, desc); }); exports.DOMException = DOMException; diff --git a/lib/internal/per_context/primordials.js b/lib/internal/per_context/primordials.js index 7e02a9e3162..6957019490c 100644 --- a/lib/internal/per_context/primordials.js +++ b/lib/internal/per_context/primordials.js @@ -37,14 +37,28 @@ function copyProps(src, dest) { } } -function copyPrototype(src, dest) { +function copyPropsRenamed(src, dest, prefix) { for (const key of Reflect.ownKeys(src)) { - if (!Reflect.getOwnPropertyDescriptor(dest, key)) { + if (typeof key === 'string') { + Reflect.defineProperty( + dest, + `${prefix}${key[0].toUpperCase()}${key.slice(1)}`, + Reflect.getOwnPropertyDescriptor(src, key)); + } + } +} + +function copyPrototype(src, dest, prefix) { + for (const key of Reflect.ownKeys(src)) { + if (typeof key === 'string') { const desc = Reflect.getOwnPropertyDescriptor(src, key); if (typeof desc.value === 'function') { desc.value = uncurryThis(desc.value); } - Reflect.defineProperty(dest, key, desc); + Reflect.defineProperty( + dest, + `${prefix}${key[0].toUpperCase()}${key.slice(1)}`, + desc); } } } @@ -83,13 +97,13 @@ primordials.SafePromise = makeSafe( 'Math', 'Reflect' ].forEach((name) => { - const target = primordials[name] = Object.create(null); - copyProps(global[name], target); + copyPropsRenamed(global[name], primordials, name); }); // Create copies of intrinsic objects [ 'Array', + 'ArrayBuffer', 'BigInt', 'Boolean', 'Date', @@ -102,18 +116,19 @@ primordials.SafePromise = makeSafe( 'Set', 'String', 'Symbol', + 'WeakMap', + 'WeakSet', ].forEach((name) => { const original = global[name]; - const target = primordials[name] = Object.setPrototypeOf({ + primordials[name] = Object.setPrototypeOf({ [name]: function(...args) { return new.target ? ReflectConstruct(original, args, new.target) : ReflectApply(original, this, args); } }[name], null); - copyProps(original, target); - const proto = primordials[name + 'Prototype'] = Object.create(null); - copyPrototype(original.prototype, proto); + copyPropsRenamed(original, primordials, name); + copyPrototype(original.prototype, primordials, `${name}Prototype`); }); Object.setPrototypeOf(primordials, null); |