* \brief A WebAssembly linear memory. * * This class represents a WebAssembly memory, either created through * instantiating a module or a host memory. * * Note that this type does not itself own any resources. It points to resources * owned within a `Store` and the `Store` must be passed in as the first * argument to the functions defined on `Memory`. Note that if the wrong `Store` * is p
| 25 | * is passed in then the process will be aborted. |
| 26 | */ |
| 27 | class Memory { |
| 28 | friend class Instance; |
| 29 | wasmtime_memory_t memory; |
| 30 | |
| 31 | public: |
| 32 | /// Creates a new memory from the raw underlying C API representation. |
| 33 | Memory(wasmtime_memory_t memory) : memory(memory) {} |
| 34 | |
| 35 | /// Creates a new host-defined memory with the type specified. |
| 36 | static Result<Memory> create(Store::Context cx, const MemoryType &ty) { |
| 37 | wasmtime_memory_t memory; |
| 38 | auto *error = wasmtime_memory_new(cx.ptr, ty.ptr.get(), &memory); |
| 39 | if (error != nullptr) { |
| 40 | return Error(error); |
| 41 | } |
| 42 | return Memory(memory); |
| 43 | } |
| 44 | |
| 45 | /// Returns the type of this memory. |
| 46 | MemoryType type(Store::Context cx) const { |
| 47 | return wasmtime_memory_type(cx.ptr, &memory); |
| 48 | } |
| 49 | |
| 50 | /// Returns the size, in WebAssembly pages, of this memory. |
| 51 | uint64_t size(Store::Context cx) const { |
| 52 | return wasmtime_memory_size(cx.ptr, &memory); |
| 53 | } |
| 54 | |
| 55 | /// Returns a `span` of where this memory is located in the host. |
| 56 | /// |
| 57 | /// Note that embedders need to be very careful in their usage of the returned |
| 58 | /// `span`. It can be invalidated with calls to `grow` and/or calls into |
| 59 | /// WebAssembly. |
| 60 | Span<uint8_t> data(Store::Context cx) const { |
| 61 | auto *base = wasmtime_memory_data(cx.ptr, &memory); |
| 62 | auto size = wasmtime_memory_data_size(cx.ptr, &memory); |
| 63 | return {base, size}; |
| 64 | } |
| 65 | |
| 66 | /// Grows the memory by `delta` WebAssembly pages. |
| 67 | /// |
| 68 | /// On success returns the previous size of this memory in units of |
| 69 | /// WebAssembly pages. |
| 70 | Result<uint64_t> grow(Store::Context cx, uint64_t delta) const { |
| 71 | uint64_t prev = 0; |
| 72 | auto *error = wasmtime_memory_grow(cx.ptr, &memory, delta, &prev); |
| 73 | if (error != nullptr) { |
| 74 | return Error(error); |
| 75 | } |
| 76 | return prev; |
| 77 | } |
| 78 | |
| 79 | /// Returns the size of a page, in bytes, for this memory. |
| 80 | /// |
| 81 | /// WebAssembly memories are made up of a whole number of pages, so the byte |
| 82 | /// size will always be a multiple of their page size. Different Wasm memories |
| 83 | /// may have different page sizes. |
| 84 | /// |
no outgoing calls
no test coverage detected