summaryrefslogtreecommitdiff
path: root/tools/js2c.py
diff options
context:
space:
mode:
authorRefael Ackermann <refack@gmail.com>2019-05-30 11:54:09 -0400
committerRich Trott <rtrott@gmail.com>2019-06-02 12:03:40 +0200
commitcb92d243e8b3603cf4abd231abe83179950927fc (patch)
tree33f4125357bb7dbd8efa85c32e0cdb71c2fcca04 /tools/js2c.py
parentdb013e1cd3947ed0ae9dddaec127f1707e91a2ee (diff)
downloadandroid-node-v8-cb92d243e8b3603cf4abd231abe83179950927fc.tar.gz
android-node-v8-cb92d243e8b3603cf4abd231abe83179950927fc.tar.bz2
android-node-v8-cb92d243e8b3603cf4abd231abe83179950927fc.zip
tools: fix js2c regression
PR-URL: https://github.com/nodejs/node/pull/27980 Reviewed-By: Michaƫl Zasso <targos@protonmail.com> Reviewed-By: Ben Noordhuis <info@bnoordhuis.nl> Reviewed-By: Joyee Cheung <joyeec9h3@gmail.com>
Diffstat (limited to 'tools/js2c.py')
-rwxr-xr-xtools/js2c.py22
1 files changed, 19 insertions, 3 deletions
diff --git a/tools/js2c.py b/tools/js2c.py
index 4131587655..c3ac53f14b 100755
--- a/tools/js2c.py
+++ b/tools/js2c.py
@@ -200,6 +200,12 @@ UnionBytes NativeModuleLoader::GetConfig() {{
}} // namespace node
"""
+ONE_BYTE_STRING = """
+static const uint8_t {0}[] = {{
+{1}
+}};
+"""
+
TWO_BYTE_STRING = """
static const uint16_t {0}[] = {{
{1}
@@ -215,15 +221,25 @@ SLUGGER_RE =re.compile('[.\-/]')
is_verbose = False
def GetDefinition(var, source, step=30):
- encoded_source = bytearray(source, 'utf-16le')
- code_points = [encoded_source[i] + (encoded_source[i+1] * 256) for i in range(0, len(encoded_source), 2)]
+ template = ONE_BYTE_STRING
+ code_points = [ord(c) for c in source]
+ if any(c > 127 for c in code_points):
+ template = TWO_BYTE_STRING
+ # Treat non-ASCII as UTF-8 and encode as UTF-16 Little Endian.
+ encoded_source = bytearray(source, 'utf-16le')
+ code_points = [
+ encoded_source[i] + (encoded_source[i + 1] * 256)
+ for i in range(0, len(encoded_source), 2)
+ ]
+
# For easier debugging, align to the common 3 char for code-points.
elements_s = ['%3s' % x for x in code_points]
# Put no more then `step` code-points in a line.
slices = [elements_s[i:i + step] for i in range(0, len(elements_s), step)]
lines = [','.join(s) for s in slices]
array_content = ',\n'.join(lines)
- definition = TWO_BYTE_STRING.format(var, array_content)
+ definition = template.format(var, array_content)
+
return definition, len(code_points)