2017-08-06 119 views
7

我跟着Webassembly入門教程http://webassembly.org/getting-started/developers-guide/我可以使用Webassembly從磁盤讀取文件嗎?

它工作正常,並顯示「你好,世界!」消息在瀏覽器中。

然後我嘗試了一個小的C++代碼,打開一個文本文件並在讀取文件後進行計算(10 * 20)。

emcc編譯的文件就好了,沒有錯誤。

但是,當我通過運行emrun通過HTTP提供文件時,它無法打開文件。

這是我在emrun Web控制檯看到:

Unable to open file 
200 

是否有任何限制從本地磁盤打開的文件?

[[email protected] 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; 
    } 

    [[email protected] 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. 
[[email protected] hello]$ emrun --no_browser --port 8080 . 
+2

當然有限制。如果沒有限制,那麼webassembly將是一個巨大的巨大安全漏洞。 –

+0

你有沒有得到任何解決方案? –

回答

6

保持安全 - WebAssembly指定在一個安全的運行,沙盒 執行環境。與其他網絡代碼一樣,它將強制實施 瀏覽器的相同來源和權限政策。

所以簡短的回答是 - 是的,有限制。您無法訪問磁盤上的文件。你只是有內存塊,可以從JS調用WASM代碼,也可以調用JS函數。

但是,Emscripten中有一個有趣的功能 - 在WASM中,您可以擁有自己的「虛擬」文件系統。你可以使用它在編譯期間「附加」一些const文件並在執行時讀取它們。 見https://kripken.github.io/emscripten-site/docs/api_reference/Filesystem-API.html

相關問題