|
| 1 | +// This Source Code Form is subject to the terms of the Mozilla Public |
| 2 | +// License, v. 2.0. If a copy of the MPL was not distributed with this |
| 3 | +// file, You can obtain one at https://mozilla.org/MPL/2.0/. |
| 4 | + |
| 5 | +use crate::{ |
| 6 | + ecmascript::{ |
| 7 | + builtins::{ |
| 8 | + Array, promise::Promise, |
| 9 | + promise_objects::promise_abstract_operations::promise_capability_records::PromiseCapability, |
| 10 | + }, |
| 11 | + execution::Agent, |
| 12 | + types::{IntoValue, Value}, |
| 13 | + }, |
| 14 | + engine::{ |
| 15 | + context::{Bindable, GcScope, NoGcScope, bindable_handle}, |
| 16 | + rootable::{HeapRootData, HeapRootRef, Rootable}, |
| 17 | + }, |
| 18 | + heap::{ |
| 19 | + CompactionLists, CreateHeapData, Heap, HeapMarkAndSweep, WorkQueues, indexes::BaseIndex, |
| 20 | + }, |
| 21 | +}; |
| 22 | + |
| 23 | +#[derive(Debug, Clone, Copy)] |
| 24 | +pub struct PromiseAllSettledRecord<'a> { |
| 25 | + pub(crate) remaining_elements_count: u32, |
| 26 | + pub(crate) result_array: Array<'a>, |
| 27 | + pub(crate) promise: Promise<'a>, |
| 28 | +} |
| 29 | + |
| 30 | +#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord)] |
| 31 | +#[repr(transparent)] |
| 32 | +pub struct PromiseAllSettled<'a>(BaseIndex<'a, PromiseAllSettledRecord<'static>>); |
| 33 | + |
| 34 | +impl<'a> PromiseAllSettled<'a> { |
| 35 | + pub(crate) fn on_promise_fulfilled( |
| 36 | + self, |
| 37 | + agent: &mut Agent, |
| 38 | + index: u32, |
| 39 | + value: Value<'a>, |
| 40 | + gc: GcScope<'a, '_>, |
| 41 | + ) { |
| 42 | + let promise_all = self.bind(gc.nogc()); |
| 43 | + let value = value.bind(gc.nogc()); |
| 44 | + |
| 45 | + let result_array = promise_all.get_result_array(agent, gc.nogc()); |
| 46 | + |
| 47 | + let elements = result_array.as_mut_slice(agent); |
| 48 | + elements[index as usize] = Some(value.unbind()); |
| 49 | + |
| 50 | + let data = promise_all.get_mut(agent); |
| 51 | + |
| 52 | + // i. Set remainingElementsCount.[[Value]] to remainingElementsCount.[[Value]] - 1. |
| 53 | + data.remaining_elements_count = data.remaining_elements_count.saturating_sub(1); |
| 54 | + |
| 55 | + //ii. If remainingElementsCount.[[Value]] = 0, then |
| 56 | + if data.remaining_elements_count == 0 { |
| 57 | + // 1. Let valuesArray be CreateArrayFromList(values). |
| 58 | + // 2. Perform ? Call(resultCapability.[[Resolve]], undefined, « valuesArray »). |
| 59 | + let capability = PromiseCapability::from_promise(data.promise, true); |
| 60 | + capability.resolve(agent, result_array.into_value().unbind(), gc); |
| 61 | + } |
| 62 | + } |
| 63 | + |
| 64 | + pub(crate) fn on_promise_rejected( |
| 65 | + self, |
| 66 | + agent: &mut Agent, |
| 67 | + value: Value<'a>, |
| 68 | + gc: NoGcScope<'a, '_>, |
| 69 | + ) { |
| 70 | + let value = value.bind(gc); |
| 71 | + let promise_all = self.bind(gc); |
| 72 | + let data = promise_all.get_mut(agent); |
| 73 | + |
| 74 | + let capability = PromiseCapability::from_promise(data.promise, true); |
| 75 | + capability.reject(agent, value.unbind(), gc); |
| 76 | + } |
| 77 | + |
| 78 | + pub(crate) fn get_result_array(self, agent: &Agent, gc: NoGcScope<'a, '_>) -> Array<'a> { |
| 79 | + let data = self.get(agent); |
| 80 | + data.result_array.bind(gc).unbind() |
| 81 | + } |
| 82 | + |
| 83 | + pub(crate) const fn get_index(self) -> usize { |
| 84 | + self.0.into_index() |
| 85 | + } |
| 86 | + |
| 87 | + pub fn get(self, agent: &Agent) -> &PromiseAllSettledRecord<'a> { |
| 88 | + agent |
| 89 | + .heap |
| 90 | + .promise_all_settled_records |
| 91 | + .get(self.get_index()) |
| 92 | + .expect("PromiseAllSettledRecord not found") |
| 93 | + } |
| 94 | + |
| 95 | + pub fn get_mut(self, agent: &mut Agent) -> &mut PromiseAllSettledRecord<'static> { |
| 96 | + agent |
| 97 | + .heap |
| 98 | + .promise_all_settled_records |
| 99 | + .get_mut(self.get_index()) |
| 100 | + .expect("PromiseAllSettledRecord not found") |
| 101 | + } |
| 102 | + |
| 103 | + pub(crate) const fn _def() -> Self { |
| 104 | + Self(BaseIndex::from_u32_index(0)) |
| 105 | + } |
| 106 | +} |
| 107 | + |
| 108 | +impl AsRef<[PromiseAllSettledRecord<'static>]> for Agent { |
| 109 | + fn as_ref(&self) -> &[PromiseAllSettledRecord<'static>] { |
| 110 | + &self.heap.promise_all_settled_records |
| 111 | + } |
| 112 | +} |
| 113 | + |
| 114 | +impl AsMut<[PromiseAllSettledRecord<'static>]> for Agent { |
| 115 | + fn as_mut(&mut self) -> &mut [PromiseAllSettledRecord<'static>] { |
| 116 | + &mut self.heap.promise_all_settled_records |
| 117 | + } |
| 118 | +} |
| 119 | + |
| 120 | +impl HeapMarkAndSweep for PromiseAllSettledRecord<'static> { |
| 121 | + fn mark_values(&self, queues: &mut WorkQueues) { |
| 122 | + let Self { |
| 123 | + remaining_elements_count: _, |
| 124 | + result_array, |
| 125 | + promise, |
| 126 | + } = self; |
| 127 | + result_array.mark_values(queues); |
| 128 | + promise.mark_values(queues); |
| 129 | + } |
| 130 | + |
| 131 | + fn sweep_values(&mut self, compactions: &CompactionLists) { |
| 132 | + let Self { |
| 133 | + remaining_elements_count: _, |
| 134 | + result_array, |
| 135 | + promise, |
| 136 | + } = self; |
| 137 | + result_array.sweep_values(compactions); |
| 138 | + promise.sweep_values(compactions); |
| 139 | + } |
| 140 | +} |
| 141 | + |
| 142 | +impl Rootable for PromiseAllSettled<'_> { |
| 143 | + type RootRepr = HeapRootRef; |
| 144 | + |
| 145 | + fn to_root_repr(value: Self) -> Result<Self::RootRepr, HeapRootData> { |
| 146 | + Err(HeapRootData::PromiseAllSettled(value.unbind())) |
| 147 | + } |
| 148 | + |
| 149 | + fn from_root_repr(value: &Self::RootRepr) -> Result<Self, HeapRootRef> { |
| 150 | + Err(*value) |
| 151 | + } |
| 152 | + |
| 153 | + fn from_heap_ref(heap_ref: HeapRootRef) -> Self::RootRepr { |
| 154 | + heap_ref |
| 155 | + } |
| 156 | + |
| 157 | + fn from_heap_data(heap_data: HeapRootData) -> Option<Self> { |
| 158 | + match heap_data { |
| 159 | + HeapRootData::PromiseAllSettled(object) => Some(object), |
| 160 | + _ => None, |
| 161 | + } |
| 162 | + } |
| 163 | +} |
| 164 | + |
| 165 | +impl HeapMarkAndSweep for PromiseAllSettled<'static> { |
| 166 | + fn mark_values(&self, queues: &mut WorkQueues) { |
| 167 | + queues.promise_all_settled_records.push(*self); |
| 168 | + } |
| 169 | + |
| 170 | + fn sweep_values(&mut self, compactions: &CompactionLists) { |
| 171 | + compactions |
| 172 | + .promise_all_settled_records |
| 173 | + .shift_index(&mut self.0); |
| 174 | + } |
| 175 | +} |
| 176 | + |
| 177 | +bindable_handle!(PromiseAllSettledRecord); |
| 178 | +bindable_handle!(PromiseAllSettled); |
| 179 | + |
| 180 | +impl<'a> CreateHeapData<PromiseAllSettledRecord<'a>, PromiseAllSettled<'a>> for Heap { |
| 181 | + fn create(&mut self, data: PromiseAllSettledRecord<'a>) -> PromiseAllSettled<'a> { |
| 182 | + self.promise_all_settled_records.push(data.unbind()); |
| 183 | + self.alloc_counter += core::mem::size_of::<PromiseAllSettledRecord<'static>>(); |
| 184 | + PromiseAllSettled(BaseIndex::last_t(&self.promise_all_settled_records)) |
| 185 | + } |
| 186 | +} |
0 commit comments