I'm trying to dynamically load a side module from the main module using dlopen().
The side module loads fine as long as its size is smaller than 4KB but I need to load large-size side modules.
Here's a simple code you can test this:
side.c:
#define SIZE 4000
char dummy[SIZE] = {};
int side(int a)
{
return SIZE;
}
main.c:
#include <stdio.h>
#include <dlfcn.h>
int main()
{
void *handle;
typedef int (*func_t)(int);
handle = dlopen("side.wasm", RTLD_NOW);
if (!handle) {
printf("failed to open the library\n");
return 0;
}
func_t func = (func_t)dlsym(handle, "side");
if (!func) {
printf("failed to find the method\n");
dlclose(handle);
return 0;
}
printf("side module size: %d byte\n", func(1));
}
index.html:
<!DOCTYPE html>
<html>
<head>
</head>
<body>
<script async src="main.js"></script>
</body>
</html>
commands:
emcc side.c -s SIDE_MODULE=1 -o side.wasm
emcc main.c -s MAIN_MODULE=1 -o main.html --preload-file side.wasm
python3 -m http.server 8080
And this is the result I get in the Chrome browser:
Error in loading dynamic library side.wasm: RangeError: WebAssembly.Compile is disallowed on the main thread, if the buffer size is larger than 4KB. Use WebAssembly.compile, or compile on a worker thread.
Can someone please guild me on how to dynamically load a side module larger than 4KB?