diff options
author | Nayeem Rahman <nayeemrmn99@gmail.com> | 2020-09-19 22:30:59 +0100 |
---|---|---|
committer | GitHub <noreply@github.com> | 2020-09-19 23:30:59 +0200 |
commit | aaa5e6613a739f8e2ff7579b69c2504bcdc37d4f (patch) | |
tree | d6c198b5bc222be4c097e7aa5e9504662c98a974 /op_crates/web | |
parent | 79e5b57663becad3614bce98e31c7d1d494cb50d (diff) |
fix(cli/rt): make some web API constructors illegal at runtime (#7468)
Diffstat (limited to 'op_crates/web')
-rw-r--r-- | op_crates/web/02_abort_signal.js | 9 | ||||
-rw-r--r-- | op_crates/web/abort_controller_test.js | 20 |
2 files changed, 27 insertions, 2 deletions
diff --git a/op_crates/web/02_abort_signal.js b/op_crates/web/02_abort_signal.js index 908e85ac9..2b51ef1ea 100644 --- a/op_crates/web/02_abort_signal.js +++ b/op_crates/web/02_abort_signal.js @@ -5,6 +5,8 @@ const signalAbort = Symbol("signalAbort"); const remove = Symbol("remove"); + const illegalConstructorKey = Symbol("illegalConstructorKey"); + class AbortSignal extends EventTarget { #aborted = false; #abortAlgorithms = new Set(); @@ -29,7 +31,10 @@ this.#abortAlgorithms.delete(algorithm); } - constructor() { + constructor(key) { + if (key != illegalConstructorKey) { + throw new TypeError("Illegal constructor."); + } super(); this.onabort = null; this.addEventListener("abort", (evt) => { @@ -50,7 +55,7 @@ } class AbortController { - #signal = new AbortSignal(); + #signal = new AbortSignal(illegalConstructorKey); get signal() { return this.#signal; diff --git a/op_crates/web/abort_controller_test.js b/op_crates/web/abort_controller_test.js index a2fb12c65..0243c6628 100644 --- a/op_crates/web/abort_controller_test.js +++ b/op_crates/web/abort_controller_test.js @@ -9,6 +9,19 @@ function assertEquals(left, right) { assert(left === right); } +function assertThrows(fn) { + let error = null; + try { + fn(); + } catch (error_) { + error = error_; + } + if (error == null) { + throw new Error("Didn't throw."); + } + return error; +} + function basicAbortController() { controller = new AbortController(); assert(controller); @@ -64,12 +77,19 @@ function controllerHasProperToString() { assertEquals(actual, "[object AbortController]"); } +function abortSignalIllegalConstructor() { + const error = assertThrows(() => new AbortSignal()); + assert(error instanceof TypeError); + assertEquals(error.message, "Illegal constructor."); +} + function main() { basicAbortController(); signalCallsOnabort(); signalEventListener(); onlyAbortsOnce(); controllerHasProperToString(); + abortSignalIllegalConstructor(); } main(); |