Repository navigation
Throw DOMExceptions from Rust JSG #7951
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
Draft
bartlomieju
wants to merge
7
commits into
main
Choose a base branch
from
biwanczuk/rust-jsg-domexception
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
6455333
Throw DOMExceptions from Rust JSG
bartlomieju 31bdc38
Merge remote-tracking branch 'origin/main' into biwanczuk/rust-jsg-do…
bartlomieju 3ccd0e9
Keep terminations and DOMException names when throwing from Rust JSG
bartlomieju b0ace60
Merge remote-tracking branch 'origin/main' into biwanczuk/rust-jsg-do…
bartlomieju 8391307
Return a JsObject from jsg::Lock::domException()
bartlomieju 1f219c1
Brand DOMExceptions and create and recognize them with V8 alone
bartlomieju a2a09fb
Implement the global DOMException in the per-isolate bootstrap
bartlomieju File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,152 @@ | ||
| 'use strict'; | ||
|
|
||
| // The global DOMException (https://webidl.spec.whatwg.org/#idl-DOMException). | ||
| // | ||
| // A DOMException looks as the C++ JSG class's instances do (src/workerd/jsg/dom-exception.h): | ||
| // own read-only `code`, `name` and `message` properties, an enumerable own `stack`, and the | ||
| // legacy code constants on both the constructor and the prototype. Native code creates | ||
| // DOMExceptions without running this constructor (jsg::newDOMException()), over the prototype | ||
| // that install() hands it, and recognizes them by the brand that utils.initDOMException() sets. | ||
| // This constructor sets the same brand, so utils.isDOMException() is true for either kind, and | ||
| // for a subclass instance. | ||
| // | ||
| // DOMException is a function rather than a class so that its `prototype` property is | ||
| // writable, as a JSG class's is unless the immutable_api_prototypes flag is set. | ||
|
|
||
| const { | ||
| ErrorCaptureStackTrace, | ||
| ErrorPrototype, | ||
| ObjectCreate, | ||
| ObjectDefineProperty, | ||
| ObjectFreeze, | ||
| ObjectGetOwnPropertyDescriptor, | ||
| ObjectGetPrototypeOf, | ||
| SymbolFor, | ||
| SymbolToStringTag, | ||
| TypeError, | ||
| } = primordials; | ||
|
|
||
| // The legacy codes, in the order JSG defines the constants | ||
| // (JSG_DOM_EXCEPTION_FOR_EACH_ERROR_NAME). | ||
| const LEGACY_CODES: readonly (readonly [string, number])[] = [ | ||
| ['INDEX_SIZE_ERR', 1], | ||
| ['DOMSTRING_SIZE_ERR', 2], | ||
| ['HIERARCHY_REQUEST_ERR', 3], | ||
| ['WRONG_DOCUMENT_ERR', 4], | ||
| ['INVALID_CHARACTER_ERR', 5], | ||
| ['NO_DATA_ALLOWED_ERR', 6], | ||
| ['NO_MODIFICATION_ALLOWED_ERR', 7], | ||
| ['NOT_FOUND_ERR', 8], | ||
| ['NOT_SUPPORTED_ERR', 9], | ||
| ['INUSE_ATTRIBUTE_ERR', 10], | ||
| ['INVALID_STATE_ERR', 11], | ||
| ['SYNTAX_ERR', 12], | ||
| ['INVALID_MODIFICATION_ERR', 13], | ||
| ['NAMESPACE_ERR', 14], | ||
| ['INVALID_ACCESS_ERR', 15], | ||
| ['VALIDATION_ERR', 16], | ||
| ['TYPE_MISMATCH_ERR', 17], | ||
| ['SECURITY_ERR', 18], | ||
| ['NETWORK_ERR', 19], | ||
| ['ABORT_ERR', 20], | ||
| ['URL_MISMATCH_ERR', 21], | ||
| ['QUOTA_EXCEEDED_ERR', 22], | ||
| ['TIMEOUT_ERR', 23], | ||
| ['INVALID_NODE_TYPE_ERR', 24], | ||
| ['DATA_CLONE_ERR', 25], | ||
| ]; | ||
|
|
||
| // The message and name are converted as Web IDL converts a DOMString, which a template literal | ||
| // does too: a Symbol throws a TypeError. | ||
| function DOMException(this: object, ...args: unknown[]): void { | ||
| if (new.target === undefined) { | ||
| throw new TypeError( | ||
| "Failed to construct 'DOMException': Please use the 'new' operator, this object constructor cannot be called as a function." | ||
| ); | ||
| } | ||
| const message = args[0] === undefined ? '' : `${args[0] as string}`; | ||
| const name = args[1] === undefined ? 'Error' : `${args[1] as string}`; | ||
| utils.initDOMException(this, name, message); | ||
| // Leaves out this constructor's frame, but not a subclass constructor's. | ||
| ErrorCaptureStackTrace(this, DOMException); | ||
| // Error properties are not enumerable, but a DOMException's always have been. | ||
| ObjectDefineProperty(this, 'stack', { __proto__: null, enumerable: true }); | ||
| } | ||
|
|
||
| // Defines `key` on `target` with the attributes it has on `source`, which JSG chooses by | ||
| // compatibility flags, and `value`. | ||
| function copyProperty( | ||
| target: object, | ||
| source: object, | ||
| key: string | symbol, | ||
| value: unknown | ||
| ): void { | ||
| const descriptor = ObjectGetOwnPropertyDescriptor(source, key); | ||
| if (descriptor === undefined) { | ||
| throw new TypeError(`DOMException has no ${String(key)} to copy`); | ||
| } | ||
| ObjectDefineProperty(target, key, { ...descriptor, __proto__: null, value }); | ||
| } | ||
|
|
||
| // Gives DOMException the properties of `native`, the C++ class it replaces, with their | ||
| // attributes and in their order. | ||
| function setUpClass(native: Function): void { | ||
| const nativePrototype = native.prototype as object; | ||
| copyProperty(DOMException, native, 'length', 0); | ||
| copyProperty(DOMException, native, 'name', 'DOMException'); | ||
|
|
||
| const prototype = ObjectCreate(ErrorPrototype) as object; | ||
| for (let i = 0; i < LEGACY_CODES.length; i++) { | ||
| const [name, code] = LEGACY_CODES[i] as readonly [string, number]; | ||
| copyProperty(DOMException, native, name, code); | ||
| copyProperty(prototype, nativePrototype, name, code); | ||
| } | ||
| copyProperty(prototype, nativePrototype, 'constructor', DOMException); | ||
| // Present under the set_tostring_tag flag. | ||
| if ( | ||
| ObjectGetOwnPropertyDescriptor(nativePrototype, SymbolToStringTag) !== | ||
| undefined | ||
| ) { | ||
| copyProperty(prototype, nativePrototype, SymbolToStringTag, 'DOMException'); | ||
| } | ||
| // Tools recognize a runtime class by this symbol. | ||
| const internalClass = SymbolFor('cloudflare:internal-class'); | ||
| copyProperty(prototype, nativePrototype, internalClass, internalClass); | ||
| // util.inspect() formats an instance of a runtime class through this symbol. | ||
| const inspect = utils.getApiSymbol('kResourceTypeInspect'); | ||
| copyProperty( | ||
| prototype, | ||
| nativePrototype, | ||
| inspect, | ||
| ObjectFreeze({ __proto__: null }) | ||
| ); | ||
|
|
||
| // Not writable under the immutable_api_prototypes flag. | ||
| copyProperty(DOMException, native, 'prototype', prototype); | ||
| } | ||
|
|
||
| // Replaces the C++ class as the global DOMException, keeping the property where JSG defines it | ||
| // (on the global object's prototype) and its attributes, and has native code create instances | ||
| // of this class from now on. | ||
| function install(): void { | ||
| let owner: object | null = globalThis; | ||
| while (owner !== null) { | ||
| const descriptor = ObjectGetOwnPropertyDescriptor(owner, 'DOMException'); | ||
| if (descriptor !== undefined) { | ||
| setUpClass(descriptor.value as Function); | ||
| ObjectDefineProperty(owner, 'DOMException', { | ||
| ...descriptor, | ||
| __proto__: null, | ||
| value: DOMException, | ||
| }); | ||
| utils.setDOMExceptionPrototype(DOMException.prototype as object); | ||
| return; | ||
| } | ||
| owner = ObjectGetPrototypeOf(owner) as object | null; | ||
| } | ||
| throw new TypeError('the global object has no DOMException'); | ||
| } | ||
|
|
||
| module.exports = { | ||
| install, | ||
| }; | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I know you mentioned that you're still iterating.. but I'm curious... why a function and not a
class DOMException?