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:
@@ -1,9 +1,9 @@
|
||||
//! This file defines the extern "C" API extension, which are specific
|
||||
//! to the wasmtime implementation.
|
||||
|
||||
use crate::{wasm_byte_vec_t, wasm_config_t, wasm_engine_t};
|
||||
use crate::*;
|
||||
use std::str;
|
||||
use wasmtime::{OptLevel, ProfilingStrategy, Strategy};
|
||||
use wasmtime::{Extern, Linker, OptLevel, ProfilingStrategy, Strategy};
|
||||
|
||||
#[repr(u8)]
|
||||
#[derive(Clone)]
|
||||
@@ -136,3 +136,80 @@ pub unsafe extern "C" fn wasmtime_wat2wasm(
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[repr(C)]
|
||||
pub struct wasmtime_linker_t {
|
||||
linker: Linker,
|
||||
}
|
||||
|
||||
#[no_mangle]
|
||||
pub unsafe extern "C" fn wasmtime_linker_new(store: *mut wasm_store_t) -> *mut wasmtime_linker_t {
|
||||
Box::into_raw(Box::new(wasmtime_linker_t {
|
||||
linker: Linker::new(&(*store).store.borrow()),
|
||||
}))
|
||||
}
|
||||
|
||||
#[no_mangle]
|
||||
pub unsafe extern "C" fn wasmtime_linker_delete(linker: *mut wasmtime_linker_t) {
|
||||
drop(Box::from_raw(linker));
|
||||
}
|
||||
|
||||
#[no_mangle]
|
||||
pub unsafe extern "C" fn wasmtime_linker_define(
|
||||
linker: *mut wasmtime_linker_t,
|
||||
module: *const wasm_name_t,
|
||||
name: *const wasm_name_t,
|
||||
item: *const wasm_extern_t,
|
||||
) -> bool {
|
||||
let linker = &mut (*linker).linker;
|
||||
let module = match str::from_utf8((*module).as_slice()) {
|
||||
Ok(s) => s,
|
||||
Err(_) => return false,
|
||||
};
|
||||
let name = match str::from_utf8((*name).as_slice()) {
|
||||
Ok(s) => s,
|
||||
Err(_) => return false,
|
||||
};
|
||||
let item = match &(*item).which {
|
||||
ExternHost::Func(e) => Extern::Func(e.borrow().clone()),
|
||||
ExternHost::Table(e) => Extern::Table(e.borrow().clone()),
|
||||
ExternHost::Global(e) => Extern::Global(e.borrow().clone()),
|
||||
ExternHost::Memory(e) => Extern::Memory(e.borrow().clone()),
|
||||
};
|
||||
linker.define(module, name, item).is_ok()
|
||||
}
|
||||
|
||||
#[no_mangle]
|
||||
pub unsafe extern "C" fn wasmtime_linker_define_wasi(
|
||||
linker: *mut wasmtime_linker_t,
|
||||
instance: *const wasi_instance_t,
|
||||
) -> bool {
|
||||
let linker = &mut (*linker).linker;
|
||||
(*instance).wasi.add_to_linker(linker).is_ok()
|
||||
}
|
||||
|
||||
#[no_mangle]
|
||||
pub unsafe extern "C" fn wasmtime_linker_define_instance(
|
||||
linker: *mut wasmtime_linker_t,
|
||||
name: *const wasm_name_t,
|
||||
instance: *const wasm_instance_t,
|
||||
) -> bool {
|
||||
let linker = &mut (*linker).linker;
|
||||
let name = match str::from_utf8((*name).as_slice()) {
|
||||
Ok(s) => s,
|
||||
Err(_) => return false,
|
||||
};
|
||||
linker
|
||||
.instance(name, &(*instance).instance.borrow())
|
||||
.is_ok()
|
||||
}
|
||||
|
||||
#[no_mangle]
|
||||
pub unsafe extern "C" fn wasmtime_linker_instantiate(
|
||||
linker: *const wasmtime_linker_t,
|
||||
module: *const wasm_module_t,
|
||||
trap: *mut *mut wasm_trap_t,
|
||||
) -> *mut wasm_instance_t {
|
||||
let linker = &(*linker).linker;
|
||||
handle_instantiate(linker.instantiate(&(*module).module.borrow()), trap)
|
||||
}
|
||||
|
||||
@@ -5,6 +5,7 @@
|
||||
|
||||
// TODO complete the C API
|
||||
|
||||
use anyhow::Result;
|
||||
use std::cell::RefCell;
|
||||
use std::panic::{self, AssertUnwindSafe};
|
||||
use std::{mem, ptr, slice};
|
||||
@@ -691,6 +692,15 @@ pub unsafe extern "C" fn wasm_instance_delete(instance: *mut wasm_instance_t) {
|
||||
let _ = Box::from_raw(instance);
|
||||
}
|
||||
|
||||
impl wasm_instance_t {
|
||||
fn new(instance: Instance) -> wasm_instance_t {
|
||||
wasm_instance_t {
|
||||
instance: HostRef::new(instance),
|
||||
exports_cache: RefCell::new(None),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[no_mangle]
|
||||
pub unsafe extern "C" fn wasm_instance_new(
|
||||
store: *mut wasm_store_t,
|
||||
@@ -722,12 +732,16 @@ pub unsafe extern "C" fn wasm_instance_new(
|
||||
}
|
||||
return ptr::null_mut();
|
||||
}
|
||||
match Instance::new(module, &externs) {
|
||||
handle_instantiate(Instance::new(module, &externs), result)
|
||||
}
|
||||
|
||||
unsafe fn handle_instantiate(
|
||||
instance: Result<Instance>,
|
||||
result: *mut *mut wasm_trap_t,
|
||||
) -> *mut wasm_instance_t {
|
||||
match instance {
|
||||
Ok(instance) => {
|
||||
let instance = Box::new(wasm_instance_t {
|
||||
instance: HostRef::new(instance),
|
||||
exports_cache: RefCell::new(None),
|
||||
});
|
||||
let instance = Box::new(wasm_instance_t::new(instance));
|
||||
if !result.is_null() {
|
||||
(*result) = ptr::null_mut();
|
||||
}
|
||||
|
||||
@@ -168,7 +168,7 @@ pub unsafe extern "C" fn wasi_config_preopen_dir(
|
||||
|
||||
#[repr(C)]
|
||||
pub struct wasi_instance_t {
|
||||
wasi: Wasi,
|
||||
pub wasi: Wasi,
|
||||
export_cache: HashMap<String, Box<wasm_extern_t>>,
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user