From 3faf75aa883647dfa1f3be2bc13cf7bc463d92e1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bartek=20Iwa=C5=84czuk?= Date: Wed, 6 Oct 2021 00:27:05 +0200 Subject: feat(ext/ffi): add support for buffer arguments (#12335) This commit adds support for passing buffer arguments across FFI boundary. Co-authored-by: eliassjogreen Co-authored-by: Bert Belder --- test_ffi/src/lib.rs | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) (limited to 'test_ffi/src') diff --git a/test_ffi/src/lib.rs b/test_ffi/src/lib.rs index c91d05e05..cc6063ca3 100644 --- a/test_ffi/src/lib.rs +++ b/test_ffi/src/lib.rs @@ -1,3 +1,5 @@ +// Copyright 2018-2021 the Deno authors. All rights reserved. MIT license. + use std::thread::sleep; use std::time::Duration; @@ -6,6 +8,13 @@ pub extern "C" fn print_something() { println!("something"); } +#[allow(clippy::not_unsafe_ptr_arg_deref)] +#[no_mangle] +pub extern "C" fn print_buffer(ptr: *const u8, len: usize) { + let buf = unsafe { std::slice::from_raw_parts(ptr, len) }; + println!("{:?}", buf); +} + #[no_mangle] pub extern "C" fn add_u32(a: u32, b: u32) -> u32 { a + b @@ -51,3 +60,10 @@ pub extern "C" fn sleep_blocking(ms: u64) { let duration = Duration::from_millis(ms); sleep(duration); } + +#[allow(clippy::not_unsafe_ptr_arg_deref)] +#[no_mangle] +pub extern "C" fn nonblocking_buffer(ptr: *const u8, len: usize) { + let buf = unsafe { std::slice::from_raw_parts(ptr, len) }; + assert_eq!(buf, vec![1, 2, 3, 4, 5, 6, 7, 8]); +} -- cgit v1.2.3