From 76de56091bcda1eae8cfb398981c8470d06268e3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bartek=20Iwa=C5=84czuk?= Date: Sun, 10 Oct 2021 15:18:02 +0200 Subject: [PATCH] test(ext/ffi): add test for multiple buffers (#12373) --- test_ffi/src/lib.rs | 13 +++++++++++++ test_ffi/tests/integration_tests.rs | 1 + test_ffi/tests/test.js | 10 ++++++++-- 3 files changed, 22 insertions(+), 2 deletions(-) diff --git a/test_ffi/src/lib.rs b/test_ffi/src/lib.rs index cc6063ca34..d7fd5182da 100644 --- a/test_ffi/src/lib.rs +++ b/test_ffi/src/lib.rs @@ -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 diff --git a/test_ffi/tests/integration_tests.rs b/test_ffi/tests/integration_tests.rs index 28176f1876..e16e97e24e 100644 --- a/test_ffi/tests/integration_tests.rs +++ b/test_ffi/tests/integration_tests.rs @@ -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\ diff --git a/test_ffi/tests/test.js b/test_ffi/tests/test.js index 098541265c..fbd023a91b 100644 --- a/test_ffi/tests/test.js +++ b/test_ffi/tests/test.js @@ -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" }, @@ -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)); @@ -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;