@@ -244,11 +244,6 @@ std::string ValueToJson(Local<Value> v, int formatIndent = -1);
244244// Limitation: enum values must be in range of [-128, 128)
245245template <typename Type>
246246struct EnumDefineBuilder {
247- template <Type val>
248- inline static Local<Value> serialize () {
249- return Number::newNumber (static_cast <int >(val));
250- }
251-
252247 inline static Local<Value> keys () {
253248 try {
254249 auto arr = Array::newArray ();
@@ -266,7 +261,7 @@ struct EnumDefineBuilder {
266261 try {
267262 auto obj = Object::newObject ();
268263 for (auto & [value, name] : magic_enum::enum_entries<Type>()) {
269- obj.set (String::newString (name), Number::newNumber (( int ) value));
264+ obj.set (String::newString (name), Number::newNumber (static_cast < int64_t >( value) ));
270265 }
271266 return obj;
272267 } catch (const std::exception&) {
@@ -299,37 +294,18 @@ struct EnumDefineBuilder {
299294 return Local<Value>();
300295 }
301296
302- template <
303- Type val,
304- std::enable_if_t <std::is_enum_v<Type>, char > max = static_cast <char >(*magic_enum::enum_values<Type>().rbegin())>
305- inline static void buildBuilder (script::ClassDefineBuilder<void >& builder) {
306- if constexpr (static_cast <char >(val) > max) return ;
307- if constexpr (!magic_enum::enum_name (val).empty ()) {
308- fmt::print (" {} = {},\n " , magic_enum::enum_name (val), static_cast <int >(val));
309- builder.property (magic_enum::enum_name (val).data (), &serialize<val>);
310- }
311- buildBuilder<static_cast <Type>((static_cast <char >(val) + 1 )), max>(builder);
312- }
313-
314- template <
315- std::enable_if_t <std::is_enum_v<Type>, char > max = static_cast <char >(*magic_enum::enum_values<Type>().rbegin())>
316297 inline static ClassDefine<void > build (std::string const & enumName) {
317298 script::ClassDefineBuilder<void > builder = defineClass (enumName);
318- // fmt::print("枚举 {} 可能取值:\n", enumName);
319- // buildBuilder<*magic_enum::enum_values<Type>().begin(), max>(builder);
320299
321300 for (auto & [val, name] : magic_enum::enum_entries<Type>()) {
322- // fmt::print("{} = {},\n", name, static_cast<int>(val));
323- auto _val = val;
324- auto _name = name;
325- builder.property (std::string (name), [=]() -> Local<Value> {
301+ builder.property (std::string (name), [enumName, val, name{std::string{name}}]() -> Local<Value> {
326302 try {
327- return Number::newNumber (static_cast <int >(_val ));
303+ return Number::newNumber (static_cast <int64_t >(val ));
328304 } catch (const std::exception&) {
329305 lse::LegacyScriptEngine::getInstance ().getSelf ().getLogger ().error (
330306 " Error in get {}.{}" ,
331307 enumName,
332- _name
308+ name
333309 );
334310 }
335311 return Local<Value>();
0 commit comments