From 4e9da1053d8128df5ced222daa01fed8d00fd188 Mon Sep 17 00:00:00 2001 From: Alon Zakai Date: Mon, 15 Jun 2015 15:27:37 -0700 Subject: [PATCH] refactor string memory initializer into tools/shared.py --- emcc | 24 +----------------------- tools/shared.py | 24 ++++++++++++++++++++++++ 2 files changed, 25 insertions(+), 23 deletions(-) diff --git a/emcc b/emcc index 57110025a..725540742 100755 --- a/emcc +++ b/emcc @@ -1355,29 +1355,7 @@ try: if not membytes: return '' if not memory_init_file: # memory initializer in a string literal - s = list(membytes) - if shared.Settings.ASSERTIONS: - # append checksum of length and content - crcTable = [] - for i in range(256): - crc = i - for bit in range(8): - crc = (crc >> 1) ^ ((crc & 1) * 0xedb88320) - crcTable.append(crc) - crc = 0xffffffff - n = len(s) - crc = crcTable[(crc ^ n) & 0xff] ^ (crc >> 8) - crc = crcTable[(crc ^ (n >> 8)) & 0xff] ^ (crc >> 8) - for i in s: - crc = crcTable[(crc ^ i) & 0xff] ^ (crc >> 8) - for i in range(4): - s.append((crc >> (8 * i)) & 0xff) - s = ''.join(map(chr, s)) - s = s.replace('\\', '\\\\').replace("'", "\\'") - s = s.replace('\n', '\\n').replace('\r', '\\r') - def escape(x): return '\\x{:02x}'.format(ord(x.group())) - s = re.sub('[\x80-\xff]', escape, s) - return "memoryInitializer = '%s';" % s + return "memoryInitializer = '%s';" % shared.JS.generate_string_initializer(list(membytes)) open(memfile, 'wb').write(''.join(map(chr, membytes))) if DEBUG: # Copy into temp dir as well, so can be run there too diff --git a/tools/shared.py b/tools/shared.py index 0ed580484..0c1bcc4cd 100644 --- a/tools/shared.py +++ b/tools/shared.py @@ -1810,6 +1810,30 @@ class JS: if len(contents) <= JS.INITIALIZER_CHUNK_SIZE: return None return JS.replace_initializers(src, JS.split_initializer(contents)) + @staticmethod + def generate_string_initializer(s): + if Settings.ASSERTIONS: + # append checksum of length and content + crcTable = [] + for i in range(256): + crc = i + for bit in range(8): + crc = (crc >> 1) ^ ((crc & 1) * 0xedb88320) + crcTable.append(crc) + crc = 0xffffffff + n = len(s) + crc = crcTable[(crc ^ n) & 0xff] ^ (crc >> 8) + crc = crcTable[(crc ^ (n >> 8)) & 0xff] ^ (crc >> 8) + for i in s: + crc = crcTable[(crc ^ i) & 0xff] ^ (crc >> 8) + for i in range(4): + s.append((crc >> (8 * i)) & 0xff) + s = ''.join(map(chr, s)) + s = s.replace('\\', '\\\\').replace("'", "\\'") + s = s.replace('\n', '\\n').replace('\r', '\\r') + def escape(x): return '\\x{:02x}'.format(ord(x.group())) + return re.sub('[\x80-\xff]', escape, s) + # Compression of code and data for smaller downloads class Compression: on = False