docs: 补充注释与格式化
This commit is contained in:
@@ -1,2 +1 @@
|
||||
//! 类编译核心预留模块(当前为空,为后续类相关公共状态预留位置)。
|
||||
|
||||
|
||||
@@ -3,9 +3,9 @@
|
||||
//! 函数:`emit_class`。
|
||||
|
||||
use crate::{CompileCtx, Emitter, FunctionBodyContext};
|
||||
use oxide_bytecode::opcode::OpCode;
|
||||
use oxide_ir::inst::Inst;
|
||||
use oxide_ir::operand::Operand;
|
||||
use oxide_bytecode::opcode::OpCode;
|
||||
use oxide_parser::{Class, ClassElement, MethodDefinitionKind, PropertyKey};
|
||||
|
||||
impl Emitter {
|
||||
@@ -66,9 +66,20 @@ impl Emitter {
|
||||
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)?;
|
||||
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)?;
|
||||
compiler.emit_public_field_init(
|
||||
Operand::This,
|
||||
&field.key,
|
||||
field.computed,
|
||||
field.value.as_ref(),
|
||||
field_ctx,
|
||||
)?;
|
||||
}
|
||||
}
|
||||
Ok(())
|
||||
@@ -108,7 +119,9 @@ impl Emitter {
|
||||
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
|
||||
.insts
|
||||
.push(Inst::new(OpCode::RETURN, Operand::None, Operand::None, Operand::None));
|
||||
}
|
||||
module
|
||||
};
|
||||
|
||||
@@ -5,10 +5,10 @@ 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.
|
||||
/// 按源码顺序发静态字段初始化与静态块。
|
||||
/// JS 类语义要求两者交织执行(如 `static x = 1; static { this.y = 1 }
|
||||
/// static z = this.y + 1` 必须按此顺序),若全部字段先于静态块发出,
|
||||
/// `z` 会在 `y` 赋值前读到它。
|
||||
pub(crate) fn emit_class_static_elements(
|
||||
&self, elements: &[ClassElement], ctor_reg: u32, ctx: &mut CompileCtx,
|
||||
) -> Result<(), String> {
|
||||
@@ -23,9 +23,20 @@ impl Emitter {
|
||||
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), private.name.as_str(), prop.value.as_ref(), ctx)?;
|
||||
self.emit_private_field_init(
|
||||
Operand::Reg(ctor_reg),
|
||||
private.name.as_str(),
|
||||
prop.value.as_ref(),
|
||||
ctx,
|
||||
)?;
|
||||
} else {
|
||||
self.emit_public_field_init(Operand::Reg(ctor_reg), &prop.key, prop.computed, prop.value.as_ref(), ctx)?;
|
||||
self.emit_public_field_init(
|
||||
Operand::Reg(ctor_reg),
|
||||
&prop.key,
|
||||
prop.computed,
|
||||
prop.value.as_ref(),
|
||||
ctx,
|
||||
)?;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -2,10 +2,10 @@
|
||||
//! 负责单个方法函数体(含 `super` 与 home object)。
|
||||
|
||||
use crate::{CompileCtx, Emitter, FunctionBodyContext};
|
||||
use oxide_ir::inst::Inst;
|
||||
use oxide_ir::operand::Operand;
|
||||
use oxide_bytecode::module::Constant;
|
||||
use oxide_bytecode::opcode::OpCode;
|
||||
use oxide_ir::inst::Inst;
|
||||
use oxide_ir::operand::Operand;
|
||||
use oxide_parser::{ClassElement, MethodDefinitionKind, PropertyKey};
|
||||
|
||||
impl Emitter {
|
||||
@@ -36,9 +36,19 @@ impl Emitter {
|
||||
match method.kind {
|
||||
MethodDefinitionKind::Method => {
|
||||
if method.computed {
|
||||
ctx.inst(Inst::new(OpCode::SET_PROP_DYNAMIC, Operand::Reg(home_reg), Operand::Reg(key_reg), Operand::Reg(accessor_reg)));
|
||||
ctx.inst(Inst::new(
|
||||
OpCode::SET_PROP_DYNAMIC,
|
||||
Operand::Reg(home_reg),
|
||||
Operand::Reg(key_reg),
|
||||
Operand::Reg(accessor_reg),
|
||||
));
|
||||
} else {
|
||||
ctx.inst(Inst::new(OpCode::SET_PROP, Operand::Reg(home_reg), Operand::Reg(accessor_reg), Operand::Reg(key_reg)));
|
||||
ctx.inst(Inst::new(
|
||||
OpCode::SET_PROP,
|
||||
Operand::Reg(home_reg),
|
||||
Operand::Reg(accessor_reg),
|
||||
Operand::Reg(key_reg),
|
||||
));
|
||||
}
|
||||
}
|
||||
MethodDefinitionKind::Get | MethodDefinitionKind::Set => {
|
||||
|
||||
@@ -1,10 +1,10 @@
|
||||
//! 类原型对象构造:`emit_class_prototype` 建立 `Class.prototype` 与属性初始化。
|
||||
|
||||
use crate::{CompileCtx, Emitter};
|
||||
use oxide_ir::inst::Inst;
|
||||
use oxide_ir::operand::Operand;
|
||||
use oxide_bytecode::module::Constant;
|
||||
use oxide_bytecode::opcode::OpCode;
|
||||
use oxide_ir::inst::Inst;
|
||||
use oxide_ir::operand::Operand;
|
||||
|
||||
impl Emitter {
|
||||
pub(crate) fn emit_class_prototype(
|
||||
@@ -17,21 +17,46 @@ impl Emitter {
|
||||
let parent_proto_key_reg = ctx.alloc_reg();
|
||||
ctx.inst(Inst::load_const(Operand::Reg(parent_proto_key_reg), proto_key_idx));
|
||||
let parent_proto_reg = ctx.alloc_reg();
|
||||
ctx.inst(Inst::new(OpCode::GET_PROP, Operand::Reg(super_reg), Operand::Reg(parent_proto_reg), Operand::Reg(parent_proto_key_reg)));
|
||||
ctx.inst(Inst::new(
|
||||
OpCode::GET_PROP,
|
||||
Operand::Reg(super_reg),
|
||||
Operand::Reg(parent_proto_reg),
|
||||
Operand::Reg(parent_proto_key_reg),
|
||||
));
|
||||
let proto_link_idx = ctx.add_constant(Constant::String("__proto__".to_string()));
|
||||
let proto_link_key_reg = ctx.alloc_reg();
|
||||
ctx.inst(Inst::load_const(Operand::Reg(proto_link_key_reg), proto_link_idx));
|
||||
ctx.inst(Inst::new(OpCode::SET_PROP, Operand::Reg(proto_reg), Operand::Reg(parent_proto_reg), Operand::Reg(proto_link_key_reg)));
|
||||
ctx.inst(Inst::new(OpCode::SET_PROP, Operand::Reg(ctor_reg), Operand::Reg(super_reg), Operand::Reg(proto_link_key_reg)));
|
||||
ctx.inst(Inst::new(
|
||||
OpCode::SET_PROP,
|
||||
Operand::Reg(proto_reg),
|
||||
Operand::Reg(parent_proto_reg),
|
||||
Operand::Reg(proto_link_key_reg),
|
||||
));
|
||||
ctx.inst(Inst::new(
|
||||
OpCode::SET_PROP,
|
||||
Operand::Reg(ctor_reg),
|
||||
Operand::Reg(super_reg),
|
||||
Operand::Reg(proto_link_key_reg),
|
||||
));
|
||||
}
|
||||
let ctor_key_idx = ctx.add_constant(Constant::String("constructor".to_string()));
|
||||
let ctor_key_reg = ctx.alloc_reg();
|
||||
ctx.inst(Inst::load_const(Operand::Reg(ctor_key_reg), ctor_key_idx));
|
||||
ctx.inst(Inst::new(OpCode::SET_PROP, Operand::Reg(proto_reg), Operand::Reg(ctor_reg), Operand::Reg(ctor_key_reg)));
|
||||
ctx.inst(Inst::new(
|
||||
OpCode::SET_PROP,
|
||||
Operand::Reg(proto_reg),
|
||||
Operand::Reg(ctor_reg),
|
||||
Operand::Reg(ctor_key_reg),
|
||||
));
|
||||
let proto_key_idx = ctx.add_constant(Constant::String("prototype".to_string()));
|
||||
let proto_key_reg = ctx.alloc_reg();
|
||||
ctx.inst(Inst::load_const(Operand::Reg(proto_key_reg), proto_key_idx));
|
||||
ctx.inst(Inst::new(OpCode::SET_PROP, Operand::Reg(ctor_reg), Operand::Reg(proto_reg), Operand::Reg(proto_key_reg)));
|
||||
ctx.inst(Inst::new(
|
||||
OpCode::SET_PROP,
|
||||
Operand::Reg(ctor_reg),
|
||||
Operand::Reg(proto_reg),
|
||||
Operand::Reg(proto_key_reg),
|
||||
));
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user