feat(c-api) Reorganize the crate to put the Wasmer C API as deprecated code.

This commit is contained in:
Ivan Enderlin
2020-09-22 09:35:01 +02:00
parent a2e744aba6
commit 7cafae2b96
14 changed files with 219 additions and 218 deletions

View File

@@ -1,8 +1,7 @@
//! Create, read, destroy export definitions (function, global, memory
//! and table) on an instance.
use crate::{
error::{update_last_error, CApiError},
use crate::deprecated::{
global::wasmer_global_t,
import::wasmer_import_func_t,
instance::CAPIInstance,
@@ -12,6 +11,7 @@ use crate::{
value::{wasmer_value, wasmer_value_t, wasmer_value_tag},
wasmer_byte_array, wasmer_result_t,
};
use crate::error::{update_last_error, CApiError};
use libc::{c_int, c_uint};
use std::ptr::{self, NonNull};
use std::slice;
@@ -41,9 +41,9 @@ pub struct wasmer_export_func_t;
/// exposed to C.
pub(crate) struct NamedExports(pub Vec<NamedExport>);
/// Opaque pointer to the opaque structure `crate::NamedExports`,
/// which is a wrapper around a vector of the opaque structure
/// `crate::NamedExport`.
/// Opaque pointer to the opaque structure
/// `crate::deprecated::NamedExports`, which is a wrapper around a
/// vector of the opaque structure `crate::deprecated::NamedExport`.
///
/// Check the `wasmer_instance_exports()` function to learn more.
#[repr(C)]

View File

@@ -1,7 +1,10 @@
//! Create, set, get and destroy global variables of an instance.
use crate::deprecated::{
get_global_store,
value::{wasmer_value_t, wasmer_value_tag},
};
use crate::error::update_last_error;
use crate::value::{wasmer_value_t, wasmer_value_tag};
use std::ptr::NonNull;
use wasmer::Global;
@@ -20,7 +23,7 @@ pub struct wasmer_global_t;
/// The caller owns the object and should call `wasmer_global_destroy` to free it.
#[no_mangle]
pub extern "C" fn wasmer_global_new(value: wasmer_value_t, mutable: bool) -> *mut wasmer_global_t {
let store = crate::get_global_store();
let store = get_global_store();
let global = if mutable {
Global::new_mut(store, value.into())
} else {

View File

@@ -1,7 +1,7 @@
//! Functions and types for dealing with Emscripten imports
use super::*;
use crate::{
use crate::deprecated::{
get_slice_checked,
instance::{wasmer_instance_t, CAPIInstance},
module::wasmer_module_t,

View File

@@ -1,14 +1,15 @@
//! Create, read, destroy import definitions (function, global, memory
//! and table) on an instance.
use crate::{
error::{update_last_error, CApiError},
use crate::deprecated::{
export::{wasmer_import_export_kind, wasmer_import_export_value},
get_global_store,
instance::{wasmer_instance_context_t, CAPIInstance},
module::wasmer_module_t,
value::wasmer_value_tag,
wasmer_byte_array, wasmer_result_t,
};
use crate::error::{update_last_error, CApiError};
use libc::c_uint;
use std::ffi::{c_void, CStr};
use std::os::raw::c_char;
@@ -687,7 +688,7 @@ pub unsafe extern "C" fn wasmer_import_func_new(
let returns: Vec<ValType> = returns.iter().cloned().map(|x| x.into()).collect();
let func_type = FunctionType::new(params, &returns[..]);
let store = crate::get_global_store();
let store = get_global_store();
let env_ptr = Box::into_raw(Box::new(LegacyEnv::default()));

View File

@@ -1,5 +1,5 @@
use super::*;
use crate::get_slice_checked;
use crate::deprecated::{get_global_store, get_slice_checked};
use libc::c_uchar;
use std::path::PathBuf;
use std::ptr;
@@ -174,7 +174,7 @@ fn wasmer_wasi_generate_import_object_inner(
_ => panic!("Version {:?} is invalid.", version),
};
let store = crate::get_global_store();
let store = get_global_store();
let mut wasi_state_builder = wasi::WasiState::new(
arg_vec
@@ -209,7 +209,7 @@ 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 store = crate::get_global_store();
let store = get_global_store();
let mut wasi_state_builder = wasi::WasiState::new("wasmer-wasi-default-program-name");
let wasi_state = wasi_state_builder.build().unwrap();
let mut wasi_env = wasi::WasiEnv::new(wasi_state);

View File

@@ -1,13 +1,14 @@
//! Instantiate a module, call functions, and read exports.
use crate::{
error::{update_last_error, CApiError},
use crate::deprecated::{
export::{wasmer_exports_t, wasmer_import_export_kind, NamedExport, NamedExports},
get_global_store,
import::{wasmer_import_t, FunctionWrapper},
memory::wasmer_memory_t,
value::{wasmer_value, wasmer_value_t, wasmer_value_tag},
wasmer_result_t,
};
use crate::error::{update_last_error, CApiError};
use libc::{c_char, c_int, c_void};
use std::collections::HashMap;
use std::ffi::CStr;
@@ -191,7 +192,7 @@ pub unsafe extern "C" fn wasmer_instantiate(
}
let bytes: &[u8] = slice::from_raw_parts_mut(wasm_bytes.as_ptr(), wasm_bytes_len as usize);
let store = crate::get_global_store();
let store = get_global_store();
let module_result = Module::from_binary(store, bytes);
let module = match module_result {

View File

@@ -1,9 +1,7 @@
//! Create, read, write, grow, destroy memory of an instance.
use crate::{
error::{update_last_error, CApiError},
wasmer_limits_t, wasmer_result_t,
};
use crate::deprecated::{get_global_store, wasmer_limits_t, wasmer_result_t};
use crate::error::{update_last_error, CApiError};
use std::{cell::Cell, ptr};
use wasmer::{Bytes, Memory, MemoryType, Pages};
@@ -67,7 +65,7 @@ pub unsafe extern "C" fn wasmer_memory_new(
} else {
None
};
let store = crate::get_global_store();
let store = get_global_store();
let desc = MemoryType::new(Pages(limits.min), max, false);
match Memory::new(store, desc) {
Ok(new_memory) => {

View File

@@ -0,0 +1,180 @@
//! # Wasmer Runtime C API
//!
//! Wasmer is a standalone JIT WebAssembly runtime, aiming to be fully
//! compatible with Emscripten, Rust and Go. [Learn
//! more](https://github.com/wasmerio/wasmer).
//!
//! This crate exposes a C and C++ API for the Wasmer runtime.
//!
//! # Usage
//!
//! The C and C++ header files can be found in the source tree of this
//! crate, respectively [`wasmer.h`][wasmer_h] and
//! [`wasmer.hh`][wasmer_hh]. They are automatically generated, and always
//! up-to-date in this repository.
//!
//! Here is a simple example to use the C API:
//!
//! ```c
//! #include <stdio.h>
//! #include "wasmer.h"
//! #include <assert.h>
//! #include <stdint.h>
//!
//! int main()
//! {
//! // Read the Wasm file bytes.
//! FILE *file = fopen("sum.wasm", "r");
//! fseek(file, 0, SEEK_END);
//! long len = ftell(file);
//! uint8_t *bytes = malloc(len);
//! fseek(file, 0, SEEK_SET);
//! fread(bytes, 1, len, file);
//! fclose(file);
//!
//! // Prepare the imports.
//! wasmer_import_t imports[] = {};
//!
//! // Instantiate!
//! wasmer_instance_t *instance = NULL;
//! wasmer_result_t instantiation_result = wasmer_instantiate(&instance, bytes, len, imports, 0);
//!
//! assert(instantiation_result == WASMER_OK);
//!
//! // Let's call a function.
//! // Start by preparing the arguments.
//!
//! // Value of argument #1 is `7i32`.
//! wasmer_value_t argument_one;
//! argument_one.tag = WASM_I32;
//! argument_one.value.I32 = 7;
//!
//! // Value of argument #2 is `8i32`.
//! wasmer_value_t argument_two;
//! argument_two.tag = WASM_I32;
//! argument_two.value.I32 = 8;
//!
//! // Prepare the arguments.
//! wasmer_value_t arguments[] = {argument_one, argument_two};
//!
//! // Prepare the return value.
//! wasmer_value_t result_one;
//! wasmer_value_t results[] = {result_one};
//!
//! // Call the `sum` function with the prepared arguments and the return value.
//! wasmer_result_t call_result = wasmer_instance_call(instance, "sum", arguments, 2, results, 1);
//!
//! // Let's display the result.
//! printf("Call result: %d\n", call_result);
//! printf("Result: %d\n", results[0].value.I32);
//!
//! // `sum(7, 8) == 15`.
//! assert(results[0].value.I32 == 15);
//! assert(call_result == WASMER_OK);
//!
//! wasmer_instance_destroy(instance);
//!
//! return 0;
//! }
//! ```
//!
//! [wasmer_h]: ./wasmer.h
//! [wasmer_hh]: ./wasmer.hh
use lazy_static::lazy_static;
pub mod export;
pub mod global;
pub mod import;
pub mod instance;
pub mod memory;
pub mod module;
pub mod table;
// `not(target_family = "windows")` is simpler than `unix`. See build.rs
// if you want to change the meaning of these `cfg`s in the header file.
/*
TODO: reenable `trampoline` module when the refactor gains feature parity with Wasmer master
#[cfg(all(not(target_family = "windows"), target_arch = "x86_64"))]
pub mod trampoline;*/
pub mod value;
/// The `wasmer_result_t` enum is a type that represents either a
/// success, or a failure.
#[allow(non_camel_case_types)]
#[repr(C)]
pub enum wasmer_result_t {
/// Represents a success.
WASMER_OK = 1,
/// Represents a failure.
WASMER_ERROR = 2,
}
/// The `wasmer_limits_t` struct is a type that describes the limits of something
/// such as a memory or a table. See the `wasmer_memory_new()` function to get
/// more information.
#[repr(C)]
pub struct wasmer_limits_t {
/// The minimum number of allowed pages.
pub min: u32,
/// The maximum number of allowed pages.
pub max: wasmer_limit_option_t,
}
/// The `wasmer_limit_option_t` struct represents an optional limit
/// for `wasmer_limits_t`.
#[repr(C)]
pub struct wasmer_limit_option_t {
/// Whether the limit is set.
pub has_some: bool,
/// The limit value.
pub some: u32,
}
#[repr(C)]
pub struct wasmer_byte_array {
pub bytes: *const u8,
pub bytes_len: u32,
}
impl wasmer_byte_array {
/// Get the data as a slice
pub unsafe fn as_slice<'a>(&self) -> &'a [u8] {
get_slice_checked(self.bytes, self.bytes_len as usize)
}
/// Copy the data into an owned Vec
pub unsafe fn as_vec(&self) -> Vec<u8> {
let mut out = Vec::with_capacity(self.bytes_len as usize);
out.extend_from_slice(self.as_slice());
out
}
/// Read the data as a &str, returns an error if the string is not valid UTF8
pub unsafe fn as_str<'a>(&self) -> Result<&'a str, std::str::Utf8Error> {
std::str::from_utf8(self.as_slice())
}
}
/// Gets a slice from a pointer and a length, returning an empty slice if the
/// pointer is null
#[inline]
pub(crate) unsafe fn get_slice_checked<'a, T>(ptr: *const T, len: usize) -> &'a [T] {
if ptr.is_null() {
&[]
} else {
std::slice::from_raw_parts(ptr, len)
}
}
lazy_static! {
pub(crate) static ref GLOBAL_STORE: wasmer::Store =
wasmer::Store::new(&*crate::wasm_c_api::wasm_engine_new().inner);
}
pub(crate) fn get_global_store() -> &'static wasmer::Store {
&*GLOBAL_STORE
}

View File

@@ -1,12 +1,13 @@
//! Compile, validate, instantiate, serialize, and destroy modules.
use crate::{
error::{update_last_error, CApiError},
use crate::deprecated::{
export::wasmer_import_export_kind,
get_global_store,
import::{wasmer_import_object_t, wasmer_import_t, CAPIImportObject},
instance::{wasmer_instance_t, CAPIInstance},
wasmer_byte_array, wasmer_result_t,
};
use crate::error::{update_last_error, CApiError};
use libc::c_int;
use std::collections::HashMap;
use std::ptr::NonNull;
@@ -33,7 +34,7 @@ pub unsafe extern "C" fn wasmer_compile(
wasm_bytes_len: u32,
) -> wasmer_result_t {
let bytes: &[u8] = slice::from_raw_parts_mut(wasm_bytes, wasm_bytes_len as usize);
let store = crate::get_global_store();
let store = get_global_store();
let result = Module::from_binary(store, bytes);
let new_module = match result {
Ok(instance) => instance,
@@ -68,7 +69,7 @@ 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);
let store = crate::get_global_store();
let store = get_global_store();
Module::validate(store, bytes).is_ok()
}
@@ -297,7 +298,7 @@ pub unsafe extern "C" fn wasmer_module_deserialize(
return wasmer_result_t::WASMER_ERROR;
};
let store = crate::get_global_store();
let store = get_global_store();
match Module::deserialize(store, serialized_module) {
Ok(deserialized_module) => {

View File

@@ -1,6 +1,7 @@
//! Create, grow, destroy tables of an instance.
use crate::{error::update_last_error, wasmer_limits_t, wasmer_result_t};
use crate::deprecated::{get_global_store, wasmer_limits_t, wasmer_result_t};
use crate::error::update_last_error;
use std::ptr::NonNull;
use wasmer::{ExternRef, Table, TableType, Val, ValType};
@@ -45,7 +46,7 @@ pub unsafe extern "C" fn wasmer_table_new(
minimum: limits.min,
maximum: max,
};
let store = crate::get_global_store();
let store = get_global_store();
let result = Table::new(store, desc, get_default_table_value(ValType::FuncRef));
let new_table = match result {
Ok(table) => table,

View File

@@ -2,89 +2,6 @@
#![doc(html_logo_url = "https://avatars3.githubusercontent.com/u/44205449?s=200&v=4")]
// temporary while in transition
#![allow(unused_variables)]
//! # Wasmer Runtime C API
//!
//! Wasmer is a standalone JIT WebAssembly runtime, aiming to be fully
//! compatible with Emscripten, Rust and Go. [Learn
//! more](https://github.com/wasmerio/wasmer).
//!
//! This crate exposes a C and C++ API for the Wasmer runtime.
//!
//! # Usage
//!
//! The C and C++ header files can be found in the source tree of this
//! crate, respectively [`wasmer.h`][wasmer_h] and
//! [`wasmer.hh`][wasmer_hh]. They are automatically generated, and always
//! up-to-date in this repository.
//!
//! Here is a simple example to use the C API:
//!
//! ```c
//! #include <stdio.h>
//! #include "wasmer.h"
//! #include <assert.h>
//! #include <stdint.h>
//!
//! int main()
//! {
//! // Read the Wasm file bytes.
//! FILE *file = fopen("sum.wasm", "r");
//! fseek(file, 0, SEEK_END);
//! long len = ftell(file);
//! uint8_t *bytes = malloc(len);
//! fseek(file, 0, SEEK_SET);
//! fread(bytes, 1, len, file);
//! fclose(file);
//!
//! // Prepare the imports.
//! wasmer_import_t imports[] = {};
//!
//! // Instantiate!
//! wasmer_instance_t *instance = NULL;
//! wasmer_result_t instantiation_result = wasmer_instantiate(&instance, bytes, len, imports, 0);
//!
//! assert(instantiation_result == WASMER_OK);
//!
//! // Let's call a function.
//! // Start by preparing the arguments.
//!
//! // Value of argument #1 is `7i32`.
//! wasmer_value_t argument_one;
//! argument_one.tag = WASM_I32;
//! argument_one.value.I32 = 7;
//!
//! // Value of argument #2 is `8i32`.
//! wasmer_value_t argument_two;
//! argument_two.tag = WASM_I32;
//! argument_two.value.I32 = 8;
//!
//! // Prepare the arguments.
//! wasmer_value_t arguments[] = {argument_one, argument_two};
//!
//! // Prepare the return value.
//! wasmer_value_t result_one;
//! wasmer_value_t results[] = {result_one};
//!
//! // Call the `sum` function with the prepared arguments and the return value.
//! wasmer_result_t call_result = wasmer_instance_call(instance, "sum", arguments, 2, results, 1);
//!
//! // Let's display the result.
//! printf("Call result: %d\n", call_result);
//! printf("Result: %d\n", results[0].value.I32);
//!
//! // `sum(7, 8) == 15`.
//! assert(results[0].value.I32 == 15);
//! assert(call_result == WASMER_OK);
//!
//! wasmer_instance_destroy(instance);
//!
//! return 0;
//! }
//! ```
//!
//! [wasmer_h]: ./wasmer.h
//! [wasmer_hh]: ./wasmer.hh
#![deny(
dead_code,
unused_imports,
@@ -94,111 +11,8 @@
unreachable_patterns
)]
#[allow(unused_imports)]
#[macro_use]
extern crate cfg_if;
#[macro_use]
extern crate lazy_static;
#[macro_use]
extern crate thiserror;
pub mod deprecated;
pub mod error;
mod ordered_resolver;
/// cbindgen:ignore
pub mod wasm_c_api;
pub mod error;
pub mod export;
pub mod global;
pub mod import;
pub mod instance;
pub mod memory;
pub mod module;
mod ordered_resolver;
pub mod table;
// `not(target_family = "windows")` is simpler than `unix`. See build.rs
// if you want to change the meaning of these `cfg`s in the header file.
/*
TODO: reenable `trampoline` module when the refactor gains feature parity with Wasmer master
#[cfg(all(not(target_family = "windows"), target_arch = "x86_64"))]
pub mod trampoline;*/
pub mod value;
/// The `wasmer_result_t` enum is a type that represents either a
/// success, or a failure.
#[allow(non_camel_case_types)]
#[repr(C)]
pub enum wasmer_result_t {
/// Represents a success.
WASMER_OK = 1,
/// Represents a failure.
WASMER_ERROR = 2,
}
/// The `wasmer_limits_t` struct is a type that describes the limits of something
/// such as a memory or a table. See the `wasmer_memory_new()` function to get
/// more information.
#[repr(C)]
pub struct wasmer_limits_t {
/// The minimum number of allowed pages.
pub min: u32,
/// The maximum number of allowed pages.
pub max: wasmer_limit_option_t,
}
/// The `wasmer_limit_option_t` struct represents an optional limit
/// for `wasmer_limits_t`.
#[repr(C)]
pub struct wasmer_limit_option_t {
/// Whether the limit is set.
pub has_some: bool,
/// The limit value.
pub some: u32,
}
#[repr(C)]
pub struct wasmer_byte_array {
pub bytes: *const u8,
pub bytes_len: u32,
}
impl wasmer_byte_array {
/// Get the data as a slice
pub unsafe fn as_slice<'a>(&self) -> &'a [u8] {
get_slice_checked(self.bytes, self.bytes_len as usize)
}
/// Copy the data into an owned Vec
pub unsafe fn as_vec(&self) -> Vec<u8> {
let mut out = Vec::with_capacity(self.bytes_len as usize);
out.extend_from_slice(self.as_slice());
out
}
/// Read the data as a &str, returns an error if the string is not valid UTF8
pub unsafe fn as_str<'a>(&self) -> Result<&'a str, std::str::Utf8Error> {
std::str::from_utf8(self.as_slice())
}
}
/// Gets a slice from a pointer and a length, returning an empty slice if the
/// pointer is null
#[inline]
pub(crate) unsafe fn get_slice_checked<'a, T>(ptr: *const T, len: usize) -> &'a [T] {
if ptr.is_null() {
&[]
} else {
std::slice::from_raw_parts(ptr, len)
}
}
lazy_static! {
pub(crate) static ref GLOBAL_STORE: wasmer::Store =
wasmer::Store::new(&*crate::wasm_c_api::wasm_engine_new().inner);
}
pub(crate) fn get_global_store() -> &'static wasmer::Store {
&*GLOBAL_STORE
}

View File

@@ -1,11 +1,13 @@
//! entrypoints for the standard C API
use cfg_if::cfg_if;
use std::convert::{TryFrom, TryInto};
use std::ffi::c_void;
use std::mem;
use std::ptr::{self, NonNull};
use std::slice;
use std::sync::Arc;
use thiserror::Error;
pub(crate) mod utils;
#[cfg(feature = "wasi")]