Skip to content

Commit a1e5612

Browse files
authored
[cpp] More func comparisons (#12889)
* Override the callableId function for class function closures It was falling back to getting the callableId for the signature, which isn't how comparisons work for these class function closures * Add extra tests
1 parent c5d752b commit a1e5612

2 files changed

Lines changed: 46 additions & 0 deletions

File tree

src/generators/cpp/gen/cppGenClassImplementation.ml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ let write_callable_header callable_name captures func return_type_str prefix out
2020
| None ->
2121
Printf.sprintf "\t%s() {} \n" callable_name |> output);
2222

23+
(* Override __Compare *)
2324
output "\tint __Compare(const ::hx::Object* inRhs) const override {\n";
2425
Printf.sprintf "\t\tauto casted = dynamic_cast<const %s*>(inRhs);\n" callable_name |> output;
2526
output "\t\tif (!casted) { return -1; }\n";
@@ -28,6 +29,11 @@ let write_callable_header callable_name captures func return_type_str prefix out
2829
output "\t\treturn 0;\n";
2930
output "\t}\n";
3031

32+
(* Override callableId, Haxe function closures are only compared based on name, not signature *)
33+
output "\tstd::type_index callableId() const override {\n";
34+
Printf.sprintf "\t\treturn std::type_index{ typeid(%s) };\n" callable_name |> output;
35+
output "\t}\n";
36+
3137
Printf.sprintf "\t%s HX_LOCAL_RUN(%s) override {\n" return_type_str (cpp_callable_args func.tcf_args prefix) |> output
3238

3339
let write_callable_trailer captures output =
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
package unit.issues;
2+
3+
private class GenericHolder<T> {
4+
public var value:T;
5+
public function new() {}
6+
}
7+
8+
private class ClassA {
9+
public function new() {}
10+
public function doSomething(x:Int) {}
11+
}
12+
13+
private class ClassB {
14+
public function new() {}
15+
public function doSomethingElse(x:Int) {}
16+
}
17+
18+
class Issue12872 extends Test {
19+
function test() {
20+
final a = new ClassA();
21+
final b = new ClassB();
22+
23+
final fnA:Int->Void = a.doSomething;
24+
final fnB:Int->Void = b.doSomethingElse;
25+
26+
f(fnA == fnB);
27+
f(Reflect.compareMethods(fnA, fnB));
28+
29+
final castA:Dynamic->Void = cast fnA;
30+
final castB:Dynamic->Void = cast fnB;
31+
32+
f(castA == castB);
33+
f(Reflect.compareMethods(castA, castB));
34+
35+
var holder = new GenericHolder<Dynamic->Void>();
36+
holder.value = castA;
37+
38+
f(holder.value == castB);
39+
}
40+
}

0 commit comments

Comments
 (0)