Skip to content

Commit 33c8d79

Browse files
feat: ffi to replace plugins (#11152)
This commit removes implementation of "native plugins" and replaces it with FFI API. Effectively "Deno.openPlugin" API was replaced with "Deno.dlopen" API.
1 parent 0d1a522 commit 33c8d79

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

44 files changed

+860
-511
lines changed

.github/workflows/ci.yml

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -164,11 +164,13 @@ jobs:
164164
sudo apt-get update
165165
sudo apt-get install debootstrap
166166
167-
# Note: git, nc, strace, and time, are needed to run the benchmarks.
168-
sudo debootstrap \
169-
--include=ca-certificates,curl,git,netcat-openbsd,strace,time \
170-
--no-merged-usr --variant=minbase bionic /sysroot \
171-
http://azure.archive.ubuntu.com/ubuntu
167+
# `file` and `make` are needed to build libffi-sys.
168+
# `curl` is needed to build rusty_v8.
169+
# `git`, `nc`, `strace`, and `time`, are needed to run the benchmarks.
170+
sudo debootstrap \
171+
--include=ca-certificates,curl,file,git,make,netcat-openbsd,strace,time \
172+
--no-merged-usr --variant=minbase bionic /sysroot \
173+
http://azure.archive.ubuntu.com/ubuntu
172174
sudo mount --rbind /dev /sysroot/dev
173175
sudo mount --rbind /sys /sysroot/sys
174176
sudo mount --rbind /home /sysroot/home

Cargo.lock

Lines changed: 47 additions & 5 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,13 @@ members = [
66
"cli",
77
"core",
88
"runtime",
9-
"test_plugin",
9+
"test_ffi",
1010
"test_util",
1111
"extensions/broadcast_channel",
1212
"extensions/console",
1313
"extensions/crypto",
1414
"extensions/fetch",
15+
"extensions/ffi",
1516
"extensions/http",
1617
"extensions/net",
1718
"extensions/timers",

cli/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ deno_console = { version = "0.13.0", path = "../extensions/console" }
2525
deno_core = { version = "0.95.0", path = "../core" }
2626
deno_crypto = { version = "0.27.0", path = "../extensions/crypto" }
2727
deno_fetch = { version = "0.36.0", path = "../extensions/fetch" }
28+
deno_ffi = { version = "0.1.0", path = "../extensions/ffi" }
2829
deno_http = { version = "0.4.0", path = "../extensions/http" }
2930
deno_net = { version = "0.4.0", path = "../extensions/net" }
3031
deno_timers = { version = "0.11.0", path = "../extensions/timers" }

cli/diagnostics.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ const UNSTABLE_DENO_PROPS: &[&str] = &[
5454
"listen",
5555
"listenDatagram",
5656
"loadavg",
57-
"openPlugin",
57+
"dlopen",
5858
"osRelease",
5959
"ppid",
6060
"resolveDns",

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

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2131,7 +2131,7 @@ declare namespace Deno {
21312131
| "write"
21322132
| "net"
21332133
| "env"
2134-
| "plugin"
2134+
| "ffi"
21352135
| "hrtime";
21362136

21372137
/** The current status of the permission. */
@@ -2167,8 +2167,8 @@ declare namespace Deno {
21672167
variable?: string;
21682168
}
21692169

2170-
export interface PluginPermissionDescriptor {
2171-
name: "plugin";
2170+
export interface FFIPermissionDescriptor {
2171+
name: "ffi";
21722172
}
21732173

21742174
export interface HrtimePermissionDescriptor {
@@ -2183,7 +2183,7 @@ declare namespace Deno {
21832183
| WritePermissionDescriptor
21842184
| NetPermissionDescriptor
21852185
| EnvPermissionDescriptor
2186-
| PluginPermissionDescriptor
2186+
| FFIPermissionDescriptor
21872187
| HrtimePermissionDescriptor;
21882188

21892189
export interface PermissionStatusEventMap {

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

Lines changed: 42 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -107,36 +107,44 @@ declare namespace Deno {
107107
swapFree: number;
108108
}
109109

110-
/** **UNSTABLE**: new API, yet to be vetted.
111-
*
112-
* Open and initialize a plugin.
113-
*
114-
* ```ts
115-
* import { assert } from "https://deno.land/std/testing/asserts.ts";
116-
* const rid = Deno.openPlugin("./path/to/some/plugin.so");
117-
*
118-
* // The Deno.core namespace is needed to interact with plugins, but this is
119-
* // internal so we use ts-ignore to skip type checking these calls.
120-
* // @ts-ignore
121-
* const { op_test_sync, op_test_async } = Deno.core.ops();
122-
*
123-
* assert(op_test_sync);
124-
* assert(op_test_async);
125-
*
126-
* // @ts-ignore
127-
* const result = Deno.core.opSync("op_test_sync");
128-
*
129-
* // @ts-ignore
130-
* const result = await Deno.core.opAsync("op_test_sync");
131-
* ```
132-
*
133-
* Requires `allow-plugin` permission.
110+
/** All possible types for interfacing with foreign functions */
111+
export type NativeType =
112+
| "void"
113+
| "u8"
114+
| "i8"
115+
| "u16"
116+
| "i16"
117+
| "u32"
118+
| "i32"
119+
| "u64"
120+
| "i64"
121+
| "usize"
122+
| "isize"
123+
| "f32"
124+
| "f64";
125+
126+
/** A foreign function as defined by its parameter and result types */
127+
export interface ForeignFunction {
128+
parameters: NativeType[];
129+
result: NativeType;
130+
}
131+
132+
/** A dynamic library resource */
133+
export interface DynamicLibrary<S extends Record<string, ForeignFunction>> {
134+
/** All of the registered symbols along with functions for calling them */
135+
symbols: { [K in keyof S]: (...args: unknown[]) => unknown };
136+
137+
close(): void;
138+
}
139+
140+
/** **UNSTABLE**: new API
134141
*
135-
* The plugin system is not stable and will change in the future, hence the
136-
* lack of docs. For now take a look at the example
137-
* https://github.com/denoland/deno/tree/main/test_plugin
142+
* Opens a dynamic library and registers symbols
138143
*/
139-
export function openPlugin(filename: string): number;
144+
export function dlopen<S extends Record<string, ForeignFunction>>(
145+
filename: string,
146+
symbols: S,
147+
): DynamicLibrary<S>;
140148

141149
/** The log category for a diagnostic message. */
142150
export enum DiagnosticCategory {
@@ -1043,14 +1051,14 @@ declare namespace Deno {
10431051
*/
10441052
net?: "inherit" | boolean | string[];
10451053

1046-
/** Specifies if the `plugin` permission should be requested or revoked.
1047-
* If set to `"inherit"`, the current `plugin` permission will be inherited.
1048-
* If set to `true`, the global `plugin` permission will be requested.
1049-
* If set to `false`, the global `plugin` permission will be revoked.
1054+
/** Specifies if the `ffi` permission should be requested or revoked.
1055+
* If set to `"inherit"`, the current `ffi` permission will be inherited.
1056+
* If set to `true`, the global `ffi` permission will be requested.
1057+
* If set to `false`, the global `ffi` permission will be revoked.
10501058
*
10511059
* Defaults to "inherit".
10521060
*/
1053-
plugin?: "inherit" | boolean;
1061+
ffi?: "inherit" | boolean;
10541062

10551063
/** Specifies if the `read` permission should be requested or revoked.
10561064
* If set to `"inherit"`, the current `read` permission will be inherited.
@@ -1137,7 +1145,7 @@ declare interface WorkerOptions {
11371145
* For example: `["https://deno.land", "localhost:8080"]`.
11381146
*/
11391147
net?: "inherit" | boolean | string[];
1140-
plugin?: "inherit" | boolean;
1148+
ffi?: "inherit" | boolean;
11411149
read?: "inherit" | boolean | Array<string | URL>;
11421150
run?: "inherit" | boolean | Array<string | URL>;
11431151
write?: "inherit" | boolean | Array<string | URL>;

0 commit comments

Comments
 (0)