diff --git a/Cargo.lock b/Cargo.lock index 6d2b49672..3e0fe1f2d 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1402,6 +1402,15 @@ dependencies = [ "winapi", ] +[[package]] +name = "toml" +version = "0.5.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ffc92d160b1eef40665be3a05630d003936a3bc7da7421277846c2613e92c71a" +dependencies = [ + "serde", +] + [[package]] name = "tracing" version = "0.1.13" diff --git a/lib/api/src/import_object.rs b/lib/api/src/import_object.rs index 4911eab59..cca748f22 100644 --- a/lib/api/src/import_object.rs +++ b/lib/api/src/import_object.rs @@ -9,7 +9,7 @@ use std::{ sync::{Arc, Mutex}, }; use wasmer_engine::Resolver; -use wasmer_runtime::Export; +pub use wasmer_runtime::{Export, ExportFunction, ExportGlobal, ExportMemory, ExportTable}; /// The `LikeNamespace` trait represents objects that act as a namespace for imports. /// For example, an `Instance` or `Namespace` could be diff --git a/lib/api/src/lib.rs b/lib/api/src/lib.rs index 39146b8ff..24e387c03 100644 --- a/lib/api/src/lib.rs +++ b/lib/api/src/lib.rs @@ -14,7 +14,10 @@ mod types; pub use crate::exports::{ExportError, Exportable, Exports}; pub use crate::externals::{Extern, Function, Global, Memory, Table}; -pub use crate::import_object::{ImportObject, ImportObjectIterator, LikeNamespace}; +pub use crate::import_object::{ + Export, ExportFunction, ExportGlobal, ExportMemory, ExportTable, ImportObject, + ImportObjectIterator, LikeNamespace, +}; pub use crate::instance::Instance; pub use crate::memory_view::MemoryView; pub use crate::module::Module; diff --git a/lib/runtime-c-api/Cargo.toml b/lib/runtime-c-api/Cargo.toml index 56420bfe0..8504613cf 100644 --- a/lib/runtime-c-api/Cargo.toml +++ b/lib/runtime-c-api/Cargo.toml @@ -38,9 +38,9 @@ optional = true [features] default = ["cranelift-backend", "wasi"] -singlepass-backend = ["wasmer/compiler-singlepass", "wasmer/default-compiler-singlepass"] -cranelift-backend = ["wasmer/compiler-cranelift", "wasmer/default-compiler-cranelift"] -llvm-backend = ["wasmer/compiler-llvm", "wasmer/default-compiler-llvm"] +singlepass-backend = ["wasmer/singlepass"] +cranelift-backend = ["wasmer/cranelift"] +llvm-backend = ["wasmer/llvm"] wasi = ["wasmer-wasi"] #emscripten = ["wasmer-emscripten"] # used to avoid generating standard Wasm C API types in our header files diff --git a/lib/runtime-c-api/src/export.rs b/lib/runtime-c-api/src/export.rs index d4a0b6cc1..4fb040f6a 100644 --- a/lib/runtime-c-api/src/export.rs +++ b/lib/runtime-c-api/src/export.rs @@ -8,29 +8,24 @@ use crate::{ memory::wasmer_memory_t, module::wasmer_module_t, table::wasmer_table_t, - value::{wasmer_value, wasmer_value_t, wasmer_value_tag}, + value::{/*wasmer_value,*/ wasmer_value_t, wasmer_value_tag}, wasmer_byte_array, wasmer_result_t, }; use libc::{c_int, c_uint}; use std::{ptr, slice}; -use wasmer::export::{ExportDescriptor, ExternDescriptor}; -use wasmer::wasm::{Memory, Value}; -use wasmer::{Instance, Module}; +use wasmer::{ExportType, ExternType, ImportType, Instance, Memory, Module}; /// Intermediate representation of an `Export` instance that is /// exposed to C. pub(crate) struct NamedExport { - /// The export name. - pub(crate) name: String, - - /// The export instance. - pub(crate) extern_descriptor: ExternDescriptor, + /// The export type and name. + pub(crate) export_type: ExportType, /// The instance that holds the export. pub(crate) instance: *mut Instance, } -/// Opaque pointer to `NamedExport`. +/// Opaque pointer to `ImportType`. #[repr(C)] #[derive(Clone)] pub struct wasmer_export_t; @@ -42,7 +37,7 @@ pub struct wasmer_export_func_t; /// Intermediate representation of a vector of `NamedExport` that is /// exposed to C. -pub(crate) struct NamedExports(pub Vec); +pub(crate) struct NamedExports(pub Vec); /// Opaque pointer to the opaque structure `crate::NamedExports`, /// which is a wrapper around a vector of the opaque structure @@ -259,7 +254,7 @@ pub unsafe extern "C" fn wasmer_exports_get( return ptr::null_mut(); } let named_exports = &mut *(exports as *mut NamedExports); - &mut (*named_exports).0[idx as usize] as *mut NamedExport as *mut wasmer_export_t + &mut (*named_exports).0[idx as usize] as *mut ExportType as *mut wasmer_export_t } /// Gets wasmer_export kind @@ -269,12 +264,7 @@ pub unsafe extern "C" fn wasmer_export_kind( export: *mut wasmer_export_t, ) -> wasmer_import_export_kind { let named_export = &*(export as *mut NamedExport); - match named_export.extern_descriptor { - ExternDescriptor::Table(_) => wasmer_import_export_kind::WASM_TABLE, - ExternDescriptor::Function { .. } => wasmer_import_export_kind::WASM_FUNCTION, - ExternDescriptor::Global(_) => wasmer_import_export_kind::WASM_GLOBAL, - ExternDescriptor::Memory(_) => wasmer_import_export_kind::WASM_MEMORY, - } + (&named_export.export_type).into() } /// Sets the result parameter to the arity of the params of the wasmer_export_func_t @@ -290,8 +280,8 @@ pub unsafe extern "C" fn wasmer_export_func_params_arity( result: *mut u32, ) -> wasmer_result_t { let named_export = &*(func as *const NamedExport); - let export = &named_export.extern_descriptor; - if let ExternDescriptor::Function(ref signature) = *export { + let export = &named_export.export_type.ty(); + if let ExternType::Function(ref signature) = *export { *result = signature.params().len() as u32; wasmer_result_t::WASMER_OK } else { @@ -316,8 +306,8 @@ pub unsafe extern "C" fn wasmer_export_func_params( params_len: u32, ) -> wasmer_result_t { let named_export = &*(func as *const NamedExport); - let export = &named_export.extern_descriptor; - if let ExternDescriptor::Function(ref signature) = *export { + let export = &named_export.export_type.ty(); + if let ExternType::Function(ref signature) = *export { let params: &mut [wasmer_value_tag] = slice::from_raw_parts_mut(params, params_len as usize); for (i, item) in signature.params().iter().enumerate() { @@ -346,11 +336,11 @@ pub unsafe extern "C" fn wasmer_export_func_returns( returns_len: u32, ) -> wasmer_result_t { let named_export = &*(func as *const NamedExport); - let export = &named_export.extern_descriptor; - if let ExternDescriptor::Function(ref signature) = *export { + let export = &named_export.export_type.ty(); + if let ExternType::Function(ref signature) = *export { let returns: &mut [wasmer_value_tag] = slice::from_raw_parts_mut(returns, returns_len as usize); - for (i, item) in signature.returns().iter().enumerate() { + for (i, item) in signature.results().iter().enumerate() { returns[i] = item.into(); } wasmer_result_t::WASMER_OK @@ -375,9 +365,9 @@ pub unsafe extern "C" fn wasmer_export_func_returns_arity( result: *mut u32, ) -> wasmer_result_t { let named_export = &*(func as *const NamedExport); - let export = &named_export.extern_descriptor; - if let ExternDescriptor::Function(ref signature) = *export { - *result = signature.returns().len() as u32; + let export = &named_export.export_type.ty(); + if let ExternType::Function(ref signature) = *export { + *result = signature.results().len() as u32; wasmer_result_t::WASMER_OK } else { update_last_error(CApiError { @@ -411,7 +401,10 @@ pub unsafe extern "C" fn wasmer_export_to_memory( let named_export = &*(export as *const NamedExport); let instance = &*named_export.instance; - if let Ok(exported_memory) = instance.exports.get::(&named_export.name) { + if let Ok(exported_memory) = instance + .exports + .get::(&named_export.export_type.name()) + { let mem = Box::new(exported_memory.clone()); *memory = Box::into_raw(mem) as *mut wasmer_memory_t; wasmer_result_t::WASMER_OK @@ -431,8 +424,8 @@ pub unsafe extern "C" fn wasmer_export_to_memory( pub unsafe extern "C" fn wasmer_export_name(export: *mut wasmer_export_t) -> wasmer_byte_array { let named_export = &*(export as *mut NamedExport); wasmer_byte_array { - bytes: named_export.name.as_ptr(), - bytes_len: named_export.name.len() as u32, + bytes: named_export.export_type.name().as_ptr(), + bytes_len: named_export.export_type.name().len() as u32, } } @@ -452,7 +445,7 @@ pub unsafe extern "C" fn wasmer_export_func_call( results: *mut wasmer_value_t, results_len: c_uint, ) -> wasmer_result_t { - if func.is_null() { + /*if func.is_null() { update_last_error(CApiError { msg: "func ptr is null".to_string(), }); @@ -466,7 +459,7 @@ pub unsafe extern "C" fn wasmer_export_func_call( return wasmer_result_t::WASMER_ERROR; } - let params: Vec = { + let params: Vec = { if params_len == 0 { vec![] } else { @@ -481,31 +474,38 @@ pub unsafe extern "C" fn wasmer_export_func_call( let named_export = &*(func as *mut NamedExport); let results: &mut [wasmer_value_t] = slice::from_raw_parts_mut(results, results_len as usize); + */ - let instance = &*named_export.instance; - let result = instance.call(&named_export.name, ¶ms[..]); + /* let instance = &*named_export.instance; + let get_result = || { + let f: DynFunc = instanec.exports.get(&named_export.name)?; + f.call(¶ms[..]) + }; + let result = get_result();*/ + unimplemented!("DynFunc not yet implemented") + /* match result { Ok(results_vec) => { if !results_vec.is_empty() { let ret = match results_vec[0] { - Value::I32(x) => wasmer_value_t { + Val::I32(x) => wasmer_value_t { tag: wasmer_value_tag::WASM_I32, value: wasmer_value { I32: x }, }, - Value::I64(x) => wasmer_value_t { + Val::I64(x) => wasmer_value_t { tag: wasmer_value_tag::WASM_I64, value: wasmer_value { I64: x }, }, - Value::F32(x) => wasmer_value_t { + Val::F32(x) => wasmer_value_t { tag: wasmer_value_tag::WASM_F32, value: wasmer_value { F32: x }, }, - Value::F64(x) => wasmer_value_t { + Val::F64(x) => wasmer_value_t { tag: wasmer_value_tag::WASM_F64, value: wasmer_value { F64: x }, }, - Value::V128(_) => unimplemented!("returning V128 type"), + Val::V128(_) => unimplemented!("returning V128 type"), }; results[0] = ret; } @@ -516,19 +516,55 @@ pub unsafe extern "C" fn wasmer_export_func_call( wasmer_result_t::WASMER_ERROR } } + */ } -impl<'a> From> for NamedExportDescriptor { - fn from(ed: ExportDescriptor) -> Self { - let kind = match ed.ty { - ExternDescriptor::Memory(_) => wasmer_import_export_kind::WASM_MEMORY, - ExternDescriptor::Global(_) => wasmer_import_export_kind::WASM_GLOBAL, - ExternDescriptor::Table(_) => wasmer_import_export_kind::WASM_TABLE, - ExternDescriptor::Function(_) => wasmer_import_export_kind::WASM_FUNCTION, - }; +impl From for NamedExportDescriptor { + fn from(et: ExportType) -> Self { NamedExportDescriptor { - name: ed.name.to_string(), - kind, + name: et.name().to_string(), + kind: et.into(), } } } + +impl From<&ImportType> for wasmer_import_export_kind { + fn from(it: &ImportType) -> Self { + it.ty().into() + } +} + +impl From for wasmer_import_export_kind { + fn from(it: ImportType) -> Self { + (&it).into() + } +} + +impl From<&ExportType> for wasmer_import_export_kind { + fn from(et: &ExportType) -> Self { + et.ty().into() + } +} + +impl From for wasmer_import_export_kind { + fn from(et: ExportType) -> Self { + (&et).into() + } +} + +impl From<&ExternType> for wasmer_import_export_kind { + fn from(et: &ExternType) -> Self { + match et { + ExternType::Memory(_) => wasmer_import_export_kind::WASM_MEMORY, + ExternType::Global(_) => wasmer_import_export_kind::WASM_GLOBAL, + ExternType::Table(_) => wasmer_import_export_kind::WASM_TABLE, + ExternType::Function(_) => wasmer_import_export_kind::WASM_FUNCTION, + } + } +} + +impl From for wasmer_import_export_kind { + fn from(et: ExternType) -> Self { + (&et).into() + } +} diff --git a/lib/runtime-c-api/src/global.rs b/lib/runtime-c-api/src/global.rs index 79e0cd6ad..996cca24d 100644 --- a/lib/runtime-c-api/src/global.rs +++ b/lib/runtime-c-api/src/global.rs @@ -1,7 +1,7 @@ //! Create, set, get and destroy global variables of an instance. use crate::value::{wasmer_value_t, wasmer_value_tag}; -use wasmer::wasm::Global; +use wasmer::Global; #[repr(C)] #[derive(Clone)] @@ -21,12 +21,14 @@ pub unsafe extern "C" fn wasmer_global_new( value: wasmer_value_t, mutable: bool, ) -> *mut wasmer_global_t { - let global = if mutable { - Global::new_mutable(value.into()) + let global_store = todo!("Implement global store"); + /*let global = if mutable { + Global::new_mut(value.into()) } else { Global::new(value.into()) }; Box::into_raw(Box::new(global)) as *mut wasmer_global_t + */ } /// Gets the value stored by the given Global @@ -53,9 +55,9 @@ pub extern "C" fn wasmer_global_get_descriptor( global: *mut wasmer_global_t, ) -> wasmer_global_descriptor_t { let global = unsafe { &*(global as *mut Global) }; - let descriptor = global.descriptor(); + let descriptor = global.ty(); wasmer_global_descriptor_t { - mutable: descriptor.mutable, + mutable: descriptor.mutability.into(), kind: descriptor.ty.into(), } } diff --git a/lib/runtime-c-api/src/import/mod.rs b/lib/runtime-c-api/src/import/mod.rs index 79631de1c..029810b60 100644 --- a/lib/runtime-c-api/src/import/mod.rs +++ b/lib/runtime-c-api/src/import/mod.rs @@ -12,18 +12,21 @@ use crate::{ use libc::c_uint; use std::{ convert::TryFrom, - ffi::{c_void, CStr}, + ffi::c_void, os::raw::c_char, - ptr, slice, - sync::Arc, + ptr, + slice, + //sync::Arc, }; -use wasmer::import::{ImportObject, ImportObjectIterator}; -use wasmer::vm::Ctx; -use wasmer::wasm::{Export, FuncSig, Global, Memory, Module, Table, Type}; -use wasmer_runtime_core::{ +use wasmer::{ + Export, ExportFunction, ExportGlobal, ExportMemory, ExportTable, Global, ImportObject, + ImportObjectIterator, ImportType, Memory, Module, Table, +}; +//use wasmer::wasm::{Export, FuncSig, Global, Memory, Module, Table, Type}; +/*use wasmer_runtime_core::{ export::{Context, FuncPointer}, module::ImportName, -}; +};*/ #[repr(C)] pub struct wasmer_import_t { @@ -122,11 +125,9 @@ pub unsafe extern "C" fn wasmer_import_object_get_import( } let import_out = &mut *import; let import_export_value_out = &mut *import_export_value; - if let Some(export) = - import_object.maybe_with_namespace(namespace_str, |ns| ns.get_export(name_str)) - { + if let Some(export) = import_object.get_export(namespace_str, name_str) { match export { - Export::Function { .. } => { + Export::Function(function) => { if tag != wasmer_import_export_kind::WASM_FUNCTION { update_last_error(CApiError { msg: format!("Found function, expected {}", tag.to_str()), @@ -134,8 +135,8 @@ pub unsafe extern "C" fn wasmer_import_object_get_import( return wasmer_result_t::WASMER_ERROR; } import_out.tag = wasmer_import_export_kind::WASM_FUNCTION; - let writer = import_export_value_out.func as *mut Export; - *writer = export.clone(); + let writer = import_export_value_out.func as *mut ExportFunction; + *writer = function.clone(); } Export::Memory(memory) => { if tag != wasmer_import_export_kind::WASM_MEMORY { @@ -145,7 +146,7 @@ pub unsafe extern "C" fn wasmer_import_object_get_import( return wasmer_result_t::WASMER_ERROR; } import_out.tag = wasmer_import_export_kind::WASM_MEMORY; - let writer = import_export_value_out.func as *mut Memory; + let writer = import_export_value_out.func as *mut ExportMemory; *writer = memory.clone(); } Export::Table(table) => { @@ -156,7 +157,7 @@ pub unsafe extern "C" fn wasmer_import_object_get_import( return wasmer_result_t::WASMER_ERROR; } import_out.tag = wasmer_import_export_kind::WASM_TABLE; - let writer = import_export_value_out.func as *mut Table; + let writer = import_export_value_out.func as *mut ExportTable; *writer = table.clone(); } Export::Global(global) => { @@ -167,7 +168,7 @@ pub unsafe extern "C" fn wasmer_import_object_get_import( return wasmer_result_t::WASMER_ERROR; } import_out.tag = wasmer_import_export_kind::WASM_GLOBAL; - let writer = import_export_value_out.func as *mut Global; + let writer = import_export_value_out.func as *mut ExportGlobal; *writer = global.clone(); } } @@ -205,8 +206,8 @@ pub unsafe extern "C" fn wasmer_import_object_iterate_functions( return std::ptr::null_mut(); } let import_object: &ImportObject = &*(import_object as *const ImportObject); - let iter_inner = Box::new(import_object.clone_ref().into_iter().filter(|(_, _, e)| { - if let Export::Function { .. } = e { + let iter_inner = Box::new(import_object.clone_ref().into_iter().filter(|((_, _), e)| { + if let Export::Function(_) = e { true } else { false @@ -238,7 +239,7 @@ pub unsafe extern "C" fn wasmer_import_object_iter_next( let out = &mut *import; // TODO: the copying here can be optimized away, we just need to use a different type of // iterator internally - if let Some((namespace, name, export)) = iter.0.next() { + if let Some(((namespace, name), export)) = iter.0.next() { let ns = { let mut n = namespace.clone(); n.shrink_to_fit(); @@ -266,8 +267,8 @@ pub unsafe extern "C" fn wasmer_import_object_iter_next( std::mem::forget(name); match export { - Export::Function { .. } => { - let func = Box::new(export.clone()); + Export::Function(function) => { + let func = Box::new(function.clone()); out.tag = wasmer_import_export_kind::WASM_FUNCTION; out.value = wasmer_import_export_value { @@ -384,7 +385,7 @@ pub unsafe extern "C" fn wasmer_import_object_extend( ) -> wasmer_result_t { let import_object: &mut ImportObject = &mut *(import_object as *mut ImportObject); - let mut extensions: Vec<(String, String, Export)> = Vec::new(); + let mut extensions: Vec<((String, String), Export)> = Vec::new(); let imports: &[wasmer_import_t] = slice::from_raw_parts(imports, imports_len as usize); for import in imports { @@ -415,24 +416,24 @@ pub unsafe extern "C" fn wasmer_import_object_extend( let export = match import.tag { wasmer_import_export_kind::WASM_MEMORY => { - let mem = import.value.memory as *mut Memory; + let mem = import.value.memory as *mut ExportMemory; Export::Memory((&*mem).clone()) } wasmer_import_export_kind::WASM_FUNCTION => { - let func_export = import.value.func as *mut Export; - (&*func_export).clone() + let func_export = import.value.func as *mut ExportFunction; + Export::Function((&*func_export).clone()) } wasmer_import_export_kind::WASM_GLOBAL => { - let global = import.value.global as *mut Global; + let global = import.value.global as *mut ExportGlobal; Export::Global((&*global).clone()) } wasmer_import_export_kind::WASM_TABLE => { - let table = import.value.table as *mut Table; + let table = import.value.table as *mut ExportTable; Export::Table((&*table).clone()) } }; - let extension = (module_name.to_string(), import_name.to_string(), export); + let extension = ((module_name.to_string(), import_name.to_string()), export); extensions.push(extension) } @@ -454,88 +455,7 @@ pub unsafe extern "C" fn wasmer_import_descriptors( return; } let module = &*(module as *const Module); - let total_imports = module.info().imported_functions.len() - + module.info().imported_tables.len() - + module.info().imported_globals.len() - + module.info().imported_memories.len(); - let mut descriptors: Vec = Vec::with_capacity(total_imports); - - for ( - _index, - ImportName { - namespace_index, - name_index, - }, - ) in &module.info().imported_functions - { - let namespace = module.info().namespace_table.get(*namespace_index); - let name = module.info().name_table.get(*name_index); - descriptors.push(NamedImportDescriptor { - module: namespace.to_string(), - name: name.to_string(), - kind: wasmer_import_export_kind::WASM_FUNCTION, - }); - } - - for ( - _index, - ( - ImportName { - namespace_index, - name_index, - }, - _, - ), - ) in &module.info().imported_tables - { - let namespace = module.info().namespace_table.get(*namespace_index); - let name = module.info().name_table.get(*name_index); - descriptors.push(NamedImportDescriptor { - module: namespace.to_string(), - name: name.to_string(), - kind: wasmer_import_export_kind::WASM_TABLE, - }); - } - - for ( - _index, - ( - ImportName { - namespace_index, - name_index, - }, - _, - ), - ) in &module.info().imported_globals - { - let namespace = module.info().namespace_table.get(*namespace_index); - let name = module.info().name_table.get(*name_index); - descriptors.push(NamedImportDescriptor { - module: namespace.to_string(), - name: name.to_string(), - kind: wasmer_import_export_kind::WASM_GLOBAL, - }); - } - - for ( - _index, - ( - ImportName { - namespace_index, - name_index, - }, - _, - ), - ) in &module.info().imported_memories - { - let namespace = module.info().namespace_table.get(*namespace_index); - let name = module.info().name_table.get(*name_index); - descriptors.push(NamedImportDescriptor { - module: namespace.to_string(), - name: name.to_string(), - kind: wasmer_import_export_kind::WASM_MEMORY, - }); - } + let descriptors = module.imports().collect::>(); let named_import_descriptors: Box = Box::new(NamedImportDescriptors(descriptors)); @@ -543,7 +463,7 @@ pub unsafe extern "C" fn wasmer_import_descriptors( Box::into_raw(named_import_descriptors) as *mut wasmer_import_descriptors_t; } -pub struct NamedImportDescriptors(Vec); +pub struct NamedImportDescriptors(Vec); /// Frees the memory for the given import descriptors #[allow(clippy::cast_ptr_alignment)] @@ -579,7 +499,7 @@ pub unsafe extern "C" fn wasmer_import_descriptors_get( return ptr::null_mut(); } let named_import_descriptors = &mut *(import_descriptors as *mut NamedImportDescriptors); - &mut (*named_import_descriptors).0[idx as usize] as *mut NamedImportDescriptor + &mut (*named_import_descriptors).0[idx as usize] as *mut ImportType as *mut wasmer_import_descriptor_t } @@ -589,10 +509,10 @@ pub unsafe extern "C" fn wasmer_import_descriptors_get( pub unsafe extern "C" fn wasmer_import_descriptor_name( import_descriptor: *mut wasmer_import_descriptor_t, ) -> wasmer_byte_array { - let named_import_descriptor = &*(import_descriptor as *mut NamedImportDescriptor); + let named_import_descriptor = &*(import_descriptor as *mut ImportType); wasmer_byte_array { - bytes: named_import_descriptor.name.as_ptr(), - bytes_len: named_import_descriptor.name.len() as u32, + bytes: named_import_descriptor.name().as_ptr(), + bytes_len: named_import_descriptor.name().len() as u32, } } @@ -602,10 +522,10 @@ pub unsafe extern "C" fn wasmer_import_descriptor_name( pub unsafe extern "C" fn wasmer_import_descriptor_module_name( import_descriptor: *mut wasmer_import_descriptor_t, ) -> wasmer_byte_array { - let named_import_descriptor = &*(import_descriptor as *mut NamedImportDescriptor); + let named_import_descriptor = &*(import_descriptor as *mut ImportType); wasmer_byte_array { - bytes: named_import_descriptor.module.as_ptr(), - bytes_len: named_import_descriptor.module.len() as u32, + bytes: named_import_descriptor.module().as_ptr(), + bytes_len: named_import_descriptor.module().len() as u32, } } @@ -615,8 +535,8 @@ pub unsafe extern "C" fn wasmer_import_descriptor_module_name( pub unsafe extern "C" fn wasmer_import_descriptor_kind( export: *mut wasmer_import_descriptor_t, ) -> wasmer_import_export_kind { - let named_import_descriptor = &*(export as *mut NamedImportDescriptor); - named_import_descriptor.kind.clone() + let named_import_descriptor = &*(export as *mut ImportType); + named_import_descriptor.ty().into() } /// Sets the result parameter to the arity of the params of the wasmer_import_func_t @@ -631,8 +551,9 @@ pub unsafe extern "C" fn wasmer_import_func_params_arity( func: *const wasmer_import_func_t, result: *mut u32, ) -> wasmer_result_t { - let export = &*(func as *const Export); - if let Export::Function { ref signature, .. } = *export { + todo!("Figure out how to get a usable siganture from an ExportFunction") + /*let function = &*(func as *const ExportFunction); + if let Export::Function(function) = *export { *result = signature.params().len() as u32; wasmer_result_t::WASMER_OK } else { @@ -641,6 +562,7 @@ pub unsafe extern "C" fn wasmer_import_func_params_arity( }); wasmer_result_t::WASMER_ERROR } + */ } /// Creates new host function, aka imported function. `func` is a @@ -669,6 +591,8 @@ pub unsafe extern "C" fn wasmer_import_func_new( returns: *const wasmer_value_tag, returns_len: c_uint, ) -> *mut wasmer_import_func_t { + unimplemented!("`wasmer_import_func_new` cannot be implemented yet") + /* let params: &[wasmer_value_tag] = slice::from_raw_parts(params, params_len as usize); let params: Vec = params.iter().cloned().map(|x| x.into()).collect(); let returns: &[wasmer_value_tag] = slice::from_raw_parts(returns, returns_len as usize); @@ -680,6 +604,7 @@ pub unsafe extern "C" fn wasmer_import_func_new( signature: Arc::new(FuncSig::new(params, returns)), }); Box::into_raw(export) as *mut wasmer_import_func_t + */ } /// Stop the execution of a host function, aka imported function. The @@ -702,6 +627,8 @@ pub unsafe extern "C" fn wasmer_trap( ctx: *const wasmer_instance_context_t, error_message: *const c_char, ) -> wasmer_result_t { + todo!("wasmer_trap: manually trap without Ctx") + /* if ctx.is_null() { update_last_error(CApiError { msg: "ctx ptr is null in wasmer_trap".to_string(), @@ -733,6 +660,7 @@ pub unsafe extern "C" fn wasmer_trap( // cbindgen, and get an acceptable clean code. #[allow(unreachable_code)] wasmer_result_t::WASMER_OK + */ } /// Sets the params buffer to the parameter types of the given wasmer_import_func_t @@ -748,8 +676,10 @@ pub unsafe extern "C" fn wasmer_import_func_params( params: *mut wasmer_value_tag, params_len: c_uint, ) -> wasmer_result_t { - let export = &*(func as *const Export); - if let Export::Function { ref signature, .. } = *export { + todo!("Figure out how to get a usable signature from an `ExportFunction`") + /* + let function = &*(func as *const ExportFunction); + if let Export::Function(function) = *export { let params: &mut [wasmer_value_tag] = slice::from_raw_parts_mut(params, params_len as usize); for (i, item) in signature.params().iter().enumerate() { @@ -762,6 +692,7 @@ pub unsafe extern "C" fn wasmer_import_func_params( }); wasmer_result_t::WASMER_ERROR } + */ } /// Sets the returns buffer to the parameter types of the given wasmer_import_func_t @@ -777,20 +708,16 @@ pub unsafe extern "C" fn wasmer_import_func_returns( returns: *mut wasmer_value_tag, returns_len: c_uint, ) -> wasmer_result_t { - let export = &*(func as *const Export); - if let Export::Function { ref signature, .. } = *export { + todo!("Figure out how to get a usable signature from an `ExportFunction`") + /* + let function = &*(func as *const ExportFunction); let returns: &mut [wasmer_value_tag] = slice::from_raw_parts_mut(returns, returns_len as usize); for (i, item) in signature.returns().iter().enumerate() { returns[i] = item.into(); } wasmer_result_t::WASMER_OK - } else { - update_last_error(CApiError { - msg: "func ptr error in wasmer_import_func_returns".to_string(), - }); - wasmer_result_t::WASMER_ERROR - } + */ } /// Sets the result parameter to the arity of the returns of the wasmer_import_func_t @@ -805,16 +732,12 @@ pub unsafe extern "C" fn wasmer_import_func_returns_arity( func: *const wasmer_import_func_t, result: *mut u32, ) -> wasmer_result_t { - let export = &*(func as *const Export); - if let Export::Function { ref signature, .. } = *export { - *result = signature.returns().len() as u32; - wasmer_result_t::WASMER_OK - } else { - update_last_error(CApiError { - msg: "func ptr error in wasmer_import_func_results_arity".to_string(), - }); - wasmer_result_t::WASMER_ERROR - } + todo!("Figure out how to get a usable signature from an `ExportFunction`") + /* + let function = &*(func as *const ExportFunction); + *result = signature.returns().len() as u32; + wasmer_result_t::WASMER_OK + */ } /// Frees memory for the given Func @@ -822,7 +745,7 @@ pub unsafe extern "C" fn wasmer_import_func_returns_arity( #[no_mangle] pub extern "C" fn wasmer_import_func_destroy(func: *mut wasmer_import_func_t) { if !func.is_null() { - unsafe { Box::from_raw(func as *mut Export) }; + unsafe { Box::from_raw(func as *mut ExportFunction) }; } } @@ -833,9 +756,3 @@ pub extern "C" fn wasmer_import_object_destroy(import_object: *mut wasmer_import unsafe { Box::from_raw(import_object as *mut ImportObject) }; } } - -struct NamedImportDescriptor { - module: String, - name: String, - kind: wasmer_import_export_kind, -} diff --git a/lib/runtime-c-api/src/import/wasi.rs b/lib/runtime-c-api/src/import/wasi.rs index fab0a1c5f..8d3b72f22 100644 --- a/lib/runtime-c-api/src/import/wasi.rs +++ b/lib/runtime-c-api/src/import/wasi.rs @@ -68,6 +68,8 @@ pub unsafe extern "C" fn wasmer_wasi_generate_import_object( mapped_dirs: *const wasmer_wasi_map_dir_entry_t, mapped_dirs_len: c_uint, ) -> *mut wasmer_import_object_t { + todo!("wasmer_wasi_generate_import_object: blocked on global store") + /* let arg_list = get_slice_checked(args, args_len as usize); let env_list = get_slice_checked(envs, envs_len as usize); let preopened_file_list = get_slice_checked(preopened_files, preopened_files_len as usize); @@ -81,6 +83,7 @@ pub unsafe extern "C" fn wasmer_wasi_generate_import_object( mapped_dir_list, ) .unwrap_or(ptr::null_mut()) + */ } /// Creates a WASI import object for a specific version. @@ -145,6 +148,8 @@ fn wasmer_wasi_generate_import_object_inner( preopened_file_list: &[wasmer_byte_array], mapped_dir_list: &[wasmer_wasi_map_dir_entry_t], ) -> Result<*mut wasmer_import_object_t, str::Utf8Error> { + todo!("wasmer_wasi_generate_import_object_inner: blocked on global store") + /* let arg_vec = arg_list.iter().map(|arg| unsafe { arg.as_vec() }).collect(); let env_vec = env_list .iter() @@ -174,6 +179,7 @@ fn wasmer_wasi_generate_import_object_inner( mapped_dir_vec, )); Ok(Box::into_raw(import_object) as *mut wasmer_import_object_t) + */ } /// Convenience function that creates a WASI import object with no arguments, @@ -184,9 +190,20 @@ fn wasmer_wasi_generate_import_object_inner( #[no_mangle] pub unsafe extern "C" fn wasmer_wasi_generate_default_import_object() -> *mut wasmer_import_object_t { - let import_object = Box::new(wasi::generate_import_object(vec![], vec![], vec![], vec![])); + /* let mut wasi_state_builder = wasi::WasiState::new(); + let wasi_state = wasi_state_builder.build().unwrap(); + let mut wasi_env = WasiEnv::new(wasi_state); + // this API will now leak a `Memory` + let memory = todo!("get a memory"); + let store = todo!("somehow get a store"); + wasi_env.set_memory(memory); + let import_object = Box::new(wasi::generate_import_object_from_env(store, )); Box::into_raw(import_object) as *mut wasmer_import_object_t + */ + unimplemented!( + "This function can't be implemented until more backwards compatibilty code exists" + ); } #[cfg(test)] diff --git a/lib/runtime-c-api/src/instance.rs b/lib/runtime-c-api/src/instance.rs index e200f3ca4..8ea8642af 100644 --- a/lib/runtime-c-api/src/instance.rs +++ b/lib/runtime-c-api/src/instance.rs @@ -2,17 +2,18 @@ use crate::{ error::{update_last_error, CApiError}, - export::{wasmer_exports_t, wasmer_import_export_kind, NamedExport, NamedExports}, + export::{wasmer_exports_t, NamedExports}, import::wasmer_import_t, memory::wasmer_memory_t, - value::{wasmer_value, wasmer_value_t, wasmer_value_tag}, + value::wasmer_value_t, + //value::{wasmer_value, wasmer_value_t, wasmer_value_tag}, wasmer_result_t, }; use libc::{c_char, c_int, c_void}; -use std::{collections::HashMap, ffi::CStr, ptr, slice}; -use wasmer::import::{ImportObject, Namespace}; -use wasmer::vm::Ctx; -use wasmer::wasm::{Export, Global, Instance, Memory, Table, Value}; +//use std::{collections::HashMap, ptr, slice}; +use std::ptr; +//use wasmer::{ExportType, Exports, Global, ImportObject, Instance, Memory, Table}; +use wasmer::{ExportType, Instance}; /// Opaque pointer to a `wasmer_runtime::Instance` value in Rust. /// @@ -107,6 +108,8 @@ pub unsafe extern "C" fn wasmer_instantiate( }); return wasmer_result_t::WASMER_ERROR; } + todo!("Figure out how instantiating works") + /* let imports: &[wasmer_import_t] = slice::from_raw_parts(imports, imports_len as usize); let mut import_object = ImportObject::new(); let mut namespaces = HashMap::new(); @@ -136,7 +139,7 @@ pub unsafe extern "C" fn wasmer_instantiate( return wasmer_result_t::WASMER_ERROR; }; - let namespace = namespaces.entry(module_name).or_insert_with(Namespace::new); + let namespace = namespaces.entry(module_name).or_insert_with(Exports::new); // TODO check that tag is actually in bounds here let export = match import.tag { @@ -164,7 +167,9 @@ pub unsafe extern "C" fn wasmer_instantiate( } let bytes: &[u8] = slice::from_raw_parts_mut(wasm_bytes, wasm_bytes_len as usize); - let module_result = wasmer::compiler::compile(bytes); + let store = todo!("Get global store"); + */ + /*let module_result = Module::compile(store, bytes); let module = match module_result { Ok(module) => module, Err(error) => { @@ -182,6 +187,7 @@ pub unsafe extern "C" fn wasmer_instantiate( }; *instance = Box::into_raw(Box::new(new_instance)) as *mut wasmer_instance_t; wasmer_result_t::WASMER_OK + */ } /// Returns the instance context. Learn more by looking at the @@ -207,10 +213,13 @@ pub extern "C" fn wasmer_instance_context_get( return ptr::null() as _; } + unimplemented!("wasmer_instance_context_get: API changed") + /* let instance = unsafe { &*(instance as *const Instance) }; let context: *const Ctx = instance.context() as *const _; context as *const wasmer_instance_context_t + */ } /// Calls an exported function of a WebAssembly instance by `name` @@ -272,6 +281,8 @@ pub unsafe extern "C" fn wasmer_instance_call( results: *mut wasmer_value_t, results_len: u32, ) -> wasmer_result_t { + unimplemented!("wasmer_instance_call: DynFunc not yet implemented!") + /* if instance.is_null() { update_last_error(CApiError { msg: "instance ptr is null".to_string(), @@ -297,7 +308,7 @@ pub unsafe extern "C" fn wasmer_instance_call( } let params: &[wasmer_value_t] = slice::from_raw_parts(params, params_len as usize); - let params: Vec = params.iter().cloned().map(|x| x.into()).collect(); + let params: Vec = params.iter().cloned().map(|x| x.into()).collect(); let func_name_c = CStr::from_ptr(name); let func_name_r = func_name_c.to_str().unwrap(); @@ -309,23 +320,23 @@ pub unsafe extern "C" fn wasmer_instance_call( Ok(results_vec) => { if !results_vec.is_empty() { let ret = match results_vec[0] { - Value::I32(x) => wasmer_value_t { + Val::I32(x) => wasmer_value_t { tag: wasmer_value_tag::WASM_I32, value: wasmer_value { I32: x }, }, - Value::I64(x) => wasmer_value_t { + Val::I64(x) => wasmer_value_t { tag: wasmer_value_tag::WASM_I64, value: wasmer_value { I64: x }, }, - Value::F32(x) => wasmer_value_t { + Val::F32(x) => wasmer_value_t { tag: wasmer_value_tag::WASM_F32, value: wasmer_value { F32: x }, }, - Value::F64(x) => wasmer_value_t { + Val::F64(x) => wasmer_value_t { tag: wasmer_value_tag::WASM_F64, value: wasmer_value { F64: x }, }, - Value::V128(_) => unimplemented!("calling function with V128 parameter"), + Val::V128(_) => unimplemented!("calling function with V128 parameter"), }; results[0] = ret; } @@ -336,6 +347,7 @@ pub unsafe extern "C" fn wasmer_instance_call( wasmer_result_t::WASMER_ERROR } } + */ } /// Gets all the exports of the given WebAssembly instance. @@ -391,15 +403,7 @@ pub unsafe extern "C" fn wasmer_instance_exports( } let instance_ref = &mut *(instance as *mut Instance); - let mut exports_vec: Vec = Vec::with_capacity(instance_ref.exports().count()); - - for export_descriptor in instance_ref.module.exports().into_iter() { - exports_vec.push(NamedExport { - name: export_descriptor.name.to_string(), - extern_descriptor: export_descriptor.ty, - instance: instance as *mut Instance, - }); - } + let mut exports_vec: Vec = instance_ref.module().exports().collect(); let named_exports: Box = Box::new(NamedExports(exports_vec)); @@ -440,6 +444,10 @@ pub extern "C" fn wasmer_instance_context_data_set( instance: *mut wasmer_instance_t, data_ptr: *mut c_void, ) { + unimplemented!( + "wasmer_instance_context_data_set: API changed in a way that this is non-obvious" + ) + /* if instance.is_null() { return; } @@ -447,6 +455,7 @@ pub extern "C" fn wasmer_instance_context_data_set( let instance = unsafe { &mut *(instance as *mut Instance) }; instance.context_mut().data = data_ptr; + */ } /// Gets the `memory_idx`th memory of the instance. @@ -476,9 +485,11 @@ pub extern "C" fn wasmer_instance_context_memory( ctx: *const wasmer_instance_context_t, _memory_idx: u32, ) -> *const wasmer_memory_t { - let ctx = unsafe { &*(ctx as *const Ctx) }; + unimplemented!("wasmer_instance_context_memory: API changed") + /*let ctx = unsafe { &*(ctx as *const Ctx) }; let memory = ctx.memory(0); memory as *const Memory as *const wasmer_memory_t + */ } /// Gets the data that can be hold by an instance. @@ -494,6 +505,8 @@ pub extern "C" fn wasmer_instance_context_memory( pub extern "C" fn wasmer_instance_context_data_get( ctx: *const wasmer_instance_context_t, ) -> *mut c_void { + unimplemented!("wasmer_instance_context_data_get: API changed") + /* if ctx.is_null() { return ptr::null_mut() as _; } @@ -501,6 +514,7 @@ pub extern "C" fn wasmer_instance_context_data_get( let ctx = unsafe { &*(ctx as *const Ctx) }; ctx.data + */ } /// Frees memory for the given `wasmer_instance_t`. diff --git a/lib/runtime-c-api/src/lib.rs b/lib/runtime-c-api/src/lib.rs index 591ea5547..9a514f33a 100644 --- a/lib/runtime-c-api/src/lib.rs +++ b/lib/runtime-c-api/src/lib.rs @@ -86,14 +86,12 @@ #![deny( dead_code, unused_imports, - unused_variables, + // temporarily disabled + //unused_variables, unused_unsafe, unreachable_patterns )] -#[cbindgen::ignore] -mod wasm_c_api; - pub mod error; pub mod export; pub mod global; diff --git a/lib/runtime-c-api/src/memory.rs b/lib/runtime-c-api/src/memory.rs index aaf6d5644..dc67ade75 100644 --- a/lib/runtime-c-api/src/memory.rs +++ b/lib/runtime-c-api/src/memory.rs @@ -5,9 +5,7 @@ use crate::{ wasmer_limits_t, wasmer_result_t, }; use std::{cell::Cell, ptr}; -use wasmer::wasm::Memory; -use wasmer::MemoryType; -use wasmer::{Bytes, Pages}; +use wasmer::{Bytes, Memory, Pages}; /// Opaque pointer to a `wasmer_runtime::Memory` value in Rust. /// @@ -64,22 +62,15 @@ pub unsafe extern "C" fn wasmer_memory_new( memory: *mut *mut wasmer_memory_t, limits: wasmer_limits_t, ) -> wasmer_result_t { + unimplemented!("wasmer_memory_new needs the global store") + /* let max = if limits.max.has_some { Some(Pages(limits.max.some)) } else { None }; let desc = MemoryType::new(Pages(limits.min), max, false); - let new_desc = match desc { - Ok(desc) => desc, - Err(error) => { - update_last_error(CApiError { - msg: error.to_string(), - }); - return wasmer_result_t::WASMER_ERROR; - } - }; - let result = Memory::new(new_desc); + let result = Memory::new(desc); let new_memory = match result { Ok(memory) => memory, Err(error) => { @@ -89,6 +80,7 @@ pub unsafe extern "C" fn wasmer_memory_new( }; *memory = Box::into_raw(Box::new(new_memory)) as *mut wasmer_memory_t; wasmer_result_t::WASMER_OK + */ } /// Grows a memory by the given number of pages (of 65Kb each). @@ -122,12 +114,8 @@ pub extern "C" fn wasmer_memory_grow(memory: *mut wasmer_memory_t, delta: u32) - let delta_result = memory.grow(Pages(delta)); match delta_result { - Ok(_) => wasmer_result_t::WASMER_OK, - Err(grow_error) => { - update_last_error(grow_error); - - wasmer_result_t::WASMER_ERROR - } + Some(_) => wasmer_result_t::WASMER_OK, + _ => wasmer_result_t::WASMER_ERROR, } } diff --git a/lib/runtime-c-api/src/module.rs b/lib/runtime-c-api/src/module.rs index 73ec64c4b..17becfc92 100644 --- a/lib/runtime-c-api/src/module.rs +++ b/lib/runtime-c-api/src/module.rs @@ -2,16 +2,17 @@ use crate::{ error::{update_last_error, CApiError}, - export::wasmer_import_export_kind, + //export::wasmer_import_export_kind, import::{wasmer_import_object_t, wasmer_import_t}, instance::wasmer_instance_t, - wasmer_byte_array, wasmer_result_t, + wasmer_byte_array, + wasmer_result_t, }; use libc::c_int; -use std::{collections::HashMap, slice}; -use wasmer::compiler::{compile, default_compiler}; -use wasmer::{Export, Global, ImportObject, Namespace, Table}; -use wasmer::{Instance, Memory, Module}; +use std::slice; +//use wasmer::compiler::{compile, default_compiler}; +//use wasmer::{Exports, Global, ImportObject, Module, Exports, Table}; +use wasmer::Module; //use wasmer_runtime_core::{cache::Artifact, load_cache_with}; #[repr(C)] @@ -33,8 +34,11 @@ pub unsafe extern "C" fn wasmer_compile( wasm_bytes: *mut u8, wasm_bytes_len: u32, ) -> wasmer_result_t { + unimplemented!("Blocked on global store"); + /* let bytes: &[u8] = slice::from_raw_parts_mut(wasm_bytes, wasm_bytes_len as usize); - let result = compile(bytes); + let store = todo!("implement global store"); + let result = Module::compile(store, bytes); let new_module = match result { Ok(instance) => instance, Err(error) => { @@ -44,6 +48,7 @@ pub unsafe extern "C" fn wasmer_compile( }; *module = Box::into_raw(Box::new(new_module)) as *mut wasmer_module_t; wasmer_result_t::WASMER_OK + */ } /// Validates a sequence of bytes hoping it represents a valid WebAssembly module. @@ -68,7 +73,8 @@ pub unsafe extern "C" fn wasmer_validate(wasm_bytes: *const u8, wasm_bytes_len: let bytes: &[u8] = slice::from_raw_parts(wasm_bytes, wasm_bytes_len as usize); - wasmer_runtime_core::validate(bytes) + let store = todo!("implement global store"); + //Module::validate(store, bytes).is_ok() } /// Creates a new Instance from the given module and imports. @@ -85,6 +91,7 @@ pub unsafe extern "C" fn wasmer_module_instantiate( imports: *mut wasmer_import_t, imports_len: c_int, ) -> wasmer_result_t { + /* let imports: &[wasmer_import_t] = slice::from_raw_parts(imports, imports_len as usize); let mut import_object = ImportObject::new(); let mut namespaces = HashMap::new(); @@ -114,8 +121,12 @@ pub unsafe extern "C" fn wasmer_module_instantiate( return wasmer_result_t::WASMER_ERROR; }; - let namespace = namespaces.entry(module_name).or_insert_with(Namespace::new); + let namespace = namespaces.entry(module_name).or_insert_with(Exports::new); + */ + todo!("figure out how to instantiate") + + /* let export = match import.tag { wasmer_import_export_kind::WASM_MEMORY => { let mem = import.value.memory as *mut Memory; @@ -151,6 +162,7 @@ pub unsafe extern "C" fn wasmer_module_instantiate( *instance = Box::into_raw(Box::new(new_instance)) as *mut wasmer_instance_t; wasmer_result_t::WASMER_OK + */ } /// Given: @@ -164,6 +176,8 @@ pub unsafe extern "C" fn wasmer_module_import_instantiate( module: *const wasmer_module_t, import_object: *const wasmer_import_object_t, ) -> wasmer_result_t { + todo!("Figure out how to instanitate") + /* let import_object: &ImportObject = &*(import_object as *const ImportObject); let module: &Module = &*(module as *const Module); @@ -177,6 +191,7 @@ pub unsafe extern "C" fn wasmer_module_import_instantiate( *instance = Box::into_raw(Box::new(new_instance)) as *mut wasmer_instance_t; return wasmer_result_t::WASMER_OK; + */ } /// Serialize the given Module. diff --git a/lib/runtime-c-api/src/table.rs b/lib/runtime-c-api/src/table.rs index 08f671675..cb4495156 100644 --- a/lib/runtime-c-api/src/table.rs +++ b/lib/runtime-c-api/src/table.rs @@ -1,8 +1,7 @@ //! Create, grow, destroy tables of an instance. use crate::{error::update_last_error, wasmer_limits_t, wasmer_result_t}; -use wasmer::types::{ElementType, TableDescriptor}; -use wasmer::wasm::Table; +use wasmer::{Table, Val, ValType}; #[repr(C)] #[derive(Clone)] @@ -22,13 +21,14 @@ 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 { + unimplemented!("wasmer_table_new needs a global store") + /*let max = if limits.max.has_some { Some(limits.max.some) } else { None }; - let desc = TableDescriptor { - element: ElementType::Anyfunc, + let desc = TableType { + ty: Type::FuncRef, minimum: limits.min, maximum: max, }; @@ -42,6 +42,7 @@ pub unsafe extern "C" fn wasmer_table_new( }; *table = Box::into_raw(Box::new(new_table)) as *mut wasmer_table_t; wasmer_result_t::WASMER_OK + */ } /// Grows a Table by the given number of elements. @@ -54,7 +55,18 @@ pub unsafe extern "C" fn wasmer_table_new( #[no_mangle] pub extern "C" fn wasmer_table_grow(table: *mut wasmer_table_t, delta: u32) -> wasmer_result_t { let table = unsafe { &*(table as *mut Table) }; - let delta_result = table.grow(delta); + let table_type = table.ty().ty; + // TODO: this logic should be in wasmer itself + let table_default_value = match table_type { + ValType::I32 => Val::I32(0), + ValType::I64 => Val::I64(0), + ValType::F32 => Val::F32(0.), + ValType::F64 => Val::F64(0.), + ValType::V128 => Val::V128(0), + ValType::AnyRef => todo!("Figure out what the default AnyRef value is"), + ValType::FuncRef => todo!("Figure out what the default FuncRef value is"), + }; + let delta_result = table.grow(delta, table_default_value); match delta_result { Ok(_) => wasmer_result_t::WASMER_OK, Err(grow_error) => { diff --git a/lib/runtime-c-api/src/value.rs b/lib/runtime-c-api/src/value.rs index f540109d8..6a71f281c 100644 --- a/lib/runtime-c-api/src/value.rs +++ b/lib/runtime-c-api/src/value.rs @@ -1,6 +1,6 @@ //! Create and map Rust to WebAssembly values. -use wasmer::wasm::Value; +use wasmer::Val; use wasmer::ValType; /// Represents all possibles WebAssembly value types. @@ -69,7 +69,7 @@ pub struct wasmer_value_t { pub value: wasmer_value, } -impl From for Value { +impl From for Val { fn from(v: wasmer_value_t) -> Self { unsafe { #[allow(unreachable_patterns, non_snake_case)] @@ -77,45 +77,47 @@ impl From for Value { wasmer_value_t { tag: wasmer_value_tag::WASM_I32, value: wasmer_value { I32 }, - } => Value::I32(I32), + } => Val::I32(I32), wasmer_value_t { tag: wasmer_value_tag::WASM_I64, value: wasmer_value { I64 }, - } => Value::I64(I64), + } => Val::I64(I64), wasmer_value_t { tag: wasmer_value_tag::WASM_F32, value: wasmer_value { F32 }, - } => Value::F32(F32), + } => Val::F32(F32), wasmer_value_t { tag: wasmer_value_tag::WASM_F64, value: wasmer_value { F64 }, - } => Value::F64(F64), + } => Val::F64(F64), _ => unreachable!("unknown WASM type"), } } } } -impl From for wasmer_value_t { - fn from(val: Value) -> Self { +impl From for wasmer_value_t { + fn from(val: Val) -> Self { match val { - Value::I32(x) => wasmer_value_t { + Val::I32(x) => wasmer_value_t { tag: wasmer_value_tag::WASM_I32, value: wasmer_value { I32: x }, }, - Value::I64(x) => wasmer_value_t { + Val::I64(x) => wasmer_value_t { tag: wasmer_value_tag::WASM_I64, value: wasmer_value { I64: x }, }, - Value::F32(x) => wasmer_value_t { + Val::F32(x) => wasmer_value_t { tag: wasmer_value_tag::WASM_F32, value: wasmer_value { F32: x }, }, - Value::F64(x) => wasmer_value_t { + Val::F64(x) => wasmer_value_t { tag: wasmer_value_tag::WASM_F64, value: wasmer_value { F64: x }, }, - Value::V128(_) => unimplemented!("V128 not supported in C API"), + Val::V128(_) => unimplemented!("V128 not supported in C API"), + Val::AnyRef(_) => unimplemented!("AnyRef not supported in C API"), + Val::FuncRef(_) => unimplemented!("AnyFunc not supported in C API"), } } } diff --git a/lib/runtime-c-api/wasmer.h b/lib/runtime-c-api/wasmer.h index 975f4840a..936251bc4 100644 --- a/lib/runtime-c-api/wasmer.h +++ b/lib/runtime-c-api/wasmer.h @@ -236,7 +236,7 @@ typedef struct { } wasmer_value_t; /** - * Opaque pointer to `NamedExport`. + * Opaque pointer to `ImportType`. */ typedef struct { diff --git a/lib/runtime-c-api/wasmer.hh b/lib/runtime-c-api/wasmer.hh index cc799d2ae..67f5417b9 100644 --- a/lib/runtime-c-api/wasmer.hh +++ b/lib/runtime-c-api/wasmer.hh @@ -180,7 +180,7 @@ struct wasmer_value_t { wasmer_value value; }; -/// Opaque pointer to `NamedExport`. +/// Opaque pointer to `ImportType`. struct wasmer_export_t { }; diff --git a/lib/wasm-common/src/types.rs b/lib/wasm-common/src/types.rs index 4c3529b54..178cd63c3 100644 --- a/lib/wasm-common/src/types.rs +++ b/lib/wasm-common/src/types.rs @@ -210,6 +210,8 @@ impl ExternType { } } +// TODO: `shrink_to_fit` these or change it to `Box<[Type]>` if not using +// Cow or something else /// The signature of a function that is either implemented /// in a Wasm module or exposed to Wasm by the host. /// @@ -332,10 +334,7 @@ impl GlobalType { /// let global = GlobalType::new(Type::I64, Mutability::Var); /// ``` pub fn new(ty: Type, mutability: Mutability) -> Self { - Self { - ty: ty, - mutability: mutability, - } + Self { ty, mutability } } }