Skip to content

[cpp] Fix field access on AtomicObject results - #13095

Merged
Simn merged 2 commits into
developmentfrom
fix-cpp-atomic-object-load
Oct 30, 2026
Merged

Simn merged 2 commits into
developmentfrom
fix-cpp-atomic-object-load

Conversation

@Simn

@Simn Simn commented Oct 29, 2026 •

Copy link
Copy Markdown
Member

On C++, accessing a field directly on the result of AtomicObject.load, store, exchange or compareExchange fails to compile:

class C {
	public var x = 1;
	public function new() {}
}

function main() {
	final a = new haxe.atomic.AtomicObject(new C());
	trace(a.load().x); // error: 'class hx::Object' has no member named 'x'
}

Cause. These methods are inline and return an untyped __global__.__hxcpp_atomic_object_* call, which returns an hx::Object in C++. After inlining, the expression has the concrete type, C here, so hxcpp doesn't insert a conversion and generates ->x on the hx::Object.

Assigning the result to a variable or passing it to a function happens to work, because those paths convert it, so this went unnoticed. hxcoro ran into it when it accessed a field on a loaded value (HaxeFoundation/hxcoro#192).

Fix. As @Aidan63 suggested, AtomicObject and AtomicInt now bind the typed hx::atomic::AtomicObject and hx::atomic::AtomicInt classes from HaxeFoundation/hxcpp#1441 as @:cpp.ManagedType externs, instead of using untyped calls. With typed calls, hxcpp converts the results itself. The methods stay inline, and the cppia externs are unchanged. This requires hxcpp with #1441.

Test. TestAtomicObject.testFieldAccessOnResult accesses a field on the result of each method. With current hxcpp master, the cpp and cppia unit tests pass locally on Windows.

🤖 Generated with Claude Code

load, store, exchange and compareExchange are inline and return untyped
__global__ calls, which return hx::Object in C++. After inlining, the
expression has the concrete type, so hxcpp doesn't convert it, and a
field access directly on the result, like a.load().field, generated a
member access on hx::Object that failed to compile. The cast makes hxcpp
convert the result.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@Simn

Simn commented Oct 29, 2026

Copy link
Copy Markdown
Member Author

@Aidan63 Don't we have much nicer ways to expressing this stuff nowadays?

@Aidan63

Aidan63 commented Oct 29, 2026

Copy link
Copy Markdown
Contributor

Yeah, cpp.ManagedType externs is what I've been moving internal implementations over to get rid of all this untyped and casting stuff (see Mutex.hx as an example).
It does require additions on the hxcpp side though to add properly typed code instead of the dynamic based functions it currently has. That's super simple for a very small self contained API like the atomic stuff, so I can look at that if you'd like.

@Simn

Simn commented Oct 29, 2026

Copy link
Copy Markdown
Member Author

That would be great, thanks!

@Aidan63

Aidan63 commented Oct 29, 2026

Copy link
Copy Markdown
Contributor

Once HaxeFoundation/hxcpp#1441 is merged you should be able to bung the following two files into this merge to get the typed version of the API, seems to work fine for me. I assume there are other areas of the haxe std which has the same issue for cpp.

AtomicInt.hx

package haxe.atomic;

@:include("hx/atomic/AtomicInt.hpp")
@:cpp.ManagedType({ type : "AtomicInt", namespace : [ "hx", "atomic" ], flags : [ StandardNaming ] })
private extern class AtomicIntImpl {
	function new(value:Int);

	function fetchAdd(value:Int):Int;
	function fetchSub(value:Int):Int;
	function fetchAnd(value:Int):Int;
	function fetchOr(value:Int):Int;
	function fetchXor(value:Int):Int;
	function load():Int;
	function store(value:Int):Int;
	function exchange(value:Int):Int;
	function compareExchange(expected:Int, replacement:Int):Int;
}

private typedef AtomicIntData = AtomicIntImpl;

abstract AtomicInt(AtomicIntData) {
	public #if !scriptable inline #end function new(value:Int):Void {
		this = new AtomicIntImpl(value);
	}

	public #if !scriptable inline #end function add(b:Int):Int {
		return this.fetchAdd(b);
	}

	public #if !scriptable inline #end function sub(b:Int):Int {
		return this.fetchSub(b);
	}

	public #if !scriptable inline #end function and(b:Int):Int {
		return this.fetchAnd(b);
	}

	public #if !scriptable inline #end function or(b:Int):Int {
		return this.fetchOr(b);
	}

	public #if !scriptable inline #end function xor(b:Int):Int {
		return this.fetchXor(b);
	}

	public #if !scriptable inline #end function compareExchange(expected:Int, replacement:Int):Int {
		return this.compareExchange(expected, replacement);
	}

	public #if !scriptable inline #end function exchange(value:Int):Int {
		return this.exchange(value);
	}

	public #if !scriptable inline #end function load():Int {
		return this.load();
	}

	public #if !scriptable inline #end function store(value:Int):Int {
		return this.store(value);
	}
}

AtomicObject.hx

package haxe.atomic;

@:include("hx/atomic/AtomicObject.hpp")
@:cpp.ManagedType({ type : "AtomicObject", namespace : [ "hx", "atomic" ], flags : [ StandardNaming ] })
private extern class AtomicObjectImpl {
	function new(value:Dynamic);

	function load():Dynamic; 
	function store(value:Dynamic):Dynamic;
	function exchange(value:Dynamic):Dynamic;
	function compareExchange(expected:Dynamic, replacement:Dynamic):Dynamic;
}

private typedef AtomicObjectData<T:{}> = AtomicObjectImpl;

abstract AtomicObject<T:{}>(AtomicObjectData<T>) {
	public #if !scriptable inline #end function new(value:T):Void {
		this = new AtomicObjectImpl(value);
	}

	public #if !scriptable inline #end function compareExchange(expected:T, replacement:T):T {
		return this.compareExchange(expected, replacement);
	}

	public #if !scriptable inline #end function exchange(value:T):T {
		return this.exchange(value);
	}

	public #if !scriptable inline #end function load():T {
		return this.load();
	}

	public #if !scriptable inline #end function store(value:T):T {
		return this.store(value);
	}
}

Instead of casting the results of the untyped __global__ calls, bind
hx::atomic::AtomicObject and hx::atomic::AtomicInt (added in
HaxeFoundation/hxcpp#1441) as @:cpp.ManagedType externs. The calls are
typed now, so hxcpp converts the results itself and a field access on
the result works. AtomicInt also uses the typed class instead of
_hx_atomic_* calls on a field of a wrapper class. The cppia externs are
unchanged.

Co-Authored-By: Aidan Lee <10246746+Aidan63@users.noreply.github.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@Simn

Simn commented Oct 30, 2026

Copy link
Copy Markdown
Member Author

That looks so much better, thanks!

Claude was happy to go with your implementation, the only minor difference is this:

I kept the #if cppia extern declarations, which his version drops. cppia scripts need them to call the code compiled into the host.

@Simn
Simn merged commit 7b14c00 into development Oct 30, 2026
60 of 61 checks passed
@Simn
Simn deleted the fix-cpp-atomic-object-load branch October 30, 2026 06:17
HomuHomu833 pushed a commit to ShadowEngineTeam/libhxcpp that referenced this pull request Nov 2, 2026
Since HaxeFoundation/haxe#13095, Haxe 5's haxe.atomic.AtomicInt and
AtomicObject on C++ are hx::atomic::AtomicInt and AtomicObject from
hx/atomic/AtomicInt.hpp and AtomicObject.hpp, which stock added in
HaxeFoundation/hxcpp#1441. Without them a program using haxe.atomic did
not compile with the current Haxe 5. Haxe 4 and 5.0.0-preview.1 keep using
the atomic intrinsics, which are unchanged.

They take the form stock settled on in HaxeFoundation/hxcpp#1442 and
HaxeFoundation/hxcpp#1443: compareExchange returns the value it found,
which equals expected only when it stored, and each operation is a GC
safe point, not a GC free zone, which was 15 to 25 times slower and let a
collection move an object a load had just read. Unlike stock's, each holds
its std::atomic itself instead of behind an Impl it allocated and never
freed, so no native block leaks per atomic. Both are header only, so no
build file changes, and AtomicInt is allocated as data, since it holds no
pointers.

Tests: HaxeAtomics checks every AtomicInt operation and four threads
adding at once; CppiaHaxeAtomics uses AtomicInt from a script through the
host, interpreted and with the JIT; TestAtomicObject in the native suite
checks AtomicObject's operations, strings comparing by value, and values
only it holds surviving collections. C++ has AtomicObject only from the
Haxe 5 with HaxeFoundation/haxe#13095, so that test runs where the native
suite's other Haxe 5 tests do. openfl#416.
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.

2 participants