Exemple #1
0
        /// <summary>
        /// http://www.ecma-international.org/ecma-262/5.1/#sec-12.10
        /// </summary>
        /// <param name="withStatement"></param>
        /// <returns></returns>
        public Completion ExecuteWithStatement(WithStatement withStatement)
        {
            var val    = _engine.EvaluateExpression(withStatement.Object);
            var obj    = TypeConverter.ToObject(_engine, _engine.GetValue(val));
            var oldEnv = _engine.ExecutionContext.LexicalEnvironment;
            var newEnv = LexicalEnvironment.NewObjectEnvironment(_engine, obj, oldEnv, true);

            _engine.ExecutionContext.LexicalEnvironment = newEnv;

            Completion c;

            try
            {
                c = ExecuteStatement(withStatement.Body);
            }
            catch (JavaScriptException e)
            {
                c          = new Completion(Completion.Throw, e.Error, null);
                c.Location = withStatement.Location;
            }
            finally
            {
                _engine.ExecutionContext.LexicalEnvironment = oldEnv;
            }

            return(c);
        }
        public EngineInstance(IWindow window, IDictionary <String, Object> assignments, IEnumerable <Assembly> libs)
        {
            var context = window.Document.Context;
            var logger  = context.GetService <IConsoleLogger>();

            _engine     = new Engine();
            _prototypes = new PrototypeCache(_engine);
            _references = new ReferenceCache();
            _libs       = libs;
            _engine.SetValue("console", new ConsoleInstance(_engine, logger));

            foreach (var assignment in assignments)
            {
                _engine.SetValue(assignment.Key, assignment.Value);
            }

            _window    = GetDomNode(window);
            _lexicals  = LexicalEnvironment.NewObjectEnvironment(_engine, _window, _engine.ExecutionContext.LexicalEnvironment, true);
            _variables = LexicalEnvironment.NewObjectEnvironment(_engine, _engine.Global, null, false);

            foreach (var lib in libs)
            {
                this.AddConstructors(_window, lib);
                this.AddConstructors(_window, lib);
                this.AddInstances(_window, lib);
            }
        }
Exemple #3
0
        public EngineInstance(IWindow window, IDictionary <String, Object> assignments)
        {
            var logger       = default(IConsoleLogger);
            var context      = window.Document.Context;
            var createLogger = context.Configuration.Services.OfType <Func <IBrowsingContext, IConsoleLogger> >().FirstOrDefault();

            if (createLogger != null)
            {
                logger = createLogger.Invoke(context);
            }

            _objects = new Dictionary <Object, DomNodeInstance>();
            _engine  = new Engine();
            _engine.SetValue("console", new ConsoleInstance(_engine, logger));

            foreach (var assignment in assignments)
            {
                _engine.SetValue(assignment.Key, assignment.Value);
            }

            _window       = GetDomNode(window);
            _lexicals     = LexicalEnvironment.NewObjectEnvironment(_engine, _window, _engine.ExecutionContext.LexicalEnvironment, true);
            _variables    = LexicalEnvironment.NewObjectEnvironment(_engine, _engine.Global, null, false);
            _constructors = new DomConstructors(this);
            _constructors.Configure();

            this.AddConstructors(_window, typeof(INode));
            this.AddConstructors(_window, this.GetType());
            this.AddInstances(_window, this.GetType());
        }
        protected override Completion ExecuteInternal()
        {
            var jsValue = _object.GetValue();
            var obj     = TypeConverter.ToObject(_engine, jsValue);
            var oldEnv  = _engine.ExecutionContext.LexicalEnvironment;
            var newEnv  = LexicalEnvironment.NewObjectEnvironment(_engine, obj, oldEnv, provideThis: true, withEnvironment: true);

            _engine.UpdateLexicalEnvironment(newEnv);

            Completion c;

            try
            {
                c = _body.Execute();
            }
            catch (JavaScriptException e)
            {
                c = new Completion(CompletionType.Throw, e.Error, null, _statement.Location);
            }
            finally
            {
                _engine.UpdateLexicalEnvironment(oldEnv);
            }

            return(c);
        }
        public void Evaluate(String source, ScriptOptions options)
        {
            var context = new DomNodeInstance(_engine, options.Context ?? new AnalysisWindow(options.Document));
            var env     = LexicalEnvironment.NewObjectEnvironment(_engine, context, _engine.ExecutionContext.LexicalEnvironment, true);

            _engine.EnterExecutionContext(env, _variable, context);
            _engine.Execute(source);
            _engine.LeaveExecutionContext();
        }
        public EngineInstance(IWindow window, IDictionary <String, Object> assignments)
        {
            _objects = new Dictionary <Object, DomNodeInstance>();
            _engine  = new Engine();
            _engine.SetValue("console", new ConsoleInstance(_engine));

            foreach (var assignment in assignments)
            {
                _engine.SetValue(assignment.Key, assignment.Value);
            }

            _window       = GetDomNode(window);
            _lexicals     = LexicalEnvironment.NewObjectEnvironment(_engine, _window, _engine.ExecutionContext.LexicalEnvironment, true);
            _variables    = LexicalEnvironment.NewObjectEnvironment(_engine, _engine.Global, null, false);
            _constructors = new DomConstructors(this);
            _constructors.Configure();

            this.AddConstructors(_window, typeof(INode));
            this.AddConstructors(_window, this.GetType());
        }
        /// <summary>
        /// http://www.ecma-international.org/ecma-262/5.1/#sec-12.10
        /// </summary>
        /// <param name="withStatement"></param>
        /// <returns></returns>
        public void ExecuteWithStatement(RuntimeState state)
        {
            WithStatement             withStatement = (WithStatement)state.arg;
            ExecuteWithStatementLocal local         = null;

            if (state.stage == 0)
            {
                if (state.calleeReturned)
                {
                    state.calleeReturned = false;
                    state.local          = local = new ExecuteWithStatementLocal();
                    var jsValue = _engine.GetValue(state.calleeReturnValue, true);
                    var obj     = TypeConverter.ToObject(_engine, jsValue);
                    local.oldEnv = _engine.ExecutionContext.LexicalEnvironment;
                    var newEnv = LexicalEnvironment.NewObjectEnvironment(_engine, obj, local.oldEnv, true);
                    _engine.UpdateLexicalEnvironment(newEnv);
                    state.stage = 1;
                }
                else
                {
                    Call(_engine.EvaluateExpression, withStatement.Object);
                    return;
                }
            }

            // Stage 1

            if (local == null)
            {
                local = (ExecuteWithStatementLocal)state.local;
            }

            if (state.calleeReturned)
            {
                _engine.UpdateLexicalEnvironment(local.oldEnv);
                Return(state.calleeReturnValue);
                return;
            }
        }
Exemple #8
0
        public Engine(Action <Options> options)
        {
            _executionContexts = new Stack <ExecutionContext>();

            Global = GlobalObject.CreateGlobalObject(this);

            Object   = ObjectConstructor.CreateObjectConstructor(this);
            Function = FunctionConstructor.CreateFunctionConstructor(this);

            Array   = ArrayConstructor.CreateArrayConstructor(this);
            String  = StringConstructor.CreateStringConstructor(this);
            RegExp  = RegExpConstructor.CreateRegExpConstructor(this);
            Number  = NumberConstructor.CreateNumberConstructor(this);
            Boolean = BooleanConstructor.CreateBooleanConstructor(this);
            Date    = DateConstructor.CreateDateConstructor(this);
            Math    = MathInstance.CreateMathObject(this);
            Json    = JsonInstance.CreateJsonObject(this);

            Error          = ErrorConstructor.CreateErrorConstructor(this, "Error");
            EvalError      = ErrorConstructor.CreateErrorConstructor(this, "EvalError");
            RangeError     = ErrorConstructor.CreateErrorConstructor(this, "RangeError");
            ReferenceError = ErrorConstructor.CreateErrorConstructor(this, "ReferenceError");
            SyntaxError    = ErrorConstructor.CreateErrorConstructor(this, "SyntaxError");
            TypeError      = ErrorConstructor.CreateErrorConstructor(this, "TypeError");
            UriError       = ErrorConstructor.CreateErrorConstructor(this, "URIError");

            // Because the properties might need some of the built-in object
            // their configuration is delayed to a later step

            Global.Configure();

            Object.Configure();
            Object.PrototypeObject.Configure();

            Function.Configure();
            Function.PrototypeObject.Configure();

            Array.Configure();
            Array.PrototypeObject.Configure();

            String.Configure();
            String.PrototypeObject.Configure();

            RegExp.Configure();
            RegExp.PrototypeObject.Configure();

            Number.Configure();
            Number.PrototypeObject.Configure();

            Boolean.Configure();
            Boolean.PrototypeObject.Configure();

            Date.Configure();
            Date.PrototypeObject.Configure();

            Math.Configure();
            Json.Configure();

            Error.Configure();
            Error.PrototypeObject.Configure();

            // create the global environment http://www.ecma-international.org/ecma-262/5.1/#sec-10.2.3
            GlobalEnvironment = LexicalEnvironment.NewObjectEnvironment(this, Global, null, false);

            // create the global execution context http://www.ecma-international.org/ecma-262/5.1/#sec-10.4.1.1
            EnterExecutionContext(GlobalEnvironment, GlobalEnvironment, Global);

            Options = new Options();

            if (options != null)
            {
                options(Options);
            }

            Eval = new EvalFunctionInstance(this, new string[0], LexicalEnvironment.NewDeclarativeEnvironment(this, ExecutionContext.LexicalEnvironment), StrictModeScope.IsStrictModeCode);
            Global.FastAddProperty("eval", Eval, true, false, true);

            _statements  = new StatementInterpreter(this);
            _expressions = new ExpressionInterpreter(this);

            if (Options._IsClrAllowed)
            {
                Global.FastAddProperty("System", new NamespaceReference(this, "System"), false, false, false);
                Global.FastAddProperty("importNamespace", new ClrFunctionInstance(this, (thisObj, arguments) =>
                {
                    return(new NamespaceReference(this, TypeConverter.ToString(arguments.At(0))));
                }), false, false, false);
            }

            ClrTypeConverter = new DefaultTypeConverter(this);
            BreakPoints      = new List <BreakPoint>();
            DebugHandler     = new DebugHandler(this);
        }
 public JavaScriptEngine()
 {
     _engine = new Engine();
     _engine.SetValue("console", new ConsoleInstance(_engine));
     _variable = LexicalEnvironment.NewObjectEnvironment(_engine, _engine.Global, null, false);
 }
Exemple #10
0
        public Engine(Action <Options> options)
        {
            _executionContexts = new ExecutionContextStack();

            Global = GlobalObject.CreateGlobalObject(this);

            Object   = ObjectConstructor.CreateObjectConstructor(this);
            Function = FunctionConstructor.CreateFunctionConstructor(this);

            Symbol   = SymbolConstructor.CreateSymbolConstructor(this);
            Array    = ArrayConstructor.CreateArrayConstructor(this);
            Map      = MapConstructor.CreateMapConstructor(this);
            Set      = SetConstructor.CreateSetConstructor(this);
            Iterator = IteratorConstructor.CreateIteratorConstructor(this);
            String   = StringConstructor.CreateStringConstructor(this);
            RegExp   = RegExpConstructor.CreateRegExpConstructor(this);
            Number   = NumberConstructor.CreateNumberConstructor(this);
            Boolean  = BooleanConstructor.CreateBooleanConstructor(this);
            //            Date = DateConstructor.CreateDateConstructor(this);
            Math = MathInstance.CreateMathObject(this);
            Json = JsonInstance.CreateJsonObject(this);

            Error          = ErrorConstructor.CreateErrorConstructor(this, "Error");
            EvalError      = ErrorConstructor.CreateErrorConstructor(this, "EvalError");
            RangeError     = ErrorConstructor.CreateErrorConstructor(this, "RangeError");
            ReferenceError = ErrorConstructor.CreateErrorConstructor(this, "ReferenceError");
            SyntaxError    = ErrorConstructor.CreateErrorConstructor(this, "SyntaxError");
            TypeError      = ErrorConstructor.CreateErrorConstructor(this, "TypeError");
            UriError       = ErrorConstructor.CreateErrorConstructor(this, "URIError");

            GlobalSymbolRegistry = new GlobalSymbolRegistry();

            // Because the properties might need some of the built-in object
            // their configuration is delayed to a later step

            Global.Configure();

            Object.Configure();
            Object.PrototypeObject.Configure();

            Symbol.Configure();
            Symbol.PrototypeObject.Configure();

            Function.Configure();
            Function.PrototypeObject.Configure();

            Array.Configure();
            Array.PrototypeObject.Configure();

            Map.Configure();
            Map.PrototypeObject.Configure();

            Set.Configure();
            Set.PrototypeObject.Configure();

            Iterator.Configure();
            Iterator.PrototypeObject.Configure();

            String.Configure();
            String.PrototypeObject.Configure();

            RegExp.Configure();
            RegExp.PrototypeObject.Configure();

            Number.Configure();
            Number.PrototypeObject.Configure();

            Boolean.Configure();
            Boolean.PrototypeObject.Configure();

            //            Date.Configure();
            //            Date.PrototypeObject.Configure();

            Math.Configure();
            Json.Configure();

            Error.Configure();
            Error.PrototypeObject.Configure();

            // create the global environment http://www.ecma-international.org/ecma-262/5.1/#sec-10.2.3
            GlobalEnvironment = LexicalEnvironment.NewObjectEnvironment(this, Global, null, false);

            // create the global execution context http://www.ecma-international.org/ecma-262/5.1/#sec-10.4.1.1
            EnterExecutionContext(GlobalEnvironment, GlobalEnvironment, Global);

            Options = new Options();

            options?.Invoke(Options);

            // gather some options as fields for faster checks
            _isDebugMode = Options.IsDebugMode;

            if (_isDebugMode)
            {
                MyAPIGateway.Utilities.ShowMessage("SpaceJS", "Debug mode enabled.");
            }

            _isStrict                 = Options.IsStrict;
            _maxStatements            = Options._MaxStatements;
            _referenceResolver        = Options.ReferenceResolver;
            _memoryLimit              = Options._MemoryLimit;
            _runBeforeStatementChecks = (_maxStatements > 0 && _maxStatements < int.MaxValue) ||
                                        Options._TimeoutInterval.Ticks > 0 ||
                                        _memoryLimit > 0 ||
                                        _isDebugMode;

            _referencePool         = new ReferencePool();
            _argumentsInstancePool = new ArgumentsInstancePool(this);
            _jsValueArrayPool      = new JsValueArrayPool();

            Eval = new EvalFunctionInstance(this, System.ArrayExt.Empty <string>(), LexicalEnvironment.NewDeclarativeEnvironment(this, ExecutionContext.LexicalEnvironment), StrictModeScope.IsStrictModeCode);
            Global.FastAddProperty("eval", Eval, true, false, true);

            _statements  = new StatementInterpreter(this);
            _expressions = new ExpressionInterpreter(this);

            /*            if (Options._IsClrAllowed)
             *          {
             *              Global.FastAddProperty("System", new NamespaceReference(this, "System"), false, false, false);
             *              Global.FastAddProperty("importNamespace", new ClrFunctionInstance(
             *                  this,
             *                  "importNamespace",
             *                  (thisObj, arguments) => new NamespaceReference(this, TypeConverter.ToString(arguments.At(0)))), false, false, false);
             *          }
             */
            //            ClrTypeConverter = new DefaultTypeConverter(this);
        }