diff options
author | Jukka Jylänki <jujjyl@gmail.com> | 2013-09-01 19:16:58 +0300 |
---|---|---|
committer | Alon Zakai <alonzakai@gmail.com> | 2013-09-10 14:39:32 -0700 |
commit | c33af789cb9d37ccea5cfb7cc3eef87c2eb8d2fd (patch) | |
tree | 72f867b6ae962effa6b8a759ffc99e66e8f99b2c /src/preamble.js | |
parent | 0869e83dee252df71177f7b0b7e0e8b2ab3de321 (diff) |
Add support for marshalling UTF-16 strings to/from JS. Allows passing wchar_t strings to JS when building with -fshort-wchar. Closes #1565.
Diffstat (limited to 'src/preamble.js')
-rw-r--r-- | src/preamble.js | 30 |
1 files changed, 30 insertions, 0 deletions
diff --git a/src/preamble.js b/src/preamble.js index 5f0d720e..abcd1c67 100644 --- a/src/preamble.js +++ b/src/preamble.js @@ -569,6 +569,36 @@ function Pointer_stringify(ptr, /* optional */ length) { } Module['Pointer_stringify'] = Pointer_stringify; +// Given a pointer 'ptr' to a null-terminated UTF16LE-encoded string in the emscripten HEAP, returns +// a copy of that string as a Javascript String object. +function UTF16ToString(ptr) { + var i = 0; + + var str = ''; + while (1) { + var codeUnit = {{{ makeGetValue('ptr', 'i*2', 'i16') }}}; + if (codeUnit == 0) + return str; + ++i; + // fromCharCode constructs a character from a UTF-16 code unit, so we can pass the UTF16 string right through. + str += String.fromCharCode(codeUnit); + } +} +Module['UTF16ToString'] = UTF16ToString; + +// Copies the given Javascript String object 'str' to the emscripten HEAP at address 'outPtr', +// null-terminated and encoded in UTF16LE form. The copy will require at most (str.length*2+1)*2 bytes of space in the HEAP. +function stringToUTF16(str, outPtr) { + for(var i = 0; i < str.length; ++i) { + // charCodeAt returns a UTF-16 encoded code unit, so it can be directly written to the HEAP. + var codeUnit = str.charCodeAt(i); // possibly a lead surrogate + {{{ makeSetValue('outPtr', 'i*2', 'codeUnit', 'i16') }}} + } + // Null-terminate the pointer to the HEAP. + {{{ makeSetValue('outPtr', 'str.length*2', 0, 'i16') }}} +} +Module['stringToUTF16'] = stringToUTF16; + // 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 UTF32ToString(ptr) { |