Skip to content

Commit 204c88b

Browse files
authored
Merge pull request #5915 from kripken/eval1-followups
Followups for eval-removal 1 (#5751)
2 parents 7c7b37d + b94d1c9 commit 204c88b

File tree

3 files changed

+43
-46
lines changed

3 files changed

+43
-46
lines changed

emcc.py

Lines changed: 15 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,12 @@ def exit_with_error(message):
107107

108108
class Intermediate(object):
109109
counter = 0
110+
# this method uses the global 'final' variable, which contains the current
111+
# final output file. if a method alters final, and calls this method, then it
112+
# must modify final globally (i.e. it can't receive final as a param and
113+
# return it)
114+
# TODO: refactor all this, a singleton that abstracts over the final output
115+
# and saving of intermediates
110116
def save_intermediate(name=None, suffix='js'):
111117
name = os.path.join(shared.get_emscripten_temp_dir(), 'emcc-%d%s.%s' % (Intermediate.counter, '' if name is None else '-' + name, suffix))
112118
if isinstance(final, list):
@@ -1633,49 +1639,14 @@ def get_final():
16331639
if options.post_js:
16341640
options.post_js = options.post_js.replace('\r\n', '\n')
16351641
outfile = open(final, 'w')
1636-
outfile.write(options.pre_js)
1637-
outfile.write(src) # this may be large, don't join it to others
1642+
# pre-js code goes right after the Module integration code (so it
1643+
# can use Module), we have a marker for it
1644+
outfile.write(src.replace('// {{PRE_JSES}}', options.pre_js))
16381645
outfile.write(options.post_js)
16391646
outfile.close()
16401647
options.pre_js = src = options.post_js = None
16411648
if DEBUG: save_intermediate('pre-post')
16421649

1643-
if not shared.Settings.SIDE_MODULE:
1644-
# The Module object: Our interface to the outside world. We import
1645-
# and export values on it. There are various ways Module can be used:
1646-
# 1. Not defined. We create it here
1647-
# 2. A function parameter, function(Module) { ..generated code.. }
1648-
# 3. pre-run appended it, var Module = {}; ..generated code..
1649-
# 4. External script tag defines var Module.
1650-
# We need to check if Module already exists (e.g. case 3 above).
1651-
# Substitution will be replaced with actual code on later stage of the build,
1652-
# this way Closure Compiler will not mangle it (e.g. case 4. above).
1653-
# Note that if you want to run closure, and also to use Module
1654-
# after the generated code, you will need to define var Module = {};
1655-
# before the code. Then that object will be used in the code, and you
1656-
# can continue to use Module afterwards as well.
1657-
if options.use_closure_compiler:
1658-
# `if (!Module)` is crucial for Closure Compiler here as it will otherwise replace every `Module` occurrence with a string
1659-
module_header = '''
1660-
var Module;
1661-
if (!Module) Module = %s;
1662-
''' % shared.JS.module_export_name_substitution_pattern
1663-
else:
1664-
module_header = '''
1665-
var Module = typeof %(EXPORT_NAME)s !== 'undefined' ? %(EXPORT_NAME)s : {};
1666-
''' % {"EXPORT_NAME": shared.Settings.EXPORT_NAME}
1667-
1668-
# Append module header now as --pre-js is already added
1669-
logging.debug('Appending module header')
1670-
src = open(final).read()
1671-
final += '.wh.js'
1672-
outfile = open(final, 'w')
1673-
outfile.write(module_header)
1674-
outfile.write(src)
1675-
outfile.close()
1676-
src = None
1677-
if DEBUG: save_intermediate('with-header')
1678-
16791650
# Apply a source code transformation, if requested
16801651
if options.js_transform:
16811652
shutil.copyfile(final, final + '.tr.js')
@@ -1869,9 +1840,9 @@ def get_eliminate():
18691840
wasm_text_target, misc_temp_files, optimizer)
18701841

18711842
if shared.Settings.MODULARIZE:
1872-
final = modularize(final)
1843+
modularize()
18731844

1874-
final = module_export_name_substitution(final)
1845+
module_export_name_substitution()
18751846

18761847
# The JS is now final. Move it to its final location
18771848
shutil.move(final, js_target)
@@ -2433,7 +2404,8 @@ def do_binaryen(target, asm_target, options, memfile, wasm_binary_target,
24332404
f.close()
24342405

24352406

2436-
def modularize(final):
2407+
def modularize():
2408+
global final
24372409
logging.debug('Modularizing, assigning to var ' + shared.Settings.EXPORT_NAME)
24382410
src = open(final).read()
24392411
final = final + '.modular.js'
@@ -2453,10 +2425,10 @@ def modularize(final):
24532425
''' % {"EXPORT_NAME": shared.Settings.EXPORT_NAME, "src": src})
24542426
f.close()
24552427
if DEBUG: save_intermediate('modularized', 'js')
2456-
return final
24572428

24582429

2459-
def module_export_name_substitution(final):
2430+
def module_export_name_substitution():
2431+
global final
24602432
logging.debug('Private module export name substitution with ' + shared.Settings.EXPORT_NAME)
24612433
src = open(final).read()
24622434
final = final + '.module_export_name_substitution.js'
@@ -2465,7 +2437,6 @@ def module_export_name_substitution(final):
24652437
f.write(src.replace(shared.JS.module_export_name_substitution_pattern, replacement))
24662438
f.close()
24672439
if DEBUG: save_intermediate('module_export_name_substitution', 'js')
2468-
return final
24692440

24702441

24712442
def generate_html(target, options, js_target, target_basename,

src/shell.js

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,30 @@
1+
#if SIDE_MODULE == 0
2+
// The Module object: Our interface to the outside world. We import
3+
// and export values on it. There are various ways Module can be used:
4+
// 1. Not defined. We create it here
5+
// 2. A function parameter, function(Module) { ..generated code.. }
6+
// 3. pre-run appended it, var Module = {}; ..generated code..
7+
// 4. External script tag defines var Module.
8+
// We need to check if Module already exists (e.g. case 3 above).
9+
// Substitution will be replaced with actual code on later stage of the build,
10+
// this way Closure Compiler will not mangle it (e.g. case 4. above).
11+
// Note that if you want to run closure, and also to use Module
12+
// after the generated code, you will need to define var Module = {};
13+
// before the code. Then that object will be used in the code, and you
14+
// can continue to use Module afterwards as well.
15+
#if USE_CLOSURE_COMPILER
16+
// if (!Module)` is crucial for Closure Compiler here as it will otherwise replace every `Module` occurrence with a string
17+
var Module;
18+
if (!Module) Module = "__EMSCRIPTEN_PRIVATE_MODULE_EXPORT_NAME_SUBSTITUTION__";
19+
#else
20+
var Module = typeof {{{ EXPORT_NAME }}} !== 'undefined' ? {{{ EXPORT_NAME }}} : {};
21+
#endif // USE_CLOSURE_COMPILER
22+
#endif // SIDE_MODULE
23+
24+
// --pre-jses are emitted after the Module integration code, so that they can
25+
// refer to Module (if they choose; they can also define Module)
26+
// {{PRE_JSES}}
27+
128
// Sometimes an existing Module object exists with properties
229
// meant to overwrite the default module functionality. Here
330
// we collect those properties and reapply _after_ we configure

tests/test_core.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6761,8 +6761,7 @@ def test_modularize_closure_pre(self):
67616761
'--pre-js', path_from_root('tests', 'core', 'modularize_closure_pre.js'),
67626762
'--closure', '1',
67636763
'-s', 'MODULARIZE=1',
6764-
'-g1',
6765-
'-O2'
6764+
'-g1'
67666765
]
67676766
def post(filename):
67686767
src = open(filename, 'a')

0 commit comments

Comments
 (0)