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>
This commit is contained in:
Michaël Zasso
2019-11-22 18:04:46 +01:00
parent 35c6e0cc2b
commit 0646eda4fc
117 changed files with 1317 additions and 943 deletions

View File

@@ -1,26 +1,21 @@
'use strict';
const {
JSON,
Object: {
create: ObjectCreate,
keys: ObjectKeys,
getOwnPropertyDescriptor: ObjectGetOwnPropertyDescriptor,
},
ObjectPrototype: {
hasOwnProperty: ObjectHasOwnProperty
},
MapPrototype: {
entries: MapEntries
}, uncurryThis
JSONParse,
ObjectCreate,
ObjectKeys,
ObjectGetOwnPropertyDescriptor,
ObjectPrototypeHasOwnProperty,
MapPrototypeEntries,
WeakMapPrototypeGet,
uncurryThis,
} = primordials;
const MapIteratorNext = uncurryThis(MapEntries(new Map()).next);
const WeakMapGet = uncurryThis(WeakMap.prototype.get);
const MapIteratorNext = uncurryThis(MapPrototypeEntries(new Map()).next);
function ObjectGetValueSafe(obj, key) {
const desc = ObjectGetOwnPropertyDescriptor(obj, key);
return ObjectHasOwnProperty(desc, 'value') ? desc.value : undefined;
return ObjectPrototypeHasOwnProperty(desc, 'value') ? desc.value : undefined;
}
// See https://sourcemaps.info/spec.html for SourceMap V3 specification.
@@ -115,7 +110,7 @@ function lineLengths(content) {
function sourceMapFromFile(sourceMapFile) {
try {
const content = fs.readFileSync(sourceMapFile, 'utf8');
const data = JSON.parse(content);
const data = JSONParse(content);
return sourcesToAbsolute(dirname(sourceMapFile), data);
} catch (err) {
debug(err.stack);
@@ -134,7 +129,7 @@ function sourceMapFromDataUrl(basePath, url) {
const decodedData = base64 ?
Buffer.from(data, 'base64').toString('utf8') : data;
try {
const parsedData = JSON.parse(decodedData);
const parsedData = JSONParse(decodedData);
return sourcesToAbsolute(basePath, parsedData);
} catch (err) {
debug(err.stack);
@@ -182,7 +177,7 @@ function rekeySourceMap(cjsModuleInstance, newInstance) {
function sourceMapCacheToObject() {
const obj = ObjectCreate(null);
const it = MapEntries(esmSourceMapCache);
const it = MapPrototypeEntries(esmSourceMapCache);
let entry;
while (!(entry = MapIteratorNext(it)).done) {
const k = entry.value[0];
@@ -211,7 +206,7 @@ function appendCJSCache(obj) {
for (let i = 0; i < cjsModules.length; i++) {
const key = cjsModules[i];
const module = ObjectGetValueSafe(cjsModuleCache, key);
const value = WeakMapGet(cjsSourceMapCache, module);
const value = WeakMapPrototypeGet(cjsSourceMapCache, module);
if (value) {
// This is okay because `obj` has a null prototype.
obj[`file://${key}`] = {