Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(plugin_runner): support shared tokio runtime for wasix #107

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions Cargo.lock

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

10 changes: 10 additions & 0 deletions crates/swc/src/plugin.rs
Original file line number Diff line number Diff line change
Expand Up @@ -100,13 +100,23 @@ impl RustPlugins {
.expect("plugin module should be loaded");

let plugin_name = plugin_module_bytes.get_module_name().to_string();
let runtime = swc_plugin_runner::wasix_runtime::build_wasi_runtime(
crate::config::PLUGIN_MODULE_CACHE
.inner
.get()
.unwrap()
.lock()
.get_fs_cache_root()
.map(|v| std::path::PathBuf::from(v)),
);
let mut transform_plugin_executor =
swc_plugin_runner::create_plugin_transform_executor(
&self.source_map,
&self.unresolved_mark,
&self.metadata_context,
plugin_module_bytes,
Some(p.1),
runtime,
);

let span = tracing::span!(
Expand Down
3 changes: 3 additions & 0 deletions crates/swc_core/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -206,6 +206,9 @@ plugin_transform_host_native_filesystem_cache = [
"swc_plugin_runner/filesystem_cache",
]

plugin_transform_host_native_shared_runtime = [
"swc_plugin_runner/plugin_transform_host_native_shared_runtime",
]
### Internal features that public features are relying on.
### This is not supposed to be used directly, and does not gaurantee
### stability across each versions.
Expand Down
6 changes: 6 additions & 0 deletions crates/swc_plugin_runner/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,11 @@ plugin_transform_host_native = [
"wasmer-wasix/host-threads",
"wasmer-compiler-cranelift/default",
]
plugin_transform_host_native_shared_runtime = [
"tokio",
"wasmer-wasix/webc_runner",
]

# Supports a cache allow to store compiled bytecode into filesystem location.
# This feature implies in-memory cache support. This is not supported on wasm32 target.
filesystem_cache = ["wasmer-cache"]
Expand All @@ -51,6 +56,7 @@ once_cell = "1.10.0"
parking_lot = "0.12.0"
serde = { version = "1.0.126", features = ["derive"] }
serde_json = "1.0.64"
tokio = { version = "1", default-features = false, optional = true }
tracing = "0.1.32"
wasmer = { version = "3.3.0", default-features = false }
wasmer-wasix = { version = "0.4.0", default-features = false }
Expand Down
1 change: 1 addition & 0 deletions crates/swc_plugin_runner/benches/ecma_invoke.rs
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,7 @@ fn bench_transform(b: &mut Bencher, plugin_dir: &Path) {
)),
Box::new(plugin_module.clone()),
None,
None,
);

let experimental_metadata: VersionedSerializable<AHashMap<String, String>> =
Expand Down
11 changes: 11 additions & 0 deletions crates/swc_plugin_runner/src/cache.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@ const MODULE_SERIALIZATION_VERSION: &str = "v6";

#[derive(Default)]
pub struct PluginModuleCacheInner {
#[cfg(all(not(target_arch = "wasm32"), feature = "filesystem_cache"))]
fs_cache_root: Option<String>,
#[cfg(all(not(target_arch = "wasm32"), feature = "filesystem_cache"))]
fs_cache_store: Option<FileSystemCache>,
// Stores the string representation of the hash of the plugin module to store into
Expand All @@ -51,6 +53,13 @@ pub struct PluginModuleCacheInner {
}

impl PluginModuleCacheInner {
pub fn get_fs_cache_root(&self) -> Option<String> {
#[cfg(all(not(target_arch = "wasm32"), feature = "filesystem_cache"))]
return self.fs_cache_root.clone();

None
}

/// Check if the cache contains bytes for the corresponding key.
pub fn contains(&self, key: &str) -> bool {
let is_in_cache = self.memory_cache_store.contains_key(key)
Expand Down Expand Up @@ -183,6 +192,8 @@ impl PluginModuleCache {
fs_cache_store_root: &Option<String>,
) -> PluginModuleCacheInner {
PluginModuleCacheInner {
#[cfg(all(not(target_arch = "wasm32"), feature = "filesystem_cache"))]
fs_cache_root: fs_cache_store_root.clone(),
#[cfg(all(not(target_arch = "wasm32"), feature = "filesystem_cache"))]
fs_cache_store: if enable_fs_cache_store {
create_filesystem_cache(fs_cache_store_root)
Expand Down
4 changes: 4 additions & 0 deletions crates/swc_plugin_runner/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ mod imported_fn;
mod memory_interop;
pub mod plugin_module_bytes;
mod transform_executor;
pub mod wasix_runtime;

use plugin_module_bytes::PluginModuleBytes;

Expand All @@ -26,13 +27,15 @@ pub fn create_plugin_transform_executor(
metadata_context: &Arc<TransformPluginMetadataContext>,
plugin_module: Box<dyn PluginModuleBytes>,
plugin_config: Option<serde_json::Value>,
runtime: Option<Arc<dyn wasmer_wasix::WasiRuntime + Send + Sync>>,
) -> TransformExecutor {
TransformExecutor::new(
plugin_module,
source_map,
unresolved_mark,
metadata_context,
plugin_config,
runtime,
)
}

Expand All @@ -43,6 +46,7 @@ pub fn create_plugin_transform_executor(
metadata_context: &Arc<TransformPluginMetadataContext>,
plugin_module: Box<dyn PluginModuleBytes>,
plugin_config: Option<serde_json::Value>,
runtime: Option<()>,
) -> TransformExecutor {
unimplemented!("Transform plugin cannot be used without serialization support")
}
Loading