grol-io / grol

Go REPL Open Language
https://grol.io
Apache License 2.0
12 stars 1 forks source link

[wasm/js] find or implement `fs.open` mapping to local storage and working #124

Open ldemailly opened 1 month ago

ldemailly commented 1 month ago

so load/save can work on the web client version

ldemailly commented 1 month ago

Possible pointers from slack #webassembly : (thanks Bailey Hayes)

The emscripten toolchain has a couple different impls for filesystems. IDB FS uses IndexedDB: https://emscripten.org/docs/api_reference/Filesystem-API.html?highlight=fs#filesystem-api-idbfs Note there are some gotchas to using localStorage: https://emscripten.org/docs/compiling/Deploying-Pages.html?highlight=localstorage#providing-a-quick-second-time-load If you build targeting wasip2 and then use jco to build for the web, you'd be able to use the shims as part of the jco ecosytem (wasip2): https://github.com/bytecodealliance/jco/blob/main/packages/preview2-shim/lib/nodejs/filesystem.js In practice, I usually see most folks use an in-memory fs for portability (like emscripten's). A third option is to build a wasip2 component and then compose with wasi-virt to virtualize the fs (aka it's an in-memory fs): https://github.com/bytecodealliance/wasi-virt. This approach is more bleeding edge and will likely have a few rough edges.

You could also take a look at jsfs, which implements all of the os stdlib io functions with a persistent IndexedDB backend. (You need to use this wasm_exec.js along with it).

https://developer.mozilla.org/en-US/docs/Web/API/IndexedDB_API is (maybe) available everywhere local storage is (to be verified)