Skip to content

Commit 5fc15eb

Browse files
committed
binaryen get_subresource_location timing fix (emscripten-core#5296)
1 parent e8d5176 commit 5fc15eb

2 files changed

Lines changed: 30 additions & 5 deletions

File tree

emcc.py

Lines changed: 21 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1115,10 +1115,10 @@ def check(input_file):
11151115
os.environ['EMCC_WASM_BACKEND_BINARYEN'] = '1'
11161116

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

11231123
shared.Settings.ASM_JS = 2 # when targeting wasm, we use a wasm Memory, but that is not compatible with asm.js opts
11241124
shared.Settings.GLOBAL_BASE = 1024 # leave some room for mapping global vars
@@ -2313,6 +2313,23 @@ def do_binaryen(final, target, asm_target, options, memfile, wasm_binary_target,
23132313
passes.append('minifyWhitespace')
23142314
final = shared.Building.js_optimizer_no_asmjs(final, passes)
23152315
if DEBUG: save_intermediate('postclean', 'js')
2316+
# replace placeholder strings with correct subresource locations
2317+
f = open(final, 'r')
2318+
js = f.read()
2319+
f.close()
2320+
f = open(final, 'w')
2321+
for tup in [
2322+
(wasm_text_target, shared.FilenameReplacementStrings.WASM_TEXT_FILE),
2323+
(wasm_binary_target, shared.FilenameReplacementStrings.WASM_BINARY_FILE),
2324+
(asm_target, shared.FilenameReplacementStrings.ASMJS_CODE_FILE)
2325+
]:
2326+
if not os.path.isfile(tup[0]):
2327+
continue
2328+
js = js.replace(tup[1], shared.JS.get_subresource_location(tup[0]))
2329+
if shared.Settings.SINGLE_FILE:
2330+
os.remove(tup[0])
2331+
f.write(js)
2332+
f.close()
23162333
return final
23172334

23182335

tools/shared.py

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2265,6 +2265,12 @@ def reconfigure_cache():
22652265
global Cache
22662266
Cache = cache.Cache(debug=DEBUG_CACHE)
22672267

2268+
# Temporary workaround for SINGLE_FILE, pending refactoring
2269+
class FilenameReplacementStrings:
2270+
WASM_TEXT_FILE = 'FILENAME_REPLACEMENT_STRINGS_WASM_TEXT_FILE'
2271+
WASM_BINARY_FILE = 'FILENAME_REPLACEMENT_STRINGS_WASM_BINARY_FILE'
2272+
ASMJS_CODE_FILE = 'FILENAME_REPLACEMENT_STRINGS_ASMJS_CODE_FILE'
2273+
22682274
class JS:
22692275
memory_initializer_pattern = '/\* memory initializer \*/ allocate\(\[([\d, ]*)\], "i8", ALLOC_NONE, ([\d+Runtime\.GLOBAL_BASEHgb]+)\);'
22702276
no_memory_initializer_pattern = '/\* no memory initializer \*/'
@@ -2279,7 +2285,9 @@ def to_nice_ident(ident): # limited version of the JS function toNiceIdent
22792285

22802286
# Returns the subresource location for run-time access
22812287
@staticmethod
2282-
def get_subresource_location(path, data_uri=Settings.SINGLE_FILE):
2288+
def get_subresource_location(path, data_uri=None):
2289+
if data_uri is None:
2290+
data_uri = Settings.SINGLE_FILE
22832291
if data_uri:
22842292
Settings.INCLUDE_THIRD_PARTY_SODIUMUTIL = 1
22852293
f = open(path, 'rb')

0 commit comments

Comments
 (0)