| 1 | module main |
| 2 | |
| 3 | import arena |
| 4 | import context |
| 5 | import scheme |
| 6 | import time |
| 7 | |
| 8 | // Text borrows the region until main copies it after this function pops it. |
| 9 | fn run_borrowed(region &arena.Arena, mut ctx context.Context) string { |
| 10 | region.push() |
| 11 | defer { region.pop() } |
| 12 | mut vm := scheme.new_with_options(scheme.Options{ max_steps: 0 }) or { return err.msg() } |
| 13 | value := vm.with_context(mut ctx, fn (mut vm scheme.VM) !scheme.Value { |
| 14 | return vm.eval('(let loop () (loop))') |
| 15 | }) or { |
| 16 | if err is scheme.CancellationRequest { return err.msg() } |
| 17 | return 'evaluation failed: ${err.msg()}' |
| 18 | } |
| 19 | return scheme.write(value) or { err.msg() } |
| 20 | } |
| 21 | |
| 22 | fn main() { |
| 23 | // Contexts, channels, and timer/cancel machinery belong to the host. |
| 24 | // A standard timer goroutine may remain alive after cancel() returns. |
| 25 | mut background := context.background() |
| 26 | mut ctx, cancel := context.with_timeout(mut background, 20 * time.millisecond) |
| 27 | defer { cancel() } |
| 28 | mut region := arena.new() |
| 29 | borrowed := run_borrowed(®ion, mut ctx) |
| 30 | text := borrowed.clone() |
| 31 | region.free() |
| 32 | println(text) |
| 33 | unsafe { text.free() } |
| 34 | } |
| 35 | |