This commit is contained in:
Andrew Morris
2023-03-20 17:59:07 +11:00
parent 44de4a88f4
commit d2c437a9b6
28 changed files with 597 additions and 196 deletions

View File

@@ -1,6 +1,9 @@
use std::collections::BTreeMap;
use std::rc::Rc;
use num_bigint::BigInt;
use num_bigint::Sign;
use crate::builtins::BUILTIN_VALS;
use super::instruction::Instruction;
@@ -37,6 +40,7 @@ pub enum BytecodeType {
Register = 0x0e,
Builtin = 0x10,
Class = 0x11,
BigInt = 0x13,
Unrecognized = 0xff,
}
@@ -62,6 +66,8 @@ impl BytecodeType {
0x10 => Builtin,
0x11 => Class,
0x13 => BigInt,
_ => Unrecognized,
};
}
@@ -133,6 +139,7 @@ impl BytecodeDecoder {
constructor: self.decode_val(registers),
instance_prototype: self.decode_val(registers),
})),
BytecodeType::BigInt => Val::BigInt(self.decode_bigint()),
BytecodeType::Unrecognized => std::panic!("Unrecognized bytecode type at {}", self.pos - 1),
};
}
@@ -151,6 +158,22 @@ impl BytecodeDecoder {
return f64::from_le_bytes(buf);
}
pub fn decode_bigint(&mut self) -> BigInt {
let sign = match self.decode_byte() {
0 => Sign::Minus,
1 => Sign::NoSign,
2 => Sign::Plus,
_ => std::panic!("Invalid sign for bigint"),
};
let len = self.decode_varsize_uint();
let bytes = &self.data[self.pos..self.pos + len];
self.pos += len;
return BigInt::from_bytes_le(sign, bytes);
}
pub fn decode_string(&mut self) -> String {
let len = self.decode_varsize_uint();
let start = self.pos; // Start after decoding varsize