Skip to content

Commit

Permalink
Merge #1358
Browse files Browse the repository at this point in the history
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 <[email protected]>
Co-authored-by: Mark McCaskey <[email protected]>
  • Loading branch information
3 people authored Apr 21, 2020
2 parents d746135 + 4f23ed7 commit ab106af
Show file tree
Hide file tree
Showing 15 changed files with 115 additions and 102 deletions.
2 changes: 1 addition & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 5 additions & 1 deletion lib/api/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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::{
Expand Down Expand Up @@ -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};
}

Expand Down
10 changes: 5 additions & 5 deletions lib/runtime-c-api/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand All @@ -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"]

Expand Down
55 changes: 28 additions & 27 deletions lib/runtime-c-api/src/export.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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,
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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,
}
}

Expand All @@ -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 {
Expand All @@ -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() {
Expand Down Expand Up @@ -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() {
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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,
}
}
Expand Down
2 changes: 1 addition & 1 deletion lib/runtime-c-api/src/global.rs
Original file line number Diff line number Diff line change
@@ -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)]
Expand Down
2 changes: 1 addition & 1 deletion lib/runtime-c-api/src/import/emscripten.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)]
Expand Down
8 changes: 4 additions & 4 deletions lib/runtime-c-api/src/import/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)]
Expand Down
24 changes: 15 additions & 9 deletions lib/runtime-c-api/src/instance.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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.
///
Expand Down Expand Up @@ -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) => {
Expand Down Expand Up @@ -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,
});
}
Expand Down
2 changes: 0 additions & 2 deletions lib/runtime-c-api/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -90,8 +90,6 @@
unused_unsafe,
unreachable_patterns
)]
extern crate wasmer_runtime;
extern crate wasmer_runtime_core;

pub mod error;
pub mod export;
Expand Down
8 changes: 3 additions & 5 deletions lib/runtime-c-api/src/memory.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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.
///
Expand Down
9 changes: 5 additions & 4 deletions lib/runtime-c-api/src/module.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
4 changes: 2 additions & 2 deletions lib/runtime-c-api/src/table.rs
Original file line number Diff line number Diff line change
@@ -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)]
Expand Down
2 changes: 1 addition & 1 deletion lib/runtime-c-api/src/trampoline.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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),
Expand Down
6 changes: 3 additions & 3 deletions lib/runtime-c-api/src/value.rs
Original file line number Diff line number Diff line change
@@ -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.
///
Expand Down Expand Up @@ -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,
Expand Down
Loading

0 comments on commit ab106af

Please sign in to comment.