| 1 | module scheme |
| 2 | |
| 3 | import arena |
| 4 | |
| 5 | fn native_exists_expect(mut vm VM, source string, expected string) { |
| 6 | value := vm.eval(source) or { panic(err) } |
| 7 | actual := write(value) or { panic(err) } |
| 8 | if actual != expected { panic('expected ${expected}, got ${actual}') } |
| 9 | } |
| 10 | |
| 11 | fn test_slot_exists_checks_native_and_ordinary_public_layouts() { |
| 12 | mut vm := new() |
| 13 | native_exists_expect(mut vm, '(define c (make-class (quote ()) (quote ((x #:init-value 1))))) |
| 14 | (define object (make c)) |
| 15 | (define m (make <method> #:specializers (list <integer>) #:procedure (lambda (v) v))) |
| 16 | (define s (make <slot> #:name (quote value))) (define g (make-generic (quote g))) |
| 17 | (list (slot-exists? c (quote name)) (slot-exists? c (quote missing)) |
| 18 | (slot-exists? m (quote specializers)) (slot-exists? s (quote name)) |
| 19 | (slot-exists? g (quote methods)) (slot-exists? object (quote x)) |
| 20 | (slot-exists? 42 (quote missing)))', '(#t #f #t #t #t #t #f)') |
| 21 | } |
| 22 | |
| 23 | fn test_slot_exists_vile_frontend_checks_generic_and_metaclass_fields() { |
| 24 | mut vm := new() |
| 25 | value := vm.eval_alt('g := make-generic(quote(g)) |
| 26 | list(slot-exists?(g, quote(procedure)), slot-exists?(g, quote(extended-by)), |
| 27 | slot-exists?(g, quote(extends)), slot-exists?(class-of(42), quote(name)), |
| 28 | slot-exists?(42, quote(name)))') or { panic(err) } |
| 29 | actual := write(value) or { panic(err) } |
| 30 | if actual != '(#t #t #f #t #f)' { panic('wrong Vile native slot existence: ${actual}') } |
| 31 | } |
| 32 | |
| 33 | fn test_slot_exists_validates_names_and_vm_owners_while_borrowed_values_are_live() { |
| 34 | mut vm := new() |
| 35 | predicate := vm.lookup('slot-exists?') or { panic(err) } |
| 36 | mut child := arena.new() |
| 37 | child.push() |
| 38 | method := vm.eval('(make <method> #:specializers (list <integer>) #:procedure (lambda (v) v))') or { panic(err) } |
| 39 | child.pop() |
| 40 | name := symbol('specializers') or { panic(err) } |
| 41 | if !(vm.call(predicate, [method, name]) or { panic(err) }).is_true() { |
| 42 | panic('native layout could not be read while its borrowed owner was live') |
| 43 | } |
| 44 | if _ := vm.call(predicate, [method, integer(0)]) { |
| 45 | panic('slot-exists? accepted a non-symbol name') |
| 46 | } else { |
| 47 | if !err.msg().contains('symbol') { panic(err) } |
| 48 | } |
| 49 | mut other := new() |
| 50 | other_predicate := other.lookup('slot-exists?') or { panic(err) } |
| 51 | if _ := other.call(other_predicate, [method, name]) { |
| 52 | panic('slot-exists? accepted another VM native object') |
| 53 | } else { |
| 54 | if !err.msg().contains('another VM') { panic(err) } |
| 55 | } |
| 56 | child.free() |
| 57 | native_exists_expect(mut vm, '(slot-exists? <integer> (quote name))', '#t') |
| 58 | } |
| 59 | |