refactor string memory initializer into tools/shared.py

This commit is contained in:
Alon Zakai 2015-06-15 15:27:37 -07:00
Родитель 4c46de3435
Коммит 4e9da1053d
2 изменённых файлов: 25 добавлений и 23 удалений

24
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

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

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