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, LuaDeskop.StackTraceCompileOptions, 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
} // ctor public void Dispose() { chunk = null; global = null; } // proc Dispose
public LuaTask(LuaChunk chunk) { this.chunk = chunk; this.global = new LuaGlobal(LuaTaskFactory.Lua); } // ctor
} // func RegisterUniqueName internal static void RegisterMethod(string sUniqueName, LuaChunk chunk) { lock (registeredChunks) registeredChunks[sUniqueName] = new WeakReference(chunk); } // proc RegsiterMethod
} // class LuaLoadReturnClosure #endregion private object LuaLoadReturn(LuaChunk c, LuaTable defaultEnv) { var run = new LuaLoadReturnClosure(); run.env = defaultEnv; run.chunk = c; run.@this = this; return new Func<LuaTable, LuaResult>(run.Run); } // func LuaLoadReturn
} // 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
} // proc DoChunk /// <summary>Executes a precompiled chunk on the lua environment.</summary> /// <param name="chunk">Compiled chunk.</param> /// <param name="callArgs">Arguments for the chunk.</param> /// <returns>Return values of the chunk.</returns> public LuaResult DoChunk(LuaChunk chunk, params object[] callArgs) { if (chunk == null) throw new ArgumentException(Properties.Resources.rsChunkNotCompiled, "chunk"); if (lua != chunk.Lua) throw new ArgumentException(Properties.Resources.rsChunkWrongScriptManager, "chunk"); return chunk.Run(this, callArgs); } // func DoChunk