summaryrefslogtreecommitdiff
path: root/wasm2c/wasm-rt.h
diff options
context:
space:
mode:
authorKeith Winstein <keithw@cs.stanford.edu>2022-10-02 23:27:36 -0700
committerGitHub <noreply@github.com>2022-10-03 06:27:36 +0000
commitcbc56844fe4e3452a4ba53c4b25247a9b62e0e3d (patch)
tree8b105276fad3a8c21c6c0bf6244c73f37e5fa550 /wasm2c/wasm-rt.h
parentbd991b56153cf453014f58d0a3bd06bfa363ec17 (diff)
downloadwabt-cbc56844fe4e3452a4ba53c4b25247a9b62e0e3d.tar.gz
wabt-cbc56844fe4e3452a4ba53c4b25247a9b62e0e3d.tar.bz2
wabt-cbc56844fe4e3452a4ba53c4b25247a9b62e0e3d.zip
wasm2c: implement the reference-types proposal (#1887)
Restores current versions of all non-SIMD tests in the core testsuite and multi-memory and exception-handling proposals.
Diffstat (limited to 'wasm2c/wasm-rt.h')
-rw-r--r--wasm2c/wasm-rt.h91
1 files changed, 71 insertions, 20 deletions
diff --git a/wasm2c/wasm-rt.h b/wasm2c/wasm-rt.h
index c27905fc..f78a8b42 100644
--- a/wasm2c/wasm-rt.h
+++ b/wasm2c/wasm-rt.h
@@ -130,27 +130,40 @@ typedef enum {
WASM_RT_I64,
WASM_RT_F32,
WASM_RT_F64,
+ WASM_RT_FUNCREF,
+ WASM_RT_EXTERNREF,
} wasm_rt_type_t;
/**
- * A function type for all `funcref` functions in a Table. All functions are
- * stored in this canonical form, but must be cast to their proper signature to
- * call.
+ * A generic function pointer type, both for Wasm functions (`code`)
+ * and host functions (`hostcode`). All function pointers are stored
+ * in this canonical form, but must be cast to their proper signature
+ * to call.
*/
-typedef void (*wasm_rt_funcref_t)(void);
+typedef void (*wasm_rt_function_ptr_t)(void);
-/** A single element of a Table. */
+/** A function instance (the runtime representation of a function).
+ * These can be stored in tables of type funcref, or used as values. */
typedef struct {
/** The index as returned from `wasm_rt_register_func_type`. */
uint32_t func_type;
/** The function. The embedder must know the actual C signature of the
* function and cast to it before calling. */
- wasm_rt_funcref_t func;
+ wasm_rt_function_ptr_t func;
/** A function instance is a closure of the function over an instance
* of the originating module. The module_instance element will be passed into
* the function at runtime. */
void* module_instance;
-} wasm_rt_elem_t;
+} wasm_rt_funcref_t;
+
+/** Default (null) value of a funcref */
+static const wasm_rt_funcref_t wasm_rt_funcref_null_value = {0, NULL, NULL};
+
+/** The type of an external reference (opaque to WebAssembly). */
+typedef void* wasm_rt_externref_t;
+
+/** Default (null) value of an externref */
+static const wasm_rt_externref_t wasm_rt_externref_null_value = NULL;
/** A Memory object. */
typedef struct {
@@ -163,16 +176,27 @@ typedef struct {
uint32_t size;
} wasm_rt_memory_t;
-/** A Table object. */
+/** A Table of type funcref. */
+typedef struct {
+ /** The table element data, with an element count of `size`. */
+ wasm_rt_funcref_t* data;
+ /** The maximum element count of this Table object. If there is no maximum,
+ * `max_size` is 0xffffffffu (i.e. UINT32_MAX). */
+ uint32_t max_size;
+ /** The current element count of the table. */
+ uint32_t size;
+} wasm_rt_funcref_table_t;
+
+/** A Table of type externref. */
typedef struct {
/** The table element data, with an element count of `size`. */
- wasm_rt_elem_t* data;
+ wasm_rt_externref_t* data;
/** The maximum element count of this Table object. If there is no maximum,
* `max_size` is 0xffffffffu (i.e. UINT32_MAX). */
uint32_t max_size;
/** The current element count of the table. */
uint32_t size;
-} wasm_rt_table_t;
+} wasm_rt_externref_table_t;
/** Initialize the runtime. */
void wasm_rt_init(void);
@@ -308,23 +332,50 @@ uint32_t wasm_rt_grow_memory(wasm_rt_memory_t*, uint32_t pages);
void wasm_rt_free_memory(wasm_rt_memory_t*);
/**
- * Initialize a Table object with an element count of `elements` and a maximum
- * page size of `max_elements`.
+ * Initialize a funcref Table object with an element count of `elements` and a
+ * maximum size of `max_elements`.
*
* ```
- * wasm_rt_table_t my_table;
- * // 5 elemnets and a maximum of 10 elements.
- * wasm_rt_allocate_table(&my_table, 5, 10);
+ * wasm_rt_funcref_table_t my_table;
+ * // 5 elements and a maximum of 10 elements.
+ * wasm_rt_allocate_funcref_table(&my_table, 5, 10);
* ```
*/
-void wasm_rt_allocate_table(wasm_rt_table_t*,
- uint32_t elements,
- uint32_t max_elements);
+void wasm_rt_allocate_funcref_table(wasm_rt_funcref_table_t*,
+ uint32_t elements,
+ uint32_t max_elements);
+
+/**
+ * Free a funcref Table object.
+ */
+void wasm_rt_free_funcref_table(wasm_rt_funcref_table_t*);
+
+/**
+ * Initialize an externref Table object with an element count
+ * of `elements` and a maximum size of `max_elements`.
+ * Usage as per wasm_rt_allocate_funcref_table.
+ */
+void wasm_rt_allocate_externref_table(wasm_rt_externref_table_t*,
+ uint32_t elements,
+ uint32_t max_elements);
+
+/**
+ * Free an externref Table object.
+ */
+void wasm_rt_free_externref_table(wasm_rt_externref_table_t*);
/**
- * Free a Table object.
+ * Grow a Table object by `delta` elements (giving the new elements the value
+ * `init`), and return the previous element count. If this new element count is
+ * greater than the maximum element count, the grow fails and 0xffffffffu
+ * (UINT32_MAX) is returned instead.
*/
-void wasm_rt_free_table(wasm_rt_table_t*);
+uint32_t wasm_rt_grow_funcref_table(wasm_rt_funcref_table_t*,
+ uint32_t delta,
+ wasm_rt_funcref_t init);
+uint32_t wasm_rt_grow_externref_table(wasm_rt_externref_table_t*,
+ uint32_t delta,
+ wasm_rt_externref_t init);
#ifdef __cplusplus
}