问题
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:
- Compile Emscripten to C, then use Clang to compile it to LLVM, then use Emscripten to compile it to JavaScript.
- Compile Clang to LLVM, then compile that to JavaScript through Emscripten.
- Write some C++ and run it through JavaScript Clang, then run that LLVM through Emscripten to get JavaSscript
- Run the resulting JavaScript!
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.
回答1:
In theory, it may be possible to develop a self-hosting version of Emscripten that runs in a browser, though this hasn't been done yet.
Alternatively, it is possible to run a C++ compiler (or even an entire operating system) using an x86 emulator in JavaScript.
来源:https://stackoverflow.com/questions/44314441/how-do-i-compile-c-to-javascript-in-a-browser