/* * Registers the indexing function of CLR objects * passed to Lua */ private void createIndexingMetaFunction(KopiLua.LuaState luaState) { LuaDll.lua_pushstring(luaState, "luaNet_indexfunction"); LuaDll.luaL_dostring(luaState, MetaFunctions.luaIndexFunction); // steffenj: lua_dostring renamed to luaL_dostring //LuaDLL.lua_pushstdcallcfunction(luaState,indexFunction); LuaDll.lua_rawset(luaState, (int)LuaIndexes.LUA_REGISTRYINDEX); }
public void lua_registerTest() { bool wasCalled = false; LuaDll.lua_CFunction testFunctionDelegate = delegate(lua_State state) { wasCalled = true; LuaDll.lua_pushstring(state, "Success!"); return(1); }; // Create a new state var L = LuaDll.luaL_newstate(); // Register the given function by name LuaDll.lua_register(L, "test_function", testFunctionDelegate); // Call the function by name var result = LuaDll.luaL_dostring(L, "return test_function()"); Assert.AreEqual(0, result); Assert.IsTrue(wasCalled); // Check that the function returned the same string. var resultIndex = LuaDll.lua_gettop(L); var resultString = LuaDll.lua_tostring(L, resultIndex); Assert.AreEqual("Success!", resultString); }
public Lua() { 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, Lua.init_luanet); // steffenj: lua_dostring renamed to luaL_dostring // We need to keep this in a managed reference so the delegate doesn't get garbage collected panicCallback = new KopiLua.LuaNativeFunction(PanicCallback); LuaDll.lua_atpanic(luaState, panicCallback); // LuaDLL.lua_atlock(luaState, lockCallback = new KopiLua.LuaNativeFunction(LockCallback)); // LuaDLL.lua_atunlock(luaState, unlockCallback = new KopiLua.LuaNativeFunction(UnlockCallback)); }
/* * CAUTION: LuaInterface.Lua instances can't share the same lua state! */ public Lua(KopiLua.LuaState lState) { LuaDll.lua_pushstring(lState, "LUAINTERFACE LOADED"); LuaDll.lua_gettable(lState, (int)LuaIndexes.LUA_REGISTRYINDEX); if (LuaDll.lua_toboolean(lState, -1)) { LuaDll.lua_settop(lState, -2); throw new LuaException("There is already a LuaInterface.Lua instance associated with this Lua state"); } else { LuaDll.lua_settop(lState, -2); LuaDll.lua_pushstring(lState, "LUAINTERFACE LOADED"); LuaDll.lua_pushboolean(lState, true); LuaDll.lua_settable(lState, (int)LuaIndexes.LUA_REGISTRYINDEX); this.luaState = lState; LuaDll.lua_pushvalue(lState, (int)LuaIndexes.LUA_GLOBALSINDEX); LuaDll.lua_getglobal(lState, "luanet"); LuaDll.lua_pushstring(lState, "getmetatable"); LuaDll.lua_getglobal(lState, "getmetatable"); LuaDll.lua_settable(lState, -3); LuaDll.lua_replace(lState, (int)LuaIndexes.LUA_GLOBALSINDEX); translator = new ObjectTranslator(this, this.luaState); LuaDll.lua_replace(lState, (int)LuaIndexes.LUA_GLOBALSINDEX); LuaDll.luaL_dostring(lState, Lua.init_luanet); // steffenj: lua_dostring renamed to luaL_dostring } }
public void luaL_registerTest() { // Create a new state var L = LuaDll.luaL_newstate(); LuaDll.lua_CFunction func = delegate(lua_State state) { LuaDll.lua_pushstring(state, "Success!"); return(1); }; LuaDll.luaL_Reg[] testLib = { new LuaDll.luaL_Reg { name = "square", func = delegate(lua_State _L) { var value = LuaDll.lua_tonumber(_L, 1); LuaDll.lua_pushnumber(_L, value * value); return(1); } }, new LuaDll.luaL_Reg { name = "add", func = delegate(lua_State _L) { var value1 = LuaDll.lua_tonumber(_L, 1); var value2 = LuaDll.lua_tonumber(_L, 2); LuaDll.lua_pushnumber(_L, value1 + value2); return(1); } }, // Must have one nulled entry to indicate the end of the array new LuaDll.luaL_Reg { name = null, func = null } }; LuaDll.luaL_register(L, "TestLibrary", testLib); LuaDll.lua_pop(L, 1); var result = LuaDll.luaL_dostring(L, "return TestLibrary.square(9) + TestLibrary.add(3.14, 4.13)"); Assert.AreEqual(0, result); Assert.AreEqual(88.27, LuaDll.lua_tonumber(L, 1)); }