-
Notifications
You must be signed in to change notification settings - Fork 62
Implement Promise.all #797
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
base: main
Are you sure you want to change the base?
Changes from all commits
4a4f709
e362e8b
565924e
1df4536
e3e5aba
d3741e5
f2272ad
d6edf22
3d32bba
7811f5d
f2439a6
bf3783a
8fcbf6f
493bcde
1a9325a
5180052
65b3f5b
c09da6d
f2f513e
b0bd7ff
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,160 @@ | ||
// 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/. | ||
|
||
use core::ops::{Index, IndexMut}; | ||
|
||
use crate::{ | ||
ecmascript::{ | ||
builtins::{ | ||
Array, promise::Promise, | ||
promise_objects::promise_abstract_operations::promise_capability_records::PromiseCapability, | ||
}, | ||
execution::Agent, | ||
types::{IntoValue, Value}, | ||
}, | ||
engine::context::{Bindable, GcScope, NoGcScope}, | ||
heap::{ | ||
CompactionLists, CreateHeapData, Heap, HeapMarkAndSweep, WorkQueues, indexes::BaseIndex, | ||
}, | ||
}; | ||
|
||
#[derive(Debug, Clone, Copy)] | ||
pub struct PromiseAllRecord<'a> { | ||
pub remaining_unresolved_promise_count: u32, | ||
pub result_array: Array<'a>, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. thought: Here it might be more appropriate to use the Hmmm, |
||
pub promise: Promise<'a>, | ||
} | ||
|
||
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord)] | ||
#[repr(transparent)] | ||
pub struct PromiseAll<'a>(pub(crate) BaseIndex<'a, PromiseAllRecord<'a>>); | ||
|
||
impl<'a> PromiseAllRecord<'a> { | ||
pub(crate) fn on_promise_fufilled( | ||
&mut self, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. issue: Aight, let's move this method to pub(crate) fn on_promise_fufilled(
self,
agent: &mut Agent,
index: u32,
value: Value<'a>,
mut gc: GcScope<'a, '_>,
) {
let promise_all = self.bind(gc.nogc());
let value = value.bind(gc.nogc());
let data = promise_all.get_mut(&mut agent.heap.promise_all_records); // splitting heap borrow
let result_array = data.result_array.bind(gc.nogc()); // note: this copies the handle from the heap to stack; on stack we must always bind to make sure we don't use the handle after GC.
let elements = result_array.as_mut_slice(...arrayheap...); // we want to split the heap borrow so we can get the elements slice as mutable at the same time as we get data as mutable. ArrayHeap is the thing you want to pass here here, IIRC.
elements[index as usize] = Some(value.unbind());
data.remaining_unresolved_promise_count -= 1;
if data.remaining_unresolved_promise_count == 0 {
let capability = ...;
capability.resolve(agent, result_array.into_value().unbind(), gc);
}
} Now everything should be up-and-up from the GC perspective. |
||
agent: &mut Agent, | ||
index: u32, | ||
value: Value<'a>, | ||
mut gc: GcScope<'a, '_>, | ||
) { | ||
value.bind(gc.nogc()); | ||
let elements = self.result_array.as_mut_slice(agent); | ||
elements[index as usize] = Some(value.unbind()); | ||
|
||
self.remaining_unresolved_promise_count -= 1; | ||
if self.remaining_unresolved_promise_count == 0 { | ||
eprintln!("Promise fulfilled: {:#?}", elements); | ||
let capability = PromiseCapability::from_promise(self.promise.unbind(), true); | ||
capability.resolve(agent, self.result_array.into_value().unbind(), gc); | ||
} | ||
} | ||
} | ||
|
||
impl PromiseAll<'_> { | ||
pub(crate) const fn get_index(self) -> usize { | ||
self.0.into_index() | ||
} | ||
} | ||
|
||
impl Index<PromiseAll<'_>> for Agent { | ||
type Output = PromiseAllRecord<'static>; | ||
|
||
fn index(&self, index: PromiseAll) -> &Self::Output { | ||
&self.heap.promise_all_records[index] | ||
} | ||
} | ||
|
||
impl IndexMut<PromiseAll<'_>> for Agent { | ||
fn index_mut(&mut self, index: PromiseAll) -> &mut Self::Output { | ||
&mut self.heap.promise_all_records[index] | ||
} | ||
} | ||
|
||
impl Index<PromiseAll<'_>> for Vec<Option<PromiseAllRecord<'static>>> { | ||
type Output = PromiseAllRecord<'static>; | ||
|
||
fn index(&self, index: PromiseAll) -> &Self::Output { | ||
self.get(index.get_index()) | ||
.expect("PromiseAllRecord out of bounds") | ||
.as_ref() | ||
.expect("PromiseAllRecord slot empty") | ||
} | ||
} | ||
|
||
impl IndexMut<PromiseAll<'_>> for Vec<Option<PromiseAllRecord<'static>>> { | ||
fn index_mut(&mut self, index: PromiseAll) -> &mut Self::Output { | ||
self.get_mut(index.get_index()) | ||
.expect("PromiseAllRecord out of bounds") | ||
.as_mut() | ||
.expect("PromiseAllRecord slot empty") | ||
} | ||
} | ||
|
||
impl HeapMarkAndSweep for PromiseAllRecord<'static> { | ||
fn mark_values(&self, queues: &mut WorkQueues) { | ||
let Self { | ||
remaining_unresolved_promise_count: _, | ||
result_array, | ||
promise, | ||
} = self; | ||
result_array.mark_values(queues); | ||
promise.mark_values(queues); | ||
} | ||
|
||
fn sweep_values(&mut self, compactions: &CompactionLists) { | ||
let Self { | ||
remaining_unresolved_promise_count: _, | ||
result_array, | ||
promise, | ||
} = self; | ||
result_array.sweep_values(compactions); | ||
promise.sweep_values(compactions); | ||
} | ||
} | ||
|
||
unsafe impl Bindable for PromiseAllRecord<'_> { | ||
type Of<'a> = PromiseAllRecord<'a>; | ||
|
||
#[inline(always)] | ||
fn unbind(self) -> Self::Of<'static> { | ||
unsafe { core::mem::transmute::<Self, Self::Of<'static>>(self) } | ||
} | ||
|
||
#[inline(always)] | ||
fn bind<'a>(self, _gc: NoGcScope<'a, '_>) -> Self::Of<'a> { | ||
unsafe { core::mem::transmute::<Self, Self::Of<'a>>(self) } | ||
} | ||
} | ||
|
||
impl HeapMarkAndSweep for PromiseAll<'static> { | ||
fn mark_values(&self, queues: &mut WorkQueues) { | ||
queues.promise_all_records.push(*self); | ||
komyg marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} | ||
|
||
fn sweep_values(&mut self, compactions: &CompactionLists) { | ||
compactions.promise_all_records.shift_index(&mut self.0); | ||
} | ||
} | ||
|
||
unsafe impl Bindable for PromiseAll<'_> { | ||
type Of<'a> = PromiseAll<'a>; | ||
|
||
#[inline(always)] | ||
fn unbind(self) -> Self::Of<'static> { | ||
unsafe { core::mem::transmute::<Self, Self::Of<'static>>(self) } | ||
} | ||
|
||
#[inline(always)] | ||
fn bind<'a>(self, _gc: NoGcScope<'a, '_>) -> Self::Of<'a> { | ||
unsafe { core::mem::transmute::<Self, Self::Of<'a>>(self) } | ||
} | ||
} | ||
|
||
impl<'a> CreateHeapData<PromiseAllRecord<'a>, PromiseAll<'a>> for Heap { | ||
fn create(&mut self, data: PromiseAllRecord<'a>) -> PromiseAll<'a> { | ||
self.promise_all_records.push(Some(data.unbind())); | ||
self.alloc_counter += core::mem::size_of::<Option<PromiseAllRecord<'static>>>(); | ||
PromiseAll(BaseIndex::last(&self.promise_all_records)) | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -145,7 +145,7 @@ impl PromiseReactionJob { | |
let Self { reaction, argument } = self; | ||
let reaction = reaction.take(agent).bind(gc.nogc()); | ||
let argument = argument.take(agent).bind(gc.nogc()); | ||
// The following are substeps of point 1 in NewPromiseReactionJob. | ||
|
||
let (handler_result, promise_capability) = match agent[reaction].handler { | ||
PromiseReactionHandler::Empty => { | ||
let capability = agent[reaction].capability.clone().unwrap().bind(gc.nogc()); | ||
|
@@ -288,6 +288,42 @@ impl PromiseReactionJob { | |
} | ||
} | ||
} | ||
PromiseReactionHandler::PromiseAll { promise_all, index } => { | ||
let capability = agent[reaction].capability.clone().unwrap().bind(gc.nogc()); | ||
match agent[reaction].reaction_type { | ||
PromiseReactionType::Fulfill => { | ||
// Take out the record to drop the vec borrow before we use `agent`/`gc` | ||
let rec = { | ||
let slot = agent | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. issue: You don't want to do this; what you want to do instead is to implement the I assume you currently get an error about calling If GC triggers during To deal with this, we implement |
||
.heap | ||
.promise_all_records | ||
.get_mut(promise_all.get_index()) | ||
.expect("PromiseAllRecord out of bounds"); | ||
slot.take().expect("PromiseAllRecord slot empty") | ||
}; | ||
|
||
// Bind to current scope and mutate | ||
let mut rec_bound = rec.unbind().bind(gc.nogc()); | ||
rec_bound.on_promise_fufilled( | ||
agent, | ||
index, | ||
argument.clone().unbind(), | ||
gc.reborrow(), | ||
); | ||
|
||
// Write back with 'static lifetime | ||
agent | ||
.heap | ||
.promise_all_records | ||
.get_mut(promise_all.get_index()) | ||
.unwrap() | ||
.replace(rec_bound.unbind()); | ||
|
||
(Ok(argument), capability) | ||
} | ||
PromiseReactionType::Reject => (Err(JsError::new(argument)), capability), | ||
} | ||
} | ||
}; | ||
|
||
// f. If promiseCapability is undefined, then | ||
|
@@ -348,7 +384,8 @@ pub(crate) fn new_promise_reaction_job( | |
| PromiseReactionHandler::AsyncFromSyncIteratorClose(_) | ||
| PromiseReactionHandler::AsyncModule(_) | ||
| PromiseReactionHandler::DynamicImport { .. } | ||
| PromiseReactionHandler::DynamicImportEvaluate { .. } => None, | ||
| PromiseReactionHandler::DynamicImportEvaluate { .. } | ||
| PromiseReactionHandler::PromiseAll { .. } => None, | ||
}; | ||
|
||
// 4. Return the Record { [[Job]]: job, [[Realm]]: handlerRealm }. | ||
|
Uh oh!
There was an error while loading. Please reload this page.