예제 #1
0
        public async Task <IExecuteResult> Execute()
        {
            if (Lua == default(Lua))
            {
                await Compile();
            }
            Writer?.Dispose();
            Writer = new StringWriter();
            if (Globals != null)
            {
                Globals.Console?.Dispose();
                Globals.Console = Writer;
            }

            var result = await ExecuteThreaded <object[]> .Execute(() =>
                                                                   Lua.DoString(SourceCode), (int)ExecuteProperties.Timeout);

            if (result.Exception is LuaScriptException scriptEx)
            {
                var number = new Regex("[0-9]+");
                var match  = number.Match(scriptEx.Message);
                if (match.Success)
                {
                    int num = int.Parse(match.Value);
                    List <IDiagnostic> diagnostics =
                        new List <IDiagnostic> {
                        new LuaDiagnostic(scriptEx.Message, num, Severity.Error)
                    };
                    CompileResult = new LuaCompileResult(result.ElapsedMilliseconds, SourceCode, diagnostics,
                                                         diagnostics,
                                                         new List <Exception> {
                        scriptEx
                    });
                }
            }
            else
            {
                CompileResult = new LuaCompileResult(0, SourceCode, null, null, null);
            }

            IExecuteResult executeResult =
                new LuaExecuteResult(
                    result.ElapsedMilliseconds,
                    Writer.ToString(),
                    result.ReturnValue,
                    CompileResult,
                    result.Exception);

            ExecuteResult = executeResult;
            return(executeResult);
        }
예제 #2
0
        /// <summary>
        ///     Initialize Lua Script State (this function does not compile, LUA is a scripting language)
        /// </summary>
        /// <returns></returns>
        public Task <ICompileResult> Compile()
        {
            if (Lua == default(Lua))
            {
                Lua = new Lua();
                Lua.RegisterFunction("print", this, typeof(LuaCompiler).GetMethod("Print")); //console out
                //Initialize Globals
                try {
                    if (Globals != null)
                    {
                        foreach (var property in Globals.GetType().GetProperties())
                        {
                            Lua[property.Name] = property.GetValue(Globals);
                        }
                    }
                } catch {
                    //reflection can cause many exceptions
                }
            }
            ICompileResult compileResult = new LuaCompileResult(0, SourceCode, null, null, null);

            CompileResult = compileResult;
            return(Task.FromResult(compileResult));
        }