Skip to content

Commit

Permalink
support ffi
Browse files Browse the repository at this point in the history
  • Loading branch information
manyuanrong committed Jan 21, 2021
1 parent 413f79a commit 5826753
Show file tree
Hide file tree
Showing 15 changed files with 664 additions and 0 deletions.
18 changes: 18 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,24 @@ jobs:
service_account_key: ${{ secrets.GCP_SA_KEY }}
export_default_credentials: true

- name: Setup GNU Tools (Linux)
uses: actions-rs/toolchain@v1
if: startsWith(matrix.os, 'ubuntu')
with:
toolchain: stable
target: x86_64-unknown-linux-gnu

- name: Setup GNU Tools (Windows)
uses: actions-rs/toolchain@v1
if: startsWith(matrix.os, 'windows')
with:
toolchain: stable
target: x86_64-pc-windows-gnu

- name: Setup GNU Tools (MacOS)
if: startsWith(matrix.os, 'macOS')
run: brew install autoconf automake libtool libffi

- name: Configure canary build
if: |
matrix.kind == 'test_release' &&
Expand Down
42 changes: 42 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ members = [
"core",
"runtime",
"test_plugin",
"test_ffi",
"test_util",
"op_crates/fetch",
"op_crates/web",
Expand Down
52 changes: 52 additions & 0 deletions cli/dts/lib.deno.unstable.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1341,6 +1341,58 @@ declare namespace Deno {
* ```
*/
export function sleepSync(millis: number): Promise<void>;

export type DyLibraryDataType =
| "i8"
| "i16"
| "i32"
| "i64"
| "u8"
| "u16"
| "u32"
| "u64"
| "f32"
| "f64"
| "cstr";

export interface CallDylibraryOptions {
params?: {
typeName: DyLibraryDataType;
value: any;
}[];
returnType?: DyLibraryDataType;
}

export class DyLibrary {
/** Call dynamic library function
*
* ```ts
* const lib = await Deno.loadLibrary("./libtest.dylib");
* const rval = lib.call("some_func", {
* params: [{ typeName: "i32", value: 10 }]
* returnType: "i32"
* });
* console.log(rval);
* ```
*/
call<T = any>(name: string, options?: CallDylibraryOptions): T;

/** Unload dynamic library */
close(): void;
}

/** **UNSTABLE**: new API, yet to be vetted.
*
* Load a dynamic library from the given file name,
*
* ```ts
* const lib = await Deno.loadLibrary("./libtest.dylib");
* lib.call("some_func");
* ```
*
* Requires `allow-all` permission.
*/
export function loadLibrary(filename: string): DyLibrary;
}

declare function fetch(
Expand Down
1 change: 1 addition & 0 deletions runtime/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ lazy_static = "1.4.0"
libc = "0.2.82"
log = "0.4.13"
notify = "5.0.0-pre.4"
libffi = "1.0.0"
percent-encoding = "2.1.0"
regex = "1.4.3"
ring = "0.16.19"
Expand Down
35 changes: 35 additions & 0 deletions runtime/js/40_ffi.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
// Copyright 2018-2020 the Deno authors. All rights reserved. MIT license.

((window) => {
const core = window.Deno.core;

class DyLibaray {
#rid = 0;
constructor(rid) {
this.#rid = rid;
}

call(name, options) {
const { params = [], returnType = "" } = options ?? {};
return core.jsonOpSync("op_call_libaray_ffi", {
rid: this.#rid,
name,
params,
returnType,
});
}

close() {
core.close(this.#rid);
}
}

function loadLibrary(filename) {
const rid = core.jsonOpSync("op_load_libaray", { filename });
return new DyLibaray(rid);
}

window.__bootstrap.ffi = {
loadLibrary,
};
})(this);
1 change: 1 addition & 0 deletions runtime/js/90_deno_ns.js
Original file line number Diff line number Diff line change
Expand Up @@ -131,5 +131,6 @@
symlinkSync: __bootstrap.fs.symlinkSync,
HttpClient: __bootstrap.fetch.HttpClient,
createHttpClient: __bootstrap.fetch.createHttpClient,
loadLibrary: __bootstrap.ffi.loadLibrary,
};
})(this);
Loading

0 comments on commit 5826753

Please sign in to comment.