Esempio n. 1
0
        public static int Add(IntPtr ptr)
        {
            try
            {
                int top = LuaNativeMethods.lua_gettop(ptr);
                if (top == 2)
                {
                    int delay;
                    CheckType(ptr, 1, out delay);
                    LuaDelegate luaDelegate;
                    CheckType(ptr, 2, out luaDelegate);
                    Action <int> ua;
                    if (luaDelegate.Delegate != null)
                    {
                        ua = (Action <int>)luaDelegate.Delegate;
                    }
                    else
                    {
                        IntPtr ml = LuaState.Get(ptr).StatePointer;
                        ua = (int id) =>
                        {
                            int error = PushTry(ml);
                            LuaObject.PushValue(ml, id);
                            luaDelegate.ProtectedCall(1, error);
                            LuaNativeMethods.lua_settop(ml, error - 1);
                        };
                    }

                    luaDelegate.Delegate = ua;
                    LuaObject.PushValue(ptr, true);
                    LuaObject.PushValue(ptr, Add(delay, ua));
                    return(2);
                }
                else if (top == 3)
                {
                    int delay, cycle;
                    CheckType(ptr, 1, out delay);
                    CheckType(ptr, 2, out cycle);
                    LuaDelegate luaDelegate;
                    CheckType(ptr, 3, out luaDelegate);
                    Func <int, bool> ua;

                    if (luaDelegate.Delegate != null)
                    {
                        ua = (Func <int, bool>)luaDelegate.Delegate;
                    }
                    else
                    {
                        IntPtr ml = LuaState.Get(ptr).StatePointer;
                        ua = (int id) =>
                        {
                            int error = PushTry(ml);
                            LuaObject.PushValue(ml, id);
                            luaDelegate.ProtectedCall(1, error);
                            bool ret = LuaNativeMethods.lua_toboolean(ml, -1);
                            LuaNativeMethods.lua_settop(ml, error - 1);
                            return(ret);
                        };
                    }

                    luaDelegate.Delegate = ua;
                    LuaObject.PushValue(ptr, true);
                    LuaObject.PushValue(ptr, Add(delay, cycle, ua));
                    return(2);
                }

                return(Error(ptr, "Argument error"));
            }
            catch (Exception e)
            {
                return(Error(ptr, e));
            }
        }
Esempio n. 2
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);
        }