module scheme import os import rand fn file_test_directory() string { path := os.join_path(os.vtmp_dir(), 'vile-file-ports-' + rand.ulid()) os.mkdir_all(path) or { panic(err) } return path } fn file_eval(mut vm VM, source string) Value { return vm.eval(source) or { panic('${source}: ${err}') } } fn file_expect_error(mut vm VM, source string) { if value := vm.eval(source) { assert false, '${source} returned ${value.kind()}' } assert vm.active.len == 0 assert isnil(vm.ports) || isnil(vm.ports.bindings) } fn test_file_ports_read_utf8_datums_and_flush_buffered_and_direct_writes() { root := file_test_directory() defer { os.rmdir_all(root) or {} } path := os.join_path(root, '位馃榾.scm') mut vm := new() out := vm.open_output_file(path) or { panic(err) } vm.define('out', out) or { panic(err) } file_eval(mut vm, r'(display "(alpha \"馃榾\")" out) (newline out) (write-char #\位 out)') assert out.output_pointer().stream.pending.len > 0 vm.close_output_port(out) or { panic(err) } assert !out.output_pointer().file.is_opened assert os.read_file(path) or { panic(err) } == '(alpha "馃榾")\n位' vm.close_output_port(out) or { panic(err) } input := vm.open_input_file(path) or { panic(err) } assert write(vm.read(input) or { panic(err) }) or { panic(err) } == '(alpha "馃榾")' assert vm.read_char(input) or { panic(err) }.as_character() or { panic(err) } == `\n` assert vm.peek_char(input) or { panic(err) }.as_character() or { panic(err) } == `位` assert vm.read_char(input) or { panic(err) }.as_character() or { panic(err) } == `位` assert vm.read(input) or { panic(err) }.tag == .eof vm.close_input_port(input) or { panic(err) } assert !input.input_pointer().file.is_opened assert input.input_pointer().buffer.len == 0 for length in [1, 4095, 4096, 4097, 65537] { mut sink := vm.open_output_file(path) or { panic(err) }.output_pointer() sink.write_text('prefix') or { panic(err) } sink.write_text('x'.repeat(length)) or { panic(err) } sink.write_text('suffix') or { panic(err) } sink.stream.close() or { panic(err) } assert os.read_file(path) or { panic(err) } == 'prefix' + 'x'.repeat(length) + 'suffix' } } fn test_file_controller_returns_multiple_values_and_closes_escaped_ports() { root := file_test_directory() defer { os.rmdir_all(root) or {} } mut vm := new() vm.define('path', string_value(os.join_path(root, 'data'))) or { panic(err) } assert file_eval(mut vm, '(define held #f) (call-with-values (lambda () (call-with-output-file path (lambda (p) (set! held p) (display "hello" p) (values 20 22)))) +)').as_integer() or { panic(err) } == 42 held := vm.lookup('held') or { panic(err) } assert held.output_pointer().closed assert !held.output_pointer().file.is_opened assert os.read_file(os.join_path(root, 'data')) or { panic(err) } == 'hello' assert file_eval(mut vm, '(call-with-values (lambda () (call-with-input-file path (lambda (p) (set! held p) (values)))) (lambda () 99))').as_integer() or { panic(err) } == 99 assert vm.lookup('held') or { panic(err) }.input_pointer().closed assert isnil(vm.ports.bindings) file_expect_error(mut vm, '(read-char held)') file_expect_error(mut vm, '(write 1 held)') } fn test_file_selection_restores_nested_ports_on_normal_return_and_ordinary_error() { root := file_test_directory() defer { os.rmdir_all(root) or {} } mut vm := new() for name in ['first', 'second'] { vm.define(name, string_value(os.join_path(root, name))) or { panic(err) } } prior := vm.current_output_port() or { panic(err) } result := file_eval(mut vm, '(define outer #f) (define inner #f) (with-output-to-file first (lambda () (set! outer (current-output-port)) (display "a") (with-output-to-file second (lambda () (set! inner (current-output-port)) (display "b"))) (display "c") 42))') assert result.as_integer() or { panic(err) } == 42 assert eqv(vm.current_output_port() or { panic(err) }, prior) assert os.read_file(os.join_path(root, 'first')) or { panic(err) } == 'ac' assert os.read_file(os.join_path(root, 'second')) or { panic(err) } == 'b' for name in ['outer', 'inner'] { assert vm.lookup(name) or { panic(err) }.output_pointer().closed } file_expect_error(mut vm, '(with-output-to-file first (lambda () (set! outer (current-output-port)) (display "x") (with-output-to-file second (lambda () (set! inner (current-output-port)) (display "y") (car 3)))))') assert eqv(vm.current_output_port() or { panic(err) }, prior) assert os.read_file(os.join_path(root, 'first')) or { panic(err) } == 'x' assert os.read_file(os.join_path(root, 'second')) or { panic(err) } == 'y' for name in ['outer', 'inner'] { assert vm.lookup(name) or { panic(err) }.output_pointer().closed } os.write_file(os.join_path(root, 'first'), '20 22') or { panic(err) } input_prior := vm.current_input_port() or { panic(err) } assert file_eval(mut vm, '(with-input-from-file first (lambda () (+ (read) (read))))').as_integer() or { panic(err) } == 42 assert eqv(vm.current_input_port() or { panic(err) }, input_prior) } fn test_file_continuation_escape_closes_and_reentry_restores_the_new_enclosing_port() { root := file_test_directory() defer { os.rmdir_all(root) or {} } mut vm := new() vm.define('path', string_value(os.join_path(root, 'data'))) or { panic(err) } prior := vm.current_output_port() or { panic(err) } assert file_eval(mut vm, '(define saved #f) (define held #f) (call/cc (lambda (exit) (with-output-to-file path (lambda () (set! held (current-output-port)) (display "a") (let ((n (call/cc (lambda (k) (set! saved k) 0)))) (exit (+ n 10)))))))').as_integer() or { panic(err) } == 10 assert eqv(vm.current_output_port() or { panic(err) }, prior) assert vm.lookup('held') or { panic(err) }.output_pointer().closed alternate := vm.open_output_file(os.join_path(root, 'alternate')) or { panic(err) } vm.set_output_port(alternate) or { panic(err) } for n in 1 .. 5 { gc_collect() assert vm.call(vm.lookup('saved') or { panic(err) }, [integer(n)]) or { panic(err) }.as_integer() or { panic(err) } == n + 10 assert eqv(vm.current_output_port() or { panic(err) }, alternate) assert !alternate.output_pointer().closed assert isnil(vm.ports.bindings) } assert os.read_file(os.join_path(root, 'data')) or { panic(err) } == 'a' vm.close_output_port(alternate) or { panic(err) } } fn file_nested_failure(mut vm VM, args []Value) !Value { check_arity('nested-failure', args.len, 0, 0)! outer := vm.current_output_port()! if _ := vm.eval('(with-output-to-file second (lambda () (display "inner") (car 3)))') { return failure('expected inner failure') } if !eqv(vm.current_output_port()!, outer) { return failure('inner cleanup changed outer port') } if outer.output_pointer().closed { return failure('inner cleanup closed outer port') } return vm.eval('(display "after") 42') } fn test_nested_host_entry_failure_cleans_only_its_own_file_extents() { root := file_test_directory() defer { os.rmdir_all(root) or {} } mut vm := new() for name in ['first', 'second'] { vm.define(name, string_value(os.join_path(root, name))) or { panic(err) } } vm.register('nested-failure', file_nested_failure) or { panic(err) } prior := vm.current_output_port() or { panic(err) } assert file_eval(mut vm, '(with-output-to-file first (lambda () (display "before") (nested-failure)))').as_integer() or { panic(err) } == 42 assert os.read_file(os.join_path(root, 'first')) or { panic(err) } == 'beforeafter' assert os.read_file(os.join_path(root, 'second')) or { panic(err) } == 'inner' assert eqv(vm.current_output_port() or { panic(err) }, prior) assert isnil(vm.ports.bindings) } fn test_file_open_errors_and_close_failure_do_not_leak_descriptors_or_selection() { root := file_test_directory() defer { os.rmdir_all(root) or {} } mut vm := new() vm.define('path', string_value(os.join_path(root, 'missing', 'data'))) or { panic(err) } for source in ['(open-input-file path)', '(open-output-file path)', '(call-with-input-file path (lambda (p) 1))', '(with-output-to-file path (lambda () 1))', '(open-input-file 1)', '(open-output-file)', '(call-with-output-file path 1)', '(with-input-from-file path 1)', '(call-with-input-file path)', '(with-output-to-file)'] { file_expect_error(mut vm, source) } vm.define('path', string_value(os.join_path(root, 'data') + '\x00suffix')) or { panic(err) } file_expect_error(mut vm, '(open-output-file path)') assert !os.exists(os.join_path(root, 'data')) $if linux { if os.exists('/dev/full') { prior := vm.current_output_port() or { panic(err) } file_expect_error(mut vm, '(define held #f) (with-output-to-file "/dev/full" (lambda () (set! held (current-output-port)) (display "cannot flush")))') assert eqv(vm.current_output_port() or { panic(err) }, prior) held := vm.lookup('held') or { panic(err) } assert held.output_pointer().closed assert !held.output_pointer().file.is_opened vm.close_output_port(held) or { panic(err) } } } } fn test_file_finalizer_callback_flushes_once_and_ignores_repeated_close() { root := file_test_directory() defer { os.rmdir_all(root) or {} } mut vm := new() path := os.join_path(root, 'data') out := vm.open_output_file(path) or { panic(err) } mut port := out.output_pointer() port.write_text('pending') or { panic(err) } finalize_port_file(port.stream, unsafe { nil }) assert !port.stream.file.is_opened assert os.read_file(path) or { panic(err) } == 'pending' // Reuse the path with a new descriptor; stale finalization cannot close it. other := vm.open_output_file(path) or { panic(err) } finalize_port_file(port.stream, unsafe { nil }) assert other.output_pointer().file.is_opened vm.close_output_port(out) or { panic(err) } vm.close_output_port(other) or { panic(err) } } fn test_call_with_file_keeps_escaped_port_open_until_callback_really_returns() { root := file_test_directory() defer { os.rmdir_all(root) or {} } mut vm := new() path := os.join_path(root, 'data') vm.define('path', string_value(path)) or { panic(err) } assert file_eval(mut vm, '(define held #f) (define saved #f) (call/cc (lambda (exit) (call-with-output-file path (lambda (p) (set! held p) (display "a" p) (call/cc (lambda (k) (set! saved k) (exit 10))) (display "b" p) 42))))').as_integer() or { panic(err) } == 10 held := vm.lookup('held') or { panic(err) } assert !held.output_pointer().closed assert held.output_pointer().file.is_opened assert isnil(vm.ports.bindings) gc_collect() assert vm.call(vm.lookup('saved') or { panic(err) }, [integer(1)]) or { panic(err) }.as_integer() or { panic(err) } == 42 assert held.output_pointer().closed assert !held.output_pointer().file.is_opened assert os.read_file(path) or { panic(err) } == 'ab' // The actual callback already returned, so subsequent reentry may observe // a closed port. It must fail cleanly and leave the VM's bindings restored. file_expect_error(mut vm, '(saved 2)') os.write_file(path, '20 22') or { panic(err) } assert file_eval(mut vm, '(call/cc (lambda (exit) (call-with-input-file path (lambda (p) (set! held p) (let ((first (read p))) (call/cc (lambda (k) (set! saved k) (exit first))) (+ first (read p)))))))').as_integer() or { panic(err) } == 20 input := vm.lookup('held') or { panic(err) } assert !input.input_pointer().closed gc_collect() assert vm.call(vm.lookup('saved') or { panic(err) }, [integer(0)]) or { panic(err) }.as_integer() or { panic(err) } == 42 assert input.input_pointer().closed assert isnil(vm.ports.bindings) }