impl Hash for Value

main
_ 2023-09-26 14:38:38 -05:00
parent 8d80ebc052
commit 693efa47bd
2 changed files with 23 additions and 3 deletions

View File

@ -18,14 +18,14 @@ pub (crate) fn compile_bytecode (source: Vec <u8>) -> Vec <u8> {
},
};
let mut child = Command::new ("luac")
let mut child = Command::new ("luac5.4")
.arg ("-o") // Output to...
.arg ("-") // Standard output
.arg ("-") // Input from standard input
.stdin (Stdio::piped ())
.stdout (Stdio::piped ())
.spawn ()
.expect ("failed to execute `luac`. Is Lua installed?");
.expect ("failed to execute `luac5.4`. Is Lua installed?");
let mut stdin = child.stdin.take ().expect ("failed to get stdin");
std::thread::spawn (move || {

View File

@ -65,7 +65,7 @@ pub enum Instruction {
VarArgPrep (i32),
}
#[derive (Clone, Debug, PartialEq)]
#[derive (Clone, Debug, Hash, PartialEq)]
pub struct BogusClosure {
idx: usize,
upvalues: Vec <Value>,
@ -118,6 +118,26 @@ impl From <f64> for Value {
}
}
impl std::hash::Hash for Value {
fn hash <H: std::hash::Hasher> (&self, state: &mut H) {
// Per https://doc.rust-lang.org/std/hash/trait.Hash.html#prefix-collisions
[0xff].hash (state);
match self {
// TODO: Weaken to a Lua error
Self::Nil => panic! ("can't hash a nil value"),
Self::Boolean (x) => x.hash (state),
Self::Float (x) => x.to_ne_bytes ().hash (state),
Self::Integer (x) => x.hash (state),
Self::String (x) => x.as_ptr ().hash (state),
Self::BogusArg (_) => panic! ("can't hash Bogus values"),
Self::BogusClosure (_) => panic! ("can't hash Bogus values"),
Self::BogusEnv (_) => panic! ("can't hash Bogus values"),
Self::BogusPrint => panic! ("can't hash Bogus values"),
}
}
}
impl Value {
fn as_float (&self) -> Option <f64> {
match self {