From b4a66042a6e90da853d0a450cad671c6907ebf74 Mon Sep 17 00:00:00 2001 From: Chris Speciale <26172437+dimensionscape@users.noreply.github.com> Date: Sat, 28 Nov 2026 05:23:38 -0500 Subject: [PATCH] Let a start read from a Dynamic field reach indexOf and lastIndexOf Since aaf03883, a String's and a typed Array's indexOf and lastIndexOf have an Int overload beside the Dynamic one, and null() an overload of its own. A start read from a Dynamic field, as in text.indexOf(" ", o.start), is an hx::Val, which converts to both an Int and a Dynamic, so generated code that passes one stopped compiling: C2668 on MSVC, "call to member function 'indexOf' is ambiguous" on clang. hxc's WebAssembly build fails this way in its own source. A template overload now takes a start of any class type that no other overload matches exactly and passes it on as a Dynamic, as every start went before aaf03883. Int, Dynamic and null() starts keep their own overloads, so an Int start is still not boxed. In hxString.h the conversion goes through a defaulted template parameter, because Dynamic is not complete there. Tests: TestSearch in test/haxe compares a start read from a Dynamic field with the other kinds, on Strings and Int arrays. Without the change TestSearch.cpp does not compile (C2668 on all four calls); with it test/haxe passes on Windows x64, 2880 assertions. --- Changes.md | 1 + include/Array.h | 6 ++++++ include/hxString.h | 8 ++++++++ test/haxe/TestSearch.hx | 12 ++++++++++-- 4 files changed, 25 insertions(+), 2 deletions(-) diff --git a/Changes.md b/Changes.md index be6d42729..a3fc7a43d 100644 --- a/Changes.md +++ b/Changes.md @@ -2,6 +2,7 @@ libhxcpp 6.0.3 (unreleased) ------------------------------------------------------------ +* Fixed a String's or a typed Array's indexOf and lastIndexOf not compiling when the start is read from a Dynamic field, as in text.indexOf(" ", o.start): since 6.0.0 an Int start has an overload of its own, and the hx::Val of a field read converts to both an Int and a Dynamic, so the call was ambiguous (C2668 on MSVC, "call to member function 'indexOf' is ambiguous" on clang). Such a start goes to the Dynamic overload again, as every start did before 6.0.0, and an Int start is still not boxed * Fixed the C++ build tool, built with haxe compile-cpp.hxml in tools/hxcpp, not compiling since 6.0.0: ThreadPool.hx declared the class that ends a compile thread's job before one of its imports, which Haxe refuses with "import and using may not appear after a declaration". The neko build tool, hxcpp.n, was unaffected, and run.n uses a C++ build tool only once one is built, so only rebuilding it failed. CI now type-checks it (libhxcpp #471) * Fixed a cppia cast to a class or interface whose value goes unused, cast(o, SomeInterface) as a statement, checking nothing under the JIT: a cast that fails ran on, where the interpreter and compiled code throw. Haxe's TestCasts.testInterfaces makes such casts in closures, which the JIT now compiles. The JIT leaves out only the unused casts that cannot fail * Fixed the collector marking and moving the variables a cppia closure captures in place in the closure: a closure packs its captures at 4-byte offsets, so on 64-bit a String or object captured after an Int, as text in var count = 1; var text = "x"; function() return count + text, sits at an odd multiple of 4, which is undefined behaviour, reported by UBSan, and a fault on a CPU that requires alignment. Each capture is copied out to be marked or visited now, and written back if the moving collector moved it. Interpreted and jitted closures were both affected diff --git a/include/Array.h b/include/Array.h index 19509b97e..1caf114c0 100644 --- a/include/Array.h +++ b/include/Array.h @@ -924,6 +924,10 @@ class Array_obj : public hx::ArrayBase return indexOf(inValue, fromIndex==null() ? 0 : fromIndex->__ToInt()); } int indexOf(ELEM_ inValue, const null &) { return indexOf(inValue, 0); } + // Any other class converts to both as well, as the hx::Val of a Dynamic field + // read does, so it takes the Dynamic overload + template + typename std::enable_if::value, int>::type indexOf(ELEM_ inValue, const T &fromIndex) { return indexOf(inValue, Dynamic(fromIndex)); } int indexOf(ELEM_ inValue, int fromIndex) { int len = length; @@ -948,6 +952,8 @@ class Array_obj : public hx::ArrayBase return lastIndexOf(inValue, fromIndex==null() ? (int)length-1 : fromIndex->__ToInt()); } int lastIndexOf(ELEM_ inValue, const null &) { return lastIndexOf(inValue, (int)length-1); } + template + typename std::enable_if::value, int>::type lastIndexOf(ELEM_ inValue, const T &fromIndex) { return lastIndexOf(inValue, Dynamic(fromIndex)); } int lastIndexOf(ELEM_ inValue, int fromIndex) { int len = length; diff --git a/include/hxString.h b/include/hxString.h index 45cbb844b..947b5245b 100644 --- a/include/hxString.h +++ b/include/hxString.h @@ -7,6 +7,7 @@ #include #include +#include #ifdef __OBJC__ #import @@ -170,6 +171,13 @@ class HXCPP_EXTERN_CLASS_ATTRIBUTES String int lastIndexOf(const ::String &inValue, int inStart) const; inline int indexOf(const ::String &inValue, const null &) const { return indexOf(inValue, 0); } inline int lastIndexOf(const ::String &inValue, const null &) const { return lastIndexOf(inValue, length); } + // Any other class converts to both as well, as the hx::Val of a Dynamic field + // read does, so it takes the Dynamic overload. D stands for Dynamic, which is + // not complete here, so that the conversion waits for a use + template + inline typename std::enable_if::value, int>::type indexOf(const ::String &inValue, const T &inStart) const { return indexOf(inValue, D(inStart)); } + template + inline typename std::enable_if::value, int>::type lastIndexOf(const ::String &inValue, const T &inStart) const { return lastIndexOf(inValue, D(inStart)); } Array split(const ::String &inDelimiter) const; ::String substr(int inPos,Dynamic inLen) const; ::String substring(int inStartIndex, Dynamic inEndIndex) const; diff --git a/test/haxe/TestSearch.hx b/test/haxe/TestSearch.hx index 9197dd397..4c3c16c7e 100644 --- a/test/haxe/TestSearch.hx +++ b/test/haxe/TestSearch.hx @@ -60,8 +60,10 @@ class TestSearch extends Test return -1; } - // Int starts are taken without boxing them; typed, boxed, omitted, null - // and reflected starts must all give the same result + // Int starts are taken without boxing them; typed, boxed, omitted, null, + // reflected and Dynamic field starts must all give the same result. A + // field read converts to both an Int and a Dynamic, so it needs its own + // overload in C++ or the call does not compile function checkArray(a:Array, v:Int, ?pos:haxe.PosInfos) { Assert.equals(arrayIndexOf(a, v, 0), a.indexOf(v), pos); @@ -79,6 +81,9 @@ class TestSearch extends Test Assert.equals(a.lastIndexOf(v, s), a.lastIndexOf(v, boxed), pos); Assert.equals(a.indexOf(v, s), Reflect.callMethod(a, indexOf, [v, s]), pos); Assert.equals(a.lastIndexOf(v, s), Reflect.callMethod(a, lastIndexOf, [v, s]), pos); + var holder:Dynamic = { start: s }; + Assert.equals(a.indexOf(v, s), a.indexOf(v, holder.start), pos); + Assert.equals(a.lastIndexOf(v, s), a.lastIndexOf(v, holder.start), pos); } } @@ -98,6 +103,9 @@ class TestSearch extends Test Assert.equals(text.lastIndexOf(needle, s), text.lastIndexOf(needle, boxed), pos); Assert.equals(text.indexOf(needle, s), dyn.indexOf(needle, s), pos); Assert.equals(text.lastIndexOf(needle, s), dyn.lastIndexOf(needle, s), pos); + var holder:Dynamic = { start: s }; + Assert.equals(text.indexOf(needle, s), text.indexOf(needle, holder.start), pos); + Assert.equals(text.lastIndexOf(needle, s), text.lastIndexOf(needle, holder.start), pos); } }