aboutsummaryrefslogtreecommitdiff
path: root/tests/utf32.cpp
diff options
context:
space:
mode:
authorJukka Jylänki <jujjyl@gmail.com>2013-08-29 16:50:26 +0300
committerAlon Zakai <alonzakai@gmail.com>2013-09-10 14:38:39 -0700
commit65e1c6f5dcb22980abc7845f79a3a738c5e3e852 (patch)
treee12007636c7a57010942d8dc5cc7eb54f5462cca /tests/utf32.cpp
parentea729bea8c5008dedcdfd79aaf3b2deba3eec52b (diff)
Add functions for marshalling wchar_t (which is fixed 32-bit UTF32LE on Unix) strings to and from JS, and add a test.
Diffstat (limited to 'tests/utf32.cpp')
-rw-r--r--tests/utf32.cpp26
1 files changed, 26 insertions, 0 deletions
diff --git a/tests/utf32.cpp b/tests/utf32.cpp
new file mode 100644
index 00000000..a3e660ee
--- /dev/null
+++ b/tests/utf32.cpp
@@ -0,0 +1,26 @@
+#include <stdio.h>
+#include <string>
+#include <emscripten.h>
+#include <cassert>
+#include <wchar.h>
+
+// This code tests that utf32-encoded std::wstrings can be marshalled between C++ and JS.
+int main() {
+ std::wstring wstr = L"abc\u2603\u20AC123"; // U+2603 is snowman, U+20AC is the Euro sign.
+ const int len = (wstr.length()+1)*4;
+ char *memory = new char[len];
+
+ asm("var str = Module.utf32_to_jsstring(%0);"
+ "Module.print(str);"
+ "Module.jsstring_to_utf32(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 char *srcPtr = reinterpret_cast<const char *>(wstr.c_str());
+ for(int i = 0; i < len; ++i) {
+ assert(memory[i] == srcPtr[i]);
+ }
+ printf("OK.\n");
+ delete[] memory;
+}