cargo fmt

This commit is contained in:
Bo Yao
2021-04-21 15:42:47 -07:00
parent 2eac442646
commit 24bcc9349e
224 changed files with 2778 additions and 7749 deletions

View File

@@ -142,9 +142,8 @@ pub unsafe extern "C" fn wasmer_export_descriptors(
) {
let module = &*(module as *const Module);
let named_export_descriptors: Box<NamedExportDescriptors> = Box::new(NamedExportDescriptors(
module.exports().into_iter().map(|e| e.into()).collect(),
));
let named_export_descriptors: Box<NamedExportDescriptors> =
Box::new(NamedExportDescriptors(module.exports().into_iter().map(|e| e.into()).collect()));
*export_descriptors =
Box::into_raw(named_export_descriptors) as *mut wasmer_export_descriptors_t;
}
@@ -403,10 +402,8 @@ pub unsafe extern "C" fn wasmer_export_to_memory(
let named_export = &*(export as *const NamedExport);
let instance = named_export.instance.as_ref();
if let Ok(exported_memory) = instance
.instance
.exports
.get::<Memory>(&named_export.export_type.name())
if let Ok(exported_memory) =
instance.instance.exports.get::<Memory>(&named_export.export_type.name())
{
let mem = Box::new(exported_memory.clone());
*memory = Box::into_raw(mem) as *mut wasmer_memory_t;
@@ -449,16 +446,12 @@ pub unsafe extern "C" fn wasmer_export_func_call(
results_len: c_uint,
) -> wasmer_result_t {
if func.is_null() {
update_last_error(CApiError {
msg: "func ptr is null".to_string(),
});
update_last_error(CApiError { msg: "func ptr is null".to_string() });
return wasmer_result_t::WASMER_ERROR;
}
if params_len > 0 && params.is_null() {
update_last_error(CApiError {
msg: "params ptr is null".to_string(),
});
update_last_error(CApiError { msg: "params ptr is null".to_string() });
return wasmer_result_t::WASMER_ERROR;
}
@@ -479,11 +472,7 @@ pub unsafe extern "C" fn wasmer_export_func_call(
let results: &mut [wasmer_value_t] = slice::from_raw_parts_mut(results, results_len as usize);
let instance = named_export.instance.as_ref();
let f: &Function = match instance
.instance
.exports
.get(&named_export.export_type.name())
{
let f: &Function = match instance.instance.exports.get(&named_export.export_type.name()) {
Ok(f) => f,
Err(err) => {
update_last_error(err);
@@ -530,10 +519,7 @@ pub unsafe extern "C" fn wasmer_export_func_call(
impl From<ExportType> for NamedExportDescriptor {
fn from(et: ExportType) -> Self {
NamedExportDescriptor {
name: et.name().to_string(),
kind: et.into(),
}
NamedExportDescriptor { name: et.name().to_string(), kind: et.into() }
}
}

View File

@@ -61,10 +61,7 @@ pub unsafe extern "C" fn wasmer_global_get_descriptor(
) -> wasmer_global_descriptor_t {
let global = &*(global as *mut Global);
let descriptor = global.ty();
wasmer_global_descriptor_t {
mutable: descriptor.mutability.into(),
kind: descriptor.ty.into(),
}
wasmer_global_descriptor_t { mutable: descriptor.mutability.into(), kind: descriptor.ty.into() }
}
/// Frees memory for the given Global

View File

@@ -343,9 +343,7 @@ pub unsafe extern "C" fn wasmer_import_object_iter_at_end(
let mut import_object_iter = if let Some(import_object_iter) = import_object_iter {
import_object_iter.cast::<WasmerImportObjectIterator>()
} else {
update_last_error(CApiError {
msg: "import_object_iter must not be null".to_owned(),
});
update_last_error(CApiError { msg: "import_object_iter must not be null".to_owned() });
return true;
};
let iter = import_object_iter.as_mut();
@@ -359,11 +357,7 @@ pub unsafe extern "C" fn wasmer_import_object_iter_destroy(
import_object_iter: Option<NonNull<wasmer_import_object_iter_t>>,
) {
if let Some(import_object_iter) = import_object_iter {
let _ = Box::from_raw(
import_object_iter
.cast::<WasmerImportObjectIterator>()
.as_ptr(),
);
let _ = Box::from_raw(import_object_iter.cast::<WasmerImportObjectIterator>().as_ptr());
}
}
@@ -426,10 +420,8 @@ pub unsafe extern "C" fn wasmer_import_object_extend(
let mut import_data: HashMap<String, Exports> = HashMap::new();
let imports: &[wasmer_import_t] = slice::from_raw_parts(imports, imports_len as usize);
for import in imports {
let module_name = slice::from_raw_parts(
import.module_name.bytes,
import.module_name.bytes_len as usize,
);
let module_name =
slice::from_raw_parts(import.module_name.bytes, import.module_name.bytes_len as usize);
let module_name = if let Ok(s) = std::str::from_utf8(module_name) {
s
} else {
@@ -438,10 +430,8 @@ pub unsafe extern "C" fn wasmer_import_object_extend(
});
return wasmer_result_t::WASMER_ERROR;
};
let import_name = slice::from_raw_parts(
import.import_name.bytes,
import.import_name.bytes_len as usize,
);
let import_name =
slice::from_raw_parts(import.import_name.bytes, import.import_name.bytes_len as usize);
let import_name = if let Ok(s) = std::str::from_utf8(import_name) {
s
} else {
@@ -461,9 +451,7 @@ pub unsafe extern "C" fn wasmer_import_object_extend(
// TODO: investigate consistent usage of `FunctionWrapper` in this context
let func_wrapper = import.value.func as *mut FunctionWrapper;
let func_export = (*func_wrapper).func.as_ptr();
import_object
.instance_pointers_to_update
.push((*func_wrapper).legacy_env.clone());
import_object.instance_pointers_to_update.push((*func_wrapper).legacy_env.clone());
Extern::Function((&*func_export).clone())
}
wasmer_import_export_kind::WASM_GLOBAL => {
@@ -476,9 +464,7 @@ pub unsafe extern "C" fn wasmer_import_object_extend(
}
};
let export_entry = import_data
.entry(module_name.to_string())
.or_insert_with(Exports::new);
let export_entry = import_data.entry(module_name.to_string()).or_insert_with(Exports::new);
export_entry.insert(import_name.to_string(), export);
}
@@ -646,9 +632,7 @@ unsafe impl Sync for LegacyEnv {}
impl LegacyEnv {
pub(crate) fn ctx_ptr(&self) -> *mut CAPIInstance {
self.instance_ptr
.map(|p| p.as_ptr())
.unwrap_or(ptr::null_mut())
self.instance_ptr.map(|p| p.as_ptr()).unwrap_or(ptr::null_mut())
}
}
@@ -766,9 +750,7 @@ pub unsafe extern "C" fn wasmer_trap(
error_message: *const c_char,
) -> wasmer_result_t {
if error_message.is_null() {
update_last_error(CApiError {
msg: "error_message is null in wasmer_trap".to_string(),
});
update_last_error(CApiError { msg: "error_message is null in wasmer_trap".to_string() });
return wasmer_result_t::WASMER_ERROR;
}

View File

@@ -213,9 +213,8 @@ pub unsafe extern "C" fn wasmer_wasi_generate_default_import_object() -> *mut wa
let mut wasi_state_builder = wasi::WasiState::new("wasmer-wasi-default-program-name");
let wasi_state = wasi_state_builder.build().unwrap();
let wasi_env = wasi::WasiEnv::new(wasi_state);
let import_object_inner: Box<dyn NamedResolver> = Box::new(
wasi::generate_import_object_from_env(store, wasi_env, wasi::WasiVersion::Latest),
);
let import_object_inner: Box<dyn NamedResolver> =
Box::new(wasi::generate_import_object_from_env(store, wasi_env, wasi::WasiVersion::Latest));
let import_object: Box<CAPIImportObject> = Box::new(CAPIImportObject {
import_object: import_object_inner,
imported_memories: vec![],

View File

@@ -124,34 +124,25 @@ pub unsafe extern "C" fn wasmer_instantiate(
let wasm_bytes = if let Some(wasm_bytes_inner) = wasm_bytes {
wasm_bytes_inner
} else {
update_last_error(CApiError {
msg: "wasm bytes ptr is null".to_string(),
});
update_last_error(CApiError { msg: "wasm bytes ptr is null".to_string() });
return wasmer_result_t::WASMER_ERROR;
};
if imports_len > 0 && imports.is_null() {
update_last_error(CApiError {
msg: "imports ptr is null".to_string(),
});
update_last_error(CApiError { msg: "imports ptr is null".to_string() });
return wasmer_result_t::WASMER_ERROR;
}
let mut imported_memories = vec![];
let mut instance_pointers_to_update = vec![];
let imports: &[wasmer_import_t] = if imports_len == 0 {
&[]
} else {
slice::from_raw_parts(imports, imports_len as usize)
};
let imports: &[wasmer_import_t] =
if imports_len == 0 { &[] } else { slice::from_raw_parts(imports, imports_len as usize) };
let mut import_object = ImportObject::new();
let mut namespaces = HashMap::new();
for import in imports {
let module_name = slice::from_raw_parts(
import.module_name.bytes,
import.module_name.bytes_len as usize,
);
let module_name =
slice::from_raw_parts(import.module_name.bytes, import.module_name.bytes_len as usize);
let module_name = if let Ok(s) = std::str::from_utf8(module_name) {
s
} else {
@@ -160,10 +151,8 @@ pub unsafe extern "C" fn wasmer_instantiate(
});
return wasmer_result_t::WASMER_ERROR;
};
let import_name = slice::from_raw_parts(
import.import_name.bytes,
import.import_name.bytes_len as usize,
);
let import_name =
slice::from_raw_parts(import.import_name.bytes, import.import_name.bytes_len as usize);
let import_name = if let Ok(s) = std::str::from_utf8(import_name) {
s
} else {
@@ -223,11 +212,7 @@ pub unsafe extern "C" fn wasmer_instantiate(
return wasmer_result_t::WASMER_ERROR;
}
};
let c_api_instance = CAPIInstance {
instance: new_instance,
imported_memories,
ctx_data: None,
};
let c_api_instance = CAPIInstance { instance: new_instance, imported_memories, ctx_data: None };
let c_api_instance_pointer = Box::into_raw(Box::new(c_api_instance));
for to_update in instance_pointers_to_update {
let mut to_update_guard = to_update.lock().unwrap();
@@ -322,25 +307,19 @@ pub unsafe extern "C" fn wasmer_instance_call(
results_len: u32,
) -> wasmer_result_t {
if instance.is_null() {
update_last_error(CApiError {
msg: "instance ptr is null".to_string(),
});
update_last_error(CApiError { msg: "instance ptr is null".to_string() });
return wasmer_result_t::WASMER_ERROR;
}
if name.is_null() {
update_last_error(CApiError {
msg: "name ptr is null".to_string(),
});
update_last_error(CApiError { msg: "name ptr is null".to_string() });
return wasmer_result_t::WASMER_ERROR;
}
if params_len > 0 && params.is_null() {
update_last_error(CApiError {
msg: "params ptr is null".to_string(),
});
update_last_error(CApiError { msg: "params ptr is null".to_string() });
return wasmer_result_t::WASMER_ERROR;
}
@@ -469,10 +448,7 @@ pub unsafe extern "C" fn wasmer_instance_exports(
.instance
.module()
.exports()
.map(|export_type| NamedExport {
export_type,
instance,
})
.map(|export_type| NamedExport { export_type, instance })
.collect();
let named_exports: Box<NamedExports> = Box::new(NamedExports(exports_vec));
@@ -578,19 +554,13 @@ pub unsafe extern "C" fn wasmer_instance_context_memory(
{
name
} else {
update_last_error(CApiError {
msg: "Could not find an exported memory".to_string(),
});
update_last_error(CApiError { msg: "Could not find an exported memory".to_string() });
return None;
};
let memory = instance
.instance
.exports
.get_memory(exported_memory_name)
.expect(&format!(
"Module exports memory named `{}` but it's inaccessible",
&exported_memory_name
));
let memory = instance.instance.exports.get_memory(exported_memory_name).expect(&format!(
"Module exports memory named `{}` but it's inaccessible",
&exported_memory_name
));
Some(&*(Box::into_raw(Box::new(memory.clone())) as *const wasmer_memory_t))
}
}

View File

@@ -60,11 +60,7 @@ pub unsafe extern "C" fn wasmer_memory_new(
memory: *mut *mut wasmer_memory_t,
limits: wasmer_limits_t,
) -> wasmer_result_t {
let max = if limits.max.has_some {
Some(Pages(limits.max.some))
} else {
None
};
let max = if limits.max.has_some { Some(Pages(limits.max.some)) } else { None };
let store = get_global_store();
let desc = MemoryType::new(Pages(limits.min), max, false);
match Memory::new(store, desc) {
@@ -73,9 +69,7 @@ pub unsafe extern "C" fn wasmer_memory_new(
wasmer_result_t::WASMER_OK
}
Err(err) => {
update_last_error(CApiError {
msg: err.to_string(),
});
update_last_error(CApiError { msg: err.to_string() });
wasmer_result_t::WASMER_ERROR
}
}
@@ -101,9 +95,7 @@ pub unsafe extern "C" fn wasmer_memory_new(
#[no_mangle]
pub extern "C" fn wasmer_memory_grow(memory: *mut wasmer_memory_t, delta: u32) -> wasmer_result_t {
if memory.is_null() {
update_last_error(CApiError {
msg: "`memory` is NULL.".to_string(),
});
update_last_error(CApiError { msg: "`memory` is NULL.".to_string() });
return wasmer_result_t::WASMER_ERROR;
}
@@ -114,9 +106,7 @@ pub extern "C" fn wasmer_memory_grow(memory: *mut wasmer_memory_t, delta: u32) -
match grow_result {
Ok(_) => wasmer_result_t::WASMER_OK,
Err(err) => {
update_last_error(CApiError {
msg: err.to_string(),
});
update_last_error(CApiError { msg: err.to_string() });
wasmer_result_t::WASMER_ERROR
}
}

View File

@@ -92,10 +92,8 @@ pub unsafe extern "C" fn wasmer_module_instantiate(
let mut import_object = ImportObject::new();
let mut namespaces = HashMap::new();
for import in imports {
let module_name = slice::from_raw_parts(
import.module_name.bytes,
import.module_name.bytes_len as usize,
);
let module_name =
slice::from_raw_parts(import.module_name.bytes, import.module_name.bytes_len as usize);
let module_name = if let Ok(s) = std::str::from_utf8(module_name) {
s
} else {
@@ -104,10 +102,8 @@ pub unsafe extern "C" fn wasmer_module_instantiate(
});
return wasmer_result_t::WASMER_ERROR;
};
let import_name = slice::from_raw_parts(
import.import_name.bytes,
import.import_name.bytes_len as usize,
);
let import_name =
slice::from_raw_parts(import.import_name.bytes, import.import_name.bytes_len as usize);
let import_name = if let Ok(s) = std::str::from_utf8(import_name) {
s
} else {
@@ -153,11 +149,7 @@ pub unsafe extern "C" fn wasmer_module_instantiate(
}
};
let c_api_instance = CAPIInstance {
instance: new_instance,
imported_memories,
ctx_data: None,
};
let c_api_instance = CAPIInstance { instance: new_instance, imported_memories, ctx_data: None };
*instance = Box::into_raw(Box::new(c_api_instance)) as *mut wasmer_instance_t;
wasmer_result_t::WASMER_OK
@@ -224,9 +216,7 @@ pub unsafe extern "C" fn wasmer_module_serialize(
wasmer_result_t::WASMER_OK
}
Err(_) => {
update_last_error(CApiError {
msg: "Failed to serialize the module".to_string(),
});
update_last_error(CApiError { msg: "Failed to serialize the module".to_string() });
wasmer_result_t::WASMER_ERROR
}
}
@@ -268,10 +258,8 @@ pub unsafe extern "C" fn wasmer_serialized_module_from_bytes(
return wasmer_result_t::WASMER_ERROR;
}
let serialized_module_bytes: &[u8] = slice::from_raw_parts(
serialized_module_bytes,
serialized_module_bytes_length as usize,
);
let serialized_module_bytes: &[u8] =
slice::from_raw_parts(serialized_module_bytes, serialized_module_bytes_length as usize);
*serialized_module = Box::into_raw(Box::new(serialized_module_bytes)) as _;
wasmer_result_t::WASMER_OK
@@ -293,9 +281,7 @@ pub unsafe extern "C" fn wasmer_module_deserialize(
let serialized_module: &[u8] = if let Some(sm) = serialized_module {
&*(sm as *const wasmer_serialized_module_t as *const &[u8])
} else {
update_last_error(CApiError {
msg: "`serialized_module` pointer is null".to_string(),
});
update_last_error(CApiError { msg: "`serialized_module` pointer is null".to_string() });
return wasmer_result_t::WASMER_ERROR;
};

View File

@@ -36,16 +36,8 @@ pub unsafe extern "C" fn wasmer_table_new(
table: *mut *mut wasmer_table_t,
limits: wasmer_limits_t,
) -> wasmer_result_t {
let max = if limits.max.has_some {
Some(limits.max.some)
} else {
None
};
let desc = TableType {
ty: ValType::FuncRef,
minimum: limits.min,
maximum: max,
};
let max = if limits.max.has_some { Some(limits.max.some) } else { None };
let desc = TableType { ty: ValType::FuncRef, minimum: limits.min, maximum: max };
let store = get_global_store();
let result = Table::new(store, desc, get_default_table_value(ValType::FuncRef));
let new_table = match result {

View File

@@ -74,22 +74,18 @@ impl From<wasmer_value_t> for Val {
unsafe {
#[allow(unreachable_patterns, non_snake_case)]
match v {
wasmer_value_t {
tag: wasmer_value_tag::WASM_I32,
value: wasmer_value { I32 },
} => Val::I32(I32),
wasmer_value_t {
tag: wasmer_value_tag::WASM_I64,
value: wasmer_value { I64 },
} => Val::I64(I64),
wasmer_value_t {
tag: wasmer_value_tag::WASM_F32,
value: wasmer_value { F32 },
} => Val::F32(F32),
wasmer_value_t {
tag: wasmer_value_tag::WASM_F64,
value: wasmer_value { F64 },
} => Val::F64(F64),
wasmer_value_t { tag: wasmer_value_tag::WASM_I32, value: wasmer_value { I32 } } => {
Val::I32(I32)
}
wasmer_value_t { tag: wasmer_value_tag::WASM_I64, value: wasmer_value { I64 } } => {
Val::I64(I64)
}
wasmer_value_t { tag: wasmer_value_tag::WASM_F32, value: wasmer_value { F32 } } => {
Val::F32(F32)
}
wasmer_value_t { tag: wasmer_value_tag::WASM_F64, value: wasmer_value { F64 } } => {
Val::F64(F64)
}
_ => unreachable!("unknown Wasm type"),
}
}
@@ -99,22 +95,18 @@ impl From<wasmer_value_t> for Val {
impl From<Val> for wasmer_value_t {
fn from(val: Val) -> Self {
match val {
Val::I32(x) => wasmer_value_t {
tag: wasmer_value_tag::WASM_I32,
value: wasmer_value { I32: x },
},
Val::I64(x) => wasmer_value_t {
tag: wasmer_value_tag::WASM_I64,
value: wasmer_value { I64: x },
},
Val::F32(x) => wasmer_value_t {
tag: wasmer_value_tag::WASM_F32,
value: wasmer_value { F32: x },
},
Val::F64(x) => wasmer_value_t {
tag: wasmer_value_tag::WASM_F64,
value: wasmer_value { F64: x },
},
Val::I32(x) => {
wasmer_value_t { tag: wasmer_value_tag::WASM_I32, value: wasmer_value { I32: x } }
}
Val::I64(x) => {
wasmer_value_t { tag: wasmer_value_tag::WASM_I64, value: wasmer_value { I64: x } }
}
Val::F32(x) => {
wasmer_value_t { tag: wasmer_value_tag::WASM_F32, value: wasmer_value { F32: x } }
}
Val::F64(x) => {
wasmer_value_t { tag: wasmer_value_tag::WASM_F64, value: wasmer_value { F64: x } }
}
Val::V128(_) => unimplemented!("V128 not supported in C API"),
Val::ExternRef(_) => unimplemented!("ExternRef not supported in C API"),
Val::FuncRef(_) => unimplemented!("FuncRef not supported in C API"),

View File

@@ -144,11 +144,7 @@ pub unsafe extern "C" fn wasmer_last_error_message(
let buffer = slice::from_raw_parts_mut(buffer.cast::<u8>().as_ptr(), length);
ptr::copy_nonoverlapping(
error_message.as_ptr(),
buffer.as_mut_ptr(),
error_message.len(),
);
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.

View File

@@ -17,9 +17,7 @@ pub struct OrderedResolver {
impl Resolver for OrderedResolver {
fn resolve(&self, index: u32, _module: &str, _name: &str) -> Option<Export> {
self.externs
.get(index as usize)
.map(|extern_| extern_.to_export())
self.externs.get(index as usize).map(|extern_| extern_.to_export())
}
}

View File

@@ -435,9 +435,7 @@ pub extern "C" fn wasm_engine_new_with_config(
where
M: ToString,
{
update_last_error(CApiError {
msg: msg.to_string(),
});
update_last_error(CApiError { msg: msg.to_string() });
return None;
}

View File

@@ -18,10 +18,7 @@ pub struct wasm_func_t {
impl wasm_func_t {
pub(crate) fn new(function: Function) -> Self {
Self {
tag: CApiExternTag::Function,
inner: Box::new(function),
}
Self { tag: CApiExternTag::Function, inner: Box::new(function) }
}
}
@@ -171,10 +168,7 @@ pub unsafe extern "C" fn wasm_func_new_with_env(
let function = Function::new_with_env(
&store.inner,
func_sig,
WrapperEnv {
env,
env_finalizer: Arc::new(env_finalizer),
},
WrapperEnv { env, env_finalizer: Arc::new(env_finalizer) },
trampoline,
);

View File

@@ -16,10 +16,7 @@ pub struct wasm_global_t {
impl wasm_global_t {
pub(crate) fn new(global: Global) -> Self {
Self {
tag: CApiExternTag::Global,
inner: Box::new(global),
}
Self { tag: CApiExternTag::Global, inner: Box::new(global) }
}
}

View File

@@ -14,10 +14,7 @@ pub struct wasm_memory_t {
impl wasm_memory_t {
pub(crate) fn new(memory: Memory) -> Self {
Self {
tag: CApiExternTag::Memory,
inner: Box::new(memory),
}
Self { tag: CApiExternTag::Memory, inner: Box::new(memory) }
}
}

View File

@@ -99,25 +99,17 @@ impl Clone for wasm_extern_t {
fn clone(&self) -> Self {
match self.get_tag() {
CApiExternTag::Function => Self {
inner: wasm_extern_inner {
function: unsafe { self.inner.function.clone() },
},
},
CApiExternTag::Memory => Self {
inner: wasm_extern_inner {
memory: unsafe { self.inner.memory.clone() },
},
},
CApiExternTag::Global => Self {
inner: wasm_extern_inner {
global: unsafe { self.inner.global.clone() },
},
},
CApiExternTag::Table => Self {
inner: wasm_extern_inner {
table: unsafe { self.inner.table.clone() },
},
inner: wasm_extern_inner { function: unsafe { self.inner.function.clone() } },
},
CApiExternTag::Memory => {
Self { inner: wasm_extern_inner { memory: unsafe { self.inner.memory.clone() } } }
}
CApiExternTag::Global => {
Self { inner: wasm_extern_inner { global: unsafe { self.inner.global.clone() } } }
}
CApiExternTag::Table => {
Self { inner: wasm_extern_inner { table: unsafe { self.inner.table.clone() } } }
}
}
}
}

View File

@@ -13,10 +13,7 @@ pub struct wasm_table_t {
impl wasm_table_t {
pub(crate) fn new(table: Table) -> Self {
Self {
tag: CApiExternTag::Table,
inner: Box::new(table),
}
Self { tag: CApiExternTag::Table, inner: Box::new(table) }
}
}

View File

@@ -39,9 +39,7 @@ pub unsafe extern "C" fn wasm_module_new(
let bytes = bytes.into_slice()?;
let module = c_try!(Module::from_binary(&store.inner, bytes));
Some(Box::new(wasm_module_t {
inner: Arc::new(module),
}))
Some(Box::new(wasm_module_t { inner: Arc::new(module) }))
}
/// Deletes a WebAssembly module.
@@ -488,11 +486,7 @@ 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 {
inner: Arc::new(module),
},
))))
Some(NonNull::new_unchecked(Box::into_raw(Box::new(wasm_module_t { inner: Arc::new(module) }))))
}
/// Serializes a module into a binary representation that the

View File

@@ -16,10 +16,7 @@ pub extern "C" fn wasm_exporttype_new(
extern_type: Option<Box<wasm_externtype_t>>,
) -> Option<Box<wasm_exporttype_t>> {
let name = unsafe { owned_wasm_name_t::new(name?) };
Some(Box::new(wasm_exporttype_t {
name,
extern_type: extern_type?,
}))
Some(Box::new(wasm_exporttype_t { name, extern_type: extern_type? }))
}
#[no_mangle]

View File

@@ -13,11 +13,7 @@ impl WasmFunctionType {
let params: Box<wasm_valtype_vec_t> = Box::new(function_type.params().into());
let results: Box<wasm_valtype_vec_t> = Box::new(function_type.results().into());
Self {
function_type,
params,
results,
}
Self { function_type, params, results }
}
}
@@ -36,9 +32,7 @@ pub struct wasm_functype_t {
impl wasm_functype_t {
pub(crate) fn new(function_type: FunctionType) -> Self {
Self {
extern_type: wasm_externtype_t::new(ExternType::Function(function_type)),
}
Self { extern_type: wasm_externtype_t::new(ExternType::Function(function_type)) }
}
pub(crate) fn inner(&self) -> &WasmFunctionType {
@@ -61,24 +55,15 @@ pub unsafe extern "C" fn wasm_functype_new(
let params = params?;
let results = results?;
let params_as_valtype: Vec<ValType> = params
.into_slice()?
.into_iter()
.map(|val| val.as_ref().into())
.collect::<Vec<_>>();
let results_as_valtype: Vec<ValType> = results
.into_slice()?
.iter()
.map(|val| val.as_ref().into())
.collect::<Vec<_>>();
let params_as_valtype: Vec<ValType> =
params.into_slice()?.into_iter().map(|val| val.as_ref().into()).collect::<Vec<_>>();
let results_as_valtype: Vec<ValType> =
results.into_slice()?.iter().map(|val| val.as_ref().into()).collect::<Vec<_>>();
wasm_valtype_vec_delete(Some(params));
wasm_valtype_vec_delete(Some(results));
Some(Box::new(wasm_functype_t::new(FunctionType::new(
params_as_valtype,
results_as_valtype,
))))
Some(Box::new(wasm_functype_t::new(FunctionType::new(params_as_valtype, results_as_valtype))))
}
#[no_mangle]

View File

@@ -15,10 +15,7 @@ impl WasmGlobalType {
pub(crate) fn new(global_type: GlobalType) -> Self {
let content = Box::new(global_type.ty.into());
Self {
global_type,
content,
}
Self { global_type, content }
}
}
@@ -31,9 +28,7 @@ pub struct wasm_globaltype_t {
impl wasm_globaltype_t {
pub(crate) fn new(global_type: GlobalType) -> Self {
Self {
extern_type: wasm_externtype_t::new(ExternType::Global(global_type)),
}
Self { extern_type: wasm_externtype_t::new(ExternType::Global(global_type)) }
}
pub(crate) fn inner(&self) -> &WasmGlobalType {
@@ -55,10 +50,8 @@ pub unsafe extern "C" fn wasm_globaltype_new(
) -> Option<Box<wasm_globaltype_t>> {
let valtype = valtype?;
let mutability: wasm_mutability_enum = mutability.try_into().ok()?;
let global_type = Box::new(wasm_globaltype_t::new(GlobalType::new(
(*valtype).into(),
mutability.into(),
)));
let global_type =
Box::new(wasm_globaltype_t::new(GlobalType::new((*valtype).into(), mutability.into())));
wasm_valtype_delete(Some(valtype));

View File

@@ -18,17 +18,9 @@ pub extern "C" fn wasm_importtype_new(
name: Option<&wasm_name_t>,
extern_type: Option<Box<wasm_externtype_t>>,
) -> Option<Box<wasm_importtype_t>> {
let (module, name) = unsafe {
(
owned_wasm_name_t::new(module?),
owned_wasm_name_t::new(name?),
)
};
Some(Box::new(wasm_importtype_t {
name,
module,
extern_type: extern_type?,
}))
let (module, name) =
unsafe { (owned_wasm_name_t::new(module?), owned_wasm_name_t::new(name?)) };
Some(Box::new(wasm_importtype_t { name, module, extern_type: extern_type? }))
}
#[no_mangle]
@@ -61,10 +53,6 @@ impl From<&ImportType> for wasm_importtype_t {
let name: owned_wasm_name_t = other.name().to_string().into();
let extern_type: Box<wasm_externtype_t> = Box::new(other.ty().into());
wasm_importtype_t {
module,
name,
extern_type,
}
wasm_importtype_t { module, name, extern_type }
}
}

View File

@@ -11,16 +11,10 @@ impl WasmMemoryType {
pub(crate) fn new(memory_type: MemoryType) -> Self {
let limits = Box::new(wasm_limits_t {
min: memory_type.minimum.0 as _,
max: memory_type
.maximum
.map(|max| max.0 as _)
.unwrap_or(LIMITS_MAX_SENTINEL),
max: memory_type.maximum.map(|max| max.0 as _).unwrap_or(LIMITS_MAX_SENTINEL),
});
Self {
memory_type,
limits,
}
Self { memory_type, limits }
}
}
@@ -33,9 +27,7 @@ pub struct wasm_memorytype_t {
impl wasm_memorytype_t {
pub(crate) fn new(memory_type: MemoryType) -> Self {
Self {
extern_type: wasm_externtype_t::new(ExternType::Memory(memory_type)),
}
Self { extern_type: wasm_externtype_t::new(ExternType::Memory(memory_type)) }
}
pub(crate) fn inner(&self) -> &WasmMemoryType {
@@ -53,15 +45,10 @@ wasm_declare_boxed_vec!(memorytype);
#[no_mangle]
pub unsafe extern "C" fn wasm_memorytype_new(limits: &wasm_limits_t) -> Box<wasm_memorytype_t> {
let min_pages = Pages(limits.min as _);
let max_pages = if limits.max == LIMITS_MAX_SENTINEL {
None
} else {
Some(Pages(limits.max as _))
};
let max_pages =
if limits.max == LIMITS_MAX_SENTINEL { None } else { Some(Pages(limits.max as _)) };
Box::new(wasm_memorytype_t::new(MemoryType::new(
min_pages, max_pages, false,
)))
Box::new(wasm_memorytype_t::new(MemoryType::new(min_pages, max_pages, false)))
}
#[no_mangle]

View File

@@ -50,10 +50,7 @@ impl owned_wasm_name_t {
/// You must ensure that the data pointed to by `wasm_name_t` is valid and
/// that it is not owned by anyone else.
pub unsafe fn new(name: &wasm_name_t) -> Self {
Self(wasm_name_t {
size: name.size,
data: name.data,
})
Self(wasm_name_t { size: name.size, data: name.data })
}
}

View File

@@ -23,11 +23,7 @@ impl WasmTableType {
});
let content = Box::new(table_type.ty.into());
Self {
table_type,
limits,
content,
}
Self { table_type, limits, content }
}
}
@@ -40,9 +36,7 @@ pub struct wasm_tabletype_t {
impl wasm_tabletype_t {
pub(crate) fn new(table_type: TableType) -> Self {
Self {
extern_type: wasm_externtype_t::new(ExternType::Table(table_type)),
}
Self { extern_type: wasm_externtype_t::new(ExternType::Table(table_type)) }
}
pub(crate) fn inner(&self) -> &WasmTableType {
@@ -61,11 +55,7 @@ pub unsafe extern "C" fn wasm_tabletype_new(
limits: &wasm_limits_t,
) -> Option<Box<wasm_tabletype_t>> {
let valtype = valtype?;
let max_elements = if limits.max == LIMITS_MAX_SENTINEL {
None
} else {
Some(limits.max as _)
};
let max_elements = if limits.max == LIMITS_MAX_SENTINEL { None } else { Some(limits.max as _) };
let table_type = Box::new(wasm_tabletype_t::new(TableType::new(
(*valtype).into(),
limits.min as _,

View File

@@ -50,9 +50,7 @@ pub struct wasm_valtype_t {
impl Default for wasm_valtype_t {
fn default() -> Self {
Self {
valkind: wasm_valkind_enum::WASM_I32,
}
Self { valkind: wasm_valkind_enum::WASM_I32 }
}
}
@@ -72,9 +70,7 @@ impl From<&wasm_valtype_t> for ValType {
impl From<ValType> for wasm_valtype_t {
fn from(other: ValType) -> Self {
Self {
valkind: other.into(),
}
Self { valkind: other.into() }
}
}
@@ -91,7 +87,5 @@ pub unsafe extern "C" fn wasm_valtype_delete(_valtype: Option<Box<wasm_valtype_t
#[no_mangle]
pub unsafe extern "C" fn wasm_valtype_kind(valtype: Option<&wasm_valtype_t>) -> wasm_valkind_t {
valtype
.expect("`wasm_valtype_kind: argument is a null pointer")
.valkind as wasm_valkind_t
valtype.expect("`wasm_valtype_kind: argument is a null pointer").valkind as wasm_valkind_t
}

View File

@@ -133,10 +133,7 @@ mod tests {
#[test]
fn test_wasmer_is_headless() {
set_var(
"COMPILER",
if cfg!(feature = "compiler") { "0" } else { "1" },
);
set_var("COMPILER", if cfg!(feature = "compiler") { "0" } else { "1" });
(assert_c! {
#include "tests/wasmer_wasm.h"
@@ -155,23 +152,9 @@ mod tests {
#[test]
fn test_wasmer_is_compiler_available() {
set_var(
"CRANELIFT",
if cfg!(feature = "cranelift") {
"1"
} else {
"0"
},
);
set_var("CRANELIFT", if cfg!(feature = "cranelift") { "1" } else { "0" });
set_var("LLVM", if cfg!(feature = "llvm") { "1" } else { "0" });
set_var(
"SINGLEPASS",
if cfg!(feature = "singlepass") {
"1"
} else {
"0"
},
);
set_var("SINGLEPASS", if cfg!(feature = "singlepass") { "1" } else { "0" });
(assert_c! {
#include "tests/wasmer_wasm.h"
@@ -196,14 +179,7 @@ mod tests {
fn test_wasmer_is_engine_available() {
set_var("JIT", if cfg!(feature = "jit") { "1" } else { "0" });
set_var("NATIVE", if cfg!(feature = "native") { "1" } else { "0" });
set_var(
"OBJECT_FILE",
if cfg!(feature = "object-file") {
"1"
} else {
"0"
},
);
set_var("OBJECT_FILE", if cfg!(feature = "object-file") { "1" } else { "0" });
(assert_c! {
#include "tests/wasmer_wasm.h"

View File

@@ -55,9 +55,7 @@ pub struct wasmer_features_t {
/// See the module's documentation.
#[no_mangle]
pub extern "C" fn wasmer_features_new() -> Box<wasmer_features_t> {
Box::new(wasmer_features_t {
inner: Features::new(),
})
Box::new(wasmer_features_t { inner: Features::new() })
}
/// Delete a [`wasmer_features_t`].

View File

@@ -215,10 +215,7 @@ pub extern "C" fn wasmer_metering_get_remaining_points(instance: &wasm_instance_
/// See module's documentation.
#[no_mangle]
pub extern "C" fn wasmer_metering_points_are_exhausted(instance: &wasm_instance_t) -> bool {
matches!(
get_remaining_points(&instance.inner),
MeteringPoints::Exhausted,
)
matches!(get_remaining_points(&instance.inner), MeteringPoints::Exhausted,)
}
/// Set a new amount of points for the given metering middleware.
@@ -313,7 +310,5 @@ pub extern "C" fn wasmer_metering_as_middleware(
) -> Option<Box<wasmer_middleware_t>> {
let metering = metering?;
Some(Box::new(wasmer_middleware_t {
inner: metering.inner,
}))
Some(Box::new(wasmer_middleware_t { inner: metering.inner }))
}

View File

@@ -147,10 +147,7 @@ pub unsafe extern "C" fn wasmer_triple_new(
triple: Option<&wasm_name_t>,
) -> Option<Box<wasmer_triple_t>> {
let triple = triple?;
let triple = c_try!(str::from_utf8(slice::from_raw_parts(
triple.data,
triple.size
)));
let triple = c_try!(str::from_utf8(slice::from_raw_parts(triple.data, triple.size)));
Some(Box::new(wasmer_triple_t {
inner: c_try!(Triple::from_str(triple).map_err(|e| CApiError { msg: e.to_string() })),
@@ -183,9 +180,7 @@ pub unsafe extern "C" fn wasmer_triple_new(
/// See also [`wasmer_triple_new`].
#[no_mangle]
pub extern "C" fn wasmer_triple_new_from_host() -> Box<wasmer_triple_t> {
Box::new(wasmer_triple_t {
inner: Triple::host(),
})
Box::new(wasmer_triple_t { inner: Triple::host() })
}
/// Delete a [`wasmer_triple_t`].
@@ -265,9 +260,7 @@ pub struct wasmer_cpu_features_t {
/// See [`wasmer_cpu_features_t`].
#[no_mangle]
pub extern "C" fn wasmer_cpu_features_new() -> Box<wasmer_cpu_features_t> {
Box::new(wasmer_cpu_features_t {
inner: CpuFeature::set(),
})
Box::new(wasmer_cpu_features_t { inner: CpuFeature::set() })
}
/// Delete a [`wasmer_cpu_features_t`].

View File

@@ -171,11 +171,9 @@ fn wasi_get_unordered_imports_inner(
let store = &store.inner;
let version = c_try!(
get_wasi_version(&module.inner, false).ok_or_else(|| CApiError {
msg: "could not detect a WASI version on the given module".to_string(),
})
);
let version = c_try!(get_wasi_version(&module.inner, false).ok_or_else(|| CApiError {
msg: "could not detect a WASI version on the given module".to_string(),
}));
let import_object = generate_import_object_from_env(store, wasi_env.inner.clone(), version);

View File

@@ -114,10 +114,7 @@ wasm_declare_vec!(val);
impl Clone for wasm_val_t {
fn clone(&self) -> Self {
wasm_val_t {
kind: self.kind,
of: self.of.clone(),
}
wasm_val_t { kind: self.kind, of: self.of.clone() }
}
}
@@ -130,18 +127,10 @@ pub unsafe extern "C" fn wasm_val_copy(
out.kind = val.kind;
out.of = match val.kind.try_into() {
Ok(kind) => match kind {
wasm_valkind_enum::WASM_I32 => wasm_val_inner {
int32_t: val.of.int32_t,
},
wasm_valkind_enum::WASM_I64 => wasm_val_inner {
int64_t: val.of.int64_t,
},
wasm_valkind_enum::WASM_F32 => wasm_val_inner {
float32_t: val.of.float32_t,
},
wasm_valkind_enum::WASM_F64 => wasm_val_inner {
float64_t: val.of.float64_t,
},
wasm_valkind_enum::WASM_I32 => wasm_val_inner { int32_t: val.of.int32_t },
wasm_valkind_enum::WASM_I64 => wasm_val_inner { int64_t: val.of.int64_t },
wasm_valkind_enum::WASM_F32 => wasm_val_inner { float32_t: val.of.float32_t },
wasm_valkind_enum::WASM_F64 => wasm_val_inner { float64_t: val.of.float64_t },
wasm_valkind_enum::WASM_ANYREF => wasm_val_inner { wref: val.of.wref },
wasm_valkind_enum::WASM_FUNCREF => wasm_val_inner { wref: val.of.wref },
},

View File

@@ -13,9 +13,7 @@ pub struct OutputCapturer {
impl OutputCapturer {
pub fn new() -> Self {
Self {
buffer: VecDeque::new(),
}
Self { buffer: VecDeque::new() }
}
}
@@ -48,36 +46,21 @@ impl WasiFile for OutputCapturer {
// fail when reading or Seeking
impl Read for OutputCapturer {
fn read(&mut self, _buf: &mut [u8]) -> io::Result<usize> {
Err(io::Error::new(
io::ErrorKind::Other,
"can not read from capturing stdout",
))
Err(io::Error::new(io::ErrorKind::Other, "can not read from capturing stdout"))
}
fn read_to_end(&mut self, _buf: &mut Vec<u8>) -> std::io::Result<usize> {
Err(std::io::Error::new(
std::io::ErrorKind::Other,
"can not read from capturing stdout",
))
Err(std::io::Error::new(std::io::ErrorKind::Other, "can not read from capturing stdout"))
}
fn read_to_string(&mut self, _buf: &mut String) -> io::Result<usize> {
Err(io::Error::new(
io::ErrorKind::Other,
"can not read from capturing stdout",
))
Err(io::Error::new(io::ErrorKind::Other, "can not read from capturing stdout"))
}
fn read_exact(&mut self, _buf: &mut [u8]) -> io::Result<()> {
Err(io::Error::new(
io::ErrorKind::Other,
"can not read from capturing stdout",
))
Err(io::Error::new(io::ErrorKind::Other, "can not read from capturing stdout"))
}
}
impl Seek for OutputCapturer {
fn seek(&mut self, _pos: io::SeekFrom) -> io::Result<u64> {
Err(io::Error::new(
io::ErrorKind::Other,
"can not seek capturing stdout",
))
Err(io::Error::new(io::ErrorKind::Other, "can not seek capturing stdout"))
}
}
impl Write for OutputCapturer {

View File

@@ -175,24 +175,18 @@ pub struct wasi_env_t {
#[no_mangle]
pub extern "C" fn wasi_env_new(mut config: Box<wasi_config_t>) -> Option<Box<wasi_env_t>> {
if !config.inherit_stdout {
config
.state_builder
.stdout(Box::new(capture_files::OutputCapturer::new()));
config.state_builder.stdout(Box::new(capture_files::OutputCapturer::new()));
}
if !config.inherit_stderr {
config
.state_builder
.stderr(Box::new(capture_files::OutputCapturer::new()));
config.state_builder.stderr(Box::new(capture_files::OutputCapturer::new()));
}
// TODO: impl capturer for stdin
let wasi_state = c_try!(config.state_builder.build());
Some(Box::new(wasi_env_t {
inner: WasiEnv::new(wasi_state),
}))
Some(Box::new(wasi_env_t { inner: WasiEnv::new(wasi_state) }))
}
/// Delete a [`wasi_env_t`].
@@ -272,10 +266,7 @@ fn read_inner(wasi_file: &mut Box<dyn WasiFile>, inner_buffer: &mut [u8]) -> isi
if let Some(oc) = wasi_file.downcast_mut::<capture_files::OutputCapturer>() {
let total_to_read = min(inner_buffer.len(), oc.buffer.len());
for (address, value) in inner_buffer
.iter_mut()
.zip(oc.buffer.drain(..total_to_read))
{
for (address, value) in inner_buffer.iter_mut().zip(oc.buffer.drain(..total_to_read)) {
*address = value;
}
@@ -367,11 +358,9 @@ fn wasi_get_imports_inner(
let store = &store.inner;
let version = c_try!(
get_wasi_version(&module.inner, false).ok_or_else(|| CApiError {
msg: "could not detect a WASI version on the given module".to_string(),
})
);
let version = c_try!(get_wasi_version(&module.inner, false).ok_or_else(|| CApiError {
msg: "could not detect a WASI version on the given module".to_string(),
}));
let import_object = generate_import_object_from_env(store, wasi_env.inner.clone(), version);