Skip to content

Commit

Permalink
test(ext/ffi): add test for multiple buffers (denoland#12373)
Browse files Browse the repository at this point in the history
  • Loading branch information
bartlomieju committed Oct 10, 2021
1 parent 29f9e14 commit 76de560
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 2 deletions.
13 changes: 13 additions & 0 deletions test_ffi/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,19 @@ pub extern "C" fn print_buffer(ptr: *const u8, len: usize) {
println!("{:?}", buf);
}

#[allow(clippy::not_unsafe_ptr_arg_deref)]
#[no_mangle]
pub extern "C" fn print_buffer2(
ptr1: *const u8,
len1: usize,
ptr2: *const u8,
len2: usize,
) {
let buf1 = unsafe { std::slice::from_raw_parts(ptr1, len1) };
let buf2 = unsafe { std::slice::from_raw_parts(ptr2, len2) };
println!("{:?} {:?}", buf1, buf2);
}

#[no_mangle]
pub extern "C" fn add_u32(a: u32, b: u32) -> u32 {
a + b
Expand Down
1 change: 1 addition & 0 deletions test_ffi/tests/integration_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ fn basic() {
dlopen doesn't panic\n\
something\n\
[1, 2, 3, 4, 5, 6, 7, 8]\n\
[1, 2, 3, 4, 5, 6, 7, 8] [9, 10]\n\
579\n\
579\n\
579\n\
Expand Down
10 changes: 8 additions & 2 deletions test_ffi/tests/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,10 @@ try {
const dylib = Deno.dlopen(libPath, {
"print_something": { parameters: [], result: "void" },
"print_buffer": { parameters: ["buffer", "usize"], result: "void" },
"print_buffer2": {
parameters: ["buffer", "usize", "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" },
Expand All @@ -39,7 +43,9 @@ const dylib = Deno.dlopen(libPath, {

dylib.symbols.print_something();
const buffer = new Uint8Array([1, 2, 3, 4, 5, 6, 7, 8]);
const buffer2 = new Uint8Array([9, 10]);
dylib.symbols.print_buffer(buffer, buffer.length);
dylib.symbols.print_buffer2(buffer, buffer.length, buffer2, buffer2.length);
console.log(dylib.symbols.add_u32(123, 456));
console.log(dylib.symbols.add_i32(123, 456));
console.log(dylib.symbols.add_u64(123, 456));
Expand Down Expand Up @@ -68,8 +74,8 @@ function deferred() {
}

const promise = deferred();
const buffer2 = new Uint8Array([1, 2, 3, 4, 5, 6, 7, 8]);
dylib.symbols.nonblocking_buffer(buffer2, buffer2.length).then(() => {
const buffer3 = new Uint8Array([1, 2, 3, 4, 5, 6, 7, 8]);
dylib.symbols.nonblocking_buffer(buffer3, buffer3.length).then(() => {
promise.resolve();
});
await promise;
Expand Down

0 comments on commit 76de560

Please sign in to comment.