0

Sorry for the basic question here. I'm trying to wrap my head around Emscripten, ASM.js, and compiling lower level languages to javascript.

Here is what I don't understand. You can do things in a native C program that you can't (and shouldn't) do in browser based js. For example, in a native app you can access or the file system or shut the computer down.

Suppose I wrote a C program that reads /etc/passwd and then shuts the computer down. Then, I compiled that program to js and popped it in a <script> tag. What would happen when I visit the page with the <script> tag in it? Obviously, it's not going to shut the computer down, but would it even compile?

It just seems to me that javascript running in a browser is so limited compared to lower level languages that I can't see how any meaningful applications can be simply compiled to js without totally breaking it.

Dominic P
  • 2,284
  • 2
  • 27
  • 46

1 Answers1

2

Check Emscripten docs. For example, it says:

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.

And if you scroll down there, you'll find this:

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.

I believe that this should answer your question. At the end of the day, Emscripten/ASM will try to emulate low-level details with JavaScript-based polyfills.

Matías Fidemraizer
  • 63,804
  • 18
  • 124
  • 206
  • Thanks for the info. I did see that part of the docs actually, and it kind of makes sense. I guess I just see the things that can be done in the browser as such a small subset of the things a native app can do that I don't see how one can simply "compile" a native app to run in the browser. Does that make sense? – Dominic P Sep 05 '16 at 18:10
  • @DominicP Actually Emscripten is a transpiler from ASM to ASMJS, and Emscripten provides a set of JavaScript APIs to mimic native ones. That's all. How it does it? It translate native calls into JavaScript/ASMJS calls to their equivalents in Emscripten running either in the browser or Node. – Matías Fidemraizer Sep 05 '16 at 20:16
  • yes, that much makes sense. But, what would be the JavaScript equivalent of the native call to shut down the computer, for example? Or, the native call to read data from a serial port? Or, the native call to read SMART data from a connected drive? Or... How could these kinds of things be mimiced in js? – Dominic P Sep 05 '16 at 23:24
  • @DominicP If something isn't supported, it won't be translated to JS at all. Obviously, Emscripten/ASMJS are limited to what JavaScript can do. I don't have a definitive answer to each case where JS has a limitation, but you can be sure that you can't use a serial port or shutdown a computer from a browser-based app. But Node can do it already so it will depend on the target enviroment. – Matías Fidemraizer Sep 06 '16 at 04:48