diff --git a/src/interpreter/module.rs b/src/interpreter/module.rs index 65bb1ee..7bdf4ca 100644 --- a/src/interpreter/module.rs +++ b/src/interpreter/module.rs @@ -305,7 +305,7 @@ fn prepare_function_locals(function_type: &FunctionType, function_body: &FuncBod }) .collect::>().into_iter().rev() // TODO: default values (zero), not null - .chain(function_body.locals().iter().map(|l| VariableInstance::new(true, l.value_type().into(), RuntimeValue::Null))) + .chain(function_body.locals().iter().map(|l| VariableInstance::new(true, l.value_type().into(), RuntimeValue::default(l.value_type().into())))) .collect::, _>>() } diff --git a/src/interpreter/value.rs b/src/interpreter/value.rs index c3dda58..e1de6c2 100644 --- a/src/interpreter/value.rs +++ b/src/interpreter/value.rs @@ -111,6 +111,17 @@ pub trait Float: ArithmeticOps { } impl RuntimeValue { + /// Creates new default value of given type. + pub fn default(variable_type: VariableType) -> Self { + match variable_type { + VariableType::AnyFunc => RuntimeValue::AnyFunc(0), + VariableType::I32 => RuntimeValue::I32(0), + VariableType::I64 => RuntimeValue::I64(0), + VariableType::F32 => RuntimeValue::F32(0f32), + VariableType::F64 => RuntimeValue::F64(0f64), + } + } + /// Creates new value by interpreting passed u32 as f32. pub fn decode_f32(val: u32) -> Self { RuntimeValue::F32(val.transmute_into())