UnicodeSymbols.cpp 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. /*
  2. * Copyright (c) 2021, Tim Flynn <trflynn89@pm.me>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #include <LibUnicode/UnicodeSymbols.h>
  7. #if ENABLE_UNICODE_DATA
  8. # if defined(__serenity__)
  9. # include <LibDl/dlfcn.h>
  10. # include <LibDl/dlfcn_integration.h>
  11. # else
  12. # include <dlfcn.h>
  13. # endif
  14. #else
  15. # include <AK/Function.h>
  16. # include <LibUnicode/Locale.h>
  17. #endif
  18. namespace Unicode::Detail {
  19. #if !ENABLE_UNICODE_DATA
  20. template<typename T>
  21. struct FunctionStub;
  22. template<typename ReturnType, typename... ParameterTypes>
  23. struct FunctionStub<Function<ReturnType(ParameterTypes...)>> {
  24. static constexpr auto make_stub()
  25. {
  26. if constexpr (IsVoid<ReturnType>)
  27. return [](ParameterTypes...) {};
  28. else
  29. return [](ParameterTypes...) -> ReturnType { return {}; };
  30. }
  31. };
  32. #endif
  33. // This loader supports 3 modes:
  34. //
  35. // 1. When the Unicode data generators are enabled, and the target is Serenity, the symbols are
  36. // dynamically loaded from the shared library containing them.
  37. //
  38. // 2. When the Unicode data generators are enabled, and the target is Lagom, the symbols are
  39. // dynamically loaded from the main program.
  40. //
  41. // 3. When the Unicode data generators are disabled, the symbols are stubbed out to empty lambdas.
  42. // This allows callers to remain agnostic as to whether the generators are enabled.
  43. Symbols const& Symbols::ensure_loaded()
  44. {
  45. static Symbols symbols {};
  46. static bool initialized = false;
  47. if (initialized)
  48. return symbols;
  49. #if ENABLE_UNICODE_DATA
  50. # if defined(__serenity__)
  51. static void* libunicodedata = MUST(__dlopen("libunicodedata.so.serenity", RTLD_NOW));
  52. auto load_symbol = [&]<typename T>(T& dest, char const* name) {
  53. dest = reinterpret_cast<T>(MUST(__dlsym(libunicodedata, name)));
  54. };
  55. # else
  56. static void* libunicodedata = dlopen(nullptr, RTLD_NOW);
  57. VERIFY(libunicodedata);
  58. auto load_symbol = [&]<typename T>(T& dest, char const* name) {
  59. dest = reinterpret_cast<T>(dlsym(libunicodedata, name));
  60. VERIFY(dest);
  61. };
  62. # endif
  63. #else
  64. auto load_symbol = []<typename T>(T& dest, char const*) {
  65. dest = +FunctionStub<Function<RemovePointer<T>>>::make_stub();
  66. };
  67. #endif
  68. load_symbol(symbols.code_point_display_name, "unicode_code_point_display_name");
  69. load_symbol(symbols.canonical_combining_class, "unicode_canonical_combining_class");
  70. load_symbol(symbols.simple_uppercase_mapping, "unicode_simple_uppercase_mapping");
  71. load_symbol(symbols.simple_lowercase_mapping, "unicode_simple_lowercase_mapping");
  72. load_symbol(symbols.special_case_mapping, "unicode_special_case_mapping");
  73. load_symbol(symbols.general_category_from_string, "unicode_general_category_from_string");
  74. load_symbol(symbols.code_point_has_general_category, "unicode_code_point_has_general_category");
  75. load_symbol(symbols.property_from_string, "unicode_property_from_string");
  76. load_symbol(symbols.code_point_has_property, "unicode_code_point_has_property");
  77. load_symbol(symbols.script_from_string, "unicode_script_from_string");
  78. load_symbol(symbols.code_point_has_script, "unicode_code_point_has_script");
  79. load_symbol(symbols.code_point_has_script_extension, "unicode_code_point_has_script_extension");
  80. load_symbol(symbols.locale_from_string, "unicode_locale_from_string");
  81. load_symbol(symbols.get_locale_language_mapping, "unicode_get_locale_language_mapping");
  82. load_symbol(symbols.get_locale_territory_mapping, "unicode_get_locale_territory_mapping");
  83. load_symbol(symbols.get_locale_script_tag_mapping, "unicode_get_locale_script_tag_mapping");
  84. load_symbol(symbols.get_locale_long_currency_mapping, "unicode_get_locale_long_currency_mapping");
  85. load_symbol(symbols.get_locale_short_currency_mapping, "unicode_get_locale_short_currency_mapping");
  86. load_symbol(symbols.get_locale_narrow_currency_mapping, "unicode_get_locale_narrow_currency_mapping");
  87. load_symbol(symbols.get_locale_numeric_currency_mapping, "unicode_get_locale_numeric_currency_mapping");
  88. load_symbol(symbols.get_locale_key_mapping, "unicode_get_locale_key_mapping");
  89. load_symbol(symbols.get_locale_list_pattern_mapping, "unicode_get_locale_list_pattern_mapping");
  90. load_symbol(symbols.resolve_language_alias, "unicode_resolve_language_alias");
  91. load_symbol(symbols.resolve_territory_alias, "unicode_resolve_territory_alias");
  92. load_symbol(symbols.resolve_script_tag_alias, "unicode_resolve_script_tag_alias");
  93. load_symbol(symbols.resolve_variant_alias, "unicode_resolve_variant_alias");
  94. load_symbol(symbols.resolve_subdivision_alias, "unicode_resolve_subdivision_alias");
  95. load_symbol(symbols.resolve_complex_language_aliases, "unicode_resolve_complex_language_aliases");
  96. load_symbol(symbols.add_likely_subtags, "unicode_add_likely_subtags");
  97. load_symbol(symbols.resolve_most_likely_territory, "unicode_resolve_most_likely_territory");
  98. initialized = true;
  99. return symbols;
  100. }
  101. }