Пример #1
0
        static int TestFunc(IntPtr lua_state)
        {
            ILua lua = GmodInterop.GetLuaFromState(lua_state);

            lua.Pop(lua.Top());

            lua.PushString(random);

            return(1);
        }
Пример #2
0
        static int TestFunc(IntPtr lua_state)
        {
            ILua lua = GmodInterop.GetLuaFromState(lua_state);

            lua.PushNumber(random_number);

            lua.PushString(random_string);

            return(2);
        }
Пример #3
0
        public Task <bool> Start(ILua lua, GetILuaFromLuaStatePointer lua_extructor, ModuleAssemblyLoadContext assembly_context)
        {
            TaskCompletionSource <bool> taskCompletion = new TaskCompletionSource <bool>();

            try
            {
                int stack_state = lua.Top();

                lua.PushString(random1);
                lua.PushString(random2);
                lua.PushManagedClosure(lua =>
                {
                    if (lua.Top() != 1)
                    {
                        throw new Exception("Managed closure execution stack has incorrect number of items");
                    }

                    double num = lua.GetNumber(1);

                    lua.Pop(1);

                    string first  = lua.GetString(GmodInterop.GetUpvalueIndex(1));
                    string second = lua.GetString(GmodInterop.GetUpvalueIndex(2));

                    lua.PushString(first + num + second);

                    return(1);
                }, 2);

                if (lua.Top() != stack_state + 1)
                {
                    throw new Exception("Wrong number of items left on the stack");
                }

                lua.PushNumber(random3);

                lua.MCall(1, 1);

                string ret = lua.GetString(-1);
                lua.Pop(1);

                if (ret != random1 + random3 + random2)
                {
                    throw new Exception("Return string is incorrect");
                }

                taskCompletion.TrySetResult(true);
            }
            catch (Exception e)
            {
                taskCompletion.TrySetException(new Exception[] { e });
            }

            return(taskCompletion.Task);
        }
        static int ClosureTestFunc(IntPtr lua_state)
        {
            ILua lua = GmodInterop.GetLuaFromState(lua_state);

            lua.Pop(lua.Top());

            string upvalue = lua.GetString(GmodInterop.GetUpvalueIndex(1, false));

            lua.PushString(upvalue + string_to_add);

            return(1);
        }
        internal static int ManagedDelegateGC(IntPtr lua_state)
        {
            ILua lua = GmodInterop.GetLuaFromState(lua_state);

            lua.PushSpecial(SPECIAL_TABLES.SPECIAL_GLOB);
            lua.GetField(-1, ManagedFunctionIdField);
            int managed_delegate_type_id = (int)lua.GetNumber(-1);

            lua.Pop(2);

            IntPtr managed_delegate_handle = lua.GetUserType(1, managed_delegate_type_id);

            GCHandle.FromIntPtr(managed_delegate_handle).Free();

            return(0);
        }
        public Task <bool> Start(ILua lua, GetILuaFromLuaStatePointer lua_extructor, ModuleAssemblyLoadContext assembly_context)
        {
            TaskCompletionSource <bool> taskCompletion = new TaskCompletionSource <bool>();

            try
            {
                int lua_state = lua.Top();

                lua.PushString(random);
                lua.PushManagedClosure(lua =>
                {
                    if (lua.Top() != 0)
                    {
                        throw new Exception("Managed closure execution stack is non-empty");
                    }

                    string upvalue = lua.GetString(GmodInterop.GetUpvalueIndex(1));

                    lua.PushString(upvalue + upvalue);

                    return(1);
                }, 1);

                if (lua.Top() != lua_state + 1)
                {
                    throw new Exception("There is incorrect number of items on the Lua stack");
                }

                lua.MCall(0, 1);

                string ret = lua.GetString(-1);
                lua.Pop(1);

                if (ret != random + random)
                {
                    throw new Exception("Return string is incorrect");
                }

                taskCompletion.TrySetResult(true);
            }
            catch (Exception e)
            {
                taskCompletion.TrySetException(new Exception[] { e });
            }

            return(taskCompletion.Task);
        }
        internal static int ManagedDelegateExecutor(IntPtr lua_state)
        {
            ILua lua = GmodInterop.GetLuaFromState(lua_state);

            try
            {
                lua.PushSpecial(SPECIAL_TABLES.SPECIAL_GLOB);
                lua.GetField(-1, ManagedFunctionIdField);
                int managed_delegate_type_id = (int)lua.GetNumber(-1);
                lua.Pop(2);

                IntPtr managed_delegate_handle = lua.GetUserType(GmodInterop.GetUpvalueIndex(1, false), managed_delegate_type_id);

                Func <ILua, int> managed_delegate = (Func <ILua, int>)GCHandle.FromIntPtr(managed_delegate_handle).Target;

                return(Math.Max(0, managed_delegate(lua)));
            }
            catch (Exception e)
            {
                lua.Pop(lua.Top());
                lua.PushString(".NET Exception: " + e.ToString());
                return(-1);
            }
        }