public static int MakeArray(IntPtr ptr) { try { Type t; CheckType(ptr, 1, out t); LuaNativeMethods.luaL_checktype(ptr, 2, LuaTypes.TYPE_TABLE); int n = LuaNativeMethods.lua_rawlen(ptr, 2); Array array = Array.CreateInstance(t, n); for (int k = 0; k < n; k++) { LuaNativeMethods.lua_rawgeti(ptr, 2, k + 1); object obj = LuaObject.CheckVar(ptr, -1); array.SetValue(LuaObject.ChangeType(obj, t), k); LuaNativeMethods.lua_pop(ptr, 1); } LuaObject.PushValue(ptr, true); LuaObject.PushValue(ptr, array); return(2); } catch (Exception e) { return(Error(ptr, e)); } }
public static int CreateClass(IntPtr ptr) { try { string cls; CheckType(ptr, 1, out cls); Type t = LuaObject.FindType(cls); if (t == null) { return(Error(ptr, string.Format("Can't find {0} to create", cls))); } ConstructorInfo[] cis = t.GetConstructors(); ConstructorInfo target = null; for (int n = 0; n < cis.Length; n++) { ConstructorInfo ci = cis[n]; if (LuaObject.MatchType(ptr, LuaNativeMethods.lua_gettop(ptr), 2, ci.GetParameters())) { target = ci; break; } } if (target != null) { ParameterInfo[] pis = target.GetParameters(); object[] args = new object[pis.Length]; for (int n = 0; n < pis.Length; n++) { args[n] = LuaObject.ChangeType(LuaObject.CheckVar(ptr, n + 2), pis[n].ParameterType); } object ret = target.Invoke(args); LuaObject.PushValue(ptr, true); LuaObject.PushVar(ptr, ret); return(2); } LuaObject.PushValue(ptr, true); return(1); } catch (Exception e) { return(Error(ptr, e)); } }
public static int LuaNewIndex(IntPtr ptr) { try { Array a = (Array)CheckSelf(ptr); int i; CheckType(ptr, 2, out i); LuaObject.Assert(i > 0, "index base 1"); object o = CheckVar(ptr, 3); Type et = a.GetType().GetElementType(); a.SetValue(LuaObject.ChangeType(o, et), i - 1); return(LuaObject.Ok(ptr)); } catch (Exception e) { return(Error(ptr, e)); } }