Can I read files from the disk by using Webassembly?

空扰寡人 提交于 2019-11-30 17:18:23

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

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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!