Track const in stack frames

This commit is contained in:
Andrew Morris
2023-04-04 09:51:49 +10:00
parent e54abc981f
commit 9084677dd4
8 changed files with 31 additions and 15 deletions

View File

@@ -39,8 +39,9 @@ impl ArrayMappingFrame {
}
impl StackFrameTrait for ArrayMappingFrame {
fn write_this(&mut self, this: Val) {
fn write_this(&mut self, _const: bool, this: Val) -> Result<(), Val> {
self.this = this.as_array_data();
Ok(())
}
fn write_param(&mut self, param: Val) {
@@ -103,7 +104,7 @@ impl StackFrameTrait for ArrayMappingFrame {
}
}
LoadFunctionResult::StackFrame(mut new_frame) => {
new_frame.write_this(self.this_arg.clone());
new_frame.write_this(true, self.this_arg.clone())?;
new_frame.write_param(el.clone());
new_frame.write_param(Val::Number(array_i as f64));
new_frame.write_param(Val::Array(array_data.clone()));

View File

@@ -29,8 +29,9 @@ struct ReduceFrame {
}
impl StackFrameTrait for ReduceFrame {
fn write_this(&mut self, this: Val) {
fn write_this(&mut self, _const: bool, this: Val) -> Result<(), Val> {
self.this = this.as_array_data();
Ok(())
}
fn write_param(&mut self, param: Val) {

View File

@@ -29,13 +29,15 @@ struct ReduceRightFrame {
}
impl StackFrameTrait for ReduceRightFrame {
fn write_this(&mut self, this: Val) {
fn write_this(&mut self, _const: bool, this: Val) -> Result<(), Val> {
self.this = this.as_array_data();
match &self.this {
None => {}
Some(ad) => self.array_i = ad.elements.len(),
};
Ok(())
}
fn write_param(&mut self, param: Val) {

View File

@@ -206,8 +206,13 @@ enum SortTreeNodeData {
}
impl StackFrameTrait for SortFrame {
fn write_this(&mut self, this: Val) {
fn write_this(&mut self, const_: bool, this: Val) -> Result<(), Val> {
if const_ {
return type_error!("Cannot sort const array");
}
self.this = this.as_array_data();
Ok(())
}
fn write_param(&mut self, param: Val) {

View File

@@ -17,6 +17,7 @@ use crate::vs_value::{LoadFunctionResult, Val, ValTrait};
pub struct BytecodeStackFrame {
pub decoder: BytecodeDecoder,
pub registers: Vec<Val>,
pub const_this: bool,
pub param_start: usize,
pub param_end: usize,
pub this_target: Option<usize>,
@@ -91,8 +92,10 @@ impl BytecodeStackFrame {
}
impl StackFrameTrait for BytecodeStackFrame {
fn write_this(&mut self, this: Val) {
fn write_this(&mut self, _const: bool, this: Val) -> Result<(), Val> {
self.registers[1] = this;
self.const_this = _const;
Ok(())
}
fn write_param(&mut self, param: Val) {
@@ -217,11 +220,11 @@ impl StackFrameTrait for BytecodeStackFrame {
self.this_target = this_target;
if this_target.is_some() {
new_frame.write_this(self.registers[this_target.unwrap()].clone());
new_frame.write_this(false, self.registers[this_target.unwrap()].clone())?;
}
} else {
self.this_target = None;
new_frame.write_this(self.decoder.decode_val(&self.registers));
new_frame.write_this(false, self.decoder.decode_val(&self.registers))?;
}
self.transfer_parameters(&mut new_frame);
@@ -302,10 +305,13 @@ impl StackFrameTrait for BytecodeStackFrame {
LoadFunctionResult::StackFrame(mut new_frame) => {
self.transfer_parameters(&mut new_frame);
new_frame.write_this(match &obj {
ThisArg::Register(reg_i) => self.registers[reg_i.clone()].clone(),
ThisArg::Val(val) => val.clone(),
});
new_frame.write_this(
false,
match &obj {
ThisArg::Register(reg_i) => self.registers[reg_i.clone()].clone(),
ThisArg::Val(val) => val.clone(),
},
)?;
self.return_target = self.decoder.decode_register_index();
@@ -386,7 +392,7 @@ impl StackFrameTrait for BytecodeStackFrame {
}
LoadFunctionResult::StackFrame(mut new_frame) => {
self.transfer_parameters(&mut new_frame);
new_frame.write_this(instance);
new_frame.write_this(false, instance)?;
self.return_target = None;
self.this_target = self.decoder.decode_register_index();

View File

@@ -17,7 +17,7 @@ impl FirstStackFrame {
}
impl StackFrameTrait for FirstStackFrame {
fn write_this(&mut self, _this: Val) {
fn write_this(&mut self, _const: bool, _this: Val) -> Result<(), Val> {
panic!("Not appropriate for FirstStackFrame");
}

View File

@@ -17,7 +17,7 @@ pub enum FrameStepOk {
pub type FrameStepResult = Result<FrameStepOk, Val>;
pub trait StackFrameTrait {
fn write_this(&mut self, this: Val);
fn write_this(&mut self, const_: bool, this: Val) -> Result<(), Val>;
fn write_param(&mut self, param: Val);
fn step(&mut self) -> FrameStepResult;
fn apply_call_result(&mut self, call_result: CallResult);

View File

@@ -51,6 +51,7 @@ impl VsFunction {
pos: self.start,
},
registers,
const_this: true,
param_start: self.binds.len() + 2,
param_end: self.parameter_count + 2,
this_target: None,