Merge Backend into EngineBuilder and refactor feature flags

This commit is contained in:
Manos Pitsidianakis
2022-07-27 15:50:30 +03:00
parent a1e82a501e
commit 214346af3b
69 changed files with 363 additions and 313 deletions

View File

@@ -11,7 +11,7 @@ autoexamples = false
[dependencies]
wasmer = { version = "=2.3.0", path = "lib/api", default-features = false }
wasmer-compiler = { version = "=2.3.0", path = "lib/compiler", features = ["engine_compilation"] }
wasmer-compiler = { version = "=2.3.0", path = "lib/compiler", features = ["compiler"] }
wasmer-compiler-cranelift = { version = "=2.3.0", path = "lib/compiler-cranelift", optional = true }
wasmer-compiler-singlepass = { version = "=2.3.0", path = "lib/compiler-singlepass", optional = true }
wasmer-compiler-llvm = { version = "=2.3.0", path = "lib/compiler-llvm", optional = true }
@@ -64,7 +64,7 @@ glob = "0.3"
rustc_version = "0.4"
[dev-dependencies]
wasmer = { version = "=2.3.0", path = "lib/api", default-features = false, features = ["default-cranelift"] }
wasmer = { version = "=2.3.0", path = "lib/api", default-features = false, features = ["cranelift"] }
anyhow = "1.0"
criterion = "0.3"
lazy_static = "1.4"

View File

@@ -359,7 +359,7 @@ check-wasmer-wasm:
check-capi: capi-setup
RUSTFLAGS="${RUSTFLAGS}" $(CARGO_BINARY) check $(CARGO_TARGET) --manifest-path lib/c-api/Cargo.toml \
--no-default-features --features wat,compilation,wasi,middlewares $(capi_compiler_features)
--no-default-features --features wat,compiler,wasi,middlewares $(capi_compiler_features)
build-wasmer:
$(CARGO_BINARY) build $(CARGO_TARGET) --release --manifest-path lib/cli/Cargo.toml $(compiler_features) --bin wasmer
@@ -421,50 +421,50 @@ build-docs-capi: capi-setup
# when generating the documentation, we rename it to its
# crate's name. Then we restore the lib's name.
sed "$(SEDI)" -e 's/name = "wasmer" # ##lib.name##/name = "wasmer_c_api" # ##lib.name##/' lib/c-api/Cargo.toml
RUSTFLAGS="${RUSTFLAGS}" $(CARGO_BINARY) doc $(CARGO_TARGET) --manifest-path lib/c-api/Cargo.toml --no-deps --features wat,compilation,cranelift,wasi
RUSTFLAGS="${RUSTFLAGS}" $(CARGO_BINARY) doc $(CARGO_TARGET) --manifest-path lib/c-api/Cargo.toml --no-deps --features wat,compiler,cranelift,wasi
sed "$(SEDI)" -e 's/name = "wasmer_c_api" # ##lib.name##/name = "wasmer" # ##lib.name##/' lib/c-api/Cargo.toml
build-capi: capi-setup
RUSTFLAGS="${RUSTFLAGS}" $(CARGO_BINARY) build $(CARGO_TARGET) --manifest-path lib/c-api/Cargo.toml --release \
--no-default-features --features wat,compilation,wasi,middlewares $(capi_compiler_features)
--no-default-features --features wat,compiler,wasi,middlewares $(capi_compiler_features)
build-capi-singlepass: capi-setup
RUSTFLAGS="${RUSTFLAGS}" $(CARGO_BINARY) build $(CARGO_TARGET) --manifest-path lib/c-api/Cargo.toml --release \
--no-default-features --features wat,compilation,singlepass,wasi,middlewares
--no-default-features --features wat,compiler,singlepass,wasi,middlewares
build-capi-singlepass-universal: capi-setup
RUSTFLAGS="${RUSTFLAGS}" $(CARGO_BINARY) build $(CARGO_TARGET) --manifest-path lib/c-api/Cargo.toml --release \
--no-default-features --features wat,compilation,singlepass,wasi,middlewares
--no-default-features --features wat,compiler,singlepass,wasi,middlewares
build-capi-cranelift: capi-setup
RUSTFLAGS="${RUSTFLAGS}" $(CARGO_BINARY) build $(CARGO_TARGET) --manifest-path lib/c-api/Cargo.toml --release \
--no-default-features --features wat,compilation,cranelift,wasi,middlewares
--no-default-features --features wat,compiler,cranelift,wasi,middlewares
build-capi-cranelift-universal: capi-setup
RUSTFLAGS="${RUSTFLAGS}" $(CARGO_BINARY) build $(CARGO_TARGET) --manifest-path lib/c-api/Cargo.toml --release \
--no-default-features --features wat,compilation,cranelift,wasi,middlewares
--no-default-features --features wat,compiler,cranelift,wasi,middlewares
build-capi-llvm: capi-setup
RUSTFLAGS="${RUSTFLAGS}" $(CARGO_BINARY) build $(CARGO_TARGET) --manifest-path lib/c-api/Cargo.toml --release \
--no-default-features --features wat,compilation,llvm,wasi,middlewares
--no-default-features --features wat,compiler,llvm,wasi,middlewares
build-capi-llvm-universal: capi-setup
RUSTFLAGS="${RUSTFLAGS}" $(CARGO_BINARY) build $(CARGO_TARGET) --manifest-path lib/c-api/Cargo.toml --release \
--no-default-features --features wat,compilation,llvm,wasi,middlewares
--no-default-features --features wat,compiler,llvm,wasi,middlewares
# Headless (we include the minimal to be able to run)
build-capi-headless-universal: capi-setup
RUSTFLAGS="${RUSTFLAGS}" $(CARGO_BINARY) build $(CARGO_TARGET) --manifest-path lib/c-api/Cargo.toml --release \
--no-default-features --features compilation,wasi
--no-default-features --features compiler-headless,wasi
build-capi-headless-all: capi-setup
RUSTFLAGS="${RUSTFLAGS}" $(CARGO_BINARY) build $(CARGO_TARGET) --manifest-path lib/c-api/Cargo.toml --release \
--no-default-features --features compilation,wasi
--no-default-features --features compiler-headless,wasi
build-capi-headless-ios: capi-setup
RUSTFLAGS="${RUSTFLAGS}" cargo lipo --manifest-path lib/c-api/Cargo.toml --release \
--no-default-features --features wasi
--no-default-features --features compiler-headless,wasi
#####
#
@@ -520,7 +520,7 @@ test-capi: build-capi package-capi $(foreach compiler_engine,$(capi_compilers_en
test-capi-crate-%:
WASMER_CAPI_CONFIG=$(shell echo $@ | sed -e s/test-capi-crate-//) $(CARGO_BINARY) test $(CARGO_TARGET) --manifest-path lib/c-api/Cargo.toml --release \
--no-default-features --features wat,compilation,wasi,middlewares $(capi_compiler_features) -- --nocapture
--no-default-features --features wat,compiler,wasi,middlewares $(capi_compiler_features) -- --nocapture
test-capi-integration-%:
# Test the Wasmer C API tests for C
@@ -698,7 +698,7 @@ update-testsuite:
lint-packages: RUSTFLAGS += -D dead-code -D nonstandard-style -D unused-imports -D unused-mut -D unused-variables -D unused-unsafe -D unreachable-patterns -D bad-style -D improper-ctypes -D unused-allocation -D unused-comparisons -D while-true -D unconditional-recursion -D bare-trait-objects -D function_item_references # TODO: add `-D missing-docs`
lint-packages:
RUSTFLAGS="${RUSTFLAGS}" cargo clippy --all -- -D clippy::all
RUSTFLAGS="${RUSTFLAGS}" cargo clippy --all --exclude wasmer-cli -- -D clippy::all
RUSTFLAGS="${RUSTFLAGS}" cargo clippy --manifest-path lib/cli/Cargo.toml $(compiler_features) -- -D clippy::all
RUSTFLAGS="${RUSTFLAGS}" cargo clippy --manifest-path fuzz/Cargo.toml $(compiler_features) -- -D clippy::all

View File

@@ -149,15 +149,21 @@ pub fn run_basic_dynamic_function(store: &Store, compiler_name: &str, c: &mut Cr
fn run_static_benchmarks(_c: &mut Criterion) {
#[cfg(feature = "llvm")]
{
let mut store =
Store::new_with_engine(&Backend::new(wasmer_compiler_llvm::LLVM::new()).engine());
let mut store = Store::new_with_engine(
&EngineBuilder::new(Some(wasmer_compiler_llvm::LLVM::new()), None, None).engine(),
);
run_basic_static_function(&store, "llvm", c);
}
#[cfg(feature = "cranelift")]
{
let mut store = Store::new_with_engine(
&Backend::new(wasmer_compiler_cranelift::Cranelift::new()).engine(),
&EngineBuilder::new(
Some(wasmer_compiler_cranelift::Cranelift::new()),
None,
None,
)
.engine(),
);
run_basic_static_function(&store, "cranelift", c);
}
@@ -165,7 +171,12 @@ fn run_static_benchmarks(_c: &mut Criterion) {
#[cfg(feature = "singlepass")]
{
let mut store = Store::new_with_engine(
&Backend::new(wasmer_compiler_singlepass::Singlepass::new()).engine(),
&EngineBuilder::new(
Some(wasmer_compiler_singlepass::Singlepass::new()),
None,
None,
)
.engine(),
);
run_basic_static_function(&store, "singlepass", c);
}
@@ -174,15 +185,21 @@ fn run_static_benchmarks(_c: &mut Criterion) {
fn run_dynamic_benchmarks(_c: &mut Criterion) {
#[cfg(feature = "llvm")]
{
let mut store =
Store::new_with_engine(&Backend::new(wasmer_compiler_llvm::LLVM::new()).engine());
let mut store = Store::new_with_engine(
&EngineBuilder::new(Some(wasmer_compiler_llvm::LLVM::new()), None, None).engine(),
);
run_basic_dynamic_function(&store, "llvm", c);
}
#[cfg(feature = "cranelift")]
{
let mut store = Store::new_with_engine(
&Backend::new(wasmer_compiler_cranelift::Cranelift::new()).engine(),
&EngineBuilder::new(
Some(wasmer_compiler_cranelift::Cranelift::new()),
None,
None,
)
.engine(),
);
run_basic_dynamic_function(&store, "cranelift", c);
}
@@ -190,7 +207,12 @@ fn run_dynamic_benchmarks(_c: &mut Criterion) {
#[cfg(feature = "singlepass")]
{
let mut store = Store::new_with_engine(
&Backend::new(wasmer_compiler_singlepass::Singlepass::new()).engine(),
&EngineBuilder::new(
Some(wasmer_compiler_singlepass::Singlepass::new()),
None,
None,
)
.engine(),
);
run_basic_dynamic_function(&store, "singlepass", c);
}

View File

@@ -130,7 +130,7 @@ let wasm_bytes = wat2wasm(
)?;
let compiler_config = Cranelift::default();
let engine = Backend::new(compiler_config).engine();
let engine = Universal::new(compiler_config).engine();
let mut store = Store::new(&engine);
let module = Module::new(&store, wasm_bytes)?;
let instance = Instance::new(&module, &imports! {})?;

View File

@@ -11,7 +11,7 @@
//! Ready?
use wasmer::{imports, wat2wasm, Instance, Module, Store, Value};
use wasmer_compiler::Backend;
use wasmer_compiler::EngineBuilder;
use wasmer_compiler_cranelift::Cranelift;
fn main() -> Result<(), Box<dyn std::error::Error>> {
@@ -33,7 +33,7 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {
let compiler = Cranelift::default();
// Create the store
let mut store = Store::new_with_engine(&Backend::new(compiler).engine());
let mut store = Store::new_with_engine(&EngineBuilder::new(compiler, None, None).engine());
println!("Compiling module...");
// Let's compile the Wasm module.

View File

@@ -11,7 +11,7 @@
//! Ready?
use wasmer::{imports, wat2wasm, Instance, Module, Store, Value};
use wasmer_compiler::Backend;
use wasmer_compiler::EngineBuilder;
use wasmer_compiler_llvm::LLVM;
fn main() -> Result<(), Box<dyn std::error::Error>> {
@@ -33,7 +33,7 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {
let compiler = LLVM::default();
// Create the store
let mut store = Store::new_with_engine(&Backend::new(compiler).engine());
let mut store = Store::new_with_engine(&EngineBuilder::new(compiler, None, None).engine());
println!("Compiling module...");
// Let's compile the Wasm module.

View File

@@ -11,7 +11,7 @@
//! Ready?
use wasmer::{imports, wat2wasm, Instance, Module, Store, Value};
use wasmer_compiler::Backend;
use wasmer_compiler::EngineBuilder;
use wasmer_compiler_singlepass::Singlepass;
fn main() -> Result<(), Box<dyn std::error::Error>> {
@@ -33,7 +33,7 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {
let compiler = Singlepass::default();
// Create the store
let mut store = Store::new_with_engine(&Backend::new(compiler).engine());
let mut store = Store::new_with_engine(&EngineBuilder::new(compiler, None, None).engine());
println!("Compiling module...");
// Let's compile the Wasm module.

View File

@@ -20,7 +20,7 @@ use wasmer::{
imports, wat2wasm, Function, FunctionEnv, FunctionEnvMut, Instance, Module, Store,
TypedFunction,
};
use wasmer_compiler::Backend;
use wasmer_compiler::EngineBuilder;
use wasmer_compiler_cranelift::Cranelift;
// First we need to create an error type that we'll use to signal the end of execution.
@@ -58,7 +58,8 @@ fn main() -> anyhow::Result<()> {
// Note that we don't need to specify the engine/compiler if we want to use
// the default provided by Wasmer.
// You can use `Store::default()` for that.
let mut store = Store::new_with_engine(&Backend::new(Cranelift::default()).engine());
let mut store =
Store::new_with_engine(&EngineBuilder::new(Cranelift::default(), None, None).engine());
let env = FunctionEnv::new(&mut store, ());
println!("Compiling module...");

View File

@@ -19,7 +19,7 @@
//! Ready?
use wasmer::{imports, wat2wasm, Instance, Module, Store, Value};
use wasmer_compiler::Backend;
use wasmer_compiler::EngineBuilder;
use wasmer_compiler_cranelift::Cranelift;
fn main() -> Result<(), Box<dyn std::error::Error>> {
@@ -49,7 +49,7 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {
//
// In this case, the engine is `wasmer_compiler` which roughly
// means that the executable code will live in memory.
let engine = Backend::new(compiler_config).engine();
let engine = EngineBuilder::new(compiler_config, None, None).engine();
// Create a store, that holds the engine.
let mut store = Store::new_with_engine(&engine);

View File

@@ -20,7 +20,7 @@
use std::str::FromStr;
use wasmer::{wat2wasm, Module, RuntimeError, Store};
use wasmer_compiler::Backend;
use wasmer_compiler::EngineBuilder;
use wasmer_compiler_cranelift::Cranelift;
use wasmer_types::{CpuFeature, Target, Triple};
@@ -70,13 +70,7 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {
// That's where we specify the target for the compiler.
//
// Use the Universal engine.
let engine = Backend::new(compiler_config)
// Here we go.
// Pass the target to the engine! The engine will share
// this information with the compiler.
.target(target)
// Get the engine.
.engine();
let mut engine = EngineBuilder::new(compiler_config, Some(target), None).engine();
// Create a store, that holds the engine.
let mut store = Store::new_with_engine(&engine);

View File

@@ -52,7 +52,7 @@ use wasmer::Instance;
use wasmer::Module;
use wasmer::Store;
use wasmer::Value;
use wasmer_compiler::Backend;
use wasmer_compiler::EngineBuilder;
use wasmer_compiler_cranelift::Cranelift;
fn main() -> Result<(), Box<dyn std::error::Error>> {
@@ -82,7 +82,7 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {
println!("Creating univesral engine...");
// Define the engine that will drive everything.
let engine = Backend::new(compiler_config).engine();
let engine = EngineBuilder::new(compiler_config, None, None).engine();
// Create a store, that holds the engine.
let mut store = Store::new_with_engine(&engine);
@@ -105,7 +105,7 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {
{
println!("Creating headless Universal engine...");
// We create a headless Universal engine.
let engine = Backend::headless().engine();
let engine = EngineBuilder::headless().engine();
let mut store = Store::new_with_engine(&engine);
let mut env = FunctionEnv::new(&mut store, ());

View File

@@ -14,7 +14,7 @@
//! Ready?
use wasmer::{imports, wat2wasm, FunctionEnv, Instance, Module, Store, TypedFunction};
use wasmer_compiler::Backend;
use wasmer_compiler::EngineBuilder;
use wasmer_compiler_cranelift::Cranelift;
fn main() -> Result<(), Box<dyn std::error::Error>> {
@@ -39,7 +39,8 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {
// Note that we don't need to specify the engine/compiler if we want to use
// the default provided by Wasmer.
// You can use `Store::default()` for that.
let mut store = Store::new_with_engine(&Backend::new(Cranelift::default()).engine());
let mut store =
Store::new_with_engine(&EngineBuilder::new(Cranelift::default(), None, None).engine());
let mut env = FunctionEnv::new(&mut store, ());
println!("Compiling module...");

View File

@@ -18,7 +18,7 @@
//! Ready?
use wasmer::{imports, wat2wasm, FunctionEnv, Instance, Module, Store, TypedFunction, Value};
use wasmer_compiler::Backend;
use wasmer_compiler::EngineBuilder;
use wasmer_compiler_cranelift::Cranelift;
fn main() -> Result<(), Box<dyn std::error::Error>> {
@@ -40,7 +40,8 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {
// Note that we don't need to specify the engine/compiler if we want to use
// the default provided by Wasmer.
// You can use `Store::default()` for that.
let mut store = Store::new_with_engine(&Backend::new(Cranelift::default()).engine());
let mut store =
Store::new_with_engine(&EngineBuilder::new(Cranelift::default(), None, None).engine());
let mut env = FunctionEnv::new(&mut store, ());
println!("Compiling module...");

View File

@@ -18,7 +18,7 @@
use wasmer::{
imports, wat2wasm, FunctionEnv, Instance, Module, Mutability, Store, Type, TypedFunction, Value,
};
use wasmer_compiler::Backend;
use wasmer_compiler::EngineBuilder;
use wasmer_compiler_cranelift::Cranelift;
fn main() -> Result<(), Box<dyn std::error::Error>> {
@@ -40,7 +40,8 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {
// Note that we don't need to specify the engine/compiler if we want to use
// the default provided by Wasmer.
// You can use `Store::default()` for that.
let mut store = Store::new_with_engine(&Backend::new(Cranelift::default()).engine());
let mut store =
Store::new_with_engine(&EngineBuilder::new(Cranelift::default(), None, None).engine());
let mut env = FunctionEnv::new(&mut store, ());
println!("Compiling module...");

View File

@@ -12,7 +12,7 @@
//! Ready?
use wasmer::{imports, wat2wasm, FunctionEnv, Instance, Module, Store, TypedFunction, WasmPtr};
use wasmer_compiler::Backend;
use wasmer_compiler::EngineBuilder;
use wasmer_compiler_cranelift::Cranelift;
fn main() -> Result<(), Box<dyn std::error::Error>> {
@@ -37,7 +37,8 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {
// Note that we don't need to specify the engine/compiler if we want to use
// the default provided by Wasmer.
// You can use `Store::default()` for that.
let mut store = Store::new_with_engine(&Backend::new(Cranelift::default()).engine());
let mut store =
Store::new_with_engine(&EngineBuilder::new(Cranelift::default(), None, None).engine());
let mut env = FunctionEnv::new(&mut store, ());
println!("Compiling module...");

View File

@@ -11,7 +11,7 @@
//! Ready?
use wasmer::{imports, wat2wasm, Features, FunctionEnv, Instance, Module, Store, Value};
use wasmer_compiler::Backend;
use wasmer_compiler::EngineBuilder;
use wasmer_compiler_cranelift::Cranelift;
fn main() -> anyhow::Result<()> {
@@ -36,7 +36,7 @@ fn main() -> anyhow::Result<()> {
features.multi_value(true);
// Set up the engine. That's where we define the features!
let engine = Backend::new(compiler).features(features);
let engine = EngineBuilder::new(compiler, None, Some(features));
// Now, let's define the store, and compile the module.
let mut store = Store::new_with_engine(&engine.engine());

View File

@@ -10,7 +10,7 @@ use wasmer::{
imports, wat2wasm, Function, FunctionEnv, FunctionEnvMut, Instance, Module, Store,
TypedFunction,
};
use wasmer_compiler::Backend;
use wasmer_compiler::EngineBuilder;
use wasmer_compiler_cranelift::Cranelift;
fn main() -> anyhow::Result<()> {
@@ -47,7 +47,8 @@ fn main() -> anyhow::Result<()> {
// However for the purposes of showing what's happening, we create a compiler
// (`Cranelift`) and pass it to an engine (`Universal`). We then pass the engine to
// the store and are now ready to compile and run WebAssembly!
let mut store = Store::new_with_engine(&Backend::new(Cranelift::default()).engine());
let mut store =
Store::new_with_engine(&EngineBuilder::new(Cranelift::default(), None, None).engine());
// We then use our store and Wasm bytes to compile a `Module`.
// A `Module` is a compiled WebAssembly module that isn't ready to execute yet.

View File

@@ -19,7 +19,7 @@ use wasmer::{
imports, wat2wasm, Function, FunctionEnv, FunctionType, Global, Instance, Memory, Module,
Store, Table, Type, Value,
};
use wasmer_compiler::Backend;
use wasmer_compiler::EngineBuilder;
use wasmer_compiler_cranelift::Cranelift;
fn main() -> Result<(), Box<dyn std::error::Error>> {
@@ -44,7 +44,8 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {
// Note that we don't need to specify the engine/compiler if we want to use
// the default provided by Wasmer.
// You can use `Store::default()` for that.
let mut store = Store::new_with_engine(&Backend::new(Cranelift::default()).engine());
let mut store =
Store::new_with_engine(&EngineBuilder::new(Cranelift::default(), None, None).engine());
let mut env = FunctionEnv::new(&mut store, ());
println!("Compiling module...");

View File

@@ -21,7 +21,7 @@ use wasmer::{
imports, wat2wasm, Function, FunctionEnv, FunctionEnvMut, FunctionType, Instance, Module,
Store, Type, TypedFunction, Value,
};
use wasmer_compiler::Backend;
use wasmer_compiler::EngineBuilder;
use wasmer_compiler_cranelift::Cranelift;
fn main() -> Result<(), Box<dyn std::error::Error>> {
@@ -45,7 +45,8 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {
// Note that we don't need to specify the engine/compiler if we want to use
// the default provided by Wasmer.
// You can use `Store::default()` for that.
let mut store = Store::new_with_engine(&Backend::new(Cranelift::default()).engine());
let mut store =
Store::new_with_engine(&EngineBuilder::new(Cranelift::default(), None, None).engine());
let mut env1 = FunctionEnv::new(&mut store, ());
struct MyEnv;
let mut env2 = FunctionEnv::new(&mut store, MyEnv {});

View File

@@ -24,7 +24,7 @@ use wasmer::{
imports, wat2wasm, Function, FunctionEnv, FunctionEnvMut, Instance, Module, Store,
TypedFunction,
};
use wasmer_compiler::Backend;
use wasmer_compiler::EngineBuilder;
use wasmer_compiler_cranelift::Cranelift;
fn main() -> Result<(), Box<dyn std::error::Error>> {
@@ -52,7 +52,8 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {
// Note that we don't need to specify the engine/compiler if we want to use
// the default provided by Wasmer.
// You can use `Store::default()` for that.
let mut store = Store::new_with_engine(&Backend::new(Cranelift::default()).engine());
let mut store =
Store::new_with_engine(&EngineBuilder::new(Cranelift::default(), None, None).engine());
println!("Compiling module...");
// Let's compile the Wasm module.

View File

@@ -18,7 +18,7 @@
use wasmer::{
imports, wat2wasm, FunctionEnv, Global, Instance, Module, Store, TypedFunction, Value,
};
use wasmer_compiler::Backend;
use wasmer_compiler::EngineBuilder;
use wasmer_compiler_cranelift::Cranelift;
fn main() -> Result<(), Box<dyn std::error::Error>> {
@@ -40,7 +40,8 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {
// Note that we don't need to specify the engine/compiler if we want to use
// the default provided by Wasmer.
// You can use `Store::default()` for that.
let mut store = Store::new_with_engine(&Backend::new(Cranelift::default()).engine());
let mut store =
Store::new_with_engine(&EngineBuilder::new(Cranelift::default(), None, None).engine());
let mut env = FunctionEnv::new(&mut store, ());
println!("Compiling module...");

View File

@@ -15,7 +15,7 @@
//! Ready?
use wasmer::{imports, wat2wasm, FunctionEnv, Instance, Module, Store, TypedFunction};
use wasmer_compiler::Backend;
use wasmer_compiler::EngineBuilder;
use wasmer_compiler_cranelift::Cranelift;
fn main() -> Result<(), Box<dyn std::error::Error>> {
@@ -39,7 +39,8 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {
// Note that we don't need to specify the engine/compiler if we want to use
// the default provided by Wasmer.
// You can use `Store::default()` for that.
let mut store = Store::new_with_engine(&Backend::new(Cranelift::default()).engine());
let mut store =
Store::new_with_engine(&EngineBuilder::new(Cranelift::default(), None, None).engine());
let mut env = FunctionEnv::new(&mut store, ());
println!("Compiling module...");

View File

@@ -18,7 +18,7 @@ use std::mem;
use wasmer::{
imports, wat2wasm, Bytes, FunctionEnv, Instance, Module, Pages, Store, TypedFunction,
};
use wasmer_compiler::Backend;
use wasmer_compiler::EngineBuilder;
use wasmer_compiler_cranelift::Cranelift;
// this example is a work in progress:
@@ -59,7 +59,8 @@ fn main() -> anyhow::Result<()> {
// Note that we don't need to specify the engine/compiler if we want to use
// the default provided by Wasmer.
// You can use `Store::default()` for that.
let mut store = Store::new_with_engine(&Backend::new(Cranelift::default()).engine());
let mut store =
Store::new_with_engine(&EngineBuilder::new(Cranelift::default(), None, None).engine());
let mut env = FunctionEnv::new(&mut store, ());
println!("Compiling module...");

View File

@@ -19,7 +19,7 @@ use std::sync::Arc;
use wasmer::wasmparser::Operator;
use wasmer::CompilerConfig;
use wasmer::{imports, wat2wasm, FunctionEnv, Instance, Module, Store, TypedFunction};
use wasmer_compiler::Backend;
use wasmer_compiler::EngineBuilder;
use wasmer_compiler_cranelift::Cranelift;
use wasmer_middlewares::{
metering::{get_remaining_points, set_remaining_points, MeteringPoints},
@@ -70,7 +70,8 @@ fn main() -> anyhow::Result<()> {
//
// We use our previously create compiler configuration
// with the Universal engine.
let mut store = Store::new_with_engine(&Backend::new(compiler_config).engine());
let mut store =
Store::new_with_engine(&EngineBuilder::new(compiler_config, None, None).engine());
let mut env = FunctionEnv::new(&mut store, ());
println!("Compiling module...");

View File

@@ -2,7 +2,7 @@ use wasmer::{
imports, wat2wasm, Function, FunctionEnv, FunctionEnvMut, Instance, Module, Store, TableType,
Type, TypedFunction, Value,
};
use wasmer_compiler::Backend;
use wasmer_compiler::EngineBuilder;
use wasmer_compiler_cranelift::Cranelift;
/// A function we'll call through a table.
@@ -52,7 +52,8 @@ fn main() -> anyhow::Result<()> {
)?;
// We set up our store with an engine and a compiler.
let mut store = Store::new_with_engine(&Backend::new(Cranelift::default()).engine());
let mut store =
Store::new_with_engine(&EngineBuilder::new(Cranelift::default(), None, None).engine());
let mut env = FunctionEnv::new(&mut store, ());
// Then compile our Wasm.
let module = Module::new(&store, wasm_bytes)?;

View File

@@ -6,7 +6,7 @@ use wasmer::{
wat2wasm, BaseTunables, FunctionEnv, Instance, Memory, MemoryType, Module, Pages, Store,
TableType, Target, Tunables,
};
use wasmer_compiler::Backend;
use wasmer_compiler::EngineBuilder;
use wasmer_compiler_cranelift::Cranelift;
/// A custom tunables that allows you to set a memory limit.
@@ -137,7 +137,7 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {
// Any compiler and any engine do the job here
let compiler = Cranelift::default();
let engine = Backend::new(compiler).engine();
let engine = EngineBuilder::new(compiler, None, None).engine();
// Here is where the fun begins

View File

@@ -16,7 +16,7 @@
//! Ready?
use wasmer::{FunctionEnv, Instance, Module, Store};
use wasmer_compiler::Backend;
use wasmer_compiler::EngineBuilder;
use wasmer_compiler_cranelift::Cranelift;
use wasmer_wasi::WasiState;
@@ -32,7 +32,8 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {
// Note that we don't need to specify the engine/compiler if we want to use
// the default provided by Wasmer.
// You can use `Store::default()` for that.
let mut store = Store::new_with_engine(&Backend::new(Cranelift::default()).engine());
let mut store =
Store::new_with_engine(&EngineBuilder::new(Cranelift::default(), None, None).engine());
println!("Compiling module...");
// Let's compile the Wasm module.

View File

@@ -13,7 +13,7 @@
use std::io::{Read, Write};
use wasmer::{FunctionEnv, Instance, Module, Store};
use wasmer_compiler::Backend;
use wasmer_compiler::EngineBuilder;
use wasmer_compiler_cranelift::Cranelift;
use wasmer_wasi::{Pipe, WasiState};
@@ -29,7 +29,8 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {
// Note that we don't need to specify the engine/compiler if we want to use
// the default provided by Wasmer.
// You can use `Store::default()` for that.
let mut store = Store::new_with_engine(&Backend::new(Cranelift::default()).engine());
let mut store =
Store::new_with_engine(&EngineBuilder::new(Cranelift::default(), None, None).engine());
println!("Compiling module...");
// Let's compile the Wasm module.

View File

@@ -3,7 +3,7 @@
use libfuzzer_sys::{arbitrary, arbitrary::Arbitrary, fuzz_target};
use wasm_smith::{Config, ConfiguredModule};
use wasmer::{CompilerConfig, Engine, Module, Store};
use wasmer_compiler::Backend;
use wasmer_compiler::EngineBuilder;
use wasmer_compiler_cranelift::Cranelift;
use wasmer_compiler_llvm::LLVM;
use wasmer_compiler_singlepass::Singlepass;
@@ -47,7 +47,7 @@ fuzz_target!(|module: ConfiguredModule<NoImportsConfig>| {
compiler.enable_verifier();
compile_and_compare(
"universal-cranelift",
Backend::new(compiler.clone()).engine(),
EngineBuilder::new(compiler.clone(), None, None).engine(),
&wasm_bytes,
);
@@ -56,14 +56,14 @@ fuzz_target!(|module: ConfiguredModule<NoImportsConfig>| {
compiler.enable_verifier();
compile_and_compare(
"universal-llvm",
Backend::new(compiler.clone()).engine(),
EngineBuilder::new(compiler.clone(), None, None).engine(),
&wasm_bytes,
);
let compiler = Singlepass::default();
compile_and_compare(
"universal-singlepass",
Backend::new(compiler.clone()).engine(),
EngineBuilder::new(compiler.clone(), None, None).engine(),
&wasm_bytes,
);
});

View File

@@ -5,7 +5,7 @@ use anyhow::Result;
use libfuzzer_sys::{arbitrary, arbitrary::Arbitrary, fuzz_target};
use wasm_smith::{Config, ConfiguredModule};
use wasmer::{imports, CompilerConfig, Instance, Module, Store, Val};
use wasmer_compiler::Backend;
use wasmer_compiler::EngineBuilder;
#[cfg(feature = "cranelift")]
use wasmer_compiler_cranelift::Cranelift;
#[cfg(feature = "llvm")]
@@ -48,7 +48,7 @@ impl std::fmt::Debug for WasmSmithModule {
#[cfg(feature = "singlepass")]
fn maybe_instantiate_singlepass(wasm_bytes: &[u8]) -> Result<Option<Instance>> {
let compiler = Singlepass::default();
let mut store = Store::new_with_engine(&Backend::new(compiler).engine());
let mut store = Store::new_with_engine(&EngineBuilder::new(compiler, None, None).engine());
let module = Module::new(&store, &wasm_bytes);
let module = match module {
Ok(m) => m,
@@ -69,7 +69,7 @@ fn maybe_instantiate_cranelift(wasm_bytes: &[u8]) -> Result<Option<Instance>> {
let mut compiler = Cranelift::default();
compiler.canonicalize_nans(true);
compiler.enable_verifier();
let mut store = Store::new_with_engine(&Backend::new(compiler).engine());
let mut store = Store::new_with_engine(&EngineBuilder::new(compiler, None, None).engine());
let module = Module::new(&store, &wasm_bytes)?;
let instance = Instance::new(&module, &imports! {})?;
Ok(Some(instance))
@@ -80,7 +80,7 @@ fn maybe_instantiate_llvm(wasm_bytes: &[u8]) -> Result<Option<Instance>> {
let mut compiler = LLVM::default();
compiler.canonicalize_nans(true);
compiler.enable_verifier();
let mut store = Store::new_with_engine(&Backend::new(compiler).engine());
let mut store = Store::new_with_engine(&EngineBuilder::new(compiler, None, None).engine());
let module = Module::new(&store, &wasm_bytes)?;
let instance = Instance::new(&module, &imports! {})?;
Ok(Some(instance))

View File

@@ -5,7 +5,7 @@ use std::sync::Arc;
use wasm_smith::{Config, ConfiguredModule};
use wasmer::wasmparser::Operator;
use wasmer::{imports, CompilerConfig, Instance, Module, Store};
use wasmer_compiler::Backend;
use wasmer_compiler::EngineBuilder;
use wasmer_compiler_cranelift::Cranelift;
use wasmer_middlewares::Metering;
@@ -56,7 +56,7 @@ fuzz_target!(|module: WasmSmithModule| {
compiler.enable_verifier();
let metering = Arc::new(Metering::new(10, cost));
compiler.push_middleware(metering);
let mut store = Store::new_with_engine(&Backend::new(compiler).engine());
let mut store = Store::new_with_engine(&EngineBuilder::new(compiler, None, None).engine());
let module = Module::new(&store, &wasm_bytes).unwrap();
match Instance::new(&module, &imports! {}) {
Ok(_) => {}

View File

@@ -3,7 +3,7 @@
use libfuzzer_sys::{arbitrary, arbitrary::Arbitrary, fuzz_target};
use wasm_smith::{Config, ConfiguredModule};
use wasmer::{imports, CompilerConfig, Instance, Module, Store};
use wasmer_compiler::Backend;
use wasmer_compiler::EngineBuilder;
use wasmer_compiler_cranelift::Cranelift;
#[derive(Arbitrary, Debug, Default, Copy, Clone)]
@@ -42,7 +42,7 @@ fuzz_target!(|module: WasmSmithModule| {
let mut compiler = Cranelift::default();
compiler.canonicalize_nans(true);
compiler.enable_verifier();
let mut store = Store::new_with_engine(&Backend::new(compiler).engine());
let mut store = Store::new_with_engine(&EngineBuilder::new(compiler, None, None).engine());
let module = Module::new(&store, &wasm_bytes).unwrap();
match Instance::new(&module, &imports! {}) {
Ok(_) => {}

View File

@@ -3,7 +3,7 @@
use libfuzzer_sys::{arbitrary, arbitrary::Arbitrary, fuzz_target};
use wasm_smith::{Config, ConfiguredModule};
use wasmer::{imports, CompilerConfig, Instance, Module, Store};
use wasmer_compiler::Backend;
use wasmer_compiler::EngineBuilder;
use wasmer_compiler_llvm::LLVM;
#[derive(Arbitrary, Debug, Default, Copy, Clone)]
@@ -42,7 +42,7 @@ fuzz_target!(|module: WasmSmithModule| {
let mut compiler = LLVM::default();
compiler.canonicalize_nans(true);
compiler.enable_verifier();
let mut store = Store::new_with_engine(&Backend::new(compiler).engine());
let mut store = Store::new_with_engine(&EngineBuilder::new(compiler, None, None).engine());
let module = Module::new(&store, &wasm_bytes).unwrap();
match Instance::new(&module, &imports! {}) {
Ok(_) => {}

View File

@@ -3,7 +3,7 @@
use libfuzzer_sys::{arbitrary, arbitrary::Arbitrary, fuzz_target};
use wasm_smith::{Config, ConfiguredModule};
use wasmer::{imports, Instance, Module, Store};
use wasmer_compiler::Backend;
use wasmer_compiler::EngineBuilder;
use wasmer_compiler_singlepass::Singlepass;
#[derive(Arbitrary, Debug, Default, Copy, Clone)]
@@ -40,7 +40,7 @@ fuzz_target!(|module: WasmSmithModule| {
}
let compiler = Singlepass::default();
let mut store = Store::new_with_engine(&Backend::new(compiler).engine());
let mut store = Store::new_with_engine(&EngineBuilder::new(compiler, None, None).engine());
let module = Module::new(&store, &wasm_bytes);
let module = match module {
Ok(m) => m,

View File

@@ -88,32 +88,22 @@ std = []
core = ["hashbrown"]
# Features for `sys`.
sys = []
sys-default = ["sys", "wat", "default-cranelift", "default-engine_compilation"]
sys = [
"wasmer-compiler/translator",
"wasmer-compiler/compiler",
]
sys-default = ["sys", "wat", "cranelift"]
# - Compilers.
compiler = [
"sys",
"wasmer-compiler/translator",
]
singlepass = ["compiler", "wasmer-compiler-singlepass"]
cranelift = ["compiler", "wasmer-compiler-cranelift"]
llvm = ["compiler", "wasmer-compiler-llvm"]
default-compiler = []
default-singlepass = ["default-compiler", "singlepass"]
default-cranelift = ["default-compiler", "cranelift"]
default-llvm = ["default-compiler", "llvm"]
# - Engines.
engine = ["sys"]
engine_compilation = [
"engine", "wasmer-compiler/engine_compilation"
]
default-engine = []
default-engine_compilation = [
"default-engine",
"engine_compilation",
]
# - Deprecated features.
jit = ["engine_compilation"]
jit = ["engine"]
# Features for `js`.
js = ["wasm-bindgen", "js-sys"]
@@ -124,4 +114,4 @@ wasm-types-polyfill = ["js", "wasmparser"]
js-serializable-module = []
[package.metadata.docs.rs]
features = ["compiler", "core", "cranelift", "default-compiler", "default-engine", "engine", "jit", "native", "singlepass", "sys", "sys-default", "engine_compilation"]
features = ["compiler", "core", "cranelift", "engine", "jit", "native", "singlepass", "sys", "sys-default" ]

View File

@@ -307,8 +307,8 @@
#![cfg_attr(not(feature = "wat"), doc = "(disabled),")]
//! enables `wasmer` to parse the WebAssembly text format,
//! - `compilation`
#![cfg_attr(feature = "compilation", doc = "(enabled),")]
#![cfg_attr(not(feature = "compilation"), doc = "(disabled),")]
#![cfg_attr(feature = "compiler", doc = "(enabled),")]
#![cfg_attr(not(feature = "compiler"), doc = "(disabled),")]
//! enables compilation with the wasmer engine.
//!
//! The features that set defaults come in sets that are mutually exclusive.

View File

@@ -64,26 +64,6 @@ pub mod vm {
#[cfg(feature = "wat")]
pub use wat::parse_bytes as wat2wasm;
// The compilers are mutually exclusive
#[cfg(any(
all(
feature = "default-llvm",
any(feature = "default-cranelift", feature = "default-singlepass")
),
all(feature = "default-cranelift", feature = "default-singlepass")
))]
compile_error!(
r#"The `default-singlepass`, `default-cranelift` and `default-llvm` features are mutually exclusive.
If you wish to use more than one compiler, you can simply create the own store. Eg.:
```
use wasmer::{Store, Backend, Singlepass};
let engine = Backend::new(Singlepass::default()).engine();
let mut store = Store::new_with_engine(&engine);
```"#
);
#[cfg(feature = "singlepass")]
pub use wasmer_compiler_singlepass::Singlepass;
@@ -93,17 +73,13 @@ pub use wasmer_compiler_cranelift::{Cranelift, CraneliftOptLevel};
#[cfg(feature = "llvm")]
pub use wasmer_compiler_llvm::{LLVMOptLevel, LLVM};
#[cfg(feature = "engine_compilation")]
pub use wasmer_compiler::{Artifact, Backend, Engine};
pub use wasmer_compiler::Engine;
#[cfg(feature = "compiler")]
pub use wasmer_compiler::{Artifact, EngineBuilder};
/// Version number of this crate.
pub const VERSION: &str = env!("CARGO_PKG_VERSION");
/// The Deprecated JIT Engine (please use `Universal` instead)
#[cfg(feature = "jit")]
#[deprecated(since = "2.0.0", note = "Please use the `universal` feature instead")]
pub type JIT = Backend;
/// This type is deprecated, it has been replaced by TypedFunction.
#[deprecated(
since = "3.0.0",

View File

@@ -4,7 +4,7 @@ use std::sync::{Arc, RwLock};
#[cfg(feature = "compiler")]
use wasmer_compiler::CompilerConfig;
#[cfg(feature = "compiler")]
use wasmer_compiler::{Backend, Engine, Tunables};
use wasmer_compiler::{Engine, EngineBuilder, Tunables};
use wasmer_vm::{init_traps, TrapHandler, TrapHandlerFn};
use wasmer_vm::StoreObjects;
@@ -39,7 +39,7 @@ impl Store {
#[cfg(feature = "compiler")]
/// Creates a new `Store` with a specific [`CompilerConfig`].
pub fn new(compiler_config: Box<dyn CompilerConfig>) -> Self {
let engine = Backend::new(compiler_config).engine();
let engine = EngineBuilder::new(compiler_config, None, None).engine();
Self::new_with_tunables(&engine, BaseTunables::for_target(engine.target()))
}
@@ -120,20 +120,21 @@ unsafe impl Send for Store {}
unsafe impl Sync for Store {}
// We only implement default if we have assigned a default compiler and engine
#[cfg(all(feature = "default-compiler", feature = "default-engine"))]
#[cfg(feature = "compiler")]
impl Default for Store {
fn default() -> Self {
// We store them on a function that returns to make
// sure this function doesn't emit a compile error even if
// more than one compiler is enabled.
#[allow(unreachable_code)]
#[cfg(any(feature = "cranelift", feature = "llvm", feature = "singlepass"))]
fn get_config() -> impl CompilerConfig + 'static {
cfg_if::cfg_if! {
if #[cfg(feature = "default-cranelift")] {
if #[cfg(feature = "cranelift")] {
wasmer_compiler_cranelift::Cranelift::default()
} else if #[cfg(feature = "default-llvm")] {
} else if #[cfg(feature = "llvm")] {
wasmer_compiler_llvm::LLVM::default()
} else if #[cfg(feature = "default-singlepass")] {
} else if #[cfg(feature = "singlepass")] {
wasmer_compiler_singlepass::Singlepass::default()
} else {
compile_error!("No default compiler chosen")
@@ -142,19 +143,28 @@ impl Default for Store {
}
#[allow(unreachable_code, unused_mut)]
fn get_engine(mut config: impl CompilerConfig + 'static) -> Engine {
fn get_engine() -> Engine {
cfg_if::cfg_if! {
if #[cfg(feature = "engine_compilation")] {
wasmer_compiler::Backend::new(config)
if #[cfg(feature = "compiler")] {
cfg_if::cfg_if! {
if #[cfg(any(feature = "cranelift", feature = "llvm", feature = "singlepass"))]
{
let config = get_config();
wasmer_compiler::EngineBuilder::new(Box::new(config) as Box<dyn CompilerConfig>, None, None)
.engine()
} else {
wasmer_compiler::EngineBuilder::headless()
.engine()
}
}
} else {
compile_error!("No default engine chosen")
}
}
}
let config = get_config();
let engine = get_engine(config);
let engine = get_engine();
let tunables = BaseTunables::for_target(engine.target());
Self::new_with_tunables(&engine, tunables)
}

View File

@@ -48,7 +48,7 @@ inline-c = "0.1.5"
default = [
"wat",
"cranelift",
"compilation",
"compiler",
"wasi",
"middlewares",
]
@@ -58,12 +58,15 @@ middlewares = [
"compiler",
"wasmer-middlewares",
]
compilation = [
"wasmer-compiler/engine_compilation",
"compiler",
]
compiler = [
"wasmer-api/compiler",
"wasmer-compiler/translator",
"wasmer-compiler/compiler",
]
compiler-headless = [
"wasmer-api/compiler",
"wasmer-compiler/translator",
"wasmer-compiler/compiler",
]
singlepass = [
"wasmer-compiler-singlepass",
@@ -79,7 +82,7 @@ llvm = [
]
# Deprecated features.
jit = ["compilation"]
jit = ["compiler"]
# TODO: Port this feature.
#emscripten = ["wasmer-emscripten"]

View File

@@ -140,7 +140,7 @@ fn build_wasm_c_api_headers(crate_dir: &str, out_dir: &str) {
pre_header = PRE_HEADER
);
map_feature_as_c_define!("compilation", UNIVERSAL_FEATURE_AS_C_DEFINE, pre_header);
map_feature_as_c_define!("compiler", UNIVERSAL_FEATURE_AS_C_DEFINE, pre_header);
map_feature_as_c_define!("compiler", COMPILER_FEATURE_AS_C_DEFINE, pre_header);
map_feature_as_c_define!("wasi", WASI_FEATURE_AS_C_DEFINE, pre_header);
map_feature_as_c_define!("middlewares", MIDDLEWARES_FEATURE_AS_C_DEFINE, pre_header);

View File

@@ -1,8 +1,6 @@
#[cfg(feature = "compiler")]
pub use super::unstable::engine::wasmer_is_compiler_available;
pub use super::unstable::engine::{
wasm_config_set_features, wasm_config_set_target, wasmer_is_engine_available,
};
pub use super::unstable::engine::{wasm_config_set_features, wasm_config_set_target};
use super::unstable::features::wasmer_features_t;
#[cfg(feature = "middlewares")]
pub use super::unstable::middlewares::wasm_config_push_middleware;
@@ -11,8 +9,7 @@ use super::unstable::middlewares::wasmer_middleware_t;
use super::unstable::target_lexicon::wasmer_target_t;
use crate::error::update_last_error;
use cfg_if::cfg_if;
#[cfg(feature = "compilation")]
use wasmer_compiler::{Backend, Engine};
use wasmer_compiler::{Engine, EngineBuilder};
/// Kind of compilers that can be used by the engines.
///
@@ -67,13 +64,7 @@ pub enum wasmer_engine_t {
impl Default for wasmer_engine_t {
fn default() -> Self {
cfg_if! {
if #[cfg(feature = "compilation")] {
Self::UNIVERSAL
} else {
compile_error!("Please enable one of the engines")
}
}
}
}
@@ -229,12 +220,6 @@ pub extern "C" fn wasm_config_set_compiler(
/// // Create the configuration.
/// wasm_config_t* config = wasm_config_new();
///
/// // Use the Universal engine, if available.
/// if (wasmer_is_engine_available(UNIVERSAL)) {
/// wasm_config_set_engine(config, UNIVERSAL);
/// }
/// // OK, let's do not specify any particular engine.
///
/// // Create the engine.
/// wasm_engine_t* engine = wasm_engine_new_with_config(config);
///
@@ -267,7 +252,7 @@ pub struct wasm_engine_t {
#[cfg(feature = "compiler")]
use wasmer_api::CompilerConfig;
#[cfg(all(feature = "compiler", any(feature = "compilation", feature = "dylib")))]
#[cfg(all(feature = "compiler", any(feature = "compiler", feature = "dylib")))]
fn get_default_compiler_config() -> Box<dyn CompilerConfig> {
cfg_if! {
if #[cfg(feature = "cranelift")] {
@@ -283,7 +268,7 @@ fn get_default_compiler_config() -> Box<dyn CompilerConfig> {
}
cfg_if! {
if #[cfg(all(feature = "compilation", feature = "compiler"))] {
if #[cfg(feature = "compiler")] {
/// Creates a new Universal engine with the default compiler.
///
/// # Example
@@ -294,10 +279,10 @@ cfg_if! {
#[no_mangle]
pub extern "C" fn wasm_engine_new() -> Box<wasm_engine_t> {
let compiler_config: Box<dyn CompilerConfig> = get_default_compiler_config();
let engine: Engine = Backend::new(compiler_config).engine();
let engine: Engine = EngineBuilder::new(compiler_config, None, None).engine();
Box::new(wasm_engine_t { inner: engine })
}
} else if #[cfg(feature = "compilation")] {
} else if #[cfg(feature = "compiler-headless")] {
/// Creates a new headless Universal engine.
///
/// # Example
@@ -307,7 +292,7 @@ cfg_if! {
/// cbindgen:ignore
#[no_mangle]
pub extern "C" fn wasm_engine_new() -> Box<wasm_engine_t> {
let engine: Engine = Backend::headless().engine();
let engine: Engine = EngineBuilder::headless().engine();
Box::new(wasm_engine_t { inner: engine })
}
} else {
@@ -320,7 +305,7 @@ cfg_if! {
/// cbindgen:ignore
#[no_mangle]
pub extern "C" fn wasm_engine_new() -> Box<wasm_engine_t> {
unimplemented!("No engine attached; You might want to recompile `wasmer_c_api` with for example `--feature compilation`");
unimplemented!("No engine attached; You might want to recompile `wasmer_c_api` with for example `--feature compiler`");
}
}
}
@@ -374,7 +359,8 @@ pub extern "C" fn wasm_engine_new_with_config(
}
let config = config?;
#[cfg(not(any(feature = "compiler", feature = "compiler-headless")))]
return return_with_error("Wasmer has not been compiled with the `compiler` feature.");
cfg_if! {
if #[cfg(feature = "compiler")] {
#[allow(unused_mut)]
@@ -417,19 +403,16 @@ pub extern "C" fn wasm_engine_new_with_config(
compiler_config.canonicalize_nans(true);
}
#[cfg(not(feature = "compilation"))]
return return_with_error("Wasmer has not been compiled with the `compilation` feature.");
#[cfg(feature = "compilation")]
let inner: Engine =
{
let mut builder = Backend::new(compiler_config);
let mut builder = EngineBuilder::new(compiler_config, None, None);
if let Some(target) = config.target {
builder = builder.target(target.inner);
builder.set_target(Some(target.inner));
}
if let Some(features) = config.features {
builder = builder.features(features.inner);
builder.set_features(Some(features.inner));
}
builder.engine()
@@ -437,22 +420,18 @@ pub extern "C" fn wasm_engine_new_with_config(
Some(Box::new(wasm_engine_t { inner }))
} else {
let inner: Engine =
cfg_if! {
if #[cfg(feature = "compilation")] {
let mut builder = Backend::headless();
{
let mut builder = EngineBuilder::headless();
if let Some(target) = config.target {
builder = builder.target(target.inner);
builder.set_target(Some(target.inner));
}
if let Some(features) = config.features {
builder = builder.features(features.inner);
builder.set_features(Some(features.inner));
}
builder.engine()
} else {
return return_with_error("Wasmer has not been compiled with the `compilation` feature.");
}
};
Some(Box::new(wasm_engine_t { inner }))
}

View File

@@ -62,7 +62,7 @@ mod macros;
/// ```
///
/// To configure the engine, see the [`wasm_config_new`][engine::wasm_config_new].
#[cfg(feature = "compilation")]
#[cfg(any(feature = "compiler", feature = "compiler-headless"))]
pub mod engine;
/// cbindgen:ignore

View File

@@ -161,7 +161,7 @@ pub extern "C" fn wasmer_is_headless() -> bool {
/// compiled library.
#[no_mangle]
pub extern "C" fn wasmer_is_engine_available(engine: wasmer_engine_t) -> bool {
matches!(engine, wasmer_engine_t::UNIVERSAL if cfg!(feature = "compilation"))
matches!(engine, wasmer_engine_t::UNIVERSAL if cfg!(feature = "compiler"))
}
#[cfg(test)]
@@ -234,11 +234,7 @@ mod tests {
fn test_wasmer_is_engine_available() {
set_var(
"UNIVERSAL",
if cfg!(feature = "compilation") {
"1"
} else {
"0"
},
if cfg!(feature = "compiler") { "1" } else { "0" },
);
(assert_c! {

View File

@@ -6,7 +6,7 @@ use tempfile::TempDir;
use wasmer::{Module, Store};
use wasmer_cache::Cache;
use wasmer_cache::{FileSystemCache, Hash};
use wasmer_compiler::Backend;
use wasmer_compiler::EngineBuilder;
use wasmer_compiler_singlepass::Singlepass;
fn random_key() -> Hash {
@@ -17,7 +17,7 @@ pub fn store_cache_universal(c: &mut Criterion) {
let tmp_dir = TempDir::new().unwrap();
let mut fs_cache = FileSystemCache::new(tmp_dir.path()).unwrap();
let compiler = Singlepass::default();
let store = Store::new_with_engine(&Backend::new(compiler).engine());
let store = Store::new_with_engine(&EngineBuilder::new(compiler, None, None).engine());
let module = Module::new(
&store,
std::fs::read("../../lib/c-api/examples/assets/qjs.wasm").unwrap(),
@@ -36,7 +36,7 @@ pub fn load_cache_universal(c: &mut Criterion) {
let tmp_dir = TempDir::new().unwrap();
let mut fs_cache = FileSystemCache::new(tmp_dir.path()).unwrap();
let compiler = Singlepass::default();
let store = Store::new_with_engine(&Backend::new(compiler).engine());
let store = Store::new_with_engine(&EngineBuilder::new(compiler, None, None).engine());
let module = Module::new(
&store,
std::fs::read("../../lib/c-api/examples/assets/qjs.wasm").unwrap(),
@@ -54,7 +54,7 @@ pub fn store_cache_native(c: &mut Criterion) {
let tmp_dir = TempDir::new().unwrap();
let mut fs_cache = FileSystemCache::new(tmp_dir.path()).unwrap();
let compiler = Singlepass::default();
let store = Store::new_with_engine(&Backend::new(compiler).engine());
let store = Store::new_with_engine(&EngineBuilder::new(compiler, None, None).engine());
let module = Module::new(
&store,
std::fs::read("../../lib/c-api/examples/assets/qjs.wasm").unwrap(),
@@ -73,7 +73,7 @@ pub fn load_cache_native(c: &mut Criterion) {
let tmp_dir = TempDir::new().unwrap();
let mut fs_cache = FileSystemCache::new(tmp_dir.path()).unwrap();
let compiler = Singlepass::default();
let store = Store::new_with_engine(&Backend::new(compiler).engine());
let store = Store::new_with_engine(&EngineBuilder::new(compiler, None, None).engine());
let module = Module::new(
&store,
std::fs::read("../../lib/c-api/examples/assets/qjs.wasm").unwrap(),

View File

@@ -18,7 +18,7 @@ path = "src/bin/wasmer_compiler.rs"
doc = false
[dependencies]
wasmer-compiler = { version = "=2.3.0", path = "../compiler", features = ["engine_compilation"] }
wasmer-compiler = { version = "=2.3.0", path = "../compiler", features = ["compiler"] }
wasmer-types = { version = "=2.3.0", path = "../types" }
atty = "0.2"
colored = "2.0"
@@ -52,6 +52,7 @@ default = []
engine = []
compiler = [
"wasmer-compiler/translator",
"wasmer-compiler/compiler",
]
singlepass = [
"wasmer-compiler-singlepass",

View File

@@ -55,7 +55,8 @@ impl Compile {
Target::new(target_triple.clone(), features)
})
.unwrap_or_default();
let (mut engine, compiler_type) = self.store.get_engine_for_target(target.clone())?;
let (engine_builder, compiler_type) = self.store.get_engine_for_target(target.clone())?;
let engine = engine_builder.engine();
let output_filename = self
.output
.file_stem()
@@ -96,7 +97,7 @@ impl Compile {
.map(|table_type| tunables.table_style(table_type))
.collect();
let artifact = ArtifactBuild::new(
&mut engine,
&mut engine.inner_mut(),
&wasm_bytes,
&target,
memory_styles,

View File

@@ -27,7 +27,8 @@ impl Validate {
Triple::from_str("x86_64-linux-gnu").unwrap(),
CpuFeature::SSE2 | CpuFeature::AVX,
);
let (engine, _compiler_type) = self.store.get_engine_for_target(target)?;
let (engine_builder, _compiler_type) = self.store.get_engine_for_target(target)?;
let engine = engine_builder.engine();
let module_contents = std::fs::read(&self.path)?;
if !is_wasm(&module_contents) {
bail!("`wasmer validate` only validates WebAssembly files");

View File

@@ -174,7 +174,8 @@ impl CompilerOptions {
compiler_config: Box<dyn CompilerConfig>,
) -> Result<EngineBuilder> {
let features = self.get_features(compiler_config.default_features_for_target(&target))?;
let engine: EngineBuilder = EngineBuilder::new(Some(compiler_config.compiler()), features);
let engine: EngineBuilder =
EngineBuilder::new(compiler_config, Some(target), Some(features));
Ok(engine)
}

View File

@@ -26,7 +26,7 @@ required-features = ["headless"]
[dependencies]
wasmer = { version = "=2.3.0", path = "../api", default-features = false }
wasmer-compiler = { version = "=2.3.0", path = "../compiler", features = ["engine_compilation", ] }
wasmer-compiler = { version = "=2.3.0", path = "../compiler", features = ["compiler", ] }
wasmer-compiler-cranelift = { version = "=2.3.0", path = "../compiler-cranelift", optional = true }
wasmer-compiler-singlepass = { version = "=2.3.0", path = "../compiler-singlepass", optional = true }
wasmer-compiler-llvm = { version = "=2.3.0", path = "../compiler-llvm", optional = true }
@@ -64,7 +64,7 @@ default = [
"cache",
"wasi",
"emscripten",
"compilation",
"compiler",
]
cache = ["wasmer-cache"]
cache-blake3-pure = ["wasmer-cache/blake3-pure"]
@@ -74,9 +74,7 @@ emscripten = ["wasmer-emscripten"]
wat = ["wasmer/wat"]
compiler = [
"wasmer-compiler/translator",
]
compilation = [
"wasmer-compiler/engine_compilation",
"wasmer-compiler/compiler",
]
experimental-io-devices = [
"wasmer-wasi-experimental-io-devices",

View File

@@ -262,7 +262,7 @@ impl Run {
fn get_store_module(&self) -> Result<(Store, Module)> {
let contents = std::fs::read(self.path.clone())?;
if wasmer_compiler::Artifact::is_deserializable(&contents) {
let engine = wasmer_compiler::Backend::headless().engine();
let engine = wasmer_compiler::EngineBuilder::headless().engine();
let store = Store::new_with_engine(&engine);
let module = unsafe { Module::deserialize_from_file(&store, &self.path)? };
return Ok((store, module));

View File

@@ -14,7 +14,6 @@ use structopt::StructOpt;
use wasmer::*;
#[cfg(feature = "compiler")]
use wasmer_compiler::CompilerConfig;
#[cfg(feature = "compilation")]
use wasmer_compiler::Engine;
#[derive(Debug, Clone, StructOpt, Default)]
@@ -110,7 +109,7 @@ impl CompilerOptions {
Ok((store, compiler_type))
}
#[cfg(feature = "compilation")]
#[cfg(feature = "compiler")]
fn get_engine(
&self,
target: Target,
@@ -118,9 +117,7 @@ impl CompilerOptions {
) -> Result<Box<Engine>> {
let features = self.get_features(compiler_config.default_features_for_target(&target))?;
let engine: Box<Engine> = Box::new(
wasmer_compiler::Backend::new(compiler_config)
.features(features)
.target(target)
wasmer_compiler::EngineBuilder::new(compiler_config, Some(target), Some(features))
.engine(),
);
@@ -320,7 +317,7 @@ impl StoreOptions {
Ok((store, compiler_type))
}
#[cfg(feature = "compilation")]
#[cfg(feature = "compiler")]
fn get_engine_with_compiler(
&self,
target: Target,
@@ -335,9 +332,8 @@ impl StoreOptions {
// If we don't have a compiler, but we have an engine
#[cfg(not(feature = "compiler"))]
impl StoreOptions {
#[cfg(feature = "compilation")]
fn get_engine_headless(&self) -> Result<Engine> {
let engine: Engine = wasmer_compiler::Backend::headless().engine();
let engine: Engine = wasmer_compiler::EngineBuilder::headless().engine();
Ok(engine)
}

View File

@@ -12,7 +12,7 @@ readme = "README.md"
edition = "2018"
[dependencies]
wasmer-compiler = { path = "../compiler", version = "=2.3.0", features = ["translator"], default-features = false }
wasmer-compiler = { path = "../compiler", version = "=2.3.0", features = ["translator", "compiler"], default-features = false }
wasmer-types = { path = "../types", version = "=2.3.0", default-features = false, features = ["std"] }
cranelift-entity = { version = "0.82", default-features = false }
cranelift-codegen = { version = "0.82", default-features = false, features = ["x86", "arm64"] }

View File

@@ -5,12 +5,12 @@ This crate contains a compiler implementation based on Cranelift.
## Usage
```rust
use wasmer::{Store, Backend};
use wasmer::{Store, EngineBuilder};
use wasmer_compiler_cranelift::Cranelift;
let compiler = Cranelift::new();
// Put it into an engine and add it to the store
let mut store = Store::new_with_engine(&Backend::new(compiler).engine());
let mut store = Store::new_with_engine(&EngineBuilder::new(compiler, None, None).engine());
```
*Note: you can find a [full working example using Cranelift compiler

View File

@@ -13,7 +13,7 @@ edition = "2018"
[dependencies]
wasmer-compiler = { path = "../compiler", version = "=2.3.0", features = [
"translator",
"translator", "compiler"
] }
wasmer-vm = { path = "../vm", version = "=2.3.0" }
wasmer-types = { path = "../types", version = "=2.3.0" }

View File

@@ -5,12 +5,12 @@ This crate contains a compiler implementation based on [the LLVM Compiler Infras
## Usage
```rust
use wasmer::{Store, Backend};
use wasmer::{Store, EngineBuilder};
use wasmer_compiler_llvm::LLVM;
let compiler = LLVM::new();
// Put it into an engine and add it to the store
let mut store = Store::new_with_engine(&Backend::new(compiler).engine());
let mut store = Store::new_with_engine(&EngineBuilder::new(Some(Box::new(compiler)), None, None).engine());
```
*Note: you can find a [full working example using LLVM compiler here][example].*

View File

@@ -12,7 +12,7 @@ readme = "README.md"
edition = "2018"
[dependencies]
wasmer-compiler = { path = "../compiler", version = "=2.3.0", features = ["translator"], default-features = false }
wasmer-compiler = { path = "../compiler", version = "=2.3.0", features = ["translator", "compiler"], default-features = false }
wasmer-types = { path = "../types", version = "=2.3.0", default-features = false, features = ["std"] }
hashbrown = { version = "0.11", optional = true }
gimli = { version = "0.26", optional = true }

View File

@@ -5,12 +5,12 @@ This crate contains a compiler implementation based on the Singlepass linear com
## Usage
```rust
use wasmer::{Store, Backend};
use wasmer::{Store, EngineBuilder};
use wasmer_compiler_singlepass::Singlepass;
let compiler = Singlepass::new();
// Put it into an engine and add it to the store
let mut store = Store::new_with_engine(&Backend::new(compiler).engine());
let mut store = Store::new_with_engine(&EngineBuilder::new(compiler, None, None).engine());
```
*Note: you can find a [full working example using Singlepass compiler

View File

@@ -43,7 +43,7 @@ default = ["std", "enable-serde" ]
# `CompilerConfig`, as well as the included wasmparser.
# Disable this feature if you just want a headless engine.
translator = ["wasmparser"]
engine_compilation = []
compiler = ["translator"]
std = ["wasmer-types/std"]
core = ["hashbrown", "wasmer-types/core"]
enable-serde = ["serde", "serde_bytes", "wasmer-types/enable-serde"]

View File

@@ -1,17 +1,19 @@
//! Define `ArtifactBuild` to allow compiling and instantiating to be
//! done as separate steps.
#[cfg(feature = "engine_compilation")]
#[cfg(feature = "compiler")]
use super::trampoline::{libcall_trampoline_len, make_libcall_trampolines};
use crate::ArtifactCreate;
#[cfg(feature = "compiler")]
use crate::EngineInner;
use crate::Features;
use crate::MetadataHeader;
use crate::{ArtifactCreate, EngineBuilder};
#[cfg(feature = "engine_compilation")]
#[cfg(feature = "compiler")]
use crate::{ModuleEnvironment, ModuleMiddlewareChain};
use enumset::EnumSet;
use std::mem;
use wasmer_types::entity::PrimaryMap;
#[cfg(feature = "engine_compilation")]
#[cfg(feature = "compiler")]
use wasmer_types::CompileModuleInfo;
use wasmer_types::SerializeError;
use wasmer_types::{
@@ -38,16 +40,16 @@ impl ArtifactBuild {
}
/// Compile a data buffer into a `ArtifactBuild`, which may then be instantiated.
#[cfg(feature = "engine_compilation")]
#[cfg(feature = "compiler")]
pub fn new(
inner_engine: &mut EngineBuilder,
inner_engine: &mut EngineInner,
data: &[u8],
target: &Target,
memory_styles: PrimaryMap<MemoryIndex, MemoryStyle>,
table_styles: PrimaryMap<TableIndex, TableStyle>,
) -> Result<Self, CompileError> {
let environ = ModuleEnvironment::new();
let features = inner_engine.features();
let features = inner_engine.features().clone();
let translation = environ.translate(data).map_err(CompileError::Wasm)?;
@@ -60,7 +62,7 @@ impl ArtifactBuild {
let compile_info = CompileModuleInfo {
module,
features: features.clone(),
features,
memory_styles,
table_styles,
};
@@ -117,8 +119,15 @@ impl ArtifactBuild {
}
/// Compile a data buffer into a `ArtifactBuild`, which may then be instantiated.
#[cfg(not(feature = "engine_compilation"))]
pub fn new(_engine: &EngineBuilder, _data: &[u8]) -> Result<Self, CompileError> {
#[cfg(not(feature = "compiler"))]
#[cfg(not(target_arch = "wasm32"))]
pub fn new(
_inner_engine: &mut EngineInner,
_data: &[u8],
_target: &Target,
_memory_styles: PrimaryMap<MemoryIndex, MemoryStyle>,
_table_styles: PrimaryMap<TableIndex, TableStyle>,
) -> Result<Self, CompileError> {
Err(CompileError::Codegen(
"Compilation is not enabled in the engine".to_string(),
))

View File

@@ -1,9 +1,7 @@
//! Generic Artifact abstraction for Wasmer Engines.
mod artifact_builder;
mod engine_builder;
mod trampoline;
pub use self::artifact_builder::ArtifactBuild;
pub use self::engine_builder::EngineBuilder;
pub use self::trampoline::*;

View File

@@ -1,17 +1,17 @@
//! Define `Artifact`, based on `ArtifactBuild`
//! to allow compiling and instantiating to be done as separate steps.
use crate::engine::inner::{Engine, EngineInner};
use crate::engine::link::link_module;
use crate::ArtifactBuild;
use crate::ArtifactCreate;
use crate::Features;
#[cfg(feature = "engine_compilation")]
#[cfg(feature = "compiler")]
use crate::ModuleEnvironment;
use crate::{
register_frame_info, resolve_imports, FunctionExtent, GlobalFrameInfoRegistration,
InstantiationError, MetadataHeader, RuntimeError, Tunables,
};
use crate::{Engine, EngineInner};
use enumset::EnumSet;
use std::sync::Arc;
use std::sync::Mutex;
@@ -37,7 +37,7 @@ pub struct Artifact {
impl Artifact {
/// Compile a data buffer into a `ArtifactBuild`, which may then be instantiated.
#[cfg(feature = "engine_compilation")]
#[cfg(feature = "compiler")]
pub fn new(
engine: &Engine,
data: &[u8],
@@ -59,7 +59,7 @@ impl Artifact {
.collect();
let artifact = ArtifactBuild::new(
inner_engine.builder_mut(),
&mut inner_engine,
data,
engine.target(),
memory_styles,
@@ -70,7 +70,7 @@ impl Artifact {
}
/// Compile a data buffer into a `ArtifactBuild`, which may then be instantiated.
#[cfg(not(feature = "engine_compilation"))]
#[cfg(not(feature = "compiler"))]
pub fn new(_engine: &Engine, _data: &[u8]) -> Result<Self, CompileError> {
Err(CompileError::Codegen(
"Compilation is not enabled in the engine".to_string(),

View File

@@ -1,25 +1,27 @@
use super::Engine;
use crate::{CompilerConfig, Features};
use wasmer_types::Target;
use crate::CompilerConfig;
use wasmer_types::{Features, Target};
/// The Backend builder
pub struct Backend {
#[allow(dead_code)]
/// The Builder contents of `Engine`
pub struct EngineBuilder {
/// The compiler
compiler_config: Option<Box<dyn CompilerConfig>>,
/// The machine target
target: Option<Target>,
/// The features to compile the Wasm module with
features: Option<Features>,
}
impl Backend {
/// Create a new Backend
pub fn new<T>(compiler_config: T) -> Self
impl EngineBuilder {
/// Create a new builder with pre-made components
pub fn new<T>(compiler_config: T, target: Option<Target>, features: Option<Features>) -> Self
where
T: Into<Box<dyn CompilerConfig>>,
{
Self {
compiler_config: Some(compiler_config.into()),
target: None,
features: None,
target,
features,
}
}
@@ -33,35 +35,39 @@ impl Backend {
}
/// Set the target
pub fn target(mut self, target: Target) -> Self {
self.target = Some(target);
pub fn set_target(&mut self, target: Option<Target>) -> &mut Self {
self.target = target;
self
}
/// Set the features
pub fn features(mut self, features: Features) -> Self {
self.features = Some(features);
pub fn set_features(&mut self, features: Option<Features>) -> &mut Self {
self.features = features;
self
}
/// Build the `Engine` for this configuration
#[cfg(feature = "engine_compilation")]
#[cfg(feature = "compiler")]
pub fn engine(self) -> Engine {
let target = self.target.unwrap_or_default();
if let Some(compiler_config) = self.compiler_config {
let features = self
.features
.unwrap_or_else(|| compiler_config.default_features_for_target(&target));
let compiler = compiler_config.compiler();
Engine::new(compiler, target, features)
Engine::new(compiler_config, target, features)
} else {
Engine::headless()
}
}
/// Build the `Engine` for this configuration
#[cfg(not(feature = "engine_compilation"))]
#[cfg(not(feature = "compiler"))]
pub fn engine(self) -> Engine {
Engine::headless()
}
/// The Wasm features
pub fn features(&self) -> Option<&Features> {
self.features.as_ref()
}
}

View File

@@ -1,4 +1,5 @@
//! The WebAssembly possible errors
#[cfg(not(target_arch = "wasm32"))]
use crate::engine::trap::RuntimeError;
use thiserror::Error;
pub use wasmer_types::{DeserializeError, ImportError, SerializeError};
@@ -16,6 +17,7 @@ pub enum LinkError {
#[error("Error while importing {0:?}.{1:?}: {2}")]
Import(String, String, ImportError),
#[cfg(not(target_arch = "wasm32"))]
/// A trap ocurred during linking.
#[error("RuntimeError occurred during linking: {0}")]
Trap(#[source] RuntimeError),
@@ -44,6 +46,7 @@ pub enum InstantiationError {
CpuFeature(String),
/// A runtime error occured while invoking the start function
#[cfg(not(target_arch = "wasm32"))]
#[error(transparent)]
Start(RuntimeError),
}

View File

@@ -1,21 +1,28 @@
//! Universal compilation.
#[cfg(feature = "engine_compilation")]
use crate::Compiler;
use crate::EngineBuilder;
use crate::{Artifact, CodeMemory};
#[cfg(not(target_arch = "wasm32"))]
use crate::Artifact;
#[cfg(not(target_arch = "wasm32"))]
use crate::CodeMemory;
#[cfg(feature = "compiler")]
use crate::{Compiler, CompilerConfig};
#[cfg(not(target_arch = "wasm32"))]
use crate::{FunctionExtent, Tunables};
#[cfg(not(target_arch = "wasm32"))]
use memmap2::Mmap;
#[cfg(not(target_arch = "wasm32"))]
use std::path::Path;
use std::sync::atomic::{AtomicUsize, Ordering::SeqCst};
use std::sync::{Arc, Mutex};
use wasmer_types::entity::PrimaryMap;
use wasmer_types::FunctionBody;
#[cfg(not(target_arch = "wasm32"))]
use wasmer_types::{
CompileError, DeserializeError, Features, FunctionIndex, FunctionType, LocalFunctionIndex,
ModuleInfo, SignatureIndex, Target,
entity::PrimaryMap, DeserializeError, FunctionBody, FunctionIndex, FunctionType,
LocalFunctionIndex, ModuleInfo, SignatureIndex,
};
use wasmer_types::{CompileError, Features, Target};
#[cfg(not(target_arch = "wasm32"))]
use wasmer_types::{CustomSection, CustomSectionProtection, SectionIndex};
#[cfg(not(target_arch = "wasm32"))]
use wasmer_vm::{
FunctionBodyPtr, SectionBodyPtr, SignatureRegistry, VMFunctionBody, VMSharedSignatureIndex,
VMTrampoline,
@@ -32,12 +39,19 @@ pub struct Engine {
impl Engine {
/// Create a new `Engine` with the given config
#[cfg(feature = "engine_compilation")]
pub fn new(compiler: Box<dyn Compiler>, target: Target, features: Features) -> Self {
#[cfg(feature = "compiler")]
pub fn new(
compiler_config: Box<dyn CompilerConfig>,
target: Target,
features: Features,
) -> Self {
Self {
inner: Arc::new(Mutex::new(EngineInner {
builder: EngineBuilder::new(Some(compiler), features),
compiler: Some(compiler_config.compiler()),
features,
#[cfg(not(target_arch = "wasm32"))]
code_memory: vec![],
#[cfg(not(target_arch = "wasm32"))]
signatures: SignatureRegistry::new(),
})),
target: Arc::new(target),
@@ -61,8 +75,11 @@ impl Engine {
pub fn headless() -> Self {
Self {
inner: Arc::new(Mutex::new(EngineInner {
builder: EngineBuilder::new(None, Features::default()),
compiler: None,
features: Features::default(),
#[cfg(not(target_arch = "wasm32"))]
code_memory: vec![],
#[cfg(not(target_arch = "wasm32"))]
signatures: SignatureRegistry::new(),
})),
target: Arc::new(Target::default()),
@@ -70,11 +87,13 @@ impl Engine {
}
}
pub(crate) fn inner(&self) -> std::sync::MutexGuard<'_, EngineInner> {
/// Get reference to `EngineInner`.
pub fn inner(&self) -> std::sync::MutexGuard<'_, EngineInner> {
self.inner.lock().unwrap()
}
pub(crate) fn inner_mut(&self) -> std::sync::MutexGuard<'_, EngineInner> {
/// Get mutable reference to `EngineInner`.
pub fn inner_mut(&self) -> std::sync::MutexGuard<'_, EngineInner> {
self.inner.lock().unwrap()
}
@@ -84,12 +103,14 @@ impl Engine {
}
/// Register a signature
#[cfg(not(target_arch = "wasm32"))]
pub fn register_signature(&self, func_type: &FunctionType) -> VMSharedSignatureIndex {
let compiler = self.inner();
compiler.signatures().register(func_type)
}
/// Lookup a signature
#[cfg(not(target_arch = "wasm32"))]
pub fn lookup_signature(&self, sig: VMSharedSignatureIndex) -> Option<FunctionType> {
let compiler = self.inner();
compiler.signatures().lookup(sig)
@@ -101,7 +122,8 @@ impl Engine {
}
/// Compile a WebAssembly binary
#[cfg(feature = "engine_compilation")]
#[cfg(feature = "compiler")]
#[cfg(not(target_arch = "wasm32"))]
pub fn compile(
&self,
binary: &[u8],
@@ -111,7 +133,8 @@ impl Engine {
}
/// Compile a WebAssembly binary
#[cfg(not(feature = "engine_compilation"))]
#[cfg(not(feature = "compiler"))]
#[cfg(not(target_arch = "wasm32"))]
pub fn compile(
&self,
_binary: &[u8],
@@ -122,6 +145,7 @@ impl Engine {
))
}
#[cfg(not(target_arch = "wasm32"))]
/// Deserializes a WebAssembly module
///
/// # Safety
@@ -131,6 +155,7 @@ impl Engine {
Ok(Arc::new(Artifact::deserialize(self, bytes)?))
}
#[cfg(not(target_arch = "wasm32"))]
/// Deserializes a WebAssembly module from a path
///
/// # Safety
@@ -162,38 +187,47 @@ impl Engine {
/// The inner contents of `Engine`
pub struct EngineInner {
/// The builder (include compiler and cpu features)
builder: EngineBuilder,
#[cfg(feature = "compiler")]
/// The compiler and cpu features
compiler: Option<Box<dyn Compiler>>,
#[cfg(feature = "compiler")]
/// The compiler and cpu features
features: Features,
/// The code memory is responsible of publishing the compiled
/// functions to memory.
#[cfg(not(target_arch = "wasm32"))]
code_memory: Vec<CodeMemory>,
/// The signature registry is used mainly to operate with trampolines
/// performantly.
#[cfg(not(target_arch = "wasm32"))]
signatures: SignatureRegistry,
}
impl EngineInner {
/// Gets the compiler associated to this engine.
#[cfg(feature = "engine_compilation")]
#[cfg(feature = "compiler")]
pub fn compiler(&self) -> Result<&dyn Compiler, CompileError> {
self.builder.compiler()
match self.compiler.as_ref() {
None => Err(CompileError::Codegen(
"The Engine is not compiled in.".to_string(),
)),
Some(compiler) => Ok(&**compiler),
}
}
/// Validate the module
pub fn validate(&self, data: &[u8]) -> Result<(), CompileError> {
self.builder.validate(data)
let compiler = self.compiler()?;
compiler.validate_module(&self.features, data)
}
/// The Wasm features
pub fn features(&self) -> &Features {
self.builder.features()
}
pub fn builder_mut(&mut self) -> &mut EngineBuilder {
&mut self.builder
&self.features
}
/// Allocate compiled functions into memory
#[cfg(not(target_arch = "wasm32"))]
#[allow(clippy::type_complexity)]
pub(crate) fn allocate(
&mut self,
@@ -286,11 +320,13 @@ impl EngineInner {
))
}
#[cfg(not(target_arch = "wasm32"))]
/// Make memory containing compiled code executable.
pub(crate) fn publish_compiled_code(&mut self) {
self.code_memory.last_mut().unwrap().publish();
}
#[cfg(not(target_arch = "wasm32"))]
/// Register DWARF-type exception handling information associated with the code.
pub(crate) fn publish_eh_frame(&mut self, eh_frame: Option<&[u8]>) -> Result<(), CompileError> {
self.code_memory
@@ -305,6 +341,7 @@ impl EngineInner {
}
/// Shared signature registry.
#[cfg(not(target_arch = "wasm32"))]
pub fn signatures(&self) -> &SignatureRegistry {
&self.signatures
}

View File

@@ -1,35 +1,48 @@
//! The Wasmer Engine.
mod error;
#[cfg(not(target_arch = "wasm32"))]
mod resolver;
#[cfg(not(target_arch = "wasm32"))]
mod trap;
#[cfg(not(target_arch = "wasm32"))]
mod tunables;
#[cfg(feature = "translator")]
#[cfg(not(target_arch = "wasm32"))]
mod artifact;
#[cfg(feature = "translator")]
mod backend;
mod builder;
#[cfg(feature = "translator")]
#[cfg(not(target_arch = "wasm32"))]
mod code_memory;
#[cfg(feature = "translator")]
mod inner;
#[cfg(feature = "translator")]
#[cfg(not(target_arch = "wasm32"))]
mod link;
#[cfg(feature = "translator")]
#[cfg(not(target_arch = "wasm32"))]
mod unwind;
pub use self::error::{InstantiationError, LinkError};
#[cfg(not(target_arch = "wasm32"))]
pub use self::resolver::resolve_imports;
#[cfg(not(target_arch = "wasm32"))]
pub use self::trap::*;
#[cfg(not(target_arch = "wasm32"))]
pub use self::tunables::Tunables;
#[cfg(feature = "translator")]
#[cfg(not(target_arch = "wasm32"))]
pub use self::artifact::Artifact;
#[cfg(feature = "translator")]
pub use self::backend::Backend;
pub use self::builder::EngineBuilder;
#[cfg(feature = "translator")]
#[cfg(not(target_arch = "wasm32"))]
pub use self::code_memory::CodeMemory;
#[cfg(feature = "translator")]
pub use self::inner::Engine;
pub use self::inner::{Engine, EngineInner};
#[cfg(feature = "translator")]
#[cfg(not(target_arch = "wasm32"))]
pub use self::link::link_module;

View File

@@ -51,11 +51,9 @@ mod lib {
}
}
#[cfg(not(target_arch = "wasm32"))]
mod engine;
mod traits;
#[cfg(not(target_arch = "wasm32"))]
pub use crate::engine::*;
pub use crate::traits::*;

View File

@@ -354,7 +354,7 @@ mod tests {
use std::sync::Arc;
use wasmer::{
imports, wat2wasm, Backend, CompilerConfig, Cranelift, Module, Store, TypedFunction,
imports, wat2wasm, CompilerConfig, Cranelift, EngineBuilder, Module, Store, TypedFunction,
};
fn cost_function(operator: &Operator) -> u64 {
@@ -386,7 +386,8 @@ mod tests {
let metering = Arc::new(Metering::new(10, cost_function));
let mut compiler_config = Cranelift::default();
compiler_config.push_middleware(metering);
let mut store = Store::new_with_engine(&Backend::new(compiler_config).engine());
let mut store =
Store::new_with_engine(&EngineBuilder::new(compiler_config, None, None).engine());
let module = Module::new(&store, bytecode()).unwrap();
// Instantiate
@@ -434,7 +435,8 @@ mod tests {
let metering = Arc::new(Metering::new(10, cost_function));
let mut compiler_config = Cranelift::default();
compiler_config.push_middleware(metering);
let mut store = Store::new_with_engine(&Backend::new(compiler_config).engine());
let mut store =
Store::new_with_engine(&EngineBuilder::new(compiler_config, None, None).engine());
let module = Module::new(&store, bytecode()).unwrap();
// Instantiate

View File

@@ -49,7 +49,7 @@ tracing-wasm = "0.2"
default = ["sys-default"]
sys = ["wasmer/sys"]
sys-default = ["wasmer/wat", "wasmer/default-engine_compilation", "sys", "logging", "host-fs", "sys-poll", "host-vnet" ]
sys-default = ["wasmer/wat", "wasmer/compiler", "sys", "logging", "host-fs", "sys-poll", "host-vnet" ]
sys-poll = []
js = ["wasmer/js", "mem-fs", "wasmer-vfs/no-time", "getrandom/js", "chrono"]

View File

@@ -51,15 +51,15 @@ impl Config {
}
pub fn engine(&self, compiler_config: Box<dyn CompilerConfig>) -> Box<Engine> {
let mut engine = wasmer_compiler::Backend::new(compiler_config);
let mut engine = wasmer_compiler::EngineBuilder::new(compiler_config, None, None);
if let Some(ref features) = self.features {
engine = engine.features(features.clone())
engine.set_features(Some(features.clone()));
}
Box::new(engine.engine())
}
pub fn engine_headless(&self) -> Box<Engine> {
Box::new(wasmer_compiler::Backend::headless().engine())
Box::new(wasmer_compiler::EngineBuilder::headless().engine())
}
pub fn compiler_config(