From 0646eda4fc0affb98e13c30acb522e63b7fd6dde Mon Sep 17 00:00:00 2001 From: Michaƫl Zasso Date: Fri, 22 Nov 2019 18:04:46 +0100 Subject: 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 Reviewed-By: Matteo Collina Reviewed-By: Colin Ihrig Reviewed-By: Trivikram Kamat --- lib/internal/url.js | 45 +++++++++++++++++++++++++++------------------ 1 file changed, 27 insertions(+), 18 deletions(-) (limited to 'lib/internal/url.js') diff --git a/lib/internal/url.js b/lib/internal/url.js index 860fa4d7ad..b4c047be52 100644 --- a/lib/internal/url.js +++ b/lib/internal/url.js @@ -1,6 +1,15 @@ 'use strict'; -const { Object, Reflect } = primordials; +const { + ObjectCreate, + ObjectDefineProperties, + ObjectDefineProperty, + ObjectGetOwnPropertySymbols, + ObjectGetPrototypeOf, + ObjectKeys, + ReflectGetOwnPropertyDescriptor, + ReflectOwnKeys, +} = primordials; const { inspect } = require('internal/util/inspect'); const { @@ -74,8 +83,8 @@ const searchParams = Symbol('query'); const kFormat = Symbol('format'); // https://tc39.github.io/ecma262/#sec-%iteratorprototype%-object -const IteratorPrototype = Object.getPrototypeOf( - Object.getPrototypeOf([][Symbol.iterator]()) +const IteratorPrototype = ObjectGetPrototypeOf( + ObjectGetPrototypeOf([][Symbol.iterator]()) ); const unpairedSurrogateRe = @@ -164,10 +173,10 @@ class URLSearchParams { // Record // Need to use reflection APIs for full spec compliance. this[searchParams] = []; - const keys = Reflect.ownKeys(init); + const keys = ReflectOwnKeys(init); for (let i = 0; i < keys.length; i++) { const key = keys[i]; - const desc = Reflect.getOwnPropertyDescriptor(init, key); + const desc = ReflectGetOwnPropertyDescriptor(init, key); if (desc !== undefined && desc.enumerable) { const typedKey = toUSVString(key); const typedValue = toUSVString(init[key]); @@ -338,7 +347,7 @@ class URL { [inspect.custom](depth, opts) { if (this == null || - Object.getPrototypeOf(this[context]) !== URLContext.prototype) { + ObjectGetPrototypeOf(this[context]) !== URLContext.prototype) { throw new ERR_INVALID_THIS('URL'); } @@ -347,7 +356,7 @@ class URL { const ctor = getConstructorOf(this); - const obj = Object.create({ + const obj = ObjectCreate({ constructor: ctor === null ? URL : ctor }); @@ -374,7 +383,7 @@ class URL { } } -Object.defineProperties(URL.prototype, { +ObjectDefineProperties(URL.prototype, { [kFormat]: { enumerable: false, configurable: false, @@ -846,7 +855,7 @@ function serializeParams(array) { // Mainly to mitigate func-name-matching ESLint rule function defineIDLClass(proto, classStr, obj) { // https://heycam.github.io/webidl/#dfn-class-string - Object.defineProperty(proto, Symbol.toStringTag, { + ObjectDefineProperty(proto, Symbol.toStringTag, { writable: false, enumerable: false, configurable: true, @@ -854,16 +863,16 @@ function defineIDLClass(proto, classStr, obj) { }); // https://heycam.github.io/webidl/#es-operations - for (const key of Object.keys(obj)) { - Object.defineProperty(proto, key, { + for (const key of ObjectKeys(obj)) { + ObjectDefineProperty(proto, key, { writable: true, enumerable: true, configurable: true, value: obj[key] }); } - for (const key of Object.getOwnPropertySymbols(obj)) { - Object.defineProperty(proto, key, { + for (const key of ObjectGetOwnPropertySymbols(obj)) { + ObjectDefineProperty(proto, key, { writable: true, enumerable: false, configurable: true, @@ -1137,7 +1146,7 @@ defineIDLClass(URLSearchParams.prototype, 'URLSearchParams', { }); // https://heycam.github.io/webidl/#es-iterable-entries -Object.defineProperty(URLSearchParams.prototype, Symbol.iterator, { +ObjectDefineProperty(URLSearchParams.prototype, Symbol.iterator, { writable: true, configurable: true, value: URLSearchParams.prototype.entries @@ -1145,7 +1154,7 @@ Object.defineProperty(URLSearchParams.prototype, Symbol.iterator, { // https://heycam.github.io/webidl/#dfn-default-iterator-object function createSearchParamsIterator(target, kind) { - const iterator = Object.create(URLSearchParamsIteratorPrototype); + const iterator = ObjectCreate(URLSearchParamsIteratorPrototype); iterator[context] = { target, kind, @@ -1155,12 +1164,12 @@ function createSearchParamsIterator(target, kind) { } // https://heycam.github.io/webidl/#dfn-iterator-prototype-object -const URLSearchParamsIteratorPrototype = Object.create(IteratorPrototype); +const URLSearchParamsIteratorPrototype = ObjectCreate(IteratorPrototype); defineIDLClass(URLSearchParamsIteratorPrototype, 'URLSearchParams Iterator', { next() { if (!this || - Object.getPrototypeOf(this) !== URLSearchParamsIteratorPrototype) { + ObjectGetPrototypeOf(this) !== URLSearchParamsIteratorPrototype) { throw new ERR_INVALID_THIS('URLSearchParamsIterator'); } @@ -1402,7 +1411,7 @@ function constructUrl(flags, protocol, username, password, ctx.fragment = fragment; ctx.host = host; - const url = Object.create(URL.prototype); + const url = ObjectCreate(URL.prototype); url[context] = ctx; const params = new URLSearchParams(); url[searchParams] = params; -- cgit v1.2.3