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