public WritableContext(Engine engine, ScriptVars vars) : base(engine) { this.vars = vars; foreach (var(key, value) in vars) { var property = key.ToCamelCase(); if (value != null) { FastAddProperty(property, FromObject(engine, value), true, true, true); } } }
public ExecutionContext Extend(ScriptVars vars, ScriptOptions options) { var engine = Engine; if (options.AsContext) { var contextInstance = new ObjectInstance(engine); foreach (var(key, value) in vars) { var property = key.ToCamelCase(); if (value != null) { contextInstance.FastAddProperty(property, JsValue.FromObject(engine, value), true, true, true); this[property] = value; } } engine.SetValue("ctx", contextInstance); engine.SetValue("context", contextInstance); } else { foreach (var(key, value) in vars) { var property = key.ToCamelCase(); if (value != null) { engine.SetValue(property, value); this[property] = value; } } } engine.SetValue("async", true); return(this); }
public string?Interpolate(ScriptVars vars, string script) { Guard.NotNull(vars, nameof(vars)); Guard.NotNullOrEmpty(script, nameof(script)); try { var engine = CreateEngine(vars, false); Execute(engine, script); var converted = engine.GetCompletionValue().ToObject()?.ToString() ?? "null"; return(converted == "undefined" ? "null" : converted); } catch (Exception ex) { return(ex.Message); } }
public bool Evaluate(ScriptVars vars, string script) { Guard.NotNull(vars, nameof(vars)); Guard.NotNullOrEmpty(script, nameof(script)); try { var engine = CreateEngine(vars, false); Execute(engine, script); var converted = Equals(engine.GetCompletionValue().ToObject(), true); return(converted); } catch { return(false); } }
public async Task <IJsonValue> ExecuteAsync(ScriptVars vars, string script, ScriptOptions options = default, CancellationToken ct = default) { Guard.NotNull(vars, nameof(vars)); Guard.NotNullOrEmpty(script, nameof(script)); using (var cts = new CancellationTokenSource(timeoutExecution)) { using (var combined = CancellationTokenSource.CreateLinkedTokenSource(cts.Token, ct)) { var tcs = new TaskCompletionSource <IJsonValue>(); await using (combined.Token.Register(() => tcs.TrySetCanceled(combined.Token))) { var context = CreateEngine(options) .Extend(vars, options) .Extend(extensions) .ExtendAsync(extensions, tcs.TrySetException, combined.Token); context.Engine.SetValue("complete", new Action <JsValue?>(value => { tcs.TrySetResult(JsonMapper.Map(value)); })); var result = Execute(context.Engine, script); if (!context.IsAsync) { tcs.TrySetResult(JsonMapper.Map(result)); } return(await tcs.Task); } } } }
private ExecutionContext CreateEngine(ScriptVars vars, ScriptOptions options, ExceptionHandler?exceptionHandler = null, bool async = false, CancellationToken ct = default) { var engine = new Engine(options => { options.AddObjectConverter(DefaultConverter.Instance); options.SetReferencesResolver(NullPropagation.Instance); options.Strict(); options.TimeoutInterval(TimeoutScript); }); if (options.CanDisallow) { engine.AddDisallow(); } if (options.CanReject) { engine.AddReject(); } foreach (var extension in extensions) { extension.Extend(engine); } var context = new ExecutionContext(engine, ct, exceptionHandler); context.AddVariables(vars, options); foreach (var extension in extensions) { extension.Extend(context, async); } return(context); }
public ScriptVars(ScriptVars source) : base(source) { }