Enum.cpp 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. #include "vm/Enum.h"
  2. #include "il2cpp-object-internals.h"
  3. #include "il2cpp-class-internals.h"
  4. #include "gc/WriteBarrier.h"
  5. #include "vm/Array.h"
  6. #include "vm/Class.h"
  7. #include "vm/Reflection.h"
  8. #include "vm/GenericClass.h"
  9. #include "vm/Field.h"
  10. #include "vm/String.h"
  11. #include "vm/Type.h"
  12. #include "utils/MemoryRead.h"
  13. namespace il2cpp
  14. {
  15. namespace vm
  16. {
  17. static uint64_t GetEnumFieldValue(Il2CppClass* enumType, FieldInfo* field)
  18. {
  19. const Il2CppType* type = NULL;
  20. const char* ptr = Class::GetFieldDefaultValue(field, &type);
  21. switch (Class::GetEnumBaseType(enumType)->type)
  22. {
  23. case IL2CPP_TYPE_I1: // Sign extend
  24. return static_cast<int64_t>(static_cast<int8_t>(*ptr));
  25. case IL2CPP_TYPE_U1:
  26. return (uint8_t)*ptr;
  27. case IL2CPP_TYPE_CHAR:
  28. return utils::ReadChar(ptr);
  29. case IL2CPP_TYPE_I2: // Sign extend
  30. return static_cast<int64_t>(static_cast<int16_t>(utils::Read16(ptr)));
  31. case IL2CPP_TYPE_U2:
  32. return utils::Read16(ptr);
  33. case IL2CPP_TYPE_I4: // Sign extend
  34. return static_cast<int64_t>(static_cast<int32_t>(utils::ReadCompressedInt32(&ptr)));
  35. case IL2CPP_TYPE_U4:
  36. return utils::ReadCompressedUInt32(&ptr);
  37. case IL2CPP_TYPE_U8:
  38. case IL2CPP_TYPE_I8:
  39. return utils::Read64(ptr);
  40. default:
  41. IL2CPP_ASSERT(0);
  42. return 0;
  43. }
  44. }
  45. bool Enum::GetEnumValuesAndNames(Il2CppClass* enumType, Il2CppArray** values, Il2CppArray** names)
  46. {
  47. size_t nvalues = Class::GetNumFields(enumType) ? Class::GetNumFields(enumType) - 1 : 0;
  48. bool sorted = true;
  49. gc::WriteBarrier::GenericStore(values, vm::Array::New(il2cpp_defaults.uint64_class, (il2cpp_array_size_t)nvalues));
  50. uint64_t field_value, previous_value = 0;
  51. gc::WriteBarrier::GenericStore(names, vm::Array::New(il2cpp_defaults.string_class, (il2cpp_array_size_t)nvalues));
  52. if (enumType->generic_class)
  53. enumType = GenericClass::GetTypeDefinition(enumType->generic_class);
  54. FieldInfo* field;
  55. void* iter = NULL;
  56. int j = 0;
  57. while ((field = Class::GetFields(enumType, &iter)))
  58. {
  59. if (strcmp("value__", field->name) == 0)
  60. continue;
  61. if (Field::IsDeleted(field))
  62. continue;
  63. il2cpp_array_setref(*names, j, il2cpp::vm::String::New(Field::GetName(field)));
  64. field_value = GetEnumFieldValue(enumType, field);
  65. il2cpp_array_set(*values, uint64_t, j, field_value);
  66. if (previous_value > field_value)
  67. sorted = false;
  68. previous_value = field_value;
  69. j++;
  70. }
  71. return sorted;
  72. }
  73. } /* namespace vm */
  74. } /* namespace il2cpp */