Skip to content

Commit c5dc312

Browse files
committed
Adding empty struct support
1 parent 1ca943a commit c5dc312

File tree

7 files changed

+27
-8
lines changed

7 files changed

+27
-8
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ PFFI is a portable foreign function interface for R6RS Scheme implementations.
2020
- `define-type-alias` is introduced, similar usage as `typedef` in C.
2121
- `boolean` support for Scheme boolean.
2222
- Supporting array foreign variable.
23+
- Empty struct, i.e. `(define-foreign-struct foo)`, is supported
2324

2425
## Example
2526

src/pffi/compat.mzscheme.sls

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -204,10 +204,15 @@
204204

205205
(define-syntax callback
206206
(syntax-rules ()
207+
((_ (conv ...) ret (args ...))
208+
(_cprocedure
209+
(->immutable-list (list (ffi-type-descriptor-alias args) ...))
210+
(ffi-type-descriptor-alias ret)))
207211
((_ ret (args ...))
208212
(_cprocedure
209213
(->immutable-list (list (ffi-type-descriptor-alias args) ...))
210214
(ffi-type-descriptor-alias ret)))))
215+
211216
(define-ftype void _void)
212217

213218
(define (open-shared-object path)

src/pffi/helper.chezscheme.sls

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,10 +45,16 @@
4545
(lambda (x)
4646
(define (register-type-alias! name alias)
4747
(hashtable-set! *typedef-table* name alias))
48-
(syntax-case x (*)
48+
(syntax-case x (* callback)
4949
((_ name (* alias))
5050
(register-type-alias! (syntax->datum #'name) 'void*)
5151
#'(define-ftype name void*))
52+
((_ name (callback (conv ...) ret (args ...)))
53+
(register-type-alias! (syntax->datum #'name) (syntax->datum 'void*))
54+
#'(define-ftype name void*))
55+
((_ name (callback ret (args ...)))
56+
(register-type-alias! (syntax->datum #'name) 'void*)
57+
#'(define-ftype name void*))
5258
((_ name alias)
5359
(register-type-alias! (syntax->datum #'name) (syntax->datum #'alias))
5460
#'(define-ftype name alias)))))

src/pffi/misc.sls

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,10 +34,10 @@
3434
(import (only (rnrs) define-syntax syntax-rules define *)
3535
(only (srfi :13) string-map)
3636
(only (srfi :1) take drop drop-right split-at)
37-
(only (pffi compat) pointer))
37+
(only (pffi compat) pointer callback))
3838

3939
(define-syntax define-type-alias
40-
(syntax-rules (*)
40+
(syntax-rules (* callback)
4141
((_ name (* alias)) (define name pointer))
4242
((_ name alias) (define name alias))))
4343

src/pffi/struct.sls

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -193,10 +193,12 @@
193193
t))
194194

195195
(define (struct-alignment lis)
196-
(apply max (map (lambda (l)
197-
(if (foreign-struct-descriptor? l)
198-
(generic-foreign-struct-descriptor-alignment l)
199-
(type-descriptor-size l))) lis)))
196+
(if (null? lis)
197+
0
198+
(apply max (map (lambda (l)
199+
(if (foreign-struct-descriptor? l)
200+
(generic-foreign-struct-descriptor-alignment l)
201+
(type-descriptor-size l))) lis))))
200202

201203
;; ref
202204
;; http://en.wikipedia.org/wiki/Data_structure_alignment

src/pffi/struct/helper.sls

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@
6666
x (car fields))))))
6767
(let loop ((clauses clauses) (fs #f) (par #f) (proto #f) (align #f))
6868
(syntax-case clauses (fields parent protocol alginment)
69-
(() (list fs par proto align))
69+
(() (list (or fs '()) par proto align))
7070
(((fields defs (... ...)) . rest)
7171
(or (not fs)
7272
(syntax-violation who "only one fields clause allowed" x

tests/test.scm

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -382,4 +382,9 @@
382382
(test-equal "count p-st" 10 (st-parent-count st))))
383383

384384

385+
;; empty struct
386+
(let ()
387+
(define-foreign-struct ok)
388+
(test-equal "size of empty struct" 0 size-of-ok))
389+
385390
(test-end)

0 commit comments

Comments
 (0)