1
0
Fork 0
mirror of https://github.com/denoland/deno.git synced 2024-12-24 08:09:08 -05:00

refactor(ext/node): use primordials (#15912)

This commit is contained in:
Marcos Casagrande 2022-09-17 09:05:04 +02:00 committed by GitHub
parent 684841a18c
commit 513e934fa7
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 35 additions and 21 deletions

View file

@ -11,6 +11,12 @@
ObjectEntries, ObjectEntries,
ObjectCreate, ObjectCreate,
ObjectDefineProperty, ObjectDefineProperty,
Proxy,
ReflectDefineProperty,
ReflectGetOwnPropertyDescriptor,
ReflectOwnKeys,
Set,
SetPrototypeHas,
} = window.__bootstrap.primordials; } = window.__bootstrap.primordials;
function assert(cond) { function assert(cond) {
@ -50,29 +56,29 @@
return success; return success;
}, },
ownKeys(_target) { ownKeys(_target) {
const globalThisKeys = Reflect.ownKeys(globalThis); const globalThisKeys = ReflectOwnKeys(globalThis);
const nodeGlobalsKeys = Reflect.ownKeys(nodeGlobals); const nodeGlobalsKeys = ReflectOwnKeys(nodeGlobals);
const nodeGlobalsKeySet = new Set(nodeGlobalsKeys); const nodeGlobalsKeySet = new Set(nodeGlobalsKeys);
return [ return [
...ArrayPrototypeFilter( ...ArrayPrototypeFilter(
globalThisKeys, globalThisKeys,
(k) => !nodeGlobalsKeySet.has(k), (k) => !SetPrototypeHas(nodeGlobalsKeySet, k),
), ),
...nodeGlobalsKeys, ...nodeGlobalsKeys,
]; ];
}, },
defineProperty(_target, prop, desc) { defineProperty(_target, prop, desc) {
if (prop in nodeGlobals) { if (prop in nodeGlobals) {
return Reflect.defineProperty(nodeGlobals, prop, desc); return ReflectDefineProperty(nodeGlobals, prop, desc);
} else { } else {
return Reflect.defineProperty(globalThis, prop, desc); return ReflectDefineProperty(globalThis, prop, desc);
} }
}, },
getOwnPropertyDescriptor(_target, prop) { getOwnPropertyDescriptor(_target, prop) {
if (prop in nodeGlobals) { if (prop in nodeGlobals) {
return Reflect.getOwnPropertyDescriptor(nodeGlobals, prop); return ReflectGetOwnPropertyDescriptor(nodeGlobals, prop);
} else { } else {
return Reflect.getOwnPropertyDescriptor(globalThis, prop); return ReflectGetOwnPropertyDescriptor(globalThis, prop);
} }
}, },
has(_target, prop) { has(_target, prop) {

View file

@ -20,16 +20,23 @@
ObjectKeys, ObjectKeys,
ObjectPrototype, ObjectPrototype,
ObjectCreate, ObjectCreate,
Proxy,
SafeMap, SafeMap,
SafeWeakMap, SafeWeakMap,
SafeArrayIterator,
JSONParse, JSONParse,
String,
StringPrototypeEndsWith, StringPrototypeEndsWith,
StringPrototypeIndexOf, StringPrototypeIndexOf,
StringPrototypeIncludes,
StringPrototypeMatch, StringPrototypeMatch,
StringPrototypeSlice, StringPrototypeSlice,
StringPrototypeSplit,
StringPrototypeStartsWith, StringPrototypeStartsWith,
StringPrototypeCharCodeAt, StringPrototypeCharCodeAt,
RegExpPrototypeTest, RegExpPrototypeTest,
Error,
TypeError,
} = window.__bootstrap.primordials; } = window.__bootstrap.primordials;
const core = window.Deno.core; const core = window.Deno.core;
const ops = core.ops; const ops = core.ops;
@ -254,9 +261,9 @@
Module.builtinModules = node.builtinModules; Module.builtinModules = node.builtinModules;
Module._extensions = Object.create(null); Module._extensions = ObjectCreate(null);
Module._cache = Object.create(null); Module._cache = ObjectCreate(null);
Module._pathCache = Object.create(null); Module._pathCache = ObjectCreate(null);
let modulePaths = []; let modulePaths = [];
Module.globalPaths = modulePaths; Module.globalPaths = modulePaths;
@ -402,7 +409,7 @@
parent.filename, parent.filename,
); );
if (denoDirPath) { if (denoDirPath) {
paths.push(denoDirPath); ArrayPrototypePush(paths, denoDirPath);
} }
} }
const lookupPathsResult = ops.op_require_resolve_lookup_paths( const lookupPathsResult = ops.op_require_resolve_lookup_paths(
@ -411,7 +418,7 @@
parent?.filename ?? "", parent?.filename ?? "",
); );
if (lookupPathsResult) { if (lookupPathsResult) {
paths.push(...lookupPathsResult); ArrayPrototypePush(paths, ...new SafeArrayIterator(lookupPathsResult));
} }
return paths; return paths;
}; };
@ -668,10 +675,11 @@
function enrichCJSError(error) { function enrichCJSError(error) {
if (error instanceof SyntaxError) { if (error instanceof SyntaxError) {
if ( if (
error.message.includes( StringPrototypeIncludes(
error.message,
"Cannot use import statement outside a module", "Cannot use import statement outside a module",
) || ) ||
error.message.includes("Unexpected token 'export'") StringPrototypeIncludes(error.message, "Unexpected token 'export'")
) { ) {
console.error( console.error(
'To load an ES module, set "type": "module" in the package.json or use ' + 'To load an ES module, set "type": "module" in the package.json or use ' +
@ -745,8 +753,8 @@
}; };
function stripBOM(content) { function stripBOM(content) {
if (content.charCodeAt(0) === 0xfeff) { if (StringPrototypeCharCodeAt(content, 0) === 0xfeff) {
content = content.slice(1); content = StringPrototypeSlice(content, 1);
} }
return content; return content;
} }
@ -865,13 +873,13 @@
/** @param specifier {string} */ /** @param specifier {string} */
function packageSpecifierSubPath(specifier) { function packageSpecifierSubPath(specifier) {
let parts = specifier.split("/"); let parts = StringPrototypeSplit(specifier, "/");
if (parts[0].startsWith("@")) { if (StringPrototypeStartsWith(parts[0], "@")) {
parts = parts.slice(2); parts = ArrayPrototypeSlice(parts, 2);
} else { } else {
parts = parts.slice(1); parts = ArrayPrototypeSlice(parts, 1);
} }
return parts.join("/"); return ArrayPrototypeJoin(parts, "/");
} }
window.__bootstrap.internals = { window.__bootstrap.internals = {