Skip to content

Commit

Permalink
Merge #2892
Browse files Browse the repository at this point in the history
2892: Implement new Context API for Wasmer 3.0 r=epilys a=Amanieu

### Overview

This PR reworks the way Wasmer manages objects used by instances, specifically: `Instance`, `Memory`, `Table`, `Function`, `Global` and `ExternRef`. Previously these were tracked using reference counting, but this cannot work fully because Wasm code can manipulate references to functions (and by implication the instance that function is from) using tables. 

The new approach uses a `Context` type which owns all of the objects listed above: objects are only freed when the `Context` is dropped. The `Instance`, `Memory`, `Table`, `Function`, `Global` and `ExternRef` types are now simply wrappers around an integer which indexes a vector in the `Context`. As a result, any function on those "handle" types now need to take a `&Context` or `&mut Context` as a parameter to access the actual data in the context. This also vastly simplifies Wasmer's thread-safety story: since all mutating operations require a `&mut Context`, no synchronization or internal locks are needed.

Additionally, the way host functions are represented is also changed. The `WasmerEnv` trait is removed since it was unergonomic and hard to use correctly. Instead, the state for host functions is stored directly as the `T` in `Context<T>`. This state is initialized when the context is created, and can be accessed through the `data` and `data_mut` methods on `Context<T>`. All host functions receive a `ContextMut<'_, T>` as their first argument, which gives them access to the host state of the context. This is much better than the old approach since it provides mutable access without locks and allows all host functions to share the same state.

This design is heavily inspired by [this RFC](https://github.com/bytecodealliance/rfcs/blob/main/accepted/new-api.md) from Wasmtime.

### TODO

- [x] #2985
- [x] #2908
- [x] #2973 Upgrade `c-api` to use the new API
- [x] Update wasmer-wasi to use the new API
- [x] #2909
- [x] #2913
- [x] #2912 
- #2911  (Non blocking)
- [ ] #2910

Fixes #2893
Fixes #2873 
Fixes #2866
Fixes #1840 
Fixes #1734
Fixes #1632
Fixes #1522
Fixes #1630 
Fixes #2838
Fixes #2856 
Fixes #2544
Fixes #2816 

Co-authored-by: Amanieu d'Antras <amanieu@gmail.com>
Co-authored-by: ptitSeb <sebastien.chev@gmail.com>
Co-authored-by: Manos Pitsidianakis <manos@wasmer.io>
Co-authored-by: Syrus Akbary <me@syrusakbary.com>
  • Loading branch information
5 people authored Jul 19, 2022
2 parents 6f768cb + 9773235 commit 06474c1
Show file tree
Hide file tree
Showing 227 changed files with 12,591 additions and 12,682 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ Looking for changes that affect our C API? See the [C API Changelog](lib/c-api/C
- [#2999](https://github.com/wasmerio/wasmer/pull/2999) Allow `--invoke` CLI option for Emscripten files without a `main` function
- [#2946](https://github.com/wasmerio/wasmer/pull/2946) Remove dylib,staticlib engines in favor of a single Universal engine
- [#2949](https://github.com/wasmerio/wasmer/pull/2949) Switch back to using custom LLVM builds on CI
- #2892 Renamed `get_native_function` to `get_typed_function`, marked former as deprecated.

### Fixed
- [#2963](https://github.com/wasmerio/wasmer/pull/2963) Remove accidental dependency on libwayland and libxcb in ClI
Expand Down
151 changes: 100 additions & 51 deletions Cargo.lock

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

Loading

0 comments on commit 06474c1

Please sign in to comment.