Skip to content

[cpp] HXCPP_GC_GENERATIONAL emits an invalid write barrier for cpp.Function fields #13001

Description

@Tutez64

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:

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.Function<Int->Int, cpp.abi.Abi>;

	public function new(callback:cpp.Function<Int->Int, cpp.abi.Abi>) {
		this.callback = callback;
	}
}
-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<int(int)> does not have, so the generated C++ fails to compile:

error: cpp::Function<int(int)> 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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions