64

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.

tshepang
  • 12,111
  • 21
  • 91
  • 136
Wassim AZIRAR
  • 10,823
  • 38
  • 121
  • 174
  • 2
    I'm running now node.js to execute javascript code under my linux server, so I want to let other people to add plugins by writing c code. This is why I need a library or an engine like v8 juice to convert c code to javascript automaticaly. – Wassim AZIRAR Mar 04 '11 at 10:12
  • Not an Answer, just a comment: There is an JScript compiler windows/.NET brings. It's called jsc.exe. It can't compile every JavaScript. If it is installed it's located either in c:\Windows\winsxs or in C:\Windows\Microsoft.NET\Framework\. If you have an plain javascript algorithm without fancy stuff, it may work. – Laokoon Oct 06 '16 at 07:48
  • It doesn't make any sense to me. – Rolf Nov 03 '16 at 11:04
  • 1
    Just use Haxe, much better but similar language that compiles to all targets imaginary. From Javascript to C, runs on devices, servers, browsers, even embedded and airborne software. – Ska Jan 04 '17 at 14:51
  • until now I have been manually translating javascripts to c++, and even a partial parser would be helpful making the translation easier, however I have not really seen any worth mentioning, so I guess you will have to do it manually – serup Jan 26 '17 at 07:39
  • @Ska do you have an example of haxe running on embedded and airborne hardware? – nurettin Nov 27 '17 at 07:07

6 Answers6

47

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.
Anderson Green
  • 30,230
  • 67
  • 195
  • 328
47

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.

David Given
  • 13,277
  • 9
  • 76
  • 123
  • 9
    Nonetheless, computer programs are deterministic so it's theoretically possible to compile everything back into C because all the mechanisms used in interpreting JavaScript exist in C already. Someone would have to spend time making that tool. Who's down to volunteer? x} – trusktr Dec 13 '15 at 08:06
  • 1
    It's possible and very easy. All you have to do is put PHP as a preprocessor for C instead of what it now uses(#ifdef won't cut it if you want the resulting code to be readable, and you will need constructs like generic maps to do the conversion). After that, all you do is create a wall between "compile-time" code and "runtime-code", after this, it's trivial to marshall js to c and visa versa via json, and let c evaluate javascript to internal representation of javascript, but to do this and have nice code, you need a more powerful preprocessor for c. – Dmytro Aug 10 '16 at 03:27
  • Sadly at the moment there is no "nice way" to have maps in C, all of them are ugly and messy and bug prone, and javascript relies almost exclusively on maps. You can do it in C++ but At this point you may as well just embed V8 because it's written in C++ and will do it better than you will, but won't have "absolute free unmangled abstractions and gdb-friendliness" that C has. With a more powerful preprocessor, C will easily be able to bridge to any upward language(that supports runtime generics via objects, whereas in C you need to invent an object to store the metadata of void pointers) – Dmytro Aug 10 '16 at 03:29
  • I emphasize the preprocessor because the whole purpose of translating ANYTHING to C, is to turn your abstractions into free and gdb-friendly abstractions. That is, you gain fine-grained control over where your data is, how it is stored at any moment, and have absolutely no mangling and have perfect debuggability at per-instruction level. This is absolutely possible, but all attempts to do so never quite made the golden bridge from C to javascript, if we would, C++ would not exist since C would just be the best language to use at any level of programming. Sadly C needs new toys. – Dmytro Aug 10 '16 at 03:42
9

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/

arpo
  • 1,831
  • 2
  • 27
  • 48
8

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.

user3015682
  • 1,215
  • 12
  • 13
5

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

ceilingcat
  • 671
  • 7
  • 11
  • Unfortunately qjsc is not a JS to C transpiler; it merely outputs a bytecode for Bellard's interpreter, as a C array. I believe the OP (like myself) was looking for something that output idiomatic C, at least where possible. ts2c crashes on every input I've tried. Still looking. – Graham Toal Mar 10 '23 at 22:06
1

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.

Mallik
  • 61
  • 5