aboutsummaryrefslogtreecommitdiff
path: root/src
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 /src
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 'src')
-rw-r--r--src/preamble.js27
1 files changed, 27 insertions, 0 deletions
diff --git a/src/preamble.js b/src/preamble.js
index 227b3043..465e47a3 100644
--- a/src/preamble.js
+++ b/src/preamble.js
@@ -569,6 +569,33 @@ function Pointer_stringify(ptr, /* optional */ length) {
}
Module['Pointer_stringify'] = Pointer_stringify;
+// Given a pointer 'ptr' to a null-terminated UTF32LE-encoded string in the emscripten HEAP, returns
+// a copy of that string as a Javascript String object.
+function utf32_to_jsstring(ptr) {
+ var i = 0;
+
+ var str = '';
+ while (1) {
+ var utf32 = {{{ makeGetValue('ptr', 'i*4', 'i32') }}};
+ if (utf32 == 0)
+ return str;
+ ++i;
+ str += String.fromCharCode(utf32);
+ }
+}
+Module['utf32_to_jsstring'] = utf32_to_jsstring;
+
+// Copies the given Javascript String object 'str' to the emscripten HEAP at address 'outPtr',
+// null-terminated and encoded in UTF32LE form. The copy will require (str.length+1)*4 bytes of space in the HEAP.
+function jsstring_to_utf32(str, outPtr) {
+ for(var i = 0; i < str.length; ++i) {
+ var utf32 = str.charCodeAt(i);
+ {{{ makeSetValue('outPtr', 'i*4', 'utf32', 'i32') }}}
+ }
+ {{{ makeSetValue('outPtr', 'str.length*4', 0, 'i32') }}}
+}
+Module['jsstring_to_utf32'] = jsstring_to_utf32;
+
// Memory management
var PAGE_SIZE = 4096;