|
@@ -24,55 +24,60 @@ import lua.Lua;
|
|
|
|
|
|
class StringMap<T> implements haxe.Constraints.IMap<String,T> {
|
|
|
|
|
|
- private var k : Dynamic; // Is item exists
|
|
|
- private var v : Dynamic; // Values table
|
|
|
+ private var h : lua.Table<String,T>;
|
|
|
+ static var tnull : Dynamic = lua.Table.create();
|
|
|
|
|
|
public inline function new() : Void {
|
|
|
- // these need to be plain anonymous tables,
|
|
|
- // so that we can set arbitrary string values.
|
|
|
- v = untyped __lua__("{}");
|
|
|
- k = untyped __lua__("{}");
|
|
|
+ h = lua.Table.create();
|
|
|
}
|
|
|
|
|
|
public inline function set( key : String, value : T ) : Void untyped {
|
|
|
- untyped __lua__("{0}[{1}] = {2}", v, key, value);
|
|
|
- untyped __lua__("{0}[{1}] = true", k, key);
|
|
|
+ if (value == null){
|
|
|
+ h[key] = tnull;
|
|
|
+ } else {
|
|
|
+ h[key] = value;
|
|
|
+ }
|
|
|
}
|
|
|
|
|
|
public inline function get( key : String ) : Null<T> untyped {
|
|
|
- return untyped __lua__("{0}[{1}]", v, key);
|
|
|
+ var ret = h[key];
|
|
|
+ if (ret == tnull){
|
|
|
+ return null;
|
|
|
+ } else {
|
|
|
+ return ret;
|
|
|
+ }
|
|
|
}
|
|
|
|
|
|
public inline function exists( key : String ) : Bool untyped {
|
|
|
- return untyped __lua__("({0}[{1}] or false)", k, key);
|
|
|
+ return Lua.rawget(h,key) != null;
|
|
|
}
|
|
|
|
|
|
public function remove( key : String ) : Bool untyped {
|
|
|
- if (untyped __lua__("not {0}[{1}]", k, key)) return false;
|
|
|
- untyped __lua__("{0}[{1}] = nil", v, key);
|
|
|
- untyped __lua__("{0}[{1}] = nil", k, key);
|
|
|
- return true;
|
|
|
+ if (Lua.rawget(h,key) == null){
|
|
|
+ return false;
|
|
|
+ } else {
|
|
|
+ Lua.rawset(h,key,null);
|
|
|
+ return true;
|
|
|
+ }
|
|
|
}
|
|
|
|
|
|
public function keys() : Iterator<String> {
|
|
|
- var cur : Array<String> = [];
|
|
|
- untyped __lua__("for _k,_v in pairs({1}) do
|
|
|
- if(_v)then {0}:push(_k) end
|
|
|
- end", cur, k);
|
|
|
+ var cur = Lua.next(h,null).index;
|
|
|
return {
|
|
|
next : function() {
|
|
|
- var ret = cur.pop();
|
|
|
- return ret;
|
|
|
+ var ret = cur;
|
|
|
+ cur = Lua.next(h,cur).index;
|
|
|
+ return cast ret;
|
|
|
},
|
|
|
- hasNext : function() return cur.length > 0
|
|
|
+ hasNext : function() return cur != null
|
|
|
}
|
|
|
}
|
|
|
|
|
|
public function iterator() : Iterator<T> {
|
|
|
var it = keys();
|
|
|
- return {
|
|
|
+ return untyped {
|
|
|
hasNext : function() return it.hasNext(),
|
|
|
- next : function() return untyped __lua__("{0}[{1}]", v, it.next())
|
|
|
+ next : function() return h[it.next()]
|
|
|
};
|
|
|
}
|
|
|
|