* Compute instance exports on demand.

Instead having instances eagerly compute a Vec of Externs, and bumping
the refcount for each Extern, compute Externs on demand.

This also enables `Instance::get_export` to avoid doing a linear search.

This also means that the closure returned by `get0` and friends now
holds an `InstanceHandle` to dynamically hold the instance live rather
than being scoped to a lifetime.

* Compute module imports and exports on demand too.

And compute Extern::ty on demand too.

* Add a utility function for computing an ExternType.

* Add a utility function for looking up a function's signature.

* Add a utility function for computing the ValType of a Global.

* Rename wasmtime_environ::Export to EntityIndex.

This helps differentiate it from other Export types in the tree, and
describes what it is.

* Fix a typo in a comment.

* Simplify module imports and exports.

* Make `Instance::exports` return the export names.

This significantly simplifies the public API, as it's relatively common
to need the names, and this avoids the need to do a zip with
`Module::exports`.

This also changes `ImportType` and `ExportType` to have public members
instead of private members and accessors, as I find that simplifies the
usage particularly in cases where there are temporary instances.

* Remove `Instance::module`.

This doesn't quite remove `Instance`'s `module` member, it gets a step
closer.

* Use a InstanceHandle utility function.

* Don't consume self in the `Func::get*` methods.

Instead, just create a closure containing the instance handle and the
export for them to call.

* Use `ExactSizeIterator` to avoid needing separate `num_*` methods.

* Rename `Extern::func()` etc. to `into_func()` etc.

* Revise examples to avoid using `nth`.

* Add convenience methods to instance for getting specific extern types.

* Use the convenience functions in more tests and examples.

* Avoid cloning strings for `ImportType` and `ExportType`.

* Remove more obviated clone() calls.

* Simplify `Func`'s closure state.

* Make wasmtime::Export's fields private.

This makes them more consistent with ExportType.

* Fix compilation error.

* Make a lifetime parameter explicit, and use better lifetime names.

Instead of 'me, use 'instance and 'module to make it clear what the
lifetime is.

* More lifetime cleanups.
This commit is contained in:
Dan Gohman
2020-04-20 13:55:33 -07:00
committed by GitHub
parent 967827f4b5
commit 9364eb1d98
57 changed files with 788 additions and 875 deletions

View File

@@ -41,10 +41,10 @@ pub extern "C" fn wasm_extern_kind(e: &wasm_extern_t) -> wasm_externkind_t {
#[no_mangle]
pub extern "C" fn wasm_extern_type(e: &wasm_extern_t) -> Box<wasm_externtype_t> {
let ty = match &e.which {
ExternHost::Func(f) => ExternType::Func(f.borrow().ty().clone()),
ExternHost::Global(f) => ExternType::Global(f.borrow().ty().clone()),
ExternHost::Table(f) => ExternType::Table(f.borrow().ty().clone()),
ExternHost::Memory(f) => ExternType::Memory(f.borrow().ty().clone()),
ExternHost::Func(f) => ExternType::Func(f.borrow().ty()),
ExternHost::Global(f) => ExternType::Global(f.borrow().ty()),
ExternHost::Table(f) => ExternType::Table(f.borrow().ty()),
ExternHost::Memory(f) => ExternType::Memory(f.borrow().ty()),
};
Box::new(wasm_externtype_t::new(ty))
}

View File

@@ -246,7 +246,7 @@ fn _wasmtime_func_call(
#[no_mangle]
pub extern "C" fn wasm_func_type(f: &wasm_func_t) -> Box<wasm_functype_t> {
Box::new(wasm_functype_t::new(f.func().borrow().ty().clone()))
Box::new(wasm_functype_t::new(f.func().borrow().ty()))
}
#[no_mangle]
@@ -272,10 +272,10 @@ pub unsafe extern "C" fn wasmtime_caller_export_get(
let name = str::from_utf8(name.as_slice()).ok()?;
let export = caller.caller.get_export(name)?;
let which = match export {
Extern::Func(f) => ExternHost::Func(HostRef::new(f.clone())),
Extern::Global(g) => ExternHost::Global(HostRef::new(g.clone())),
Extern::Memory(m) => ExternHost::Memory(HostRef::new(m.clone())),
Extern::Table(t) => ExternHost::Table(HostRef::new(t.clone())),
Extern::Func(f) => ExternHost::Func(HostRef::new(f)),
Extern::Global(g) => ExternHost::Global(HostRef::new(g)),
Extern::Memory(m) => ExternHost::Memory(HostRef::new(m)),
Extern::Table(t) => ExternHost::Table(HostRef::new(t)),
};
Some(Box::new(wasm_extern_t { which }))
}

View File

@@ -71,7 +71,7 @@ pub extern "C" fn wasm_global_as_extern(g: &wasm_global_t) -> &wasm_extern_t {
#[no_mangle]
pub extern "C" fn wasm_global_type(g: &wasm_global_t) -> Box<wasm_globaltype_t> {
let globaltype = g.global().borrow().ty().clone();
let globaltype = g.global().borrow().ty();
Box::new(wasm_globaltype_t::new(globaltype))
}

View File

@@ -145,12 +145,11 @@ pub extern "C" fn wasm_instance_exports(instance: &wasm_instance_t, out: &mut wa
let instance = &instance.instance.borrow();
instance
.exports()
.iter()
.map(|e| match e {
Extern::Func(f) => ExternHost::Func(HostRef::new(f.clone())),
Extern::Global(f) => ExternHost::Global(HostRef::new(f.clone())),
Extern::Memory(f) => ExternHost::Memory(HostRef::new(f.clone())),
Extern::Table(f) => ExternHost::Table(HostRef::new(f.clone())),
.map(|e| match e.into_extern() {
Extern::Func(f) => ExternHost::Func(HostRef::new(f)),
Extern::Global(f) => ExternHost::Global(HostRef::new(f)),
Extern::Memory(f) => ExternHost::Memory(HostRef::new(f)),
Extern::Table(f) => ExternHost::Table(HostRef::new(f)),
})
.collect()
});

View File

@@ -51,7 +51,7 @@ pub extern "C" fn wasm_memory_as_extern(m: &wasm_memory_t) -> &wasm_extern_t {
#[no_mangle]
pub extern "C" fn wasm_memory_type(m: &wasm_memory_t) -> Box<wasm_memorytype_t> {
let ty = m.memory().borrow().ty().clone();
let ty = m.memory().borrow().ty();
Box::new(wasm_memorytype_t::new(ty))
}

View File

@@ -46,13 +46,11 @@ pub extern "C" fn wasmtime_module_new(
handle_result(Module::from_binary(store, binary), |module| {
let imports = module
.imports()
.iter()
.map(|i| wasm_importtype_t::new(i.clone()))
.map(|i| wasm_importtype_t::new(i.module().to_owned(), i.name().to_owned(), i.ty()))
.collect::<Vec<_>>();
let exports = module
.exports()
.iter()
.map(|e| wasm_exporttype_t::new(e.clone()))
.map(|e| wasm_exporttype_t::new(e.name().to_owned(), e.ty()))
.collect::<Vec<_>>();
let module = Box::new(wasm_module_t {
module: HostRef::new(module),

View File

@@ -54,7 +54,7 @@ pub unsafe extern "C" fn wasm_table_new(
#[no_mangle]
pub extern "C" fn wasm_table_type(t: &wasm_table_t) -> Box<wasm_tabletype_t> {
let ty = t.table().borrow().ty().clone();
let ty = t.table().borrow().ty();
Box::new(wasm_tabletype_t::new(ty))
}

View File

@@ -1,12 +1,12 @@
use crate::{wasm_externtype_t, wasm_name_t};
use once_cell::unsync::OnceCell;
use std::str;
use wasmtime::ExportType;
use wasmtime::ExternType;
#[repr(C)]
#[derive(Clone)]
pub struct wasm_exporttype_t {
ty: ExportType,
name: String,
ty: ExternType,
name_cache: OnceCell<wasm_name_t>,
type_cache: OnceCell<wasm_externtype_t>,
}
@@ -14,8 +14,9 @@ pub struct wasm_exporttype_t {
wasmtime_c_api_macros::declare_ty!(wasm_exporttype_t);
impl wasm_exporttype_t {
pub(crate) fn new(ty: ExportType) -> wasm_exporttype_t {
pub(crate) fn new(name: String, ty: ExternType) -> wasm_exporttype_t {
wasm_exporttype_t {
name,
ty,
name_cache: OnceCell::new(),
type_cache: OnceCell::new(),
@@ -29,19 +30,18 @@ pub extern "C" fn wasm_exporttype_new(
ty: Box<wasm_externtype_t>,
) -> Option<Box<wasm_exporttype_t>> {
let name = name.take();
let name = str::from_utf8(&name).ok()?;
let ty = ExportType::new(name, ty.ty());
Some(Box::new(wasm_exporttype_t::new(ty)))
let name = String::from_utf8(name).ok()?;
Some(Box::new(wasm_exporttype_t::new(name, ty.ty())))
}
#[no_mangle]
pub extern "C" fn wasm_exporttype_name(et: &wasm_exporttype_t) -> &wasm_name_t {
et.name_cache
.get_or_init(|| wasm_name_t::from_name(&et.ty.name()))
.get_or_init(|| wasm_name_t::from_name(et.name.clone()))
}
#[no_mangle]
pub extern "C" fn wasm_exporttype_type(et: &wasm_exporttype_t) -> &wasm_externtype_t {
et.type_cache
.get_or_init(|| wasm_externtype_t::new(et.ty.ty().clone()))
.get_or_init(|| wasm_externtype_t::new(et.ty.clone()))
}

View File

@@ -1,12 +1,13 @@
use crate::{wasm_externtype_t, wasm_name_t};
use once_cell::unsync::OnceCell;
use std::str;
use wasmtime::ImportType;
use wasmtime::ExternType;
#[repr(C)]
#[derive(Clone)]
pub struct wasm_importtype_t {
pub(crate) ty: ImportType,
pub(crate) module: String,
pub(crate) name: String,
pub(crate) ty: ExternType,
module_cache: OnceCell<wasm_name_t>,
name_cache: OnceCell<wasm_name_t>,
type_cache: OnceCell<wasm_externtype_t>,
@@ -15,8 +16,10 @@ pub struct wasm_importtype_t {
wasmtime_c_api_macros::declare_ty!(wasm_importtype_t);
impl wasm_importtype_t {
pub(crate) fn new(ty: ImportType) -> wasm_importtype_t {
pub(crate) fn new(module: String, name: String, ty: ExternType) -> wasm_importtype_t {
wasm_importtype_t {
module,
name,
ty,
module_cache: OnceCell::new(),
name_cache: OnceCell::new(),
@@ -33,26 +36,25 @@ pub extern "C" fn wasm_importtype_new(
) -> Option<Box<wasm_importtype_t>> {
let module = module.take();
let name = name.take();
let module = str::from_utf8(&module).ok()?;
let name = str::from_utf8(&name).ok()?;
let ty = ImportType::new(module, name, ty.ty());
Some(Box::new(wasm_importtype_t::new(ty)))
let module = String::from_utf8(module).ok()?;
let name = String::from_utf8(name).ok()?;
Some(Box::new(wasm_importtype_t::new(module, name, ty.ty())))
}
#[no_mangle]
pub extern "C" fn wasm_importtype_module(it: &wasm_importtype_t) -> &wasm_name_t {
it.module_cache
.get_or_init(|| wasm_name_t::from_name(&it.ty.module()))
.get_or_init(|| wasm_name_t::from_name(it.module.clone()))
}
#[no_mangle]
pub extern "C" fn wasm_importtype_name(it: &wasm_importtype_t) -> &wasm_name_t {
it.name_cache
.get_or_init(|| wasm_name_t::from_name(&it.ty.name()))
.get_or_init(|| wasm_name_t::from_name(it.name.clone()))
}
#[no_mangle]
pub extern "C" fn wasm_importtype_type(it: &wasm_importtype_t) -> &wasm_externtype_t {
it.type_cache
.get_or_init(|| wasm_externtype_t::new(it.ty.ty().clone()))
.get_or_init(|| wasm_externtype_t::new(it.ty.clone()))
}

View File

@@ -9,8 +9,8 @@ use std::slice;
pub type wasm_name_t = wasm_byte_vec_t;
impl wasm_name_t {
pub(crate) fn from_name(name: &str) -> wasm_name_t {
name.to_string().into_bytes().into()
pub(crate) fn from_name(name: String) -> wasm_name_t {
name.into_bytes().into()
}
}

View File

@@ -7,6 +7,7 @@ use std::fs::File;
use std::os::raw::{c_char, c_int};
use std::path::{Path, PathBuf};
use std::slice;
use std::str;
use wasi_common::{
old::snapshot_0::WasiCtxBuilder as WasiSnapshot0CtxBuilder, preopen_dir,
WasiCtxBuilder as WasiPreview1CtxBuilder,
@@ -296,7 +297,7 @@ pub unsafe extern "C" fn wasi_instance_new(
})),
Err(e) => {
*trap = Box::into_raw(Box::new(wasm_trap_t {
trap: HostRef::new(Trap::new(e.to_string())),
trap: HostRef::new(Trap::new(e)),
}));
None
@@ -312,26 +313,26 @@ pub extern "C" fn wasi_instance_bind_import<'a>(
instance: &'a mut wasi_instance_t,
import: &wasm_importtype_t,
) -> Option<&'a wasm_extern_t> {
let module = import.ty.module();
let name = import.ty.name();
let module = &import.module;
let name = str::from_utf8(import.name.as_bytes()).ok()?;
let export = match &instance.wasi {
WasiInstance::Preview1(wasi) => {
if module != "wasi_snapshot_preview1" {
return None;
}
wasi.get_export(name)?
wasi.get_export(&name)?
}
WasiInstance::Snapshot0(wasi) => {
if module != "wasi_unstable" {
return None;
}
wasi.get_export(name)?
wasi.get_export(&name)?
}
};
if export.ty() != import.ty.ty().func()? {
if &export.ty() != import.ty.func()? {
return None;
}

View File

@@ -10,6 +10,6 @@ pub extern "C" fn wasmtime_wat2wasm(
Err(_) => return bad_utf8(),
};
handle_result(wat::parse_str(wat).map_err(|e| e.into()), |bytes| {
ret.set_buffer(bytes.into())
ret.set_buffer(bytes)
})
}