forked from danielgerlag/conductor
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathScriptEngineHost.cs
More file actions
47 lines (39 loc) · 1.5 KB
/
ScriptEngineHost.cs
File metadata and controls
47 lines (39 loc) · 1.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
using System;
using System.Collections.Generic;
using Conductor.Domain.Interfaces;
using Conductor.Domain.Models;
using Microsoft.Scripting;
namespace Conductor.Domain.Scripting
{
public class ScriptEngineHost : IScriptEngineHost
{
private readonly IScriptEngineFactory _engineFactory;
public ScriptEngineHost(IScriptEngineFactory engineFactory)
{
_engineFactory = engineFactory;
}
public void Execute(Resource resource, IDictionary<string, object> inputs)
{
var engine = _engineFactory.GetEngine(resource.ContentType);
var source = engine.CreateScriptSourceFromString(resource.Content, SourceCodeKind.Statements);
var scope = engine.CreateScope(inputs);
source.Execute(scope);
SanitizeScope(inputs);
}
public dynamic EvaluateExpression(string expression, IDictionary<string, object> inputs)
{
var engine = _engineFactory.GetExpressionEngine();
var source = engine.CreateScriptSourceFromString(expression, SourceCodeKind.Expression);
var scope = engine.CreateScope(inputs);
return source.Execute(scope);
}
public T EvaluateExpression<T>(string expression, IDictionary<string, object> inputs)
{
return EvaluateExpression(expression, inputs);
}
private void SanitizeScope(IDictionary<string, object> scope)
{
scope.Remove("__builtins__");
}
}
}