@@ -107,6 +107,12 @@ def exit_with_error(message):
107
107
108
108
class Intermediate (object ):
109
109
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
110
116
def save_intermediate (name = None , suffix = 'js' ):
111
117
name = os .path .join (shared .get_emscripten_temp_dir (), 'emcc-%d%s.%s' % (Intermediate .counter , '' if name is None else '-' + name , suffix ))
112
118
if isinstance (final , list ):
@@ -1633,49 +1639,14 @@ def get_final():
1633
1639
if options .post_js :
1634
1640
options .post_js = options .post_js .replace ('\r \n ' , '\n ' )
1635
1641
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 ))
1638
1645
outfile .write (options .post_js )
1639
1646
outfile .close ()
1640
1647
options .pre_js = src = options .post_js = None
1641
1648
if DEBUG : save_intermediate ('pre-post' )
1642
1649
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
-
1679
1650
# Apply a source code transformation, if requested
1680
1651
if options .js_transform :
1681
1652
shutil .copyfile (final , final + '.tr.js' )
@@ -1869,9 +1840,9 @@ def get_eliminate():
1869
1840
wasm_text_target , misc_temp_files , optimizer )
1870
1841
1871
1842
if shared .Settings .MODULARIZE :
1872
- final = modularize (final )
1843
+ modularize ()
1873
1844
1874
- final = module_export_name_substitution (final )
1845
+ module_export_name_substitution ()
1875
1846
1876
1847
# The JS is now final. Move it to its final location
1877
1848
shutil .move (final , js_target )
@@ -2433,7 +2404,8 @@ def do_binaryen(target, asm_target, options, memfile, wasm_binary_target,
2433
2404
f .close ()
2434
2405
2435
2406
2436
- def modularize (final ):
2407
+ def modularize ():
2408
+ global final
2437
2409
logging .debug ('Modularizing, assigning to var ' + shared .Settings .EXPORT_NAME )
2438
2410
src = open (final ).read ()
2439
2411
final = final + '.modular.js'
@@ -2453,10 +2425,10 @@ def modularize(final):
2453
2425
''' % {"EXPORT_NAME" : shared .Settings .EXPORT_NAME , "src" : src })
2454
2426
f .close ()
2455
2427
if DEBUG : save_intermediate ('modularized' , 'js' )
2456
- return final
2457
2428
2458
2429
2459
- def module_export_name_substitution (final ):
2430
+ def module_export_name_substitution ():
2431
+ global final
2460
2432
logging .debug ('Private module export name substitution with ' + shared .Settings .EXPORT_NAME )
2461
2433
src = open (final ).read ()
2462
2434
final = final + '.module_export_name_substitution.js'
@@ -2465,7 +2437,6 @@ def module_export_name_substitution(final):
2465
2437
f .write (src .replace (shared .JS .module_export_name_substitution_pattern , replacement ))
2466
2438
f .close ()
2467
2439
if DEBUG : save_intermediate ('module_export_name_substitution' , 'js' )
2468
- return final
2469
2440
2470
2441
2471
2442
def generate_html (target , options , js_target , target_basename ,
0 commit comments