Initial commit of C API for metering and middleware.

This commit is contained in:
Nick Lewycky
2021-02-09 01:20:34 -08:00
parent c11a37f0da
commit 197d0f7a49
10 changed files with 183 additions and 17 deletions

View File

@@ -4,6 +4,8 @@ use crate::error::{update_last_error, CApiError};
use cfg_if::cfg_if;
use std::sync::Arc;
use wasmer::Engine;
#[cfg(feature = "middlewares")]
use wasmer_compiler::ModuleMiddleware;
#[cfg(feature = "jit")]
use wasmer_engine_jit::JIT;
#[cfg(feature = "native")]
@@ -11,6 +13,10 @@ use wasmer_engine_native::Native;
#[cfg(feature = "object-file")]
use wasmer_engine_object_file::ObjectFile;
#[cfg(feature = "middlewares")]
#[cfg(not(feature = "compiler"))]
compile_error!("middlewares features requires compilers feature");
/// Kind of compilers that can be used by the engines.
///
/// This is a Wasmer-specific type with Wasmer-specific functions for
@@ -86,6 +92,16 @@ impl Default for wasmer_engine_t {
}
}
/// Opaque representing a middleware.
///
#[cfg(feature = "middlewares")]
#[derive(Debug)]
#[repr(C)]
#[allow(non_camel_case_types)]
pub struct wasmer_module_middleware_t {
pub(super) inner: Arc<dyn ModuleMiddleware>,
}
/// A configuration holds the compiler and the engine used by the store.
///
/// cbindgen:ignore
@@ -96,6 +112,8 @@ pub struct wasm_config_t {
#[cfg(feature = "compiler")]
compiler: wasmer_compiler_t,
pub(super) target: Option<Box<wasm_target_t>>,
#[cfg(feature = "middlewares")]
pub(super) middlewares: Vec<wasmer_module_middleware_t>,
}
/// Create a new default Wasmer configuration.
@@ -246,6 +264,16 @@ pub extern "C" fn wasm_config_set_engine(config: &mut wasm_config_t, engine: was
config.engine = engine;
}
// TODO: documentation
#[cfg(feature = "middlewares")]
#[no_mangle]
pub extern "C" fn wasm_config_push_middleware(
config: &mut wasm_config_t,
middleware: Box<wasmer_module_middleware_t>,
) {
config.middlewares.push(*middleware);
}
/// An engine is used by the store to drive the compilation and the
/// execution of a WebAssembly module.
///
@@ -311,7 +339,7 @@ cfg_if! {
/// cbindgen:ignore
#[no_mangle]
pub extern "C" fn wasm_engine_new() -> Box<wasm_engine_t> {
let mut compiler_config: Box<dyn CompilerConfig> = get_default_compiler_config();
let compiler_config: Box<dyn CompilerConfig> = get_default_compiler_config();
let engine: Arc<dyn Engine + Send + Sync> = Arc::new(Native::new(compiler_config).engine());
Box::new(wasm_engine_t { inner: engine })
}
@@ -448,6 +476,14 @@ pub extern "C" fn wasm_engine_new_with_config(
},
};
cfg_if! {
if #[cfg(feature = "middlewares")] {
for middleware in config.middlewares {
compiler_config.push_middleware(middleware.inner);
}
}
}
let inner: Arc<dyn Engine + Send + Sync> = match config.engine {
wasmer_engine_t::JIT => {
cfg_if! {