//! Class definition evaluation: computed member names, static initializers and //! static blocks, for a class declared once (template-keyed nodes) and for each //! evaluation of a class in a function body (`Expr::ClassExprFresh`: `class_eval_begin` //! makes the evaluation, and every later step's bridge takes it and returns it). use super::*; /// `RegisterClassComputed*` kinds understood by `class_define_computed`. const COMPUTED_METHOD: f64 = 0.0; const COMPUTED_ACCESSOR: f64 = 1.0; impl<'a> FuncEmitCtx<'a> { pub(super) fn try_emit_expr_class_eval(&mut self, func: &mut Function, expr: &Expr) -> bool { match expr { // Every evaluation of a class in a function body is fresh, the first one // included, so no evaluation is the shared class. Expr::ClassIsFirstEvaluation { value, .. } => { self.with_tdz_suppressed(|ctx| ctx.emit_expr(func, value)); func.instruction(&Instruction::Drop); func.instruction(&Instruction::I64Const(TAG_FALSE as i64)); } Expr::ClassExprFresh { template, evaluation_owner, named_statics, computed_keys, computed_statics, static_init_order, captured_args, definition_steps, .. } => { // ClassDefinitionEvaluation order: the binding, the computed names in // source order, then static fields and blocks in source order. The // evaluation stays on the operand stack: each step takes it and // returns it, and it is the expression's value. let ctor_slot = self.class_ctor_slot(template); let (base, init) = self.builtin_base_init(template); self.emit_class_bridge( func, "class_eval_begin", template, &[&Expr::Number(ctor_slot), &base, &Expr::Number(init)], ); if let Some(owner) = evaluation_owner { self.emit_local_tee(func, owner); } for step in definition_steps { if let Some((class_name, operands)) = self.emitter.computed_define_operands(step) { let operands: Vec<&Expr> = operands.iter().collect(); self.emit_class_bridge_on( func, "class_define_computed", class_name, &operands, ); continue; } self.emit_expr(func, step); func.instruction(&Instruction::Drop); if let Some((slot, key)) = staged_computed_key(template, step, computed_keys) { let key = template_static_read(template, key); self.emit_class_bridge_on( func, "class_eval_slot", template, &[&Expr::String(slot.clone()), &key], ); } } for (slot, key) in unstaged_computed_keys(template, definition_steps, computed_keys) { let key = template_static_read(template, key); self.emit_class_bridge_on( func, "class_eval_slot", template, &[&Expr::String(slot.clone()), &key], ); } if !captured_args.is_empty() { let caps = Expr::Array(captured_args.clone(), None); self.with_tdz_suppressed(|ctx| { ctx.emit_class_bridge_on(func, "class_eval_caps", template, &[&caps]) }); } for step in static_init_order { match step { perry_hir::ClassFreshStaticInit::Named(i) => { let Some((name, init)) = named_statics.get(*i as usize) else { continue; }; self.emit_class_bridge_on( func, "class_eval_static", template, &[&Expr::String(name.clone()), init], ); } perry_hir::ClassFreshStaticInit::Computed(i) => { let Some((slot, init)) = computed_statics.get(*i as usize) else { continue; }; self.emit_class_bridge_on( func, "class_eval_static_computed", template, &[&Expr::String(slot.clone()), init], ); } perry_hir::ClassFreshStaticInit::Block(i) => { let block = self.emitter.static_block_slot(template, *i); self.emit_class_bridge_on( func, "class_eval_block", template, &[&Expr::Number(block)], ); } } } } Expr::RegisterClassComputedMethod { .. } | Expr::RegisterClassComputedAccessor { .. } => { let Some((class_name, operands)) = self.emitter.computed_define_operands(expr) else { return false; }; // A class declared once: no evaluation owns the member. let operands: Vec<&Expr> = std::iter::once(&Expr::Undefined).chain(&operands).collect(); self.emit_class_bridge(func, "class_define_computed", class_name, &operands); } // `static [key] = value` of a class declared once. Expr::ClassStaticSymbolSet { class_name, key, value, } => { let ctor_slot = self.class_ctor_slot(class_name); let key = template_static_read(class_name, key); self.emit_class_bridge( func, "class_static_define", class_name, &[&Expr::Number(ctor_slot), &key, value], ); } _ => return false, } true } } impl WasmModuleEmitter { /// A `RegisterClassComputed*` node's class and the `class_define_computed` operands /// after its class and owner: `ctorSlot, key, kind, isStatic, a, b, protoKind, length`, /// `a` and `b` being the method's (or getter's and setter's) table slots and /// `protoKind` and `length` a method's `fn_proto_code` and `length`. WASM and async (JS) /// bodies share it. pub(in crate::emit) fn computed_define_operands<'e>( &self, expr: &'e Expr, ) -> Option<(&'e str, Vec)> { let (class_name, key_expr, is_static, kind, slots, proto_kind, length) = match expr { Expr::RegisterClassComputedMethod { class_name, key_expr, method_name, is_static, .. } => { let idx = self .class_computed_map .get(&(class_name.clone(), method_name.clone())) .copied(); let proto_kind = idx.map_or(0.0, |idx| self.fn_proto_code(idx)); let length = idx .and_then(|idx| self.fn_lengths.get(&idx)) .map_or(0.0, |&l| f64::from(l)); let method = self.computed_fn_slot(class_name, Some(method_name)); let slots = [method, -1.0]; ( class_name, key_expr, is_static, COMPUTED_METHOD, slots, proto_kind, length, ) } Expr::RegisterClassComputedAccessor { class_name, key_expr, getter_name, setter_name, is_static, .. } => { let getter = self.computed_fn_slot(class_name, getter_name.as_ref()); let setter = self.computed_fn_slot(class_name, setter_name.as_ref()); ( class_name, key_expr, is_static, COMPUTED_ACCESSOR, [getter, setter], 0.0, 0.0, ) } _ => return None, }; let operands = vec![ Expr::Number(self.class_ctor_slot(class_name)), key_expr.as_ref().clone(), Expr::Number(kind), Expr::Bool(*is_static), Expr::Number(slots[0]), Expr::Number(slots[1]), Expr::Number(proto_kind), Expr::Number(length), ]; Some((class_name, operands)) } /// The table slot of `class_name`'s computed member function `name`, or -1. fn computed_fn_slot(&self, class_name: &str, name: Option<&String>) -> f64 { name.and_then(|name| { self.class_computed_map .get(&(class_name.to_string(), name.clone())) }) .and_then(|idx| self.func_to_table_idx.get(idx)) .map_or(-1.0, |&slot| slot as f64) } /// The table slot of `class_name`'s static block `index`, or -1. pub(in crate::emit) fn static_block_slot(&self, class_name: &str, index: u32) -> f64 { self.class_static_map .get(class_name) .and_then(|statics| statics.get(&format!("__perry_static_init_{index}"))) .and_then(|idx| self.func_to_table_idx.get(idx)) .map_or(-1.0, |&slot| slot as f64) } } /// A definition step of `template`'s evaluation that resolves a computed field key into /// its slot on the class's static side (`StaticFieldSet { template, slot }`), with the /// key's read-back from `computed_keys`. The evaluation copies the key right after the /// step: the static side is shared by every evaluation, and an `await` in a later key /// would let another evaluation overwrite the slot before a later copy. pub(in crate::emit) fn staged_computed_key<'e>( template: &str, step: &Expr, computed_keys: &'e [(String, Expr)], ) -> Option<&'e (String, Expr)> { match step { Expr::StaticFieldSet { class_name, field_name, .. } if class_name == template => computed_keys.iter().find(|(slot, _)| slot == field_name), _ => None, } } /// The `computed_keys` no definition step resolves, copied after all of them. pub(in crate::emit) fn unstaged_computed_keys<'e>( template: &'e str, steps: &'e [Expr], computed_keys: &'e [(String, Expr)], ) -> impl Iterator { computed_keys.iter().filter(move |(slot, _)| { !steps.iter().any(|step| { staged_computed_key(template, step, computed_keys).is_some_and(|(s, _)| s == slot) }) }) } /// A computed key slot is written on the class's static side /// (`StaticFieldSet { class, slot }`) and read back as /// `PropertyGet(ClassRef(class), slot)`; the read takes the same static storage. pub(in crate::emit) fn template_static_read(class_name: &str, key: &Expr) -> Expr { match key { Expr::PropertyGet { object, property, .. } if matches!(object.as_ref(), Expr::ClassRef(c) if c == class_name) => { Expr::StaticFieldGet { class_name: class_name.to_string(), field_name: property.clone(), } } _ => key.clone(), } }