Repository navigation
fix(temporal): use internal slots in subclass coercions + write-back after Reflect.construct #5793
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -699,11 +699,34 @@ pub(crate) fn lower(ctx: &mut FnCtx<'_>, expr: &Expr) -> Result<String> { | |
| let t = lower_expr(ctx, target)?; | ||
| let a = lower_expr(ctx, args)?; | ||
| let nt = lower_expr(ctx, new_target)?; | ||
| Ok(ctx.block().call( | ||
| let result = ctx.block().call( | ||
| DOUBLE, | ||
| "js_reflect_construct", | ||
| &[(DOUBLE, &t), (DOUBLE, &a), (DOUBLE, &nt)], | ||
| )) | ||
| ); | ||
| // Write-back captured outer locals: when `target` is a | ||
| // statically-known user class, the constructor body stores | ||
| // mutations to `this.__perry_cap_*` but can't reach the | ||
| // caller's outer alloca slots. Read the fields back here | ||
| // (e.g. `++called` in a subclass constructor is visible | ||
| // after `Reflect.construct(Sub, args)` returns). | ||
| let class_name: Option<String> = match target.as_ref() { | ||
| Expr::ClassRef(cn) => Some(cn.clone()), | ||
| Expr::LocalGet(id) => ctx | ||
| .local_id_to_name | ||
| .get(id) | ||
| .and_then(|name| ctx.local_class_aliases.get(name)) | ||
| .cloned(), | ||
| _ => None, | ||
| }; | ||
| if let Some(cn) = class_name { | ||
| if let Some(class) = ctx.classes.get(cn.as_str()).copied() { | ||
| let bits = ctx.block().bitcast_double_to_i64(&result); | ||
| let inst_handle = ctx.block().and(I64, &bits, POINTER_MASK_I64); | ||
| crate::lower_call::emit_class_capture_writeback(ctx, class, &inst_handle); | ||
|
Comment on lines
+724
to
+726
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🎯 Functional Correctness | 🟠 Major | 🏗️ Heavy lift Use the constructed receiver, not If the target constructor returns an object/function, 🤖 Prompt for AI Agents |
||
| } | ||
| } | ||
| Ok(result) | ||
| } | ||
| Expr::ReflectDefineProperty { | ||
| target, | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,82 @@ | ||
| //! Post-construction capture write-back. | ||
| //! | ||
| //! After a `new ClassName(args…)` or `Reflect.construct(…)` that inlines a | ||
| //! constructor which may mutate captured outer locals, this module reads the | ||
| //! updated value back from the instance's `__perry_cap_<id>` fields and stores | ||
| //! it to the outer local's LLVM alloca slot so the mutation is visible to the | ||
| //! caller. | ||
|
|
||
| use crate::expr::FnCtx; | ||
| use crate::nanbox::POINTER_MASK_I64; | ||
| use crate::types::{DOUBLE, I32, I64}; | ||
|
|
||
| /// After construction of a class whose constructor may have mutated captured | ||
| /// outer locals (via `++captured_var`, `captured_var = x`, etc.), read back | ||
| /// each `__perry_cap_<id>` field from the freshly-constructed instance and | ||
| /// store the updated value to the outer local's LLVM slot. | ||
| /// | ||
| /// ECMAScript requires shared-binding semantics for captured variables: a | ||
| /// class nested inside a function shares the SAME binding as the outer scope. | ||
| /// Perry's capture mechanism passes the captured value as an extra constructor | ||
| /// param (by value), so `++called` inside the constructor only updates the | ||
| /// constructor-local copy. After construction completes, this helper reads the | ||
| /// mutated value back from `this.__perry_cap_*` and writes it to the outer | ||
| /// local's alloca slot, making the mutation visible to the caller. | ||
| /// | ||
| /// `obj_handle` is the raw i64 object pointer (not nanboxed). | ||
| pub(crate) fn emit_class_capture_writeback( | ||
| ctx: &mut FnCtx<'_>, | ||
| class: &perry_hir::Class, | ||
| obj_handle: &str, | ||
| ) { | ||
| // Cap params are synthesized in `synthesize_class_captures` as extra | ||
| // constructor params with name `__perry_cap_<outer_id>`. They are NOT in | ||
| // `class.fields` — they are PropertySet stmts on `this` in the ctor body. | ||
| // Iterate ctor params to find which outer locals were captured. | ||
| let Some(ctor) = class.constructor.as_ref() else { | ||
| return; | ||
| }; | ||
| for param in &ctor.params { | ||
| let Some(id_str) = param.name.strip_prefix("__perry_cap_") else { | ||
| continue; | ||
| }; | ||
| let Ok(outer_id) = id_str.parse::<u32>() else { | ||
| continue; | ||
| }; | ||
| // Only write back to locals that are actually in scope (same-function | ||
| // construction). Cross-module construction has no accessible outer local. | ||
| let Some(outer_slot) = ctx.locals.get(&outer_id).cloned() else { | ||
| continue; | ||
| }; | ||
| // Read the updated capture value from the instance field. | ||
| let field_name = ¶m.name; | ||
| let key_idx = ctx.strings.intern(field_name); | ||
| let key_handle_global = format!("@{}", ctx.strings.entry(key_idx).handle_global); | ||
| let key_box = ctx.block().load(DOUBLE, &key_handle_global); | ||
| let key_bits = ctx.block().bitcast_double_to_i64(&key_box); | ||
| let key_handle = ctx.block().and(I64, &key_bits, POINTER_MASK_I64); | ||
| let val = ctx.block().call( | ||
| DOUBLE, | ||
| "js_object_get_field_by_name_f64", | ||
| &[(I64, obj_handle), (I64, &key_handle)], | ||
| ); | ||
| // Store the updated value. Handle boxed locals (shared across multiple | ||
| // closures) via js_box_set; plain locals via a direct slot store. | ||
| if ctx.boxed_vars.contains(&outer_id) { | ||
| let box_dbl = ctx.block().load(DOUBLE, &outer_slot); | ||
| let box_ptr = ctx.block().bitcast_double_to_i64(&box_dbl); | ||
| ctx.block() | ||
| .call_void("js_box_set", &[(I64, &box_ptr), (DOUBLE, &val)]); | ||
| } else { | ||
| ctx.block().store(DOUBLE, &val, &outer_slot); | ||
| // If this local also has an i32 fast-path slot (counter / integer | ||
| // local), keep it in sync. Use fptosi→i64→trunc→i32 to handle | ||
| // unsigned values safely (direct fptosi→i32 is UB above INT32_MAX). | ||
| if let Some(i32_slot) = ctx.i32_counter_slots.get(&outer_id).cloned() { | ||
| let v_i64 = ctx.block().fptosi(DOUBLE, &val, crate::types::I64); | ||
| let v_i32 = ctx.block().trunc(crate::types::I64, &v_i64, I32); | ||
| ctx.block().store(I32, &v_i32, &i32_slot); | ||
|
Comment on lines
+75
to
+78
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🩺 Stability & Availability | 🟠 Major | 🏗️ Heavy lift Avoid
🤖 Prompt for AI Agents |
||
| } | ||
| } | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -503,6 +503,8 @@ fn emit_typed_shape_layout_init(ctx: &mut FnCtx<'_>, class_name: &str, obj_handl | |
| ); | ||
| } | ||
|
|
||
| pub(crate) use super::capture_writeback::emit_class_capture_writeback; | ||
|
|
||
| /// Lower `new ClassName(args…)` — Phase C.1. | ||
| /// | ||
| /// Strategy: allocate an anonymous object via `js_object_alloc(0, N)` | ||
|
|
@@ -1226,6 +1228,13 @@ fn lower_new_impl( | |
| // The inline-ctor path does this at its tail (below); this | ||
| // standalone-symbol path returns here, so it must do it too. | ||
| emit_typed_shape_layout_init(ctx, class_name, &obj_handle); | ||
| // Write-back: propagate constructor mutations to outer captured locals. | ||
| // The standalone constructor symbol receives captured values by value | ||
| // and stores mutations to `this.__perry_cap_*` fields, but never | ||
| // updates the outer local's alloca slot. Read the fields back here so | ||
| // the enclosing scope sees the updated values (e.g. `++called` in a | ||
| // subclass constructor is visible after `new SubClass(...)` returns). | ||
| emit_class_capture_writeback(ctx, class, &obj_handle); | ||
|
Comment on lines
+1231
to
+1237
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🎯 Functional Correctness | 🟠 Major | ⚡ Quick win Mirror write-back on the inline constructor path. This call fixes only the standalone-symbol branch. The inlined constructor branch later restores constructor locals and returns without 🤖 Prompt for AI Agents |
||
| let is_derived = class.extends.is_some() | ||
| || class.extends_name.is_some() | ||
| || class.native_extends.is_some() | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -255,6 +255,24 @@ pub fn temporal_value_ref<'a>(value: f64) -> Option<&'a TemporalValue> { | |
| unsafe { Some(&*(*(addr as *const TemporalCell)).value) } | ||
| } | ||
|
|
||
| /// Like [`temporal_value_ref`] but also resolves `class X extends Temporal.<Type>` | ||
| /// subclass instances by reading the stashed `__perry_temporal_cell__` field. | ||
| /// Use this in `ToTemporalXxx` coercions so `compare`/`from`/`until`/`since` | ||
| /// use internal slots instead of property getters. (#5587) | ||
| #[cfg(feature = "temporal")] | ||
| #[inline] | ||
| #[allow(clippy::needless_lifetimes)] | ||
| pub fn temporal_value_ref_or_subclass<'a>(value: f64) -> Option<&'a TemporalValue> { | ||
| temporal_value_ref(value).or_else(|| { | ||
| let bits = value.to_bits(); | ||
| if !crate::value::JSValue::from_bits(bits).is_pointer() { | ||
| return None; | ||
| } | ||
| let addr = (bits & NANBOX_PTR_MASK) as usize; | ||
| unsafe { crate::object::temporal_subclass_cell(addr) }.and_then(temporal_value_ref) | ||
|
Comment on lines
+266
to
+272
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🎯 Functional Correctness | 🟠 Major | 🏗️ Heavy lift Require a real Temporal-subclass brand here. This fallback trusts any object that has a 🤖 Prompt for AI Agents |
||
| }) | ||
| } | ||
|
|
||
| /// The brand sub-kind of a Temporal value, or `None` if not a Temporal cell. | ||
| #[inline] | ||
| pub fn temporal_kind(value: f64) -> Option<TemporalKind> { | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🎯 Functional Correctness | 🟠 Major | 🏗️ Heavy lift
Don’t read capture fields from the return-override result.
resultis the value returned byjs_new_function_construct_apply; for constructors that explicitly return an object/function, it may not be the allocated receiver that receivedthis.__perry_cap_*. Reading write-back fields fromresultcan writeundefined/wrong data back to outer locals. The write-back needs access to the actual constructed receiver before return override, or equivalent runtime support.🤖 Prompt for AI Agents