Put source_hash in function hash

This commit is contained in:
Andrew Morris
2023-08-14 10:59:32 +10:00
parent 4c6b249c10
commit 31a00cba19
14 changed files with 135 additions and 35 deletions

View File

@@ -10,7 +10,6 @@ use crate::builtins::BUILTIN_VALS;
use crate::bytecode::Bytecode;
use crate::vs_class::VsClass;
use crate::vs_function::VsFunction;
use crate::vs_function_metadata::VsFunctionMetadata;
use crate::vs_object::VsObject;
use crate::vs_symbol::VsSymbol;
use crate::vs_value::ToVal;
@@ -134,7 +133,7 @@ impl BytecodeDecoder {
}
.to_val()
}
BytecodeType::Function => self.decode_function(false),
BytecodeType::Function => self.decode_function(false, registers),
BytecodeType::Pointer => self.decode_pointer(registers),
BytecodeType::Register => match registers[self.decode_register_index().unwrap()].clone() {
Val::Void => Val::Undefined,
@@ -152,7 +151,7 @@ impl BytecodeDecoder {
}
.to_val(),
BytecodeType::BigInt => self.decode_bigint().to_val(),
BytecodeType::GeneratorFunction => self.decode_function(true),
BytecodeType::GeneratorFunction => self.decode_function(true, registers),
BytecodeType::Unrecognized => panic!("Unrecognized bytecode type at {}", self.pos - 1),
}
}
@@ -281,11 +280,8 @@ impl BytecodeDecoder {
}
}
pub fn decode_function(&mut self, is_generator: bool) -> Val {
// TODO: Use actual content hash
let mut hash = [0u8; 32];
hash[0] = (self.pos & 0xff) as u8;
hash[1] = ((self.pos >> 8) & 0xff) as u8;
pub fn decode_function(&mut self, is_generator: bool, registers: &mut Vec<Val>) -> Val {
let metadata = self.decode_val(registers);
// TODO: Support >256
let register_count = self.decode_byte() as usize;
@@ -293,10 +289,7 @@ impl BytecodeDecoder {
VsFunction {
bytecode: self.bytecode.clone(),
metadata: VsFunctionMetadata {
name: Rc::from(""),
hash,
},
metadata,
is_generator,
register_count,
parameter_count,

View File

@@ -23,7 +23,6 @@ mod virtual_machine;
pub mod vs_array;
pub mod vs_class;
mod vs_function;
mod vs_function_metadata;
pub mod vs_object;
mod vs_symbol;
pub mod vs_value;

View File

@@ -194,7 +194,10 @@ pub fn op_eq_impl(left: &Val, right: &Val) -> Result<bool, Val> {
}
}
left.metadata.hash == right.metadata.hash
op_triple_eq_impl(
&left.metadata.sub(&"hash".to_val())?,
&right.metadata.sub(&"hash".to_val())?,
)?
}
_ => {
if left.is_truthy() != right.is_truthy() {
@@ -316,7 +319,10 @@ pub fn op_triple_eq_impl(left: &Val, right: &Val) -> Result<bool, Val> {
}
}
left.metadata.hash == right.metadata.hash
op_triple_eq_impl(
&left.metadata.sub(&"hash".to_val())?,
&right.metadata.sub(&"hash".to_val())?,
)?
}
#[allow(clippy::vtable_address_comparisons)] // TODO: Is this ok?
(Val::Static(left), Val::Static(right)) => std::ptr::eq(&**left, &**right),

View File

@@ -2,7 +2,6 @@ use std::rc::Rc;
use crate::bytecode::Bytecode;
use crate::make_generator_frame::MakeGeneratorFrame;
use crate::vs_function_metadata::VsFunctionMetadata;
use crate::vs_value::ToVal;
use super::bytecode_decoder::BytecodeDecoder;
@@ -13,7 +12,7 @@ use super::vs_value::Val;
#[derive(Debug, Clone)]
pub struct VsFunction {
pub bytecode: Rc<Bytecode>,
pub metadata: VsFunctionMetadata,
pub metadata: Val,
pub is_generator: bool,
pub register_count: usize,
pub parameter_count: usize,

View File

@@ -1,7 +0,0 @@
use std::rc::Rc;
#[derive(Clone, Debug)]
pub struct VsFunctionMetadata {
pub name: Rc<str>,
pub hash: [u8; 32],
}