JSCPP icon indicating copy to clipboard operation
JSCPP copied to clipboard

CPP -> JS compiler

Open nickkolok opened this issue 9 years ago • 7 comments

Is it possible not to execute C++ code but only translate it to JS, which could be written to a file and then executed?

nickkolok avatar Jul 28 '15 23:07 nickkolok

This was my first thought when I started this project. However, given how many machine/platform/C++ specific behaviors exist in C++, most of the operations need to done with complex logic.

For example, a=b+c involves type compatibility checking, + operator overriding, integer promotion, integer to float promotion, unsigned to signed promotion, overflow checking, implicit type conversion, assignment operator overriding and maybe some other things. So I am not confident enough to be able to compile C++ into a human-readable JavaScript source file.

If we don't aim for readability, then Emscripten already does that pretty efficiently with asm.js.

felixhao28 avatar Jul 29 '15 14:07 felixhao28

Thank you, I've read about Emscripten, and then my first thought was: could we emscript Emscripten with Emscripten to get Emscripten in browser? Sounds a bit crazy, and perharps the resulting emscripted Emscripten would be too big and/or slow to use it for real purposes... Or Emscripten is not emscriptable. Moreover, in that case we should emscript a C++ compilre.

nickkolok avatar Jul 29 '15 21:07 nickkolok

Emscripten uses LLVM, so you'd need to compile that first. :P

RangerMauve avatar Jul 29 '15 21:07 RangerMauve

You guys are right. Emscripten runs offline since it requires a local C++ compiler to generate LLVM for it first. Although they seem to have an example of Emscriptened Clang Compiler and llvm.js but I cannot get these two working together (and it is slow).

felixhao28 avatar Jul 30 '15 01:07 felixhao28

One possible way could be to use CFRONT (https://github.com/mingodad/cfront-3) generating Javascript instead of C CFRONT itself compiled to WASM, the later is something I'm thinking to experiment with.

mingodad avatar Nov 07 '21 18:11 mingodad

Just found this project https://github.com/filhodanuvem/cloudblocks that says:

Transpiler from C to Javascript made as a final project in UFJF university.

It uses a bison grammar I'll look at it to see how far it goes.

mingodad avatar Nov 07 '21 19:11 mingodad