HaxeFoundation / HaxeFoundation/haxe

[cpp] Unable to use Class references tagged with `@:nativeGen`

Open
#12,534 1 comment 0 reactions 0 assignees View on GitHub
Dominant language
Haxe
Stars
6.9k
Forks
715
Avg merge
2d 2h
Merged PRs (30d)
11

Description

I was testing with the capabilities of `@:nativeGen` when I noticed that apparently if you want to reference a class for various reasons (for example, `Std.isOfType()`) it will return false regardless if it's a correct input.

Looking further, it seems to be because the Haxe compiler automatically sets `null` to any kind of reference of the core class.

Example Source:
```haxe
package;

@:nativeGen
class MyClass {
public function new(?arg:String)
{
trace(arg);
}
}
@:nativeGen
class MyOtherClass extends MyClass
{
public function new()
{
super("Overrided");
}
}

@:nativeGen
class Main
{
static function main() {

var int = Std.parseInt("234");

var thing = MyClass;

if (MyOtherClass is MyClass)
trace(int);
new MyClass("My Class Test");
}
}
```

Output:

```cpp
// Generated by Haxe 4.3.6
#include

#ifndef INCLUDED_6dd33a6ac0a7028f
#define INCLUDED_6dd33a6ac0a7028f
#include "Main.h"
#endif
#ifndef INCLUDED_f94a69a5dd97ef77
#define INCLUDED_f94a69a5dd97ef77
#include "MyClass.h"
#endif
#ifndef INCLUDED_1dc702ddb70657b6
#define INCLUDED_1dc702ddb70657b6
#include "MyOtherClass.h"
#endif
#ifndef INCLUDED_58b6f7d5e7ae9899
#define INCLUDED_58b6f7d5e7ae9899
#include "Std.h"
#endif
#ifndef INCLUDED_haxe_Log
#include
#endif

HX_LOCAL_STACK_FRAME(_hx_pos_e47a9afac0942eb9_22_main,"Main","main",0xed0e206e,"Main.main","Main.hx",22,0x087e5c05)

void Main::main(){
HX_STACKFRAME(&_hx_pos_e47a9afac0942eb9_22_main)
HXLINE( 24) ::Dynamic _hx_int = ::Std::parseInt(HX_("234",53,1d,26,00));
HXLINE( 26) ::Dynamic thing = null();
HXLINE( 28) if (::Std::isOfType(null(),null())) {
HXLINE( 29) ::haxe::Log_obj::trace(_hx_int,::hx::SourceInfo(HX_("Main.hx",05,5c,7e,08),29,HX_("Main",59,64,2f,33),HX_("main",39,38,56,48)));
}
HXLINE( 30) ::MyClass(HX_("My Class Test",8e,3d,75,75));
}

```

the expected output should be that the sequence "234" is outputted, but not in this case.

Contributor guide

Open the contributing guide

Research direction

Start by reproducing the issue with the Haxe source example and inspect the generated C++ around `thing` and the `MyOtherClass is MyClass` check. Trace why these class references become `null`; done means the generated program outputs `234` while preserving the shown class construction behavior.

Written by the indexing model from the issue text.

Assessment

Tech stack
cpp
Domain
compilers
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Clearly specified
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.