Skip to content

Give a cppia member field declared Bool its boolean storage - #1367

Merged
tobil4sk merged 3 commits into
HaxeFoundation:masterfrom
MeguminBOT:fix-cppia-bool-member-storage
Nov 5, 2026
Merged

tobil4sk merged 3 commits into
HaxeFoundation:masterfrom
MeguminBOT:fix-cppia-bool-member-storage

Conversation

@MeguminBOT

@MeguminBOT MeguminBOT commented Aug 13, 2026 •

Copy link
Copy Markdown
Contributor

The problem

A class member declared Bool in a cppia module gets an integer slot, so reading it back through
reflection gives you 1 instead of true. A static field of the same declared type, in the same
module, gives you true:

member 1, static true

Conditions and comparisons still work, since they are happy with the integer. Anything that looks at
the value itself does not.

Why

CppiaVar::linkVarTypes has two forms:

  • The static form calls fieldStorageFromType, which has a case for Bool and returns fsBool.
  • The member form switches on exprType instead. By the time it runs, TypeData::link has
    already mapped the Bool type name to etInt, so the field ends up as fsInt.

That is why the two fields above disagree.

The fix

In src/hx/cppia/CppiaVars.cpp, the member form now calls fieldStorageFromType too:

storeType = typeId==0 ? fsObject : fieldStorageFromType(type);

ioOffset += sTypeSize[exprType];

The slot is still int sized, so the layout and the AlignOffset call above it do not change.

Test

test/cppia covers it. ClientBoolField in Client.hx holds a Bool member and a Bool static,
and testBoolMemberStorage in cases/TestCommon.hx reads both back through Reflect.field.

cd test/cppia
haxe compile-host.hxml
haxe compile-client.hxml
cd bin && ./CppiaHost.exe client.cppia

Without the fix that test fails with Member Bool did not read back as a boolean, and the static
assertion beside it still passes, which is the pair that shows the two forms disagreeing. Same result
with or without -jit.

Reproducing by hand

Script.hx, built with haxe -m Script --cppia script.cppia:

class Holder {
   public var flag:Bool = true;
   public static var staticFlag:Bool = true;
   public function new() {}
}

class Script {
   public static function run():String {
      return 'member ' + Std.string(Reflect.field(new Holder(), 'flag'))
         + ', static ' + Std.string(Reflect.field(Holder, 'staticFlag'));
   }

   public static function main():Void {}
}

Load it from a host built with -D scriptable, using
cpp.cppia.Module.fromData(bytes).boot(), and call Script.run through reflection.

result
before member 1, static true
after member true, static true

Same result with the JIT on or off, since the storage is decided at link time.

Let me know if I got anything wrong!

@MeguminBOT

Copy link
Copy Markdown
Contributor Author

Extended the tests.

Comment thread src/hx/cppia/CppiaVars.cpp Resolved
@MeguminBOT
MeguminBOT requested a review from tobil4sk August 27, 2026 10:17
Comment thread test/cppia/cases/TestCommon.hx Resolved

@tobil4sk tobil4sk left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good to me now, the new code is cleaner and more consistent with the static implementation:

storeType = typeId==0 ? fsObject : fieldStorageFromType(type);

dimensionscape pushed a commit to openfl/libhxcpp that referenced this pull request Oct 15, 2026
A script class laid out its members by expression type alone, so a Bool
member got int storage while a Bool static got Bool storage, and reading
the member into Dynamic gave an Int.

HaxeFoundation/hxcpp#1367

Fixes #21
dimensionscape added a commit to openfl/libhxcpp that referenced this pull request Oct 15, 2026
Cppia holds a Bool as an int and boxes it as Bool only where the
expression says it is one. Calls through the vtable, closures and
property accessors boxed their result by the int type alone, so a Bool
they returned reached Dynamic as an Int: Std.isOfType(x, Bool) was false
and it printed and serialized as 1. They now box by the declared return
type, as direct calls already did.

Found by the stricter Bool comparison of HaxeFoundation/hxcpp#1009,
which these Ints failed, alongside the Bool members of
HaxeFoundation/hxcpp#1367.
dimensionscape pushed a commit to openfl/libhxcpp that referenced this pull request Oct 15, 2026
Type.typeof on a Bool member field and a Bool static read by name must
both give TBool.

HaxeFoundation/hxcpp#1367
dimensionscape pushed a commit to openfl/libhxcpp that referenced this pull request Oct 15, 2026
Type.typeof on a Bool member field and a Bool static read by name must
both give TBool.

HaxeFoundation/hxcpp#1367
dimensionscape pushed a commit to openfl/libhxcpp that referenced this pull request Oct 15, 2026
Type.typeof on a Bool member field and a Bool static read by name must
both give TBool.

HaxeFoundation/hxcpp#1367
dimensionscape pushed a commit to openfl/libhxcpp that referenced this pull request Oct 15, 2026
Type.typeof on a Bool member field and a Bool static read by name must
both give TBool.

HaxeFoundation/hxcpp#1367
dimensionscape pushed a commit to openfl/libhxcpp that referenced this pull request Oct 15, 2026
Type.typeof on a Bool member field and a Bool static read by name must
both give TBool.

HaxeFoundation/hxcpp#1367
dimensionscape added a commit to openfl/libhxcpp that referenced this pull request Oct 15, 2026
Cppia holds a Bool as an int and boxes it as Bool only where the
expression says it is one. Calls through the vtable, closures and
property accessors boxed their result by the int type alone, and the JIT
stored a Bool argument of a script enum as an Int variant, so those
Bools reached Dynamic as an Int: Std.isOfType(x, Bool) was false and
they printed and serialized as 1. The calls now box by the declared
return type, as direct calls already did, and the JIT stores a Bool
enum argument as a Bool.

Found by the stricter Bool comparison of HaxeFoundation/hxcpp#1009,
which these Ints failed, alongside the Bool members of
HaxeFoundation/hxcpp#1367.
dimensionscape pushed a commit to openfl/libhxcpp that referenced this pull request Oct 15, 2026
Type.typeof on a Bool member field and a Bool static read by name must
both give TBool.

HaxeFoundation/hxcpp#1367
dimensionscape pushed a commit to openfl/libhxcpp that referenced this pull request Oct 15, 2026
Type.typeof on a Bool member field and a Bool static read by name must
both give TBool.

HaxeFoundation/hxcpp#1367
dimensionscape pushed a commit to openfl/libhxcpp that referenced this pull request Oct 15, 2026
Type.typeof on a Bool member field and a Bool static read by name must
both give TBool.

HaxeFoundation/hxcpp#1367
@tobil4sk

Copy link
Copy Markdown
Member

I refactored the cppia tests in #1440. The changes there allow for a simpler test in this PR, here is what I would suggest:

--- a/test/cppia/cases/client/TestClient.hx
+++ b/test/cppia/cases/client/TestClient.hx
@@ -6,6 +6,8 @@ class TestClient extends Test {
        public static var clientBool2 = true;
        public static var clientBool3 = false;
 
+       var memberBool = true;
+
        function testThread() {
                // See if it compiles
                Assert.notNull(sys.thread.Thread.current());
@@ -55,6 +57,9 @@ class TestClient extends Test {
                var hostBools = HostBase.hostBool0 + "/" + HostBase.hostBool1 + "/" + HostBase.hostBool2 + "/" + HostBase.hostBool3;
                var clientBools = clientBool0 + "/" + clientBool1 + "/" + clientBool2 + "/" + clientBool3;
                Assert.equals(hostBools, clientBools);
+
+               Assert.same(Type.ValueType.TBool, Type.typeof(clientBool0));
+               Assert.same(Type.ValueType.TBool, Type.typeof(memberBool));
        }
 
        function testIssue926() {

MeguminBOT and others added 3 commits November 4, 2026 11:27
CppiaVar::linkVarTypes has two forms. The static form asks fieldStorageFromType, which
answers fsBool for Bool. The member form switches on exprType instead, and TypeData::link
has already mapped Bool onto etInt by the time it runs, so the field is laid out as fsInt
and reads back through reflection as 1 rather than true.

The member form now asks fieldStorageFromType as well. The slot stays the size of an int,
leaving the layout and the AlignOffset call above it unchanged.
Every storage fieldStorageFromType returns maps onto an exprType with the
same size, so the switch collapses to a table lookup and the offsets stay
byte-identical.
@tobil4sk
tobil4sk force-pushed the fix-cppia-bool-member-storage branch from 59c19e3 to eebb510 Compare November 4, 2026 11:29
@tobil4sk
tobil4sk merged commit 38dab2c into HaxeFoundation:master Nov 5, 2026
167 of 183 checks passed
@tobil4sk

tobil4sk commented Nov 5, 2026

Copy link
Copy Markdown
Member

Thank you!

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