HACKER Q&A
📣 sallahualaM

C/C++ plugin make JavaScipt end up with C/C++ binary?


Happen to know if there's C/C++ plugin or a separately standalone PL of a compiler which is mostly (~90-93-%) JavaScipt but compile to or end up with C/C++ binary?


  👤 gregjor Accepted Answer ✓
There's no such thing as a "C/C++ binary." C and C++ both get compiled into machine code, and we call the result a binary, or executable. Executables, or binaries, contain low-level machine instructions, regardless of the original source language.

Javascript does not get compiled to an executable. It gets interpreted from source, or (more likely) just-in-time compiled into bytecode, which then gets executed by a runtime -- the Javascript interpreter in a browser, or Node.js on a server.

Python, Ruby, PHP, and numerous other languages are called interpreted because they require an interpreter and runtime libraries to execute. C, C++, Pascal, Go, Rust get compiled to executable binaries.

I can't think of any reason Javascript can't get compiled to a binary, or translated to C or C++, but that's not how Javascript generally gets executed and deployed.


👤 sargstuff
There are virtual machine plug-ins that add interpreted C/C++ to environments (browser, c/c++ interpreter for command line shell, gnumeric/excell, ....).

There are tools which can be run in same developer environment that javascript is run in which can be used to translate javascript into compilable C/C++ code (perhaps also adding/including the step of compilation of c/c++ code into runable binary)

Typically, a vm which directly interprets/runs javascript is used in place of the transpile steps (change javascript into c/c++ source code / compile source / run source)


👤 _ua_
I don't have personal experience using this but have come across https://github.com/surma/jsxx.

👤 stefanos82
Just go with quickjs, I think this is what you are looking for.

https://bellard.org/quickjs/