JavaScript: typed array toLocaleString looks its method up with the number as receiver

%TypedArray%.prototype.toLocaleString invokes each element's method
(Invoke, section 7.3.18), so a getter for toLocaleString on
Number.prototype now gets the number as this, as the call already does.
Array.prototype.toLocaleString keeps the element's object for both.
This commit is contained in:
dotCooCoo committed 2026-11-22 08:02:26 -08:00
1 parent 287dd72309
commit abb1fa5f57
2 files changed
+13 -4

No files matched your search

+5 -4
View File
@@ -340,16 +340,17 @@ fn typed_locale_item(interp: &mut Interp, v: Value) -> JsResult<Option<Str>> {
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<Option<Str>> {
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)
}
+8
View File
@@ -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",
);