1 /**
2  * \file wasmtime/extern.h
3  *
4  * \brief Definition of #wasmtime_extern_t and external items.
5  */
6 
7 #ifndef WASMTIME_EXTERN_H
8 #define WASMTIME_EXTERN_H
9 
10 #include <wasmtime/module.h>
11 #include <wasmtime/sharedmemory.h>
12 #include <wasmtime/store.h>
13 
14 #ifdef __cplusplus
15 extern "C" {
16 #endif
17 
18 /// \brief Representation of a function in Wasmtime.
19 ///
20 /// Functions in Wasmtime are represented as an index into a store and don't
21 /// have any data or destructor associated with the #wasmtime_func_t value.
22 /// Functions cannot interoperate between #wasmtime_store_t instances and if the
23 /// wrong function is passed to the wrong store then it may trigger an assertion
24 /// to abort the process.
25 typedef struct wasmtime_func {
26   /// Internal identifier of what store this belongs to.
27   ///
28   /// This field may be zero when used in conjunction with #wasmtime_val_t
29   /// to represent a null `funcref` value in WebAssembly. For a valid function
30   /// this field is otherwise never zero.
31   uint64_t store_id;
32   /// Private field for Wasmtime, undefined if `store_id` is zero.
33   size_t __private;
34 } wasmtime_func_t;
35 
36 /// \brief Representation of a table in Wasmtime.
37 ///
38 /// Tables in Wasmtime are represented as an index into a store and don't
39 /// have any data or destructor associated with the #wasmtime_table_t value.
40 /// Tables cannot interoperate between #wasmtime_store_t instances and if the
41 /// wrong table is passed to the wrong store then it may trigger an assertion
42 /// to abort the process.
43 typedef struct wasmtime_table {
44   /// Internal identifier of what store this belongs to, never zero.
45   uint64_t store_id;
46   /// Private field for Wasmtime.
47   size_t __private;
48 } wasmtime_table_t;
49 
50 /// \brief Representation of a memory in Wasmtime.
51 ///
52 /// Memories in Wasmtime are represented as an index into a store and don't
53 /// have any data or destructor associated with the #wasmtime_memory_t value.
54 /// Memories cannot interoperate between #wasmtime_store_t instances and if the
55 /// wrong memory is passed to the wrong store then it may trigger an assertion
56 /// to abort the process.
57 typedef struct wasmtime_memory {
58   /// Internal identifier of what store this belongs to, never zero.
59   uint64_t store_id;
60   /// Private field for Wasmtime.
61   size_t __private;
62 } wasmtime_memory_t;
63 
64 /// \brief Representation of a global in Wasmtime.
65 ///
66 /// Globals in Wasmtime are represented as an index into a store and don't
67 /// have any data or destructor associated with the #wasmtime_global_t value.
68 /// Globals cannot interoperate between #wasmtime_store_t instances and if the
69 /// wrong global is passed to the wrong store then it may trigger an assertion
70 /// to abort the process.
71 typedef struct wasmtime_global {
72   /// Internal identifier of what store this belongs to, never zero.
73   uint64_t store_id;
74   /// Private field for Wasmtime.
75   size_t __private;
76 } wasmtime_global_t;
77 
78 /// \brief Discriminant of #wasmtime_extern_t
79 typedef uint8_t wasmtime_extern_kind_t;
80 
81 /// \brief Value of #wasmtime_extern_kind_t meaning that #wasmtime_extern_t is a
82 /// function
83 #define WASMTIME_EXTERN_FUNC 0
84 /// \brief Value of #wasmtime_extern_kind_t meaning that #wasmtime_extern_t is a
85 /// global
86 #define WASMTIME_EXTERN_GLOBAL 1
87 /// \brief Value of #wasmtime_extern_kind_t meaning that #wasmtime_extern_t is a
88 /// table
89 #define WASMTIME_EXTERN_TABLE 2
90 /// \brief Value of #wasmtime_extern_kind_t meaning that #wasmtime_extern_t is a
91 /// memory
92 #define WASMTIME_EXTERN_MEMORY 3
93 /// \brief Value of #wasmtime_extern_kind_t meaning that #wasmtime_extern_t is a
94 /// shared memory
95 #define WASMTIME_EXTERN_SHAREDMEMORY 4
96 
97 /**
98  * \typedef wasmtime_extern_union_t
99  * \brief Convenience alias for #wasmtime_extern_union
100  *
101  * \union wasmtime_extern_union
102  * \brief Container for different kinds of extern items.
103  *
104  * This type is contained in #wasmtime_extern_t and contains the payload for the
105  * various kinds of items an extern wasm item can be.
106  */
107 typedef union wasmtime_extern_union {
108   /// Field used if #wasmtime_extern_t::kind is #WASMTIME_EXTERN_FUNC
109   wasmtime_func_t func;
110   /// Field used if #wasmtime_extern_t::kind is #WASMTIME_EXTERN_GLOBAL
111   wasmtime_global_t global;
112   /// Field used if #wasmtime_extern_t::kind is #WASMTIME_EXTERN_TABLE
113   wasmtime_table_t table;
114   /// Field used if #wasmtime_extern_t::kind is #WASMTIME_EXTERN_MEMORY
115   wasmtime_memory_t memory;
116   /// Field used if #wasmtime_extern_t::kind is #WASMTIME_EXTERN_SHAREDMEMORY
117   struct wasmtime_sharedmemory *sharedmemory;
118 } wasmtime_extern_union_t;
119 
120 /**
121  * \typedef wasmtime_extern_t
122  * \brief Convenience alias for #wasmtime_extern_t
123  *
124  * \union wasmtime_extern
125  * \brief Container for different kinds of extern items.
126  *
127  * Note that this structure may contain an owned value, namely
128  * #wasmtime_module_t, depending on the context in which this is used. APIs
129  * which consume a #wasmtime_extern_t do not take ownership, but APIs that
130  * return #wasmtime_extern_t require that #wasmtime_extern_delete is called to
131  * deallocate the value.
132  */
133 typedef struct wasmtime_extern {
134   /// Discriminant of which field of #of is valid.
135   wasmtime_extern_kind_t kind;
136   /// Container for the extern item's value.
137   wasmtime_extern_union_t of;
138 } wasmtime_extern_t;
139 
140 /// \brief Deletes a #wasmtime_extern_t.
141 void wasmtime_extern_delete(wasmtime_extern_t *val);
142 
143 /// \brief Returns the type of the #wasmtime_extern_t defined within the given
144 /// store.
145 ///
146 /// Does not take ownership of `context` or `val`, but the returned
147 /// #wasm_externtype_t is an owned value that needs to be deleted.
148 wasm_externtype_t *wasmtime_extern_type(wasmtime_context_t *context,
149                                         wasmtime_extern_t *val);
150 
151 #ifdef __cplusplus
152 } // extern "C"
153 #endif
154 
155 #endif // WASMTIME_EXTERN_H
156