static int MetaNewIndexFunctionInternal(lua_State L) { var isIndexingClassObject = IsIndexingClassObject(L); System.Type typeObject = null; if (isIndexingClassObject) { typeObject = (System.Type)Lua.ObjectAtInternal(L, 1); } object thisObject = null; if (!isIndexingClassObject) { thisObject = Lua.ObjectAtInternal(L, 1); typeObject = thisObject.GetType(); } Lua.Assert(typeObject != null, "Should has a type."); if (Api.lua_isnumber(L, 2)) { if (typeObject != null && typeObject.IsArray) { var array = (System.Array)thisObject; var value = Lua.ValueAtInternal(L, 3); var index = (int)Api.lua_tointeger(L, 2); array.SetValue(Lua.ConvertTo(value, typeObject.GetElementType()), index); } else { Lua.SetValueAtIndexOfObject(L, thisObject, typeObject, new object[] { (int)Api.lua_tointeger(L, 2) }, Lua.ValueAtInternal(L, 3)); } } else if (Api.lua_isstring(L, 2)) { Lua.SetMember(L, thisObject, typeObject, Api.lua_tostring(L, 2), Lua.ValueAtInternal(L, 3), hasPrivatePrivillage: false); } else if (Api.lua_istable(L, 2)) { var host = Lua.CheckHost(L); using (var p = LuaTable.MakeRefTo(host, 2)) { using (var ret = host.testPrivillage.InvokeMultiRet(p)) { var name = (string)ret[1]; Lua.SetMember(L, thisObject, typeObject, name, Lua.ValueAtInternal(L, 3), hasPrivatePrivillage: true); } } } else { Lua.SetValueAtIndexOfObject(L, thisObject, typeObject, new object[] { Lua.ValueAtInternal(L, 2) }, Lua.ValueAtInternal(L, 3)); } return(0); }
public LuaTable GetBehaviourTable() { if (luaBehaviourRef != Api.LUA_NOREF) { Api.lua_rawgeti(L, Api.LUA_REGISTRYINDEX, luaBehaviourRef); var t = LuaTable.MakeRefTo(L, -1); Api.lua_pop(L, 1); return(t); } return(null); }
object InvokeInternal(LuaTable target, int nrets, params object[] args) { var L = CheckValid(); var top = Api.lua_gettop(L); try { Push(); int self = 0; if (target != null) { target.Push(); self = 1; } for (int i = 0; i < args.Length; ++i) { L.PushValue(args[i]); } L.Call(self + args.Length, nrets); if (nrets == 0) { return(null); } else if (nrets == 1) { var ret = L.ValueAt(-1); Api.lua_settop(L, top); return(ret); } else { nrets = Api.lua_gettop(L) - top; LuaTable ret = null; Api.lua_createtable(L, nrets, 0); for (int i = 0; i < nrets; ++i) { Api.lua_pushvalue(L, top + i + 1); Api.lua_seti(L, -2, i + 1); } ret = LuaTable.MakeRefTo(L, -1); Api.lua_settop(L, top); return(ret); } } catch (Exception e) { Api.lua_settop(L, top); throw e; } }
public static void Open(Lua L) { // math lib Api.lua_getglobal(L, "math"); using (var t = LuaTable.MakeRefTo(L, -1)) { var funcs = new FuncReg[] { new FuncReg("clamp01", LuaFunction.NewFunction( L, "function(v) return (v > 1 and 1) or (v < 0 and 0 or v) end")), new FuncReg("lerp", LuaFunction.NewFunction( L, "function(a, b, f) return (a-b)*f + b end")) }; RegisterAndDisposeFuncs(t, funcs); } Api.lua_pop(L, 1); }
static int MetaIndexFunctionInternal(lua_State L) { var isIndexingClassObject = IsIndexingClassObject(L); System.Type typeObject = null; if (isIndexingClassObject) { typeObject = (System.Type)Lua.ObjectAtInternal(L, 1); } object thisObject = null; if (!isIndexingClassObject) { thisObject = Lua.ObjectAtInternal(L, 1); typeObject = thisObject.GetType(); } Lua.Assert(typeObject != null, "Should have a type"); if (Api.lua_isinteger(L, 2)) { if (typeObject != null && typeObject.IsArray) { var array = (System.Array)thisObject; Lua.PushValueInternal(L, array.GetValue((int)Api.lua_tointeger(L, 2))); return(1); } else { return(Lua.IndexObjectInternal(L, thisObject, typeObject, new object[] { (int)Api.lua_tointeger(L, 2) })); } } else if (Api.lua_isstring(L, 2)) { return(Lua.GetMember(L, thisObject, typeObject, Api.lua_tostring(L, 2), false, null, null)); } else if (Api.lua_istable(L, 2)) { var host = Lua.CheckHost(L); using (var p = LuaTable.MakeRefTo(host, 2)) { var isGettingTypeObject = (bool)host.isIndexingTypeObject.Invoke1(p); if (isGettingTypeObject) { Lua.PushObjectInternal(L, typeObject); return(1); } using (var ret = host.testPrivillage.InvokeMultiRet(p)) { var name = (string)ret[1]; var hasPrivatePrivillage = (bool)ret[2]; var retrievingNestedType = (bool)ret[5]; if (retrievingNestedType) { var flags = System.Reflection.BindingFlags.Public; if (hasPrivatePrivillage) { flags |= System.Reflection.BindingFlags.NonPublic; } var nestedType = typeObject.GetNestedType(name, flags); if (nestedType != null) { Lua.PushTypeInternal(L, nestedType); } else { Api.lua_pushnil(L); } return(1); } var exactTypes = (Type[])ret[3]; var genericTypes = (Type[])ret[4]; return(Lua.GetMember(L, thisObject, typeObject, name, hasPrivatePrivillage, exactTypes, genericTypes)); } } } else { return(Lua.IndexObjectInternal(L, thisObject, typeObject, new object[] { Lua.ValueAtInternal(L, 2) })); } }