diff --git a/crates/js/src/builtins/array.rs b/crates/js/src/builtins/array.rs index 48dfe1832a..0f45f1cb50 100644 --- a/crates/js/src/builtins/array.rs +++ b/crates/js/src/builtins/array.rs @@ -389,7 +389,10 @@ fn concat(interp: &mut Interp, this: Value, args: &[Value], _: bool) -> JsResult let mut n = 0u64; for e in std::iter::once(Value::Object(o)).chain(args.iter().cloned()) { interp.use_fuel()?; - let override_spread = match (e.as_object(), interp.well_known(|w| &w.is_concat_spreadable)) { + // Code run without the ECMAScript 2015 option reads no such Symbol, even in a realm + // that has it from an earlier run. + let spreadable = if interp.options.es2015 { interp.well_known(|w| &w.is_concat_spreadable) } else { None }; + let override_spread = match (e.as_object(), spreadable) { (Some(_), Some(sym)) => interp.get_value_sym(&e, &sym)?, _ => Value::Undefined, }; diff --git a/crates/js/src/interp.rs b/crates/js/src/interp.rs index 2aa5c933c9..206f7b93c7 100644 --- a/crates/js/src/interp.rs +++ b/crates/js/src/interp.rs @@ -1634,7 +1634,10 @@ impl Interp { if !self.scope_has(object, name)? { return Ok(false); } - if with_this && let Some(sym) = self.well_known(|w| &w.unscopables) { + if with_this + && self.options.es2015 + && let Some(sym) = self.well_known(|w| &w.unscopables) + { let blocked = self.get_value_sym(&Value::Object(object), &sym)?; if let Value::Object(o) = blocked { let value = self.rooted(&[Value::Object(o)], |i| i.get_from(o, name, &Value::Object(o)))?; diff --git a/crates/js/src/proxy.rs b/crates/js/src/proxy.rs index e709cfd808..b9bf04e87a 100644 --- a/crates/js/src/proxy.rs +++ b/crates/js/src/proxy.rs @@ -441,12 +441,15 @@ impl Interp { } /// The object's own names and its own Symbols together, from a single [[OwnPropertyKeys]]: - /// one `ownKeys` trap call for a proxy. + /// one `ownKeys` trap call for a proxy. With [`crate::Options::es2015`] the names come in + /// ECMAScript order (array indices ascending first), through proxies without an `ownKeys` + /// trap too; without it they come in the order `for-in` visits them. pub(crate) fn own_keys_and_symbols(&mut self, id: ObjId) -> JsResult { + let ecma_order = self.options.es2015; if self.is_proxy(id) { - return self.proxy_own_keys(id, false); + return self.proxy_own_keys(id, ecma_order); } - let mut keys = self.own_keys(id)?; + let mut keys = if ecma_order { self.ecma_own_keys(id)? } else { self.own_keys(id)? }; keys.syms = self.own_symbols(id)?; keys.held.add(self, keys.syms.len().saturating_mul(KEY_ENTRY_BYTES))?; Ok(keys) diff --git a/crates/js/src/proxy_tests.rs b/crates/js/src/proxy_tests.rs index efb6bb78e8..09ffb4b7a3 100644 --- a/crates/js/src/proxy_tests.rs +++ b/crates/js/src/proxy_tests.rs @@ -852,6 +852,38 @@ fn concat_honors_proxy_spreadability_overrides() { type_errors(&["var p = Proxy.revocable([], {}); p.revoke(); [].concat(p.proxy)"]); } +#[test] +fn spreadability_and_unscopables_kept_from_an_earlier_run_apply_only_with_the_option() { + let mut realm = es(); + let setup = "var a = [1, 2]; a[Symbol.isConcatSpreadable] = false; + var o = { x: 'own' }; o[Symbol.unscopables] = { x: true }; var x = 'outer'; true"; + assert_eq!(realm.run(setup), Ok(JsValue::Bool(true))); + realm.set_es2015(false); + assert_eq!(realm.run("var c = [].concat(a), w; with (o) { w = x; } c.length === 2 && c[0] === 1 && w === 'own'"), Ok(JsValue::Bool(true))); + realm.set_es2015(true); + assert_eq!(realm.run("var c = [].concat(a), w; with (o) { w = x; } c.length === 1 && c[0] === a && w === 'outer'"), Ok(JsValue::Bool(true))); +} + +#[test] +fn define_properties_defines_array_index_keys_in_ascending_order() { + truth( + "var log = [], bag = {}; bag.b = { value: 0 }; bag[2] = { value: 2 }; bag[1] = { value: 1 }; + var p = new Proxy({}, { defineProperty: function (t, k, d) { log.push(k); return Reflect.defineProperty(t, k, d); } }); + Object.defineProperties(p, bag); + var wrapped = [], q = new Proxy({}, { defineProperty: function (t, k, d) { wrapped.push(k); return Reflect.defineProperty(t, k, d); } }); + Object.defineProperties(q, new Proxy(bag, {})); + log.join() === '1,2,b' && wrapped.join() === '1,2,b'", + ); + // Without the option, descriptors are read in the order for-in visits the names. + let mut realm = Realm::new(); + let src = "var order = [], bag = {}; bag.b = 0; bag[2] = 0; bag[1] = 0; + var seen = [], descs = {}; + for (var k in bag) { order.push(k); (function (k) { Object.defineProperty(descs, k, { enumerable: true, get: function () { seen.push(k); return { value: 0 }; } }); })(k); } + Object.defineProperties({}, descs); + seen.join() === order.join()"; + assert_eq!(realm.run(src), Ok(JsValue::Bool(true))); +} + #[test] fn typed_array_statics_construct_through_proxy_receivers() { truth_collecting( @@ -1527,12 +1559,23 @@ fn a_host_function_refuses_a_proxy_and_names_the_argument() { } } assert!(seen.borrow().is_empty(), "the host function did not run"); - // The traps did not run either. + // The same argument with a trap is refused too, before the host function or the trap runs. let mut realm = es(); - let f: HostFunction = Rc::new(|_| Ok(Data::Undefined)); + let calls = Rc::new(RefCell::new(0usize)); + let counter = calls.clone(); + let f: HostFunction = Rc::new(move |_| { + *counter.borrow_mut() += 1; + Ok(Data::Undefined) + }); realm.define_host_object("host", vec![("take".into(), HostItem::Function(f))]).unwrap(); - let r = realm.run("var ran = false; try { host.take(new Proxy({}, { ownKeys: function () { ran = true; return []; } })); } catch (e) {} ran"); - assert_eq!(r, Ok(JsValue::Bool(false))); + let r = realm.run( + "var ran = false, threw = false; + try { host.take(new Proxy({}, { ownKeys: function () { ran = true; return []; } })); } + catch (e) { threw = e instanceof TypeError; } + threw && !ran", + ); + assert_eq!(r, Ok(JsValue::Bool(true))); + assert_eq!(*calls.borrow(), 0, "the host function did not run"); } #[test] @@ -2269,13 +2312,15 @@ fn json_proxy_lengths_check_limits_before_reading_elements() { #[test] fn transparent_proxies_construct_bound_builtins_without_allowing_subclasses() { truth( - "var B = Map.bind(null, [[1, 7]]), P = new Proxy(B, {}), Q = new Proxy(P, {}); + "var B = Map.bind(null, [[1, 7]]), proto = Object.create(Map.prototype); + Object.defineProperty(B, 'prototype', { value: proto }); + var P = new Proxy(B, {}), Q = new Proxy(P, {}); var a = new P(), b = new Q(), c = Reflect.construct(Q, []); var R = new Proxy(Q.bind(null), {}), d = new R(); var U = new Proxy(Uint8Array.bind(null, [7, 8]), {}), u = new U(); - Object.defineProperty(B, 'prototype', { value: Map.prototype }); a.get(1) === 7 && b.get(1) === 7 && c.get(1) === 7 && d.get(1) === 7 && - Object.getPrototypeOf(a) === Map.prototype && u.length === 2 && u[1] === 8 && + Object.getPrototypeOf(a) === proto && Object.getPrototypeOf(b) === proto && Object.getPrototypeOf(c) === proto && + Object.getPrototypeOf(d) === Map.prototype && u.length === 2 && u[1] === 8 && type_error(function () { class Child extends P {} new Child(); })", ); }