I'm playing with idea of loading С/Rust/etc functions via FFI in Racket. I'd like to specify list of function names as strings and then just load them by some helper function. Main problem is creating identifier/word from a string. For example, it is very simple in Rebol:
foo: "test1"
set to-word (rejoin [foo "_result_data"]) some
print test1_result_data
but in Racket I have to use syntax stuff. So I've found examples like How do I define functions using Racket macros? and Racket Macro to auto-define functions given a list. They cover a lot knowledge I need so I've written next snippet:
#lang racket
(require (for-syntax racket/syntax 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 (define-ffi-func stx)
(syntax-case stx ()
[(_)
(let ([elem->fn-id
(λ (elem-str)
(format-id
stx "~a"
(datum->syntax stx (string->symbol elem-str))))]
[rustlib
(ffi-lib "/path/to/libffitest.dylib")
]
)
(with-syntax
([((method cation) ...)
(map
(λ (elem)
; I can load C code here for sure :) But not much more
; (define c (get-ffi-obj elem rustlib (_fun -> _int)
; (lambda ()
; (error 'rustlib
; "installed lib does not provide given method"))))
(list (elem->fn-id elem) elem)
)
*method-names*)])
#`(begin
(define (method)
; I'm curious what should be here
1
)
...)))]))
(define-ffi-func)
; Actually one, two and hello methods are in scope
; but surely they all return 1
(two)
But still I can't bond new Racket methods with ffi calls. I guess I can't match them in with-syntax
body but further syntax definition knows nothing about external modules (e.g.
#`(begin
(define (ate)
(get-ffi-obj elem rustlib (_fun -> _int))
)
...)))]))
will not work.
I think I'm missing something about binding match too.
How can I get FFI-bonded methods by specifying a list of names? Thanks in advance!