Can I read files from the disk by using Webassembly?

前端 未结 2 1301
后悔当初
后悔当初 2021-01-01 14:49

I followed the Webassembly getting started tutorial http://webassembly.org/getting-started/developers-guide/

It worked fine and displayed the \"Hello, world!\" messa

相关标签:
2条回答
  • 2021-01-01 15:02

    Keep secure — WebAssembly is specified to be run in a safe, sandboxed execution environment. Like other web code, it will enforce the browser's same-origin and permissions policies.

    So the short answer is — yes, there are restrictions. You have no access to files on disks. You just have block of memory, WASM code could be called from JS and also WASM could call JS functions.

    But, there's one interesting feature in Emscripten — in the WASM you can have your own "virtual" file system with files. You can use it to "attach" some const files during compilation time and read them at the execution time. See https://kripken.github.io/emscripten-site/docs/api_reference/Filesystem-API.html

    0 讨论(0)
  • 2021-01-01 15:04

    You can package files or directories into the WASM virtual file system using the --embed-file flag.

    In your case this would look like:

    emcc pfile.cpp -s WASM=1 -o pfile.html -v --embed-file test.txt

    Docs: https://kripken.github.io/emscripten-site/docs/porting/files/packaging_files.html

    0 讨论(0)
提交回复
热议问题