Skip to content

c-api: component-model: Getting type of a function #11438

Closed

Description

@MangoPeachGrape

(Part of the outstanding items of the component model: #8036 (comment))


There is currently no way to get the type of a function, i.e. the parameters and the return type.

See this implementation in the #9812 PR:

/// Representation of a field in a record type or a case in a variant type
typedef struct wasmtime_component_type_field_t {
/// Name of the record field or variant case
wasm_name_t name;
/// Type of the record field or variant case (may be null for variant case)
wasmtime_component_type_t *ty;
} wasmtime_component_type_field_t;
WASMTIME_COMPONENT_DECLARE_VEC_NEW(type_field_vec,
wasmtime_component_type_field_t);
/// Representation of a result type
typedef struct wasmtime_component_type_result_t {
/// Type of the ok value (if there is one)
wasmtime_component_type_t *ok_ty;
/// Type of the error value (if there is one)
wasmtime_component_type_t *err_ty;
} wasmtime_component_type_result_t;
/// Container for different kind of component model type data
typedef union wasmtime_component_type_payload_t {
/// Field used if #wasmtime_component_type_t::kind is
/// #WASMTIME_COMPONENT_KIND_LIST
wasmtime_component_type_t *list;
/// Field used if #wasmtime_component_type_t::kind is
/// #WASMTIME_COMPONENT_KIND_RECORD
wasmtime_component_type_field_vec_t record;
/// Field used if #wasmtime_component_type_t::kind is
/// #WASMTIME_COMPONENT_KIND_TUPLE
wasmtime_component_type_vec_t tuple;
/// Field used if #wasmtime_component_type_t::kind is
/// #WASMTIME_COMPONENT_KIND_VARIANT
wasmtime_component_type_field_vec_t variant;
/// Field used if #wasmtime_component_type_t::kind is
/// #WASMTIME_COMPONENT_KIND_ENUM
wasmtime_component_string_vec_t enumeration;
/// Field used if #wasmtime_component_type_t::kind is
/// #WASMTIME_COMPONENT_KIND_OPTION
wasmtime_component_type_t *option;
/// Field used if #wasmtime_component_type_t::kind is
/// #WASMTIME_COMPONENT_KIND_RESULT
wasmtime_component_type_result_t result;
/// Field used if #wasmtime_component_type_t::kind is
/// #WASMTIME_COMPONENT_KIND_FLAGS
wasmtime_component_string_vec_t flags;
} wasmtime_component_type_payload_t;
/**
* \brief Representation of a component model type
*
* Many kind of types own data, so those need to be properly dropped in
* wasmtime, and therefore should not live on the stack.
*/
typedef struct wasmtime_component_type_t {
/// Discriminant indicating what kind of type it is, and which field of
/// #payload is valid, if any
wasmtime_component_kind_t kind;
/// Container for the type data, if any
wasmtime_component_type_payload_t payload;
} wasmtime_component_type_t;
WASMTIME_COMPONENT_DECLARE_VEC_NEW(type_vec, wasmtime_component_type_t);
#undef WASMTIME_COMPONENT_DECLARE_VEC_NEW
/**
* \brief Creates a new #wasmtime_component_type_t
*
* This is usually used to create inner types, typically as part of an option or
* result. In this case, ownership is given out to the outer value.
*
* In case where a top level #wasmtime_component_type_t is created (for example
* to be passed directly to #wasmtime_component_linker_define_func), then it
* should be deleted with #wasmtime_component_type_delete
*
* \return a pointer to the newly created #wasmtime_component_type_t
*/
wasmtime_component_type_t *wasmtime_component_type_new();
/**
* \brief Deletes a #wasmtime_component_type_t previously created by
* #wasmtime_component_type_new
*
* This should not be called if the type has been given out as part of an outer
* #wasmtime_component_type_t
*
* \param val the #wasmtime_component_type_t to delete
*/
void wasmtime_component_type_delete(wasmtime_component_type_t *ty);

Activity

  1. added
    wasmtime:c-api
    wasm-proposal:component-model
    on Aug 15, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    wasm-proposal:component-modelwasmtime:c-api

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions