diff --git a/src/state.rs b/src/state.rs index 30b957d..3592004 100644 --- a/src/state.rs +++ b/src/state.rs @@ -457,7 +457,7 @@ impl <'a> State <'a> { _ => panic! ("K[C] must be a string"), }; - let val = t.borrow ().get (Value::String (Rc::clone (key))); + let val = t.borrow ().get_str (key.as_str ()).clone (); *self.reg_mut (*a) = val; }, @@ -733,13 +733,13 @@ impl <'a> State <'a> { let key = match k.get (b).unwrap () { Value::String (s) => s.as_ref (), - _ => panic! ("GetTabUp only supports string keys"), + _ => panic! ("SetField only supports string keys"), }; let mut dst = self.reg (*a).as_table () .expect ("SetField only works on tables").borrow_mut (); - dst.insert (Value::from (key.as_str ()), value); + dst.insert_str (key.as_str (), value); }, Instruction::SetI (a, b, c, k_flag) => { let value = if *k_flag { diff --git a/src/value.rs b/src/value.rs index b4495d2..b1f514c 100644 --- a/src/value.rs +++ b/src/value.rs @@ -36,6 +36,8 @@ pub enum Value { BogusClosure (Rc >), } +const NIL: Value = Value::Nil; + impl fmt::Debug for Value { fn fmt (&self, f: &mut fmt::Formatter) -> fmt::Result { match self { @@ -254,7 +256,14 @@ impl fmt::Debug for Table { impl Table { fn get_inner (&self, key: &Value) -> Value { - self.hash.get (key).cloned ().unwrap_or_default () + // self.hash.get (key).cloned ().unwrap_or_default () + for (hay, value) in &self.hash { + if key == hay { + return value.clone (); + } + } + + Value::Nil } pub fn get > (&self, key: A) -> Value { @@ -265,7 +274,24 @@ impl Table { self.get_inner (&(key.into ())) } + pub fn get_str (&self, key: &str) -> &Value { + for (hay, value) in &self.hash { + if Some (key) == hay.as_str () { + return value; + } + } + + &NIL + } + fn insert_inner (&mut self, a: Value, b: Value) { + for (hay, value) in &mut self.hash { + if &a == hay { + *value = b; + return; + } + } + self.hash.insert (a, b); } @@ -286,6 +312,17 @@ impl Table { self.insert_inner (k.into (), v.into ()) } + pub fn insert_str (&mut self, key: &str, v: Value) { + for (hay, value) in &mut self.hash { + if Some (key) == hay.as_str () { + *value = v; + return; + } + } + + self.hash.insert (key.into (), v); + } + pub fn length (&self) -> i64 { for i in 1..i64::MAX { if self.get (i) == Value::Nil {