More deduping of builtins, fix todo

This commit is contained in:
Andrew Morris
2023-03-20 09:20:51 +11:00
parent 8dfb174cb2
commit 358c816fe3
6 changed files with 36 additions and 22 deletions

View File

@@ -6,3 +6,4 @@ edition = "2021"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies]
valuescript_common = { path = "../valuescript_common" }

View File

@@ -1,3 +1,5 @@
use valuescript_common::BUILTIN_COUNT;
use crate::number_builtin::NUMBER_BUILTIN;
use crate::string_builtin::STRING_BUILTIN;
@@ -5,14 +7,5 @@ use super::debug::DEBUG;
use super::math::MATH;
use super::vs_value::ValTrait;
// TODO: Investigate whether a static array can be used for this and why rust
// seems to not like it when I try.
pub fn get_builtin(index: usize) -> &'static dyn ValTrait {
return match index {
0 => &MATH,
1 => &DEBUG,
2 => &STRING_BUILTIN,
3 => &NUMBER_BUILTIN,
_ => std::panic!(""),
};
}
pub static BUILTIN_VALS: [&'static (dyn ValTrait + Sync); BUILTIN_COUNT] =
[&DEBUG, &MATH, &STRING_BUILTIN, &NUMBER_BUILTIN];

View File

@@ -1,7 +1,8 @@
use std::collections::BTreeMap;
use std::rc::Rc;
use super::builtins::get_builtin;
use crate::builtins::BUILTIN_VALS;
use super::instruction::Instruction;
use super::vs_array::VsArray;
use super::vs_class::VsClass;
@@ -127,7 +128,7 @@ impl BytecodeDecoder {
BytecodeType::Function => self.decode_function_header(),
BytecodeType::Pointer => self.decode_pointer(),
BytecodeType::Register => registers[self.decode_register_index().unwrap()].clone(),
BytecodeType::Builtin => Val::Static(get_builtin(self.decode_varsize_uint())),
BytecodeType::Builtin => Val::Static(BUILTIN_VALS[self.decode_varsize_uint()]),
BytecodeType::Class => Val::Class(Rc::new(VsClass {
constructor: self.decode_val(registers),
instance_prototype: self.decode_val(registers),