[wasmtime-api] Implementation of classes for run-{reflect,start,global,memory}-c (#295)

Implements apis for reflect-c, start-c, run-global-c and run-memory-c
This commit is contained in:
Yury Delendik
2019-08-26 10:07:02 -05:00
committed by GitHub
parent 45fd9dadd8
commit c94c383a7c
9 changed files with 1324 additions and 431 deletions

View File

@@ -0,0 +1,47 @@
use cranelift_entity::PrimaryMap;
use failure::Error;
use wasmtime_environ::Module;
use wasmtime_runtime::{InstanceHandle, VMGlobalDefinition};
use super::create_handle::create_handle;
use crate::{GlobalType, Mutability, Val};
#[allow(dead_code)]
pub struct GlobalState {
definition: Box<VMGlobalDefinition>,
handle: InstanceHandle,
}
pub fn create_global(
gt: &GlobalType,
val: Val,
) -> Result<(wasmtime_runtime::Export, GlobalState), Error> {
let mut definition = Box::new(VMGlobalDefinition::new());
unsafe {
match val {
Val::I32(i) => *definition.as_i32_mut() = i,
Val::I64(i) => *definition.as_i64_mut() = i,
Val::F32(f) => *definition.as_u32_mut() = f,
Val::F64(f) => *definition.as_u64_mut() = f,
_ => unimplemented!("create_global for {:?}", gt),
}
}
let global = cranelift_wasm::Global {
ty: gt.content().get_cranelift_type(),
mutability: match gt.mutability() {
Mutability::Const => false,
Mutability::Var => true,
},
initializer: cranelift_wasm::GlobalInit::Import, // TODO is it right?
};
let mut handle = create_handle(Module::new(), PrimaryMap::new(), Box::new(())).expect("handle");
Ok((
wasmtime_runtime::Export::Global {
definition: definition.as_mut(),
vmctx: handle.vmctx_mut_ptr(),
global,
},
GlobalState { definition, handle },
))
}