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 /// Private data for use in Wasmtime. 30 size_t __private; 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 * 66 wasmtime_instance_new(wasmtime_context_t *store, 67 const wasmtime_module_t *module, 68 const wasmtime_extern_t *imports, size_t nimports, 69 wasmtime_instance_t *instance, wasm_trap_t **trap); 70 71 /** 72 * \brief Get an export by name from an instance. 73 * 74 * \param store the store that owns `instance` 75 * \param instance the instance to lookup within 76 * \param name the export name to lookup 77 * \param name_len the byte length of `name` 78 * \param item where to store the returned value 79 * 80 * Returns nonzero if the export was found, and `item` is filled in. Otherwise 81 * returns 0. 82 * 83 * Doesn't take ownership of any arguments but does return ownership of the 84 * #wasmtime_extern_t. 85 */ 86 WASM_API_EXTERN bool wasmtime_instance_export_get( 87 wasmtime_context_t *store, const wasmtime_instance_t *instance, 88 const char *name, size_t name_len, wasmtime_extern_t *item); 89 90 /** 91 * \brief Get an export by index from an instance. 92 * 93 * \param store the store that owns `instance` 94 * \param instance the instance to lookup within 95 * \param index the index to lookup 96 * \param name where to store the name of the export 97 * \param name_len where to store the byte length of the name 98 * \param item where to store the export itself 99 * 100 * Returns nonzero if the export was found, and `name`, `name_len`, and `item` 101 * are filled in. Otherwise returns 0. 102 * 103 * Doesn't take ownership of any arguments but does return ownership of the 104 * #wasmtime_extern_t. The `name` pointer return value is owned by the `store` 105 * and must be immediately used before calling any other APIs on 106 * #wasmtime_context_t. 107 */ 108 WASM_API_EXTERN bool wasmtime_instance_export_nth( 109 wasmtime_context_t *store, const wasmtime_instance_t *instance, 110 size_t index, char **name, size_t *name_len, wasmtime_extern_t *item); 111 112 /** 113 * \brief A #wasmtime_instance_t, pre-instantiation, that is ready to be 114 * instantiated. 115 * 116 * Must be deleted using #wasmtime_instance_pre_delete. 117 * 118 * For more information see the Rust documentation: 119 * https://docs.wasmtime.dev/api/wasmtime/struct.InstancePre.html 120 */ 121 typedef struct wasmtime_instance_pre wasmtime_instance_pre_t; 122 123 /** 124 * \brief Delete a previously created wasmtime_instance_pre_t. 125 */ 126 WASM_API_EXTERN void 127 wasmtime_instance_pre_delete(wasmtime_instance_pre_t *instance_pre); 128 129 /** 130 * \brief Instantiates instance within the given store. 131 * 132 * This will also run the function's startup function, if there is one. 133 * 134 * For more information on instantiation see #wasmtime_instance_new. 135 * 136 * \param instance_pre the pre-initialized instance 137 * \param store the store in which to create the instance 138 * \param instance where to store the returned instance 139 * \param trap_ptr where to store the returned trap 140 * 141 * \return One of three things can happen as a result of this function. First 142 * the module could be successfully instantiated and returned through 143 * `instance`, meaning the return value and `trap` are both set to `NULL`. 144 * Second the start function may trap, meaning the return value and `instance` 145 * are set to `NULL` and `trap` describes the trap that happens. Finally 146 * instantiation may fail for another reason, in which case an error is returned 147 * and `trap` and `instance` are set to `NULL`. 148 * 149 * This function does not take ownership of any of its arguments, and all return 150 * values are owned by the caller. 151 */ 152 WASM_API_EXTERN wasmtime_error_t *wasmtime_instance_pre_instantiate( 153 const wasmtime_instance_pre_t *instance_pre, wasmtime_context_t *store, 154 wasmtime_instance_t *instance, wasm_trap_t **trap_ptr); 155 156 /** 157 * \brief Get the module (as a shallow clone) for a instance_pre. 158 * 159 * The returned module is owned by the caller and the caller **must** 160 * delete it via `wasmtime_module_delete`. 161 */ 162 WASM_API_EXTERN wasmtime_module_t * 163 wasmtime_instance_pre_module(wasmtime_instance_pre_t *instance_pre); 164 165 #ifdef __cplusplus 166 } // extern "C" 167 #endif 168 169 #endif // WASMTIME_INSTANCE_H 170