boa_engine 0.17.0

Boa is a Javascript lexer, parser and compiler written in Rust. Currently, it has support for some of the language.
Documentation
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
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
use crate::{
    builtins::{function::FunctionKind, promise::PromiseCapability, Promise},
    error::JsNativeError,
    module::{ModuleKind, Referrer},
    object::FunctionObjectBuilder,
    vm::{opcode::Operation, CompletionType},
    Context, JsResult, JsValue, NativeFunction,
};

/// `CallEval` implements the Opcode Operation for `Opcode::CallEval`
///
/// Operation:
///  - Call a function named "eval".
#[derive(Debug, Clone, Copy)]
pub(crate) struct CallEval;

impl Operation for CallEval {
    const NAME: &'static str = "CallEval";
    const INSTRUCTION: &'static str = "INST - CallEval";

    fn execute(context: &mut Context<'_>) -> JsResult<CompletionType> {
        if context.vm.runtime_limits.recursion_limit() <= context.vm.frames.len() {
            return Err(JsNativeError::runtime_limit()
                .with_message(format!(
                    "Maximum recursion limit {} exceeded",
                    context.vm.runtime_limits.recursion_limit()
                ))
                .into());
        }
        if context.vm.runtime_limits.stack_size_limit() <= context.vm.stack.len() {
            return Err(JsNativeError::runtime_limit()
                .with_message("Maximum call stack size exceeded")
                .into());
        }
        let argument_count = context.vm.read::<u32>();
        let mut arguments = Vec::with_capacity(argument_count as usize);
        for _ in 0..argument_count {
            arguments.push(context.vm.pop());
        }
        arguments.reverse();

        let func = context.vm.pop();
        let this = context.vm.pop();

        let object = match func {
            JsValue::Object(ref object) if object.is_callable() => object.clone(),
            _ => {
                return Err(JsNativeError::typ()
                    .with_message("not a callable function")
                    .into());
            }
        };

        // A native function with the name "eval" implies, that is this the built-in eval function.
        let eval = object
            .borrow()
            .as_function()
            .map(|f| matches!(f.kind(), FunctionKind::Native { .. }))
            .unwrap_or_default();

        let strict = context.vm.frame().code_block.strict();

        if eval {
            if let Some(x) = arguments.get(0) {
                let result = crate::builtins::eval::Eval::perform_eval(x, true, strict, context)?;
                context.vm.push(result);
            } else {
                context.vm.push(JsValue::Undefined);
            }
        } else {
            let result = object.__call__(&this, &arguments, context)?;
            context.vm.push(result);
        }
        Ok(CompletionType::Normal)
    }
}

/// `CallEvalSpread` implements the Opcode Operation for `Opcode::CallEvalSpread`
///
/// Operation:
///  - Call a function named "eval" where the arguments contain spreads.
#[derive(Debug, Clone, Copy)]
pub(crate) struct CallEvalSpread;

impl Operation for CallEvalSpread {
    const NAME: &'static str = "CallEvalSpread";
    const INSTRUCTION: &'static str = "INST - CallEvalSpread";

    fn execute(context: &mut Context<'_>) -> JsResult<CompletionType> {
        if context.vm.runtime_limits.recursion_limit() <= context.vm.frames.len() {
            return Err(JsNativeError::runtime_limit()
                .with_message(format!(
                    "Maximum recursion limit {} exceeded",
                    context.vm.runtime_limits.recursion_limit()
                ))
                .into());
        }
        if context.vm.runtime_limits.stack_size_limit() <= context.vm.stack.len() {
            return Err(JsNativeError::runtime_limit()
                .with_message("Maximum call stack size exceeded")
                .into());
        }

        // Get the arguments that are stored as an array object on the stack.
        let arguments_array = context.vm.pop();
        let arguments_array_object = arguments_array
            .as_object()
            .expect("arguments array in call spread function must be an object");
        let arguments = arguments_array_object
            .borrow()
            .properties()
            .dense_indexed_properties()
            .expect("arguments array in call spread function must be dense")
            .clone();

        let func = context.vm.pop();
        let this = context.vm.pop();

        let object = match func {
            JsValue::Object(ref object) if object.is_callable() => object.clone(),
            _ => {
                return Err(JsNativeError::typ()
                    .with_message("not a callable function")
                    .into());
            }
        };

        // A native function with the name "eval" implies, that is this the built-in eval function.
        let eval = object
            .borrow()
            .as_function()
            .map(|f| matches!(f.kind(), FunctionKind::Native { .. }))
            .unwrap_or_default();

        let strict = context.vm.frame().code_block.strict();

        if eval {
            if let Some(x) = arguments.get(0) {
                let result = crate::builtins::eval::Eval::perform_eval(x, true, strict, context)?;
                context.vm.push(result);
            } else {
                context.vm.push(JsValue::Undefined);
            }
        } else {
            let result = object.__call__(&this, &arguments, context)?;
            context.vm.push(result);
        }
        Ok(CompletionType::Normal)
    }
}

/// `Call` implements the Opcode Operation for `Opcode::Call`
///
/// Operation:
///  - Call a function
#[derive(Debug, Clone, Copy)]
pub(crate) struct Call;

impl Operation for Call {
    const NAME: &'static str = "Call";
    const INSTRUCTION: &'static str = "INST - Call";

    fn execute(context: &mut Context<'_>) -> JsResult<CompletionType> {
        if context.vm.runtime_limits.recursion_limit() <= context.vm.frames.len() {
            return Err(JsNativeError::runtime_limit()
                .with_message(format!(
                    "Maximum recursion limit {} exceeded",
                    context.vm.runtime_limits.recursion_limit()
                ))
                .into());
        }
        if context.vm.runtime_limits.stack_size_limit() <= context.vm.stack.len() {
            return Err(JsNativeError::runtime_limit()
                .with_message("Maximum call stack size exceeded")
                .into());
        }
        let argument_count = context.vm.read::<u32>();
        let mut arguments = Vec::with_capacity(argument_count as usize);
        for _ in 0..argument_count {
            arguments.push(context.vm.pop());
        }
        arguments.reverse();

        let func = context.vm.pop();
        let this = context.vm.pop();

        let object = match func {
            JsValue::Object(ref object) if object.is_callable() => object.clone(),
            _ => {
                return Err(JsNativeError::typ()
                    .with_message("not a callable function")
                    .into());
            }
        };

        let result = object.__call__(&this, &arguments, context)?;

        context.vm.push(result);
        Ok(CompletionType::Normal)
    }
}

#[derive(Debug, Clone, Copy)]
pub(crate) struct CallSpread;

impl Operation for CallSpread {
    const NAME: &'static str = "CallSpread";
    const INSTRUCTION: &'static str = "INST - CallSpread";

    fn execute(context: &mut Context<'_>) -> JsResult<CompletionType> {
        if context.vm.runtime_limits.recursion_limit() <= context.vm.frames.len() {
            return Err(JsNativeError::runtime_limit()
                .with_message(format!(
                    "Maximum recursion limit {} exceeded",
                    context.vm.runtime_limits.recursion_limit()
                ))
                .into());
        }
        if context.vm.runtime_limits.stack_size_limit() <= context.vm.stack.len() {
            return Err(JsNativeError::runtime_limit()
                .with_message("Maximum call stack size exceeded")
                .into());
        }

        // Get the arguments that are stored as an array object on the stack.
        let arguments_array = context.vm.pop();
        let arguments_array_object = arguments_array
            .as_object()
            .expect("arguments array in call spread function must be an object");
        let arguments = arguments_array_object
            .borrow()
            .properties()
            .dense_indexed_properties()
            .expect("arguments array in call spread function must be dense")
            .clone();

        let func = context.vm.pop();
        let this = context.vm.pop();

        let object = match func {
            JsValue::Object(ref object) if object.is_callable() => object.clone(),
            _ => {
                return Err(JsNativeError::typ()
                    .with_message("not a callable function")
                    .into())
            }
        };

        let result = object.__call__(&this, &arguments, context)?;

        context.vm.push(result);
        Ok(CompletionType::Normal)
    }
}

/// `ImportCall` implements the Opcode Operation for `Opcode::ImportCall`
///
/// Operation:
///  - Dynamically imports a module
#[derive(Debug, Clone, Copy)]
pub(crate) struct ImportCall;

impl Operation for ImportCall {
    const NAME: &'static str = "ImportCall";
    const INSTRUCTION: &'static str = "INST - ImportCall";

    fn execute(context: &mut Context<'_>) -> JsResult<CompletionType> {
        // Import Calls
        // Runtime Semantics: Evaluation
        // https://tc39.es/ecma262/#sec-import-call-runtime-semantics-evaluation

        // 1. Let referrer be GetActiveScriptOrModule().
        // 2. If referrer is null, set referrer to the current Realm Record.
        let referrer = context
            .vm
            .active_runnable
            .clone()
            .map_or_else(|| Referrer::Realm(context.realm().clone()), Into::into);

        // 3. Let argRef be ? Evaluation of AssignmentExpression.
        // 4. Let specifier be ? GetValue(argRef).
        let arg = context.vm.pop();

        // 5. Let promiseCapability be ! NewPromiseCapability(%Promise%).
        let cap = PromiseCapability::new(
            &context.intrinsics().constructors().promise().constructor(),
            context,
        )
        .expect("operation cannot fail for the %Promise% intrinsic");
        let promise = cap.promise().clone();

        // 6. Let specifierString be Completion(ToString(specifier)).
        match arg.to_string(context) {
            // 7. IfAbruptRejectPromise(specifierString, promiseCapability).
            Err(err) => {
                let err = err.to_opaque(context);
                cap.reject().call(&JsValue::undefined(), &[err], context)?;
            }
            // 8. Perform HostLoadImportedModule(referrer, specifierString, empty, promiseCapability).
            Ok(specifier) => context.module_loader().load_imported_module(
                referrer.clone(),
                specifier.clone(),
                Box::new(move |completion, context| {
                    // `ContinueDynamicImport ( promiseCapability, moduleCompletion )`
                    // https://tc39.es/ecma262/#sec-ContinueDynamicImport

                    // `FinishLoadingImportedModule ( referrer, specifier, payload, result )`
                    // https://tc39.es/ecma262/#sec-FinishLoadingImportedModule
                    let module = match completion {
                        // 1. If result is a normal completion, then
                        Ok(m) => {
                            match referrer {
                                Referrer::Module(module) => {
                                    let ModuleKind::SourceText(src) = module.kind() else {
                                        panic!("referrer cannot be a synthetic module");
                                    };

                                    let sym = context.interner_mut().get_or_intern(&*specifier);

                                    let mut loaded_modules = src.loaded_modules().borrow_mut();

                                    //     a. If referrer.[[LoadedModules]] contains a Record whose [[Specifier]] is specifier, then
                                    //     b. Else,
                                    //         i. Append the Record { [[Specifier]]: specifier, [[Module]]: result.[[Value]] } to referrer.[[LoadedModules]].
                                    let entry =
                                        loaded_modules.entry(sym).or_insert_with(|| m.clone());

                                    //         i. Assert: That Record's [[Module]] is result.[[Value]].
                                    debug_assert_eq!(&m, entry);

                                    // Same steps apply to referrers below
                                }
                                Referrer::Realm(realm) => {
                                    let mut loaded_modules = realm.loaded_modules().borrow_mut();
                                    let entry = loaded_modules
                                        .entry(specifier)
                                        .or_insert_with(|| m.clone());
                                    debug_assert_eq!(&m, entry);
                                }
                                Referrer::Script(script) => {
                                    let mut loaded_modules = script.loaded_modules().borrow_mut();
                                    let entry = loaded_modules
                                        .entry(specifier)
                                        .or_insert_with(|| m.clone());
                                    debug_assert_eq!(&m, entry);
                                }
                            }

                            m
                        }
                        // 1. If moduleCompletion is an abrupt completion, then
                        Err(err) => {
                            // a. Perform ! Call(promiseCapability.[[Reject]], undefined, « moduleCompletion.[[Value]] »).
                            let err = err.to_opaque(context);
                            cap.reject()
                                .call(&JsValue::undefined(), &[err], context)
                                .expect("default `reject` function cannot throw");

                            // b. Return unused.
                            return;
                        }
                    };

                    // 2. Let module be moduleCompletion.[[Value]].
                    // 3. Let loadPromise be module.LoadRequestedModules().
                    let load = module.load(context);

                    // 4. Let rejectedClosure be a new Abstract Closure with parameters (reason) that captures promiseCapability and performs the following steps when called:
                    // 5. Let onRejected be CreateBuiltinFunction(rejectedClosure, 1, "", « »).
                    let on_rejected = FunctionObjectBuilder::new(
                        context,
                        NativeFunction::from_copy_closure_with_captures(
                            |_, args, cap, context| {
                                //     a. Perform ! Call(promiseCapability.[[Reject]], undefined, « reason »).
                                cap.reject()
                                    .call(&JsValue::undefined(), args, context)
                                    .expect("default `reject` function cannot throw");

                                //     b. Return unused.
                                Ok(JsValue::undefined())
                            },
                            cap.clone(),
                        ),
                    )
                    .build();

                    // 6. Let linkAndEvaluateClosure be a new Abstract Closure with no parameters that captures module, promiseCapability, and onRejected and performs the following steps when called:
                    // 7. Let linkAndEvaluate be CreateBuiltinFunction(linkAndEvaluateClosure, 0, "", « »).
                    let link_evaluate = FunctionObjectBuilder::new(
                        context,
                        NativeFunction::from_copy_closure_with_captures(
                            |_, _, (module, cap, on_rejected), context| {
                                // a. Let link be Completion(module.Link()).
                                // b. If link is an abrupt completion, then
                                if let Err(e) = module.link(context) {
                                    // i. Perform ! Call(promiseCapability.[[Reject]], undefined, « link.[[Value]] »).
                                    let e = e.to_opaque(context);
                                    cap.reject()
                                        .call(&JsValue::undefined(), &[e], context)
                                        .expect("default `reject` function cannot throw");
                                    // ii. Return unused.
                                    return Ok(JsValue::undefined());
                                }

                                // c. Let evaluatePromise be module.Evaluate().
                                let evaluate = module.evaluate(context);

                                // d. Let fulfilledClosure be a new Abstract Closure with no parameters that captures module and promiseCapability and performs the following steps when called:
                                // e. Let onFulfilled be CreateBuiltinFunction(fulfilledClosure, 0, "", « »).
                                let fulfill = FunctionObjectBuilder::new(
                                    context,
                                    NativeFunction::from_copy_closure_with_captures(
                                        |_, _, (module, cap), context| {
                                            // i. Let namespace be GetModuleNamespace(module).
                                            let namespace = module.namespace(context);

                                            // ii. Perform ! Call(promiseCapability.[[Resolve]], undefined, « namespace »).
                                            cap.resolve()
                                                .call(
                                                    &JsValue::undefined(),
                                                    &[namespace.into()],
                                                    context,
                                                )
                                                .expect("default `resolve` function cannot throw");

                                            // iii. Return unused.
                                            Ok(JsValue::undefined())
                                        },
                                        (module.clone(), cap.clone()),
                                    ),
                                )
                                .build();

                                // f. Perform PerformPromiseThen(evaluatePromise, onFulfilled, onRejected).
                                Promise::perform_promise_then(
                                    &evaluate,
                                    Some(fulfill),
                                    Some(on_rejected.clone()),
                                    None,
                                    context,
                                );

                                // g. Return unused.
                                Ok(JsValue::undefined())
                            },
                            (module.clone(), cap.clone(), on_rejected.clone()),
                        ),
                    )
                    .build();

                    // 8. Perform PerformPromiseThen(loadPromise, linkAndEvaluate, onRejected).
                    Promise::perform_promise_then(
                        &load,
                        Some(link_evaluate),
                        Some(on_rejected),
                        None,
                        context,
                    );

                    // 9. Return unused.
                }),
                context,
            ),
        };

        // 9. Return promiseCapability.[[Promise]].
        context.vm.push(promise);

        Ok(CompletionType::Normal)
    }
}