示例#1
0
        /// <summary>
        /// Creates a function based on an expression. Ex: "x + 1"
        /// </summary>
        public ScriptFunction(IScriptEngineFactory engineFactory, IEnumerable<string> argumentNames, string expression, bool addReturnStatement)
        {
            this.engineFactory = engineFactory;
            this.expression = expression;

            // generate the javascript for the function
            StringBuilder argsDecl = new StringBuilder();

            foreach (string arg in argumentNames)
            {
                if (argsDecl.Length > 0)
                    argsDecl.Append(",");
                argsDecl.Append(arg);
            }

            if(addReturnStatement)
                functionBody = "(" + argsDecl + "){return (" + expression + ");}";
            else
                functionBody = "(" + argsDecl + "){ " + expression + " }";

            // assign a unique name to this function
            lock (functionNames)
            {
                if (!functionNames.TryGetValue(functionBody, out functionName))
                {
                    functionName = "_func_" + functionNames.Count;
                    functionNames.Add(functionBody, functionName);
                }
            }

            // Verify the syntax of the function. This will also register the function with the current script engine.
            EnsureCompiled(engineFactory.GetScriptEngine());
        }
示例#2
0
        static Func <object, object> CreateScriptFunction(string argName1, string expression, IScriptEngineFactory factory, bool addReturnStatement)
        {
            ScriptFunction function  = new ScriptFunction(factory, new string[] { argName1 }, expression, addReturnStatement);
            Marshaler      marshaler = new Marshaler(factory.GetScriptEngine());

            return((a) => function.Evaluate(new object[] { a }, marshaler));
        }
示例#3
0
        public object Evaluate(IEnumerable <object> arguments, Marshaler marshaler)
        {
            ScriptEngine engine = engineFactory.GetScriptEngine();

            EnsureCompiled(engine);

            // call the function
            object[] wrappedArgs = arguments.Select(marshaler.Wrap).ToArray();
            object   result;

            try
            {
                result = engine.CallGlobalFunction(functionName, wrappedArgs);
            }
            catch (Jurassic.JavaScriptException err)
            {
                throw new ScriptFunctionEvaluationException(expression, err);
            }
            return(marshaler.Unwrap(result));
        }
示例#4
0
        /// <summary>
        /// Creates a function based on an expression. Ex: "x + 1"
        /// </summary>
        public ScriptFunction(IScriptEngineFactory engineFactory, IEnumerable <string> argumentNames, string expression, bool addReturnStatement)
        {
            this.engineFactory = engineFactory;
            this.expression    = expression;

            // generate the javascript for the function
            StringBuilder argsDecl = new StringBuilder();

            foreach (string arg in argumentNames)
            {
                if (argsDecl.Length > 0)
                {
                    argsDecl.Append(",");
                }
                argsDecl.Append(arg);
            }

            if (addReturnStatement)
            {
                functionBody = "(" + argsDecl + "){return (" + expression + ");}";
            }
            else
            {
                functionBody = "(" + argsDecl + "){ " + expression + " }";
            }

            // assign a unique name to this function
            lock (functionNames)
            {
                if (!functionNames.TryGetValue(functionBody, out functionName))
                {
                    functionName = "_func_" + functionNames.Count;
                    functionNames.Add(functionBody, functionName);
                }
            }

            // Verify the syntax of the function. This will also register the function with the current script engine.
            EnsureCompiled(engineFactory.GetScriptEngine());
        }
示例#5
0
        public IScriptEngine GetEngineByName(string shortName)
        {
            if (shortName == null)
            {
                throw new NullReferenceException();                   // NullPointerException();
            }
            //look for registered name first
            Object obj = nameAssociations.ContainsKey(shortName) ? nameAssociations[shortName] : null;

            if (null != obj)
            {
                IScriptEngineFactory spi = (IScriptEngineFactory)obj;
                try
                {
                    IScriptEngine engine = spi.GetScriptEngine();
                    engine.SetBindings(GetBindings(), ScriptContext_Fields.GLOBAL_SCOPE);
                    return(engine);
                }
                catch (System.Exception exp)
                {
                    throw exp;
                    //if (DEBUG) exp.printStackTrace();
                }
            }

            foreach (IScriptEngineFactory spi in engineSpis)
            {
                IList <String> names = null;
                try
                {
                    names = spi.Names;
                }
                catch (System.Exception exp)
                {
                    throw exp;
                    //if (DEBUG) exp.printStackTrace();
                }

                if (names != null)
                {
                    foreach (String name in names)
                    {
                        if (shortName.Equals(name))
                        {
                            try
                            {
                                IScriptEngine engine = spi.GetScriptEngine();
                                engine.SetBindings(GetBindings(), ScriptContext_Fields.GLOBAL_SCOPE);
                                return(engine);
                            }
                            catch (System.Exception exp)
                            {
                                throw exp;
                                //if (DEBUG) exp.printStackTrace();
                            }
                        }
                    }
                }
            }

            return(null);
        }