コード例 #1
0
        /// <summary>
        /// http://www.ecma-international.org/ecma-262/5.1/#sec-13.2
        /// </summary>
        /// <param name="functionDeclaration"></param>
        /// <returns></returns>
        public FunctionInstance CreateFunctionObject(FunctionDeclaration functionDeclaration)
        {
            var functionObject = new ScriptFunctionInstance(
                Engine,
                functionDeclaration,
                LexicalEnvironment.NewDeclarativeEnvironment(Engine, Engine.ExecutionContext.LexicalEnvironment),
                functionDeclaration.Strict
                )
            {
                Extensible = true
            };

            return(functionObject);
        }
コード例 #2
0
        public ObjectInstance Construct(JsValue[] arguments)
        {
            var    argCount = arguments.Length;
            string p        = "";
            string body     = "";

            if (argCount == 1)
            {
                body = TypeConverter.ToString(arguments[0]);
            }
            else if (argCount > 1)
            {
                var firstArg = arguments[0];
                p = TypeConverter.ToString(firstArg);
                for (var k = 1; k < argCount - 1; k++)
                {
                    var nextArg = arguments[k];
                    p += "," + TypeConverter.ToString(nextArg);
                }

                body = TypeConverter.ToString(arguments[argCount - 1]);
            }

            var parameters = this.ParseArgumentNames(p);
            var parser     = new JavaScriptParser();
            FunctionExpression function;

            try
            {
                var functionExpression = "function(" + p + ") { " + body + "}";
                function = parser.ParseFunctionExpression(functionExpression);
            }
            catch (ParserException)
            {
                throw new JavaScriptException(Engine.SyntaxError);
            }

            // todo: check if there is not a way to use the FunctionExpression directly instead of creating a FunctionDeclaration
            var functionObject = new ScriptFunctionInstance(
                Engine,
                new FunctionDeclaration
            {
                Type = SyntaxNodes.FunctionDeclaration,
                Body = new BlockStatement
                {
                    Type = SyntaxNodes.BlockStatement,
                    Body = new [] { function.Body }
                },
                Parameters = parameters.Select(x => new Identifier
                {
                    Type = SyntaxNodes.Identifier,
                    Name = x
                }).ToArray(),
                FunctionDeclarations = function.FunctionDeclarations,
                VariableDeclarations = function.VariableDeclarations
            },
                LexicalEnvironment.NewDeclarativeEnvironment(Engine, Engine.ExecutionContext.LexicalEnvironment),
                function.Strict
                )
            {
                Extensible = true
            };

            return(functionObject);
        }