示例#1
0
        public async Task <Result> ExecuteScriptAsync(string script, CancellationToken cancellationToken)
        {
            using (var context = new ZvsContext(EntityContextConnection))
            {
                JintEngine.SetValue("zvsContext", context);

                JintEngine.SetValue("logInfo", new Action <object>(LogInfo));
                JintEngine.SetValue("logWarn", new Action <object>(LogWarn));
                JintEngine.SetValue("logError", new Action <object>(LogError));
                JintEngine.SetValue("setTimeout", new Action <string, double>(SetTimeout));
                JintEngine.SetValue("shell", new Func <string, string, Process>(Shell));

                JintEngine.SetValue("runDeviceNameCommandName", new Func <string, string, string, Result>(RunDeviceNameCommandName));
                JintEngine.SetValue("runCommand", new Func <int, string, string, Result>(RunCommand));

                JintEngine.SetValue("require", new Action <string>(Require));

                JintEngine.SetValue("mappath", new Func <string, string>(MapPath));
                try
                {
                    //pull out import statements
                    //import them into the JintEngine by running each script
                    //then run the JintEngine as normal

                    var result = await Task.Run(() => JintEngine.Execute(script), cancellationToken);

                    return(Result.ReportSuccessFormat("JavaScript execution complete. {0}", result));
                }
                catch (Exception ex)
                {
                    return(Result.ReportErrorFormat("JavaScript execution error. {0}", ex.Message));
                }
            }
        }
示例#2
0
        public void AddHostObject(object hostObject, string name)
        {
            switch (Type)
            {
            case JsScriptRunnerType.Jint:
            {
                JintEngine.SetValue(name, hostObject);          // pass 'scriptingConnector' to Js
                break;
            }

            case JsScriptRunnerType.ClearScriptDebugMode:      // fallback to JsScriptRunnerType.ClearScript
            case JsScriptRunnerType.ClearScript:
            {
                ClearScriptEngine.AddHostObject(name, hostObject);          // pass 'scriptingConnector' to Js
                break;
            }
            }
        }