test an opaque record across a module boundary

A bodyless pub record ahead of a private definition exports the handle
and keeps the fields, the way flecs keeps ecs_world_t. It worked with
nothing added, and nothing said so.
This commit is contained in:
2026-10-08 11:09:42 +03:00
parent 6c8b38b7e2
commit e9960488fb
4 changed files with 33 additions and 3 deletions

View File

@@ -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

View File

@@ -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))

View File

@@ -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.