mirror of
https://github.com/servo/servo.git
synced 2025-06-06 16:45:39 +00:00
* Re-implement readablestream: basics and default reader and controller --------- Co-authored-by: Jason Tsai <jason@pews.dev> Signed-off-by: Wu Wayne <yuweiwu@pm.me> Add remaining WebIDLs of ReadableStream (#32605) * Add Reader's WebIDL files * Add necessary methods in ReadableStream.webidl Signed-off-by: Wu Wayne <yuweiwu@pm.me> Create safe wrapper for JSFunctions (#32620) * Create safe wrapper for JSFunctions Signed-off-by: Bentaimia Haddadi <haddadi.taym@gmail.com> * Add assert to check if the name ends in a null character Signed-off-by: Bentaimia Haddadi <haddadi.taym@gmail.com> * Create macro to wrap unsafe extern "C" function calls Signed-off-by: Bentaimia Haddadi <haddadi.taym@gmail.com> * Remove WRAPPER_FN Signed-off-by: Bentaimia Haddadi <haddadi.taym@gmail.com> * Add macro example documentation Signed-off-by: Bentaimia Haddadi <haddadi.taym@gmail.com> * Use C-string literals Signed-off-by: Bentaimia Haddadi <haddadi.taym@gmail.com> * Ensure name is Cstr type Signed-off-by: Bentaimia Haddadi <haddadi.taym@gmail.com> * Scope #[allow(unsafe_code)] Signed-off-by: Bentaimia Haddadi <haddadi.taym@gmail.com> --------- Signed-off-by: Bentaimia Haddadi <haddadi.taym@gmail.com> Signed-off-by: Wu Wayne <yuweiwu@pm.me> Start implementation of default controller and reader Start implementation of default controller and reader * implement basic internal slots, with todos Signed-off-by: gterzian <2792687+gterzian@users.noreply.github.com> * enum for controller Signed-off-by: gterzian <2792687+gterzian@users.noreply.github.com> * re-implement native controller methods Signed-off-by: gterzian <2792687+gterzian@users.noreply.github.com> * add calling into pull algo Signed-off-by: gterzian <2792687+gterzian@users.noreply.github.com> * more details on chunk enqueuing Signed-off-by: gterzian <2792687+gterzian@users.noreply.github.com> * add fulfill read request, clean-up warnings Signed-off-by: gterzian <2792687+gterzian@users.noreply.github.com> * read request and reader typing Signed-off-by: gterzian <2792687+gterzian@users.noreply.github.com> * allow for more than one non-native underlying source type Signed-off-by: gterzian <2792687+gterzian@users.noreply.github.com> * add todo for should pull Signed-off-by: gterzian <2792687+gterzian@users.noreply.github.com> * add underlying source dom struct container Signed-off-by: gterzian <2792687+gterzian@users.noreply.github.com> * remove rc around source type * add default controller init in stream constructor * setup source container with prototype of source dict * clean-up docs, dispatch of controller in pull algo call * turn off SM streams * remove prototype setting on underlying source container * fix read request promise resolving * tidy * clean-up js conversions in read req handlers * add queue with sizes concept * use dom in pull promise handlers * Demonstrate using dictionary as callback this object. * move value with size to a struct * fmt * put readable stream state in a cell * nits in expectations * remove allow unroot by passing read result directly to promise resolving * tidy * root default controller inside call_pull_if_needed --------- Signed-off-by: gterzian <2792687+gterzian@users.noreply.github.com> Co-authored-by: Josh Matthews <josh@joshmatthews.net> Signed-off-by: Wu Wayne <yuweiwu@pm.me> ReadableStream: implement Cancel and Locked (#33136) * implement Locked * implement Cancel and close Signed-off-by: Wu Wayne <yuweiwu@pm.me> Add GetPromiseIsHandled and SetAnyPromiseIsHandled to Promise Signed-off-by: Taym <haddadi.taym@gmail.com> mach fmt Signed-off-by: Taym <haddadi.taym@gmail.com> Readablestream default controller: get desired size (#33497) Signed-off-by: gterzian <2792687+gterzian@users.noreply.github.com> stream: implement controller close (#33498) Signed-off-by: gterzian <2792687+gterzian@users.noreply.github.com> implement stream default controller error (#33503) Signed-off-by: gterzian <2792687+gterzian@users.noreply.github.com> Readablestream default controller: enqueue (#33528) * Implement ReadableStreamDefaultControllerMethods::Enqueue Signed-off-by: Wu Wayne <yuweiwu@pm.me> * Add spec comments Signed-off-by: Wu Wayne <yuweiwu@pm.me> --------- Signed-off-by: Wu Wayne <yuweiwu@pm.me> readablestream default controller: fulfill read requests (#33542) Signed-off-by: gterzian <2792687+gterzian@users.noreply.github.com> Fix extract_size_algorithm (#33561) Signed-off-by: Wu Wayne <yuweiwu@pm.me> Readablestream default controller: use strategy size (#33551) * readablestream default controller: use strategy size, fallible enqueue Signed-off-by: gterzian <2792687+gterzian@users.noreply.github.com> docs * readablestream default controller: clear strategy size Signed-off-by: gterzian <2792687+gterzian@users.noreply.github.com> * prevent potential re-borrow panics when calling into the strategy size Signed-off-by: gterzian <2792687+gterzian@users.noreply.github.com> * document readablestream constructor Signed-off-by: gterzian <2792687+gterzian@users.noreply.github.com> --------- Signed-off-by: gterzian <2792687+gterzian@users.noreply.github.com> Readablestream: impl default controller should pull, start algo (#33586) * implement should-pull algo for default controller Signed-off-by: gterzian <2792687+gterzian@users.noreply.github.com> * add start algorithm setup for default controller Signed-off-by: gterzian <2792687+gterzian@users.noreply.github.com> --------- Signed-off-by: gterzian <2792687+gterzian@users.noreply.github.com> implement promise native handling for start and pull algorithms (#33603) Signed-off-by: gterzian <2792687+gterzian@users.noreply.github.com> Implement ReadableStreamDefaultReader (#33160) * Implement ReadableStreamDefaultReader Make the stream mutable readable-stream-reader-generic-release Proper error types when releasing Closed Cancel Signed-off-by: Taym <haddadi.taym@gmail.com> * follow the spec more closely Signed-off-by: Taym <haddadi.taym@gmail.com> --------- Signed-off-by: Taym <haddadi.taym@gmail.com> Implement ReadableStreamDefaultReader read (#34007) * Implement ReadableStreamDefaultReader read Signed-off-by: Taym <haddadi.taym@gmail.com> * Perform readRequest’s error steps with stream.stored_error Signed-off-by: Taym <haddadi.taym@gmail.com> --------- Signed-off-by: Taym <haddadi.taym@gmail.com> Improve ReadableStreamDefaultReader close (#34014) * improve ReadableStreamDefaultReader close Signed-off-by: Taym <haddadi.taym@gmail.com> * remove resolve_closed_promise Signed-off-by: Taym <haddadi.taym@gmail.com> --------- Signed-off-by: Taym <haddadi.taym@gmail.com> Use Rc<Box<[u8]>> for queue to optimize get_in_memory_bytes Signed-off-by: Taym <haddadi.taym@gmail.com> Signed-off-by: gterzian <2792687+gterzian@users.noreply.github.com> Signed-off-by: Taym <haddadi.taym@gmail.com> * Improve read_a_chunk and stop_reading implemntation (#34077) Signed-off-by: Taym <haddadi.taym@gmail.com> Signed-off-by: gterzian <2792687+gterzian@users.noreply.github.com> Signed-off-by: Taym <haddadi.taym@gmail.com> Signed-off-by: Taym Haddadi <haddadi.taym@gmail.com> * Implement ReadableStreamDefaultReader::Constructor (#34056) * Implement ReadableStreamDefaultReader::Constructor Signed-off-by: Taym <haddadi.taym@gmail.com> * make start_reading returns ReadableStreamDefaultReader Signed-off-by: Taym <haddadi.taym@gmail.com> * Fix can_gc Signed-off-by: Taym <haddadi.taym@gmail.com> * Add canGc to ReadableStream::GetReader Signed-off-by: Taym <haddadi.taym@gmail.com> --------- Signed-off-by: Taym <haddadi.taym@gmail.com> Signed-off-by: gterzian <2792687+gterzian@users.noreply.github.com> Signed-off-by: Taym <haddadi.taym@gmail.com> Signed-off-by: Taym Haddadi <haddadi.taym@gmail.com> * Readablestream fix CanGc (#34080) Signed-off-by: Taym <haddadi.taym@gmail.com> Signed-off-by: gterzian <2792687+gterzian@users.noreply.github.com> Signed-off-by: Taym <haddadi.taym@gmail.com> Signed-off-by: Taym Haddadi <haddadi.taym@gmail.com> * correct ReadableStream::error_native implementation and fix clippy warnings (#34088) Signed-off-by: Taym <haddadi.taym@gmail.com> Signed-off-by: Taym Haddadi <haddadi.taym@gmail.com> * turn assertion of stream present on controller on a early return with false (#34097) Signed-off-by: gterzian <2792687+gterzian@users.noreply.github.com> Signed-off-by: Taym <haddadi.taym@gmail.com> Signed-off-by: Taym Haddadi <haddadi.taym@gmail.com> * Fix already mutably borrowed crash (#34105) Signed-off-by: Taym <haddadi.taym@gmail.com> Signed-off-by: Taym Haddadi <haddadi.taym@gmail.com> * Refactor `get_in_memory_bytes` to return `Option<Vec<u8>> and avoid `unreachable!` panic (#34123) Signed-off-by: Taym <haddadi.taym@gmail.com> Signed-off-by: Taym Haddadi <haddadi.taym@gmail.com> * Set ReadableStream ReadableStreamDefaultReader in start_reading (#34125) Signed-off-by: Taym <haddadi.taym@gmail.com> Signed-off-by: Taym Haddadi <haddadi.taym@gmail.com> * Fix Unhandled rejection with value: object `TypeError: stream is not locked` (#34204) Signed-off-by: Taym <haddadi.taym@gmail.com> Signed-off-by: Taym Haddadi <haddadi.taym@gmail.com> * Fix assert!(self.is_readable()) crash in ReadableStream::close (#34207) Signed-off-by: Taym <haddadi.taym@gmail.com> Signed-off-by: Taym Haddadi <haddadi.taym@gmail.com> * fix call to to_js_object in underlying source algos (#34098) Signed-off-by: gterzian <2792687+gterzian@users.noreply.github.com> Signed-off-by: Taym <haddadi.taym@gmail.com> Signed-off-by: Taym Haddadi <haddadi.taym@gmail.com> * do not assume presence of a stream when performing pull steps (#34244) * do not assume presence of a stream when performing pull steps Signed-off-by: gterzian <2792687+gterzian@users.noreply.github.com> * add doc comments Co-authored-by: Taym Haddadi <haddadi.taym@gmail.com> Signed-off-by: Gregory Terzian <2792687+gterzian@users.noreply.github.com> --------- Signed-off-by: gterzian <2792687+gterzian@users.noreply.github.com> Signed-off-by: Gregory Terzian <2792687+gterzian@users.noreply.github.com> Co-authored-by: Taym Haddadi <haddadi.taym@gmail.com> Signed-off-by: Taym <haddadi.taym@gmail.com> Signed-off-by: Taym Haddadi <haddadi.taym@gmail.com> * gracefully handle failure of underlying source algorithms (#34243) Signed-off-by: gterzian <2792687+gterzian@users.noreply.github.com> Signed-off-by: Taym <haddadi.taym@gmail.com> Signed-off-by: Taym Haddadi <haddadi.taym@gmail.com> * ensure result of calling start algo is an object (#34245) Signed-off-by: gterzian <2792687+gterzian@users.noreply.github.com> Signed-off-by: Taym <haddadi.taym@gmail.com> Signed-off-by: Taym Haddadi <haddadi.taym@gmail.com> * return js failed error if underlying source constructor threw (#34246) Signed-off-by: gterzian <2792687+gterzian@users.noreply.github.com> Signed-off-by: Taym <haddadi.taym@gmail.com> Signed-off-by: Taym Haddadi <haddadi.taym@gmail.com> * Use JSVal for ValueWithSize::value (#34259) Signed-off-by: Taym <haddadi.taym@gmail.com> Signed-off-by: Taym Haddadi <haddadi.taym@gmail.com> * fix release reader lock, (#34255) fix setting stream on controller in new, fix matching fallthrough, reduce visibility of controller error method Signed-off-by: gterzian <2792687+gterzian@users.noreply.github.com> Signed-off-by: Taym <haddadi.taym@gmail.com> Signed-off-by: Taym Haddadi <haddadi.taym@gmail.com> * in stream cancel, reject promist if locked (#34271) Signed-off-by: gterzian <2792687+gterzian@users.noreply.github.com> Signed-off-by: Taym <haddadi.taym@gmail.com> Signed-off-by: Taym Haddadi <haddadi.taym@gmail.com> * Fix UnderlyingSourceContainer::call_start_algorithm (#34277) Signed-off-by: Taym <haddadi.taym@gmail.com> Signed-off-by: Taym Haddadi <haddadi.taym@gmail.com> * implement controller cancel steps, fix stream cancel method (#34301) Signed-off-by: gterzian <2792687+gterzian@users.noreply.github.com> Signed-off-by: Taym <haddadi.taym@gmail.com> Signed-off-by: Taym Haddadi <haddadi.taym@gmail.com> * fix conditional in perform pull steps (#34324) Signed-off-by: gterzian <2792687+gterzian@users.noreply.github.com> Signed-off-by: Taym <haddadi.taym@gmail.com> Signed-off-by: Taym Haddadi <haddadi.taym@gmail.com> * set reader closed promise to one resolved with undefined if stream closed on init (#34321) Signed-off-by: gterzian <2792687+gterzian@users.noreply.github.com> Signed-off-by: Taym <haddadi.taym@gmail.com> Signed-off-by: Taym Haddadi <haddadi.taym@gmail.com> * fix init of stream and controller (#34323) Signed-off-by: gterzian <2792687+gterzian@users.noreply.github.com> Signed-off-by: Taym <haddadi.taym@gmail.com> Signed-off-by: Taym Haddadi <haddadi.taym@gmail.com> * Stream: Fix reborrow in controller enqueue, and fix error and exception handling. (#34338) * fix re-borrow in controller enqueue Signed-off-by: gterzian <2792687+gterzian@users.noreply.github.com> * do not call to_jsval on JSFailed error in enqueue Signed-off-by: gterzian <2792687+gterzian@users.noreply.github.com> * fix error and exception handling in controller enqueue Signed-off-by: gterzian <2792687+gterzian@users.noreply.github.com> * remove TODO about correctness of stored error, since this was done as part of the switch to a js val. Signed-off-by: gterzian <2792687+gterzian@users.noreply.github.com> --------- Signed-off-by: gterzian <2792687+gterzian@users.noreply.github.com> Signed-off-by: Taym <haddadi.taym@gmail.com> Signed-off-by: Taym Haddadi <haddadi.taym@gmail.com> * Stream: Fix incorrect "this" object in underlying source callbacks (#34368) * in controller close, throw type error if stream cannot be closed Signed-off-by: gterzian <2792687+gterzian@users.noreply.github.com> * store original js object for underlying source, for use as this object in callbacks Signed-off-by: gterzian <2792687+gterzian@users.noreply.github.com> --------- Signed-off-by: gterzian <2792687+gterzian@users.noreply.github.com> Signed-off-by: Taym <haddadi.taym@gmail.com> Signed-off-by: Taym Haddadi <haddadi.taym@gmail.com> * fix conditional logic in enqueue to ensure pull is called into (#34375) Signed-off-by: gterzian <2792687+gterzian@users.noreply.github.com> Signed-off-by: Taym <haddadi.taym@gmail.com> Signed-off-by: Taym Haddadi <haddadi.taym@gmail.com> * Stream: Fix bytelength queueing strategy (#34376) * fix handling of value that is not an object in bytelength queuing strategy Signed-off-by: gterzian <2792687+gterzian@users.noreply.github.com> * return type error if strategy size call fails, to prevent panic because no exception is pending Signed-off-by: gterzian <2792687+gterzian@users.noreply.github.com> --------- Signed-off-by: gterzian <2792687+gterzian@users.noreply.github.com> Signed-off-by: Taym <haddadi.taym@gmail.com> Signed-off-by: Taym Haddadi <haddadi.taym@gmail.com> * set correct default count queuing size strategy (#34389) Signed-off-by: gterzian <2792687+gterzian@users.noreply.github.com> Signed-off-by: Taym <haddadi.taym@gmail.com> Signed-off-by: Taym Haddadi <haddadi.taym@gmail.com> * use proto in stream constructor (#34441) Signed-off-by: gterzian <2792687+gterzian@users.noreply.github.com> Signed-off-by: Taym <haddadi.taym@gmail.com> Signed-off-by: Taym Haddadi <haddadi.taym@gmail.com> * fix edge cases in get_desired_size (#34440) Signed-off-by: gterzian <2792687+gterzian@users.noreply.github.com> Signed-off-by: Taym <haddadi.taym@gmail.com> Signed-off-by: Taym Haddadi <haddadi.taym@gmail.com> * Stream: fix algo and strategy calls error handling. (#34424) * fix error handling in cancel steps Signed-off-by: gterzian <2792687+gterzian@users.noreply.github.com> * in pull steps, reject promise if pull algo throws Signed-off-by: gterzian <2792687+gterzian@users.noreply.github.com> * if start algorithm fails, rethrow the error Signed-off-by: gterzian <2792687+gterzian@users.noreply.github.com> * when the strategy size fails, directly get the pending exception and use it to error the stream Signed-off-by: gterzian <2792687+gterzian@users.noreply.github.com> * add error handling to enqueue value with size Signed-off-by: gterzian <2792687+gterzian@users.noreply.github.com> * when enqueueing a value errors, ensure we error and stream with the same error used to throw an exception Signed-off-by: gterzian <2792687+gterzian@users.noreply.github.com> --------- Signed-off-by: gterzian <2792687+gterzian@users.noreply.github.com> Signed-off-by: Taym <haddadi.taym@gmail.com> Signed-off-by: Taym Haddadi <haddadi.taym@gmail.com> * fix native use of streams (#34468) Signed-off-by: gterzian <2792687+gterzian@users.noreply.github.com> Signed-off-by: Taym <haddadi.taym@gmail.com> Signed-off-by: Taym Haddadi <haddadi.taym@gmail.com> * Implement readablestreamdefaulttee (#34405) * Implement readablestreamdefaulttee Signed-off-by: Taym <haddadi.taym@gmail.com> * Create UnderlyingSourceType::Tee each stream Signed-off-by: Taym <haddadi.taym@gmail.com> * Use Dom instead of DomRoot Signed-off-by: Taym <haddadi.taym@gmail.com> * Queue a microtask for readRequest chunk steps Signed-off-by: Taym <haddadi.taym@gmail.com> * fix create_readable_stream Signed-off-by: Taym <haddadi.taym@gmail.com> * Remove unnecessary Rc Signed-off-by: Taym <haddadi.taym@gmail.com> * Use correct doc link Signed-off-by: Taym <haddadi.taym@gmail.com> * Add #[allow(crown::unrooted_must_root)] Signed-off-by: Taym <haddadi.taym@gmail.com> * Fix crash in ClosedPromiseRejectionHandler Signed-off-by: Taym <haddadi.taym@gmail.com> * reflect TeeReadRequest and TeeUnderlyingSource Signed-off-by: Taym <haddadi.taym@gmail.com> * fix can_gc Signed-off-by: Taym <haddadi.taym@gmail.com> * reflect tee source, and fix use of mutable dom for tee source and request Signed-off-by: gterzian <2792687+gterzian@users.noreply.github.com> * Fix typo that resolves multiple test failures in 'Tee' tests Signed-off-by: Taym <haddadi.taym@gmail.com> * Fix readable-streams/tee.any.js test Signed-off-by: Taym <haddadi.taym@gmail.com> --------- Signed-off-by: Taym <haddadi.taym@gmail.com> Signed-off-by: gterzian <2792687+gterzian@users.noreply.github.com> Co-authored-by: gterzian <2792687+gterzian@users.noreply.github.com> Signed-off-by: Taym <haddadi.taym@gmail.com> Signed-off-by: Taym Haddadi <haddadi.taym@gmail.com> * Align ReadableStreamDefaultReader with spec and fix additional tests in default-reader.any.js (#34531) And fix crate::DomTypeHolder usage * Align ReadableStreamDefaultReader with spec and fix additional tests in default-reader.any.js Signed-off-by: Taym <haddadi.taym@gmail.com> * make reader rooted in Constructor and acquire_default_reader Signed-off-by: Taym <haddadi.taym@gmail.com> * Remove spaces Signed-off-by: Taym <haddadi.taym@gmail.com> --------- Signed-off-by: Taym <haddadi.taym@gmail.com> Signed-off-by: Taym Haddadi <haddadi.taym@gmail.com> * Streams: fetch stream chunks should be uint8 arrays (#34553) * fetch stream chunks should be uint8 arrays Signed-off-by: gterzian <2792687+gterzian@users.noreply.github.com> * fix clippy Signed-off-by: Taym Haddadi <haddadi.taym@gmail.com> --------- Signed-off-by: gterzian <2792687+gterzian@users.noreply.github.com> Signed-off-by: Taym Haddadi <haddadi.taym@gmail.com> Co-authored-by: Taym Haddadi <haddadi.taym@gmail.com> Signed-off-by: Taym <haddadi.taym@gmail.com> Signed-off-by: Taym Haddadi <haddadi.taym@gmail.com> * Update wpt test for ReadableStream reimplementation (#34579) Signed-off-by: Taym <haddadi.taym@gmail.com> Signed-off-by: Taym Haddadi <haddadi.taym@gmail.com> * Fix ignore_malloc_size_of in readablestream tee (#34578) Signed-off-by: Taym <haddadi.taym@gmail.com> Signed-off-by: Taym Haddadi <haddadi.taym@gmail.com> * Remove incorrect use of handle array, fail test safely by giving only one reason (#34560) Signed-off-by: gterzian <2792687+gterzian@users.noreply.github.com> Signed-off-by: Taym <haddadi.taym@gmail.com> Signed-off-by: Taym Haddadi <haddadi.taym@gmail.com> * Update more wpt test for ReadableStream reimplementation (#34598) Signed-off-by: Taym <haddadi.taym@gmail.com> Signed-off-by: Taym Haddadi <haddadi.taym@gmail.com> * Fix doc and rename Tee to DefaultTee (#34612) Signed-off-by: Taym <haddadi.taym@gmail.com> Signed-off-by: Taym Haddadi <haddadi.taym@gmail.com> * fix: Address review comments Signed-off-by: Taym <haddadi.taym@gmail.com> Signed-off-by: Taym Haddadi <haddadi.taym@gmail.com> * Update response-stream-with-broken-then.any.js.ini test expectation Signed-off-by: Taym Haddadi <haddadi.taym@gmail.com> * fix reflect_dom_object can_gc Signed-off-by: Taym Haddadi <haddadi.taym@gmail.com> * Fix compositeReason for DefaultTeeUnderlyingSource (#34627) * Fix compositeReason for DefaultTeeUnderlyingSource Signed-off-by: Taym Haddadi <haddadi.taym@gmail.com> * Update test Signed-off-by: Taym Haddadi <haddadi.taym@gmail.com> --------- Signed-off-by: Taym Haddadi <haddadi.taym@gmail.com> * Last fixes stream (#34636) * remove now unsused from_js method of readable stream * fix documenation of error steps * return type error instread of panicking on a todo, when trying to construct a stream of type bytes Signed-off-by: gterzian <2792687+gterzian@users.noreply.github.com> --------- Signed-off-by: Gregory Terzian <2792687+gterzian@users.noreply.github.com> * fix crown rooting related errors (#34662) Signed-off-by: Gregory Terzian <2792687+gterzian@users.noreply.github.com> --------- Signed-off-by: Taym <haddadi.taym@gmail.com> Signed-off-by: gterzian <2792687+gterzian@users.noreply.github.com> Signed-off-by: Taym Haddadi <haddadi.taym@gmail.com> Signed-off-by: Gregory Terzian <2792687+gterzian@users.noreply.github.com> Co-authored-by: Wu Wayne <yuweiwu@pm.me> Co-authored-by: Taym Haddadi <haddadi.taym@gmail.com>
376 lines
13 KiB
Rust
376 lines
13 KiB
Rust
/* This Source Code Form is subject to the terms of the Mozilla Public
|
|
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
* file, You can obtain one at https://mozilla.org/MPL/2.0/. */
|
|
|
|
//! Native representation of JS Promise values.
|
|
//!
|
|
//! This implementation differs from the traditional Rust DOM object, because the reflector
|
|
//! is provided by SpiderMonkey and has no knowledge of an associated native representation
|
|
//! (ie. dom::Promise). This means that native instances use native reference counting (Rc)
|
|
//! to ensure that no memory is leaked, which means that there can be multiple instances of
|
|
//! native Promise values that refer to the same JS value yet are distinct native objects
|
|
//! (ie. address equality for the native objects is meaningless).
|
|
|
|
use std::ptr;
|
|
use std::rc::Rc;
|
|
|
|
use dom_struct::dom_struct;
|
|
use js::conversions::ToJSValConvertible;
|
|
use js::jsapi::{
|
|
AddRawValueRoot, CallArgs, GetFunctionNativeReserved, Heap, JSAutoRealm, JSContext, JSObject,
|
|
JS_ClearPendingException, JS_GetFunctionObject, JS_NewFunction, NewFunctionWithReserved,
|
|
PromiseState, PromiseUserInputEventHandlingState, RemoveRawValueRoot,
|
|
SetFunctionNativeReserved,
|
|
};
|
|
use js::jsval::{Int32Value, JSVal, ObjectValue, UndefinedValue};
|
|
use js::rust::wrappers::{
|
|
AddPromiseReactions, CallOriginalPromiseReject, CallOriginalPromiseResolve,
|
|
GetPromiseIsHandled, GetPromiseState, IsPromiseObject, NewPromiseObject, RejectPromise,
|
|
ResolvePromise, SetAnyPromiseIsHandled, SetPromiseUserInputEventHandlingState,
|
|
};
|
|
use js::rust::{HandleObject, HandleValue, MutableHandleObject, Runtime};
|
|
|
|
use crate::dom::bindings::conversions::root_from_object;
|
|
use crate::dom::bindings::error::{Error, Fallible};
|
|
use crate::dom::bindings::reflector::{DomObject, MutDomObject, Reflector};
|
|
use crate::dom::bindings::settings_stack::AutoEntryScript;
|
|
use crate::dom::globalscope::GlobalScope;
|
|
use crate::dom::promisenativehandler::PromiseNativeHandler;
|
|
use crate::realms::{enter_realm, AlreadyInRealm, InRealm};
|
|
use crate::script_runtime::{CanGc, JSContext as SafeJSContext};
|
|
use crate::script_thread::ScriptThread;
|
|
|
|
#[dom_struct]
|
|
#[crown::unrooted_must_root_lint::allow_unrooted_in_rc]
|
|
pub struct Promise {
|
|
reflector: Reflector,
|
|
/// Since Promise values are natively reference counted without the knowledge of
|
|
/// the SpiderMonkey GC, an explicit root for the reflector is stored while any
|
|
/// native instance exists. This ensures that the reflector will never be GCed
|
|
/// while native code could still interact with its native representation.
|
|
#[ignore_malloc_size_of = "SM handles JS values"]
|
|
permanent_js_root: Heap<JSVal>,
|
|
}
|
|
|
|
/// Private helper to enable adding new methods to `Rc<Promise>`.
|
|
trait PromiseHelper {
|
|
fn initialize(&self, cx: SafeJSContext);
|
|
}
|
|
|
|
impl PromiseHelper for Rc<Promise> {
|
|
#[allow(unsafe_code)]
|
|
fn initialize(&self, cx: SafeJSContext) {
|
|
let obj = self.reflector().get_jsobject();
|
|
self.permanent_js_root.set(ObjectValue(*obj));
|
|
unsafe {
|
|
assert!(AddRawValueRoot(
|
|
*cx,
|
|
self.permanent_js_root.get_unsafe(),
|
|
c"Promise::root".as_ptr(),
|
|
));
|
|
}
|
|
}
|
|
}
|
|
|
|
impl Drop for Promise {
|
|
#[allow(unsafe_code)]
|
|
fn drop(&mut self) {
|
|
unsafe {
|
|
let object = self.permanent_js_root.get().to_object();
|
|
assert!(!object.is_null());
|
|
let cx = Runtime::get();
|
|
assert!(!cx.is_null());
|
|
RemoveRawValueRoot(cx, self.permanent_js_root.get_unsafe());
|
|
}
|
|
}
|
|
}
|
|
|
|
impl Promise {
|
|
pub fn new(global: &GlobalScope, can_gc: CanGc) -> Rc<Promise> {
|
|
let realm = enter_realm(global);
|
|
let comp = InRealm::Entered(&realm);
|
|
Promise::new_in_current_realm(comp, can_gc)
|
|
}
|
|
|
|
pub fn new_in_current_realm(_comp: InRealm, can_gc: CanGc) -> Rc<Promise> {
|
|
let cx = GlobalScope::get_cx();
|
|
rooted!(in(*cx) let mut obj = ptr::null_mut::<JSObject>());
|
|
Promise::create_js_promise(cx, obj.handle_mut(), can_gc);
|
|
Promise::new_with_js_promise(obj.handle(), cx)
|
|
}
|
|
|
|
#[allow(unsafe_code)]
|
|
pub fn duplicate(&self) -> Rc<Promise> {
|
|
let cx = GlobalScope::get_cx();
|
|
Promise::new_with_js_promise(self.reflector().get_jsobject(), cx)
|
|
}
|
|
|
|
#[allow(unsafe_code, crown::unrooted_must_root)]
|
|
pub fn new_with_js_promise(obj: HandleObject, cx: SafeJSContext) -> Rc<Promise> {
|
|
unsafe {
|
|
assert!(IsPromiseObject(obj));
|
|
let promise = Promise {
|
|
reflector: Reflector::new(),
|
|
permanent_js_root: Heap::default(),
|
|
};
|
|
let promise = Rc::new(promise);
|
|
promise.init_reflector(obj.get());
|
|
promise.initialize(cx);
|
|
promise
|
|
}
|
|
}
|
|
|
|
#[allow(unsafe_code)]
|
|
// The apparently-unused CanGc parameter reflects the fact that the JS API calls
|
|
// like JS_NewFunction can trigger a GC.
|
|
fn create_js_promise(cx: SafeJSContext, mut obj: MutableHandleObject, _can_gc: CanGc) {
|
|
unsafe {
|
|
let do_nothing_func = JS_NewFunction(
|
|
*cx,
|
|
Some(do_nothing_promise_executor),
|
|
/* nargs = */ 2,
|
|
/* flags = */ 0,
|
|
ptr::null(),
|
|
);
|
|
assert!(!do_nothing_func.is_null());
|
|
rooted!(in(*cx) let do_nothing_obj = JS_GetFunctionObject(do_nothing_func));
|
|
assert!(!do_nothing_obj.is_null());
|
|
obj.set(NewPromiseObject(*cx, do_nothing_obj.handle()));
|
|
assert!(!obj.is_null());
|
|
let is_user_interacting = if ScriptThread::is_user_interacting() {
|
|
PromiseUserInputEventHandlingState::HadUserInteractionAtCreation
|
|
} else {
|
|
PromiseUserInputEventHandlingState::DidntHaveUserInteractionAtCreation
|
|
};
|
|
SetPromiseUserInputEventHandlingState(obj.handle(), is_user_interacting);
|
|
}
|
|
}
|
|
|
|
#[allow(crown::unrooted_must_root, unsafe_code)]
|
|
pub fn new_resolved(
|
|
global: &GlobalScope,
|
|
cx: SafeJSContext,
|
|
value: HandleValue,
|
|
) -> Fallible<Rc<Promise>> {
|
|
let _ac = JSAutoRealm::new(*cx, global.reflector().get_jsobject().get());
|
|
rooted!(in(*cx) let p = unsafe { CallOriginalPromiseResolve(*cx, value) });
|
|
assert!(!p.handle().is_null());
|
|
Ok(Promise::new_with_js_promise(p.handle(), cx))
|
|
}
|
|
|
|
#[allow(crown::unrooted_must_root, unsafe_code)]
|
|
pub fn new_rejected(
|
|
global: &GlobalScope,
|
|
cx: SafeJSContext,
|
|
value: HandleValue,
|
|
) -> Fallible<Rc<Promise>> {
|
|
let _ac = JSAutoRealm::new(*cx, global.reflector().get_jsobject().get());
|
|
rooted!(in(*cx) let p = unsafe { CallOriginalPromiseReject(*cx, value) });
|
|
assert!(!p.handle().is_null());
|
|
Ok(Promise::new_with_js_promise(p.handle(), cx))
|
|
}
|
|
|
|
#[allow(unsafe_code)]
|
|
pub fn resolve_native<T>(&self, val: &T)
|
|
where
|
|
T: ToJSValConvertible,
|
|
{
|
|
let cx = GlobalScope::get_cx();
|
|
let _ac = enter_realm(self);
|
|
rooted!(in(*cx) let mut v = UndefinedValue());
|
|
unsafe {
|
|
val.to_jsval(*cx, v.handle_mut());
|
|
}
|
|
self.resolve(cx, v.handle());
|
|
}
|
|
|
|
#[allow(crown::unrooted_must_root, unsafe_code)]
|
|
pub fn resolve(&self, cx: SafeJSContext, value: HandleValue) {
|
|
unsafe {
|
|
if !ResolvePromise(*cx, self.promise_obj(), value) {
|
|
JS_ClearPendingException(*cx);
|
|
}
|
|
}
|
|
}
|
|
|
|
#[allow(unsafe_code)]
|
|
pub fn reject_native<T>(&self, val: &T)
|
|
where
|
|
T: ToJSValConvertible,
|
|
{
|
|
let cx = GlobalScope::get_cx();
|
|
let _ac = enter_realm(self);
|
|
rooted!(in(*cx) let mut v = UndefinedValue());
|
|
unsafe {
|
|
val.to_jsval(*cx, v.handle_mut());
|
|
}
|
|
self.reject(cx, v.handle());
|
|
}
|
|
|
|
#[allow(unsafe_code)]
|
|
pub fn reject_error(&self, error: Error) {
|
|
let cx = GlobalScope::get_cx();
|
|
let _ac = enter_realm(self);
|
|
rooted!(in(*cx) let mut v = UndefinedValue());
|
|
unsafe {
|
|
error.to_jsval(*cx, &self.global(), v.handle_mut());
|
|
}
|
|
self.reject(cx, v.handle());
|
|
}
|
|
|
|
#[allow(crown::unrooted_must_root, unsafe_code)]
|
|
pub fn reject(&self, cx: SafeJSContext, value: HandleValue) {
|
|
unsafe {
|
|
if !RejectPromise(*cx, self.promise_obj(), value) {
|
|
JS_ClearPendingException(*cx);
|
|
}
|
|
}
|
|
}
|
|
|
|
#[allow(unsafe_code)]
|
|
pub fn is_fulfilled(&self) -> bool {
|
|
let state = unsafe { GetPromiseState(self.promise_obj()) };
|
|
matches!(state, PromiseState::Rejected | PromiseState::Fulfilled)
|
|
}
|
|
|
|
#[allow(unsafe_code)]
|
|
pub fn is_rejected(&self) -> bool {
|
|
let state = unsafe { GetPromiseState(self.promise_obj()) };
|
|
matches!(state, PromiseState::Rejected)
|
|
}
|
|
|
|
#[allow(unsafe_code)]
|
|
pub fn is_pending(&self) -> bool {
|
|
let state = unsafe { GetPromiseState(self.promise_obj()) };
|
|
matches!(state, PromiseState::Pending)
|
|
}
|
|
|
|
#[allow(unsafe_code)]
|
|
pub fn promise_obj(&self) -> HandleObject {
|
|
let obj = self.reflector().get_jsobject();
|
|
unsafe {
|
|
assert!(IsPromiseObject(obj));
|
|
}
|
|
obj
|
|
}
|
|
|
|
#[allow(unsafe_code)]
|
|
pub fn append_native_handler(
|
|
&self,
|
|
handler: &PromiseNativeHandler,
|
|
_comp: InRealm,
|
|
can_gc: CanGc,
|
|
) {
|
|
let _ais = AutoEntryScript::new(&handler.global());
|
|
let cx = GlobalScope::get_cx();
|
|
rooted!(in(*cx) let resolve_func =
|
|
create_native_handler_function(*cx,
|
|
handler.reflector().get_jsobject(),
|
|
NativeHandlerTask::Resolve,
|
|
can_gc));
|
|
|
|
rooted!(in(*cx) let reject_func =
|
|
create_native_handler_function(*cx,
|
|
handler.reflector().get_jsobject(),
|
|
NativeHandlerTask::Reject,
|
|
can_gc));
|
|
|
|
unsafe {
|
|
let ok = AddPromiseReactions(
|
|
*cx,
|
|
self.promise_obj(),
|
|
resolve_func.handle(),
|
|
reject_func.handle(),
|
|
);
|
|
assert!(ok);
|
|
}
|
|
}
|
|
|
|
#[allow(unsafe_code)]
|
|
pub fn get_promise_is_handled(&self) -> bool {
|
|
unsafe { GetPromiseIsHandled(self.reflector().get_jsobject()) }
|
|
}
|
|
|
|
#[allow(unsafe_code)]
|
|
pub fn set_promise_is_handled(&self) -> bool {
|
|
let cx = GlobalScope::get_cx();
|
|
unsafe { SetAnyPromiseIsHandled(*cx, self.reflector().get_jsobject()) }
|
|
}
|
|
}
|
|
|
|
#[allow(unsafe_code)]
|
|
unsafe extern "C" fn do_nothing_promise_executor(
|
|
_cx: *mut JSContext,
|
|
argc: u32,
|
|
vp: *mut JSVal,
|
|
) -> bool {
|
|
let args = CallArgs::from_vp(vp, argc);
|
|
*args.rval() = UndefinedValue();
|
|
true
|
|
}
|
|
|
|
const SLOT_NATIVEHANDLER: usize = 0;
|
|
const SLOT_NATIVEHANDLER_TASK: usize = 1;
|
|
|
|
#[derive(PartialEq)]
|
|
enum NativeHandlerTask {
|
|
Resolve = 0,
|
|
Reject = 1,
|
|
}
|
|
|
|
#[allow(unsafe_code)]
|
|
unsafe extern "C" fn native_handler_callback(
|
|
cx: *mut JSContext,
|
|
argc: u32,
|
|
vp: *mut JSVal,
|
|
) -> bool {
|
|
let cx = SafeJSContext::from_ptr(cx);
|
|
let in_realm_proof = AlreadyInRealm::assert_for_cx(cx);
|
|
|
|
let args = CallArgs::from_vp(vp, argc);
|
|
rooted!(in(*cx) let v = *GetFunctionNativeReserved(args.callee(), SLOT_NATIVEHANDLER));
|
|
assert!(v.get().is_object());
|
|
|
|
let handler = root_from_object::<PromiseNativeHandler>(v.to_object(), *cx)
|
|
.expect("unexpected value for native handler in promise native handler callback");
|
|
|
|
rooted!(in(*cx) let v = *GetFunctionNativeReserved(args.callee(), SLOT_NATIVEHANDLER_TASK));
|
|
match v.to_int32() {
|
|
v if v == NativeHandlerTask::Resolve as i32 => handler.resolved_callback(
|
|
*cx,
|
|
HandleValue::from_raw(args.get(0)),
|
|
InRealm::Already(&in_realm_proof),
|
|
CanGc::note(),
|
|
),
|
|
v if v == NativeHandlerTask::Reject as i32 => handler.rejected_callback(
|
|
*cx,
|
|
HandleValue::from_raw(args.get(0)),
|
|
InRealm::Already(&in_realm_proof),
|
|
CanGc::note(),
|
|
),
|
|
_ => panic!("unexpected native handler task value"),
|
|
};
|
|
|
|
true
|
|
}
|
|
|
|
#[allow(unsafe_code)]
|
|
// The apparently-unused CanGc argument reflects the fact that the JS API calls
|
|
// like NewFunctionWithReserved can trigger a GC.
|
|
fn create_native_handler_function(
|
|
cx: *mut JSContext,
|
|
holder: HandleObject,
|
|
task: NativeHandlerTask,
|
|
_can_gc: CanGc,
|
|
) -> *mut JSObject {
|
|
unsafe {
|
|
let func = NewFunctionWithReserved(cx, Some(native_handler_callback), 1, 0, ptr::null());
|
|
assert!(!func.is_null());
|
|
|
|
rooted!(in(cx) let obj = JS_GetFunctionObject(func));
|
|
assert!(!obj.is_null());
|
|
SetFunctionNativeReserved(obj.get(), SLOT_NATIVEHANDLER, &ObjectValue(*holder));
|
|
SetFunctionNativeReserved(obj.get(), SLOT_NATIVEHANDLER_TASK, &Int32Value(task as i32));
|
|
obj.get()
|
|
}
|
|
}
|