Skip to content

Commit b99b91a

Browse files
Tutez64cursoragent
authored andcommitted
Call a parent toString that takes an optional argument.
HX_DEFINE_SCRIPTABLE always calls __superString::toString() with no arguments. A Haxe toString(indent = 0) is one C++ parameter, so that call does not compile. The zero-argument form is used when it exists. Otherwise null is passed for the default. Co-authored-by: Cursor <cursoragent@cursor.com>
1 parent 5d5a143 commit b99b91a

1 file changed

Lines changed: 19 additions & 1 deletion

File tree

‎include/hx/Scriptable.h‎

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -220,6 +220,24 @@ void __Visit(HX_VISIT_PARAMS) HXCPP_OVERRIDE { super::__Visit(HX_VISIT_ARG); ::h
220220
#endif
221221

222222

223+
/*
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.
227+
*/
228+
template<typename T>
229+
inline auto _hx_scriptable_super_toString(T *inSelf, int) -> decltype(inSelf->T::toString(), ::String())
230+
{
231+
return inSelf->T::toString();
232+
}
233+
234+
template<typename T>
235+
inline ::String _hx_scriptable_super_toString(T *inSelf, ...)
236+
{
237+
return inSelf->T::toString(null());
238+
}
239+
240+
223241
#define HX_DEFINE_SCRIPTABLE(ARG_LIST) \
224242
inline void *operator new( size_t inSize, int inExtraDataSize ) \
225243
{ \
@@ -235,7 +253,7 @@ void __Visit(HX_VISIT_PARAMS) HXCPP_OVERRIDE { super::__Visit(HX_VISIT_ARG); ::h
235253
void ** __GetScriptVTable() HXCPP_OVERRIDE { return __scriptVTable; } \
236254
::String toString() HXCPP_OVERRIDE { if (__scriptVTable[0] ) \
237255
{ ::hx::CppiaCtx *ctx = ::hx::CppiaCtx::getCurrent(); ::hx::AutoStack a(ctx); ctx->pushObject(this); return ctx->runString(__scriptVTable[0]); } \
238-
else return __superString::toString(); } \
256+
else return _hx_scriptable_super_toString<__superString>(static_cast<__superString *>(this), 0); } \
239257
::String __ToString() const HXCPP_OVERRIDE { return ::hx::ScriptableToString(__scriptVTable[-1]); } \
240258
::hx::Class __GetClass() const HXCPP_OVERRIDE { return ::hx::ScriptableGetClass(__scriptVTable[-1]); } \
241259
int __GetType() const HXCPP_OVERRIDE { return ::hx::ScriptableGetType(__scriptVTable[-1]); } \

0 commit comments

Comments
 (0)