Skip to content

Commit

Permalink
refactor: cleanup assert() & AssertionError definitions (#13859)
Browse files Browse the repository at this point in the history
  • Loading branch information
crowlKats authored Mar 19, 2022
1 parent ad8e238 commit e55dee7
Show file tree
Hide file tree
Showing 8 changed files with 30 additions and 60 deletions.
21 changes: 21 additions & 0 deletions ext/web/00_infra.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
((window) => {
const core = Deno.core;
const {
Error,
RegExp,
ArrayPrototypeMap,
StringPrototypeCharCodeAt,
Expand Down Expand Up @@ -275,6 +276,24 @@
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 = {
collectSequenceOfCodepoints,
ASCII_DIGIT,
Expand All @@ -299,5 +318,7 @@
collectHttpQuotedString,
forgivingBase64Encode,
forgivingBase64Decode,
AssertionError,
assert,
};
})(globalThis);
21 changes: 1 addition & 20 deletions ext/web/02_timers.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
const {
ArrayPrototypePush,
ArrayPrototypeShift,
Error,
FunctionPrototypeCall,
Map,
MapPrototypeDelete,
Expand All @@ -22,25 +21,7 @@
TypeError,
} = window.__bootstrap.primordials;
const { webidl } = window.__bootstrap;

// 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);
}
}
const { assert } = window.__bootstrap.infra;

function opNow() {
return core.opSync("op_now");
Expand Down
20 changes: 1 addition & 19 deletions ext/web/06_streams.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@
BigInt64ArrayPrototype,
BigUint64ArrayPrototype,
DataView,
Error,
Int8ArrayPrototype,
Int16ArrayPrototype,
Int32ArrayPrototype,
Expand Down Expand Up @@ -58,24 +57,7 @@
WeakMapPrototypeSet,
} = globalThis.__bootstrap.primordials;
const consoleInternal = window.__bootstrap.console;

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);
}
}
const { AssertionError, assert } = window.__bootstrap.infra;

/** @template T */
class Deferred {
Expand Down
16 changes: 0 additions & 16 deletions runtime/js/06_util.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
((window) => {
const {
decodeURIComponent,
Error,
ObjectPrototypeIsPrototypeOf,
Promise,
SafeArrayIterator,
Expand Down Expand Up @@ -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() {
let resolve;
let reject;
Expand Down Expand Up @@ -154,8 +140,6 @@
log,
setLogDebug,
createResolvable,
assert,
AssertionError,
pathFromURL,
writable,
nonEnumerable,
Expand Down
2 changes: 1 addition & 1 deletion runtime/js/13_buffer.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
"use strict";

((window) => {
const { assert } = window.__bootstrap.util;
const { assert } = window.__bootstrap.infra;
const {
TypedArrayPrototypeSubarray,
TypedArrayPrototypeSlice,
Expand Down
3 changes: 2 additions & 1 deletion runtime/js/40_process.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@
const core = window.Deno.core;
const { FsFile } = window.__bootstrap.files;
const { readAll } = window.__bootstrap.io;
const { assert, pathFromURL } = window.__bootstrap.util;
const { pathFromURL } = window.__bootstrap.util;
const { assert } = window.__bootstrap.infra;
const {
ArrayPrototypeMap,
TypeError,
Expand Down
4 changes: 2 additions & 2 deletions runtime/js/40_testing.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
const { setExitHandler } = window.__bootstrap.os;
const { Console, inspectArgs } = window.__bootstrap.console;
const { serializePermissions } = window.__bootstrap.permissions;
const { assert } = window.__bootstrap.util;
const { assert } = window.__bootstrap.infra;
const {
AggregateErrorPrototype,
ArrayPrototypeFilter,
Expand Down Expand Up @@ -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_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_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_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"],
Expand Down
3 changes: 2 additions & 1 deletion runtime/js/99_main.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ delete Object.prototype.__proto__;
PromisePrototypeThen,
TypeError,
} = window.__bootstrap.primordials;
const infra = window.__bootstrap.infra;
const util = window.__bootstrap.util;
const eventTarget = window.__bootstrap.eventTarget;
const globalInterfaces = window.__bootstrap.globalInterfaces;
Expand Down Expand Up @@ -705,7 +706,7 @@ delete Object.prototype.__proto__;
ObjectFreeze(globalThis.Deno.core);
} else {
delete globalThis.Deno;
util.assert(globalThis.Deno === undefined);
infra.assert(globalThis.Deno === undefined);
}
}

Expand Down

0 comments on commit e55dee7

Please sign in to comment.