Esempio n. 1
0
        public async Task <V8ScriptEngine> ExecuteAsync(IEnumerable <IncludeScript> scripts, ExecutionOptions options = null)
        {
            var scriptList = PrecheckScripts(scripts);

            if (scriptList == null)
            {
                return(null);
            }

            foreach (var includeScript in scriptList)
            {
                if (string.IsNullOrEmpty(includeScript.ScriptId) && !string.IsNullOrEmpty(includeScript.Code))
                {
                    includeScript.ScriptId = includeScript.Code.GetHashCode().ToString(CultureInfo.InvariantCulture);
                }
            }

            if (options == null)
            {
                options = new ExecutionOptions();
            }

            var configAction = new Action <V8ScriptEngine>(engine => engine.ApplyOptions(options));

            return(await ExecuteAsync(scriptList, configAction, options));
        }
Esempio n. 2
0
        public async Task <V8ScriptEngine> ExecuteAsync(IEnumerable <IncludeScript> scripts, Action <V8ScriptEngine> configAction, ExecutionOptions options = null)
        {
            var scriptList = PrecheckScripts(scripts);

            if (scriptList == null)
            {
                return(null);
            }

            if (options == null)
            {
                options = new ExecutionOptions();
            }

            IEnumerable <V8Script> compiledScripts = scriptList.Select(x => _scriptCompiler.Compile(x, options.AddToCache, options.CacheExpirationSeconds));

            GetEngine();

            if (AddConsoleReference)
            {
                _scriptEngine.AddHostType("Console", typeof(Console));
            }

            RequireManager.BuildRequirer(_scriptCompiler, _scriptEngine);

            if (configAction != null)
            {
                configAction(_scriptEngine);
            }

            if (options.Scripts != null)
            {
                foreach (var script in options.Scripts)
                {
                    var compiledInclude = _scriptCompiler.Compile(script, options.AddToCache);
                    if (compiledInclude != null)
                    {
                        _scriptEngine.Execute(compiledInclude);
                    }
                }
            }

            foreach (var compiledScript in compiledScripts)
            {
                //Only create a wrapping task if the script has a timeout.
                CancellationToken cancellationToken;
                if (TryCreateCancellationToken(out cancellationToken))
                {
                    using (cancellationToken.Register(_scriptEngine.Interrupt))
                    {
                        try
                        {
                            V8Script script = compiledScript;
                            await Task.Run(() => _scriptEngine.Execute(script), cancellationToken).ConfigureAwait(false);
                        }
                        catch (ScriptInterruptedException ex)
                        {
                            var newEx = new ScriptInterruptedException(
                                "Script interruption occurred, this often indicates a script timeout.  Examine the data and inner exception for more information.", ex);
                            newEx.Data.Add("Timeout", _settings.ScriptTimeoutMilliSeconds);
                            newEx.Data.Add("ScriptId", compiledScript.Name);

                            throw newEx;
                        }
                    }
                }
                else
                {
                    _scriptEngine.Execute(compiledScript);
                }
            }

            return(_scriptEngine);
        }
Esempio n. 3
0
 public async Task <V8ScriptEngine> ExecuteAsync(string scriptId, string code, ExecutionOptions options = null)
 {
     return(await ExecuteAsync(new[] { new IncludeScript {
                                           ScriptId = scriptId, Code = code
                                       } }, options));
 }
Esempio n. 4
0
 public async Task <V8ScriptEngine> ExecuteAsync(string scriptId, string code, Action <V8ScriptEngine> configAction, ExecutionOptions options = null)
 {
     return(await ExecuteAsync(new[] { new IncludeScript {
                                           Code = code, ScriptId = scriptId
                                       } }, configAction, options));
 }