コード例 #1
0
ファイル: Test.cs プロジェクト: ModTheGungeon/MicroLua
        public void LuaCLRFunctionUsage()
        {
            using (var lua = new LuaState()) {
                lua.EnterArea();

                lua.PushLuaCLRFunction(TestFunction);
                lua.SetGlobal("test");

                lua.BeginProtCall();
                lua.LoadString("return test('not hello', 0, true)");
                try {
                    lua.ExecProtCall(0);
                } catch (Exception ex) {
                    Assert.NotNull(ex);
                    Assert.IsInstanceOf(typeof(LuaException), ex);
                    Assert.NotNull(ex.InnerException);
                    Assert.IsInstanceOf(typeof(InvalidOperationException), ex.InnerException);
                    Assert.AreEqual("Wrong password!", ex.InnerException.Message);
                }

                lua.BeginProtCall();
                lua.LoadString("return test('hello', 1000, false)");
                lua.ExecProtCall(0);

                var obj = lua.ToCLR();
                lua.Pop();

                Assert.AreEqual("Hello, world!", obj);

                lua.LeaveArea();
            }
        }
コード例 #2
0
ファイル: Test.cs プロジェクト: ModTheGungeon/MicroLua
        public void Generics()
        {
            using (var lua = new LuaState()) {
                lua.EnterArea();
                lua.LoadInteropLibrary();

                lua.PushCLR(new Helper());
                lua.SetGlobal("helper");

                lua.BeginProtCall();
                lua.LoadString(@"
                    GENERIC = interop.generic
                    Assembly_MicroLua_Tests = interop.assembly('MicroLua.Tests')
                    MicroLua = { Tests = {
                        Helper = interop.type(Assembly_MicroLua_Tests, 'MicroLua.Tests.Helper')
                    } }
                "); // some small shortcuts
                lua.ExecProtCall(0);

                try {
                    lua.BeginProtCall();
                    lua.LoadString(@"
                        return helper:GenericMethod(
                            interop.generic(MicroLua.Tests.Helper, MicroLua.Tests.Helper),
                            1, 2
                        )
                    ");
                    lua.ExecProtCall(0);
                } catch (LuaException e) {
                    Assert.AreEqual("Incorrect length of generic parameters for method GenericMethod.", e.InnerException?.Message);
                }

                lua.BeginProtCall();
                lua.LoadString(@"
                    return helper:GenericMethod(
                        interop.generic(MicroLua.Tests.Helper),
                        1, 2
                    )
                ");
                lua.ExecProtCall(0, cleanup: true);
                var result = lua.ToCLR();
                lua.Pop();
                Assert.AreEqual("MicroLua.Tests.Helper | 1 + 2 = 3", result);

                lua.BeginProtCall();
                lua.LoadString(@"
                    return helper:GenericMethod(
                        1, 2
                    )
                ");
                lua.ExecProtCall(0);
                var nongeneric_result = lua.ToCLR();
                lua.Pop();
                Assert.AreEqual("1 + 2 = 3", nongeneric_result);

                lua.LeaveArea();
            }
        }
コード例 #3
0
ファイル: Hooks.cs プロジェクト: shaisle/ETGMod.Reloaded
        internal object TryRun(LuaState lua, long token, object target, object[] args, out bool returned)
        {
            _Logger.Debug($"Trying to run method hook (token {token})");
            lua.EnterArea();
            returned = false;

            object return_value = null;

            // TODO TODO TODO
            // CONTINUE THIS

            int fun_ref;

            if (Hooks.TryGetValue(token, out fun_ref))
            {
                _Logger.Debug($"Hook found");
                // target == null --> static

                var objs_offs = 1;
                if (target == null)
                {
                    objs_offs = 0;
                }

                lua.BeginProtCall();
                lua.PushLuaReference(fun_ref);
                var lua_args_len = args.Length;
                if (target != null)
                {
                    lua.PushCLR(target);
                    lua_args_len += 1;
                }
                for (int i = 0; i < args.Length; i++)
                {
                    lua.PushCLR(args[i]);
                }
                var top = lua.StackTop;
                lua.ExecProtCall(lua_args_len);
                var results_len = lua.StackTop - top;

                Type return_type;
                if (HookReturns.TryGetValue(token, out return_type))
                {
                    if (results_len > 0)
                    {
                        returned     = true;
                        return_value = lua.ToCLR();

                        lua.Pop(results_len);
                    }
                }
            }
            else
            {
                _Logger.Debug($"Hook not found");
            }

            lua.LeaveArea();
            return(return_value);
        }
コード例 #4
0
        private int _CreateNewEnvironment(ModInfo info)
        {
            LuaState.EnterArea();

            LuaState.PushCLR(info);
            LuaState.SetGlobal("MOD");

            LuaState.GetGlobal("package");
            LuaState.GetField("path");
            var prev_path = LuaState.ToString();

            LuaState.Pop();
            LuaState.PushString(Path.Combine(Paths.ResourcesFolder, "lua/?.lua"));
            LuaState.SetField("path");
            LuaState.Pop();

            LuaState.BeginProtCall();
            LuaState.LoadFile(Path.Combine(Paths.ResourcesFolder, "lua/env.lua"));
            LuaState.ExecProtCall(0, cleanup: true);

            var env_ref = LuaState.MakeLuaReference();

            LuaState.Pop();

            LuaState.GetGlobal("package");
            LuaState.PushString(prev_path);
            LuaState.SetField("path");
            LuaState.Pop();

            LuaState.PushNil();
            LuaState.SetGlobal("MOD");

            LuaState.LeaveArea();
            return(env_ref);
        }
コード例 #5
0
ファイル: Test.cs プロジェクト: ModTheGungeon/MicroLua
        public void Environments()
        {
            using (var lua = new LuaState()) {
                lua.EnterArea();

                lua.BeginProtCall();
                lua.LoadString("return hello");
                var func_ref = lua.MakeLuaReference();

                lua.PushNewTable();
                lua.PushString("Hello, world!");
                lua.SetField("hello");
                var env_ref = lua.MakeLuaReference();

                lua.SetEnvironment();

                lua.ExecProtCall(0);
                var val = lua.ToCLR();
                lua.Pop();
                Assert.AreEqual("Hello, world!", val);

                lua.PushLuaReference(func_ref);
                lua.GetEnvironment();
                lua.PushLuaReference(env_ref);
                Assert.IsTrue(lua.AreEqual(-1, -2));
                lua.Pop(3);

                lua.LeaveArea();
            }
        }
コード例 #6
0
        private void _SetupSandbox(int env_ref)
        {
            LuaState.EnterArea();

            LuaState.BeginProtCall();
            LuaState.BeginProtCall();
            LuaState.LoadFile(Path.Combine(Paths.ResourcesFolder, "lua/sandbox.lua"));
            LuaState.ExecProtCall(0, cleanup: true);
            LuaState.PushLuaReference(env_ref);
            LuaState.ExecProtCall(1, cleanup: true);

            LuaState.LeaveAreaCleanup();
        }
コード例 #7
0
ファイル: Test.cs プロジェクト: ModTheGungeon/MicroLua
        public void StaticType()
        {
            using (var lua = new LuaState()) {
                lua.EnterArea();

                lua.PushType(typeof(Helper));
                lua.SetGlobal("Helper");

                lua.BeginProtCall();
                lua.LoadString("return Helper.Overload('hello')");
                lua.ExecProtCall(0);
                Assert.AreEqual("hello", lua.ToString());
                lua.Pop();

                lua.LoadString("return Helper.StaticTestField");
                lua.ProtCall(0);
                var test_field = lua.ToCLR();
                Assert.AreEqual(Helper.StaticTestField, test_field);
                lua.Pop();

                lua.LoadString("return Helper.StaticTestProp");
                lua.ProtCall(0);
                var test_prop = lua.ToCLR();
                Assert.AreEqual(Helper.StaticTestProp, test_prop);
                lua.Pop();

                lua.LoadString("Helper.StaticTestField = 'hacked'");
                lua.VoidProtCall(0);
                Assert.AreEqual("hacked", Helper.StaticTestField);

                lua.LoadString("Helper.StaticTestProp = 'hacked'");
                lua.VoidProtCall(0);
                Assert.AreEqual("hacked", Helper.StaticTestProp);

                lua.LoadString("return Helper('ctor').TestField");
                lua.ProtCall(0);
                var val = lua.ToCLR();
                Assert.AreEqual("ctor", val);
                lua.Pop();

                lua.LoadString("return Helper(42).TestField");
                lua.ProtCall(0);
                var val2 = lua.ToCLR();
                Assert.AreEqual("42", val2);
                lua.Pop();

                lua.LeaveArea();
            }
        }
コード例 #8
0
ファイル: Test.cs プロジェクト: ModTheGungeon/MicroLua
        public void Traceback()
        {
            using (var lua = new LuaState()) {
                lua.EnterArea();

                var helper = new Helper();
                lua.PushCLR(helper);
                lua.SetGlobal("test");

                lua.BeginProtCall();
                lua.BeginProtCall();
                lua.LoadString(@"
                    function testf()
                        test:CauseException()
                    end
                    return testf
                ");
                lua.ExecProtCall(0);
                try { lua.ExecProtCall(0); } catch (LuaException ex) {
                    Assert.NotNull(ex);
                    Assert.NotNull(ex.InnerException);
                    Assert.NotNull(ex.InnerException.InnerException);
                    Assert.IsInstanceOf(typeof(TargetInvocationException), ex.InnerException);
                    Assert.IsInstanceOf(typeof(InvalidOperationException), ex.InnerException.InnerException);
                    Assert.AreEqual("Hello", ex.InnerException.InnerException.Message);

                    Assert.AreEqual("[C]: ?", ex.TracebackArray[0]);
                    Assert.AreEqual("[C]: in function '_error'", ex.TracebackArray[1]);
                    Assert.AreEqual("[string \"MicroLua\"]:25: in function <[string \"MicroLua\"]:21>", ex.TracebackArray[2]);
                    Assert.AreEqual("(tail call): ?", ex.TracebackArray[3]);
                    Assert.AreEqual("[string \"...\"]:3: in function <[string \"...\"]:2>", ex.TracebackArray[4]);
                }

                lua.LeaveArea();
            }
        }
コード例 #9
0
ファイル: Test.cs プロジェクト: ModTheGungeon/MicroLua
        public void StaticType2()
        {
            using (var lua = new LuaState()) {
                lua.EnterArea();
                lua.LoadInteropLibrary();

                lua.BeginProtCall();
                lua.LoadString(@"
                    local asm = interop.assembly('MicroLua.Tests')
                    local test = interop.namespace(asm, 'MicroLua.Tests.TestNamespace')
                    local c = test.C()
                    test.B.AInstance:DoThing(c)
                ");
                lua.ExecProtCall(0);

                lua.LeaveArea();
            }
        }