Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(ext/ffi): support alias names for symbol definitions #13090

Merged
merged 4 commits into from
Jan 11, 2022
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion cli/dts/lib.deno.unstable.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -120,12 +120,13 @@ declare namespace Deno {
| "f64"
| "pointer";

/** A foreign function as defined by its parameter and result types */
export interface ForeignFunction<
Parameters extends readonly NativeType[] = readonly NativeType[],
Result extends NativeType = NativeType,
NonBlocking extends boolean = boolean,
> {
/** Name of the symbol, defaults to the key name in symbols object. */
name?: string;
parameters: Parameters;
result: Result;
/** When true, function calls will run on a dedicated blocking thread and will return a Promise resolving to the `result`. */
Expand Down
11 changes: 8 additions & 3 deletions ext/ffi/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -78,13 +78,17 @@ impl Resource for DynamicLibraryResource {
impl DynamicLibraryResource {
fn register(
&mut self,
symbol: String,
name: String,
foreign_fn: ForeignFunction,
) -> Result<(), AnyError> {
let symbol = match &foreign_fn.name {
Some(symbol) => symbol,
None => &name,
};
// By default, Err returned by this function does not tell
// which symbol wasn't exported. So we'll modify the error
// message to include the name of symbol.
let fn_ptr = match unsafe { self.lib.symbol::<*const c_void>(&symbol) } {
let fn_ptr = match unsafe { self.lib.symbol::<*const c_void>(symbol) } {
Ok(value) => Ok(value),
Err(err) => Err(generic_error(format!(
"Failed to register symbol {}: {}",
Expand All @@ -103,7 +107,7 @@ impl DynamicLibraryResource {
);

self.symbols.insert(
symbol,
name,
Symbol {
cif,
ptr,
Expand Down Expand Up @@ -337,6 +341,7 @@ impl From<U32x2> for u64 {
#[derive(Deserialize, Debug)]
#[serde(rename_all = "camelCase")]
struct ForeignFunction {
name: Option<String>,
parameters: Vec<NativeType>,
result: NativeType,
}
Expand Down
2 changes: 2 additions & 0 deletions test_ffi/tests/integration_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,8 @@ fn basic() {
579\n\
579.9119873046875\n\
579.912\n\
After sleep_blocking\n\
true\n\
Before\n\
true\n\
After\n\
Expand Down
25 changes: 20 additions & 5 deletions test_ffi/tests/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,11 @@ assertThrows(
);

const dylib = Deno.dlopen(libPath, {
"print_something": { parameters: [], result: "void" },
"printSomething": {
name: "print_something",
parameters: [],
result: "void",
},
DjDeveloperr marked this conversation as resolved.
Show resolved Hide resolved
"print_buffer": { parameters: ["pointer", "usize"], result: "void" },
"print_buffer2": {
parameters: ["pointer", "usize", "pointer", "usize"],
Expand All @@ -49,15 +53,21 @@ const dylib = Deno.dlopen(libPath, {
"add_f32": { parameters: ["f32", "f32"], result: "f32" },
"add_f64": { parameters: ["f64", "f64"], result: "f64" },
"fill_buffer": { parameters: ["u8", "pointer", "usize"], result: "void" },
"sleep_blocking": { parameters: ["u64"], result: "void", nonblocking: true },
"sleep_nonblocking": {
name: "sleep_blocking",
parameters: ["u64"],
result: "void",
nonblocking: true,
},
"sleep_blocking": { parameters: ["u64"], result: "void" },
"nonblocking_buffer": {
parameters: ["pointer", "usize"],
result: "void",
nonblocking: true,
},
});

dylib.symbols.print_something();
dylib.symbols.printSomething();
const buffer = new Uint8Array([1, 2, 3, 4, 5, 6, 7, 8]);
const buffer2 = new Uint8Array([9, 10]);
dylib.symbols.print_buffer(buffer, buffer.length);
Expand Down Expand Up @@ -150,8 +160,13 @@ dylib.symbols.nonblocking_buffer(buffer3, buffer3.length).then(() => {
});
await promise;

const start = performance.now();
dylib.symbols.sleep_blocking(100).then(() => {
let start = performance.now();
dylib.symbols.sleep_blocking(100);
console.log("After sleep_blocking");
console.log(performance.now() - start >= 100);

start = performance.now();
dylib.symbols.sleep_nonblocking(100).then(() => {
console.log("After");
console.log(performance.now() - start >= 100);
// Close after task is complete.
Expand Down