-
Notifications
You must be signed in to change notification settings - Fork 1.4k
c-api: component-model: Values and function calling #10697
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
Merged
alexcrichton
merged 24 commits into
bytecodealliance:main
from
MangoPeachGrape:c-api/component-model/val
May 23, 2025
Merged
Changes from all commits
Commits
Show all changes
24 commits
Select commit
Hold shift + click to select a range
358f78a
c-api: component-model: Primitive values
MangoPeachGrape 1dad666
c-api: component-model: Function calling
MangoPeachGrape 151581d
A test
MangoPeachGrape a20c540
Take args as mut to avoid copying
MangoPeachGrape ac9eb55
String and char
MangoPeachGrape 77c103e
Rethink value ownership semantics, add list values
MangoPeachGrape 13e9be6
Record values
MangoPeachGrape 5040404
Make take Rust values as refs in `::from()` functions
MangoPeachGrape c2351c6
Define host functions
MangoPeachGrape c9f7a35
`wasmtime_component_valrecord_new()`
MangoPeachGrape c392a4e
Use `u32` instead of `char` as its not ffi safe
MangoPeachGrape 8f422af
Test records and strings in c-api -> vm -> host func -> vm -> c-api
MangoPeachGrape 9983d65
`wasmtime_component_vallist_new()`
MangoPeachGrape e644e3e
Test lists
MangoPeachGrape 85f9c79
Fix formatting
MangoPeachGrape d9e81ef
Use existing `declare_vecs` construct
MangoPeachGrape 73ec3c6
Add rest of helper functions
MangoPeachGrape c131719
Add documentation
MangoPeachGrape 84f2298
Fix multiline comments
MangoPeachGrape 50b0270
Third time's the charm
MangoPeachGrape 01bc672
Fourth time's the charm prtest:full
MangoPeachGrape e8f72ef
Doxygen file headers
MangoPeachGrape c9fbe6b
Fix other missing documentation
MangoPeachGrape 4f6c70c
Small fix to docs
MangoPeachGrape File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,157 @@ | ||
/// \file wasmtime/component/val.h | ||
|
||
#ifndef WASMTIME_COMPONENT_VAL_H | ||
#define WASMTIME_COMPONENT_VAL_H | ||
|
||
#include <wasmtime/conf.h> | ||
|
||
#ifdef WASMTIME_FEATURE_COMPONENT_MODEL | ||
|
||
#ifdef __cplusplus | ||
extern "C" { | ||
#endif | ||
|
||
/// \brief Discriminant used in #wasmtime_component_val_t::kind | ||
typedef uint8_t wasmtime_component_valkind_t; | ||
|
||
/// \brief Value of #wasmtime_component_valkind_t meaning that | ||
/// #wasmtime_component_val_t is a bool | ||
#define WASMTIME_COMPONENT_BOOL 0 | ||
/// \brief Value of #wasmtime_component_valkind_t meaning that | ||
/// #wasmtime_component_val_t is a s8 | ||
#define WASMTIME_COMPONENT_S8 1 | ||
/// \brief Value of #wasmtime_component_valkind_t meaning that | ||
/// #wasmtime_component_val_t is a u8 | ||
#define WASMTIME_COMPONENT_U8 2 | ||
/// \brief Value of #wasmtime_component_valkind_t meaning that | ||
/// #wasmtime_component_val_t is a s16 | ||
#define WASMTIME_COMPONENT_S16 3 | ||
/// \brief Value of #wasmtime_component_valkind_t meaning that | ||
/// #wasmtime_component_val_t is a u16 | ||
#define WASMTIME_COMPONENT_U16 4 | ||
/// \brief Value of #wasmtime_component_valkind_t meaning that | ||
/// #wasmtime_component_val_t is a s32 | ||
#define WASMTIME_COMPONENT_S32 5 | ||
/// \brief Value of #wasmtime_component_valkind_t meaning that | ||
/// #wasmtime_component_val_t is a u32 | ||
#define WASMTIME_COMPONENT_U32 6 | ||
/// \brief Value of #wasmtime_component_valkind_t meaning that | ||
/// #wasmtime_component_val_t is a s64 | ||
#define WASMTIME_COMPONENT_S64 7 | ||
/// \brief Value of #wasmtime_component_valkind_t meaning that | ||
/// #wasmtime_component_val_t is a u64 | ||
#define WASMTIME_COMPONENT_U64 8 | ||
/// \brief Value of #wasmtime_component_valkind_t meaning that | ||
/// #wasmtime_component_val_t is a f32 | ||
#define WASMTIME_COMPONENT_F32 9 | ||
/// \brief Value of #wasmtime_component_valkind_t meaning that | ||
/// #wasmtime_component_val_t is a f64 | ||
#define WASMTIME_COMPONENT_F64 10 | ||
/// \brief Value of #wasmtime_component_valkind_t meaning that | ||
/// #wasmtime_component_val_t is a char | ||
#define WASMTIME_COMPONENT_CHAR 11 | ||
/// \brief Value of #wasmtime_component_valkind_t meaning that | ||
/// #wasmtime_component_val_t is a string | ||
#define WASMTIME_COMPONENT_STRING 12 | ||
/// \brief Value of #wasmtime_component_valkind_t meaning that | ||
/// #wasmtime_component_val_t is a list | ||
#define WASMTIME_COMPONENT_LIST 13 | ||
/// \brief Value of #wasmtime_component_valkind_t meaning that | ||
/// #wasmtime_component_val_t is a record | ||
#define WASMTIME_COMPONENT_RECORD 14 | ||
|
||
struct wasmtime_component_val; | ||
struct wasmtime_component_valrecord_entry; | ||
|
||
#define DECLARE_VEC(name, type) \ | ||
/** \brief A vec of a type */ \ | ||
typedef struct name { \ | ||
/** Length of the vec */ \ | ||
size_t size; \ | ||
/** Pointer to the elements */ \ | ||
type *data; \ | ||
} name##_t; \ | ||
\ | ||
/** \brief Create vec from \p ptr and \p size */ \ | ||
WASM_API_EXTERN void name##_new(name##_t *out, size_t size, type *ptr); \ | ||
/** \brief Create an empty vec */ \ | ||
WASM_API_EXTERN void name##_new_empty(name##_t *out); \ | ||
/** \brief Create a vec with length \p size */ \ | ||
WASM_API_EXTERN void name##_new_uninit(name##_t *out, size_t size); \ | ||
/** \brief Copy \p src to \p dst */ \ | ||
WASM_API_EXTERN void name##_copy(name##_t *dst, const name##_t *src); \ | ||
/** \brief Delete \p value */ \ | ||
WASM_API_EXTERN void name##_delete(name##_t *value); | ||
|
||
DECLARE_VEC(wasmtime_component_vallist, struct wasmtime_component_val) | ||
DECLARE_VEC(wasmtime_component_valrecord, | ||
struct wasmtime_component_valrecord_entry) | ||
|
||
#undef DECLARE_VEC | ||
|
||
/// \brief Represents possible runtime values which a component function can | ||
/// either consume or produce | ||
typedef union { | ||
/// Field used if #wasmtime_component_val_t::kind is #WASMTIME_COMPONENT_BOOL | ||
bool boolean; | ||
/// Field used if #wasmtime_component_val_t::kind is #WASMTIME_COMPONENT_S8 | ||
int8_t s8; | ||
/// Field used if #wasmtime_component_val_t::kind is #WASMTIME_COMPONENT_U8 | ||
uint8_t u8; | ||
/// Field used if #wasmtime_component_val_t::kind is #WASMTIME_COMPONENT_S16 | ||
int16_t s16; | ||
/// Field used if #wasmtime_component_val_t::kind is #WASMTIME_COMPONENT_U16 | ||
uint16_t u16; | ||
/// Field used if #wasmtime_component_val_t::kind is #WASMTIME_COMPONENT_S32 | ||
int32_t s32; | ||
/// Field used if #wasmtime_component_val_t::kind is #WASMTIME_COMPONENT_U32 | ||
uint32_t u32; | ||
/// Field used if #wasmtime_component_val_t::kind is #WASMTIME_COMPONENT_S64 | ||
int64_t s64; | ||
/// Field used if #wasmtime_component_val_t::kind is #WASMTIME_COMPONENT_U64 | ||
uint64_t u64; | ||
/// Field used if #wasmtime_component_val_t::kind is #WASMTIME_COMPONENT_F32 | ||
float32_t f32; | ||
/// Field used if #wasmtime_component_val_t::kind is #WASMTIME_COMPONENT_F64 | ||
float64_t f64; | ||
/// Field used if #wasmtime_component_val_t::kind is #WASMTIME_COMPONENT_CHAR | ||
uint32_t character; | ||
/// Field used if #wasmtime_component_val_t::kind is | ||
/// #WASMTIME_COMPONENT_STRING | ||
wasm_name_t string; | ||
/// Field used if #wasmtime_component_val_t::kind is #WASMTIME_COMPONENT_LIST | ||
wasmtime_component_vallist_t list; | ||
/// Field used if #wasmtime_component_val_t::kind is | ||
/// #WASMTIME_COMPONENT_RECORD | ||
wasmtime_component_valrecord_t record; | ||
} wasmtime_component_valunion_t; | ||
|
||
/// \brief Represents possible runtime values which a component function can | ||
/// either consume or produce | ||
typedef struct wasmtime_component_val { | ||
/// The type discriminant | ||
wasmtime_component_valkind_t kind; | ||
/// Value of type \ref kind | ||
wasmtime_component_valunion_t of; | ||
} wasmtime_component_val_t; | ||
|
||
/// \brief A pair of a name and a value that represents one entry in a value | ||
/// with kind #WASMTIME_COMPONENT_RECORD | ||
typedef struct wasmtime_component_valrecord_entry { | ||
/// The name of this entry | ||
wasm_name_t name; | ||
/// The value of this entry | ||
wasmtime_component_val_t val; | ||
} wasmtime_component_valrecord_entry_t; | ||
|
||
/// \brief Calls the destructor on \p value deallocating any owned memory | ||
WASM_API_EXTERN void | ||
wasmtime_component_val_delete(wasmtime_component_val_t *value); | ||
|
||
#ifdef __cplusplus | ||
} // extern "C" | ||
#endif | ||
|
||
#endif // WASMTIME_FEATURE_COMPONENT_MODEL | ||
|
||
#endif // WASMTIME_COMPONENT_VAL_H |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
use wasmtime::component::{Func, Val}; | ||
|
||
use crate::{WasmtimeStoreContextMut, wasmtime_error_t}; | ||
|
||
use super::wasmtime_component_val_t; | ||
|
||
#[unsafe(no_mangle)] | ||
pub unsafe extern "C" fn wasmtime_component_func_call( | ||
func: &Func, | ||
mut context: WasmtimeStoreContextMut<'_>, | ||
args: *const wasmtime_component_val_t, | ||
args_len: usize, | ||
results: *mut wasmtime_component_val_t, | ||
results_len: usize, | ||
) -> Option<Box<wasmtime_error_t>> { | ||
let c_args = unsafe { std::slice::from_raw_parts(args, args_len) }; | ||
let c_results = unsafe { std::slice::from_raw_parts_mut(results, results_len) }; | ||
|
||
let args = c_args.iter().map(Val::from).collect::<Vec<_>>(); | ||
let mut results = vec![Val::Bool(false); results_len]; | ||
|
||
let result = func | ||
.call(&mut context, &args, &mut results) | ||
.and_then(|_| func.post_return(&mut context)); | ||
MangoPeachGrape marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
crate::handle_result(result, |_| { | ||
for (c_val, rust_val) in std::iter::zip(c_results, results) { | ||
*c_val = wasmtime_component_val_t::from(&rust_val); | ||
} | ||
}) | ||
} |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.