很抱歉问这个基本问题。我正在尝试了解 Emscripten、ASM.js,并将较低级别的语言编译为 javascript。
这是我不明白的地方。您可以在 native C 程序中执行在基于浏览器的 js 中无法(也不应该)执行的操作。例如,在 native 应用程序中,您可以访问文件系统或关闭计算机。
假设我编写了一个 C 程序来读取/etc/passwd,然后关闭计算机。然后,我将该程序编译为 js 并将其弹出到 <script>
中。标签。当我访问 <script>
的页面时会发生什么标签在里面吗?显然,它不会关闭计算机,但它会编译吗?
在我看来,与较低级别的语言相比,在浏览器中运行的 JavaScript 是如此有限,以至于我无法看到如何在不完全破坏它的情况下将任何有意义的应用程序简单地编译为 js。
最佳答案
检查Emscripten docs 。例如,它说:
Applications compiled with Emscripten usually expect synchronous I/O, so Emscripten itself provides file systems with completely synchronous interfaces.
However, due to JavaScript’s event-driven nature, most persistent storage options offer only asynchronous interfaces. Emscripten offers multiple file systems that can be mounted with FS.mount() to help deal with persistence depending on the execution context.
如果你向下滚动,你会发现这个:
MEMFS
This is the default file system mounted at / when the runtime is initialized. All files exist strictly in-memory, and any data written to them is lost when the page is reloaded.
我相信这应该可以回答你的问题。最终,Emscripten/ASM 将尝试使用基于 JavaScript 的填充函数来模拟低级细节。
关于javascript - 了解 Emscripten/ASM.js 和浏览器沙箱,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39325711/