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
//! `Reflect`.
//!
//! `Reflect` duplicates operations the language performs implicitly, with one
//! difference that matters: it reports success as a boolean instead of throwing or
//! silently doing nothing. That is why `Reflect.defineProperty` is the safe way to
//! install a property and why the test suite reaches for `Reflect.apply` when it
//! wants to call a function without spreading the arguments.
use crate::builtins::{arg, define_method, install_tag};
use crate::interp::{Interp, JsResult};
use crate::object::Property;
use crate::value::Value;
/// Install `Reflect`.
pub fn install(interp: &mut Interp<'_>) {
let object_proto = interp.realm.object_proto.clone();
let reflect = interp.new_object(Some(object_proto));
// §28.1: `Object.prototype.toString.call(Reflect)` is `[object Reflect]`.
install_tag(&reflect, "Reflect", interp);
for (name, length, func) in [
("apply", 3, apply as crate::object::NativeFn),
("construct", 2, construct),
("defineProperty", 3, define_property),
("deleteProperty", 2, delete_property),
("get", 2, get),
("set", 3, set),
("has", 2, has),
("ownKeys", 1, own_keys),
("getOwnPropertyDescriptor", 2, get_own_property_descriptor),
("getPrototypeOf", 1, get_prototype_of),
("setPrototypeOf", 2, set_prototype_of),
("isExtensible", 1, is_extensible),
("preventExtensions", 1, prevent_extensions),
] {
// `define_method` already installs `{ writable: true, enumerable: false,
// configurable: true }`, which is exactly what §28.1 asks for — `prop-desc` reads
// `enumerable` back and asserts `false`.
define_method(&reflect, name, length, func, interp);
}
let global = interp.realm.global.clone();
// The global binding is `{ writable: true, enumerable: false, configurable: true }`,
// which `define_data` (enumerable) does not give — `prop-desc` verifies all three.
global.define(
"Reflect",
Property::with_attributes(Value::Object(reflect), true, false),
);
}
fn apply(interp: &mut Interp<'_>, _this: Value, args: &[Value]) -> JsResult<Value> {
let target = arg(args, 0);
let this_arg = arg(args, 1);
// Unlike `Function.prototype.apply`, `Reflect.apply` requires an arguments list;
// `undefined` is a TypeError rather than an empty call.
let rest = match arg(args, 2) {
Value::Object(object) => {
let length = interp
.get_property(&object, "length")?
.map(|value| value.to_number().max(0.0) as usize)
.unwrap_or(0);
let mut out = Vec::with_capacity(length);
for index in 0..length {
out.push(
interp
.get_property(&object, &index.to_string())?
.unwrap_or(Value::Undefined),
);
}
out
}
_ => return Err(interp.type_error("Reflect.apply arguments must be an array-like")),
};
interp.call_value(target, this_arg, rest)
}
fn construct(interp: &mut Interp<'_>, _this: Value, args: &[Value]) -> JsResult<Value> {
let target = arg(args, 0);
let rest = array_like(interp, &arg(args, 1))?;
// `Reflect.construct(target, args, newTarget)`: the third argument decides the
// instance's prototype, which is the whole point of the three-argument form — it is how
// a subclass is built without calling its constructor. Dropping it made
// `Object.getPrototypeOf(Reflect.construct(C, [], D))` answer `C.prototype`.
//
// The `IsConstructor` check covers only the *supplied* third argument, which is exactly
// where the reference puts it. With two arguments the constructor is whatever the
// target is, and a non-constructor target is refused by `[[Construct]]` instead —
// the same `TypeError`, but reached one layer deeper, so its frame is not ours to
// invent here.
let new_target = match args.get(2) {
Some(Value::Undefined) | None => target.clone(),
Some(candidate) => {
if !matches!(candidate, Value::Object(o) if o.is_constructor()) {
return Err(interp.not_a_constructor_error_for(candidate.clone()));
}
candidate.clone()
}
};
interp.construct(target, new_target, rest)
}
/// Read an array-like argument into a vector.
fn array_like(interp: &mut Interp<'_>, value: &Value) -> JsResult<Vec<Value>> {
match value {
Value::Undefined | Value::Null => Ok(Vec::new()),
Value::Object(object) => {
let length = interp
.get_property(object, "length")?
.map(|value| value.to_number().max(0.0) as usize)
.unwrap_or(0);
let mut out = Vec::with_capacity(length);
for index in 0..length {
out.push(
interp
.get_property(object, &index.to_string())?
.unwrap_or(Value::Undefined),
);
}
Ok(out)
}
_ => Err(interp.type_error("expected an array-like")),
}
}
/// `Reflect.defineProperty`: like `Object.defineProperty` but returns a boolean.
fn define_property(interp: &mut Interp<'_>, _this: Value, args: &[Value]) -> JsResult<Value> {
let target = arg(args, 0);
let Some(object) = target.as_object().cloned() else {
return Err(interp.type_error("Reflect.defineProperty target is not an object"));
};
// The key has to be a real `PropertyKey`, not its string form: `to_js_string()` on a
// symbol yields `"Symbol()"`, so a symbol-keyed property read as absent and every
// definition on a non-extensible object answered `false` — which is what made
// `Object.freeze` of a symbol-keyed object fail at its own second step.
let key = interp.property_key(&arg(args, 1))?;
if !object.is_extensible() && !crate::object::has_own(&object, &key) {
return Ok(Value::Bool(false));
}
// The definition itself is shared with `Object.defineProperty`; only the failure
// reporting differs, and that is the `false` above.
let accepted = crate::builtins::object::define_property_on(interp, &object, args)?;
Ok(Value::Bool(accepted))
}
fn delete_property(interp: &mut Interp<'_>, _this: Value, args: &[Value]) -> JsResult<Value> {
let target = arg(args, 0);
let Some(object) = target.as_object().cloned() else {
return Err(interp.type_error("Reflect.deleteProperty target is not an object"));
};
// `ToPropertyKey`, not `to_js_string`: a symbol key has to stay a symbol
// (`delete-symbol-properties`), and a property key whose `toString` throws has to
// propagate (`return-abrupt-from-property-key`).
let key = interp.property_key(&arg(args, 1))?;
Ok(Value::Bool(interp.delete_property_key(&object, &key)?))
}
fn get(interp: &mut Interp<'_>, _this: Value, args: &[Value]) -> JsResult<Value> {
let target = arg(args, 0);
let Some(object) = target.as_object().cloned() else {
return Err(interp.type_error("Reflect.get target is not an object"));
};
// `ToPropertyKey`, not `ToString`: a symbol has to stay a symbol. Going through a
// string turned every `Reflect.get(o, sym)` into a read of `""`, and `proxy_get`
// forwards through this function, so a Proxy's `get` trap appeared to return
// `undefined` for `Symbol.iterator` even when the trap answered with a function.
let key = interp.property_key(&arg(args, 1))?;
// The third argument is the **receiver**, and dropping it is a real bug, not a
// simplification. `[[Get]]` hands the receiver to whatever getter it finds, so
// `Reflect.get(inner, "stack", proxy)` must run `stack`'s getter with `this` bound to
// the *proxy* — which answers `undefined`, because a proxy carries no `[[ErrorData]]`.
// Without it the getter ran with `inner` as `this` and produced a trace.
//
// The same mistake had already been fixed in `Reflect.set` two screens down, with a
// comment explaining it; `get` was left behind. `Error/prototype/stack/getter-receiver-
// is-proxy` is what finally noticed, through a `Reflect.get(t, key, receiver)` written
// inside a proxy trap — the ordinary "does this forward its receiver" shape.
let receiver = match args.get(2) {
None | Some(Value::Undefined) => Value::Object(object.clone()),
Some(receiver) => receiver.clone(),
};
Ok(interp
.get_property_key_for(&object, Some(&receiver), &key)?
.unwrap_or(Value::Undefined))
}
fn set(interp: &mut Interp<'_>, _this: Value, args: &[Value]) -> JsResult<Value> {
let target = arg(args, 0);
let Some(object) = target.as_object().cloned() else {
return Err(interp.type_error("Reflect.set target is not an object"));
};
let key = interp.property_key(&arg(args, 1))?;
// The fourth argument is the **receiver**, and it is not decoration: `[[Set]]` hands it
// to the setter it finds and passes it down to `OrdinarySet`, so a typed array's element
// store can tell whether it is writing through itself or through something else. The
// argument used to be dropped here, which made
// `Reflect.set(ta, 100, v, "not an object")` behave as a direct store.
let value = arg(args, 2);
let receiver = match args.get(3) {
None | Some(Value::Undefined) => Value::Object(object.clone()),
Some(receiver) => receiver.clone(),
};
// The `bool` is the answer, not decoration: `[[Set]]` reports a refusal instead of
// raising, which is the whole reason `Reflect.set` exists. Returning `true`
// unconditionally made `Reflect.set(sample, "foo", 42)` claim success on a read-only
// property — `Set/key-is-not-numeric-index` and four sibling cases check it.
let accepted = interp.set_property_with_key_receiver(
&object,
&key,
value,
Some(&receiver),
crate::interp::DeleteFailure::Ignore,
)?;
// A refused array `length` store is a *refusal*, not a success: the write path reports
// it through `DeleteFailure::Ignore` as "accepted", because the sloppy-mode assignment
// that also uses that path has no answer to give. `Reflect.set` does, and
// `define-own-prop-length-coercion-order-set` asserts `!Reflect.set(…)` for a `length`
// a `valueOf` locked mid-conversion.
let accepted = accepted
&& !(key.as_str() == Some("length") && object.is_array() && !object.length_writable());
Ok(Value::Bool(accepted))
}
fn has(interp: &mut Interp<'_>, _this: Value, args: &[Value]) -> JsResult<Value> {
let target = arg(args, 0);
let Some(object) = target.as_object().cloned() else {
return Err(interp.type_error("Reflect.has target is not an object"));
};
// `[[HasProperty]]`, which walks the prototype chain and asks a Proxy's `has` trap.
// The hand-rolled version here only looked at own properties and an array index, so
// `Reflect.has({}, "toString")` and every symbol key answered `false`.
let key = interp.property_key(&arg(args, 1))?;
Ok(Value::Bool(interp.has_own_or_inherited(&object, &key)?))
}
fn own_keys(interp: &mut Interp<'_>, _this: Value, args: &[Value]) -> JsResult<Value> {
let target = arg(args, 0);
let Some(object) = target.as_object().cloned() else {
return Err(interp.type_error("Reflect.ownKeys target is not an object"));
};
// Through the interpreter, not the free function: a proxy reports its own list, and
// that is the whole reason `Reflect.ownKeys` takes a proxy seriously.
let keys: Vec<Value> = interp
.object_own_property_keys(&object)?
.iter()
.map(|key| interp.key_value(key))
.collect();
let array = interp.new_array(keys);
Ok(Value::Object(array))
}
/// `Reflect.getOwnPropertyDescriptor(target, key)` — `Object.getOwnPropertyDescriptor`'s
/// non-throwing twin, and the only builtin that reaches `[[GetOwnProperty]]` directly.
fn get_own_property_descriptor(
interp: &mut Interp<'_>,
_this: Value,
args: &[Value],
) -> JsResult<Value> {
let target = arg(args, 0);
let Some(object) = target.as_object().cloned() else {
return Err(interp.type_error("Reflect.getOwnPropertyDescriptor target is not an object"));
};
let key = interp.property_key(&arg(args, 1))?;
let Some(property) = interp.object_get_own_property(&object, &key)? else {
return Ok(Value::Undefined);
};
let descriptor = interp.new_object(Some(interp.realm.object_proto.clone()));
if property.is_accessor() {
descriptor.define_data("get", property.getter.unwrap_or(Value::Undefined));
descriptor.define_data("set", property.setter.unwrap_or(Value::Undefined));
} else {
descriptor.define_data("value", property.value.unwrap_or(Value::Undefined));
descriptor.define_data("writable", Value::Bool(property.writable));
}
descriptor.define_data("enumerable", Value::Bool(property.enumerable));
descriptor.define_data("configurable", Value::Bool(property.configurable));
Ok(Value::Object(descriptor))
}
fn get_prototype_of(interp: &mut Interp<'_>, _this: Value, args: &[Value]) -> JsResult<Value> {
let target = arg(args, 0);
let Some(object) = target.as_object().cloned() else {
return Err(interp.type_error("Reflect.getPrototypeOf target is not an object"));
};
Ok(interp
.object_prototype_of(&object)?
.map_or(Value::Null, Value::Object))
}
fn set_prototype_of(interp: &mut Interp<'_>, _this: Value, args: &[Value]) -> JsResult<Value> {
// §28.2.2 step 1: a non-object target is a `TypeError` (`target-is-not-object-throws`).
let target = arg(args, 0);
let Some(object) = target.as_object().cloned() else {
return Err(interp.type_error("Reflect.setPrototypeOf target is not an object"));
};
let proto = match arg(args, 1) {
Value::Object(proto) => Some(proto),
Value::Null => None,
_ => return Err(interp.type_error("prototype must be an object or null")),
};
// A refusal is a `false` here rather than a `TypeError`: that difference from
// `Object.setPrototypeOf` is the whole point of the `Reflect` form.
Ok(Value::Bool(interp.object_set_prototype_of(&object, proto)?))
}
fn is_extensible(interp: &mut Interp<'_>, _this: Value, args: &[Value]) -> JsResult<Value> {
// §28.2.4 step 1: a non-object target is a `TypeError`, not `false`
// (`isExtensible/target-is-not-object-throws`, `…-target-is-symbol-throws`).
let target = arg(args, 0);
let Some(object) = target.as_object().cloned() else {
return Err(interp.type_error("Reflect.isExtensible target is not an object"));
};
Ok(Value::Bool(interp.object_is_extensible(&object)?))
}
fn prevent_extensions(interp: &mut Interp<'_>, _this: Value, args: &[Value]) -> JsResult<Value> {
let target = arg(args, 0);
let Some(object) = target.as_object().cloned() else {
return Err(interp.type_error("Reflect.preventExtensions target is not an object"));
};
Ok(Value::Bool(interp.object_prevent_extensions(&object)?))
}