-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathProgram.cs
More file actions
40 lines (33 loc) · 1.29 KB
/
Program.cs
File metadata and controls
40 lines (33 loc) · 1.29 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
// Basic example — execute Python code in a secure sandbox.
//
// Mirrors: src/wasm_sandbox/examples/python_basics.rs
//
// Prerequisites:
// just dotnet build # builds the .NET SDK + bundled guest package
using HyperlightSandbox.Api;
using HyperlightSandbox.Guest.Python;
Console.WriteLine("=== Hyperlight Sandbox .NET — Basic Example ===\n");
using var sandbox = new SandboxBuilder()
.WithPythonModule()
.Build();
// --- Test 1: Basic code execution ---
Console.WriteLine("═══ Test 1: Basic code execution ═══");
var result = sandbox.Run("""
import math
primes = [n for n in range(2, 50) if all(n % i != 0 for i in range(2, int(math.sqrt(n)) + 1))]
print(f"Primes under 50: {primes}")
print(f"Count: {len(primes)}")
""");
Console.WriteLine($"stdout: {result.Stdout}");
Console.WriteLine($"stderr: {result.Stderr}");
Console.WriteLine($"exit_code: {result.ExitCode}");
Console.WriteLine($"success: {result.Success}\n");
// --- Test 2: Multiple runs ---
Console.WriteLine("═══ Test 2: Multiple sequential runs ═══");
for (int i = 1; i <= 3; i++)
{
var r = sandbox.Run($"print('Run {i}: Hello from the sandbox!')");
Console.WriteLine($" Run {i}: {r.Stdout.Trim()}");
}
Console.WriteLine("\n✅ Basic example finished successfully!");
return 0;