diff options
author | Alon Zakai <alonzakai@gmail.com> | 2013-09-12 14:47:17 -0700 |
---|---|---|
committer | Alon Zakai <alonzakai@gmail.com> | 2013-09-12 14:47:17 -0700 |
commit | 6010666be99cd0322babba1174cfbc65c776deb5 (patch) | |
tree | dad63b03b751394c169de61fbf2c195f4faf5344 /tests/utf32.cpp | |
parent | 38890204ed1f5f8dd34cced7c42fc9cf42dccab5 (diff) | |
parent | f9dff9b3f2e95b2ca8e5b8fd97538f301fd080fe (diff) |
Merge branch 'incoming'
Diffstat (limited to 'tests/utf32.cpp')
-rw-r--r-- | tests/utf32.cpp | 53 |
1 files changed, 53 insertions, 0 deletions
diff --git a/tests/utf32.cpp b/tests/utf32.cpp new file mode 100644 index 00000000..6b75b244 --- /dev/null +++ b/tests/utf32.cpp @@ -0,0 +1,53 @@ +#include <stdio.h> +#include <string> +#include <emscripten.h> +#include <cassert> +#include <wchar.h> + +typedef unsigned int utf32; +typedef unsigned short utf16; + +// This code tests that Unicode std::wstrings can be marshalled between C++ and JS. +int main() { + std::wstring wstr = L"abc\u2603\u20AC\U0002007C123 --- abc\u2603\u20AC\U0002007C123"; // U+2603 is snowman, U+20AC is the Euro sign, U+2007C is a Chinese Han character that looks like three raindrops. + + printf("sizeof(wchar_t): %d.\n", (int)sizeof(wchar_t)); + + if (sizeof(wchar_t) == 4) { + utf32 *memory = new utf32[wstr.length()+1]; + + asm("var str = Module.UTF32ToString(%0);" + "Module.print(str);" + "Module.stringToUTF32(str, %1);" + : + : "r"(wstr.c_str()), "r"(memory)); + + // Compare memory to confirm that the string is intact after taking a route through JS side. + const utf32 *srcPtr = reinterpret_cast<const utf32 *>(wstr.c_str()); + for(int i = 0;; ++i) { + assert(memory[i] == srcPtr[i]); + if (srcPtr[i] == 0) + break; + } + delete[] memory; + } else { // sizeof(wchar_t) == 2, and we're building with -fshort-wchar. + utf16 *memory = new utf16[2*wstr.length()+1]; + + asm("var str = Module.UTF16ToString(%0);" + "Module.print(str);" + "Module.stringToUTF16(str, %1);" + : + : "r"(wstr.c_str()), "r"(memory)); + + // Compare memory to confirm that the string is intact after taking a route through JS side. + const utf16 *srcPtr = reinterpret_cast<const utf16 *>(wstr.c_str()); + for(int i = 0;; ++i) { + assert(memory[i] == srcPtr[i]); + if (srcPtr[i] == 0) + break; + } + delete[] memory; + } + + printf("OK.\n"); +} |