From c30315cfe1d366af1e359c6e8131b5441fbd063b Mon Sep 17 00:00:00 2001 From: Michael Droettboom Date: Wed, 5 Sep 2018 12:33:55 -0400 Subject: [PATCH 1/2] Avoid using UTF8ToString The only places we were using it was for strings that are ASCII anyway, so this is both more efficient and avoids this Chrome bug: https://bugs.chromium.org/p/chromium/issues/detail?id=868404 --- src/hiwire.c | 9 +++++++++ src/hiwire.h | 10 ++++++++++ src/python2js.c | 8 ++++---- 3 files changed, 23 insertions(+), 4 deletions(-) diff --git a/src/hiwire.c b/src/hiwire.c index 933125932..d0a7e287c 100644 --- a/src/hiwire.c +++ b/src/hiwire.c @@ -67,6 +67,15 @@ EM_JS(int, hiwire_string_utf8, (int ptr), { return Module.hiwire_new_value(UTF8ToString(ptr)); }); +EM_JS(int, hiwire_string_ascii, (int ptr), { + var jsstr = ""; + var idx = ptr; + for (var idx = ptr; Module.HEAPU8[idx] != 0; ++idx) { + jsstr += String.fromCharCode(Module.HEAPU8[idx]); + } + return Module.hiwire_new_value(jsstr); +}); + EM_JS(int, hiwire_bytes, (int ptr, int len), { var bytes = new Uint8ClampedArray(Module.HEAPU8.buffer, ptr, len); return Module.hiwire_new_value(bytes); diff --git a/src/hiwire.h b/src/hiwire.h index a7cad62c7..dccab81da 100644 --- a/src/hiwire.h +++ b/src/hiwire.h @@ -86,6 +86,16 @@ hiwire_string_ucs1(int ptr, int len); int hiwire_string_utf8(int ptr); +/** + * Create a new Javascript string, given a pointer to a null-terminated buffer + * containing ascii (well, technically latin-1). The string data itself is + * copied. + * + * Returns: New reference + */ +int +hiwire_string_ascii(int ptr); + /** * Create a new Javascript Uint8ClampedArray, given a pointer to a buffer and a * length, in bytes. diff --git a/src/python2js.c b/src/python2js.c index a6935d354..1b190c4ff 100644 --- a/src/python2js.c +++ b/src/python2js.c @@ -21,7 +21,7 @@ pythonexc2js() int exc; if (type == NULL || type == Py_None || value == NULL || value == Py_None) { - excval = hiwire_string_utf8((int)"No exception type or value"); + excval = hiwire_string_ascii((int)"No exception type or value"); PyErr_Print(); PyErr_Clear(); goto exit; @@ -31,7 +31,7 @@ pythonexc2js() if (tbmod == NULL) { PyObject* repr = PyObject_Repr(value); if (repr == NULL) { - excval = hiwire_string_utf8((int)"Could not get repr for exception"); + excval = hiwire_string_ascii((int)"Could not get repr for exception"); } else { excval = python2js(repr); Py_DECREF(repr); @@ -46,7 +46,7 @@ pythonexc2js() } if (format_exception == NULL) { excval = - hiwire_string_utf8((int)"Could not get format_exception function"); + hiwire_string_ascii((int)"Could not get format_exception function"); } else { PyObject* pylines; if (no_traceback) { @@ -58,7 +58,7 @@ pythonexc2js() } if (pylines == NULL) { excval = - hiwire_string_utf8((int)"Error calling traceback.format_exception"); + hiwire_string_ascii((int)"Error calling traceback.format_exception"); PyErr_Print(); PyErr_Clear(); goto exit; From d29fa842b66038bb6263d89f2abd656e08678db2 Mon Sep 17 00:00:00 2001 From: Michael Droettboom Date: Wed, 5 Sep 2018 16:04:30 -0400 Subject: [PATCH 2/2] Use emscripten's AsciiToString --- src/hiwire.c | 7 +------ 1 file changed, 1 insertion(+), 6 deletions(-) diff --git a/src/hiwire.c b/src/hiwire.c index d0a7e287c..f6f411cc0 100644 --- a/src/hiwire.c +++ b/src/hiwire.c @@ -68,12 +68,7 @@ EM_JS(int, hiwire_string_utf8, (int ptr), { }); EM_JS(int, hiwire_string_ascii, (int ptr), { - var jsstr = ""; - var idx = ptr; - for (var idx = ptr; Module.HEAPU8[idx] != 0; ++idx) { - jsstr += String.fromCharCode(Module.HEAPU8[idx]); - } - return Module.hiwire_new_value(jsstr); + return Module.hiwire_new_value(AsciiToString(ptr)); }); EM_JS(int, hiwire_bytes, (int ptr, int len), {