Skip to content

Commit

Permalink
Consolidate result type to one enum
Browse files Browse the repository at this point in the history
  • Loading branch information
bjfish committed Feb 12, 2019
1 parent b70e319 commit 019ef9e
Show file tree
Hide file tree
Showing 7 changed files with 84 additions and 138 deletions.
74 changes: 25 additions & 49 deletions lib/runtime-c-api/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,33 +29,9 @@ pub struct wasmer_instance_context_t();
#[allow(non_camel_case_types)]
#[no_mangle]
#[repr(C)]
pub enum wasmer_compile_result_t {
WASMER_COMPILE_OK = 1,
WASMER_COMPILE_ERROR = 2,
}

#[allow(non_camel_case_types)]
#[no_mangle]
#[repr(C)]
pub enum wasmer_call_result_t {
WASMER_CALL_OK = 1,
WASMER_CALL_ERROR = 2,
}

#[allow(non_camel_case_types)]
#[no_mangle]
#[repr(C)]
pub enum wasmer_memory_result_t {
WASMER_MEMORY_OK = 1,
WASMER_MEMORY_ERROR = 2,
}

#[allow(non_camel_case_types)]
#[no_mangle]
#[repr(C)]
pub enum wasmer_table_result_t {
WASMER_TABLE_OK = 1,
WASMER_TABLE_ERROR = 2,
pub enum wasmer_result_t {
WASMER_OK = 1,
WASMER_ERROR = 2,
}

#[repr(u32)]
Expand Down Expand Up @@ -131,7 +107,7 @@ pub extern "C" fn wasmer_import_object_new() -> *mut wasmer_import_object_t {
pub unsafe extern "C" fn wasmer_memory_new(
mut memory: *mut *mut wasmer_memory_t,
limits: wasmer_limits_t,
) -> wasmer_memory_result_t {
) -> wasmer_result_t {
let desc = MemoryDescriptor {
minimum: Pages(limits.min),
maximum: Some(Pages(limits.max)),
Expand All @@ -142,26 +118,26 @@ pub unsafe extern "C" fn wasmer_memory_new(
Ok(memory) => memory,
Err(error) => {
update_last_error(error);
return wasmer_memory_result_t::WASMER_MEMORY_ERROR;
return wasmer_result_t::WASMER_ERROR;
}
};
unsafe { *memory = Box::into_raw(Box::new(new_memory)) as *mut wasmer_memory_t };
wasmer_memory_result_t::WASMER_MEMORY_OK
wasmer_result_t::WASMER_OK
}

#[allow(clippy::cast_ptr_alignment)]
#[no_mangle]
pub extern "C" fn wasmer_memory_grow(
memory: *mut wasmer_memory_t,
delta: uint32_t,
) -> wasmer_memory_result_t {
) -> wasmer_result_t {
let memory = unsafe { Box::from_raw(memory as *mut Memory) };
let maybe_delta = memory.grow(Pages(delta));
Box::into_raw(memory);
if let Some(_delta) = maybe_delta {
wasmer_memory_result_t::WASMER_MEMORY_OK
wasmer_result_t::WASMER_OK
} else {
wasmer_memory_result_t::WASMER_MEMORY_ERROR
wasmer_result_t::WASMER_ERROR
}
}

Expand All @@ -178,7 +154,7 @@ pub extern "C" fn wasmer_memory_length(memory: *mut wasmer_memory_t) -> uint32_t
pub unsafe extern "C" fn wasmer_table_new(
mut table: *mut *mut wasmer_table_t,
limits: wasmer_limits_t,
) -> wasmer_table_result_t {
) -> wasmer_result_t {
let desc = TableDescriptor {
element: ElementType::Anyfunc,
minimum: limits.min,
Expand All @@ -189,26 +165,26 @@ pub unsafe extern "C" fn wasmer_table_new(
Ok(table) => table,
Err(error) => {
update_last_error(error);
return wasmer_table_result_t::WASMER_TABLE_ERROR;
return wasmer_result_t::WASMER_ERROR;
}
};
unsafe { *table = Box::into_raw(Box::new(new_table)) as *mut wasmer_table_t };
wasmer_table_result_t::WASMER_TABLE_OK
wasmer_result_t::WASMER_OK
}

#[allow(clippy::cast_ptr_alignment)]
#[no_mangle]
pub extern "C" fn wasmer_table_grow(
table: *mut wasmer_table_t,
delta: uint32_t,
) -> wasmer_table_result_t {
) -> wasmer_result_t {
let table = unsafe { Box::from_raw(table as *mut Table) };
let maybe_delta = table.grow(delta);
Box::into_raw(table);
if let Some(_delta) = maybe_delta {
wasmer_table_result_t::WASMER_TABLE_OK
wasmer_result_t::WASMER_OK
} else {
wasmer_table_result_t::WASMER_TABLE_ERROR
wasmer_result_t::WASMER_ERROR
}
}

Expand Down Expand Up @@ -305,23 +281,23 @@ pub unsafe extern "C" fn wasmer_instantiate(
wasm_bytes: *mut uint8_t,
wasm_bytes_len: uint32_t,
import_object: *mut wasmer_import_object_t,
) -> wasmer_compile_result_t {
) -> wasmer_result_t {
let import_object = unsafe { Box::from_raw(import_object as *mut ImportObject) };
if wasm_bytes.is_null() {
return wasmer_compile_result_t::WASMER_COMPILE_ERROR;
return wasmer_result_t::WASMER_ERROR;
}
let bytes: &[u8] =
unsafe { ::std::slice::from_raw_parts_mut(wasm_bytes, wasm_bytes_len as usize) };
let result = wasmer_runtime::instantiate(bytes, &*import_object);
let new_instance = match result {
Ok(instance) => instance,
Err(error) => {
return wasmer_compile_result_t::WASMER_COMPILE_ERROR;
return wasmer_result_t::WASMER_ERROR;
}
};
unsafe { *instance = Box::into_raw(Box::new(new_instance)) as *mut wasmer_instance_t };
Box::into_raw(import_object);
wasmer_compile_result_t::WASMER_COMPILE_OK
wasmer_result_t::WASMER_OK
}

#[allow(clippy::cast_ptr_alignment)]
Expand All @@ -333,17 +309,17 @@ pub unsafe extern "C" fn wasmer_instance_call(
params_len: c_int,
results: *mut wasmer_value_t,
results_len: c_int,
) -> wasmer_call_result_t {
) -> wasmer_result_t {
// TODO handle params and results
if instance.is_null() {
return wasmer_call_result_t::WASMER_CALL_ERROR;
return wasmer_result_t::WASMER_ERROR;
}
if name.is_null() {
return wasmer_call_result_t::WASMER_CALL_ERROR;
return wasmer_result_t::WASMER_ERROR;
}

if params.is_null() {
return wasmer_call_result_t::WASMER_CALL_ERROR;
return wasmer_result_t::WASMER_ERROR;
}

let params: &[wasmer_value_t] = slice::from_raw_parts(params, params_len as usize);
Expand Down Expand Up @@ -382,11 +358,11 @@ pub unsafe extern "C" fn wasmer_instance_call(
};
results[0] = ret;
}
wasmer_call_result_t::WASMER_CALL_OK
wasmer_result_t::WASMER_OK
}
Err(err) => {
update_last_error(err);
wasmer_call_result_t::WASMER_CALL_ERROR
wasmer_result_t::WASMER_ERROR
}
}
}
Expand Down
8 changes: 4 additions & 4 deletions lib/runtime-c-api/tests/test-import-function.c
Original file line number Diff line number Diff line change
Expand Up @@ -39,15 +39,15 @@ int main()
fclose(file);

wasmer_instance_t *instance = NULL;
wasmer_compile_result_t compile_result = wasmer_instantiate(&instance, bytes, len, import_object);
wasmer_result_t compile_result = wasmer_instantiate(&instance, bytes, len, import_object);
printf("Compile result: %d\n", compile_result);
assert(compile_result == WASMER_COMPILE_OK);
assert(compile_result == WASMER_OK);

wasmer_value_t params[] = {};
wasmer_value_t results[] = {};
wasmer_call_result_t call_result = wasmer_instance_call(instance, "hello_wasm", params, 0, results, 0);
wasmer_result_t call_result = wasmer_instance_call(instance, "hello_wasm", params, 0, results, 0);
printf("Call result: %d\n", call_result);
assert(call_result == WASMER_CALL_OK);
assert(call_result == WASMER_OK);

assert(print_str_called);
assert(memory_len == 17);
Expand Down
12 changes: 6 additions & 6 deletions lib/runtime-c-api/tests/test-instantiate.c
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,9 @@ int main()
fclose(file);

wasmer_instance_t *instance = NULL;
wasmer_compile_result_t compile_result = wasmer_instantiate(&instance, bytes, len, import_object);
wasmer_result_t compile_result = wasmer_instantiate(&instance, bytes, len, import_object);
printf("Compile result: %d\n", compile_result);
assert(compile_result == WASMER_COMPILE_OK);
assert(compile_result == WASMER_OK);

wasmer_value_t param_one;
param_one.tag = WASM_I32;
Expand All @@ -32,16 +32,16 @@ int main()
wasmer_value_t result_one;
wasmer_value_t results[] = {result_one};

wasmer_call_result_t call_result = wasmer_instance_call(instance, "sum", params, 2, results, 1);
wasmer_result_t call_result = wasmer_instance_call(instance, "sum", params, 2, results, 1);
printf("Call result: %d\n", call_result);
printf("Result: %d\n", results[0].value.I32);
assert(results[0].value.I32 == 15);
assert(call_result == WASMER_CALL_OK);
assert(call_result == WASMER_OK);


wasmer_call_result_t call_result2 = wasmer_instance_call(instance, "sum", params, 1, results, 1);
wasmer_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);
assert(call_result2 == WASMER_ERROR);

int error_len = wasmer_last_error_length();
printf("Error len: `%d`\n", error_len);
Expand Down
14 changes: 7 additions & 7 deletions lib/runtime-c-api/tests/test-memory.c
Original file line number Diff line number Diff line change
Expand Up @@ -9,16 +9,16 @@ int main()
wasmer_limits_t descriptor;
descriptor.min = 10;
descriptor.max = 15;
wasmer_memory_result_t memory_result = wasmer_memory_new(&memory, descriptor);
wasmer_result_t memory_result = wasmer_memory_new(&memory, descriptor);
printf("Memory result: %d\n", memory_result);
assert(memory_result == WASMER_MEMORY_OK);
assert(memory_result == WASMER_OK);

uint32_t len = wasmer_memory_length(memory);
printf("Memory pages length: %d\n", len);
assert(len == 10);

wasmer_memory_result_t grow_result = wasmer_memory_grow(memory, 2);
assert(grow_result == WASMER_MEMORY_OK);
wasmer_result_t grow_result = wasmer_memory_grow(memory, 2);
assert(grow_result == WASMER_OK);

uint32_t new_len = wasmer_memory_length(memory);
printf("Memory pages length: %d\n", new_len);
Expand All @@ -29,8 +29,8 @@ int main()
assert(bytes_len == 12 * 65536);

// Err, grow beyond max
wasmer_memory_result_t grow_result2 = wasmer_memory_grow(memory, 10);
assert(grow_result2 == WASMER_MEMORY_ERROR);
wasmer_result_t grow_result2 = wasmer_memory_grow(memory, 10);
assert(grow_result2 == WASMER_ERROR);
// int error_len = wasmer_last_error_length();
// char *error_str = malloc(error_len);
// wasmer_last_error_message(error_str, error_len);
Expand All @@ -42,7 +42,7 @@ int main()
// 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);
// wasmer_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);
//
Expand Down
16 changes: 8 additions & 8 deletions lib/runtime-c-api/tests/test-tables.c
Original file line number Diff line number Diff line change
Expand Up @@ -9,23 +9,23 @@ int main()
wasmer_limits_t descriptor;
descriptor.min = 10;
descriptor.max = 15;
wasmer_table_result_t table_result = wasmer_table_new(&table, descriptor);
wasmer_result_t table_result = wasmer_table_new(&table, descriptor);
printf("Table result: %d\n", table_result);
assert(table_result == WASMER_TABLE_OK);
assert(table_result == WASMER_OK);

uint32_t len = wasmer_table_length(table);
printf("Table length: %d\n", len);
assert(len == 15);

// wasmer_table_result_t grow_result1 = wasmer_table_grow(&table, 5);
// assert(grow_result1 == WASMER_TABLE_OK);
// wasmer_result_t grow_result1 = wasmer_table_grow(&table, 5);
// assert(grow_result1 == WASMER_OK);
// uint32_t len_grow1 = wasmer_table_length(table);
// printf("Table length: %d\n", len_grow1);
// assert(len_grow1 == 15);

// // Try to grow beyond max
// wasmer_table_result_t grow_result2 = wasmer_table_grow(&table, 1);
// assert(grow_result2 == WASMER_TABLE_ERROR);
// wasmer_result_t grow_result2 = wasmer_table_grow(&table, 1);
// assert(grow_result2 == WASMER_ERROR);
// uint32_t len_grow2 = wasmer_table_length(table);
// printf("Table length: %d\n", len_grow2);
// assert(len_grow2 == 15);
Expand All @@ -34,9 +34,9 @@ int main()
// 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);
// wasmer_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);
// assert(table_result == WASMER_ERROR);

printf("Destroy table\n");
wasmer_table_destroy(table);
Expand Down
Loading

0 comments on commit 019ef9e

Please sign in to comment.