diff options
| author | DjDeveloper <43033058+DjDeveloperr@users.noreply.github.com> | 2022-01-12 17:08:26 +0530 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2022-01-12 12:38:26 +0100 |
| commit | 62291e9b0e99406ac7f5a95dc329400f9f966825 (patch) | |
| tree | 94c0e151828539c52d48a4d3d92a1e0931fe21aa /test_ffi | |
| parent | 79b698f88b5f8e247df7280ccb52c2a8271a426c (diff) | |
feat(ext/ffi): UnsafeFnPointer API (#13340)
Diffstat (limited to 'test_ffi')
| -rw-r--r-- | test_ffi/src/lib.rs | 11 | ||||
| -rw-r--r-- | test_ffi/tests/ffi_types.ts | 12 | ||||
| -rw-r--r-- | test_ffi/tests/integration_tests.rs | 2 | ||||
| -rw-r--r-- | test_ffi/tests/test.js | 26 |
4 files changed, 51 insertions, 0 deletions
diff --git a/test_ffi/src/lib.rs b/test_ffi/src/lib.rs index 93b274b4b..a04c2c2fd 100644 --- a/test_ffi/src/lib.rs +++ b/test_ffi/src/lib.rs @@ -1,5 +1,6 @@ // Copyright 2018-2022 the Deno authors. All rights reserved. MIT license. +use std::os::raw::c_void; use std::thread::sleep; use std::time::Duration; @@ -101,3 +102,13 @@ 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]); } + +#[no_mangle] +pub extern "C" fn get_add_u32_ptr() -> *const c_void { + add_u32 as *const c_void +} + +#[no_mangle] +pub extern "C" fn get_sleep_blocking_ptr() -> *const c_void { + sleep_blocking as *const c_void +} diff --git a/test_ffi/tests/ffi_types.ts b/test_ffi/tests/ffi_types.ts index e10cfb894..dca361581 100644 --- a/test_ffi/tests/ffi_types.ts +++ b/test_ffi/tests/ffi_types.ts @@ -109,3 +109,15 @@ const result4 = remote.symbols.method19(); // @ts-expect-error: Invalid argument result4.then((_0: Deno.TypedArray) => {}); result4.then((_1: Deno.UnsafePointer) => {}); + +const ptr = new Deno.UnsafePointer(0n); +const fnptr = new Deno.UnsafeFnPointer( + ptr, + { + parameters: ["u32", "pointer"], + result: "void", + } as const, +); +// @ts-expect-error: Invalid argument +fnptr.call(null, null); +fnptr.call(0, null); diff --git a/test_ffi/tests/integration_tests.rs b/test_ffi/tests/integration_tests.rs index 91de4412e..c818f12d9 100644 --- a/test_ffi/tests/integration_tests.rs +++ b/test_ffi/tests/integration_tests.rs @@ -56,6 +56,8 @@ fn basic() { true\n\ false\n\ 579\n\ + true\n\ + 579\n\ 579\n\ 579\n\ 579\n\ diff --git a/test_ffi/tests/test.js b/test_ffi/tests/test.js index 7ebcf4460..a9681ab9f 100644 --- a/test_ffi/tests/test.js +++ b/test_ffi/tests/test.js @@ -65,6 +65,14 @@ const dylib = Deno.dlopen(libPath, { result: "void", nonblocking: true, }, + "get_add_u32_ptr": { + parameters: [], + result: "pointer", + }, + "get_sleep_blocking_ptr": { + parameters: [], + result: "pointer", + }, }); dylib.symbols.printSomething(); @@ -97,6 +105,24 @@ console.log(stringPtrview.getCString(11)); console.log(Boolean(dylib.symbols.is_null_ptr(ptr))); console.log(Boolean(dylib.symbols.is_null_ptr(null))); console.log(Boolean(dylib.symbols.is_null_ptr(Deno.UnsafePointer.of(into)))); + +const addU32Ptr = dylib.symbols.get_add_u32_ptr(); +const addU32 = new Deno.UnsafeFnPointer(addU32Ptr, { + parameters: ["u32", "u32"], + result: "u32", +}); +console.log(addU32.call(123, 456)); + +const sleepBlockingPtr = dylib.symbols.get_sleep_blocking_ptr(); +const sleepNonBlocking = new Deno.UnsafeFnPointer(sleepBlockingPtr, { + nonblocking: true, + parameters: ["u64"], + result: "void", +}); +const before = performance.now(); +await sleepNonBlocking.call(100); +console.log(performance.now() - before >= 100); + console.log(dylib.symbols.add_u32(123, 456)); assertThrows( () => { |
