Skip to content

Commit 9ede187

Browse files
committed
feat: ffi support
1 parent cf32026 commit 9ede187

14 files changed

+594
-0
lines changed

Cargo.lock

+42
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

+1
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ members = [
66
"core",
77
"runtime",
88
"test_plugin",
9+
"test_ffi",
910
"test_util",
1011
"op_crates/fetch",
1112
"op_crates/web",

cli/dts/lib.deno.unstable.d.ts

+51
Original file line numberDiff line numberDiff line change
@@ -1255,6 +1255,57 @@ declare namespace Deno {
12551255
* ```
12561256
*/
12571257
export function sleepSync(millis: number): Promise<void>;
1258+
1259+
export type DyLibraryDataType =
1260+
| "i8"
1261+
| "i16"
1262+
| "i32"
1263+
| "i64"
1264+
| "u8"
1265+
| "u16"
1266+
| "u32"
1267+
| "u64"
1268+
| "f32"
1269+
| "f64";
1270+
1271+
export interface CallDylibraryOptions {
1272+
params?: {
1273+
typeName: DyLibraryDataType;
1274+
value: any;
1275+
}[];
1276+
returnType?: DyLibraryDataType;
1277+
}
1278+
1279+
export class DyLibrary {
1280+
/** Call dynamic library function
1281+
*
1282+
* ```ts
1283+
* const lib = await Deno.loadLibrary("./libtest.dylib");
1284+
* const rval = lib.call("some_func", {
1285+
* params: [{ typeName: "i32", value: 10 }]
1286+
* returnType: "i32"
1287+
* });
1288+
* console.log(rval);
1289+
* ```
1290+
*/
1291+
call<T = any>(name: string, options?: CallDylibraryOptions): T;
1292+
1293+
/** Unload dynamic library */
1294+
close(): void;
1295+
}
1296+
1297+
/** **UNSTABLE**: new API, yet to be vetted.
1298+
*
1299+
* Load a dynamic library from the given file name,
1300+
*
1301+
* ```ts
1302+
* const lib = await Deno.loadLibrary("./libtest.dylib");
1303+
* lib.call("some_func");
1304+
* ```
1305+
*
1306+
* Requires `allow-all` permission.
1307+
*/
1308+
export function loadLibrary(filename: string): DyLibrary;
12581309
}
12591310

12601311
declare function fetch(

runtime/Cargo.toml

+1
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ lazy_static = "1.4.0"
4747
libc = "0.2.82"
4848
log = "0.4.13"
4949
notify = "5.0.0-pre.4"
50+
libffi = "1.0.0"
5051
percent-encoding = "2.1.0"
5152
regex = "1.4.3"
5253
ring = "0.16.19"

runtime/js/40_ffi.js

+35
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
// Copyright 2018-2020 the Deno authors. All rights reserved. MIT license.
2+
3+
((window) => {
4+
const core = window.Deno.core;
5+
6+
class DyLibaray {
7+
#rid = 0;
8+
constructor(rid) {
9+
this.#rid = rid;
10+
}
11+
12+
call(name, options) {
13+
const { params = [], returnType = "" } = options ?? {};
14+
return core.jsonOpSync("op_call_libaray_ffi", {
15+
rid: this.#rid,
16+
name,
17+
params,
18+
returnType,
19+
});
20+
}
21+
22+
close() {
23+
core.close(this.#rid);
24+
}
25+
}
26+
27+
function loadLibrary(filename) {
28+
const rid = core.jsonOpSync("op_load_libaray", { filename });
29+
return new DyLibaray(rid);
30+
}
31+
32+
window.__bootstrap.ffi = {
33+
loadLibrary,
34+
};
35+
})(this);

runtime/js/90_deno_ns.js

+1
Original file line numberDiff line numberDiff line change
@@ -130,5 +130,6 @@
130130
symlinkSync: __bootstrap.fs.symlinkSync,
131131
HttpClient: __bootstrap.fetch.HttpClient,
132132
createHttpClient: __bootstrap.fetch.createHttpClient,
133+
loadLibrary: __bootstrap.ffi.loadLibrary,
133134
};
134135
})(this);

0 commit comments

Comments
 (0)