0
0
Fork 0
mirror of https://github.com/denoland/deno.git synced 2025-03-03 17:34:47 -05:00

refactor: use primordials in runtime/, part1 (#11241)

This commit is contained in:
Bartek Iwańczuk 2021-07-03 16:58:08 +02:00 committed by GitHub
parent d4de477ef9
commit 6137c8046d
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
8 changed files with 82 additions and 31 deletions

View file

@ -8,7 +8,7 @@ failures:
exit(0) exit(0)
AssertionError: Test case attempted to exit with exit code: 0 AssertionError: Test case attempted to exit with exit code: 0
at assert (deno:runtime/js/06_util.js:33:13) at assert (deno:runtime/js/06_util.js:36:13)
at deno:runtime/js/40_testing.js:78:9 at deno:runtime/js/40_testing.js:78:9
at Object.exit (deno:runtime/js/30_os.js:48:7) at Object.exit (deno:runtime/js/30_os.js:48:7)
at [WILDCARD]/test/exit_sanitizer.ts:2:8 at [WILDCARD]/test/exit_sanitizer.ts:2:8
@ -21,7 +21,7 @@ AssertionError: Test case attempted to exit with exit code: 0
exit(1) exit(1)
AssertionError: Test case attempted to exit with exit code: 1 AssertionError: Test case attempted to exit with exit code: 1
at assert (deno:runtime/js/06_util.js:33:13) at assert (deno:runtime/js/06_util.js:36:13)
at deno:runtime/js/40_testing.js:78:9 at deno:runtime/js/40_testing.js:78:9
at Object.exit (deno:runtime/js/30_os.js:48:7) at Object.exit (deno:runtime/js/30_os.js:48:7)
at [WILDCARD]/test/exit_sanitizer.ts:6:8 at [WILDCARD]/test/exit_sanitizer.ts:6:8
@ -34,7 +34,7 @@ AssertionError: Test case attempted to exit with exit code: 1
exit(2) exit(2)
AssertionError: Test case attempted to exit with exit code: 2 AssertionError: Test case attempted to exit with exit code: 2
at assert (deno:runtime/js/06_util.js:33:13) at assert (deno:runtime/js/06_util.js:36:13)
at deno:runtime/js/40_testing.js:78:9 at deno:runtime/js/40_testing.js:78:9
at Object.exit (deno:runtime/js/30_os.js:48:7) at Object.exit (deno:runtime/js/30_os.js:48:7)
at [WILDCARD]/test/exit_sanitizer.ts:10:8 at [WILDCARD]/test/exit_sanitizer.ts:10:8

View file

@ -2,6 +2,8 @@
"use strict"; "use strict";
((window) => { ((window) => {
const { ObjectFreeze, StringPrototypeSplit } = window.__bootstrap.primordials;
const build = { const build = {
target: "unknown", target: "unknown",
arch: "unknown", arch: "unknown",
@ -11,13 +13,13 @@
}; };
function setBuildInfo(target) { function setBuildInfo(target) {
const [arch, vendor, os, env] = target.split("-", 4); const [arch, vendor, os, env] = StringPrototypeSplit(target, "-", 4);
build.target = target; build.target = target;
build.arch = arch; build.arch = arch;
build.vendor = vendor; build.vendor = vendor;
build.os = os; build.os = os;
build.env = env; build.env = env;
Object.freeze(build); ObjectFreeze(build);
} }
window.__bootstrap.build = { window.__bootstrap.build = {

View file

@ -3,6 +3,7 @@
((window) => { ((window) => {
const core = window.Deno.core; const core = window.Deno.core;
const { Error } = window.__bootstrap.primordials;
const { BadResource, Interrupted } = core; const { BadResource, Interrupted } = core;
class NotFound extends Error { class NotFound extends Error {

View file

@ -2,6 +2,8 @@
"use strict"; "use strict";
((window) => { ((window) => {
const { ObjectFreeze } = window.__bootstrap.primordials;
const version = { const version = {
deno: "", deno: "",
v8: "", v8: "",
@ -17,7 +19,7 @@
version.v8 = v8Version; version.v8 = v8Version;
version.typescript = tsVersion; version.typescript = tsVersion;
Object.freeze(version); ObjectFreeze(version);
} }
window.__bootstrap.version = { window.__bootstrap.version = {

View file

@ -2,6 +2,15 @@
"use strict"; "use strict";
((window) => { ((window) => {
const {
FunctionPrototypeCall,
Map,
MapPrototypeGet,
MapPrototypeSet,
ObjectDefineProperty,
TypeError,
Symbol,
} = window.__bootstrap.primordials;
const illegalConstructorKey = Symbol("illegalConstructorKey"); const illegalConstructorKey = Symbol("illegalConstructorKey");
function requiredArguments( function requiredArguments(
@ -23,29 +32,34 @@
if (typeof wrappedHandler.handler !== "function") { if (typeof wrappedHandler.handler !== "function") {
return; return;
} }
return wrappedHandler.handler.call(this, ...args); return FunctionPrototypeCall(wrappedHandler.handler, this, ...args);
} }
wrappedHandler.handler = handler; wrappedHandler.handler = handler;
return wrappedHandler; return wrappedHandler;
} }
function defineEventHandler(emitter, name, defaultValue = undefined) { function defineEventHandler(emitter, name, defaultValue = undefined) {
// HTML specification section 8.1.5.1 // HTML specification section 8.1.5.1
Object.defineProperty(emitter, `on${name}`, { ObjectDefineProperty(emitter, `on${name}`, {
get() { get() {
return this[handlerSymbol]?.get(name)?.handler ?? defaultValue; if (!this[handlerSymbol]) {
return defaultValue;
}
return MapPrototypeGet(this[handlerSymbol], name)?.handler ??
defaultValue;
}, },
set(value) { set(value) {
if (!this[handlerSymbol]) { if (!this[handlerSymbol]) {
this[handlerSymbol] = new Map(); this[handlerSymbol] = new Map();
} }
let handlerWrapper = this[handlerSymbol]?.get(name); let handlerWrapper = MapPrototypeGet(this[handlerSymbol], name);
if (handlerWrapper) { if (handlerWrapper) {
handlerWrapper.handler = value; handlerWrapper.handler = value;
} else { } else {
handlerWrapper = makeWrappedHandler(value); handlerWrapper = makeWrappedHandler(value);
this.addEventListener(name, handlerWrapper); this.addEventListener(name, handlerWrapper);
} }
this[handlerSymbol].set(name, handlerWrapper); MapPrototypeSet(this[handlerSymbol], name, handlerWrapper);
}, },
configurable: true, configurable: true,
enumerable: true, enumerable: true,

View file

@ -2,7 +2,10 @@
"use strict"; "use strict";
((window) => { ((window) => {
const { ObjectDefineProperty, StringPrototypeReplace, TypeError, Promise } =
window.__bootstrap.primordials;
const { build } = window.__bootstrap.build; const { build } = window.__bootstrap.build;
const { URL } = window.__bootstrap.url;
let logDebug = false; let logDebug = false;
let logSource = "JS"; let logSource = "JS";
@ -51,7 +54,7 @@
p, p,
value, value,
) { ) {
Object.defineProperty(o, p, { ObjectDefineProperty(o, p, {
value, value,
configurable: false, configurable: false,
writable: false, writable: false,
@ -60,12 +63,22 @@
// Keep in sync with `fromFileUrl()` in `std/path/win32.ts`. // Keep in sync with `fromFileUrl()` in `std/path/win32.ts`.
function pathFromURLWin32(url) { function pathFromURLWin32(url) {
let path = decodeURIComponent( let p = StringPrototypeReplace(
url.pathname url.pathname,
.replace(/^\/*([A-Za-z]:)(\/|$)/, "$1/") /^\/*([A-Za-z]:)(\/|$)/,
.replace(/\//g, "\\") "$1/",
.replace(/%(?![0-9A-Fa-f]{2})/g, "%25"),
); );
p = StringPrototypeReplace(
p,
/\//g,
"\\",
);
p = StringPrototypeReplace(
p,
/%(?![0-9A-Fa-f]{2})/g,
"%25",
);
let path = decodeURIComponent(p);
if (url.hostname != "") { if (url.hostname != "") {
// Note: The `URL` implementation guarantees that the drive letter and // Note: The `URL` implementation guarantees that the drive letter and
// hostname are mutually exclusive. Otherwise it would not have been valid // hostname are mutually exclusive. Otherwise it would not have been valid
@ -82,7 +95,7 @@
} }
return decodeURIComponent( return decodeURIComponent(
url.pathname.replace(/%(?![0-9A-Fa-f]{2})/g, "%25"), StringPrototypeReplace(url.pathname, /%(?![0-9A-Fa-f]{2})/g, "%25"),
); );
} }

View file

@ -3,7 +3,17 @@
((window) => { ((window) => {
const core = window.Deno.core; const core = window.Deno.core;
const {
ArrayIsArray,
ArrayPrototypeMap,
Error,
Uint8Array,
StringPrototypeStartsWith,
String,
SymbolIterator,
} = window.__bootstrap.primordials;
const webidl = window.__bootstrap.webidl; const webidl = window.__bootstrap.webidl;
const { URL } = window.__bootstrap.url;
const { Window } = window.__bootstrap.globalInterfaces; const { Window } = window.__bootstrap.globalInterfaces;
const { getLocationHref } = window.__bootstrap.location; const { getLocationHref } = window.__bootstrap.location;
const { log, pathFromURL } = window.__bootstrap.util; const { log, pathFromURL } = window.__bootstrap.util;
@ -75,13 +85,13 @@
`Expected 'array' or 'boolean' for ${permission} permission, "${value}" received`, `Expected 'array' or 'boolean' for ${permission} permission, "${value}" received`,
); );
} }
} else if (!Array.isArray(value) && typeof value !== "boolean") { } else if (!ArrayIsArray(value) && typeof value !== "boolean") {
throw new Error( throw new Error(
`Expected 'array' or 'boolean' for ${permission} permission, ${typeof value} received`, `Expected 'array' or 'boolean' for ${permission} permission, ${typeof value} received`,
); );
//Casts URLs to absolute routes //Casts URLs to absolute routes
} else if (Array.isArray(value)) { } else if (ArrayIsArray(value)) {
value = value.map((route) => { value = ArrayPrototypeMap(value, (route) => {
if (route instanceof URL) { if (route instanceof URL) {
route = pathFromURL(route); route = pathFromURL(route);
} }
@ -174,8 +184,9 @@
const sourceCode = core.decode(new Uint8Array()); const sourceCode = core.decode(new Uint8Array());
if ( if (
specifier.startsWith("./") || specifier.startsWith("../") || StringPrototypeStartsWith(specifier, "./") ||
specifier.startsWith("/") || type == "classic" StringPrototypeStartsWith(specifier, "../") ||
StringPrototypeStartsWith(specifier, "/") || type == "classic"
) { ) {
const baseUrl = getLocationHref(); const baseUrl = getLocationHref();
if (baseUrl != null) { if (baseUrl != null) {
@ -289,7 +300,7 @@
if ( if (
webidl.type(transferOrOptions) === "Object" && webidl.type(transferOrOptions) === "Object" &&
transferOrOptions !== undefined && transferOrOptions !== undefined &&
transferOrOptions[Symbol.iterator] !== undefined transferOrOptions[SymbolIterator] !== undefined
) { ) {
const transfer = webidl.converters["sequence<object>"]( const transfer = webidl.converters["sequence<object>"](
transferOrOptions, transferOrOptions,

View file

@ -7,6 +7,12 @@
((window) => { ((window) => {
const core = window.Deno.core; const core = window.Deno.core;
const {
Uint8Array,
ArrayPrototypePush,
TypedArrayPrototypeSubarray,
TypedArrayPrototypeSet,
} = window.__bootstrap.primordials;
const DEFAULT_BUFFER_SIZE = 32 * 1024; const DEFAULT_BUFFER_SIZE = 32 * 1024;
// Seek whence values. // Seek whence values.
// https://golang.org/pkg/io/#pkg-constants // https://golang.org/pkg/io/#pkg-constants
@ -36,7 +42,9 @@
} else { } else {
let nwritten = 0; let nwritten = 0;
while (nwritten < result) { while (nwritten < result) {
nwritten += await dst.write(b.subarray(nwritten, result)); nwritten += await dst.write(
TypedArrayPrototypeSubarray(b, nwritten, result),
);
} }
n += nwritten; n += nwritten;
} }
@ -56,7 +64,7 @@
break; break;
} }
yield b.subarray(0, result); yield TypedArrayPrototypeSubarray(b, 0, result);
} }
} }
@ -72,7 +80,7 @@
break; break;
} }
yield b.subarray(0, result); yield TypedArrayPrototypeSubarray(b, 0, result);
} }
} }
@ -119,7 +127,7 @@
const buf = new Uint8Array(READ_PER_ITER); const buf = new Uint8Array(READ_PER_ITER);
const read = await r.read(buf); const read = await r.read(buf);
if (typeof read == "number") { if (typeof read == "number") {
buffers.push(new Uint8Array(buf.buffer, 0, read)); ArrayPrototypePush(buffers, new Uint8Array(buf.buffer, 0, read));
} else { } else {
break; break;
} }
@ -137,7 +145,7 @@
let n = 0; let n = 0;
for (const buf of buffers) { for (const buf of buffers) {
contents.set(buf, n); TypedArrayPrototypeSet(contents, buf, n);
n += buf.byteLength; n += buf.byteLength;
} }
@ -151,7 +159,7 @@
const buf = new Uint8Array(READ_PER_ITER); const buf = new Uint8Array(READ_PER_ITER);
const read = r.readSync(buf); const read = r.readSync(buf);
if (typeof read == "number") { if (typeof read == "number") {
buffers.push(new Uint8Array(buf.buffer, 0, read)); ArrayPrototypePush(buffers, new Uint8Array(buf.buffer, 0, read));
} else { } else {
break; break;
} }
@ -166,7 +174,7 @@
let n = 0; let n = 0;
for (const buf of buffers) { for (const buf of buffers) {
contents.set(buf, n); TypedArrayPrototypeSet(contents, buf, n);
n += buf.byteLength; n += buf.byteLength;
} }