diff --git a/crates/js/src/builtins/array.rs b/crates/js/src/builtins/array.rs index f4f2a161bc..9b7ca4bfa5 100644 --- a/crates/js/src/builtins/array.rs +++ b/crates/js/src/builtins/array.rs @@ -340,16 +340,17 @@ fn typed_locale_item(interp: &mut Interp, v: Value) -> JsResult> { locale_call(interp, v, true) } -/// Calls the element's `toLocaleString`, looked up on its object, with the value itself as `this` -/// when `on_value` is true and with the object otherwise. +/// Calls the element's `toLocaleString`, looked up on its object. With `on_value`, the value +/// itself is `this` for the lookup's getter and for the call (Invoke, section 7.3.18); otherwise +/// the object is. fn locale_call(interp: &mut Interp, v: Value, on_value: bool) -> JsResult> { if v.is_nullish() { return Ok(None); } let e = interp.to_object(&v)?; - let f = interp.get_from(e, &crate::heap::key("toLocaleString"), &Value::Object(e))?; - require_callable(interp, &f, "an element's toLocaleString")?; let this = if on_value { v } else { Value::Object(e) }; + let f = interp.get_from(e, &crate::heap::key("toLocaleString"), &this)?; + require_callable(interp, &f, "an element's toLocaleString")?; let s = interp.call(&f, &this, &[])?; interp.to_str(&s).map(Some) } diff --git a/crates/js/src/collections_tests.rs b/crates/js/src/collections_tests.rs index 3fe01a3327..62bf45e03b 100644 --- a/crates/js/src/collections_tests.rs +++ b/crates/js/src/collections_tests.rs @@ -416,6 +416,14 @@ fn typed_array_methods() { ), "number,number;object" ); + // A getter for the method gets the number as `this` too (Invoke, section 7.3.18). + assert_eq!( + string( + "Object.defineProperty(Number.prototype, 'toLocaleString', {get: function () { 'use strict'; var t = typeof this; \ + return function () { return t; }; }}); new Uint8Array([7]).toLocaleString() + ';' + [7].toLocaleString()" + ), + "number;object" + ); truth( "Object.getPrototypeOf(Int8Array) === Object.getPrototypeOf(Float64Array) && Object.getPrototypeOf(Int8Array.prototype) !== Object.prototype", );