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.

user7637745
  • 965
  • 2
  • 14
  • 27
Sophie McCarrell
  • 2,831
  • 8
  • 31
  • 64
  • 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? – UnholySheep Jun 01 '17 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++. – Sophie McCarrell Jun 01 '17 at 18:28
  • 4
    I love C++, but why are you trying to use a spoon to cut the meat? – DIEGO CARRASCAL Jun 01 '17 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 – UnholySheep Jun 01 '17 at 18:37
  • So they have to connect to my server. They need an internet connection and I have to run their code on my server. I want them to be able to run it client side. Where are the scientist in SO?! I want to be able to run C++ code without touching a server, outside the initial load. Another use case, create an app with React Native to teach people C++. Y'all gotta get more creative. Anywho an answer isn't imperative, perhaps no one has solved this yet, in which case I'll leave it open until so. – Sophie McCarrell Jun 04 '17 at 20:33
  • Have you seen Cheerp? [1](https://www.leaningtech.com/cheerp/), [2](https://github.com/leaningtech/cheerp-meta), [3](https://github.com/leaningtech/cheerp-meta/wiki/Cheerp-Tutorial). Look especially at this last link, and see if that's the sort of answer you're looking for. – ShreevatsaR Nov 18 '17 at 20:01
  • 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? – gman Nov 19 '17 at 06:36
  • @JasonMcCarrell Why do we need to get creative? You want this use case so develop the technology to do it :) – Clint Apr 05 '18 at 00:08
  • 1
    This question is pretty dead, but I just wanted to say -- having tried something like this (it was C, not C++, but that turned out to be quite difficult too) you're better off just uploading the code to your server, running it in a sandbox, and piping back down the results. Something like [ideone.com](https://www.ideone.com/). – Nic Oct 26 '18 at 02:38
  • @gman C++ is for real man - they dont need hand holding and interactive tutorial, jsfiddle etc. - all they need is editor and compiler.. okay and a book on c++ – Deian Aug 09 '20 at 00:53
  • I once wrote a "BASIC" language compiler that produced IBM 360/370 assembler code then compiled that and ran it on the mainframe but this is not something to take on casually methinks – Mark Schultheiss Feb 04 '22 at 23:11
  • @Nic OP stated above that they don't want to run the code on the server. Running it in the browser is the point of the question. There are good reasons not to want to do this: security, reducing server load, being able to host an interactive compiler without a server, such as on GitHub pages, letting the user work offline, etc... – ggorlen Apr 27 '22 at 20:01

1 Answers1

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.

Anderson Green
  • 30,230
  • 67
  • 195
  • 328