-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProgram.cs
More file actions
54 lines (48 loc) · 1.57 KB
/
Copy pathProgram.cs
File metadata and controls
54 lines (48 loc) · 1.57 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
48
49
50
51
52
53
54
// SPDX-License-Identifier: Apache-2.0
// Copyright 2026 Pocok contributors
using Pocok.Scripting.CSharp;
using Pocok.Scripting.Execution;
using Pocok.Scripting.JavaScript;
using Pocok.Scripting.Python;
IScriptEngineAdapter[] adapters =
[
new JavaScriptScriptEngineAdapter(),
new CSharpScriptEngineAdapter(),
new PythonScriptEngineAdapter()
];
var registry = new ScriptEngineRegistry(adapters);
var runner = new ScriptRunner(registry);
var sources = new Dictionary<ScriptEngineId, string>
{
[ScriptEngineId.JavaScript] = "21 * 2;",
[ScriptEngineId.CSharp] = "21 * 2",
[ScriptEngineId.Python] = "21 * 2"
};
foreach (ScriptEngineDescriptor descriptor in registry.Descriptors)
{
if (!descriptor.IsAvailable)
{
Console.WriteLine($"{descriptor.Language}: {descriptor.UnavailableCode}");
continue;
}
var options = new ScriptExecutionOptions { Timeout = TimeSpan.FromSeconds(5) };
if (descriptor.Id == ScriptEngineId.JavaScript)
options = options with
{
MaxStatements = 1_000,
MaxRecursionDepth = 32,
MaxMemoryBytes = 8 * 1024 * 1024
};
ScriptResult<int> result = await runner.ExecuteAsync<int>(
new ScriptExecutionRequest(descriptor.Id, "console", sources[descriptor.Id])
{
ExpectResult = true
},
options);
Console.WriteLine(result.IsSuccess
? $"{descriptor.Language}: {result.Value}"
: $"{descriptor.Language}: {result.Failure!.Code}");
if (!result.IsSuccess || result.Value != 42)
return 1;
}
return 0;