예제 #1
0
        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);
                }
            }
        }
예제 #2
0
        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);
        }
예제 #3
0
        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);
            }
        }
예제 #4
0
        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);
            }
        }
예제 #5
0
        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);
                    }
                }
            }
        }
예제 #6
0
        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);
        }
예제 #7
0
 public ScriptVars(ScriptVars source)
     : base(source)
 {
 }