Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(ext/dom_exception): align better with spec #15097

Merged
merged 9 commits into from Jul 20, 2022
Merged
Show file tree
Hide file tree
Changes from 8 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 15 additions & 1 deletion cli/tests/unit/dom_exception_test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
import { assertEquals, assertStringIncludes } from "./test_util.ts";
import {
assertEquals,
assertNotEquals,
assertStringIncludes,
} from "./test_util.ts";

Deno.test(function customInspectFunction() {
const blob = new DOMException("test");
Expand All @@ -8,3 +12,13 @@ Deno.test(function customInspectFunction() {
);
assertStringIncludes(Deno.inspect(DOMException.prototype), "DOMException");
});

Deno.test(function nameToCodeMappingPrototypeAccess() {
const newCode = 100;
const objectPrototype = Object.prototype as unknown as {
pollution: number;
};
objectPrototype.pollution = newCode;
assertNotEquals(newCode, new DOMException("test", "pollution").code);
Reflect.deleteProperty(objectPrototype, "pollution");
});
43 changes: 29 additions & 14 deletions ext/web/01_dom_exception.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,18 @@
ObjectEntries,
ObjectPrototypeIsPrototypeOf,
ObjectSetPrototypeOf,
Symbol,
SymbolFor,
} = window.__bootstrap.primordials;
const webidl = window.__bootstrap.webidl;
const consoleInternal = window.__bootstrap.console;

const _name = Symbol("name");
const _message = Symbol("message");
const _code = Symbol("code");

// Defined in WebIDL 4.3.
// https://heycam.github.io/webidl/#idl-DOMException
// https://webidl.spec.whatwg.org/#idl-DOMException
const INDEX_SIZE_ERR = 1;
const DOMSTRING_SIZE_ERR = 2;
const HIERARCHY_REQUEST_ERR = 3;
Expand Down Expand Up @@ -52,9 +57,10 @@
const DATA_CLONE_ERR = 25;

// Defined in WebIDL 2.8.1.
// https://heycam.github.io/webidl/#dfn-error-names-table
// https://webidl.spec.whatwg.org/#dfn-error-names-table
/** @type {Record<string, number>} */
const nameToCodeMapping = {
__proto__: null,
IndexSizeError: INDEX_SIZE_ERR,
HierarchyRequestError: HIERARCHY_REQUEST_ERR,
WrongDocumentError: WRONG_DOCUMENT_ERR,
Expand All @@ -80,24 +86,30 @@
};

// Defined in WebIDL 4.3.
// https://heycam.github.io/webidl/#idl-DOMException
// https://webidl.spec.whatwg.org/#idl-DOMException
class DOMException {
#message = "";
#name = "";
#code = 0;
[_message];
[_name];
[_code];

// https://webidl.spec.whatwg.org/#dom-domexception-domexception
constructor(message = "", name = "Error") {
this.#message = webidl.converters.DOMString(message, {
message = webidl.converters.DOMString(message, {
prefix: "Failed to construct 'DOMException'",
context: "Argument 1",
});
this.#name = webidl.converters.DOMString(name, {
name = webidl.converters.DOMString(name, {
prefix: "Failed to construct 'DOMException'",
context: "Argument 2",
});
this.#code = nameToCodeMapping[this.#name] ?? 0;
const code = nameToCodeMapping[name] ?? 0;

this[_message] = message;
this[_name] = name;
this[_code] = code;
this[webidl.brand] = webidl.brand;

const error = new Error(this.#message);
const error = new Error(message);
error.name = "DOMException";
ObjectDefineProperty(this, "stack", {
value: error.stack,
Expand All @@ -115,20 +127,23 @@
}

get message() {
return this.#message;
webidl.assertBranded(this, DOMExceptionPrototype);
return this[_message];
}

get name() {
return this.#name;
webidl.assertBranded(this, DOMExceptionPrototype);
return this[_name];
}

get code() {
return this.#code;
webidl.assertBranded(this, DOMExceptionPrototype);
return this[_code];
}

[SymbolFor("Deno.customInspect")](inspect) {
if (ObjectPrototypeIsPrototypeOf(DOMExceptionPrototype, this)) {
return `DOMException: ${this.#message}`;
return `DOMException: ${this[_message]}`;
} else {
return inspect(consoleInternal.createFilteredInspectProxy({
object: this,
Expand Down