feat: 实现for-of与解构赋值

This commit is contained in:
爱特每羊队
2026-06-14 23:45:12 +08:00
parent c766d69d0a
commit 21e3693c63
15 changed files with 891 additions and 81 deletions
+47 -14
View File
@@ -44,6 +44,8 @@ pub(crate) enum Label {
DoWhileEnd(u32),
ForInStart(u32),
ForInEnd(u32),
ForOfStart(u32),
ForOfEnd(u32),
SwitchEnd(u32),
SwitchCase(u32, u32),
CatchBody(u32),
@@ -86,6 +88,23 @@ pub(crate) enum FunctionBodyContext {
ClassElement,
}
pub(crate) enum ParamSpec<'a> {
Identifier(String),
Pattern {
synthetic_name: String,
pattern: &'a oxide_parser::BindingPattern<'a>,
},
}
impl ParamSpec<'_> {
pub(crate) fn register_name(&self) -> &str {
match self {
Self::Identifier(name) => name,
Self::Pattern { synthetic_name, .. } => synthetic_name,
}
}
}
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
enum ConstantKey {
Number(u64),
@@ -366,18 +385,23 @@ impl Compiler {
pub(crate) fn extract_function_parts<'a>(
&self, function: &'a oxide_parser::Function<'a>,
) -> Result<(Vec<String>, &'a [Statement<'a>]), String> {
let mut param_names = Vec::new();
for param in &function.params.items {
) -> Result<(Vec<ParamSpec<'a>>, &'a [Statement<'a>]), String> {
let mut param_specs = Vec::new();
for (idx, param) in function.params.items.iter().enumerate() {
match &param.pattern {
oxide_parser::BindingPattern::BindingIdentifier(bi) => {
param_names.push(bi.name.to_string());
param_specs.push(ParamSpec::Identifier(bi.name.to_string()));
}
pattern => {
param_specs.push(ParamSpec::Pattern {
synthetic_name: format!("@@param_{idx}"),
pattern,
});
}
_ => return Err("destructuring parameters not supported".into()),
}
}
let body_stmts: &[Statement] = if let Some(body) = &function.body { &body.statements } else { &[] };
Ok((param_names, body_stmts))
Ok((param_specs, body_stmts))
}
/// Compile a function body (used for FD, FE, and arrow functions).
@@ -388,8 +412,8 @@ impl Compiler {
/// (true for arrow functions, which have lexical super) or reset to false
/// (false for regular functions, which create a new super scope).
pub(crate) fn compile_function_body<'a>(
&self, param_names: &[String], body_stmts: &[Statement<'a>], parent_ctx: &CompileCtx, is_expression_body: bool,
is_arrow: bool,
&self, param_specs: &[ParamSpec<'a>], body_stmts: &[Statement<'a>], parent_ctx: &CompileCtx,
is_expression_body: bool, is_arrow: bool,
) -> Result<CompiledModule, String> {
let body_context = if is_arrow {
FunctionBodyContext::Arrow
@@ -397,7 +421,7 @@ impl Compiler {
FunctionBodyContext::Ordinary
};
self.compile_function_body_with_bindings(
param_names,
param_specs,
body_stmts,
parent_ctx,
is_expression_body,
@@ -407,8 +431,8 @@ impl Compiler {
}
pub(crate) fn compile_function_body_with_bindings<'a>(
&self, param_names: &[String], body_stmts: &[Statement<'a>], parent_ctx: &CompileCtx, is_expression_body: bool,
extra_bindings: &[(&str, u8)], body_context: FunctionBodyContext,
&self, param_specs: &[ParamSpec<'a>], body_stmts: &[Statement<'a>], parent_ctx: &CompileCtx,
is_expression_body: bool, extra_bindings: &[(&str, u8)], body_context: FunctionBodyContext,
) -> Result<CompiledModule, String> {
let mut ctx = CompileCtx::new();
@@ -467,7 +491,8 @@ impl Compiler {
let param_base = ctx.next_reg;
// Register parameters as initialized.
for name in param_names {
for spec in param_specs {
let name = spec.register_name();
let reg = ctx.alloc_reg();
ctx.declare_initialized(name, reg, VariableDeclarationKind::Var, false)?;
}
@@ -480,11 +505,19 @@ impl Compiler {
ctx.reset_regs();
// Emit pass - reallocate params (same order = same regs after reset)
for name in param_names {
for spec in param_specs {
let name = spec.register_name();
let reg = ctx.alloc_reg();
ctx.declare_initialized(name, reg, VariableDeclarationKind::Var, false)?;
}
for spec in param_specs {
if let ParamSpec::Pattern { synthetic_name, pattern } = spec {
let src_reg = ctx.lookup(synthetic_name)?;
self.emit_binding_pattern(pattern, src_reg, VariableDeclarationKind::Var, false, &mut ctx)?;
}
}
// Emit body statements.
// Capture the last statement's expression result for expression-body arrows.
let mut last_result_reg = None;
@@ -530,7 +563,7 @@ impl Compiler {
bytecode: ctx.bytecode,
constants: ctx.constants,
n_registers: ctx.max_regs,
n_args: param_names.len() as u8,
n_args: param_specs.len() as u8,
param_base,
builtin_reg_map: ctx.builtin_reg_map,
sub_modules: ctx.sub_modules,
+141 -4
View File
@@ -6,6 +6,102 @@ use oxide_parser::{
use crate::compiler::{is_side_effect_free, CompileCtx, Compiler, Label};
impl Compiler {
fn count_binding_pattern(&self, pattern: &oxide_parser::BindingPattern, ctx: &mut CompileCtx) {
match pattern {
oxide_parser::BindingPattern::BindingIdentifier(_) => {
ctx.alloc_reg();
ctx.projected_pc += 1;
}
oxide_parser::BindingPattern::ArrayPattern(ap) => {
ctx.projected_pc += 1; // FOR_OF_INIT
for elem in &ap.elements {
ctx.alloc_reg();
ctx.projected_pc += 1; // FOR_OF_DONE
ctx.alloc_reg();
ctx.projected_pc += 1; // FOR_OF_NEXT
if let Some(inner) = elem {
self.count_binding_pattern(inner, ctx);
}
}
if let Some(rest) = &ap.rest {
self.count_rest_array(ctx);
self.count_binding_pattern(&rest.argument, ctx);
}
ctx.projected_pc += 1; // FOR_OF_CLOSE
}
oxide_parser::BindingPattern::ObjectPattern(op) => {
for prop in &op.properties {
ctx.alloc_reg();
ctx.projected_pc += 5; // LOAD_VAR + LOAD_CONST + IC_GET_PROP + 3 ext
ctx.alloc_reg();
self.count_binding_pattern(&prop.value, ctx);
}
if let Some(rest) = &op.rest {
ctx.alloc_reg();
ctx.projected_pc += 2; // REST_OBJECT + ext
self.count_binding_pattern(&rest.argument, ctx);
}
}
oxide_parser::BindingPattern::AssignmentPattern(ap) => {
ctx.alloc_reg();
ctx.projected_pc += 2; // LOAD_CONST undefined + STRICT_EQ
ctx.projected_pc += 1; // JMP_IF_FALSE
self.count_expression(&ap.right, ctx);
ctx.projected_pc += 1; // LOAD_VAR default
self.count_binding_pattern(&ap.left, ctx);
}
}
}
fn count_rest_array(&self, ctx: &mut CompileCtx) {
ctx.alloc_reg(); // rest
ctx.projected_pc += 1; // NEW_ARRAY
ctx.alloc_reg(); // idx
ctx.projected_pc += 1; // LOAD_CONST 0
ctx.alloc_reg(); // has
ctx.projected_pc += 2; // FOR_OF_DONE + JMP_IF_FALSE
ctx.alloc_reg(); // val
ctx.projected_pc += 1; // FOR_OF_NEXT
ctx.projected_pc += 1; // SET_ELEM
ctx.alloc_reg(); // inc tmp
ctx.projected_pc += 2; // INC_PRE + JMP
}
fn count_array_assignment(&self, ap: &oxide_parser::ArrayAssignmentTarget, ctx: &mut CompileCtx) {
ctx.projected_pc += 1; // FOR_OF_INIT
for elem in &ap.elements {
ctx.alloc_reg();
ctx.projected_pc += 1; // FOR_OF_DONE
ctx.alloc_reg();
ctx.projected_pc += 1; // FOR_OF_NEXT
if elem.is_some() {
ctx.alloc_reg();
ctx.projected_pc += 1;
}
}
if ap.rest.is_some() {
self.count_rest_array(ctx);
ctx.alloc_reg();
ctx.projected_pc += 1;
}
ctx.projected_pc += 1; // FOR_OF_CLOSE
}
fn count_object_assignment(&self, op: &oxide_parser::ObjectAssignmentTarget, ctx: &mut CompileCtx) {
for _ in &op.properties {
ctx.alloc_reg();
ctx.projected_pc += 5; // LOAD_VAR + LOAD_CONST + IC_GET_PROP + 3 ext
ctx.alloc_reg();
ctx.projected_pc += 1;
}
if op.rest.is_some() {
ctx.alloc_reg();
ctx.projected_pc += 2; // REST_OBJECT + ext
ctx.alloc_reg();
ctx.projected_pc += 1;
}
}
fn count_class(&self, class: &oxide_parser::Class, ctx: &mut CompileCtx) {
ctx.alloc_reg(); // ctor_reg
ctx.alloc_reg(); // proto_reg
@@ -57,11 +153,11 @@ impl Compiler {
}
Statement::VariableDeclaration(decl) => {
for d in &decl.declarations {
ctx.alloc_reg();
if let Some(init) = &d.init {
self.count_expression(init, ctx);
ctx.projected_pc += 1; // STORE_VAR
self.count_binding_pattern(&d.id, ctx);
} else {
ctx.alloc_reg();
ctx.projected_pc += 2; // LOAD_CONST(undefined) + STORE_VAR
}
}
@@ -126,11 +222,11 @@ impl Compiler {
self.count_expression(expr, ctx);
} else if let ForStatementInit::VariableDeclaration(decl) = init {
for d in &decl.declarations {
ctx.alloc_reg();
if let Some(init_expr) = &d.init {
self.count_expression(init_expr, ctx);
ctx.projected_pc += 1; // STORE_VAR
self.count_binding_pattern(&d.id, ctx);
} else {
ctx.alloc_reg();
ctx.projected_pc += 2; // LOAD_CONST(undefined) + STORE_VAR
}
}
@@ -181,6 +277,41 @@ impl Compiler {
ctx.label_map.insert(end_label, ctx.projected_pc);
ctx.projected_pc += 1; // FOR_IN_CLEANUP
}
Statement::ForOfStatement(fo) => {
let id = ctx.next_label_id();
let start_label = Label::ForOfStart(id);
let end_label = Label::ForOfEnd(id);
self.count_expression(&fo.right, ctx);
ctx.projected_pc += 1; // FOR_OF_INIT
ctx.label_map.insert(start_label, ctx.projected_pc);
ctx.alloc_reg(); // has_reg
ctx.projected_pc += 2; // FOR_OF_DONE + JMP_IF_FALSE
ctx.alloc_reg(); // val_reg
ctx.projected_pc += 1; // FOR_OF_NEXT
match &fo.left {
oxide_parser::ForStatementLeft::VariableDeclaration(decl) => {
for d in &decl.declarations {
self.count_binding_pattern(&d.id, ctx);
}
}
oxide_parser::ForStatementLeft::AssignmentTargetIdentifier(_) => {
ctx.alloc_reg();
ctx.projected_pc += 1;
}
oxide_parser::ForStatementLeft::ArrayAssignmentTarget(ap) => {
self.count_array_assignment(ap, ctx);
}
oxide_parser::ForStatementLeft::ObjectAssignmentTarget(op) => {
self.count_object_assignment(op, ctx);
}
_ => {}
}
self.count_statement(&fo.body, ctx);
ctx.projected_pc += 1; // JMP back
ctx.label_map.insert(end_label, ctx.projected_pc);
ctx.projected_pc += 1; // FOR_OF_CLOSE
}
Statement::SwitchStatement(sw) => {
let id = ctx.next_label_id();
let end_label = Label::SwitchEnd(id);
@@ -410,6 +541,12 @@ impl Compiler {
self.count_expression(&assign.right, ctx);
ctx.alloc_reg();
ctx.projected_pc += 1; // SET_PROP_DYNAMIC
} else if let oxide_parser::AssignmentTarget::ArrayAssignmentTarget(ap) = &assign.left {
self.count_expression(&assign.right, ctx);
self.count_array_assignment(ap, ctx);
} else if let oxide_parser::AssignmentTarget::ObjectAssignmentTarget(op) = &assign.left {
self.count_expression(&assign.right, ctx);
self.count_object_assignment(op, ctx);
} else {
self.count_expression(&assign.right, ctx);
ctx.alloc_reg();
+398 -32
View File
@@ -1,15 +1,305 @@
use crate::compiler::Label;
use crate::opcode::{self, OpCode};
use oxide_parser::{
AssignmentOperator, Class, ClassElement, Expression, ForStatementInit, ForStatementLeft, MethodDefinitionKind,
AssignmentOperator, AssignmentTarget, AssignmentTargetMaybeDefault, AssignmentTargetProperty, BindingPattern,
Class, ClassElement, Expression, ForStatementInit, ForStatementLeft, MethodDefinitionKind, ObjectAssignmentTarget,
PropertyKey, PropertyKind, SimpleAssignmentTarget, Statement, UnaryOperator, UpdateOperator,
VariableDeclarationKind,
};
use crate::compiler::{is_int_literal, is_side_effect_free, BinaryOperator, CompileCtx, Compiler, FunctionBodyContext};
use crate::compiler::{
is_int_literal, is_side_effect_free, BinaryOperator, CompileCtx, Compiler, FunctionBodyContext, ParamSpec,
};
use crate::module::Constant;
impl Compiler {
fn static_property_name(&self, key: &PropertyKey) -> Result<String, String> {
match key {
PropertyKey::StaticIdentifier(ident) => Ok(ident.name.as_str().to_string()),
PropertyKey::Identifier(ident) => Ok(ident.name.as_str().to_string()),
PropertyKey::StringLiteral(s) => Ok(s.value.to_string()),
PropertyKey::NumericLiteral(n) => Ok(n.value.to_string()),
_ => Err("computed destructuring keys not yet supported".into()),
}
}
fn emit_default_if_undefined(
&self, val_reg: u8, default_expr: &Expression, ctx: &mut CompileCtx,
) -> Result<u8, String> {
let undef_reg = self.emit_undefined(ctx);
let eq_reg = ctx.alloc_reg();
ctx.emit(opcode::encode(OpCode::STRICT_EQ, eq_reg, val_reg, undef_reg));
let jump_pos = ctx.bytecode.len();
ctx.emit(opcode::encode_jmp_if_false(eq_reg, 0));
let default_reg = self.emit_expression(default_expr, ctx)?;
ctx.emit(opcode::encode(OpCode::LOAD_VAR, val_reg, default_reg, 0));
let after = ctx.bytecode.len();
let offset = after as isize - jump_pos as isize;
ctx.bytecode[jump_pos] = opcode::encode_jmp_if_false(eq_reg, offset as i16);
Ok(val_reg)
}
fn emit_bind_target(
&self, name: &str, src_reg: u8, kind: VariableDeclarationKind, is_const: bool, ctx: &mut CompileCtx,
) -> Result<(), String> {
let var_reg = ctx.alloc_reg();
ctx.declare(name, var_reg, kind, is_const)?;
ctx.emit(opcode::encode(OpCode::STORE_VAR, var_reg, src_reg, if is_const { 1 } else { 0 }));
ctx.init_var(name);
Ok(())
}
fn emit_assign_target(&self, target: &AssignmentTarget, src_reg: u8, ctx: &mut CompileCtx) -> Result<(), String> {
match target {
AssignmentTarget::AssignmentTargetIdentifier(id) => {
let name = id.name.as_str();
let var_reg = ctx.lookup_or_global(name);
ctx.emit(opcode::encode(
OpCode::STORE_VAR,
var_reg,
src_reg,
if ctx.lookup_const_flag(name) { 1 } else { 0 },
));
Ok(())
}
AssignmentTarget::ArrayAssignmentTarget(ap) => self.emit_array_assignment(ap, src_reg, ctx),
AssignmentTarget::ObjectAssignmentTarget(op) => self.emit_object_assignment(op, src_reg, ctx),
_ => Err("assignment target not supported".into()),
}
}
pub(crate) fn emit_binding_pattern(
&self, pattern: &BindingPattern, src_reg: u8, kind: VariableDeclarationKind, is_const: bool,
ctx: &mut CompileCtx,
) -> Result<(), String> {
match pattern {
BindingPattern::BindingIdentifier(bi) => {
self.emit_bind_target(bi.name.as_str(), src_reg, kind, is_const, ctx)
}
BindingPattern::ArrayPattern(ap) => self.emit_array_binding(ap, src_reg, kind, is_const, ctx),
BindingPattern::ObjectPattern(op) => self.emit_object_binding(op, src_reg, kind, is_const, ctx),
BindingPattern::AssignmentPattern(ap) => {
let val_reg = self.emit_default_if_undefined(src_reg, &ap.right, ctx)?;
self.emit_binding_pattern(&ap.left, val_reg, kind, is_const, ctx)
}
}
}
fn emit_array_binding(
&self, ap: &oxide_parser::ArrayPattern, src_reg: u8, kind: VariableDeclarationKind, is_const: bool,
ctx: &mut CompileCtx,
) -> Result<(), String> {
ctx.emit(opcode::encode(OpCode::FOR_OF_INIT, 0, src_reg, 0));
for elem in &ap.elements {
let has_reg = ctx.alloc_reg();
ctx.emit(opcode::encode(OpCode::FOR_OF_DONE, has_reg, 0, 0));
let val_reg = ctx.alloc_reg();
ctx.emit(opcode::encode(OpCode::FOR_OF_NEXT, val_reg, 0, 0));
if let Some(pattern) = elem {
self.emit_binding_pattern(pattern, val_reg, kind, is_const, ctx)?;
}
}
if let Some(rest) = &ap.rest {
let rest_reg = self.emit_collect_rest_array(ctx)?;
self.emit_binding_pattern(&rest.argument, rest_reg, kind, is_const, ctx)?;
}
ctx.emit(opcode::encode(OpCode::FOR_OF_CLOSE, 0, 0, 0));
Ok(())
}
fn emit_collect_rest_array(&self, ctx: &mut CompileCtx) -> Result<u8, String> {
let rest_reg = ctx.alloc_reg();
ctx.emit(opcode::encode(OpCode::NEW_ARRAY, rest_reg, 0, 0));
let idx_reg = ctx.alloc_reg();
let zero_idx = ctx.add_constant(Constant::Int(0));
ctx.emit_load_const(idx_reg, zero_idx);
let loop_start = ctx.bytecode.len();
let has_reg = ctx.alloc_reg();
ctx.emit(opcode::encode(OpCode::FOR_OF_DONE, has_reg, 0, 0));
let end_jmp = ctx.bytecode.len();
ctx.emit(opcode::encode_jmp_if_false(has_reg, 0));
let val_reg = ctx.alloc_reg();
ctx.emit(opcode::encode(OpCode::FOR_OF_NEXT, val_reg, 0, 0));
ctx.emit(opcode::encode(OpCode::SET_ELEM, rest_reg, idx_reg, val_reg));
let tmp_reg = ctx.alloc_reg();
ctx.emit(opcode::encode(OpCode::INC_PRE, idx_reg, tmp_reg, tmp_reg));
let back = loop_start as isize - ctx.bytecode.len() as isize;
ctx.emit(opcode::encode_jmp(back as i16));
let after = ctx.bytecode.len();
ctx.bytecode[end_jmp] = opcode::encode_jmp_if_false(has_reg, (after as isize - end_jmp as isize) as i16);
Ok(rest_reg)
}
fn emit_object_property_read(&self, src_reg: u8, key: &str, ctx: &mut CompileCtx) -> u8 {
let prop_reg = ctx.alloc_reg();
ctx.emit(opcode::encode(OpCode::LOAD_VAR, prop_reg, src_reg, 0));
let key_idx = ctx.add_constant(Constant::String(key.to_string()));
let key_reg = ctx.alloc_reg();
ctx.emit_load_const(key_reg, key_idx);
ctx.emit(opcode::encode(OpCode::IC_GET_PROP, 0, prop_reg, key_reg));
ctx.emit(0);
ctx.emit(0);
ctx.emit(0);
prop_reg
}
fn emit_property_key_expression(&self, key: &PropertyKey, ctx: &mut CompileCtx) -> Result<u8, String> {
match key {
PropertyKey::Identifier(ident) => {
let name = ident.name.as_str();
let var_reg = ctx.lookup(name)?;
let key_reg = ctx.alloc_reg();
ctx.emit(opcode::encode(OpCode::LOAD_VAR, key_reg, var_reg, 0));
Ok(key_reg)
}
PropertyKey::StringLiteral(s) => {
let key_idx = ctx.add_constant(Constant::String(s.value.to_string()));
let key_reg = ctx.alloc_reg();
ctx.emit_load_const(key_reg, key_idx);
Ok(key_reg)
}
PropertyKey::NumericLiteral(n) => {
let key_idx = ctx.add_constant(Constant::Number(n.value));
let key_reg = ctx.alloc_reg();
ctx.emit_load_const(key_reg, key_idx);
Ok(key_reg)
}
PropertyKey::StaticIdentifier(ident) => {
let key_idx = ctx.add_constant(Constant::String(ident.name.as_str().to_string()));
let key_reg = ctx.alloc_reg();
ctx.emit_load_const(key_reg, key_idx);
Ok(key_reg)
}
_ => Err("computed destructuring key expression not supported".into()),
}
}
fn emit_object_property_read_key(
&self, src_reg: u8, key: &PropertyKey, computed: bool, ctx: &mut CompileCtx,
) -> Result<(u8, Option<String>), String> {
if !computed {
let key_name = self.static_property_name(key)?;
let prop_reg = self.emit_object_property_read(src_reg, &key_name, ctx);
return Ok((prop_reg, Some(key_name)));
}
let prop_reg = ctx.alloc_reg();
ctx.emit(opcode::encode(OpCode::LOAD_VAR, prop_reg, src_reg, 0));
let key_reg = self.emit_property_key_expression(key, ctx)?;
let val_reg = ctx.alloc_reg();
ctx.emit(opcode::encode(OpCode::GET_PROP_DYNAMIC, prop_reg, key_reg, val_reg));
Ok((val_reg, None))
}
fn emit_object_binding(
&self, op: &oxide_parser::ObjectPattern, src_reg: u8, kind: VariableDeclarationKind, is_const: bool,
ctx: &mut CompileCtx,
) -> Result<(), String> {
let mut excluded = Vec::new();
for prop in &op.properties {
let (prop_reg, static_key) = self.emit_object_property_read_key(src_reg, &prop.key, prop.computed, ctx)?;
if let Some(key) = static_key {
excluded.push(key);
}
self.emit_binding_pattern(&prop.value, prop_reg, kind, is_const, ctx)?;
}
if let Some(rest) = &op.rest {
let rest_reg = ctx.alloc_reg();
let excluded_idx = ctx.add_constant(Constant::String(excluded.join("\0")));
ctx.emit(opcode::encode(OpCode::REST_OBJECT, rest_reg, src_reg, 0));
ctx.emit(excluded_idx as u32);
self.emit_binding_pattern(&rest.argument, rest_reg, kind, is_const, ctx)?;
}
Ok(())
}
fn emit_assignment_maybe_default(
&self, target: &AssignmentTargetMaybeDefault, src_reg: u8, ctx: &mut CompileCtx,
) -> Result<(), String> {
match target {
AssignmentTargetMaybeDefault::AssignmentTargetWithDefault(default) => {
let val_reg = self.emit_default_if_undefined(src_reg, &default.init, ctx)?;
self.emit_assign_target(&default.binding, val_reg, ctx)
}
AssignmentTargetMaybeDefault::ArrayAssignmentTarget(ap) => self.emit_array_assignment(ap, src_reg, ctx),
AssignmentTargetMaybeDefault::ObjectAssignmentTarget(op) => self.emit_object_assignment(op, src_reg, ctx),
AssignmentTargetMaybeDefault::AssignmentTargetIdentifier(id) => {
let name = id.name.as_str();
let var_reg = ctx.lookup_or_global(name);
ctx.emit(opcode::encode(
OpCode::STORE_VAR,
var_reg,
src_reg,
if ctx.lookup_const_flag(name) { 1 } else { 0 },
));
Ok(())
}
_ => Err("assignment target not supported".into()),
}
}
fn emit_array_assignment(
&self, ap: &oxide_parser::ArrayAssignmentTarget, src_reg: u8, ctx: &mut CompileCtx,
) -> Result<(), String> {
ctx.emit(opcode::encode(OpCode::FOR_OF_INIT, 0, src_reg, 0));
for elem in &ap.elements {
let has_reg = ctx.alloc_reg();
ctx.emit(opcode::encode(OpCode::FOR_OF_DONE, has_reg, 0, 0));
let val_reg = ctx.alloc_reg();
ctx.emit(opcode::encode(OpCode::FOR_OF_NEXT, val_reg, 0, 0));
if let Some(target) = elem {
self.emit_assignment_maybe_default(target, val_reg, ctx)?;
}
}
if let Some(rest) = &ap.rest {
let rest_reg = self.emit_collect_rest_array(ctx)?;
self.emit_assign_target(&rest.target, rest_reg, ctx)?;
}
ctx.emit(opcode::encode(OpCode::FOR_OF_CLOSE, 0, 0, 0));
Ok(())
}
fn emit_object_assignment(
&self, op: &ObjectAssignmentTarget, src_reg: u8, ctx: &mut CompileCtx,
) -> Result<(), String> {
let mut excluded = Vec::new();
for prop in &op.properties {
match prop {
AssignmentTargetProperty::AssignmentTargetPropertyIdentifier(id) => {
let key = id.binding.name.as_str().to_string();
excluded.push(key.clone());
let mut prop_reg = self.emit_object_property_read(src_reg, &key, ctx);
if let Some(default_expr) = &id.init {
prop_reg = self.emit_default_if_undefined(prop_reg, default_expr, ctx)?;
}
let name = id.binding.name.as_str();
let var_reg = ctx.lookup_or_global(name);
ctx.emit(opcode::encode(
OpCode::STORE_VAR,
var_reg,
prop_reg,
if ctx.lookup_const_flag(name) { 1 } else { 0 },
));
}
AssignmentTargetProperty::AssignmentTargetPropertyProperty(prop) => {
let (prop_reg, static_key) =
self.emit_object_property_read_key(src_reg, &prop.name, prop.computed, ctx)?;
if let Some(key) = static_key {
excluded.push(key);
}
self.emit_assignment_maybe_default(&prop.binding, prop_reg, ctx)?;
}
}
}
if let Some(rest) = &op.rest {
let rest_reg = ctx.alloc_reg();
let excluded_idx = ctx.add_constant(Constant::String(excluded.join("\0")));
ctx.emit(opcode::encode(OpCode::REST_OBJECT, rest_reg, src_reg, 0));
ctx.emit(excluded_idx as u32);
self.emit_assign_target(&rest.target, rest_reg, ctx)?;
}
Ok(())
}
fn class_property_name(&self, key: &PropertyKey) -> Result<String, String> {
match key {
PropertyKey::StaticIdentifier(ident) => Ok(ident.name.as_str().to_string()),
@@ -266,35 +556,34 @@ impl Compiler {
Statement::VariableDeclaration(decl) => {
let mut r = None;
for d in &decl.declarations {
let name = match &d.id {
oxide_parser::BindingPattern::BindingIdentifier(bi) => bi.name.as_str(),
_ => return Err("destructuring not supported".into()),
};
let is_const = matches!(decl.kind, VariableDeclarationKind::Const);
if is_const && d.init.is_none() {
return Err("const declaration must have an initializer".into());
}
let var_reg = ctx.alloc_reg();
ctx.declare(name, var_reg, decl.kind, is_const)?;
if let Some(init) = &d.init {
let val_reg = self.emit_expression(init, ctx)?;
let const_flag = if is_const { 1 } else { 0 };
ctx.emit(opcode::encode(OpCode::STORE_VAR, var_reg, val_reg, const_flag));
ctx.init_var(name);
self.emit_binding_pattern(&d.id, val_reg, decl.kind, is_const, ctx)?;
// Name inference (D-04): if the initializer is an arrow function,
// set the compiled sub_module's function_name.
if matches!(*init, Expression::ArrowFunctionExpression(_)) {
if let Some(sub_mod) = ctx.sub_modules.last_mut() {
sub_mod.function_name = Some(name.to_string());
if let BindingPattern::BindingIdentifier(bi) = &d.id {
if matches!(*init, Expression::ArrowFunctionExpression(_)) {
if let Some(sub_mod) = ctx.sub_modules.last_mut() {
sub_mod.function_name = Some(bi.name.to_string());
}
}
}
r = Some(val_reg);
} else {
let BindingPattern::BindingIdentifier(bi) = &d.id else {
return Err("destructuring declaration requires an initializer".into());
};
let idx = ctx.add_constant(Constant::Undefined);
let tmp = ctx.alloc_reg();
ctx.emit_load_const(tmp, idx);
let var_reg = ctx.alloc_reg();
ctx.declare(bi.name.as_str(), var_reg, decl.kind, is_const)?;
ctx.emit(opcode::encode(OpCode::STORE_VAR, var_reg, tmp, 0));
ctx.init_var(name);
ctx.init_var(bi.name.as_str());
r = Some(var_reg);
}
}
@@ -415,16 +704,13 @@ impl Compiler {
self.emit_expression(expr, ctx)?;
} else if let ForStatementInit::VariableDeclaration(decl) = init {
for d in &decl.declarations {
let name = match &d.id {
oxide_parser::BindingPattern::BindingIdentifier(bi) => bi.name.as_str(),
_ => return Err("destructuring not supported".into()),
};
let var_reg = ctx.alloc_reg();
ctx.declare(name, var_reg, decl.kind, matches!(decl.kind, VariableDeclarationKind::Const))?;
let is_const = matches!(decl.kind, VariableDeclarationKind::Const);
if let Some(init_expr) = &d.init {
let val_reg = self.emit_expression(init_expr, ctx)?;
ctx.emit(opcode::encode(OpCode::STORE_VAR, var_reg, val_reg, 0));
ctx.init_var(name);
self.emit_binding_pattern(&d.id, val_reg, decl.kind, is_const, ctx)?;
} else if let BindingPattern::BindingIdentifier(bi) = &d.id {
let var_reg = ctx.alloc_reg();
ctx.declare(bi.name.as_str(), var_reg, decl.kind, is_const)?;
}
}
}
@@ -504,6 +790,51 @@ impl Compiler {
ctx.pop_loop();
Ok(None)
}
Statement::ForOfStatement(fo) => {
let id = ctx.next_label_id();
let start_label = Label::ForOfStart(id);
let end_label = Label::ForOfEnd(id);
let iter_src_reg = self.emit_expression(&fo.right, ctx)?;
ctx.emit(opcode::encode(OpCode::FOR_OF_INIT, 0, iter_src_reg, 0));
ctx.push_loop(end_label, start_label);
let has_reg = ctx.alloc_reg();
ctx.emit(opcode::encode(OpCode::FOR_OF_DONE, has_reg, 0, 0));
let end_pos = ctx.resolve_label(end_label)?;
let offset = (end_pos as isize) - (ctx.bytecode.len() as isize);
ctx.emit(opcode::encode_jmp_if_false(has_reg, offset as i16));
let val_reg = ctx.alloc_reg();
ctx.emit(opcode::encode(OpCode::FOR_OF_NEXT, val_reg, 0, 0));
match &fo.left {
ForStatementLeft::VariableDeclaration(decl) => {
for d in &decl.declarations {
self.emit_binding_pattern(&d.id, val_reg, decl.kind, false, ctx)?;
}
}
ForStatementLeft::AssignmentTargetIdentifier(id_ref) => {
let name = id_ref.name.as_str();
let var_reg = ctx.lookup_or_global(name);
ctx.emit(opcode::encode(OpCode::STORE_VAR, var_reg, val_reg, 0));
}
ForStatementLeft::ArrayAssignmentTarget(ap) => {
self.emit_array_assignment(ap, val_reg, ctx)?;
}
ForStatementLeft::ObjectAssignmentTarget(op) => {
self.emit_object_assignment(op, val_reg, ctx)?;
}
_ => return Err("unsupported for-of left-hand side".into()),
}
self.emit_statement(&fo.body, ctx)?;
let start_pos = ctx.resolve_label(start_label)?;
let offset = (start_pos as isize) - (ctx.bytecode.len() as isize);
ctx.emit(opcode::encode_jmp(offset as i16));
ctx.emit(opcode::encode(OpCode::FOR_OF_CLOSE, 0, 0, 0));
ctx.pop_loop();
Ok(None)
}
Statement::SwitchStatement(sw) => {
let id = ctx.next_label_id();
let end_label = Label::SwitchEnd(id);
@@ -572,9 +903,17 @@ impl Compiler {
// Extract params
let mut param_names = Vec::new();
for param in &fd.params.items {
if let oxide_parser::BindingPattern::BindingIdentifier(bi) = &param.pattern {
param_names.push(bi.name.to_string());
for (idx, param) in fd.params.items.iter().enumerate() {
match &param.pattern {
oxide_parser::BindingPattern::BindingIdentifier(bi) => {
param_names.push(ParamSpec::Identifier(bi.name.to_string()));
}
pattern => {
param_names.push(ParamSpec::Pattern {
synthetic_name: format!("@@param_{idx}"),
pattern,
});
}
}
}
@@ -1105,6 +1444,17 @@ impl Compiler {
ctx.emit(opcode::encode(OpCode::STORE_VAR, var_reg, val_reg, const_flag));
Ok(val_reg)
}
} else if matches!(
&assign.left,
oxide_parser::AssignmentTarget::ArrayAssignmentTarget(_)
| oxide_parser::AssignmentTarget::ObjectAssignmentTarget(_)
) {
if assign.operator != AssignmentOperator::Assign {
return Err("compound destructuring assignment not supported".into());
}
let val_reg = self.emit_expression(&assign.right, ctx)?;
self.emit_assign_target(&assign.left, val_reg, ctx)?;
Ok(val_reg)
} else {
Err("assignment target not supported".into())
}
@@ -1295,9 +1645,17 @@ impl Compiler {
// Extract param names (same pattern as FunctionExpression)
let mut param_names = Vec::new();
for param in &arrow.params.items {
if let oxide_parser::BindingPattern::BindingIdentifier(bi) = &param.pattern {
param_names.push(bi.name.to_string());
for (idx, param) in arrow.params.items.iter().enumerate() {
match &param.pattern {
oxide_parser::BindingPattern::BindingIdentifier(bi) => {
param_names.push(ParamSpec::Identifier(bi.name.to_string()));
}
pattern => {
param_names.push(ParamSpec::Pattern {
synthetic_name: format!("@@param_{idx}"),
pattern,
});
}
}
}
@@ -1321,9 +1679,17 @@ impl Compiler {
Expression::FunctionExpression(fe) => {
// FunctionExpression: compile body, emit LOAD_CONST(BytecodeFunc)
let mut param_names = Vec::new();
for param in &fe.params.items {
if let oxide_parser::BindingPattern::BindingIdentifier(bi) = &param.pattern {
param_names.push(bi.name.to_string());
for (idx, param) in fe.params.items.iter().enumerate() {
match &param.pattern {
oxide_parser::BindingPattern::BindingIdentifier(bi) => {
param_names.push(ParamSpec::Identifier(bi.name.to_string()));
}
pattern => {
param_names.push(ParamSpec::Pattern {
synthetic_name: format!("@@param_{idx}"),
pattern,
});
}
}
}
+3
View File
@@ -73,6 +73,7 @@ pub enum OpCode {
DELETE_PROP_STATIC = 0x39,
DELETE_PROP_DYNAMIC = 0x3A,
INSTANCEOF = 0x3B,
REST_OBJECT = 0x3E,
// -- Variable (0x30-0x32) --
LOAD_VAR = 0x30,
@@ -233,6 +234,7 @@ impl TryFrom<u8> for OpCode {
0x39 => Ok(OpCode::DELETE_PROP_STATIC),
0x3A => Ok(OpCode::DELETE_PROP_DYNAMIC),
0x3B => Ok(OpCode::INSTANCEOF),
0x3E => Ok(OpCode::REST_OBJECT),
0x40 => Ok(OpCode::CALL),
0x41 => Ok(OpCode::RETURN),
0x42 => Ok(OpCode::CALL_NATIVE),
@@ -342,6 +344,7 @@ impl fmt::Display for OpCode {
OpCode::DELETE_PROP_STATIC => "DELETE_PROP_STATIC",
OpCode::DELETE_PROP_DYNAMIC => "DELETE_PROP_DYNAMIC",
OpCode::INSTANCEOF => "INSTANCEOF",
OpCode::REST_OBJECT => "REST_OBJECT",
OpCode::LOAD_VAR => "LOAD_VAR",
OpCode::STORE_VAR => "STORE_VAR",
OpCode::LOAD_CONST => "LOAD_CONST",