summaryrefslogtreecommitdiff
path: root/ext/ffi/00_ffi.js
diff options
context:
space:
mode:
authorBartek IwaƄczuk <biwanczuk@gmail.com>2021-10-06 00:27:05 +0200
committerGitHub <noreply@github.com>2021-10-06 00:27:05 +0200
commit3faf75aa883647dfa1f3be2bc13cf7bc463d92e1 (patch)
treeedb141ebe411a348abe068c014828606e6614d93 /ext/ffi/00_ffi.js
parent58bb63f355085bc8dbd845f1cae688e5ce453eec (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.js41
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,
+ });
+ }
+ };
}
}