* \brief Owner of all WebAssembly objects * * A `Store` owns all WebAssembly objects such as instances, globals, functions, * memories, etc. A `Store` is one of the main central points about working with * WebAssembly since it's an argument to almost all APIs. The `Store` serves as * a form of "context" to give meaning to the pointers of `Func` and friends. * * A `Store` can be sent between
| 39 | * will be deallocated when the `Store` is deallocated. |
| 40 | */ |
| 41 | class Store { |
| 42 | WASMTIME_OWN_WRAPPER(Store, wasmtime_store); |
| 43 | |
| 44 | private: |
| 45 | static void finalizer(void *ptr) { |
| 46 | std::unique_ptr<std::any> _ptr(static_cast<std::any *>(ptr)); |
| 47 | } |
| 48 | |
| 49 | public: |
| 50 | /// Creates a new `Store` within the provided `Engine`. |
| 51 | explicit Store(Engine &engine) |
| 52 | : ptr(wasmtime_store_new(engine.capi(), nullptr, finalizer)) {} |
| 53 | |
| 54 | /** |
| 55 | * \brief An interior pointer into a `Store`. |
| 56 | * |
| 57 | * A `Context` object is created from either a `Store` or a `Caller`. It is an |
| 58 | * interior pointer into a `Store` and cannot be used outside the lifetime of |
| 59 | * the original object it was created from. |
| 60 | * |
| 61 | * This object is an argument to most APIs in Wasmtime but typically doesn't |
| 62 | * need to be constructed explicitly since it can be created from a `Store&` |
| 63 | * or a `Caller&`. |
| 64 | */ |
| 65 | class Context { |
| 66 | friend class Global; |
| 67 | friend class Table; |
| 68 | friend class Memory; |
| 69 | friend class Func; |
| 70 | friend class Instance; |
| 71 | friend class Linker; |
| 72 | friend class Val; |
| 73 | friend class Store; |
| 74 | friend class Tag; |
| 75 | wasmtime_context_t *ptr; |
| 76 | |
| 77 | public: |
| 78 | /// Creates a context from the raw C API pointer. |
| 79 | explicit Context(wasmtime_context_t *ptr) : ptr(ptr) {} |
| 80 | |
| 81 | /// Creates a context referencing the provided `Store`. |
| 82 | Context(Store &store) : Context(wasmtime_store_context(store.ptr.get())) {} |
| 83 | /// Creates a context referencing the provided `Store`. |
| 84 | Context(Store *store) : Context(*store) {} |
| 85 | /// Creates a context referencing the provided `Caller`. |
| 86 | Context(Caller &caller); |
| 87 | /// Creates a context referencing the provided `Caller`. |
| 88 | Context(Caller *caller); |
| 89 | |
| 90 | #ifdef WASMTIME_FEATURE_GC |
| 91 | /// Runs a garbage collection pass in the referenced store to collect loose |
| 92 | /// `externref` values, if any are available. |
| 93 | Result<std::monostate> gc() { |
| 94 | auto *error = wasmtime_context_gc(ptr); |
| 95 | if (error != nullptr) { |
| 96 | return Error(error); |
| 97 | } |
| 98 | return std::monostate(); |