From 5680d33dd91af30d085ecd991e74dd56e367e8ea Mon Sep 17 00:00:00 2001 From: DjDeveloper <43033058+DjDeveloperr@users.noreply.github.com> Date: Tue, 11 Jan 2022 11:51:16 +0530 Subject: [PATCH] feat(ext/ffi): support alias names for symbol definitions (#13090) --- cli/dts/lib.deno.unstable.d.ts | 2 ++ ext/ffi/lib.rs | 11 ++++++++--- test_ffi/tests/integration_tests.rs | 2 ++ test_ffi/tests/test.js | 25 ++++++++++++++++++++----- 4 files changed, 32 insertions(+), 8 deletions(-) diff --git a/cli/dts/lib.deno.unstable.d.ts b/cli/dts/lib.deno.unstable.d.ts index a371f37d1b..4b43c181be 100644 --- a/cli/dts/lib.deno.unstable.d.ts +++ b/cli/dts/lib.deno.unstable.d.ts @@ -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`. */ diff --git a/ext/ffi/lib.rs b/ext/ffi/lib.rs index c38788d30f..2a6799b5ff 100644 --- a/ext/ffi/lib.rs +++ b/ext/ffi/lib.rs @@ -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 {}: {}", @@ -103,7 +107,7 @@ impl DynamicLibraryResource { ); self.symbols.insert( - symbol, + name, Symbol { cif, ptr, @@ -337,6 +341,7 @@ impl From for u64 { #[derive(Deserialize, Debug)] #[serde(rename_all = "camelCase")] struct ForeignFunction { + name: Option, parameters: Vec, result: NativeType, } diff --git a/test_ffi/tests/integration_tests.rs b/test_ffi/tests/integration_tests.rs index 39550925fa..91de4412e0 100644 --- a/test_ffi/tests/integration_tests.rs +++ b/test_ffi/tests/integration_tests.rs @@ -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\ diff --git a/test_ffi/tests/test.js b/test_ffi/tests/test.js index 2f04659215..7ebcf4460d 100644 --- a/test_ffi/tests/test.js +++ b/test_ffi/tests/test.js @@ -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"], @@ -49,7 +53,13 @@ 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", @@ -57,7 +67,7 @@ const dylib = Deno.dlopen(libPath, { }, }); -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); @@ -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.