Skip to content

Commit

Permalink
feat(ext/ffi): support alias names for symbol definitions (#13090)
Browse files Browse the repository at this point in the history
  • Loading branch information
DjDeveloperr authored Jan 11, 2022
1 parent 91f6c5f commit 5680d33
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 8 deletions.
2 changes: 2 additions & 0 deletions cli/dts/lib.deno.unstable.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,8 @@ declare namespace Deno {
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",
},
"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

0 comments on commit 5680d33

Please sign in to comment.