diff options
author | Alon Zakai <alonzakai@gmail.com> | 2012-07-15 13:50:05 -0700 |
---|---|---|
committer | Alon Zakai <alonzakai@gmail.com> | 2012-07-15 13:50:05 -0700 |
commit | f1d11329a4d309682f0deb5c4cb2671fea8c9c00 (patch) | |
tree | 284fa99a0b58264c7870b7ed7a0aaf545d42a854 | |
parent | 83b96fcd9d9c060f3ea063cec04e603af4442de0 (diff) |
utf parsing in writeStringToMemory as well
-rw-r--r-- | src/library.js | 2 | ||||
-rw-r--r-- | src/preamble.js | 35 | ||||
-rw-r--r-- | src/runtime.js | 13 | ||||
-rwxr-xr-x | tests/runner.py | 9 |
4 files changed, 25 insertions, 34 deletions
diff --git a/src/library.js b/src/library.js index 002a93a6..6403c1fd 100644 --- a/src/library.js +++ b/src/library.js @@ -373,7 +373,7 @@ LibraryManager.library = { output.printer(output.buffer.join('')); output.buffer = []; } else { - output.buffer.push(utf8.feed(val)); + output.buffer.push(utf8.processCChar(val)); } } if (!output) { diff --git a/src/preamble.js b/src/preamble.js index 777325c4..5c5e64fc 100644 --- a/src/preamble.js +++ b/src/preamble.js @@ -521,7 +521,7 @@ function Pointer_stringify(ptr, /* optional */ length) { while (1) { t = {{{ makeGetValue('ptr', 'i', 'i8', 0, 1) }}}; if (nullTerminated && t == 0) break; - ret += utf8.feed(t); + ret += utf8.processCChar(t); i += 1; if (!nullTerminated && i == length) break; } @@ -734,22 +734,9 @@ Module['String_len'] = String_len; // This processes a JS string into a C-line array of numbers, 0-terminated. // For LLVM-originating strings, see parser.js:parseLLVMString function function intArrayFromString(stringy, dontAddNull, length /* optional */) { - var ret = []; - var t; - var i = 0; - if (length === undefined) { - length = stringy.length; - } - while (i < length) { - var chr = stringy.charCodeAt(i); - if (chr > 0xFF) { -#if ASSERTIONS - assert(false, 'Character code ' + chr + ' (' + stringy[i] + ') at offset ' + i + ' not in 0x00-0xFF.'); -#endif - chr &= 0xFF; - } - ret.push(chr); - i = i + 1; + var ret = (new Runtime.UTF8Processor()).processJSString(stringy); + if (length) { + ret.length = length; } if (!dontAddNull) { ret.push(0); @@ -776,21 +763,13 @@ Module['intArrayToString'] = intArrayToString; // Write a Javascript array to somewhere in the heap function writeStringToMemory(string, buffer, dontAddNull) { + var array = intArrayFromString(string, dontAddNull); var i = 0; - while (i < string.length) { - var chr = string.charCodeAt(i); - if (chr > 0xFF) { -#if ASSERTIONS - assert(false, 'Character code ' + chr + ' (' + string[i] + ') at offset ' + i + ' not in 0x00-0xFF.'); -#endif - chr &= 0xFF; - } + while (i < array.length) { + var chr = array[i]; {{{ makeSetValue('buffer', 'i', 'chr', 'i8') }}} i = i + 1; } - if (!dontAddNull) { - {{{ makeSetValue('buffer', 'i', '0', 'i8') }}} - } } Module['writeStringToMemory'] = writeStringToMemory; diff --git a/src/runtime.js b/src/runtime.js index e39fa61e..6defbb35 100644 --- a/src/runtime.js +++ b/src/runtime.js @@ -334,10 +334,13 @@ var Runtime = { return Runtime.funcWrappers[func]; }, + // Returns a processor of UTF. + // processCChar() receives characters from a C-like UTF representation and returns JS string fragments. + // processJSString() receives a JS string and returns a C-like UTF representation in an array UTF8Processor: function() { var buffer = []; var needed = 0; - this.feed = function (code) { + this.processCChar = function (code) { code = code & 0xff; if (needed) { buffer.push(code); @@ -366,6 +369,14 @@ var Runtime = { buffer.length = 0; return ret; } + this.processJSString = function(string) { + string = unescape(encodeURIComponent(string)); + var ret = []; + for (var i = 0; i < string.length; i++) { + ret.push(string.charCodeAt(i)); + } + return ret; + } }, #if RUNTIME_DEBUG diff --git a/tests/runner.py b/tests/runner.py index d45b11e7..a434ce3b 100755 --- a/tests/runner.py +++ b/tests/runner.py @@ -4485,14 +4485,15 @@ def process(filename): #include <stdio.h> #include <emscripten.h> - char *c = "μ†ℱ ╋ℯ╳╋"; - int main() { + char *c = "μ†ℱ ╋ℯ╳╋"; printf("%d %d %d %d %s\n", c[0]&0xff, c[1]&0xff, c[2]&0xff, c[3]&0xff, c); - //emscripten_run_script("Module.print(Pointer_stringify(Module.getValue(_c, '*')))"); + emscripten_run_script("cheez = Module._malloc(100);" + "Module.writeStringToMemory(\"μ†ℱ ╋ℯ╳╋\", cheez);" + "Module.print([Pointer_stringify(cheez), Module.getValue(cheez, 'i8')&0xff, Module.getValue(cheez+1, 'i8')&0xff, Module.getValue(cheez+2, 'i8')&0xff, Module.getValue(cheez+3, 'i8')&0xff, ]);"); } ''' - self.do_run(src, '206 188 226 128 μ†ℱ ╋ℯ╳╋\n')#μ†ℱ ╋ℯ╳╋\n'); + self.do_run(src, '206 188 226 128 μ†ℱ ╋ℯ╳╋\nμ†ℱ ╋ℯ╳╋,206,188,226,128\n'); def test_direct_string_constant_usage(self): if self.emcc_args is None: return self.skip('requires libcxx') |