Skip to content

Give cpp::Function a null mPtr for generational write barriers. - #1425

Open
Tutez64 wants to merge 1 commit into
HaxeFoundation:masterfrom
Tutez64:bugfix/gc-generational-function-field
Open

Tutez64 wants to merge 1 commit into
HaxeFoundation:masterfrom
Tutez64:bugfix/gc-generational-function-field

Conversation

@Tutez64

@Tutez64 Tutez64 commented Oct 16, 2026

Copy link
Copy Markdown

With HXCPP_GC_GENERATIONAL, Haxe treats a cpp.Function field as an object and emits a write barrier on _hx_v.mPtr in its setter:

inline ::cpp::Function< int  (int) > _hx_set_callback(::hx::StackContext *_hx_ctx,::cpp::Function< int  (int) > _hx_v) { HX_OBJ_WB(this,_hx_v.mPtr) return callback=_hx_v; }

cpp::Function has no mPtr, so any class with such a field fails to compile:

error: 'class cpp::Function<int(int)>' has no member named 'mPtr'

HaxeFoundation/haxe#13002 fixes the generator (HaxeFoundation/haxe#13001), but only for future compilers. This gives cpp::Function a static null mPtr under HXCPP_GC_GENERATIONAL, so the barrier that current compilers emit compiles and does nothing, which is right since a function pointer is not a GC object.

test/haxe, which already builds with HXCPP_GC_GENERATIONAL, now has a cpp.Function field and calls through it. With Haxe 4.3.7 it fails to compile without the change, and passes with it.

With HXCPP_GC_GENERATIONAL, Haxe treats a cpp.Function field as an
object and emits HX_OBJ_WB(this,_hx_v.mPtr) in its setter, but
cpp::Function has no mPtr, so any class with such a field fails to
compile. HaxeFoundation/haxe#13002 stops emitting the barrier; until
every compiler in use has it, a static null mPtr makes the barrier a
no-op, which is right since a function pointer is not a GC object.

test/haxe, built with HXCPP_GC_GENERATIONAL, now has a cpp.Function
field and calls through it.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
dimensionscape added a commit to openfl/libhxcpp that referenced this pull request Oct 16, 2026
With HXCPP_GC_GENERATIONAL, Haxe 4.3.7 writes a barrier on _hx_v.mPtr in
the setter of every cpp.Function field, and cpp::Function had no mPtr,
so any class with such a field failed to compile. A static null mPtr
under HXCPP_GC_GENERATIONAL makes the barrier compile and do nothing;
HX_OBJ_WB skips a null value, and a function pointer is not a GC object.
HaxeFoundation/haxe#13002 stops the generator emitting the barrier, for
compilers that have it.

From HaxeFoundation/hxcpp#1425 by Tutez64.

Fixes #167

Co-authored-by: Enzo Persillet (Tutez) <enzopersillet@gmail.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant