4

Is there any program which does this?

I mean, V8 does this at runtime under the hood, but is there any way to output the c++ code?

We need a javascript to c++ converter or translator.

  • 3
    When it comes down to it, any code is just data. It's parsed by the interpreter. It wouldn't be such a longshot that you could take the parsed data, and compile into code that would be readable by C++. V8 does something like this when it interprets at runtime, creating some C-type low level abstraction of the JS code. Watch some V8 stuff and you'll understand what I'm talking about. As for your suggestion to read programming books, I find it to be coming from an unstable and overly emotional place. Perhaps you should meditate. This is a place to be constructive, not to quickdraw. – Funkodebat Mar 25 '15 at 15:33
  • 2
    For the other way, there is emscripten github.com/kripken/emscripten I don't know of a JS->C++ compiler. In the earlier days of compilers, it was quite common to create a compiler that creates C-code and then let a C compiler create the machine-code to support different platforms. Today, there are indermediate "languages" like LLVM or gcc intermediate code from which the compiler creates machine code. – Josef Mar 25 '15 at 16:27
  • 1
    @josef Ah yes, I remember looking at that when I was considering faster 3d / physics implementations in js. – Funkodebat Mar 25 '15 at 20:10
  • Do you want to port JS code to C++, as in translate the code into something that can be maintained, or do you want to compile JS code to C++, as in produce C++ code just to then compile it to native code with a C++ compiler? Or is the purpose to call code written in JS from a C++ program? – Gilles 'SO- stop being evil' Mar 26 '15 at 9:59
  • 1
    Ideally it'd be human readable C++ code, but I've considered if it weren't. Basically I've got some JS code and it needs to go on a microcontroller, so I'll probably have to write some C++ code to do the same thing. – Funkodebat Mar 26 '15 at 20:59
3

The only project I found still working in that direction is JXcore. Initially, they planned to switch from V8 to a LLVM based JS engine in version 2. That goal seems to have moved to "Down the Road" currently and isn't even in the roadmap anymore.

Mozilla was looking into using LLVM in 2009 and stopped that efforts too.

It seems there is no speed gain in compiling JavaScript to machine code (modern engines do that with their jit compilers for the parts of the code where it makes a difference) and the work just isn't worth it.

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