Skip to content

Commit e55dee7

Browse files
authored
refactor: cleanup assert() & AssertionError definitions (#13859)
1 parent ad8e238 commit e55dee7

File tree

8 files changed

+30
-60
lines changed

8 files changed

+30
-60
lines changed

ext/web/00_infra.js

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
((window) => {
1212
const core = Deno.core;
1313
const {
14+
Error,
1415
RegExp,
1516
ArrayPrototypeMap,
1617
StringPrototypeCharCodeAt,
@@ -275,6 +276,24 @@
275276
return StringPrototypeMatch(s, HTTP_BETWEEN_WHITESPACE)?.[1] ?? "";
276277
}
277278

279+
class AssertionError extends Error {
280+
constructor(msg) {
281+
super(msg);
282+
this.name = "AssertionError";
283+
}
284+
}
285+
286+
/**
287+
* @param {unknown} cond
288+
* @param {string=} msg
289+
* @returns {asserts cond}
290+
*/
291+
function assert(cond, msg = "Assertion failed.") {
292+
if (!cond) {
293+
throw new AssertionError(msg);
294+
}
295+
}
296+
278297
window.__bootstrap.infra = {
279298
collectSequenceOfCodepoints,
280299
ASCII_DIGIT,
@@ -299,5 +318,7 @@
299318
collectHttpQuotedString,
300319
forgivingBase64Encode,
301320
forgivingBase64Decode,
321+
AssertionError,
322+
assert,
302323
};
303324
})(globalThis);

ext/web/02_timers.js

Lines changed: 1 addition & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66
const {
77
ArrayPrototypePush,
88
ArrayPrototypeShift,
9-
Error,
109
FunctionPrototypeCall,
1110
Map,
1211
MapPrototypeDelete,
@@ -22,25 +21,7 @@
2221
TypeError,
2322
} = window.__bootstrap.primordials;
2423
const { webidl } = window.__bootstrap;
25-
26-
// Shamelessly cribbed from extensions/fetch/11_streams.js
27-
class AssertionError extends Error {
28-
constructor(msg) {
29-
super(msg);
30-
this.name = "AssertionError";
31-
}
32-
}
33-
34-
/**
35-
* @param {unknown} cond
36-
* @param {string=} msg
37-
* @returns {asserts cond}
38-
*/
39-
function assert(cond, msg = "Assertion failed.") {
40-
if (!cond) {
41-
throw new AssertionError(msg);
42-
}
43-
}
24+
const { assert } = window.__bootstrap.infra;
4425

4526
function opNow() {
4627
return core.opSync("op_now");

ext/web/06_streams.js

Lines changed: 1 addition & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@
2121
BigInt64ArrayPrototype,
2222
BigUint64ArrayPrototype,
2323
DataView,
24-
Error,
2524
Int8ArrayPrototype,
2625
Int16ArrayPrototype,
2726
Int32ArrayPrototype,
@@ -58,24 +57,7 @@
5857
WeakMapPrototypeSet,
5958
} = globalThis.__bootstrap.primordials;
6059
const consoleInternal = window.__bootstrap.console;
61-
62-
class AssertionError extends Error {
63-
constructor(msg) {
64-
super(msg);
65-
this.name = "AssertionError";
66-
}
67-
}
68-
69-
/**
70-
* @param {unknown} cond
71-
* @param {string=} msg
72-
* @returns {asserts cond}
73-
*/
74-
function assert(cond, msg = "Assertion failed.") {
75-
if (!cond) {
76-
throw new AssertionError(msg);
77-
}
78-
}
60+
const { AssertionError, assert } = window.__bootstrap.infra;
7961

8062
/** @template T */
8163
class Deferred {

runtime/js/06_util.js

Lines changed: 0 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44
((window) => {
55
const {
66
decodeURIComponent,
7-
Error,
87
ObjectPrototypeIsPrototypeOf,
98
Promise,
109
SafeArrayIterator,
@@ -34,19 +33,6 @@
3433
}
3534
}
3635

37-
class AssertionError extends Error {
38-
constructor(msg) {
39-
super(msg);
40-
this.name = "AssertionError";
41-
}
42-
}
43-
44-
function assert(cond, msg = "Assertion failed.") {
45-
if (!cond) {
46-
throw new AssertionError(msg);
47-
}
48-
}
49-
5036
function createResolvable() {
5137
let resolve;
5238
let reject;
@@ -154,8 +140,6 @@
154140
log,
155141
setLogDebug,
156142
createResolvable,
157-
assert,
158-
AssertionError,
159143
pathFromURL,
160144
writable,
161145
nonEnumerable,

runtime/js/13_buffer.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
"use strict";
77

88
((window) => {
9-
const { assert } = window.__bootstrap.util;
9+
const { assert } = window.__bootstrap.infra;
1010
const {
1111
TypedArrayPrototypeSubarray,
1212
TypedArrayPrototypeSlice,

runtime/js/40_process.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,8 @@
55
const core = window.Deno.core;
66
const { FsFile } = window.__bootstrap.files;
77
const { readAll } = window.__bootstrap.io;
8-
const { assert, pathFromURL } = window.__bootstrap.util;
8+
const { pathFromURL } = window.__bootstrap.util;
9+
const { assert } = window.__bootstrap.infra;
910
const {
1011
ArrayPrototypeMap,
1112
TypeError,

runtime/js/40_testing.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
const { setExitHandler } = window.__bootstrap.os;
77
const { Console, inspectArgs } = window.__bootstrap.console;
88
const { serializePermissions } = window.__bootstrap.permissions;
9-
const { assert } = window.__bootstrap.util;
9+
const { assert } = window.__bootstrap.infra;
1010
const {
1111
AggregateErrorPrototype,
1212
ArrayPrototypeFilter,
@@ -82,7 +82,7 @@
8282
"op_fdatasync_async": ["flush pending data operations for a file to disk", "awaiting the result of a `Deno.fdatasync` call"],
8383
"op_fetch_send": ["send a HTTP request", "awaiting the result of a `fetch` call"],
8484
"op_ffi_call_nonblocking": ["do a non blocking ffi call", "awaiting the returned promise"] ,
85-
"op_ffi_call_ptr_nonblocking": ["do a non blocking ffi call", "awaiting the returned promise"],
85+
"op_ffi_call_ptr_nonblocking": ["do a non blocking ffi call", "awaiting the returned promise"],
8686
"op_flock_async": ["lock a file", "awaiting the result of a `Deno.flock` call"],
8787
"op_fs_events_poll": ["get the next file system event", "breaking out of a for await loop looping over `Deno.FsEvents`"],
8888
"op_fstat_async": ["get file metadata", "awaiting the result of a `Deno.File#fstat` call"],

runtime/js/99_main.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ delete Object.prototype.__proto__;
2525
PromisePrototypeThen,
2626
TypeError,
2727
} = window.__bootstrap.primordials;
28+
const infra = window.__bootstrap.infra;
2829
const util = window.__bootstrap.util;
2930
const eventTarget = window.__bootstrap.eventTarget;
3031
const globalInterfaces = window.__bootstrap.globalInterfaces;
@@ -705,7 +706,7 @@ delete Object.prototype.__proto__;
705706
ObjectFreeze(globalThis.Deno.core);
706707
} else {
707708
delete globalThis.Deno;
708-
util.assert(globalThis.Deno === undefined);
709+
infra.assert(globalThis.Deno === undefined);
709710
}
710711
}
711712

0 commit comments

Comments
 (0)