-
-
Notifications
You must be signed in to change notification settings - Fork 613
Expand file tree
/
Copy pathmod.rs
More file actions
82 lines (69 loc) · 2.5 KB
/
mod.rs
File metadata and controls
82 lines (69 loc) · 2.5 KB
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
use super::IndexOperand;
use crate::{Context, JsExpect, JsResult, error::JsNativeError, vm::opcode::Operation};
/// `New` implements the Opcode Operation for `Opcode::New`
///
/// Operation:
/// - Call construct on a function.
#[derive(Debug, Clone, Copy)]
pub(crate) struct New;
impl New {
#[inline(always)]
pub(super) fn operation(argument_count: IndexOperand, context: &mut Context) -> JsResult<()> {
let func = context
.vm
.stack
.calling_convention_get_function(argument_count.into());
let cons = func
.as_object()
.ok_or_else(|| JsNativeError::typ().with_message("not a constructor"))?
.clone();
context.vm.stack.push(cons.clone()); // Push new.target
cons.__construct__(argument_count.into()).resolve(context)?;
Ok(())
}
}
impl Operation for New {
const NAME: &'static str = "New";
const INSTRUCTION: &'static str = "INST - New";
const COST: u8 = 3;
}
/// `NewSpread` implements the Opcode Operation for `Opcode::NewSpread`
///
/// Operation:
/// - Call construct on a function where the arguments contain spreads.
#[derive(Debug, Clone, Copy)]
pub(crate) struct NewSpread;
impl NewSpread {
#[inline(always)]
pub(super) fn operation((): (), context: &mut Context) -> JsResult<()> {
// Get the arguments that are stored as an array object on the stack.
let arguments_array = context.vm.stack.pop();
let arguments_array_object = arguments_array
.as_object()
.js_expect("arguments array in call spread function must be an object")?;
let arguments = arguments_array_object
.borrow()
.properties()
.to_dense_indexed_properties()
.js_expect("arguments array in call spread function must be dense")?;
let func = context.vm.stack.pop();
let cons = func
.as_object()
.ok_or_else(|| JsNativeError::typ().with_message("not a constructor"))?
.clone();
let argument_count = arguments.len();
context.vm.stack.push(func);
context
.vm
.stack
.calling_convention_push_arguments(&arguments);
context.vm.stack.push(cons.clone()); // Push new.target
cons.__construct__(argument_count).resolve(context)?;
Ok(())
}
}
impl Operation for NewSpread {
const NAME: &'static str = "NewSpread";
const INSTRUCTION: &'static str = "INST - NewSpread";
const COST: u8 = 3;
}