Neat! That might work for the linear memory, but as I said in an earlier reply there is more to the state of a running module than just its memory.
On Sat, Dec 17, 2022 at 10:40 AM Steve Dekorte <st...@dekorte.com> wrote: > > Just for fun, I tried asking ChatGPT: > > [[Please write some Javascript code which stores the state of a > WebAssembly instance to a byte array.]] > > // First, create a WebAssembly instance and obtain a reference to its > memory object > const wasmInstance = new WebAssembly.Instance(wasmModule); > const memory = wasmInstance.exports.memory; > > // Next, create a JavaScript typed array to hold the contents of the > memory object > const memoryArray = new Uint8Array(memory.buffer); > > // Finally, use the copy method to copy the contents of the memory object > to the typed array > memory.copy(memoryArray); > > [[Thanks. Could you also write some Javascript code which creates a new > WebAssembly instance and uses this byte array to restore the previous > state?]] > > // First, create a WebAssembly module using the WebAssembly.compile > function > const wasmModule = await WebAssembly.compile(wasmCode); > > // Next, create a new WebAssembly instance and obtain a reference to its > memory object > const wasmInstance = new WebAssembly.Instance(wasmModule); > const memory = wasmInstance.exports.memory; > > // Use the copy method to copy the contents of the byte array into the > memory object of the WebAssembly instance > memory.copy(byteArray); > > // Now, the WebAssembly instance has been restored to the previous state > > //...It is important to note that the byte array must be the same size as > the memory object of the WebAssembly instance, and it must contain a valid > snapshot of the state of the WebAssembly instance. If the byte array is not > the correct size or does not contain a valid snapshot, the WebAssembly > instance may not be restored correctly. > > I haven’t had a chance to give it a try yet. Does it look reasonable? > > Steve > > > -- > You received this message because you are subscribed to the Google Groups > "emscripten-discuss" group. > To unsubscribe from this group and stop receiving emails from it, send an > email to emscripten-discuss+unsubscr...@googlegroups.com. > To view this discussion on the web visit > https://groups.google.com/d/msgid/emscripten-discuss/f1967bf2cf0d44dc90bd7557a64839f4%40smtp.hushmail.com > . > -- You received this message because you are subscribed to the Google Groups "emscripten-discuss" group. To unsubscribe from this group and stop receiving emails from it, send an email to emscripten-discuss+unsubscr...@googlegroups.com. To view this discussion on the web visit https://groups.google.com/d/msgid/emscripten-discuss/CAL_va28VFRxNwFGv2R6f2rDhYpTie0e8Pie%3Dt5hb4y2G5%3Dgzzw%40mail.gmail.com.