summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDjDeveloper <43033058+DjDeveloperr@users.noreply.github.com>2022-01-11 11:51:16 +0530
committerGitHub <noreply@github.com>2022-01-11 07:21:16 +0100
commit5680d33dd91af30d085ecd991e74dd56e367e8ea (patch)
treea706ec4b60e66b9d79e74cabba3d683e738f4ecc
parent91f6c5fc7e6f66f0e963c5cfbec281da4bcfc496 (diff)
feat(ext/ffi): support alias names for symbol definitions (#13090)
-rw-r--r--cli/dts/lib.deno.unstable.d.ts2
-rw-r--r--ext/ffi/lib.rs11
-rw-r--r--test_ffi/tests/integration_tests.rs2
-rw-r--r--test_ffi/tests/test.js25
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 a371f37d1..4b43c181b 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 c38788d30..2a6799b5f 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<U32x2> for u64 {
#[derive(Deserialize, Debug)]
#[serde(rename_all = "camelCase")]
struct ForeignFunction {
+ name: Option<String>,
parameters: Vec<NativeType>,
result: NativeType,
}
diff --git a/test_ffi/tests/integration_tests.rs b/test_ffi/tests/integration_tests.rs
index 39550925f..91de4412e 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 2f0465921..7ebcf4460 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.