This repository has been archived on 2026-09-15. You can view files and clone it. You cannot open issues or pull requests or push a commit.
Files
OxideJS/crates/oxide_emit/src/class/field.rs
T
2026-08-05 21:46:47 +08:00

43 lines
1.8 KiB
Rust
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
//! 类静态字段/元素 emit:`emit_class_static_elements`。
use crate::{CompileCtx, Emitter};
use oxide_ir::operand::Operand;
use oxide_parser::{ClassElement, PropertyKey};
impl Emitter {
/// Emit static field initializers and static blocks in source order.
/// JS class semantics interleave them (e.g. `static x = 1; static {
/// this.y = 1 } static z = this.y + 1` must run in that order). Emitting
/// all fields before all blocks would make `z` read `y` before it is set.
pub(crate) fn emit_class_static_elements(
&self, elements: &[ClassElement], ctor_reg: u8, ctx: &mut CompileCtx,
) -> Result<(), String> {
let saved_static_this = ctx.static_block_this_reg;
ctx.static_block_this_reg = Some(ctor_reg);
for element in elements {
match element {
ClassElement::PropertyDefinition(prop) => {
let prop = prop.as_ref();
if prop.r#static {
if let PropertyKey::PrivateIdentifier(private) = &prop.key {
self.emit_private_field_init(Operand::Reg(ctor_reg as u32), private.name.as_str(), prop.value.as_ref(), ctx)?;
} else {
self.emit_public_field_init(Operand::Reg(ctor_reg as u32), &prop.key, prop.computed, prop.value.as_ref(), ctx)?;
}
}
}
ClassElement::StaticBlock(block) => {
ctx.push_scope();
for stmt in &block.body {
self.emit_statement(stmt, ctx)?;
}
ctx.pop_scope();
}
_ => {}
}
}
ctx.static_block_this_reg = saved_static_this;
Ok(())
}
}