WebAssembly/Files

From Free Pascal wiki
Revision as of 23:41, 20 June 2022 by Nickysn (talk | contribs) (Started a new page, describing WASI filesystem access and caveats)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to navigationJump to search

The WASI target supports filesystem access and this is implemented in the Free Pascal RTL. However, there are some caveats, due to the way WASI is designed.

File access, using wasmtime

By default wasmtime disallows any file access, only standard input and output. This is because WebAssembly can be used for sandboxing and running untrusted code. To enable access to a directory, you need to run your program like that:

wasmtime run --dir <DIRECTORY> program.wasm

You can also specify the way the directory is visible inside the program, like this:

wasmtime run --mapdir <GUEST_DIR::HOST_DIR> program.wasm

For example, if you want /home/blabla/tralala to be visible as the root directory in your program, you can do:

 wasmtime run --mapdir /::/home/blabla/tralala program.wasm

Caveats

  • There is no WASI API function to obtain or change the current directory. All the Free Pascal RTL functions for dealing with the current directory are actually emulated inside the RTL. However, there's no way to specify the initial directory, so, for example, if you run your program like that:
wasmtime run --dir / program.wasm

To grant it access to the root directory and therefore to the entire filesystem, you would expect that your program would also inherit the current directory, however that's not the case. Your program will assume the current directory is /.