| | | 1 | | using System.Dynamic; |
| | | 2 | | using Elsa.Expressions.Contracts; |
| | | 3 | | using Elsa.Expressions.Helpers; |
| | | 4 | | using Elsa.Expressions.Models; |
| | | 5 | | using Elsa.Expressions.JavaScript.Contracts; |
| | | 6 | | using Humanizer; |
| | | 7 | | using Jint; |
| | | 8 | | |
| | | 9 | | namespace Elsa.Expressions.JavaScript.Expressions; |
| | | 10 | | |
| | | 11 | | /// <summary> |
| | | 12 | | /// Evaluates JavaScript expressions. |
| | | 13 | | /// </summary> |
| | | 14 | | public class JavaScriptExpressionHandler : IExpressionHandler |
| | | 15 | | { |
| | | 16 | | private readonly IJavaScriptEvaluator _javaScriptEvaluator; |
| | | 17 | | |
| | | 18 | | /// <summary> |
| | | 19 | | /// Initializes a new instance of the <see cref="JavaScriptExpressionHandler"/> class. |
| | | 20 | | /// </summary> |
| | 98 | 21 | | public JavaScriptExpressionHandler(IJavaScriptEvaluator javaScriptEvaluator) |
| | | 22 | | { |
| | 98 | 23 | | _javaScriptEvaluator = javaScriptEvaluator; |
| | 98 | 24 | | } |
| | | 25 | | |
| | | 26 | | /// <inheritdoc /> |
| | | 27 | | public async ValueTask<object?> EvaluateAsync(Expression expression, Type returnType, ExpressionExecutionContext con |
| | | 28 | | { |
| | 98 | 29 | | var javaScriptExpression = expression.Value.ConvertTo<string>() ?? ""; |
| | 196 | 30 | | return await _javaScriptEvaluator.EvaluateAsync(javaScriptExpression, returnType, context, options, engine => Co |
| | 98 | 31 | | } |
| | | 32 | | |
| | | 33 | | private void ConfigureEngine(Engine engine, ExpressionEvaluatorOptions options) |
| | | 34 | | { |
| | 98 | 35 | | var args = new ExpandoObject() as IDictionary<string, object>; |
| | | 36 | | |
| | 202 | 37 | | foreach (var (name, value) in options.Arguments) |
| | 3 | 38 | | args[name.Camelize()] = value; |
| | | 39 | | |
| | 98 | 40 | | engine.SetValue("args", args); |
| | 98 | 41 | | } |
| | | 42 | | } |