-
Notifications
You must be signed in to change notification settings - Fork 5.6k
feat(ext/ffi): add support for buffer arguments #12335
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
5d71bc8
24a13d4
746f8be
6c775fb
1f92c47
b73e4b6
2a9f0da
e080158
e716539
39e5e37
4d132fa
838a94c
5f4d9d4
fc34425
b88f389
2f1ac45
9bb4271
32cc817
b2c622f
f7bdcd3
2d659e8
5587fbd
9483ca7
efd5e8c
93bef64
6e5d988
3f339a3
84ac1a5
067b136
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -12,6 +12,7 @@ const libPath = `${targetDir}/${libPrefix}test_ffi.${libSuffix}`; | |
const resourcesPre = Deno.resources(); | ||
const dylib = Deno.dlopen(libPath, { | ||
"print_something": { parameters: [], result: "void" }, | ||
"print_buffer": { parameters: ["buffer", "usize"], result: "void" }, | ||
"add_u32": { parameters: ["u32", "u32"], result: "u32" }, | ||
"add_i32": { parameters: ["i32", "i32"], result: "i32" }, | ||
"add_u64": { parameters: ["u64", "u64"], result: "u64" }, | ||
|
@@ -21,9 +22,16 @@ const dylib = Deno.dlopen(libPath, { | |
"add_f32": { parameters: ["f32", "f32"], result: "f32" }, | ||
"add_f64": { parameters: ["f64", "f64"], result: "f64" }, | ||
"sleep_blocking": { parameters: ["u64"], result: "void", nonblocking: true }, | ||
"nonblocking_buffer": { | ||
parameters: ["buffer", "usize"], | ||
result: "void", | ||
nonblocking: true, | ||
}, | ||
}); | ||
|
||
dylib.symbols.print_something(); | ||
const buffer = new Uint8Array([1, 2, 3, 4, 5, 6, 7, 8]); | ||
dylib.symbols.print_buffer(buffer, buffer.length); | ||
Comment on lines
+33
to
+34
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is a bit unhandy... I wonder if we could just sniff out length automatically in case of There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
It's possible of course to sniff out the length automatically on the JS/Rust side (for Uint8Array too, btw). |
||
console.log(dylib.symbols.add_u32(123, 456)); | ||
console.log(dylib.symbols.add_i32(123, 456)); | ||
console.log(dylib.symbols.add_u64(123, 456)); | ||
|
@@ -34,6 +42,30 @@ console.log(dylib.symbols.add_f32(123.123, 456.789)); | |
console.log(dylib.symbols.add_f64(123.123, 456.789)); | ||
|
||
// Test non blocking calls | ||
|
||
function deferred() { | ||
let methods; | ||
const promise = new Promise((resolve, reject) => { | ||
methods = { | ||
async resolve(value) { | ||
await value; | ||
resolve(value); | ||
}, | ||
reject(reason) { | ||
reject(reason); | ||
}, | ||
}; | ||
}); | ||
return Object.assign(promise, methods); | ||
} | ||
|
||
const promise = deferred(); | ||
const buffer2 = new Uint8Array([1, 2, 3, 4, 5, 6, 7, 8]); | ||
dylib.symbols.nonblocking_buffer(buffer2, buffer2.length).then(() => { | ||
promise.resolve(); | ||
}); | ||
await promise; | ||
|
||
const start = performance.now(); | ||
dylib.symbols.sleep_blocking(100).then(() => { | ||
console.log("After"); | ||
|
Uh oh!
There was an error while loading. Please reload this page.