From cc18cd85c75c184d159d6a636c9a924140de1353 Mon Sep 17 00:00:00 2001
From: Brion Vibber <brion@pobox.com>
Date: Fri, 20 Sep 2019 22:08:36 -0700
Subject: [PATCH] Update intArrayToString to match docs, and update docs

intArrayToString is documented as taking null-terminated input,
but previously did not stop at a 0 entry.

Switched it to use UTF8ArrayToString internally using the
array length as maximum length. This both fixes the discrepancy
with the documentation by allowing 0-terminated input arrays,
and improves general compatibility by allowing input data to
contain UTF-8 strings without being corrupted on output to
JavaScript.

Updated documentation for both intArrayToString and its
almost-inverse, intArrayFromString, to indicate that UTF-8
is used (this was already the case, but not documented,
for intArrayFromString).

Fixes #4840
---
 site/source/docs/api_reference/preamble.js.rst |  4 ++--
 src/arrayUtils.js                              | 13 +------------
 2 files changed, 3 insertions(+), 14 deletions(-)

diff --git a/site/source/docs/api_reference/preamble.js.rst b/site/source/docs/api_reference/preamble.js.rst
index 10b4ac090577e..ac43159ec277d 100644
--- a/site/source/docs/api_reference/preamble.js.rst
+++ b/site/source/docs/api_reference/preamble.js.rst
@@ -234,7 +234,7 @@ Conversion functions — strings, pointers and arrays
 
 .. js:function:: intArrayFromString(stringy, dontAddNull[, length])
 
-  This converts a JavaScript string into a C-line array of numbers, 0-terminated.
+  This converts a JavaScript string into a C-line array of numbers, 0-terminated, encoded as UTF-8.
 
   :param stringy: The string to be converted.
   :type stringy: String
@@ -246,7 +246,7 @@ Conversion functions — strings, pointers and arrays
 
 .. js:function:: intArrayToString(array)
 
-  This creates a JavaScript string from a zero-terminated C-line array of numbers.
+  This creates a JavaScript string from an optionally zero-terminated C-line array of numbers, interpreted as UTF-8.
 
   :param array: The array to convert.
   :returns: A ``String``, containing the content of ``array``.
diff --git a/src/arrayUtils.js b/src/arrayUtils.js
index d8940dea944ef..cecf7c64c4552 100644
--- a/src/arrayUtils.js
+++ b/src/arrayUtils.js
@@ -13,16 +13,5 @@ function intArrayFromString(stringy, dontAddNull, length) {
 }
 
 function intArrayToString(array) {
-  var ret = [];
-  for (var i = 0; i < array.length; i++) {
-    var chr = array[i];
-    if (chr > 0xFF) {
-      if (ASSERTIONS) {
-        assert(false, 'Character code ' + chr + ' (' + String.fromCharCode(chr) + ')  at offset ' + i + ' not in 0x00-0xFF.');
-      }
-      chr &= 0xFF;
-    }
-    ret.push(String.fromCharCode(chr));
-  }
-  return ret.join('');
+  return UTF8ArrayToString(array, 0, array.length);
 }