Skip to content

Commit 5f4f78a

Browse files
author
Ralph Kuepper
committed
fix(runtime): strict arr[i]=v / arr.length=n on a frozen array throws TypeError
Perry compiles everything strict, so a source-level array element or `length` assignment is `Set(O, P, V, Throw = true)`. On a *frozen* array these targets are non-writable, so the write must throw a TypeError -- Perry silently no-oped instead, leaving a batch of test262 missed-negatives: "use strict"; const a = Object.freeze([1, 2, 3]); a[0] = 9; // was: silent; now: TypeError read-only property '0' a[5] = 9; // was: silent; now: TypeError not extensible a.length = 1; // was: silent; now: TypeError read-only property 'length' Adding a NEW index to any non-extensible array (frozen / sealed / preventExtensions'd) likewise now throws "object is not extensible", matching V8/Node. Design -- strict variants beside the silent helpers, so ONLY user assignments throw and every internal caller keeps its no-Throw contract: * js_array_set_f64_extend_strict / js_array_set_index_or_string_strict -- element writes; share array_strict_index_write_guard (existing index of a frozen array -> read-only; new index of a non-extensible array -> not- extensible; sealed/non-extensible still permit overwriting an existing index, so only is_frozen gates the in-bounds case). * js_array_set_length_strict -- frozen `length` write, even for an equal or invalid new value (V8 rejects the non-writable write before coercing, so a frozen arr.length = -1 is a TypeError, not a RangeError; the guard precedes the length parse). The plain js_array_set_{f64_extend,index_or_string,length} helpers keep their silent behaviour for their internal callers -- Object.defineProperty (which uses them as raw [[DefineOwnProperty]] slot-writers), array-object length coercion, polymorphic_index, and freshly-allocated runtime arrays. The assignment paths are re-pointed to the strict variants: the arr[i]=v codegen (js_typed_feedback_array_set_{f64_extend,index_or_string} wrappers, emitted only for the source assignment, plus the realloc slow path in expr/index.rs), the arr.length=v codegen (expr/property_set.rs), the generic PutValue helper js_object_set_field_by_name, and js_put_value_set (strict-flag gated). Verified on an internal Linux sweep host, matching Node byte-for-byte in both default (auto-optimize) and PERRY_NO_AUTO_OPTIMIZE builds: * frozen element (existing/new) + length (shrink/grow/same) all throw; * sealed / preventExtensions add-new-index throws; existing-index overwrite still succeeds; * normal (unfrozen) element / length / string-index / computed / dense-loop writes are unchanged (no over-throw); * Object.defineProperty(frozenArr, ...) (same-value ok, changed-value throws via its own validation), and frozen concat/map/slice/spread read-then-build ops, all unaffected. Out of scope (separate, non-frozen subclusters): sealed-array .length shrink, and defineProperty-installed non-writable index/length descriptors.
1 parent d4eebd5 commit 5f4f78a

9 files changed

Lines changed: 207 additions & 11 deletions

File tree

‎crates/perry-codegen/src/expr/index.rs‎

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -345,9 +345,14 @@ pub(crate) fn lower_index_set_fast(
345345
"js_typed_feedback_record_fallback_call",
346346
&[(I64, feedback_site_id)],
347347
);
348+
// Strict `arr[i] = v`: a frozen array's element is non-writable and a
349+
// non-extensible array rejects a new index, so route to the throwing
350+
// variant. (The inline fast/medium paths above are only reached for
351+
// arrays with a proven dense-numeric layout, which excludes frozen /
352+
// sealed / non-extensible arrays — those always fall to this call.)
348353
let new_handle = blk.call(
349354
I64,
350-
"js_array_set_f64_extend",
355+
"js_array_set_f64_extend_strict",
351356
&[(I64, &arr_handle), (I32, &idx_i32), (DOUBLE, val_double)],
352357
);
353358
let new_box = nanbox_pointer_inline(blk, &new_handle);

‎crates/perry-codegen/src/expr/property_set.rs‎

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -194,8 +194,11 @@ pub(crate) fn lower(ctx: &mut FnCtx<'_>, expr: &Expr) -> Result<String> {
194194
let blk = ctx.block();
195195
let arr_bits = blk.bitcast_double_to_i64(&arr_box);
196196
let arr_handle = blk.and(I64, &arr_bits, POINTER_MASK_I64);
197+
// `arr.length = v` is a strict `Set(O,"length",v,true)`: a frozen
198+
// array's `length` is non-writable, so route to the throwing
199+
// variant instead of the silent internal helper.
197200
blk.call_void(
198-
"js_array_set_length",
201+
"js_array_set_length_strict",
199202
&[(I64, &arr_handle), (DOUBLE, &val_double)],
200203
);
201204
return Ok(val_double);

‎crates/perry-codegen/src/runtime_decls/arrays.rs‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@ pub fn declare_phase_b_arrays(module: &mut LlModule) {
5151
// Extending variant: returns a possibly-realloc'd pointer that the
5252
// caller must write back to the local slot.
5353
module.declare_function("js_array_set_f64_extend", I64, &[I64, I32, DOUBLE]);
54+
module.declare_function("js_array_set_f64_extend_strict", I64, &[I64, I32, DOUBLE]);
5455
module.declare_function("js_array_fill_f64_const_extend", I64, &[I64, I32, DOUBLE]);
5556
module.declare_function("js_array_fill_f64_iota_extend", I64, &[I64, I32]);
5657
module.declare_function("js_array_fill_f64_const_len_extend", I64, &[I64, DOUBLE]);
@@ -135,6 +136,7 @@ pub fn declare_phase_b_arrays(module: &mut LlModule) {
135136
module.declare_function("js_array_delete", I32, &[I64, I32]);
136137
// Closes #304: `arr.length = N` truncate / extend.
137138
module.declare_function("js_array_set_length", VOID, &[I64, DOUBLE]);
139+
module.declare_function("js_array_set_length_strict", VOID, &[I64, DOUBLE]);
138140
// Array.from() — js_array_clone handles arrays, Sets, and Maps.
139141
module.declare_function("js_array_clone", I64, &[I64]);
140142
// #2773: Array.from(source) — throws TypeError for nullish sources, keeps

‎crates/perry-runtime/src/array/indexing.rs‎

Lines changed: 140 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,31 @@ const MAX_DENSE_ARRAY_GROW_LENGTH: u32 = 1_000_000;
1616
/// benchmark for 6 hours (Regression Check, v0.5.1129–v0.5.1150).
1717
const DENSE_ARRAY_GAP_LIMIT: u32 = 1024;
1818

19+
/// A strict-mode element write (`arr[i] = v`) to a **frozen** array's existing
20+
/// index is `[[Set]]` on a non-writable data property with `Throw = true`
21+
/// (ECMA-262 §10.4.2.4 → OrdinarySetWithOwnDescriptor step 2.b.i), so it must
22+
/// throw a **TypeError** rather than silently no-op. Perry compiles everything
23+
/// strict, so the codegen `arr[i] = v` fast paths — which call these
24+
/// `js_array_set_f64*` helpers directly — carry the strict-`Set` contract.
25+
/// Matches V8's message. (test262 built-ins/Array element-write-on-frozen.)
26+
#[cold]
27+
fn throw_frozen_array_index_write(index: u32) -> ! {
28+
crate::collection_iter::throw_type_error(&format!(
29+
"Cannot assign to read only property '{index}' of object '[object Array]'"
30+
));
31+
}
32+
33+
/// A strict-mode write that would *add* a new index to a non-extensible
34+
/// (frozen / sealed / preventExtensions'd) array — `arr[i] = v` with
35+
/// `i >= length` — is `CreateDataProperty` on a non-extensible object with
36+
/// `Throw = true`, so it must throw a **TypeError**. Matches V8's message.
37+
#[cold]
38+
fn throw_array_not_extensible_add(index: u32) -> ! {
39+
crate::collection_iter::throw_type_error(&format!(
40+
"Cannot add property {index}, object is not extensible"
41+
));
42+
}
43+
1944
/// Lazily-memoized address of the `Array.prototype` array, and a sticky flag
2045
/// recording whether anyone has installed an indexed property on it. An
2146
/// out-of-bounds element read on an ordinary array must fall through to
@@ -830,6 +855,61 @@ pub extern "C" fn js_array_set_f64(arr: *mut ArrayHeader, index: u32, value: f64
830855
}
831856
}
832857

858+
/// Strict-mode `arr[i] = v` — the user-visible element assignment, i.e.
859+
/// `Set(O, ToString(i), v, true)` (PutValue with `Throw = true`). On a frozen
860+
/// array this must throw a **TypeError** instead of silently no-oping: writing
861+
/// an existing index is a read-only violation; adding a new index (or writing
862+
/// any index of a sealed / preventExtensions'd array past its length) is a
863+
/// not-extensible violation.
864+
///
865+
/// Kept separate from `js_array_set_f64_extend` so the *internal* callers of the
866+
/// latter — `Object.defineProperty(arr, i, …)` (which uses it as a raw
867+
/// `[[DefineOwnProperty]]` slot-writer after clearing attrs),
868+
/// `polymorphic_index`, and freshly-allocated runtime arrays — retain their
869+
/// silent, non-throwing contract. Only the `arr[i] = v` assignment codegen
870+
/// (`index_set` / `index` / `field_set_by_name`) routes here.
871+
/// test262 built-ins/Array element/add on frozen|sealed|non-extensible.
872+
/// Strict-mode guard for a would-be `arr[index] = v` element write: throws the
873+
/// spec `Set`-with-`Throw` TypeError when `arr` is frozen (existing index →
874+
/// read-only) or non-extensible and the index is new (→ not-extensible). No-op
875+
/// for writable slots, buffers, and typed arrays (which own their store
876+
/// semantics). Shared by the strict element-write entry points.
877+
#[inline]
878+
pub(crate) fn array_strict_index_write_guard(arr: *mut ArrayHeader, index: u32) {
879+
let clean = clean_arr_ptr_mut(arr);
880+
if clean.is_null()
881+
|| crate::buffer::is_registered_buffer(clean as usize)
882+
|| crate::typedarray::lookup_typed_array_kind(clean as usize).is_some()
883+
{
884+
return;
885+
}
886+
let flags = array_object_flags(clean);
887+
let length = unsafe { (*clean).length };
888+
if index < length {
889+
// Existing index: only a *frozen* array's data is non-writable; a
890+
// sealed / non-extensible array still permits overwriting it.
891+
if flags & crate::gc::OBJ_FLAG_FROZEN != 0 {
892+
throw_frozen_array_index_write(index);
893+
}
894+
} else if flags
895+
& (crate::gc::OBJ_FLAG_FROZEN | crate::gc::OBJ_FLAG_SEALED | crate::gc::OBJ_FLAG_NO_EXTEND)
896+
!= 0
897+
{
898+
// New index on a non-extensible array: cannot add the property.
899+
throw_array_not_extensible_add(index);
900+
}
901+
}
902+
903+
#[no_mangle]
904+
pub extern "C" fn js_array_set_f64_extend_strict(
905+
arr: *mut ArrayHeader,
906+
index: u32,
907+
value: f64,
908+
) -> *mut ArrayHeader {
909+
array_strict_index_write_guard(arr, index);
910+
js_array_set_f64_extend(arr, index, value)
911+
}
912+
833913
/// Set an element in an array by index, extending the array if needed
834914
/// Returns the (possibly reallocated) array pointer
835915
/// This mimics JavaScript's arr[i] = value behavior
@@ -1485,3 +1565,63 @@ pub extern "C" fn js_array_set_index_or_string(
14851565
}
14861566
arr
14871567
}
1568+
1569+
/// Strict-mode `arr[key] = v` (dynamic index-or-string key) — `Set` with
1570+
/// `Throw = true`. For a canonical numeric index this enforces the frozen /
1571+
/// non-extensible guard (throwing a TypeError) before delegating; non-index
1572+
/// keys fall through to the ordinary path unchanged. This is the assignment
1573+
/// entry point behind `js_typed_feedback_array_set_index_or_string`; the plain
1574+
/// `js_array_set_index_or_string` keeps its silent contract for any internal
1575+
/// caller. test262 built-ins/Array element-write-on-frozen (string/dynamic key).
1576+
#[no_mangle]
1577+
pub extern "C" fn js_array_set_index_or_string_strict(
1578+
arr: *mut ArrayHeader,
1579+
idx: f64,
1580+
value: f64,
1581+
) -> *mut ArrayHeader {
1582+
if !arr.is_null() {
1583+
// Resolve the canonical array-index interpretation of the key (mirrors
1584+
// the numeric branch of `js_array_set_index_or_string`), and guard it.
1585+
// A string key that spells an index (`"0"`) also targets the element
1586+
// store, so ToString it and re-parse.
1587+
let index = canonical_index_of_set_key(idx);
1588+
if let Some(i) = index {
1589+
array_strict_index_write_guard(arr, i);
1590+
}
1591+
}
1592+
js_array_set_index_or_string(arr, idx, value)
1593+
}
1594+
1595+
/// The canonical array index (`0..2^32-1`) a dynamic `arr[key] = v` key targets,
1596+
/// or `None` for a non-index key. Numbers use the array-index boundary; string
1597+
/// keys are parsed via their ToString so `arr["3"]` on a frozen array throws
1598+
/// like `arr[3]`.
1599+
fn canonical_index_of_set_key(idx: f64) -> Option<u32> {
1600+
let bits = idx.to_bits();
1601+
let top16 = bits >> 48;
1602+
// Heap string / SSO key: parse the string as a canonical index.
1603+
if top16 == 0x7FFF || top16 == 0x7FF9 {
1604+
let s = crate::value::js_get_string_pointer_unified(idx) as *const crate::StringHeader;
1605+
if s.is_null() {
1606+
return None;
1607+
}
1608+
let len = unsafe { (*s).byte_len as usize };
1609+
let data = unsafe { (s as *const u8).add(std::mem::size_of::<crate::StringHeader>()) };
1610+
let bytes = unsafe { std::slice::from_raw_parts(data, len) };
1611+
let name = std::str::from_utf8(bytes).ok()?;
1612+
return crate::object::canonical_array_index(name);
1613+
}
1614+
// Numeric key.
1615+
let n = if (bits & crate::value::TAG_MASK) == crate::value::INT32_TAG {
1616+
crate::value::JSValue::from_bits(bits).as_int32() as f64
1617+
} else if !(0x7FF8..=0x7FFF).contains(&top16) {
1618+
idx
1619+
} else {
1620+
return None;
1621+
};
1622+
if n.is_finite() && n.trunc() == n && n >= 0.0 && n < u32::MAX as f64 {
1623+
Some(n as u32)
1624+
} else {
1625+
None
1626+
}
1627+
}

‎crates/perry-runtime/src/array/mod.rs‎

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -79,8 +79,8 @@ pub use self::indexing::{
7979
js_array_get_element, js_array_get_element_f64, js_array_get_f64, js_array_get_f64_unchecked,
8080
js_array_get_index_or_string, js_array_get_length, js_array_length,
8181
js_array_numeric_get_f64_unboxed, js_array_numeric_set_f64_unboxed, js_array_set_f64,
82-
js_array_set_f64_extend, js_array_set_f64_unchecked, js_array_set_index_or_string,
83-
js_array_set_string_key,
82+
js_array_set_f64_extend, js_array_set_f64_extend_strict, js_array_set_f64_unchecked,
83+
js_array_set_index_or_string, js_array_set_index_or_string_strict, js_array_set_string_key,
8484
};
8585
pub use self::is_array::js_array_is_array;
8686
pub(crate) use self::iter_methods::throw_reduce_of_empty;
@@ -119,7 +119,8 @@ pub(crate) use self::push_pop::guard_writable_length;
119119
pub use self::push_pop::{
120120
js_array_delete, js_array_grow, js_array_numeric_push_f64_unboxed, js_array_pop_f64,
121121
js_array_push_f64, js_array_push_hole, js_array_push_spread_f64, js_array_set_length,
122-
js_array_shift_f64, js_array_unshift_f64, js_array_unshift_jsvalue, js_array_unshift_variadic,
122+
js_array_set_length_strict, js_array_shift_f64, js_array_unshift_f64, js_array_unshift_jsvalue,
123+
js_array_unshift_variadic,
123124
};
124125
pub use self::reduce_right::js_array_reduce_right;
125126
pub use self::search::{

‎crates/perry-runtime/src/array/push_pop.rs‎

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -380,6 +380,32 @@ pub extern "C" fn js_array_pop_f64(arr: *mut ArrayHeader) -> f64 {
380380
/// JSValue). The JS ArraySetLength path coerces it with `Number(...)`, then
381381
/// rejects NaN, negative, fractional, infinite, and >uint32 lengths with
382382
/// `RangeError: Invalid array length`.
383+
/// `arr.length = v` as the user-visible assignment — `Set(O, "length", v, true)`
384+
/// (PutValue with `Throw = true`, ECMA-262 §13.15.2). A frozen array's `length`
385+
/// is a non-writable data property, so the write must throw a **TypeError**
386+
/// rather than silently no-op — even when `v` equals the current length or is an
387+
/// invalid length (V8 rejects the non-writable write before coercing the value,
388+
/// so a frozen `arr.length = -1` is a TypeError, not a RangeError; hence the
389+
/// guard precedes `array_length_from_property_value_or_throw`).
390+
///
391+
/// This throwing variant is separate from `js_array_set_length` so the *internal*
392+
/// callers of the latter (`Object.defineProperty(arr, "length", …)`,
393+
/// array-object length coercion, stream/url bookkeeping) keep their silent,
394+
/// non-throwing `[[DefineOwnProperty]]`/no-Throw contract. Only the assignment
395+
/// codegen paths (`field_set_by_name` / `property_set` / proxy `PutValue`) route
396+
/// here. test262 built-ins/Array length-write-on-frozen.
397+
#[no_mangle]
398+
pub extern "C" fn js_array_set_length_strict(arr: *mut ArrayHeader, new_length: f64) {
399+
let arr = clean_arr_ptr_mut(arr);
400+
if arr.is_null() {
401+
return;
402+
}
403+
if unsafe { array_object_flags(arr) } & crate::gc::OBJ_FLAG_FROZEN != 0 {
404+
throw_non_writable_length();
405+
}
406+
js_array_set_length(arr, new_length);
407+
}
408+
383409
#[no_mangle]
384410
pub extern "C" fn js_array_set_length(arr: *mut ArrayHeader, new_length: f64) {
385411
let arr = clean_arr_ptr_mut(arr);

‎crates/perry-runtime/src/object/field_set_by_name.rs‎

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -577,7 +577,12 @@ pub extern "C" fn js_object_set_field_by_name(
577577
let gc_header =
578578
(obj as *const u8).sub(crate::gc::GC_HEADER_SIZE) as *const crate::gc::GcHeader;
579579
if (*gc_header).obj_type == crate::gc::GC_TYPE_ARRAY {
580-
crate::array::js_array_set_length(obj as *mut crate::array::ArrayHeader, value);
580+
// Assignment (`arr.length = v`) is a strict `Set` with
581+
// `Throw = true`: throw on a frozen array's non-writable length.
582+
crate::array::js_array_set_length_strict(
583+
obj as *mut crate::array::ArrayHeader,
584+
value,
585+
);
581586
return;
582587
}
583588
}
@@ -614,11 +619,13 @@ pub extern "C" fn js_object_set_field_by_name(
614619
};
615620
let arr = obj as *mut crate::array::ArrayHeader;
616621
if name == "length" {
617-
crate::array::js_array_set_length(arr, value);
622+
// Strict `Set(arr, "length", v, true)` — throw on frozen.
623+
crate::array::js_array_set_length_strict(arr, value);
618624
return;
619625
}
620626
if let Some(index) = super::canonical_array_index(name) {
621-
crate::array::js_array_set_f64_extend(arr, index, value);
627+
// Strict `Set(arr, i, v, true)` — throw on frozen/non-extensible.
628+
crate::array::js_array_set_f64_extend_strict(arr, index, value);
622629
return;
623630
}
624631
// Own-accessor short-circuit — an Array can carry a named accessor

‎crates/perry-runtime/src/proxy/put_value.rs‎

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -188,7 +188,14 @@ pub extern "C" fn js_put_value_set(
188188
}
189189
if target.to_bits() == receiver.to_bits() && key_is_length(property_key) {
190190
if let Some(arr) = array_ptr_from_value(target) {
191-
crate::array::js_array_set_length(arr, value);
191+
// PutValue(`arr.length = v`) is `Set(O, "length", v, Throw)`. In
192+
// strict mode a frozen array's non-writable `length` makes the
193+
// write throw a TypeError instead of silently no-oping.
194+
if strict != 0 {
195+
crate::array::js_array_set_length_strict(arr, value);
196+
} else {
197+
crate::array::js_array_set_length(arr, value);
198+
}
192199
return value;
193200
}
194201
}

‎crates/perry-runtime/src/typed_feedback.rs‎

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1782,7 +1782,10 @@ pub extern "C" fn js_typed_feedback_array_set_f64_extend(
17821782
if !pass {
17831783
record_fallback_call(site_id);
17841784
}
1785-
crate::array::js_array_set_f64_extend(arr, index, value)
1785+
// This wrapper is only emitted for the source-level `arr[i] = v` assignment,
1786+
// so it carries strict `Set`-with-`Throw` semantics: a frozen array's element
1787+
// is non-writable and a non-extensible array rejects a new index → TypeError.
1788+
crate::array::js_array_set_f64_extend_strict(arr, index, value)
17861789
}
17871790

17881791
#[no_mangle]
@@ -2029,7 +2032,9 @@ pub extern "C" fn js_typed_feedback_array_set_index_or_string(
20292032
} else {
20302033
record_guard_pass(site_id);
20312034
}
2032-
crate::array::js_array_set_index_or_string(arr, idx, value)
2035+
// Assignment-site wrapper → strict `Set` with `Throw = true` (frozen /
2036+
// non-extensible array element write throws a TypeError).
2037+
crate::array::js_array_set_index_or_string_strict(arr, idx, value)
20332038
}
20342039

20352040
#[no_mangle]

0 commit comments

Comments
 (0)