Convert Javascript to Haxe?

Javascript has tons and tons of libraries out there, with huge mindshare.

Has anyone built or attempted to build a javascript to haxe converter?

This would ultimately allow the effort put into opensource javascript to be reused in haxe/openfl.

NOTE: I’m not interested in building haxe externs to javascript. I’d like to convert javascript to haxe, so great javascript libs can be reused cross-platform via haxe.

Thanks!

I don’t know of any, and there’s probably none out there.

It’s probably not a big technical challenge,
if you hack the v8 engine (or similar) to get javascript => ast (assuming there’s such a thing, but I imagine there is) then all you need is ast => haxe which should be straightforward.

Well at least that’s a theory.

I’ve definitely converted JavaScript to Haxe, but I’ve always done so by hand. Depending on how it’s written, you basically copy and paste, though you need to adapt into classes, generally it isn’t too bad.

I’m not sure how well you could automate it, since it’s untyped?

A bit of inference and Dynamic elsewhere I guess, though that does complicate the conversion.

Even if every variable was declared as dynamic, and it ported somewhat automatically, it’d be a boon to lots of devs.

I’m sure there would be some drawbacks (no compile time type checking), but most libraries don’t need to be optimized to work - it’s only when you find a stress point that you should need to optimize… which I’m sure would come up more often in games, but still, i think would have some merit…

You can also use universal-transpiler to convert a subset of JavaScript into Haxe.

Cool… thanks for the info!

If anyone runs into this post there is also haxe refactor 4.2.0

3 Likes

Ceylon has a thing called flow based typing. According to Wikipedia, its a superset of inferred typing, that allows to fully guess types at compiling stages that are fully dynamic.

I wanted to convert a javascript class to Haxe. (Which is Nakama Backend JS Client, nakama-js.umd.js) in order to do that, I’ve tried to use refactor tool. which is a haxelib tool after installing refactor, I’ve executed the following command:

haxelib run refactor js_to_haxe nakama-js.umd.js

but i get the following error:

Called from ? line 1
Called from Main.hx line 72
Called from hant/Process.hx line 196
Called from hant/Process.hx line 99
Called from a C function
Called from hant/Process.hx line 174
Called from hant/Process.hx line 173
Called from a C function
Called from C:\MyProg\_tools\motion-twin\haxe\std/neko/Lib.hx line 42
Uncaught exception - load.c(237) : Failed to load library : hant.ndll
Stack trace:
        Called from a C function
        Called from hant/Process.hx line 10
        Called from a C function
        Called from hant/Process.hx line 10
        Called from hant/Process.hx line 174
        Called from a C function
        Called from hant/Process.hx line 99
        Called from hant/Process.hx line 196
        Called from Main.hx line 72
        Called from ? line 1

I also tried installing hant lib. nothing changed.

I have used it for action script but I have not tried it with JS. When I ran it with AS I did not encounter any issues.