mirror of
https://github.com/fluencelabs/wasmer
synced 2025-03-16 16:20:49 +00:00
Add initial error messaging
This commit is contained in:
parent
66bf13c9dd
commit
b70e319f5c
@ -2,6 +2,8 @@ extern crate wasmer_runtime;
|
||||
extern crate wasmer_runtime_core;
|
||||
|
||||
use libc::{c_char, c_int, int32_t, int64_t, uint32_t, uint8_t};
|
||||
use std::cell::RefCell;
|
||||
use std::error::Error;
|
||||
use std::ffi::CStr;
|
||||
use std::slice;
|
||||
use std::str;
|
||||
@ -139,6 +141,7 @@ pub unsafe extern "C" fn wasmer_memory_new(
|
||||
let new_memory = match result {
|
||||
Ok(memory) => memory,
|
||||
Err(error) => {
|
||||
update_last_error(error);
|
||||
return wasmer_memory_result_t::WASMER_MEMORY_ERROR;
|
||||
}
|
||||
};
|
||||
@ -185,6 +188,7 @@ pub unsafe extern "C" fn wasmer_table_new(
|
||||
let new_table = match result {
|
||||
Ok(table) => table,
|
||||
Err(error) => {
|
||||
update_last_error(error);
|
||||
return wasmer_table_result_t::WASMER_TABLE_ERROR;
|
||||
}
|
||||
};
|
||||
@ -381,7 +385,7 @@ pub unsafe extern "C" fn wasmer_instance_call(
|
||||
wasmer_call_result_t::WASMER_CALL_OK
|
||||
}
|
||||
Err(err) => {
|
||||
println!("Call Err: {:?}", err);
|
||||
update_last_error(err);
|
||||
wasmer_call_result_t::WASMER_CALL_ERROR
|
||||
}
|
||||
}
|
||||
@ -537,3 +541,62 @@ impl From<wasmer_value_tag> for Type {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Error reporting
|
||||
|
||||
thread_local! {
|
||||
static LAST_ERROR: RefCell<Option<Box<Error>>> = RefCell::new(None);
|
||||
}
|
||||
|
||||
fn update_last_error<E: Error + 'static>(err: E) {
|
||||
LAST_ERROR.with(|prev| {
|
||||
*prev.borrow_mut() = Some(Box::new(err));
|
||||
});
|
||||
}
|
||||
|
||||
/// Retrieve the most recent error, clearing it in the process.
|
||||
fn take_last_error() -> Option<Box<Error>> {
|
||||
LAST_ERROR.with(|prev| prev.borrow_mut().take())
|
||||
}
|
||||
|
||||
#[no_mangle]
|
||||
pub extern "C" fn wasmer_last_error_length() -> c_int {
|
||||
LAST_ERROR.with(|prev| match *prev.borrow() {
|
||||
Some(ref err) => err.to_string().len() as c_int + 1,
|
||||
None => 0,
|
||||
})
|
||||
}
|
||||
|
||||
#[no_mangle]
|
||||
pub unsafe extern "C" fn wasmer_last_error_message(buffer: *mut c_char, length: c_int) -> c_int {
|
||||
if buffer.is_null() {
|
||||
// buffer pointer is null
|
||||
return -1;
|
||||
}
|
||||
|
||||
let last_error = match take_last_error() {
|
||||
Some(err) => err,
|
||||
None => return 0,
|
||||
};
|
||||
|
||||
let error_message = last_error.to_string();
|
||||
|
||||
let buffer = slice::from_raw_parts_mut(buffer as *mut u8, length as usize);
|
||||
|
||||
if error_message.len() >= buffer.len() {
|
||||
// buffer to small for err message
|
||||
return -1;
|
||||
}
|
||||
|
||||
ptr::copy_nonoverlapping(
|
||||
error_message.as_ptr(),
|
||||
buffer.as_mut_ptr(),
|
||||
error_message.len(),
|
||||
);
|
||||
|
||||
// Add a trailing null so people using the string as a `char *` don't
|
||||
// accidentally read into garbage.
|
||||
buffer[error_message.len()] = 0;
|
||||
|
||||
error_message.len() as c_int
|
||||
}
|
||||
|
@ -38,6 +38,19 @@ int main()
|
||||
assert(results[0].value.I32 == 15);
|
||||
assert(call_result == WASMER_CALL_OK);
|
||||
|
||||
|
||||
wasmer_call_result_t call_result2 = wasmer_instance_call(instance, "sum", params, 1, results, 1);
|
||||
printf("Call result bad: %d\n", call_result2);
|
||||
assert(call_result2 == WASMER_CALL_ERROR);
|
||||
|
||||
int error_len = wasmer_last_error_length();
|
||||
printf("Error len: `%d`\n", error_len);
|
||||
char *error_str = malloc(error_len);
|
||||
wasmer_last_error_message(error_str, error_len);
|
||||
printf("Error str: `%s`\n", error_str);
|
||||
assert(0 == strcmp(error_str, "Call error"));
|
||||
free(error_str);
|
||||
|
||||
printf("Destroy instance\n");
|
||||
wasmer_instance_destroy(instance);
|
||||
printf("Destroy import object\n");
|
||||
|
@ -31,6 +31,26 @@ int main()
|
||||
// Err, grow beyond max
|
||||
wasmer_memory_result_t grow_result2 = wasmer_memory_grow(memory, 10);
|
||||
assert(grow_result2 == WASMER_MEMORY_ERROR);
|
||||
// int error_len = wasmer_last_error_length();
|
||||
// char *error_str = malloc(error_len);
|
||||
// wasmer_last_error_message(error_str, error_len);
|
||||
// assert(0 == strcmp(error_str, "Creation error"));
|
||||
// free(error_str);
|
||||
|
||||
|
||||
// wasmer_memory_t *bad_memory = NULL;
|
||||
// wasmer_limits_t bad_descriptor;
|
||||
// bad_descriptor.min = 15;
|
||||
// bad_descriptor.max = 10;
|
||||
// wasmer_memory_result_t bad_memory_result = wasmer_memory_new(&bad_memory, bad_descriptor);
|
||||
// printf("Bad memory result: %d\n", bad_memory_result);
|
||||
// assert(memory_result == WASMER_MEMORY_ERROR);
|
||||
//
|
||||
// int error_len = wasmer_last_error_length();
|
||||
// char *error_str = malloc(error_len);
|
||||
// wasmer_last_error_message(error_str, error_len);
|
||||
// assert(0 == strcmp(error_str, "Creation error"));
|
||||
// free(error_str);
|
||||
|
||||
printf("Destroy memory\n");
|
||||
wasmer_memory_destroy(memory);
|
||||
|
@ -30,6 +30,14 @@ int main()
|
||||
// printf("Table length: %d\n", len_grow2);
|
||||
// assert(len_grow2 == 15);
|
||||
|
||||
// wasmer_table_t *table_bad = NULL;
|
||||
// wasmer_limits_t bad_descriptor;
|
||||
// bad_descriptor.min = 15;
|
||||
// bad_descriptor.max = 10;
|
||||
// wasmer_table_result_t table_bad_result = wasmer_table_new(&table_bad, bad_descriptor);
|
||||
// printf("Table result: %d\n", table_bad_result);
|
||||
// assert(table_result == WASMER_TABLE_ERROR);
|
||||
|
||||
printf("Destroy table\n");
|
||||
wasmer_table_destroy(table);
|
||||
return 0;
|
||||
|
@ -114,6 +114,10 @@ wasmer_compile_result_t wasmer_instantiate(wasmer_instance_t **instance,
|
||||
uint32_t wasm_bytes_len,
|
||||
wasmer_import_object_t *import_object);
|
||||
|
||||
int wasmer_last_error_length(void);
|
||||
|
||||
int wasmer_last_error_message(char *buffer, int length);
|
||||
|
||||
uint8_t *wasmer_memory_data(wasmer_memory_t *mem);
|
||||
|
||||
uint32_t wasmer_memory_data_length(wasmer_memory_t *mem);
|
||||
|
@ -114,6 +114,10 @@ wasmer_compile_result_t wasmer_instantiate(wasmer_instance_t **instance,
|
||||
uint32_t wasm_bytes_len,
|
||||
wasmer_import_object_t *import_object);
|
||||
|
||||
int wasmer_last_error_length();
|
||||
|
||||
int wasmer_last_error_message(char *buffer, int length);
|
||||
|
||||
uint8_t *wasmer_memory_data(wasmer_memory_t *mem);
|
||||
|
||||
uint32_t wasmer_memory_data_length(wasmer_memory_t *mem);
|
||||
|
@ -1,6 +1,8 @@
|
||||
use crate::types::{
|
||||
FuncSig, GlobalDescriptor, MemoryDescriptor, MemoryIndex, TableDescriptor, TableIndex, Type,
|
||||
};
|
||||
use std::error::Error as StdError;
|
||||
use std::fmt;
|
||||
use std::sync::Arc;
|
||||
|
||||
pub type Result<T> = std::result::Result<T, Error>;
|
||||
@ -154,6 +156,14 @@ impl PartialEq for CallError {
|
||||
}
|
||||
}
|
||||
|
||||
impl fmt::Display for CallError {
|
||||
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
||||
write!(f, "Call error")
|
||||
}
|
||||
}
|
||||
|
||||
impl StdError for CallError {}
|
||||
|
||||
/// This error type is produced when creating something,
|
||||
/// like a `Memory` or a `Table`.
|
||||
#[derive(Debug, Clone)]
|
||||
@ -168,6 +178,14 @@ impl PartialEq for CreationError {
|
||||
}
|
||||
}
|
||||
|
||||
impl fmt::Display for CreationError {
|
||||
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
||||
write!(f, "Creation error")
|
||||
}
|
||||
}
|
||||
|
||||
impl StdError for CreationError {}
|
||||
|
||||
/// The amalgamation of all errors that can occur
|
||||
/// during the compilation, instantiation, or execution
|
||||
/// of a webassembly module.
|
||||
|
Loading…
x
Reference in New Issue
Block a user