public virtual object Execute(string script, IScriptingScope scope) { if (scope == null) { throw new ArgumentNullException(nameof(scope)); } var jsScope = scope as JavascriptScope; if (jsScope == null) { throw new ArgumentException("Invalid argument 'scope'."); } if (string.IsNullOrEmpty(script)) { return(null); } try { return(jsScope.Engine.Execute(script) .GetCompletionValue()? .ToObject()); } catch (Exception ex) { throw new ScriptingException($"The javascript execution errors; {ex.Message}.", ex); } }
public object ExecuteCompileUnit(object compileUnit, IScriptingScope scope) { if (compileUnit == null) { throw new ArgumentNullException(nameof(compileUnit)); } if (scope == null) { throw new ArgumentNullException(nameof(scope)); } if (!(compileUnit is Esprima.Ast.Program)) { throw new ArgumentException("Invalid compileUnit."); } var jsScope = scope as JavascriptScope; if (jsScope == null) { throw new ArgumentException("Invalid argument 'scope'."); } try { return(jsScope.Engine.Execute(compileUnit as Esprima.Ast.Program) .GetCompletionValue()? .ToObject()); } catch (Exception ex) { throw new ScriptingException($"The javascript execution errors; {ex.Message}.", ex); } }
public object Evaluate(IScriptingScope scope, string script) { if (scope == null) { throw new ArgumentNullException(nameof(scope)); } if (!(scope is FilesScriptScope fileScope)) { throw new ArgumentException($"Expected a scope of type {nameof(FilesScriptScope)}", nameof(scope)); } if (script.StartsWith("text('") && script.EndsWith("')")) { var filePath = script.Substring(6, script.Length - 8); var fileInfo = fileScope.FileProvider.GetRelativeFileInfo(fileScope.BasePath, filePath); if (!fileInfo.Exists) { throw new FileNotFoundException(filePath); } using (var fileStream = fileInfo.CreateReadStream()) { using (var streamReader = new StreamReader(fileStream)) { return(streamReader.ReadToEnd()); } } } else if (script.StartsWith("base64('") && script.EndsWith("')")) { var filePath = script.Substring(8, script.Length - 10); var fileInfo = fileScope.FileProvider.GetRelativeFileInfo(fileScope.BasePath, filePath); if (!fileInfo.Exists) { throw new FileNotFoundException(filePath); } using (var fileStream = fileInfo.CreateReadStream()) { using (var ms = new MemoryStream()) { fileStream.CopyTo(ms); return(Convert.ToBase64String(ms.ToArray())); } } } else { throw new ArgumentException($"Unknown command '{script}'"); } }
public object Evaluate(IScriptingScope scope, string script) { if (scope == null) { throw new ArgumentNullException(nameof(scope)); } var jsScope = scope as JavaScriptScope; if (jsScope == null) { throw new ArgumentException($"Expected a scope of type {nameof(JavaScriptScope)}", nameof(scope)); } var result = jsScope.Engine.Execute(script).GetCompletionValue()?.ToObject(); return(result); }
public object Evaluate(IScriptingScope scope, string script) { if (scope == null) { throw new ArgumentNullException(nameof(scope)); } var jsScope = scope as JavaScriptScope; if (jsScope == null) { throw new ArgumentException($"Expected a scope of type {nameof(JavaScriptScope)}", nameof(scope)); } var parsedAst = _memoryCache.GetOrCreate(script, entry => { var parser = new JavaScriptParser(); return(parser.Parse(script)); }); var result = jsScope.Engine.Execute(parsedAst).GetCompletionValue()?.ToObject(); return(result); }
static RuleBenchmark() { var services = RuleTests.CreateRuleServiceCollection() .AddCondition <HomepageCondition, HomepageConditionEvaluator, ConditionFactory <HomepageCondition> >() .AddSingleton <IGlobalMethodProvider, DefaultLayersMethodProvider>() .AddMemoryCache() .AddScripting() .AddJavaScriptEngine(); var mockHttpContextAccessor = new Mock <IHttpContextAccessor>(); var context = new DefaultHttpContext(); context.Request.Path = new PathString("/"); mockHttpContextAccessor.Setup(_ => _.HttpContext).Returns(context); services.AddSingleton <IHttpContextAccessor>(mockHttpContextAccessor.Object); var serviceProvider = services.BuildServiceProvider(); var scriptingManager = serviceProvider.GetRequiredService <IScriptingManager>(); _engine = scriptingManager.GetScriptingEngine("js"); _scope = _engine.CreateScope(scriptingManager.GlobalMethodProviders.SelectMany(x => x.GetMethods()), serviceProvider, null, null); _ruleService = serviceProvider.GetRequiredService <IRuleService>(); _rule = new Rule { Conditions = new List <Condition> { new HomepageCondition { Value = true } } }; }
public JavascriptEvalutor(ExecutionContext executionContext) { engine = new JavascriptEngine(); scope = engine.CreateScope(new ScriptingContext(executionContext)); }