null refs

This commit is contained in:
Graham Kelly 2024-04-09 13:52:02 -04:00
parent 9d16b582ea
commit 908ad937e1
6 changed files with 235 additions and 206 deletions

View file

@ -945,6 +945,10 @@ impl<'a> WasmFuncBackend<'a> {
Operator::RefFunc { func_index } => {
Some(wasm_encoder::Instruction::RefFunc(func_index.index() as u32))
}
Operator::RefNull { ty } => {
let h: wasm_encoder::RefType = ty.clone().into();
Some(wasm_encoder::Instruction::RefNull(h.heap_type))
}
};
if let Some(inst) = inst {

View file

@ -18,7 +18,8 @@ pub struct InterpContext {
pub globals: PerEntity<Global, ConstVal>,
pub fuel: u64,
pub trace_handler: Option<Box<dyn Fn(usize, Vec<ConstVal>) -> bool + Send>>,
pub import_hander: Option<Box<dyn FnMut(&mut InterpContext,&str,&[ConstVal]) -> InterpResult>>
pub import_hander:
Option<Box<dyn FnMut(&mut InterpContext, &str, &[ConstVal]) -> InterpResult>>,
}
type MultiVal = SmallVec<[ConstVal; 2]>;
@ -93,7 +94,7 @@ impl InterpContext {
pub fn call(&mut self, module: &Module<'_>, mut func: Func, args: &[ConstVal]) -> InterpResult {
let mut args = args.to_vec();
'redo: loop{
'redo: loop {
let body = match &module.funcs[func] {
FuncDecl::Lazy(..) => panic!("Un-expanded function"),
FuncDecl::Compiled(..) => panic!("Already-compiled function"),
@ -155,7 +156,11 @@ impl InterpContext {
_ => return result,
}
}
&ValueDef::Operator(Operator::CallIndirect { table_index, .. }, args, _) => {
&ValueDef::Operator(
Operator::CallIndirect { table_index, .. },
args,
_,
) => {
let args = body.arg_pool[args]
.iter()
.map(|&arg| {
@ -221,7 +226,9 @@ impl InterpContext {
}
smallvec![]
}
&ValueDef::None | &ValueDef::Placeholder(..) | &ValueDef::BlockParam(..) => {
&ValueDef::None
| &ValueDef::Placeholder(..)
| &ValueDef::BlockParam(..) => {
unreachable!();
}
};
@ -248,12 +255,15 @@ impl InterpContext {
let idx = args2.last().unwrap().as_u32().unwrap() as usize;
let fu = self.tables[table].elements[idx];
func = fu;
args = args2[..args2.len()-1].to_vec();
args = args2[..args2.len() - 1].to_vec();
continue 'redo;
// let result = self.call(module, func, &args[..args.len() - 1]);
// return result;
}
&Terminator::ReturnCall { func: fu, args: ref args2 } => {
&Terminator::ReturnCall {
func: fu,
args: ref args2,
} => {
let args2 = args2
.iter()
.map(|&arg| {
@ -322,7 +332,7 @@ impl InterpContext {
fn call_import(&mut self, name: &str, args: &[ConstVal]) -> InterpResult {
let mut r = self.import_hander.take().unwrap();
let rs = r(self,name,args);
let rs = r(self, name, args);
self.import_hander = Some(r);
return rs;
}

View file

@ -27,7 +27,7 @@ impl From<wasmparser::ValType> for Type {
}
impl From<wasmparser::RefType> for Type {
fn from(ty: wasmparser::RefType) -> Self {
if ty.is_extern_ref(){
if ty.is_extern_ref() {
return Type::ExternRef;
}
match ty.type_index() {
@ -49,7 +49,7 @@ impl std::fmt::Display for Type {
Type::F64 => write!(f, "f64"),
Type::V128 => write!(f, "v128"),
Type::FuncRef => write!(f, "funcref"),
Type::ExternRef => write!(f,"externref"),
Type::ExternRef => write!(f, "externref"),
Type::TypedFuncRef(nullable, idx) => write!(
f,
"funcref({}, {})",
@ -68,7 +68,9 @@ impl From<Type> for wasm_encoder::ValType {
Type::F32 => wasm_encoder::ValType::F32,
Type::F64 => wasm_encoder::ValType::F64,
Type::V128 => wasm_encoder::ValType::V128,
Type::FuncRef | Type::TypedFuncRef(..) | Type::ExternRef => wasm_encoder::ValType::Ref(ty.into()),
Type::FuncRef | Type::TypedFuncRef(..) | Type::ExternRef => {
wasm_encoder::ValType::Ref(ty.into())
}
}
}
}

View file

@ -190,9 +190,9 @@ impl<'a> Module<'a> {
}
pub fn to_wasm_bytes(&self) -> Result<Vec<u8>> {
backend::compile(self).map(|a|a.finish())
backend::compile(self).map(|a| a.finish())
}
pub fn to_encoded_module(&self) -> Result<wasm_encoder::Module>{
pub fn to_encoded_module(&self) -> Result<wasm_encoder::Module> {
backend::compile(self)
}

View file

@ -485,7 +485,10 @@ pub fn op_inputs(
params.push(Type::TypedFuncRef(true, sig_index.index() as u32));
Ok(params.into())
}
Operator::RefIsNull => Ok(vec![op_stack.context("in getting stack")?.last().unwrap().0].into()),
Operator::RefIsNull => {
Ok(vec![op_stack.context("in getting stack")?.last().unwrap().0].into())
}
Operator::RefNull { ty } => Ok(Cow::Borrowed(&[])),
Operator::RefFunc { .. } => Ok(Cow::Borrowed(&[])),
Operator::MemoryCopy { .. } => Ok(Cow::Borrowed(&[Type::I32, Type::I32, Type::I32])),
Operator::MemoryFill { .. } => Ok(Cow::Borrowed(&[Type::I32, Type::I32, Type::I32])),
@ -961,6 +964,7 @@ pub fn op_outputs(
let ty = module.funcs[*func_index].sig();
Ok(vec![Type::TypedFuncRef(true, ty.index() as u32)].into())
}
Operator::RefNull { ty } => Ok(vec![ty.clone()].into()),
}
}
@ -1431,6 +1435,7 @@ impl Operator {
Operator::CallRef { .. } => &[All],
Operator::RefIsNull => &[],
Operator::RefFunc { .. } => &[],
Operator::RefNull { ty } => &[],
}
}
@ -1927,6 +1932,7 @@ impl std::fmt::Display for Operator {
Operator::CallRef { sig_index } => write!(f, "call_ref<{}>", sig_index)?,
Operator::RefIsNull => write!(f, "ref_is_null")?,
Operator::RefFunc { func_index } => write!(f, "ref_func<{}>", func_index)?,
Operator::RefNull { ty } => write!(f, "ref_null<{}>", ty)?,
}
Ok(())

View file

@ -1,6 +1,7 @@
//! Operators.
use crate::{entity::EntityRef, Func, Global, Memory, Signature, Table, Type};
use anyhow::Context;
use std::convert::TryFrom;
pub use wasmparser::{Ieee32, Ieee64};
@ -635,6 +636,9 @@ pub enum Operator {
sig_index: Signature,
},
RefIsNull,
RefNull {
ty: Type,
},
RefFunc {
func_index: Func,
},
@ -1289,6 +1293,9 @@ impl<'a, 'b> std::convert::TryFrom<&'b wasmparser::Operator<'a>> for Operator {
&wasmparser::Operator::MemoryFill { mem } => Ok(Operator::MemoryFill {
mem: Memory::from(mem),
}),
&wasmparser::Operator::RefNull { hty } => Ok(Operator::RefNull {
ty: wasmparser::RefType::new(true, hty).unwrap().into(),
}),
_ => Err(()),
}
}