mirror of
https://github.com/denoland/deno.git
synced 2024-12-22 07:14:47 -05:00
test(ext/ffi): add tests for different parameter and return types (#12158)
This commit is contained in:
parent
7c9c9e5c83
commit
60b68e63f1
4 changed files with 60 additions and 4 deletions
|
@ -2,7 +2,7 @@
|
|||
|
||||
import { assertThrows, unitTest } from "./test_util.ts";
|
||||
|
||||
unitTest(function dlopenInvalidArguments() {
|
||||
unitTest({ perms: { ffi: true } }, function dlopenInvalidArguments() {
|
||||
const filename = "/usr/lib/libc.so.6";
|
||||
assertThrows(() => {
|
||||
// @ts-expect-error: ForeignFunction cannot be null
|
||||
|
|
|
@ -4,6 +4,41 @@ pub extern "C" fn print_something() {
|
|||
}
|
||||
|
||||
#[no_mangle]
|
||||
pub extern "C" fn add(a: u32, b: u32) -> u32 {
|
||||
pub extern "C" fn add_u32(a: u32, b: u32) -> u32 {
|
||||
a + b
|
||||
}
|
||||
|
||||
#[no_mangle]
|
||||
pub extern "C" fn add_i32(a: i32, b: i32) -> i32 {
|
||||
a + b
|
||||
}
|
||||
|
||||
#[no_mangle]
|
||||
pub extern "C" fn add_u64(a: u64, b: u64) -> u64 {
|
||||
a + b
|
||||
}
|
||||
|
||||
#[no_mangle]
|
||||
pub extern "C" fn add_i64(a: i64, b: i64) -> i64 {
|
||||
a + b
|
||||
}
|
||||
|
||||
#[no_mangle]
|
||||
pub extern "C" fn add_usize(a: usize, b: usize) -> usize {
|
||||
a + b
|
||||
}
|
||||
|
||||
#[no_mangle]
|
||||
pub extern "C" fn add_isize(a: isize, b: isize) -> isize {
|
||||
a + b
|
||||
}
|
||||
|
||||
#[no_mangle]
|
||||
pub extern "C" fn add_f32(a: f32, b: f32) -> f32 {
|
||||
a + b
|
||||
}
|
||||
|
||||
#[no_mangle]
|
||||
pub extern "C" fn add_f64(a: f64, b: f64) -> f64 {
|
||||
a + b
|
||||
}
|
||||
|
|
|
@ -39,6 +39,13 @@ fn basic() {
|
|||
let expected = "\
|
||||
something\n\
|
||||
579\n\
|
||||
579\n\
|
||||
579\n\
|
||||
579\n\
|
||||
579\n\
|
||||
579\n\
|
||||
579.9119873046875\n\
|
||||
579.912\n\
|
||||
Correct number of resources\n";
|
||||
assert_eq!(stdout, expected);
|
||||
assert_eq!(stderr, "");
|
||||
|
|
|
@ -12,11 +12,25 @@ const libPath = `${targetDir}/${libPrefix}test_ffi.${libSuffix}`;
|
|||
const resourcesPre = Deno.resources();
|
||||
const dylib = Deno.dlopen(libPath, {
|
||||
"print_something": { parameters: [], result: "void" },
|
||||
"add": { parameters: ["u32", "u32"], result: "u32" },
|
||||
"add_u32": { parameters: ["u32", "u32"], result: "u32" },
|
||||
"add_i32": { parameters: ["i32", "i32"], result: "i32" },
|
||||
"add_u64": { parameters: ["u64", "u64"], result: "u64" },
|
||||
"add_i64": { parameters: ["i64", "i64"], result: "i64" },
|
||||
"add_usize": { parameters: ["usize", "usize"], result: "usize" },
|
||||
"add_isize": { parameters: ["isize", "isize"], result: "isize" },
|
||||
"add_f32": { parameters: ["f32", "f32"], result: "f32" },
|
||||
"add_f64": { parameters: ["f64", "f64"], result: "f64" },
|
||||
});
|
||||
|
||||
dylib.symbols.print_something();
|
||||
console.log(dylib.symbols.add(123, 456));
|
||||
console.log(dylib.symbols.add_u32(123, 456));
|
||||
console.log(dylib.symbols.add_i32(123, 456));
|
||||
console.log(dylib.symbols.add_u64(123, 456));
|
||||
console.log(dylib.symbols.add_i64(123, 456));
|
||||
console.log(dylib.symbols.add_usize(123, 456));
|
||||
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));
|
||||
|
||||
dylib.close();
|
||||
const resourcesPost = Deno.resources();
|
||||
|
|
Loading…
Reference in a new issue