1
0
Fork 0
mirror of https://github.com/denoland/deno.git synced 2024-12-22 07:14:47 -05:00

test(ext/ffi): add test for multiple buffers (#12373)

This commit is contained in:
Bartek Iwańczuk 2021-10-10 15:18:02 +02:00 committed by GitHub
parent 29f9e14457
commit 76de56091b
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 22 additions and 2 deletions

View file

@ -15,6 +15,19 @@ pub extern "C" fn print_buffer(ptr: *const u8, len: usize) {
println!("{:?}", buf); 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] #[no_mangle]
pub extern "C" fn add_u32(a: u32, b: u32) -> u32 { pub extern "C" fn add_u32(a: u32, b: u32) -> u32 {
a + b a + b

View file

@ -40,6 +40,7 @@ fn basic() {
dlopen doesn't panic\n\ dlopen doesn't panic\n\
something\n\ something\n\
[1, 2, 3, 4, 5, 6, 7, 8]\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\ 579\n\
579\n\ 579\n\

View file

@ -21,6 +21,10 @@ try {
const dylib = Deno.dlopen(libPath, { const dylib = Deno.dlopen(libPath, {
"print_something": { parameters: [], result: "void" }, "print_something": { parameters: [], result: "void" },
"print_buffer": { parameters: ["buffer", "usize"], 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_u32": { parameters: ["u32", "u32"], result: "u32" },
"add_i32": { parameters: ["i32", "i32"], result: "i32" }, "add_i32": { parameters: ["i32", "i32"], result: "i32" },
"add_u64": { parameters: ["u64", "u64"], result: "u64" }, "add_u64": { parameters: ["u64", "u64"], result: "u64" },
@ -39,7 +43,9 @@ const dylib = Deno.dlopen(libPath, {
dylib.symbols.print_something(); dylib.symbols.print_something();
const buffer = new Uint8Array([1, 2, 3, 4, 5, 6, 7, 8]); 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_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_u32(123, 456));
console.log(dylib.symbols.add_i32(123, 456)); console.log(dylib.symbols.add_i32(123, 456));
console.log(dylib.symbols.add_u64(123, 456)); console.log(dylib.symbols.add_u64(123, 456));
@ -68,8 +74,8 @@ function deferred() {
} }
const promise = deferred(); const promise = deferred();
const buffer2 = new Uint8Array([1, 2, 3, 4, 5, 6, 7, 8]); const buffer3 = new Uint8Array([1, 2, 3, 4, 5, 6, 7, 8]);
dylib.symbols.nonblocking_buffer(buffer2, buffer2.length).then(() => { dylib.symbols.nonblocking_buffer(buffer3, buffer3.length).then(() => {
promise.resolve(); promise.resolve();
}); });
await promise; await promise;