From 0cb81951aff530b28b5ded8bd57cf34922b93ac6 Mon Sep 17 00:00:00 2001 From: Carter Snook Date: Wed, 10 Nov 2021 07:55:46 -0600 Subject: [PATCH] test(ffi): add mutable buffer tests (#12701) --- test_ffi/src/lib.rs | 9 +++++++++ test_ffi/tests/test.js | 16 ++++++++++++++++ 2 files changed, 25 insertions(+) diff --git a/test_ffi/src/lib.rs b/test_ffi/src/lib.rs index d7fd5182da..38275e5474 100644 --- a/test_ffi/src/lib.rs +++ b/test_ffi/src/lib.rs @@ -74,6 +74,15 @@ pub extern "C" fn sleep_blocking(ms: u64) { sleep(duration); } +#[allow(clippy::not_unsafe_ptr_arg_deref)] +#[no_mangle] +pub extern "C" fn fill_buffer(value: u8, buf: *mut u8, len: usize) { + let buf = unsafe { std::slice::from_raw_parts_mut(buf, len) }; + for itm in buf.iter_mut() { + *itm = value; + } +} + #[allow(clippy::not_unsafe_ptr_arg_deref)] #[no_mangle] pub extern "C" fn nonblocking_buffer(ptr: *const u8, len: usize) { diff --git a/test_ffi/tests/test.js b/test_ffi/tests/test.js index fbd023a91b..35d51c0063 100644 --- a/test_ffi/tests/test.js +++ b/test_ffi/tests/test.js @@ -33,6 +33,7 @@ const dylib = Deno.dlopen(libPath, { "add_isize": { parameters: ["isize", "isize"], result: "isize" }, "add_f32": { parameters: ["f32", "f32"], result: "f32" }, "add_f64": { parameters: ["f64", "f64"], result: "f64" }, + "fill_buffer": { parameters: ["u8", "buffer", "usize"], result: "void" }, "sleep_blocking": { parameters: ["u64"], result: "void", nonblocking: true }, "nonblocking_buffer": { parameters: ["buffer", "usize"], @@ -55,6 +56,21 @@ console.log(dylib.symbols.add_isize(123, 456)); console.log(dylib.symbols.add_f32(123.123, 456.789)); console.log(dylib.symbols.add_f64(123.123, 456.789)); +// test mutating sync calls + +function test_fill_buffer(fillValue, arr) { + let buf = new Uint8Array(arr); + dylib.symbols.fill_buffer(fillValue, buf, buf.length); + for (let i = 0; i < buf.length; i++) { + if (buf[i] !== fillValue) { + throw new Error(`Found '${buf[i]}' in buffer, expected '${fillValue}'.`); + } + } +} + +test_fill_buffer(0, [2, 3, 4]); +test_fill_buffer(5, [2, 7, 3, 2, 1]); + // Test non blocking calls function deferred() {