Skip to content

Commit 89af7b0

Browse files
committed
LuaTables can now be indexed by LuaVals.
1 parent 1d3351b commit 89af7b0

File tree

3 files changed

+112
-72
lines changed

3 files changed

+112
-72
lines changed

luavm/src/lib/lua_values/lua_obj.rs

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,10 @@ pub trait LuaObj {
66
fn clone_box(&self) -> Box<LuaObj>;
77
/// Checks whther the underlying type is a float or an int.
88
fn is_number(&self) -> bool;
9-
/// Checks whether the underlying type is converted to a float when processed in
10-
/// arithmetic expressions.
11-
fn is_float(&self) -> bool;
9+
/// Returns true if the underlying type is either a float or a string.
10+
/// In Lua, if either of these two types are used in an arithmetic
11+
/// expression, then both arguments are converted to floats.
12+
fn is_aop_float(&self) -> bool;
1213
/// Checks whether the underlying type is a string or not.
1314
fn is_string(&self) -> bool;
1415
/// Converts the underlying type to an int.
@@ -17,6 +18,10 @@ pub trait LuaObj {
1718
fn to_float(&self) -> Result<f64, LuaError>;
1819
/// Converts the underlying type to a string.
1920
fn to_string(&self) -> Result<String, LuaError>;
21+
/// Gets a reference to the underlying string.
22+
fn get_string_ref(&self) -> Option<&str> {
23+
None
24+
}
2025
}
2126

2227
/// Boxes the given `LuaObj`, and returns the address of the box.
@@ -43,7 +48,7 @@ impl LuaObj for LuaInt {
4348
true
4449
}
4550

46-
fn is_float(&self) -> bool {
51+
fn is_aop_float(&self) -> bool {
4752
false
4853
}
4954

@@ -73,7 +78,7 @@ impl LuaObj for LuaFloat {
7378
true
7479
}
7580

76-
fn is_float(&self) -> bool {
81+
fn is_aop_float(&self) -> bool {
7782
true
7883
}
7984

@@ -107,7 +112,7 @@ impl LuaObj for LuaString {
107112
false
108113
}
109114

110-
fn is_float(&self) -> bool {
115+
fn is_aop_float(&self) -> bool {
111116
true
112117
}
113118

@@ -126,4 +131,8 @@ impl LuaObj for LuaString {
126131
fn to_string(&self) -> Result<String, LuaError> {
127132
Ok(self.v.clone())
128133
}
134+
135+
fn get_string_ref(&self) -> Option<&str> {
136+
Some(&self.v)
137+
}
129138
}

luavm/src/lib/lua_values/lua_table.rs

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -5,26 +5,22 @@ use LuaVal;
55
/// Represents a table in Lua.
66
#[derive(Trace, Finalize)]
77
pub struct LuaTable {
8-
v: GcCell<HashMap<String, LuaVal>>,
8+
v: GcCell<HashMap<LuaVal, LuaVal>>,
99
}
1010

1111
impl LuaTable {
1212
/// Creates a table with the given keys, and values.
13-
pub fn new(hm: HashMap<String, LuaVal>) -> LuaTable {
13+
pub fn new(hm: HashMap<LuaVal, LuaVal>) -> LuaTable {
1414
LuaTable { v: GcCell::new(hm) }
1515
}
1616

1717
/// Sets the given attribute to `val`.
18-
pub fn set(&self, attr: &str, val: LuaVal) {
19-
self.v
20-
.borrow_mut()
21-
.entry(attr.to_string())
22-
.or_insert(LuaVal::new())
23-
.set(val);
18+
pub fn set_attr(&self, attr: LuaVal, val: LuaVal) {
19+
self.v.borrow_mut().insert(attr, val);
2420
}
2521

2622
/// Gets a reference to given attribute.
27-
pub fn get_attr(&self, attr: &str) -> LuaVal {
23+
pub fn get_attr(&self, attr: &LuaVal) -> LuaVal {
2824
match self.v.borrow().get(attr) {
2925
Some(val) => val.clone(),
3026
None => LuaVal::new(),

0 commit comments

Comments
 (0)