22

I've got a bunch of math/dsp algorithms in C. Single functions, one function per file, no fancy linking or includes or preprocessor directives, only c99 and standard library calls (mainly memset and memmoves to handle array copying etc). Translating them is not a big problem, but it takes time. I was wondering if there was some C -> Javascript translation tool, in order to speed up the work.

Please mind I don't need a perfect translation, I will put my hand on the final result anyway. What I need is a simple tool that helps with the basic, "boring" things.

Anyone heard of such a thing?

Edit: seems that emscripten will do the trick.

janesconference
  • 6,333
  • 8
  • 55
  • 73
  • 4
    The way that C allows pointers to be used, such a tool would surely fail on all but the most contrived examples. – cdhowie Nov 18 '10 at 01:24
  • 1
    I'm noot looking for a perfect translator that can understand the pointer logic, I'm looking for a mildly-stupid workhorse that can do the boring parts automatically. – janesconference Nov 18 '10 at 01:32

2 Answers2

26

Update: it seems The Times They Are a-Changin' :)

See this article: DOOM Ported to JavaScript and HTML5

Zakai compiled the DOOM source code from C to JavaScript using Emscripten and Clang and optimized it with Closure Compiler.

And here is the Project page. Have fun! :)

gblazex
  • 49,155
  • 12
  • 98
  • 91
  • 4
    ahah i found the way: translate c to Java with this: http://www.axiomsol.com/, then translate Java to Javascript with GWT (just joking). – janesconference Nov 18 '10 at 10:33
  • 1
    Anyway: 1) You parse C and, if you realze it's an integer division (you look at the types of the operands), you Math.floor the result in js 2) You don't care what is useless, you pass the parameter anyway in the translated script. It's redundant, not incorrect. Mind I don't look for a translator that optimizes, I'm gonna do do the optimizations. 3) Looks more complex, but you can always lokk at the array type and calculate how many elements you're copying. 3b) Objects in C? You're talking about structures or you're thinking about OO techniques in C? – janesconference Nov 18 '10 at 10:41
  • See my update, it seem some folks got it working for DOOM :) – gblazex Jun 03 '11 at 17:51
  • I see you found the project. Why isn't your accepted answer on the top? I already missed it :) – gblazex Jun 03 '11 at 17:53
  • The trick used it emulating a CPU. It's... really inefficient! But it works :) – Lodewijk Jul 26 '14 at 19:41
  • Unfortunately, your link to the ultimate proof of concept was [taken down by id Software](https://wiki.mozilla.org/Legal/Infringement_Notices/3_June_2011) – Jongware Jul 28 '14 at 08:39
  • @janesconference +1 :) –  Nov 28 '15 at 00:15
  • readwrite updated their link structure, heres the new link http://readwrite.com/2011/05/31/doom-ported-to-javascript-and/ – Daveman May 14 '16 at 13:00
19

FYI, Emscripten does exactly that.

neu242
  • 15,796
  • 20
  • 79
  • 114
janesconference
  • 6,333
  • 8
  • 55
  • 73