public Lua() { luaState = LuaLib.luaL_newstate(); // steffenj: Lua 5.1.1 API change (lua_open is gone) //LuaLib.luaopen_base(luaState); // steffenj: luaopen_* no longer used LuaLib.luaL_openlibs(luaState); // steffenj: Lua 5.1.1 API change (luaopen_base is gone, just open all libs right here) LuaLib.lua_pushstring(luaState, "LUAINTERFACE LOADED"); LuaLib.lua_pushboolean(luaState, true); LuaLib.lua_settable(luaState, (int)LuaIndexes.Registry); LuaLib.lua_newtable(luaState); LuaLib.lua_setglobal(luaState, "luanet"); //LuaLib.lua_pushvalue(luaState, (int)LuaIndexes.Globals); LuaCore.lua_pushglobaltable(luaState); LuaLib.lua_getglobal(luaState, "luanet"); LuaLib.lua_pushstring(luaState, "getmetatable"); LuaLib.lua_getglobal(luaState, "getmetatable"); LuaLib.lua_settable(luaState, -3); //LuaLib.lua_replace(luaState, (int)LuaIndexes.Globals); LuaCore.lua_rawseti(luaState, LuaCore.LUA_REGISTRYINDEX, LuaCore.LUA_RIDX_GLOBALS); translator = new ObjectTranslator(this, luaState); //LuaLib.lua_replace(luaState, (int)LuaIndexes.Globals); LuaCore.lua_rawseti(luaState, LuaCore.LUA_REGISTRYINDEX, LuaCore.LUA_RIDX_GLOBALS); LuaLib.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 LuaCore.lua_CFunction(PanicCallback); LuaLib.lua_atpanic(luaState, panicCallback); //LuaLib.lua_atlock(luaState, lockCallback = new LuaCore.lua_CFunction(LockCallback)); //LuaLib.lua_atunlock(luaState, unlockCallback = new LuaCore.lua_CFunction(UnlockCallback)); }
/* * CAUTION: LuaInterface.Lua instances can't share the same lua state! */ public Lua(LuaCore.lua_State lState) { LuaLib.lua_pushstring(lState, "LUAINTERFACE LOADED"); LuaLib.lua_gettable(lState, (int)LuaIndexes.Registry); if (LuaLib.lua_toboolean(lState, -1)) { LuaLib.lua_settop(lState, -2); throw new LuaException("There is already a LuaInterface.Lua instance associated with this Lua state"); } else { LuaLib.lua_settop(lState, -2); LuaLib.lua_pushstring(lState, "LUAINTERFACE LOADED"); LuaLib.lua_pushboolean(lState, true); LuaLib.lua_settable(lState, (int)LuaIndexes.Registry); luaState = lState; //LuaLib.lua_pushvalue(lState, (int)LuaIndexes.Globals); LuaCore.lua_pushglobaltable(luaState); LuaLib.lua_getglobal(lState, "luanet"); LuaLib.lua_pushstring(lState, "getmetatable"); LuaLib.lua_getglobal(lState, "getmetatable"); LuaLib.lua_settable(lState, -3); //LuaLib.lua_replace(lState, (int)LuaIndexes.Globals); LuaCore.lua_rawseti(luaState, LuaCore.LUA_REGISTRYINDEX, LuaCore.LUA_RIDX_GLOBALS); translator = new ObjectTranslator(this, luaState); //LuaLib.lua_replace(lState, (int)LuaIndexes.Globals); LuaCore.lua_rawseti(luaState, LuaCore.LUA_REGISTRYINDEX, LuaCore.LUA_RIDX_GLOBALS); LuaLib.luaL_dostring(lState, Lua.init_luanet); // steffenj: lua_dostring renamed to luaL_dostring } _StatePassed = true; }
public static void lua_setglobal(LuaCore.lua_State luaState, string name) { LuaCore.lua_setglobal(luaState, name); //lua_pushstring(luaState,name); //lua_insert(luaState,-2); //lua_settable(luaState, (int)LuaIndexes.Globals); }
// Starting with 5.1 the auxlib version of checkudata throws an exception if the type isn't right // Instead, we want to run our own version that checks the type and just returns null for failure private static IntPtr checkudata_raw(LuaCore.lua_State L, int ud, string tname) { var p = LuaCore.lua_touserdata(L, ud); if (p != IntPtr.Zero) { /* value is a userdata? */ if (LuaCore.lua_getmetatable(L, ud) != 0) { bool isEqual; /* does it have a metatable? */ LuaCore.lua_getfield(L, (int)LuaIndexes.Registry, tname); /* get correct metatable */ isEqual = LuaCore.lua_rawequal(L, -1, -2) != 0; // NASTY - we need our own version of the lua_pop macro // lua_pop(L, 2); /* remove both metatables */ LuaCore.lua_settop(L, -(2) - 1); if (isEqual) /* does it have the correct mt? */ { return(p); } } } return(IntPtr.Zero); }
/* * Lets go of a previously allocated reference to a table, function * or userdata */ #region lua debug functions /// <summary> /// Activates the debug hook /// </summary> /// <param name = "mask">Mask</param> /// <param name = "count">Count</param> /// <returns>see lua docs. -1 if hook is already set</returns> /// <author>Reinhard Ostermeier</author> public void SetDebugHook(EventMasks mask, int count) { if (hookCallback.IsNull()) { hookCallback = new LuaCore.lua_Hook(DebugHookCallback); LuaCore.lua_sethook(luaState, hookCallback, (int)mask, count); return; } }
public static LuaCore.lua_CFunction lua_tocfunction(LuaCore.lua_State luaState, int index) { var ptr = LuaCore.lua_tocfunction(luaState, index); if (ptr == IntPtr.Zero) { return(null); } return(Marshal.GetDelegateForFunctionPointer <LuaCore.lua_CFunction>(ptr)); }
// steffenj: END Lua 5.1.1 API change (lua_newtable is gone, lua_createtable is new) // steffenj: BEGIN Lua 5.1.1 API change (lua_dofile now in LuaLib as luaL_dofile macro) public static int luaL_dofile(LuaCore.lua_State luaState, string fileName) { int result = LuaCore.luaL_loadfile(luaState, fileName); if (result != 0) { return(result); } return(LuaCore.lua_pcall(luaState, 0, -1, 0)); }
/// <summary> /// Called for each lua_lock call /// </summary> /// <param name = "luaState"></param> /// Not yet used /*int LockCallback(LuaCore.lua_State luaState) * { * // Monitor.Enter(luaLock); * return 0; * }*/ /// <summary> /// Called for each lua_unlock call /// </summary> /// <param name = "luaState"></param> /// Not yet used /*int UnlockCallback(LuaCore.lua_State luaState) * { * // Monitor.Exit(luaLock); * return 0; * }*/ public void Close() { if (_StatePassed) { return; } ////// if(luaState != LuaCore.lua_State.Zero) if (!luaState.IsNull()) { LuaCore.lua_close(luaState); } //luaState = LuaCore.lua_State.Zero; <- suggested by Christopher Cebulski http://luaforge.net/forum/forum.php?thread_id = 44593&forum_id = 146 }
public static IntPtr luaL_checkudata(LuaCore.lua_State luaState, int stackPos, string meta) { return(LuaCore.luaL_checkudata(luaState, stackPos, meta)); }
// steffenj: END Lua 5.1.1 API change (lua_strlen is now lua_objlen) // steffenj: BEGIN Lua 5.1.1 API change (lua_dostring is now a macro luaL_dostring) public static int luaL_loadstring(LuaCore.lua_State luaState, string chunk) { return(LuaCore.luaL_loadstring(luaState, chunk)); }
// steffenj: BEGIN Lua 5.1.1 API change (new function luaL_openlibs) public static void luaL_openlibs(LuaCore.lua_State luaState) { LuaCore.luaL_openlibs(luaState); }
// steffenj: BEGIN Lua 5.1.1 API change (lua_open replaced by luaL_newstate) public static LuaCore.lua_State luaL_newstate() { return(LuaCore.luaL_newstate()); }
public static int luaL_loadfile(LuaCore.lua_State luaState, string filename) { return(LuaCore.luaL_loadfile(luaState, filename)); }
/// <summary> /// Gets the hook mask. /// </summary> /// <returns>hook mask</returns> /// <author>Reinhard Ostermeier</author> public EventMasks GetHookMask() { return((EventMasks)LuaCore.lua_gethookmask(luaState)); }
public static int luaL_newmetatable(LuaCore.lua_State luaState, string meta) { return(LuaCore.luaL_newmetatable(luaState, meta)); }
/// <summary> /// Sets local (see lua docs) /// </summary> /// <param name = "luaDebug">lua debug structure</param> /// <param name = "n">see lua docs</param> /// <returns>see lua docs</returns> /// <author>Reinhard Ostermeier</author> public string SetLocal(LuaCore.lua_Debug luaDebug, int n) { return(LuaCore.lua_setlocal(luaState, luaDebug, n).ToString()); }
public static void lua_pushlightuserdata(LuaCore.lua_State luaState, object udata) { var handle = GCHandle.Alloc(udata, GCHandleType.Pinned); LuaCore.lua_pushlightuserdata(luaState, handle.AddrOfPinnedObject()); }
// steffenj: BEGIN additional Lua API functions new in Lua 5.1 public static int lua_gc(LuaCore.lua_State luaState, GCOptions what, int data) { return(LuaCore.lua_gc(luaState, (int)what, data)); }
public static int lua_next(LuaCore.lua_State luaState, int index) { return(LuaCore.lua_next(luaState, index)); }
public static bool lua_checkstack(LuaCore.lua_State luaState, int extra) { return(LuaCore.lua_checkstack(luaState, extra) != 0); }
public static void lua_error(LuaCore.lua_State luaState) { LuaCore.lua_error(luaState); }
// steffenj: BEGIN Lua 5.1.1 API change (luaL_getmetatable is now a macro using lua_getfield) public static void lua_getfield(LuaCore.lua_State luaState, int stackPos, string meta) { LuaCore.lua_getfield(luaState, stackPos, meta); }
public static bool luaL_getmetafield(LuaCore.lua_State luaState, int stackPos, string field) { return(LuaCore.luaL_getmetafield(luaState, stackPos, field) != 0); }
public static void luaL_error(LuaCore.lua_State luaState, string message) { LuaCore.luaL_error(luaState, message); }
/// <summary> /// Removes the debug hook /// </summary> /// <returns>see lua docs</returns> /// <author>Reinhard Ostermeier</author> public void RemoveDebugHook() { hookCallback = null; LuaCore.lua_sethook(luaState, null, 0, 0); }
public static void luaL_where(LuaCore.lua_State luaState, int level) { LuaCore.luaL_where(luaState, level); }
/// <summary> /// Gets the hook count /// </summary> /// <returns>see lua docs</returns> /// <author>Reinhard Ostermeier</author> public int GetHookCount() { return(LuaCore.lua_gethookcount(luaState)); }
public static int luaL_loadbuffer(LuaCore.lua_State luaState, string buff, string name) { return(LuaCore.luaL_loadbuffer(luaState, buff, (uint)buff.Length, name)); }
/// <summary> /// Sets up value (see lua docs) /// </summary> /// <param name = "funcindex">see lua docs</param> /// <param name = "n">see lua docs</param> /// <returns>see lua docs</returns> /// <author>Reinhard Ostermeier</author> public string SetUpValue(int funcindex, int n) { return(LuaCore.lua_setupvalue(luaState, funcindex, n).ToString()); }
public static string lua_typename(LuaCore.lua_State luaState, LuaTypes type) { return(LuaCore.lua_typename(luaState, (int)type).ToString()); }