public static int LoadFile(IntPtr ptr) { Loader(ptr); if (LuaNativeMethods.lua_isnil(ptr, -1)) { string fileName = LuaNativeMethods.lua_tostring(ptr, 1); return(LuaObject.Error(ptr, "Can't find {0}", fileName)); } return(2); }
public static int ProtectedCall(IntPtr ptr) { int status; if (LuaNativeMethods.lua_type(ptr, 1) != LuaTypes.TYPE_FUNCTION) { return(LuaObject.Error(ptr, "arg 1 expect function")); } LuaNativeMethods.luaL_checktype(ptr, 1, LuaTypes.TYPE_FUNCTION); status = LuaNativeMethods.lua_pcall(ptr, LuaNativeMethods.lua_gettop(ptr) - 1, LuaNativeMethods.LUAMultRet, 0); LuaNativeMethods.lua_pushboolean(ptr, status == 0); LuaNativeMethods.lua_insert(ptr, 1); return(LuaNativeMethods.lua_gettop(ptr)); /* return status + all results */ }
public static int Import(IntPtr ptr) { try { LuaNativeMethods.luaL_checktype(ptr, 1, LuaTypes.TYPE_STRING); string str = LuaNativeMethods.lua_tostring(ptr, 1); string[] ns = str.Split('.'); LuaNativeMethods.lua_pushglobaltable(ptr); for (int n = 0; n < ns.Length; n++) { LuaNativeMethods.lua_getfield(ptr, -1, ns[n]); if (!LuaNativeMethods.lua_istable(ptr, -1)) { return(LuaObject.Error(ptr, "expect {0} is type table", ns)); } LuaNativeMethods.lua_remove(ptr, -2); } LuaNativeMethods.lua_pushnil(ptr); while (LuaNativeMethods.lua_next(ptr, -2) != 0) { string key = LuaNativeMethods.lua_tostring(ptr, -2); LuaNativeMethods.lua_getglobal(ptr, key); if (!LuaNativeMethods.lua_isnil(ptr, -1)) { LuaNativeMethods.lua_pop(ptr, 1); return(LuaObject.Error(ptr, "{0} had existed, import can't overload it.", key)); } LuaNativeMethods.lua_pop(ptr, 1); LuaNativeMethods.lua_setglobal(ptr, key); } LuaNativeMethods.lua_pop(ptr, 1); LuaObject.PushValue(ptr, true); return(1); } catch (Exception e) { return(LuaObject.Error(ptr, e)); } }
public static int DoFile(IntPtr ptr) { int n = LuaNativeMethods.lua_gettop(ptr); Loader(ptr); if (!LuaNativeMethods.lua_toboolean(ptr, -2)) { return(2); } else { if (LuaNativeMethods.lua_isnil(ptr, -1)) { string fileName = LuaNativeMethods.lua_tostring(ptr, 1); return(LuaObject.Error(ptr, "Can't find {0}", fileName)); } int k = LuaNativeMethods.lua_gettop(ptr); LuaNativeMethods.lua_call(ptr, 0, LuaNativeMethods.LUAMultRet); k = LuaNativeMethods.lua_gettop(ptr); return(k - n); } }
public static int Loader(IntPtr ptr) { string fileName = LuaNativeMethods.lua_tostring(ptr, 1); byte[] bytes = LoadFile(fileName); if (bytes != null) { if (LuaNativeMethods.luaL_loadbuffer(ptr, bytes, bytes.Length, "@" + fileName) == 0) { LuaObject.PushValue(ptr, true); LuaNativeMethods.lua_insert(ptr, -2); return(2); } else { string errstr = LuaNativeMethods.lua_tostring(ptr, -1); return(LuaObject.Error(ptr, errstr)); } } LuaObject.PushValue(ptr, true); LuaNativeMethods.lua_pushnil(ptr); return(2); }