1 /**
2  * \file wasmtime/instance.h
3  *
4  * Wasmtime APIs for interacting with wasm instances.
5  */
6 
7 #ifndef WASMTIME_INSTANCE_H
8 #define WASMTIME_INSTANCE_H
9 
10 #include <wasm.h>
11 #include <wasmtime/extern.h>
12 #include <wasmtime/module.h>
13 #include <wasmtime/store.h>
14 
15 #ifdef __cplusplus
16 extern "C" {
17 #endif
18 
19 /// \brief Representation of a instance in Wasmtime.
20 ///
21 /// Instances are represented with a 64-bit identifying integer in Wasmtime.
22 /// They do not have any destructor associated with them. Instances cannot
23 /// interoperate between #wasmtime_store_t instances and if the wrong instance
24 /// is passed to the wrong store then it may trigger an assertion to abort the
25 /// process.
26 typedef struct wasmtime_instance {
27   /// Internal identifier of what store this belongs to, never zero.
28   uint64_t store_id;
29   /// Internal index within the store.
30   size_t index;
31 } wasmtime_instance_t;
32 
33 /**
34  * \brief Instantiate a wasm module.
35  *
36  * This function will instantiate a WebAssembly module with the provided
37  * imports, creating a WebAssembly instance. The returned instance can then
38  * afterwards be inspected for exports.
39  *
40  * \param store the store in which to create the instance
41  * \param module the module that's being instantiated
42  * \param imports the imports provided to the module
43  * \param nimports the size of `imports`
44  * \param instance where to store the returned instance
45  * \param trap where to store the returned trap
46  *
47  * This function requires that `imports` is the same size as the imports that
48  * `module` has. Additionally the `imports` array must be 1:1 lined up with the
49  * imports of the `module` specified. This is intended to be relatively low
50  * level, and #wasmtime_linker_instantiate is provided for a more ergonomic
51  * name-based resolution API.
52  *
53  * The states of return values from this function are similar to
54  * #wasmtime_func_call where an error can be returned meaning something like a
55  * link error in this context. A trap can be returned (meaning no error or
56  * instance is returned), or an instance can be returned (meaning no error or
57  * trap is returned).
58  *
59  * Note that this function requires that all `imports` specified must be owned
60  * by the `store` provided as well.
61  *
62  * This function does not take ownership of any of its arguments, but all return
63  * values are owned by the caller.
64  */
65 WASM_API_EXTERN wasmtime_error_t *wasmtime_instance_new(
66     wasmtime_context_t *store,
67     const wasmtime_module_t *module,
68     const wasmtime_extern_t* imports,
69     size_t nimports,
70     wasmtime_instance_t *instance,
71     wasm_trap_t **trap
72 );
73 
74 /**
75  * \brief Get an export by name from an instance.
76  *
77  * \param store the store that owns `instance`
78  * \param instance the instance to lookup within
79  * \param name the export name to lookup
80  * \param name_len the byte length of `name`
81  * \param item where to store the returned value
82  *
83  * Returns nonzero if the export was found, and `item` is filled in. Otherwise
84  * returns 0.
85  *
86  * Doesn't take ownership of any arguments but does return ownership of the
87  * #wasmtime_extern_t.
88  */
89 WASM_API_EXTERN bool wasmtime_instance_export_get(
90     wasmtime_context_t *store,
91     const wasmtime_instance_t *instance,
92     const char *name,
93     size_t name_len,
94     wasmtime_extern_t *item
95 );
96 
97 /**
98  * \brief Get an export by index from an instance.
99  *
100  * \param store the store that owns `instance`
101  * \param instance the instance to lookup within
102  * \param index the index to lookup
103  * \param name where to store the name of the export
104  * \param name_len where to store the byte length of the name
105  * \param item where to store the export itself
106  *
107  * Returns nonzero if the export was found, and `name`, `name_len`, and `item`
108  * are filled in. Otherwise returns 0.
109  *
110  * Doesn't take ownership of any arguments but does return ownership of the
111  * #wasmtime_extern_t. The `name` pointer return value is owned by the `store`
112  * and must be immediately used before calling any other APIs on
113  * #wasmtime_context_t.
114  */
115 WASM_API_EXTERN bool wasmtime_instance_export_nth(
116     wasmtime_context_t *store,
117     const wasmtime_instance_t *instance,
118     size_t index,
119     char **name,
120     size_t *name_len,
121     wasmtime_extern_t *item
122 );
123 
124 #ifdef __cplusplus
125 }  // extern "C"
126 #endif
127 
128 #endif // WASMTIME_INSTANCE_H
129