Skip to content

A lightweight WebAssembly runtime that is fast, secure, and standards-compliant

Simplify C++ binding definitions (#11936)

Alex Crichton311d023a2e65
diff --git a/crates/c-api/include/wasmtime/component/component.hh b/crates/c-api/include/wasmtime/component/component.hh
index 234d67ca99..fc08945eef 100644
--- a/crates/c-api/include/wasmtime/component/component.hh
+++ b/crates/c-api/include/wasmtime/component/component.hh
@@ -29,76 +29,14 @@ namespace component {
* instances.
*/
class ExportIndex {
- friend class Component;
-
- struct deleter {
- void operator()(wasmtime_component_export_index_t *p) const {
- wasmtime_component_export_index_delete(p);
- }
- };
-
- std::unique_ptr<wasmtime_component_export_index_t, deleter> ptr;
-
-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())) {}
- /// Copies another index into this one.
- ExportIndex &operator=(const ExportIndex &other) {
- ptr.reset(wasmtime_component_export_index_clone(other.ptr.get()));
- return *this;
- }
-
- ~ExportIndex() = default;
- /// Moves resources from another component into this one.
- ExportIndex(ExportIndex &&other) = default;
- /// Moves resources from another component into this one.
- ExportIndex &operator=(ExportIndex &&other) = default;
-
- /// \brief Returns the underlying C API pointer.
- const wasmtime_component_export_index_t *capi() const { return ptr.get(); }
-
- /// \brief Returns the underlying C API pointer.
- wasmtime_component_export_index_t *capi() { return ptr.get(); }
+ WASMTIME_CLONE_WRAPPER(ExportIndex, wasmtime_component_export_index);
};
/**
* \brief Representation of a compiled WebAssembly component.
*/
class Component {
- struct deleter {
- void operator()(wasmtime_component_t *p) const {
- wasmtime_component_delete(p);
- }
- };
-
- std::unique_ptr<wasmtime_component_t, deleter> ptr;
-
- Component(wasmtime_component_t *raw) : ptr(raw) {}
-
-public:
- /// Copies another component into this one.
- Component(const Component &other)
- : ptr(wasmtime_component_clone(other.ptr.get())) {}
- /// Copies another component into this one.
- Component &operator=(const Component &other) {
- ptr.reset(wasmtime_component_clone(other.ptr.get()));
- return *this;
- }
- ~Component() = default;
- /// Moves resources from another component into this one.
- Component(Component &&other) = default;
- /// Moves resources from another component into this one.
- Component &operator=(Component &&other) = default;
-
- /// \brief Returns the underlying C API pointer.
- const wasmtime_component_t *capi() const { return ptr.get(); }
-
- /// \brief Returns the underlying C API pointer.
- wasmtime_component_t *capi() { return ptr.get(); }
+ WASMTIME_CLONE_WRAPPER(Component, wasmtime_component);
#ifdef WASMTIME_FEATURE_COMPILER
/**
diff --git a/crates/c-api/include/wasmtime/component/linker.hh b/crates/c-api/include/wasmtime/component/linker.hh
index fcf56068b8..5ddbcc2016 100644
--- a/crates/c-api/include/wasmtime/component/linker.hh
+++ b/crates/c-api/include/wasmtime/component/linker.hh
@@ -26,24 +26,7 @@ namespace component {
* `Linker` can also be used to link in WASI functions to instantiate a module.
*/
class LinkerInstance {
- struct deleter {
- void operator()(wasmtime_component_linker_instance_t *p) const {
- wasmtime_component_linker_instance_delete(p);
- }
- };
-
- std::unique_ptr<wasmtime_component_linker_instance_t, deleter> ptr;
-
-public:
- /// Creates a new linker instance from the given C API pointer.
- explicit LinkerInstance(wasmtime_component_linker_instance_t *ptr)
- : ptr(ptr) {}
-
- /// \brief Returns the underlying C API pointer.
- const wasmtime_component_linker_instance_t *capi() const { return ptr.get(); }
-
- /// \brief Returns the underlying C API pointer.
- wasmtime_component_linker_instance_t *capi() { return ptr.get(); }
+ WASMTIME_OWN_WRAPPER(LinkerInstance, wasmtime_component_linker_instance);
/**
* \brief Adds a module to this linker instance under the specified name.
@@ -88,7 +71,7 @@ private:
Result<std::monostate> result =
(*func)(Store::Context(store), args_span, results_span);
if (!result) {
- return result.err().release();
+ return result.err().capi_release();
}
return nullptr;
}
@@ -127,7 +110,7 @@ private:
F *func = reinterpret_cast<F *>(env);
Result<std::monostate> result = (*func)(Store::Context(store), rep);
if (!result) {
- return result.err().release();
+ return result.err().capi_release();
}
return nullptr;
}
@@ -160,15 +143,8 @@ public:
* \brief Class used to instantiate a `Component` into an instance.
*/
class Linker {
- struct deleter {
- void operator()(wasmtime_component_linker_t *p) const {
- wasmtime_component_linker_delete(p);
- }
- };
-
- std::unique_ptr<wasmtime_component_linker_t, deleter> ptr;
+ WASMTIME_OWN_WRAPPER(Linker, wasmtime_component_linker);
-public:
/// Creates a new linker which will instantiate in the given engine.
explicit Linker(Engine &engine)
: ptr(wasmtime_component_linker_new(engine.capi())) {}
@@ -234,12 +210,6 @@ public:
return std::monostate();
}
#endif // WASMTIME_FEATURE_WASI
-
- /// \brief Returns the underlying C API pointer.
- const wasmtime_component_linker_t *capi() const { return ptr.get(); }
-
- /// \brief Returns the underlying C API pointer.
- wasmtime_component_linker_t *capi() { return ptr.get(); }
};
} // namespace component
diff --git a/crates/c-api/include/wasmtime/component/val.hh b/crates/c-api/include/wasmtime/component/val.hh
index 2a51d30fb2..1f0dfe558c 100644
--- a/crates/c-api/include/wasmtime/component/val.hh
+++ b/crates/c-api/include/wasmtime/component/val.hh
@@ -419,37 +419,13 @@ public:
/// Class representing a component model `resource` value which is either a
/// guest or host-defined resource.
class ResourceType {
- struct deleter {
- void operator()(wasmtime_component_resource_type_t *p) const {
- wasmtime_component_resource_type_delete(p);
- }
- };
-
- std::unique_ptr<wasmtime_component_resource_type_t, deleter> ptr;
-
-public:
- /// \brief Takes ownership of `raw` and wraps it with this class.
- explicit ResourceType(wasmtime_component_resource_type_t *raw) : ptr(raw) {}
+ WASMTIME_CLONE_WRAPPER(ResourceType, wasmtime_component_resource_type);
/// \brief Creates a new host resource type with the specified `ty`
/// identifier.
explicit ResourceType(uint32_t ty)
: ptr(wasmtime_component_resource_type_new_host(ty)) {}
- /// Copies another resource into this one.
- ResourceType(const ResourceType &other)
- : ptr(wasmtime_component_resource_type_clone(other.ptr.get())) {}
- /// Copies another resource into this one.
- ResourceType &operator=(const ResourceType &other) {
- ptr.reset(wasmtime_component_resource_type_clone(other.ptr.get()));
- return *this;
- }
- ~ResourceType() = default;
- /// Moves resources from another resource into this one.
- ResourceType(ResourceType &&other) = default;
- /// Moves resources from another resource into this one.
- ResourceType &operator=(ResourceType &&other) = default;
-
/// \brief Compares two resource types for equality.
bool operator==(const ResourceType &b) const {
return wasmtime_component_resource_type_equal(capi(), b.capi());
@@ -459,12 +435,6 @@ public:
bool operator!=(const ResourceType &b) const {
return !wasmtime_component_resource_type_equal(capi(), b.capi());
}
-
- /// \brief Returns the underlying C API pointer.
- const wasmtime_component_resource_type_t *capi() const { return ptr.get(); }
-
- /// \brief Returns the underlying C API pointer.
- wasmtime_component_resource_type_t *capi() { return ptr.get(); }
};
class ResourceHost;
@@ -472,40 +442,7 @@ class ResourceHost;
/// Class representing a component model `resource` value which is either a
/// guest or host-defined resource.
class ResourceAny {
- struct deleter {
- void operator()(wasmtime_component_resource_any_t *p) const {
- wasmtime_component_resource_any_delete(p);
- }
- };
-
- std::unique_ptr<wasmtime_component_resource_any_t, deleter> ptr;
-
-public:
- /// \brief Takes ownership of `raw` and wraps it with this class.
- explicit ResourceAny(wasmtime_component_resource_any_t *raw) : ptr(raw) {}
-
- /// Copies another resource into this one.
- ResourceAny(const ResourceAny &other)
- : ptr(wasmtime_component_resource_any_clone(other.ptr.get())) {}
- /// Copies another resource into this one.
- ResourceAny &operator=(const ResourceAny &other) {
- ptr.reset(wasmtime_component_resource_any_clone(other.ptr.get()));
- return *this;
- }
- ~ResourceAny() = default;
- /// Moves resources from another resource into this one.
- ResourceAny(ResourceAny &&other) = default;
- /// Moves resources from another resource into this one.
- ResourceAny &operator=(ResourceAny &&other) = default;
-
- /// \brief Returns the underlying C API pointer.
- const wasmtime_component_resource_any_t *capi() const { return ptr.get(); }
-
- /// \brief Returns the underlying C API pointer.
- wasmtime_component_resource_any_t *capi() { return ptr.get(); }
-
- /// \brief Gives up ownership of the underlying C pointer to the caller.
- wasmtime_component_resource_any_t *capi_release() { return ptr.release(); }
+ WASMTIME_CLONE_WRAPPER(ResourceAny, wasmtime_component_resource_any);
/// \brief Returns whether this resource is owned.
bool owned() const { return wasmtime_component_resource_any_owned(capi()); }
@@ -529,51 +466,12 @@ public:
/// \brief Attempts to convert this resource to a host-defined resource.
Result<ResourceHost> to_host(Store::Context cx) const;
-
- /**
- * Converts the raw C API representation to this class without taking
- * ownership.
- */
- static const ResourceAny *
- from_capi(wasmtime_component_resource_any_t *const *capi) {
- return reinterpret_cast<const ResourceAny *>(capi);
- }
};
/// Class representing a component model `resource` value which is a host-owned
/// resource.
class ResourceHost {
- struct deleter {
- void operator()(wasmtime_component_resource_host_t *p) const {
- wasmtime_component_resource_host_delete(p);
- }
- };
-
- std::unique_ptr<wasmtime_component_resource_host_t, deleter> ptr;
-
-public:
- /// \brief Takes ownership of `raw` and wraps it with this class.
- explicit ResourceHost(wasmtime_component_resource_host_t *raw) : ptr(raw) {}
-
- /// Copies another resource into this one.
- ResourceHost(const ResourceHost &other)
- : ptr(wasmtime_component_resource_host_clone(other.ptr.get())) {}
- /// Copies another resource into this one.
- ResourceHost &operator=(const ResourceHost &other) {
- ptr.reset(wasmtime_component_resource_host_clone(other.ptr.get()));
- return *this;
- }
- ~ResourceHost() = default;
- /// Moves resources from another resource into this one.
- ResourceHost(ResourceHost &&other) = default;
- /// Moves resources from another resource into this one.
- ResourceHost &operator=(ResourceHost &&other) = default;
-
- /// \brief Returns the underlying C API pointer.
- const wasmtime_component_resource_host_t *capi() const { return ptr.get(); }
-
- /// \brief Returns the underlying C API pointer.
- wasmtime_component_resource_host_t *capi() { return ptr.get(); }
+ WASMTIME_CLONE_WRAPPER(ResourceHost, wasmtime_component_resource_host);
/// \brief Creates a new host-defined resource with the specified `owned`,
/// `rep`, and `ty` identifiers.
diff --git a/crates/c-api/include/wasmtime/config.hh b/crates/c-api/include/wasmtime/config.hh
index 6f66dd6de1..e510e5b1b2 100644
--- a/crates/c-api/include/wasmtime/config.hh
+++ b/crates/c-api/include/wasmtime/config.hh
@@ -8,6 +8,7 @@
#include <wasmtime/conf.h>
#include <wasmtime/config.h>
#include <wasmtime/error.hh>
+#include <wasmtime/helpers.hh>
#include <wasmtime/types/memory.hh>
namespace wasmtime {
@@ -50,17 +51,9 @@ enum class ProfilingStrategy {
* documentation](https://docs.wasmtime.dev/api/wasmtime/struct.PoolingAllocationConfig.html).
*/
class PoolAllocationConfig {
- friend class Config;
+ WASMTIME_OWN_WRAPPER(PoolAllocationConfig,
+ wasmtime_pooling_allocation_config);
- struct deleter {
- void operator()(wasmtime_pooling_allocation_config_t *p) const {
- wasmtime_pooling_allocation_config_delete(p);
- }
- };
-
- std::unique_ptr<wasmtime_pooling_allocation_config_t, deleter> ptr;
-
-public:
PoolAllocationConfig() : ptr(wasmtime_pooling_allocation_config_new()) {}
/// \brief Configures the maximum number of “unused warm slots” to retain in
@@ -251,15 +244,8 @@ public:
* documentation](https://docs.wasmtime.dev/api/wasmtime/struct.Config.html).
*/
class Config {
- friend class Engine;
+ WASMTIME_OWN_WRAPPER(Config, wasm_config);
- struct deleter {
- void operator()(wasm_config_t *p) const { wasm_config_delete(p); }
- };
-
- std::unique_ptr<wasm_config_t, deleter> ptr;
-
-public:
/// \brief Creates configuration with all the default settings.
Config() : ptr(wasm_config_new()) {}
@@ -578,7 +564,7 @@ private:
M *memory = reinterpret_cast<M *>(env);
Result<std::monostate> result = memory->grow_memory(new_size);
if (!result)
- return result.err().release();
+ return result.err().capi_release();
return nullptr;
}
@@ -594,7 +580,7 @@ private:
creator->new_memory(MemoryType::Ref(ty), minimum, maximum,
reserved_size_in_bytes, guard_size_in_bytes);
if (!result) {
- return result.err().release();
+ return result.err().capi_release();
}
Memory memory = result.unwrap();
memory_ret->env = std::make_unique<Memory>(memory).release();
@@ -623,7 +609,7 @@ public:
///
/// https://docs.wasmtime.dev/api/wasmtime/struct.Config.html#method.allocation_strategy
void pooling_allocation_strategy(const PoolAllocationConfig &config) {
- wasmtime_pooling_allocation_strategy_set(ptr.get(), config.ptr.get());
+ wasmtime_pooling_allocation_strategy_set(ptr.get(), config.capi());
}
#endif // WASMTIME_FEATURE_POOLING_ALLOCATOR
};
diff --git a/crates/c-api/include/wasmtime/engine.hh b/crates/c-api/include/wasmtime/engine.hh
index eab2137e27..ab9f8ac2fc 100644
--- a/crates/c-api/include/wasmtime/engine.hh
+++ b/crates/c-api/include/wasmtime/engine.hh
@@ -8,6 +8,7 @@
#include <memory>
#include <wasmtime/config.hh>
#include <wasmtime/engine.h>
+#include <wasmtime/helpers.hh>
namespace wasmtime {
@@ -19,35 +20,16 @@ namespace wasmtime {
* operations in Wasmtime.
*/
class Engine {
- friend class Store;
- friend class Module;
- friend class Linker;
+/// bridging wasm.h vs wasmtime.h conventions
+#define wasm_engine_clone wasmtime_engine_clone
+ WASMTIME_CLONE_WRAPPER(Engine, wasm_engine);
+#undef wasm_engine_clone
- struct deleter {
- void operator()(wasm_engine_t *p) const { wasm_engine_delete(p); }
- };
-
- std::unique_ptr<wasm_engine_t, deleter> ptr;
-
-public:
/// \brief Creates an engine with default compilation settings.
Engine() : ptr(wasm_engine_new()) {}
/// \brief Creates an engine with the specified compilation settings.
explicit Engine(Config config)
- : ptr(wasm_engine_new_with_config(config.ptr.release())) {}
-
- /// Copies another engine into this one.
- Engine(const Engine &other) : ptr(wasmtime_engine_clone(other.ptr.get())) {}
- /// Copies another engine into this one.
- Engine &operator=(const Engine &other) {
- ptr.reset(wasmtime_engine_clone(other.ptr.get()));
- return *this;
- }
- ~Engine() = default;
- /// Moves resources from another engine into this one.
- Engine(Engine &&other) = default;
- /// Moves resources from another engine into this one.
- Engine &operator=(Engine &&other) = default;
+ : ptr(wasm_engine_new_with_config(config.capi_release())) {}
/// \brief Increments the current epoch which may result in interrupting
/// currently executing WebAssembly in connected stores if the epoch is now
@@ -56,12 +38,6 @@ public:
/// \brief Returns whether this engine is using Pulley for execution.
void is_pulley() const { wasmtime_engine_is_pulley(ptr.get()); }
-
- /// \brief Returns the underlying C API pointer.
- const wasm_engine_t *capi() const { return ptr.get(); }
-
- /// \brief Returns the underlying C API pointer.
- wasm_engine_t *capi() { return ptr.get(); }
};
} // namespace wasmtime
diff --git a/crates/c-api/include/wasmtime/error.hh b/crates/c-api/include/wasmtime/error.hh
index 2d89fc6bad..91c9bae12a 100644
--- a/crates/c-api/include/wasmtime/error.hh
+++ b/crates/c-api/include/wasmtime/error.hh
@@ -11,6 +11,7 @@
#include <string>
#include <variant>
#include <wasmtime/error.h>
+#include <wasmtime/helpers.hh>
namespace wasmtime {
@@ -23,17 +24,7 @@ class Trace;
* description of the error that occurred.
*/
class Error {
- struct deleter {
- void operator()(wasmtime_error_t *p) const { wasmtime_error_delete(p); }
- };
-
- std::unique_ptr<wasmtime_error_t, deleter> ptr;
-
-public:
- /// \brief Creates an error from the raw C API representation
- ///
- /// Takes ownership of the provided `error`.
- Error(wasmtime_error_t *error) : ptr(error) {}
+ WASMTIME_OWN_WRAPPER(Error, wasmtime_error);
/// \brief Creates an error with the provided message.
Error(const std::string &s) : ptr(wasmtime_error_new(s.c_str())) {}
@@ -61,9 +52,6 @@ public:
///
/// Note that the `trace` cannot outlive this error object.
Trace trace() const;
-
- /// Release ownership of this error, acquiring the underlying C raw pointer.
- wasmtime_error_t *release() { return ptr.release(); }
};
/// \brief Used to print an error.
diff --git a/crates/c-api/include/wasmtime/func.hh b/crates/c-api/include/wasmtime/func.hh
index 1dc4e05356..84ec28e1d0 100644
--- a/crates/c-api/include/wasmtime/func.hh
+++ b/crates/c-api/include/wasmtime/func.hh
@@ -355,7 +355,7 @@ class Func {
Result<std::monostate, Trap> result =
(*func)(Caller(caller), args_span, results_span);
if (!result) {
- return result.err().ptr.release();
+ return result.err().capi_release();
}
return nullptr;
}
@@ -371,7 +371,7 @@ class Func {
F *func = reinterpret_cast<F *>(env); // NOLINT
auto trap = HostFunc::invoke(*func, cx, args_and_results);
if (trap) {
- return trap->ptr.release();
+ return trap->capi_release();
}
return nullptr;
}
diff --git a/crates/c-api/include/wasmtime/helpers.hh b/crates/c-api/include/wasmtime/helpers.hh
new file mode 100644
index 0000000000..26f0d2f73b
--- /dev/null
+++ b/crates/c-api/include/wasmtime/helpers.hh
@@ -0,0 +1,79 @@
+#ifndef WASMTIME_HELPERS_HH
+#define WASMTIME_HELPERS_HH
+
+#include <memory>
+
+#define WASMTIME_OWN_WRAPPER(name, capi_type) \
+public: \
+ /** \
+ * \brief Non-owning reference to an instance of this type. \
+ */ \
+ using Raw = capi_type##_t; \
+ \
+private: \
+ struct deleter { \
+ void operator()(Raw *p) const { capi_type##_delete(p); } \
+ }; \
+ \
+ std::unique_ptr<Raw, deleter> ptr; \
+ \
+public: \
+ /** \
+ * \brief Takes ownership of `raw` and wraps it with this class. \
+ */ \
+ explicit name(Raw *raw) : ptr(raw) {} \
+ \
+ ~name() = default; \
+ \
+ /** \
+ * \brief Moves type information from another type into this one. \
+ */ \
+ name(name &&other) = default; \
+ \
+ /** \
+ * \brief Moves type information from another type into this one. \
+ */ \
+ name &operator=(name &&other) = default; \
+ \
+ /** \
+ * \brief Returns the underlying C API pointer. \
+ */ \
+ const Raw *capi() const { return ptr.get(); } \
+ \
+ /** \
+ * \brief Returns the underlying C API pointer. \
+ */ \
+ Raw *capi() { return ptr.get(); } \
+ \
+ /** \
+ * \brief Releases the underlying C API pointer. \
+ */ \
+ Raw *capi_release() { return ptr.release(); } \
+ \
+ /** \
+ * Converts the raw C API representation to this class without taking \
+ * ownership. \
+ */ \
+ static const name *from_capi(Raw *const *capi) { \
+ static_assert(sizeof(name) == sizeof(void *)); \
+ return reinterpret_cast<const name *>(capi); \
+ }
+
+#define WASMTIME_CLONE_WRAPPER(name, capi_type) \
+ WASMTIME_OWN_WRAPPER(name, capi_type) \
+ \
+public: \
+ /** \
+ * \brief Copies another type into this one. \
+ */ \
+ name(const name &other) : ptr(capi_type##_clone(other.ptr.get())) {} \
+ \
+ /** \
+ * \brief Copies another type into this one. \
+ */ \
+ name &operator=(const name &other) { \
+ ptr.reset(capi_type##_clone(other.ptr.get())); \
+ return *this; \
+ }
+
+#endif // WASMTIME_HELPERS_HH
diff --git a/crates/c-api/include/wasmtime/instance.hh b/crates/c-api/include/wasmtime/instance.hh
index 22723e0ef3..1aa75a060f 100644
--- a/crates/c-api/include/wasmtime/instance.hh
+++ b/crates/c-api/include/wasmtime/instance.hh
@@ -64,7 +64,7 @@ public:
}
wasmtime_instance_t instance;
wasm_trap_t *trap = nullptr;
- auto *error = wasmtime_instance_new(cx.ptr, m.ptr.get(), raw_imports.data(),
+ auto *error = wasmtime_instance_new(cx.ptr, m.capi(), raw_imports.data(),
raw_imports.size(), &instance, &trap);
if (error != nullptr) {
return TrapError(Error(error));
diff --git a/crates/c-api/include/wasmtime/linker.hh b/crates/c-api/include/wasmtime/linker.hh
index be11c3e337..d0caca235e 100644
--- a/crates/c-api/include/wasmtime/linker.hh
+++ b/crates/c-api/include/wasmtime/linker.hh
@@ -8,6 +8,7 @@
#include <wasmtime/engine.hh>
#include <wasmtime/error.hh>
#include <wasmtime/extern.hh>
+#include <wasmtime/helpers.hh>
#include <wasmtime/instance.hh>
#include <wasmtime/linker.h>
#include <wasmtime/store.hh>
@@ -23,16 +24,10 @@ namespace wasmtime {
* `Linker` can also be used to link in WASI functions to instantiate a module.
*/
class Linker {
- struct deleter {
- void operator()(wasmtime_linker_t *p) const { wasmtime_linker_delete(p); }
- };
+ WASMTIME_OWN_WRAPPER(Linker, wasmtime_linker);
- std::unique_ptr<wasmtime_linker_t, deleter> ptr;
-
-public:
/// Creates a new linker which will instantiate in the given engine.
- explicit Linker(Engine &engine)
- : ptr(wasmtime_linker_new(engine.ptr.get())) {}
+ explicit Linker(Engine &engine) : ptr(wasmtime_linker_new(engine.capi())) {}
/// Configures whether shadowing previous names is allowed or not.
///
@@ -86,7 +81,7 @@ public:
TrapResult<Instance> instantiate(Store::Context cx, const Module &m) {
wasmtime_instance_t instance;
wasm_trap_t *trap = nullptr;
- auto *error = wasmtime_linker_instantiate(ptr.get(), cx.ptr, m.ptr.get(),
+ auto *error = wasmtime_linker_instantiate(ptr.get(), cx.ptr, m.capi(),
&instance, &trap);
if (error != nullptr) {
return TrapError(Error(error));
@@ -102,7 +97,7 @@ public:
Result<std::monostate> module(Store::Context cx, std::string_view name,
const Module &m) {
auto *error = wasmtime_linker_module(ptr.get(), cx.ptr, name.data(),
- name.size(), m.ptr.get());
+ name.size(), m.capi());
if (error != nullptr) {
return Error(error);
}
@@ -187,7 +182,7 @@ public:
/// as a trap.
Result<std::monostate> define_unknown_imports_as_traps(Module &module) {
auto *error = wasmtime_linker_define_unknown_imports_as_traps(
- ptr.get(), module.ptr.get());
+ ptr.get(), module.capi());
if (error != nullptr) {
return Error(error);
}
@@ -200,7 +195,7 @@ public:
Result<std::monostate>
define_unknown_imports_as_default_values(Store::Context cx, Module &module) {
auto *error = wasmtime_linker_define_unknown_imports_as_default_values(
- ptr.get(), cx.ptr, module.ptr.get());
+ ptr.get(), cx.ptr, module.capi());
if (error != nullptr) {
return Error(error);
}
diff --git a/crates/c-api/include/wasmtime/module.hh b/crates/c-api/include/wasmtime/module.hh
index e3edccd91e..4a28439de8 100644
--- a/crates/c-api/include/wasmtime/module.hh
+++ b/crates/c-api/include/wasmtime/module.hh
@@ -8,6 +8,7 @@
#include <memory>
#include <string_view>
#include <wasmtime/engine.hh>
+#include <wasmtime/helpers.hh>
#include <wasmtime/module.h>
#include <wasmtime/span.hh>
#include <wasmtime/types/export.hh>
@@ -25,31 +26,7 @@ namespace wasmtime {
* argument to other APIs to instantiate it.
*/
class Module {
- friend class Store;
- friend class Instance;
- friend class Linker;
-
- struct deleter {
- void operator()(wasmtime_module_t *p) const { wasmtime_module_delete(p); }
- };
-
- std::unique_ptr<wasmtime_module_t, deleter> ptr;
-
- Module(wasmtime_module_t *raw) : ptr(raw) {}
-
-public:
- /// Copies another module into this one.
- Module(const Module &other) : ptr(wasmtime_module_clone(other.ptr.get())) {}
- /// Copies another module into this one.
- Module &operator=(const Module &other) {
- ptr.reset(wasmtime_module_clone(other.ptr.get()));
- return *this;
- }
- ~Module() = default;
- /// Moves resources from another module into this one.
- Module(Module &&other) = default;
- /// Moves resources from another module into this one.
- Module &operator=(Module &&other) = default;
+ WASMTIME_CLONE_WRAPPER(Module, wasmtime_module);
#ifdef WASMTIME_FEATURE_COMPILER
/**
@@ -80,7 +57,7 @@ public:
static Result<Module> compile(Engine &engine, Span<uint8_t> wasm) {
wasmtime_module_t *ret = nullptr;
auto *error =
- wasmtime_module_new(engine.ptr.get(), wasm.data(), wasm.size(), &ret);
+ wasmtime_module_new(engine.capi(), wasm.data(), wasm.size(), &ret);
if (error != nullptr) {
return Error(error);
}
@@ -95,7 +72,7 @@ public:
*/
static Result<std::monostate> validate(Engine &engine, Span<uint8_t> wasm) {
auto *error =
- wasmtime_module_validate(engine.ptr.get(), wasm.data(), wasm.size());
+ wasmtime_module_validate(engine.capi(), wasm.data(), wasm.size());
if (error != nullptr) {
return Error(error);
}
@@ -117,7 +94,7 @@ public:
*/
static Result<Module> deserialize(Engine &engine, Span<uint8_t> wasm) {
wasmtime_module_t *ret = nullptr;
- auto *error = wasmtime_module_deserialize(engine.ptr.get(), wasm.data(),
+ auto *error = wasmtime_module_deserialize(engine.capi(), wasm.data(),
wasm.size(), &ret);
if (error != nullptr) {
return Error(error);
@@ -141,7 +118,7 @@ public:
const std::string &path) {
wasmtime_module_t *ret = nullptr;
auto *error =
- wasmtime_module_deserialize_file(engine.ptr.get(), path.c_str(), &ret);
+ wasmtime_module_deserialize_file(engine.capi(), path.c_str(), &ret);
if (error != nullptr) {
return Error(error);
}
@@ -183,12 +160,6 @@ public:
return ret;
}
#endif // WASMTIME_FEATURE_COMPILER
-
- /// \brief Returns the underlying C API pointer.
- const wasmtime_module_t *capi() const { return ptr.get(); }
-
- /// \brief Returns the underlying C API pointer.
- wasmtime_module_t *capi() { return ptr.get(); }
};
} // namespace wasmtime
diff --git a/crates/c-api/include/wasmtime/store.hh b/crates/c-api/include/wasmtime/store.hh
index f4614c3ce5..ec56126196 100644
--- a/crates/c-api/include/wasmtime/store.hh
+++ b/crates/c-api/include/wasmtime/store.hh
@@ -11,6 +11,7 @@
#include <wasmtime/conf.h>
#include <wasmtime/engine.hh>
#include <wasmtime/error.hh>
+#include <wasmtime/helpers.hh>
#include <wasmtime/store.h>
#include <wasmtime/wasi.hh>
@@ -39,12 +40,9 @@ enum class DeadlineKind {
* will be deallocated when the `Store` is deallocated.
*/
class Store {
- struct deleter {
- void operator()(wasmtime_store_t *p) const { wasmtime_store_delete(p); }
- };
-
- std::unique_ptr<wasmtime_store_t, deleter> ptr;
+ WASMTIME_OWN_WRAPPER(Store, wasmtime_store);
+private:
static void finalizer(void *ptr) {
std::unique_ptr<std::any> _ptr(static_cast<std::any *>(ptr));
}
@@ -52,7 +50,7 @@ class Store {
public:
/// Creates a new `Store` within the provided `Engine`.
explicit Store(Engine &engine)
- : ptr(wasmtime_store_new(engine.ptr.get(), nullptr, finalizer)) {}
+ : ptr(wasmtime_store_new(engine.capi(), nullptr, finalizer)) {}
/**
* \brief An interior pointer into a `Store`.
@@ -140,7 +138,7 @@ public:
/// `Linker::define_wasi` because otherwise no host functions will use the
/// WASI state.
Result<std::monostate> set_wasi(WasiConfig config) {
- auto *error = wasmtime_context_set_wasi(ptr, config.ptr.release());
+ auto *error = wasmtime_context_set_wasi(ptr, config.capi_release());
if (error != nullptr) {
return Error(error);
}
@@ -231,12 +229,6 @@ public:
/// GC-managed objects, if any are available.
void gc() { context().gc(); }
- /// \brief Returns the underlying C API pointer.
- const wasmtime_store_t *capi() const { return ptr.get(); }
-
- /// \brief Returns the underlying C API pointer.
- wasmtime_store_t *capi() { return ptr.get(); }
-
private:
template <typename F>
static wasmtime_error_t *
@@ -248,7 +240,7 @@ private:
auto result = callback(ctx, *epoch_deadline_delta);
if (!result) {
- return result.err().release();
+ return result.err().capi_release();
}
*update_kind = static_cast<wasmtime_update_deadline_kind_t>(result.ok());
return nullptr;
diff --git a/crates/c-api/include/wasmtime/trap.hh b/crates/c-api/include/wasmtime/trap.hh
index c7a8df5ca2..b20d828cab 100644
--- a/crates/c-api/include/wasmtime/trap.hh
+++ b/crates/c-api/include/wasmtime/trap.hh
@@ -111,20 +111,8 @@ inline Trace Error::trace() const {
* frames on the stack.
*/
class Trap {
- friend class Linker;
- friend class Instance;
- friend class Func;
- template <typename Params, typename Results> friend class TypedFunc;
+ WASMTIME_OWN_WRAPPER(Trap, wasm_trap);
- struct deleter {
- void operator()(wasm_trap_t *p) const { wasm_trap_delete(p); }
- };
-
- std::unique_ptr<wasm_trap_t, deleter> ptr;
-
- Trap(wasm_trap_t *ptr) : ptr(ptr) {}
-
-public:
/// Creates a new host-defined trap with the specified message.
explicit Trap(std::string_view msg)
: Trap(wasmtime_trap_new(msg.data(), msg.size())) {}
diff --git a/crates/c-api/include/wasmtime/wasi.hh b/crates/c-api/include/wasmtime/wasi.hh
index e5f2450bc8..7f972d41ba 100644
--- a/crates/c-api/include/wasmtime/wasi.hh
+++ b/crates/c-api/include/wasmtime/wasi.hh
@@ -10,6 +10,7 @@
#include <vector>
#include <wasi.h>
#include <wasmtime/conf.h>
+#include <wasmtime/helpers.hh>
#ifdef WASMTIME_FEATURE_WASI
@@ -21,15 +22,8 @@ namespace wasmtime {
* This is inserted into a store with `Store::Context::set_wasi`.
*/
class WasiConfig {
- friend class Store;
+ WASMTIME_OWN_WRAPPER(WasiConfig, wasi_config);
- struct deleter {
- void operator()(wasi_config_t *p) const { wasi_config_delete(p); }
- };
-
- std::unique_ptr<wasi_config_t, deleter> ptr;
-
-public:
/// Creates a new configuration object with default settings.
WasiConfig() : ptr(wasi_config_new()) {}
@@ -102,9 +96,6 @@ public:
return wasi_config_preopen_dir(ptr.get(), path.c_str(), guest_path.c_str(),
dir_perms, file_perms);
}
-
- /// \brief Returns the underlying C API pointer.
- [[nodiscard]] wasi_config_t *capi() { return ptr.get(); }
};
} // namespace wasmtime