예제 #1
0
        private void EvaluateCommand(string command, object context, string fileName, AsyncResultCallback <object> callback)
        {
            object result = Script.Undefined;

            command = command.Substr(1, command.Length - 2).Trim();
            if (command.Length != 0)
            {
                WebCommand webCommand = WebCommand.TryParseCommand(command, _webRequest, _webResponse);
                if (webCommand != null)
                {
                    webCommand.HandleCommand(callback);
                    return;
                }
                else
                {
                    try {
                        Script.SetField(context, "$", _webRequest);
                        Script.SetField(context, "$$", _webResponse);

                        ScriptInstance script = ScriptEngine.CreateScript(command);
                        result = script.RunInNewContext(context);
                    }
                    catch (Exception e) {
                        Console.Error(e.Message);
                    }
                }
            }

            callback(null, result);
        }
예제 #2
0
        public object Execute(string scopePath, string scriptPath, string contextMember, object contextObject)
        {
            ScriptInstance script     = null;
            string         scriptCode = String.Empty;

            if (FileSystem.ExistsSync(scriptPath))
            {
                scriptCode = FileSystem.ReadFileTextSync(scriptPath, Encoding.UTF8);

                string sharedScriptPath = Path.Join(scopePath, "_shared.js");
                if (FileSystem.ExistsSync(sharedScriptPath))
                {
                    scriptCode = FileSystem.ReadFileTextSync(sharedScriptPath, Encoding.UTF8) + "\r\n" + scriptCode;
                }

                script = ScriptEngine.CreateScript(scriptCode);
            }

            if (script != null)
            {
                Dictionary <string, object> context = new Dictionary <string, object>();
                context["app"]     = _appObject;
                context["data"]    = _dataObject;
                context["console"] = typeof(Console);
                context["require"] = (Func <string, object>)LoadModule;

                if (String.IsNullOrEmpty(contextMember) == false)
                {
                    context[contextMember] = contextObject;
                }

                return(script.RunInNewContext(context));
            }

            return(Script.Undefined);
        }