Пример #1
0
        /// <summary>
        /// Constructs instance of ActiveScriptSiteWrapper
        /// </summary>
        /// <param name="documentVersion">The host-defined document version string</param>
        public ActiveScriptSite(string documentVersion)
        {
            DocumentVersion = documentVersion;

            try
            {
                // Prefer Chakra
                _jsEngine = new ChakraJsEngine() as IActiveScript;
            }
            catch
            {
                _jsEngine = null;
            }

            if (_jsEngine == null)
            {
                // No need to catch here - engine of last resort
                _jsEngine = new JsEngine() as IActiveScript;
            }

            if (_jsEngine != null)
            {
                _jsEngine.SetScriptSite(this);

                _jsParser = new ActiveScriptParserWrapper(_jsEngine);
                _jsParser.InitNew();
            }

            if (_jsEngine == null)
            {
                throw new JsEngineLoadException(Strings.Runtime_JsEngineNotLoaded);
            }
        }
    /// <summary>
    /// Initializes a new instance of the <see cref="ScriptEngine"/> class.
    /// </summary>
    /// <param name="language">The scripting language. Standard Windows Script engines names are 'jscript' or 'vbscript'.</param>
    public ScriptEngine(string language)
    {
        if (language == null)
        {
            throw new ArgumentNullException("language");
        }

        Type engine = Type.GetTypeFromProgID(language, true);

        _engine = Activator.CreateInstance(engine) as IActiveScript;
        if (_engine == null)
        {
            throw new ArgumentException(language + " is not an Windows Script Engine", "language");
        }

        _site = new ScriptSite();
        _engine.SetScriptSite(_site);

        // support 32-bit & 64-bit process
        if (IntPtr.Size == 4)
        {
            _parse32 = _engine as IActiveScriptParse32;
            _parse32.InitNew();
        }
        else
        {
            _parse64 = _engine as IActiveScriptParse64;
            _parse64.InitNew();
        }
    }
 protected ActiveScriptJavascriptRuntimeBase(IActiveScript scriptEngine)
 {
     ScriptEngine = scriptEngine;
     ScriptEngine.SetScriptSite(this);
     JsParse = new ActiveScriptParseWrapper(ScriptEngine);
     JsParse.InitNew();
 }
Пример #4
0
        /// <summary>
        /// Initializes a new instance of the <see cref="ScriptEngine"/> class.
        /// </summary>
        /// <param name="language">The scripting language. Standard Windows Script engines names are 'jscript' or 'vbscript'.</param>
        public ScriptEngine()
        {
            var  guid = new System.Guid("{16d51579-a30b-4c8b-a276-0ff4dc41e755}");            // Chakra IE9/IE10 JS Engine
            Type t    = Type.GetTypeFromCLSID(guid, true);

            _engine = Activator.CreateInstance(t) as IActiveScript;

            if (_engine == null)
            {
                throw new Exception("Unable to initialize the Chakra engine.");
            }

            _site = new ScriptSite();
            _engine.SetScriptSite(_site);

            if (IntPtr.Size == 4)
            {
                _parse32 = _engine as IActiveScriptParse32;
                _parse32.InitNew();
            }
            else
            {
                _parse64 = _engine as IActiveScriptParse64;
                _parse64.InitNew();
            }
        }
Пример #5
0
        /// <summary>
        /// Initializes a new instance of the <see cref="ScriptEngine"/> class.
        /// </summary>
        public ScriptEngine()
        {
            try
            {
                _engine = Activator.CreateInstance(Type.GetTypeFromCLSID(new Guid("{16d51579-a30b-4c8b-a276-0ff4dc41e755}"), true)) as IActiveScript;
            }
            catch
            {
                _engine = Activator.CreateInstance(Type.GetTypeFromProgID("javascript", true)) as IActiveScript;
            }

            Site = new ScriptSite();
            _engine.SetScriptSite(Site);

            // support 32-bit & 64-bit process
            if (IntPtr.Size == 4)
            {
                _parse32 = (IActiveScriptParse32)_engine;
                _parse32.InitNew();
            }
            else
            {
                _parse64 = (IActiveScriptParse64)_engine;
                _parse64.InitNew();
            }
        }
Пример #6
0
        public static object CreateScriptObject(ScriptSiteBase scriptSite, string scriptText)
        {
            IActiveScript      engine = (IActiveScript)engineCache[scriptSite];
            IActiveScriptParse parser = (IActiveScriptParse)engine;

            if (engine == null)
            {
                engine = (IActiveScript) new JScriptEngine();
                engine.SetScriptSite(scriptSite);
                foreach (string name in scriptSite.GetNamedItems())
                {
                    engine.AddNamedItem(name, ScriptItem.IsVisible);
                }
                parser = (IActiveScriptParse)engine;
                parser.InitNew();
                engineCache.Add(scriptSite, engine);
            }

            EXCEPINFO ei;
            object    result;
            IScript   scriptObject;

            parser.ParseScriptText(scriptText, null, null, null, IntPtr.Zero, 1, ScriptText.None, out result, out ei);
            engine.GetScriptDispatch(null, out scriptObject);

            return(scriptObject);
        }
Пример #7
0
        /// <summary>
        /// Constructs an instance of the ActiveScript JavaScript engine
        /// </summary>
        /// <param name="clsid">CLSID of JavaScript engine</param>
        /// <param name="engineMode">JavaScript engine mode</param>
        /// <param name="lowerIeVersion">Lowest supported version of Internet Explorer</param>
        /// <param name="languageVersion">Version of script language</param>
        /// <param name="useEcmaScript5Polyfill">Flag for whether to use the ECMAScript 5 Polyfill</param>
        /// <param name="useJson2Library">Flag for whether to use the JSON2 library</param>
        protected ActiveScriptJsEngineBase(string clsid, JsEngineMode engineMode, string lowerIeVersion,
                                           ScriptLanguageVersion languageVersion, bool useEcmaScript5Polyfill, bool useJson2Library)
        {
            _engineMode      = engineMode;
            _engineModeName  = JsEngineModeHelpers.GetModeName(engineMode);
            _documentVersion = DateTime.UtcNow.ToString("o");

            _dispatcher.Invoke(() =>
            {
                _pActiveScript = IntPtr.Zero;

                try
                {
                    _pActiveScript = ComHelpers.CreateInstanceByClsid <IActiveScript>(clsid);
                    _activeScript  = (IActiveScript)Marshal.GetObjectForIUnknown(_pActiveScript);
                }
                catch (Exception e)
                {
                    throw new JsEngineLoadException(
                        string.Format(CommonStrings.Runtime_IeJsEngineNotLoaded,
                                      _engineModeName, lowerIeVersion, e.Message), _engineModeName);
                }

                if (languageVersion != ScriptLanguageVersion.None)
                {
                    var activeScriptProperty = _activeScript as IActiveScriptProperty;
                    if (activeScriptProperty != null)
                    {
                        object scriptLanguageVersion = (int)languageVersion;
                        uint result = activeScriptProperty.SetProperty((uint)ScriptProperty.InvokeVersioning,
                                                                       IntPtr.Zero, ref scriptLanguageVersion);
                        if (result != (uint)ScriptHResult.Ok)
                        {
                            throw new JsEngineLoadException(
                                string.Format(NetFrameworkStrings.Runtime_ActiveScriptLanguageVersionSelectionFailed, languageVersion));
                        }
                    }
                }

                _activeScriptParse = new ActiveScriptParseWrapper(_pActiveScript, _activeScript);
                _activeScriptParse.InitNew();

                _pActiveScriptGarbageCollector = ComHelpers.QueryInterfaceNoThrow <IActiveScriptGarbageCollector>(_pActiveScript);
                _activeScriptGarbageCollector  = _activeScript as IActiveScriptGarbageCollector;

                _activeScript.SetScriptSite(this);
                _activeScript.SetScriptState(ScriptState.Started);

                InitScriptDispatch();
            });

            LoadResources(useEcmaScript5Polyfill, useJson2Library);
        }
Пример #8
0
 private void ScriptFunc()
 {
     _scriptEngineObject = new VBScriptEngine();
     _scriptEngine       = (IActiveScript)_scriptEngineObject;
     _scriptParser       = new ActiveScriptParseWrapper(_scriptEngine);
     _scriptParser.InitNew();
     _scriptEngine.SetScriptSite(this);
     _scriptEngine.SetScriptState(ScriptState.Started);
     _scriptEngine.SetScriptState(ScriptState.Connected);
     while (true)
     {
         if (_running == 0)
         {
             break;
         }
         string code = "";
         lock (_runnables)
         {
             _runnables.ForEach(snippet => code += snippet + "\n");
             _runnables.Clear();
         }
         if (code.Trim().Length > 0)
         {
             try
             {
                 /*_scriptEngine.GetScriptThreadState(SCRIPTTHREADID_BASE, out ScriptThreadState state);
                  * Console.WriteLine(string.Format("Scipt thread state for thread '{0}' is {1}", _processThread.Name, state));
                  * _scriptEngine.GetScriptState(out ScriptState sciptState);
                  * Console.WriteLine(string.Format("Scipt state for thread '{0}' is {1}", _processThread.Name, sciptState));*/
                 _scriptParser.ParseScriptText(code, null, null, null, IntPtr.Zero, 0, ScriptText.IsVisible, out object result, out EXCEPINFO ei);
             }
             catch (Exception e)
             {
                 Console.WriteLine(string.Format("Exception while parsing following code for thread '{0}'\nException: {1}\nCode:\n{2}", _processThread.Name, e, code));
             }
         }
         else if (_running == 2)
         {
             break;
         }
         Thread.Sleep(100);
     }
     _scriptEngine.Close();
     Marshal.ReleaseComObject(_scriptEngine);
     Marshal.ReleaseComObject(_scriptEngineObject);
     _scriptParser.ReleaseComObject();
     _scriptEngine = null;
     _scriptParser = null;
 }
        /// <summary>
        /// Constructs instance of <see cref="ActiveScriptSiteWrapper"/>
        /// </summary>
        /// <param name="pActiveScript">Pointer to an instance of native JavaScript engine</param>
        /// <param name="activeScript">Instance of native JavaScript engine</param>
        /// <param name="documentVersion">Host-defined document version string</param>
        public ActiveScriptSiteWrapper(IntPtr pActiveScript, IActiveScript activeScript,
                                       string documentVersion)
        {
            _activeScript = activeScript;

            _activeScriptParse = new ActiveScriptParseWrapper(pActiveScript, _activeScript);
            _activeScriptParse.InitNew();

            _activeScript.SetScriptSite(this);
            _activeScript.SetScriptState(ScriptState.Started);

            InitScriptDispatch();

            DocumentVersion = documentVersion;
        }
        /// <summary>
        /// Constructs instance of <see cref="ActiveScriptSiteWrapper"/>
        /// </summary>
        /// <param name="pActiveScript">Pointer to an instance of native JavaScript engine</param>
        /// <param name="activeScript">Instance of native JavaScript engine</param>
        /// <param name="documentVersion">Host-defined document version string</param>
        public ActiveScriptSiteWrapper(IntPtr pActiveScript, IActiveScript activeScript,
			string documentVersion)
        {
            _activeScript = activeScript;

            _activeScriptParse = new ActiveScriptParseWrapper(pActiveScript, _activeScript);
            _activeScriptParse.InitNew();

            _activeScript.SetScriptSite(this);
            _activeScript.SetScriptState(ScriptState.Started);

            InitScriptDispatch();

            DocumentVersion = documentVersion;
        }
Пример #11
0
        public void Initialize()
        {
            try {
                // Prefer Chakra
                _jsEngine = new ChakraJavaScriptEngine() as IActiveScript;
            } catch (Exception e) {
                // TODO: Make catch more specific
                _jsEngine = null;
            }

            if (_jsEngine == null)
            {
                // No need to catch here - engine of last resort
                _jsEngine = new JavaScriptEngine() as IActiveScript;
            }

            _jsEngine.SetScriptSite(this);
            _jsParse = new ActiveScriptParseWrapper(_jsEngine);
            _jsParse.InitNew();
        }
Пример #12
0
        /// <summary>
        /// Initialises this script engine instance with the required internal components.
        /// </summary>
        /// <param name="activeScriptObject">The IActiveScript instance to initialise this script engine with.</param>
        private void Initialise(object activeScriptObject)
        {
            IActiveScript activeScriptInstance = activeScriptObject as IActiveScript;

            if (activeScriptInstance == null)
            {
                throw new InvalidActiveScriptClassException();
            }

            this.parser = ActiveScriptParse.MakeActiveScriptParse(activeScriptInstance);

            this.parser.InitNew();

            this.scriptSite = new ActiveScriptSite(this);
            activeScriptInstance.SetScriptSite(this.scriptSite);

            this.hostObjects = new Dictionary <string, object>();
            this.scripts     = new Dictionary <ulong, ScriptInfo>();

            this.activeScript = activeScriptInstance;
        }
Пример #13
0
        /// <summary>
        /// Initializes a new instance of the <see cref="ScriptEngine"/> class.
        /// </summary>
        /// <param name="language">The scripting language. Standard Windows Script engines names are 'jscript' or 'vbscript'.</param>
        public ScriptEngine(string language)
        {
            if (language == null)
            {
                throw new ArgumentNullException(nameof(language));
            }

            Type engine;
            Guid clsid;

            if (Guid.TryParse(language, out clsid))
            {
                engine = Type.GetTypeFromCLSID(clsid, true);
            }
            else
            {
                engine = Type.GetTypeFromProgID(language, true);
            }

            _engine = Activator.CreateInstance(engine) as IActiveScript;
            if (_engine == null)
            {
                throw new ArgumentException(language + " is not an Windows Script Engine", nameof(language));
            }

            Site = new ScriptSite();
            _engine.SetScriptSite(Site);

            // support 32-bit & 64-bit process
            if (IntPtr.Size == 4)
            {
                _parse32 = (IActiveScriptParse32)_engine;
                _parse32.InitNew();
            }
            else
            {
                _parse64 = (IActiveScriptParse64)_engine;
                _parse64.InitNew();
            }
        }
    /// <summary>
    /// Initializes a new instance of the <see cref="ScriptEngine"/> class.
    /// </summary>
    /// <param name="language">The scripting language. Standard Windows Script engines names are 'jscript' or 'vbscript'.</param>
    public ScriptEngine(string language)
    {
        if (language == null)
        {
            throw new ArgumentNullException("language");
        }

        // support CLSID format (for chakra support that doesn't have a name on its own)
        Type engine;
        Guid clsid;

        if (Guid.TryParse(language, out clsid))
        {
            engine = Type.GetTypeFromCLSID(clsid, true);
        }
        else
        {
            engine = Type.GetTypeFromProgID(language, true);
        }
        _engine = Activator.CreateInstance(engine) as IActiveScript;
        if (_engine == null)
        {
            throw new ArgumentException(language + " is not an Windows Script Engine", "language");
        }

        _site = new ScriptSite();
        _engine.SetScriptSite(_site);

        // support 32-bit & 64-bit process
        if (IntPtr.Size == 4)
        {
            _parse32 = _engine as IActiveScriptParse32;
            _parse32.InitNew();
        }
        else
        {
            _parse64 = _engine as IActiveScriptParse64;
            _parse64.InitNew();
        }
    }
Пример #15
0
        public void Initialize()
        {
            try
            {
                // Prefer Chakra
                jsEngine = new ChakraJavaScriptEngine() as IActiveScript;
            }
            catch
            {
                jsEngine = null;
            }

            if (jsEngine == null)
            {
                // No need to catch here - engine of last resort
                jsEngine = new JavaScriptEngine() as IActiveScript;
            }

            jsEngine.SetScriptSite(this);
            jsParse = new ActiveScriptParseWrapper(jsEngine);
            jsParse.InitNew();
        }
Пример #16
0
        public void Initialize()
        {
            try
            {
                engine = new ChakraJavaScriptEngine() as IActiveScript;
            }
            catch
            {
                engine = new JavaScriptEngine() as IActiveScript;
            }

            if (engine == null)
            {
                throw new Exception("Could not create IE JavaScript engine.");
            }

            parser = new ActiveScriptParseWrapper(engine);

            parser.InitNew();
            engine.SetScriptSite(this);
            engine.SetScriptState(ScriptState.Started);
        }
Пример #17
0
        /// <summary>
        /// Initializes a new instance of the <see cref="ScriptEngine"/> class.
        /// </summary>
        /// <param name="language">The scripting language. Standard Windows Script engines names are 'jscript' or 'vbscript'.</param>
        public ScriptEngine()
        {
            var guid = new System.Guid("{16d51579-a30b-4c8b-a276-0ff4dc41e755}"); // Chakra IE9/IE10 JS Engine
            Type t = Type.GetTypeFromCLSID(guid, true);

            _engine = Activator.CreateInstance(t) as IActiveScript;

            if (_engine == null) {
                throw new Exception("Unable to initialize the Chakra engine.");
            }

            _site = new ScriptSite();
            _engine.SetScriptSite(_site);

            if (IntPtr.Size == 4) {
                _parse32 = _engine as IActiveScriptParse32;
                _parse32.InitNew();
            }
            else {
                _parse64 = _engine as IActiveScriptParse64;
                _parse64.InitNew();
            }
        }
Пример #18
0
        public IEJavaScriptEngine()
        {
            try
            {
                engine = new ChakraJavaScriptEngine() as IActiveScript;
            }
            catch
            {
                engine = new JavaScriptEngine() as IActiveScript;
            }

            if (engine == null)
            {
                throw new Exception("Could not create IE JavaScript engine.");
            }

            parser = new ActiveScriptParseWrapper(engine);

            parser.InitNew();
            engine.SetScriptSite(this);

            engine.GetScriptDispatch(null, out dispatch);
            dispatchType = dispatch.GetType();
        }
Пример #19
0
        /// <summary> 
        /// Initializes a new instance of the <see cref="ScriptEngine"/> class. 
        /// </summary>
        public ScriptEngine()
        {
            try
            {
                _engine = Activator.CreateInstance(Type.GetTypeFromCLSID(new Guid("{16d51579-a30b-4c8b-a276-0ff4dc41e755}"), true)) as IActiveScript;
            }
            catch
            {
            	_engine = Activator.CreateInstance(Type.GetTypeFromProgID("javascript", true)) as IActiveScript;
            }

            Site = new ScriptSite();
            _engine.SetScriptSite(Site);

            // support 32-bit & 64-bit process 
            if (IntPtr.Size == 4)
            {
                _parse32 = (IActiveScriptParse32)_engine;
                _parse32.InitNew();
            }
            else
            {
                _parse64 = (IActiveScriptParse64)_engine;
                _parse64.InitNew();
            }
        }
Пример #20
0
 public override void SetScriptSite(IActiveScriptSite site)
 {
     activeScript.SetScriptSite(site);
 }
Пример #21
0
        /// <summary>
        ///     Initializes a new instance of the <see cref="ScriptEngine" /> class.
        /// </summary>
        /// <param name="language">The scripting language. Standard Windows Script engines names are 'jscript' or 'vbscript'.</param>
        public ScriptEngine(string language)
        {
            if (language == null)
                throw new ArgumentNullException("language");

            var engine = Type.GetTypeFromProgID(language, true);
            _engine = Activator.CreateInstance(engine) as IActiveScript;
            if (_engine == null)
                throw new ArgumentException(language + " is not an Windows Script Engine", "language");

            _site = new ScriptSite();
            _engine.SetScriptSite(_site);

            // support 32-bit & 64-bit process
            if (IntPtr.Size == 4)
            {
                _parse32 = _engine as IActiveScriptParse32;
                _parse32.InitNew();
            }
            else
            {
                _parse64 = _engine as IActiveScriptParse64;
                _parse64.InitNew();
            }
        }
Пример #22
0
 /// <summary>
 /// Informs the scripting engine of the <see cref="IActiveScriptSite"/> interface site
 /// provided by the host. Call this method before any other <see cref="IActiveScript"/>
 /// interface methods is used.
 /// </summary>
 /// <param name="site">The host-supplied script site to be associated with this instance
 /// of the scripting engine. The site must be uniquely assigned to this scripting engine
 /// instance; it cannot be shared with other scripting engines.</param>
 public void SetScriptSite(IActiveScriptSite site)
 {
     _activeScript.SetScriptSite(site);
 }