|
1 | 1 | use crate::{ |
2 | 2 | ecmascript::{ |
3 | | - builtins::promise_objects::promise_abstract_operations::{ |
4 | | - promise_all_record::PromiseAllRecord, |
5 | | - promise_all_settled_record::PromiseAllSettledRecord, |
| 3 | + builtins::{ |
| 4 | + Array, promise::Promise, |
| 5 | + promise_objects::promise_abstract_operations::promise_capability_records::PromiseCapability, |
6 | 6 | }, |
7 | 7 | execution::Agent, |
8 | | - types::Value, |
| 8 | + types::{BUILTIN_STRING_MEMORY, IntoValue, OrdinaryObject, Value}, |
9 | 9 | }, |
10 | 10 | engine::{ |
11 | | - context::{Bindable, GcScope, bindable_handle}, |
| 11 | + context::{Bindable, GcScope, NoGcScope, bindable_handle}, |
12 | 12 | rootable::{HeapRootData, HeapRootRef, Rootable}, |
13 | 13 | }, |
14 | 14 | heap::{ |
15 | | - CompactionLists, CreateHeapData, Heap, HeapMarkAndSweep, WorkQueues, indexes::BaseIndex, |
| 15 | + CompactionLists, CreateHeapData, Heap, HeapMarkAndSweep, ObjectEntry, WorkQueues, |
| 16 | + indexes::BaseIndex, |
16 | 17 | }, |
17 | 18 | }; |
18 | 19 |
|
19 | 20 | #[derive(Debug, Clone, Copy)] |
20 | | -pub enum PromiseGroupRecord<'a> { |
21 | | - PromiseAll(PromiseAllRecord<'a>), |
22 | | - PromiseAllSettled(PromiseAllSettledRecord<'a>), |
| 21 | +pub enum PromiseGroupType { |
| 22 | + PromiseAll, |
| 23 | + PromiseAllSettled, |
| 24 | +} |
| 25 | + |
| 26 | +#[derive(Debug, Clone, Copy)] |
| 27 | +pub struct PromiseGroupRecord<'a> { |
| 28 | + pub(crate) promise_group_type: PromiseGroupType, |
| 29 | + pub(crate) remaining_elements_count: u32, |
| 30 | + pub(crate) result_array: Array<'a>, |
| 31 | + pub(crate) promise: Promise<'a>, |
23 | 32 | } |
24 | 33 |
|
25 | 34 | #[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord)] |
26 | 35 | #[repr(transparent)] |
27 | 36 | pub struct PromiseGroup<'a>(BaseIndex<'a, PromiseGroupRecord<'static>>); |
28 | 37 |
|
29 | 38 | impl<'a> PromiseGroup<'a> { |
30 | | - pub(crate) fn on_promise_fulfilled( |
| 39 | + pub(crate) fn on_promise_all_fulfilled( |
31 | 40 | self, |
32 | 41 | agent: &mut Agent, |
33 | 42 | index: u32, |
34 | 43 | value: Value<'a>, |
35 | | - mut gc: GcScope<'a, '_>, |
| 44 | + gc: GcScope<'a, '_>, |
36 | 45 | ) { |
| 46 | + let promise_all = self.bind(gc.nogc()); |
37 | 47 | let value = value.bind(gc.nogc()); |
38 | | - let promise_group = self.bind(gc.nogc()); |
39 | | - |
40 | | - let promise_group = promise_group.get_mut(agent); |
41 | | - match promise_group { |
42 | | - PromiseGroupRecord::PromiseAll(promise_all) => { |
43 | | - // i. Set remainingElementsCount.[[Value]] to remainingElementsCount.[[Value]] - 1. |
44 | | - promise_all.remaining_elements_count = |
45 | | - promise_all.remaining_elements_count.saturating_sub(1); |
46 | | - promise_all.on_promise_fulfilled(agent, index, value.unbind(), gc.reborrow()); |
47 | | - } |
48 | | - PromiseGroupRecord::PromiseAllSettled(promise_all_settled) => { |
49 | | - // 13. Set remainingElementsCount.[[Value]] to remainingElementsCount.[[Value]] - 1. |
50 | | - promise_all_settled.remaining_elements_count = promise_all_settled |
51 | | - .remaining_elements_count |
52 | | - .saturating_sub(1); |
53 | | - promise_all_settled.on_promise_fulfilled( |
54 | | - agent, |
55 | | - index, |
56 | | - value.unbind(), |
57 | | - gc.reborrow(), |
58 | | - ); |
59 | | - } |
60 | | - }; |
61 | | - } |
62 | | - |
63 | | - pub(crate) fn on_promise_rejected( |
| 48 | + |
| 49 | + let result_array = promise_all.get_result_array(agent, gc.nogc()); |
| 50 | + |
| 51 | + let elements = result_array.as_mut_slice(agent); |
| 52 | + elements[index as usize] = Some(value.unbind()); |
| 53 | + |
| 54 | + let data = promise_all.get_mut(agent); |
| 55 | + |
| 56 | + // i. Set remainingElementsCount.[[Value]] to remainingElementsCount.[[Value]] - 1. |
| 57 | + data.remaining_elements_count = data.remaining_elements_count.saturating_sub(1); |
| 58 | + |
| 59 | + //ii. If remainingElementsCount.[[Value]] = 0, then |
| 60 | + if data.remaining_elements_count == 0 { |
| 61 | + // 1. Let valuesArray be CreateArrayFromList(values). |
| 62 | + // 2. Perform ? Call(resultCapability.[[Resolve]], undefined, « valuesArray »). |
| 63 | + let capability = PromiseCapability::from_promise(data.promise, true); |
| 64 | + capability.resolve(agent, result_array.into_value().unbind(), gc); |
| 65 | + } |
| 66 | + } |
| 67 | + |
| 68 | + pub(crate) fn on_promise_all_rejected( |
| 69 | + self, |
| 70 | + agent: &mut Agent, |
| 71 | + value: Value<'a>, |
| 72 | + gc: NoGcScope<'a, '_>, |
| 73 | + ) { |
| 74 | + let value = value.bind(gc); |
| 75 | + let promise_all = self.bind(gc); |
| 76 | + let data = promise_all.get_mut(agent); |
| 77 | + |
| 78 | + let capability = PromiseCapability::from_promise(data.promise, true); |
| 79 | + capability.reject(agent, value.unbind(), gc); |
| 80 | + } |
| 81 | + |
| 82 | + pub(crate) fn on_promise_all_settled_fulfilled( |
64 | 83 | self, |
65 | 84 | agent: &mut Agent, |
66 | 85 | index: u32, |
67 | 86 | value: Value<'a>, |
68 | | - mut gc: GcScope<'a, '_>, |
| 87 | + gc: GcScope<'a, '_>, |
69 | 88 | ) { |
| 89 | + let promise_all = self.bind(gc.nogc()); |
70 | 90 | let value = value.bind(gc.nogc()); |
71 | | - let promise_group = self.bind(gc.nogc()); |
72 | | - |
73 | | - let promise_group = promise_group.get_mut(agent); |
74 | | - match promise_group { |
75 | | - PromiseGroupRecord::PromiseAll(promise_all) => { |
76 | | - promise_all.on_promise_rejected(agent, value.unbind(), gc.nogc()) |
77 | | - } |
78 | | - PromiseGroupRecord::PromiseAllSettled(promise_all_settled) => { |
79 | | - // 13. Set remainingElementsCount.[[Value]] to remainingElementsCount.[[Value]] - 1. |
80 | | - promise_all_settled.remaining_elements_count = promise_all_settled |
81 | | - .remaining_elements_count |
82 | | - .saturating_sub(1); |
83 | | - promise_all_settled.on_promise_rejected( |
84 | | - agent, |
85 | | - index, |
86 | | - value.unbind(), |
87 | | - gc.reborrow(), |
88 | | - ); |
89 | | - } |
| 91 | + |
| 92 | + let result_array = promise_all.get_result_array(agent, gc.nogc()); |
| 93 | + |
| 94 | + // Let obj be OrdinaryObjectCreate(%Object.prototype%). |
| 95 | + // 10. Perform ! CreateDataPropertyOrThrow(obj, "status", "fulfilled"). |
| 96 | + // 11. Perform ! CreateDataPropertyOrThrow(obj, "value", x). |
| 97 | + let obj = OrdinaryObject::create_object( |
| 98 | + agent, |
| 99 | + Some( |
| 100 | + agent |
| 101 | + .current_realm_record() |
| 102 | + .intrinsics() |
| 103 | + .object_prototype() |
| 104 | + .into(), |
| 105 | + ), |
| 106 | + &[ |
| 107 | + ObjectEntry::new_data_entry( |
| 108 | + BUILTIN_STRING_MEMORY.status.into(), |
| 109 | + BUILTIN_STRING_MEMORY.fulfilled.into(), |
| 110 | + ), |
| 111 | + ObjectEntry::new_data_entry(BUILTIN_STRING_MEMORY.value.into(), value.unbind()), |
| 112 | + ], |
| 113 | + ) |
| 114 | + .bind(gc.nogc()); |
| 115 | + |
| 116 | + let elements = result_array.as_mut_slice(agent); |
| 117 | + elements[index as usize] = Some(obj.unbind().into_value()); |
| 118 | + |
| 119 | + let data = promise_all.get_mut(agent); |
| 120 | + |
| 121 | + // 13. Set remainingElementsCount.[[Value]] to remainingElementsCount.[[Value]] - 1. |
| 122 | + data.remaining_elements_count = data.remaining_elements_count.saturating_sub(1); |
| 123 | + |
| 124 | + // 14. If remainingElementsCount.[[Value]] = 0, then |
| 125 | + if data.remaining_elements_count == 0 { |
| 126 | + // a. Let valuesArray be CreateArrayFromList(values). |
| 127 | + // b. Return ? Call(promiseCapability.[[Resolve]], undefined, « valuesArray »). |
| 128 | + let capability = PromiseCapability::from_promise(data.promise, true); |
| 129 | + capability.resolve(agent, result_array.into_value().unbind(), gc); |
90 | 130 | } |
91 | 131 | } |
92 | 132 |
|
| 133 | + pub(crate) fn on_promise_all_settled_rejected( |
| 134 | + self, |
| 135 | + agent: &mut Agent, |
| 136 | + index: u32, |
| 137 | + value: Value<'a>, |
| 138 | + gc: GcScope<'a, '_>, |
| 139 | + ) { |
| 140 | + let promise_all = self.bind(gc.nogc()); |
| 141 | + let value = value.bind(gc.nogc()); |
| 142 | + |
| 143 | + let result_array = promise_all.get_result_array(agent, gc.nogc()); |
| 144 | + |
| 145 | + // Let obj be OrdinaryObjectCreate(%Object.prototype%). |
| 146 | + // 10. Perform ! CreateDataPropertyOrThrow(obj, "status", "rejected"). |
| 147 | + // 11. Perform ! CreateDataPropertyOrThrow(obj, "reason", x). |
| 148 | + let obj = OrdinaryObject::create_object( |
| 149 | + agent, |
| 150 | + Some( |
| 151 | + agent |
| 152 | + .current_realm_record() |
| 153 | + .intrinsics() |
| 154 | + .object_prototype() |
| 155 | + .into(), |
| 156 | + ), |
| 157 | + &[ |
| 158 | + ObjectEntry::new_data_entry( |
| 159 | + BUILTIN_STRING_MEMORY.status.into(), |
| 160 | + BUILTIN_STRING_MEMORY.rejected.into(), |
| 161 | + ), |
| 162 | + ObjectEntry::new_data_entry(BUILTIN_STRING_MEMORY.reason.into(), value.unbind()), |
| 163 | + ], |
| 164 | + ) |
| 165 | + .bind(gc.nogc()); |
| 166 | + |
| 167 | + let elements = result_array.as_mut_slice(agent); |
| 168 | + elements[index as usize] = Some(obj.unbind().into_value()); |
| 169 | + |
| 170 | + let data = promise_all.get_mut(agent); |
| 171 | + |
| 172 | + // 13. Set remainingElementsCount.[[Value]] to remainingElementsCount.[[Value]] - 1. |
| 173 | + data.remaining_elements_count = data.remaining_elements_count.saturating_sub(1); |
| 174 | + |
| 175 | + // 14. If remainingElementsCount.[[Value]] = 0, then |
| 176 | + if data.remaining_elements_count == 0 { |
| 177 | + // a. Let valuesArray be CreateArrayFromList(values). |
| 178 | + // b. Return ? Call(promiseCapability.[[Resolve]], undefined, « valuesArray »). |
| 179 | + let capability = PromiseCapability::from_promise(data.promise, true); |
| 180 | + capability.resolve(agent, result_array.into_value().unbind(), gc); |
| 181 | + } |
| 182 | + } |
| 183 | + |
| 184 | + pub(crate) fn get_result_array(self, agent: &Agent, gc: NoGcScope<'a, '_>) -> Array<'a> { |
| 185 | + let data = self.get(agent); |
| 186 | + data.result_array.bind(gc).unbind() |
| 187 | + } |
| 188 | + |
93 | 189 | pub(crate) const fn get_index(self) -> usize { |
94 | 190 | self.0.into_index() |
95 | 191 | } |
@@ -129,21 +225,25 @@ impl AsMut<[PromiseGroupRecord<'static>]> for Agent { |
129 | 225 |
|
130 | 226 | impl HeapMarkAndSweep for PromiseGroupRecord<'static> { |
131 | 227 | fn mark_values(&self, queues: &mut WorkQueues) { |
132 | | - match self { |
133 | | - PromiseGroupRecord::PromiseAll(promise_all) => promise_all.mark_values(queues), |
134 | | - PromiseGroupRecord::PromiseAllSettled(promise_all_settled) => { |
135 | | - promise_all_settled.mark_values(queues) |
136 | | - } |
137 | | - } |
| 228 | + let Self { |
| 229 | + promise_group_type: _, |
| 230 | + remaining_elements_count: _, |
| 231 | + result_array, |
| 232 | + promise, |
| 233 | + } = self; |
| 234 | + result_array.mark_values(queues); |
| 235 | + promise.mark_values(queues); |
138 | 236 | } |
139 | 237 |
|
140 | 238 | fn sweep_values(&mut self, compactions: &CompactionLists) { |
141 | | - match self { |
142 | | - PromiseGroupRecord::PromiseAll(promise_all) => promise_all.sweep_values(compactions), |
143 | | - PromiseGroupRecord::PromiseAllSettled(promise_all_settled) => { |
144 | | - promise_all_settled.sweep_values(compactions) |
145 | | - } |
146 | | - } |
| 239 | + let Self { |
| 240 | + promise_group_type: _, |
| 241 | + remaining_elements_count: _, |
| 242 | + result_array, |
| 243 | + promise, |
| 244 | + } = self; |
| 245 | + result_array.sweep_values(compactions); |
| 246 | + promise.sweep_values(compactions); |
147 | 247 | } |
148 | 248 | } |
149 | 249 |
|
|
0 commit comments