mirror of
https://github.com/fluencelabs/wasmer
synced 2025-04-25 02:12:13 +00:00
Merge #1358
1358: Update C API to use new API r=MarkMcCaskey a=MarkMcCaskey # Review - [ ] Add a short description of the the change to the CHANGELOG.md file Co-authored-by: Mark McCaskey <mark@wasmer.io> Co-authored-by: Mark McCaskey <5770194+MarkMcCaskey@users.noreply.github.com>
This commit is contained in:
commit
ab106af422
2
Cargo.lock
generated
2
Cargo.lock
generated
@ -2934,8 +2934,8 @@ version = "0.16.2"
|
||||
dependencies = [
|
||||
"cbindgen",
|
||||
"libc",
|
||||
"wasmer",
|
||||
"wasmer-emscripten",
|
||||
"wasmer-runtime",
|
||||
"wasmer-runtime-core",
|
||||
"wasmer-wasi",
|
||||
]
|
||||
|
@ -74,6 +74,7 @@ pub mod wasm {
|
||||
pub use wasmer_runtime_core::global::Global;
|
||||
pub use wasmer_runtime_core::instance::{DynFunc, Instance};
|
||||
pub use wasmer_runtime_core::memory::Memory;
|
||||
pub use wasmer_runtime_core::module::Module;
|
||||
pub use wasmer_runtime_core::table::Table;
|
||||
pub use wasmer_runtime_core::types::{ExportDescriptor, ExternDescriptor, ImportDescriptor};
|
||||
pub use wasmer_runtime_core::types::{
|
||||
@ -281,13 +282,16 @@ pub mod codegen {
|
||||
// TODO: `import` or `imports`?
|
||||
pub mod import {
|
||||
//! Types and functions for Wasm imports.
|
||||
pub use wasmer_runtime_core::import::{ImportObject, LikeNamespace, Namespace};
|
||||
pub use wasmer_runtime_core::import::{
|
||||
ImportObject, ImportObjectIterator, LikeNamespace, Namespace,
|
||||
};
|
||||
pub use wasmer_runtime_core::types::{ExternDescriptor, ImportDescriptor};
|
||||
pub use wasmer_runtime_core::{func, imports};
|
||||
}
|
||||
|
||||
pub mod export {
|
||||
//! Types and functions for Wasm exports.
|
||||
pub use wasmer_runtime_core::export::Export;
|
||||
pub use wasmer_runtime_core::types::{ExportDescriptor, ExternDescriptor};
|
||||
}
|
||||
|
||||
|
@ -17,9 +17,9 @@ crate-type = ["cdylib", "rlib", "staticlib"]
|
||||
[dependencies]
|
||||
libc = "0.2.60"
|
||||
|
||||
[dependencies.wasmer-runtime]
|
||||
[dependencies.wasmer]
|
||||
default-features = false
|
||||
path = "../runtime"
|
||||
path = "../api"
|
||||
version = "0.16.2"
|
||||
|
||||
[dependencies.wasmer-runtime-core]
|
||||
@ -40,9 +40,9 @@ optional = true
|
||||
|
||||
[features]
|
||||
default = ["cranelift-backend", "wasi"]
|
||||
singlepass-backend = ["wasmer-runtime/singlepass", "wasmer-runtime/default-backend-singlepass"]
|
||||
cranelift-backend = ["wasmer-runtime/cranelift", "wasmer-runtime/default-backend-cranelift"]
|
||||
llvm-backend = ["wasmer-runtime/llvm", "wasmer-runtime/default-backend-llvm"]
|
||||
singlepass-backend = ["wasmer/singlepass", "wasmer/default-backend-singlepass"]
|
||||
cranelift-backend = ["wasmer/cranelift", "wasmer/default-backend-cranelift"]
|
||||
llvm-backend = ["wasmer/llvm", "wasmer/default-backend-llvm"]
|
||||
wasi = ["wasmer-wasi"]
|
||||
emscripten = ["wasmer-emscripten"]
|
||||
|
||||
|
@ -13,8 +13,9 @@ use crate::{
|
||||
};
|
||||
use libc::{c_int, c_uint};
|
||||
use std::{ptr, slice};
|
||||
use wasmer_runtime::{Instance, Module, Value};
|
||||
use wasmer_runtime_core::{export::Export, module::ExportIndex};
|
||||
use wasmer::export::{ExportDescriptor, ExternDescriptor};
|
||||
use wasmer::wasm::{Memory, Value};
|
||||
use wasmer::{Instance, Module};
|
||||
|
||||
/// Intermediate representation of an `Export` instance that is
|
||||
/// exposed to C.
|
||||
@ -23,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,
|
||||
@ -145,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;
|
||||
@ -268,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,
|
||||
}
|
||||
}
|
||||
|
||||
@ -289,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 {
|
||||
@ -315,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() {
|
||||
@ -345,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() {
|
||||
@ -374,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 {
|
||||
@ -408,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
|
||||
@ -517,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,
|
||||
}
|
||||
}
|
||||
|
@ -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_runtime::Global;
|
||||
use wasmer::wasm::Global;
|
||||
|
||||
#[repr(C)]
|
||||
#[derive(Clone)]
|
||||
|
@ -4,8 +4,8 @@ use super::*;
|
||||
use crate::{get_slice_checked, instance::wasmer_instance_t, module::wasmer_module_t};
|
||||
|
||||
use std::ptr;
|
||||
use wasmer::wasm::{Instance, Module};
|
||||
use wasmer_emscripten::{EmscriptenData, EmscriptenGlobals};
|
||||
use wasmer_runtime::{Instance, Module};
|
||||
|
||||
/// Type used to construct an import_object_t with Emscripten imports.
|
||||
#[repr(C)]
|
||||
|
@ -17,12 +17,12 @@ use std::{
|
||||
ptr, slice,
|
||||
sync::Arc,
|
||||
};
|
||||
use wasmer_runtime::{Ctx, Global, Memory, Module, Table};
|
||||
use wasmer::import::{ImportObject, ImportObjectIterator};
|
||||
use wasmer::vm::Ctx;
|
||||
use wasmer::wasm::{Export, FuncSig, Global, Memory, Module, Table, Type};
|
||||
use wasmer_runtime_core::{
|
||||
export::{Context, Export, FuncPointer},
|
||||
import::{ImportObject, ImportObjectIterator},
|
||||
export::{Context, FuncPointer},
|
||||
module::ImportName,
|
||||
types::{FuncSig, Type},
|
||||
};
|
||||
|
||||
#[repr(C)]
|
||||
|
@ -10,11 +10,9 @@ use crate::{
|
||||
};
|
||||
use libc::{c_char, c_int, c_void};
|
||||
use std::{collections::HashMap, ffi::CStr, ptr, slice};
|
||||
use wasmer_runtime::{Ctx, Global, Instance, Memory, Table, Value};
|
||||
use wasmer_runtime_core::{
|
||||
export::Export,
|
||||
import::{ImportObject, Namespace},
|
||||
};
|
||||
use wasmer::import::{ImportObject, Namespace};
|
||||
use wasmer::vm::Ctx;
|
||||
use wasmer::wasm::{Export, Global, Instance, Memory, Table, Value};
|
||||
|
||||
/// Opaque pointer to a `wasmer_runtime::Instance` value in Rust.
|
||||
///
|
||||
@ -166,7 +164,15 @@ pub unsafe extern "C" fn wasmer_instantiate(
|
||||
}
|
||||
|
||||
let bytes: &[u8] = slice::from_raw_parts_mut(wasm_bytes, wasm_bytes_len as usize);
|
||||
let result = wasmer_runtime::instantiate(bytes, &import_object);
|
||||
let module_result = wasmer::compiler::compile(bytes);
|
||||
let module = match module_result {
|
||||
Ok(module) => module,
|
||||
Err(error) => {
|
||||
update_last_error(error);
|
||||
return wasmer_result_t::WASMER_ERROR;
|
||||
}
|
||||
};
|
||||
let result = module.instantiate(&import_object);
|
||||
let new_instance = match result {
|
||||
Ok(instance) => instance,
|
||||
Err(error) => {
|
||||
@ -387,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,
|
||||
});
|
||||
}
|
||||
|
@ -90,8 +90,6 @@
|
||||
unused_unsafe,
|
||||
unreachable_patterns
|
||||
)]
|
||||
extern crate wasmer_runtime;
|
||||
extern crate wasmer_runtime_core;
|
||||
|
||||
pub mod error;
|
||||
pub mod export;
|
||||
|
@ -5,11 +5,9 @@ use crate::{
|
||||
wasmer_limits_t, wasmer_result_t,
|
||||
};
|
||||
use std::{cell::Cell, ptr};
|
||||
use wasmer_runtime::Memory;
|
||||
use wasmer_runtime_core::{
|
||||
types::MemoryDescriptor,
|
||||
units::{Bytes, Pages},
|
||||
};
|
||||
use wasmer::types::MemoryDescriptor;
|
||||
use wasmer::units::{Bytes, Pages};
|
||||
use wasmer::wasm::Memory;
|
||||
|
||||
/// Opaque pointer to a `wasmer_runtime::Memory` value in Rust.
|
||||
///
|
||||
|
@ -9,10 +9,11 @@ use crate::{
|
||||
};
|
||||
use libc::c_int;
|
||||
use std::{collections::HashMap, slice};
|
||||
use wasmer_runtime::{
|
||||
compile, default_compiler, Global, ImportObject, Instance, Memory, Module, Table,
|
||||
};
|
||||
use wasmer_runtime_core::{cache::Artifact, export::Export, import::Namespace, load_cache_with};
|
||||
use wasmer::compiler::{compile, default_compiler};
|
||||
use wasmer::import::{ImportObject, Namespace};
|
||||
use wasmer::wasm::{Export, Global, Table};
|
||||
use wasmer::{Instance, Memory, Module};
|
||||
use wasmer_runtime_core::{cache::Artifact, load_cache_with};
|
||||
|
||||
#[repr(C)]
|
||||
pub struct wasmer_module_t;
|
||||
|
@ -1,8 +1,8 @@
|
||||
//! Create, grow, destroy tables of an instance.
|
||||
|
||||
use crate::{error::update_last_error, wasmer_limits_t, wasmer_result_t};
|
||||
use wasmer_runtime::Table;
|
||||
use wasmer_runtime_core::types::{ElementType, TableDescriptor};
|
||||
use wasmer::types::{ElementType, TableDescriptor};
|
||||
use wasmer::wasm::Table;
|
||||
|
||||
#[repr(C)]
|
||||
#[derive(Clone)]
|
||||
|
@ -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),
|
||||
|
@ -1,7 +1,7 @@
|
||||
//! Create and map Rust to WebAssembly values.
|
||||
|
||||
use wasmer_runtime::Value;
|
||||
use wasmer_runtime_core::types::Type;
|
||||
use wasmer::types::Type;
|
||||
use wasmer::wasm::Value;
|
||||
|
||||
/// Represents all possibles WebAssembly value types.
|
||||
///
|
||||
@ -146,7 +146,7 @@ impl From<wasmer_value_tag> for Type {
|
||||
}
|
||||
}
|
||||
|
||||
impl From<&wasmer_runtime::wasm::Type> for wasmer_value_tag {
|
||||
impl From<&wasmer::wasm::Type> for wasmer_value_tag {
|
||||
fn from(ty: &Type) -> Self {
|
||||
match *ty {
|
||||
Type::I32 => wasmer_value_tag::WASM_I32,
|
||||
|
@ -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)]
|
||||
|
Loading…
x
Reference in New Issue
Block a user