Update C API to use new API

This commit is contained in:
Mark McCaskey
2020-04-06 17:09:50 -07:00
parent 99f8c949d1
commit 83f0a043e7
12 changed files with 59 additions and 32 deletions

2
Cargo.lock generated
View File

@ -2941,8 +2941,8 @@ version = "0.16.2"
dependencies = [
"cbindgen",
"libc",
"wasmer",
"wasmer-emscripten",
"wasmer-runtime",
"wasmer-runtime-core",
"wasmer-wasi",
]

View File

@ -66,15 +66,20 @@ pub mod wasm {
//!
//! # Tables
pub use wasmer_runtime_core::global::Global;
pub use wasmer_runtime_core::instance::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::{
FuncSig, GlobalDescriptor, MemoryDescriptor, TableDescriptor, Type, Value,
};
pub use wasmer_runtime_core::vm::Ctx;
}
pub mod import {
//! Types and functions for Wasm imports.
pub use wasmer_runtime_core::import::{ImportObject, ImportObjectIterator, Namespace};
pub use wasmer_runtime_core::types::{ExternDescriptor, ImportDescriptor};
pub use wasmer_runtime_core::{func, imports};
}
@ -217,3 +222,23 @@ impl CompiledModule for Module {
Ok(())
}
}
// Below this line is things copied from `wasmer-runtime` to make the C API work.
// All these additions should be reviewed carefully before shipping.
/// Compile WebAssembly binary code into a [`Module`].
/// This function is useful if it is necessary to
/// compile a module before it can be instantiated
/// (otherwise, the [`instantiate`] function should be used).
///
/// [`Module`]: struct.Module.html
/// [`instantiate`]: fn.instantiate.html
///
/// # Params:
/// * `wasm`: A `&[u8]` containing the
/// binary code of the wasm module you want to compile.
/// # Errors:
/// If the operation fails, the function returns `Err(error::CompileError::...)`.
pub fn compile(wasm: &[u8]) -> error::CompileResult<Module> {
wasmer_runtime_core::compile_with(&wasm[..], &default_compiler())
}

View File

@ -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"]

View File

@ -13,7 +13,8 @@ use crate::{
};
use libc::{c_int, c_uint};
use std::{ptr, slice};
use wasmer_runtime::{Instance, Module, Value};
use wasmer::wasm::Value;
use wasmer::{Instance, Module};
use wasmer_runtime_core::{export::Export, module::ExportIndex};
/// Intermediate representation of an `Export` instance that is

View File

@ -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)]

View File

@ -17,12 +17,11 @@ use std::{
ptr, slice,
sync::Arc,
};
use wasmer_runtime::{Ctx, Global, Memory, Module, Table};
use wasmer::import::{ImportObject, ImportObjectIterator};
use wasmer::wasm::{Ctx, FuncSig, Global, Memory, Module, Table, Type};
use wasmer_runtime_core::{
export::{Context, Export, FuncPointer},
import::{ImportObject, ImportObjectIterator},
module::ImportName,
types::{FuncSig, Type},
};
#[repr(C)]

View File

@ -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::wasm::{Ctx, Global, Instance, Memory, Table, Value};
use wasmer_runtime_core::export::Export;
/// 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::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) => {

View File

@ -90,8 +90,6 @@
unused_unsafe,
unreachable_patterns
)]
extern crate wasmer_runtime;
extern crate wasmer_runtime_core;
pub mod error;
pub mod export;

View File

@ -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.
///

View File

@ -9,9 +9,9 @@ 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::import::ImportObject;
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};
#[repr(C)]

View File

@ -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)]

View File

@ -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,