1
0
Fork 0
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:
Elias Sjögreen 2021-09-20 11:38:28 -07:00 committed by Bert Belder
parent 7c9c9e5c83
commit 60b68e63f1
No known key found for this signature in database
GPG key ID: 7A77887B2E2ED461
4 changed files with 60 additions and 4 deletions

View file

@ -2,7 +2,7 @@
import { assertThrows, unitTest } from "./test_util.ts"; import { assertThrows, unitTest } from "./test_util.ts";
unitTest(function dlopenInvalidArguments() { unitTest({ perms: { ffi: true } }, function dlopenInvalidArguments() {
const filename = "/usr/lib/libc.so.6"; const filename = "/usr/lib/libc.so.6";
assertThrows(() => { assertThrows(() => {
// @ts-expect-error: ForeignFunction cannot be null // @ts-expect-error: ForeignFunction cannot be null

View file

@ -4,6 +4,41 @@ pub extern "C" fn print_something() {
} }
#[no_mangle] #[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 a + b
} }

View file

@ -39,6 +39,13 @@ fn basic() {
let expected = "\ let expected = "\
something\n\ something\n\
579\n\ 579\n\
579\n\
579\n\
579\n\
579\n\
579\n\
579.9119873046875\n\
579.912\n\
Correct number of resources\n"; Correct number of resources\n";
assert_eq!(stdout, expected); assert_eq!(stdout, expected);
assert_eq!(stderr, ""); assert_eq!(stderr, "");

View file

@ -12,11 +12,25 @@ const libPath = `${targetDir}/${libPrefix}test_ffi.${libSuffix}`;
const resourcesPre = Deno.resources(); const resourcesPre = Deno.resources();
const dylib = Deno.dlopen(libPath, { const dylib = Deno.dlopen(libPath, {
"print_something": { parameters: [], result: "void" }, "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(); 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(); dylib.close();
const resourcesPost = Deno.resources(); const resourcesPost = Deno.resources();