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

Implement new Context API for Wasmer 3.0 #2892

Merged
merged 24 commits into from
Jul 19, 2022
Merged

Implement new Context API for Wasmer 3.0 #2892

merged 24 commits into from
Jul 19, 2022

Conversation

Amanieu
Copy link
Contributor

@Amanieu Amanieu commented May 20, 2022

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 from Wasmtime.

TODO

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

Amanieu and others added 24 commits July 19, 2022 15:31
It was never fully implemented and we're moving to a different way of
tracking ExternRef lifetimes.
Co-authored-by: ptitSeb <[email protected]>
Co-authored-by: Manos Pitsidianakis <[email protected]>
@epilys
Copy link
Contributor

epilys commented Jul 19, 2022

bors r+

@bors
Copy link
Contributor

bors bot commented Jul 19, 2022

Build succeeded:

@bors bors bot merged commit 06474c1 into master Jul 19, 2022
@bors bors bot deleted the context_api branch July 19, 2022 12:38
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment