| 1 | module main |
| 2 | |
| 3 | import scheme |
| 4 | |
| 5 | pub struct HostContext { |
| 6 | mut: |
| 7 | signal chan int |
| 8 | reason string |
| 9 | } |
| 10 | |
| 11 | pub fn (mut ctx HostContext) done() chan int { return ctx.signal } |
| 12 | |
| 13 | pub fn (mut ctx HostContext) err() IError { return error(ctx.reason) } |
| 14 | |
| 15 | fn test_external_custom_context_adapter_borrows_the_original_host_object() { |
| 16 | mut ctx := &HostContext{ reason: 'old host reason' } |
| 17 | mut vm := scheme.new() |
| 18 | vm.register('stop-host', fn [mut ctx] (mut _vm scheme.VM, _args []scheme.Value) !scheme.Value { |
| 19 | ctx.reason = 'new host reason' |
| 20 | ctx.signal.close() |
| 21 | return scheme.unspecified() |
| 22 | }) or { panic(err) } |
| 23 | if _ := vm.with_context(mut ctx, fn (mut vm scheme.VM) !scheme.Value { |
| 24 | return vm.eval('(stop-host) 99') |
| 25 | }) { |
| 26 | assert false |
| 27 | } else { |
| 28 | assert err is scheme.CancellationRequest |
| 29 | assert scheme.is_control_transfer(err) |
| 30 | assert err.msg() == 'evaluation canceled: new host reason' |
| 31 | } |
| 32 | assert ctx.reason == 'new host reason' |
| 33 | assert vm.eval('42') or { panic(err) }.as_integer() or { panic(err) } == 42 |
| 34 | } |
| 35 | |