static void TestFunctionScope() { JavascriptContext ctx = new JavascriptContext(); ctx.SetParameter("XB", 0); ctx.RegisterMethod("\"use strict\";var cyclic; var one_time; (function(){ /* XBuildr-globals: cyclic one_time */ var mathy = $.require(\"math\"); XB += 1; var kyle = 5; this.c = function() { return 2; }}).call(this);", "a"); ctx.RegisterMethod("markus = 4; var a = 5; var b = a*b*999; XB += c();", "b"); try { ctx.CallMethodFromDict("a"); ctx.CallMethodFromDict("b"); } catch (JavascriptException e) { Console.WriteLine("Threw error: {0} at code {1} at line {2}", (string)e.Data["V8StackTrace"], e.Source, e.Line); Console.WriteLine("**********"); } Console.WriteLine("Parameter XB = {0}", ctx.GetParameter("XB")); ctx.Dispose(); }
static void BasicTest() { JavascriptContext ctx = new JavascriptContext(); ctx.SetParameter("XB", "begin: "); ctx.RegisterMethod("XB += 'included_test ';", "test"); ctx.RegisterMethod("XB += 'included_thr '; var a = sam + 4;", "thr"); try { Console.WriteLine("Parameter XB={0}", ctx.GetParameter("XB")); ctx.CallMethodFromDict("test"); Console.WriteLine("After test, parameter XB={0}", ctx.GetParameter("XB")); ctx.CallMethodFromDict("thr"); Console.WriteLine("After thr, parameter XB={0}", ctx.GetParameter("XB")); } catch (JavascriptException e) { string err = (string)e.Data["V8StackTrace"]; Console.WriteLine("Called thr, threw error: {0} at code {1} at line {2}", err, e.Source, e.Line); Console.WriteLine("**********"); var stacktrace = (string)e.Data["V8StackTrace"]; Console.WriteLine(FancifyStackTrace("XB += 'included_thr '; var a = sam + 4;", stacktrace)); Console.WriteLine("**********"); Console.WriteLine("After throwing, parameter XB={0}", ctx.GetParameter("XB")); } //ctx.RegisterMethod("function test() { return \"Success!!\"; };", "test"); //ctx.RegisterMethod("function add(a,b) { return a + b; };", "add"); //ctx.RegisterMethod("function und() { return undefined; };", "und"); //ctx.RegisterMethod("function thr(i) { i = i || 0;\n if (i > 3)\n { var a = sam + 5; return a; };\n return thr(i+1);\n}", "thr"); //var result = ctx.CallMethodFromDict("test");//, new object[] { }); //Console.WriteLine("Called test, got result: "); //Console.WriteLine(result); //Console.WriteLine("Ran add(2,3)"); //result = ctx.CallMethod("add", new object[] { 2, 3 }); //Console.WriteLine("Called add, got result: "); //Console.WriteLine(result); //try //{ // Console.WriteLine("Ran thr"); // result = ctx.CallMethodFromDict("thr");//, new object[] { }); // Console.WriteLine("Called thr, got result: "); // Console.WriteLine(result); //} //catch (JavascriptException e) //{ // string err = (string)e.Data["V8StackTrace"]; // Console.WriteLine("Called thr, threw error: {0} at code {1} at line {2}", err, e.Source, e.Line); // var stacktrace = (string)e.Data["V8StackTrace"]; // Console.WriteLine(FancifyStackTrace("function thr(i) { i = i || 0;\n if (i > 3)\n { var a = sam + 5; return a; };\n return thr(i+1);\n}", stacktrace)); //} }
static void RegisterFunctions(int iterations) { JavascriptContext ctx = new JavascriptContext(); ctx.SetParameter("XB", 0); ctx.RegisterMethod("XB += 1;", "a"); ctx.RegisterMethod("var a = 5; var b = a*b*999; XB += 1;", "b"); var watch = new Stopwatch(); watch.Start(); for (int i = 0; i < iterations; i++) { ctx.CallMethodFromDict("a"); ctx.CallMethodFromDict("b"); } watch.Stop(); Console.WriteLine("Took {0}ms to run {1} iterations of simple methods using RegisterFunctions.", watch.ElapsedMilliseconds, iterations); ctx.Dispose(); }