public async void Cached_Script_Shows_Cache_Hits() { var manager = new RuntimeManager(new ManualManagerSettings()); await manager.ExecuteAsync("test", "var i = 0; i++;"); await manager.ExecuteAsync("test", "var i = 0; i++;"); CachedV8Script cached; manager.TryGetCached("test", out cached); cached.CacheHits.ShouldBeGreaterThan(1); }
public async Task Basic_Http_Get_Headers_Are_Retrieved() { var subject = new TestObject(); var manager = new RuntimeManager(new ManualManagerSettings { ScriptTimeoutMilliSeconds = 0 }); HttpPackageHelpers.RegisterPackage(manager.RequireManager); manager.AddConsoleReference = true; var options = new ExecutionOptions(); options.HostObjects.Add(new HostObject { Name = "subject", Target = subject }); var scriptAwaiter = new ScriptAwaiter(); options.HostObjects.Add(new HostObject { Name = "scriptAwaiter", Target = scriptAwaiter }); var code = "var requestFactory = require('javascript_request_factory');" + "var http = requestFactory.create({url:'http://www.baidu.com/'});" + "var data = encodeURIComponent('errorMsg=&to=http%253A%252F%252Fwww.zhonghuasuan.com%252F&token=5b9c1a3c6f2db8c737b7788ac560a397&account=111111&password=111111');" + "Console.WriteLine('aaaa111'); var aa = http.getString({timeout:10,headers:{token:'aaaaaatoken'}});Console.WriteLine(aa);"; await manager.ExecuteAsync("testScript", code, options); }
public async void Basic_Http_Get_Body_Is_Retrieved() { var subject = new TestObject(); var manager = new RuntimeManager(new ManualManagerSettings { ScriptTimeoutMilliSeconds = 0 }); HttpPackageHelpers.RegisterPackage(manager.RequireManager); manager.AddConsoleReference = true; var options = new ExecutionOptions(); options.HostObjects.Add(new HostObject { Name = "subject", Target = subject }); var scriptAwaiter = new ScriptAwaiter(); options.HostObjects.Add(new HostObject { Name = "scriptAwaiter", Target = scriptAwaiter }); var code = "var request = require('request');" + "request({url: 'http://api.icndb.com/jokes/random/1', json: true}," + " function (error, response, body) {subject.Response = response; subject.Body = body; subject.Joke = body.value[0].joke; scriptAwaiter.Callback();});"; await manager.ExecuteAsync("testScript", code, options); await scriptAwaiter.T; subject.Joke.ShouldNotBeNull(); }
public async void Multiple_Scripts_Types_Are_Run_First_To_Last() { var subject = new TestObject(); var manager = new RuntimeManager(new ManualManagerSettings()); await manager.ExecuteAsync(new List <IncludeScript> { new IncludeScript { Uri = ".\\TestMainScript.js", ScriptId = "testScript" }, new IncludeScript { Code = "subject.TestString = 'test string3';", ScriptId = "testScript3" }, new IncludeScript { Uri = "https://gist.githubusercontent.com/eswann/62fc90e542f7697263db/raw/026c42ef0043a70a8c3b720e3026ecee9c691a06/gistfile1.txt", ScriptId = "TestScript4" } }, new ExecutionOptions { HostObjects = new List <HostObject> { new HostObject { Name = "subject", Target = subject } }, }); subject.Name.ShouldEqual("Name"); subject.Count.ShouldEqual(20); subject.TestString.ShouldEqual("test string3"); }
public void Javascript_Error_Is_Raised() { var manager = new RuntimeManager(new ManualManagerSettings()); Assert.Throws<ScriptEngineException>(async () => await manager.ExecuteAsync("test", "Console.WriteLine('wrote this to the console');")); }
public async void Basic_Http_Get_Headers_Are_Retrieved() { var subject = new TestObject(); var manager = new RuntimeManager(new ManualManagerSettings { ScriptTimeoutMilliSeconds = 0 }); PackageHelpers.RegisterRequestPackages(); manager.AddConsoleReference = true; var options = new ExecutionOptions(); options.HostObjects.Add(new HostObject { Name = "subject", Target = subject }); var scriptAwaiter = new ScriptAwaiter(); options.HostObjects.Add(new HostObject { Name = "scriptAwaiter", Target = scriptAwaiter }); var code = "var request = require('request');" + "request({url: 'http://api.icndb.com/jokes/random/1', json: true}," + " function (error, response, body) {subject.Response = response; subject.Headers = response.headers; scriptAwaiter.Callback();});"; await manager.ExecuteAsync("testScript", code, options); await scriptAwaiter.T; subject.Headers.Count().ShouldBeGreaterThan(0); }
public async void Multiple_Scripts_With_String_Code_Are_Run() { var subject = new TestObject(); var manager = new RuntimeManager(new ManualManagerSettings()); await manager.ExecuteAsync(new List <IncludeScript> { new IncludeScript { Code = "subject.Count = 10;", ScriptId = "testScript" }, new IncludeScript { Code = "subject.TestString = 'test string1';", ScriptId = "testScript2" } }, new ExecutionOptions { HostObjects = new List <HostObject> { new HostObject { Name = "subject", Target = subject } }, }); subject.Name.ShouldEqual("Name"); subject.Count.ShouldEqual(10); subject.TestString.ShouldEqual("test string1"); }
public async void Multiple_Scripts_Types_Are_Run_First_To_Last() { var subject = new TestObject(); var manager = new RuntimeManager(new ManualManagerSettings()); await manager.ExecuteAsync(new List <IncludeScript> { new IncludeScript { Uri = ".\\TestMainScript.js", ScriptId = "testScript" }, new IncludeScript { Code = "subject.TestString = 'test string3';", ScriptId = "testScript3" } }, new ExecutionOptions { HostObjects = new List <HostObject> { new HostObject { Name = "subject", Target = subject } }, }); subject.Name.ShouldEqual("Name"); subject.Count.ShouldEqual(10); subject.TestString.ShouldEqual("test string3"); }
public async void Script_With_String_Code_Include_Is_Included() { var preScript = "var x = 'test string1';"; var subject = new TestObject(); var manager = new RuntimeManager(new ManualManagerSettings()); await manager.ExecuteAsync("testscript", "subject.Count = 10; subject.TestString = x;", new ExecutionOptions { HostObjects = new List <HostObject> { new HostObject { Name = "subject", Target = subject } }, Scripts = new List <IncludeScript> { new IncludeScript { Code = preScript, ScriptId = "testScript2" } } }); subject.Name.ShouldEqual("Name"); subject.Count.ShouldEqual(10); subject.TestString.ShouldEqual("test string1"); }
public void Javascript_Error_Is_Raised() { var manager = new RuntimeManager(new ManualManagerSettings()); Assert.Throws <ScriptEngineException>(async() => await manager.ExecuteAsync("test", "Console.WriteLine('wrote this to the console');")); }
public async void Script_With_Require_Is_Used() { var subject = new TestObject(); var manager = new RuntimeManager(new ManualManagerSettings { ScriptTimeoutMilliSeconds = 0 }); RequireManager.RegisterPackage(new RequiredPackage { PackageId = "testRequire", ScriptUri = "https://gist.githubusercontent.com/eswann/76ecaba02dee33cf26b4/raw/bcafe0a389c84ba44d6ee1661e66b2213aa2ffa0/testRequire" }); await manager.ExecuteAsync("testscript", "var testObject = require('testRequire'); subject.Count = 10; subject.TestString = testObject.getText();", new ExecutionOptions { HostObjects = new List <HostObject> { new HostObject { Name = "subject", Target = subject } }, }); subject.Name.ShouldEqual("Name"); subject.Count.ShouldEqual(10); subject.TestString.ShouldEqual("testText"); }
public async void Included_Variables_Are_Accessible_Via_Script_Object() { var subject = new TestObject(); var manager = new RuntimeManager(new ManualManagerSettings()); var engine = await manager.ExecuteAsync("testscript", "subject.Count = 10; subject.TestString = x;", new ExecutionOptions { HostObjects = new List <HostObject> { new HostObject { Name = "subject", Target = subject } }, Scripts = new List <IncludeScript> { new IncludeScript { Uri = ".\\TestIncludeScript.js", ScriptId = "testScript2" } } }); subject.Name.ShouldEqual("Name"); subject.Count.ShouldEqual(10); subject.TestString.ShouldEqual("test string1"); engine.Script.x = "test string1"; }
public async void Script_Returns_Engine() { var manager = new RuntimeManager(new ManualManagerSettings()); var engine = await manager.ExecuteAsync("test", "var i = 0; i++;"); Assert.AreEqual(1, engine.Script.i); }
public async void Script_Returns_Engine() { var manager = new RuntimeManager(new ManualManagerSettings()); var engine = await manager.ExecuteAsync("test", "var i = 0; i++;"); Assert.AreEqual(1, engine.Script.i); }
public async void Script_Can_Write_To_Console() { var manager = new RuntimeManager(new ManualManagerSettings()) { AddConsoleReference = true }; await manager.ExecuteAsync("test", "Console.WriteLine('wrote this to the console');"); }
public async void Compiled_Script_Is_Cached_By_Default() { var manager = new RuntimeManager(new ManualManagerSettings()); await manager.ExecuteAsync("test", "var i = 0; i++;"); CachedV8Script cached; manager.TryGetCached("test", out cached).ShouldBeTrue(); }
public async void Compiled_Script_Is_Cached_By_Default() { var manager = new RuntimeManager(new ManualManagerSettings()); await manager.ExecuteAsync("test", "var i = 0; i++;"); CachedV8Script cached; manager.TryGetCached("test", out cached).ShouldBeTrue(); }
public async void Script_With_0_Timeout_Works() { var manager = new RuntimeManager(new ManualManagerSettings { ScriptTimeoutMilliSeconds = 0 }) { AddConsoleReference = true }; var script = "Console.WriteLine('Started:' + new Date().toJSON()); " + "var now = new Date().getTime(); " + "Console.WriteLine('Finished:' + new Date().toJSON());"; await manager.ExecuteAsync("test", script); }
public async void Script_With_String_Code_Is_Run() { var subject = new TestObject(); var manager = new RuntimeManager(new ManualManagerSettings()); await manager.ExecuteAsync(new List<IncludeScript> { new IncludeScript { Code = "subject.Count = 10; subject.TestString = 'test string1';", ScriptId = "testScript" } }, new ExecutionOptions { HostObjects = new List<HostObject> { new HostObject { Name = "subject", Target = subject } }, }); subject.Name.ShouldEqual("Name"); subject.Count.ShouldEqual(10); subject.TestString.ShouldEqual("test string1"); }
public async void Script_With_Http_Reference_Is_Run() { var subject = new TestObject(); var manager = new RuntimeManager(new ManualManagerSettings()); await manager.ExecuteAsync(new List<IncludeScript> { new IncludeScript { Uri = "https://gist.githubusercontent.com/eswann/66796164c1904b6135ea/raw/5491642164a45ae34af2926137950de3249d1e91/gistfile1.txt", ScriptId = "testScript" } }, new ExecutionOptions { HostObjects = new List<HostObject> { new HostObject { Name = "subject", Target = subject } }, }); subject.Name.ShouldEqual("Name"); subject.Count.ShouldEqual(10); subject.TestString.ShouldEqual("test string1"); }
public void Script_Exceeding_Timeout_Errs() { var manager = new RuntimeManager(new ManualManagerSettings{ScriptTimeoutMilliSeconds = 500}) { AddConsoleReference = true }; var script = "Console.WriteLine('Started:' + new Date().toJSON()); " + "var now = new Date().getTime(); while(new Date().getTime() < now + 1000){{ /* do nothing */ }}; " + "Console.WriteLine('Finished:' + new Date().toJSON());"; Assert.Throws<ScriptInterruptedException>(async () => await manager.ExecuteAsync("test", script)); }
public async void Script_With_Typed_Subject_Can_Be_Executed() { var subject = new TestObject(); var manager = new RuntimeManager(new ManualManagerSettings()); await manager.ExecuteAsync("testscript", "subject.Count = 10;", new List <HostObject> { new HostObject { Name = "subject", Target = subject } }); subject.Name.ShouldEqual("Name"); subject.Count.ShouldEqual(10); }
public async void Script_With_File_Include_Is_Included() { var subject = new TestObject(); var manager = new RuntimeManager(new ManualManagerSettings()); await manager.ExecuteAsync("testscript", "subject.Count = 10; subject.TestString = x;", new ExecutionOptions { HostObjects = new List<HostObject> {new HostObject {Name = "subject", Target = subject}}, Scripts = new List<IncludeScript> {new IncludeScript {Uri = ".\\TestIncludeScript.js", ScriptId = "testScript2"}} }); subject.Name.ShouldEqual("Name"); subject.Count.ShouldEqual(10); subject.TestString.ShouldEqual("test string1"); }
public async void Script_With_Http_Include_Is_Included() { var subject = new TestObject(); var manager = new RuntimeManager(new ManualManagerSettings()); await manager.ExecuteAsync("testscript", "subject.Count = 10; subject.TestString = x;", new ExecutionOptions { HostObjects = new List<HostObject> {new HostObject {Name = "subject", Target = subject}}, Scripts = new List<IncludeScript> { new IncludeScript { Uri = "https://gist.githubusercontent.com/eswann/3215f3afff3a602c0f3a/raw/a9ace53fca80fdcdefba60c4f7bf803bf5239905/gistfile1.txt", ScriptId = "testScript2" } } }); subject.Name.ShouldEqual("Name"); subject.Count.ShouldEqual(10); subject.TestString.ShouldEqual("test string1"); }
public void Script_Exceeding_Timeout_Errs() { var manager = new RuntimeManager(new ManualManagerSettings { ScriptTimeoutMilliSeconds = 500 }) { AddConsoleReference = true }; var script = "Console.WriteLine('Started:' + new Date().toJSON()); " + "var now = new Date().getTime(); while(new Date().getTime() < now + 1000){{ /* do nothing */ }}; " + "Console.WriteLine('Finished:' + new Date().toJSON());"; Assert.Throws <ScriptInterruptedException>(async() => await manager.ExecuteAsync("test", script)); }
public async void Script_With_0_Timeout_Works() { var manager = new RuntimeManager(new ManualManagerSettings { ScriptTimeoutMilliSeconds = 0 }) { AddConsoleReference = true }; var script = "Console.WriteLine('Started:' + new Date().toJSON()); " + "var now = new Date().getTime(); " + "Console.WriteLine('Finished:' + new Date().toJSON());"; await manager.ExecuteAsync("test", script); }
public async void Included_Variables_Are_Accessible_Via_Script_Object() { var subject = new TestObject(); var manager = new RuntimeManager(new ManualManagerSettings()); var engine = await manager.ExecuteAsync("testscript", "subject.Count = 10; subject.TestString = x;", new ExecutionOptions { HostObjects = new List<HostObject> { new HostObject { Name = "subject", Target = subject } }, Scripts = new List<IncludeScript> { new IncludeScript { Uri = ".\\TestIncludeScript.js", ScriptId = "testScript2" } } }); subject.Name.ShouldEqual("Name"); subject.Count.ShouldEqual(10); subject.TestString.ShouldEqual("test string1"); engine.Script.x = "test string1"; }
public async void Require_Named_With_Explicit_Name_Succeeds() { var subject = new TestObject(); var manager = new RuntimeManager(new ManualManagerSettings { ScriptTimeoutMilliSeconds = 0 }); await manager.ExecuteAsync("testscript", @"var testObject = requireNamed('testRequire', 'https://gist.githubusercontent.com/eswann/3ff5ec2bcc63b7d2cdaa/raw/26e30f6a9761dbbe5634b2de1549c4a455b7a7d6/TestDiffNameRequire.js'); subject.Count = 10; subject.TestString = testObject.getText();", new ExecutionOptions { HostObjects = new List<HostObject> { new HostObject { Name = "subject", Target = subject } }, }); subject.Name.ShouldEqual("Name"); subject.Count.ShouldEqual(10); subject.TestString.ShouldEqual("testText"); }
public async void Require_Specified_As_Script_HttpPath_Path_Is_Used() { var subject = new TestObject(); var manager = new RuntimeManager(new ManualManagerSettings {ScriptTimeoutMilliSeconds = 0}); await manager.ExecuteAsync("testscript", @"var testObject = require('https://gist.githubusercontent.com/eswann/76ecaba02dee33cf26b4/raw/bcafe0a389c84ba44d6ee1661e66b2213aa2ffa0/testRequire'); subject.Count = 10; subject.TestString = testObject.getText();", new ExecutionOptions { HostObjects = new List<HostObject> {new HostObject {Name = "subject", Target = subject}}, }); subject.Name.ShouldEqual("Name"); subject.Count.ShouldEqual(10); subject.TestString.ShouldEqual("testText"); }
public async void Script_With_Require_Is_Used() { var subject = new TestObject(); var manager = new RuntimeManager(new ManualManagerSettings {ScriptTimeoutMilliSeconds = 0}); RequireManager.RegisterPackage(new RequiredPackage { PackageId = "testRequire", ScriptUri = ".\\TestRequire.js" }); await manager.ExecuteAsync("testscript", "var testObject = require('testRequire'); subject.Count = 10; subject.TestString = testObject.getText();", new ExecutionOptions { HostObjects = new List<HostObject> {new HostObject {Name = "subject", Target = subject}}, }); subject.Name.ShouldEqual("Name"); subject.Count.ShouldEqual(10); subject.TestString.ShouldEqual("testText"); }
public async void Multiple_Scripts_Types_Are_Run_First_To_Last() { var subject = new TestObject(); var manager = new RuntimeManager(new ManualManagerSettings()); await manager.ExecuteAsync(new List<IncludeScript> { new IncludeScript {Uri = ".\\TestMainScript.js", ScriptId = "testScript"}, new IncludeScript {Code = "subject.TestString = 'test string3';", ScriptId = "testScript3"} }, new ExecutionOptions { HostObjects = new List<HostObject> {new HostObject {Name = "subject", Target = subject}}, }); subject.Name.ShouldEqual("Name"); subject.Count.ShouldEqual(10); subject.TestString.ShouldEqual("test string3"); }
public async void Multiple_Scripts_Types_Are_Run_First_To_Last() { var subject = new TestObject(); var manager = new RuntimeManager(new ManualManagerSettings()); await manager.ExecuteAsync(new List<IncludeScript> { new IncludeScript {Uri = ".\\TestMainScript.js", ScriptId = "testScript"}, new IncludeScript {Code = "subject.TestString = 'test string3';", ScriptId = "testScript3"}, new IncludeScript {Uri = "https://gist.githubusercontent.com/eswann/62fc90e542f7697263db/raw/026c42ef0043a70a8c3b720e3026ecee9c691a06/gistfile1.txt", ScriptId = "TestScript4"} }, new ExecutionOptions { HostObjects = new List<HostObject> {new HostObject {Name = "subject", Target = subject}}, }); subject.Name.ShouldEqual("Name"); subject.Count.ShouldEqual(20); subject.TestString.ShouldEqual("test string3"); }
public async void Script_With_Dynamic_Subject_Can_Be_Executed() { dynamic subject = new ExpandoObject(); subject.Name = "Name"; subject.Count = 0; var manager = new RuntimeManager(new ManualManagerSettings()); await manager.ExecuteAsync("test", "subject.Count = 10;", new List <HostObject> { new HostObject { Name = "subject", Target = subject } }); //Should craps out on dynamics Assert.AreEqual(subject.Name, "Name"); Assert.AreEqual(subject.Count, 10); }
public async void Script_Referencing_Specific_Type_Executes() { var manager = new RuntimeManager(new ManualManagerSettings()); var hostType = new HostType { Name = "MathStuff", Type = typeof(System.Math) }; var subject = new TestObject(); await manager.ExecuteAsync("test", "subject.Result = MathStuff.Pow(10,2);", new List<HostObject> { new HostObject { Name = "subject", Target = subject } }, new List<HostType> { hostType }); subject.Result.ShouldEqual(100); }
public async void TestLog1() { var subject = new TestObject(); var manager = new RuntimeManager(new ManualManagerSettings { ScriptTimeoutMilliSeconds = 0 }); LogPackageHelpers.RegisterPackage(manager.RequireManager); var options = new ExecutionOptions(); options.HostObjects.Add(new HostObject { Name = "subject", Target = subject }); var code = "var logFactory = require('javascript_log_factory');" + "var log = this.logFactory.create({trace:true});" + //"subject.StatusCode = content.ExecuteNonQuery(\"update school set address ='1' where id = 1\");"; "try{ aa.ttt =1}catch(err){log.info(err)}"; await manager.ExecuteAsync("testScript", code, options); }
public async void Script_With_File_Reference_Is_Run() { var subject = new TestObject(); var manager = new RuntimeManager(new ManualManagerSettings()); await manager.ExecuteAsync(new List <IncludeScript> { new IncludeScript { Uri = ".\\TestMainScript.js", ScriptId = "testScript" } }, new ExecutionOptions { HostObjects = new List <HostObject> { new HostObject { Name = "subject", Target = subject } }, }); subject.Name.ShouldEqual("Name"); subject.Count.ShouldEqual(10); subject.TestString.ShouldEqual("test string1"); }
public async void Require_Specified_As_Script_Local_Path_Is_Used() { var subject = new TestObject(); var manager = new RuntimeManager(new ManualManagerSettings { ScriptTimeoutMilliSeconds = 0 }); await manager.ExecuteAsync("testscript", @"var testObject = require('.\\TestRequire.js'); subject.Count = 10; subject.TestString = testObject.getText();", new ExecutionOptions { HostObjects = new List <HostObject> { new HostObject { Name = "subject", Target = subject } }, }); subject.Name.ShouldEqual("Name"); subject.Count.ShouldEqual(10); subject.TestString.ShouldEqual("testText"); }
public async void Script_With_Http_Reference_Is_Run() { var subject = new TestObject(); var manager = new RuntimeManager(new ManualManagerSettings()); await manager.ExecuteAsync(new List <IncludeScript> { new IncludeScript { Uri = "https://gist.githubusercontent.com/eswann/66796164c1904b6135ea/raw/5491642164a45ae34af2926137950de3249d1e91/gistfile1.txt", ScriptId = "testScript" } }, new ExecutionOptions { HostObjects = new List <HostObject> { new HostObject { Name = "subject", Target = subject } }, }); subject.Name.ShouldEqual("Name"); subject.Count.ShouldEqual(10); subject.TestString.ShouldEqual("test string1"); }
public async void Require_Named_With_Explicit_Name_Succeeds() { var subject = new TestObject(); var manager = new RuntimeManager(new ManualManagerSettings { ScriptTimeoutMilliSeconds = 0 }); await manager.ExecuteAsync("testscript", @"var testObject = requireNamed('testRequire', 'https://gist.githubusercontent.com/eswann/3ff5ec2bcc63b7d2cdaa/raw/26e30f6a9761dbbe5634b2de1549c4a455b7a7d6/TestDiffNameRequire.js'); subject.Count = 10; subject.TestString = testObject.getText();", new ExecutionOptions { HostObjects = new List <HostObject> { new HostObject { Name = "subject", Target = subject } }, }); subject.Name.ShouldEqual("Name"); subject.Count.ShouldEqual(10); subject.TestString.ShouldEqual("testText"); }
public async void Basic_Http_Get_Body_Is_Retrieved() { var subject = new TestObject(); var manager = new RuntimeManager(new ManualManagerSettings { ScriptTimeoutMilliSeconds = 0 }); PackageHelpers.RegisterRequestPackages(); manager.AddConsoleReference = true; var options = new ExecutionOptions(); options.HostObjects.Add(new HostObject { Name = "subject", Target = subject }); var scriptAwaiter = new ScriptAwaiter(); options.HostObjects.Add(new HostObject { Name = "scriptAwaiter", Target = scriptAwaiter }); var code = "var request = require('request');" + "request({url: 'http://api.icndb.com/jokes/random/1', json: true}," + " function (error, response, body) {subject.Response = response; subject.Body = body; subject.Joke = body.value[0].joke; scriptAwaiter.Callback();});"; await manager.ExecuteAsync("testScript", code, options); await scriptAwaiter.T; subject.Joke.ShouldNotBeNull(); }
public async void Require_Named_With_Explicit_Name_Succeeds() { var subject = new TestObject(); var manager = new RuntimeManager(new ManualManagerSettings { ScriptTimeoutMilliSeconds = 0 }); await manager.ExecuteAsync("testscript", @"var testObject = requireNamed('testRequire', '.\\TestDiffNameRequire.js'); subject.Count = 10; subject.TestString = testObject.getText();", new ExecutionOptions { HostObjects = new List <HostObject> { new HostObject { Name = "subject", Target = subject } }, }); subject.Name.ShouldEqual("Name"); subject.Count.ShouldEqual(10); subject.TestString.ShouldEqual("testText"); }
public async void Script_Referencing_Specific_Type_Executes() { var manager = new RuntimeManager(new ManualManagerSettings()); var hostType = new HostType { Name = "MathStuff", Type = typeof(System.Math) }; var subject = new TestObject(); await manager.ExecuteAsync("test", "subject.Result = MathStuff.Pow(10,2);", new List <HostObject> { new HostObject { Name = "subject", Target = subject } }, new List <HostType> { hostType }); subject.Result.ShouldEqual(100); }
public async void TestSql1() { var subject = new TestObject(); var manager = new RuntimeManager(new ManualManagerSettings { ScriptTimeoutMilliSeconds = 0 }); SqlPackageHelpers.RegisterPackage(manager.RequireManager, new AntOrmDbExecutor()); manager.AddConsoleReference = true; var options = new ExecutionOptions(); options.HostObjects.Add(new HostObject { Name = "subject", Target = subject }); var code = "var dbFactory = require('javascript_sql_factory');" + "var content = this.dbFactory.create({name:'testorm',type:'mysql'});" + //"subject.StatusCode = content.ExecuteNonQuery(\"update school set address ='1' where id = 1\");"; "var arr = content.insert(\"insert into school (name,address,datachange_lasttime) values (@name,@address,now())\",{name:'test111',address:'wowowo'});"; await manager.ExecuteAsync("testScript", code, options); subject.StatusCode.ShouldEqual(0); }
public async void TestSql1() { var subject = new TestObject(); var manager = new RuntimeManager(new ManualManagerSettings { ScriptTimeoutMilliSeconds = 0 }); SqlPackageHelpers.RegisterPackage(manager.RequireManager, new AntOrmDbExecutor()); manager.AddConsoleReference = true; var options = new ExecutionOptions(); options.HostObjects.Add(new HostObject { Name = "subject", Target = subject }); var code = "var dbFactory = require('javascript_sql_factory');" + "var content = this.dbFactory.create('testorm','mysql');" + //"subject.StatusCode = content.ExecuteNonQuery(\"update school set address ='1' where id = 1\");"; "var arr = content.query(\"SELECT DataChange_LastTime FROM school LIMIT 1\");Console.WriteLine(arr.ToString('yyyy-MM-dd'))"; await manager.ExecuteAsync("testScript", code, options); subject.StatusCode.ShouldEqual(0); }
public async void Script_With_Http_Include_Is_Included() { var subject = new TestObject(); var manager = new RuntimeManager(new ManualManagerSettings()); await manager.ExecuteAsync("testscript", "subject.Count = 10; subject.TestString = x;", new ExecutionOptions { HostObjects = new List <HostObject> { new HostObject { Name = "subject", Target = subject } }, Scripts = new List <IncludeScript> { new IncludeScript { Uri = "https://gist.githubusercontent.com/eswann/3215f3afff3a602c0f3a/raw/a9ace53fca80fdcdefba60c4f7bf803bf5239905/gistfile1.txt", ScriptId = "testScript2" } } }); subject.Name.ShouldEqual("Name"); subject.Count.ShouldEqual(10); subject.TestString.ShouldEqual("test string1"); }
public async void Script_With_No_Subject_Can_Be_Executed() { var manager = new RuntimeManager(new ManualManagerSettings()); await manager.ExecuteAsync("test", "var i = 0; i++;"); }
public async void Script_Can_Write_To_Console() { var manager = new RuntimeManager(new ManualManagerSettings()) {AddConsoleReference = true}; await manager.ExecuteAsync("test", "Console.WriteLine('wrote this to the console');"); }
private void invokeJsCode(string code) { if (this.catchBox.CheckState.Equals(CheckState.Checked)) { code = "try{\n" + code + "\n}catch(err){\nhost.err=err.message;\nhost.ex=err;\n}"; } isRun = true; Enable(false); Task.Factory.StartNew(async() => { try { code = "var tabris;" + "(function (){\n tabris = tabris || require('javascript_tabris'); \n" + code + "\n})();"; dynamic host = new ExpandoObject(); var option = new ExecutionOptions { HostObjects = new List <HostObject> { new HostObject { Name = "host", Target = host } } }; await manager.ExecuteAsync(Guid.NewGuid().ToString(), code, option); try { if (!string.IsNullOrEmpty(host.err.ToString())) { logAction(LogLevel.ERROR, host.err, ""); } var exception = host.ex as DynamicObject; if (exception != null) { var kv = exception.GetDynamicProperties(); if (kv != null) { foreach (var itemKeyValuePair in kv) { if (itemKeyValuePair.Value is Exception) { Exception ex = (Exception)itemKeyValuePair.Value; while (ex.InnerException != null) { ex = ex.InnerException; } logAction(LogLevel.ERROR, ex.Message, ""); } } } } } catch (Exception) { } //if (Index == 0) //{ // Thread.Sleep(10000); // logAction(LogLevel.ERROR, "ces", ""); //} } catch (ScriptEngineException ex) { logAction(LogLevel.ERROR, ((Microsoft.ClearScript.ScriptEngineException)ex).ErrorDetails, ""); } catch (Exception ex) { logAction(LogLevel.ERROR, ex.Message, ""); } finally { Enable(true); isRun = false; } }).ContinueWith((t) => { if (t.IsFaulted) { Exception ex = t.Exception; while (ex is AggregateException && ex.InnerException != null) { ex = ex.InnerException; } logAction(LogLevel.ERROR, ex.Message, ""); } else if (t.IsCanceled) { logAction(LogLevel.ERROR, "Canclled.", ""); } }); }
public async void Require_Named_With_Explicit_Name_Succeeds() { var subject = new TestObject(); var manager = new RuntimeManager(new ManualManagerSettings { ScriptTimeoutMilliSeconds = 0 }); await manager.ExecuteAsync("testscript", @"var testObject = requireNamed('testRequire', '.\\TestDiffNameRequire.js'); subject.Count = 10; subject.TestString = testObject.getText();", new ExecutionOptions { HostObjects = new List<HostObject> { new HostObject { Name = "subject", Target = subject } }, }); subject.Name.ShouldEqual("Name"); subject.Count.ShouldEqual(10); subject.TestString.ShouldEqual("testText"); }
private void invokeJsCode(string code, bool isDebuger = false) { code = code.Replace("``{{", "\"+").Replace("}}``", "+\"").Replace("`{{", "\'+").Replace("}}`", "+\'").Replace("{{", "\"+").Replace("}}", "+\""); bool tryCatch = this.catchBox.CheckState.Equals(CheckState.Checked); isRun = true; Enable(false); Task.Factory.StartNew(async() => { try { var codeLines = string.Join("\n", code.Split('\n').ToList().Select(r => (tryCatch? " " : " ") + r)); if (codeLines.StartsWith(" ")) { codeLines = codeLines.Substring(8); } else if (codeLines.StartsWith(" ")) { codeLines = codeLines.Substring(4); } if (isDebuger) { if (tryCatch) { code = $@" var tabris,console; (function () {{ debugger; try{{ tabris = tabris || require('javascript_tabris'); console = console || this.tabris.create('LOG'); {codeLines} }}catch(err){{ host.err=err.message; host.ex=err; }} }})()"; } else { code = $@" var tabris,console; (function () {{ debugger; tabris = tabris || require('javascript_tabris'); console = console || this.tabris.create('LOG'); {codeLines} }})()"; } //code = "debugger;\nvar tabris;\n" + "(function (){\n tabris = tabris || require('javascript_tabris'); \n" + code + "\n})();"; } else { if (tryCatch) { code = $@" var tabris,console; (function () {{ try{{ tabris = tabris || require('javascript_tabris'); console = console || this.tabris.create('LOG'); {codeLines} }}catch(err){{ host.err=err.message; host.ex=err; }} }})()"; //code = "var tabris;\n" + "(function (){\n tabris = tabris || require('javascript_tabris'); \n" + code + "\n})();"; } else { code = $@" var tabris,console; (function () {{ tabris = tabris || require('javascript_tabris'); console = console || this.tabris.create('LOG'); {codeLines} }})()"; } } dynamic host = new ExpandoObject(); var option = new ExecutionOptions { HostObjects = new List <HostObject> { new HostObject { Name = "host", Target = host } } }; var scriptAwaiter = new ScriptAwaiter(); if (code.Contains("scriptAwaiter")) { option.HostObjects.Add(new HostObject { Name = "scriptAwaiter", Target = scriptAwaiter }); } try { await manager.ExecuteAsync(Guid.NewGuid().ToString(), code, option); if (code.Contains("scriptAwaiter")) { await scriptAwaiter.T; } } catch (Exception) { } finally { if (isDebuger) { this.BeginInvoke(new EventHandler(delegate { this.codemirrow.Visible = true; debuggerBrower.Visible = false; })); } } try { if (!string.IsNullOrEmpty(host.err.ToString())) { logAction(LogLevel.ERROR, host.err, ""); } var exception = host.ex as DynamicObject; if (exception != null) { var kv = exception.GetDynamicProperties(); if (kv != null) { foreach (var itemKeyValuePair in kv) { if (itemKeyValuePair.Value is Exception) { Exception ex = (Exception)itemKeyValuePair.Value; while (ex.InnerException != null) { ex = ex.InnerException; } logAction(LogLevel.ERROR, ex.Message, ""); } } } } } catch (Exception ex) { } //if (Index == 0) //{ // Thread.Sleep(10000); // logAction(LogLevel.ERROR, "ces", ""); //} } catch (ScriptEngineException ex) { logAction(LogLevel.ERROR, ((Microsoft.ClearScript.ScriptEngineException)ex).ErrorDetails, ""); } catch (Exception ex) { logAction(LogLevel.ERROR, ex.Message, ""); } finally { Enable(true); isRun = false; } }).ContinueWith((t) => { if (t.IsFaulted) { Exception ex = t.Exception; while (ex is AggregateException && ex.InnerException != null) { ex = ex.InnerException; } logAction(LogLevel.ERROR, ex.Message, ""); } else if (t.IsCanceled) { logAction(LogLevel.ERROR, "Canclled.", ""); } }); }
public async void Require_Specified_As_Script_Local_Path_Is_Used() { var subject = new TestObject(); var manager = new RuntimeManager(new ManualManagerSettings {ScriptTimeoutMilliSeconds = 0}); await manager.ExecuteAsync("testscript", @"var testObject = require('.\\TestRequire.js'); subject.Count = 10; subject.TestString = testObject.getText();", new ExecutionOptions { HostObjects = new List<HostObject> {new HostObject {Name = "subject", Target = subject}}, }); subject.Name.ShouldEqual("Name"); subject.Count.ShouldEqual(10); subject.TestString.ShouldEqual("testText"); }
public async void Script_With_No_Subject_Can_Be_Executed() { var manager = new RuntimeManager(new ManualManagerSettings()); await manager.ExecuteAsync("test", "var i = 0; i++;"); }