wasi-sdk icon indicating copy to clipboard operation
wasi-sdk copied to clipboard

Any example or documentation how to run it in browser?

Open fsmoke opened this issue 3 years ago • 6 comments

I want use wasi-sdk as backend c++ compiler/std-libraries to build my little library. Emscripten it seems too huge and highweight for my tasks. So I need just build library as wasm with supporting memory allocation(new/delete) and run functions from this library from javascript inside browser's page.

But i spent already many time to find normal tutorial: "from beginning to the end" - but found only very very scattered information with different technologies and libraries....

Is there any normal documentation: how can i just build c++ libarary with wasi-sdk and call it's functions from pure js ???

PS I am very new in wasm - so sorry for may be dumb questions

fsmoke avatar May 15 '22 15:05 fsmoke

You would need a Wasi web polyfill that provide all the syscalls you are using at runtime. like https://wasi.dev/polyfill/ ( could be very outdated ) and as an example https://pmp-p.github.io/wasi/

pmp-p avatar May 15 '22 18:05 pmp-p

If you are targeting the web then emscripten is really you best bet today. wasi-sdk it not really designed to target the web, at last not today.

emscripten's output can be very small or very large depending on what features you use. You pay for what you use the compiler has good dead code elimination at -Os or Oz. For example a simple hello_world program is just a few bytes of JS and few bytes of Wasm.

In terms of supporting memory allocation, the allocator used in wasi-libc and emscripten is the same by default (dlmalloc). You can avoid malloc and free in both cases, but if your program depends on an allocator (indirectly) it will have roughly the same codesize cost in both wasi-sdk and emscripten.

sbc100 avatar May 16 '22 16:05 sbc100

I found this package that looks very interesting: https://github.com/qrdate/tinywasi the code is easy to read. take a look: https://github.com/qrdate/tinywasi/blob/main/src/TinyWASI.ts

spirobel avatar Jun 18 '22 06:06 spirobel

You are looking for https://github.com/toyobayashi/wasm-util/blob/23117669ac178ea9b693b5b20d87f89be232a31e/src/wasi/preview1.ts

WASI polyfill for browser, written in pure JS, compatible with Node.js WASI API.

toyobayashi avatar Feb 27 '23 07:02 toyobayashi

If you are targeting the web then emscripten is really you best bet today. wasi-sdk it not really designed to target the web, at last not today.

Fascinating. That just saved me asking a question here. I was going the try to use the WASI-SDK I downloaded instead of downloading Emscripten, which results in the temporary file system I am building on running out of disk space during downloading.

guest271314 avatar Jun 04 '23 20:06 guest271314