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

refactor: cleanup assert() & AssertionError definitions (#13859)

This commit is contained in:
Leo Kettmeir 2022-03-19 13:57:37 +01:00 committed by GitHub
parent ad8e238348
commit e55dee7fd8
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
8 changed files with 30 additions and 60 deletions

View file

@ -11,6 +11,7 @@
((window) => { ((window) => {
const core = Deno.core; const core = Deno.core;
const { const {
Error,
RegExp, RegExp,
ArrayPrototypeMap, ArrayPrototypeMap,
StringPrototypeCharCodeAt, StringPrototypeCharCodeAt,
@ -275,6 +276,24 @@
return StringPrototypeMatch(s, HTTP_BETWEEN_WHITESPACE)?.[1] ?? ""; return StringPrototypeMatch(s, HTTP_BETWEEN_WHITESPACE)?.[1] ?? "";
} }
class AssertionError extends Error {
constructor(msg) {
super(msg);
this.name = "AssertionError";
}
}
/**
* @param {unknown} cond
* @param {string=} msg
* @returns {asserts cond}
*/
function assert(cond, msg = "Assertion failed.") {
if (!cond) {
throw new AssertionError(msg);
}
}
window.__bootstrap.infra = { window.__bootstrap.infra = {
collectSequenceOfCodepoints, collectSequenceOfCodepoints,
ASCII_DIGIT, ASCII_DIGIT,
@ -299,5 +318,7 @@
collectHttpQuotedString, collectHttpQuotedString,
forgivingBase64Encode, forgivingBase64Encode,
forgivingBase64Decode, forgivingBase64Decode,
AssertionError,
assert,
}; };
})(globalThis); })(globalThis);

View file

@ -6,7 +6,6 @@
const { const {
ArrayPrototypePush, ArrayPrototypePush,
ArrayPrototypeShift, ArrayPrototypeShift,
Error,
FunctionPrototypeCall, FunctionPrototypeCall,
Map, Map,
MapPrototypeDelete, MapPrototypeDelete,
@ -22,25 +21,7 @@
TypeError, TypeError,
} = window.__bootstrap.primordials; } = window.__bootstrap.primordials;
const { webidl } = window.__bootstrap; const { webidl } = window.__bootstrap;
const { assert } = window.__bootstrap.infra;
// Shamelessly cribbed from extensions/fetch/11_streams.js
class AssertionError extends Error {
constructor(msg) {
super(msg);
this.name = "AssertionError";
}
}
/**
* @param {unknown} cond
* @param {string=} msg
* @returns {asserts cond}
*/
function assert(cond, msg = "Assertion failed.") {
if (!cond) {
throw new AssertionError(msg);
}
}
function opNow() { function opNow() {
return core.opSync("op_now"); return core.opSync("op_now");

View file

@ -21,7 +21,6 @@
BigInt64ArrayPrototype, BigInt64ArrayPrototype,
BigUint64ArrayPrototype, BigUint64ArrayPrototype,
DataView, DataView,
Error,
Int8ArrayPrototype, Int8ArrayPrototype,
Int16ArrayPrototype, Int16ArrayPrototype,
Int32ArrayPrototype, Int32ArrayPrototype,
@ -58,24 +57,7 @@
WeakMapPrototypeSet, WeakMapPrototypeSet,
} = globalThis.__bootstrap.primordials; } = globalThis.__bootstrap.primordials;
const consoleInternal = window.__bootstrap.console; const consoleInternal = window.__bootstrap.console;
const { AssertionError, assert } = window.__bootstrap.infra;
class AssertionError extends Error {
constructor(msg) {
super(msg);
this.name = "AssertionError";
}
}
/**
* @param {unknown} cond
* @param {string=} msg
* @returns {asserts cond}
*/
function assert(cond, msg = "Assertion failed.") {
if (!cond) {
throw new AssertionError(msg);
}
}
/** @template T */ /** @template T */
class Deferred { class Deferred {

View file

@ -4,7 +4,6 @@
((window) => { ((window) => {
const { const {
decodeURIComponent, decodeURIComponent,
Error,
ObjectPrototypeIsPrototypeOf, ObjectPrototypeIsPrototypeOf,
Promise, Promise,
SafeArrayIterator, SafeArrayIterator,
@ -34,19 +33,6 @@
} }
} }
class AssertionError extends Error {
constructor(msg) {
super(msg);
this.name = "AssertionError";
}
}
function assert(cond, msg = "Assertion failed.") {
if (!cond) {
throw new AssertionError(msg);
}
}
function createResolvable() { function createResolvable() {
let resolve; let resolve;
let reject; let reject;
@ -154,8 +140,6 @@
log, log,
setLogDebug, setLogDebug,
createResolvable, createResolvable,
assert,
AssertionError,
pathFromURL, pathFromURL,
writable, writable,
nonEnumerable, nonEnumerable,

View file

@ -6,7 +6,7 @@
"use strict"; "use strict";
((window) => { ((window) => {
const { assert } = window.__bootstrap.util; const { assert } = window.__bootstrap.infra;
const { const {
TypedArrayPrototypeSubarray, TypedArrayPrototypeSubarray,
TypedArrayPrototypeSlice, TypedArrayPrototypeSlice,

View file

@ -5,7 +5,8 @@
const core = window.Deno.core; const core = window.Deno.core;
const { FsFile } = window.__bootstrap.files; const { FsFile } = window.__bootstrap.files;
const { readAll } = window.__bootstrap.io; const { readAll } = window.__bootstrap.io;
const { assert, pathFromURL } = window.__bootstrap.util; const { pathFromURL } = window.__bootstrap.util;
const { assert } = window.__bootstrap.infra;
const { const {
ArrayPrototypeMap, ArrayPrototypeMap,
TypeError, TypeError,

View file

@ -6,7 +6,7 @@
const { setExitHandler } = window.__bootstrap.os; const { setExitHandler } = window.__bootstrap.os;
const { Console, inspectArgs } = window.__bootstrap.console; const { Console, inspectArgs } = window.__bootstrap.console;
const { serializePermissions } = window.__bootstrap.permissions; const { serializePermissions } = window.__bootstrap.permissions;
const { assert } = window.__bootstrap.util; const { assert } = window.__bootstrap.infra;
const { const {
AggregateErrorPrototype, AggregateErrorPrototype,
ArrayPrototypeFilter, ArrayPrototypeFilter,
@ -82,7 +82,7 @@
"op_fdatasync_async": ["flush pending data operations for a file to disk", "awaiting the result of a `Deno.fdatasync` call"], "op_fdatasync_async": ["flush pending data operations for a file to disk", "awaiting the result of a `Deno.fdatasync` call"],
"op_fetch_send": ["send a HTTP request", "awaiting the result of a `fetch` call"], "op_fetch_send": ["send a HTTP request", "awaiting the result of a `fetch` call"],
"op_ffi_call_nonblocking": ["do a non blocking ffi call", "awaiting the returned promise"] , "op_ffi_call_nonblocking": ["do a non blocking ffi call", "awaiting the returned promise"] ,
"op_ffi_call_ptr_nonblocking": ["do a non blocking ffi call", "awaiting the returned promise"], "op_ffi_call_ptr_nonblocking": ["do a non blocking ffi call", "awaiting the returned promise"],
"op_flock_async": ["lock a file", "awaiting the result of a `Deno.flock` call"], "op_flock_async": ["lock a file", "awaiting the result of a `Deno.flock` call"],
"op_fs_events_poll": ["get the next file system event", "breaking out of a for await loop looping over `Deno.FsEvents`"], "op_fs_events_poll": ["get the next file system event", "breaking out of a for await loop looping over `Deno.FsEvents`"],
"op_fstat_async": ["get file metadata", "awaiting the result of a `Deno.File#fstat` call"], "op_fstat_async": ["get file metadata", "awaiting the result of a `Deno.File#fstat` call"],

View file

@ -25,6 +25,7 @@ delete Object.prototype.__proto__;
PromisePrototypeThen, PromisePrototypeThen,
TypeError, TypeError,
} = window.__bootstrap.primordials; } = window.__bootstrap.primordials;
const infra = window.__bootstrap.infra;
const util = window.__bootstrap.util; const util = window.__bootstrap.util;
const eventTarget = window.__bootstrap.eventTarget; const eventTarget = window.__bootstrap.eventTarget;
const globalInterfaces = window.__bootstrap.globalInterfaces; const globalInterfaces = window.__bootstrap.globalInterfaces;
@ -705,7 +706,7 @@ delete Object.prototype.__proto__;
ObjectFreeze(globalThis.Deno.core); ObjectFreeze(globalThis.Deno.core);
} else { } else {
delete globalThis.Deno; delete globalThis.Deno;
util.assert(globalThis.Deno === undefined); infra.assert(globalThis.Deno === undefined);
} }
} }