- 01
- 02
- 03
- 04
- 05
- 06
- 07
- 08
- 09
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
- 50
- 51
- 52
- 53
- 54
- 55
- 56
- 57
- 58
#lang racket
(require (for-syntax racket/syntax syntax/to-string))
(require ffi/unsafe)
(define-for-syntax *method-names*
; Given I have hello, one and two methods in my shared lib
(list "hello"
"one"
"two"
))
(define-syntax (load-ffi-functions stx)
(syntax-case stx ()
[(_ name lib ffi-func)
(let ([elem->fn-id
(λ (elem-str)
(display elem-str)
(format-id
stx "~a"
(datum->syntax stx (string->symbol elem-str))))]
)
(with-syntax ([(_)
(elem->fn-id "one")])
#`(begin
(define (name)
(printf (string->symbol name))
(ffi-func name lib (_fun -> _int))
)
)
)
)]))
(define rustlib (ffi-lib "./libffitest.dylib"))
(define-syntax (define-ffi-func stx)
(syntax-case stx ()
[(_ lib ffi-func)
(let ([elem->fn-id
(λ (elem-str)
(format-id
stx "~a"
(datum->syntax stx (string->symbol elem-str))))]
)
(with-syntax
([((method name) ...)
(map
(λ (elem)
(list (elem->fn-id elem) elem)
)
*method-names*)])
#`(begin
(define method
(ffi-func name lib (_fun -> _int))
)
...)))]))
(define-ffi-func rustlib get-ffi-obj)
(+ (one) (two) (one))
Когда мне показали, как это правильно сделать, я немного ох#ел.
Desktop 17.07.2018 23:41 # 0
kamanalnadzor 17.07.2018 23:49 # −3
Desktop 17.07.2018 23:55 # +1
guest8 19.07.2018 02:06 # −999