Skip to content

Commit

Permalink
Merge #2940
Browse files Browse the repository at this point in the history
2940: Merge `wasmer3` back to `master` branch r=epilys a=epilys

Since the next planned release is 3.0, we can move its development in the main branch.

Co-authored-by: Amanieu d'Antras <amanieu@gmail.com>
Co-authored-by: Manos Pitsidianakis <manos@wasmer.io>
Co-authored-by: Wolfgang Silbermayr <wolfgang@silbermayr.at>
  • Loading branch information
4 people authored Jun 8, 2022
2 parents 447c2e3 + da4e9fe commit 6c7ce0b
Show file tree
Hide file tree
Showing 190 changed files with 3,898 additions and 4,031 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,10 @@ Looking for changes that affect our C API? See the [C API Changelog](lib/c-api/C
- [#2802](https://github.com/wasmerio/wasmer/pull/2802) Support Dylib engine with Singlepass
- [#2836](https://github.com/wasmerio/wasmer/pull/2836) Improve TrapInformation data stored at runtime
- [#2864](https://github.com/wasmerio/wasmer/pull/2864) `wasmer-cli`: remove wasi-experimental-io-devices from default builds
- #2864 wasmer-cli: remove wasi-experimental-io-devices from default builds
- [#2933](https://github.com/wasmerio/wasmer/pull/2933) Rename NativeFunc to TypedFunction.
### Changed
- #2868 Removed loupe crate dependency

### Fixed
- [#2829](https://github.com/wasmerio/wasmer/pull/2829) Improve error message oriented from JS object.
Expand Down
45 changes: 4 additions & 41 deletions Cargo.lock

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

1 change: 0 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,6 @@ serial_test = "0.5"
wasmer-engine-dummy = { path = "tests/lib/engine-dummy" }
compiler-test-derive = { path = "tests/lib/compiler-test-derive" }
tempfile = "3.1"
loupe = "0.1"
# For logging tests using the `RUST_LOG=debug` when testing
test-log = { version = "0.2", default-features = false, features = ["trace"] }
tracing = { version = "0.1", default-features = false, features = ["log"] }
Expand Down
4 changes: 2 additions & 2 deletions benches/static_and_dynamic_functions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ pub fn run_basic_static_function(store: &Store, compiler_name: &str, c: &mut Cri
};
let instance = Instance::new(&module, &import_object).unwrap();
let dyn_f: &Function = instance.exports.get("add").unwrap();
let f: NativeFunc<(i32, i32), i32> = dyn_f.native().unwrap();
let f: TypedFunction<(i32, i32), i32> = dyn_f.native().unwrap();

c.bench_function(&format!("basic static func {}", compiler_name), |b| {
b.iter(|| {
Expand All @@ -50,7 +50,7 @@ pub fn run_basic_static_function(store: &Store, compiler_name: &str, c: &mut Cri
});

let dyn_f_many: &Function = instance.exports.get("add20").unwrap();
let f_many: NativeFunc<
let f_many: TypedFunction<
(
i32,
i32,
Expand Down
95 changes: 95 additions & 0 deletions docs/migration_to_3.0.0.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
# Migrating from Wasmer 2.x to Wasmer 3.0.0

This document will describe the differences between Wasmer 2.x and Wasmer 3.0.0
and provide examples to make migrating to the new API as simple as possible.

## Table of Contents

- [Rationale for changes in 3.0.0](#rationale-for-changes-in-300)
- [How to use Wasmer 3.0.0](#how-to-use-wasmer-300)
- [Installing Wasmer CLI](#installing-wamser-cli)
- [Using Wasmer 3.0.0](#using-wamser-300)
- [Project structure](#project-structure)
- [Differences](#differences)
- [Managing imports](#managing-imports)

## Rationale for changes in 3.0.0

This version introduces the following changes to make the Wasmer API more ergonomic and safe:

1. `ImportsObject` and the traits `Resolver`, `NamedResolver`, etc have been removed and replaced with a single simple type `Imports`. This reduces the complexity of setting up an `Instance`. The helper macro `imports!` can still be used.

## How to use Wasmer 3.0.0

### Installing Wasmer CLI

See [wasmer.io] for installation instructions.

If you already have wasmer installed, run `wasmer self-update`.

Install the latest versions of Wasmer with [wasmer-nightly] or by following the
steps described in the documentation: [Getting Started][getting-started].

### Using Wasmer 3.0.0

See the [examples] to find out how to do specific things in Wasmer 3.0.0.

## Project Structure

TODO

## Differences

### Managing imports

Instantiating a Wasm module is similar to 2.x.x.:

```rust
let import_object: Imports = imports! {
"env" => {
"host_function" => host_function,
},
};
let instance = Instance::new(&module, &import_object).expect("Could not instantiate module.");
```

You can also build the `Imports` object manually:

```rust
let mut import_object: Imports = Imports::new();
import_object.define("env", "host_function", host_function);
let instance = Instance::new(&module, &import_object).expect("Could not instantiate module.");
```

#### `ChainableNamedResolver` is removed

Chaining imports with a trait has been deemed too complex for what it does; it's possible to chain (i.e. override) an `Imports`' contents by using its implementation of `std::iter::Extend` from the Rust standard library:

```rust
let imports1: Imports = todo!();
let mut imports2: Imports = todo!();

imports2.extend(&imports);
// This is equivalent to the following:
// for ((ns, name), ext) in imports1.into_iter() {
// imports2.define(&ns &name, ext);
// }
```

[examples]: https://docs.wasmer.io/integrations/examples
[wasmer]: https://crates.io/crates/wasmer
[wasmer-wasi]: https://crates.io/crates/wasmer-wasi
[wasmer-emscripten]: https://crates.io/crates/wasmer-emscripten
[wasmer-engine]: https://crates.io/crates/wasmer-engine
[wasmer-compiler]: https://crates.io/crates/wasmer-compiler
[wasmer.io]: https://wasmer.io
[wasmer-nightly]: https://github.com/wasmerio/wasmer-nightly/
[getting-started]: https://docs.wasmer.io/ecosystem/wasmer/getting-started
[instance-example]: https://docs.wasmer.io/integrations/examples/instance
[imports-exports-example]: https://docs.wasmer.io/integrations/examples/imports-and-exports
[host-functions-example]: https://docs.wasmer.io/integrations/examples/host-functions
[memory]: https://docs.wasmer.io/integrations/examples/memory
[memory-pointers]: https://docs.wasmer.io/integrations/examples/memory-pointers
[host-functions]: https://docs.wasmer.io/integrations/examples/host-functions
[errors]: https://docs.wasmer.io/integrations/examples/errors
[exit-early]: https://docs.wasmer.io/integrations/examples/exit-early
4 changes: 2 additions & 2 deletions examples/early_exit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@

use anyhow::bail;
use std::fmt;
use wasmer::{imports, wat2wasm, Function, Instance, Module, NativeFunc, Store};
use wasmer::{imports, wat2wasm, Function, Instance, Module, Store, TypedFunction};
use wasmer_compiler_cranelift::Cranelift;
use wasmer_engine_universal::Universal;

Expand Down Expand Up @@ -82,7 +82,7 @@ fn main() -> anyhow::Result<()> {
//
// Get the `run` function which we'll use as our entrypoint.
println!("Calling `run` function...");
let run_func: NativeFunc<(i32, i32), i32> = instance.exports.get_native_function("run")?;
let run_func: TypedFunction<(i32, i32), i32> = instance.exports.get_native_function("run")?;

// When we call a function it can either succeed or fail. We expect it to fail.
match run_func.call(1, 7) {
Expand Down
2 changes: 1 addition & 1 deletion examples/exports_function.rs
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {
assert_eq!(result.to_vec(), vec![Value::I32(3)]);

// That was fun. But what if we can get rid of the `Value`s? Well,
// that's possible with the `NativeFunction` API. The function
// that's possible with the `TypedFunction` API. The function
// will use native Rust values.
//
// Note that `native` takes 2 generic parameters: `Args` and
Expand Down
Loading

0 comments on commit 6c7ce0b

Please sign in to comment.