forked from alex-eg/sex
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:
@@ -52,7 +52,10 @@
|
|||||||
(test-assert "struct keeps C's meaning"
|
(test-assert "struct keeps C's meaning"
|
||||||
(not (emits? "(struct raw ((x int)))" "typedef")))
|
(not (emits? "(struct raw ((x int)))" "typedef")))
|
||||||
(test-assert "a record needs a name"
|
(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'
|
;; An ops block is mangled functions plus a registry entry. `pub'
|
||||||
;; inside it means what it means outside.
|
;; inside it means what it means outside.
|
||||||
|
|||||||
@@ -13,14 +13,15 @@
|
|||||||
# reads the imported struct's fields; and that a closure type crossing
|
# reads the imported struct's fields; and that a closure type crossing
|
||||||
# the boundary works both ways -- one built in the module and called
|
# 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
|
# 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
|
# The public forms carry comments in their headers, which the reduction
|
||||||
# to a prototype and to an extern both have to look past.
|
# to a prototype and to an extern both have to look past.
|
||||||
|
|
||||||
SEXC ?= ../../sexc
|
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:
|
check:
|
||||||
@$(SEXC) greet.sex -c -o greet.o
|
@$(SEXC) greet.sex -c -o greet.o
|
||||||
|
|||||||
@@ -40,6 +40,13 @@
|
|||||||
|
|
||||||
(.show-line v)
|
(.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))
|
(var c colour (colour/rgb 1 2 3))
|
||||||
(match c
|
(match c
|
||||||
((rgb (r g b)) (printf "colour %d%d%d\n" r g b))
|
((rgb (r g b)) (printf "colour %d%d%d\n" r g b))
|
||||||
|
|||||||
@@ -6,6 +6,7 @@
|
|||||||
;;; greet-app.sex.
|
;;; greet-app.sex.
|
||||||
|
|
||||||
(pub include stdio.h)
|
(pub include stdio.h)
|
||||||
|
(include stdlib.h)
|
||||||
|
|
||||||
(pub var greet-count ;; a comment in the header of a public form is
|
(pub var greet-count ;; a comment in the header of a public form is
|
||||||
;; not part of it: what the importer is given has
|
;; not part of it: what the importer is given has
|
||||||
@@ -60,6 +61,24 @@
|
|||||||
(pub fn manhattan ((self (* const vec2))) int
|
(pub fn manhattan ((self (* const vec2))) int
|
||||||
(return (+ (. self x) (. self y)))))
|
(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
|
;;; 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
|
;;; the declarations -- and of the constructors, which is why those are
|
||||||
;;; static: two units defining colourːrgb would not link.
|
;;; static: two units defining colourːrgb would not link.
|
||||||
|
|||||||
Reference in New Issue
Block a user