1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
//! `CallFrame`
//!
//! This module will provides everything needed to implement the `CallFrame`

use crate::{
    builtins::{
        iterable::IteratorRecord,
        promise::{PromiseCapability, ResolvingFunctions},
    },
    environments::{BindingLocator, EnvironmentStack},
    object::{JsFunction, JsObject},
    realm::Realm,
    vm::CodeBlock,
    JsValue,
};
use boa_gc::{Finalize, Gc, Trace};
use thin_vec::ThinVec;

use super::{ActiveRunnable, Vm};

bitflags::bitflags! {
    /// Flags associated with a [`CallFrame`].
    #[derive(Debug, Default, Clone, Copy)]
    pub(crate) struct CallFrameFlags: u8 {
        /// When we return from this [`CallFrame`] to stop execution and
        /// return from [`crate::Context::run()`], and leave the remaining [`CallFrame`]s unchanged.
        const EXIT_EARLY = 0b0000_0001;

        /// Was this [`CallFrame`] created from the `__construct__()` internal object method?
        const CONSTRUCT = 0b0000_0010;

        /// Does this [`CallFrame`] need to push registers on [`Vm::push_frame()`].
        const REGISTERS_ALREADY_PUSHED = 0b0000_0100;
    }
}

/// A `CallFrame` holds the state of a function call.
#[derive(Clone, Debug, Finalize, Trace)]
pub struct CallFrame {
    pub(crate) code_block: Gc<CodeBlock>,
    pub(crate) pc: u32,
    /// The register pointer, points to the first register in the stack.
    // TODO: Check if storing the frame pointer instead of argument count and computing the
    //       argument count based on the pointers would be better for accessing the arguments
    //       and the elements before the register pointer.
    pub(crate) rp: u32,
    pub(crate) argument_count: u32,
    pub(crate) env_fp: u32,

    // Iterators and their `[[Done]]` flags that must be closed when an abrupt completion is thrown.
    pub(crate) iterators: ThinVec<IteratorRecord>,

    // The stack of bindings being updated.
    pub(crate) binding_stack: Vec<BindingLocator>,

    /// How many iterations a loop has done.
    pub(crate) loop_iteration_count: u64,

    /// \[\[ScriptOrModule\]\]
    pub(crate) active_runnable: Option<ActiveRunnable>,

    /// \[\[Environment\]\]
    pub(crate) environments: EnvironmentStack,

    /// \[\[Realm\]\]
    pub(crate) realm: Realm,

    // SAFETY: Nothing in `CallFrameFlags` requires tracing, so this is safe.
    #[unsafe_ignore_trace]
    pub(crate) flags: CallFrameFlags,
}

/// ---- `CallFrame` public API ----
impl CallFrame {
    /// Retrieves the [`CodeBlock`] of this call frame.
    #[inline]
    #[must_use]
    pub const fn code_block(&self) -> &Gc<CodeBlock> {
        &self.code_block
    }
}

/// ---- `CallFrame` creation methods ----
impl CallFrame {
    /// This is the size of the function prologue.
    ///
    /// The position of the elements are relative to the [`CallFrame::fp`] (register pointer).
    ///
    /// ```text
    ///                      Setup by the caller
    ///   ┌─────────────────────────────────────────────────────────┐ ┌───── register pointer
    ///   ▼                                                         ▼ ▼
    /// | -(2 + N): this | -(1 + N): func | -N: arg1 | ... | -1: argN | 0: local1 | ... | K: localK |
    ///   ▲                              ▲   ▲                      ▲   ▲                         ▲
    ///   └──────────────────────────────┘   └──────────────────────┘   └─────────────────────────┘
    ///         function prologue                    arguments              Setup by the callee
    ///   ▲
    ///   └─ Frame pointer
    /// ```
    ///
    /// ### Example
    ///
    /// The following function calls, generate the following stack:
    ///
    /// ```JavaScript
    /// function x(a) {
    /// }
    /// function y(b, c) {
    ///     return x(b + c)
    /// }
    ///
    /// y(1, 2)
    /// ```
    ///
    /// ```text
    ///     caller prologue    caller arguments   callee prologue   callee arguments
    ///   ┌─────────────────┐   ┌─────────┐   ┌─────────────────┐  ┌──────┐
    ///   ▼                 ▼   ▼         ▼   │                 ▼  ▼      ▼
    /// | 0: undefined | 1: y | 2: 1 | 3: 2 | 4: undefined | 5: x | 6:  3  |
    /// ▲                                   ▲                            ▲
    /// │       caller register pointer ────┤                            │
    /// │                                   │                callee register pointer
    /// │                             callee frame pointer
    /// │
    /// └─────  caller frame pointer
    /// ```
    ///
    /// Some questions:
    ///
    /// - Who is responsible for cleaning up the stack after a call? The rust caller.
    pub(crate) const FUNCTION_PROLOGUE: u32 = 2;
    pub(crate) const THIS_POSITION: u32 = 2;
    pub(crate) const FUNCTION_POSITION: u32 = 1;
    pub(crate) const PROMISE_CAPABILITY_PROMISE_REGISTER_INDEX: u32 = 0;
    pub(crate) const PROMISE_CAPABILITY_RESOLVE_REGISTER_INDEX: u32 = 1;
    pub(crate) const PROMISE_CAPABILITY_REJECT_REGISTER_INDEX: u32 = 2;
    pub(crate) const ASYNC_GENERATOR_OBJECT_REGISTER_INDEX: u32 = 3;

    /// Creates a new `CallFrame` with the provided `CodeBlock`.
    pub(crate) fn new(
        code_block: Gc<CodeBlock>,
        active_runnable: Option<ActiveRunnable>,
        environments: EnvironmentStack,
        realm: Realm,
    ) -> Self {
        Self {
            code_block,
            pc: 0,
            rp: 0,
            env_fp: 0,
            argument_count: 0,
            iterators: ThinVec::new(),
            binding_stack: Vec::new(),
            loop_iteration_count: 0,
            active_runnable,
            environments,
            realm,
            flags: CallFrameFlags::empty(),
        }
    }

    /// Updates a `CallFrame`'s `argument_count` field with the value provided.
    pub(crate) fn with_argument_count(mut self, count: u32) -> Self {
        self.argument_count = count;
        self
    }

    /// Updates a `CallFrame`'s `env_fp` field with the value provided.
    pub(crate) fn with_env_fp(mut self, env_fp: u32) -> Self {
        self.env_fp = env_fp;
        self
    }

    /// Updates a `CallFrame`'s `flags` field with the value provided.
    pub(crate) fn with_flags(mut self, flags: CallFrameFlags) -> Self {
        self.flags = flags;
        self
    }

    pub(crate) fn this(&self, vm: &Vm) -> JsValue {
        let this_index = self.rp - self.argument_count - Self::THIS_POSITION;
        vm.stack[this_index as usize].clone()
    }

    pub(crate) fn function(&self, vm: &Vm) -> Option<JsObject> {
        let function_index = self.rp - self.argument_count - Self::FUNCTION_POSITION;
        if let Some(object) = vm.stack[function_index as usize].as_object() {
            return Some(object.clone());
        }

        None
    }

    pub(crate) fn arguments<'stack>(&self, vm: &'stack Vm) -> &'stack [JsValue] {
        let rp = self.rp as usize;
        let argument_count = self.argument_count as usize;
        let arguments_start = rp - argument_count;
        &vm.stack[arguments_start..rp]
    }

    pub(crate) fn argument<'stack>(&self, index: usize, vm: &'stack Vm) -> Option<&'stack JsValue> {
        self.arguments(vm).get(index)
    }

    pub(crate) fn fp(&self) -> u32 {
        self.rp - self.argument_count - Self::FUNCTION_PROLOGUE
    }

    pub(crate) fn restore_stack(&self, vm: &mut Vm) {
        let fp = self.fp();
        vm.stack.truncate(fp as usize);
    }

    /// Returns the async generator object, if the function that this [`CallFrame`] is from an async generator, [`None`] otherwise.
    pub(crate) fn async_generator_object(&self, stack: &[JsValue]) -> Option<JsObject> {
        if !self.code_block().is_async_generator() {
            return None;
        }

        self.register(Self::ASYNC_GENERATOR_OBJECT_REGISTER_INDEX, stack)
            .as_object()
            .cloned()
    }

    pub(crate) fn promise_capability(&self, stack: &[JsValue]) -> Option<PromiseCapability> {
        if !self.code_block().is_async() {
            return None;
        }

        let promise = self
            .register(Self::PROMISE_CAPABILITY_PROMISE_REGISTER_INDEX, stack)
            .as_object()
            .cloned()?;
        let resolve = self
            .register(Self::PROMISE_CAPABILITY_RESOLVE_REGISTER_INDEX, stack)
            .as_object()
            .cloned()
            .and_then(JsFunction::from_object)?;
        let reject = self
            .register(Self::PROMISE_CAPABILITY_REJECT_REGISTER_INDEX, stack)
            .as_object()
            .cloned()
            .and_then(JsFunction::from_object)?;

        Some(PromiseCapability {
            promise,
            functions: ResolvingFunctions { resolve, reject },
        })
    }

    pub(crate) fn set_promise_capability(
        &self,
        stack: &mut [JsValue],
        promise_capability: Option<&PromiseCapability>,
    ) {
        debug_assert!(
            self.code_block().is_async(),
            "Only async functions have a promise capability"
        );

        self.set_register(
            Self::PROMISE_CAPABILITY_PROMISE_REGISTER_INDEX,
            promise_capability
                .map(PromiseCapability::promise)
                .cloned()
                .map_or_else(JsValue::undefined, Into::into),
            stack,
        );
        self.set_register(
            Self::PROMISE_CAPABILITY_RESOLVE_REGISTER_INDEX,
            promise_capability
                .map(PromiseCapability::resolve)
                .cloned()
                .map_or_else(JsValue::undefined, Into::into),
            stack,
        );
        self.set_register(
            Self::PROMISE_CAPABILITY_REJECT_REGISTER_INDEX,
            promise_capability
                .map(PromiseCapability::reject)
                .cloned()
                .map_or_else(JsValue::undefined, Into::into),
            stack,
        );
    }

    /// Returns the register at the given index.
    ///
    /// # Panics
    ///
    /// If the index is out of bounds.
    pub(crate) fn register<'stack>(&self, index: u32, stack: &'stack [JsValue]) -> &'stack JsValue {
        debug_assert!(index < self.code_block().register_count);
        let at = self.rp + index;
        &stack[at as usize]
    }

    /// Sets the register at the given index.
    ///
    /// # Panics
    ///
    /// If the index is out of bounds.
    pub(crate) fn set_register(&self, index: u32, value: JsValue, stack: &mut [JsValue]) {
        debug_assert!(index < self.code_block().register_count);
        let at = self.rp + index;
        stack[at as usize] = value;
    }

    /// Does this have the [`CallFrameFlags::EXIT_EARLY`] flag.
    pub(crate) fn exit_early(&self) -> bool {
        self.flags.contains(CallFrameFlags::EXIT_EARLY)
    }
    /// Set the [`CallFrameFlags::EXIT_EARLY`] flag.
    pub(crate) fn set_exit_early(&mut self, early_exit: bool) {
        self.flags.set(CallFrameFlags::EXIT_EARLY, early_exit);
    }
    /// Does this have the [`CallFrameFlags::CONSTRUCT`] flag.
    pub(crate) fn construct(&self) -> bool {
        self.flags.contains(CallFrameFlags::CONSTRUCT)
    }
    /// Does this [`CallFrame`] need to push registers on [`Vm::push_frame()`].
    pub(crate) fn registers_already_pushed(&self) -> bool {
        self.flags
            .contains(CallFrameFlags::REGISTERS_ALREADY_PUSHED)
    }
}

/// ---- `CallFrame` stack methods ----
impl CallFrame {
    pub(crate) fn set_register_pointer(&mut self, pointer: u32) {
        self.rp = pointer;
    }
}

/// Indicates how a generator function that has been called/resumed should return.
#[derive(Copy, Clone, Debug, PartialEq, Eq, Default)]
#[repr(u8)]
#[allow(missing_docs)]
pub enum GeneratorResumeKind {
    #[default]
    Normal = 0,
    Throw,
    Return,
}

impl From<GeneratorResumeKind> for JsValue {
    fn from(value: GeneratorResumeKind) -> Self {
        Self::new(value as u8)
    }
}

impl JsValue {
    /// Convert value to [`GeneratorResumeKind`].
    ///
    /// # Panics
    ///
    /// If not a integer type or not in the range `1..=2`.
    #[track_caller]
    pub(crate) fn to_generator_resume_kind(&self) -> GeneratorResumeKind {
        if let Self::Integer(value) = self {
            match *value {
                0 => return GeneratorResumeKind::Normal,
                1 => return GeneratorResumeKind::Throw,
                2 => return GeneratorResumeKind::Return,
                _ => unreachable!("generator kind must be a integer between 1..=2, got {value}"),
            }
        }

        unreachable!("generator kind must be a integer type")
    }
}