1
0
Fork 0
mirror of https://github.com/denoland/deno.git synced 2024-12-23 15:49:44 -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,
ObjectCreate,
ObjectDefineProperty,
Proxy,
ReflectDefineProperty,
ReflectGetOwnPropertyDescriptor,
ReflectOwnKeys,
Set,
SetPrototypeHas,
} = window.__bootstrap.primordials;
function assert(cond) {
@ -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) {

View file

@ -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;
@ -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;
@ -402,7 +409,7 @@
parent.filename,
);
if (denoDirPath) {
paths.push(denoDirPath);
ArrayPrototypePush(paths, denoDirPath);
}
}
const lookupPathsResult = ops.op_require_resolve_lookup_paths(
@ -411,7 +418,7 @@
parent?.filename ?? "",
);
if (lookupPathsResult) {
paths.push(...lookupPathsResult);
ArrayPrototypePush(paths, ...new SafeArrayIterator(lookupPathsResult));
}
return paths;
};
@ -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 ' +
@ -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;
}
@ -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 = {