Remove Vallish, use refs and clones

This commit is contained in:
Andrew Morris
2023-06-21 17:16:03 +10:00
parent 53695d7948
commit b33ef2b5b0
14 changed files with 177 additions and 248 deletions

View File

@@ -13,7 +13,6 @@ use crate::vs_object::VsObject;
use crate::vs_symbol::VsSymbol;
use crate::vs_value::ToVal;
use crate::vs_value::Val;
use crate::Vallish;
#[derive(Clone)]
pub struct BytecodeDecoder {
@@ -97,74 +96,66 @@ impl BytecodeDecoder {
return BytecodeType::from_byte(self.peek_byte());
}
pub fn decode_vallish<'a>(&mut self, registers: &'a Vec<Val>) -> Vallish<'a> {
use Vallish::*;
pub fn decode_val(&mut self, registers: &Vec<Val>) -> Val {
return match self.decode_type() {
BytecodeType::End => panic!("Cannot decode end"),
BytecodeType::Void => Own(Val::Void),
BytecodeType::Undefined => Own(Val::Undefined),
BytecodeType::Null => Own(Val::Null),
BytecodeType::False => Own(Val::Bool(false)),
BytecodeType::True => Own(Val::Bool(true)),
BytecodeType::SignedByte => Own((self.decode_signed_byte() as f64).to_val()),
BytecodeType::Number => Own(self.decode_number().to_val()),
BytecodeType::String => Own(self.decode_string().to_val()),
BytecodeType::Array => Own(self.decode_vec_val(registers).to_val()),
BytecodeType::Void => Val::Void,
BytecodeType::Undefined => Val::Undefined,
BytecodeType::Null => Val::Null,
BytecodeType::False => Val::Bool(false),
BytecodeType::True => Val::Bool(true),
BytecodeType::SignedByte => (self.decode_signed_byte() as f64).to_val(),
BytecodeType::Number => self.decode_number().to_val(),
BytecodeType::String => self.decode_string().to_val(),
BytecodeType::Array => self.decode_vec_val(registers).to_val(),
BytecodeType::Object => {
let mut string_map: BTreeMap<String, Val> = BTreeMap::new();
let mut symbol_map: BTreeMap<VsSymbol, Val> = BTreeMap::new();
while self.peek_type() != BytecodeType::End {
let key = self.decode_vallish(registers);
let key = self.decode_val(registers);
let value = self.decode_val(registers);
match key.get_ref() {
match key {
Val::String(string) => string_map.insert(string.to_string(), value),
Val::Symbol(symbol) => symbol_map.insert(symbol.clone(), value),
Val::Symbol(symbol) => symbol_map.insert(symbol, value),
key => string_map.insert(key.to_string(), value),
};
}
self.decode_type(); // End (TODO: assert)
Own(
VsObject {
string_map,
symbol_map,
prototype: None,
}
.to_val(),
)
}
BytecodeType::Function => Own(self.decode_function(false)),
BytecodeType::Pointer => Own(self.decode_pointer(registers)),
BytecodeType::Register => match registers[self.decode_register_index().unwrap()] {
Val::Void => Own(Val::Undefined),
ref val => Ref(val),
},
BytecodeType::TakeRegister => match registers[self.decode_register_index().unwrap()] {
Val::Void => Own(Val::Undefined),
ref val => Ref(val),
},
BytecodeType::Builtin => Own(BUILTIN_VALS[self.decode_varsize_uint()]()),
BytecodeType::Class => Own(
VsClass {
constructor: self.decode_val(registers),
instance_prototype: self.decode_val(registers),
VsObject {
string_map,
symbol_map,
prototype: None,
}
.to_val(),
),
BytecodeType::BigInt => Own(self.decode_bigint().to_val()),
BytecodeType::GeneratorFunction => Own(self.decode_function(true)),
.to_val()
}
BytecodeType::Function => self.decode_function(false),
BytecodeType::Pointer => self.decode_pointer(registers),
BytecodeType::Register => match registers[self.decode_register_index().unwrap()].clone() {
Val::Void => Val::Undefined,
val => val,
},
BytecodeType::TakeRegister => {
match registers[self.decode_register_index().unwrap()].clone() {
Val::Void => Val::Undefined,
val => val,
}
}
BytecodeType::Builtin => BUILTIN_VALS[self.decode_varsize_uint()](),
BytecodeType::Class => VsClass {
constructor: self.decode_val(registers),
instance_prototype: self.decode_val(registers),
}
.to_val(),
BytecodeType::BigInt => self.decode_bigint().to_val(),
BytecodeType::GeneratorFunction => self.decode_function(true),
BytecodeType::Unrecognized => panic!("Unrecognized bytecode type at {}", self.pos - 1),
};
}
pub fn decode_val(&mut self, registers: &Vec<Val>) -> Val {
return self.decode_vallish(registers).get_own();
}
pub fn decode_vec_val(&mut self, registers: &Vec<Val>) -> Vec<Val> {
let mut vals: Vec<Val> = Vec::new();