Skip to content
Merged
Show file tree
Hide file tree
Changes from 16 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: 0 additions & 1 deletion crates/c-api/include/wasmtime.h
Original file line number Diff line number Diff line change
Expand Up @@ -205,7 +205,6 @@
#include <wasmtime/val.h>
#include <wasmtime/async.h>
#include <wasmtime/component.h>
#include <wasmtime/wasip2.h>
#include <wasmtime/wat.h>
// IWYU pragma: end_exports
// clang-format on
Expand Down
4 changes: 4 additions & 0 deletions crates/c-api/include/wasmtime/component.hh
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,9 @@
#define WASMTIME_COMPONENT_HH

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

#endif // WASMTIME_COMPONENT_HH
11 changes: 6 additions & 5 deletions crates/c-api/include/wasmtime/component/component.hh
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@
* \file wasmtime/component/component.hh
*/

#ifndef WASMTIME_COMPONENT_HH
#define WASMTIME_COMPONENT_HH
#ifndef WASMTIME_COMPONENT_COMPONENT_HH
#define WASMTIME_COMPONENT_COMPONENT_HH

#include <wasmtime/conf.h>

Expand Down Expand Up @@ -39,9 +39,10 @@ class ExportIndex {

std::unique_ptr<wasmtime_component_export_index_t, deleter> ptr;

ExportIndex(wasmtime_component_export_index_t *raw) : ptr(raw) {}

public:
/// \brief Constructs an ExportIndex from the underlying C API struct.
explicit ExportIndex(wasmtime_component_export_index_t *raw) : ptr(raw) {}

/// Copies another index into this one.
ExportIndex(const ExportIndex &other)
: ptr(wasmtime_component_export_index_clone(other.ptr.get())) {}
Expand Down Expand Up @@ -227,4 +228,4 @@ public:

#endif // WASMTIME_FEATURE_COMPONENT_MODEL

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

#ifndef WASMTIME_COMPONENT_FUNC_HH
#define WASMTIME_COMPONENT_FUNC_HH

#include <wasmtime/conf.h>

#ifdef WASMTIME_FEATURE_COMPONENT_MODEL

#include <string_view>
#include <wasmtime/component/func.h>
#include <wasmtime/component/val.hh>
#include <wasmtime/error.hh>
#include <wasmtime/span.hh>
#include <wasmtime/store.hh>

namespace wasmtime {
namespace component {

/**
* \brief Class representing an instantiated WebAssembly component.
*/
class Func {
wasmtime_component_func_t func;

public:
/// \brief Constructs an Func from the underlying C API struct.
explicit Func(const wasmtime_component_func_t &func) : func(func) {}

/// \brief Returns the underlying C API pointer.
const wasmtime_component_func_t *capi() const { return &func; }

/// \brief Invokes this component function with the provided `args` and the
/// results are placed in `results`.
Result<std::monostate> call(Store::Context cx, Span<const Val> args,
Span<Val> results) const {
wasmtime_error_t *error = wasmtime_component_func_call(
&func, cx.capi(), Val::to_capi(args.data()), args.size(),
Val::to_capi(results.data()), results.size());
if (error != nullptr) {
return Error(error);
}
return std::monostate();
}

/**
* \brief Invokes the `post-return` canonical ABI option, if specified.
*/
Result<std::monostate> post_return(Store::Context cx) const {
wasmtime_error_t *error =
wasmtime_component_func_post_return(&func, cx.capi());
if (error != nullptr) {
return Error(error);
}
return std::monostate();
}
};

} // namespace component
} // namespace wasmtime

#endif // WASMTIME_FEATURE_COMPONENT_MODEL

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

#ifndef WASMTIME_COMPONENT_INSTANCE_HH
#define WASMTIME_COMPONENT_INSTANCE_HH

#include <wasmtime/conf.h>

#ifdef WASMTIME_FEATURE_COMPONENT_MODEL

#include <string_view>
#include <wasmtime/component/component.hh>
#include <wasmtime/component/func.hh>
#include <wasmtime/component/instance.h>
#include <wasmtime/store.hh>

namespace wasmtime {
namespace component {

/**
* \brief Class representing an instantiated WebAssembly component.
*/
class Instance {
wasmtime_component_instance_t instance;

public:
/// \brief Constructs an Instance from the underlying C API struct.
explicit Instance(const wasmtime_component_instance_t &inst)
: instance(inst) {}

/// \brief Looks up an exported item from this instance by name, returning the
/// index at which it can be found.
///
/// The returned `ExportIndex` references the underlying item within this
/// instance which can then be accessed via that index specifically. The
/// `instance` provided as an argument to this function is the containing
/// export instance, if any, that `name` is looked up under.
std::optional<ExportIndex> get_export_index(Store::Context cx,
const ExportIndex *instance,
std::string_view name) const {
wasmtime_component_export_index_t *ret =
wasmtime_component_instance_get_export_index(
&this->instance, cx.capi(), instance ? instance->capi() : nullptr,
name.data(), name.size());
if (ret == nullptr) {
return std::nullopt;
}
return ExportIndex(ret);
}

/// \brief Looks up an exported function by its export index.
std::optional<Func> get_func(Store::Context cx,
const ExportIndex &index) const {
wasmtime_component_func_t ret;
bool found = wasmtime_component_instance_get_func(&instance, cx.capi(),
index.capi(), &ret);
if (!found)
return std::nullopt;
return Func(ret);
}

/// \brief Returns the underlying C API pointer.
const wasmtime_component_instance_t *capi() const { return &instance; }
};

} // namespace component
} // namespace wasmtime

#endif // WASMTIME_FEATURE_COMPONENT_MODEL

#endif // WASMTIME_COMPONENT_INSTANCE_H
10 changes: 10 additions & 0 deletions crates/c-api/include/wasmtime/component/linker.h
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,16 @@ typedef struct wasmtime_component_linker_instance_t
WASM_API_EXTERN wasmtime_component_linker_t *
wasmtime_component_linker_new(const wasm_engine_t *engine);

/**
* \brief Configures whether this linker allows later definitions to shadow
* previous definitions.
*
* By default this setting is `false`.
*/
WASM_API_EXTERN void
wasmtime_component_linker_allow_shadowing(wasmtime_component_linker_t *linker,
bool allow);

/**
* \brief Returns the "root instance" of this linker, used to define names into
* the root namespace.
Expand Down
Loading