Esempio n. 1
0
        public LuaState()
        {
            if (mainThread == 0)
            {
                mainThread = System.Threading.Thread.CurrentThread.ManagedThreadId;
            }

            L           = LuaDLL.luaL_newstate();
            statemap[L] = this;



            refQueue = new Queue <UnrefPair>();
            ObjectCache.make(L);

            LuaDLL.lua_atpanic(L, panicCallback);

            LuaDLL.luaL_openlibs(L);

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

            LuaDLL.lua_dostring(L, PCallCSFunction);
            PCallCSFunctionRef = LuaDLL.luaL_ref(L, LuaIndexes.LUA_REGISTRYINDEX);

            string newindexfun = @"

local getmetatable=getmetatable
local rawget=rawget
local error=error
local type=type
local function newindex(ud,k,v)
    local t=getmetatable(ud)
    repeat
        local h=rawget(t,k)
        if h then
            if h[2] then
                h[2](ud,v)
                return
            else
                error('property '..k..' is read only')
            end
        end
        t=rawget(t,'__parent')
    until t==nil
    error('can not find '..k)
end

return newindex
";

            string indexfun = @"
local type=type
local error=error
local rawget=rawget
local getmetatable=getmetatable
local function index(ud,k)
    local t=getmetatable(ud)
    repeat
        local fun=rawget(t,k)
        local tp=type(fun)
        if tp=='function' then
            return fun
        elseif tp=='table' then
            local f=fun[1]
            if f then
                return f(ud)
            else
                error('property '..k..' is write only')
            end
        end
        t = rawget(t,'__parent')
    until t==nil
    error('Can not find '..k)
end

return index
";

            newindex_func = (LuaFunction)doString(newindexfun);
            index_func    = (LuaFunction)doString(indexfun);

            setupPushVar();

            pcall(L, init);

            createGameObject();
        }
Esempio n. 2
0
        static int init(IntPtr L)
        {
            LuaDLL.luaL_openlibs(L);

            LuaDLL.lua_pushlightuserdata(L, L);
            LuaDLL.lua_setglobal(L, "__main_state");

            LuaDLL.lua_pushcfunction(L, print);
            LuaDLL.lua_setglobal(L, "print");

            LuaDLL.lua_pushcfunction(L, pcall);
            LuaDLL.lua_setglobal(L, "pcall");

            LuaDLL.lua_pushcfunction(L, import);
            LuaDLL.lua_setglobal(L, "import");


            string resumefunc = @"
local resume = coroutine.resume
local unpack = unpack or table.unpack
coroutine.resume=function(co,...)
	local ret={resume(co,...)}
	if not ret[1] then UnityEngine.Debug.LogError(debug.traceback(co,ret[2])) end
	return unpack(ret)
end
";

            // overload resume function for report error
            if (LuaDLL.lua_dostring(L, resumefunc) != 0)
            {
                LuaObject.lastError(L);
            }

            LuaDLL.lua_pushcfunction(L, dofile);
            LuaDLL.lua_setglobal(L, "dofile");

            LuaDLL.lua_pushcfunction(L, loadfile);
            LuaDLL.lua_setglobal(L, "loadfile");

            LuaDLL.lua_pushcfunction(L, loader);
            int loaderFunc = LuaDLL.lua_gettop(L);

            LuaDLL.lua_getglobal(L, "package");
#if LUA_5_3
            LuaDLL.lua_getfield(L, -1, "searchers");
#else
            LuaDLL.lua_getfield(L, -1, "loaders");
#endif
            int loaderTable = LuaDLL.lua_gettop(L);

            // Shift table elements right
            for (int e = LuaDLL.lua_rawlen(L, loaderTable) + 1; e > 1; e--)
            {
                LuaDLL.lua_rawgeti(L, loaderTable, e - 1);
                LuaDLL.lua_rawseti(L, loaderTable, e);
            }
            LuaDLL.lua_pushvalue(L, loaderFunc);
            LuaDLL.lua_rawseti(L, loaderTable, 1);
            LuaDLL.lua_settop(L, 0);
            return(0);
        }
Esempio n. 3
0
        public LuaState()
        {
            mainThread = System.Threading.Thread.CurrentThread.ManagedThreadId;

            L           = LuaDLL.luaL_newstate();
            statemap[L] = this;
            if (main == null)
            {
                main = this;
            }

            refQueue = new Queue <UnrefPair>();

            LuaDLL.luaL_openlibs(L);

            ObjectCache.make(L);

            LuaDLL.lua_pushlightuserdata(L, L);
            LuaDLL.lua_setglobal(L, "__main_state");

            LuaDLL.lua_pushcfunction(L, print);
            LuaDLL.lua_setglobal(L, "print");

            LuaDLL.lua_pushcfunction(L, pcall);
            LuaDLL.lua_setglobal(L, "pcall");

            LuaDLL.lua_pushcfunction(L, import);
            LuaDLL.lua_setglobal(L, "import");

            string resumefunc = @"
local resume = coroutine.resume
coroutine.resume=function(co,...)
	local ret={resume(co,...)}
	if not ret[1] then UnityEngine.Debug.LogError(debug.traceback(co,ret[2])) end
	return unpack(ret)
end
";

            // overload resume function for report error
            if (LuaDLL.lua_dostring(L, resumefunc) != 0)
            {
                LuaObject.throwLuaError(L);
            }

            LuaDLL.lua_pushcfunction(L, dofile);
            LuaDLL.lua_setglobal(L, "dofile");

            LuaDLL.lua_pushcfunction(L, loadfile);
            LuaDLL.lua_setglobal(L, "loadfile");

            LuaDLL.lua_pushcfunction(L, loader);
            int loaderFunc = LuaDLL.lua_gettop(L);

            LuaDLL.lua_getglobal(L, "package");
#if LUA_5_3
            LuaDLL.lua_getfield(L, -1, "searchers");
#else
            LuaDLL.lua_getfield(L, -1, "loaders");
#endif
            int loaderTable = LuaDLL.lua_gettop(L);

            // Shift table elements right
            for (int e = LuaDLL.lua_rawlen(L, loaderTable) + 1; e > 1; e--)
            {
                LuaDLL.lua_rawgeti(L, loaderTable, e - 1);
                LuaDLL.lua_rawseti(L, loaderTable, e);
            }
            LuaDLL.lua_pushvalue(L, loaderFunc);
            LuaDLL.lua_rawseti(L, loaderTable, 1);
            LuaDLL.lua_settop(L, 0);
        }