Skip to content

Commit

Permalink
binaryen get_subresource_location timing fix (emscripten-core#5296)
Browse files Browse the repository at this point in the history
  • Loading branch information
buu700 committed Jun 15, 2017
1 parent e8d5176 commit 5fc15eb
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 5 deletions.
25 changes: 21 additions & 4 deletions emcc.py
Original file line number Diff line number Diff line change
Expand Up @@ -1115,10 +1115,10 @@ def check(input_file):
os.environ['EMCC_WASM_BACKEND_BINARYEN'] = '1'

if shared.Settings.BINARYEN:
# set file locations, so that JS glue can find what it needs
shared.Settings.WASM_TEXT_FILE = shared.JS.get_subresource_location(wasm_text_target)
shared.Settings.WASM_BINARY_FILE = shared.JS.get_subresource_location(wasm_binary_target)
shared.Settings.ASMJS_CODE_FILE = shared.JS.get_subresource_location(asm_target)
# placeholder strings for JS glue, to be replaced with subresource locations in do_binaryen
shared.Settings.WASM_TEXT_FILE = shared.FilenameReplacementStrings.WASM_TEXT_FILE
shared.Settings.WASM_BINARY_FILE = shared.FilenameReplacementStrings.WASM_BINARY_FILE
shared.Settings.ASMJS_CODE_FILE = shared.FilenameReplacementStrings.ASMJS_CODE_FILE

shared.Settings.ASM_JS = 2 # when targeting wasm, we use a wasm Memory, but that is not compatible with asm.js opts
shared.Settings.GLOBAL_BASE = 1024 # leave some room for mapping global vars
Expand Down Expand Up @@ -2313,6 +2313,23 @@ def do_binaryen(final, target, asm_target, options, memfile, wasm_binary_target,
passes.append('minifyWhitespace')
final = shared.Building.js_optimizer_no_asmjs(final, passes)
if DEBUG: save_intermediate('postclean', 'js')
# replace placeholder strings with correct subresource locations
f = open(final, 'r')
js = f.read()
f.close()
f = open(final, 'w')
for tup in [
(wasm_text_target, shared.FilenameReplacementStrings.WASM_TEXT_FILE),
(wasm_binary_target, shared.FilenameReplacementStrings.WASM_BINARY_FILE),
(asm_target, shared.FilenameReplacementStrings.ASMJS_CODE_FILE)
]:
if not os.path.isfile(tup[0]):
continue
js = js.replace(tup[1], shared.JS.get_subresource_location(tup[0]))
if shared.Settings.SINGLE_FILE:
os.remove(tup[0])
f.write(js)
f.close()
return final


Expand Down
10 changes: 9 additions & 1 deletion tools/shared.py
Original file line number Diff line number Diff line change
Expand Up @@ -2265,6 +2265,12 @@ def reconfigure_cache():
global Cache
Cache = cache.Cache(debug=DEBUG_CACHE)

# Temporary workaround for SINGLE_FILE, pending refactoring
class FilenameReplacementStrings:
WASM_TEXT_FILE = 'FILENAME_REPLACEMENT_STRINGS_WASM_TEXT_FILE'
WASM_BINARY_FILE = 'FILENAME_REPLACEMENT_STRINGS_WASM_BINARY_FILE'
ASMJS_CODE_FILE = 'FILENAME_REPLACEMENT_STRINGS_ASMJS_CODE_FILE'

class JS:
memory_initializer_pattern = '/\* memory initializer \*/ allocate\(\[([\d, ]*)\], "i8", ALLOC_NONE, ([\d+Runtime\.GLOBAL_BASEHgb]+)\);'
no_memory_initializer_pattern = '/\* no memory initializer \*/'
Expand All @@ -2279,7 +2285,9 @@ def to_nice_ident(ident): # limited version of the JS function toNiceIdent

# Returns the subresource location for run-time access
@staticmethod
def get_subresource_location(path, data_uri=Settings.SINGLE_FILE):
def get_subresource_location(path, data_uri=None):
if data_uri is None:
data_uri = Settings.SINGLE_FILE
if data_uri:
Settings.INCLUDE_THIRD_PARTY_SODIUMUTIL = 1
f = open(path, 'rb')
Expand Down

0 comments on commit 5fc15eb

Please sign in to comment.