Skip to content

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

Merge wasm_tag type into wasm_functype / clean up C API tag headers (#12803)

Vasily Chekalkin483eb43234c1
diff --git a/ci/vendor-c-api-headers.sh b/ci/vendor-c-api-headers.sh
index fa0b417927..5f002f4b5f 100755
--- a/ci/vendor-c-api-headers.sh
+++ b/ci/vendor-c-api-headers.sh
@@ -2,7 +2,7 @@
# Script to re-vendor the headers from the `wasm-c-api` proposal.
-rev="2ce1367c9d1271c83fb63bef26d896a2f290cd23"
+rev="9d6b93764ac96cdd9db51081c363e09d2d488b4d"
files="wasm.h wasm.hh"
set -ex
diff --git a/crates/c-api/include/doc-wasm.h b/crates/c-api/include/doc-wasm.h
index d7911f0e9a..4a92d15d77 100644
--- a/crates/c-api/include/doc-wasm.h
+++ b/crates/c-api/include/doc-wasm.h
@@ -187,7 +187,7 @@
* \fn wasm_name_new_empty
* \brief Convenience alias
*
- * \fn wasm_name_new_new_uninitialized
+ * \fn wasm_name_new_uninitialized
* \brief Convenience alias
*
* \fn wasm_name_new_from_string
@@ -611,11 +611,77 @@
* #wasmtime_memorytype_maximum or #wasmtime_memorytype_minimum instead.
*/
+/**
+ * \struct wasm_tagtype_t
+ * \brief An opaque object representing the type of a tag.
+ *
+ * \typedef wasm_tagtype_t
+ * \brief Convenience alias for #wasm_tagtype_t
+ *
+ * \struct wasm_tagtype_vec_t
+ * \brief A list of #wasm_tagtype_t values.
+ *
+ * \var wasm_tagtype_vec_t::size
+ * \brief Length of this vector.
+ *
+ * \var wasm_tagtype_vec_t::data
+ * \brief Pointer to the base of this vector
+ *
+ * \typedef wasm_tagtype_vec_t
+ * \brief Convenience alias for #wasm_tagtype_vec_t
+ *
+ * \fn void wasm_tagtype_delete(wasm_tagtype_t *);
+ * \brief Deletes a type.
+ *
+ * \fn void wasm_tagtype_vec_new_empty(wasm_tagtype_vec_t *out);
+ * \brief Creates an empty vector.
+ *
+ * See #wasm_byte_vec_new_empty for more information.
+ *
+ * \fn void wasm_tagtype_vec_new_uninitialized(wasm_tagtype_vec_t *out, size_t);
+ * \brief Creates a vector with the given capacity.
+ *
+ * See #wasm_byte_vec_new_uninitialized for more information.
+ *
+ * \fn void wasm_tagtype_vec_new(wasm_tagtype_vec_t *out, size_t, wasm_tagtype_t *const[]);
+ * \brief Creates a vector with the provided contents.
+ *
+ * See #wasm_byte_vec_new for more information.
+ *
+ * \fn void wasm_tagtype_vec_copy(wasm_tagtype_vec_t *out, const wasm_tagtype_vec_t *)
+ * \brief Copies one vector to another
+ *
+ * See #wasm_byte_vec_copy for more information.
+ *
+ * \fn void wasm_tagtype_vec_delete(wasm_tagtype_vec_t *out)
+ * \brief Deallocates memory for a vector.
+ *
+ * See #wasm_byte_vec_delete for more information.
+ *
+ * \fn wasm_tagtype_t* wasm_tagtype_copy(const wasm_tagtype_t *)
+ * \brief Creates a new value which matches the provided one.
+ *
+ * The caller is responsible for deleting the returned value.
+ *
+ * \fn wasm_tagtype_t* wasm_tagtype_new(wasm_functype_t *)
+ * \brief Creates a new tag type.
+ *
+ * This function takes ownership of the #wasm_functype_t argument.
+ *
+ * The caller is responsible for deallocating the returned type.
+ *
+ * \fn const wasm_functype_t* wasm_tagtype_functype(const wasm_tagtype_t *);
+ * \brief Returns the function type of this tag type.
+ *
+ * The returned #wasm_functype_t is owned by the #wasm_tagtype_t parameter, the
+ * caller should not deallocate it.
+ */
+
/**
* \struct wasm_externtype_t
* \brief An opaque object representing the type of a external value. Can be
* seen as a superclass of #wasm_functype_t, #wasm_tabletype_t,
- * #wasm_globaltype_t, and #wasm_memorytype_t.
+ * #wasm_globaltype_t, #wasm_memorytype_t, and #wasm_tagtype_t.
*
* \typedef wasm_externtype_t
* \brief Convenience alias for #wasm_externtype_t
@@ -675,6 +741,21 @@
*
* This is returned from #wasm_extern_kind and #wasm_externtype_kind to
* determine what kind of type is wrapped.
+ *
+ * \def WASM_EXTERN_FUNC
+ * \brief Indicator that an extern is a function.
+ *
+ * \def WASM_EXTERN_GLOBAL
+ * \brief Indicator that an extern is a global.
+ *
+ * \def WASM_EXTERN_TABLE
+ * \brief Indicator that an extern is a table.
+ *
+ * \def WASM_EXTERN_MEMORY
+ * \brief Indicator that an extern is a memory.
+ *
+ * \def WASM_EXTERN_TAG
+ * \brief Indicator that an extern is a tag.
*/
/**
@@ -702,6 +783,12 @@
* The returned value is owned by the #wasm_memorytype_t argument and should not
* be deleted.
*
+ * \fn wasm_externtype_t* wasm_tagtype_as_externtype(wasm_tagtype_t *)
+ * \brief Converts a #wasm_tagtype_t to a #wasm_externtype_t
+ *
+ * The returned value is owned by the #wasm_tagtype_t argument and should not
+ * be deleted.
+ *
* \fn const wasm_externtype_t* wasm_functype_as_externtype_const(const wasm_functype_t *)
* \brief Converts a #wasm_functype_t to a #wasm_externtype_t
*
@@ -726,6 +813,12 @@
* The returned value is owned by the #wasm_memorytype_t argument and should not
* be deleted.
*
+ * \fn const wasm_externtype_t* wasm_tagtype_as_externtype_const(const wasm_tagtype_t *)
+ * \brief Converts a #wasm_tagtype_t to a #wasm_externtype_t
+ *
+ * The returned value is owned by the #wasm_tagtype_t argument and should not
+ * be deleted.
+ *
* \fn wasm_functype_t* wasm_externtype_as_functype(wasm_externtype_t *)
* \brief Attempts to convert a #wasm_externtype_t to a #wasm_functype_t
*
@@ -747,6 +840,13 @@
* be deleted. Returns `NULL` if the provided argument is not a
* #wasm_memorytype_t.
*
+ * \fn wasm_tagtype_t* wasm_externtype_as_tagtype(wasm_externtype_t *)
+ * \brief Attempts to convert a #wasm_externtype_t to a #wasm_tagtype_t
+ *
+ * The returned value is owned by the #wasm_tagtype_t argument and should not
+ * be deleted. Returns `NULL` if the provided argument is not a
+ * #wasm_tagtype_t.
+ *
* \fn wasm_globaltype_t* wasm_externtype_as_globaltype(wasm_externtype_t *)
* \brief Attempts to convert a #wasm_externtype_t to a #wasm_globaltype_t
*
@@ -775,6 +875,13 @@
* be deleted. Returns `NULL` if the provided argument is not a
* #wasm_memorytype_t.
*
+ * \fn const wasm_tagtype_t* wasm_externtype_as_tagtype_const(const wasm_externtype_t *)
+ * \brief Attempts to convert a #wasm_externtype_t to a #wasm_tagtype_t
+ *
+ * The returned value is owned by the #wasm_tagtype_t argument and should not
+ * be deleted. Returns `NULL` if the provided argument is not a
+ * #wasm_tagtype_t.
+ *
* \fn const wasm_globaltype_t* wasm_externtype_as_globaltype_const(const wasm_externtype_t *)
* \brief Attempts to convert a #wasm_externtype_t to a #wasm_globaltype_t
*
diff --git a/crates/c-api/include/wasm.h b/crates/c-api/include/wasm.h
index c8b3cb7d31..dc43a26a42 100644
--- a/crates/c-api/include/wasm.h
+++ b/crates/c-api/include/wasm.h
@@ -1,5 +1,5 @@
// Wasmtime-vendored copy of this header file as present in upstream:
-// <https://github.com/WebAssembly/wasm-c-api/blob/2ce1367c9d1271c83fb63bef26d896a2f290cd23/include/wasm.h>
+// <https://github.com/WebAssembly/wasm-c-api/blob/9d6b93764ac96cdd9db51081c363e09d2d488b4d/include/wasm.h>
//
// Wasmtime maintainers can update this vendored copy in-tree with the
// ./ci/vendor-c-api-headers.sh shell script.
@@ -106,7 +106,7 @@ typedef wasm_byte_vec_t wasm_name_t;
#define wasm_name wasm_byte_vec
#define wasm_name_new wasm_byte_vec_new
#define wasm_name_new_empty wasm_byte_vec_new_empty
-#define wasm_name_new_new_uninitialized wasm_byte_vec_new_uninitialized
+#define wasm_name_new_uninitialized wasm_byte_vec_new_uninitialized
#define wasm_name_copy wasm_byte_vec_copy
#define wasm_name_delete wasm_byte_vec_delete
@@ -253,6 +253,15 @@ WASM_API_EXTERN own wasm_memorytype_t* wasm_memorytype_new(const wasm_limits_t*)
WASM_API_EXTERN const wasm_limits_t* wasm_memorytype_limits(const wasm_memorytype_t*);
+// Tag Types
+
+WASM_DECLARE_TYPE(tagtype)
+
+WASM_API_EXTERN own wasm_tagtype_t* wasm_tagtype_new(own wasm_functype_t*);
+
+WASM_API_EXTERN const wasm_functype_t* wasm_tagtype_functype(const wasm_tagtype_t*);
+
+
// Extern Types
WASM_DECLARE_TYPE(externtype)
@@ -263,6 +272,7 @@ enum wasm_externkind_enum {
WASM_EXTERN_GLOBAL,
WASM_EXTERN_TABLE,
WASM_EXTERN_MEMORY,
+ WASM_EXTERN_TAG,
};
WASM_API_EXTERN wasm_externkind_t wasm_externtype_kind(const wasm_externtype_t*);
@@ -271,21 +281,25 @@ WASM_API_EXTERN wasm_externtype_t* wasm_functype_as_externtype(wasm_functype_t*)
WASM_API_EXTERN wasm_externtype_t* wasm_globaltype_as_externtype(wasm_globaltype_t*);
WASM_API_EXTERN wasm_externtype_t* wasm_tabletype_as_externtype(wasm_tabletype_t*);
WASM_API_EXTERN wasm_externtype_t* wasm_memorytype_as_externtype(wasm_memorytype_t*);
+WASM_API_EXTERN wasm_externtype_t* wasm_tagtype_as_externtype(wasm_tagtype_t*);
WASM_API_EXTERN wasm_functype_t* wasm_externtype_as_functype(wasm_externtype_t*);
WASM_API_EXTERN wasm_globaltype_t* wasm_externtype_as_globaltype(wasm_externtype_t*);
WASM_API_EXTERN wasm_tabletype_t* wasm_externtype_as_tabletype(wasm_externtype_t*);
WASM_API_EXTERN wasm_memorytype_t* wasm_externtype_as_memorytype(wasm_externtype_t*);
+WASM_API_EXTERN wasm_tagtype_t* wasm_externtype_as_tagtype(wasm_externtype_t*);
WASM_API_EXTERN const wasm_externtype_t* wasm_functype_as_externtype_const(const wasm_functype_t*);
WASM_API_EXTERN const wasm_externtype_t* wasm_globaltype_as_externtype_const(const wasm_globaltype_t*);
WASM_API_EXTERN const wasm_externtype_t* wasm_tabletype_as_externtype_const(const wasm_tabletype_t*);
WASM_API_EXTERN const wasm_externtype_t* wasm_memorytype_as_externtype_const(const wasm_memorytype_t*);
+WASM_API_EXTERN const wasm_externtype_t* wasm_tagtype_as_externtype_const(const wasm_tagtype_t*);
WASM_API_EXTERN const wasm_functype_t* wasm_externtype_as_functype_const(const wasm_externtype_t*);
WASM_API_EXTERN const wasm_globaltype_t* wasm_externtype_as_globaltype_const(const wasm_externtype_t*);
WASM_API_EXTERN const wasm_tabletype_t* wasm_externtype_as_tabletype_const(const wasm_externtype_t*);
WASM_API_EXTERN const wasm_memorytype_t* wasm_externtype_as_memorytype_const(const wasm_externtype_t*);
+WASM_API_EXTERN const wasm_tagtype_t* wasm_externtype_as_tagtype_const(const wasm_externtype_t*);
// Import Types
diff --git a/crates/c-api/include/wasm.hh b/crates/c-api/include/wasm.hh
index e03408241b..7ade36fcc0 100644
--- a/crates/c-api/include/wasm.hh
+++ b/crates/c-api/include/wasm.hh
@@ -1,5 +1,5 @@
// Wasmtime-vendored copy of this header file as present in upstream:
-// <https://github.com/WebAssembly/wasm-c-api/blob/2ce1367c9d1271c83fb63bef26d896a2f290cd23/include/wasm.hh>
+// <https://github.com/WebAssembly/wasm-c-api/blob/9d6b93764ac96cdd9db51081c363e09d2d488b4d/include/wasm.hh>
//
// Wasmtime maintainers can update this vendored copy in-tree with the
// ./ci/vendor-c-api-headers.sh shell script.
@@ -14,6 +14,7 @@
#include <cstdint>
#include <cstring>
#include <memory>
+#include <new>
#include <limits>
#include <string>
@@ -299,13 +300,14 @@ public:
// External Types
enum class ExternKind : uint8_t {
- FUNC, GLOBAL, TABLE, MEMORY
+ FUNC, GLOBAL, TABLE, MEMORY, TAG
};
class FuncType;
class GlobalType;
class TableType;
class MemoryType;
+class TagType;
class WASM_API_EXTERN ExternType {
friend class destroyer;
@@ -324,11 +326,13 @@ public:
auto global() -> GlobalType*;
auto table() -> TableType*;
auto memory() -> MemoryType*;
+ auto tag() -> TagType*;
auto func() const -> const FuncType*;
auto global() const -> const GlobalType*;
auto table() const -> const TableType*;
auto memory() const -> const MemoryType*;
+ auto tag() const -> const TagType*;
};
@@ -411,6 +415,24 @@ public:
};
+// Tag Types
+
+class WASM_API_EXTERN TagType : public ExternType {
+ friend class destroyer;
+ void destroy();
+
+protected:
+ TagType() = default;
+ ~TagType() = default;
+
+public:
+ static auto make(own<FuncType>&&) -> own<TagType>;
+ auto copy() const -> own<TagType>;
+
+ auto functype() const -> const FuncType*;
+};
+
+
// Import Types
using Name = vec<byte_t>;
diff --git a/crates/c-api/include/wasmtime.h b/crates/c-api/include/wasmtime.h
index 3d403790dd..814da24344 100644
--- a/crates/c-api/include/wasmtime.h
+++ b/crates/c-api/include/wasmtime.h
@@ -201,7 +201,6 @@
#include <wasmtime/sharedmemory.h>
#include <wasmtime/store.h>
#include <wasmtime/table.h>
-#include <wasmtime/tag.h>
#include <wasmtime/trap.h>
#include <wasmtime/val.h>
#include <wasmtime/async.h>
diff --git a/crates/c-api/include/wasmtime/tag.h b/crates/c-api/include/wasmtime/tag.h
deleted file mode 100644
index d614052e4e..0000000000
--- a/crates/c-api/include/wasmtime/tag.h
+++ /dev/null
@@ -1,94 +0,0 @@
-/**
- * \file wasmtime/tag.h
- *
- * \brief Wasmtime APIs for WebAssembly exception tag types.
- *
- * This header defines the C API for `wasmtime_tagtype_t`, the type descriptor
- * for WebAssembly exception tags (wasm exception-handling proposal). Because
- * `wasm.h` is vendored from the upstream wasm-c-api repository and does not
- * yet include tag-type support, the declarations live here instead.
- */
-
-#ifndef WASMTIME_TAG_H
-#define WASMTIME_TAG_H
-
-#include <stddef.h>
-#include <wasm.h>
-
-#ifdef __cplusplus
-extern "C" {
-#endif
-
-/**
- * \brief Opaque type representing a WebAssembly exception tag type.
- *
- * A tag type is described by a function type whose parameters are the exception
- * payload types and whose results are the tag's result types (currently always
- * empty, but reserved for the stack-switching proposal).
- */
-typedef struct wasmtime_tagtype_t wasmtime_tagtype_t;
-
-/**
- * \brief Value returned by #wasm_externtype_kind for exception tags.
- *
- * This constant extends the `WASM_EXTERN_*` range from `wasm.h` (0–3) with
- * tag support. It is distinct from #WASMTIME_EXTERN_SHAREDMEMORY (which is
- * a discriminant for the runtime #wasmtime_extern_t union in
- * `wasmtime/extern.h`).
- */
-#define WASMTIME_EXTERNTYPE_TAG 4
-
-/**
- * \brief Creates a new tag type from the given function type.
- *
- * The function type describes the exception payload: its parameters are the
- * tag's exception payload types and its results are the tag's result types.
- * `engine` is used to resolve `functype` if it has not yet been interned.
- *
- * Returns an owned #wasmtime_tagtype_t that must be freed with
- * #wasmtime_tagtype_delete.
- */
-WASM_API_EXTERN wasmtime_tagtype_t *
-wasmtime_tagtype_new(wasm_engine_t *engine, const wasm_functype_t *functype);
-
-/// \brief Deletes a #wasmtime_tagtype_t.
-WASM_API_EXTERN void wasmtime_tagtype_delete(wasmtime_tagtype_t *);
-
-/// \brief Returns a copy of the given #wasmtime_tagtype_t (caller owns the
-/// result).
-WASM_API_EXTERN wasmtime_tagtype_t *
-wasmtime_tagtype_copy(const wasmtime_tagtype_t *);
-
-/**
- * \brief Returns the function type describing this tag's exception payload.
- *
- * The caller owns the returned #wasm_functype_t and must free it with
- * #wasm_functype_delete.
- */
-WASM_API_EXTERN wasm_functype_t *
-wasmtime_tagtype_functype(const wasmtime_tagtype_t *);
-
-/// \brief Converts a #wasmtime_tagtype_t to a #wasm_externtype_t (borrowed).
-WASM_API_EXTERN wasm_externtype_t *
-wasmtime_tagtype_as_externtype(wasmtime_tagtype_t *);
-
-/// \brief Converts a const #wasmtime_tagtype_t to a const #wasm_externtype_t
-/// (borrowed).
-WASM_API_EXTERN const wasm_externtype_t *
-wasmtime_tagtype_as_externtype_const(const wasmtime_tagtype_t *);
-
-/// \brief Converts a #wasm_externtype_t to a #wasmtime_tagtype_t, or NULL if
-/// not a tag.
-WASM_API_EXTERN wasmtime_tagtype_t *
-wasmtime_externtype_as_tagtype(wasm_externtype_t *);
-
-/// \brief Converts a const #wasm_externtype_t to a const #wasmtime_tagtype_t,
-/// or NULL if not a tag.
-WASM_API_EXTERN const wasmtime_tagtype_t *
-wasmtime_externtype_as_tagtype_const(const wasm_externtype_t *);
-
-#ifdef __cplusplus
-} // extern "C"
-#endif
-
-#endif // WASMTIME_TAG_H
diff --git a/crates/c-api/include/wasmtime/types/extern.hh b/crates/c-api/include/wasmtime/types/extern.hh
index c6b55d603e..755c3f8265 100644
--- a/crates/c-api/include/wasmtime/types/extern.hh
+++ b/crates/c-api/include/wasmtime/types/extern.hh
@@ -7,7 +7,6 @@
#include <variant>
#include <wasm.h>
-#include <wasmtime/tag.h>
#include <wasmtime/types/export.hh>
#include <wasmtime/types/func.hh>
#include <wasmtime/types/global.hh>
@@ -61,8 +60,8 @@ private:
return wasm_externtype_as_tabletype_const(ptr);
case WASM_EXTERN_MEMORY:
return wasm_externtype_as_memorytype_const(ptr);
- case WASMTIME_EXTERNTYPE_TAG:
- return wasmtime_externtype_as_tagtype_const(ptr);
+ case WASM_EXTERN_TAG:
+ return wasm_externtype_as_tagtype_const(ptr);
}
std::abort();
}
diff --git a/crates/c-api/include/wasmtime/types/tag.hh b/crates/c-api/include/wasmtime/types/tag.hh
index 9cf239906b..5f4180af79 100644
--- a/crates/c-api/include/wasmtime/types/tag.hh
+++ b/crates/c-api/include/wasmtime/types/tag.hh
@@ -6,8 +6,7 @@
#define WASMTIME_TYPES_TAG_HH
#include <memory>
-#include <wasmtime/engine.hh>
-#include <wasmtime/tag.h>
+#include <wasm.h>
#include <wasmtime/types/func.hh>
namespace wasmtime {
@@ -20,21 +19,21 @@ namespace wasmtime {
*/
class TagType {
struct deleter {
- void operator()(wasmtime_tagtype_t *p) const { wasmtime_tagtype_delete(p); }
+ void operator()(wasm_tagtype_t *p) const { wasm_tagtype_delete(p); }
};
- std::unique_ptr<wasmtime_tagtype_t, deleter> ptr;
+ std::unique_ptr<wasm_tagtype_t, deleter> ptr;
public:
/// Non-owning reference to a `TagType`, must not be used after the original
/// owner is deleted.
class Ref {
friend class TagType;
- const wasmtime_tagtype_t *ptr;
+ const wasm_tagtype_t *ptr;
public:
/// Creates a reference from the raw underlying C API representation.
- Ref(const wasmtime_tagtype_t *ptr) : ptr(ptr) {}
+ Ref(const wasm_tagtype_t *ptr) : ptr(ptr) {}
/// Creates a reference to the provided `TagType`.
Ref(const TagType &ty) : Ref(ty.ptr.get()) {}
@@ -42,28 +41,27 @@ public:
///
/// The caller owns the returned `FuncType`.
FuncType functype() const {
- return FuncType(wasmtime_tagtype_functype(ptr));
+ return FuncType(wasm_functype_copy(wasm_tagtype_functype(ptr)));
}
};
private:
Ref ref;
- TagType(wasmtime_tagtype_t *ptr) : ptr(ptr), ref(ptr) {}
+ TagType(wasm_tagtype_t *ptr) : ptr(ptr), ref(ptr) {}
public:
- /// Creates a new tag type from the given function type and engine.
- TagType(Engine &engine, FuncType &functype)
- : ptr(wasmtime_tagtype_new(engine.capi(), functype.ptr.get())),
- ref(ptr.get()) {}
+ /// Creates a new tag type from the given function type.
+ /// Copies `functype` so the original is not consumed.
+ explicit TagType(const FuncType &functype)
+ : TagType(wasm_tagtype_new(wasm_functype_copy(functype.ptr.get()))) {}
/// Copies a reference into a uniquely owned tag type.
- TagType(Ref other) : TagType(wasmtime_tagtype_copy(other.ptr)) {}
+ TagType(Ref other) : TagType(wasm_tagtype_copy(other.ptr)) {}
/// Copies another tag type into this one.
- TagType(const TagType &other)
- : TagType(wasmtime_tagtype_copy(other.ptr.get())) {}
+ TagType(const TagType &other) : TagType(wasm_tagtype_copy(other.ptr.get())) {}
/// Copies another tag type into this one.
TagType &operator=(const TagType &other) {
- ptr.reset(wasmtime_tagtype_copy(other.ptr.get()));
+ ptr.reset(wasm_tagtype_copy(other.ptr.get()));
ref = ptr.get();
return *this;
}
diff --git a/crates/c-api/src/types/func.rs b/crates/c-api/src/types/func.rs
index 220801a3f1..af506ee1fb 100644
--- a/crates/c-api/src/types/func.rs
+++ b/crates/c-api/src/types/func.rs
@@ -102,6 +102,12 @@ impl wasm_functype_t {
}
}
+ pub(crate) fn from_cfunc(ty: CFuncType) -> wasm_functype_t {
+ wasm_functype_t {
+ ext: wasm_externtype_t::from_cextern_type(CExternType::Func(ty)),
+ }
+ }
+
pub(crate) fn try_from(e: &wasm_externtype_t) -> Option<&wasm_functype_t> {
match &e.which {
CExternType::Func(_) => Some(unsafe { &*(e as *const _ as *const _) }),
diff --git a/crates/c-api/src/types/tag.rs b/crates/c-api/src/types/tag.rs
index 9c915e3fd5..249313de73 100644
--- a/crates/c-api/src/types/tag.rs
+++ b/crates/c-api/src/types/tag.rs
@@ -1,27 +1,39 @@
-use crate::{CExternType, wasm_engine_t, wasm_externtype_t, wasm_functype_t};
+use crate::{CExternType, CFuncType, wasm_externtype_t, wasm_functype_t};
+use std::cell::OnceCell;
use wasmtime::TagType;
#[repr(transparent)]
#[derive(Clone)]
-pub struct wasmtime_tagtype_t {
+pub struct wasm_tagtype_t {
ext: wasm_externtype_t,
}
-wasmtime_c_api_macros::declare_ty!(wasmtime_tagtype_t);
+wasmtime_c_api_macros::declare_ty!(wasm_tagtype_t);
#[derive(Clone)]
pub(crate) struct CTagType {
- ty: TagType,
+ ty: CFuncType,
+ functype_cache: OnceCell<Box<wasm_functype_t>>,
}
impl CTagType {
pub(crate) fn new(ty: TagType) -> CTagType {
- CTagType { ty }
+ CTagType {
+ ty: CFuncType::new(ty.ty().clone()),
+ functype_cache: OnceCell::new(),
+ }
+ }
+
+ fn from_cfunc(ty: CFuncType) -> CTagType {
+ CTagType {
+ ty,
+ functype_cache: OnceCell::new(),
+ }
}
}
-impl wasmtime_tagtype_t {
- pub(crate) fn try_from(e: &wasm_externtype_t) -> Option<&wasmtime_tagtype_t> {
+impl wasm_tagtype_t {
+ pub(crate) fn try_from(e: &wasm_externtype_t) -> Option<&wasm_tagtype_t> {
match &e.which {
CExternType::Tag(_) => Some(unsafe { &*(e as *const _ as *const _) }),
_ => None,
@@ -37,58 +49,45 @@ impl wasmtime_tagtype_t {
}
/// Creates a new tag type from a function type describing the exception payload.
-///
-/// The tag type takes ownership of the exception payload shape from `functype`
-/// (which may have both params and results for forward compatibility with the
-/// stack-switching proposal). Returns an owned `wasmtime_tagtype_t` that must
-/// be freed with `wasmtime_tagtype_delete`.
+/// Takes ownership of `functype`.
#[unsafe(no_mangle)]
-pub extern "C" fn wasmtime_tagtype_new(
- engine: &wasm_engine_t,
- functype: &wasm_functype_t,
-) -> Box<wasmtime_tagtype_t> {
- let func_ty = functype.ty().ty(&engine.engine);
- let tag_ty = TagType::new(func_ty);
- Box::new(wasmtime_tagtype_t {
- ext: wasm_externtype_t::from_cextern_type(CExternType::Tag(CTagType::new(tag_ty))),
+pub extern "C" fn wasm_tagtype_new(functype: Box<wasm_functype_t>) -> Box<wasm_tagtype_t> {
+ let cfunc = functype.ty().clone();
+ Box::new(wasm_tagtype_t {
+ ext: wasm_externtype_t::from_cextern_type(CExternType::Tag(CTagType::from_cfunc(cfunc))),
})
}
-/// Returns the function type describing the exception payload of this tag type.
-///
-/// The caller owns the returned `wasm_functype_t` and must free it with
-/// `wasm_functype_delete`.
+/// Returns a borrowed reference to the function type describing this tag's exception payload.
#[unsafe(no_mangle)]
-pub extern "C" fn wasmtime_tagtype_functype(tt: &wasmtime_tagtype_t) -> Box<wasm_functype_t> {
- Box::new(wasm_functype_t::new(tt.cty().ty.ty().clone()))
+pub extern "C" fn wasm_tagtype_functype(tt: &wasm_tagtype_t) -> &wasm_functype_t {
+ let cty = tt.cty();
+ cty.functype_cache
+ .get_or_init(|| Box::new(wasm_functype_t::from_cfunc(cty.ty.clone())))
}
-/// Converts a `wasmtime_tagtype_t` to a `wasm_externtype_t` (borrowed).
+/// Converts a `wasm_tagtype_t` to a `wasm_externtype_t` (borrowed).
#[unsafe(no_mangle)]
-pub extern "C" fn wasmtime_tagtype_as_externtype(ty: &wasmtime_tagtype_t) -> &wasm_externtype_t {
+pub extern "C" fn wasm_tagtype_as_externtype(ty: &wasm_tagtype_t) -> &wasm_externtype_t {
&ty.ext
}
-/// Converts a const `wasmtime_tagtype_t` to a const `wasm_externtype_t` (borrowed).
+/// Converts a const `wasm_tagtype_t` to a const `wasm_externtype_t` (borrowed).
#[unsafe(no_mangle)]
-pub extern "C" fn wasmtime_tagtype_as_externtype_const(
- ty: &wasmtime_tagtype_t,
-) -> &wasm_externtype_t {
+pub extern "C" fn wasm_tagtype_as_externtype_const(ty: &wasm_tagtype_t) -> &wasm_externtype_t {
&ty.ext
}
-/// Converts a `wasm_externtype_t` to a `wasmtime_tagtype_t`, or NULL if not a tag.
+/// Converts a `wasm_externtype_t` to a `wasm_tagtype_t`, or NULL if not a tag.
#[unsafe(no_mangle)]
-pub extern "C" fn wasmtime_externtype_as_tagtype(
- et: &wasm_externtype_t,
-) -> Option<&wasmtime_tagtype_t> {
- wasmtime_externtype_as_tagtype_const(et)
+pub extern "C" fn wasm_externtype_as_tagtype(et: &wasm_externtype_t) -> Option<&wasm_tagtype_t> {
+ wasm_externtype_as_tagtype_const(et)
}
-/// Converts a const `wasm_externtype_t` to a const `wasmtime_tagtype_t`, or NULL if not a tag.
+/// Converts a const `wasm_externtype_t` to a const `wasm_tagtype_t`, or NULL if not a tag.
#[unsafe(no_mangle)]
-pub extern "C" fn wasmtime_externtype_as_tagtype_const(
+pub extern "C" fn wasm_externtype_as_tagtype_const(
et: &wasm_externtype_t,
-) -> Option<&wasmtime_tagtype_t> {
- wasmtime_tagtype_t::try_from(et)
+) -> Option<&wasm_tagtype_t> {
+ wasm_tagtype_t::try_from(et)
}
diff --git a/crates/c-api/tests/tag_type.cc b/crates/c-api/tests/tag_type.cc
index 7470c36dcf..a2f7db1ead 100644
--- a/crates/c-api/tests/tag_type.cc
+++ b/crates/c-api/tests/tag_type.cc
@@ -1,4 +1,3 @@
-#include <wasmtime/tag.h>
#include <wasmtime/types/tag.hh>
#include <gtest/gtest.h>
@@ -14,13 +13,13 @@ TEST(TagType, Simple) {
// Tag with no payload: empty functype.
FuncType empty_ft({}, {});
- TagType empty(engine, empty_ft);
+ TagType empty(empty_ft);
auto empty_func = empty->functype();
EXPECT_EQ(empty_func->params().size(), 0u);
// Tag with i32 and i64 payload types.
FuncType ft({ValKind::I32, ValKind::I64}, {});
- TagType t(engine, ft);
+ TagType t(ft);
auto func = t->functype();
auto params = func->params();
EXPECT_EQ(params.size(), 2u);
@@ -65,8 +64,8 @@ TEST(TagType, ModuleExportEnumeration) {
EXPECT_EQ(params.begin()->kind(), ValKind::I32);
}
-// Verify that wasm_externtype_kind returns WASMTIME_EXTERNTYPE_TAG for tag
-// exports and that the C-level cast functions work correctly.
+// Verify that wasm_externtype_kind returns WASM_EXTERN_TAG for tag exports
+// and that the C-level cast functions work correctly.
TEST(TagType, ExternTypeKindAndCast) {
Config config;
config.wasm_exceptions(true);
@@ -88,8 +87,8 @@ TEST(TagType, ExternTypeKindAndCast) {
*reinterpret_cast<const wasm_exporttype_t *const *>(&e);
const wasm_externtype_t *ext = wasm_exporttype_type(raw_et);
- EXPECT_EQ(wasm_externtype_kind(ext), WASMTIME_EXTERNTYPE_TAG);
- EXPECT_NE(wasmtime_externtype_as_tagtype_const(ext), nullptr);
+ EXPECT_EQ(wasm_externtype_kind(ext), WASM_EXTERN_TAG);
+ EXPECT_NE(wasm_externtype_as_tagtype_const(ext), nullptr);
EXPECT_EQ(wasm_externtype_as_functype_const(ext), nullptr);
EXPECT_EQ(wasm_externtype_as_globaltype_const(ext), nullptr);
EXPECT_EQ(wasm_externtype_as_memorytype_const(ext), nullptr);