| 1 | module main |
| 2 | |
| 3 | import arena |
| 4 | import scheme |
| 5 | |
| 6 | struct Outcome { |
| 7 | text string |
| 8 | limited bool |
| 9 | used i64 |
| 10 | limit i64 |
| 11 | } |
| 12 | |
| 13 | // Value and IError stay in this region. Only borrowed text and scalar fields |
| 14 | // return; main copies the text after this helper's defer restores the host. |
| 15 | fn evaluate_borrowed(region &arena.Arena) Outcome { |
| 16 | region.push() |
| 17 | defer { region.pop() } |
| 18 | mut vm := scheme.new_with_options(scheme.Options{ max_steps: 0, max_frames: 4 }) or { return Outcome{ text: err.msg() } } |
| 19 | // This is absolute used(), including VM setup and controller allocations. |
| 20 | limit := i64(region.used()) + 128 * 1024 |
| 21 | value := vm.with_arena_usage_limit(region, limit, fn (mut vm scheme.VM) !scheme.Value { |
| 22 | return vm.eval('(let loop ((n 0)) (cons n n) (loop (+ n 1)))') |
| 23 | }) or { |
| 24 | if err is scheme.ArenaUsageLimitRequest { |
| 25 | return Outcome{ text: err.msg(), limited: true, used: err.used, limit: err.limit } |
| 26 | } |
| 27 | return Outcome{ text: err.msg() } |
| 28 | } |
| 29 | return Outcome{ text: scheme.write(value) or { err.msg() } } |
| 30 | } |
| 31 | |
| 32 | fn main() { |
| 33 | mut region := arena.new() |
| 34 | borrowed := evaluate_borrowed(®ion) |
| 35 | owned := Outcome{ ...borrowed, text: borrowed.text.clone() } |
| 36 | region.free() |
| 37 | println(owned.text) |
| 38 | unsafe { owned.text.free() } |
| 39 | } |
| 40 | |