Storing WASM system state

29 views
Skip to first unread message

Steve Dekorte

unread,
Dec 15, 2022, 8:09:18 PM12/15/22
to emscripten-discuss
Is there JS emscripten API for storing and restoring a WASM system state (all non-code memory) to/from a buffer? Is there any sample code demonstrating this?

Sam Clegg

unread,
Dec 16, 2022, 4:10:03 PM12/16/22
to emscripte...@googlegroups.com
On Thu, Dec 15, 2022 at 5:09 PM Steve Dekorte <st...@dekorte.com> wrote:
Is there JS emscripten API for storing and restoring a WASM system state (all non-code memory) to/from a buffer? Is there any sample code demonstrating this?

While saving and restoring the entire Wasm memory is relatively straight forward (its just an ArrayBuffer operation), one possible problem you would face is that emscripten stores a lot of its state in JS.  Any kind of save/restore mechanism would likely also need to take that into account.  Perhaps you could explain why you are trying to do this and it might make it clear if that would be a problem in your case?

cheers,
sam


--
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-disc...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/emscripten-discuss/029cdcef-982f-42d3-9ad5-fb5e8e9ebc81n%40googlegroups.com.

Steve Dekorte

unread,
Dec 16, 2022, 4:29:29 PM12/16/22
to emscripten-discuss

I've ported an interpreter for scripting language to WASM using emscripten and gotten a REPL working for it in a web page. I'm interested in the possibility of making a Smalltalk/LISP like development environment with it. There would be other ways to save state in the system, but storing the complete system state would be a nice place to start.

Sam Clegg

unread,
Dec 16, 2022, 8:32:37 PM12/16/22
to emscripte...@googlegroups.com
On Fri, Dec 16, 2022 at 1:29 PM Steve Dekorte <st...@dekorte.com> wrote:

I've ported an interpreter for scripting language to WASM using emscripten and gotten a REPL working for it in a web page. I'm interested in the possibility of making a Smalltalk/LISP like development environment with it. There would be other ways to save state in the system, but storing the complete system state would be a nice place to start.

Saving the entire state of JS would most likely be hard, but would depend on what features you use.  Saving the state of the wasm linear memory would just be a question of saving and restoring the HEADU8 typed array (i.e. pretty simple).    

There are other parts of the running wasm module (in addition to the linear memory) that might also prove hard to save/restore, such as (mutable) wasm globals and the wasm table (this last one might be very hard if a user rand addFunction to add a function to the table at runtime).
 
On Friday, December 16, 2022 at 1:10:03 PM UTC-8 s...@google.com wrote:
On Thu, Dec 15, 2022 at 5:09 PM Steve Dekorte <st...@dekorte.com> wrote:
Is there JS emscripten API for storing and restoring a WASM system state (all non-code memory) to/from a buffer? Is there any sample code demonstrating this?

While saving and restoring the entire Wasm memory is relatively straight forward (its just an ArrayBuffer operation), one possible problem you would face is that emscripten stores a lot of its state in JS.  Any kind of save/restore mechanism would likely also need to take that into account.  Perhaps you could explain why you are trying to do this and it might make it clear if that would be a problem in your case?

cheers,
sam


--
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-disc...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/emscripten-discuss/029cdcef-982f-42d3-9ad5-fb5e8e9ebc81n%40googlegroups.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-disc...@googlegroups.com.

Steve Dekorte

unread,
Dec 17, 2022, 1:40:33 PM12/17/22
to emscripte...@googlegroups.com

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


Sam Clegg

unread,
Dec 17, 2022, 4:40:49 PM12/17/22
to emscripte...@googlegroups.com
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.

--
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-disc...@googlegroups.com.

Steve Dekorte

unread,
Dec 17, 2022, 4:58:55 PM12/17/22
to emscripte...@googlegroups.com

For my use case, I only want to use HTML/JS to start WASM and hand it a canvas and events. Things like i/o contexts would have to be reconstructed if possible. Is there anything else?

On Dec 17, 2022, at 1:40 PM, 'Sam Clegg' via emscripten-discuss <emscripte...@googlegroups.com> wrote:


You received this message because you are subscribed to a topic in the Google Groups "emscripten-discuss" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/emscripten-discuss/-b2JbVbsfo4/unsubscribe.
To unsubscribe from this group and all its topics, send an email to emscripten-disc...@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.

Sam Clegg

unread,
Dec 17, 2022, 6:45:26 PM12/17/22
to emscripte...@googlegroups.com
Inside the instance there are wasm globals (emscripten doesn't make heavy use of them but they exist) and the indirect function table. 

On the JS there are a lot of potential things... but it sounds like you are planning to avoid them so maybe it won't be an issue for you.

Александр Гурьянов

unread,
Dec 18, 2022, 3:08:23 AM12/18/22
to emscripte...@googlegroups.com
I also tried to do this, and it's work:


The only problem is emscripten FS, you need keep fs state too (it might be hard).

вс, 18 дек. 2022 г. в 02:45, 'Sam Clegg' via emscripten-discuss <emscripte...@googlegroups.com>:

Steve Dekorte

unread,
Dec 19, 2022, 5:46:28 PM12/19/22
to emscripten-discuss
Thanks! Is there a working version for the current emscripten? I won't be using emscripten FS.
Reply all
Reply to author
Forward
0 new messages