Skip to content
Advertisement

Convert javascript code to c code [closed]

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.

Advertisement

Answer

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.

User contributions licensed under: CC BY-SA
3 People found this is helpful
Advertisement