Skip to content

Store a Bool a cppia script puts in a Map as a Bool - #487

Merged
dimensionscape merged 1 commit into
mainfrom
fix/cppia-map-bool
Nov 26, 2026
Merged

dimensionscape merged 1 commit into
mainfrom
fix/cppia-map-bool

Conversation

@dimensionscape

Copy link
Copy Markdown
Member

Fixes #485.

A Bool that cppia script code puts in a Map was stored as an Int. A read typed Bool converted it back, but read as Dynamic it was 1: Std.isOfType(value, Bool) was false, map.get(key) == true was false, and toString printed 1. It was the same interpreted and under the JIT, for StringMap, IntMap and ObjectMap, and for a Map<String, Dynamic> given a Bool. Compiled code was right.

Cause

The Haxe standard library gives scriptable hosts a setBool on the three classes, and a script's map.set(key, value) with a Bool value is compiled to a call to it. It calls the Int setter with a C++ bool:

private function setBool(key:String, val:Bool):Void {
	untyped __string_hash_set_int(__cpp__("HX_MAP_THIS"), key, val);
}

The classes' own set(key, bool) overloads, which compiled code uses, call the untyped setter. This is reported as HaxeFoundation/haxe#13246, and the runtime can absorb it for every Haxe it supports.

The change

__int_hash_set_int, __string_hash_set_int and __object_hash_set_int each get an overload in include/hx/StdLibs.h that takes exactly a bool and stores it through the untyped setter, as compiled code does. The standard library's setBool passes a bool, so it reaches the overload, and every other caller still reaches the Int setter. It is a template on a trait and not a plain bool parameter, which made the classes' own set(key, unsigned int) ambiguous.

This changes what a script observes: it now reads true or false where it read 1 or 0.

Tests

test/regression/CppiaMapBool runs one script interpreted and jitted: String, Int and object keys, true and false, read as Dynamic, compared, through toString, into a typed Bool, and beside Int and mixed values that must stay what they are. Before the change 32 of its 56 lines were wrong.

Run on 64-bit Windows with MSVC and Haxe 4.3.7:

  • CppiaMapBool, also built with -D HXCPP_GC_GENERATIONAL, where the setters take an owner.
  • test/cppia, interpreted and with -jit: 56 assertions each.
  • test/haxe: 2698 assertions.
  • CppiaBoolBoxing, CppiaBoolBuiltins, CppiaJitBoolField, IntMapHashSwitch, Int64HashDynamicValues, GenerationalMapEdges, GenerationalMapYoungLog, ObjectMapMixedInt64Float, MapFormattingCycles, MapShrinkRegrow, WeakMapValueStorage, GcFreezeObjectMap.

Those ran before the branch was rebased onto the commit that opens 6.0.1, which changes only the version and Changes.md. CppiaMapBool and CppiaBoolBoxing ran again after it.

Not run here: the whole of test/regression, test/std, and the other platforms.

The Haxe standard library gives scriptable hosts a setBool on StringMap,
IntMap and ObjectMap, and the cppia generator calls it for map.set(key,
value) with a Bool value. It calls the Int setter with a C++ bool, so the
value was stored as an Int: read back as a Dynamic it was 1,
Std.isOfType(value, Bool) was false, map.get(key) == true was false and
toString printed 1. Compiled code was right, since the classes' own
set(key, bool) overloads call the untyped setter.

The three Int setters get an overload that takes exactly a bool and stores
it through the untyped setter, as compiled code does. It is a template on a
trait rather than a plain bool parameter, which made the classes' own
set(key, unsigned int) ambiguous.

HaxeFoundation/haxe#13246
@dimensionscape
dimensionscape merged commit 3a8f4f8 into main Nov 26, 2026
37 checks passed
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.

cppia: a Bool a script puts in a Map reads back as 1

1 participant