emscripten-core / emscripten-core/emscripten

How to make MODULARIZE'd modules share file system?

Open
#12,074 25 comments 1 reaction 0 assignees View on GitHub
Dominant language
C++
Stars
27.6k
Forks
3.6k
Avg merge
1d 1h
Merged PRs (30d)
105

Description

I'm planning to have a few programs compiled with Emscripten MODULARIZE=1 mode and a dummy regular module that only preloads file system, so that I can easily use multiple modules on a single HTML page.

Will modularized programs share the FS with the dummy module, i.e. will `cat` program be able to read files provided by the emscriptenfs's FS? Do I need to initialize the FS of `cat` somehow to point it to global FS from the dummy program?

`cowsay` program is here only to demonstrate that I indeed plan to have several programs, not just `cat`.

```
emscriptenfs.js: test/test.txt test/test.pdf test/test.png test/test.svg test/test.tex
emcc emscriptenfs.c -o $@ --preload-file test -s FORCE_FILESYSTEM=1 -s INVOKE_RUN=0

cat.js:
emcc cat.c -o $@ -s INVOKE_RUN=0 -s EXPORTED_FUNCTIONS='["_main"]' -s EXPORTED_RUNTIME_METHODS='["callMain"]' -s MODULARIZE=1 -s EXPORT_NAME=cat

cowsay.js:
emcc cowsay.c -o $@ -s INVOKE_RUN=0 -s EXPORTED_FUNCTIONS='["_main"]' -s EXPORTED_RUNTIME_METHODS='["callMain"]' -s MODULARIZE=1 -s EXPORT_NAME=cowsay
```

```cpp
// emscriptenfs.c

#include

int main(int argc, char* argv[])
{
puts("Hello world!");
}
```

```cpp
// cat.c

#include

#define BUFSIZE 100

int main( int argc, char *argv[] )
{
char *filename;
FILE *file;
char buf[BUFSIZE];

if (argc != 2) {
fprintf (stderr, "Usage: %s \n", argv[0]);
return 1;
}

file = fopen (argv[1], "r");
while (fgets (buf, BUFSIZE, file))
puts (buf);

fclose (file);

return 0;
}
```

```cpp
// cowsay.cpp

#include

int main(int argc, char **argv)
{
int i;

if (argc == 1)
printf("< moOh >\n");
for (i = 1; i < argc; i++)
if (i == 1)
printf("/ %s \\\n", argv[i]);
else if (i == argc - 1)
printf("\\ %s /\n", argv[i]);
else
printf("| %s |\n", argv[i]);
printf(" \\ ^__^\n");
printf(" (oo)\\_______\n");
printf(" (__)\\ )\\/\\\n");
printf(" ||----w |\n");
printf(" || ||\n");
return (0);
}
```

```
// works
cowsay().then(Module => Module.callMain(['hi']));

// fails with signature mismatch error
cat().then(Module => Module.callMain(['test/test.txt']));
```

Thank you!

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.