private void loadJsClassFile(string classpath, string classname) { string safeClasspath = classpath.Replace("..\\", ""); string path = System.IO.Path.Combine(JS_SRC_PATH, safeClasspath + ".js"); _jsContext.Execute(File.ReadAllText(path)); }
public void SetJsIntegerProperty() { using (JsContext js = jsEngine.CreateContext()) { var v = 42; js.Execute("var x = {}"); var x = js.GetVariable("x") as JsObject; x["the_answer"] = v; Assert.That(js.Execute("x.the_answer"), Is.EqualTo(v)); } }
/// <summary> /// Evaluates the specified JavaScript expression /// </summary> /// <param name="expression">Expression to evaluate</param> /// <returns>Result of the expression</returns> protected override object InnerEvaluate(string expression) { try { return(_context.Execute(expression)); } catch (OriginalJsException ex) { throw ConvertJavaScriptExceptionToJsRuntimeException(ex); } }
public void SetJsStringProperty() { using (JsContext js = jsEngine.CreateContext()) { var v = "This was set from managed code!"; js.Execute("var x = {}"); var x = js.GetVariable("x") as JsObject; x["a_string"] = v; Assert.That(js.Execute("x.a_string"), Is.EqualTo(v)); } }
public void AccessPropertiesAndCallMethodsTest() { using (JsEngine js = new JsEngine(4, 32)) { using (JsContext context = js.CreateContext()) { context.SetVariable("m", new Test()); context.Execute("m.Value = 42"); var result = context.Execute("m.PrintValue('And the answer is (again!):')"); Assert.AreEqual(result, "And the answer is (again!): 42"); } } }
static void TestCase1() { #if DEBUG JsBridge.dbugTestCallbacks(); #endif JsTypeDefinition jstypedef = new JsTypeDefinition("AA"); jstypedef.AddMember(new JsMethodDefinition("B", args => { args.SetResult(100); })); jstypedef.AddMember(new JsMethodDefinition("C", args => { args.SetResult(true); })); //=============================================================== //create js engine and context using (JsEngine engine = new JsEngine()) using (JsContext ctx = engine.CreateContext()) { if (!jstypedef.IsRegisterd) { ctx.RegisterTypeDefinition(jstypedef); } GC.Collect(); System.Diagnostics.Stopwatch stwatch = new System.Diagnostics.Stopwatch(); stwatch.Start(); TestMe1 t1 = new TestMe1(); var proxy = ctx.CreateWrapper(t1, jstypedef); for (int i = 2000; i >= 0; --i) { ctx.SetVariableFromAny("x", proxy); object result = ctx.Execute("(function(){if(x.C()){return x.B();}else{return 0;}})()"); } //test value of object re = ctx.Execute("(function(){function myNumberType(n) { this.number = n;}" + "myNumberType.prototype.valueOf = function() { return this.number;};" + "myObj = new myNumberType(4);" + "return myObj + 3;" + "})()"); stwatch.Stop(); Console.WriteLine("met1 template:" + stwatch.ElapsedMilliseconds.ToString()); //Assert.That(result, Is.EqualTo(100)); } }
private void button8_Click(object sender, EventArgs e) { int version = JsBridge.LibVersion; #if DEBUG JsBridge.dbugTestCallbacks(); #endif using (JsEngine engine = new JsEngine()) using (JsContext ctx = engine.CreateContext(new MyJsTypeDefinitionBuilder())) { GC.Collect(); System.Diagnostics.Stopwatch stwatch = new System.Diagnostics.Stopwatch(); stwatch.Reset(); stwatch.Start(); var ab = new AboutMe(); ctx.SetVariableAutoWrap("x", ab); string testsrc = @"(function(){ x.AttachEvent('mousedown',function(evArgs){}); x.FireEventMouseDown({}); })()"; object result = ctx.Execute(testsrc); stwatch.Stop(); Console.WriteLine("met1 template:" + stwatch.ElapsedMilliseconds.ToString()); } }
public void CompilationException() { using (JsContext js = jsEngine.CreateContext()) { js.Execute("a+§"); } }
public void JsObjectException() { using (JsContext js = jsEngine.CreateContext()) { js.Execute("throw {msg:'Error!'}"); } }
public void SimpleExpressionException() { using (JsContext js = jsEngine.CreateContext()) { js.Execute("throw 'xxx'"); } }
static void TestColl1() { #if DEBUG JsBridge.dbugTestCallbacks(); #endif //create js engine and context using (JsEngine engine = new JsEngine()) using (JsContext ctx = engine.CreateContext()) { GC.Collect(); System.Diagnostics.Stopwatch stwatch = new System.Diagnostics.Stopwatch(); stwatch.Start(); string[] ta = { "test" }; ctx.SetVariableFromAny("ta", ta); object result = ctx.Execute("(function(){return JSON.stringify(ta);})()"); //please parse the string with some json lib //(eg Json.net) to check the first elem if ((string)result != "[\"" + ta[0] + "\"]") { throw new Exception("!"); } stwatch.Stop(); Console.WriteLine("result " + result); Console.WriteLine("met3 managed reflection:" + stwatch.ElapsedMilliseconds.ToString()); } }
static void TestCase2() { #if DEBUG JsBridge.dbugTestCallbacks(); #endif //create js engine and context using (JsEngine engine = new JsEngine()) using (JsContext ctx = engine.CreateContext()) { GC.Collect(); System.Diagnostics.Stopwatch stwatch = new System.Diagnostics.Stopwatch(); stwatch.Start(); TestMe1 t1 = new TestMe1(); for (int i = 2000; i >= 0; --i) { ctx.SetVariableFromAny("x", t1); object result = ctx.Execute("(function(){if(x.C()){return x.B();}else{return 0;}})()"); } stwatch.Stop(); Console.WriteLine("met2 managed reflection:" + stwatch.ElapsedMilliseconds.ToString()); //Assert.That(result, Is.EqualTo(100)); } }
static void TestOverload() { #if DEBUG JsBridge.dbugTestCallbacks(); #endif //create js engine and context using (JsEngine engine = new JsEngine()) using (JsContext ctx = engine.CreateContext()) { GC.Collect(); System.Diagnostics.Stopwatch stwatch = new System.Diagnostics.Stopwatch(); stwatch.Start(); A <B> a = new A <B>(); Base base1 = new Base(); B b = new B(); //---- Console.WriteLine("pure .net behaviour"); //test .net side a.Add(null); //Add(T a) a.Add(b); //Add(T a) a.Add(base1); //Add(Base a) a.Add(new object()); // Add(object a) a.Add(new object[0]); // Add(object a) //---------- Console.WriteLine("----------"); Console.WriteLine("Espresso behaviour"); ctx.SetVariableFromAny("a", a); ctx.SetVariableFromAny("b", b); ctx.SetVariableFromAny("base1", base1); ctx.Execute("(function(){a.Add(null);})()"); //Add(T a) ctx.Execute("(function(){a.Add(b);})()"); //Add(T a) ctx.Execute("(function(){a.Add(base1);})()"); //Add(Base a) ctx.Execute("(function(){a.Add({});})()"); // Add(object a) ctx.Execute("(function(){a.Add([]);})()"); // Add(object a) // if we get to here, we haven't thrown the exception stwatch.Stop(); Console.WriteLine("met4 managed reflection:" + stwatch.ElapsedMilliseconds.ToString()); } }
public void GetJsIntegerProperty() { using (JsContext js = jsEngine.CreateContext()) { js.Execute("var x = { the_answer: 42 }"); var x = js.GetVariable("x") as JsObject; Assert.That(x["the_answer"], Is.EqualTo(42)); } }
public void TestExecute() { var expr = JsContext.Execute(() => { JsContext.Add(new JsCall("init")); JsContext.Add(new JsCall("init2", 123)); }); Assert.AreEqual("init();init2(123);", expr.Encode()); }
public void GetJsStringProperty() { using (JsContext js = jsEngine.CreateContext()) { js.Execute("var x = { a_string: 'This was set from Javascript!' }"); var x = js.GetVariable("x") as JsObject; Assert.That(x["a_string"], Is.EqualTo("This was set from Javascript!")); } }
public void SetManagedStringProperty() { var t = new TestClass(); using (JsContext js = jsEngine.CreateContext()) { js.SetVariableFromAny("o", t); js.Execute("o.StringProperty = 'This was set from Javascript!'"); Assert.That(t.StringProperty, Is.EqualTo("This was set from Javascript!")); } }
private void button1_Click(object sender, EventArgs e) { #if DEBUG JsBridge.dbugTestCallbacks(); #endif JsTypeDefinition jstypedef = new JsTypeDefinition("AA"); jstypedef.AddMember(new JsMethodDefinition("B", args => { args.SetResult(100); })); jstypedef.AddMember(new JsMethodDefinition("C", args => { args.SetResult(true); })); //=============================================================== //create js engine and context using (JsEngine engine = new JsEngine()) using (JsContext ctx = engine.CreateContext()) { if (!jstypedef.IsRegisterd) { ctx.RegisterTypeDefinition(jstypedef); } GC.Collect(); System.Diagnostics.Stopwatch stwatch = new System.Diagnostics.Stopwatch(); stwatch.Start(); TestMe1 t1 = new TestMe1(); var proxy = ctx.CreateWrapper(t1, jstypedef); for (int i = 2000; i >= 0; --i) { ctx.SetVariableFromAny("x", proxy); object result = ctx.Execute("(function(){if(x.C()){return x.B();}else{return 0;}})()"); } //for (int i = 1; i >= 0; --i) //{ // ctx.SetVariableFromAny("x", proxy); // object result = ctx.Execute(@" // var http = require('http'); // var server = http.createServer(function(req, res) { // res.writeHead(200); // res.end('Hello Http'); // }); // server.listen(8080); // "); //} stwatch.Stop(); Console.WriteLine("met1 template:" + stwatch.ElapsedMilliseconds.ToString()); //Assert.That(result, Is.EqualTo(100)); } }
public void SetManagedIntegerProperty() { var t = new TestClass(); using (JsContext js = jsEngine.CreateContext()) { js.SetVariableFromAny("o", t); js.Execute("o.Int32Property = 42"); Assert.That(t.Int32Property, Is.EqualTo(42)); } }
public void TestInit() { var btn = new Button(); JsContext.Execute(() => { btn.Id = "btn1"; btn.Text = "my text"; }); var script = JsContext.Execute(btn.Init).Encode(); Assert.AreEqual("$$('btn1').jasty(\"Button\", \"init\", [{\"id\":\"btn1\", \"text\":\"my text\", \"visible\":true}]);", script); }
public void tellme_Test() { using (JsEngine js = new JsEngine(4, 32)) { using (JsContext context = js.CreateContext()) { context.Execute("var x = {'answer':42, 'tellme':function (x) { return x+' '+this.answer; }}"); dynamic x = context.GetVariable("x"); var result = x.tellme("What is the answer to ...?"); Assert.AreEqual(result, "What is the answer to...? 42"); } } }
public void GetManagedBoolProperty() { var v = 42; var t = new TestClass { BoolProperty = true, Int32Property = 30 }; using (JsContext js = jsEngine.CreateContext()) { js.SetVariableFromAny("o", t); Assert.That(js.Execute("(function(){if(o.BoolProperty){return 10; }else{ return 20;}})()"), Is.EqualTo(10)); } }
public void GetManagedStringProperty() { var v = "The lazy dog bla bla bla..."; var t = new TestClass { StringProperty = v }; using (JsContext js = jsEngine.CreateContext()) { js.SetVariableFromAny("o", t); Assert.That(js.Execute("o.StringProperty"), Is.EqualTo(v)); } }
public void GetManagedIntegerProperty() { var v = 42; var t = new TestClass { Int32Property = v }; using (JsContext js = jsEngine.CreateContext()) { js.SetVariableFromAny("o", t); Assert.That(js.Execute("o.Int32Property"), Is.EqualTo(v)); } }
public void GetMixedProperty1() { using (JsContext js = jsEngine.CreateContext()) { var t = new TestClass(); js.SetVariableFromAny("o", t); js.Execute("var x = { nested: o }; x.nested.Int32Property = 42"); var x = js.GetVariable("x") as JsObject; Assert.That(x["nested"], Is.EqualTo(t)); Assert.That(t.Int32Property, Is.EqualTo(42)); } }
private void button12_Click(object sender, EventArgs e) { //very basic *** //----------------- //test tsc.js //this needs EspressoHostForTsc //----------------- string tsc_esprCode = File.ReadAllText("../../Samples/js_tools/tsc2_5/tsc_espr.js"); StringBuilder stbuilder = new StringBuilder(); stbuilder.Append(tsc_esprCode); //----------------- int version = JsBridge.LibVersion; #if DEBUG JsBridge.dbugTestCallbacks(); #endif using (JsEngine engine = new JsEngine()) using (JsContext ctx = engine.CreateContext(new MyJsTypeDefinitionBuilder())) { GC.Collect(); System.Diagnostics.Stopwatch stwatch = new System.Diagnostics.Stopwatch(); stwatch.Reset(); stwatch.Start(); var my_expr_ext = new EspressoHostForTsc_25(); ctx.SetVariableAutoWrap("my_expr_ext", my_expr_ext); string testsrc = @"(function(){ // test1: general compile through commamd line // ts.executeCommandLine(['greeter.ts']); //------------------------------------------------- // test 2: generate ast var filename=""greeter.ts""; //example only my_expr_ext.ConsoleLog(filename); //parse const sourceFile = ts.createSourceFile(filename, my_expr_ext.readFile(filename),2, false); //send output as json to managed host my_expr_ext.ConsoleLog(JSON.stringify( sourceFile)); })()"; stbuilder.Append(testsrc); ctx.Execute(stbuilder.ToString()); stwatch.Stop(); Console.WriteLine("met1 template:" + stwatch.ElapsedMilliseconds.ToString()); } }
/// <summary> /// Initializes a new instance of the <see cref="VroomJsEngine"/> class. /// </summary> /// <exception cref="System.Exception">V8 engine context couldn't be created.</exception> public VroomJsEngine() { try { _context = JsEngine.Value.CreateContext(); } catch (Exception ex) { throw new Exception("V8 engine context couldn't be created.", ex); } _context.Execute(""); _context.SetVariable("console", new EngineConsole()); }
public void SetManagedNestedProperty() { var t = new TestClass(); var n = new TestClass(); using (JsContext js = jsEngine.CreateContext()) { js.SetVariableFromAny("o", t); js.SetVariableFromAny("n", n); js.Execute("o.NestedObject = n; o.NestedObject.Int32Property = 42"); Assert.That(t.NestedObject, Is.EqualTo(n)); Assert.That(n.Int32Property, Is.EqualTo(42)); } }
public void CallJsProperty() { using (JsContext js = jsEngine.CreateContext()) { js.Execute("var x = { f: function (a, b) { return [a*2, b+'!']; }}"); var x = js.GetVariable("x") as JsObject; var f = x["f"] as JsFunction; var r = f.Invoke(21, "Can't believe this worked"); Assert.That(r, Is.AssignableTo <object[]>()); object[] a = (object[])r; Assert.That(a.Length, Is.EqualTo(2)); Assert.That(a[0], Is.EqualTo(42)); Assert.That(a[1], Is.EqualTo("Can't believe this worked!")); } }
public void CallManagedMethod() { var t = new TestClass { Int32Property = 13, StringProperty = "Wow" }; using (JsContext js = jsEngine.CreateContext()) { js.SetVariableFromAny("o", t); js.Execute("var r = o.Method1(29, '!')"); object r = js.GetVariable("r"); Assert.That(r, Is.AssignableTo <TestClass>()); TestClass c = (TestClass)r; Assert.That(c.Int32Property, Is.EqualTo(42)); Assert.That(c.StringProperty, Is.EqualTo("Wow!")); } }