diff options
Diffstat (limited to 'test_ffi')
-rw-r--r-- | test_ffi/src/lib.rs | 16 | ||||
-rw-r--r-- | test_ffi/tests/integration_tests.rs | 1 | ||||
-rw-r--r-- | test_ffi/tests/test.js | 32 |
3 files changed, 49 insertions, 0 deletions
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]); +} diff --git a/test_ffi/tests/integration_tests.rs b/test_ffi/tests/integration_tests.rs index 0b2eae854..0ae395da8 100644 --- a/test_ffi/tests/integration_tests.rs +++ b/test_ffi/tests/integration_tests.rs @@ -38,6 +38,7 @@ fn basic() { assert!(output.status.success()); let expected = "\ something\n\ + [1, 2, 3, 4, 5, 6, 7, 8]\n\ 579\n\ 579\n\ 579\n\ diff --git a/test_ffi/tests/test.js b/test_ffi/tests/test.js index 09c5f7417..fc354139d 100644 --- a/test_ffi/tests/test.js +++ b/test_ffi/tests/test.js @@ -12,6 +12,7 @@ const libPath = `${targetDir}/${libPrefix}test_ffi.${libSuffix}`; const resourcesPre = Deno.resources(); const dylib = Deno.dlopen(libPath, { "print_something": { parameters: [], result: "void" }, + "print_buffer": { parameters: ["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" }, @@ -21,9 +22,16 @@ const dylib = Deno.dlopen(libPath, { "add_f32": { parameters: ["f32", "f32"], result: "f32" }, "add_f64": { parameters: ["f64", "f64"], result: "f64" }, "sleep_blocking": { parameters: ["u64"], result: "void", nonblocking: true }, + "nonblocking_buffer": { + parameters: ["buffer", "usize"], + result: "void", + nonblocking: true, + }, }); dylib.symbols.print_something(); +const buffer = new Uint8Array([1, 2, 3, 4, 5, 6, 7, 8]); +dylib.symbols.print_buffer(buffer, buffer.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)); @@ -34,6 +42,30 @@ console.log(dylib.symbols.add_f32(123.123, 456.789)); console.log(dylib.symbols.add_f64(123.123, 456.789)); // Test non blocking calls + +function deferred() { + let methods; + const promise = new Promise((resolve, reject) => { + methods = { + async resolve(value) { + await value; + resolve(value); + }, + reject(reason) { + reject(reason); + }, + }; + }); + return Object.assign(promise, methods); +} + +const promise = deferred(); +const buffer2 = new Uint8Array([1, 2, 3, 4, 5, 6, 7, 8]); +dylib.symbols.nonblocking_buffer(buffer2, buffer2.length).then(() => { + promise.resolve(); +}); +await promise; + const start = performance.now(); dylib.symbols.sleep_blocking(100).then(() => { console.log("After"); |