Skip to content

Commit

Permalink
feat(runtime): add a Cargo feature flag to enable the ffi API
Browse files Browse the repository at this point in the history
The `ffi` ext is difficult to cross compile (see denoland/rusty_v8#1640)
  • Loading branch information
lucasfernog-crabnebula committed Oct 23, 2024
1 parent 92ed4d3 commit 4631804
Show file tree
Hide file tree
Showing 8 changed files with 79 additions and 22 deletions.
29 changes: 25 additions & 4 deletions runtime/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ repository.workspace = true
description = "Provides the deno runtime library"

[features]
default = ["ffi"]
# "fake" feature that allows to generate docs on docs.rs
docsrs = []
# A feature that allows excluding `./js/99_main.js` from the exported extension.
Expand All @@ -27,6 +28,8 @@ hmr = ["include_js_files_for_snapshotting"]
# conditionally exclude the runtime source transpilation logic, and add an
# assertion that a snapshot is provided.
only_snapshotted_js_sources = ["include_js_files_for_snapshotting"]
# enable the FFI API
ffi = ["deno_ffi"]

[lints.rust]
unexpected_cfgs = { level = "warn", check-cfg = ['cfg(tokio_unstable)'] }
Expand All @@ -49,7 +52,7 @@ deno_core.workspace = true
deno_cron.workspace = true
deno_crypto.workspace = true
deno_fetch.workspace = true
deno_ffi.workspace = true
deno_ffi = { workspace = true, optional = true }
deno_fs = { workspace = true, features = ["sync_fs"] }
deno_http.workspace = true
deno_io.workspace = true
Expand Down Expand Up @@ -80,7 +83,7 @@ deno_core.workspace = true
deno_cron.workspace = true
deno_crypto.workspace = true
deno_fetch.workspace = true
deno_ffi.workspace = true
deno_ffi = { workspace = true, optional = true }
deno_fs = { workspace = true, features = ["sync_fs"] }
deno_http.workspace = true
deno_io.workspace = true
Expand Down Expand Up @@ -108,7 +111,13 @@ http.workspace = true
http-body-util.workspace = true
hyper.workspace = true
hyper-util.workspace = true
hyper_v014 = { workspace = true, features = ["server", "stream", "http1", "http2", "runtime"] }
hyper_v014 = { workspace = true, features = [
"server",
"stream",
"http1",
"http2",
"runtime",
] }
libc.workspace = true
log.workspace = true
netif = "0.1.6"
Expand All @@ -130,7 +139,19 @@ uuid.workspace = true
which.workspace = true

[target.'cfg(windows)'.dependencies]
winapi = { workspace = true, features = ["commapi", "knownfolders", "mswsock", "objbase", "psapi", "shlobj", "tlhelp32", "winbase", "winerror", "winuser", "winsock2"] }
winapi = { workspace = true, features = [
"commapi",
"knownfolders",
"mswsock",
"objbase",
"psapi",
"shlobj",
"tlhelp32",
"winbase",
"winerror",
"winuser",
"winsock2",
] }
ntapi = "0.4.0"
windows-sys.workspace = true

Expand Down
60 changes: 45 additions & 15 deletions runtime/errors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,12 +32,10 @@ use deno_crypto::GenerateKeyError;
use deno_crypto::ImportKeyError;
use deno_fetch::FetchError;
use deno_fetch::HttpClientCreateError;
use deno_ffi::CallError;
use deno_ffi::CallbackError;
use deno_ffi::DlfcnError;
use deno_ffi::IRError;
use deno_ffi::ReprError;
use deno_ffi::StaticError;
#[cfg(feature = "ffi")]
use deno_ffi::{
CallError, CallbackError, DlfcnError, IRError, ReprError, StaticError,
};
use deno_fs::FsOpsError;
use deno_http::HttpError;
use deno_http::HttpNextError;
Expand Down Expand Up @@ -504,6 +502,7 @@ fn get_web_blob_error_class(e: &BlobError) -> &'static str {
}
}

#[cfg(feature = "ffi")]
fn get_ffi_repr_error_class(e: &ReprError) -> &'static str {
match e {
ReprError::InvalidOffset => "TypeError",
Expand All @@ -527,6 +526,7 @@ fn get_ffi_repr_error_class(e: &ReprError) -> &'static str {
}
}

#[cfg(feature = "ffi")]
fn get_ffi_dlfcn_error_class(e: &DlfcnError) -> &'static str {
match e {
DlfcnError::RegisterSymbol { .. } => "Error",
Expand All @@ -536,6 +536,7 @@ fn get_ffi_dlfcn_error_class(e: &DlfcnError) -> &'static str {
}
}

#[cfg(feature = "ffi")]
fn get_ffi_static_error_class(e: &StaticError) -> &'static str {
match e {
StaticError::Dlfcn(e) => get_ffi_dlfcn_error_class(e),
Expand All @@ -545,6 +546,7 @@ fn get_ffi_static_error_class(e: &StaticError) -> &'static str {
}
}

#[cfg(feature = "ffi")]
fn get_ffi_callback_error_class(e: &CallbackError) -> &'static str {
match e {
CallbackError::Resource(e) => get_error_class_name(e).unwrap_or("Error"),
Expand All @@ -553,6 +555,7 @@ fn get_ffi_callback_error_class(e: &CallbackError) -> &'static str {
}
}

#[cfg(feature = "ffi")]
fn get_ffi_call_error_class(e: &CallError) -> &'static str {
match e {
CallError::IR(_) => "TypeError",
Expand Down Expand Up @@ -1021,8 +1024,18 @@ pub fn get_error_class_name(e: &AnyError) -> Option<&'static str> {
.map(get_web_stream_resource_error_class)
})
.or_else(|| e.downcast_ref::<BlobError>().map(get_web_blob_error_class))
.or_else(|| e.downcast_ref::<IRError>().map(|_| "TypeError"))
.or_else(|| e.downcast_ref::<ReprError>().map(get_ffi_repr_error_class))
.or_else(|| {
#[cfg(feature = "ffi")]
return e.downcast_ref::<IRError>().map(|_| "TypeError");
#[cfg(not(feature = "ffi"))]
None
})
.or_else(|| {
#[cfg(feature = "ffi")]
return e.downcast_ref::<ReprError>().map(get_ffi_repr_error_class);
#[cfg(not(feature = "ffi"))]
None
})
.or_else(|| e.downcast_ref::<HttpError>().map(get_http_error))
.or_else(|| e.downcast_ref::<HttpNextError>().map(get_http_next_error))
.or_else(|| {
Expand All @@ -1031,18 +1044,35 @@ pub fn get_error_class_name(e: &AnyError) -> Option<&'static str> {
})
.or_else(|| e.downcast_ref::<FsOpsError>().map(get_fs_error))
.or_else(|| {
e.downcast_ref::<DlfcnError>()
.map(get_ffi_dlfcn_error_class)
#[cfg(feature = "ffi")]
return e
.downcast_ref::<DlfcnError>()
.map(get_ffi_dlfcn_error_class);
#[cfg(not(feature = "ffi"))]
None
})
.or_else(|| {
#[cfg(feature = "ffi")]
return e
.downcast_ref::<StaticError>()
.map(get_ffi_static_error_class);
#[cfg(not(feature = "ffi"))]
None
})
.or_else(|| {
e.downcast_ref::<StaticError>()
.map(get_ffi_static_error_class)
#[cfg(feature = "ffi")]
return e
.downcast_ref::<CallbackError>()
.map(get_ffi_callback_error_class);
#[cfg(not(feature = "ffi"))]
None
})
.or_else(|| {
e.downcast_ref::<CallbackError>()
.map(get_ffi_callback_error_class)
#[cfg(feature = "ffi")]
return e.downcast_ref::<CallError>().map(get_ffi_call_error_class);
#[cfg(not(feature = "ffi"))]
None
})
.or_else(|| e.downcast_ref::<CallError>().map(get_ffi_call_error_class))
.or_else(|| e.downcast_ref::<TlsError>().map(get_tls_error_class))
.or_else(|| e.downcast_ref::<CronError>().map(get_cron_error_class))
.or_else(|| e.downcast_ref::<CanvasError>().map(get_canvas_error))
Expand Down
5 changes: 3 additions & 2 deletions runtime/js/90_deno_ns.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ import {
import * as timers from "ext:deno_web/02_timers.js";
import * as httpClient from "ext:deno_fetch/22_http_client.js";
import * as console from "ext:deno_console/01_console.js";
import * as ffi from "ext:deno_ffi/00_ffi.js";
import * as net from "ext:deno_net/01_net.js";
import * as tls from "ext:deno_net/02_tls.js";
import * as serve from "ext:deno_http/00_serve.ts";
Expand All @@ -30,6 +29,8 @@ import * as kv from "ext:deno_kv/01_db.ts";
import * as cron from "ext:deno_cron/01_cron.ts";
import * as webgpuSurface from "ext:deno_webgpu/02_surface.js";

const ffi = await import("ext:deno_ffi/00_ffi.js").catch(() => ({}));

const denoNs = {
Process: process.Process,
run: process.run,
Expand Down Expand Up @@ -169,7 +170,7 @@ denoNsUnstableById[unstableIds.kv] = {
denoNsUnstableById[unstableIds.net] = {
listenDatagram: net.createListenDatagram(
op_net_listen_udp,
op_net_listen_unixpacket,
op_net_listen_unixpacket
),
};

Expand Down
2 changes: 2 additions & 0 deletions runtime/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ pub use deno_core;
pub use deno_cron;
pub use deno_crypto;
pub use deno_fetch;
#[cfg(feature = "ffi")]
pub use deno_ffi;
pub use deno_fs;
pub use deno_http;
Expand Down Expand Up @@ -69,6 +70,7 @@ pub static UNSTABLE_GRANULAR_FLAGS: &[UnstableGranularFlag] = &[
show_in_help: true,
id: 2,
},
#[cfg(feature = "ffi")]
UnstableGranularFlag {
name: deno_ffi::UNSTABLE_FEATURE_NAME,
help_text: "Enable unstable FFI APIs",
Expand Down
1 change: 0 additions & 1 deletion runtime/shared.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@ extension!(runtime,
deno_crypto,
deno_broadcast_channel,
deno_node,
deno_ffi,
deno_net,
deno_napi,
deno_http,
Expand Down
2 changes: 2 additions & 0 deletions runtime/snapshot.rs
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ impl deno_fetch::FetchPermissions for Permissions {
}
}

#[cfg(feature = "ffi")]
impl deno_ffi::FfiPermissions for Permissions {
fn check_partial_no_path(
&mut self,
Expand Down Expand Up @@ -278,6 +279,7 @@ pub fn create_runtime_snapshot(
deno_broadcast_channel::deno_broadcast_channel::init_ops_and_esm(
deno_broadcast_channel::InMemoryBroadcastChannel::default(),
),
#[cfg(feature = "ffi")]
deno_ffi::deno_ffi::init_ops_and_esm::<Permissions>(),
deno_net::deno_net::init_ops_and_esm::<Permissions>(None, None),
deno_tls::deno_tls::init_ops_and_esm(),
Expand Down
1 change: 1 addition & 0 deletions runtime/web_worker.rs
Original file line number Diff line number Diff line change
Expand Up @@ -464,6 +464,7 @@ impl WebWorker {
deno_broadcast_channel::deno_broadcast_channel::init_ops_and_esm(
services.broadcast_channel,
),
#[cfg(feature = "ffi")]
deno_ffi::deno_ffi::init_ops_and_esm::<PermissionsContainer>(),
deno_net::deno_net::init_ops_and_esm::<PermissionsContainer>(
services.root_cert_store_provider.clone(),
Expand Down
1 change: 1 addition & 0 deletions runtime/worker.rs
Original file line number Diff line number Diff line change
Expand Up @@ -379,6 +379,7 @@ impl MainWorker {
deno_broadcast_channel::deno_broadcast_channel::init_ops_and_esm(
services.broadcast_channel.clone(),
),
#[cfg(feature = "ffi")]
deno_ffi::deno_ffi::init_ops_and_esm::<PermissionsContainer>(),
deno_net::deno_net::init_ops_and_esm::<PermissionsContainer>(
services.root_cert_store_provider.clone(),
Expand Down

0 comments on commit 4631804

Please sign in to comment.