| 1 | module scheme |
| 2 | |
| 3 | import os |
| 4 | import time |
| 5 | |
| 6 | fn include_control_root() string { |
| 7 | root := os.join_path(os.temp_dir(), 'vile-include-control-${os.getpid()}-${time.sys_mono_now()}') |
| 8 | os.mkdir_all(root) or { panic(err) } |
| 9 | return root |
| 10 | } |
| 11 | |
| 12 | fn test_continuations_resume_included_forms_without_reexecuting_prior_forms() { |
| 13 | root := include_control_root() |
| 14 | defer { os.rmdir_all(root) or {} } |
| 15 | os.write_file(os.join_path(root, 'body.scm'), '(set! visits (+ visits 1)) |
| 16 | (call/cc (lambda (k) (set! saved k) 0)) |
| 17 | (set! returns (+ returns 1)) returns') or { panic(err) } |
| 18 | entry := os.join_path(root, 'entry.scm') |
| 19 | os.write_file(entry, '(define visits 0) (define returns 0) (define saved #f) |
| 20 | (include "body.scm") (list visits returns)') or { panic(err) } |
| 21 | mut vm := new() |
| 22 | assert write(vm.eval_file(entry) or { panic(err) }) or { panic(err) } == '(1 1)' |
| 23 | continuation := vm.lookup('saved') or { panic(err) } |
| 24 | code := continuation.procedure_pointer().continuation.function.code |
| 25 | assert !isnil(code.sources) |
| 26 | assert isnil(code.compiler_vm) && isnil(code.known_globals) && isnil(code.syntax_base) |
| 27 | for count in 2 .. 5 { |
| 28 | gc_collect() |
| 29 | assert write(vm.call(continuation, [integer(0)]) or { panic(err) }) or { panic(err) } == '(1 ${count})' |
| 30 | } |
| 31 | } |
| 32 | |
| 33 | fn test_macro_generated_inclusion_preserves_local_macro_hygiene_and_lexical_variables() { |
| 34 | root := include_control_root() |
| 35 | defer { os.rmdir_all(root) or {} } |
| 36 | os.write_file(os.join_path(root, 'body.scm'), '(let-syntax ((plus-x (syntax-rules () ((_ y) (+ x y))))) (plus-x 22))') or { panic(err) } |
| 37 | entry := os.join_path(root, 'entry.scm') |
| 38 | os.write_file(entry, '(define-syntax splice (syntax-rules () ((_ path) (include path)))) |
| 39 | (let ((x 20)) (splice "body.scm"))') or { panic(err) } |
| 40 | mut vm := new() |
| 41 | assert vm.eval_file(entry) or { panic(err) }.whole() == 42 |
| 42 | } |
| 43 | |
| 44 | fn test_included_forms_use_the_selected_host_namespace() { |
| 45 | root := include_control_root() |
| 46 | defer { os.rmdir_all(root) or {} } |
| 47 | os.write_file(os.join_path(root, 'body.scm'), '(define answer (+ 20 22))') or { panic(err) } |
| 48 | entry := os.join_path(root, 'entry.scm') |
| 49 | os.write_file(entry, '(include "body.scm") answer') or { panic(err) } |
| 50 | mut vm := new() |
| 51 | vm.define('answer', integer(99)) or { panic(err) } |
| 52 | space := vm.new_environment() or { panic(err) } |
| 53 | vm.import_bindings(space, vm.interaction_environment() or { panic(err) }, { |
| 54 | '+': '+' |
| 55 | }) or { panic(err) } |
| 56 | assert vm.eval_file_in(entry, space) or { panic(err) }.whole() == 42 |
| 57 | assert vm.lookup('answer') or { panic(err) }.whole() == 99 |
| 58 | assert vm.lookup_in(space, 'answer') or { panic(err) }.whole() == 42 |
| 59 | } |
| 60 | |
| 61 | fn test_inclusion_cycle_detection_handles_symbolic_link_aliases() { |
| 62 | $if windows { |
| 63 | return |
| 64 | } |
| 65 | root := include_control_root() |
| 66 | defer { os.rmdir_all(root) or {} } |
| 67 | entry := os.join_path(root, 'entry.scm') |
| 68 | os.write_file(entry, '(include "alias.scm")') or { panic(err) } |
| 69 | os.symlink(entry, os.join_path(root, 'alias.scm')) or { panic(err) } |
| 70 | mut vm := new() |
| 71 | if _ := vm.eval_file(entry) { |
| 72 | assert false |
| 73 | } else { |
| 74 | assert err.msg().contains('cyclic source inclusion'), err.msg() |
| 75 | } |
| 76 | assert vm.eval('42') or { panic(err) }.whole() == 42 |
| 77 | } |
| 78 | |
| 79 | fn test_library_declaration_expansion_shares_the_calling_machine_budget() { |
| 80 | root := include_control_root() |
| 81 | defer { os.rmdir_all(root) or {} } |
| 82 | for i in 0 .. 5 { |
| 83 | os.write_file(os.join_path(root, 'part${i}.sld'), '(define-library (part${i}) |
| 84 | (cond-expand (vile)) (cond-expand (vile)))') or { panic(err) } |
| 85 | } |
| 86 | entry := os.join_path(root, 'entry.scm') |
| 87 | os.write_file(entry, '(import (part0) (part1) (part2) (part3) (part4))') or { panic(err) } |
| 88 | mut vm := new_with_options(Options{ max_expansions: 6 }) or { panic(err) } |
| 89 | if _ := vm.eval_file(entry) { |
| 90 | assert false |
| 91 | } else { |
| 92 | assert err.msg().contains('source expansion limit'), err.msg() |
| 93 | } |
| 94 | assert vm.active.len == 0 |
| 95 | assert vm.eval('42') or { panic(err) }.whole() == 42 |
| 96 | } |
| 97 | |