Convert javascript code to c code

JavascriptCCompiler Construction

Javascript Problem Overview


Is there any way to convert C code to JavaScript and from JavaScript to C? I found V8 juice which can generate JavaScript-side classes from C++, but it's only one way (C++ to JavaScript).

I'm not looking for a software.

Javascript Solutions


Solution 1 - Javascript

Very, very tricky --- Javascript is a heavily dynamic language where pretty much everything can be changed at run time: names of variables, functions, types, etc. As such it maps very badly onto C. And that's not even considering eval(), which will let you construct arbitrary chunks of Javascript in strings and run them.

Any Javascript translator would have to be able to cope with such things, which means it would have to translate the Javascript into C at run-time --- which makes it a JIT, which you're already using.

You may want to look at writing C bindings for Javascript instead. These will allow your Javascript code to call out to C code and vice versa. This would allow people to write plugins in C, compile them into .so shared libraries, which you can now load and run from your Javascript code. This means you don't need to translate anything.

Javascript's not my area so I can't recommend any particular mechanism, I'm afraid --- but I'd be very surprised if V8Juice, which you've already found, didn't let you do this.

Solution 2 - Javascript

There are a few compilers that translate JavaScript and TypeScript to C:

  • QuickJS compiles JavaScript to C using an embedded JavaScript engine. This implementation is fairly complete, unlike the other compilers listed here.
  • ts2c translates JavaScript and TypeScript source code to C, but it only supports a small subset of JavaScript's features.
  • NectarJS compiles a subset of JavaScript to C or WebAssembly.

Similarly, it may be possible to compile some statically-typed JavaScript programs to WebAssembly using AssemblyScript, and then decompile them to C using wasm2c.

Alternatively, it might be possible to compile JavaScript to another language that compiles to C:

  • Compile JavaScript to Python, and then compile Python to C using Cython or RPython. Since these compilers are compatible with a subset of Python, this should allow a subset of JavaScript to be translated to C.
  • Compile JavaScript to Lua using Castl, and then translate the Lua code to C using lua2c.

Solution 3 - Javascript

This project looks quite promising, even though it is a work in progress.

https://github.com/andrei-markeev/ts2c

You can convert JavaScript to C online here:

https://andrei-markeev.github.io/ts2c/

Solution 4 - Javascript

Why convert when you can simply embed?

https://code.google.com/p/v8/ "V8 can run standalone, or can be embedded into any C++ application."

Being embedded into a C++ application allows JavaScript to access any system that the C++ application has access to, eliminating the need to convert in the first place. I would limit what is has access to somewhat for security reasons though. Web-browsers are obviously the most prominent form of JavaScript being embedded in a C++ application. As implied by the name, JavaScript is a scripting language, not intended to be compiled into assembly/machine code as is C code.

Solution 5 - Javascript

The QuickJS JavaScript interpreter from Fabrice Bellard also includes a JS to C compiler.

Solution 6 - Javascript

C language can do hell of lot stuff that javascript or v8 engine can't do, specifically system program such as memory management, direct memory access, interrupts, assembly code, thread management. So it is not possible to convert c to javascript unless it is very simple code without some of system code. With nodeJS (not v8 javascript) along with system wrapper code do some of system level functions. Again it is complex part of conversion. Even if we able to convert this simple code the execution of c and converted javascript differs as it it is based on v8 engine.

It is possible to convert javascript to C. Because C can virtually handle any execution stuff including that of interpreters.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionWassim AZIRARView Question on Stackoverflow
Solution 1 - JavascriptDavid GivenView Answer on Stackoverflow
Solution 2 - JavascriptAnderson GreenView Answer on Stackoverflow
Solution 3 - JavascriptarpoView Answer on Stackoverflow
Solution 4 - Javascriptuser3015682View Answer on Stackoverflow
Solution 5 - JavascriptceilingcatView Answer on Stackoverflow
Solution 6 - JavascriptMallikView Answer on Stackoverflow