diff options
author | Bartek IwaĆczuk <biwanczuk@gmail.com> | 2021-10-06 00:27:05 +0200 |
---|---|---|
committer | GitHub <noreply@github.com> | 2021-10-06 00:27:05 +0200 |
commit | 3faf75aa883647dfa1f3be2bc13cf7bc463d92e1 (patch) | |
tree | edb141ebe411a348abe068c014828606e6614d93 /ext/ffi/00_ffi.js | |
parent | 58bb63f355085bc8dbd845f1cae688e5ce453eec (diff) |
feat(ext/ffi): add support for buffer arguments (#12335)
This commit adds support for passing buffer arguments across
FFI boundary.
Co-authored-by: eliassjogreen <eliassjogreen1@gmail.com>
Co-authored-by: Bert Belder <bertbelder@gmail.com>
Diffstat (limited to 'ext/ffi/00_ffi.js')
-rw-r--r-- | ext/ffi/00_ffi.js | 41 |
1 files changed, 34 insertions, 7 deletions
diff --git a/ext/ffi/00_ffi.js b/ext/ffi/00_ffi.js index c7fdd0e8c..25eba0233 100644 --- a/ext/ffi/00_ffi.js +++ b/ext/ffi/00_ffi.js @@ -4,7 +4,9 @@ ((window) => { const core = window.Deno.core; const __bootstrap = window.__bootstrap; - + const { + ArrayBuffer, + } = window.__bootstrap.primordials; class DynamicLibrary { #rid; symbols = {}; @@ -13,15 +15,40 @@ this.#rid = core.opSync("op_ffi_load", { path, symbols }); for (const symbol in symbols) { - this.symbols[symbol] = symbols[symbol].nonblocking - ? (...parameters) => - core.opAsync("op_ffi_call_nonblocking", { + const isNonBlocking = symbols[symbol].nonblocking; + + this.symbols[symbol] = (...args) => { + const parameters = []; + const buffers = []; + + for (const arg of args) { + if ( + arg?.buffer instanceof ArrayBuffer && + arg.byteLength !== undefined + ) { + parameters.push(buffers.length); + buffers.push(arg); + } else { + parameters.push(arg); + } + } + + if (isNonBlocking) { + return core.opAsync("op_ffi_call_nonblocking", { + rid: this.#rid, + symbol, + parameters, + buffers, + }); + } else { + return core.opSync("op_ffi_call", { rid: this.#rid, symbol, parameters, - }) - : (...parameters) => - core.opSync("op_ffi_call", { rid: this.#rid, symbol, parameters }); + buffers, + }); + } + }; } } |