utf parsing in writeStringToMemory as well

This commit is contained in:
Alon Zakai 2012-07-15 13:50:05 -07:00
Родитель 83b96fcd9d
Коммит f1d11329a4
4 изменённых файлов: 25 добавлений и 34 удалений

Просмотреть файл

@ -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) {

Просмотреть файл

@ -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;

Просмотреть файл

@ -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

Просмотреть файл

@ -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')