Recently I've run into Cheerp, a C++ to Javascript compiler, which uses a modified version of clang to generate Javascript code from C++ sources.

That makes me wonder: why in the seven kingdoms would someone do this in their right mind?

I mean: why would you take a language that is not designed for web at all, that is far more convoluted and bureaucratic, write your code and then compile it into Javascript itself?

Can anybody see any advantages in doing so?

We surely can discard performance as a reason, because in the end it generates pure Javascript code.

Is there anyone here that have real experience with this?

P.S.: I'm not sure if this is an on topic question, but this is the most general forum about programming that I could find in the StackExchange network.

Edit

Although this seems like a subjective question, it is not.

I am asking for reasons why this tool could be useful. I got interested at first, but started wondering why would someone use it.

closed as primarily opinion-based by gnat, GlenH7, Bart van Ingen Schenau, user40980, jwenting Aug 25 '14 at 11:16

Many good questions generate some degree of opinion based on expert experience, but answers to this question will tend to be almost entirely based on opinions, rather than facts, references, or specific expertise. If this question can be reworded to fit the rules in the help center, please edit the question.

  • 2
    "avoid asking subjective questions where … your question is just a rant in disguise: “______ sucks, am I right?”" (help center). See meta.programmers.stackexchange.com/questions/6483/… – gnat Aug 23 '14 at 17:56
  • This is not entirely subjective. I'm asking for opinions from someone who has experience with this tool, because I got interested at first, but I'm not so sure this can be useful... – Henrique Barcelos Aug 23 '14 at 18:00
  • One reason could be skills inertia. Although somebody with years of C++ exp could switch to almost any language, he/she may be able to whip up code a lot faster using C++ itself. I also question you stating "take a language that is not designed for web at all,...". Most of the popular server-side languages (excluding PHP) were "not designed for the web either". You could easily (with skills + exp) build your backend in C/C++ and you should save plenty a buck on server costs. – Joe Aug 23 '14 at 20:38
  • 1
    I think that many people would actually consider JavaScript more convoluted than C++. – Maciej Chałapuk Aug 24 '14 at 0:35
  • 3
    this is Programmers not SO, why shouldn't the question be opinion based? I thought this was set up so we could have somewhere to post this kind of question? – CashCow Aug 26 '14 at 9:54

Questioned framework is built on top of LLVM, which can be used to compile anything to anything (pretty awesome BTW).

Using LLVM as a cpp-to-js compiler should be taken into consideration when existing C++ program (let's say >=100kLOC) is to be ported to the web browser. From a technical point of view, I see 2 cases:

  1. Creating nice JavaScript API around reusable parts of C++ code and coding the rest JavaScript.
  2. Implementing platform-specific parts in JavaScript and sticking with C++ for the main logic.

Porting almost all C++ GUI programs will require creating browser-based implementation of used UI libraries. If ported program had nothing to do with the network, significant amount of code that handles user accounts and data storage must also be added.

Cheerp provides DOM and GL APIs (that could be used to port GUI libraries) and also implements automagical server function call mechanism (that could greatly simplify writing client-server code). I don't have any experience in using this framework, but after looking at the number of commits in cheerp-clang and cheerp-llvm repositories, I would say that it's pretty mature and worth trying out.

From functionality point of view, I see compile-to-js potential in:

  • Advanced libraries coded in C++ (cryptographic algorithms, neural nets, etc.),
  • Everyday programs (mail clients, IM clients, media players, IDEs),
  • 3D games (check out port of Quake III game to WebGL),
  • Web browsers (as a punishment for excessive usage of recursion :P).

Code compiled from C++ could come off better in microbenchmarks than native JavaScript because of LLVM's compiler optimizations.

  • That is what I'm talking about! Not such things as "laziness of the programmer"... I have "advanced-beginner" experience with C++, but never engaged on a "real world" project with it. In fact, the most important to me is the first item on your list, that did not crossed my mind: being able to use well-known, well-tested and trustable C++ libraries with Javascript, although the others are interesting too. Thank you for your contribution. I'd like to wait for other opinions before marking it as solved, but you have my +1 – Henrique Barcelos Aug 24 '14 at 19:15
  • @HenriqueBarcelos - even if my library was well tested and trustable, C++ has so many nooks and crannies that I wouldn't trust it to cross compile and still be functional. – Telastyn Aug 25 '14 at 3:08
  • @Telastyn - OK, maybe I will elaborate a little on the architecture of LLVM. Front-ends compile from programming languages to language-independent instruction set and type system. Middle layer makes optimizations on LLVM's byte-code. Back ends translates compiled code to machine code or any other language. This kind of design makes LLVM language-agnostic and effective in compiling anything to anything. It works well - please, check out link from my post or just search. <<OPINION>>I think it's actually one of the best pieces of software out there today.<</OPINION>> – Maciej Chałapuk Aug 25 '14 at 20:21
  • @MaciejChałapuk - I understand that. But the C++ -> LLVM step is the problematic part. Depending on the expectations of the library, that may not behave as expected. – Telastyn Aug 25 '14 at 20:55

Not the answer you're looking for? Browse other questions tagged or ask your own question.