chore(c-api) Use Option<&T> rather than Option<NonNull<T>>.

This commit is contained in:
Ivan Enderlin
2020-11-16 11:07:02 +01:00
parent 10555eb01e
commit cf9e2fbcdd
5 changed files with 17 additions and 34 deletions

View File

@@ -6,7 +6,6 @@ mod table;
pub use function::*;
pub use global::*;
pub use memory::*;
use std::ptr::NonNull;
use std::sync::Arc;
pub use table::*;
use wasmer::{Extern, Instance};
@@ -22,10 +21,9 @@ wasm_declare_boxed_vec!(extern);
#[no_mangle]
pub unsafe extern "C" fn wasm_func_as_extern(
func: Option<NonNull<wasm_func_t>>,
func: Option<&wasm_func_t>,
) -> Option<Box<wasm_extern_t>> {
let func = func?;
let func = func.as_ref();
Some(Box::new(wasm_extern_t {
instance: func.instance.clone(),
@@ -35,10 +33,9 @@ pub unsafe extern "C" fn wasm_func_as_extern(
#[no_mangle]
pub unsafe extern "C" fn wasm_global_as_extern(
global: Option<NonNull<wasm_global_t>>,
global: Option<&wasm_global_t>,
) -> Option<Box<wasm_extern_t>> {
let global = global?;
let global = global.as_ref();
Some(Box::new(wasm_extern_t {
// TODO: update this if global does hold onto an `instance`
@@ -49,10 +46,9 @@ pub unsafe extern "C" fn wasm_global_as_extern(
#[no_mangle]
pub unsafe extern "C" fn wasm_memory_as_extern(
memory: Option<NonNull<wasm_memory_t>>,
memory: Option<&wasm_memory_t>,
) -> Option<Box<wasm_extern_t>> {
let memory = memory?;
let memory = memory.as_ref();
Some(Box::new(wasm_extern_t {
// TODO: update this if global does hold onto an `instance`
@@ -63,10 +59,9 @@ pub unsafe extern "C" fn wasm_memory_as_extern(
#[no_mangle]
pub unsafe extern "C" fn wasm_table_as_extern(
table: Option<NonNull<wasm_table_t>>,
table: Option<&wasm_table_t>,
) -> Option<Box<wasm_extern_t>> {
let table = table?;
let table = table.as_ref();
Some(Box::new(wasm_extern_t {
// TODO: update this if global does hold onto an `instance`
@@ -77,10 +72,9 @@ pub unsafe extern "C" fn wasm_table_as_extern(
#[no_mangle]
pub unsafe extern "C" fn wasm_extern_as_func(
r#extern: Option<NonNull<wasm_extern_t>>,
r#extern: Option<&wasm_extern_t>,
) -> Option<Box<wasm_func_t>> {
let r#extern = r#extern?;
let r#extern = r#extern.as_ref();
if let Extern::Function(f) = &r#extern.inner {
Some(Box::new(wasm_func_t {
@@ -94,10 +88,9 @@ pub unsafe extern "C" fn wasm_extern_as_func(
#[no_mangle]
pub unsafe extern "C" fn wasm_extern_as_global(
r#extern: Option<NonNull<wasm_extern_t>>,
r#extern: Option<&wasm_extern_t>,
) -> Option<Box<wasm_global_t>> {
let r#extern = r#extern?;
let r#extern = r#extern.as_ref();
if let Extern::Global(g) = &r#extern.inner {
Some(Box::new(wasm_global_t { inner: g.clone() }))
@@ -108,10 +101,9 @@ pub unsafe extern "C" fn wasm_extern_as_global(
#[no_mangle]
pub unsafe extern "C" fn wasm_extern_as_memory(
r#extern: Option<NonNull<wasm_extern_t>>,
r#extern: Option<&wasm_extern_t>,
) -> Option<Box<wasm_memory_t>> {
let r#extern = r#extern?;
let r#extern = r#extern.as_ref();
if let Extern::Memory(m) = &r#extern.inner {
Some(Box::new(wasm_memory_t { inner: m.clone() }))
@@ -122,10 +114,9 @@ pub unsafe extern "C" fn wasm_extern_as_memory(
#[no_mangle]
pub unsafe extern "C" fn wasm_extern_as_table(
r#extern: Option<NonNull<wasm_extern_t>>,
r#extern: Option<&wasm_extern_t>,
) -> Option<Box<wasm_table_t>> {
let r#extern = r#extern?;
let r#extern = r#extern.as_ref();
if let Extern::Table(t) = &r#extern.inner {
Some(Box::new(wasm_table_t { inner: t.clone() }))

View File

@@ -4,7 +4,6 @@ use super::types::{
wasm_importtype_vec_t,
};
use crate::error::update_last_error;
use std::ptr::NonNull;
use std::sync::Arc;
use wasmer::Module;
@@ -138,7 +137,7 @@ pub unsafe extern "C" fn wasm_module_imports(
pub unsafe extern "C" fn wasm_module_deserialize(
store: &wasm_store_t,
bytes: *const wasm_byte_vec_t,
) -> Option<NonNull<wasm_module_t>> {
) -> Option<*const wasm_module_t> {
// TODO: read config from store and use that to decide which compiler to use
let byte_slice = if bytes.is_null() || (&*bytes).into_slice().is_none() {
@@ -150,11 +149,9 @@ pub unsafe extern "C" fn wasm_module_deserialize(
let module = c_try!(Module::deserialize(&store.inner, byte_slice));
Some(NonNull::new_unchecked(Box::into_raw(Box::new(
wasm_module_t {
Some(Box::into_raw(Box::new(wasm_module_t {
inner: Arc::new(module),
},
))))
})) as *const _)
}
#[no_mangle]

View File

@@ -1,5 +1,4 @@
use super::engine::wasm_engine_t;
use std::ptr::NonNull;
use wasmer::Store;
/// Opaque wrapper around `Store`
@@ -10,11 +9,9 @@ pub struct wasm_store_t {
#[no_mangle]
pub unsafe extern "C" fn wasm_store_new(
engine: Option<NonNull<wasm_engine_t>>,
engine: Option<&wasm_engine_t>,
) -> Option<Box<wasm_store_t>> {
let engine = engine?;
let engine = engine.as_ref();
let store = Store::new(&*engine.inner);
Some(Box::new(wasm_store_t { inner: store }))

View File

@@ -1,6 +1,5 @@
use super::{wasm_externtype_t, wasm_valtype_t, wasm_valtype_vec_t, WasmExternType};
use std::mem;
use std::ptr::NonNull;
use wasmer::{ExternType, FunctionType, ValType};
#[derive(Debug)]
@@ -124,12 +123,12 @@ pub unsafe extern "C" fn wasm_functype_delete(_function_type: Option<Box<wasm_fu
#[no_mangle]
pub unsafe extern "C" fn wasm_functype_copy(
function_type: Option<NonNull<wasm_functype_t>>,
function_type: Option<&wasm_functype_t>,
) -> Option<Box<wasm_functype_t>> {
let function_type = function_type?;
Some(Box::new(wasm_functype_t::new(
function_type.as_ref().inner().function_type.clone(),
function_type.inner().function_type.clone(),
)))
}

View File

@@ -1,7 +1,6 @@
use super::types::{wasm_ref_t, wasm_valkind_enum};
use crate::error::{update_last_error, CApiError};
use std::convert::{TryFrom, TryInto};
use std::ptr::NonNull;
use wasmer::Val;
#[allow(non_camel_case_types)]
@@ -69,10 +68,10 @@ pub unsafe extern "C" fn wasm_val_copy(
}
#[no_mangle]
pub unsafe extern "C" fn wasm_val_delete(val: Option<NonNull<wasm_val_t>>) {
pub unsafe extern "C" fn wasm_val_delete(val: Option<&wasm_val_t>) {
if let Some(v_inner) = val {
// TODO: figure out where wasm_val is allocated first...
let _ = Box::from_raw(v_inner.as_ptr());
let _ = Box::from_raw(v_inner);
}
}