26

我遵循了 Webassembly 入门教程http://webassembly.org/getting-started/developers-guide/

它工作正常并显示“你好,世界!” 浏览器中的消息。

然后我尝试了一个小的 C++ 代码,它打开一个文本文件并在读取文件后进行计算 (10 * 20)。

emcc 编译的文件很好,没有错误。

但是当我通过运行 emrun 通过 HTTP 提供文件时,它无法打开文件。

这是我在 emrun Web 控制台中看到的:

Unable to open file
200

从本地磁盘打开文件有什么限制吗?

    [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 .
4

2 回答 2

19

保持安全——WebAssembly 被指定在安全的沙盒执行环境中运行。与其他 Web 代码一样,它将强制执行浏览器的同源和权限策略。

所以简短的回答是——是的,有限制。您无权访问磁盘上的文件。你只有一块内存,可以从 JS 调用 WASM 代码,也可以调用 JS 函数。

但是,Emscripten 中有一个有趣的特性——在 WASM 中,您可以拥有自己的带有文件的“虚拟”文件系统。您可以使用它在编译期间“附加”一些 const 文件并在执行时读取它们。见https://kripken.github.io/emscripten-site/docs/api_reference/Filesystem-API.html

于 2017-08-07T21:43:17.410 回答
11

--embed-file您可以使用该标志将文件或目录打包到 WASM 虚拟文件系统中。

在你的情况下,这看起来像:

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

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

于 2018-05-06T07:59:01.367 回答