Skip to content

Commit

Permalink
refactor(ext/node): use primordials (#15912)
Browse files Browse the repository at this point in the history
  • Loading branch information
marcosc90 authored Sep 17, 2022
1 parent 684841a commit 513e934
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 21 deletions.
20 changes: 13 additions & 7 deletions ext/node/01_node.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,12 @@
ObjectEntries,
ObjectCreate,
ObjectDefineProperty,
Proxy,
ReflectDefineProperty,
ReflectGetOwnPropertyDescriptor,
ReflectOwnKeys,
Set,
SetPrototypeHas,
} = window.__bootstrap.primordials;

function assert(cond) {
Expand Down Expand Up @@ -50,29 +56,29 @@
return success;
},
ownKeys(_target) {
const globalThisKeys = Reflect.ownKeys(globalThis);
const nodeGlobalsKeys = Reflect.ownKeys(nodeGlobals);
const globalThisKeys = ReflectOwnKeys(globalThis);
const nodeGlobalsKeys = ReflectOwnKeys(nodeGlobals);
const nodeGlobalsKeySet = new Set(nodeGlobalsKeys);
return [
...ArrayPrototypeFilter(
globalThisKeys,
(k) => !nodeGlobalsKeySet.has(k),
(k) => !SetPrototypeHas(nodeGlobalsKeySet, k),
),
...nodeGlobalsKeys,
];
},
defineProperty(_target, prop, desc) {
if (prop in nodeGlobals) {
return Reflect.defineProperty(nodeGlobals, prop, desc);
return ReflectDefineProperty(nodeGlobals, prop, desc);
} else {
return Reflect.defineProperty(globalThis, prop, desc);
return ReflectDefineProperty(globalThis, prop, desc);
}
},
getOwnPropertyDescriptor(_target, prop) {
if (prop in nodeGlobals) {
return Reflect.getOwnPropertyDescriptor(nodeGlobals, prop);
return ReflectGetOwnPropertyDescriptor(nodeGlobals, prop);
} else {
return Reflect.getOwnPropertyDescriptor(globalThis, prop);
return ReflectGetOwnPropertyDescriptor(globalThis, prop);
}
},
has(_target, prop) {
Expand Down
36 changes: 22 additions & 14 deletions ext/node/02_require.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,16 +20,23 @@
ObjectKeys,
ObjectPrototype,
ObjectCreate,
Proxy,
SafeMap,
SafeWeakMap,
SafeArrayIterator,
JSONParse,
String,
StringPrototypeEndsWith,
StringPrototypeIndexOf,
StringPrototypeIncludes,
StringPrototypeMatch,
StringPrototypeSlice,
StringPrototypeSplit,
StringPrototypeStartsWith,
StringPrototypeCharCodeAt,
RegExpPrototypeTest,
Error,
TypeError,
} = window.__bootstrap.primordials;
const core = window.Deno.core;
const ops = core.ops;
Expand Down Expand Up @@ -254,9 +261,9 @@

Module.builtinModules = node.builtinModules;

Module._extensions = Object.create(null);
Module._cache = Object.create(null);
Module._pathCache = Object.create(null);
Module._extensions = ObjectCreate(null);
Module._cache = ObjectCreate(null);
Module._pathCache = ObjectCreate(null);
let modulePaths = [];
Module.globalPaths = modulePaths;

Expand Down Expand Up @@ -402,7 +409,7 @@
parent.filename,
);
if (denoDirPath) {
paths.push(denoDirPath);
ArrayPrototypePush(paths, denoDirPath);
}
}
const lookupPathsResult = ops.op_require_resolve_lookup_paths(
Expand All @@ -411,7 +418,7 @@
parent?.filename ?? "",
);
if (lookupPathsResult) {
paths.push(...lookupPathsResult);
ArrayPrototypePush(paths, ...new SafeArrayIterator(lookupPathsResult));
}
return paths;
};
Expand Down Expand Up @@ -668,10 +675,11 @@
function enrichCJSError(error) {
if (error instanceof SyntaxError) {
if (
error.message.includes(
StringPrototypeIncludes(
error.message,
"Cannot use import statement outside a module",
) ||
error.message.includes("Unexpected token 'export'")
StringPrototypeIncludes(error.message, "Unexpected token 'export'")
) {
console.error(
'To load an ES module, set "type": "module" in the package.json or use ' +
Expand Down Expand Up @@ -745,8 +753,8 @@
};

function stripBOM(content) {
if (content.charCodeAt(0) === 0xfeff) {
content = content.slice(1);
if (StringPrototypeCharCodeAt(content, 0) === 0xfeff) {
content = StringPrototypeSlice(content, 1);
}
return content;
}
Expand Down Expand Up @@ -865,13 +873,13 @@

/** @param specifier {string} */
function packageSpecifierSubPath(specifier) {
let parts = specifier.split("/");
if (parts[0].startsWith("@")) {
parts = parts.slice(2);
let parts = StringPrototypeSplit(specifier, "/");
if (StringPrototypeStartsWith(parts[0], "@")) {
parts = ArrayPrototypeSlice(parts, 2);
} else {
parts = parts.slice(1);
parts = ArrayPrototypeSlice(parts, 1);
}
return parts.join("/");
return ArrayPrototypeJoin(parts, "/");
}

window.__bootstrap.internals = {
Expand Down

0 comments on commit 513e934

Please sign in to comment.