Add a wasmtime::Linker type (#1384)

* Add a `wasmtime::Linker` type

This commit adds a new type to the `wasmtime` crate, a `Linker`. This
linker is intended to vastly simplify calling `Instance::new` by easily
performing name resolution and incrementally defining state over time.
The goal here is to start down a path of making linking wasm modules in
`wasmtime` a first-class and ergonomic operation. This is highly likely
to evolve over time and get tweaked through releases as we iterate
towards a design well-suited for `wasmtime`, but this is intended to at
least be the initial foundation for such functionality.

This commit additionally also adds a C API for the linker and switches
the existing linking examples to using this linker in both Rust and C.

One piece of future work I'd like to tackle next is to integrate WASI
into the `wasmtime` crate in a more first-class manner. This [`Linker`]
type provides a great location to hook into the instantiation process to
easily instantiate modules with WASI imports. That's a relatively large
refactoring for now though and I figured it'd be best left for a
different time.

Closes #727
This commit is contained in:
Alex Crichton
2020-03-23 21:02:31 -05:00
committed by GitHub
parent 021ebb3748
commit 0d4bde4ab3
13 changed files with 630 additions and 110 deletions

View File

@@ -4,6 +4,7 @@
#define WASMTIME_API_H
#include <wasm.h>
#include <wasi.h>
#ifdef __cplusplus
extern "C" {
@@ -67,6 +68,39 @@ WASM_API_EXTERN bool wasmtime_wat2wasm(
own wasm_byte_vec_t *error_message
);
#define WASMTIME_DECLARE_OWN(name) \
typedef struct wasmtime_##name##_t wasmtime_##name##_t; \
\
WASM_API_EXTERN void wasmtime_##name##_delete(own wasmtime_##name##_t*);
WASMTIME_DECLARE_OWN(linker)
WASM_API_EXTERN own wasmtime_linker_t* wasmtime_linker_new(wasm_store_t* store);
WASM_API_EXTERN bool wasmtime_linker_define(
wasmtime_linker_t *linker,
const wasm_name_t *module,
const wasm_name_t *name,
const wasm_extern_t *item
);
WASM_API_EXTERN bool wasmtime_linker_define_wasi(
wasmtime_linker_t *linker,
const wasi_instance_t *instance
);
WASM_API_EXTERN bool wasmtime_linker_define_instance(
wasmtime_linker_t *linker,
const wasm_name_t *name,
const wasm_instance_t *instance
);
WASM_API_EXTERN wasm_instance_t* wasmtime_linker_instantiate(
const wasmtime_linker_t *linker,
const wasm_module_t *module,
own wasm_trap_t **trap
);
#undef own
#ifdef __cplusplus