Recently I\'ve run into Cheerp, a C++ to Javascript compiler, which uses a modif
ID: 647832 • Letter: R
Question
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.
Explanation / Answer
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:
Creating nice JavaScript API around reusable parts of C++ code and coding the rest JavaScript.
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.
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.