|
12 | 12 | typedef unsigned int utf32;
|
13 | 13 | typedef unsigned short utf16;
|
14 | 14 |
|
| 15 | +EM_JS_DEPS(deps, "$UTF32ToString,$stringToUTF32"); |
| 16 | + |
15 | 17 | // This code tests that Unicode std::wstrings can be marshalled between C++ and JS.
|
16 | 18 | int main() {
|
17 |
| - 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. |
| 19 | + // U+2603 is snowman, |
| 20 | + // U+20AC is the Euro sign, |
| 21 | + // U+2007C is a Chinese Han character that looks like three raindrops. |
| 22 | + std::wstring wstr = L"abc\u2603\u20AC\U0002007C123 --- abc\u2603\u20AC\U0002007C123"; |
| 23 | + |
| 24 | + printf("sizeof(wchar_t): %d.\n", (int)sizeof(wchar_t)); |
18 | 25 |
|
19 |
| - printf("sizeof(wchar_t): %d.\n", (int)sizeof(wchar_t)); |
| 26 | + if (sizeof(wchar_t) == 4) { |
| 27 | + utf32 *memory = new utf32[wstr.length()+1]; |
20 | 28 |
|
21 |
| - if (sizeof(wchar_t) == 4) { |
22 |
| - utf32 *memory = new utf32[wstr.length()+1]; |
| 29 | + EM_ASM({ |
| 30 | + var str = UTF32ToString($0); |
| 31 | + out(str); |
| 32 | + var numBytesWritten = stringToUTF32(str, $1, Number($2)); |
| 33 | + if (numBytesWritten != 23*4) throw 'stringToUTF32 wrote an invalid length ' + numBytesWritten; |
| 34 | + }, wstr.c_str(), memory, (wstr.length()+1)*sizeof(utf32)); |
23 | 35 |
|
24 |
| - EM_ASM({ |
25 |
| - var str = UTF32ToString($0); |
26 |
| - out(str); |
27 |
| - var numBytesWritten = stringToUTF32(str, $1, Number($2)); |
28 |
| - if (numBytesWritten != 23*4) throw 'stringToUTF32 wrote an invalid length ' + numBytesWritten; |
29 |
| - }, wstr.c_str(), memory, (wstr.length()+1)*sizeof(utf32)); |
| 36 | + // Compare memory to confirm that the string is intact after taking a route |
| 37 | + // through JS side. |
| 38 | + const utf32 *srcPtr = reinterpret_cast<const utf32 *>(wstr.c_str()); |
| 39 | + for (int i = 0;; ++i) { |
| 40 | + assert(memory[i] == srcPtr[i]); |
| 41 | + if (srcPtr[i] == 0) |
| 42 | + break; |
| 43 | + } |
30 | 44 |
|
31 |
| - // Compare memory to confirm that the string is intact after taking a route through JS side. |
32 |
| - const utf32 *srcPtr = reinterpret_cast<const utf32 *>(wstr.c_str()); |
33 |
| - for(int i = 0;; ++i) { |
34 |
| - assert(memory[i] == srcPtr[i]); |
35 |
| - if (srcPtr[i] == 0) |
36 |
| - break; |
37 |
| - } |
| 45 | + EM_ASM({ |
| 46 | + var str = UTF32ToString($0); |
| 47 | + out(str); |
| 48 | + var numBytesWritten = stringToUTF32(str, $1, Number($2)); |
| 49 | + if (numBytesWritten != 5*4) throw 'stringToUTF32 wrote an invalid length ' + numBytesWritten; |
| 50 | + }, wstr.c_str(), memory, 6*sizeof(utf32)); |
| 51 | + assert(memory[5] == 0); |
38 | 52 |
|
39 |
| - EM_ASM({ |
40 |
| - var str = UTF32ToString($0); |
41 |
| - out(str); |
42 |
| - var numBytesWritten = stringToUTF32(str, $1, Number($2)); |
43 |
| - if (numBytesWritten != 5*4) throw 'stringToUTF32 wrote an invalid length ' + numBytesWritten; |
44 |
| - }, wstr.c_str(), memory, 6*sizeof(utf32)); |
45 |
| - assert(memory[5] == 0); |
| 53 | + delete[] memory; |
| 54 | + } else { |
| 55 | + // sizeof(wchar_t) == 2, and we're building with -fshort-wchar. |
| 56 | + utf16 *memory = new utf16[2*wstr.length()+1]; |
46 | 57 |
|
47 |
| - delete[] memory; |
48 |
| - } else { // sizeof(wchar_t) == 2, and we're building with -fshort-wchar. |
49 |
| - utf16 *memory = new utf16[2*wstr.length()+1]; |
| 58 | + EM_ASM({ |
| 59 | + var str = UTF16ToString($0); |
| 60 | + out(str); |
| 61 | + var numBytesWritten = stringToUTF16(str, $1, $2); |
| 62 | + if (numBytesWritten != 25*2) throw 'stringToUTF16 wrote an invalid length ' + numBytesWritten; |
| 63 | + }, wstr.c_str(), memory, (2*wstr.length()+1)*sizeof(utf16)); |
50 | 64 |
|
51 |
| - EM_ASM({ |
52 |
| - var str = UTF16ToString($0); |
53 |
| - out(str); |
54 |
| - var numBytesWritten = stringToUTF16(str, $1, $2); |
55 |
| - if (numBytesWritten != 25*2) throw 'stringToUTF16 wrote an invalid length ' + numBytesWritten; |
56 |
| - }, wstr.c_str(), memory, (2*wstr.length()+1)*sizeof(utf16)); |
| 65 | + // Compare memory to confirm that the string is intact after taking a route |
| 66 | + // through JS side. |
| 67 | + const utf16 *srcPtr = reinterpret_cast<const utf16 *>(wstr.c_str()); |
| 68 | + for (int i = 0;; ++i) { |
| 69 | + assert(memory[i] == srcPtr[i]); |
| 70 | + if (srcPtr[i] == 0) |
| 71 | + break; |
| 72 | + } |
57 | 73 |
|
58 |
| - // Compare memory to confirm that the string is intact after taking a route through JS side. |
59 |
| - const utf16 *srcPtr = reinterpret_cast<const utf16 *>(wstr.c_str()); |
60 |
| - for(int i = 0;; ++i) { |
61 |
| - assert(memory[i] == srcPtr[i]); |
62 |
| - if (srcPtr[i] == 0) |
63 |
| - break; |
64 |
| - } |
| 74 | + EM_ASM({ |
| 75 | + var str = UTF16ToString($0); |
| 76 | + out(str); |
| 77 | + var numBytesWritten = stringToUTF16(str, $1, $2); |
| 78 | + if (numBytesWritten != 5*2) throw 'stringToUTF16 wrote an invalid length ' + numBytesWritten; |
| 79 | + }, wstr.c_str(), memory, 6*sizeof(utf16)); |
| 80 | + assert(memory[5] == 0); |
65 | 81 |
|
66 |
| - EM_ASM({ |
67 |
| - var str = UTF16ToString($0); |
68 |
| - out(str); |
69 |
| - var numBytesWritten = stringToUTF16(str, $1, $2); |
70 |
| - if (numBytesWritten != 5*2) throw 'stringToUTF16 wrote an invalid length ' + numBytesWritten; |
71 |
| - }, wstr.c_str(), memory, 6*sizeof(utf16)); |
72 |
| - assert(memory[5] == 0); |
73 |
| - |
74 |
| - delete[] memory; |
75 |
| - } |
| 82 | + delete[] memory; |
| 83 | + } |
76 | 84 |
|
77 |
| - printf("OK.\n"); |
| 85 | + printf("OK.\n"); |
78 | 86 | }
|
0 commit comments