Skip to content

Commit

Permalink
Implement JsPromise wrapper (#2758)
Browse files Browse the repository at this point in the history
This Pull Request closes #2687.

It changes the following:

- Adds a `JsPromise` wrapper to manipulate promises from Rust.
- Cleans up the `promise` module to ease the integration of `JsPromise` with it.

cc @lastmjs
  • Loading branch information
jedel1043 committed Mar 29, 2023
1 parent f672456 commit bf47815
Show file tree
Hide file tree
Showing 10 changed files with 1,631 additions and 738 deletions.
27 changes: 6 additions & 21 deletions boa_engine/src/builtins/async_generator/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -114,12 +114,7 @@ impl AsyncGenerator {

// 2. Let promiseCapability be ! NewPromiseCapability(%Promise%).
let promise_capability = PromiseCapability::new(
&context
.intrinsics()
.constructors()
.promise()
.constructor()
.into(),
&context.intrinsics().constructors().promise().constructor(),
context,
)
.expect("cannot fail with promise constructor");
Expand Down Expand Up @@ -207,12 +202,7 @@ impl AsyncGenerator {

// 2. Let promiseCapability be ! NewPromiseCapability(%Promise%).
let promise_capability = PromiseCapability::new(
&context
.intrinsics()
.constructors()
.promise()
.constructor()
.into(),
&context.intrinsics().constructors().promise().constructor(),
context,
)
.expect("cannot fail with promise constructor");
Expand Down Expand Up @@ -295,12 +285,7 @@ impl AsyncGenerator {

// 2. Let promiseCapability be ! NewPromiseCapability(%Promise%).
let promise_capability = PromiseCapability::new(
&context
.intrinsics()
.constructors()
.promise()
.constructor()
.into(),
&context.intrinsics().constructors().promise().constructor(),
context,
)
.expect("cannot fail with promise constructor");
Expand Down Expand Up @@ -557,7 +542,7 @@ impl AsyncGenerator {

// 6. Let promise be ? PromiseResolve(%Promise%, completion.[[Value]]).
let promise_completion = Promise::promise_resolve(
context.intrinsics().constructors().promise().constructor(),
&context.intrinsics().constructors().promise().constructor(),
value,
context,
);
Expand Down Expand Up @@ -653,8 +638,8 @@ impl AsyncGenerator {
// 11. Perform PerformPromiseThen(promise, onFulfilled, onRejected).
Promise::perform_promise_then(
&promise,
&on_fulfilled.into(),
&on_rejected.into(),
Some(on_fulfilled),
Some(on_rejected),
None,
context,
);
Expand Down
27 changes: 6 additions & 21 deletions boa_engine/src/builtins/iterable/async_from_sync_iterator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -100,12 +100,7 @@ impl AsyncFromSyncIterator {

// 3. Let promiseCapability be ! NewPromiseCapability(%Promise%).
let promise_capability = PromiseCapability::new(
&context
.intrinsics()
.constructors()
.promise()
.constructor()
.into(),
&context.intrinsics().constructors().promise().constructor(),
context,
)
.expect("cannot fail with promise constructor");
Expand Down Expand Up @@ -145,12 +140,7 @@ impl AsyncFromSyncIterator {

// 3. Let promiseCapability be ! NewPromiseCapability(%Promise%).
let promise_capability = PromiseCapability::new(
&context
.intrinsics()
.constructors()
.promise()
.constructor()
.into(),
&context.intrinsics().constructors().promise().constructor(),
context,
)
.expect("cannot fail with promise constructor");
Expand Down Expand Up @@ -243,12 +233,7 @@ impl AsyncFromSyncIterator {

// 3. Let promiseCapability be ! NewPromiseCapability(%Promise%).
let promise_capability = PromiseCapability::new(
&context
.intrinsics()
.constructors()
.promise()
.constructor()
.into(),
&context.intrinsics().constructors().promise().constructor(),
context,
)
.expect("cannot fail with promise constructor");
Expand Down Expand Up @@ -348,7 +333,7 @@ impl AsyncFromSyncIterator {

// 6. Let valueWrapper be Completion(PromiseResolve(%Promise%, value)).
let value_wrapper = Promise::promise_resolve(
context.intrinsics().constructors().promise().constructor(),
&context.intrinsics().constructors().promise().constructor(),
value,
context,
);
Expand Down Expand Up @@ -381,8 +366,8 @@ impl AsyncFromSyncIterator {
// 11. Perform PerformPromiseThen(valueWrapper, onFulfilled, undefined, promiseCapability).
Promise::perform_promise_then(
&value_wrapper,
&on_fulfilled.into(),
&JsValue::Undefined,
Some(on_fulfilled),
None,
Some(promise_capability.clone()),
context,
);
Expand Down
Loading

0 comments on commit bf47815

Please sign in to comment.