Skip to content

Commit da522dc

Browse files
committed
ffi macro cleanup
unified interface for subsystem macros
1 parent 638fd2c commit da522dc

3 files changed

Lines changed: 31 additions & 24 deletions

File tree

ext/ffi/ffi.scm

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -143,15 +143,20 @@
143143
:c-name ,(cgen-safe-name-friendly (x->string name))
144144
:arg-types (map %resolve-typespec ,arg-types-expr)
145145
:return-type (%resolve-typespec ,rettype-expr)))]))
146+
;; cfn-specs is ((name . cfn-expr) ...), where name is a symbol
147+
;; name of cfn, and cfn-expr is (make <foreivn-c-function> ...)
148+
;; constructed above. The subsystem macro should rearrange
149+
;; cfn-specs so that cfn-expr is evaluated in proper context.
146150
(let* ([ordered-cfns (reverse cfns)]
147-
[cfn-names (map cadr ordered-cfns)]
148-
[cfn-list-expr (quasirename r
149-
`(list ,@(map make-cfn-expr ordered-cfns)))])
151+
[cfn-specs (map (^[cfn]
152+
(cons (cadr cfn) ; name
153+
(make-cfn-expr cfn))) ;expr
154+
ordered-cfns)])
150155
(ecase subsystem
151156
[(:stubgen)
152157
(quasirename r
153-
`(with-stubgen-ffi ,dlo-expr ,options ,cfn-list-expr ,forms))]
158+
`(with-stubgen-ffi ,dlo-expr ,options ,cfn-specs ,forms))]
154159
[(:native)
155160
(quasirename r
156-
`(with-native-ffi ,dlo-expr ,options ,cfn-list-expr ,cfn-names ,forms))]
161+
`(with-native-ffi ,dlo-expr ,options ,cfn-specs ,forms))]
157162
))]))))

ext/ffi/ffi/native.scm

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -57,22 +57,21 @@
5757
(er-macro-transformer
5858
(^[f r c]
5959
(match f
60-
[(_ dlo-expr options cfn-list-expr cfn-names forms)
60+
[(_ dlo-expr options cfn-specs forms)
6161
(quasirename r
6262
`(begin
63-
,@(map (^[name] (quasirename r
64-
`(define ,name)))
65-
cfn-names)
63+
,@(map (^[spec] (quasirename r
64+
`(define ,(car spec))))
65+
cfn-specs)
6666
(define _dummy
67-
(let ([dlo ,dlo-expr]
68-
[cfns ,cfn-list-expr])
69-
,@(map (^[i name]
67+
(let ([dlo ,dlo-expr])
68+
,@(map (^[spec]
7069
(quasirename r
71-
`(set! ,name
72-
(make-native-ffi-proc dlo (list-ref cfns ,i)))))
73-
(iota (length cfn-names))
74-
cfn-names)))
70+
`(set! ,(car spec)
71+
(make-native-ffi-proc dlo ,(cdr spec)))))
72+
cfn-specs)))
7573
,@forms))]))))
74+
7675
;;;
7776
;;; Type canonicalization
7877
;;;

ext/ffi/ffi/stubgen.scm

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -49,14 +49,17 @@
4949
(er-macro-transformer
5050
(^[f r c]
5151
(match f
52-
[(_ dlo-expr options cfn-list-expr body)
53-
(quasirename r
54-
`(begin
55-
(define _dummy
56-
(compile-and-link-ffi-stub ,dlo-expr
57-
,cfn-list-expr
58-
(current-module)))
59-
,@body))]))))
52+
[(_ dlo-expr options cfn-specs body)
53+
(let1 cfn-list-expr
54+
(quasirename r
55+
`(list ,@(map cdr cfn-specs)))
56+
(quasirename r
57+
`(begin
58+
(define _dummy
59+
(compile-and-link-ffi-stub ,dlo-expr
60+
,cfn-list-expr
61+
(current-module)))
62+
,@body)))]))))
6063

6164
(define (compile-and-link-ffi-stub dlobj cfn-instances mod)
6265
(let ([unit (generate-ffi-c-code-unit cfn-instances)])

0 commit comments

Comments
 (0)