Beispiel #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, LexicalEnvironment env)
        {
            var functionObject = new ScriptFunctionInstance(
                Engine,
                functionDeclaration,
                env,
                functionDeclaration.Strict || _engine._isStrict);

            return(functionObject);
        }
Beispiel #2
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);
        }
Beispiel #3
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]);
            }

            IFunction function;

            try
            {
                var functionExpression = "function f(" + p + ") { " + body + "}";
                var parser             = new JavaScriptParser(functionExpression, ParserOptions);
                function = (IFunction)parser.ParseProgram().Body[0];
            }
            catch (ParserException)
            {
                ExceptionHelper.ThrowSyntaxError(_engine);
                return(null);
            }

            var functionObject = new ScriptFunctionInstance(
                Engine,
                function,
                LexicalEnvironment.NewDeclarativeEnvironment(Engine, Engine.ExecutionContext.LexicalEnvironment),
                function.Strict
                )
            {
                Extensible = true
            };

            return(functionObject);
        }
Beispiel #4
0
        /// <summary>
        /// https://tc39.es/ecma262/#sec-runtime-semantics-instantiatefunctionobject
        /// </summary>
        internal FunctionInstance InstantiateFunctionObject(JintFunctionDefinition functionDeclaration, EnvironmentRecord env)
        {
            var functionObject = new ScriptFunctionInstance(
                Engine,
                functionDeclaration,
                env,
                functionDeclaration.ThisMode)
            {
                _realm = _realm
            };

            functionObject.MakeConstructor();

            return(functionObject);
        }
Beispiel #5
0
        public ObjectInstance Construct(JsValue[] arguments, JsValue newTarget)
        {
            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]);
            }

            IFunction function;

            try
            {
                var functionExpression = "function f(" + p + ") { " + body + "}";
                var parser             = new JavaScriptParser(functionExpression, ParserOptions);
                function = (IFunction)parser.ParseScript().Body[0];
            }
            catch (ParserException)
            {
                return(ExceptionHelper.ThrowSyntaxError <ObjectInstance>(_engine));
            }

            var functionObject = new ScriptFunctionInstance(
                Engine,
                function,
                _engine.GlobalEnvironment,
                function.Strict);

            return(functionObject);
        }
Beispiel #6
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;
            
        }
Beispiel #7
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;
        }
Beispiel #8
0
        public JsValue EvaluateFunctionExpression(FunctionExpression functionExpression)
        {
            var funcEnv = LexicalEnvironment.NewDeclarativeEnvironment(_engine, _engine.ExecutionContext.LexicalEnvironment);
            var envRec = (DeclarativeEnvironmentRecord)funcEnv.Record;

            if (functionExpression.Id != null && !String.IsNullOrEmpty(functionExpression.Id.Name))
            {
                envRec.CreateMutableBinding(functionExpression.Id.Name);
            }

            var closure = new ScriptFunctionInstance(
                _engine,
                functionExpression,
                funcEnv,
                functionExpression.Strict
                );

            if (functionExpression.Id != null && !String.IsNullOrEmpty(functionExpression.Id.Name))
            {
                envRec.InitializeImmutableBinding(functionExpression.Id.Name, closure);
            }

            return closure;
        }
Beispiel #9
0
        public JsValue EvaluateObjectExpression(ObjectExpression objectExpression)
        {
            // http://www.ecma-international.org/ecma-262/5.1/#sec-11.1.5

            var obj = _engine.Object.Construct(Arguments.Empty);
            foreach (var property in objectExpression.Properties)
            {
                var propName = property.Key.GetKey();
                var previous = obj.GetOwnProperty(propName);
                PropertyDescriptor propDesc;

                switch (property.Kind)
                {
                    case PropertyKind.Data:
                        var exprValue = _engine.EvaluateExpression(property.Value);
                        var propValue = _engine.GetValue(exprValue);
                        propDesc = new PropertyDescriptor(propValue, true, true, true);
                        break;

                    case PropertyKind.Get:
                        var getter = property.Value as FunctionExpression;

                        if (getter == null)
                        {
                            throw new JavaScriptException(_engine.SyntaxError);
                        }

                        ScriptFunctionInstance get; 
                        using (new StrictModeScope(getter.Strict))
                        {
                            get = new ScriptFunctionInstance(
                                _engine,
                                getter,
                                _engine.ExecutionContext.LexicalEnvironment, 
                                StrictModeScope.IsStrictModeCode
                            );
                        }

                        propDesc = new PropertyDescriptor(get: get, set: null, enumerable: true, configurable:true);
                        break;
                    
                    case PropertyKind.Set:
                        var setter = property.Value as FunctionExpression;

                        if (setter == null)
                        {
                            throw new JavaScriptException(_engine.SyntaxError);
                        }

                        ScriptFunctionInstance set;
                        using (new StrictModeScope(setter.Strict))
                        {
                            
                            set = new ScriptFunctionInstance(
                                _engine,
                                setter,
                                _engine.ExecutionContext.LexicalEnvironment,
                                StrictModeScope.IsStrictModeCode
                                );
                        }
                        propDesc = new PropertyDescriptor(get:null, set: set, enumerable: true, configurable: true);
                        break;

                    default:
                        throw new ArgumentOutOfRangeException();
                }

                if (previous != PropertyDescriptor.Undefined)
                {
                    if (StrictModeScope.IsStrictModeCode && previous.IsDataDescriptor() && propDesc.IsDataDescriptor())
                    {
                        throw new JavaScriptException(_engine.SyntaxError);
                    }

                    if (previous.IsDataDescriptor() && propDesc.IsAccessorDescriptor())
                    {
                        throw new JavaScriptException(_engine.SyntaxError);
                    }

                    if (previous.IsAccessorDescriptor() && propDesc.IsDataDescriptor())
                    {
                        throw new JavaScriptException(_engine.SyntaxError);
                    }

                    if (previous.IsAccessorDescriptor() && propDesc.IsAccessorDescriptor())
                    {
                        if (propDesc.Set.HasValue && previous.Set.HasValue)
                        {
                            throw new JavaScriptException(_engine.SyntaxError);
                        }

                        if (propDesc.Get.HasValue && previous.Get.HasValue)
                        {
                            throw new JavaScriptException(_engine.SyntaxError);
                        }
                    }
                }

                obj.DefineOwnProperty(propName, propDesc, false);
            }

            return obj;
        }
Beispiel #10
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);
        }
Beispiel #11
0
        public ObjectInstance Construct(JsValue[] arguments, JsValue newTarget)
        {
            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]);
            }

            IFunction function;

            try
            {
                string functionExpression;
                if (argCount == 0)
                {
                    functionExpression = "function f(){}";
                }
                else
                {
                    functionExpression = "function f(";
                    if (p.IndexOf('/') != -1)
                    {
                        // ensure comments don't screw up things
                        functionExpression += "\n" + p + "\n";
                    }
                    else
                    {
                        functionExpression += p;
                    }

                    functionExpression += ")";

                    if (body.IndexOf('/') != -1)
                    {
                        // ensure comments don't screw up things
                        functionExpression += "{\n" + body + "\n}";
                    }
                    else
                    {
                        functionExpression += "{" + body + "}";
                    }
                }

                var parser = new JavaScriptParser(functionExpression, ParserOptions);
                function = (IFunction)parser.ParseScript().Body[0];
            }
            catch (ParserException)
            {
                return(ExceptionHelper.ThrowSyntaxError <ObjectInstance>(_engine));
            }

            var functionObject = new ScriptFunctionInstance(
                Engine,
                function,
                _engine.GlobalEnvironment,
                function.Strict);

            // the function is not actually a named function
            functionObject.SetFunctionName(_functionNameAnonymous, force: true);

            return(functionObject);
        }
Beispiel #12
0
        /// <summary>
        /// https://tc39.es/ecma262/#sec-createdynamicfunction
        /// </summary>
        private ObjectInstance Construct(JsValue[] arguments, JsValue newTarget)
        {
            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]);
            }

            IFunction function = null;

            try
            {
                string functionExpression;
                if (argCount == 0)
                {
                    functionExpression = "function f(){}";
                }
                else
                {
                    functionExpression = "function f(";
                    if (p.IndexOf('/') != -1)
                    {
                        // ensure comments don't screw up things
                        functionExpression += "\n" + p + "\n";
                    }
                    else
                    {
                        functionExpression += p;
                    }

                    functionExpression += ")";

                    if (body.IndexOf('/') != -1)
                    {
                        // ensure comments don't screw up things
                        functionExpression += "{\n" + body + "\n}";
                    }
                    else
                    {
                        functionExpression += "{" + body + "}";
                    }
                }

                var parser = new JavaScriptParser(functionExpression, ParserOptions);
                function = (IFunction)parser.ParseScript().Body[0];
            }
            catch (ParserException)
            {
                ExceptionHelper.ThrowSyntaxError(_realm);
            }

            // TODO generators etc, rewrite logic
            var proto = GetPrototypeFromConstructor(newTarget, static intrinsics => intrinsics.Function.PrototypeObject);

            var functionObject = new ScriptFunctionInstance(
                Engine,
                function,
                _realm.GlobalEnv,
                function.Strict,
                proto)
            {
                _realm = _realm
            };

            functionObject.MakeConstructor();

            // the function is not actually a named function
            functionObject.SetFunctionName(_functionNameAnonymous, force: true);

            return(functionObject);
        }