public DynamicLuaTable(LuaTable table, Lua state, string path = null) : base() { this.table = table; this.state = state; if (path == null) { path = LuaHelper.GetRandomString(); //tables need a name, so we can access them } this.path = path; }
/// <summary> /// Wraps an object to prepare it for passing to LuaInterface. If no name is specified, a /// random one with the default length is used. /// </summary> public static object WrapObject(object toWrap, Lua state, string name = null) { if (toWrap is DynamicArray) { //Someone passed an DynamicArray diretly back to Lua. //He wanted to pass the value in the array, so we extract it. //When there is more than one value, this method will ignore these extra value. //This could happen in a situation like this: lua.tmp = lua("return a,b");, but //that doesn't make sense. toWrap = (toWrap as dynamic)[0]; } if (toWrap is MulticastDelegate) { //We have to deal with a problem here: RegisterFunction does not really create //a new function, but a userdata with a __call metamethod. This works fine in all //except two cases: When Lua looks for an __index or __newindex metafunction and finds //a table or userdata, Lua tries to redirect the read/write operation to that table/userdata. //In case of our function that is in reality a userdata this fails. So we have to check //for these function and create a very thin wrapper arround this to make Lua see a function instead //the real userdata. This is no problem for the other metamethods, these are called independent //from their type. (If they are not nil ;)) MulticastDelegate function = (toWrap as MulticastDelegate); if (name != null && (name.EndsWith("__index") || name.EndsWith("__newindex"))) { string tmpName = LuaHelper.GetRandomString(); state.RegisterFunction(tmpName, function.Target, function.Method); state.DoString(String.Format("function {0}(...) return {1}(...) end", name, tmpName), "DynamicLua internal operation"); } else { if (name == null) { name = LuaHelper.GetRandomString(); } state.RegisterFunction(name, function.Target, function.Method); } return(null); } else if (toWrap is DynamicLuaTable) { dynamic dlt = toWrap as DynamicLuaTable; return((LuaTable)dlt); } else { return(toWrap); } }