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   void *__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   struct {
45     /// Internal identifier of what store this belongs to, never zero.
46     uint64_t store_id;
47     /// Private field for Wasmtime.
48     uint32_t __private1;
49   };
50   /// Private field for Wasmtime.
51   uint32_t __private2;
52 } wasmtime_table_t;
53 
54 /// \brief Representation of a memory in Wasmtime.
55 ///
56 /// Memories in Wasmtime are represented as an index into a store and don't
57 /// have any data or destructor associated with the #wasmtime_memory_t value.
58 /// Memories cannot interoperate between #wasmtime_store_t instances and if the
59 /// wrong memory is passed to the wrong store then it may trigger an assertion
60 /// to abort the process.
61 typedef struct wasmtime_memory {
62   struct {
63     /// Internal identifier of what store this belongs to, never zero.
64     uint64_t store_id;
65     /// Private field for Wasmtime.
66     uint32_t __private1;
67   };
68   /// Private field for Wasmtime.
69   uint32_t __private2;
70 } wasmtime_memory_t;
71 
72 /// \brief Representation of a global in Wasmtime.
73 ///
74 /// Globals in Wasmtime are represented as an index into a store and don't
75 /// have any data or destructor associated with the #wasmtime_global_t value.
76 /// Globals cannot interoperate between #wasmtime_store_t instances and if the
77 /// wrong global is passed to the wrong store then it may trigger an assertion
78 /// to abort the process.
79 typedef struct wasmtime_global {
80   /// Internal identifier of what store this belongs to, never zero.
81   uint64_t store_id;
82   /// Private field for Wasmtime.
83   uint32_t __private1;
84   /// Private field for Wasmtime.
85   uint32_t __private2;
86   /// Private field for Wasmtime.
87   uint32_t __private3;
88 } wasmtime_global_t;
89 
90 /// \brief Discriminant of #wasmtime_extern_t
91 typedef uint8_t wasmtime_extern_kind_t;
92 
93 /// \brief Value of #wasmtime_extern_kind_t meaning that #wasmtime_extern_t is a
94 /// function
95 #define WASMTIME_EXTERN_FUNC 0
96 /// \brief Value of #wasmtime_extern_kind_t meaning that #wasmtime_extern_t is a
97 /// global
98 #define WASMTIME_EXTERN_GLOBAL 1
99 /// \brief Value of #wasmtime_extern_kind_t meaning that #wasmtime_extern_t is a
100 /// table
101 #define WASMTIME_EXTERN_TABLE 2
102 /// \brief Value of #wasmtime_extern_kind_t meaning that #wasmtime_extern_t is a
103 /// memory
104 #define WASMTIME_EXTERN_MEMORY 3
105 /// \brief Value of #wasmtime_extern_kind_t meaning that #wasmtime_extern_t is a
106 /// shared memory
107 #define WASMTIME_EXTERN_SHAREDMEMORY 4
108 
109 /**
110  * \typedef wasmtime_extern_union_t
111  * \brief Convenience alias for #wasmtime_extern_union
112  *
113  * \union wasmtime_extern_union
114  * \brief Container for different kinds of extern items.
115  *
116  * This type is contained in #wasmtime_extern_t and contains the payload for the
117  * various kinds of items an extern wasm item can be.
118  */
119 typedef union wasmtime_extern_union {
120   /// Field used if #wasmtime_extern_t::kind is #WASMTIME_EXTERN_FUNC
121   wasmtime_func_t func;
122   /// Field used if #wasmtime_extern_t::kind is #WASMTIME_EXTERN_GLOBAL
123   wasmtime_global_t global;
124   /// Field used if #wasmtime_extern_t::kind is #WASMTIME_EXTERN_TABLE
125   wasmtime_table_t table;
126   /// Field used if #wasmtime_extern_t::kind is #WASMTIME_EXTERN_MEMORY
127   wasmtime_memory_t memory;
128   /// Field used if #wasmtime_extern_t::kind is #WASMTIME_EXTERN_SHAREDMEMORY
129   struct wasmtime_sharedmemory *sharedmemory;
130 } wasmtime_extern_union_t;
131 
132 /**
133  * \typedef wasmtime_extern_t
134  * \brief Convenience alias for #wasmtime_extern_t
135  *
136  * \union wasmtime_extern
137  * \brief Container for different kinds of extern items.
138  *
139  * Note that this structure may contain an owned value, namely
140  * #wasmtime_module_t, depending on the context in which this is used. APIs
141  * which consume a #wasmtime_extern_t do not take ownership, but APIs that
142  * return #wasmtime_extern_t require that #wasmtime_extern_delete is called to
143  * deallocate the value.
144  */
145 typedef struct wasmtime_extern {
146   /// Discriminant of which field of #of is valid.
147   wasmtime_extern_kind_t kind;
148   /// Container for the extern item's value.
149   wasmtime_extern_union_t of;
150 } wasmtime_extern_t;
151 
152 /// \brief Deletes a #wasmtime_extern_t.
153 void wasmtime_extern_delete(wasmtime_extern_t *val);
154 
155 /// \brief Returns the type of the #wasmtime_extern_t defined within the given
156 /// store.
157 ///
158 /// Does not take ownership of `context` or `val`, but the returned
159 /// #wasm_externtype_t is an owned value that needs to be deleted.
160 wasm_externtype_t *wasmtime_extern_type(wasmtime_context_t *context,
161                                         wasmtime_extern_t *val);
162 
163 #ifdef __cplusplus
164 } // extern "C"
165 #endif
166 
167 #endif // WASMTIME_EXTERN_H
168