I've found a programming tool called emscripten that can compile c++ code to Javascript code using LLVM. Is it possible to compile C to Javascript using Emscripten (or a similar tool?)
Asked
Active
Viewed 1,834 times
2
-
2Just because I am curious, what situation comes up that this is a solution that is needed? – thatidiotguy Dec 17 '12 at 21:43
-
3emscripten works with C as well – elclanrs Dec 17 '12 at 21:43
-
4@thatidiotguy See Atwoods Law ("any application that can be written in JavaScript will eventually be written in JavaScript"). – John Carter Dec 17 '12 at 21:46
-
1http://cluecc.sourceforge.net/ – melpomene Dec 17 '12 at 21:47
-
@thatidiotguy In some cases, it would be useful to port a C library to Javascript, and emscripten might make it easier to do that. – Anderson Green Dec 17 '12 at 21:47
-
Now go translate this to JavaScript: `__asm__("add %esp, 0x8\n xor %eax, %eax\n ret")` – Dec 17 '12 at 21:51
-
2@H2CO3 http://bellard.org/jslinux/index.html – melpomene Dec 17 '12 at 21:53
-
@melpomene been there, done that. – Dec 17 '12 at 21:55
-
@melpomene Yeah, though that isn't a compiler, it's an x86 emulator. – Dec 17 '12 at 21:55
1 Answers
1
Emscripten doesn't know a thing about C++. It operates on LLVM IR, and cares relatively little how that code was created. You can compile just about anything (that has a LLVM-IR-emitting compiler), including C++, to LLVM IR and pass that to Emscripten.
Note that the result is highly unlikely to resemble human-written JavaScript, and will be unfit for maintenance and further development. It also has some inherent restrictions -- JavaScript isn't machine code, and even if it was, it wouldn't necessarily be compatible to the assumptions of some low-level code.