Exemplo n.º 1
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());
        }
        public DomEventInstance(DomNodeInstance node, EventInfo eventInfo = null)
        {
            Getter = new ClrFunctionInstance(node.Engine, (thisObject, arguments) => _function ?? JsValue.Null);
            Setter = new ClrFunctionInstance(node.Engine, (thisObject, arguments) =>
            {
                if (_handler != null)
                {
                    if (eventInfo != null)
                    {
                        eventInfo.RemoveEventHandler(node.Value, _handler);
                    }

                    _handler  = null;
                    _function = null;
                }

                if (arguments[0].Is <FunctionInstance>())
                {
                    _function = arguments[0].As <FunctionInstance>();
                    _handler  = (s, ev) =>
                    {
                        var sender = s.ToJsValue(node.Context);
                        var args   = ev.ToJsValue(node.Context);
                        _function.Call(sender, new [] { args });
                    };

                    if (eventInfo != null)
                    {
                        eventInfo.AddEventHandler(node.Value, _handler);
                    }
                }

                return(arguments[0]);
            });
        }
        public DomEventInstance(DomNodeInstance node, EventInfo eventInfo)
        {
            Getter = new ClrFunctionInstance(node.Engine, (thisObject, arguments) => _function ?? JsValue.Null);
            Setter = new ClrFunctionInstance(node.Engine, (thisObject, arguments) =>
            {
                if (_handler != null)
                {
                    eventInfo.RemoveEventHandler(node.Value, _handler);
                    _handler = null;
                    _function = null;
                }

                if (arguments[0].Is<FunctionInstance>())
                {
                    _function = arguments[0].As<FunctionInstance>();
                    _handler = (s, ev) => 
                    {
                        var sender = s.ToJsValue(node.Context);
                        var args = ev.ToJsValue(node.Context);
                        _function.Call(sender, new [] { args });
                    };
                    eventInfo.AddEventHandler(node.Value, _handler);
                }

                return arguments[0];
            });
        }
        public DomNodeInstance GetDomNode(Object obj)
        {
            var domNodeInstance = default(DomNodeInstance);

            if (_objects.TryGetValue(obj, out domNodeInstance) == false)
                _objects.Add(obj, domNodeInstance = new DomNodeInstance(this, obj));
            
            return domNodeInstance;
        }
        public DomNodeInstance GetDomNode(Object obj)
        {
            var domNodeInstance = default(DomNodeInstance);

            if (_objects.TryGetValue(obj, out domNodeInstance) == false)
            {
                _objects.Add(obj, domNodeInstance = new DomNodeInstance(this, obj));
            }

            return(domNodeInstance);
        }
        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());
        }
        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());
        }