/// <summary>
        /// Constructs an instance of the Active Script engine
        /// </summary>
        /// <param name="engineMode">JS engine mode</param>
        /// <param name="enableDebugging">Flag for whether to enable script debugging features</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(JsEngineMode engineMode, bool enableDebugging,
                                           bool useEcmaScript5Polyfill, bool useJson2Library)
            : base(engineMode)
        {
            string clsid;
            string lowerIeVersion;
            ScriptLanguageVersion languageVersion;

            if (_engineMode == JsEngineMode.ChakraActiveScript)
            {
                clsid           = ClassId.Chakra;
                lowerIeVersion  = "9";
                languageVersion = ScriptLanguageVersion.EcmaScript5;
            }
            else if (_engineMode == JsEngineMode.Classic)
            {
                clsid           = ClassId.Classic;
                lowerIeVersion  = "6";
                languageVersion = ScriptLanguageVersion.None;
            }
            else
            {
                throw new NotSupportedException();
            }

            _dispatcher.Invoke(() =>
            {
                try
                {
                    _activeScriptWrapper = new ActiveScriptWrapper(clsid, languageVersion);
                }
                catch (Exception e)
                {
                    throw new JsEngineLoadException(
                        string.Format(CommonStrings.Runtime_IeJsEngineNotLoaded,
                                      _engineModeName, lowerIeVersion, e.Message), _engineModeName);
                }

                if (enableDebugging)
                {
                    StartDebugging();
                }

                _activeScriptWrapper.SetScriptSite(new ScriptSite(this));
                _activeScriptWrapper.InitNew();
                _activeScriptWrapper.SetScriptState(ScriptState.Started);

                InitScriptDispatch();
            });

            LoadResources(useEcmaScript5Polyfill, useJson2Library);
        }
        /// <summary>
        /// Destroys object
        /// </summary>
        /// <param name="disposing">Flag, allowing destruction of
        /// managed objects contained in fields of class</param>
        private void Dispose(bool disposing)
        {
            if (_disposedFlag.Set())
            {
                _dispatcher.Invoke(() =>
                {
                    if (_dispatch != null)
                    {
                        ComHelpers.ReleaseComObject(ref _dispatch, !disposing);
                        _dispatch = null;
                    }

                    if (_activeScriptWrapper != null)
                    {
                        _activeScriptWrapper.Dispose();
                        _activeScriptWrapper = null;
                    }
                });

                if (disposing)
                {
                    if (_debuggingStarted && _debugDocuments != null)
                    {
                        foreach (UIntPtr debugDocumentKey in _debugDocuments.Keys)
                        {
                            var debugDocumentValue = _debugDocuments[debugDocumentKey];
                            debugDocumentValue.Close();
                        }

                        _debugDocuments.Clear();
                    }

                    if (_processDebugManagerWrapper != null)
                    {
                        _processDebugManagerWrapper.RemoveApplication(_debugApplicationCookie);
                        _debugApplicationWrapper.Close();
                    }

                    if (_hostItems != null)
                    {
                        _hostItems.Clear();
                    }

                    _lastException = null;
                }
            }
        }