Skip to content

Commit 3a8f4f8

Browse files
Store a Bool a cppia script puts in a Map as a Bool (#487)
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
1 parent ccc418f commit 3a8f4f8

6 files changed

Lines changed: 183 additions & 0 deletions

File tree

‎Changes.md‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22
libhxcpp 6.0.1 (unreleased)
33
------------------------------------------------------------
44

5+
* Fixed a Bool that a cppia script puts in a Map reading back as 1: the Haxe standard library's setBool for scripts stores it through the Int setter, so read as a Dynamic the value was 1 or 0, Std.isOfType(value, Bool) was false, map.get(key) == true was false and toString printed 1, interpreted and under the JIT, for String, Int and object keys. It is stored as a Bool, as compiled code stores it, so a script now reads true or false where it read 1 or 0 (libhxcpp #485, HaxeFoundation/haxe#13246)
6+
57
libhxcpp 6.0.0
68
------------------------------------------------------------
79

‎include/hx/StdLibs.h‎

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -265,6 +265,24 @@ HXCPP_EXTERN_CLASS_ATTRIBUTES Float __object_hash_get_float(Dynamic inHa
265265
HXCPP_EXTERN_CLASS_ATTRIBUTES cpp::Int64 __object_hash_get_int64(Dynamic inHash,Dynamic inKey);
266266
inline bool __object_hash_get_bool(Dynamic inHash,Dynamic inKey) { return __object_hash_get_int(inHash,inKey); }
267267

268+
// --- Bool values a cppia script puts in a map ---------------------------------------
269+
//
270+
// The Haxe standard library gives scripts a setBool on IntMap, StringMap and ObjectMap
271+
// that calls the Int setter with a bool, so the value was stored as an Int and read back
272+
// as 1 wherever it was not read as a Bool. A bool given to the Int setter is stored as
273+
// compiled code stores it, as a Bool. The overloads take a bool and nothing that
274+
// converts to one, so every other caller still reaches the Int setter.
275+
namespace hx { template<typename T> struct MapBoolValue { }; template<> struct MapBoolValue<bool> { typedef void Type; }; }
276+
#ifdef HXCPP_GC_GENERATIONAL
277+
#define HX_MAP_THIS_PASS owner, ioHash
278+
#else
279+
#define HX_MAP_THIS_PASS ioHash
280+
#endif
281+
template<typename T> inline typename ::hx::MapBoolValue<T>::Type __int_hash_set_int(HX_MAP_THIS_ARG,int inKey,T inValue) { __int_hash_set(HX_MAP_THIS_PASS,inKey,Dynamic(inValue)); }
282+
template<typename T> inline typename ::hx::MapBoolValue<T>::Type __string_hash_set_int(HX_MAP_THIS_ARG,String inKey,T inValue) { __string_hash_set(HX_MAP_THIS_PASS,inKey,Dynamic(inValue)); }
283+
template<typename T> inline typename ::hx::MapBoolValue<T>::Type __object_hash_set_int(HX_MAP_THIS_ARG,Dynamic inKey,T inValue) { __object_hash_set(HX_MAP_THIS_PASS,inKey,Dynamic(inValue)); }
284+
#undef HX_MAP_THIS_PASS
285+
268286
// --- Date --------------------------------------------------------------------------
269287

270288
// returns Epoch UTC timestamp (in seconds); assumes that input date parts are considered to be in local timezone date/time representation
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
function main() {
2+
var script = haxe.io.Path.join([haxe.io.Path.directory(Sys.programPath()), "script.cppia"]);
3+
for (jit in [false, true]) {
4+
cpp.cppia.Host.enableJit(jit);
5+
try {
6+
cpp.cppia.Host.runFile(script);
7+
} catch (e:Dynamic) {
8+
Sys.println("error: " + e);
9+
}
10+
}
11+
}
Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
class Key {
2+
public function new() {}
3+
}
4+
5+
class Script {
6+
static function check(name:String, d:Dynamic) {
7+
Sys.println(name + " " + Std.isOfType(d, Bool) + " " + (d == true) + " " + Std.string(d));
8+
}
9+
10+
static function main() {
11+
var yes = true;
12+
var no = false;
13+
14+
// The standard library gives scripts a setBool on each of these, which
15+
// calls the Int setter (HaxeFoundation/haxe#13246)
16+
var byString = new Map<String, Bool>();
17+
byString.set("yes", yes);
18+
byString.set("no", no);
19+
check("string", byString.get("yes"));
20+
check("string false", byString.get("no"));
21+
Sys.println("string compared " + (byString.get("yes") == true) + " " + (byString.get("no") == false));
22+
var typed:Bool = byString.get("yes");
23+
var typedFalse:Bool = byString.get("no");
24+
Sys.println("string typed " + typed + " " + typedFalse);
25+
Sys.println("string missing " + byString.get("other"));
26+
for (value in byString) {
27+
if (value) {
28+
check("string iterated", value);
29+
}
30+
}
31+
32+
var one = new Map<String, Bool>();
33+
one.set("a", yes);
34+
Sys.println("string toString " + one.toString());
35+
one.set("a", no);
36+
check("string overwritten", one.get("a"));
37+
one["a"] = yes;
38+
check("string index", one["a"]);
39+
check("string literal", ["a" => true].get("a"));
40+
41+
var byInt = new Map<Int, Bool>();
42+
byInt.set(1, yes);
43+
byInt.set(2, no);
44+
check("int", byInt.get(1));
45+
check("int false", byInt.get(2));
46+
Sys.println("int compared " + (byInt.get(1) == true) + " " + (byInt.get(2) == false));
47+
var typedInt:Bool = byInt.get(1);
48+
Sys.println("int typed " + typedInt);
49+
Sys.println("int toString " + [7 => true].toString());
50+
51+
var key = new Key();
52+
var byObject = new Map<Key, Bool>();
53+
byObject.set(key, yes);
54+
check("object", byObject.get(key));
55+
Sys.println("object compared " + (byObject.get(key) == true));
56+
var typedObject:Bool = byObject.get(key);
57+
Sys.println("object typed " + typedObject);
58+
byObject.set(key, no);
59+
check("object false", byObject.get(key));
60+
61+
// A map of any value takes a Bool the same way, beside values that
62+
// must stay what they are
63+
var mixed = new Map<String, Dynamic>();
64+
mixed.set("bool", yes);
65+
mixed.set("int", 1);
66+
mixed.set("zero", 0);
67+
mixed.set("float", 1.5);
68+
mixed.set("string", "s");
69+
check("mixed bool", mixed.get("bool"));
70+
check("mixed int", mixed.get("int"));
71+
check("mixed zero", mixed.get("zero"));
72+
check("mixed float", mixed.get("float"));
73+
check("mixed string", mixed.get("string"));
74+
75+
var nullable = new Map<String, Null<Bool>>();
76+
nullable.set("a", yes);
77+
check("nullable", nullable.get("a"));
78+
79+
// Ints go on being Ints
80+
var ints = new Map<String, Int>();
81+
ints.set("one", 1);
82+
ints.set("zero", 0);
83+
check("int value", ints.get("one"));
84+
check("int value zero", ints.get("zero"));
85+
var intKeys = new Map<Int, Int>();
86+
intKeys.set(1, 1);
87+
check("int key int value", intKeys.get(1));
88+
}
89+
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
--main Script
2+
-cppia bin/script.cppia
3+
--next
4+
--main Main
5+
-cpp bin
6+
-D scriptable
7+
--dce no
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
string true true true
2+
string false true false false
3+
string compared true true
4+
string typed true false
5+
string missing null
6+
string iterated true true true
7+
string toString [a => true]
8+
string overwritten true false false
9+
string index true true true
10+
string literal true true true
11+
int true true true
12+
int false true false false
13+
int compared true true
14+
int typed true
15+
int toString [7 => true]
16+
object true true true
17+
object compared true
18+
object typed true
19+
object false true false false
20+
mixed bool true true true
21+
mixed int false false 1
22+
mixed zero false false 0
23+
mixed float false false 1.5
24+
mixed string false false s
25+
nullable true true true
26+
int value false false 1
27+
int value zero false false 0
28+
int key int value false false 1
29+
string true true true
30+
string false true false false
31+
string compared true true
32+
string typed true false
33+
string missing null
34+
string iterated true true true
35+
string toString [a => true]
36+
string overwritten true false false
37+
string index true true true
38+
string literal true true true
39+
int true true true
40+
int false true false false
41+
int compared true true
42+
int typed true
43+
int toString [7 => true]
44+
object true true true
45+
object compared true
46+
object typed true
47+
object false true false false
48+
mixed bool true true true
49+
mixed int false false 1
50+
mixed zero false false 0
51+
mixed float false false 1.5
52+
mixed string false false s
53+
nullable true true true
54+
int value false false 1
55+
int value zero false false 0
56+
int key int value false false 1

0 commit comments

Comments
 (0)