2022-01-07 22:09:52 -05:00
|
|
|
// Copyright 2018-2022 the Deno authors. All rights reserved. MIT license.
|
2021-02-13 09:58:12 -05:00
|
|
|
|
|
|
|
// @ts-check
|
2021-07-03 15:32:28 -04:00
|
|
|
/// <reference path="../../core/internal.d.ts" />
|
2021-02-13 09:58:12 -05:00
|
|
|
/// <reference path="../../core/lib.deno_core.d.ts" />
|
chore: split web op crate (#9635)
This commit starts splitting out the deno_web op crate into multiple
smaller crates. This commit splits out WebIDL and URL API, but in the
future I want to split out each spec into its own crate. That means we
will have (in rough order of loading): `webidl`, `dom`, `streams`,
`console`, `encoding`, `url`, `file`, `fetch`, `websocket`, and
`webgpu` crates.
2021-03-12 10:17:18 -05:00
|
|
|
/// <reference path="../webidl/internal.d.ts" />
|
2021-02-13 09:58:12 -05:00
|
|
|
/// <reference path="../web/internal.d.ts" />
|
|
|
|
/// <reference path="../web/lib.deno_web.d.ts" />
|
|
|
|
|
2021-02-04 17:18:32 -05:00
|
|
|
"use strict";
|
2020-04-11 11:42:02 -04:00
|
|
|
|
2021-01-09 01:27:46 -05:00
|
|
|
((window) => {
|
2021-07-04 10:08:53 -04:00
|
|
|
const {
|
2021-09-08 17:14:26 -04:00
|
|
|
ArrayPrototypeSlice,
|
|
|
|
Error,
|
2021-07-26 07:52:59 -04:00
|
|
|
ErrorPrototype,
|
2021-07-04 10:08:53 -04:00
|
|
|
ObjectDefineProperty,
|
2022-07-20 04:12:18 -04:00
|
|
|
ObjectCreate,
|
2021-07-04 10:08:53 -04:00
|
|
|
ObjectEntries,
|
2022-02-01 12:06:11 -05:00
|
|
|
ObjectPrototypeIsPrototypeOf,
|
2021-07-04 10:08:53 -04:00
|
|
|
ObjectSetPrototypeOf,
|
2022-12-19 21:37:50 -05:00
|
|
|
SafeArrayIterator,
|
2022-07-20 04:12:18 -04:00
|
|
|
Symbol,
|
2021-07-26 07:52:59 -04:00
|
|
|
SymbolFor,
|
2021-07-04 10:08:53 -04:00
|
|
|
} = window.__bootstrap.primordials;
|
2021-02-13 09:58:12 -05:00
|
|
|
const webidl = window.__bootstrap.webidl;
|
2021-07-08 09:43:36 -04:00
|
|
|
const consoleInternal = window.__bootstrap.console;
|
2021-02-13 09:58:12 -05:00
|
|
|
|
2022-07-20 04:12:18 -04:00
|
|
|
const _name = Symbol("name");
|
|
|
|
const _message = Symbol("message");
|
|
|
|
const _code = Symbol("code");
|
|
|
|
|
2021-01-14 08:08:49 -05:00
|
|
|
// Defined in WebIDL 4.3.
|
2022-07-20 04:12:18 -04:00
|
|
|
// https://webidl.spec.whatwg.org/#idl-DOMException
|
2021-01-14 08:08:49 -05:00
|
|
|
const INDEX_SIZE_ERR = 1;
|
|
|
|
const DOMSTRING_SIZE_ERR = 2;
|
|
|
|
const HIERARCHY_REQUEST_ERR = 3;
|
|
|
|
const WRONG_DOCUMENT_ERR = 4;
|
|
|
|
const INVALID_CHARACTER_ERR = 5;
|
|
|
|
const NO_DATA_ALLOWED_ERR = 6;
|
|
|
|
const NO_MODIFICATION_ALLOWED_ERR = 7;
|
|
|
|
const NOT_FOUND_ERR = 8;
|
|
|
|
const NOT_SUPPORTED_ERR = 9;
|
|
|
|
const INUSE_ATTRIBUTE_ERR = 10;
|
|
|
|
const INVALID_STATE_ERR = 11;
|
|
|
|
const SYNTAX_ERR = 12;
|
|
|
|
const INVALID_MODIFICATION_ERR = 13;
|
|
|
|
const NAMESPACE_ERR = 14;
|
|
|
|
const INVALID_ACCESS_ERR = 15;
|
|
|
|
const VALIDATION_ERR = 16;
|
|
|
|
const TYPE_MISMATCH_ERR = 17;
|
|
|
|
const SECURITY_ERR = 18;
|
|
|
|
const NETWORK_ERR = 19;
|
|
|
|
const ABORT_ERR = 20;
|
|
|
|
const URL_MISMATCH_ERR = 21;
|
|
|
|
const QUOTA_EXCEEDED_ERR = 22;
|
|
|
|
const TIMEOUT_ERR = 23;
|
|
|
|
const INVALID_NODE_TYPE_ERR = 24;
|
|
|
|
const DATA_CLONE_ERR = 25;
|
|
|
|
|
|
|
|
// Defined in WebIDL 2.8.1.
|
2022-07-20 04:12:18 -04:00
|
|
|
// https://webidl.spec.whatwg.org/#dfn-error-names-table
|
2021-02-13 09:58:12 -05:00
|
|
|
/** @type {Record<string, number>} */
|
2022-07-20 04:12:18 -04:00
|
|
|
// the prototype should be null, to prevent user code from looking
|
|
|
|
// up Object.prototype properties, such as "toString"
|
|
|
|
const nameToCodeMapping = ObjectCreate(null, {
|
|
|
|
IndexSizeError: { value: INDEX_SIZE_ERR },
|
|
|
|
HierarchyRequestError: { value: HIERARCHY_REQUEST_ERR },
|
|
|
|
WrongDocumentError: { value: WRONG_DOCUMENT_ERR },
|
|
|
|
InvalidCharacterError: { value: INVALID_CHARACTER_ERR },
|
|
|
|
NoModificationAllowedError: { value: NO_MODIFICATION_ALLOWED_ERR },
|
|
|
|
NotFoundError: { value: NOT_FOUND_ERR },
|
|
|
|
NotSupportedError: { value: NOT_SUPPORTED_ERR },
|
|
|
|
InUseAttributeError: { value: INUSE_ATTRIBUTE_ERR },
|
|
|
|
InvalidStateError: { value: INVALID_STATE_ERR },
|
|
|
|
SyntaxError: { value: SYNTAX_ERR },
|
|
|
|
InvalidModificationError: { value: INVALID_MODIFICATION_ERR },
|
|
|
|
NamespaceError: { value: NAMESPACE_ERR },
|
|
|
|
InvalidAccessError: { value: INVALID_ACCESS_ERR },
|
|
|
|
TypeMismatchError: { value: TYPE_MISMATCH_ERR },
|
|
|
|
SecurityError: { value: SECURITY_ERR },
|
|
|
|
NetworkError: { value: NETWORK_ERR },
|
|
|
|
AbortError: { value: ABORT_ERR },
|
|
|
|
URLMismatchError: { value: URL_MISMATCH_ERR },
|
|
|
|
QuotaExceededError: { value: QUOTA_EXCEEDED_ERR },
|
|
|
|
TimeoutError: { value: TIMEOUT_ERR },
|
|
|
|
InvalidNodeTypeError: { value: INVALID_NODE_TYPE_ERR },
|
|
|
|
DataCloneError: { value: DATA_CLONE_ERR },
|
|
|
|
});
|
2021-01-14 08:08:49 -05:00
|
|
|
|
|
|
|
// Defined in WebIDL 4.3.
|
2022-07-20 04:12:18 -04:00
|
|
|
// https://webidl.spec.whatwg.org/#idl-DOMException
|
2021-07-04 10:08:53 -04:00
|
|
|
class DOMException {
|
2022-07-20 04:12:18 -04:00
|
|
|
[_message];
|
|
|
|
[_name];
|
|
|
|
[_code];
|
2020-04-11 11:42:02 -04:00
|
|
|
|
2022-07-20 04:12:18 -04:00
|
|
|
// https://webidl.spec.whatwg.org/#dom-domexception-domexception
|
2021-01-09 01:27:46 -05:00
|
|
|
constructor(message = "", name = "Error") {
|
2022-07-20 04:12:18 -04:00
|
|
|
message = webidl.converters.DOMString(message, {
|
2021-02-13 09:58:12 -05:00
|
|
|
prefix: "Failed to construct 'DOMException'",
|
|
|
|
context: "Argument 1",
|
|
|
|
});
|
2022-07-20 04:12:18 -04:00
|
|
|
name = webidl.converters.DOMString(name, {
|
2021-02-13 09:58:12 -05:00
|
|
|
prefix: "Failed to construct 'DOMException'",
|
|
|
|
context: "Argument 2",
|
|
|
|
});
|
2022-07-20 04:12:18 -04:00
|
|
|
const code = nameToCodeMapping[name] ?? 0;
|
|
|
|
|
|
|
|
this[_message] = message;
|
|
|
|
this[_name] = name;
|
|
|
|
this[_code] = code;
|
|
|
|
this[webidl.brand] = webidl.brand;
|
2021-09-08 17:14:26 -04:00
|
|
|
|
2022-07-20 04:12:18 -04:00
|
|
|
const error = new Error(message);
|
2021-10-08 11:01:02 -04:00
|
|
|
error.name = "DOMException";
|
2021-10-03 11:21:49 -04:00
|
|
|
ObjectDefineProperty(this, "stack", {
|
|
|
|
value: error.stack,
|
|
|
|
writable: true,
|
|
|
|
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.
|
2021-09-08 17:14:26 -04:00
|
|
|
ObjectDefineProperty(this, "__callSiteEvals", {
|
|
|
|
value: ArrayPrototypeSlice(error.__callSiteEvals, 1),
|
|
|
|
configurable: true,
|
|
|
|
});
|
2021-01-09 01:27:46 -05:00
|
|
|
}
|
|
|
|
|
2021-01-14 08:08:49 -05:00
|
|
|
get message() {
|
2022-07-20 04:12:18 -04:00
|
|
|
webidl.assertBranded(this, DOMExceptionPrototype);
|
|
|
|
return this[_message];
|
2021-01-14 08:08:49 -05:00
|
|
|
}
|
|
|
|
|
2021-01-09 01:27:46 -05:00
|
|
|
get name() {
|
2022-07-20 04:12:18 -04:00
|
|
|
webidl.assertBranded(this, DOMExceptionPrototype);
|
|
|
|
return this[_name];
|
2021-01-09 01:27:46 -05:00
|
|
|
}
|
2020-04-11 11:42:02 -04:00
|
|
|
|
2021-01-09 01:27:46 -05:00
|
|
|
get code() {
|
2022-07-20 04:12:18 -04:00
|
|
|
webidl.assertBranded(this, DOMExceptionPrototype);
|
|
|
|
return this[_code];
|
2021-01-09 01:27:46 -05:00
|
|
|
}
|
2021-01-14 08:08:49 -05:00
|
|
|
|
2021-07-26 07:52:59 -04:00
|
|
|
[SymbolFor("Deno.customInspect")](inspect) {
|
2022-02-01 12:06:11 -05:00
|
|
|
if (ObjectPrototypeIsPrototypeOf(DOMExceptionPrototype, this)) {
|
2022-07-20 04:12:18 -04:00
|
|
|
return `DOMException: ${this[_message]}`;
|
2021-07-08 09:43:36 -04:00
|
|
|
} else {
|
|
|
|
return inspect(consoleInternal.createFilteredInspectProxy({
|
|
|
|
object: this,
|
|
|
|
evaluate: false,
|
|
|
|
keys: [
|
|
|
|
"message",
|
|
|
|
"name",
|
|
|
|
"code",
|
|
|
|
],
|
|
|
|
}));
|
|
|
|
}
|
2021-07-04 10:08:53 -04:00
|
|
|
}
|
2021-01-14 08:08:49 -05:00
|
|
|
}
|
|
|
|
|
2021-07-26 07:52:59 -04:00
|
|
|
ObjectSetPrototypeOf(DOMException.prototype, ErrorPrototype);
|
2021-07-04 10:08:53 -04:00
|
|
|
|
2021-06-07 04:04:10 -04:00
|
|
|
webidl.configurePrototype(DOMException);
|
2022-02-01 12:06:11 -05:00
|
|
|
const DOMExceptionPrototype = DOMException.prototype;
|
2021-01-14 08:08:49 -05:00
|
|
|
|
|
|
|
for (
|
2022-12-19 21:37:50 -05:00
|
|
|
const [key, value] of new SafeArrayIterator(
|
|
|
|
ObjectEntries({
|
|
|
|
INDEX_SIZE_ERR,
|
|
|
|
DOMSTRING_SIZE_ERR,
|
|
|
|
HIERARCHY_REQUEST_ERR,
|
|
|
|
WRONG_DOCUMENT_ERR,
|
|
|
|
INVALID_CHARACTER_ERR,
|
|
|
|
NO_DATA_ALLOWED_ERR,
|
|
|
|
NO_MODIFICATION_ALLOWED_ERR,
|
|
|
|
NOT_FOUND_ERR,
|
|
|
|
NOT_SUPPORTED_ERR,
|
|
|
|
INUSE_ATTRIBUTE_ERR,
|
|
|
|
INVALID_STATE_ERR,
|
|
|
|
SYNTAX_ERR,
|
|
|
|
INVALID_MODIFICATION_ERR,
|
|
|
|
NAMESPACE_ERR,
|
|
|
|
INVALID_ACCESS_ERR,
|
|
|
|
VALIDATION_ERR,
|
|
|
|
TYPE_MISMATCH_ERR,
|
|
|
|
SECURITY_ERR,
|
|
|
|
NETWORK_ERR,
|
|
|
|
ABORT_ERR,
|
|
|
|
URL_MISMATCH_ERR,
|
|
|
|
QUOTA_EXCEEDED_ERR,
|
|
|
|
TIMEOUT_ERR,
|
|
|
|
INVALID_NODE_TYPE_ERR,
|
|
|
|
DATA_CLONE_ERR,
|
|
|
|
}),
|
|
|
|
)
|
2021-01-14 08:08:49 -05:00
|
|
|
) {
|
|
|
|
const desc = { value, enumerable: true };
|
2021-07-03 15:32:28 -04:00
|
|
|
ObjectDefineProperty(DOMException, key, desc);
|
|
|
|
ObjectDefineProperty(DOMException.prototype, key, desc);
|
2020-04-11 11:42:02 -04:00
|
|
|
}
|
2021-01-09 01:27:46 -05:00
|
|
|
|
2021-07-03 15:32:28 -04:00
|
|
|
window.__bootstrap.domException = { DOMException };
|
2021-01-09 01:27:46 -05:00
|
|
|
})(this);
|