Skip to content

Commit 295b9eb

Browse files
committed
Refactor on_promise_all_settled method in PromiseGroup to consolidate fulfillment and rejection handling, improving code clarity and reducing duplication.
1 parent f30c003 commit 295b9eb

File tree

1 file changed

+50
-89
lines changed
  • nova_vm/src/ecmascript/builtins/control_abstraction_objects/promise_objects/promise_abstract_operations

1 file changed

+50
-89
lines changed

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

Lines changed: 50 additions & 89 deletions
Original file line numberDiff line numberDiff line change
@@ -60,24 +60,15 @@ impl<'a> PromiseGroup<'a> {
6060
self.on_promise_all_rejected(agent, value.unbind(), gc.nogc());
6161
}
6262
},
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-
},
63+
PromiseGroupType::PromiseAllSettled => {
64+
self.on_promise_all_settled(
65+
agent,
66+
reaction_type,
67+
index,
68+
value.unbind(),
69+
gc.reborrow(),
70+
);
71+
}
8172
}
8273
}
8374

@@ -124,9 +115,10 @@ impl<'a> PromiseGroup<'a> {
124115
capability.reject(agent, value.unbind(), gc);
125116
}
126117

127-
pub(crate) fn on_promise_all_settled_fulfilled(
118+
pub(crate) fn on_promise_all_settled(
128119
self,
129120
agent: &mut Agent,
121+
reaction_type: PromiseReactionType,
130122
index: u32,
131123
value: Value<'a>,
132124
gc: GcScope<'a, '_>,
@@ -136,78 +128,47 @@ impl<'a> PromiseGroup<'a> {
136128

137129
let result_array = promise_all.get_result_array(agent, gc.nogc());
138130

139-
// Let obj be OrdinaryObjectCreate(%Object.prototype%).
140-
// 10. Perform ! CreateDataPropertyOrThrow(obj, "status", "fulfilled").
141-
// 11. Perform ! CreateDataPropertyOrThrow(obj, "value", x).
142-
let obj = OrdinaryObject::create_object(
143-
agent,
144-
Some(
145-
agent
146-
.current_realm_record()
147-
.intrinsics()
148-
.object_prototype()
149-
.into(),
150-
),
151-
&[
152-
ObjectEntry::new_data_entry(
153-
BUILTIN_STRING_MEMORY.status.into(),
154-
BUILTIN_STRING_MEMORY.fulfilled.into(),
131+
let obj = match reaction_type {
132+
PromiseReactionType::Fulfill => OrdinaryObject::create_object(
133+
agent,
134+
Some(
135+
agent
136+
.current_realm_record()
137+
.intrinsics()
138+
.object_prototype()
139+
.into(),
155140
),
156-
ObjectEntry::new_data_entry(BUILTIN_STRING_MEMORY.value.into(), value.unbind()),
157-
],
158-
)
159-
.bind(gc.nogc());
160-
161-
let elements = result_array.as_mut_slice(agent);
162-
elements[index as usize] = Some(obj.unbind().into_value());
163-
164-
let data = promise_all.get_mut(agent);
165-
166-
// 13. Set remainingElementsCount.[[Value]] to remainingElementsCount.[[Value]] - 1.
167-
data.remaining_elements_count = data.remaining_elements_count.saturating_sub(1);
168-
169-
// 14. If remainingElementsCount.[[Value]] = 0, then
170-
if data.remaining_elements_count == 0 {
171-
// a. Let valuesArray be CreateArrayFromList(values).
172-
// b. Return ? Call(promiseCapability.[[Resolve]], undefined, « valuesArray »).
173-
let capability = PromiseCapability::from_promise(data.promise, true);
174-
capability.resolve(agent, result_array.into_value().unbind(), gc);
175-
}
176-
}
177-
178-
pub(crate) fn on_promise_all_settled_rejected(
179-
self,
180-
agent: &mut Agent,
181-
index: u32,
182-
value: Value<'a>,
183-
gc: GcScope<'a, '_>,
184-
) {
185-
let promise_all = self.bind(gc.nogc());
186-
let value = value.bind(gc.nogc());
187-
188-
let result_array = promise_all.get_result_array(agent, gc.nogc());
189-
190-
// Let obj be OrdinaryObjectCreate(%Object.prototype%).
191-
// 10. Perform ! CreateDataPropertyOrThrow(obj, "status", "rejected").
192-
// 11. Perform ! CreateDataPropertyOrThrow(obj, "reason", x).
193-
let obj = OrdinaryObject::create_object(
194-
agent,
195-
Some(
196-
agent
197-
.current_realm_record()
198-
.intrinsics()
199-
.object_prototype()
200-
.into(),
201-
),
202-
&[
203-
ObjectEntry::new_data_entry(
204-
BUILTIN_STRING_MEMORY.status.into(),
205-
BUILTIN_STRING_MEMORY.rejected.into(),
141+
&[
142+
ObjectEntry::new_data_entry(
143+
BUILTIN_STRING_MEMORY.status.into(),
144+
BUILTIN_STRING_MEMORY.fulfilled.into(),
145+
),
146+
ObjectEntry::new_data_entry(BUILTIN_STRING_MEMORY.value.into(), value.unbind()),
147+
],
148+
)
149+
.bind(gc.nogc()),
150+
PromiseReactionType::Reject => OrdinaryObject::create_object(
151+
agent,
152+
Some(
153+
agent
154+
.current_realm_record()
155+
.intrinsics()
156+
.object_prototype()
157+
.into(),
206158
),
207-
ObjectEntry::new_data_entry(BUILTIN_STRING_MEMORY.reason.into(), value.unbind()),
208-
],
209-
)
210-
.bind(gc.nogc());
159+
&[
160+
ObjectEntry::new_data_entry(
161+
BUILTIN_STRING_MEMORY.status.into(),
162+
BUILTIN_STRING_MEMORY.rejected.into(),
163+
),
164+
ObjectEntry::new_data_entry(
165+
BUILTIN_STRING_MEMORY.reason.into(),
166+
value.unbind(),
167+
),
168+
],
169+
)
170+
.bind(gc.nogc()),
171+
};
211172

212173
let elements = result_array.as_mut_slice(agent);
213174
elements[index as usize] = Some(obj.unbind().into_value());

0 commit comments

Comments
 (0)