summaryrefslogtreecommitdiff
path: root/lib/internal/per_context
diff options
context:
space:
mode:
authorAnna Henningsen <anna@addaleax.net>2019-03-07 15:45:31 +0100
committerAnna Henningsen <anna@addaleax.net>2019-03-15 16:54:19 +0100
commitb0de48e85441ff710aab240fdfa8a34adbbee976 (patch)
tree9b0d0ee1ca888a2d0db99d6350044d6095a84570 /lib/internal/per_context
parent7e2088f773d97e00e29cacdc20e1a36b80528be0 (diff)
downloadandroid-node-v8-b0de48e85441ff710aab240fdfa8a34adbbee976.tar.gz
android-node-v8-b0de48e85441ff710aab240fdfa8a34adbbee976.tar.bz2
android-node-v8-b0de48e85441ff710aab240fdfa8a34adbbee976.zip
src,lib: make DOMException available in all Contexts
This allows using `DOMException` from Node.js code for any `vm.Context`. PR-URL: https://github.com/nodejs/node/pull/26497 Reviewed-By: James M Snell <jasnell@gmail.com>
Diffstat (limited to 'lib/internal/per_context')
-rw-r--r--lib/internal/per_context/domexception.js92
1 files changed, 92 insertions, 0 deletions
diff --git a/lib/internal/per_context/domexception.js b/lib/internal/per_context/domexception.js
new file mode 100644
index 0000000000..795acf76c0
--- /dev/null
+++ b/lib/internal/per_context/domexception.js
@@ -0,0 +1,92 @@
+'use strict';
+
+class ERR_INVALID_THIS extends TypeError {
+ constructor(type) {
+ super('Value of "this" must be of ' + type);
+ }
+
+ get code() { return 'ERR_INVALID_THIS'; }
+}
+
+const internalsMap = new WeakMap();
+
+const nameToCodeMap = new Map();
+
+class DOMException extends Error {
+ constructor(message = '', name = 'Error') {
+ super();
+ internalsMap.set(this, {
+ message: `${message}`,
+ name: `${name}`
+ });
+ }
+
+ get name() {
+ const internals = internalsMap.get(this);
+ if (internals === undefined) {
+ throw new ERR_INVALID_THIS('DOMException');
+ }
+ return internals.name;
+ }
+
+ get message() {
+ const internals = internalsMap.get(this);
+ if (internals === undefined) {
+ throw new ERR_INVALID_THIS('DOMException');
+ }
+ return internals.message;
+ }
+
+ get code() {
+ const internals = internalsMap.get(this);
+ if (internals === undefined) {
+ throw new ERR_INVALID_THIS('DOMException');
+ }
+ const code = nameToCodeMap.get(internals.name);
+ return code === undefined ? 0 : code;
+ }
+}
+
+Object.defineProperties(DOMException.prototype, {
+ [Symbol.toStringTag]: { configurable: true, value: 'DOMException' },
+ name: { enumerable: true, configurable: true },
+ message: { enumerable: true, configurable: true },
+ code: { enumerable: true, configurable: true }
+});
+
+for (const [name, codeName, value] of [
+ ['IndexSizeError', 'INDEX_SIZE_ERR', 1],
+ ['DOMStringSizeError', 'DOMSTRING_SIZE_ERR', 2],
+ ['HierarchyRequestError', 'HIERARCHY_REQUEST_ERR', 3],
+ ['WrongDocumentError', 'WRONG_DOCUMENT_ERR', 4],
+ ['InvalidCharacterError', 'INVALID_CHARACTER_ERR', 5],
+ ['NoDataAllowedError', 'NO_DATA_ALLOWED_ERR', 6],
+ ['NoModificationAllowedError', 'NO_MODIFICATION_ALLOWED_ERR', 7],
+ ['NotFoundError', 'NOT_FOUND_ERR', 8],
+ ['NotSupportedError', 'NOT_SUPPORTED_ERR', 9],
+ ['InUseAttributeError', 'INUSE_ATTRIBUTE_ERR', 10],
+ ['InvalidStateError', 'INVALID_STATE_ERR', 11],
+ ['SyntaxError', 'SYNTAX_ERR', 12],
+ ['InvalidModificationError', 'INVALID_MODIFICATION_ERR', 13],
+ ['NamespaceError', 'NAMESPACE_ERR', 14],
+ ['InvalidAccessError', 'INVALID_ACCESS_ERR', 15],
+ ['ValidationError', 'VALIDATION_ERR', 16],
+ ['TypeMismatchError', 'TYPE_MISMATCH_ERR', 17],
+ ['SecurityError', 'SECURITY_ERR', 18],
+ ['NetworkError', 'NETWORK_ERR', 19],
+ ['AbortError', 'ABORT_ERR', 20],
+ ['URLMismatchError', 'URL_MISMATCH_ERR', 21],
+ ['QuotaExceededError', 'QUOTA_EXCEEDED_ERR', 22],
+ ['TimeoutError', 'TIMEOUT_ERR', 23],
+ ['InvalidNodeTypeError', 'INVALID_NODE_TYPE_ERR', 24],
+ ['DataCloneError', 'DATA_CLONE_ERR', 25]
+ // There are some more error names, but since they don't have codes assigned,
+ // we don't need to care about them.
+]) {
+ const desc = { enumerable: true, value };
+ Object.defineProperty(DOMException, codeName, desc);
+ Object.defineProperty(DOMException.prototype, codeName, desc);
+ nameToCodeMap.set(name, value);
+}
+
+exports.DOMException = DOMException;