forked from alex-eg/sex
Reading a module's interface dropped any toplevel call that raised, not only one asking after a declaration of the module's, which no importer has yet. Lookups now tell which names they missed, and only those failures give importers nothing.
173 lines
7.2 KiB
Scheme
173 lines
7.2 KiB
Scheme
;;; Multi-module linking.
|
|
;;;
|
|
;;; Modules are only testable end to end, and nothing else in the suite
|
|
;;; links more than one translation unit. Three things have to hold at
|
|
;;; once: an imported `pub fn' comes out as a prototype with external
|
|
;;; linkage, an imported `pub var' as an extern declaration, and the
|
|
;;; module's object survives being passed after `--'. Get any of them
|
|
;;; wrong and this fails to link -- or, in the `pub var' case, links and
|
|
;;; quietly counts into a private copy.
|
|
;;;
|
|
;;; It also checks what only a second translation unit can check: that an
|
|
;;; imported type reaches the type database, by expanding a macro that
|
|
;;; reads the imported struct's fields; and that a closure type crossing
|
|
;;; the boundary works both ways -- one built in the module and called
|
|
;;; here, one built here and called there, through a code pointer that is
|
|
;;; static in the other object. And that a record can cross as a handle
|
|
;;; alone, its fields left behind and its methods still found.
|
|
;;;
|
|
;;; The public forms carry comments in their headers, which the reduction
|
|
;;; to a prototype and to an extern both have to look past. And a type the
|
|
;;; app cannot see -- private to greet, or from a module greet imports
|
|
;;; without `pub' -- crosses behind a pointer, declared and no more.
|
|
;;;
|
|
;;; The interface prints as importers get it: the declarations first, then
|
|
;;; the pub forms in the order they were written.
|
|
;;;
|
|
;;; The app's dependency rule names both modules: greet, which it imports,
|
|
;;; and ledger, which only greet does but which decides what the app is
|
|
;;; told of it.
|
|
;;;
|
|
;;; A module in a directory of its own finds its imports beside itself,
|
|
;;; not beside the unit importing it: shelf-app imports shelf/book, and
|
|
;;; book its siblings page, with `pub', and spine, without. A pub macro
|
|
;;; of page's expands into a private one, which crosses with it.
|
|
;;;
|
|
;;; Two modules may import each other with `pub': cycle/a and cycle/b,
|
|
;;; whose interfaces each import the other back.
|
|
;;;
|
|
;;; gen/made writes its pub forms by macro calls, which are expanded when
|
|
;;; its interface is read; the macros it used for that stay its own.
|
|
;;; gen/broken's call raises, which reading its interface reports.
|
|
;;;
|
|
;;; make check-modules
|
|
|
|
(import scheme
|
|
(chicken base)
|
|
(chicken file)
|
|
(chicken io)
|
|
(chicken irregex)
|
|
(chicken pathname)
|
|
(chicken process)
|
|
(chicken process-context)
|
|
srfi-1
|
|
test)
|
|
|
|
(define sexc (or (get-environment-variable "SEXC") "../../sexc"))
|
|
|
|
;;; Where the objects and the app go: not beside the sources, where what
|
|
;;; an earlier run left would pass for what this one did not build
|
|
(define scratch (create-temporary-directory))
|
|
(define (built name) (make-pathname scratch name))
|
|
|
|
;;; What PROGRAM run with ARGS writes to stderr when it fails, or #f
|
|
(define (error-of program . args)
|
|
(let* ((proc (process* program args))
|
|
(err (begin (read-string #f (process-output-port proc))
|
|
(read-string #f (process-error-port proc)))))
|
|
(process-wait proc)
|
|
(and (not (zero? (process-exit-status proc))) (string? err) err)))
|
|
|
|
;;; The lines PROGRAM prints when run with ARGS, or #f when it fails
|
|
(define (output-of program . args)
|
|
(let* ((proc (process program args))
|
|
(lines (read-lines (process-output-port proc))))
|
|
(process-wait proc)
|
|
(and (process-returned-normally? proc)
|
|
(zero? (process-exit-status proc))
|
|
lines)))
|
|
|
|
(test "the interface begins with the declarations"
|
|
'("(record cell)" "(record ledger)" "(include stdio.h)")
|
|
(take (or (output-of sexc "--public-interface" "greet.sex") '()) 3))
|
|
|
|
(test-assert "the app links with both modules"
|
|
(and (output-of sexc "ledger.sex" "-c" "-o" (built "ledger.o"))
|
|
(output-of sexc "greet.sex" "-c" "-o" (built "greet.o"))
|
|
(output-of sexc "greet-app.sex" "-o" (built "greet-app")
|
|
(string-append "--depfile=" (built "greet-app.d"))
|
|
"--" (built "greet.o") (built "ledger.o"))))
|
|
|
|
(test-assert "the dependency rule names both modules"
|
|
(irregex-match ".*/greet-app: greet-app\\.sex .*/greet\\.sex .*/ledger\\.sex"
|
|
(call-with-input-file (built "greet-app.d") read-line)))
|
|
|
|
;;; Each module has a rule of its own, so make takes one deleted since as
|
|
;;; out of date rather than as a file it has no rule to make
|
|
(test "and gives each a rule of its own"
|
|
'("greet.sex:" "ledger.sex:")
|
|
(filter-map (lambda (line)
|
|
(and (irregex-match ".*\\.sex:" line) (pathname-strip-directory line)))
|
|
(call-with-input-file (built "greet-app.d") read-lines)))
|
|
|
|
(test "a module's interface does not depend on where sexc runs"
|
|
'("(record spine)" "(import page)")
|
|
(take (or (output-of sexc "--public-interface" "shelf/book.sex") '()) 2))
|
|
|
|
(test-assert "a module in a directory finds its imports beside itself"
|
|
(and (output-of sexc "shelf/page.sex" "-c" "-o" (built "page.o"))
|
|
(output-of sexc "shelf/book.sex" "-c" "-o" (built "book.o"))
|
|
(output-of sexc "shelf-app.sex" "-o" (built "shelf-app")
|
|
"--" (built "book.o") (built "page.o"))))
|
|
|
|
(test "and the app runs through it"
|
|
'("pages 2 3" "number 2")
|
|
(output-of (built "shelf-app")))
|
|
|
|
(test-assert "modules importing each other with pub compile"
|
|
(and (output-of sexc "cycle/a.sex" "-c" "-o" (built "cycle-a.o"))
|
|
(output-of sexc "cycle/b.sex" "-c" "-o" (built "cycle-b.o"))
|
|
(output-of sexc "cycle-app.sex" "-o" (built "cycle-app")
|
|
"--" (built "cycle-a.o") (built "cycle-b.o"))))
|
|
|
|
(test "and the app runs through them"
|
|
'("cycle 1 2")
|
|
(output-of (built "cycle-app")))
|
|
|
|
;;; ...it lists pub forms a macro wrote, and not the macros that wrote them
|
|
(test "a module's interface holds what its macros wrote"
|
|
'("(pub fn forty-two () int)" "(pub fn seven () int)"
|
|
"(pub fn one () int)" "(pub fn two () int)")
|
|
(output-of sexc "--public-interface" "gen/made.sex"))
|
|
|
|
(test-assert "and an importer links with them"
|
|
(and (output-of sexc "gen/made.sex" "-c" "-o" (built "made.o"))
|
|
(output-of sexc "made-app.sex" "-o" (built "made-app")
|
|
"--" (built "made.o"))))
|
|
|
|
(test "and calls them"
|
|
'("made 42 7 1 2")
|
|
(output-of (built "made-app")))
|
|
|
|
;;; ...but a call failing for other than a name its module declares fails
|
|
;;; there as well
|
|
(test-assert "a toplevel macro call that raises fails the interface too"
|
|
(let ((err (error-of sexc "--public-interface" "gen/broken.sex")))
|
|
(and err (irregex-search "broken\\.sex:8: expanding boom: nope" err))))
|
|
|
|
;;; ...while a realization of a spec importers cannot see is refused by
|
|
;;; the module that writes it, not by an importer naming it first
|
|
(test-assert "a pub realization of a spec imported without pub is refused"
|
|
(let ((err (error-of sexc "specs/money.sex" "-C" "-o" "-")))
|
|
(and err (irregex-search "money.sex:8: importers cannot see this spec" err) #t)))
|
|
|
|
(test "the app runs through both"
|
|
'("hello, world"
|
|
"hello, sex"
|
|
"2 greetings"
|
|
"text times "
|
|
"text times "
|
|
"mood 1"
|
|
"closure 15 21 201"
|
|
"vec2 7 7"
|
|
"vec2(3 4)"
|
|
"tally 2"
|
|
"board 42"
|
|
"ledger 2"
|
|
"colour 123")
|
|
(output-of (built "greet-app")))
|
|
|
|
(delete-directory scratch #t)
|
|
|
|
(test-exit)
|