summaryrefslogtreecommitdiff
path: root/ext/web/01_dom_exception.js
diff options
context:
space:
mode:
authorud2 <sjx233@qq.com>2024-06-05 07:09:13 +0800
committerGitHub <noreply@github.com>2024-06-05 01:09:13 +0200
commit29a075de2b625e1c893f6fad163a6d6937894a7a (patch)
treebb820ec7ea3b7de99d36618408f9cbba84641390 /ext/web/01_dom_exception.js
parent32f655db540a1d1368162f820e17585911c04c27 (diff)
chore(ext/web): use `Error.captureStackTrace` in `DOMException` constructor (#23986)
This makes `DOMException`'s `stack` property behave the same as native errors' – `stack` is now an own accessor property on every instance, and the getter calls `Error.prepareStackTrace`. Upgrades `deno_core` to 0.284.0. --------- Co-authored-by: Bartek Iwańczuk <biwanczuk@gmail.com>
Diffstat (limited to 'ext/web/01_dom_exception.js')
-rw-r--r--ext/web/01_dom_exception.js65
1 files changed, 20 insertions, 45 deletions
diff --git a/ext/web/01_dom_exception.js b/ext/web/01_dom_exception.js
index 82e8f867f..f49337db5 100644
--- a/ext/web/01_dom_exception.js
+++ b/ext/web/01_dom_exception.js
@@ -9,12 +9,12 @@
import { primordials } from "ext:core/mod.js";
const {
- ArrayPrototypeSlice,
- Error,
ErrorPrototype,
+ ErrorCaptureStackTrace,
ObjectDefineProperty,
ObjectCreate,
ObjectEntries,
+ ObjectHasOwn,
ObjectPrototypeIsPrototypeOf,
ObjectSetPrototypeOf,
Symbol,
@@ -27,7 +27,6 @@ import { createFilteredInspectProxy } from "ext:deno_console/01_console.js";
const _name = Symbol("name");
const _message = Symbol("message");
const _code = Symbol("code");
-const _error = Symbol("error");
// Defined in WebIDL 4.3.
// https://webidl.spec.whatwg.org/#idl-DOMException
@@ -113,8 +112,7 @@ class DOMException {
this[_code] = code;
this[webidl.brand] = webidl.brand;
- this[_error] = new Error(message);
- this[_error].name = "DOMException";
+ ErrorCaptureStackTrace(this, DOMException);
}
get message() {
@@ -133,50 +131,27 @@ class DOMException {
}
[SymbolFor("Deno.privateCustomInspect")](inspect, inspectOptions) {
- if (ObjectPrototypeIsPrototypeOf(DOMExceptionPrototype, this)) {
- return this[_error].stack;
- } else {
- return inspect(
- createFilteredInspectProxy({
- object: this,
- evaluate: false,
- keys: [
- "message",
- "name",
- "code",
- ],
- }),
- inspectOptions,
- );
+ if (ObjectHasOwn(this, "stack")) {
+ const stack = this.stack;
+ if (typeof stack === "string") {
+ return stack;
+ }
}
+ return inspect(
+ createFilteredInspectProxy({
+ object: this,
+ evaluate: ObjectPrototypeIsPrototypeOf(DOMExceptionPrototype, this),
+ keys: [
+ "message",
+ "name",
+ "code",
+ ],
+ }),
+ inspectOptions,
+ );
}
}
-ObjectDefineProperty(DOMException.prototype, "stack", {
- get() {
- return this[_error].stack;
- },
- set(value) {
- this[_error].stack = value;
- },
- configurable: true,
-});
-
-// `DOMException` isn't a native error, so `Error.prepareStackTrace()` is
-// not called when accessing `.stack`, meaning our structured stack trace
-// hack doesn't apply. This patches it in.
-ObjectDefineProperty(DOMException.prototype, "__callSiteEvals", {
- get() {
- // Call the stack getter so `__callSiteEvals` get populated.
- this[_error].stack;
- // To be extra sure, use an empty array if `__callSiteEvals` is still not there,
- // eg. if the user overrides `Error.prepareStackTrace`.
- const callSiteEvals = this[_error].__callSiteEvals ?? [];
- return ArrayPrototypeSlice(callSiteEvals, 1);
- },
- configurable: true,
-});
-
ObjectSetPrototypeOf(DOMException.prototype, ErrorPrototype);
webidl.configureInterface(DOMException);