Recently I tried to experiment with webassembly with as few helpers as possible.
So I created a c project, included some libraries (stb_image.h) and tried to compile it.
Here is a short reproducible example:
#include <emscripten.h>
#define STBI_NO_STDIO
#define STBI_NO_FAILURE_STRINGS
#define STB_IMAGE_IMPLEMENTATION
#include "stb_image.h"
EMSCRIPTEN_KEEPALIVE
void test(){
    stbi_load_from_memory(NULL, 0, NULL, NULL, NULL, 0);
}
Here is the command I used:
emcc converter.c -s STANDALONE_WASM -o converter.wasm --no-entry
This worked fine and gave me a valid wasm file.
But then I tried to instantiate it in a browser with javascript and nothing else:
let wasm = await Webassembly.instantiateStreaming(fetch('converter.wasm'), {});
But I get this error:
Uncaught (in promise) TypeError: WebAssembly.instantiate(): Import #0 module="wasi_snapshot_preview1" error: module is not an object or function
I inspected the webassembly, and indeed my webassembly need these functions:
  (func $wasi_snapshot_preview1.fd_close (;0;) (import "wasi_snapshot_preview1" "fd_close") (param i32) (result i32))
  (func $wasi_snapshot_preview1.fd_seek (;1;) (import "wasi_snapshot_preview1" "fd_seek") (param i32 i64 i32 i32) (result i32))
  (func $wasi_snapshot_preview1.fd_write (;2;) (import "wasi_snapshot_preview1" "fd_write") (param i32 i32 i32 i32) (result i32))
  (func $wasi_snapshot_preview1.proc_exit (;3;) (import "wasi_snapshot_preview1" "proc_exit") (param i32))
I understand that these are functions that are not supported in a pure wasm module (like os calls maybe ?) but I can't find any documentation on what each of them are exactly.
So my questions are:
- What are these functions ?
- Why do I need these imported functions if the stb_imageheader is supposed to just manipulate bits in the ram with no i/o ?
- How can I tell my compiler to not use these functions (disabling the stdio library could work but I don't know how to do it)
Any insignt is apreciated !
EDIT
After experimenting with compilation of the c standard libraries, I understand what these functions are for:
- fd_writeis for printing (normally to the stdout in the os)
- fd_seekand- fd_closeare for file manipulation
there is also
fd_readto read a file but I don't need that in this code
- proc_exitto terminate the process and potentially raise an error
 
                        
Firstly I would recommend building with
-Ozor at least-O2so that the toolchain tries is upmost to shrink the resulting binaryn.I recommend building with
-Oz --profiling-funcsand then usingwasm-objdumporwasm-decompileto see why those imports and ultimately being used.When I do this is clear that
proc_exitis being usedassert_fail.. indeed it looks like they are all due to the use of the assert macro and adding-DNDEBUGmakes all those imports go away.