Esempio n. 1
0
        /// <summary>
        /// Constructs an instance of adapter for the ChakraCore JS engine
        /// </summary>
        /// <param name="settings">Settings of the ChakraCore JS engine</param>
        public ChakraCoreJsEngine(ChakraCoreSettings settings)
        {
#if NETFULL
            Initialize();
#endif
            ChakraCoreSettings chakraCoreSettings = settings ?? new ChakraCoreSettings();

            JsRuntimeAttributes attributes = JsRuntimeAttributes.AllowScriptInterrupt;
            if (chakraCoreSettings.DisableBackgroundWork)
            {
                attributes |= JsRuntimeAttributes.DisableBackgroundWork;
            }
            if (chakraCoreSettings.DisableEval)
            {
                attributes |= JsRuntimeAttributes.DisableEval;
            }
            if (chakraCoreSettings.DisableExecutablePageAllocation)
            {
                attributes |= JsRuntimeAttributes.DisableExecutablePageAllocation;
            }
            if (chakraCoreSettings.DisableFatalOnOOM)
            {
                attributes |= JsRuntimeAttributes.DisableFatalOnOOM;
            }
            if (chakraCoreSettings.DisableNativeCodeGeneration)
            {
                attributes |= JsRuntimeAttributes.DisableNativeCodeGeneration;
            }
            if (chakraCoreSettings.EnableExperimentalFeatures)
            {
                attributes |= JsRuntimeAttributes.EnableExperimentalFeatures;
            }

#if NETSTANDARD1_3
            _dispatcher = new ScriptDispatcher();
#else
            _dispatcher = new ScriptDispatcher(chakraCoreSettings.MaxStackSize);
#endif
            _promiseContinuationCallback = PromiseContinuationCallback;

            try
            {
                _dispatcher.Invoke(() =>
                {
                    _jsRuntime             = JsRuntime.Create(attributes, null);
                    _jsRuntime.MemoryLimit = settings.MemoryLimit;

                    _jsContext = _jsRuntime.CreateContext();
                    if (_jsContext.IsValid)
                    {
                        _jsContext.AddRef();
                    }
                });
            }
            catch (DllNotFoundException e)
            {
                throw WrapDllNotFoundException(e);
            }
            catch (Exception e)
            {
                throw CoreErrorHelpers.WrapEngineLoadException(e, EngineName, EngineVersion, true);
            }
            finally
            {
                if (!_jsContext.IsValid)
                {
                    Dispose();
                }
            }
        }
Esempio n. 2
0
        protected override IPrecompiledScript InnerPrecompile(string code, string documentName)
        {
            string uniqueDocumentName = _documentNameManager.GetUniqueName(documentName);

            IPrecompiledScript precompiledScript = _dispatcher.Invoke(() =>
            {
                using (CreateJsScope())
                {
                    try
                    {
                        JsParseScriptAttributes parseAttributes = JsParseScriptAttributes.None;
                        byte[] cachedBytes = JsContext.SerializeScript(code, ref parseAttributes);

                        return(new ChakraCorePrecompiledScript(code, parseAttributes, cachedBytes, uniqueDocumentName));
                    }
                    catch (OriginalException e)
                    {
                        throw WrapJsException(e, uniqueDocumentName);
                    }
                }
            });

            return(precompiledScript);
        }
Esempio n. 3
0
        /// <summary>
        /// Constructs an instance of adapter for the ChakraCore JS engine
        /// </summary>
        /// <param name="settings">Settings of the ChakraCore JS engine</param>
        public ChakraCoreJsEngine(ChakraCoreSettings settings)
        {
            ChakraCoreSettings chakraCoreSettings = settings ?? new ChakraCoreSettings();

            JsRuntimeAttributes attributes = JsRuntimeAttributes.None;

            if (chakraCoreSettings.DisableBackgroundWork)
            {
                attributes |= JsRuntimeAttributes.DisableBackgroundWork;
            }
            if (chakraCoreSettings.DisableNativeCodeGeneration)
            {
                attributes |= JsRuntimeAttributes.DisableNativeCodeGeneration;
            }
            if (chakraCoreSettings.DisableEval)
            {
                attributes |= JsRuntimeAttributes.DisableEval;
            }
            if (chakraCoreSettings.EnableExperimentalFeatures)
            {
                attributes |= JsRuntimeAttributes.EnableExperimentalFeatures;
            }

            _externalObjectFinalizeCallback = ExternalObjectFinalizeCallback;

            _dispatcher.Invoke(() =>
            {
                try
                {
                    _jsRuntime = JsRuntime.Create(attributes, null);
                    _jsContext = _jsRuntime.CreateContext();
                    _jsContext.AddRef();
                }
                catch (Exception e)
                {
                    throw new JsEngineLoadException(
                        string.Format(CoreStrings.Runtime_JsEngineNotLoaded,
                                      EngineName, e.Message), EngineName, EngineVersion, e);
                }
            });
        }