我可以使用Webassembly从磁盘读取文件吗? [英] Can I read files from the disk by using Webassembly?

查看:846
本文介绍了我可以使用Webassembly从磁盘读取文件吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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.

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

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

emcc可以很好地编译文件,没有错误。

emcc compiled the file just fine, no errors.

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

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

这是我在Emrun Web控制台中看到的内容:

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 .


推荐答案


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

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.

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

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.

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

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

这篇关于我可以使用Webassembly从磁盘读取文件吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆