Skip to content

Commit 1b7fc54

Browse files
Tutez64dimensionscape
authored andcommitted
Fall back on hx::Object::toString for any toString signature.
A Haxe toString with parameters compiles to a C++ overload that hides hx::Object::toString() without overriding it, whatever the parameters. Passing null for them only worked for a single optional one. Native code calling toString() on such an object reaches hx::Object::toString(), so the scriptable wrapper now calls that one too. The cppia tests now build host classes that inherit a toString with one optional parameter and with two required ones, and check at run time that script classes extending them print like native ones. HaxeFoundation/hxcpp#1403 Fixes #32
1 parent 329ad3e commit 1b7fc54

5 files changed

Lines changed: 38 additions & 9 deletions

File tree

‎Changes.md‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -199,6 +199,7 @@ The first libhxcpp version. It is based on hxcpp 4.3 (the changes listed under 4
199199
* Fixed cpp.Lib.load looking for ndlls in Windows64 on Windows ARM64 instead of WindowsArm64 (hxcpp #1400)
200200
* Fixed MinGW GUI builds not showing traces in the console they were started from (hxcpp #1330)
201201
* Fixed the build tool, built as C++, failing on a file group without a C or C++ standard (hxcpp #1405)
202+
* Fixed a scriptable host class failing to compile when a parent Haxe class has a toString with an optional argument (hxcpp #1403)
202203
* Added the libhxcpp define: a build that uses the library gets -D libhxcpp=<version>. Compare it with version("x.y.z"); a plain string comparison compares text
203204
* Added separate Linux ARMv7 and ARM64 build architectures (hxcpp #1355)
204205
* Fixed AndroidCompat.cpp closing its extern "C" section in the wrong place (hxcpp #1404)

‎include/hx/Scriptable.h‎

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -221,9 +221,11 @@ void __Visit(HX_VISIT_PARAMS) HXCPP_OVERRIDE { super::__Visit(HX_VISIT_ARG); ::h
221221

222222

223223
/*
224-
* `toString` with a default argument is one C++ parameter (`hx::Null<int>`), not a
225-
* zero-argument method. Calling `__superString::toString()` then fails to compile.
226-
* The zero-argument form is preferred when it exists; otherwise the default is null.
224+
* A Haxe `toString` with parameters, whatever they are, compiles to a C++ overload that
225+
* hides `hx::Object::toString()` without overriding it. In a class that inherits it,
226+
* `__superString` is that class, so `__superString::toString()` does not compile.
227+
* Native code calling `toString()` on such an object reaches `hx::Object::toString()`,
228+
* so the scriptable wrapper calls that one in this case.
227229
*/
228230
template<typename T>
229231
inline auto _hx_scriptable_super_toString(T *inSelf, int) -> decltype(inSelf->T::toString(), ::String())
@@ -234,7 +236,7 @@ inline auto _hx_scriptable_super_toString(T *inSelf, int) -> decltype(inSelf->T:
234236
template<typename T>
235237
inline ::String _hx_scriptable_super_toString(T *inSelf, ...)
236238
{
237-
return inSelf->T::toString(null());
239+
return inSelf->::hx::Object::toString();
238240
}
239241

240242

‎test/cppia/Client.hx‎

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -134,6 +134,13 @@ class ClientBoolConst {
134134
}
135135
}
136136

137+
// None overrides toString: the scriptable wrapper falls back on the host's.
138+
class ClientOptionalToString extends OptionalToString.OptionalToStringChild {}
139+
140+
class ClientRequiredToString extends OptionalToString.RequiredToStringChild {}
141+
142+
class ClientZeroArgToString extends OptionalToString.ZeroArgToString {}
143+
137144
class Client
138145
{
139146
public static var clientBool0 = true;

‎test/cppia/OptionalToString.hx‎

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,7 @@
11
/**
2-
* A scriptable host whose `toString` takes an optional argument.
3-
*
4-
* hxcpp emits that as `toString(hx::Null<int>)`. The scriptable wrapper used to call
5-
* `toString()` with no arguments, which does not compile. `ZeroArgToString` keeps the
6-
* zero-argument form compiling too.
2+
* Scriptable hosts whose `toString` takes parameters, which hide `hx::Object::toString()`
3+
* in C++, and one whose `toString` takes none. The children inherit a hiding `toString`,
4+
* so the scriptable wrapper generated for them cannot call `toString()` on its parent.
75
*/
86
class OptionalToString {
97
public function new() {}
@@ -13,10 +11,22 @@ class OptionalToString {
1311
}
1412
}
1513

14+
class RequiredToString {
15+
public function new() {}
16+
17+
public function toString(indent:Int, prefix:String):String {
18+
return prefix + indent;
19+
}
20+
}
21+
1622
class ZeroArgToString {
1723
public function new() {}
1824

1925
public function toString():String {
2026
return "zero";
2127
}
2228
}
29+
30+
class OptionalToStringChild extends OptionalToString {}
31+
32+
class RequiredToStringChild extends RequiredToString {}

‎test/cppia/cases/TestCommon.hx‎

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -211,6 +211,15 @@ class TestCommon extends Test {
211211
}
212212
}
213213

214+
@:depends(testStatus)
215+
function testHostToStringWithParameters() {
216+
// As in native code, a toString with parameters is not the one Std.string calls.
217+
Assert.equals('OptionalToStringChild', Std.string(new OptionalToString.OptionalToStringChild()));
218+
Assert.equals('ClientOptionalToString', Std.string(Type.createInstance(Type.resolveClass('ClientOptionalToString'), [])));
219+
Assert.equals('ClientRequiredToString', Std.string(Type.createInstance(Type.resolveClass('ClientRequiredToString'), [])));
220+
Assert.equals('zero', Std.string(Type.createInstance(Type.resolveClass('ClientZeroArgToString'), [])));
221+
}
222+
214223
@:depends(testStatus)
215224
function testInterfaceCalling() {
216225
final obj : IFoo = Type.createInstance(Type.resolveClass('ClientFoo'), []);

0 commit comments

Comments
 (0)