示例#1
0
        protected static void AddMember(IntPtr ptr, string name, LuaCSFunction get, LuaCSFunction set, bool instance)
        {
            CheckMethodValid(get);
            CheckMethodValid(set);

            int t = instance ? -2 : -3;

            LuaNativeMethods.lua_createtable(ptr, 2, 0);
            if (get == null)
            {
                LuaNativeMethods.lua_pushnil(ptr);
            }
            else
            {
                PushValue(ptr, get);
            }

            LuaNativeMethods.lua_rawseti(ptr, -2, 1);

            if (set == null)
            {
                LuaNativeMethods.lua_pushnil(ptr);
            }
            else
            {
                PushValue(ptr, set);
            }

            LuaNativeMethods.lua_rawseti(ptr, -2, 2);

            LuaNativeMethods.lua_setfield(ptr, t, name);
        }
示例#2
0
        public static int ToTable(IntPtr ptr, Array o)
        {
            if (o == null)
            {
                LuaNativeMethods.lua_pushnil(ptr);
                return(1);
            }

            LuaNativeMethods.lua_createtable(ptr, o.Length, 0);
            for (int n = 0; n < o.Length; n++)
            {
                LuaObject.PushVar(ptr, o.GetValue(n));
                LuaNativeMethods.lua_rawseti(ptr, -2, n + 1);
            }

            return(1);
        }
示例#3
0
 public static new void Init(IntPtr ptr)
 {
     propertyMethods["Table"]  = ToTable;
     propertyMethods["Length"] = Length;
     LuaNativeMethods.lua_createtable(ptr, 0, 5);
     LuaObject.PushValue(ptr, LuaIndex);
     LuaNativeMethods.lua_setfield(ptr, -2, "__index");
     LuaObject.PushValue(ptr, LuaNewIndex);
     LuaNativeMethods.lua_setfield(ptr, -2, "__newindex");
     LuaNativeMethods.lua_pushcfunction(ptr, LuaObject.LuaGC);
     LuaNativeMethods.lua_setfield(ptr, -2, "__gc");
     LuaNativeMethods.lua_pushcfunction(ptr, ToString);
     LuaNativeMethods.lua_setfield(ptr, -2, "__tostring");
     LuaNativeMethods.lua_pushcfunction(ptr, Len);
     LuaNativeMethods.lua_setfield(ptr, -2, "__len");
     LuaNativeMethods.lua_setfield(ptr, LuaIndexes.LUARegistryIndex, "LuaArray");
 }
示例#4
0
        public static void NewTypeTable(IntPtr ptr, string name)
        {
            string[] subt = name.Split('.');

            LuaNativeMethods.lua_pushglobaltable(ptr);

            foreach (string t in subt)
            {
                LuaNativeMethods.lua_pushstring(ptr, t);
                LuaNativeMethods.lua_rawget(ptr, -2);
                if (LuaNativeMethods.lua_isnil(ptr, -1))
                {
                    LuaNativeMethods.lua_pop(ptr, 1);
                    LuaNativeMethods.lua_createtable(ptr, 0, 0);
                    LuaNativeMethods.lua_pushstring(ptr, t);
                    LuaNativeMethods.lua_pushvalue(ptr, -2);
                    LuaNativeMethods.lua_rawset(ptr, -4);
                }

                LuaNativeMethods.lua_remove(ptr, -2);
            }
        }
示例#5
0
 public static void lua_newtable(IntPtr luaState)
 {
     LuaNativeMethods.lua_createtable(luaState, 0, 0);
 }
示例#6
0
        public static void Init(IntPtr ptr)
        {
            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..' isthis.Write only')
            end
        end
        t = rawget(t,'__parent')
    until t==nil
    error('Can not find '..k)
end

return index
";
            LuaState state    = LuaState.Get(ptr);

            newIndexFunction = (LuaFunction)state.DoString(newindexfun);
            indexFunction    = (LuaFunction)state.DoString(indexfun);

            // object method
            LuaNativeMethods.lua_createtable(ptr, 0, 4);
            AddMember(ptr, ToString);
            AddMember(ptr, GetHashCode);
            AddMember(ptr, Equals);
            AddMember(ptr, GetType);
            LuaNativeMethods.lua_setfield(ptr, LuaIndexes.LUARegistryIndex, "__luabaseobject");

            LuaArray.Init(ptr);
            LuaVarObject.Init(ptr);

            LuaNativeMethods.lua_newtable(ptr);
            LuaNativeMethods.lua_setglobal(ptr, DelgateTable);
        }