mirror of
https://github.com/justinethier/cyclone.git
synced 2025-05-18 21:29:18 +02:00
Issue #370 - Added thread-data type specifier
This commit is contained in:
parent
0c279d80e3
commit
46ae8c1ad8
3 changed files with 19 additions and 9 deletions
|
@ -70,7 +70,9 @@ Scheme | C
|
||||||
`bignum` | `mp_int`
|
`bignum` | `mp_int`
|
||||||
`opaque` | `void *`
|
`opaque` | `void *`
|
||||||
`c-void` | `void`
|
`c-void` | `void`
|
||||||
|
`thread-data` | `gc_thread_data *`
|
||||||
|
|
||||||
Useful notes:
|
Useful notes:
|
||||||
- Use `opaque` if you need to handle any kind of C pointer.
|
- Use `opaque` if you need to handle any kind of C pointer.
|
||||||
- Use `string` to handle C `const char*` (`symbol` is strictly used to represent Scheme symbols).
|
- Use `string` to handle C `const char*` (`symbol` is strictly used to represent Scheme symbols).
|
||||||
|
- `thread-data` is a special type used to pass the current thread's `gc_thread_data` instance to a C function. Objects of this type are passed implicitly when making a Scheme function call.
|
||||||
|
|
|
@ -1,17 +1,20 @@
|
||||||
;; A basic example of subtracting 2 bignums using the FFI.
|
;; A basic example of subtracting 2 bignums using the FFI.
|
||||||
;;
|
;;
|
||||||
;; This example is notable because we need to pass the
|
;; This example also shows how to pass the current thread's
|
||||||
;; current thread's data object to C so that we can pass
|
;; data object to C using the thread-data type specifier.
|
||||||
;; it along to functions in the Cyclone runtime.
|
;; The thread data object is often needed so it can be passed
|
||||||
|
;; along to functions in the Cyclone runtime.
|
||||||
;;
|
;;
|
||||||
(import (scheme base) (scheme write) (cyclone foreign) (srfi 18))
|
(import (scheme base) (scheme write) (cyclone foreign))
|
||||||
(include-c-header "sub-bignums.h")
|
(include-c-header "sub-bignums.h")
|
||||||
|
|
||||||
(c-define sub-big-nums bignum "sub_big_nums" opaque bignum bignum)
|
;; Define a C function receiving thread data and two bignum arguments
|
||||||
|
;; Note thread data is passed implicitly, so calls to sub-big-num do
|
||||||
|
;; not need to pass the thread data argument from scheme code.
|
||||||
|
(c-define sub-big-nums bignum "sub_big_nums" thread-data bignum bignum)
|
||||||
|
|
||||||
(display
|
(display
|
||||||
(sub-big-nums
|
(sub-big-nums
|
||||||
(current-thread-data)
|
|
||||||
999999999999999999999999
|
999999999999999999999999
|
||||||
222222222222222222222222))
|
222222222222222222222222))
|
||||||
(newline)
|
(newline)
|
||||||
|
|
|
@ -119,6 +119,8 @@
|
||||||
(string-append "opaque_ptr(" ,code ")"))
|
(string-append "opaque_ptr(" ,code ")"))
|
||||||
((c-void)
|
((c-void)
|
||||||
"Cyc_VOID")
|
"Cyc_VOID")
|
||||||
|
((thread-data)
|
||||||
|
"data")
|
||||||
(else
|
(else
|
||||||
(error "scm->c unable to convert scheme object of type " ,type)))))))
|
(error "scm->c unable to convert scheme object of type " ,type)))))))
|
||||||
|
|
||||||
|
@ -275,9 +277,12 @@
|
||||||
"(void *data, int argc, closure _, object k "
|
"(void *data, int argc, closure _, object k "
|
||||||
(apply string-append
|
(apply string-append
|
||||||
(map
|
(map
|
||||||
(lambda (sym/unbox)
|
(lambda (sym/unbox type)
|
||||||
(string-append ", object " (car sym/unbox)))
|
(if (eq? type 'thread-data)
|
||||||
arg-syms/unbox))
|
""
|
||||||
|
(string-append ", object " (car sym/unbox))))
|
||||||
|
arg-syms/unbox
|
||||||
|
arg-types))
|
||||||
")"))
|
")"))
|
||||||
(type-checks
|
(type-checks
|
||||||
(apply
|
(apply
|
||||||
|
|
Loading…
Add table
Reference in a new issue