diff --git a/tests/encapsulation.scm b/tests/encapsulation.scm index 8bf0285..9ecef47 100644 --- a/tests/encapsulation.scm +++ b/tests/encapsulation.scm @@ -52,7 +52,10 @@ (test-assert "struct keeps C's meaning" (not (emits? "(struct raw ((x int)))" "typedef"))) (test-assert "a record needs a name" - (reports? "(record ((x int)))" "record needs a name"))) + (reports? "(record ((x int)))" "record needs a name")) + ;; An opaque handle: the declaration is public, the fields are not + (test-assert "declared, then defined, is still defined" + (emits? "(pub record h)(record h ((x int)))" "struct h {"))) ;; An ops block is mangled functions plus a registry entry. `pub' ;; inside it means what it means outside. diff --git a/tests/modules/Makefile b/tests/modules/Makefile index af3442a..034cd95 100644 --- a/tests/modules/Makefile +++ b/tests/modules/Makefile @@ -13,14 +13,15 @@ # 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. +# 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. SEXC ?= ../../sexc -EXPECTED = hello, world\nhello, sex\n2 greetings\ntext times \ntext times \nmood 1\nclosure 15 21 201\nvec2 7 7\nvec2(3 4)\ncolour 123 +EXPECTED = hello, world\nhello, sex\n2 greetings\ntext times \ntext times \nmood 1\nclosure 15 21 201\nvec2 7 7\nvec2(3 4)\ntally 2\ncolour 123 check: @$(SEXC) greet.sex -c -o greet.o diff --git a/tests/modules/greet-app.sex b/tests/modules/greet-app.sex index e064bab..7675207 100644 --- a/tests/modules/greet-app.sex +++ b/tests/modules/greet-app.sex @@ -40,6 +40,13 @@ (.show-line v) + ;; only the handle crossed: (. t n) here is an incomplete type + (var t (* tally) (tally/make)) + (.bump t) + (.bump t) + (printf "tally %d\n" (.count t)) + (.drop t) + (var c colour (colour/rgb 1 2 3)) (match c ((rgb (r g b)) (printf "colour %d%d%d\n" r g b)) diff --git a/tests/modules/greet.sex b/tests/modules/greet.sex index a6e6660..f309a19 100644 --- a/tests/modules/greet.sex +++ b/tests/modules/greet.sex @@ -6,6 +6,7 @@ ;;; greet-app.sex. (pub include stdio.h) +(include stdlib.h) (pub var greet-count ;; a comment in the header of a public form is ;; not part of it: what the importer is given has @@ -60,6 +61,24 @@ (pub fn manhattan ((self (* const vec2))) int (return (+ (. self x) (. self y))))) +;;; An opaque record: the handle is public and the fields are not, so +;;; the importer holds a (* tally) and reaches it only through methods. +(pub record tally) + +(record tally ((n int))) + +(ops tally + (pub fn make () (* tally) + (var t (* tally) (malloc (sizeof tally))) + (= (. t n) 0) + (return t)) + (pub fn bump ((self (* tally))) void + (++ (. self n))) + (pub fn count ((self (* const tally))) int + (return (. self n))) + (pub fn drop ((self (* tally))) void + (free self))) + ;;; A pub sum is pasted whole, so the importer builds its own copy of ;;; the declarations -- and of the constructors, which is why those are ;;; static: two units defining colourːrgb would not link.