How to reinit wasm?

30 views
Skip to first unread message

Ilya Kantor

unread,
Feb 9, 2023, 2:18:27 PM2/9/23
to emscripten-discuss
Hello,

I use Emscripten to compile a CLI application, and then run its _main many times.

The problem is that it expects every call to be "from the scratch", with uninitialized data structures.

Right now, e.g. a file has `struct my_type s`, and initializes it on the first run, then it survives till the second run and spoils it.

How to re-init WASM, to clear memory for new  runs (start afresh) while keeping FS untouched?

Sam Clegg

unread,
Feb 9, 2023, 2:31:11 PM2/9/23
to emscripte...@googlegroups.com
Hi Ilya,

Emscripten doesn't have any way to do that today (at least not that I know of).

Your best bet is probably to use `-sMODULARIZE` and then construct a new instance for each time you want to call `main`.   i.e `new MyModule()` each time.

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/140af2b7-874c-4308-8563-044e066fd89fn%40googlegroups.com.

Ilya Kantor

unread,
Feb 9, 2023, 5:34:36 PM2/9/23
to emscripte...@googlegroups.com
That's a great hint, thank you!

I tried to do so, but bumped into a problem: I need to pass FS from the old module to the new one.
I'm using the most basic default MEMFS.

Any idea how to do it?

Or maybe I should recursively walk and copy files? That's not the most elegant solution, but should work.

Kind regards,
Ilya Kantor

Sam Clegg

unread,
Feb 9, 2023, 6:05:46 PM2/9/23
to emscripte...@googlegroups.com
If you are using MEMFS then its contents would (by design) be reset when you reset your memory.

If you want to preserve data between runs you would need some kind of persistent store.   Perhaps IDBFS?

Ilya Kantor

unread,
Feb 9, 2023, 6:54:00 PM2/9/23
to emscripte...@googlegroups.com
I'm sorry to say so, but WASM memory and FS are two different things by design.

There's no need to make things more complex than keeping FS while resetting memory.
I do it with my computer every day (almost) ;)

Kind regards,
Ilya Kantor

chi...@gmail.com

unread,
Feb 9, 2023, 8:06:08 PM2/9/23
to emscripten-discuss
MEMFS is an in-MEMory FileSytem. What you need is IndexedDB-based FS.

Ilya Kantor

unread,
Feb 10, 2023, 3:43:36 AM2/10/23
to emscripte...@googlegroups.com
Well... Yes, if we look at it this way, kind of makes a point.

Does this code seem good?

I expect the FS to keep the context of /project, but it doesn't (compiling with -lidbfs.js).

async function initModule() {
  Module = await createMyModule();

  let FS = Module.FS;

  FS.mkdir('/project');
  FS.mount(FS.filesystems.IDBFS, {}, '/project');
  FS.chdir('/project');
}


Ilya Kantor

unread,
Feb 10, 2023, 5:33:22 AM2/10/23
to emscripte...@googlegroups.com
P.S. Please ignore the code in the previous letter.
This is more "feature-full" (not working, but has explicit syncfs calls):

```
let FS;
async function run() {
  Module = await createMyModule();

  FS = Module.FS;

  FS.mkdir('/project');

  FS.mount(FS.filesystems.IDBFS, {}, '/project');
  await sync();
  FS.chdir('/project');

  console.log(FS.analyzePath('content').exists);  // <---- shows false on both runs!

  FS.writeFile('content', '...'); // write file on first run, expecting to see it on the second

  await sync();
}

async function sync() {
   // should save/load existing data here? (doesn't work)
  await new Promise((res, rej) => {
    FS.syncfs(true, err => err ? rej(err) : res());
  });
}

(async () => {
  await run();
  await run();
})();
```

Ilya Kantor

unread,
Feb 10, 2023, 7:45:35 AM2/10/23
to emscripte...@googlegroups.com
P.P.S. Success, all fixed. Working ;)

TAKEAWAY: suggestion to make separate load/save methods instead of syncfs(true, callback) vs syncfs(callback).

It's so easy to mistype or misunderstand when "true" means load and "nothing" means save :/

Maybe initially syncfs was indeed supposed to sync (take newest stuff from memory/db and reconcile automatically to newest everywhere) automatically, hence the name?...

Kind regards,
Ilya Kantor
Reply all
Reply to author
Forward
0 new messages