module scheme fn dispatch_hook_text(mut vm VM, source string) string { value := vm.eval(source) or { panic('${source}: ${err.msg()}') } return write(value) or { panic(err) } } fn test_goops_no_applicable_hook_installs_method_and_retries_original_call() { mut vm := new() actual := dispatch_hook_text(mut vm, '(import (vile goops)) (define calls 0) (define seen (quote ())) (define-generic f) (define-method (no-applicable-method (g ) args) (set! calls (+ calls 1)) (set! seen args) (add-method! g (make #:specializers (list ) #:procedure (lambda (a b) (+ a b)))) (quote ignored-result)) (list (f 20 22) (f 3 4) calls seen)') assert actual == '(42 7 1 (20 22))' } fn test_goops_no_applicable_hook_receives_public_accessor_and_can_escape() { mut vm := new() actual := dispatch_hook_text(mut vm, '(import (vile goops)) (define a (make-accessor (quote a))) (define escape #f) (define-method (no-applicable-method (g ) args) (escape (list (eq? g a) (class-name (class-of g)) args))) (call/cc (lambda (k) (set! escape k) (a 1 2)))') assert actual == '(#t (1 2))' } fn test_goops_no_next_hook_gets_original_and_replacement_arguments() { mut vm := new() actual := dispatch_hook_text(mut vm, '(import (vile goops)) (define-method (no-next-method (g ) args) (list (generic-function-name g) args)) (define-method (f (x )) (next-method)) (define-method (g (x )) (next-method 7 8)) (list (f 2) (g 2))') assert actual == '((f (2)) (g (7 8)))' } fn test_goops_no_next_hook_preserves_multiple_values_and_escaped_next_method() { mut vm := new() actual := dispatch_hook_text(mut vm, '(import (vile goops)) (define saved #f) (define-method (no-next-method (g ) args) (values 20 22)) (define-method (f (x )) (set! saved next-method) (next-method)) (list (call-with-values (lambda () (f 1)) +) (call-with-values saved +))') assert actual == '(42 42)' } fn test_goops_dispatch_hook_defaults_and_no_progress_hook_respect_budgets() { mut vm := new_with_options(Options{ max_steps: 2000, max_frames: 8 }) or { panic(err) } dispatch_hook_text(mut vm, '(import (vile goops)) (define-generic empty) (define-method (last (x )) (next-method))') for source in ['(empty 1)', '(last 1)', '(no-method empty (quote (1)))'] { if _ := vm.eval(source) { panic('accepted unhandled dispatch error: ${source}') } } dispatch_hook_text(mut vm, '(define-method (no-applicable-method (g ) args) 1)') if _ := vm.eval('(empty 1)') { panic('accepted endless missing-method retry') } else { assert err.msg().contains('step limit') } assert dispatch_hook_text(mut vm, '(+ 1 2)') == '3' } fn test_goops_dispatch_hooks_export_and_vile_syntax() { mut vm := new() actual := dispatch_hook_text(mut vm, '(define-library (hooks demo) (export no-next-method) (import (scheme base) (vile goops))) (import (hooks demo) (vile goops)) (define-method (no-next-method (g ) args) (car args)) (define-method (f (x )) (next-method 42)) (define method_class ) (define generic_class ) (define top_class ) (f 1)') assert actual == '42' value := vm.eval_alt('import((vile,goops)) add-method!(no-next-method, make(method_class, #:specializers, list(generic_class, top_class), #:procedure, lambda((g,args), 99))) f(1)') or { panic(err) } assert value.whole() == 99 } fn test_goops_missing_method_hook_continuation_reentry_retries_call() { mut vm := new() actual := dispatch_hook_text(mut vm, '(import (vile goops)) (define saved #f) (define-generic f) (define-method (no-applicable-method (g ) args) (call/cc (lambda (k) (set! saved k) 0)) (add-method! g (make #:specializers (list ) #:procedure (lambda (n) (+ n 1))))) (f 41)') assert actual == '42' saved := vm.lookup('saved') or { panic(err) } for _ in 0 .. 3 { result := vm.call(saved, [integer(99)]) or { panic(err) } assert result.whole() == 42 } }