| 1 | module scheme |
| 2 | |
| 3 | import arena |
| 4 | import reader |
| 5 | |
| 6 | fn test_builder_without_name_emission_allocates_no_arena_storage() { |
| 7 | code := &Code{} |
| 8 | function := &Function{ code: code } |
| 9 | mut region := arena.new() |
| 10 | region.push() |
| 11 | before := region.used() |
| 12 | builder := Builder{ code: code, function: function } |
| 13 | assert isnil(builder.table) |
| 14 | assert region.used() == before |
| 15 | region.pop() |
| 16 | region.free() |
| 17 | } |
| 18 | |
| 19 | fn assert_compiled_function_has_no_names(function &Function) { |
| 20 | assert function.names.len == 0 && function.global_slots.len == 0 |
| 21 | for child in function.children { assert_compiled_function_has_no_names(child) } |
| 22 | } |
| 23 | |
| 24 | fn test_literal_and_lexical_compilation_never_creates_a_global_name_cache() { |
| 25 | for source, expected in { |
| 26 | '42': '42' |
| 27 | '"retained λ😀"': '"retained λ😀"' |
| 28 | "'(a b c)": '(a b c)' |
| 29 | '#(1 "λ😀")': '#(1 "λ😀")' |
| 30 | '(if #t 42 (if))': '42' |
| 31 | '((lambda (x) x) 42)': '42' |
| 32 | '(let ((x 42)) ((lambda () x)))': '42' |
| 33 | } { |
| 34 | code := &Code{ doc: reader.read_all(source) or { panic(err) } } |
| 35 | mut builder := Builder{ code: code, function: &Function{ code: code } } |
| 36 | assert isnil(builder.table) |
| 37 | builder.sequence(code.doc.forms, true) |
| 38 | builder.emit(.ret, 0, 0, code.doc.forms[0]) |
| 39 | assert isnil(builder.table) |
| 40 | function := builder.finish_program() |
| 41 | assert_compiled_function_has_no_names(function) |
| 42 | mut vm := new() |
| 43 | mut machine := Machine{ function: function } |
| 44 | assert write(machine.run(mut vm) or { panic(err) }) or { panic(err) } == expected |
| 45 | } |
| 46 | } |
| 47 | |
| 48 | fn test_sequential_binding_builders_share_first_initializer_name_indices() { |
| 49 | for source, expected_names in { |
| 50 | '(begin (let* ((x global) (y (+ x global))) y) global)': ['global'] |
| 51 | '(begin (let*-values (((x) (values global)) ((y) (values (+ x global)))) y) global)': [ |
| 52 | 'values', |
| 53 | 'global', |
| 54 | ] |
| 55 | } { |
| 56 | code := &Code{ doc: reader.read_all(source) or { panic(err) } } |
| 57 | function := compile_program(code, code.doc.forms) |
| 58 | // The first initializer and final reference use the same function. |
| 59 | // A lazy cache lost while copying Builder would duplicate global here. |
| 60 | assert function.names == expected_names |
| 61 | assert function.global_slots.len == expected_names.len |
| 62 | assert function.children.len == 1 |
| 63 | assert function.children[0].names.contains('global') |
| 64 | mut vm := new() |
| 65 | vm.define('global', integer(42)) or { panic(err) } |
| 66 | mut machine := Machine{ function: function } |
| 67 | assert machine.run(mut vm) or { panic(err) }.whole() == 42 |
| 68 | } |
| 69 | } |
| 70 | |
| 71 | fn test_lazily_named_staged_macro_closures_and_diagnostics_remain_valid() { |
| 72 | mut vm := new() |
| 73 | vm.define('global', integer(40)) or { panic(err) } |
| 74 | vm.eval('(define-syntax declare (syntax-rules () ((_ exported) |
| 75 | (begin (define (exported n) (let* ((x (+ n global))) (lambda () x))))))) |
| 76 | (declare saved)') or { panic(err) } |
| 77 | closure := vm.call(vm.lookup('saved') or { panic(err) }, [integer(2)]) or { panic(err) } |
| 78 | vm.eval('(define (bad)\n (let*-values (((x) (values global))) (+ x #f)))') or { panic(err) } |
| 79 | for _ in 0 .. 4 { vm.eval('42') or { panic(err) } } |
| 80 | gc_collect() |
| 81 | assert vm.call(closure, []) or { panic(err) }.whole() == 42 |
| 82 | if _ := vm.call(vm.lookup('bad') or { panic(err) }, []) { |
| 83 | assert false |
| 84 | } else { |
| 85 | assert err is EvalError |
| 86 | if err is EvalError { |
| 87 | assert err.line == 2 |
| 88 | } |
| 89 | } |
| 90 | } |
| 91 | |