JanetDocsPlaygroundI'm feeling luckyGitHub sign in

ffi/trampoline



    cfunction
    src/core/ffi.c on line 1408, column 1

    (ffi/trampoline cc)

    Get a native function pointer that can be used as a callback and 
    passed to C libraries. This callback trampoline has the signature 
    `void trampoline(void \*ctx, void \*userdata)` in the given calling 
    convention. This is the only function signature supported. It is up 
    to the programmer to ensure that the `userdata` argument contains a 
    janet function the will be called with one argument, `ctx` which is 
    an opaque pointer. This pointer can be further inspected with 
    `ffi/read`.


1 exampleSign in to add an example
Loading...
trampoline should work at some callback method which will match these code. most provide callback should be like these! 
```c
// liba.so
void call_mul_at_callback(int i, void (*on_complete) (void*,void*), void* user_data) {
  // user_data = ctx
  // work_code
  int v = i * i + i* i * i << 12 - 5 *i;
  printf("from janet ffi\n");
  on_complete(&i, user_data);
  printf("FFI CALLBACK COMPLETE\n");
}
```

```janet
(ffi/context "liba.so")
(ffi/defbind call-mul-at-callback :void (i :int callback :ptr data :ptr))
(def cb (delay (ffi/trampoline :default)))
(call-mul-at-callback 15
                      (cb)
                      (fn[ptr]
                         (let [args (ffi/read (ffi/struct :int) ptr)]
                           (print (string/format "got value %d from ffi"
                                                 (first args))))))
```
dG94CgPlayground