Refactor and fill out wasmtime's C API (#1415)
* Refactor and improve safety of C API This commit is intended to be a relatively large refactoring of the C API which is targeted at improving the safety of our C API definitions. Not all of the APIs have been updated yet but this is intended to be the start. The goal here is to make as many functions safe as we can, expressing inputs/outputs as native Rust types rather than raw pointers wherever possible. For example instead of `*const wasm_foo_t` we'd take `&wasm_foo_t`. Instead of returning `*mut wasm_foo_t` we'd return `Box<wasm_foo_t>`. No ABI/API changes are intended from this commit, it's supposed to only change how we define all these functions internally. This commit also additionally implements a few more API bindings for exposed vector types by unifying everything into one macro. Finally, this commit moves many internal caches in the C API to the `OnceCell` type which provides a safe interface for one-time initialization. * Split apart monolithic C API `lib.rs` This commit splits the monolithic `src/lib.rs` in the C API crate into lots of smaller files. The goal here is to make this a bit more readable and digestable. Each module now contains only API bindings for a particular type, roughly organized around the grouping in the wasm.h header file already. A few more extensions were added, such as filling out `*_as_*` conversions with both const and non-const versions. Additionally many APIs were made safer in the same style as the previous commit, generally preferring Rust types rather than raw pointer types. Overall no functional change is intended here, it should be mostly just code movement and minor refactorings! * Make a few wasi C bindings safer Use safe Rust types where we can and touch up a few APIs here and there. * Implement `wasm_*type_as_externtype*` APIs This commit restructures `wasm_externtype_t` to be similar to `wasm_extern_t` so type conversion between the `*_extern_*` variants to the concrete variants are all simple casts. (checked in the case of general to concrete, of course). * Consistently imlpement host info functions in the API This commit adds a small macro crate which is then used to consistently define the various host-info-related functions in the C API. The goal here is to try to mirror what the `wasm.h` header provides to provide a full implementation of the header.
This commit is contained in:
13
crates/c-api/macros/Cargo.toml
Normal file
13
crates/c-api/macros/Cargo.toml
Normal file
@@ -0,0 +1,13 @@
|
||||
[package]
|
||||
name = "wasmtime-c-api-macros"
|
||||
version = "0.1.0"
|
||||
authors = ["The Wasmtime Project Developers"]
|
||||
edition = "2018"
|
||||
publish = false
|
||||
|
||||
[lib]
|
||||
proc-macro = true
|
||||
|
||||
[dependencies]
|
||||
quote = "1.0"
|
||||
proc-macro2 = "1.0"
|
||||
112
crates/c-api/macros/src/lib.rs
Normal file
112
crates/c-api/macros/src/lib.rs
Normal file
@@ -0,0 +1,112 @@
|
||||
//! A set of convenience macros for our wasmtime-c-api crate.
|
||||
//!
|
||||
//! These are intended to mirror the macros in the `wasm.h` header file and
|
||||
//! largely facilitate the `declare_ref` macro.
|
||||
|
||||
extern crate proc_macro;
|
||||
|
||||
use proc_macro2::{Ident, TokenStream, TokenTree};
|
||||
use quote::quote;
|
||||
|
||||
fn extract_ident(input: proc_macro::TokenStream) -> Ident {
|
||||
let input = TokenStream::from(input);
|
||||
let i = match input.into_iter().next().unwrap() {
|
||||
TokenTree::Ident(i) => i,
|
||||
_ => panic!("expected an ident"),
|
||||
};
|
||||
let name = i.to_string();
|
||||
assert!(name.ends_with("_t"));
|
||||
return i;
|
||||
}
|
||||
|
||||
#[proc_macro]
|
||||
pub fn declare_own(input: proc_macro::TokenStream) -> proc_macro::TokenStream {
|
||||
let ty = extract_ident(input);
|
||||
let name = ty.to_string();
|
||||
let delete = quote::format_ident!("{}_delete", &name[..name.len() - 2]);
|
||||
|
||||
(quote! {
|
||||
#[no_mangle]
|
||||
pub extern fn #delete(_: Box<#ty>) {}
|
||||
})
|
||||
.into()
|
||||
}
|
||||
|
||||
#[proc_macro]
|
||||
pub fn declare_ty(input: proc_macro::TokenStream) -> proc_macro::TokenStream {
|
||||
let ty = extract_ident(input);
|
||||
let name = ty.to_string();
|
||||
let copy = quote::format_ident!("{}_copy", &name[..name.len() - 2]);
|
||||
|
||||
(quote! {
|
||||
wasmtime_c_api_macros::declare_own!(#ty);
|
||||
|
||||
#[no_mangle]
|
||||
pub extern fn #copy(src: &#ty) -> Box<#ty> {
|
||||
Box::new(src.clone())
|
||||
}
|
||||
})
|
||||
.into()
|
||||
}
|
||||
|
||||
#[proc_macro]
|
||||
pub fn declare_ref(input: proc_macro::TokenStream) -> proc_macro::TokenStream {
|
||||
let ty = extract_ident(input);
|
||||
let name = ty.to_string();
|
||||
let prefix = &name[..name.len() - 2];
|
||||
let copy = quote::format_ident!("{}_copy", prefix);
|
||||
let same = quote::format_ident!("{}_same", prefix);
|
||||
let get_host_info = quote::format_ident!("{}_get_host_info", prefix);
|
||||
let set_host_info = quote::format_ident!("{}_set_host_info", prefix);
|
||||
let set_host_info_final = quote::format_ident!("{}_set_host_info_with_finalizer", prefix);
|
||||
let as_ref = quote::format_ident!("{}_as_ref", prefix);
|
||||
let as_ref_const = quote::format_ident!("{}_as_ref_const", prefix);
|
||||
|
||||
(quote! {
|
||||
wasmtime_c_api_macros::declare_own!(#ty);
|
||||
|
||||
#[no_mangle]
|
||||
pub extern fn #copy(src: &#ty) -> Box<#ty> {
|
||||
Box::new(src.clone())
|
||||
}
|
||||
|
||||
#[no_mangle]
|
||||
pub extern fn #same(a: &#ty, b: &#ty) -> bool {
|
||||
a.anyref().ptr_eq(&b.anyref())
|
||||
}
|
||||
|
||||
#[no_mangle]
|
||||
pub extern fn #get_host_info(a: &#ty) -> *mut std::os::raw::c_void {
|
||||
crate::r#ref::get_host_info(&a.anyref())
|
||||
}
|
||||
|
||||
#[no_mangle]
|
||||
pub extern fn #set_host_info(a: &#ty, info: *mut std::os::raw::c_void) {
|
||||
crate::r#ref::set_host_info(&a.anyref(), info, None)
|
||||
}
|
||||
|
||||
#[no_mangle]
|
||||
pub extern fn #set_host_info_final(
|
||||
a: &#ty,
|
||||
info: *mut std::os::raw::c_void,
|
||||
finalizer: Option<extern "C" fn(*mut std::os::raw::c_void)>,
|
||||
) {
|
||||
crate::r#ref::set_host_info(&a.anyref(), info, finalizer)
|
||||
}
|
||||
|
||||
#[no_mangle]
|
||||
pub extern fn #as_ref(a: &#ty) -> Box<crate::wasm_ref_t> {
|
||||
let r = a.anyref();
|
||||
Box::new(crate::wasm_ref_t { r })
|
||||
}
|
||||
|
||||
#[no_mangle]
|
||||
pub extern fn #as_ref_const(a: &#ty) -> Box<crate::wasm_ref_t> {
|
||||
#as_ref(a)
|
||||
}
|
||||
|
||||
// TODO: implement `wasm_ref_as_#name#`
|
||||
// TODO: implement `wasm_ref_as_#name#_const`
|
||||
})
|
||||
.into()
|
||||
}
|
||||
Reference in New Issue
Block a user