Example #1
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);
                }
            }
Example #2
0
    /// <summary>
    /// https://tc39.es/ecma262/#sec-source-text-module-record-execute-module
    /// </summary>
    internal override Completion ExecuteModule(PromiseCapability capability = null)
    {
        var moduleContext = new ExecutionContext(this, _environment, _environment, null, _realm);

        if (!_hasTLA)
        {
            using (new StrictModeScope(true, force: true))
            {
                _engine.EnterExecutionContext(moduleContext);
                var statementList = new JintStatementList(null, _source.Body);
                var result        = statementList.Execute(_engine._activeEvaluationContext ?? new EvaluationContext(_engine)); //Create new evaluation context when called from e.g. module tests
                _engine.LeaveExecutionContext();
                return(result);
            }
        }
        else
        {
            ExceptionHelper.ThrowNotImplementedException("async modules not implemented");
            return(default);
Example #3
0
        /// <summary>
        /// Evaluates a script (expression) within the current execution context.
        /// </summary>
        /// <remarks>
        /// Internally, this is used for evaluating breakpoint conditions, but may also be used for e.g. watch lists
        /// in a debugger.
        /// </remarks>
        public JsValue Evaluate(Script script)
        {
            var context = _engine._activeEvaluationContext;

            if (context == null)
            {
                throw new DebugEvaluationException("Jint has no active evaluation context");
            }
            int callStackSize = _engine.CallStack.Count;

            var        list = new JintStatementList(null, script.Body);
            Completion result;

            try
            {
                result = list.Execute(context);
            }
            catch (Exception ex)
            {
                // An error in the evaluation may return a Throw Completion, or it may throw an exception:
                throw new DebugEvaluationException("An error occurred during debugger evaluation", ex);
            }
            finally
            {
                // Restore call stack
                while (_engine.CallStack.Count > callStackSize)
                {
                    _engine.CallStack.Pop();
                }
            }

            if (result.Type == CompletionType.Throw)
            {
                // TODO: Should we return an error here? (avoid exception overhead, since e.g. breakpoint
                // evaluation may be high volume.
                var error = result.GetValueOrDefault();
                var ex    = new JavaScriptException(error).SetCallstack(_engine, result.Location);
                throw new DebugEvaluationException("An error occurred during debugger evaluation", ex);
            }

            return(result.GetValueOrDefault());
        }
        // http://www.ecma-international.org/ecma-262/6.0/#sec-blockdeclarationinstantiation
        protected override Completion ExecuteInternal()
        {
            LexicalEnvironment oldEnv = null;

            if (_lexicalDeclarations != null)
            {
                oldEnv = _engine.ExecutionContext.LexicalEnvironment;
                var blockEnv = LexicalEnvironment.NewDeclarativeEnvironment(_engine, _engine.ExecutionContext.LexicalEnvironment);
                JintStatementList.BlockDeclarationInstantiation(blockEnv, _lexicalDeclarations);
                _engine.UpdateLexicalEnvironment(blockEnv);
            }

            var blockValue = _statementList.Execute();

            if (oldEnv != null)
            {
                _engine.UpdateLexicalEnvironment(oldEnv);
            }

            return(blockValue);
        }
Example #5
0
        protected override Completion ExecuteInternal(EvaluationContext context)
        {
            EnvironmentRecord oldEnv = null;
            var engine = context.Engine;

            if (_lexicalDeclarations != null)
            {
                oldEnv = engine.ExecutionContext.LexicalEnvironment;
                var blockEnv = JintEnvironment.NewDeclarativeEnvironment(engine, engine.ExecutionContext.LexicalEnvironment);
                JintStatementList.BlockDeclarationInstantiation(engine, blockEnv, _lexicalDeclarations);
                engine.UpdateLexicalEnvironment(blockEnv);
            }

            var blockValue = _statementList.Execute(context);

            if (oldEnv is not null)
            {
                engine.UpdateLexicalEnvironment(oldEnv);
            }

            return(blockValue);
        }
Example #6
0
 public JintScript(Engine engine, Script script) : base(engine, script)
 {
     _list = new JintStatementList(_engine, null, _statement.Body);
 }
Example #7
0
 public JintScript(Script script) : base(script)
 {
     _list = new JintStatementList(script);
 }
 protected override void Initialize()
 {
     _statementList       = new JintStatementList(_engine, _statement, _statement.Body);
     _lexicalDeclarations = HoistingScope.GetLexicalDeclarations(_statement);
 }
Example #9
0
 protected override void Initialize(EvaluationContext context)
 {
     _statementList       = new JintStatementList(_statement, _statement.Body);
     _lexicalDeclarations = HoistingScope.GetLexicalDeclarations(_statement);
 }
Example #10
0
        public Completion Execute(EvaluationContext context, JsValue input)
        {
            if (!_initialized)
            {
                Initialize(context);
                _initialized = true;
            }

            var            engine      = context.Engine;
            JsValue        v           = Undefined.Instance;
            Location       l           = context.LastSyntaxNode.Location;
            JintSwitchCase defaultCase = null;
            bool           hit         = false;

            for (var i = 0; i < (uint)_jintSwitchBlock.Length; i++)
            {
                var clause = _jintSwitchBlock[i];

                EnvironmentRecord oldEnv = null;
                if (clause.LexicalDeclarations != null)
                {
                    oldEnv = engine.ExecutionContext.LexicalEnvironment;
                    var blockEnv = JintEnvironment.NewDeclarativeEnvironment(engine, oldEnv);
                    JintStatementList.BlockDeclarationInstantiation(engine, blockEnv, clause.LexicalDeclarations);
                    engine.UpdateLexicalEnvironment(blockEnv);
                }

                if (clause.Test == null)
                {
                    defaultCase = clause;
                }
                else
                {
                    var clauseSelector = clause.Test.GetValue(context).Value;
                    if (clauseSelector == input)
                    {
                        hit = true;
                    }
                }

                if (hit && clause.Consequent != null)
                {
                    var r = clause.Consequent.Execute(context);

                    if (oldEnv is not null)
                    {
                        engine.UpdateLexicalEnvironment(oldEnv);
                    }

                    if (r.Type != CompletionType.Normal)
                    {
                        return(r);
                    }

                    l = r.Location;
                    v = r.Value ?? Undefined.Instance;
                }
            }

            // do we need to execute the default case ?
            if (hit == false && defaultCase != null)
            {
                EnvironmentRecord oldEnv = null;
                if (defaultCase.LexicalDeclarations != null)
                {
                    oldEnv = engine.ExecutionContext.LexicalEnvironment;
                    var blockEnv = JintEnvironment.NewDeclarativeEnvironment(engine, oldEnv);
                    JintStatementList.BlockDeclarationInstantiation(engine, blockEnv, defaultCase.LexicalDeclarations);
                    engine.UpdateLexicalEnvironment(blockEnv);
                }

                var r = defaultCase.Consequent.Execute(context);

                if (oldEnv is not null)
                {
                    engine.UpdateLexicalEnvironment(oldEnv);
                }
                if (r.Type != CompletionType.Normal)
                {
                    return(r);
                }

                l = r.Location;
                v = r.Value ?? Undefined.Instance;
            }

            return(new Completion(CompletionType.Normal, v, null, l));
        }
        public Completion Execute(JsValue input)
        {
            if (!_initialized)
            {
                Initialize();
                _initialized = true;
            }

            JsValue        v           = Undefined.Instance;
            Location       l           = _engine._lastSyntaxNode.Location;
            JintSwitchCase defaultCase = null;
            bool           hit         = false;

            for (var i = 0; i < (uint)_jintSwitchBlock.Length; i++)
            {
                var clause = _jintSwitchBlock[i];

                LexicalEnvironment oldEnv = null;
                if (clause.LexicalDeclarations != null)
                {
                    oldEnv = _engine.ExecutionContext.LexicalEnvironment;
                    var blockEnv = LexicalEnvironment.NewDeclarativeEnvironment(_engine, oldEnv);
                    JintStatementList.BlockDeclarationInstantiation(blockEnv, clause.LexicalDeclarations);
                    _engine.UpdateLexicalEnvironment(blockEnv);
                }

                if (clause.Test == null)
                {
                    defaultCase = clause;
                }
                else
                {
                    var clauseSelector = clause.Test.GetValue();
                    if (JintBinaryExpression.StrictlyEqual(clauseSelector, input))
                    {
                        hit = true;
                    }
                }

                if (hit && clause.Consequent != null)
                {
                    var r = clause.Consequent.Execute();

                    if (oldEnv != null)
                    {
                        _engine.UpdateLexicalEnvironment(oldEnv);
                    }

                    if (r.Type != CompletionType.Normal)
                    {
                        return(r);
                    }

                    l = r.Location;
                    v = r.Value ?? Undefined.Instance;
                }
            }

            // do we need to execute the default case ?
            if (hit == false && defaultCase != null)
            {
                LexicalEnvironment oldEnv = null;
                if (defaultCase.LexicalDeclarations != null)
                {
                    oldEnv = _engine.ExecutionContext.LexicalEnvironment;
                    var blockEnv = LexicalEnvironment.NewDeclarativeEnvironment(_engine, oldEnv);
                    JintStatementList.BlockDeclarationInstantiation(blockEnv, defaultCase.LexicalDeclarations);
                    _engine.UpdateLexicalEnvironment(blockEnv);
                }

                var r = defaultCase.Consequent.Execute();

                if (oldEnv != null)
                {
                    _engine.UpdateLexicalEnvironment(oldEnv);
                }
                if (r.Type != CompletionType.Normal)
                {
                    return(r);
                }

                l = r.Location;
                v = r.Value ?? Undefined.Instance;
            }

            return(new Completion(CompletionType.Normal, v, null, l));
        }