Skip to content

Commit

Permalink
linera-execution::wasm::wasmer: set up dependencies for the Web
Browse files Browse the repository at this point in the history
  • Loading branch information
Twey committed May 15, 2024
1 parent 41ace1b commit ec7e24f
Show file tree
Hide file tree
Showing 6 changed files with 36 additions and 14 deletions.
1 change: 1 addition & 0 deletions Cargo.lock

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

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,7 @@ wasm-bindgen = "0.2.92"
wasm-bindgen-test = "0.3.42"
wasm-encoder = "0.24.1"
wasm-instrument = "0.4.0"
wasmer = { version = "4.3.0-alpha.1", features = ["singlepass"] }
wasmer = { version = "4.3.0-alpha.1", default-features = false }
wasmer-compiler-singlepass = "4.3.0-alpha.1"
wasmparser = "0.101.1"
wasmtime = "1.0"
Expand Down
3 changes: 2 additions & 1 deletion linera-execution/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ wasmtime = [
"wasm-encoder",
"wasmparser",
]
web = ["linera-base/web", "linera-views/web"]
web = ["linera-base/web", "linera-views/web", "wasmer?/js-default"]

[dependencies]
anyhow.workspace = true
Expand Down Expand Up @@ -65,6 +65,7 @@ wasmtime = { workspace = true, optional = true }

[target.'cfg(not(target_arch = "wasm32"))'.dependencies]
tokio = { workspace = true, features = ["rt-multi-thread"] }
wasmer = { workspace = true, features = ["sys-default", "singlepass"] }

[target.'cfg(target_arch = "wasm32")'.dependencies]
tokio = { workspace = true, features = ["rt"] }
Expand Down
4 changes: 3 additions & 1 deletion linera-execution/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,13 @@

fn main() {
cfg_aliases::cfg_aliases! {
web: { all(target_arch = "wasm32", feature = "web") },

with_fs: { all(not(target_arch = "wasm32"), feature = "fs") },
with_metrics: { all(not(target_arch = "wasm32"), feature = "metrics") },
with_testing: { any(test, feature = "test") },
with_tokio_multi_thread: { not(target_arch = "wasm32") },
with_wasmer: { all(not(target_arch = "wasm32"), feature = "wasmer") },
with_wasmer: { feature = "wasmer" },
with_wasmtime: { all(not(target_arch = "wasm32"), feature = "wasmtime") },

// If you change this, don't forget to update `WasmRuntime` and
Expand Down
36 changes: 27 additions & 9 deletions linera-execution/src/wasm/wasmer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ use linera_witty::{
};
use tokio::sync::Mutex;
use wasm_instrument::{gas_metering, parity_wasm};
use wasmer::{sys::EngineBuilder, Cranelift, Engine, Module, Singlepass, Store};
use wasmer::{Engine, Module, Store};

use super::{
module_cache::ModuleCache,
Expand All @@ -26,10 +26,24 @@ use crate::{
QueryContext, ServiceRuntime,
};

#[cfg(not(web))]
use {
wasmer::sys::EngineBuilder,
wasmer::Cranelift,
wasmer::Singlepass,
};

/// An [`Engine`] instance configured to run application services.
static SERVICE_ENGINE: Lazy<Engine> = Lazy::new(|| {
let compiler_config = Cranelift::new();
EngineBuilder::new(compiler_config).into()
#[cfg(web)]
{
wasmer::Engine::default()
}

#[cfg(not(web))]
{
wasmer::sys::EngineBuilder::new(wasmer::Cranelift::new()).into()
}
});

/// A cache of compiled contract modules, with their respective [`Engine`] instances.
Expand Down Expand Up @@ -253,17 +267,21 @@ impl CachedContractModule {

/// Creates a new [`Engine`] to compile a contract bytecode.
fn create_compilation_engine() -> Engine {
let mut compiler_config = Singlepass::default();
compiler_config.canonicalize_nans(true);
#[cfg(not(web))]
{
let mut compiler_config = wasmer::Singlepass::default();
compiler_config.canonicalize_nans(true);

wasmer::sys::EngineBuilder::new(compiler_config).into()
}

EngineBuilder::new(compiler_config).into()
#[cfg(web)]
wasmer::Engine::default()
}

/// Creates a [`Module`] from a compiled contract using a headless [`Engine`].
pub fn create_execution_instance(&self) -> Result<(Engine, Module), anyhow::Error> {
use wasmer::NativeEngineExt;

let engine = Engine::headless();
let engine = Engine::default();
let store = Store::new(engine.clone());
let module = unsafe { Module::deserialize(&store, &*self.compiled_bytecode) }?;
Ok((engine, module))
Expand Down
4 changes: 2 additions & 2 deletions linera-witty/src/runtime/wasmer/function.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
//! Implementations of [`InstanceWithFunction`] for Wasmer instances.
use frunk::{hlist_pat, HList};
use wasmer::{AsStoreRef, Extern, FromToNativeWasmType, TypedFunction};
use wasmer::{AsStoreRef, Extern, FromToNativeWasmType, NativeWasmTypeInto, TypedFunction};

use super::{
parameters::WasmerParameters, results::WasmerResults, EntrypointInstance, ReentrantInstance,
Expand All @@ -30,7 +30,7 @@ macro_rules! impl_instance_with_function_for {
impl<$( $types, )* Results, UserData> InstanceWithFunction<HList![$( $types ),*], Results>
for $instance
where
$( $types: FlatType + FromToNativeWasmType, )*
$( $types: FlatType + FromToNativeWasmType + NativeWasmTypeInto, )*
Results: FlatLayout + WasmerResults,
UserData: Send + 'static,
{
Expand Down

0 comments on commit ec7e24f

Please sign in to comment.