mirror of
https://github.com/fluencelabs/parity-wasm
synced 2025-06-27 05:32:07 +00:00
return custom user errors from native functions
This commit is contained in:
@ -5,11 +5,10 @@ use std::collections::HashMap;
|
||||
use builder::module;
|
||||
use elements::{ExportEntry, Internal, ImportEntry, External, GlobalEntry, GlobalType,
|
||||
InitExpr, ValueType, BlockType, Opcodes, Opcode, FunctionType};
|
||||
use interpreter::Error;
|
||||
use interpreter::{Error, CustomUserError, DummyCustomUserError, InterpreterError, ProgramInstance, ModuleInstance, CustomProgramInstance};
|
||||
use interpreter::env_native::{env_native_module, UserDefinedElements, UserFunctionExecutor, UserFunctionDescriptor};
|
||||
use interpreter::memory::MemoryInstance;
|
||||
use interpreter::module::{ModuleInstance, ModuleInstanceInterface, CallerContext, ItemIndex, ExecutionParams, ExportEntryType, FunctionSignature};
|
||||
use interpreter::program::ProgramInstance;
|
||||
use interpreter::module::{ModuleInstanceInterface, CallerContext, ItemIndex, ExecutionParams, ExportEntryType, FunctionSignature};
|
||||
use interpreter::validator::{FunctionValidationContext, Validator};
|
||||
use interpreter::value::{RuntimeValue, TryInto};
|
||||
use interpreter::variable::{VariableInstance, ExternalVariableValue, VariableType};
|
||||
@ -115,6 +114,11 @@ const SIGNATURES: &'static [UserFunctionDescriptor] = &[
|
||||
SIGNATURE_I32_I32,
|
||||
Some(ValueType::I32),
|
||||
),
|
||||
UserFunctionDescriptor::Static(
|
||||
"err",
|
||||
SIGNATURE_I32_I32,
|
||||
Some(ValueType::I32),
|
||||
),
|
||||
];
|
||||
|
||||
const NO_SIGNATURES: &'static [UserFunctionDescriptor] = &[];
|
||||
@ -135,14 +139,28 @@ impl ExternalVariableValue for MeasuredVariable {
|
||||
}
|
||||
}
|
||||
|
||||
// custom user error
|
||||
#[derive(Debug, Clone, PartialEq)]
|
||||
struct UserError {
|
||||
error_code: i32,
|
||||
}
|
||||
|
||||
impl ::std::fmt::Display for UserError {
|
||||
fn fmt(&self, f: &mut ::std::fmt::Formatter) -> Result<(), ::std::fmt::Error> {
|
||||
write!(f, "{}", self.error_code)
|
||||
}
|
||||
}
|
||||
|
||||
impl CustomUserError for UserError {}
|
||||
|
||||
// user function executor
|
||||
struct FunctionExecutor {
|
||||
pub memory: Arc<MemoryInstance>,
|
||||
pub values: Vec<i32>,
|
||||
}
|
||||
|
||||
impl UserFunctionExecutor for FunctionExecutor {
|
||||
fn execute(&mut self, name: &str, context: CallerContext) -> Result<Option<RuntimeValue>, Error> {
|
||||
impl UserFunctionExecutor<UserError> for FunctionExecutor {
|
||||
fn execute(&mut self, name: &str, context: CallerContext<UserError>) -> Result<Option<RuntimeValue>, InterpreterError<UserError>> {
|
||||
match name {
|
||||
"add" => {
|
||||
let memory_value = self.memory.get(0, 1).unwrap()[0];
|
||||
@ -166,7 +184,10 @@ impl UserFunctionExecutor for FunctionExecutor {
|
||||
self.values.push(diff as i32);
|
||||
Ok(Some(RuntimeValue::I32(diff as i32)))
|
||||
},
|
||||
_ => Err(Error::Trap("not implemented".into())),
|
||||
"err" => {
|
||||
Err(InterpreterError::User(UserError { error_code: 777 }))
|
||||
},
|
||||
_ => Err(Error::Trap("not implemented".into()).into()),
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -174,7 +195,7 @@ impl UserFunctionExecutor for FunctionExecutor {
|
||||
#[test]
|
||||
fn native_env_function() {
|
||||
// create new program
|
||||
let program = ProgramInstance::new().unwrap();
|
||||
let program = CustomProgramInstance::new().unwrap();
|
||||
// => env module is created
|
||||
let env_instance = program.module("env").unwrap();
|
||||
// => linear memory is created
|
||||
@ -186,7 +207,7 @@ fn native_env_function() {
|
||||
values: Vec::new(),
|
||||
};
|
||||
{
|
||||
let functions: UserDefinedElements = UserDefinedElements {
|
||||
let functions = UserDefinedElements {
|
||||
executor: Some(&mut executor),
|
||||
globals: HashMap::new(),
|
||||
functions: ::std::borrow::Cow::from(SIGNATURES),
|
||||
@ -282,6 +303,40 @@ fn native_env_global() {
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn native_custom_error() {
|
||||
let program = CustomProgramInstance::new().unwrap();
|
||||
let env_instance = program.module("env").unwrap();
|
||||
let env_memory = env_instance.memory(ItemIndex::Internal(0)).unwrap();
|
||||
let mut executor = FunctionExecutor { memory: env_memory.clone(), values: Vec::new() };
|
||||
let functions = UserDefinedElements {
|
||||
executor: Some(&mut executor),
|
||||
globals: HashMap::new(),
|
||||
functions: ::std::borrow::Cow::from(SIGNATURES),
|
||||
};
|
||||
let native_env_instance = Arc::new(env_native_module(env_instance, functions).unwrap());
|
||||
let params = ExecutionParams::with_external("env".into(), native_env_instance);
|
||||
|
||||
let module = module()
|
||||
.with_import(ImportEntry::new("env".into(), "err".into(), External::Function(0)))
|
||||
.function()
|
||||
.signature().param().i32().param().i32().return_type().i32().build()
|
||||
.body().with_opcodes(Opcodes::new(vec![
|
||||
Opcode::GetLocal(0),
|
||||
Opcode::GetLocal(1),
|
||||
Opcode::Call(0),
|
||||
Opcode::End,
|
||||
])).build()
|
||||
.build()
|
||||
.build();
|
||||
|
||||
let module_instance = program.add_module("main", module, Some(¶ms.externals)).unwrap();
|
||||
assert_eq!(module_instance.execute_index(0, params.clone().add_argument(RuntimeValue::I32(7)).add_argument(RuntimeValue::I32(0))),
|
||||
Err(InterpreterError::User(UserError { error_code: 777 })));
|
||||
assert_eq!(module_instance.execute_index(1, params.clone().add_argument(RuntimeValue::I32(7)).add_argument(RuntimeValue::I32(0))),
|
||||
Err(InterpreterError::User(UserError { error_code: 777 })));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn import_env_mutable_global() {
|
||||
let program = ProgramInstance::new().unwrap();
|
||||
@ -295,7 +350,7 @@ fn import_env_mutable_global() {
|
||||
|
||||
#[test]
|
||||
fn env_native_export_entry_type_check() {
|
||||
let program = ProgramInstance::new().unwrap();
|
||||
let program = CustomProgramInstance::new().unwrap();
|
||||
let mut function_executor = FunctionExecutor {
|
||||
memory: program.module("env").unwrap().memory(ItemIndex::Internal(0)).unwrap(),
|
||||
values: Vec::new(),
|
||||
|
Reference in New Issue
Block a user