11

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:

  1. Compile Emscripten to C, then use Clang to compile it to LLVM, then use Emscripten to compile it to JavaScript.
  2. Compile Clang to LLVM, then compile that to JavaScript through Emscripten.
  3. Write some C++ and run it through JavaScript Clang, then run that LLVM through Emscripten to get JavaSscript
  4. 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.

12
  • 4
    Just for clarification - you want to have the browser (client-side) compile C++ to JS and execute it? So essentially create something like a C++ interpreter? Can you elaborate on the scenario you want this for? Commented Jun 1, 2017 at 18:18
  • 1
    You got it! as-is I'm just curious, but potentially it could be used to allow people to write tools in C++ on the web and test them on the web, without exposing the server to running C++. Commented Jun 1, 2017 at 18:28
  • 4
    I love C++, but why are you trying to use a spoon to cut the meat? Commented Jun 1, 2017 at 18:37
  • 2
    While it's not impossible to do what you ask, your reasoning and the points you listed in your question make me believe you misunderstand how C++ (usually) works. It is usually compiled, not directly executed, so source code does not pose a risk on it's own. It would be far easier to just send the C++ code to a server, have it run emscripten on it and send back the generated JS to the client. Or do what everyone else does and just compile on your own machine Commented Jun 1, 2017 at 18:37
  • 3
    I'm looking for this as well and considered the same solution. Why? Because I want to create interactive C++ learning tutorials that run live on the webpage, no having to install any tools and no special server side service needed. Think static page version of jsfiddle/codepen but for C++. The less good solution would be to run emscripten as a service, send the C++ there and serve the resulting JS but I'm not interested in funding the service. @Jason McCarrel, did you get anywhere? Commented Nov 19, 2017 at 6:36

1 Answer 1

5

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.