I'm aware of Emscripten and LLVM, but neither are written in JavaScript intended for a browser.
As far as I can tell, the tools exist, but they haven't been put together, but I could very well be missing some key factor that makes it very difficult to compile C++ to JavaScript in a browser.
So I'll mention my naive implementation:
I'm sure I'm missing something from my steps. Please let me know, and let me know if there are any efforts by the community to resolve those missing pieces.
EDIT: JSCPP is the closest I've found, but it lacks a lot of features and frankly the author took on an absurd undertaking, by trying to rewrite the parser etc. on his own, rather than using existing tools. Still, close and pretty neat.
Running a C program in a browser using the standard library doesn't make any sense. Even if you had the whole C library available, you wouldn't be able to use it. The browser doesn't allow interacting with the system directly from its JavaScript runtime, it's a security measure. What's the ultimate goal?
In contrast, JavaScript has no compilation step. Instead, an interpreter in the browser reads over the JavaScript code, interprets each line, and runs it. More modern browsers use a technology known as Just-In-Time (JIT) compilation, which compiles JavaScript to executable bytecode just as it is about to run.
js can dynamically load an external C or C++ DLL file at runtime and utilize its API to perform some operations written inside it from a JavaScript program. This is basically how a Native Addon works in Node. js.
It is possible to compile C++ to JavaScript using a self-hosting version of Emscripten that runs in a browser, such as emception.
Alternatively, it is possible to run a C++ compiler (or even an entire operating system) using an x86 emulator in JavaScript.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With