Naming fixes and documentation for trampoline API.

This commit is contained in:
losfair
2019-06-05 01:38:35 +08:00
parent f1b27d5774
commit f4df568e41
3 changed files with 70 additions and 34 deletions

View File

@ -1,4 +1,7 @@
//! Trampoline emitter for transforming function calls.
use std::ffi::c_void; use std::ffi::c_void;
use std::mem;
use wasmer_runtime_core::trampoline::*; use wasmer_runtime_core::trampoline::*;
#[repr(C)] #[repr(C)]
@ -10,6 +13,7 @@ pub struct wasmer_trampoline_buffer_t;
#[repr(C)] #[repr(C)]
pub struct wasmer_trampoline_callable_t; pub struct wasmer_trampoline_callable_t;
/// Creates a new trampoline builder.
#[no_mangle] #[no_mangle]
#[allow(clippy::cast_ptr_alignment)] #[allow(clippy::cast_ptr_alignment)]
pub extern "C" fn wasmer_trampoline_buffer_builder_new() -> *mut wasmer_trampoline_buffer_builder_t pub extern "C" fn wasmer_trampoline_buffer_builder_new() -> *mut wasmer_trampoline_buffer_builder_t
@ -17,58 +21,62 @@ pub extern "C" fn wasmer_trampoline_buffer_builder_new() -> *mut wasmer_trampoli
Box::into_raw(Box::new(TrampolineBufferBuilder::new())) as *mut _ Box::into_raw(Box::new(TrampolineBufferBuilder::new())) as *mut _
} }
/// Adds a context trampoline to the builder.
#[no_mangle] #[no_mangle]
#[allow(clippy::cast_ptr_alignment)] #[allow(clippy::cast_ptr_alignment)]
pub unsafe extern "C" fn wasmer_trampoline_buffer_builder_add_context_trampoline( pub unsafe extern "C" fn wasmer_trampoline_buffer_builder_add_context_trampoline(
b: *mut wasmer_trampoline_buffer_builder_t, builder: *mut wasmer_trampoline_buffer_builder_t,
f: *const wasmer_trampoline_callable_t, func: *const wasmer_trampoline_callable_t,
ctx: *const c_void, ctx: *const c_void,
) -> usize { ) -> usize {
let b = &mut *(b as *mut TrampolineBufferBuilder); let builder = &mut *(builder as *mut TrampolineBufferBuilder);
b.add_context_trampoline(f as *const CallTarget, ctx as *const CallContext) builder.add_context_trampoline(func as *const CallTarget, ctx as *const CallContext)
} }
/// Adds a callinfo trampoline to the builder.
#[no_mangle] #[no_mangle]
#[allow(clippy::cast_ptr_alignment)] #[allow(clippy::cast_ptr_alignment)]
pub unsafe extern "C" fn wasmer_trampoline_buffer_builder_add_callinfo_trampoline( pub unsafe extern "C" fn wasmer_trampoline_buffer_builder_add_callinfo_trampoline(
b: *mut wasmer_trampoline_buffer_builder_t, builder: *mut wasmer_trampoline_buffer_builder_t,
f: *const wasmer_trampoline_callable_t, func: *const wasmer_trampoline_callable_t,
ctx: *const c_void, ctx: *const c_void,
num_params: u32, num_params: u32,
) -> usize { ) -> usize {
let b = &mut *(b as *mut TrampolineBufferBuilder); let builder = &mut *(builder as *mut TrampolineBufferBuilder);
b.add_callinfo_trampoline( builder.add_callinfo_trampoline(mem::transmute(func), ctx as *const CallContext, num_params)
::std::mem::transmute(f),
ctx as *const CallContext,
num_params,
)
} }
/// Finalizes the trampoline builder into an executable buffer.
#[no_mangle] #[no_mangle]
#[allow(clippy::cast_ptr_alignment)] #[allow(clippy::cast_ptr_alignment)]
pub unsafe extern "C" fn wasmer_trampoline_buffer_builder_build( pub unsafe extern "C" fn wasmer_trampoline_buffer_builder_build(
b: *mut wasmer_trampoline_buffer_builder_t, builder: *mut wasmer_trampoline_buffer_builder_t,
) -> *mut wasmer_trampoline_buffer_t { ) -> *mut wasmer_trampoline_buffer_t {
let b = Box::from_raw(b as *mut TrampolineBufferBuilder); let builder = Box::from_raw(builder as *mut TrampolineBufferBuilder);
Box::into_raw(Box::new(b.build())) as *mut _ Box::into_raw(Box::new(builder.build())) as *mut _
} }
/// Destroys the trampoline buffer if not null.
#[no_mangle] #[no_mangle]
#[allow(clippy::cast_ptr_alignment)] #[allow(clippy::cast_ptr_alignment)]
pub unsafe extern "C" fn wasmer_trampoline_buffer_destroy(b: *mut wasmer_trampoline_buffer_t) { pub unsafe extern "C" fn wasmer_trampoline_buffer_destroy(buffer: *mut wasmer_trampoline_buffer_t) {
Box::from_raw(b); if !buffer.is_null() {
Box::from_raw(buffer);
}
} }
/// Returns the callable pointer for the trampoline with index `idx`.
#[no_mangle] #[no_mangle]
#[allow(clippy::cast_ptr_alignment)] #[allow(clippy::cast_ptr_alignment)]
pub unsafe extern "C" fn wasmer_trampoline_buffer_get_trampoline( pub unsafe extern "C" fn wasmer_trampoline_buffer_get_trampoline(
b: *const wasmer_trampoline_buffer_t, buffer: *const wasmer_trampoline_buffer_t,
idx: usize, idx: usize,
) -> *const wasmer_trampoline_callable_t { ) -> *const wasmer_trampoline_callable_t {
let b = &*(b as *const TrampolineBuffer); let buffer = &*(buffer as *const TrampolineBuffer);
b.get_trampoline(idx) as _ buffer.get_trampoline(idx) as _
} }
/// Returns the context added by `add_context_trampoline`, from within the callee function.
#[no_mangle] #[no_mangle]
#[allow(clippy::cast_ptr_alignment)] #[allow(clippy::cast_ptr_alignment)]
pub unsafe extern "C" fn wasmer_trampoline_get_context() -> *mut c_void { pub unsafe extern "C" fn wasmer_trampoline_get_context() -> *mut c_void {

View File

@ -596,24 +596,45 @@ uint32_t wasmer_table_length(wasmer_table_t *table);
*/ */
wasmer_result_t wasmer_table_new(wasmer_table_t **table, wasmer_limits_t limits); wasmer_result_t wasmer_table_new(wasmer_table_t **table, wasmer_limits_t limits);
uintptr_t wasmer_trampoline_buffer_builder_add_callinfo_trampoline(wasmer_trampoline_buffer_builder_t *b, /**
const wasmer_trampoline_callable_t *f, * Adds a callinfo trampoline to the builder.
*/
uintptr_t wasmer_trampoline_buffer_builder_add_callinfo_trampoline(wasmer_trampoline_buffer_builder_t *builder,
const wasmer_trampoline_callable_t *func,
const void *ctx, const void *ctx,
uint32_t num_params); uint32_t num_params);
uintptr_t wasmer_trampoline_buffer_builder_add_context_trampoline(wasmer_trampoline_buffer_builder_t *b, /**
const wasmer_trampoline_callable_t *f, * Adds a context trampoline to the builder.
*/
uintptr_t wasmer_trampoline_buffer_builder_add_context_trampoline(wasmer_trampoline_buffer_builder_t *builder,
const wasmer_trampoline_callable_t *func,
const void *ctx); const void *ctx);
wasmer_trampoline_buffer_t *wasmer_trampoline_buffer_builder_build(wasmer_trampoline_buffer_builder_t *b); /**
* Finalizes the trampoline builder into an executable buffer.
*/
wasmer_trampoline_buffer_t *wasmer_trampoline_buffer_builder_build(wasmer_trampoline_buffer_builder_t *builder);
/**
* Creates a new trampoline builder.
*/
wasmer_trampoline_buffer_builder_t *wasmer_trampoline_buffer_builder_new(void); wasmer_trampoline_buffer_builder_t *wasmer_trampoline_buffer_builder_new(void);
void wasmer_trampoline_buffer_destroy(wasmer_trampoline_buffer_t *b); /**
* Destroys the trampoline buffer if not null.
*/
void wasmer_trampoline_buffer_destroy(wasmer_trampoline_buffer_t *buffer);
const wasmer_trampoline_callable_t *wasmer_trampoline_buffer_get_trampoline(const wasmer_trampoline_buffer_t *b, /**
* Returns the callable pointer for the trampoline with index `idx`.
*/
const wasmer_trampoline_callable_t *wasmer_trampoline_buffer_get_trampoline(const wasmer_trampoline_buffer_t *buffer,
uintptr_t idx); uintptr_t idx);
/**
* Returns the context added by `add_context_trampoline`, from within the callee function.
*/
void *wasmer_trampoline_get_context(void); void *wasmer_trampoline_get_context(void);
/** /**

View File

@ -470,24 +470,31 @@ uint32_t wasmer_table_length(wasmer_table_t *table);
/// and `wasmer_last_error_message` to get an error message. /// and `wasmer_last_error_message` to get an error message.
wasmer_result_t wasmer_table_new(wasmer_table_t **table, wasmer_limits_t limits); wasmer_result_t wasmer_table_new(wasmer_table_t **table, wasmer_limits_t limits);
uintptr_t wasmer_trampoline_buffer_builder_add_callinfo_trampoline(wasmer_trampoline_buffer_builder_t *b, /// Adds a callinfo trampoline to the builder.
const wasmer_trampoline_callable_t *f, uintptr_t wasmer_trampoline_buffer_builder_add_callinfo_trampoline(wasmer_trampoline_buffer_builder_t *builder,
const wasmer_trampoline_callable_t *func,
const void *ctx, const void *ctx,
uint32_t num_params); uint32_t num_params);
uintptr_t wasmer_trampoline_buffer_builder_add_context_trampoline(wasmer_trampoline_buffer_builder_t *b, /// Adds a context trampoline to the builder.
const wasmer_trampoline_callable_t *f, uintptr_t wasmer_trampoline_buffer_builder_add_context_trampoline(wasmer_trampoline_buffer_builder_t *builder,
const wasmer_trampoline_callable_t *func,
const void *ctx); const void *ctx);
wasmer_trampoline_buffer_t *wasmer_trampoline_buffer_builder_build(wasmer_trampoline_buffer_builder_t *b); /// Finalizes the trampoline builder into an executable buffer.
wasmer_trampoline_buffer_t *wasmer_trampoline_buffer_builder_build(wasmer_trampoline_buffer_builder_t *builder);
/// Creates a new trampoline builder.
wasmer_trampoline_buffer_builder_t *wasmer_trampoline_buffer_builder_new(); wasmer_trampoline_buffer_builder_t *wasmer_trampoline_buffer_builder_new();
void wasmer_trampoline_buffer_destroy(wasmer_trampoline_buffer_t *b); /// Destroys the trampoline buffer if not null.
void wasmer_trampoline_buffer_destroy(wasmer_trampoline_buffer_t *buffer);
const wasmer_trampoline_callable_t *wasmer_trampoline_buffer_get_trampoline(const wasmer_trampoline_buffer_t *b, /// Returns the callable pointer for the trampoline with index `idx`.
const wasmer_trampoline_callable_t *wasmer_trampoline_buffer_get_trampoline(const wasmer_trampoline_buffer_t *buffer,
uintptr_t idx); uintptr_t idx);
/// Returns the context added by `add_context_trampoline`, from within the callee function.
void *wasmer_trampoline_get_context(); void *wasmer_trampoline_get_context();
/// Returns true for valid wasm bytes and false for invalid bytes /// Returns true for valid wasm bytes and false for invalid bytes