/// <summary> /// Warms up the parser/lexer structures so that MoonSharp operations start faster. /// </summary> public static void WarmUp() { Script s = new Script(CoreModules.Basic); s.LoadString("return 1;"); Kill(ref s); }
public static void StringBuilderCustomConverter() { Script script = new Script(); script.Globals["getstr"] = (Func<StringBuilder>)GetString; DynValue fn = script.LoadString("print(getstr())"); fn.Function.Call(); Script.GlobalOptions.CustomConverters.SetClrToScriptCustomConversion<StringBuilder>( v => DynValue.NewString(v.ToString().ToUpper())); fn.Function.Call(); }
static void OverriddenPrint() { // redefine print to print in lowercase, for all new scripts Script.DefaultOptions.DebugPrint = s => Console.WriteLine(s.ToLower()); Script script = new Script(); DynValue fn = script.LoadString("print 'Hello, World!'"); fn.Function.Call(); // this prints "hello, world!" // redefine print to print in UPPERCASE, for this script only script.Options.DebugPrint = s => Console.WriteLine(s.ToUpper()); fn.Function.Call(); // this prints "HELLO, WORLD!" }
/// <summary> /// Warms up the parser/lexer structures so that MoonSharp operations start faster. /// </summary> public static void WarmUp() { Script s = new Script(CoreModules.Basic); s.LoadString("return 1;"); }
/// <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="code">The code.</param> /// <param name="globalTable">The global table to bind to this chunk.</param> /// <param name="codeFriendlyName">Name of the code - used to report errors, etc.</param> /// <returns> /// A DynValue containing a function which will execute the loaded code. /// </returns> public static Task <DynValue> LoadStringAsync(this Script script, string code, Table globalTable = null, string codeFriendlyName = null) { return(ExecAsync(() => script.LoadString(code, globalTable, codeFriendlyName))); }
static void Main(string[] args) { Script.WarmUp(); Stopwatch sw; sw = Stopwatch.StartNew(); var _s = new Script(); _s.LoadString(scriptText); sw.Stop(); Console.WriteLine("Build : {0} ms", sw.ElapsedMilliseconds); sw = Stopwatch.StartNew(); var script = new Script(); script.Globals.Set("check", DynValue.NewCallback(new CallbackFunction(Check))); CallbackFunction.DefaultAccessMode = InteropAccessMode.Preoptimized; //script.Globals["print"] = (Action<int, string, int>)PrintX; DynValue func = script.LoadString(scriptText); sw.Stop(); Console.WriteLine("Build 2: {0} ms", sw.ElapsedMilliseconds); sw = Stopwatch.StartNew(); for (int i = 0; i < ITERATIONS; i++) { script.Call(func); } sw.Stop(); Console.WriteLine("MoonSharp : {0} ms", sw.ElapsedMilliseconds); lua.RegisterFunction("check", typeof(Program).GetMethod("NCheck")); File.WriteAllText(@"c:\temp\hanoi.lua", scriptText); #if !PROFILER var fn = lua.LoadFile(@"c:\temp\hanoi.lua"); sw = Stopwatch.StartNew(); for (int i = 0; i < ITERATIONS; i++) { fn.Call(); } sw.Stop(); #endif Console.WriteLine("NLua : {0} ms", sw.ElapsedMilliseconds); Console.WriteLine("M# == NL ? {0}", g_MoonSharpStr.ToString() == g_NLuaStr.ToString()); Console.WriteLine("=== MoonSharp ==="); //Console.WriteLine(g_MoonSharpStr.ToString()); Console.WriteLine(""); Console.WriteLine("=== NLua ==="); //Console.WriteLine(g_NLuaStr.ToString()); Console.ReadKey(); }