예제 #1
0
        protected override void Initialize()
        {
            _body = Build(_engine, _statement.Body);

            if (_statement.Init != null)
            {
                if (_statement.Init.Type == Nodes.VariableDeclaration)
                {
                    var d = (VariableDeclaration)_statement.Init;
                    if (d.Kind != VariableDeclarationKind.Var)
                    {
                        _boundNames = new List <string>();
                        d.GetBoundNames(_boundNames);
                    }
                    _initStatement = new JintVariableDeclaration(_engine, d);
                    _shouldCreatePerIterationEnvironment = d.Kind == VariableDeclarationKind.Let;
                }
                else
                {
                    _initExpression = JintExpression.Build(_engine, (Expression)_statement.Init);
                }
            }

            if (_statement.Test != null)
            {
                _test = JintExpression.Build(_engine, _statement.Test);
            }

            if (_statement.Update != null)
            {
                _increment = JintExpression.Build(_engine, _statement.Update);
            }
        }
예제 #2
0
        protected override void Initialize(EvaluationContext context)
        {
            var engine = context.Engine;

            _switchBlock  = new JintSwitchBlock(_statement.Cases);
            _discriminant = JintExpression.Build(engine, _statement.Discriminant);
        }
예제 #3
0
            public JintSwitchCase(Engine engine, SwitchCase switchCase)
            {
                Consequent          = new JintStatementList(null, switchCase.Consequent);
                LexicalDeclarations = HoistingScope.GetLexicalDeclarations(switchCase);

                if (switchCase.Test != null)
                {
                    Test = JintExpression.Build(engine, switchCase.Test);
                }
            }
예제 #4
0
        internal Completion Execute(EvaluationContext context)
        {
            if (Function.Expression)
            {
                _bodyExpression ??= JintExpression.Build(_engine, (Expression)Function.Body);
                var jsValue = _bodyExpression?.GetValue(context).Value ?? Undefined.Instance;
                return(new Completion(CompletionType.Return, jsValue, Function.Body.Location));
            }

            var blockStatement = (BlockStatement)Function.Body;

            _bodyStatementList ??= new JintStatementList(blockStatement, blockStatement.Body);
            return(_bodyStatementList.Execute(context));
        }
예제 #5
0
        public override string ToString()
        {
            var name = TypeConverter.ToString(Function.Get(CommonProperties.Name));

            if (string.IsNullOrWhiteSpace(name))
            {
                if (Expression is not null)
                {
                    name = JintExpression.ToString(Expression._expression);
                }
            }

            return(name ?? "(anonymous)");
        }
예제 #6
0
        private static bool TryGetComputedPropertyKey <T>(T expression, Engine engine, out JsValue propertyKey)
            where T : Expression
        {
            if (expression.Type == Nodes.Identifier ||
                expression.Type == Nodes.CallExpression ||
                expression.Type == Nodes.BinaryExpression ||
                expression.Type == Nodes.UpdateExpression ||
                expression is StaticMemberExpression)
            {
                propertyKey = TypeConverter.ToPropertyKey(JintExpression.Build(engine, expression).GetValue());
                return(true);
            }

            propertyKey = string.Empty;
            return(false);
        }
예제 #7
0
        protected override void Initialize(EvaluationContext context)
        {
            _lhsKind = LhsKind.Assignment;
            var engine = context.Engine;

            if (_leftNode is VariableDeclaration variableDeclaration)
            {
                _lhsKind = variableDeclaration.Kind == VariableDeclarationKind.Var
                    ? LhsKind.VarBinding
                    : LhsKind.LexicalBinding;

                var variableDeclarationDeclaration = variableDeclaration.Declarations[0];
                var id = variableDeclarationDeclaration.Id;
                if (_lhsKind == LhsKind.LexicalBinding)
                {
                    _tdzNames = new List <string>(1);
                    id.GetBoundNames(_tdzNames);
                }

                if (id is BindingPattern bindingPattern)
                {
                    _destructuring     = true;
                    _assignmentPattern = bindingPattern;
                }
                else
                {
                    var identifier = (Identifier)id;
                    _expr = new JintIdentifierExpression(identifier);
                }
            }
            else if (_leftNode is BindingPattern bindingPattern)
            {
                _destructuring     = true;
                _assignmentPattern = bindingPattern;
            }
            else if (_leftNode is MemberExpression memberExpression)
            {
                _expr = new JintMemberExpression(memberExpression);
            }
            else
            {
                _expr = new JintIdentifierExpression((Identifier)_leftNode);
            }

            _body  = Build(_forBody);
            _right = JintExpression.Build(engine, _rightExpression);
        }
예제 #8
0
 protected override void Initialize(EvaluationContext context)
 {
     if (_statement.Declaration is ClassDeclaration classDeclaration)
     {
         _classDefinition = new ClassDefinition(className: classDeclaration.Id?.Name, classDeclaration.SuperClass, classDeclaration.Body);
     }
     else if (_statement.Declaration is FunctionDeclaration functionDeclaration)
     {
         _functionDeclaration = new JintFunctionDeclarationStatement(functionDeclaration);
     }
     else if (_statement.Declaration is AssignmentExpression assignmentExpression)
     {
         _assignmentExpression = JintAssignmentExpression.Build(context.Engine, assignmentExpression);
     }
     else
     {
         _simpleExpression = JintExpression.Build(context.Engine, (Expression)_statement.Declaration);
     }
 }
예제 #9
0
    protected override void Initialize(EvaluationContext context)
    {
        if (_statement.Declaration != null)
        {
            switch (_statement.Declaration)
            {
            case Expression e:
                _declarationExpression = JintExpression.Build(context.Engine, e);
                break;

            case Statement s:
                _declarationStatement = Build(s);
                break;

            default:
                ExceptionHelper.ThrowNotSupportedException($"Statement {_statement.Declaration.Type} is not supported in an export declaration.");
                break;
            }
        }
    }
예제 #10
0
        private static bool TryGetComputedPropertyKey <T>(T expression, Engine engine, out JsValue propertyKey)
            where T : Expression
        {
            if (expression.Type is Nodes.Identifier
                or Nodes.CallExpression
                or Nodes.BinaryExpression
                or Nodes.UpdateExpression
                or Nodes.AssignmentExpression
                or Nodes.UnaryExpression
                or Nodes.MemberExpression
                or Nodes.LogicalExpression
                or Nodes.ConditionalExpression
                or Nodes.ArrowFunctionExpression
                or Nodes.FunctionExpression)
            {
                var context = engine._activeEvaluationContext;
                propertyKey = TypeConverter.ToPropertyKey(JintExpression.Build(engine, expression).GetValue(context).Value);
                return(true);
            }

            propertyKey = string.Empty;
            return(false);
        }
        protected override void Initialize()
        {
            _declarations = new ResolvedDeclaration[_statement.Declarations.Count];
            for (var i = 0; i < _declarations.Length; i++)
            {
                var declaration = _statement.Declarations[i];

                JintExpression left           = null;
                JintExpression init           = null;
                BindingPattern bindingPattern = null;

                if (declaration.Id is BindingPattern bp)
                {
                    bindingPattern = bp;
                }
                else
                {
                    left = JintExpression.Build(_engine, declaration.Id);
                }

                if (declaration.Init != null)
                {
                    init = JintExpression.Build(_engine, declaration.Init);
                }

                var leftIdentifier = left as JintIdentifierExpression;
                _declarations[i] = new ResolvedDeclaration
                {
                    Left        = left,
                    LeftPattern = bindingPattern,
                    LeftIdentifierExpression = leftIdentifier,
                    EvalOrArguments          = leftIdentifier?.HasEvalOrArguments == true,
                    Init = init
                };
            }
        }
예제 #12
0
 protected override void Initialize(EvaluationContext context)
 {
     _expression = JintExpression.Build(context.Engine, _statement.Expression);
 }
예제 #13
0
 public JintImportExpression(Import expression) : base(expression)
 {
     _initialized      = false;
     _importExpression = null !;
 }
 public JintIfStatement(Engine engine, IfStatement statement) : base(engine, statement)
 {
     _statementConsequent = Build(engine, _statement.Consequent);
     _test      = JintExpression.Build(engine, _statement.Test);
     _alternate = _statement.Alternate != null?Build(engine, _statement.Alternate) : null;
 }
 public JintExpressionStatement(Engine engine, ExpressionStatement statement) : base(engine, statement)
 {
     _expression = JintExpression.Build(engine, statement.Expression);
 }
예제 #16
0
 protected override void Initialize(EvaluationContext context)
 {
     _labelSetName = _statement.LabelSet?.Name;
     _body         = Build(_statement.Body);
     _test         = JintExpression.Build(context.Engine, _statement.Test);
 }
예제 #17
0
        /// <summary>
        /// https://tc39.es/ecma262/#sec-runtime-semantics-forin-div-ofbodyevaluation-lhs-stmt-iterator-lhskind-labelset
        /// </summary>
        private Completion BodyEvaluation(
            JintExpression lhs,
            JintStatement stmt,
            IIterator iteratorRecord,
            IterationKind iterationKind,
            LhsKind lhsKind,
            IteratorKind iteratorKind = IteratorKind.Sync)
        {
            var    oldEnv        = _engine.ExecutionContext.LexicalEnvironment;
            var    v             = Undefined.Instance;
            var    destructuring = _destructuring;
            string lhsName       = null;

            var completionType = CompletionType.Normal;
            var close          = false;

            try
            {
                while (true)
                {
                    LexicalEnvironment iterationEnv = null;
                    if (!iteratorRecord.TryIteratorStep(out var nextResult))
                    {
                        close = true;
                        return(new Completion(CompletionType.Normal, v, null, Location));
                    }

                    if (iteratorKind == IteratorKind.Async)
                    {
                        // nextResult = await nextResult;
                    }

                    var nextValue = nextResult.Get(CommonProperties.Value);
                    close = true;

                    Reference lhsRef = null;
                    if (lhsKind != LhsKind.LexicalBinding)
                    {
                        if (!destructuring)
                        {
                            lhsRef = (Reference)lhs.Evaluate();
                        }
                    }
                    else
                    {
                        iterationEnv = LexicalEnvironment.NewDeclarativeEnvironment(_engine, oldEnv);
                        if (_tdzNames != null)
                        {
                            BindingInstantiation(iterationEnv);
                        }
                        _engine.UpdateLexicalEnvironment(iterationEnv);

                        if (!destructuring)
                        {
                            if (lhsName == null)
                            {
                                lhsName = ((Identifier)((VariableDeclaration)_leftNode).Declarations[0].Id).Name;
                            }
                            lhsRef = _engine.ResolveBinding(lhsName);
                        }
                    }

                    if (!destructuring)
                    {
                        // If lhsRef is an abrupt completion, then
                        // Let status be lhsRef.

                        if (lhsKind == LhsKind.LexicalBinding)
                        {
                            lhsRef.InitializeReferencedBinding(nextValue);
                        }
                        else
                        {
                            _engine.PutValue(lhsRef, nextValue);
                        }
                    }
                    else
                    {
                        BindingPatternAssignmentExpression.ProcessPatterns(
                            _engine,
                            _assignmentPattern,
                            nextValue,
                            iterationEnv,
                            checkObjectPatternPropertyReference: _lhsKind != LhsKind.VarBinding);

                        if (lhsKind == LhsKind.Assignment)
                        {
                            // DestructuringAssignmentEvaluation of assignmentPattern using nextValue as the argument.
                        }
                        else if (lhsKind == LhsKind.VarBinding)
                        {
                            // BindingInitialization for lhs passing nextValue and undefined as the arguments.
                        }
                        else
                        {
                            // BindingInitialization for lhs passing nextValue and iterationEnv as arguments
                        }
                    }

                    var result = stmt.Execute();
                    _engine.UpdateLexicalEnvironment(oldEnv);

                    if (!ReferenceEquals(result.Value, null))
                    {
                        v = result.Value;
                    }

                    if (result.Type == CompletionType.Break && (result.Identifier == null || result.Identifier == _statement?.LabelSet?.Name))
                    {
                        return(new Completion(CompletionType.Normal, v, null, Location));
                    }

                    if (result.Type != CompletionType.Continue || (result.Identifier != null && result.Identifier != _statement?.LabelSet?.Name))
                    {
                        if (result.Type != CompletionType.Normal)
                        {
                            return(result);
                        }
                    }
                }
            }
            catch
            {
                completionType = CompletionType.Throw;
                throw;
            }
            finally
            {
                if (close)
                {
                    iteratorRecord.Close(completionType);
                }
                _engine.UpdateLexicalEnvironment(oldEnv);
            }
        }
예제 #18
0
 protected override void Initialize(EvaluationContext context)
 {
     _argument = _statement.Argument != null
         ? JintExpression.Build(context.Engine, _statement.Argument)
         : null;
 }
예제 #19
0
 public JintDoWhileStatement(Engine engine, DoWhileStatement statement) : base(engine, statement)
 {
     _body         = Build(_engine, statement.Body);
     _test         = JintExpression.Build(engine, statement.Test);
     _labelSetName = statement.LabelSet?.Name;
 }
예제 #20
0
    protected override void Initialize(EvaluationContext context)
    {
        var expression = ((Import)_expression).Source;

        _importExpression = Build(context.Engine, expression !);
    }
 public JintWhileStatement(Engine engine, WhileStatement statement) : base(engine, statement)
 {
     _labelSetName = _statement?.LabelSet?.Name;
     _body         = Build(engine, statement.Body);
     _test         = JintExpression.Build(engine, statement.Test);
 }
예제 #22
0
 protected override void Initialize(EvaluationContext context)
 {
     _statementConsequent = Build(_statement.Consequent);
     _test      = JintExpression.Build(context.Engine, _statement.Test);
     _alternate = _statement.Alternate != null?Build(_statement.Alternate) : null;
 }
예제 #23
0
 protected override void Initialize()
 {
     _switchBlock  = new JintSwitchBlock(_engine, _statement.Cases);
     _discriminant = JintExpression.Build(_engine, _statement.Discriminant);
 }
예제 #24
0
 public JintWithStatement(Engine engine, WithStatement statement) : base(engine, statement)
 {
     _body   = Build(engine, statement.Body);
     _object = JintExpression.Build(engine, _statement.Object);
 }
예제 #25
0
        /// <summary>
        /// https://tc39.es/ecma262/#sec-runtime-semantics-forin-div-ofbodyevaluation-lhs-stmt-iterator-lhskind-labelset
        /// </summary>
        private Completion BodyEvaluation(
            EvaluationContext context,
            JintExpression lhs,
            JintStatement stmt,
            IteratorInstance iteratorRecord,
            IterationKind iterationKind,
            LhsKind lhsKind,
            IteratorKind iteratorKind = IteratorKind.Sync)
        {
            var    engine        = context.Engine;
            var    oldEnv        = engine.ExecutionContext.LexicalEnvironment;
            var    v             = Undefined.Instance;
            var    destructuring = _destructuring;
            string lhsName       = null;

            var completionType = CompletionType.Normal;
            var close          = false;

            try
            {
                while (true)
                {
                    EnvironmentRecord iterationEnv = null;
                    if (!iteratorRecord.TryIteratorStep(out var nextResult))
                    {
                        close = true;
                        return(new Completion(CompletionType.Normal, v, null, Location));
                    }

                    if (iteratorKind == IteratorKind.Async)
                    {
                        // nextResult = await nextResult;
                        ExceptionHelper.ThrowNotImplementedException("await");
                    }

                    var nextValue = nextResult.Get(CommonProperties.Value);
                    close = true;

                    var lhsRef = new ExpressionResult();
                    if (lhsKind != LhsKind.LexicalBinding)
                    {
                        if (!destructuring)
                        {
                            lhsRef = lhs.Evaluate(context);
                        }
                    }
                    else
                    {
                        iterationEnv = JintEnvironment.NewDeclarativeEnvironment(engine, oldEnv);
                        if (_tdzNames != null)
                        {
                            BindingInstantiation(iterationEnv);
                        }
                        engine.UpdateLexicalEnvironment(iterationEnv);

                        if (!destructuring)
                        {
                            var identifier = (Identifier)((VariableDeclaration)_leftNode).Declarations[0].Id;
                            lhsName ??= identifier.Name;
                            lhsRef = new ExpressionResult(ExpressionCompletionType.Normal, engine.ResolveBinding(lhsName), identifier.Location);
                        }
                    }

                    if (context.DebugMode)
                    {
                        context.Engine.DebugHandler.OnStep(_leftNode);
                    }

                    var status = new Completion();
                    if (!destructuring)
                    {
                        if (lhsRef.IsAbrupt())
                        {
                            close  = true;
                            status = new Completion(lhsRef);
                        }
                        else if (lhsKind == LhsKind.LexicalBinding)
                        {
                            ((Reference)lhsRef.Value).InitializeReferencedBinding(nextValue);
                        }
                        else
                        {
                            engine.PutValue((Reference)lhsRef.Value, nextValue);
                        }
                    }
                    else
                    {
                        status = BindingPatternAssignmentExpression.ProcessPatterns(
                            context,
                            _assignmentPattern,
                            nextValue,
                            iterationEnv,
                            checkObjectPatternPropertyReference: _lhsKind != LhsKind.VarBinding);

                        if (lhsKind == LhsKind.Assignment)
                        {
                            // DestructuringAssignmentEvaluation of assignmentPattern using nextValue as the argument.
                        }
                        else if (lhsKind == LhsKind.VarBinding)
                        {
                            // BindingInitialization for lhs passing nextValue and undefined as the arguments.
                        }
                        else
                        {
                            // BindingInitialization for lhs passing nextValue and iterationEnv as arguments
                        }
                    }

                    if (status.IsAbrupt())
                    {
                        engine.UpdateLexicalEnvironment(oldEnv);
                        if (_iterationKind == IterationKind.AsyncIterate)
                        {
                            iteratorRecord.Close(status.Type);
                            return(status);
                        }

                        if (iterationKind == IterationKind.Enumerate)
                        {
                            return(status);
                        }

                        iteratorRecord.Close(status.Type);
                        return(status);
                    }

                    var result = stmt.Execute(context);
                    engine.UpdateLexicalEnvironment(oldEnv);

                    if (!ReferenceEquals(result.Value, null))
                    {
                        v = result.Value;
                    }

                    if (result.Type == CompletionType.Break && (result.Target == null || result.Target == _statement?.LabelSet?.Name))
                    {
                        completionType = CompletionType.Normal;
                        return(new Completion(CompletionType.Normal, v, null, Location));
                    }

                    if (result.Type != CompletionType.Continue || (result.Target != null && result.Target != _statement?.LabelSet?.Name))
                    {
                        completionType = result.Type;
                        if (result.IsAbrupt())
                        {
                            close = true;
                            return(result);
                        }
                    }
                }
            }
            catch
            {
                completionType = CompletionType.Throw;
                throw;
            }
            finally
            {
                if (close)
                {
                    try
                    {
                        iteratorRecord.Close(completionType);
                    }
                    catch
                    {
                        // if we already have and exception, use it
                        if (completionType != CompletionType.Throw)
                        {
                            throw;
                        }
                    }
                }
                engine.UpdateLexicalEnvironment(oldEnv);
            }
        }
예제 #26
0
 public JintReturnStatement(Engine engine, ReturnStatement statement) : base(engine, statement)
 {
     _argument = _statement.Argument != null
         ? JintExpression.Build(engine, _statement.Argument)
         : null;
 }
예제 #27
0
 protected override void Initialize(EvaluationContext context)
 {
     _body   = Build(_statement.Body);
     _object = JintExpression.Build(context.Engine, _statement.Object);
 }
예제 #28
0
 protected override void Initialize()
 {
     _argument = JintExpression.Build(_engine, _statement.Argument);
 }