From 53e8cb330477c2e3968fb8c1001c183d29582658 Mon Sep 17 00:00:00 2001 From: Chris Speciale Date: Sat, 28 Nov 2026 04:07:36 -0500 Subject: [PATCH] Let an enum with no registered class take any enum value #492 gave HX_DO_ENUM_RTTI _hx_isEnumValueOf, which compares a value's class with the enum's own __mClass, and returned false when __mClass is null. An extern enum written by hand in C++, as Haxe's unit.issues.Issue10830 does, never registers its class, so every value of it failed its own check: the switch in that test threw Invalid Cast, and so would a typed catch of it or a cast to it. 6.0.2 shipped with it, since the haxe unit job runs only on pushes to main. An enum whose __mClass is null takes any enum value again, through the clsIdEnum check every enum used before #492. Enums Haxe generates have their class, and still check for their own. EnumUnregisteredClass is Issue10830's extern enum, with a switch, a cast from Dynamic and a typed catch, and checks that generated enums still tell each other apart. It threw Invalid Cast before this change. Tests: EnumUnregisteredClass --- Changes.md | 1 + include/hx/Macros.h | 2 +- include/hx/MacrosFixed.h | 2 +- include/hx/Object.h | 4 +- test/regression/EnumUnregisteredClass/Main.hx | 127 ++++++++++++++++++ .../EnumUnregisteredClass/build.hxml | 2 + .../EnumUnregisteredClass/stdout.txt | 4 + 7 files changed, 139 insertions(+), 3 deletions(-) create mode 100644 test/regression/EnumUnregisteredClass/Main.hx create mode 100644 test/regression/EnumUnregisteredClass/build.hxml create mode 100644 test/regression/EnumUnregisteredClass/stdout.txt diff --git a/Changes.md b/Changes.md index be6d42729..dcd2b359d 100644 --- a/Changes.md +++ b/Changes.md @@ -2,6 +2,7 @@ libhxcpp 6.0.3 (unreleased) ------------------------------------------------------------ +* Fixed 6.0.2's enum checks refusing every value of an enum whose class was never registered, such as an extern enum written by hand in C++ with HX_DO_ENUM_RTTI: a switch on such a value, a typed catch of it or a cast to it threw Invalid Cast. Such an enum takes any enum value again, as it did before 6.0.2, and the enums Haxe generates still check for their own (Haxe's unit.issues.Issue10830) * 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/hx/Macros.h b/include/hx/Macros.h index ea4b86c5d..341de0465 100644 --- a/include/hx/Macros.h +++ b/include/hx/Macros.h @@ -81,7 +81,7 @@ static ::Dynamic Create##enum_obj(::String inName,::hx::DynamicArray inArgs) \ static ::hx::ObjectPtr< ::hx::Class_obj> __mClass; \ ::hx::ObjectPtr< ::hx::Class_obj > __GetClass() const HXCPP_OVERRIDE { return __mClass; } \ static ::hx::ObjectPtr< ::hx::Class_obj> &__SGetClass() { return __mClass; } \ - static inline bool _hx_isEnumValueOf(::hx::Object *inPtr) { return __mClass.mPtr && inPtr->__GetClass().mPtr == __mClass.mPtr; } + static inline bool _hx_isEnumValueOf(::hx::Object *inPtr) { return __mClass.mPtr ? inPtr->__GetClass().mPtr == __mClass.mPtr : inPtr->_hx_isInstanceOf(::hx::clsIdEnum); } #define HX_DECLARE_IMPLEMENT_DYNAMIC ::Dynamic __mDynamicFields; \ diff --git a/include/hx/MacrosFixed.h b/include/hx/MacrosFixed.h index 1c862a3e2..07cf2fa57 100644 --- a/include/hx/MacrosFixed.h +++ b/include/hx/MacrosFixed.h @@ -73,7 +73,7 @@ static ::Dynamic Create##enum_obj(::String inName,::hx::DynamicArray inArgs) \ static ::hx::ObjectPtr< ::hx::Class_obj> __mClass; \ ::hx::ObjectPtr< ::hx::Class_obj > __GetClass() const HXCPP_OVERRIDE { return __mClass; } \ static ::hx::ObjectPtr< ::hx::Class_obj> &__SGetClass() { return __mClass; } \ - static inline bool _hx_isEnumValueOf(::hx::Object *inPtr) { return __mClass.mPtr && inPtr->__GetClass().mPtr == __mClass.mPtr; } + static inline bool _hx_isEnumValueOf(::hx::Object *inPtr) { return __mClass.mPtr ? inPtr->__GetClass().mPtr == __mClass.mPtr : inPtr->_hx_isInstanceOf(::hx::clsIdEnum); } #define HX_DECLARE_IMPLEMENT_DYNAMIC ::Dynamic __mDynamicFields; \ diff --git a/include/hx/Object.h b/include/hx/Object.h index 8359c5d90..84f723eb8 100644 --- a/include/hx/Object.h +++ b/include/hx/Object.h @@ -247,7 +247,9 @@ class HXCPP_EXTERN_CLASS_ATTRIBUTES Object // class id says that, but a generated enum has none of its own: it inherits // EnumBase_obj's clsIdEnum, which every enum value has, so a value of any enum // passed for every enum. HX_DO_ENUM_RTTI gives such an enum -// _hx_isEnumValueOf, which compares the value's class with the enum's. +// _hx_isEnumValueOf, which compares the value's class with the enum's. An +// enum whose class was never registered, as an extern written by hand in +// C++ may be, has no class to compare, and takes any enum value as before. template inline auto IsInstanceOfType(hx::Object *inPtr, int) -> decltype(T::_hx_isEnumValueOf(inPtr)) { diff --git a/test/regression/EnumUnregisteredClass/Main.hx b/test/regression/EnumUnregisteredClass/Main.hx new file mode 100644 index 000000000..d4ec32cd4 --- /dev/null +++ b/test/regression/EnumUnregisteredClass/Main.hx @@ -0,0 +1,127 @@ +// An extern enum written by hand in C++, as Haxe's unit.issues.Issue10830 +// does, never registers its class, so its __mClass stays null. 6.0.2's +// enum checks compared a value's class with that null and failed: the switch +// below threw "Invalid Cast". Such an enum takes any enum value, as before 6.0.2. +@:native('customnamespace::MyExternEnum') +extern enum MyExternEnum { + First; + Second(v:String); +} + +enum Generated { + One; + Two; +} + +enum Other { + Three; +} + +@:headerCode(' +HX_DECLARE_CLASS1(customnamespace, MyExternEnum) + +namespace customnamespace { + class MyExternEnum_obj : public hx::EnumBase_obj { + public: + typedef MyExternEnum_obj OBJ_; + + enum Type { + TFirst, + TSecond + }; + + MyExternEnum_obj() = default; + + HX_DO_ENUM_RTTI; + + static bool __GetStatic(const String&, Dynamic&, hx::PropertyAccess); + + String GetEnumName() const { return HX_CSTRING("MyExternEnum"); } + String __ToString() const { return HX_CSTRING("MyExternEnum.") + _hx_tag; } + + static MyExternEnum First(); + static Dynamic First_dyn(); + + static MyExternEnum Second(String); + static Dynamic Second_dyn(); + }; +} +') +@:cppFileCode(' +namespace customnamespace { + MyExternEnum MyExternEnum_obj::First() { + return hx::CreateEnum(HX_CSTRING("First"), Type::TFirst, 0); + } + + MyExternEnum MyExternEnum_obj::Second(String v) { + return hx::CreateEnum(HX_CSTRING("Second"), Type::TSecond, 1)->_hx_init(0, v); + } + + bool MyExternEnum_obj::__GetStatic(const String& _inName, Dynamic& _outValue, hx::PropertyAccess _propAccess) { + if (_inName == HX_CSTRING("First")) { _outValue = MyExternEnum_obj::First_dyn(); return true; } + if (_inName == HX_CSTRING("Second")) { _outValue = MyExternEnum_obj::Second_dyn(); return true; } + return hx::EnumBase_obj::__GetStatic(_inName, _outValue, _propAccess); + } + + hx::Val MyExternEnum_obj::__Field(const String& _inName, hx::PropertyAccess _propAccess) { + if (_inName == HX_CSTRING("First")) { return MyExternEnum_obj::First_dyn(); } + if (_inName == HX_CSTRING("Second")) { return MyExternEnum_obj::Second_dyn(); } + return hx::EnumBase_obj::__Field(_inName, _propAccess); + } + + int MyExternEnum_obj::__FindIndex(String _inName) { + if (_inName == HX_CSTRING("First")) { return Type::TFirst; } + if (_inName == HX_CSTRING("Second")) { return Type::TSecond; } + return hx::EnumBase_obj::__FindIndex(_inName); + } + + int MyExternEnum_obj::__FindArgCount(String _inName) { + if (_inName == HX_CSTRING("First")) { return 0; } + if (_inName == HX_CSTRING("Second")) { return 1; } + return hx::EnumBase_obj::__FindArgCount(_inName); + } + + HX_DEFINE_CREATE_ENUM(MyExternEnum_obj) + + STATIC_HX_DEFINE_DYNAMIC_FUNC0(MyExternEnum_obj, First, return) + + STATIC_HX_DEFINE_DYNAMIC_FUNC1(MyExternEnum_obj, Second, return) + + hx::Class MyExternEnum_obj::__mClass; +} +') +class Main { + static function create():MyExternEnum { + return untyped __cpp__('::customnamespace::MyExternEnum_obj::Second(HX_CSTRING("Hello, World!"))'); + } + + static function main() { + switch create() { + case First: + Sys.println("switch: First"); + case Second(v): + Sys.println("switch: Second " + v); + } + + var held:Dynamic = create(); + var back:MyExternEnum = cast held; + Sys.println("cast from Dynamic: " + (back != null)); + + try { + throw create(); + } catch (e:MyExternEnum) { + Sys.println("caught as MyExternEnum"); + } catch (e:Dynamic) { + Sys.println("caught as Dynamic"); + } + + // Registered enums still tell each other apart + try { + throw One; + } catch (e:Other) { + Sys.println("Generated.One caught as Other"); + } catch (e:Generated) { + Sys.println("Generated.One caught as Generated"); + } + } +} diff --git a/test/regression/EnumUnregisteredClass/build.hxml b/test/regression/EnumUnregisteredClass/build.hxml new file mode 100644 index 000000000..b484e67e7 --- /dev/null +++ b/test/regression/EnumUnregisteredClass/build.hxml @@ -0,0 +1,2 @@ +-m Main +--cpp bin diff --git a/test/regression/EnumUnregisteredClass/stdout.txt b/test/regression/EnumUnregisteredClass/stdout.txt new file mode 100644 index 000000000..4fdb02b99 --- /dev/null +++ b/test/regression/EnumUnregisteredClass/stdout.txt @@ -0,0 +1,4 @@ +switch: Second Hello, World! +cast from Dynamic: true +caught as MyExternEnum +Generated.One caught as Generated