private void DebugScript(string filename) { m_Script = new Script(CoreModules.Preset_HardSandbox); m_Script.Options.UseLuaErrorLocations = true; m_Script.Options.DebugPrint = s => { Console_WriteLine("{0}", s); }; ((ScriptLoaderBase)m_Script.Options.ScriptLoader).ModulePaths = ScriptLoaderBase.UnpackStringPaths("Modules/?;Modules/?.lua"); try { m_Script.LoadFile(filename, null, filename.Replace(':', '|')); } catch (Exception ex) { txtOutput.Text = ""; Console_WriteLine("{0}", ex.Message); return; } m_Script.AttachDebugger(this); Thread m_Debugger = new Thread(DebugMain); m_Debugger.Name = "MoonSharp Execution Thread"; m_Debugger.IsBackground = true; m_Debugger.Start(); }
public void Execute(ShellContext context, string p) { string targetFileName = p + "-compiled"; Script S = new Script(CoreModules.None); DynValue chunk = S.LoadFile(p); using (Stream stream = new FileStream(targetFileName, FileMode.Create, FileAccess.Write)) S.Dump(chunk, stream); }
private static void ParseCommand(Script S, string p) { if (p == "help") { Console.WriteLine("Type Lua code to execute Lua code, multilines are accepted, "); Console.WriteLine("or type one of the following commands to execute them."); Console.WriteLine(""); Console.WriteLine("Commands:"); Console.WriteLine(""); Console.WriteLine(" !exit - Exits the interpreter"); Console.WriteLine(" !debug - Starts the debugger"); Console.WriteLine(" !run <filename> - Executes the specified Lua script"); Console.WriteLine(" !compile <filename> - Compiles the file in a binary format"); Console.WriteLine(""); } else if (p == "exit") { Environment.Exit(0); } else if (p == "debug" && m_Debugger == null) { m_Debugger = new RemoteDebuggerService(); m_Debugger.Attach(S, "MoonSharp REPL interpreter", false); Process.Start(m_Debugger.HttpUrlStringLocalHost); } else if (p.StartsWith("run")) { p = p.Substring(3).Trim(); S.DoFile(p); } else if (p == "!") { ParseCommand(S, "debug"); ParseCommand(S, @"run c:\temp\test.lua"); } else if (p.StartsWith("compile")) { p = p.Substring("compile".Length).Trim(); string targetFileName = p + "-compiled"; DynValue chunk = S.LoadFile(p); using (Stream stream = new FileStream(targetFileName, FileMode.Create, FileAccess.Write)) S.Dump(chunk, stream); } }
/// <summary> /// Asynchronously loads a string containing a Lua/MoonSharp script. /// This method is supported only on .NET 4.x and .NET 4.x PCL targets. /// </summary> /// <param name="script">The script.</param> /// <param name="filename">The code.</param> /// <param name="globalContext">The global table to bind to this chunk.</param> /// <param name="friendlyFilename">The filename to be used in error messages.</param> /// <returns> /// A DynValue containing a function which will execute the loaded code. /// </returns> public static Task <DynValue> LoadFileAsync(this Script script, string filename, Table globalContext = null, string friendlyFilename = null) { return(ExecAsync(() => script.LoadFile(filename, globalContext, friendlyFilename))); }