Skip to content
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
1 change: 1 addition & 0 deletions crates/c-api/include/wasmtime/component.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
#include <wasmtime/component/func.h>
#include <wasmtime/component/instance.h>
#include <wasmtime/component/linker.h>
#include <wasmtime/component/types.h>
#include <wasmtime/component/val.h>

#endif // WASMTIME_COMPONENT_H
1 change: 1 addition & 0 deletions crates/c-api/include/wasmtime/component.hh
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
#include <wasmtime/component/func.hh>
#include <wasmtime/component/instance.hh>
#include <wasmtime/component/linker.hh>
#include <wasmtime/component/types.hh>
#include <wasmtime/component/val.hh>

#endif // WASMTIME_COMPONENT_HH
10 changes: 10 additions & 0 deletions crates/c-api/include/wasmtime/component/component.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
#define WASMTIME_COMPONENT_COMPONENT_H

#include <wasm.h>
#include <wasmtime/component/types/component.h>
#include <wasmtime/conf.h>
#include <wasmtime/error.h>

Expand Down Expand Up @@ -102,6 +103,15 @@ wasmtime_component_deserialize_file(const wasm_engine_t *engine,
WASM_API_EXTERN wasmtime_component_t *
wasmtime_component_clone(const wasmtime_component_t *component);

/**
* \brief Returns the type of this component.
*
* The returned pointer must be deallocatd with
* `wasmtime_component_type_delete`.
*/
WASM_API_EXTERN wasmtime_component_type_t *
wasmtime_component_type(const wasmtime_component_t *component);

/**
* \brief Deletes a #wasmtime_component_t created by #wasmtime_component_new
*
Expand Down
6 changes: 6 additions & 0 deletions crates/c-api/include/wasmtime/component/component.hh
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
#include <string_view>
#include <vector>
#include <wasmtime/component/component.h>
#include <wasmtime/component/types/component.hh>
#include <wasmtime/engine.hh>
#include <wasmtime/error.hh>
#include <wasmtime/span.hh>
Expand Down Expand Up @@ -159,6 +160,11 @@ class Component {
}
return std::nullopt;
};

/// \brief Returns the type of this component.
ComponentType type() const {
return ComponentType(wasmtime_component_type(ptr.get()));
}
};

} // namespace component
Expand Down
9 changes: 9 additions & 0 deletions crates/c-api/include/wasmtime/component/func.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
#ifndef WASMTIME_COMPONENT_FUNC_H
#define WASMTIME_COMPONENT_FUNC_H

#include <wasmtime/component/types/func.h>
#include <wasmtime/component/val.h>
#include <wasmtime/conf.h>
#include <wasmtime/error.h>
Expand Down Expand Up @@ -33,6 +34,14 @@ typedef struct wasmtime_component_func {
uint32_t __private2;
} wasmtime_component_func_t;

/// \brief Returns the type of this function.
///
/// The caller must deallocate the returned pointer with
/// `wasmtime_component_func_type_delete`.
WASM_API_EXTERN wasmtime_component_func_type_t *
wasmtime_component_func_type(const wasmtime_component_func_t *func,
wasmtime_context_t *context);

/**
* \brief Invokes \p func with the \p args given and returns the result.
*
Expand Down
6 changes: 6 additions & 0 deletions crates/c-api/include/wasmtime/component/func.hh
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@

#include <string_view>
#include <wasmtime/component/func.h>
#include <wasmtime/component/types/func.hh>
#include <wasmtime/component/val.hh>
#include <wasmtime/error.hh>
#include <wasmtime/span.hh>
Expand Down Expand Up @@ -54,6 +55,11 @@ public:
}
return std::monostate();
}

/// \brief Returns the type of this function.
FuncType type(Store::Context cx) const {
return FuncType(wasmtime_component_func_type(&func, cx.capi()));
}
};

} // namespace component
Expand Down
5 changes: 3 additions & 2 deletions crates/c-api/include/wasmtime/component/linker.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
#include <wasm.h>
#include <wasmtime/component/component.h>
#include <wasmtime/component/instance.h>
#include <wasmtime/component/types/func.h>
#include <wasmtime/conf.h>
#include <wasmtime/error.h>
#include <wasmtime/module.h>
Expand Down Expand Up @@ -134,8 +135,8 @@ WASM_API_EXTERN wasmtime_error_t *wasmtime_component_linker_instance_add_module(

/// Type of the callback used in #wasmtime_component_linker_instance_add_func
typedef wasmtime_error_t *(*wasmtime_component_func_callback_t)(
void *, wasmtime_context_t *, const wasmtime_component_val_t *, size_t,
wasmtime_component_val_t *, size_t);
void *, wasmtime_context_t *, const wasmtime_component_func_type_t *,
wasmtime_component_val_t *, size_t, wasmtime_component_val_t *, size_t);

/**
* \brief Define a function within this instance.
Expand Down
13 changes: 9 additions & 4 deletions crates/c-api/include/wasmtime/component/linker.hh
Original file line number Diff line number Diff line change
Expand Up @@ -61,15 +61,20 @@ private:
template <typename F>
static wasmtime_error_t *
raw_callback(void *env, wasmtime_context_t *store,
const wasmtime_component_val_t *args, size_t nargs,
const wasmtime_component_func_type_t *ty_const,
wasmtime_component_val_t *args, size_t nargs,
wasmtime_component_val_t *results, size_t nresults) {
static_assert(alignof(Val) == alignof(wasmtime_component_val_t));
static_assert(sizeof(Val) == sizeof(wasmtime_component_val_t));
wasmtime_component_func_type_t *ty =
const_cast<wasmtime_component_func_type_t *>(ty_const);
F *func = reinterpret_cast<F *>(env);
Span<const Val> args_span(Val::from_capi(args), nargs);
Span<Val> args_span(Val::from_capi(args), nargs);
Span<Val> results_span(Val::from_capi(results), nresults);
Result<std::monostate> result =
(*func)(Store::Context(store), args_span, results_span);
(*func)(Store::Context(store), *FuncType::from_capi(&ty), args_span,
results_span);

if (!result) {
return result.err().capi_release();
}
Expand All @@ -85,7 +90,7 @@ public:
template <typename F,
std::enable_if_t<
std::is_invocable_r_v<Result<std::monostate>, F, Store::Context,
Span<const Val>, Span<Val>>,
const FuncType &, Span<Val>, Span<Val>>,
bool> = true>
Result<std::monostate> add_func(std::string_view name, F &&f) {
auto *error = wasmtime_component_linker_instance_add_func(
Expand Down
13 changes: 13 additions & 0 deletions crates/c-api/include/wasmtime/component/types.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
/// \file wasmtime/component/types.h

#ifndef WASMTIME_COMPONENT_TYPES_H
#define WASMTIME_COMPONENT_TYPES_H

#include <wasmtime/component/types/component.h>
#include <wasmtime/component/types/func.h>
#include <wasmtime/component/types/instance.h>
#include <wasmtime/component/types/module.h>
#include <wasmtime/component/types/resource.h>
#include <wasmtime/component/types/val.h>

#endif // WASMTIME_COMPONENT_TYPES_H
12 changes: 12 additions & 0 deletions crates/c-api/include/wasmtime/component/types.hh
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
/// \file wasmtime/component/types.hh

#ifndef WASMTIME_COMPONENT_TYPES_HH
#define WASMTIME_COMPONENT_TYPES_HH

#include <wasmtime/component/types/component.hh>
#include <wasmtime/component/types/func.hh>
#include <wasmtime/component/types/instance.hh>
#include <wasmtime/component/types/module.hh>
#include <wasmtime/component/types/val.hh>

#endif // WASMTIME_COMPONENT_TYPES_HH
162 changes: 162 additions & 0 deletions crates/c-api/include/wasmtime/component/types/component.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,162 @@
/// \file wasmtime/component/types/component.h

#ifndef WASMTIME_COMPONENT_TYPES_COMPONENT_H
#define WASMTIME_COMPONENT_TYPES_COMPONENT_H

#include <wasmtime/conf.h>

#ifdef WASMTIME_FEATURE_COMPONENT_MODEL

#include <wasm.h>
#include <wasmtime/component/types/func.h>
#include <wasmtime/component/types/instance.h>
#include <wasmtime/component/types/module.h>
#include <wasmtime/component/types/resource.h>
#include <wasmtime/component/types/val.h>

#ifdef __cplusplus
extern "C" {
#endif

/// \brief Represents the type of a WebAssembly component.
typedef struct wasmtime_component_type_t wasmtime_component_type_t;

/// \brief Clones a component type.
///
/// The returned pointer must be deallocated wit
/// h`wasmtime_component_type_delete`.
WASM_API_EXTERN
wasmtime_component_type_t *
wasmtime_component_type_clone(const wasmtime_component_type_t *ty);

/// \brief Deallocates a component type.
WASM_API_EXTERN
void wasmtime_component_type_delete(wasmtime_component_type_t *ty);

/// \brief Returns the number of imports of a component type.
WASM_API_EXTERN
size_t wasmtime_component_type_import_count(const wasmtime_component_type_t *ty,
const wasm_engine_t *engine);

/// \brief Retrieves the import with the specified name.
///
/// The returned `wasmtime_component_item_t` must be deallocated with
/// `wasmtime_component_item_delete`.
WASM_API_EXTERN
bool wasmtime_component_type_import_get(const wasmtime_component_type_t *ty,
const wasm_engine_t *engine,
const char *name, size_t name_len,
struct wasmtime_component_item_t *ret);

/// \brief Retrieves the nth import.
///
/// The returned `wasmtime_component_item_t` must be deallocated with
/// `wasmtime_component_item_delete`.
WASM_API_EXTERN
bool wasmtime_component_type_import_nth(
const wasmtime_component_type_t *ty, const wasm_engine_t *engine,
size_t nth, const char **name_ret, size_t *name_len_ret,
struct wasmtime_component_item_t *type_ret);

/// \brief Returns the number of exports of a component type.
WASM_API_EXTERN
size_t wasmtime_component_type_export_count(const wasmtime_component_type_t *ty,
const wasm_engine_t *engine);

/// \brief Retrieves the export with the specified name.
///
/// The returned `wasmtime_component_item_t` must be deallocated with
/// `wasmtime_component_item_delete`.
WASM_API_EXTERN
bool wasmtime_component_type_export_get(const wasmtime_component_type_t *ty,
const wasm_engine_t *engine,
const char *name, size_t name_len,
struct wasmtime_component_item_t *ret);

/// \brief Retrieves the nth export.
///
/// The returned `wasmtime_component_item_t` must be deallocated with
/// `wasmtime_component_item_delete`.
WASM_API_EXTERN
bool wasmtime_component_type_export_nth(
const wasmtime_component_type_t *ty, const wasm_engine_t *engine,
size_t nth, const char **name_ret, size_t *name_len_ret,
struct wasmtime_component_item_t *type_ret);

/// \brief Value of #wasmtime_component_item_kind_t meaning that
/// #wasmtime_component_item_t is a component.
#define WASMTIME_COMPONENT_ITEM_COMPONENT 0
/// \brief Value of #wasmtime_component_item_kind_t meaning that
/// #wasmtime_component_item_t is a component instance.
#define WASMTIME_COMPONENT_ITEM_COMPONENT_INSTANCE 1
/// \brief Value of #wasmtime_component_item_kind_t meaning that
/// #wasmtime_component_item_t is a module.
#define WASMTIME_COMPONENT_ITEM_MODULE 2
/// \brief Value of #wasmtime_component_item_kind_t meaning that
/// #wasmtime_component_item_t is a component function.
#define WASMTIME_COMPONENT_ITEM_COMPONENT_FUNC 3
/// \brief Value of #wasmtime_component_item_kind_t meaning that
/// #wasmtime_component_item_t is a resource.
#define WASMTIME_COMPONENT_ITEM_RESOURCE 4
/// \brief Value of #wasmtime_component_item_kind_t meaning that
/// #wasmtime_component_item_t is a core function.
#define WASMTIME_COMPONENT_ITEM_CORE_FUNC 5
/// \brief Value of #wasmtime_component_item_kind_t meaning that
/// #wasmtime_component_item_t is a type.
#define WASMTIME_COMPONENT_ITEM_TYPE 6

/// \brief Discriminant used in #wasmtime_component_item_t::kind
typedef uint8_t wasmtime_component_item_kind_t;

/// \brief Represents a single item in a component's import or export list.
typedef union wasmtime_component_item_union {
/// Field used if #wasmtime_component_item_t::kind is
/// #WASMTIME_COMPONENT_ITEM_COMPONENT
wasmtime_component_type_t *component;
/// Field used if #wasmtime_component_item_t::kind is
/// #WASMTIME_COMPONENT_ITEM_COMPONENT_INSTANCE
wasmtime_component_instance_type_t *component_instance;
/// Field used if #wasmtime_component_item_t::kind is
/// #WASMTIME_COMPONENT_ITEM_MODULE
wasmtime_module_type_t *module;
/// Field used if #wasmtime_component_item_t::kind is
/// #WASMTIME_COMPONENT_ITEM_COMPONENT_FUNC
wasmtime_component_func_type_t *component_func;
/// Field used if #wasmtime_component_item_t::kind is
/// #WASMTIME_COMPONENT_ITEM_RESOURCE
wasmtime_component_resource_type_t *resource;
/// Field used if #wasmtime_component_item_t::kind is
/// #WASMTIME_COMPONENT_ITEM_CORE_FUNC
wasm_functype_t *core_func;
/// Field used if #wasmtime_component_item_t::kind is
/// #WASMTIME_COMPONENT_ITEM_TYPE
wasmtime_component_valtype_t type;
} wasmtime_component_item_union_t;

/// \brief Represents a single item in a component's import or export list.
typedef struct wasmtime_component_item_t {
/// The type discriminant for the `of` union.
wasmtime_component_item_kind_t kind;
/// The actual item.
wasmtime_component_item_union_t of;
} wasmtime_component_item_t;

/// \brief Clones a component item.
///
/// The returned pointer must be deallocated with
/// `wasmtime_component_item_delete`.
WASM_API_EXTERN
void wasmtime_component_item_clone(const wasmtime_component_item_t *item,
wasmtime_component_item_t *out);

/// \brief Deallocates a component item.
WASM_API_EXTERN
void wasmtime_component_item_delete(wasmtime_component_item_t *ptr);

#ifdef __cplusplus
} // extern "C"
#endif

#endif // WASMTIME_FEATURE_COMPONENT_MODEL

#endif // WASMTIME_COMPONENT_TYPES_COMPONENT_H
Loading
Loading