HaxeFoundation / HaxeFoundation/haxe
[cpp] HXCPP_GC_GENERATIONAL emits an invalid write barrier for cpp.Function fields
- Dominant language
- Haxe
- Stars
- 6.9k
- Forks
- 715
- Avg merge
- 2d 2h
- Merged PRs (30d)
- 11
Description
When `HXCPP_GC_GENERATIONAL` is enabled, assigning a `cpp.Function` instance field emits a generational write barrier even though `cpp::Function` is a native function-pointer wrapper rather than a GC object.
This affects Haxe 4.3.7 and the current development branch (`2f724abd255c5c0204d350c55bc64068c18c4ad1`).
Minimal reproduction:
```haxe
class Main {
static function main():Void {
var holder = new NativeCallbackHolder(cpp.Callable.fromStaticFunction(increment));
if (holder.callback.call(41) != 42) {
throw "Invalid native callback result";
}
}
static function increment(value:Int):Int {
return value + 1;
}
}
class NativeCallbackHolder {
public var callback:cpp.FunctionInt, cpp.abi.Abi>;
public function new(callback:cpp.FunctionInt, cpp.abi.Abi>) {
this.callback = callback;
}
}
```
```hxml
-main Main
-D HXCPP_GC_GENERATIONAL
-cpp out
```
The generated setter uses `HX_OBJ_WB_SET` for the field. That macro expects an object-like value with an `mPtr` member, which `cpp::Function` does not have, so the generated C++ fails to compile:
```text
error: cpp::Function has no member named mPtr
```
Without `HXCPP_GC_GENERATIONAL`, the same program compiles and runs successfully.
Expected behavior: `cpp.Function` fields should not receive GC write barriers, and the program should compile and return the expected callback result.
Contributor guide
Research direction
Compile the provided Main and NativeCallbackHolder reproduction with Haxe 4.3.7 or the development branch, using -D HXCPP_GC_GENERATIONAL and -cpp out. Inspect the generated C++ setter and its HX_OBJ_WB_SET use; done means the generated code compiles and the program returns the expected callback result without an invalid write barrier.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- cpp
- Domain
- compilers
- Issue type
- Bug
- Difficulty
- 3/5
- Estimated time
- 1-2 days
- Activity status
- Quiet
- Clarity
- Clearly specified
- Newbie friendliness
- 68/100