Skip to content

Commit

Permalink
Merge pull request #11520 from keymanapp/feat/core/9467-devolve-norm-…
Browse files Browse the repository at this point in the history
…to-js2-epic-ldml

feat(core): change normalize_nfd() to use JS native call instead of ICU on wasm 🙀
  • Loading branch information
srl295 authored May 27, 2024
2 parents ab7fd5e + 948f979 commit 7d9b1a1
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 3 deletions.
5 changes: 5 additions & 0 deletions core/meson.build
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,11 @@ if get_option('buildtype') == 'debug'
add_global_arguments('-DDEBUG', language : 'cpp')
endif

# shared with a number of subdirs
if cpp_compiler.get_id() == 'emscripten'
wasm_exported_runtime_methods = '-sEXPORTED_RUNTIME_METHODS=[\'UTF8ToString\',\'stringToNewUTF8\']'
endif

subdir('doc')
subdir('include')
subdir('src')
Expand Down
28 changes: 28 additions & 0 deletions core/src/util_normalize.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,24 @@
#include "core_icu.h"
#include "kmx/kmx_xstring.h"

#ifdef __EMSCRIPTEN__
#include <emscripten.h>
#include "utfcodec.hpp"
// JS implementations

EM_JS(char*, NormalizeNFD, (const char* input), {
if (!input) return input; // pass through null
const instr = Module.UTF8ToString(input);
const nfd = instr.normalize("NFD");
return stringToNewUTF8(nfd);
});
#endif

namespace km {
namespace core {
namespace util {

#ifndef __EMSCRIPTEN__

/**
* Internal function to normalize with a specified mode.
Expand All @@ -41,6 +54,7 @@ static bool normalize(const icu::Normalizer2 *n, std::u16string &str, UErrorCode
}
return U_SUCCESS(status);
}
#endif

bool normalize_nfd(std::u32string &str) {
std::u16string rstr = km::core::kmx::u32string_to_u16string(str);
Expand All @@ -53,10 +67,24 @@ bool normalize_nfd(std::u32string &str) {
}

bool normalize_nfd(std::u16string &str) {
#ifdef __EMSCRIPTEN__
std::string instr = convert<char16_t,char>(str);
const char *in = instr.c_str();
char *out = NormalizeNFD(in);
if (out == nullptr) {
assert(out != nullptr);
return false;
}
std::string outstr(out);
str = convert<char,char16_t>(outstr);
free(out);
return true;
#else
UErrorCode status = U_ZERO_ERROR;
const icu::Normalizer2 *nfd = icu::Normalizer2::getNFDInstance(status);
UASSERT_SUCCESS(status);
return normalize(nfd, str, status);
#endif
}

/**
Expand Down
2 changes: 1 addition & 1 deletion core/tests/unit/kmx/meson.build
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ common_test_keyboards_baseline = source_path / '../../../../common/test/keyboard
if cpp_compiler.get_id() == 'emscripten'
# TODO: we will move the tests to ES6 in the future
# '-sMODULARIZE', '-sEXPORT_ES6',
tests_flags += ['-lnodefs.js', '-sEXPORTED_RUNTIME_METHODS=[\'UTF8ToString\']']
tests_flags += ['-lnodefs.js', wasm_exported_runtime_methods]
endif

kmx = executable('kmx',
Expand Down
4 changes: 2 additions & 2 deletions core/tests/unit/ldml/meson.build
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ if cpp_compiler.get_id() == 'emscripten'
invalid_test_path = '/'
tests_flags += ['-lnodefs.js',
'-sNO_DISABLE_EXCEPTION_CATCHING', # for test exceptions
'-sEXPORTED_RUNTIME_METHODS=[\'UTF8ToString\']']
wasm_exported_runtime_methods]
else
tests_flags = []
test_path = meson.current_build_dir() / 'keyboards'
Expand Down Expand Up @@ -117,7 +117,7 @@ test('test_transforms', t, suite: 'ldml')
normalization_tests_flags = tests_flags

if cpp_compiler.get_id() == 'emscripten'
normalization_tests_flags += ['-lnodefs.js', '-sEXPORTED_RUNTIME_METHODS=[\'UTF8ToString\']']
normalization_tests_flags += ['-lnodefs.js', wasm_exported_runtime_methods]
endif

test_context_normalization = executable('test_context_normalization',
Expand Down

0 comments on commit 7d9b1a1

Please sign in to comment.