module scheme import os import time fn include_control_root() string { root := os.join_path(os.temp_dir(), 'vile-include-control-${os.getpid()}-${time.sys_mono_now()}') os.mkdir_all(root) or { panic(err) } return root } fn test_continuations_resume_included_forms_without_reexecuting_prior_forms() { root := include_control_root() defer { os.rmdir_all(root) or {} } os.write_file(os.join_path(root, 'body.scm'), '(set! visits (+ visits 1)) (call/cc (lambda (k) (set! saved k) 0)) (set! returns (+ returns 1)) returns') or { panic(err) } entry := os.join_path(root, 'entry.scm') os.write_file(entry, '(define visits 0) (define returns 0) (define saved #f) (include "body.scm") (list visits returns)') or { panic(err) } mut vm := new() assert write(vm.eval_file(entry) or { panic(err) }) or { panic(err) } == '(1 1)' continuation := vm.lookup('saved') or { panic(err) } code := continuation.procedure_pointer().continuation.function.code assert !isnil(code.sources) assert isnil(code.compiler_vm) && isnil(code.known_globals) && isnil(code.syntax_base) for count in 2 .. 5 { gc_collect() assert write(vm.call(continuation, [integer(0)]) or { panic(err) }) or { panic(err) } == '(1 ${count})' } } fn test_macro_generated_inclusion_preserves_local_macro_hygiene_and_lexical_variables() { root := include_control_root() defer { os.rmdir_all(root) or {} } os.write_file(os.join_path(root, 'body.scm'), '(let-syntax ((plus-x (syntax-rules () ((_ y) (+ x y))))) (plus-x 22))') or { panic(err) } entry := os.join_path(root, 'entry.scm') os.write_file(entry, '(define-syntax splice (syntax-rules () ((_ path) (include path)))) (let ((x 20)) (splice "body.scm"))') or { panic(err) } mut vm := new() assert vm.eval_file(entry) or { panic(err) }.whole() == 42 } fn test_included_forms_use_the_selected_host_namespace() { root := include_control_root() defer { os.rmdir_all(root) or {} } os.write_file(os.join_path(root, 'body.scm'), '(define answer (+ 20 22))') or { panic(err) } entry := os.join_path(root, 'entry.scm') os.write_file(entry, '(include "body.scm") answer') or { panic(err) } mut vm := new() vm.define('answer', integer(99)) or { panic(err) } space := vm.new_environment() or { panic(err) } vm.import_bindings(space, vm.interaction_environment() or { panic(err) }, { '+': '+' }) or { panic(err) } assert vm.eval_file_in(entry, space) or { panic(err) }.whole() == 42 assert vm.lookup('answer') or { panic(err) }.whole() == 99 assert vm.lookup_in(space, 'answer') or { panic(err) }.whole() == 42 } fn test_inclusion_cycle_detection_handles_symbolic_link_aliases() { $if windows { return } root := include_control_root() defer { os.rmdir_all(root) or {} } entry := os.join_path(root, 'entry.scm') os.write_file(entry, '(include "alias.scm")') or { panic(err) } os.symlink(entry, os.join_path(root, 'alias.scm')) or { panic(err) } mut vm := new() if _ := vm.eval_file(entry) { assert false } else { assert err.msg().contains('cyclic source inclusion'), err.msg() } assert vm.eval('42') or { panic(err) }.whole() == 42 } fn test_library_declaration_expansion_shares_the_calling_machine_budget() { root := include_control_root() defer { os.rmdir_all(root) or {} } for i in 0 .. 5 { os.write_file(os.join_path(root, 'part${i}.sld'), '(define-library (part${i}) (cond-expand (vile)) (cond-expand (vile)))') or { panic(err) } } entry := os.join_path(root, 'entry.scm') os.write_file(entry, '(import (part0) (part1) (part2) (part3) (part4))') or { panic(err) } mut vm := new_with_options(Options{ max_expansions: 6 }) or { panic(err) } if _ := vm.eval_file(entry) { assert false } else { assert err.msg().contains('source expansion limit'), err.msg() } assert vm.active.len == 0 assert vm.eval('42') or { panic(err) }.whole() == 42 }