static void OriginalMain() { var done = false; var sw = new Stopwatch(); sw.Start(); for (var i = 0; i < N; i++) { var jintEngine = new Jint.Engine(); jintEngine.Execute(currentScript); done = jintEngine.GetValue("done").AsBoolean(); } sw.Stop(); Console.WriteLine("jint: " + sw.ElapsedMilliseconds / N + " - " + done); done = false; sw.Restart(); for (var i = 0; i < N; i++) { var ironjsEngine = new IronJS.Hosting.CSharp.Context(); ironjsEngine.Execute(currentScript); done = ironjsEngine.GetGlobalAs<bool>("done"); } sw.Stop(); Console.WriteLine("ironjs: " + sw.ElapsedMilliseconds / N + " - " + done); done = false; sw.Restart(); for (var i = 0; i < N; i++) { using (var jsNetEngine = new Noesis.Javascript.JavascriptContext()) { jsNetEngine.Run(currentScript); done = (bool)jsNetEngine.GetParameter("done"); } } sw.Stop(); Console.WriteLine("js.net: " + sw.ElapsedMilliseconds / N + " - " + done); done = false; sw.Restart(); for (var i = 0; i < N; i++) { var jurassicEngine = new Jurassic.ScriptEngine(); jurassicEngine.Execute(currentScript); done = jurassicEngine.GetGlobalValue<bool>("done"); } sw.Stop(); Console.WriteLine("jurassic: " + sw.ElapsedMilliseconds / N + " - " + done); done = false; sw.Restart(); for (var i = 0; i < N; i++) { using (var clearscriptV8 = new Microsoft.ClearScript.V8.V8ScriptEngine()) { clearscriptV8.Execute(currentScript); done = clearscriptV8.Script.done; } } sw.Stop(); Console.WriteLine("clearscriptV8: " + sw.ElapsedMilliseconds / N + " - " + done); done = false; sw.Restart(); using (var clearscriptV8 = new Microsoft.ClearScript.V8.V8Runtime()) { var compiled = clearscriptV8.Compile(currentScript); for (var i = 0; i < N; i++) { using (var engine = clearscriptV8.CreateScriptEngine()) { engine.Evaluate(compiled); done = engine.Script.done; } } } sw.Stop(); Console.WriteLine("clearscriptV8 compiled: " + sw.ElapsedMilliseconds / N + " - " + done); done = false; sw.Restart(); for (var i = 0; i < N; i++) { var nilcontext = new NiL.JS.Core.Context(); nilcontext.Eval(currentScript); done = (bool)nilcontext.GetVariable("done"); } sw.Stop(); Console.WriteLine("niljs: " + sw.ElapsedMilliseconds / N + " - " + done); Console.Read(); }
public HandlebarsCompiler() { Engine = new Jurassic.ScriptEngine(); var ass = typeof(HandlebarsCompiler).Assembly; Engine.Execute("var exports = {};"); Engine.Execute("var module = {};"); Engine.Execute(GetEmbeddedResource("HandlebarsHelper.Scripts.handlebars-v2.0.0.js", ass)); Engine.Execute(GetEmbeddedResource("HandlebarsHelper.Scripts.ember-template-compiler.js", ass)); Engine.Execute("var precompile = exports.precompile;"); Compressor = new JavaScriptCompressor(); }
public CVG Process(CVG inputdata, object otherdata = null) { try { //HACK: this feels wrong if (!String.IsNullOrEmpty(CurrentState.Settings.Functions)) { _jsengine.Execute(CurrentState.Settings.Functions); } // Debug.WriteLine("Process();"); //INPUT HACKS // _jsengine.SetGlobalValue("gate_input", inputdata.Gate); // _jsengine.SetGlobalValue("cv_input", inputdata.CV); // _jsengine.SetGlobalValue("parameters_input", inputdata.Parameters); // _jsengine.Execute("var cvg_input = {}; cvg_input['Gate'] = gate_input;cvg_input['CV'] = cv_input;cvg_input['Parameters'] = parameters_input;"); //HACK: i hate this if (otherdata != null) { _jsengine.SetGlobalValue("otherdata_raw", Newtonsoft.Json.JsonConvert.SerializeObject(otherdata)); _jsengine.Execute("var otherdata = JSON.parse(otherdata_raw)"); } _jsengine.SetGlobalValue("cvg_input_raw", Newtonsoft.Json.JsonConvert.SerializeObject(inputdata)); _jsengine.Execute("var cvg_input = JSON.parse(cvg_input_raw)"); //TODO: this seems log winded _jsengine.Execute("var cvg_result = Process(cvg_input) "); //OUTPUT HACKS _jsengine.Execute("; if (cvg_result === undefined){ cvg_result = cvg_input}; var cvg_result_raw = JSON.stringify(cvg_result); "); // _jsengine.Execute(" var gate_result = cvg_result['Gate'] ;var cv_result = cvg_result['CV'] ;var parameters_result = cvg_result['Parameters'] ;"); //var gate_result = (Dictionary<string, List<bool>>)_jsengine.GetGlobalValue("gate_input"); //var cv_result = (Dictionary<string, List<short>>)_jsengine.GetGlobalValue("cv_result"); //var parameters_result = (Dictionary<string, List<short>>)_jsengine.GetGlobalValue("parameters_result"); //HACK: i hate this return(Newtonsoft.Json.JsonConvert.DeserializeObject <CVG>(_jsengine.GetGlobalValue <string>("cvg_result_raw"))); //return new CVG(gate_result, cv_result, parameters_result); }catch (Exception ex) { Debug.WriteLine(ex); } return(inputdata); }
protected override void InnerExecute(string code) { try { _jsEngine.Execute(code); } catch (OriginalJsException e) { throw ConvertJavascriptExceptionToJsRuntimeException(e); } }
public bool Jurassic() { bool done = false; for (var i = 0; i < N; i++) { sharedJurassic.Execute(Script); done |= sharedJurassic.GetGlobalValue <bool>("done"); } return(done); }
public void Execute(string script, params object[] parameters) { var engine = new Jurassic.ScriptEngine(); int i = 0; foreach (var p in parameters) { engine.SetGlobalValue("parameter" + i, p); i++; } engine.Execute(script); }
protected override void InnerExecute(string code, string documentName) { string uniqueDocumentName = GetUniqueDocumentName(documentName, false); lock (_executionSynchronizer) { try { var source = new OriginalStringScriptSource(code, uniqueDocumentName); _jsEngine.Execute(source); } catch (OriginalJsException e) { throw ConvertJavascriptExceptionToJsRuntimeException(e); } catch (NotImplementedException e) { throw new JsRuntimeException(e.Message, e); } } }
public static void Execute(string script) { if (Engine == JSEngine.JScript) { throw new NotImplementedException(); } else { InitializeJurassic(); jurassicScriptEngine.Execute(script); } }
private ResultPerPageFree InnerUserList(string token, int pageSize, int pageIndex) { var dic = new Dictionary <string, object> { { "t", "user/index" }, { "pagesize", pageSize }, { "pageidx", pageIndex }, { "type", "0" }, { "groupid", GroupId }, { "token", token }, { "lang", Language }, }; //var s = HttpHelper.GetString(DefaultUserListUrl, dic, _cc, null); var stream = HttpExtendedHelper.Get(DefaultUserListUrl, dic, _cc, null); var doc = new HtmlDocument(); doc.Load(stream, Encoding); var ss = doc.DocumentNode.SelectNodes("//script"); var se = new Jurassic.ScriptEngine(); foreach (var s in ss.Where(e => e.GetAttributeValue("src", string.Empty).Equals(string.Empty))) { try { se.Execute("if(typeof(wx)=='undefined') wx={}"); se.Execute(s.InnerText); } catch { // se.Execute("if(!wxwx.cgiData)delete wx"); } } var temp = se.GetGlobalValue("wx") as Jurassic.Library.ObjectInstance; var r = ResultPerPageFree.FromObjectInstance(temp.GetPropertyValue("cgiData") as ObjectInstance); return(r); }
public bool Jurassic(Parameter parameter) { StringBuilder statementBuilder = new StringBuilder(); for (int i = 0; i < parameter.Statements.Length; ++i) { statementBuilder.Append("setValue(").Append(i).Append(", ").Append(parameter.Statements[i]).AppendLine(");"); } Jurassic.ScriptEngine engine = new Jurassic.ScriptEngine(); int[] results = new int[parameter.Statements.Length]; engine.SetGlobalFunction("setValue", new Action <int, int>((index, value) => results[index] = value)); engine.Execute(statementBuilder.ToString()); return(Assert(results, parameter.Sum)); }
public void Process(Page page) { if (_language == Language.Javascript) { //engine.eval(defines + "\n" + script, context); // NativeObject o = (NativeObject) engine.get("result"); // if (o != null) { // for (Object o1 : o.getIds()) { // string key = string.valueOf(o1); // page.getResultItems().put(key, NativeObject.getProperty(o, key)); // } // } string realScript = _defines + Environment.NewLine + _script; Jurassic.ScriptEngine engine = new Jurassic.ScriptEngine(); engine.EnableExposedClrTypes = true; engine.SetGlobalValue("page", page); engine.SetGlobalValue("config", Site); engine.Execute(realScript); } else if (_language == Language.Python) { //RubyHash oRuby = (RubyHash)engine.eval(defines + "\n" + script, context); //Iterator itruby = oRuby.entrySet().iterator(); //while (itruby.hasNext()) //{ // Map.Entry pairs = (Map.Entry)itruby.next(); // page.getResultItems().put(pairs.getKey().toString(), pairs.getValue()); //} } else if (_language == Language.Ruby) { //engine.eval(defines + "\n" + script, context); //PyDictionary oJython = (PyDictionary)engine.get("result"); //Iterator it = oJython.entrySet().iterator(); //while (it.hasNext()) //{ // Map.Entry pairs = (Map.Entry)it.next(); // page.getResultItems().put(pairs.getKey().toString(), pairs.getValue()); //} } }
static object EvalJScript(string jScript, params string[] dependencies) { var engine = new Jurassic.ScriptEngine(); var scriptWithDependencies = string.Join("\r\n\r\n", dependencies) + "\r\n" + jScript; object result = null; try { var evalJScript = engine.Evaluate(scriptWithDependencies); if (evalJScript is Jurassic.Library.UserDefinedFunction) { engine.Execute(scriptWithDependencies); return engine.Evaluate("out()"); } return evalJScript; } catch (Exception ex) { throw new InvalidJavascriptException(jScript, ex); } return result; }
private static void ExecuteWithJurrasic(Test test) { Execute("jurassic", test, () => { var jurassicEngine = new Jurassic.ScriptEngine(); jurassicEngine.Execute(test.Content); }); }
public static void Execute(string script) { InitializeJurassic(); jurassicScriptEngine.Execute(script); }
/// <summary> /// Turn the encrypted s parameter into a valid signature /// </summary> /// <param name="s">s Parameter value of the URL parameters</param> /// <returns></returns> string DecryptSignature(string javascriptUrl, string s) { if (string.IsNullOrEmpty(s)) { return(string.Empty); } // try decryption by executing the Javascript from Youtube if (!string.IsNullOrEmpty(javascriptUrl)) { try { if (javascriptUrl.StartsWith("//")) { javascriptUrl = "http:" + javascriptUrl; } /* * var match = Regex.Match(javascriptUrl, @".*?-(?<id>[a-zA-Z0-9_-]+)(?:/watch_as3|/html5player)?\.(?<ext>[a-z]+)$"); * if (!match.Success) * throw new Exception(string.Format("Cannot identify player {0}", javascriptUrl)); * var player_type = match.Groups["ext"]; * var player_id = match.Groups["id"]; */ string jsContent = WebCache.Instance.GetWebData(javascriptUrl); string signatureMethodName = Regex.Match(jsContent, @"\.sig\|\|([a-zA-Z0-9$]+)\(").Groups[1].Value; Jurassic.ScriptEngine engine; if (!cachedJavascript.TryGetValue(jsContent, out engine)) { engine = new Jurassic.ScriptEngine(); // define globals that are used in the script engine.Global["window"] = engine.Global; engine.Global["document"] = engine.Global; engine.Global["navigator"] = engine.Global; // this regexp is not valid for .net but js : https://developer.mozilla.org/de/docs/Web/JavaScript/Reference/Global_Objects/RegExp var fixedJs = jsContent.Replace("[^]", "."); // cut out the global function(){...} surrounding everything, so our method is defined global in Jurassic fixedJs = fixedJs.Substring(fixedJs.IndexOf('{') + 1); fixedJs = fixedJs.Substring(0, fixedJs.LastIndexOf('}')); // due to js nature - all methods called in our target function must be defined before, so for performance cut off where our function ends var method = Regex.Match(fixedJs, "(function\\s+" + signatureMethodName + @".*?)function [a-zA-Z]+\(\)").Groups[1]; fixedJs = fixedJs.Substring(0, method.Index + method.Length); engine.Execute(fixedJs); cachedJavascript.Add(jsContent, engine); } string decrypted = engine.CallGlobalFunction(signatureMethodName, s).ToString(); if (!string.IsNullOrEmpty(decrypted)) { return(decrypted); } else { Log.Info("Javascript decryption function returned nothing!"); } } catch (Exception ex) { Log.Info("Signature decryption by executing the Javascript failed: {0}", ex.Message); } } else { Log.Info("No Javascript url for decrpyting signature!"); } return(string.Empty); }
static void Main() { const bool runIronJs = true; const bool runJint = true; const bool runJurassic = true; const int iterations = 1000; const bool reuseEngine = false; var watch = new Stopwatch(); if (runIronJs) { IronJS.Hosting.CSharp.Context ironjs; ironjs = new IronJS.Hosting.CSharp.Context(); ironjs.Execute(Script); watch.Restart(); for (var i = 0; i < iterations; i++) { if (!reuseEngine) { ironjs = new IronJS.Hosting.CSharp.Context(); } ironjs.Execute(Script); } Console.WriteLine("IronJs: {0} iterations in {1} ms", iterations, watch.ElapsedMilliseconds); } if (runJint) { Engine jint; jint = new Engine(); jint.Execute(Script); watch.Restart(); for (var i = 0; i < iterations; i++) { if (!reuseEngine) { jint = new Jint.Engine(); } jint.Execute(Script); } Console.WriteLine("Jint: {0} iterations in {1} ms", iterations, watch.ElapsedMilliseconds); } if (runJurassic) { Jurassic.ScriptEngine jurassic; jurassic = new Jurassic.ScriptEngine(); jurassic.Execute(Script); watch.Restart(); for (var i = 0; i < iterations; i++) { if (!reuseEngine) { jurassic = new Jurassic.ScriptEngine(); } jurassic.Execute(Script); } Console.WriteLine("Jurassic: {0} iterations in {1} ms", iterations, watch.ElapsedMilliseconds); } }
private ResultPerPage InnerUserList(string token, int pageSize, int pageIndex) { var dic = new Dictionary<string, object> { {"t", "user/index"}, {"pagesize", pageSize}, {"pageidx", pageIndex}, {"type", "0"}, {"groupid", GroupId}, {"token", token}, {"lang", Language}, }; //var s = HttpHelper.GetString(DefaultUserListUrl, dic, _cc, null); var stream = HttpHelper.Get(DefaultUserListUrl, dic, _cc, null); var doc = new HtmlDocument(); doc.Load(stream, Encoding); var ss = doc.DocumentNode.SelectNodes("//script"); var se = new Jurassic.ScriptEngine(); foreach (var s in ss.Where(e => e.GetAttributeValue("src", string.Empty).Equals(string.Empty))) { try { se.Execute(s.InnerText); } catch { } } var temp = se.GetGlobalValue("cgiData") as Jurassic.Library.ObjectInstance; var r = ResultPerPage.FromObjectInstance(temp); return r; }
public static void Benchmark(string code, double previousResult = 0) { // Run the javascript code. var engine = new Jurassic.ScriptEngine(); Benchmark(() => engine.Execute(new Jurassic.StringScriptSource(code)), previousResult); }
/// <summary> /// Turn the encrypted s parameter into a valid signature /// </summary> /// <param name="s">s Parameter value of the URL parameters</param> /// <returns></returns> string DecryptSignature(string javascriptUrl, string s) { if (string.IsNullOrEmpty(s)) return string.Empty; // try decryption by executing the Javascript from Youtube if (!string.IsNullOrEmpty(javascriptUrl)) { try { if (javascriptUrl.StartsWith("//")) javascriptUrl = "http:" + javascriptUrl; /* var match = Regex.Match(javascriptUrl, @".*?-(?<id>[a-zA-Z0-9_-]+)(?:/watch_as3|/html5player)?\.(?<ext>[a-z]+)$"); if (!match.Success) throw new Exception(string.Format("Cannot identify player {0}", javascriptUrl)); var player_type = match.Groups["ext"]; var player_id = match.Groups["id"]; */ string jsContent = WebCache.Instance.GetWebData(javascriptUrl); string signatureMethodName = Regex.Match(jsContent, @"\.sig\|\|([a-zA-Z0-9$]+)\(").Groups[1].Value; Jurassic.ScriptEngine engine; if (!cachedJavascript.TryGetValue(jsContent, out engine)) { engine = new Jurassic.ScriptEngine(); // define globals that are used in the script engine.Global["window"] = engine.Global; engine.Global["document"] = engine.Global; engine.Global["navigator"] = engine.Global; // this regexp is not valid for .net but js : https://developer.mozilla.org/de/docs/Web/JavaScript/Reference/Global_Objects/RegExp var fixedJs = jsContent.Replace("[^]", "."); // cut out the global function(){...} surrounding everything, so our method is defined global in Jurassic fixedJs = fixedJs.Substring(fixedJs.IndexOf('{') + 1); fixedJs = fixedJs.Substring(0, fixedJs.LastIndexOf('}')); // due to js nature - all methods called in our target function must be defined before, so for performance cut off where our function ends var method = Regex.Match(fixedJs, "(function\\s+" + signatureMethodName + @".*?)function [a-zA-Z]+\(\)").Groups[1]; fixedJs = fixedJs.Substring(0, method.Index + method.Length); engine.Execute(fixedJs); cachedJavascript.Add(jsContent, engine); } string decrypted = engine.CallGlobalFunction(signatureMethodName, s).ToString(); if (!string.IsNullOrEmpty(decrypted)) return decrypted; else Log.Info("Javascript decryption function returned nothing!"); } catch (Exception ex) { Log.Info("Signature decryption by executing the Javascript failed: {0}", ex.Message); } } else { Log.Info("No Javascript url for decrpyting signature!"); } return string.Empty; }
static void Main() { //const bool runIronJs = true; const bool runJint = true; const bool runJurassic = true; const int iterations = 1000; const bool reuseEngine = false; var watch = new Stopwatch(); //if (runIronJs) //{ // IronJS.Hosting.CSharp.Context ironjs; // ironjs = new IronJS.Hosting.CSharp.Context(); // ironjs.Execute(Script); // watch.Restart(); // for (var i = 0; i < iterations; i++) // { // if (!reuseEngine) // { // ironjs = new IronJS.Hosting.CSharp.Context(); // } // ironjs.Execute(Script); // } // Console.WriteLine("IronJs: {0} iterations in {1} ms", iterations, watch.ElapsedMilliseconds); //} if (runJint) { JSEngine jint; jint = new JSEngine(); jint.Execute(Script); watch.Restart(); for (var i = 0; i < iterations; i++) { if (!reuseEngine) { jint = new IridiumJS.JSEngine(); } jint.Execute(Script); } Console.WriteLine("Jint: {0} iterations in {1} ms", iterations, watch.ElapsedMilliseconds); } if (runJurassic) { Jurassic.ScriptEngine jurassic; jurassic = new Jurassic.ScriptEngine(); jurassic.Execute(Script); watch.Restart(); for (var i = 0; i < iterations; i++) { if (!reuseEngine) { jurassic = new Jurassic.ScriptEngine(); } jurassic.Execute(Script); } Console.WriteLine("Jurassic: {0} iterations in {1} ms", iterations, watch.ElapsedMilliseconds); } }
public object InstallBundle(Jurassic.ScriptEngine engine) { engine.Execute(Barista.Properties.Resources.lodash_min); return(Null.Value); }