Disconnects Store state fields from Compiler (#1761)

*  Moves CodeMemory, VMInterrupts and SignatureRegistry from Compiler
*  CompiledModule holds CodeMemory and GdbJitImageRegistration
*  Store keeps track of its JIT code
*  Makes "jit_int.rs" stuff Send+Sync
*  Adds the threads example.
This commit is contained in:
Yury Delendik
2020-06-02 13:44:39 -05:00
committed by GitHub
parent b41330393d
commit 15c68f2cc1
61 changed files with 982 additions and 663 deletions

View File

@@ -244,6 +244,7 @@ WASM_API_EXTERN own wasmtime_error_t *wasmtime_global_set(
// instance is returned), or an instance can be returned (meaning no error or
// trap is returned).
WASM_API_EXTERN own wasmtime_error_t *wasmtime_instance_new(
wasm_store_t *store,
const wasm_module_t *module,
const wasm_extern_t* const imports[],
size_t num_imports,

View File

@@ -36,19 +36,10 @@ pub unsafe extern "C" fn wasm_instance_new(
imports: *const Box<wasm_extern_t>,
result: Option<&mut *mut wasm_trap_t>,
) -> Option<Box<wasm_instance_t>> {
let store = &store.store;
let module = &wasm_module.module.borrow();
if !Store::same(&store, module.store()) {
if let Some(result) = result {
let trap = Trap::new("wasm_store_t must match store in wasm_module_t");
let trap = Box::new(wasm_trap_t::new(store, trap));
*result = Box::into_raw(trap);
}
return None;
}
let mut instance = ptr::null_mut();
let mut trap = ptr::null_mut();
let err = wasmtime_instance_new(
store,
wasm_module,
imports,
wasm_module.imports.len(),
@@ -60,7 +51,7 @@ pub unsafe extern "C" fn wasm_instance_new(
assert!(trap.is_null());
assert!(instance.is_null());
if let Some(result) = result {
*result = Box::into_raw(err.to_trap(store));
*result = Box::into_raw(err.to_trap(&store.store));
}
None
}
@@ -83,6 +74,7 @@ pub unsafe extern "C" fn wasm_instance_new(
#[no_mangle]
pub unsafe extern "C" fn wasmtime_instance_new(
store: &wasm_store_t,
module: &wasm_module_t,
imports: *const Box<wasm_extern_t>,
num_imports: usize,
@@ -90,6 +82,7 @@ pub unsafe extern "C" fn wasmtime_instance_new(
trap_ptr: &mut *mut wasm_trap_t,
) -> Option<Box<wasmtime_error_t>> {
_wasmtime_instance_new(
store,
module,
std::slice::from_raw_parts(imports, num_imports),
instance_ptr,
@@ -98,11 +91,13 @@ pub unsafe extern "C" fn wasmtime_instance_new(
}
fn _wasmtime_instance_new(
store: &wasm_store_t,
module: &wasm_module_t,
imports: &[Box<wasm_extern_t>],
instance_ptr: &mut *mut wasm_instance_t,
trap_ptr: &mut *mut wasm_trap_t,
) -> Option<Box<wasmtime_error_t>> {
let store = &store.store;
let imports = imports
.iter()
.map(|import| match &import.which {
@@ -114,8 +109,8 @@ fn _wasmtime_instance_new(
.collect::<Vec<_>>();
let module = &module.module.borrow();
handle_instantiate(
module.store(),
Instance::new(module, &imports),
store,
Instance::new(store, module, &imports),
instance_ptr,
trap_ptr,
)

View File

@@ -1,9 +1,10 @@
use crate::host_ref::HostRef;
use crate::{handle_result, wasmtime_error_t};
use crate::{wasm_byte_vec_t, wasm_exporttype_vec_t, wasm_importtype_vec_t};
use crate::{wasm_exporttype_t, wasm_importtype_t, wasm_store_t};
use crate::{
handle_result, wasm_byte_vec_t, wasm_exporttype_t, wasm_exporttype_vec_t, wasm_importtype_t,
wasm_importtype_vec_t, wasm_store_t, wasmtime_error_t,
};
use std::ptr;
use wasmtime::Module;
use wasmtime::{Engine, Module};
#[repr(C)]
#[derive(Clone)]
@@ -21,6 +22,14 @@ impl wasm_module_t {
}
}
#[repr(C)]
#[derive(Clone)]
pub struct wasm_shared_module_t {
module: Module,
}
wasmtime_c_api_macros::declare_own!(wasm_shared_module_t);
#[no_mangle]
pub extern "C" fn wasm_module_new(
store: &wasm_store_t,
@@ -44,7 +53,7 @@ pub extern "C" fn wasmtime_module_new(
) -> Option<Box<wasmtime_error_t>> {
let binary = binary.as_slice();
let store = &store.store;
handle_result(Module::from_binary(store, binary), |module| {
handle_result(Module::from_binary(store.engine(), binary), |module| {
let imports = module
.imports()
.map(|i| wasm_importtype_t::new(i.module().to_owned(), i.name().to_owned(), i.ty()))
@@ -73,8 +82,7 @@ pub extern "C" fn wasmtime_module_validate(
binary: &wasm_byte_vec_t,
) -> Option<Box<wasmtime_error_t>> {
let binary = binary.as_slice();
let store = &store.store;
handle_result(Module::validate(store, binary), |()| {})
handle_result(Module::validate(store.store.engine(), binary), |()| {})
}
#[no_mangle]
@@ -96,3 +104,34 @@ pub extern "C" fn wasm_module_imports(module: &wasm_module_t, out: &mut wasm_imp
.collect::<Vec<_>>();
out.set_buffer(buffer);
}
#[no_mangle]
pub extern "C" fn wasm_module_share(module: &wasm_module_t) -> Box<wasm_shared_module_t> {
Box::new(wasm_shared_module_t {
module: module.module.borrow().clone(),
})
}
#[no_mangle]
pub extern "C" fn wasm_module_obtain(
store: &wasm_store_t,
shared_module: &wasm_shared_module_t,
) -> Option<Box<wasm_module_t>> {
let module = shared_module.module.clone();
if !Engine::same(store.store.engine(), module.engine()) {
return None;
}
let imports = module
.imports()
.map(|i| wasm_importtype_t::new(i.module().to_owned(), i.name().to_owned(), i.ty()))
.collect::<Vec<_>>();
let exports = module
.exports()
.map(|e| wasm_exporttype_t::new(e.name().to_owned(), e.ty()))
.collect::<Vec<_>>();
Some(Box::new(wasm_module_t {
module: HostRef::new(&store.store, module),
imports,
exports,
}))
}