mirror of
https://github.com/fluencelabs/wasmer
synced 2025-06-29 08:31:32 +00:00
Remove more uses of wasmer_runtime_core from the C API
This commit is contained in:
@ -13,9 +13,9 @@ use crate::{
|
||||
};
|
||||
use libc::{c_int, c_uint};
|
||||
use std::{ptr, slice};
|
||||
use wasmer::wasm::Value;
|
||||
use wasmer::export::{ExportDescriptor, ExternDescriptor};
|
||||
use wasmer::wasm::{Memory, Value};
|
||||
use wasmer::{Instance, Module};
|
||||
use wasmer_runtime_core::{export::Export, module::ExportIndex};
|
||||
|
||||
/// Intermediate representation of an `Export` instance that is
|
||||
/// exposed to C.
|
||||
@ -24,7 +24,7 @@ pub(crate) struct NamedExport {
|
||||
pub(crate) name: String,
|
||||
|
||||
/// The export instance.
|
||||
pub(crate) export: Export,
|
||||
pub(crate) extern_descriptor: ExternDescriptor,
|
||||
|
||||
/// The instance that holds the export.
|
||||
pub(crate) instance: *mut Instance,
|
||||
@ -146,7 +146,7 @@ pub unsafe extern "C" fn wasmer_export_descriptors(
|
||||
let module = &*(module as *const Module);
|
||||
|
||||
let named_export_descriptors: Box<NamedExportDescriptors> = Box::new(NamedExportDescriptors(
|
||||
module.info().exports.iter().map(|e| e.into()).collect(),
|
||||
module.exports().into_iter().map(|e| e.into()).collect(),
|
||||
));
|
||||
*export_descriptors =
|
||||
Box::into_raw(named_export_descriptors) as *mut wasmer_export_descriptors_t;
|
||||
@ -269,11 +269,11 @@ 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.export {
|
||||
Export::Table(_) => wasmer_import_export_kind::WASM_TABLE,
|
||||
Export::Function { .. } => wasmer_import_export_kind::WASM_FUNCTION,
|
||||
Export::Global(_) => wasmer_import_export_kind::WASM_GLOBAL,
|
||||
Export::Memory(_) => wasmer_import_export_kind::WASM_MEMORY,
|
||||
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,
|
||||
}
|
||||
}
|
||||
|
||||
@ -290,8 +290,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.export;
|
||||
if let Export::Function { ref signature, .. } = *export {
|
||||
let export = &named_export.extern_descriptor;
|
||||
if let ExternDescriptor::Function(ref signature) = *export {
|
||||
*result = signature.params().len() as u32;
|
||||
wasmer_result_t::WASMER_OK
|
||||
} else {
|
||||
@ -316,8 +316,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.export;
|
||||
if let Export::Function { ref signature, .. } = *export {
|
||||
let export = &named_export.extern_descriptor;
|
||||
if let ExternDescriptor::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,8 +346,8 @@ 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.export;
|
||||
if let Export::Function { ref signature, .. } = *export {
|
||||
let export = &named_export.extern_descriptor;
|
||||
if let ExternDescriptor::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() {
|
||||
@ -375,8 +375,8 @@ 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.export;
|
||||
if let Export::Function { ref signature, .. } = *export {
|
||||
let export = &named_export.extern_descriptor;
|
||||
if let ExternDescriptor::Function(ref signature) = *export {
|
||||
*result = signature.returns().len() as u32;
|
||||
wasmer_result_t::WASMER_OK
|
||||
} else {
|
||||
@ -409,9 +409,9 @@ pub unsafe extern "C" fn wasmer_export_to_memory(
|
||||
memory: *mut *mut wasmer_memory_t,
|
||||
) -> wasmer_result_t {
|
||||
let named_export = &*(export as *const NamedExport);
|
||||
let export = &named_export.export;
|
||||
let instance = &*named_export.instance;
|
||||
|
||||
if let Export::Memory(exported_memory) = export {
|
||||
if let Ok(exported_memory) = instance.exports.get::<Memory>(&named_export.name) {
|
||||
let mem = Box::new(exported_memory.clone());
|
||||
*memory = Box::into_raw(mem) as *mut wasmer_memory_t;
|
||||
wasmer_result_t::WASMER_OK
|
||||
@ -518,16 +518,16 @@ pub unsafe extern "C" fn wasmer_export_func_call(
|
||||
}
|
||||
}
|
||||
|
||||
impl From<(&std::string::String, &ExportIndex)> for NamedExportDescriptor {
|
||||
fn from((name, export_index): (&String, &ExportIndex)) -> Self {
|
||||
let kind = match *export_index {
|
||||
ExportIndex::Memory(_) => wasmer_import_export_kind::WASM_MEMORY,
|
||||
ExportIndex::Global(_) => wasmer_import_export_kind::WASM_GLOBAL,
|
||||
ExportIndex::Table(_) => wasmer_import_export_kind::WASM_TABLE,
|
||||
ExportIndex::Func(_) => wasmer_import_export_kind::WASM_FUNCTION,
|
||||
impl<'a> From<ExportDescriptor<'a>> 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,
|
||||
};
|
||||
NamedExportDescriptor {
|
||||
name: name.clone(),
|
||||
name: ed.name.to_string(),
|
||||
kind,
|
||||
}
|
||||
}
|
||||
|
@ -393,10 +393,10 @@ pub unsafe extern "C" fn wasmer_instance_exports(
|
||||
let instance_ref = &mut *(instance as *mut Instance);
|
||||
let mut exports_vec: Vec<NamedExport> = Vec::with_capacity(instance_ref.exports().count());
|
||||
|
||||
for (name, export) in instance_ref.exports() {
|
||||
for export_descriptor in instance_ref.module.exports().into_iter() {
|
||||
exports_vec.push(NamedExport {
|
||||
name: name.clone(),
|
||||
export: export.clone(),
|
||||
name: export_descriptor.name.to_string(),
|
||||
extern_descriptor: export_descriptor.ty,
|
||||
instance: instance as *mut Instance,
|
||||
});
|
||||
}
|
||||
|
@ -9,10 +9,10 @@ use crate::{
|
||||
};
|
||||
use libc::c_int;
|
||||
use std::{collections::HashMap, slice};
|
||||
use wasmer::import::ImportObject;
|
||||
use wasmer::import::{ImportObject, Namespace};
|
||||
use wasmer::wasm::{Global, Table};
|
||||
use wasmer::{compile, default_compiler, Instance, Memory, Module};
|
||||
use wasmer_runtime_core::{cache::Artifact, export::Export, import::Namespace, load_cache_with};
|
||||
use wasmer_runtime_core::{cache::Artifact, export::Export, load_cache_with};
|
||||
|
||||
#[repr(C)]
|
||||
pub struct wasmer_module_t;
|
||||
|
@ -44,7 +44,7 @@ pub unsafe extern "C" fn wasmer_trampoline_buffer_builder_add_callinfo_trampolin
|
||||
ctx: *const c_void,
|
||||
num_params: u32,
|
||||
) -> usize {
|
||||
use wasmer_runtime_core::types::Type;
|
||||
use wasmer::types::Type;
|
||||
let builder = &mut *(builder as *mut TrampolineBufferBuilder);
|
||||
builder.add_callinfo_trampoline(
|
||||
mem::transmute(func),
|
||||
|
@ -171,43 +171,9 @@ impl Module {
|
||||
&self.inner.info
|
||||
}
|
||||
|
||||
/// Iterate over the [`ExportDescriptor`]s of the exports that this module provides.
|
||||
pub(crate) fn exports_iter(&self) -> impl Iterator<Item = ExportDescriptor> + '_ {
|
||||
self.info()
|
||||
.exports
|
||||
.iter()
|
||||
.map(move |(name, &ei)| ExportDescriptor {
|
||||
name,
|
||||
ty: match ei {
|
||||
ExportIndex::Func(f_idx) => {
|
||||
let sig_idx = self.info().func_assoc[f_idx].into();
|
||||
self.info().signatures[sig_idx].clone().into()
|
||||
}
|
||||
ExportIndex::Global(g_idx) => {
|
||||
let info = self.info();
|
||||
let local_global_idx =
|
||||
LocalGlobalIndex::new(g_idx.index() - info.imported_globals.len());
|
||||
info.globals[local_global_idx].desc.into()
|
||||
}
|
||||
ExportIndex::Memory(m_idx) => {
|
||||
let info = self.info();
|
||||
let local_memory_idx =
|
||||
LocalMemoryIndex::new(m_idx.index() - info.imported_memories.len());
|
||||
info.memories[local_memory_idx].into()
|
||||
}
|
||||
ExportIndex::Table(t_idx) => {
|
||||
let info = self.info();
|
||||
let local_table_idx =
|
||||
LocalTableIndex::new(t_idx.index() - info.imported_tables.len());
|
||||
info.tables[local_table_idx].into()
|
||||
}
|
||||
},
|
||||
})
|
||||
}
|
||||
|
||||
/// Get the [`ExportDescriptor`]s of the exports this [`Module`] provides.
|
||||
pub fn exports(&self) -> Vec<ExportDescriptor> {
|
||||
self.exports_iter().collect()
|
||||
self.inner.exports()
|
||||
}
|
||||
|
||||
/// Get the [`ImportDescriptor`]s describing the imports this [`Module`]
|
||||
@ -304,7 +270,46 @@ impl Clone for Module {
|
||||
}
|
||||
}
|
||||
|
||||
impl ModuleInner {}
|
||||
impl ModuleInner {
|
||||
/// Iterate over the [`ExportDescriptor`]s of the exports that this module provides.
|
||||
pub(crate) fn exports_iter(&self) -> impl Iterator<Item = ExportDescriptor> + '_ {
|
||||
self.info
|
||||
.exports
|
||||
.iter()
|
||||
.map(move |(name, &ei)| ExportDescriptor {
|
||||
name,
|
||||
ty: match ei {
|
||||
ExportIndex::Func(f_idx) => {
|
||||
let sig_idx = self.info.func_assoc[f_idx].into();
|
||||
self.info.signatures[sig_idx].clone().into()
|
||||
}
|
||||
ExportIndex::Global(g_idx) => {
|
||||
let info = &self.info;
|
||||
let local_global_idx =
|
||||
LocalGlobalIndex::new(g_idx.index() - info.imported_globals.len());
|
||||
info.globals[local_global_idx].desc.into()
|
||||
}
|
||||
ExportIndex::Memory(m_idx) => {
|
||||
let info = &self.info;
|
||||
let local_memory_idx =
|
||||
LocalMemoryIndex::new(m_idx.index() - info.imported_memories.len());
|
||||
info.memories[local_memory_idx].into()
|
||||
}
|
||||
ExportIndex::Table(t_idx) => {
|
||||
let info = &self.info;
|
||||
let local_table_idx =
|
||||
LocalTableIndex::new(t_idx.index() - info.imported_tables.len());
|
||||
info.tables[local_table_idx].into()
|
||||
}
|
||||
},
|
||||
})
|
||||
}
|
||||
|
||||
/// Get the [`ExportDescriptor`]s of the exports this [`Module`] provides.
|
||||
pub fn exports(&self) -> Vec<ExportDescriptor> {
|
||||
self.exports_iter().collect()
|
||||
}
|
||||
}
|
||||
|
||||
#[doc(hidden)]
|
||||
#[derive(Serialize, Deserialize, Debug, Clone)]
|
||||
|
Reference in New Issue
Block a user