Make memalign optional if the wasm doesn't export it

This commit is contained in:
Brandon Fish 2019-02-05 23:24:07 -06:00
parent 30e5ba36d7
commit 9d16faab9d
2 changed files with 12 additions and 7 deletions

View File

@ -143,10 +143,11 @@ pub fn call_malloc(size: u32, ctx: &mut Ctx) -> u32 {
}
pub fn call_memalign(alignment: u32, size: u32, ctx: &mut Ctx) -> u32 {
get_emscripten_data(ctx)
.memalign
.call(alignment, size)
.unwrap()
if let Some(memalign) = &get_emscripten_data(ctx).memalign {
memalign.call(alignment, size).unwrap()
} else {
panic!("Memalign is set to None");
}
}
pub fn call_memset(pointer: u32, value: u32, size: u32, ctx: &mut Ctx) -> u32 {

View File

@ -91,7 +91,7 @@ fn dynamictop_ptr(static_bump: u32) -> u32 {
pub struct EmscriptenData<'a> {
pub malloc: Func<'a, u32, u32>,
pub free: Func<'a, u32>,
pub memalign: Func<'a, (u32, u32), u32>,
pub memalign: Option<Func<'a, (u32, u32), u32>>,
pub memset: Func<'a, (u32, u32, u32), u32>,
pub stack_alloc: Func<'a, u32, u32>,
@ -102,7 +102,11 @@ impl<'a> EmscriptenData<'a> {
pub fn new(instance: &'a mut Instance) -> EmscriptenData<'a> {
let malloc = instance.func("_malloc").unwrap();
let free = instance.func("_free").unwrap();
let memalign = instance.func("_memalign").unwrap();
let memalign = if let Ok(func) = instance.func("_memalign") {
Some(func)
} else {
None
};
let memset = instance.func("_memset").unwrap();
let stack_alloc = instance.func("stackAlloc").unwrap();
@ -511,7 +515,7 @@ pub fn generate_emscripten_env(globals: &mut EmscriptenGlobals) -> ImportObject
"NaN" => Global::new(Value::F64(f64::NAN)),
"Infinity" => Global::new(Value::F64(f64::INFINITY)),
},
"math" => {
"global.Math" => {
"pow" => func!(crate::math::pow),
},
};