Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can Haxe compiler convert any C# code to Haxe?

Tags:

c#

haxe

First I am super new to Haxe, so I am asking super noob question here, and I have 2 of them.

1) My first question is I know Haxe compiler can convert from Haxe to any other language (supported) but can it convert source code in other languages(supported) to Haxe?

2) If it can do so how much is its capability like can it only convert a few files lets say in C# to Haxe or if I give a huge repo like lets say this https://github.com/dotnet/corefx or https://github.com/tensorflow/tensorflow will it convert everything in it (ya I know this is too much to ask, but anyways)??

like image 392
Vignesh S Avatar asked Dec 11 '22 11:12

Vignesh S


2 Answers

No, the Haxe compiler only transpiles in one direction, which is:

Haxe Code ⭢ Selected Target

For the other direction, you'd need an external tool independent of the Haxe compiler. Over the years, several such tools were created (some abandoned):

  • ActionScript 3 ⭢ Haxe:
    • as3hx
    • ax3
  • Java ⭢ Haxe:
    • java2haxe
    • JTransc
  • C# ⭢ Haxe:
    • CS2HX
    • Phase
  • PHP ⭢ Haxe:
    • PhpToHaxe
  • Go ⭢ Haxe:
    • TARDIS Go
  • TypeScript ⭢ Haxe:
    • node-ts2hx

There's also some tools for generating externs, rather than compiling source-to-source:

  • TypeScript ⭢ Haxe:
    • ts2hx
    • haxe-dtstohaxe
    • dts2haxe
    • dts2hx
  • Python ⭢ Haxe:
    • pyextern
like image 179
Gama11 Avatar answered Dec 25 '22 07:12

Gama11


Gama11 answer is correct but -net-lib and externs should be added since they do allow you to use c#/mono code in your Haxe c#/mono project.

Haxe can consume compiled code for all targets using externs ( compiler definitions ) for use within your Haxe project for that same target, for most targets you need to create suitable externs manually. However for some targets ( Java, C#, Flash ) Haxe can auto generate the externs required, although not always successful it can make it fairly painless to use existing libraries. I have found this very successful with flash and had varied success with Java target but it is always improving, and perhaps tried it once or twice with mono ( c# target ). In your hxml complier code you may write.

-swf-lib my.swf
-java-lib my.jar
-net-lib my.dll

You cannot use a a swf or jar file with a c# project, nor a dll with a flash or java project, atleast not this way, although you can use a swf with openfl and nme when targetting c++ via special parsing libaries.

Technically for C# this will be described in the future Haxe manual more formally than I have, see link to github markdown page for full details:

"Haxe can directly load .NET assembly files (.DLL) and convert its type definitions for use as Haxe types. To load a .NET assembly, use -net-lib library.dll compiler option. Haxe will then automatically parse types defined in that assembly file and make them available for import as Haxe types." - Quote https://github.com/HaxeFoundation/HaxeManual/wiki/Haxe-C%23

like image 24
Justinfront Avatar answered Dec 25 '22 06:12

Justinfront