-
-
Notifications
You must be signed in to change notification settings - Fork 622
Expand file tree
/
Copy pathproperty.rs
More file actions
286 lines (253 loc) · 9.37 KB
/
property.rs
File metadata and controls
286 lines (253 loc) · 9.37 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
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
use boa_string::StaticJsStrings;
use crate::{
Context, JsExpect, JsResult, JsValue, js_string,
object::{internal_methods::InternalMethodPropertyContext, shape::slot::SlotAttributes},
property::PropertyKey,
vm::opcode::{IndexOperand, Operation, RegisterOperand},
};
fn get_by_name<const LENGTH: bool>(
(dst, object, receiver, index): (RegisterOperand, &JsValue, &JsValue, IndexOperand),
context: &mut Context,
) -> JsResult<()> {
if LENGTH {
if let Some(object) = object.as_object()
&& object.is_array()
{
let value = object.borrow().properties().storage[0].clone();
context.vm.set_register(dst.into(), value);
return Ok(());
} else if let Some(string) = object.as_string() {
// NOTE: Since we’re using the prototype returned directly by `base_class()`,
// we need to handle string primitives separately due to the
// string exotic internal methods.
context
.vm
.set_register(dst.into(), (string.len() as u32).into());
return Ok(());
}
}
// OPTIMIZATION:
// Instead of calling `to_object()`, which creates a temporary wrapper object for primitive
// values (e.g., numbers, strings, booleans) just to query their prototype chain.
//
// To prevent the creation of a temporary JsObject, we directly retrieve the prototype that
// `to_object()` would produce, such as `Number.prototype`, `String.prototype`, etc.
let object = object.base_class(context)?;
let ic = &context.vm.frame().code_block().ic[usize::from(index)];
let object_borrowed = object.borrow();
if let Some((shape, slot)) = ic.get(object_borrowed.shape()) {
let mut result = if slot.attributes.contains(SlotAttributes::PROTOTYPE) {
let prototype = shape.prototype().js_expect("prototype should have value")?;
let prototype = prototype.borrow();
prototype.properties().storage[slot.index as usize].clone()
} else {
object_borrowed.properties().storage[slot.index as usize].clone()
};
drop(object_borrowed);
if slot.attributes.has_get() && result.is_object() {
result = result
.as_object()
.js_expect("should contain getter")?
.call(receiver, &[], context)?;
}
context.vm.set_register(dst.into(), result);
return Ok(());
}
drop(object_borrowed);
let key: PropertyKey = ic.name.clone().into();
let context = &mut InternalMethodPropertyContext::new(context);
let result = object.__get__(&key, receiver.clone(), context)?;
// Cache the property.
let slot = *context.slot();
if slot.is_cacheable() {
let ic = &context.vm.frame().code_block.ic[usize::from(index)];
let object_borrowed = object.borrow();
let shape = object_borrowed.shape();
ic.set(shape, slot);
}
context.vm.set_register(dst.into(), result);
Ok(())
}
fn get_by_value<const PUSH_KEY: bool>(
(dst, key, receiver, object): (
RegisterOperand,
RegisterOperand,
RegisterOperand,
RegisterOperand,
),
context: &mut Context,
) -> JsResult<()> {
let key_value = context.vm.get_register(key.into()).clone();
let base = context.vm.get_register(object.into()).clone();
let object = base.base_class(context)?;
let key_value = key_value.to_property_key(context)?;
// Fast Path
//
// NOTE: Since we’re using the prototype returned directly by `base_class()`,
// we need to handle string primitives separately due to the
// string exotic internal methods.
match &key_value {
PropertyKey::Index(index) => {
if object.is_array() {
let object_borrowed = object.borrow();
if let Some(element) = object_borrowed.properties().get_dense_property(index.get())
{
if PUSH_KEY {
context.vm.set_register(key.into(), key_value.into());
}
context.vm.set_register(dst.into(), element);
return Ok(());
}
} else if let Some(string) = base.as_string() {
let value = string
.code_unit_at(index.get() as usize)
.map_or_else(JsValue::undefined, |char| {
js_string!([char].as_slice()).into()
});
if PUSH_KEY {
context.vm.set_register(key.into(), key_value.into());
}
context.vm.set_register(dst.into(), value);
return Ok(());
}
}
PropertyKey::String(string) if *string == StaticJsStrings::LENGTH => {
if let Some(string) = base.as_string() {
let value = string.len().into();
if PUSH_KEY {
context.vm.set_register(key.into(), key_value.into());
}
context.vm.set_register(dst.into(), value);
return Ok(());
}
}
_ => {}
}
let receiver = context.vm.get_register(receiver.into());
// Slow path:
let result = object.__get__(
&key_value,
receiver.clone(),
&mut InternalMethodPropertyContext::new(context),
)?;
if PUSH_KEY {
context.vm.set_register(key.into(), key_value.into());
}
context.vm.set_register(dst.into(), result);
Ok(())
}
#[derive(Debug, Clone, Copy)]
pub(crate) struct GetLengthProperty;
impl GetLengthProperty {
#[inline(always)]
pub(crate) fn operation(
(dst, object, index): (RegisterOperand, RegisterOperand, IndexOperand),
context: &mut Context,
) -> JsResult<()> {
let object = context.vm.get_register(object.into()).clone();
get_by_name::<true>((dst, &object, &object, index), context)
}
}
impl Operation for GetLengthProperty {
const NAME: &'static str = "GetLengthProperty";
const INSTRUCTION: &'static str = "INST - GetLengthProperty";
const COST: u8 = 4;
}
/// `GetPropertyByName` implements the Opcode Operation for `Opcode::GetPropertyByName`
///
/// Operation:
/// - Get a property by name from an object.
#[derive(Debug, Clone, Copy)]
pub(crate) struct GetPropertyByName;
impl GetPropertyByName {
#[inline(always)]
pub(crate) fn operation(
(dst, object, index): (RegisterOperand, RegisterOperand, IndexOperand),
context: &mut Context,
) -> JsResult<()> {
let object = context.vm.get_register(object.into()).clone();
get_by_name::<false>((dst, &object, &object, index), context)
}
}
impl Operation for GetPropertyByName {
const NAME: &'static str = "GetPropertyByName";
const INSTRUCTION: &'static str = "INST - GetPropertyByName";
const COST: u8 = 4;
}
/// `GetPropertyByNameWithThis` implements the Opcode Operation for `Opcode::GetPropertyByNameWithThis`
///
/// Operation:
/// - Get a property by name from an object with this.
#[derive(Debug, Clone, Copy)]
pub(crate) struct GetPropertyByNameWithThis;
impl GetPropertyByNameWithThis {
#[inline(always)]
pub(crate) fn operation(
(dst, receiver, value, index): (
RegisterOperand,
RegisterOperand,
RegisterOperand,
IndexOperand,
),
context: &mut Context,
) -> JsResult<()> {
let receiver = context.vm.get_register(receiver.into()).clone();
let object = context.vm.get_register(value.into()).clone();
get_by_name::<false>((dst, &object, &receiver, index), context)
}
}
impl Operation for GetPropertyByNameWithThis {
const NAME: &'static str = "GetPropertyByNameWithThis";
const INSTRUCTION: &'static str = "INST - GetPropertyByNameWithThis";
const COST: u8 = 4;
}
/// `GetPropertyByValue` implements the Opcode Operation for `Opcode::GetPropertyByValue`
///
/// Operation:
/// - Get a property by value from an object and store it in dst.
#[derive(Debug, Clone, Copy)]
pub(crate) struct GetPropertyByValue;
impl GetPropertyByValue {
#[inline(always)]
pub(crate) fn operation(
args: (
RegisterOperand,
RegisterOperand,
RegisterOperand,
RegisterOperand,
),
context: &mut Context,
) -> JsResult<()> {
get_by_value::<false>(args, context)
}
}
impl Operation for GetPropertyByValue {
const NAME: &'static str = "GetPropertyByValue";
const INSTRUCTION: &'static str = "INST - GetPropertyByValue";
const COST: u8 = 4;
}
/// `GetPropertyByValuePush` implements the Opcode Operation for `Opcode::GetPropertyByValuePush`
///
/// Operation:
/// - Get a property by value from an object and store the key and value in registers.
#[derive(Debug, Clone, Copy)]
pub(crate) struct GetPropertyByValuePush;
impl GetPropertyByValuePush {
#[inline(always)]
pub(crate) fn operation(
args: (
RegisterOperand,
RegisterOperand,
RegisterOperand,
RegisterOperand,
),
context: &mut Context,
) -> JsResult<()> {
get_by_value::<true>(args, context)
}
}
impl Operation for GetPropertyByValuePush {
const NAME: &'static str = "GetPropertyByValuePush";
const INSTRUCTION: &'static str = "INST - GetPropertyByValuePush";
const COST: u8 = 4;
}