142 lines
6.3 KiB
Rust
142 lines
6.3 KiB
Rust
//! class 声明整体 emit:驱动头、原型、方法、字段各阶段,组装最终类对象。
|
|
//!
|
|
//! 函数:`emit_class`。
|
|
|
|
use crate::{CompileCtx, Emitter, FunctionBodyContext};
|
|
use oxide_bytecode::opcode::OpCode;
|
|
use oxide_ir::inst::Inst;
|
|
use oxide_ir::operand::Operand;
|
|
use oxide_parser::{Class, ClassElement, MethodDefinitionKind, PropertyKey};
|
|
|
|
impl Emitter {
|
|
pub(crate) fn emit_class(&self, class: &Class, ctx: &mut CompileCtx) -> Result<u32, String> {
|
|
let mut constructor_method = None;
|
|
let mut instance_fields = Vec::new();
|
|
let mut private_names = Vec::<(String, u32)>::new();
|
|
let is_derived = class.super_class.is_some();
|
|
|
|
for element in &class.body.body {
|
|
match element {
|
|
ClassElement::MethodDefinition(method) => {
|
|
let method = method.as_ref();
|
|
if let PropertyKey::PrivateIdentifier(private) = &method.key {
|
|
let name = private.name.as_str().to_string();
|
|
if private_names.iter().any(|(existing, _)| existing == &name) {
|
|
return Err(format!("duplicate private name #{name}"));
|
|
}
|
|
let id = ctx.scopes.next_private_name_id;
|
|
ctx.scopes.next_private_name_id = ctx.scopes.next_private_name_id.saturating_add(1);
|
|
private_names.push((name, id));
|
|
}
|
|
if method.kind == MethodDefinitionKind::Constructor {
|
|
if constructor_method.is_some() {
|
|
return Err("duplicate class constructor".into());
|
|
}
|
|
constructor_method = Some(method);
|
|
}
|
|
}
|
|
ClassElement::PropertyDefinition(prop) => {
|
|
let prop = prop.as_ref();
|
|
if let PropertyKey::PrivateIdentifier(private) = &prop.key {
|
|
let name = private.name.as_str().to_string();
|
|
if private_names.iter().any(|(existing, _)| existing == &name) {
|
|
return Err(format!("duplicate private name #{name}"));
|
|
}
|
|
let id = ctx.scopes.next_private_name_id;
|
|
ctx.scopes.next_private_name_id = ctx.scopes.next_private_name_id.saturating_add(1);
|
|
private_names.push((name, id));
|
|
}
|
|
if !prop.r#static {
|
|
instance_fields.push(prop);
|
|
}
|
|
}
|
|
ClassElement::AccessorProperty(_) => return Err("class accessor properties not yet supported".into()),
|
|
ClassElement::StaticBlock(_) | ClassElement::TSIndexSignature(_) => {}
|
|
}
|
|
}
|
|
|
|
let (ctor_reg, proto_reg, super_reg) = self.emit_class_header(class, ctx)?;
|
|
let ctor_name = class.id.as_ref().map(|id| id.name.to_string());
|
|
let self_binding = ctor_name.as_deref().map(|name| vec![(name, ctor_reg)]).unwrap_or_default();
|
|
let saved_derived = ctx.in_derived_constructor;
|
|
let saved_private_names = ctx.scopes.private_name_map.clone();
|
|
ctx.in_derived_constructor = is_derived;
|
|
ctx.scopes.private_name_map = private_names.clone();
|
|
|
|
let emit_instance_fields = |compiler: &Emitter, field_ctx: &mut CompileCtx| -> Result<(), String> {
|
|
for field in &instance_fields {
|
|
if let PropertyKey::PrivateIdentifier(private) = &field.key {
|
|
compiler.emit_private_field_init(
|
|
Operand::This,
|
|
private.name.as_str(),
|
|
field.value.as_ref(),
|
|
field_ctx,
|
|
)?;
|
|
} else {
|
|
compiler.emit_public_field_init(
|
|
Operand::This,
|
|
&field.key,
|
|
field.computed,
|
|
field.value.as_ref(),
|
|
field_ctx,
|
|
)?;
|
|
}
|
|
}
|
|
Ok(())
|
|
};
|
|
|
|
let mut ctor_module = if let Some(method) = constructor_method {
|
|
let (param_names, body_stmts) = self.extract_function_parts(method.value.as_ref())?;
|
|
self.compile_function_body_with_field_hooks(
|
|
¶m_names,
|
|
body_stmts,
|
|
ctx,
|
|
false,
|
|
&self_binding,
|
|
FunctionBodyContext::ClassElement,
|
|
Some(&emit_instance_fields),
|
|
is_derived,
|
|
)?
|
|
} else {
|
|
let mut module = self.compile_function_body_with_field_hooks(
|
|
&[],
|
|
&[],
|
|
ctx,
|
|
false,
|
|
&self_binding,
|
|
FunctionBodyContext::ClassElement,
|
|
Some(&emit_instance_fields),
|
|
is_derived,
|
|
)?;
|
|
if is_derived {
|
|
module.insts.clear();
|
|
module.constants.clear();
|
|
module.n_registers = 1;
|
|
module.insts.push(Inst::super_call(Operand::None, Operand::None, 0));
|
|
let mut field_ctx = CompileCtx::new();
|
|
field_ctx.scopes.private_name_map = private_names.clone();
|
|
emit_instance_fields(self, &mut field_ctx)?;
|
|
module.insts.extend(field_ctx.insts);
|
|
module.constants = field_ctx.constants;
|
|
module.n_registers = field_ctx.max_regs.max(1);
|
|
module
|
|
.insts
|
|
.push(Inst::new(OpCode::RETURN, Operand::None, Operand::None, Operand::None));
|
|
}
|
|
module
|
|
};
|
|
ctx.in_derived_constructor = saved_derived;
|
|
ctor_module.is_class_constructor = true;
|
|
ctor_module.is_derived_constructor = is_derived;
|
|
ctor_module.function_name = ctor_name.clone();
|
|
ctx.nested.push(ctor_module);
|
|
|
|
self.emit_class_prototype(ctor_reg, proto_reg, super_reg, ctx.nested.len() as u16, ctx)?;
|
|
self.emit_class_methods(&class.body.body, ctor_reg, proto_reg, &self_binding, ctx)?;
|
|
self.emit_class_static_elements(&class.body.body, ctor_reg, ctx)?;
|
|
|
|
ctx.scopes.private_name_map = saved_private_names;
|
|
Ok(ctor_reg)
|
|
}
|
|
}
|