Esempio n. 1
0
        private object LuaLoad(object ld, string source, string mode, LuaTable env)
        {
            if (String.IsNullOrEmpty(source))
            {
                source = "=(load)";
            }

            if (mode == "b" || !(ld is string || ld is LuaMethod || ld is Delegate))             // binary chunks are not implementeted
            {
                throw new NotImplementedException();
            }

            try
            {
                // collect the chunks
                if (Lua.RtInvokeable(ld))
                {
                    StringBuilder sbCode = new StringBuilder();
                    string        sPart;
                    while (!String.IsNullOrEmpty(sPart = (string)new LuaResult(RtInvokeSite(ld))[0]))
                    {
                        sbCode.Append(sPart);
                    }
                    ld = sbCode.ToString();
                }
                // create the chunk
                return(LuaLoadReturn(Lua.CompileChunk((string)ld, source, null), env));
            }
            catch (Exception e)
            {
                return(new LuaResult(null, e.Message));
            }
        }         // func LuaLoad
Esempio n. 2
0
        private object LuaLoadFile(string filename, string mode, LuaTable env)
        {
            if (mode == "b")             // binary chunks are not implementeted
            {
                throw new NotImplementedException();
            }

            // create the chunk
            return(LuaLoadReturn(Lua.CompileChunk(filename, null), env));
        }         // func LuaLoadFile
Esempio n. 3
0
        private object LuaLoadFile(string filename, string mode, LuaTable env)
        {
            if (mode == "b")             // binary chunks are not implementeted
            {
                throw new NotImplementedException();
            }

            // create the chunk
            return(LuaLoadReturn(Lua.CompileChunk(filename, null, new KeyValuePair <string, Type>("...", typeof(object[]))), env));
        }         // func LuaLoadFile
Esempio n. 4
0
        /// <summary>
        /// Runs a block of Lua code from a string.
        /// </summary>
        /// <param name="block">The code to run.</param>
        /// <param name="env">The Lua environment to run it in.</param>
        /// <returns>The script's return value, or False if an error occurred.</returns>
        public static LuaResult Run(string block, LuaGlobal env = null)
        {
            if (env == null)
            {
                env = Environment;
            }

            // Do we have this chunk cached? If so, use that version.
            var      hash          = block.GetHashCode();
            var      useCache      = false;
            LuaChunk compiledChunk = null;

            if (LuaCache.ContainsKey(hash))
            {
                compiledChunk = LuaCache[hash];
                useCache      = true;
            }
            else
            {
                // Attempt to compile and add it to the cache.
                try
                {
                    compiledChunk = IronLua.CompileChunk(block, "lol.lua", null);
                    useCache      = true;
                    LuaCache.Add(hash, compiledChunk);
                }
                catch (LuaException pax)
                {
                    //Wrap it up in a normal Exception that our custom handler can then unwrap, so we can show the context in a nice way.
                    throw new Exception("Lua parse error while trying to run this chunk:" + System.Environment.NewLine + PrepareParseError(block, pax.Line, pax.Column) + System.Environment.NewLine + pax.Message, pax);
                }
            }

            // TODO: It failed to compile? Run interpreted and hope for useful information
            LuaResult ret = null;

            try
            {
                if (useCache)
                {
                    ret = env.DoChunk(compiledChunk);
                }
                else
                {
                    ret = env.DoChunk(block, "lol.lua");
                }
            }
            catch (LuaException pax)
            {
                //Wrap it up in a normal Exception that our custom handler can then unwrap, so we can show the context in a nice way.
                throw new Exception("Lua parse error while trying to run this chunk:" + System.Environment.NewLine + PrepareParseError(block, pax.Line, pax.Column) + System.Environment.NewLine + pax.Message, pax);
            }
            return(ret);
        }
Esempio n. 5
0
        }         // func DoChunk

        private LuaResult DoChunk(string sChunkName, TextReader tr, KeyValuePair <string, object>[] args)
        {
            // Erzeuge die Parameter
            object[] callArgs;
            KeyValuePair <string, Type>[] callTypes;
            if (args != null)
            {
                callArgs  = new object[args.Length];
                callTypes = new KeyValuePair <string, Type> [args.Length];
                for (int i = 0; i < args.Length; i++)
                {
                    callArgs[i]  = args[i].Value;
                    callTypes[i] = new KeyValuePair <string, Type>(args[i].Key, args[i].Value == null ? typeof(object) : args[i].Value.GetType());
                }
            }
            else
            {
                callArgs  = new object[0];
                callTypes = new KeyValuePair <string, Type> [0];
            }

            // Führe den Block aus
            return(DoChunk(lua.CompileChunk(sChunkName, null, tr, callTypes), callArgs));
        }         // func DoChunk
Esempio n. 6
0
        public bool Initialize(string taskName, IDictionary <string, TaskPropertyInfo> parameterGroup, string taskBody, IBuildEngine taskFactoryLoggingHost)
        {
            TaskLoggingHelper log = new TaskLoggingHelper(taskFactoryLoggingHost, taskName);

            // We use the property group for the declaration
            taskProperties = (from c in parameterGroup select c.Value).ToArray();

            // Compile chunk
            try
            {
                log.LogMessage("Compile script.");
                task = lua.CompileChunk(taskBody, taskName, Lua.DefaultDebugEngine,
                                        new KeyValuePair <string, Type>("engine", typeof(IBuildEngine)),
                                        new KeyValuePair <string, Type>("log", typeof(TaskLoggingHelper))
                                        );

                return(true);
            }
            catch (LuaParseException e)
            {
                log.LogError("{0} (at line {1},{2})", e.Message, taskName, e.Line);
                return(false);
            }
        } // func Initialize
Esempio n. 7
0
        }         // func DoChunk

        /// <summary>Compiles and execute the stream.</summary>
        /// <param name="tr">Stream</param>
        /// <param name="name">Name of the stream</param>
        /// <param name="args">Parameter definition for the stream.</param>
        /// <returns>Return values of the stream.</returns>
        public LuaResult DoChunk(TextReader tr, string name, params KeyValuePair <string, object>[] args)
        {
            // Erzeuge die Parameter
            object[] callArgs;
            KeyValuePair <string, Type>[] callTypes;
            if (args != null)
            {
                callArgs  = new object[args.Length];
                callTypes = new KeyValuePair <string, Type> [args.Length];
                for (var i = 0; i < args.Length; i++)
                {
                    callArgs[i]  = args[i].Value;
                    callTypes[i] = new KeyValuePair <string, Type>(args[i].Key, args[i].Value == null ? typeof(object) : args[i].Value.GetType());
                }
            }
            else
            {
                callArgs  = new object[0];
                callTypes = new KeyValuePair <string, Type> [0];
            }

            // Führe den Block aus
            return(DoChunk(lua.CompileChunk(tr, name, null, callTypes), callArgs));
        }         // proc DoChunk
Esempio n. 8
0
        }         // func ParseArgument

        private static void RunScript(Func <string> code, string sName)
        {
            try
            {
                Stopwatch sw = new Stopwatch();
                sw.Start();

                // compile chunk
                LuaChunk c = lua.CompileChunk(code(), sName, new LuaCompileOptions()
                {
                    DebugEngine = debugEngine
                });

                string sCompileTime = String.Format("{0:N0} ms", sw.ElapsedMilliseconds);
                sw.Reset();
                sw.Start();

                // run chunk
                LuaResult r        = global.DoChunk(c);
                string    sRunTime = String.Format("{0:N0} ms", sw.ElapsedMilliseconds);

                string sSize;
                if (c.Size < 0)
                {
                    sSize = "unknown";
                }
                else if (c.Size == 0)
                {
                    sSize = String.Empty;
                }
                else
                {
                    sSize = c.Size.ToString("N0") + " byte";
                }

                // start with a new line
                if (Console.CursorLeft > 0)
                {
                    Console.WriteLine();
                }

                // print result
                if (r.Count > 0)
                {
                    for (int i = 0; i < r.Count; i++)
                    {
                        WriteVariable(i, r[i]);
                    }
                }

                // print summary
                const string csCompile = "==> compile: ";
                const string csRuntime = " run: ";

                Console.CursorLeft = Console.WindowWidth - csCompile.Length - (sSize.Length > 0 ? sSize.Length + 3 : 0) - sCompileTime.Length - csRuntime.Length - sRunTime.Length - 1;
                WriteText(ConsoleColor.DarkGreen, csCompile);
                WriteText(ConsoleColor.Green, sCompileTime);
                if (sSize.Length > 0)
                {
                    WriteText(ConsoleColor.DarkGreen, " [");
                    WriteText(ConsoleColor.Green, sSize);
                    WriteText(ConsoleColor.DarkGreen, "]");
                }
                WriteText(ConsoleColor.DarkGreen, csRuntime);
                WriteText(ConsoleColor.Green, sRunTime);
                Console.WriteLine();
            }
            catch (LuaParseException e)
            {
                WriteText(ConsoleColor.DarkRed, String.Format("Parse error at line {0:N0} (column: {1:N0}):", e.Line, e.Column));
                Console.WriteLine();
                WriteText(ConsoleColor.DarkRed, "  " + e.Message);
                Console.WriteLine();
            }
            catch (Exception e)
            {
                Exception ex = e is TargetInvocationException ? e.InnerException : e;
                WriteException(ex);
            }
        } // proc RunScript
Esempio n. 9
0
 /// <summary>Erzeugt ein Delegate aus dem Code, ohne ihn auszuführen.</summary>
 /// <param name="lua"></param>
 /// <param name="sFileName">Dateiname die gelesen werden soll.</param>
 /// <param name="options">Options for the compile process.</param>
 /// <param name="args">Parameter für den Codeblock</param>
 /// <returns>Compiled chunk.</returns>
 public static LuaChunk CompileChunk(this Lua lua, string sFileName, LuaCompileOptions options, params KeyValuePair <string, Type>[] args)
 {
     using (StreamReader sr = new StreamReader(sFileName))
         return(lua.CompileChunk(sr, Path.GetFileName(sFileName), options, args));
 }         // func CompileChunk