mirror of
https://github.com/RGBCube/serenity
synced 2025-05-31 09:58:11 +00:00
LibUnicode: Dynamically load the generated UnicodeData symbols
The generated data for libunicodedata.so is quite large, and loading it is a price paid by nearly every application by way of depending on LibRegex. In order to defer this cost until an application actually uses one of the surrounding APIs, dynamically load the generated symbols. To be able to load the symbols dynamically, the generated methods must have demangled names. Typically, this is accomplished with `extern "C"` blocks. The clang toolchain complains about this here because the types returned from the generators are strictly C++ types. So to demangle the names, we use the asm() compiler directive to manually define a symbol name; the caveat is that we *must* be sure the symbols are unique. As an extra precaution, we prefix each symbol name with "unicode_". For more details, see: https://gcc.gnu.org/onlinedocs/gcc/Asm-Labels.html This symbol loader used in this implementation provides the additional benefit of removing many [[maybe_unused]] attributes from the LibUnicode methods. Internally, if ENABLE_UNICODE_DATABASE_DOWNLOAD is OFF, the loader is able to stub out the function pointers it returns. Note that as of this commit, LibUnicode is still directly linked against LibUnicodeData. This commit is just a first step towards removing that.
This commit is contained in:
parent
749d5ebd68
commit
3fd53baa25
7 changed files with 256 additions and 101 deletions
|
@ -345,6 +345,60 @@ Optional<@return_type@> @method_name@(StringView key)
|
|||
)~~~");
|
||||
}
|
||||
|
||||
// This is a temporary duplicate of generate_value_from_string() until all generators support dynamic loading.
|
||||
template<typename ValueType>
|
||||
void generate_value_from_string_for_dynamic_loading(SourceGenerator& generator, StringView method_name_format, StringView value_type, StringView value_name, HashValueMap<ValueType> hashes, Optional<StringView> return_type = {}, StringView return_format = "{}"sv)
|
||||
{
|
||||
ensure_from_string_types_are_generated(generator);
|
||||
|
||||
generator.set("method_name", String::formatted(method_name_format, value_name));
|
||||
generator.set("value_type", value_type);
|
||||
generator.set("value_name", value_name);
|
||||
generator.set("return_type", return_type.has_value() ? *return_type : value_type);
|
||||
generator.set("size", String::number(hashes.size()));
|
||||
|
||||
generator.append(R"~~~(
|
||||
Optional<@return_type@> @method_name@(StringView key) asm("unicode_@method_name@");
|
||||
Optional<@return_type@> @method_name@(StringView key)
|
||||
{
|
||||
constexpr Array<HashValuePair<@value_type@>, @size@> hash_pairs { {
|
||||
)~~~");
|
||||
|
||||
auto hash_keys = hashes.keys();
|
||||
quick_sort(hash_keys);
|
||||
|
||||
constexpr size_t max_values_per_row = 10;
|
||||
size_t values_in_current_row = 0;
|
||||
|
||||
for (auto hash_key : hash_keys) {
|
||||
if (values_in_current_row++ > 0)
|
||||
generator.append(" ");
|
||||
|
||||
if constexpr (IsIntegral<ValueType>)
|
||||
generator.set("value"sv, String::number(hashes.get(hash_key).value()));
|
||||
else
|
||||
generator.set("value"sv, String::formatted("{}::{}", value_type, hashes.get(hash_key).value()));
|
||||
|
||||
generator.set("hash"sv, String::number(hash_key));
|
||||
generator.append("{ @hash@U, @value@ },"sv);
|
||||
|
||||
if (values_in_current_row == max_values_per_row) {
|
||||
generator.append("\n ");
|
||||
values_in_current_row = 0;
|
||||
}
|
||||
}
|
||||
|
||||
generator.set("return_statement", String::formatted(return_format, "value->value"sv));
|
||||
generator.append(R"~~~(
|
||||
} };
|
||||
|
||||
if (auto const* value = binary_search(hash_pairs, key.hash(), nullptr, HashValueComparator<@value_type@> {}))
|
||||
return @return_statement@;
|
||||
return {};
|
||||
}
|
||||
)~~~");
|
||||
}
|
||||
|
||||
template<typename IdentifierFormatter>
|
||||
void generate_enum(SourceGenerator& generator, IdentifierFormatter&& format_identifier, StringView name, StringView default_, Vector<String>& values, Vector<Alias> aliases = {})
|
||||
{
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue