Beispiel #1
0
 public ObjectCache(IntPtr ptr)
 {
     LuaNativeMethods.lua_newtable(ptr);
     LuaNativeMethods.lua_newtable(ptr);
     LuaNativeMethods.lua_pushstring(ptr, "v");
     LuaNativeMethods.lua_setfield(ptr, -2, "__mode");
     LuaNativeMethods.lua_setmetatable(ptr, -2);
     cacheRef = LuaNativeMethods.luaL_ref(ptr, LuaIndexes.LUARegistryIndex);
 }
 public LuaThreadWrapper(LuaFunction function) : base()
 {
     Logger.Log(string.Format("LuaThreadWrapper.ctor/1: {0}", LuaNativeMethods.lua_gettop(function.VariablePointer)));
     this.state    = LuaState.Get(function.VariablePointer);
     this.thread   = LuaNativeMethods.lua_newthread(function.VariablePointer);
     this.valueref = LuaNativeMethods.luaL_ref(function.VariablePointer, LuaIndexes.LUARegistryIndex);
     function.Push(function.VariablePointer);
     LuaNativeMethods.lua_xmove(function.VariablePointer, this.thread, 1);
     Logger.Log(string.Format("LuaThreadWrapper.ctor/2: {0}", LuaNativeMethods.lua_gettop(function.VariablePointer)));
 }
        public static bool CheckType(IntPtr ptr, int p, out LuaTable t)
        {
            if (LuaNativeMethods.lua_isnil(ptr, p))
            {
                t = null;
                return(true);
            }

            LuaNativeMethods.luaL_checktype(ptr, p, LuaTypes.TYPE_TABLE);
            LuaNativeMethods.lua_pushvalue(ptr, p);
            int fref = LuaNativeMethods.luaL_ref(ptr, LuaIndexes.LUARegistryIndex);

            t = new LuaTable(ptr, fref);
            return(true);
        }
        public static bool CheckType(IntPtr ptr, int p, out LuaFunction f)
        {
            if (LuaNativeMethods.lua_isnil(ptr, p))
            {
                f = null;
                return(true);
            }

            LuaNativeMethods.luaL_checktype(ptr, p, LuaTypes.TYPE_FUNCTION);
            LuaNativeMethods.lua_pushvalue(ptr, p);
            int fref = LuaNativeMethods.luaL_ref(ptr, LuaIndexes.LUARegistryIndex);

            f = new LuaFunction(ptr, fref);
            return(true);
        }
Beispiel #5
0
        public int PushTry()
        {
            if (errorRef == 0)
            {
                LuaNativeMethods.lua_pushcfunction(statePointer, LuaState.errorFunc);
                LuaNativeMethods.lua_pushvalue(statePointer, -1);
                errorRef = LuaNativeMethods.luaL_ref(statePointer, LuaIndexes.LUARegistryIndex);
            }
            else
            {
                LuaNativeMethods.lua_getref(statePointer, errorRef);
            }

            return(LuaNativeMethods.lua_gettop(statePointer));
        }
Beispiel #6
0
        public static LuaDelegate NewDelegate(IntPtr ptr, int p)
        {
            LuaState state = LuaState.Get(ptr);

            LuaNativeMethods.lua_pushvalue(ptr, p);                                         // push function

            int         fref = LuaNativeMethods.luaL_ref(ptr, LuaIndexes.LUARegistryIndex); // new ref function
            LuaDelegate f    = new LuaDelegate(ptr, fref);

            LuaNativeMethods.lua_pushvalue(ptr, p);
            LuaNativeMethods.lua_pushinteger(ptr, fref);
            LuaNativeMethods.lua_settable(ptr, -3); // __LuaDelegate[func]= fref
            state.DelegateMap[fref] = f;
            return(f);
        }
Beispiel #7
0
        public LuaState()
        {
            if (mainThread == 0)
            {
                mainThread = System.Threading.Thread.CurrentThread.ManagedThreadId;
            }

            statePointer           = LuaNativeMethods.luaL_newstate();
            statemap[statePointer] = this;

            if (Main == null)
            {
                Main = this;
            }

            refQueue = new Queue <UnrefPair>();
            ObjectCache.Make(statePointer);

            LuaNativeMethods.lua_atpanic(statePointer, PanicCallback);

            LuaNativeMethods.luaL_openlibs(statePointer);

            string callCSFunction = @"
local assert = assert
local function check(ok,...)
    assert(ok, ...)
    return ...
end
return function(cs_func)
    return function(...)
        return check(cs_func(...))
    end
end
";

            LuaNativeMethods.lua_dostring(statePointer, callCSFunction);
            callCSFunctionRef = LuaNativeMethods.luaL_ref(statePointer, LuaIndexes.LUARegistryIndex);
            SetupPushVar();
            ProtectedCall(statePointer, Init);
        }
Beispiel #8
0
 public LuaTable(LuaState state) : base(state, 0)
 {
     LuaNativeMethods.lua_newtable(VariablePointer);
     valueref = LuaNativeMethods.luaL_ref(VariablePointer, LuaIndexes.LUARegistryIndex);
 }