| 1 | module scheme |
| 2 | |
| 3 | fn slot_protocol_expect(mut vm VM, source string, expected string) { |
| 4 | value := vm.eval(source) or { panic('${source}: ${err}') } |
| 5 | actual := write(value) or { panic(err) } |
| 6 | if actual != expected { panic('expected ${expected}, got ${actual}: ${source}') } |
| 7 | } |
| 8 | |
| 9 | fn test_goops_slot_init_function_uses_class_and_slot_name() { |
| 10 | mut vm := new() |
| 11 | slot_protocol_expect(mut vm, '(define c (let ((offset 40)) |
| 12 | (class () (x #:init-form (+ offset 2)) (y #:init-value 9) (z)))) |
| 13 | (define thunk (slot-init-function c (quote x))) |
| 14 | (list (thunk) ((slot-init-function c (quote y))) |
| 15 | (slot-init-function c (quote z)) |
| 16 | (eq? thunk (slot-definition-init-thunk (class-slot-definition c (quote x)))) |
| 17 | (eq? thunk (slot-init-function c (quote x))))', '(42 9 #f #t #t)') |
| 18 | for source in ['(slot-init-function c (quote absent))', '(slot-init-function c 1)', |
| 19 | '(slot-init-function c)', '(slot-init-function (class-slot-definition c (quote x)))'] { |
| 20 | if _ := vm.eval(source) { panic('accepted invalid slot-init-function: ${source}') } |
| 21 | } |
| 22 | result := vm.eval_alt('slot-init-function(c, quote(y))()') or { panic(err) } |
| 23 | if result.whole() != 9 { panic('wrong Vile initializer result') } |
| 24 | } |
| 25 | |
| 26 | fn test_goops_raw_slot_getter_does_not_call_checked_unbound_hook() { |
| 27 | mut vm := new() |
| 28 | slot_protocol_expect(mut vm, '(define-class c () (s #:allocation #:class)) |
| 29 | (define o (make c)) (define calls 0) |
| 30 | (define d (class-slot-definition c (quote s))) |
| 31 | (define raw (slot-ref d (quote slot-ref/raw))) |
| 32 | (define checked (slot-ref d (quote slot-ref))) |
| 33 | (define-method (slot-unbound (c <class>) (o <object>) name) |
| 34 | (set! calls (+ calls 1)) (quote shared)) |
| 35 | (define-method (slot-unbound (c <class>) name) |
| 36 | (set! calls (+ calls 10)) (quote class)) |
| 37 | (list (eq? raw checked) |
| 38 | (eq? (raw o) (slot-definition-init-value d)) calls |
| 39 | (checked o) calls (class-slot-ref c (quote s)) calls)', |
| 40 | '(#f #t 0 shared 1 class 11)') |
| 41 | } |
| 42 | |
| 43 | fn test_goops_raw_and_checked_slot_getter_fields_are_independently_mutable() { |
| 44 | mut vm := new() |
| 45 | slot_protocol_expect(mut vm, '(define-class c () (s #:allocation #:class #:init-value 1)) |
| 46 | (define d (class-slot-definition c (quote s))) |
| 47 | (define checked (slot-ref d (quote slot-ref))) |
| 48 | (slot-set! d (quote slot-ref/raw) (lambda (o) (if o 90 42))) |
| 49 | (define raw (slot-ref d (quote slot-ref/raw))) |
| 50 | (define read (class-slot-ref c (quote s))) |
| 51 | (slot-set! d (quote slot-ref) (lambda (o) 7)) |
| 52 | (slot-set! d (quote slot-set!) (lambda (o v) (if o 0 (set! read v)))) |
| 53 | (class-slot-set! c (quote s) 9) |
| 54 | (list read (eq? raw (slot-ref d (quote slot-ref/raw))) |
| 55 | (checked (make c)) ((slot-ref d (quote slot-ref)) (make c)))', '(9 #t 1 7)') |
| 56 | } |
| 57 | |
| 58 | fn test_goops_class_unbound_hook_resumes_the_current_continuation() { |
| 59 | mut vm := new() |
| 60 | slot_protocol_expect(mut vm, '(define-class c () (s #:allocation #:class)) |
| 61 | (define saved #f) |
| 62 | (define-method (slot-unbound (c <class>) name) |
| 63 | (call/cc (lambda (k) (set! saved k) 41))) |
| 64 | (+ 1 (class-slot-ref c (quote s)))', '42') |
| 65 | saved := vm.lookup('saved') or { panic(err) } |
| 66 | for n in [10, 20] { |
| 67 | value := vm.call(saved, [integer(n)]) or { panic(err) } |
| 68 | if value.whole() != n + 1 { panic('class-slot-ref skipped its continuation') } |
| 69 | } |
| 70 | } |
| 71 | |
| 72 | fn test_goops_raw_class_slot_metadata_preserves_permissive_options() { |
| 73 | mut vm := new() |
| 74 | slot_protocol_expect(mut vm, '(define c |
| 75 | (make-class (quote ()) (quote ((x #:init-keyword 3 #:unknown 7))))) |
| 76 | (define repeated (make-class (quote ()) (quote ((x #:init-form 1 #:init-form 2))))) |
| 77 | (define-class repeated-allocation () |
| 78 | (x #:allocation #:class #:allocation #:instance #:init-value 3)) |
| 79 | (define-class repeated-getter () |
| 80 | (x #:allocation #:virtual #:slot-ref (lambda (o) 1) #:slot-ref (lambda (o) 2) |
| 81 | #:slot-set! (lambda (o v) v))) |
| 82 | (list (slot-definition-init-keyword (class-slot-definition c (quote x))) |
| 83 | (get-keyword #:unknown (slot-definition-options (class-slot-definition c (quote x)))) |
| 84 | (slot-definition-init-form (class-slot-definition repeated (quote x))) |
| 85 | (slot-definition-allocation (class-slot-definition repeated-allocation (quote x))) |
| 86 | (class-slot-ref repeated-allocation (quote x)) (slot-ref (make repeated-getter) (quote x)))', |
| 87 | '(3 7 1 #:class 3 1)') |
| 88 | } |
| 89 | |