mirror of
https://github.com/pjh456/OxideJS.git
synced 2026-10-23 18:21:13 +08:00
feat: 支持 class extends 与 super 调用
This commit is contained in:
@@ -70,6 +70,8 @@ pub(crate) struct CompileCtx {
|
||||
/// Used by arrow functions to capture lexical `this` (D-01).
|
||||
/// Initialized to 254 (conventional this register) at the top level.
|
||||
pub(crate) enclosing_this_reg: u8,
|
||||
pub(crate) in_derived_constructor: bool,
|
||||
pub(crate) in_instance_method: bool,
|
||||
}
|
||||
|
||||
impl CompileCtx {
|
||||
@@ -89,6 +91,8 @@ impl CompileCtx {
|
||||
builtin_reg_map: Vec::new(),
|
||||
sub_modules: Vec::new(),
|
||||
enclosing_this_reg: 254, // conventional this register at top level
|
||||
in_derived_constructor: false,
|
||||
in_instance_method: false,
|
||||
}
|
||||
}
|
||||
|
||||
@@ -297,6 +301,8 @@ impl Compiler {
|
||||
|
||||
// Propagate enclosing_this_reg so nested arrow functions capture the correct `this`.
|
||||
ctx.enclosing_this_reg = parent_ctx.enclosing_this_reg;
|
||||
ctx.in_derived_constructor = parent_ctx.in_derived_constructor;
|
||||
ctx.in_instance_method = parent_ctx.in_instance_method;
|
||||
|
||||
// Inherit parent's global scope entries so previously-declared function names
|
||||
// are visible from within the body.
|
||||
@@ -402,6 +408,8 @@ impl Compiler {
|
||||
captured_this_const_idx: 0,
|
||||
function_name: None,
|
||||
is_class_constructor: false,
|
||||
is_derived_constructor: false,
|
||||
needs_home_object: false,
|
||||
})
|
||||
}
|
||||
|
||||
@@ -460,6 +468,8 @@ impl Compiler {
|
||||
captured_this_const_idx: 0,
|
||||
function_name: None,
|
||||
is_class_constructor: false,
|
||||
is_derived_constructor: false,
|
||||
needs_home_object: false,
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
@@ -9,8 +9,18 @@ impl Compiler {
|
||||
fn count_class(&self, class: &oxide_parser::Class, ctx: &mut CompileCtx) {
|
||||
ctx.alloc_reg(); // ctor_reg
|
||||
ctx.alloc_reg(); // proto_reg
|
||||
if let Some(super_class) = &class.super_class {
|
||||
self.count_expression(super_class, ctx);
|
||||
}
|
||||
ctx.projected_pc += 2; // LOAD_CONST ctor + NEW_OBJECT proto
|
||||
|
||||
if class.super_class.is_some() {
|
||||
ctx.alloc_reg(); // parent prototype key
|
||||
ctx.alloc_reg(); // parent prototype value
|
||||
ctx.alloc_reg(); // __proto__ key
|
||||
ctx.projected_pc += 5; // LOAD_CONST + GET_PROP + LOAD_CONST + two SET_PROP
|
||||
}
|
||||
|
||||
for element in &class.body.body {
|
||||
let ClassElement::MethodDefinition(method) = element else {
|
||||
continue;
|
||||
@@ -21,7 +31,7 @@ impl Compiler {
|
||||
MethodDefinitionKind::Method => {
|
||||
ctx.alloc_reg(); // method_reg
|
||||
ctx.alloc_reg(); // key_reg
|
||||
ctx.projected_pc += 3; // LOAD_CONST method + LOAD_CONST key + SET_PROP
|
||||
ctx.projected_pc += 4; // LOAD_CONST method + SET_HOME_OBJECT + LOAD_CONST key + SET_PROP
|
||||
}
|
||||
MethodDefinitionKind::Get | MethodDefinitionKind::Set => {}
|
||||
}
|
||||
@@ -335,13 +345,32 @@ impl Compiler {
|
||||
}
|
||||
}
|
||||
Expression::CallExpression(call) => {
|
||||
if matches!(&call.callee, Expression::Super(_)) {
|
||||
for arg in &call.arguments {
|
||||
if let Some(expr) = arg.as_expression() {
|
||||
self.count_expression(expr, ctx);
|
||||
}
|
||||
}
|
||||
ctx.alloc_reg();
|
||||
ctx.projected_pc += 2; // SUPER_CALL + arg_count ext word
|
||||
return;
|
||||
}
|
||||
match &call.callee {
|
||||
Expression::StaticMemberExpression(member) => {
|
||||
self.count_expression(&member.object, ctx);
|
||||
ctx.alloc_reg();
|
||||
ctx.projected_pc += 1;
|
||||
ctx.alloc_reg();
|
||||
ctx.projected_pc += 5; // LOAD_VAR + IC_GET_PROP + 3 ext words
|
||||
if matches!(&member.object, Expression::Super(_)) {
|
||||
ctx.alloc_reg(); // this register
|
||||
ctx.projected_pc += 1; // LOAD_VAR this
|
||||
ctx.alloc_reg(); // key
|
||||
ctx.projected_pc += 1; // LOAD_CONST key
|
||||
ctx.alloc_reg(); // callee
|
||||
ctx.projected_pc += 1; // SUPER_GET_PROP
|
||||
} else {
|
||||
self.count_expression(&member.object, ctx);
|
||||
ctx.alloc_reg();
|
||||
ctx.projected_pc += 1;
|
||||
ctx.alloc_reg();
|
||||
ctx.projected_pc += 5; // LOAD_VAR + IC_GET_PROP + 3 ext words
|
||||
}
|
||||
}
|
||||
_ => {
|
||||
self.count_expression(&call.callee, ctx);
|
||||
@@ -547,6 +576,15 @@ impl Compiler {
|
||||
}
|
||||
}
|
||||
Expression::StaticMemberExpression(member) => {
|
||||
if matches!(&member.object, Expression::Super(_)) {
|
||||
ctx.alloc_reg(); // key
|
||||
ctx.projected_pc += 1; // LOAD_CONST key
|
||||
ctx.alloc_reg(); // this
|
||||
ctx.projected_pc += 1; // LOAD_VAR this
|
||||
ctx.alloc_reg(); // result
|
||||
ctx.projected_pc += 1; // SUPER_GET_PROP
|
||||
return;
|
||||
}
|
||||
self.count_expression(&member.object, ctx);
|
||||
ctx.alloc_reg();
|
||||
ctx.projected_pc += 1; // LOAD_CONST key
|
||||
|
||||
@@ -19,12 +19,9 @@ impl Compiler {
|
||||
}
|
||||
|
||||
fn emit_class(&self, class: &Class, ctx: &mut CompileCtx) -> Result<u8, String> {
|
||||
if class.super_class.is_some() {
|
||||
return Err("class extends not yet supported".into());
|
||||
}
|
||||
|
||||
let mut constructor_method = None;
|
||||
let mut instance_methods = Vec::new();
|
||||
let is_derived = class.super_class.is_some();
|
||||
|
||||
for element in &class.body.body {
|
||||
let ClassElement::MethodDefinition(method) = element else {
|
||||
@@ -59,14 +56,33 @@ impl Compiler {
|
||||
let ctor_reg = ctx.alloc_reg();
|
||||
let proto_reg = ctx.alloc_reg();
|
||||
let self_binding = ctor_name.as_deref().map(|name| vec![(name, ctor_reg)]).unwrap_or_default();
|
||||
let super_reg = if let Some(super_expr) = &class.super_class {
|
||||
Some(self.emit_expression(super_expr, ctx)?)
|
||||
} else {
|
||||
None
|
||||
};
|
||||
|
||||
let saved_derived = ctx.in_derived_constructor;
|
||||
ctx.in_derived_constructor = is_derived;
|
||||
|
||||
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_bindings(¶m_names, body_stmts, ctx, false, &self_binding)?
|
||||
} else {
|
||||
self.compile_function_body_with_bindings(&[], &[], ctx, false, &self_binding)?
|
||||
let mut module = self.compile_function_body_with_bindings(&[], &[], ctx, false, &self_binding)?;
|
||||
if is_derived {
|
||||
module.bytecode.clear();
|
||||
module.constants.clear();
|
||||
module.n_registers = 1;
|
||||
module.bytecode.push(opcode::encode(OpCode::SUPER_CALL, 0, 0, 0));
|
||||
module.bytecode.push(0);
|
||||
module.bytecode.push(opcode::encode(OpCode::RETURN, 0, 0, 0));
|
||||
}
|
||||
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.sub_modules.push(ctor_module);
|
||||
|
||||
@@ -74,16 +90,35 @@ impl Compiler {
|
||||
ctx.emit_load_const(ctor_reg, ctor_idx);
|
||||
ctx.emit(opcode::encode(OpCode::NEW_OBJECT, proto_reg, 0, 0));
|
||||
|
||||
if let Some(super_reg) = super_reg {
|
||||
let proto_key_idx = ctx.add_constant(Constant::String("prototype".to_string()));
|
||||
let parent_proto_key_reg = ctx.alloc_reg();
|
||||
ctx.emit_load_const(parent_proto_key_reg, proto_key_idx);
|
||||
let parent_proto_reg = ctx.alloc_reg();
|
||||
ctx.emit(opcode::encode(OpCode::GET_PROP, super_reg, parent_proto_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.emit_load_const(proto_link_key_reg, proto_link_idx);
|
||||
ctx.emit(opcode::encode(OpCode::SET_PROP, proto_reg, parent_proto_reg, proto_link_key_reg));
|
||||
ctx.emit(opcode::encode(OpCode::SET_PROP, ctor_reg, super_reg, proto_link_key_reg));
|
||||
}
|
||||
|
||||
for (method, method_name) in instance_methods {
|
||||
let (param_names, body_stmts) = self.extract_function_parts(method.value.as_ref())?;
|
||||
let saved_method = ctx.in_instance_method;
|
||||
ctx.in_instance_method = true;
|
||||
let mut method_module =
|
||||
self.compile_function_body_with_bindings(¶m_names, body_stmts, ctx, false, &self_binding)?;
|
||||
ctx.in_instance_method = saved_method;
|
||||
method_module.function_name = Some(method_name.clone());
|
||||
method_module.needs_home_object = true;
|
||||
ctx.sub_modules.push(method_module);
|
||||
|
||||
let method_idx = ctx.add_constant(Constant::BytecodeFunc(ctx.sub_modules.len() as u32));
|
||||
let method_reg = ctx.alloc_reg();
|
||||
ctx.emit_load_const(method_reg, method_idx);
|
||||
ctx.emit(opcode::encode(OpCode::SET_HOME_OBJECT, method_reg, proto_reg, 0));
|
||||
|
||||
let key_idx = ctx.add_constant(Constant::String(method_name));
|
||||
let key_reg = ctx.alloc_reg();
|
||||
@@ -753,6 +788,20 @@ impl Compiler {
|
||||
}
|
||||
}
|
||||
Expression::StaticMemberExpression(member) => {
|
||||
if matches!(&member.object, Expression::Super(_)) {
|
||||
if !ctx.in_instance_method && !ctx.in_derived_constructor {
|
||||
return Err("super property only supported in class methods".into());
|
||||
}
|
||||
let prop_name = member.property.name.as_str();
|
||||
let idx = ctx.add_constant(Constant::String(prop_name.to_string()));
|
||||
let key_reg = ctx.alloc_reg();
|
||||
ctx.emit_load_const(key_reg, idx);
|
||||
let this_reg = ctx.alloc_reg();
|
||||
ctx.emit(opcode::encode(OpCode::LOAD_VAR, this_reg, 254, 0));
|
||||
let result_reg = ctx.alloc_reg();
|
||||
ctx.emit(opcode::encode(OpCode::SUPER_GET_PROP, result_reg, this_reg, key_reg));
|
||||
return Ok(result_reg);
|
||||
}
|
||||
let obj_reg = self.emit_expression(&member.object, ctx)?;
|
||||
let prop_name = member.property.name.as_str();
|
||||
let idx = ctx.add_constant(Constant::String(prop_name.to_string()));
|
||||
@@ -1139,19 +1188,49 @@ impl Compiler {
|
||||
}
|
||||
Expression::ParenthesizedExpression(p) => self.emit_expression(&p.expression, ctx),
|
||||
Expression::CallExpression(call) => {
|
||||
if matches!(&call.callee, Expression::Super(_)) {
|
||||
if !ctx.in_derived_constructor {
|
||||
return Err("super() only supported in derived constructors".into());
|
||||
}
|
||||
let mut arg_regs = Vec::new();
|
||||
for arg in &call.arguments {
|
||||
if let Some(expr) = arg.as_expression() {
|
||||
arg_regs.push(self.emit_expression(expr, ctx)?);
|
||||
}
|
||||
}
|
||||
let first_arg_reg = if arg_regs.is_empty() { 0u8 } else { arg_regs[0] };
|
||||
let result_reg = ctx.alloc_reg();
|
||||
ctx.emit(opcode::encode(OpCode::SUPER_CALL, result_reg, first_arg_reg, 0));
|
||||
ctx.emit(arg_regs.len() as u32);
|
||||
return Ok(result_reg);
|
||||
}
|
||||
let (callee_reg, this_reg) = match &call.callee {
|
||||
Expression::StaticMemberExpression(member) => {
|
||||
let obj_reg = self.emit_expression(&member.object, ctx)?;
|
||||
let is_super_member = matches!(&member.object, Expression::Super(_));
|
||||
let obj_reg = if is_super_member {
|
||||
let this_reg = ctx.alloc_reg();
|
||||
ctx.emit(opcode::encode(OpCode::LOAD_VAR, this_reg, 254, 0));
|
||||
this_reg
|
||||
} else {
|
||||
self.emit_expression(&member.object, ctx)?
|
||||
};
|
||||
let prop_name = member.property.name.as_str();
|
||||
let idx = ctx.add_constant(Constant::String(prop_name.to_string()));
|
||||
let key_reg = ctx.alloc_reg();
|
||||
ctx.emit_load_const(key_reg, idx);
|
||||
let callee_reg = ctx.alloc_reg();
|
||||
ctx.emit(opcode::encode(OpCode::LOAD_VAR, callee_reg, obj_reg, 0));
|
||||
ctx.emit(opcode::encode(OpCode::IC_GET_PROP, 0, callee_reg, key_reg));
|
||||
ctx.emit(0);
|
||||
ctx.emit(0);
|
||||
ctx.emit(0);
|
||||
if is_super_member {
|
||||
if !ctx.in_instance_method && !ctx.in_derived_constructor {
|
||||
return Err("super property only supported in class methods".into());
|
||||
}
|
||||
ctx.emit(opcode::encode(OpCode::SUPER_GET_PROP, callee_reg, obj_reg, key_reg));
|
||||
} else {
|
||||
ctx.emit(opcode::encode(OpCode::LOAD_VAR, callee_reg, obj_reg, 0));
|
||||
ctx.emit(opcode::encode(OpCode::IC_GET_PROP, 0, callee_reg, key_reg));
|
||||
ctx.emit(0);
|
||||
ctx.emit(0);
|
||||
ctx.emit(0);
|
||||
}
|
||||
(callee_reg, obj_reg)
|
||||
}
|
||||
_ => {
|
||||
|
||||
@@ -123,6 +123,9 @@ fn hash_statement(stmt: &Statement, h: &mut rustc_hash::FxHasher) {
|
||||
id.name.as_str().hash(h);
|
||||
}
|
||||
class.super_class.is_some().hash(h);
|
||||
if let Some(super_class) = &class.super_class {
|
||||
hash_expression(super_class, h);
|
||||
}
|
||||
for element in &class.body.body {
|
||||
hash_class_element(element, h);
|
||||
}
|
||||
@@ -319,6 +322,9 @@ fn hash_expression(expr: &Expression, h: &mut rustc_hash::FxHasher) {
|
||||
id.name.as_str().hash(h);
|
||||
}
|
||||
class.super_class.is_some().hash(h);
|
||||
if let Some(super_class) = &class.super_class {
|
||||
hash_expression(super_class, h);
|
||||
}
|
||||
for element in &class.body.body {
|
||||
hash_class_element(element, h);
|
||||
}
|
||||
@@ -333,6 +339,9 @@ fn hash_expression(expr: &Expression, h: &mut rustc_hash::FxHasher) {
|
||||
}
|
||||
}
|
||||
}
|
||||
Expression::Super(_) => {
|
||||
19u8.hash(h);
|
||||
}
|
||||
Expression::RegExpLiteral(lit) => {
|
||||
if let Some(raw) = &lit.raw {
|
||||
raw.to_string().hash(h);
|
||||
|
||||
@@ -34,6 +34,11 @@ pub struct CompiledModule {
|
||||
/// True when this bytecode function is a class constructor.
|
||||
/// Ordinary CALL must reject it, while NEW_EXPRESSION may construct through it.
|
||||
pub is_class_constructor: bool,
|
||||
/// True when this class constructor has an `extends` clause.
|
||||
/// `this` stays uninitialized until SUPER_CALL completes.
|
||||
pub is_derived_constructor: bool,
|
||||
/// True for prototype methods whose function object needs a runtime home_object.
|
||||
pub needs_home_object: bool,
|
||||
}
|
||||
|
||||
impl CompiledModule {
|
||||
@@ -50,6 +55,8 @@ impl CompiledModule {
|
||||
captured_this_const_idx: 0,
|
||||
function_name: None,
|
||||
is_class_constructor: false,
|
||||
is_derived_constructor: false,
|
||||
needs_home_object: false,
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -74,6 +81,8 @@ impl Clone for CompiledModule {
|
||||
captured_this_const_idx: self.captured_this_const_idx,
|
||||
function_name: self.function_name.clone(),
|
||||
is_class_constructor: self.is_class_constructor,
|
||||
is_derived_constructor: self.is_derived_constructor,
|
||||
needs_home_object: self.needs_home_object,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -84,6 +84,10 @@ pub enum OpCode {
|
||||
RETURN = 0x41,
|
||||
CALL_NATIVE = 0x42,
|
||||
NEW_EXPRESSION = 0x43,
|
||||
SUPER_CALL = 0x44,
|
||||
SUPER_GET_PROP = 0x45,
|
||||
SUPER_STATIC_GET_PROP = 0x46,
|
||||
SET_HOME_OBJECT = 0x47,
|
||||
|
||||
// ── Object Property (0x50-0x5F) ──
|
||||
IC_GET_PROP = 0x50,
|
||||
@@ -217,6 +221,10 @@ impl TryFrom<u8> for OpCode {
|
||||
0x41 => Ok(OpCode::RETURN),
|
||||
0x42 => Ok(OpCode::CALL_NATIVE),
|
||||
0x43 => Ok(OpCode::NEW_EXPRESSION),
|
||||
0x44 => Ok(OpCode::SUPER_CALL),
|
||||
0x45 => Ok(OpCode::SUPER_GET_PROP),
|
||||
0x46 => Ok(OpCode::SUPER_STATIC_GET_PROP),
|
||||
0x47 => Ok(OpCode::SET_HOME_OBJECT),
|
||||
0x50 => Ok(OpCode::IC_GET_PROP),
|
||||
0x51 => Ok(OpCode::IC_SET_PROP),
|
||||
0x52 => Ok(OpCode::GET_PROP),
|
||||
@@ -311,6 +319,10 @@ impl fmt::Display for OpCode {
|
||||
OpCode::RETURN => "RETURN",
|
||||
OpCode::CALL_NATIVE => "CALL_NATIVE",
|
||||
OpCode::NEW_EXPRESSION => "NEW_EXPRESSION",
|
||||
OpCode::SUPER_CALL => "SUPER_CALL",
|
||||
OpCode::SUPER_GET_PROP => "SUPER_GET_PROP",
|
||||
OpCode::SUPER_STATIC_GET_PROP => "SUPER_STATIC_GET_PROP",
|
||||
OpCode::SET_HOME_OBJECT => "SET_HOME_OBJECT",
|
||||
OpCode::IC_GET_PROP => "IC_GET_PROP",
|
||||
OpCode::IC_SET_PROP => "IC_SET_PROP",
|
||||
OpCode::GET_PROP => "GET_PROP",
|
||||
|
||||
Reference in New Issue
Block a user