emit memory init files when closure is run, fixes #1547

This commit is contained in:
Alon Zakai 2013-08-24 19:59:16 -07:00
Родитель fcf52d655c
Коммит e3a06c12b1
3 изменённых файлов: 34 добавлений и 29 удалений

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

@ -1577,6 +1577,35 @@ try:
js_transform_tempfiles = [final]
if memory_init_file:
if shared.Settings.USE_TYPED_ARRAYS != 2:
if type(memory_init_file) == int: logging.warning('memory init file requires typed arrays mode 2')
else:
memfile = target + '.mem'
shared.try_delete(memfile)
def repl(m):
# handle chunking of the memory initializer
s = re.sub('[\[\]\n\(\)\. ]', '', m.groups(0)[0])
s = s.replace('concat', ',')
if s[-1] == ',': s = s[:-1]
open(memfile, 'wb').write(''.join(map(lambda x: chr(int(x or '0')), s.split(','))))
if DEBUG:
# Copy into temp dir as well, so can be run there too
temp_memfile = os.path.join(shared.EMSCRIPTEN_TEMP_DIR, os.path.basename(memfile))
if os.path.abspath(memfile) != os.path.abspath(memfile):
shutil.copyfile(memfile, temp_memfile)
return 'loadMemoryInitializer("%s");' % os.path.basename(memfile)
src = re.sub(shared.JS.memory_initializer_pattern, repl, open(final).read(), count=1)
open(final + '.mem.js', 'w').write(src)
final += '.mem.js'
js_transform_tempfiles[-1] = final # simple text substitution preserves comment line number mappings
if DEBUG:
if os.path.exists(memfile):
save_intermediate('meminit')
logging.debug('wrote memory initialization to %s' % memfile)
else:
logging.debug('did not see memory initialization')
# It is useful to run several js optimizer passes together, to save on unneeded unparsing/reparsing
js_optimizer_queue = []
js_optimizer_extra_info = {}
@ -1654,35 +1683,6 @@ try:
src = re.sub(r'\n+[ \n]*\n+', '\n', src)
open(final, 'w').write(src)
if memory_init_file:
if shared.Settings.USE_TYPED_ARRAYS != 2:
if type(memory_init_file) == int: logging.warning('memory init file requires typed arrays mode 2')
else:
memfile = target + '.mem'
shared.try_delete(memfile)
def repl(m):
# handle chunking of the memory initializer
s = re.sub('[\[\]\n\(\)\. ]', '', m.groups(0)[0])
s = s.replace('concat', ',')
if s[-1] == ',': s = s[:-1]
open(memfile, 'wb').write(''.join(map(lambda x: chr(int(x or '0')), s.split(','))))
if DEBUG:
# Copy into temp dir as well, so can be run there too
temp_memfile = os.path.join(shared.EMSCRIPTEN_TEMP_DIR, os.path.basename(memfile))
if os.path.abspath(memfile) != os.path.abspath(memfile):
shutil.copyfile(memfile, temp_memfile)
return 'loadMemoryInitializer("%s");' % os.path.basename(memfile)
src = re.sub(shared.JS.memory_initializer_pattern, repl, src, count=1)
open(final + '.mem.js', 'w').write(src)
final += '.mem.js'
js_transform_tempfiles[-1] = final # simple text substitution preserves comment line number mappings
if DEBUG:
if os.path.exists(memfile):
save_intermediate('meminit')
logging.debug('wrote memory initialization to %s' % memfile)
else:
logging.debug('did not see memory initialization')
def generate_source_map(map_file_base_name, offset=0):
jsrun.run_js(shared.path_from_root('tools', 'source-maps', 'sourcemapper.js'),
shared.NODE_JS, js_transform_tempfiles +

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

@ -156,7 +156,11 @@ process(sys.argv[1])
''')
transform.close()
transform_args = ['--js-transform', "%s %s" % (PYTHON, transform_filename)]
if '--memory-init-file' in self.emcc_args:
try_delete(filename + '.o.js.mem')
Building.emcc(filename + '.o.ll', Settings.serialize() + self.emcc_args + transform_args + Building.COMPILER_TEST_OPTS, filename + '.o.js')
if '--memory-init-file' in self.emcc_args:
assert os.path.exists(filename + '.o.js.mem')
if post2: post2(filename + '.o.js')
# Build JavaScript code from source code

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

@ -6762,6 +6762,7 @@ date: 18.07.2013w; day 18, month 7, year 2013, extra: 201, 3
def test_files(self):
if self.emcc_args is not None and '-O2' in self.emcc_args:
self.emcc_args += ['--closure', '1'] # Use closure here, to test we don't break FS stuff
self.emcc_args = filter(lambda x: x != '-g', self.emcc_args) # ensure we test --closure 1 --memory-init-file 1 (-g would disable closure)
Settings.CORRECT_SIGNS = 1 # Just so our output is what we expect. Can flip them both.
post = '''