Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can I read files from the disk by using Webassembly?

Tags:

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

It worked fine and displayed the "Hello, world!" message in the browser.

Then I tried a small C++ code, that opens a text file and does the calculation (10 * 20) after reading the file.

emcc compiled the file just fine, no errors.

But when I serve the file over HTTP by running emrun, it cannot open the file.

This is what I see in the emrun web console:

Unable to open file 200 

Is there any restrictions to open files from the local disk?

    [thiago@terra hello]$ cat pfile.cpp      #include <iostream>     #include <fstream>     #include <string>     using namespace std;      int main() {      string line;      int a, b, c;      ifstream myfile("test.txt");      if (myfile.is_open()) {       while (getline (myfile, line)) {        cout << line << endl;       }       myfile.close();      }      else cout << "Unable to open file" << endl;      a = 10;      b = 20;      c = a * b;      cout << c << endl;      return 0;     }      [thiago@terra hello]$ emcc pfile.cpp -s WASM=1 -o pfile.html -v                                                                INFO:root:(Emscripten: Running sanity checks)                                                                                      clang version 4.0.0 (https://github.com/kripken/emscripten-fastcomp-clang.git c7c210fee24e0227f882337521b25b1ed9c36d5b) (https://github.com/kripken/emscripten-fastcomp.git 90b726ede4acf47c1bca089de6c79a0b8f2c5d9a) (emscripten 1.37.18 : 1.37.18)                                                          Target: asmjs-unknown-emscripten Thread model: posix InstalledDir: /home/thiago/Downloads/emsdk/clang/fastcomp/build_incoming_64/bin  "/home/thiago/Downloads/emsdk/clang/fastcomp/build_incoming_64/bin/clang-4.0" -cc1 -triple asmjs-unknown-emscripten -emit-llvm-bc -emit-llvm-uselists -disable-free -main-file-name pfile.cpp -mrelocation-model static -mthread-model posix -mdisable-fp-elim -no-integrated-as -mconstructor-aliases -v -dwarf-column-info -debugger-tuning=gdb -coverage-notes-file /tmp/tmpV3VHOz/pfile_0.gcno -nostdsysteminc -nobuiltininc -resource-dir /home/thiago/Downloads/emsdk/clang/fastcomp/build_incoming_64/bin/../lib/clang/4.0.0 -D __EMSCRIPTEN_major__=1 -D __EMSCRIPTEN_minor__=37 -D __EMSCRIPTEN_tiny__=18 -D _LIBCPP_ABI_VERSION=2 -Werror=implicit-function-declaration -std=c++03 -fdeprecated-macro -fno-dwarf-directory-asm -fdebug-compilation-dir /home/thiago/hello -ferror-limit 19 -fmessage-length 164 -fobjc-runtime=gnustep -fcxx-exceptions -fexceptions -fdiagnostics-show-option -nobuiltininc -nostdsysteminc -isystem/home/thiago/Downloads/emsdk/emscripten/incoming/system/include/libcxx -isystem/home/thiago/Downloads/emsdk/emscripten/incoming/system/lib/libcxxabi/include -isystem/home/thiago/Downloads/emsdk/emscripten/incoming/system/include/compat -isystem/home/thiago/Downloads/emsdk/emscripten/incoming/system/include -isystem/home/thiago/Downloads/emsdk/emscripten/incoming/system/include/SSE -isystem/home/thiago/Downloads/emsdk/emscripten/incoming/system/include/libc -isystem/home/thiago/Downloads/emsdk/emscripten/incoming/system/lib/libc/musl/arch/emscripten -isystem/home/thiago/Downloads/emsdk/emscripten/incoming/system/local/include -isystem/home/thiago/Downloads/emsdk/emscripten/incoming/system/include/SDL -o /tmp/tmpV3VHOz/pfile_0.o -x c++ pfile.cpp clang -cc1 version 4.0.0 based upon LLVM 4.0.0 default target x86_64-unknown-linux-gnu #include "..." search starts here: #include <...> search starts here:  /home/thiago/Downloads/emsdk/emscripten/incoming/system/include/libcxx  /home/thiago/Downloads/emsdk/emscripten/incoming/system/lib/libcxxabi/include  /home/thiago/Downloads/emsdk/emscripten/incoming/system/include/compat  /home/thiago/Downloads/emsdk/emscripten/incoming/system/include  /home/thiago/Downloads/emsdk/emscripten/incoming/system/include/SSE  /home/thiago/Downloads/emsdk/emscripten/incoming/system/include/libc  /home/thiago/Downloads/emsdk/emscripten/incoming/system/lib/libc/musl/arch/emscripten  /home/thiago/Downloads/emsdk/emscripten/incoming/system/local/include  /home/thiago/Downloads/emsdk/emscripten/incoming/system/include/SDL End of search list. [thiago@terra hello]$ emrun --no_browser --port 8080 . 
like image 376
Thiago Silvino Avatar asked Aug 06 '17 18:08

Thiago Silvino


People also ask

Can WebAssembly access files?

WebAssembly allows us to run code written in native languages in the browser. Currently, file system access is not something that works in wasm because the browser did not have any such support so far. But now with this, things are changing.

What can WebAssembly be used for?

With WebAssembly you can develop high-performance web applications using open web platform technologies and various languages. WebAssembly makes it possible to create video, audio, graphics, 3D environments, multimedia games, cryptographic computations, and even portable language implementations.

Can WebAssembly access Dom?

By itself, WebAssembly cannot currently directly access the DOM; it can only call JavaScript, passing in integer and floating point primitive data types. Thus, to access any Web API, WebAssembly needs to call out to JavaScript, which then makes the Web API call.

Is WebAssembly a virtual machine?

WebAssembly (abbreviated Wasm) is a binary instruction format for a stack-based virtual machine. Wasm is designed as a portable compilation target for programming languages, enabling deployment on the web for client and server applications.


1 Answers

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

like image 95
nzeemin Avatar answered Oct 05 '22 23:10

nzeemin