1
0
Fork 0
mirror of https://github.com/denoland/deno.git synced 2024-11-24 15:19:26 -05:00

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

This commit is contained in:
DjDeveloper 2022-01-11 11:51:16 +05:30 committed by GitHub
parent 91f6c5fc7e
commit 5680d33dd9
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 32 additions and 8 deletions

View file

@ -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`. */

View file

@ -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,
}

View file

@ -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\

View file

@ -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.