| 1 | module scheme |
| 2 | |
| 3 | import arena |
| 4 | |
| 5 | // V removes assert expressions in -prod builds. Keep setup and checks outside |
| 6 | // assertions so this test exercises the production runtime too. |
| 7 | fn test_goops_production_generic_dispatch_and_tail_calls() { |
| 8 | mut region := arena.new() |
| 9 | defer { region.free() } |
| 10 | region.push() |
| 11 | mut vm := new() |
| 12 | vm.eval('(import (vile goops)) |
| 13 | (define-method (sum (n <number>) acc) |
| 14 | (if (= n 0) acc (sum (- n 1) (+ acc n)))) |
| 15 | (define-method (sum (n <string>) acc) (+ acc (string-length n))) |
| 16 | (define-method (bounded-non-tail (n <number>)) |
| 17 | (if (= n 0) 0 (+ 1 (bounded-non-tail (- n 1)))))') or { panic(err) } |
| 18 | // Constrain the actual generic tail calls, rather than one-time MOP class |
| 19 | // construction. A non-tail generic control proves the limit is active. |
| 20 | setup_options := vm.options |
| 21 | limited_options := Options{ ...setup_options, max_frames: 4 } |
| 22 | vm.options = limited_options |
| 23 | result := vm.eval('(list (sum 10000 0) (sum "hi" 40))') or { panic(err) } |
| 24 | actual := write(result) or { panic(err) } |
| 25 | if actual != '(50005000 42)' { panic('wrong generic result: ${actual}') } |
| 26 | if vm.options.max_frames != 4 { panic('generic tail check lost its frame limit') } |
| 27 | if _ := vm.eval('(bounded-non-tail 20)') { |
| 28 | panic('non-tail generic recursion ignored the four-frame limit') |
| 29 | } else { |
| 30 | if err !is EvalError || !err.msg().contains('frame limit') { panic(err) } |
| 31 | } |
| 32 | if vm.active.len != 0 { panic('frame rejection retained an evaluator') } |
| 33 | vm.options = setup_options |
| 34 | next_result := vm.eval('(define-class <base> ()) |
| 35 | (define-class <child> (<base>)) |
| 36 | (define-method (describe (x <base>)) (list (quote base))) |
| 37 | (define-method (describe (x <child>)) (cons (quote child) (next-method))) |
| 38 | (describe (make <child>))') or { panic(err) } |
| 39 | next_text := write(next_result) or { panic(err) } |
| 40 | if next_text != '(child base)' { panic('wrong next-method result: ${next_text}') } |
| 41 | shared_result := vm.eval('(define-class <shared-base> () |
| 42 | (x #:allocation #:class #:init-value 1 #:init-keyword #:x)) |
| 43 | (define-class <shared-child> (<shared-base>)) |
| 44 | (define a (make <shared-base>)) |
| 45 | (define b (make <shared-child> #:x 42)) |
| 46 | (list (slot-ref a (quote x)) (slot-ref b (quote x)))') or { panic(err) } |
| 47 | shared_text := write(shared_result) or { panic(err) } |
| 48 | if shared_text != '(42 42)' { panic('wrong shared slot result: ${shared_text}') } |
| 49 | keyword_result := vm.eval('(define-method* (kw (x <number>) #:key (y 2)) (+ x y)) |
| 50 | (define-method* (kw (x <integer>) #:key (y 2)) (+ 1 (next-method))) |
| 51 | (kw 40 #:y 1)') or { panic(err) } |
| 52 | if keyword_result.whole() != 42 { panic('wrong keyword method result') } |
| 53 | region.pop() |
| 54 | mut temporary := arena.new() |
| 55 | temporary.push() |
| 56 | if _ := vm.eval('(define-method (sum (n <number>) acc) 0)') { |
| 57 | panic('published a method from a temporary arena') |
| 58 | } |
| 59 | temporary.pop() |
| 60 | temporary.free() |
| 61 | region.push() |
| 62 | vm.options = limited_options |
| 63 | replayed := vm.eval('(sum 10000 0)') or { panic(err) } |
| 64 | if replayed.whole() != 50005000 { panic('temporary method changed the generic') } |
| 65 | region.pop() |
| 66 | } |
| 67 | |