mirror of
https://github.com/pjh456/OxideJS.git
synced 2026-10-25 03:01:12 +08:00
refactor: 清除旧代码并展开 crate
This commit is contained in:
@@ -0,0 +1,382 @@
|
||||
use crate::compiler::{CompileCtx, Compiler};
|
||||
use oxide_bytecode::module::Constant;
|
||||
use oxide_bytecode::opcode::{self, OpCode};
|
||||
use oxide_parser::{
|
||||
ArrayAssignmentTarget, ArrayPattern, AssignmentTarget, AssignmentTargetMaybeDefault, AssignmentTargetProperty,
|
||||
BindingPattern, Expression, ObjectAssignmentTarget, ObjectPattern, VariableDeclarationKind,
|
||||
};
|
||||
|
||||
impl Compiler {
|
||||
pub(crate) fn count_binding_pattern(&self, pattern: &BindingPattern, ctx: &mut CompileCtx) {
|
||||
match pattern {
|
||||
BindingPattern::BindingIdentifier(_) => {
|
||||
ctx.alloc_reg();
|
||||
ctx.projected_pc += 1;
|
||||
}
|
||||
BindingPattern::ArrayPattern(ap) => {
|
||||
ctx.count_instr(); // FOR_OF_INIT
|
||||
for elem in &ap.elements {
|
||||
ctx.alloc_reg();
|
||||
ctx.count_instr(); // FOR_OF_DONE
|
||||
ctx.alloc_reg();
|
||||
ctx.count_instr(); // 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.count_instr(); // FOR_OF_CLOSE
|
||||
}
|
||||
BindingPattern::ObjectPattern(op) => {
|
||||
for prop in &op.properties {
|
||||
self.count_object_property_read_key(&prop.key, prop.computed, ctx);
|
||||
self.count_binding_pattern(&prop.value, ctx);
|
||||
}
|
||||
if let Some(rest) = &op.rest {
|
||||
ctx.alloc_reg();
|
||||
ctx.count_instr_with_ext(1); // REST_OBJECT + excluded-keys ext
|
||||
self.count_binding_pattern(&rest.argument, ctx);
|
||||
}
|
||||
}
|
||||
BindingPattern::AssignmentPattern(ap) => {
|
||||
ctx.count_load_const(); // undefined
|
||||
ctx.count_instr(); // STRICT_EQ
|
||||
ctx.count_jump(); // JMP_IF_FALSE
|
||||
self.count_expression(&ap.right, ctx);
|
||||
ctx.count_instr(); // LOAD_VAR default
|
||||
self.count_binding_pattern(&ap.left, ctx);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub(crate) fn count_rest_array(&self, ctx: &mut CompileCtx) {
|
||||
ctx.alloc_reg(); // rest
|
||||
ctx.count_instr(); // NEW_ARRAY
|
||||
ctx.count_load_const(); // idx = 0
|
||||
ctx.alloc_reg(); // has
|
||||
ctx.count_instr(); // FOR_OF_DONE
|
||||
ctx.count_jump(); // JMP_IF_FALSE
|
||||
ctx.alloc_reg(); // val
|
||||
ctx.count_instr(); // FOR_OF_NEXT
|
||||
ctx.count_instr(); // SET_ELEM
|
||||
ctx.alloc_reg(); // inc tmp
|
||||
ctx.count_instr(); // INC_PRE
|
||||
ctx.count_jump(); // JMP
|
||||
}
|
||||
|
||||
pub(crate) fn count_array_assignment(&self, ap: &ArrayAssignmentTarget, ctx: &mut CompileCtx) {
|
||||
ctx.count_instr(); // FOR_OF_INIT
|
||||
for elem in &ap.elements {
|
||||
ctx.alloc_reg();
|
||||
ctx.count_instr(); // FOR_OF_DONE
|
||||
ctx.alloc_reg();
|
||||
ctx.count_instr(); // FOR_OF_NEXT
|
||||
if let Some(elem) = elem {
|
||||
self.count_assignment_maybe_default(elem, ctx);
|
||||
}
|
||||
}
|
||||
if let Some(rest) = &ap.rest {
|
||||
self.count_rest_array(ctx);
|
||||
self.count_assign_target(&rest.target, ctx);
|
||||
}
|
||||
ctx.count_instr(); // FOR_OF_CLOSE
|
||||
}
|
||||
|
||||
pub(crate) fn count_object_assignment(&self, op: &ObjectAssignmentTarget, ctx: &mut CompileCtx) {
|
||||
for prop in &op.properties {
|
||||
match prop {
|
||||
AssignmentTargetProperty::AssignmentTargetPropertyIdentifier(id) => {
|
||||
self.count_object_property_read_static(ctx);
|
||||
if let Some(default_expr) = &id.init {
|
||||
ctx.count_load_const(); // undefined
|
||||
ctx.count_instr(); // STRICT_EQ
|
||||
ctx.count_jump(); // JMP_IF_FALSE
|
||||
self.count_expression(default_expr, ctx);
|
||||
ctx.count_instr(); // LOAD_VAR default
|
||||
}
|
||||
ctx.alloc_reg();
|
||||
ctx.count_instr(); // STORE_VAR
|
||||
}
|
||||
AssignmentTargetProperty::AssignmentTargetPropertyProperty(prop) => {
|
||||
self.count_object_property_read_key(&prop.name, prop.computed, ctx);
|
||||
self.count_assignment_maybe_default(&prop.binding, ctx);
|
||||
}
|
||||
}
|
||||
}
|
||||
if let Some(rest) = &op.rest {
|
||||
ctx.alloc_reg();
|
||||
ctx.count_instr_with_ext(1); // REST_OBJECT + excluded-keys ext
|
||||
self.count_assign_target(&rest.target, ctx);
|
||||
}
|
||||
}
|
||||
|
||||
pub(crate) fn count_assignment_maybe_default(&self, target: &AssignmentTargetMaybeDefault, ctx: &mut CompileCtx) {
|
||||
match target {
|
||||
AssignmentTargetMaybeDefault::AssignmentTargetWithDefault(default) => {
|
||||
ctx.count_load_const(); // undefined
|
||||
ctx.count_instr(); // STRICT_EQ
|
||||
ctx.count_jump(); // JMP_IF_FALSE
|
||||
self.count_expression(&default.init, ctx);
|
||||
ctx.count_instr(); // LOAD_VAR default
|
||||
self.count_assign_target(&default.binding, ctx);
|
||||
}
|
||||
AssignmentTargetMaybeDefault::ArrayAssignmentTarget(ap) => self.count_array_assignment(ap, ctx),
|
||||
AssignmentTargetMaybeDefault::ObjectAssignmentTarget(op) => self.count_object_assignment(op, ctx),
|
||||
AssignmentTargetMaybeDefault::AssignmentTargetIdentifier(_) => {
|
||||
ctx.alloc_reg();
|
||||
ctx.count_instr(); // STORE_VAR
|
||||
}
|
||||
_ => {}
|
||||
}
|
||||
}
|
||||
|
||||
pub(crate) fn count_assign_target(&self, target: &AssignmentTarget, ctx: &mut CompileCtx) {
|
||||
match target {
|
||||
AssignmentTarget::AssignmentTargetIdentifier(_) => {
|
||||
ctx.alloc_reg();
|
||||
ctx.count_instr(); // STORE_VAR
|
||||
}
|
||||
AssignmentTarget::ArrayAssignmentTarget(ap) => self.count_array_assignment(ap, ctx),
|
||||
AssignmentTarget::ObjectAssignmentTarget(op) => self.count_object_assignment(op, ctx),
|
||||
_ => {}
|
||||
}
|
||||
}
|
||||
|
||||
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;
|
||||
let offset = ctx.checked_jump_offset(offset);
|
||||
ctx.bytecode[jump_pos] = opcode::encode_jmp_if_false(eq_reg, offset);
|
||||
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();
|
||||
let target_reg = if matches!(kind, VariableDeclarationKind::Var) {
|
||||
match ctx.declare(name, var_reg, kind, is_const) {
|
||||
Ok(()) => var_reg,
|
||||
Err(_) => ctx.lookup(name).unwrap_or(var_reg),
|
||||
}
|
||||
} else {
|
||||
ctx.declare(name, var_reg, kind, is_const)?;
|
||||
var_reg
|
||||
};
|
||||
if ctx.scopes.symbols.lookup_is_captured(name) {
|
||||
let cell_idx = ctx.scopes.cell_registry.len() as u8;
|
||||
ctx.scopes.cell_registry.push((name.to_string(), cell_idx));
|
||||
ctx.emit(opcode::encode(OpCode::MAKE_CELL, src_reg, cell_idx, 0));
|
||||
} else {
|
||||
ctx.emit(opcode::encode(OpCode::STORE_VAR, target_reg, src_reg, if is_const { 1 } else { 0 }));
|
||||
}
|
||||
ctx.init_var(name);
|
||||
Ok(())
|
||||
}
|
||||
|
||||
pub(crate) 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: &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;
|
||||
let back = ctx.checked_jump_offset(back);
|
||||
ctx.emit(opcode::encode_jmp(back));
|
||||
let after = ctx.bytecode.len();
|
||||
let end_offset = after as isize - end_jmp as isize;
|
||||
let end_offset = ctx.checked_jump_offset(end_offset);
|
||||
ctx.bytecode[end_jmp] = opcode::encode_jmp_if_false(has_reg, end_offset);
|
||||
Ok(rest_reg)
|
||||
}
|
||||
|
||||
fn emit_object_binding(
|
||||
&self, op: &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()),
|
||||
}
|
||||
}
|
||||
|
||||
pub(crate) fn emit_array_assignment(
|
||||
&self, ap: &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(())
|
||||
}
|
||||
|
||||
pub(crate) 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(())
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,432 @@
|
||||
use crate::compiler::{CompileCtx, Compiler, Label};
|
||||
use oxide_bytecode::module::Constant;
|
||||
use oxide_bytecode::opcode::{self, OpCode};
|
||||
use oxide_parser::{ChainElement, Expression, LogicalOperator, PropertyKey};
|
||||
|
||||
impl Compiler {
|
||||
pub(crate) fn count_object_property_read_static(&self, ctx: &mut CompileCtx) {
|
||||
ctx.count_load_var();
|
||||
ctx.count_load_const();
|
||||
ctx.count_ic_instr_with_ext();
|
||||
}
|
||||
|
||||
pub(crate) fn count_object_property_read_key(&self, key: &PropertyKey, computed: bool, ctx: &mut CompileCtx) {
|
||||
if !computed {
|
||||
self.count_object_property_read_static(ctx);
|
||||
let _ = key;
|
||||
return;
|
||||
}
|
||||
|
||||
ctx.count_load_var();
|
||||
match key {
|
||||
PropertyKey::Identifier(ident) => {
|
||||
let name = ident.name.as_str();
|
||||
let _ = ctx.lookup_or_builtin(name);
|
||||
ctx.count_load_var();
|
||||
}
|
||||
PropertyKey::StringLiteral(_) => {
|
||||
ctx.count_load_const();
|
||||
}
|
||||
PropertyKey::NumericLiteral(_) => {
|
||||
ctx.count_load_const();
|
||||
}
|
||||
PropertyKey::StaticIdentifier(_) => {
|
||||
ctx.count_load_const();
|
||||
}
|
||||
_ => {}
|
||||
}
|
||||
ctx.alloc_reg();
|
||||
ctx.count_instr();
|
||||
}
|
||||
|
||||
pub(crate) fn count_optional_guard(&self, ctx: &mut CompileCtx) {
|
||||
ctx.count_load_var();
|
||||
ctx.count_jump();
|
||||
}
|
||||
|
||||
pub(crate) fn count_static_member_get_preserve_base(
|
||||
&self, member: &oxide_parser::StaticMemberExpression, short_chain: bool, ctx: &mut CompileCtx,
|
||||
) {
|
||||
self.count_chainable_expression(&member.object, short_chain, ctx);
|
||||
if short_chain && member.optional {
|
||||
self.count_optional_guard(ctx);
|
||||
}
|
||||
ctx.count_load_const();
|
||||
ctx.count_load_var();
|
||||
ctx.count_ic_instr_with_ext();
|
||||
}
|
||||
|
||||
pub(crate) fn count_computed_member_get_preserve_base(
|
||||
&self, member: &oxide_parser::ComputedMemberExpression, short_chain: bool, ctx: &mut CompileCtx,
|
||||
) {
|
||||
self.count_chainable_expression(&member.object, short_chain, ctx);
|
||||
if short_chain && member.optional {
|
||||
self.count_optional_guard(ctx);
|
||||
}
|
||||
self.count_expression(&member.expression, ctx);
|
||||
ctx.alloc_reg();
|
||||
ctx.count_instr();
|
||||
}
|
||||
|
||||
pub(crate) fn count_chain_call(
|
||||
&self, call: &oxide_parser::CallExpression, short_chain: bool, ctx: &mut CompileCtx,
|
||||
) {
|
||||
match &call.callee {
|
||||
Expression::StaticMemberExpression(member) => {
|
||||
self.count_static_member_get_preserve_base(member, short_chain, ctx)
|
||||
}
|
||||
Expression::ComputedMemberExpression(member) => {
|
||||
self.count_computed_member_get_preserve_base(member, short_chain, ctx);
|
||||
}
|
||||
Expression::PrivateFieldExpression(member) => {
|
||||
self.count_chainable_expression(&member.object, short_chain, ctx);
|
||||
if short_chain && member.optional {
|
||||
self.count_optional_guard(ctx);
|
||||
}
|
||||
ctx.count_private_access();
|
||||
}
|
||||
_ => {
|
||||
self.count_chainable_expression(&call.callee, short_chain, ctx);
|
||||
if short_chain && call.optional {
|
||||
self.count_optional_guard(ctx);
|
||||
}
|
||||
ctx.alloc_reg();
|
||||
ctx.projected_pc += 1;
|
||||
}
|
||||
}
|
||||
if short_chain
|
||||
&& call.optional
|
||||
&& matches!(
|
||||
&call.callee,
|
||||
Expression::StaticMemberExpression(_)
|
||||
| Expression::ComputedMemberExpression(_)
|
||||
| Expression::PrivateFieldExpression(_)
|
||||
)
|
||||
{
|
||||
self.count_optional_guard(ctx);
|
||||
}
|
||||
for arg in &call.arguments {
|
||||
if let Some(expr) = arg.as_expression() {
|
||||
self.count_expression(expr, ctx);
|
||||
}
|
||||
}
|
||||
ctx.count_call_instr_with_arg_ext();
|
||||
ctx.count_load_var();
|
||||
}
|
||||
|
||||
pub(crate) fn count_chainable_expression(&self, expr: &Expression, short_chain: bool, ctx: &mut CompileCtx) {
|
||||
match expr {
|
||||
Expression::StaticMemberExpression(member) => {
|
||||
self.count_static_member_get_preserve_base(member, short_chain, ctx)
|
||||
}
|
||||
Expression::ComputedMemberExpression(member) => {
|
||||
self.count_computed_member_get_preserve_base(member, short_chain, ctx)
|
||||
}
|
||||
Expression::PrivateFieldExpression(member) => {
|
||||
self.count_chainable_expression(&member.object, short_chain, ctx);
|
||||
if short_chain && member.optional {
|
||||
self.count_optional_guard(ctx);
|
||||
}
|
||||
ctx.count_private_access();
|
||||
}
|
||||
Expression::CallExpression(call) => self.count_chain_call(call, short_chain, ctx),
|
||||
Expression::ChainExpression(chain) => self.count_chain_element(&chain.expression, short_chain, ctx),
|
||||
_ => self.count_expression(expr, ctx),
|
||||
}
|
||||
}
|
||||
|
||||
pub(crate) fn count_chain_element(&self, element: &ChainElement, short_chain: bool, ctx: &mut CompileCtx) {
|
||||
match element {
|
||||
ChainElement::StaticMemberExpression(member) => {
|
||||
self.count_static_member_get_preserve_base(member, short_chain, ctx)
|
||||
}
|
||||
ChainElement::ComputedMemberExpression(member) => {
|
||||
self.count_computed_member_get_preserve_base(member, short_chain, ctx)
|
||||
}
|
||||
ChainElement::PrivateFieldExpression(member) => {
|
||||
self.count_chainable_expression(&member.object, short_chain, ctx);
|
||||
if short_chain && member.optional {
|
||||
self.count_optional_guard(ctx);
|
||||
}
|
||||
ctx.count_private_access();
|
||||
}
|
||||
ChainElement::CallExpression(call) => self.count_chain_call(call, short_chain, ctx),
|
||||
ChainElement::TSNonNullExpression(_) => {}
|
||||
}
|
||||
}
|
||||
|
||||
pub(crate) fn count_logical_assign_test(&self, op: LogicalOperator, id: u32, ctx: &mut CompileCtx) {
|
||||
match op {
|
||||
LogicalOperator::And | LogicalOperator::Or => {
|
||||
ctx.projected_pc += 1;
|
||||
}
|
||||
LogicalOperator::Coalesce => {
|
||||
ctx.projected_pc += 1;
|
||||
ctx.projected_pc += 1;
|
||||
ctx.labels.label_map.insert(Label::TernaryElse(id), ctx.projected_pc);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl Compiler {
|
||||
fn emit_optional_guard(&self, reg: u8, short_label: Label, ctx: &mut CompileCtx) -> Result<(), String> {
|
||||
let short_pos = ctx.resolve_label(short_label)?;
|
||||
let dup_reg = ctx.alloc_reg();
|
||||
ctx.emit(opcode::encode(OpCode::LOAD_VAR, dup_reg, reg, 0));
|
||||
let offset = (short_pos as isize) - (ctx.bytecode.len() as isize);
|
||||
let offset = ctx.checked_jump_offset(offset);
|
||||
ctx.emit(opcode::encode_jmp_if_nullish(dup_reg, offset));
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn emit_static_member_get_preserve_base(
|
||||
&self, member: &oxide_parser::StaticMemberExpression, short_label: Option<Label>, ctx: &mut CompileCtx,
|
||||
) -> Result<(u8, u8), String> {
|
||||
let obj_reg = self.emit_chainable_expression(&member.object, short_label, ctx)?;
|
||||
if member.optional {
|
||||
if let Some(label) = short_label {
|
||||
self.emit_optional_guard(obj_reg, label, ctx)?;
|
||||
}
|
||||
}
|
||||
let idx = ctx.add_constant(Constant::String(member.property.name.as_str().to_string()));
|
||||
let key_reg = ctx.alloc_reg();
|
||||
ctx.emit_load_const(key_reg, idx);
|
||||
let value_reg = ctx.alloc_reg();
|
||||
ctx.emit(opcode::encode(OpCode::LOAD_VAR, value_reg, obj_reg, 0));
|
||||
ctx.emit(opcode::encode(OpCode::IC_GET_PROP, 0, value_reg, key_reg));
|
||||
ctx.emit(0);
|
||||
ctx.emit(0);
|
||||
ctx.emit(0);
|
||||
Ok((value_reg, obj_reg))
|
||||
}
|
||||
|
||||
fn emit_computed_member_get_preserve_base(
|
||||
&self, member: &oxide_parser::ComputedMemberExpression, short_label: Option<Label>, ctx: &mut CompileCtx,
|
||||
) -> Result<(u8, u8), String> {
|
||||
let obj_reg = self.emit_chainable_expression(&member.object, short_label, ctx)?;
|
||||
if member.optional {
|
||||
if let Some(label) = short_label {
|
||||
self.emit_optional_guard(obj_reg, label, ctx)?;
|
||||
}
|
||||
}
|
||||
let key_reg = self.emit_expression(&member.expression, ctx)?;
|
||||
let value_reg = ctx.alloc_reg();
|
||||
ctx.emit(opcode::encode(OpCode::GET_PROP_DYNAMIC, obj_reg, key_reg, value_reg));
|
||||
Ok((value_reg, obj_reg))
|
||||
}
|
||||
|
||||
fn emit_chain_call(
|
||||
&self, call: &oxide_parser::CallExpression, short_label: Option<Label>, ctx: &mut CompileCtx,
|
||||
) -> Result<u8, String> {
|
||||
let (callee_reg, this_reg) = match &call.callee {
|
||||
Expression::StaticMemberExpression(member) => {
|
||||
self.emit_static_member_get_preserve_base(member, short_label, ctx)?
|
||||
}
|
||||
Expression::ComputedMemberExpression(member) => {
|
||||
self.emit_computed_member_get_preserve_base(member, short_label, ctx)?
|
||||
}
|
||||
Expression::PrivateFieldExpression(member) => {
|
||||
let obj_reg = self.emit_chainable_expression(&member.object, short_label, ctx)?;
|
||||
if member.optional {
|
||||
if let Some(label) = short_label {
|
||||
self.emit_optional_guard(obj_reg, label, ctx)?;
|
||||
}
|
||||
}
|
||||
let key_reg = self.emit_private_id_reg(member.field.name.as_str(), ctx)?;
|
||||
let callee_reg = ctx.alloc_reg();
|
||||
ctx.emit(opcode::encode(OpCode::GET_PRIVATE, callee_reg, obj_reg, key_reg));
|
||||
(callee_reg, obj_reg)
|
||||
}
|
||||
_ => {
|
||||
let callee_reg = self.emit_chainable_expression(&call.callee, short_label, ctx)?;
|
||||
if call.optional {
|
||||
if let Some(label) = short_label {
|
||||
self.emit_optional_guard(callee_reg, label, ctx)?;
|
||||
}
|
||||
}
|
||||
let this_idx = ctx.add_constant(Constant::Undefined);
|
||||
let this_reg = ctx.alloc_reg();
|
||||
ctx.emit_load_const(this_reg, this_idx);
|
||||
(callee_reg, this_reg)
|
||||
}
|
||||
};
|
||||
if call.optional
|
||||
&& matches!(
|
||||
&call.callee,
|
||||
Expression::StaticMemberExpression(_)
|
||||
| Expression::ComputedMemberExpression(_)
|
||||
| Expression::PrivateFieldExpression(_)
|
||||
)
|
||||
{
|
||||
if let Some(label) = short_label {
|
||||
self.emit_optional_guard(callee_reg, label, ctx)?;
|
||||
}
|
||||
}
|
||||
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 op = match &call.callee {
|
||||
Expression::Identifier(ident) if ctx.is_builtin(ident.name.as_str()) => OpCode::CALL_NATIVE,
|
||||
_ => OpCode::CALL,
|
||||
};
|
||||
ctx.emit(opcode::encode(op, callee_reg, this_reg, first_arg_reg));
|
||||
ctx.emit(arg_regs.len() as u32);
|
||||
let result_reg = ctx.alloc_reg();
|
||||
ctx.emit(opcode::encode(OpCode::LOAD_VAR, result_reg, 0, 0));
|
||||
Ok(result_reg)
|
||||
}
|
||||
|
||||
fn emit_chainable_expression(
|
||||
&self, expr: &Expression, short_label: Option<Label>, ctx: &mut CompileCtx,
|
||||
) -> Result<u8, String> {
|
||||
match expr {
|
||||
Expression::StaticMemberExpression(member) => {
|
||||
let (value_reg, _) = self.emit_static_member_get_preserve_base(member, short_label, ctx)?;
|
||||
Ok(value_reg)
|
||||
}
|
||||
Expression::ComputedMemberExpression(member) => {
|
||||
let (value_reg, _) = self.emit_computed_member_get_preserve_base(member, short_label, ctx)?;
|
||||
Ok(value_reg)
|
||||
}
|
||||
Expression::PrivateFieldExpression(member) => {
|
||||
let obj_reg = self.emit_chainable_expression(&member.object, short_label, ctx)?;
|
||||
if member.optional {
|
||||
if let Some(label) = short_label {
|
||||
self.emit_optional_guard(obj_reg, label, ctx)?;
|
||||
}
|
||||
}
|
||||
let key_reg = self.emit_private_id_reg(member.field.name.as_str(), ctx)?;
|
||||
let value_reg = ctx.alloc_reg();
|
||||
ctx.emit(opcode::encode(OpCode::GET_PRIVATE, value_reg, obj_reg, key_reg));
|
||||
Ok(value_reg)
|
||||
}
|
||||
Expression::CallExpression(call) => self.emit_chain_call(call, short_label, ctx),
|
||||
Expression::ChainExpression(chain) => self.emit_chain_element(&chain.expression, short_label, ctx),
|
||||
_ => self.emit_expression(expr, ctx),
|
||||
}
|
||||
}
|
||||
|
||||
pub(crate) fn emit_chain_element(
|
||||
&self, element: &ChainElement, short_label: Option<Label>, ctx: &mut CompileCtx,
|
||||
) -> Result<u8, String> {
|
||||
match element {
|
||||
ChainElement::StaticMemberExpression(member) => {
|
||||
let (value_reg, _) = self.emit_static_member_get_preserve_base(member, short_label, ctx)?;
|
||||
Ok(value_reg)
|
||||
}
|
||||
ChainElement::ComputedMemberExpression(member) => {
|
||||
let (value_reg, _) = self.emit_computed_member_get_preserve_base(member, short_label, ctx)?;
|
||||
Ok(value_reg)
|
||||
}
|
||||
ChainElement::PrivateFieldExpression(member) => {
|
||||
let obj_reg = self.emit_chainable_expression(&member.object, short_label, ctx)?;
|
||||
if member.optional {
|
||||
if let Some(label) = short_label {
|
||||
self.emit_optional_guard(obj_reg, label, ctx)?;
|
||||
}
|
||||
}
|
||||
let key_reg = self.emit_private_id_reg(member.field.name.as_str(), ctx)?;
|
||||
let value_reg = ctx.alloc_reg();
|
||||
ctx.emit(opcode::encode(OpCode::GET_PRIVATE, value_reg, obj_reg, key_reg));
|
||||
Ok(value_reg)
|
||||
}
|
||||
ChainElement::CallExpression(call) => self.emit_chain_call(call, short_label, ctx),
|
||||
ChainElement::TSNonNullExpression(_) => Err("TS non-null expressions are not supported in JS mode".into()),
|
||||
}
|
||||
}
|
||||
|
||||
pub(crate) fn emit_logical_assign_test(
|
||||
&self, op: LogicalOperator, test_reg: u8, store_label: Label, end_label: Label, ctx: &mut CompileCtx,
|
||||
) -> Result<(), String> {
|
||||
match op {
|
||||
LogicalOperator::And => {
|
||||
let end_pos = ctx.resolve_label(end_label)?;
|
||||
let offset = (end_pos as isize) - (ctx.bytecode.len() as isize);
|
||||
let offset = ctx.checked_jump_offset(offset);
|
||||
ctx.emit(opcode::encode_jmp_if_false(test_reg, offset));
|
||||
}
|
||||
LogicalOperator::Or => {
|
||||
let end_pos = ctx.resolve_label(end_label)?;
|
||||
let offset = (end_pos as isize) - (ctx.bytecode.len() as isize);
|
||||
let offset = ctx.checked_jump_offset(offset);
|
||||
ctx.emit(opcode::encode_jmp_if_true(test_reg, offset));
|
||||
}
|
||||
LogicalOperator::Coalesce => {
|
||||
let store_pos = ctx.resolve_label(store_label)?;
|
||||
let offset = (store_pos as isize) - (ctx.bytecode.len() as isize);
|
||||
let offset = ctx.checked_jump_offset(offset);
|
||||
ctx.emit(opcode::encode_jmp_if_nullish(test_reg, offset));
|
||||
let end_pos = ctx.resolve_label(end_label)?;
|
||||
let offset = (end_pos as isize) - (ctx.bytecode.len() as isize);
|
||||
let offset = ctx.checked_jump_offset(offset);
|
||||
ctx.emit(opcode::encode_jmp(offset));
|
||||
}
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
||||
pub(crate) 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_or_builtin(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()),
|
||||
}
|
||||
}
|
||||
|
||||
pub(crate) 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))
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,73 @@
|
||||
use crate::compiler::{CompileCtx, Compiler};
|
||||
use oxide_bytecode::opcode::{self, OpCode};
|
||||
use oxide_parser::{Expression, PropertyKey};
|
||||
|
||||
impl Compiler {
|
||||
pub(crate) fn emit_public_field_init(
|
||||
&self, target_reg: u8, key: &PropertyKey, computed: bool, value: Option<&Expression>, ctx: &mut CompileCtx,
|
||||
) -> Result<(), String> {
|
||||
let key_reg = self.emit_class_key_reg(key, computed, ctx)?;
|
||||
let value_reg = if let Some(expr) = value {
|
||||
self.emit_expression(expr, ctx)?
|
||||
} else {
|
||||
self.emit_undefined(ctx)
|
||||
};
|
||||
if computed {
|
||||
ctx.emit(opcode::encode(OpCode::SET_PROP_DYNAMIC, target_reg, key_reg, value_reg));
|
||||
} else {
|
||||
ctx.emit(opcode::encode(OpCode::SET_PROP, target_reg, value_reg, key_reg));
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
||||
pub(crate) fn emit_private_field_init(
|
||||
&self, target_reg: u8, name: &str, value: Option<&Expression>, ctx: &mut CompileCtx,
|
||||
) -> Result<(), String> {
|
||||
let key_reg = self.emit_private_id_reg(name, ctx)?;
|
||||
let value_reg = if let Some(expr) = value {
|
||||
self.emit_expression(expr, ctx)?
|
||||
} else {
|
||||
self.emit_undefined(ctx)
|
||||
};
|
||||
ctx.emit(opcode::encode(OpCode::INIT_PRIVATE, target_reg, value_reg, key_reg));
|
||||
Ok(())
|
||||
}
|
||||
|
||||
pub(crate) fn emit_private_method_init(
|
||||
&self, target_reg: u8, method: &oxide_parser::MethodDefinition, home_reg: u8, ctx: &mut CompileCtx,
|
||||
) -> Result<(), String> {
|
||||
let PropertyKey::PrivateIdentifier(private) = &method.key else {
|
||||
return Err("expected private method key".into());
|
||||
};
|
||||
let name = private.name.as_str();
|
||||
let key_reg = self.emit_private_id_reg(name, ctx)?;
|
||||
let method_reg = self.emit_class_method_function(method, name, home_reg, ctx, &[])?;
|
||||
ctx.emit(opcode::encode(OpCode::INIT_PRIVATE, target_reg, method_reg, key_reg));
|
||||
Ok(())
|
||||
}
|
||||
|
||||
pub(crate) fn count_public_field_init(
|
||||
&self, key: &PropertyKey, computed: bool, value: Option<&Expression>, ctx: &mut CompileCtx,
|
||||
) {
|
||||
self.count_class_key(key, computed, ctx);
|
||||
if let Some(expr) = value {
|
||||
self.count_expression(expr, ctx);
|
||||
} else {
|
||||
ctx.alloc_reg();
|
||||
ctx.projected_pc += 1;
|
||||
}
|
||||
ctx.projected_pc += 1;
|
||||
}
|
||||
|
||||
pub(crate) fn count_private_field_init(&self, value: Option<&Expression>, ctx: &mut CompileCtx) {
|
||||
ctx.alloc_reg();
|
||||
ctx.projected_pc += 1;
|
||||
if let Some(expr) = value {
|
||||
self.count_expression(expr, ctx);
|
||||
} else {
|
||||
ctx.alloc_reg();
|
||||
ctx.projected_pc += 1;
|
||||
}
|
||||
ctx.projected_pc += 1;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,75 @@
|
||||
use crate::compiler::{CompileCtx, Compiler};
|
||||
use oxide_bytecode::module::Constant;
|
||||
use oxide_parser::PropertyKey;
|
||||
|
||||
impl Compiler {
|
||||
fn private_name_id(&self, name: &str, ctx: &CompileCtx) -> Result<u32, String> {
|
||||
ctx.scopes
|
||||
.private_name_map
|
||||
.iter()
|
||||
.find_map(|(n, id)| (n == name).then_some(*id))
|
||||
.ok_or_else(|| format!("private name #{name} is not defined"))
|
||||
}
|
||||
|
||||
pub(crate) fn emit_private_id_reg(&self, name: &str, ctx: &mut CompileCtx) -> Result<u8, String> {
|
||||
let id = self.private_name_id(name, ctx)?;
|
||||
let idx = ctx.add_constant(Constant::Int(id as i32));
|
||||
let reg = ctx.alloc_reg();
|
||||
ctx.emit_load_const(reg, idx);
|
||||
Ok(reg)
|
||||
}
|
||||
|
||||
pub(crate) fn emit_class_key_reg(
|
||||
&self, key: &PropertyKey, computed: bool, ctx: &mut CompileCtx,
|
||||
) -> Result<u8, String> {
|
||||
if !computed {
|
||||
let name = self.class_property_name(key)?;
|
||||
let idx = ctx.add_constant(Constant::String(name));
|
||||
let reg = ctx.alloc_reg();
|
||||
ctx.emit_load_const(reg, idx);
|
||||
return Ok(reg);
|
||||
}
|
||||
|
||||
if matches!(key, PropertyKey::PrivateIdentifier(_)) {
|
||||
return Err("private class elements not yet supported".into());
|
||||
}
|
||||
self.emit_expression(key.to_expression(), ctx)
|
||||
}
|
||||
|
||||
pub(crate) fn count_class_key(&self, key: &PropertyKey, computed: bool, ctx: &mut CompileCtx) {
|
||||
if computed {
|
||||
self.count_expression(key.to_expression(), ctx);
|
||||
} else {
|
||||
ctx.alloc_reg();
|
||||
ctx.projected_pc += 1;
|
||||
}
|
||||
}
|
||||
|
||||
pub(crate) 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()),
|
||||
}
|
||||
}
|
||||
|
||||
pub(crate) fn class_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()),
|
||||
PropertyKey::PrivateIdentifier(_) => Err("private class elements not yet supported".into()),
|
||||
_ => Err("unsupported class property key type".into()),
|
||||
}
|
||||
}
|
||||
|
||||
pub(crate) fn emit_undefined(&self, ctx: &mut CompileCtx) -> u8 {
|
||||
let idx = ctx.add_constant(Constant::Undefined);
|
||||
let reg = ctx.alloc_reg();
|
||||
ctx.emit_load_const(reg, idx);
|
||||
reg
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,4 @@
|
||||
pub mod binding;
|
||||
pub mod chain;
|
||||
pub mod class_field;
|
||||
pub mod class_key;
|
||||
Reference in New Issue
Block a user