Beispiel #1
2
 public static void JsReflection(string tsFileName, string jsonMapFn) {
   using (var engine = new Microsoft.ClearScript.V8.V8ScriptEngine()) {
     engine.Execute("typescript.js", File.ReadAllText(@"d:\LMCom\rew\Web4\JsLib\Scripts\typescript.js"));
     engine.Execute("underscore.js", File.ReadAllText(@"d:\LMCom\rew\Web4\JsLib\Scripts\underscore.js"));
     engine.Execute("GenerateReflection.js", File.ReadAllText(@"d:\LMCom\rew\Web4\Author\GenerateReflection.js"));
     var par = new reflectionObj { inpTSCode = File.ReadAllText(tsFileName) };
     engine.AddHostObject("inpTSCode", par);
     File.WriteAllText(jsonMapFn + ".json", (string)engine.Evaluate("author.parseReflection(inpTSCode.inpTSCode, false)"));
     File.WriteAllText(jsonMapFn + ".debug.json", (string)engine.Evaluate("author.parseReflection(inpTSCode.inpTSCode, true)"));
   }
 }
Beispiel #2
2
 public static string JSON2RJSON(string inpJson) {
   using (var engine = new Microsoft.ClearScript.V8.V8ScriptEngine()) {
     var script = Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().Location) + @"\RJSON.js";
     if (!File.Exists(script)) script = Machines.rootPath + @"JsLib\JS\External\RJSON.js";
     engine.Execute(script, File.ReadAllText(script));
     var par = new rjsonObj { inpJson = inpJson };
     engine.AddHostObject("inpJson", par);
     return (string)engine.Evaluate("JSON.stringify(RJSON.pack(JSON.parse(inpJson.inpJson)))");
   }
 }
        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();
        }
Beispiel #4
1
 private static void ExecuteWithClearScript(Test test)
 {
     Execute("clearscript", test, () =>
     {
         using (var clearscriptV8 = new Microsoft.ClearScript.V8.V8ScriptEngine())
         {
             clearscriptV8.Execute(test.Content);
         }
     });
 }
Beispiel #5
0
 protected override void InnerExecute(string code, string documentName)
 {
     try
     {
         _jsEngine.Execute(documentName, false, code);
     }
     catch (OriginalException e)
     {
         throw WrapScriptEngineException(e);
     }
     catch (OriginalInterruptedException e)
     {
         throw WrapScriptInterruptedException(e);
     }
 }
Beispiel #6
0
        /// <summary>
        /// 执行代码
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="code"></param>
        /// <param name="resultName"></param>
        /// <returns></returns>
        public T HandlerByCode <T>(string code, string resultName = "result")
        {
            using Microsoft.ClearScript.V8.V8ScriptEngine engine = GetEngine();
            string cmd = _libsPaths.Aggregate(string.Empty, (current, libPath) => current + $"{GetFileContent(libPath)}\r\n");

            cmd += code;
            engine.Execute(cmd);
            var result = ((object)engine.Script[resultName]).ToJson().JsonToDeserializeObject <T>();

            return(result);
        }
Beispiel #7
0
        private void ProcessRequire(string rootPath, Package package, Module module)
        {
            string currentPath = _fileSystem.Path.GetDirectoryName(module.FullPath);

            using (Microsoft.ClearScript.ScriptEngine engine = new Microsoft.ClearScript.V8.V8ScriptEngine())
            {
                Action <string> require = (entryPoint) =>
                {
                    ProcessModule(rootPath, package, entryPoint, currentPath);
                };
                engine.AddHostObject("require", new Action <string>(require));
                engine.Execute("(function(exports){try{" + module.TransformedContent + "}catch(e){}}({}));");
            }
        }
Beispiel #8
0
        /// <summary>
        /// 编译传递的JavaScript 代码
        /// </summary>
        /// <param name="cid">默认需求本期开奖id,回测系统使用</param>
        /// <param name="code">编译代码</param>
        /// <returns></returns>
        public MessageScriptModel Builder(string expect, string code)
        {
            StringBuilder stringBuilder = new StringBuilder();

            string system     = System.IO.File.ReadAllText(HttpContext.Current.Server.MapPath("/Js/CPQaunt.js"));
            string jsonparese = System.IO.File.ReadAllText(HttpContext.Current.Server.MapPath("/Js/json_parse.js"));
            string json2      = System.IO.File.ReadAllText(HttpContext.Current.Server.MapPath("/Js/json2.js"));

            stringBuilder.AppendLine(jsonparese);
            stringBuilder.AppendLine(json2);

            stringBuilder.AppendLine(system);
            stringBuilder.AppendLine(code);

            try
            {
                using (Microsoft.ClearScript.ScriptEngine engine = new Microsoft.ClearScript.V8.V8ScriptEngine())
                {
                    engine.AddHostObject("LotteryFacade", new CPQaunt.Facade.LotteryFacade());
                    engine.Execute(stringBuilder.ToString());

                    var s = engine.Script.main(expect);

                    MessageScriptModel message = new MessageScriptModel();
                    message.type   = MessageType.List;
                    message.status = true;

                    JavaScriptSerializer jsSerializer = new JavaScriptSerializer();
                    jsSerializer.MaxJsonLength = Int32.MaxValue;

                    message.numbers = jsSerializer.Deserialize <List <NumberModel> >(s);

                    return(message);
                }
            }
            catch (Exception e)
            {
                MessageScriptModel message = new MessageScriptModel();
                message.type    = MessageType.Log;
                message.status  = false;
                message.message = e.Message;
                return(message);
            }
        }
        public static ServerScriptModelRow Execute(
            Context context,
            SiteSettings ss,
            BaseItemModel itemModel,
            View view,
            ServerScript[] scripts,
            bool onTesting = false)
        {
            if (!(Parameters.Script.ServerScript != false &&
                  context.ContractSettings.NewFeatures() &&
                  context.ContractSettings.Script != false))
            {
                return(null);
            }
            if (!(context?.ServerScriptDepth < 10))
            {
                return(null);
            }
            itemModel = itemModel ?? new BaseItemModel();
            ServerScriptModelRow scriptValues = null;

            using (var model = new ServerScriptModel(
                       context: context,
                       ss: ss,
                       data: Values(
                           context: context,
                           ss: ss,
                           model: itemModel),
                       columns: Columns(
                           context: context,
                           ss: ss),
                       columnFilterHash: view?.ColumnFilterHash,
                       columnSorterHash: view?.ColumnSorterHash,
                       onTesting: onTesting))
            {
                using (var engine = new Microsoft.ClearScript.V8.V8ScriptEngine(
                           Microsoft.ClearScript.V8.V8ScriptEngineFlags.EnableDateTimeConversion))
                {
                    try
                    {
                        engine.ContinuationCallback = model.ContinuationCallback;
                        engine.AddHostObject("context", model.Context);
                        engine.AddHostObject("model", model.Model);
                        engine.AddHostObject("depts", model.Depts);
                        engine.AddHostObject("groups", model.Groups);
                        engine.AddHostObject("users", model.Users);
                        engine.AddHostObject("columns", model.Columns);
                        engine.AddHostObject("siteSettings", model.SiteSettings);
                        engine.AddHostObject("view", model.View);
                        engine.AddHostObject("items", model.Items);
                        engine.AddHostObject("hidden", model.Hidden);
                        engine.AddHostObject("extendedSql", model.ExtendedSql);
                        engine.AddHostObject("notifications", model.Notification);
                        foreach (var script in scripts)
                        {
                            engine.Execute(script.Body);
                        }
                    }
                    finally
                    {
                        engine.ContinuationCallback = null;
                    }
                }
                scriptValues = SetValues(
                    context: context,
                    ss: ss,
                    model: itemModel,
                    view: view,
                    data: model);
            }
            return(scriptValues);
        }