@@ -135,6 +135,29 @@ Turns a userdata into a string.
135135#### Returns
136136A string.
137137
138+ ---
139+ ### ` ffi.callback `
140+ Creates a function pointer callable from native code. This function is only available on x86, x86_64, and aarch64.
141+ #### Parameters
142+ 1 . The return type. Can be "void", "i8", "i16", "i32", "i64", "u8", "u16", "u32", "u64", "f32", "f64", "ptr" or "str".
143+ 2 . The argument type(s).
144+ 3 . The Pluto function to execute.
145+ #### Returns
146+ An FFI Callback instance.
147+ #### Example
148+ ``` pluto norun
149+ local ffi = require "pluto:ffi"
150+ local cb = ffi.callback("i32", "i32", function(val)
151+ return val + 69
152+ end)
153+ lib:wrap("void", "set_cb", "ptr")(cb)
154+ assert(lib:wrap("i32", "call_cb", "i32")(42) == 42 + 69)
155+ ```
156+
157+ ::: info
158+ This function is incompatible with [ W^X] ( https://en.wikipedia.org/wiki/W%5EX ) . ARM-based MacOS may enforce this without the ` com.apple.security.cs.allow-jit ` entitlement.
159+ :::
160+
138161---
139162## FFI Library Class
140163Obtained from ` ffi.open ` .
@@ -182,3 +205,27 @@ lib.add = lib:wrap("i32", "add", "i32", "i32")
182205assert(lib.MY_MAGIC_INT == 69)
183206assert(lib.add(38, 4) == 42)
184207```
208+
209+ ---
210+ ## FFI Callback Class
211+ Returned from ` ffi.callback ` .
212+
213+ ### ` blocking `
214+ Gets or sets whether native calls from non-Pluto threads block until processed.
215+ #### Parameters
216+ 1 . (optional) A boolean.
217+ #### Returns
218+ The current state if no parameter is provided.
219+
220+ ### ` tick `
221+ Processes pending callback invocations queued from other threads.
222+
223+ ### ` defaultreturn `
224+ Gets or sets the value returned to native code while callbacks are queued in non-blocking mode.
225+ #### Parameters
226+ 1 . (optional) The value to return.
227+ #### Returns
228+ The current default return value if no parameter is provided.
229+
230+ ### ` callcount `
231+ Returns the number of times the callback has been invoked.
0 commit comments