Skip to content

Instantly share code, notes, and snippets.

@ibilon
Created June 7, 2017 06:46
Show Gist options
  • Save ibilon/dcdedad85ab249560bab281564997bd1 to your computer and use it in GitHub Desktop.
Save ibilon/dcdedad85ab249560bab281564997bd1 to your computer and use it in GitHub Desktop.
Wrapper around lua.Table with from function for array and map
package luautils;
import lua.Lua;
import lua.Table in LuaTable;
abstract Table<K, V> (LuaTable<K, V>) from LuaTable<K, V>
{
function new (table:LuaTable<K, V>) this = table;
@:from static function fromMap<K, V> (map:Map<K, V>) : Table<K, V>
{
var table = LuaTable.create({});
for (k in map.keys())
{
Lua.rawset(table, k, map[k]);
}
return new Table(table);
}
@:from static function fromArray<T> (arr:Array<T>) : Table<Int, T>
{
var table = LuaTable.create({});
for (i in 0...arr.length)
{
LuaTable.insert(table, i, arr[i]);
}
return new Table(table);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment