// lockCallback, unlockCallback; used by debug code commented out for now public LuaInterface() { luaState = LuaDLL.luaL_newstate(); // steffenj: Lua 5.1.1 API change (lua_open is gone) //LuaDLL.luaopen_base(luaState); // steffenj: luaopen_* no longer used LuaDLL.luaL_openlibs(luaState); // steffenj: Lua 5.1.1 API change (luaopen_base is gone, just open all libs right here) LuaDLL.lua_pushstring(luaState, "LUAINTERFACE LOADED"); LuaDLL.lua_pushboolean(luaState, true); LuaDLL.lua_settable(luaState, (int)LuaIndexes.LUA_REGISTRYINDEX); LuaDLL.lua_newtable(luaState); LuaDLL.lua_setglobal(luaState, "luanet"); LuaDLL.lua_pushvalue(luaState, (int)LuaIndexes.LUA_GLOBALSINDEX); LuaDLL.lua_getglobal(luaState, "luanet"); LuaDLL.lua_pushstring(luaState, "getmetatable"); LuaDLL.lua_getglobal(luaState, "getmetatable"); LuaDLL.lua_settable(luaState, -3); LuaDLL.lua_replace(luaState, (int)LuaIndexes.LUA_GLOBALSINDEX); translator = new ObjectTranslator(this, luaState); LuaDLL.lua_replace(luaState, (int)LuaIndexes.LUA_GLOBALSINDEX); LuaDLL.luaL_dostring(luaState, ScriptStrings.InitLuaNet); // steffenj: lua_dostring renamed to luaL_dostring tracebackFunction = new SharpLua.Lua.lua_CFunction(traceback); // We need to keep this in a managed reference so the delegate doesn't get garbage collected panicCallback = new SharpLua.Lua.lua_CFunction(PanicCallback); Lua.lua_CFunction oldpanicFunc = //LuaDLL.lua_atpanic(luaState, tracebackFunction); LuaDLL.lua_atpanic(luaState, panicCallback); LuaDLL.luaL_dostring(luaState, ScriptStrings.InitClrLib); LuaDLL.luaL_dostring(luaState, ScriptStrings.InitExtLib); luaState.SetInterface(this); }
// lockCallback, unlockCallback; used by debug code commented out for now public LuaInterface() { luaState = LuaDLL.luaL_newstate(); // steffenj: Lua 5.1.1 API change (lua_open is gone) luaState.initializing = true; //LuaDLL.luaopen_base(luaState); // steffenj: luaopen_* no longer used LuaDLL.luaL_openlibs(luaState); // steffenj: Lua 5.1.1 API change (luaopen_base is gone, just open all libs right here) LuaDLL.lua_pushstring(luaState, "LUAINTERFACE LOADED"); LuaDLL.lua_pushboolean(luaState, true); LuaDLL.lua_settable(luaState, (int)LuaIndexes.LUA_REGISTRYINDEX); LuaDLL.lua_newtable(luaState); LuaDLL.lua_setglobal(luaState, "luanet"); LuaDLL.lua_pushvalue(luaState, (int)LuaIndexes.LUA_GLOBALSINDEX); LuaDLL.lua_getglobal(luaState, "luanet"); LuaDLL.lua_pushstring(luaState, "getmetatable"); LuaDLL.lua_getglobal(luaState, "getmetatable"); LuaDLL.lua_settable(luaState, -3); LuaDLL.lua_replace(luaState, (int)LuaIndexes.LUA_GLOBALSINDEX); translator = new ObjectTranslator(this, luaState); LuaDLL.lua_replace(luaState, (int)LuaIndexes.LUA_GLOBALSINDEX); tracebackFunction = new SharpLua.Lua.lua_CFunction(traceback); // We need to keep this in a managed reference so the delegate doesn't get garbage collected panicCallback = new SharpLua.Lua.lua_CFunction(PanicCallback); Lua.lua_CFunction oldpanicFunc = //LuaDLL.lua_atpanic(luaState, tracebackFunction); LuaDLL.lua_atpanic(luaState, panicCallback); DoString(ScriptStrings.InitLuaNet, "LuaNet"); DoString(ScriptStrings.InitClrLib, "ClrLib"); DoString(ScriptStrings.InitExtLib, "ExtLib"); luaState.initializing = false; luaState.SetInterface(this); }
public MetaFunctions(ObjectTranslator translator) { this.translator = translator; gcFunction = new SharpLua.Lua.lua_CFunction(this.collectObject); toStringFunction = new SharpLua.Lua.lua_CFunction(this.toString); indexFunction = new SharpLua.Lua.lua_CFunction(this.getMethod); newindexFunction = new SharpLua.Lua.lua_CFunction(this.setFieldOrProperty); baseIndexFunction = new SharpLua.Lua.lua_CFunction(this.getBaseMethod); callConstructorFunction = new SharpLua.Lua.lua_CFunction(this.callConstructor); classIndexFunction = new SharpLua.Lua.lua_CFunction(this.getClassMethod); classNewindexFunction = new SharpLua.Lua.lua_CFunction(this.setClassFieldOrProperty); execDelegateFunction = new SharpLua.Lua.lua_CFunction(this.runFunctionDelegate); }
public MetaFunctions(ObjectTranslator translator) { this.translator = translator; gcFunction = new SharpLua.Lua.lua_CFunction(this.collectObject); toStringFunction = new SharpLua.Lua.lua_CFunction(this.toString); indexFunction = new SharpLua.Lua.lua_CFunction(this.getMethod); newindexFunction = new SharpLua.Lua.lua_CFunction(this.setFieldOrProperty); baseIndexFunction = new SharpLua.Lua.lua_CFunction(this.getBaseMethod); callConstructorFunction = new SharpLua.Lua.lua_CFunction(this.callConstructor); classIndexFunction = new SharpLua.Lua.lua_CFunction(this.getClassMethod); classNewindexFunction = new SharpLua.Lua.lua_CFunction(this.setClassFieldOrProperty); execDelegateFunction = new SharpLua.Lua.lua_CFunction(this.runFunctionDelegate); //new String() }
/* * Pushes a delegate into the stack */ internal void pushFunction(SharpLua.Lua.LuaState luaState, SharpLua.Lua.lua_CFunction func) { //Console.WriteLine("function push"); if (true) { // 11/16/12 - Fix function pushing (Dirk Weltz metatable problem) // SharpLua.InterfacingTests still works // This allows metatables to have CLR defined functions Lua.lua_pushcfunction(luaState, func); } else { pushObject(luaState, func, "luaNet_function"); } }
public ObjectTranslator(LuaInterface interpreter, SharpLua.Lua.LuaState luaState) { this.interpreter = interpreter; typeChecker = new CheckType(this); metaFunctions = new MetaFunctions(this); importTypeFunction = new SharpLua.Lua.lua_CFunction(this.importType); loadAssemblyFunction = new SharpLua.Lua.lua_CFunction(this.loadAssembly); registerTableFunction = new SharpLua.Lua.lua_CFunction(this.registerTable); unregisterTableFunction = new SharpLua.Lua.lua_CFunction(this.unregisterTable); getMethodSigFunction = new SharpLua.Lua.lua_CFunction(this.getMethodSignature); getConstructorSigFunction = new SharpLua.Lua.lua_CFunction(this.getConstructorSignature); ctypeFunction = new SharpLua.Lua.lua_CFunction(this.ctype); enumFromIntFunction = new SharpLua.Lua.lua_CFunction(this.enumFromInt); createLuaObjectList(luaState); createIndexingMetaFunction(luaState); createBaseClassMetatable(luaState); createClassMetatable(luaState); createFunctionMetatable(luaState); setGlobalFunctions(luaState); }
/* * Pushes a delegate into the stack */ internal void pushFunction(SharpLua.Lua.LuaState luaState, SharpLua.Lua.lua_CFunction func) { pushObject(luaState, func, "luaNet_function"); }
//[DllImport(LUADLL, CallingConvention = CallingConvention.Cdecl)] public static Lua.lua_CFunction lua_atpanic(SharpLua.Lua.LuaState luaState, SharpLua.Lua.lua_CFunction panicf) { return(SharpLua.Lua.lua_atpanic(luaState, panicf)); }
//[DllImport(STUBDLL,CallingConvention=CallingConvention.Cdecl)] public static void lua_pushstdcallcfunction(SharpLua.Lua.LuaState luaState, SharpLua.Lua.lua_CFunction function) { SharpLua.Lua.lua_pushcfunction(luaState, function); }
public LuaFunction(SharpLua.Lua.lua_CFunction function, LuaInterface interpreter) { _Reference = 0; this.function = function; _Interpreter = interpreter; }
//internal int reference; public LuaFunction(int reference, LuaInterface interpreter) { _Reference = reference; this.function = null; _Interpreter = interpreter; }
/* * __call metafunction of CLR delegates, retrieves and calls the delegate. */ private int runFunctionDelegate(SharpLua.Lua.LuaState luaState) { SharpLua.Lua.lua_CFunction func = (SharpLua.Lua.lua_CFunction)translator.getRawNetObject(luaState, 1); LuaDLL.lua_remove(luaState, 1); return(func(luaState)); }
/* * Pushes the value of a member or a delegate to call it, depending on the type of * the member. Works with static or instance members. * Uses reflection to find members, and stores the reflected MemberInfo object in * a cache (indexed by the type of the object and the name of the member). */ private int getMember(SharpLua.Lua.LuaState luaState, IReflect objType, object obj, string methodName, BindingFlags bindingType) { bool implicitStatic = false; MemberInfo member = null; object cachedMember = checkMemberCache(memberCache, objType, methodName); //object cachedMember=null; if (cachedMember is SharpLua.Lua.lua_CFunction) { translator.pushFunction(luaState, (SharpLua.Lua.lua_CFunction)cachedMember); translator.push(luaState, true); return(2); } else if (cachedMember != null) { member = (MemberInfo)cachedMember; } else { //CP: Removed NonPublic binding search MemberInfo[] members = objType.GetMember(methodName, bindingType | BindingFlags.Public | BindingFlags.IgnoreCase /*| BindingFlags.NonPublic*/); if (members.Length > 0) { member = members[0]; } else { // If we can't find any suitable instance members, try to find them as statics - but we only want to allow implicit static // lookups for fields/properties/events -kevinh //CP: Removed NonPublic binding search and made case insensitive members = objType.GetMember(methodName, bindingType | BindingFlags.Static | BindingFlags.Public | BindingFlags.IgnoreCase /*| BindingFlags.NonPublic*/); if (members.Length > 0) { member = members[0]; implicitStatic = true; } } } if (member != null) { if (member.MemberType == MemberTypes.Field) { FieldInfo field = (FieldInfo)member; if (cachedMember == null) { setMemberCache(memberCache, objType, methodName, member); } try { translator.push(luaState, field.GetValue(obj)); } catch { LuaDLL.lua_pushnil(luaState); } } else if (member.MemberType == MemberTypes.Property) { PropertyInfo property = (PropertyInfo)member; if (cachedMember == null) { setMemberCache(memberCache, objType, methodName, member); } try { object val = property.GetValue(obj, null); translator.push(luaState, val); } catch (ArgumentException) { // If we can't find the getter in our class, recurse up to the base class and see // if they can help. if (objType is Type && !(((Type)objType) == typeof(object))) { return(getMember(luaState, ((Type)objType).BaseType, obj, methodName, bindingType)); } else { LuaDLL.lua_pushnil(luaState); } } catch (TargetInvocationException e) // Convert this exception into a Lua error { ThrowError(luaState, e); LuaDLL.lua_pushnil(luaState); } } else if (member.MemberType == MemberTypes.Event) { EventInfo eventInfo = (EventInfo)member; if (cachedMember == null) { setMemberCache(memberCache, objType, methodName, member); } translator.push(luaState, new RegisterEventHandler(translator.pendingEvents, obj, eventInfo)); } else if (!implicitStatic) { if (member.MemberType == MemberTypes.NestedType) { // kevinh - added support for finding nested types // cache us if (cachedMember == null) { setMemberCache(memberCache, objType, methodName, member); } // Find the name of our class string name = member.Name; Type dectype = member.DeclaringType; // Build a new long name and try to find the type by name string longname = dectype.FullName + "+" + name; Type nestedType = translator.FindType(longname); translator.pushType(luaState, nestedType); } else { // Member type must be 'method' SharpLua.Lua.lua_CFunction wrapper = new SharpLua.Lua.lua_CFunction((new LuaMethodWrapper(translator, objType, methodName, bindingType)).call); if (cachedMember == null) { setMemberCache(memberCache, objType, methodName, wrapper); } translator.pushFunction(luaState, wrapper); translator.push(luaState, true); return(2); } } else { // If we reach this point we found a static method, but can't use it in this context because the user passed in an instance translator.throwError(luaState, "can't pass instance to static method " + methodName); LuaDLL.lua_pushnil(luaState); } } else { // kevinh - we want to throw an exception because meerly returning 'nil' in this case // is not sufficient. valid data members may return nil and therefore there must be some // way to know the member just doesn't exist. translator.throwError(luaState, "unknown member name " + methodName); LuaDLL.lua_pushnil(luaState); } // push false because we are NOT returning a function (see luaIndexFunction) translator.push(luaState, false); return(2); }
/* * Pushes the value of a member or a delegate to call it, depending on the type of * the member. Works with static or instance members. * Uses reflection to find members, and stores the reflected MemberInfo object in * a cache (indexed by the type of the object and the name of the member). */ private int getMember(SharpLua.Lua.LuaState luaState, IReflect objType, object obj, string methodName, BindingFlags bindingType) { bool implicitStatic = false; MemberInfo member = null; object cachedMember = checkMemberCache(memberCache, objType, methodName); //object cachedMember=null; if (cachedMember is SharpLua.Lua.lua_CFunction) { translator.pushFunction(luaState, (SharpLua.Lua.lua_CFunction)cachedMember); translator.push(luaState, true); return 2; } else if (cachedMember != null) { member = (MemberInfo)cachedMember; } else { //CP: Removed NonPublic binding search MemberInfo[] members = objType.GetMember(methodName, bindingType | BindingFlags.Public | BindingFlags.IgnoreCase/*| BindingFlags.NonPublic*/); if (members.Length > 0) member = members[0]; else { // If we can't find any suitable instance members, try to find them as statics - but we only want to allow implicit static // lookups for fields/properties/events -kevinh //CP: Removed NonPublic binding search and made case insensitive members = objType.GetMember(methodName, bindingType | BindingFlags.Static | BindingFlags.Public | BindingFlags.IgnoreCase/*| BindingFlags.NonPublic*/); if (members.Length > 0) { member = members[0]; implicitStatic = true; } } } if (member != null) { if (member.MemberType == MemberTypes.Field) { FieldInfo field = (FieldInfo)member; if (cachedMember == null) setMemberCache(memberCache, objType, methodName, member); try { translator.push(luaState, field.GetValue(obj)); } catch { LuaDLL.lua_pushnil(luaState); } } else if (member.MemberType == MemberTypes.Property) { PropertyInfo property = (PropertyInfo)member; if (cachedMember == null) setMemberCache(memberCache, objType, methodName, member); try { object val = property.GetValue(obj, null); translator.push(luaState, val); } catch (ArgumentException) { // If we can't find the getter in our class, recurse up to the base class and see // if they can help. if (objType is Type && !(((Type)objType) == typeof(object))) return getMember(luaState, ((Type)objType).BaseType, obj, methodName, bindingType); else LuaDLL.lua_pushnil(luaState); } catch (TargetInvocationException e) // Convert this exception into a Lua error { ThrowError(luaState, e); LuaDLL.lua_pushnil(luaState); } } else if (member.MemberType == MemberTypes.Event) { EventInfo eventInfo = (EventInfo)member; if (cachedMember == null) setMemberCache(memberCache, objType, methodName, member); translator.push(luaState, new RegisterEventHandler(translator.pendingEvents, obj, eventInfo)); } else if (!implicitStatic) { if (member.MemberType == MemberTypes.NestedType) { // kevinh - added support for finding nested types // cache us if (cachedMember == null) setMemberCache(memberCache, objType, methodName, member); // Find the name of our class string name = member.Name; Type dectype = member.DeclaringType; // Build a new long name and try to find the type by name string longname = dectype.FullName + "+" + name; Type nestedType = translator.FindType(longname); translator.pushType(luaState, nestedType); } else { // Member type must be 'method' SharpLua.Lua.lua_CFunction wrapper = new SharpLua.Lua.lua_CFunction((new LuaMethodWrapper(translator, objType, methodName, bindingType)).call); if (cachedMember == null) setMemberCache(memberCache, objType, methodName, wrapper); translator.pushFunction(luaState, wrapper); translator.push(luaState, true); return 2; } } else { // If we reach this point we found a static method, but can't use it in this context because the user passed in an instance translator.throwError(luaState, "can't pass instance to static method " + methodName); LuaDLL.lua_pushnil(luaState); } } else { // kevinh - we want to throw an exception because meerly returning 'nil' in this case // is not sufficient. valid data members may return nil and therefore there must be some // way to know the member just doesn't exist. translator.throwError(luaState, "unknown member name " + methodName); LuaDLL.lua_pushnil(luaState); } // push false because we are NOT returning a function (see luaIndexFunction) translator.push(luaState, false); return 2; }
internal void pushCSFunction(SharpLua.Lua.lua_CFunction function) { translator.pushFunction(luaState, function); }