Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Port C API to new Context API #2973

Merged
merged 3 commits into from
Jun 30, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
68 changes: 58 additions & 10 deletions Cargo.lock

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

8 changes: 8 additions & 0 deletions lib/api/src/sys/context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,14 @@ impl<T> Context<T> {
pub fn store(&self) -> &Store {
&self.inner.store
}

/// For use with the C API
/// # Safety
///
/// This is unsafe.
pub unsafe fn transmute_data<U>(&mut self) -> &mut Context<U> {
core::mem::transmute::<&mut Self, &mut Context<U>>(self)
}
}

/// A temporary handle to a [`Context`].
Expand Down
5 changes: 3 additions & 2 deletions lib/api/src/sys/externals/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ impl Extern {
}

/// Create an `Extern` from an `wasmer_engine::Export`.
pub(crate) fn from_vm_extern(ctx: &mut impl AsContextMut, vm_extern: VMExtern) -> Self {
pub fn from_vm_extern(ctx: &mut impl AsContextMut, vm_extern: VMExtern) -> Self {
match vm_extern {
VMExtern::Function(f) => Self::Function(Function::from_vm_extern(ctx, f)),
VMExtern::Memory(m) => Self::Memory(Memory::from_vm_extern(ctx, m)),
Expand All @@ -63,7 +63,8 @@ impl Extern {
}
}

pub(crate) fn to_vm_extern(&self) -> VMExtern {
/// To `VMExtern`.
pub fn to_vm_extern(&self) -> VMExtern {
match self {
Self::Function(f) => f.to_vm_extern(),
Self::Global(g) => g.to_vm_extern(),
Expand Down
2 changes: 1 addition & 1 deletion lib/c-api/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -85,4 +85,4 @@ jit = ["universal"]
#emscripten = ["wasmer-emscripten"]

[build-dependencies]
cbindgen = "0.19"
cbindgen = "0.24"
40 changes: 40 additions & 0 deletions lib/c-api/src/wasm_c_api/context.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
use crate::wasm_c_api::store::wasm_store_t;
use libc::c_void;
use wasmer_api::Context;

/// Opaque type representing a WebAssembly context.
#[allow(non_camel_case_types)]
pub struct wasm_context_t {
pub(crate) inner: Context<*mut c_void>,
}

impl core::fmt::Debug for wasm_context_t {
fn fmt(&self, fmt: &mut std::fmt::Formatter) -> std::fmt::Result {
write!(fmt, "wasm_context_t")
}
}

/// Creates a new WebAssembly Context given a specific [engine][super::engine].
///
/// # Example
///
/// See the module's documentation.
#[no_mangle]
pub unsafe extern "C" fn wasm_context_new(
store: Option<&wasm_store_t>,
data: *mut c_void,
) -> Option<Box<wasm_context_t>> {
let store = store?;

Some(Box::new(wasm_context_t {
inner: Context::new(&store.inner, data),
}))
}

/// Deletes a WebAssembly context.
///
/// # Example
///
/// See the module's documentation.
#[no_mangle]
pub unsafe extern "C" fn wasm_context_delete(_context: Option<Box<wasm_context_t>>) {}
Loading