From 693efa47bd6524323e0faf4b04fa55324860e241 Mon Sep 17 00:00:00 2001 From: _ <_@_> Date: Tue, 26 Sep 2023 14:38:38 -0500 Subject: [PATCH] impl Hash for Value --- src/loader.rs | 4 ++-- src/state.rs | 22 +++++++++++++++++++++- 2 files changed, 23 insertions(+), 3 deletions(-) diff --git a/src/loader.rs b/src/loader.rs index 55d0cc0..f8e39c8 100644 --- a/src/loader.rs +++ b/src/loader.rs @@ -18,14 +18,14 @@ pub (crate) fn compile_bytecode (source: Vec ) -> Vec { }, }; - 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 || { diff --git a/src/state.rs b/src/state.rs index 5744f94..92c7008 100644 --- a/src/state.rs +++ b/src/state.rs @@ -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 , @@ -118,6 +118,26 @@ impl From for Value { } } +impl std::hash::Hash for Value { + fn hash (&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 { match self {