Skip to content

Commit f30c003

Browse files
committed
Implement settle method in PromiseGroup to handle fulfillment and rejection for PromiseAll and PromiseAllSettled, streamlining promise reaction processing.
1 parent 3d7a47b commit f30c003

File tree

2 files changed

+55
-44
lines changed

2 files changed

+55
-44
lines changed

nova_vm/src/ecmascript/builtins/control_abstraction_objects/promise_objects/promise_abstract_operations/promise_group_record.rs

Lines changed: 47 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,12 @@
11
use crate::{
22
ecmascript::{
33
builtins::{
4-
Array, promise::Promise,
5-
promise_objects::promise_abstract_operations::promise_capability_records::PromiseCapability,
4+
Array,
5+
promise::Promise,
6+
promise_objects::promise_abstract_operations::{
7+
promise_capability_records::PromiseCapability,
8+
promise_reaction_records::PromiseReactionType,
9+
},
610
},
711
execution::Agent,
812
types::{BUILTIN_STRING_MEMORY, IntoValue, OrdinaryObject, Value},
@@ -36,6 +40,47 @@ pub struct PromiseGroupRecord<'a> {
3640
pub struct PromiseGroup<'a>(BaseIndex<'a, PromiseGroupRecord<'static>>);
3741

3842
impl<'a> PromiseGroup<'a> {
43+
pub(crate) fn settle(
44+
self,
45+
agent: &mut Agent,
46+
reaction_type: PromiseReactionType,
47+
index: u32,
48+
value: Value<'a>,
49+
mut gc: GcScope<'a, '_>,
50+
) {
51+
let value = value.bind(gc.nogc());
52+
let record = self.get(agent);
53+
54+
match record.promise_group_type {
55+
PromiseGroupType::PromiseAll => match reaction_type {
56+
PromiseReactionType::Fulfill => {
57+
self.on_promise_all_fulfilled(agent, index, value.unbind(), gc.reborrow());
58+
}
59+
PromiseReactionType::Reject => {
60+
self.on_promise_all_rejected(agent, value.unbind(), gc.nogc());
61+
}
62+
},
63+
PromiseGroupType::PromiseAllSettled => match reaction_type {
64+
PromiseReactionType::Fulfill => {
65+
self.on_promise_all_settled_fulfilled(
66+
agent,
67+
index,
68+
value.unbind(),
69+
gc.reborrow(),
70+
);
71+
}
72+
PromiseReactionType::Reject => {
73+
self.on_promise_all_settled_rejected(
74+
agent,
75+
index,
76+
value.unbind(),
77+
gc.reborrow(),
78+
);
79+
}
80+
},
81+
}
82+
}
83+
3984
pub(crate) fn on_promise_all_fulfilled(
4085
self,
4186
agent: &mut Agent,

nova_vm/src/ecmascript/builtins/control_abstraction_objects/promise_objects/promise_abstract_operations/promise_jobs.rs

Lines changed: 8 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,7 @@ use crate::{
1212
},
1313
operations_on_objects::{call_function, get_function_realm},
1414
},
15-
builtins::{
16-
ArgumentsList, promise::Promise,
17-
promise_objects::promise_abstract_operations::promise_group_record::PromiseGroupType,
18-
},
15+
builtins::{ArgumentsList, promise::Promise},
1916
execution::{
2017
Agent, JsResult,
2118
agent::{InnerJob, Job, JsError},
@@ -292,44 +289,13 @@ impl PromiseReactionJob {
292289
index,
293290
} => {
294291
let reaction_type = agent[reaction].reaction_type;
295-
let record = promise_group.get(agent);
296-
match record.promise_group_type {
297-
PromiseGroupType::PromiseAll => match reaction_type {
298-
PromiseReactionType::Fulfill => {
299-
promise_group.on_promise_all_fulfilled(
300-
agent,
301-
index,
302-
argument.unbind(),
303-
gc.reborrow(),
304-
);
305-
}
306-
PromiseReactionType::Reject => {
307-
promise_group.on_promise_all_rejected(
308-
agent,
309-
argument.unbind(),
310-
gc.nogc(),
311-
);
312-
}
313-
},
314-
PromiseGroupType::PromiseAllSettled => match reaction_type {
315-
PromiseReactionType::Fulfill => {
316-
promise_group.on_promise_all_settled_fulfilled(
317-
agent,
318-
index,
319-
argument.unbind(),
320-
gc.reborrow(),
321-
);
322-
}
323-
PromiseReactionType::Reject => {
324-
promise_group.on_promise_all_settled_rejected(
325-
agent,
326-
index,
327-
argument.unbind(),
328-
gc.reborrow(),
329-
);
330-
}
331-
},
332-
}
292+
promise_group.settle(
293+
agent,
294+
reaction_type,
295+
index,
296+
argument.unbind(),
297+
gc.reborrow(),
298+
);
333299
return Ok(());
334300
}
335301
};

0 commit comments

Comments
 (0)