mirror of
https://github.com/denoland/deno.git
synced 2024-12-25 16:49:18 -05:00
test(ffi): add mutable buffer tests (#12701)
This commit is contained in:
parent
8ce53dd22b
commit
0cb81951af
2 changed files with 25 additions and 0 deletions
|
@ -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) {
|
||||
|
|
|
@ -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() {
|
||||
|
|
Loading…
Reference in a new issue