Skip to content

Commit 7d7dbde

Browse files
authored
Fix the imports object when running closure after emscripten (#20197)
We need to annotate that object's fields so closure doesn't minify them. If it does then the wasm fails to instantiate with something like Aborted(LinkError: WebAssembly.instantiate(): Import #0 module="env" function="__cxa_throw" error: function import requires a callable) Adds testing related to #20191 Minor code size regressions in metadce tests are because of the new "/**@export*/" comments, which would not show up in a production build (they only appear when whitespace+comments are allowed).
1 parent d6f781c commit 7d7dbde

6 files changed

+22
-7
lines changed

emscripten.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -729,8 +729,12 @@ def create_sending(metadata, library_symbols):
729729

730730
sorted_items = sorted(send_items_map.items())
731731
prefix = ''
732-
if settings.USE_CLOSURE_COMPILER:
733-
# This prevents closure compiler from minifying the field names in this object.
732+
if settings.USE_CLOSURE_COMPILER or not settings.MINIFY_WHITESPACE:
733+
# This prevents closure compiler from minifying the field names in this
734+
# object. Note that we also apply this when whitespace is not minified so
735+
# that closure can be run on the output later, after emscripten runs (and
736+
# when whitespace is present we keep comments as well, so adding more
737+
# comments is not an issue).
734738
prefix = '/** @export */\n '
735739
return '{\n ' + ',\n '.join(f'{prefix}{k}: {v}' for k, v in sorted_items) + '\n}'
736740

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
301
1+
298
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
58269
1+
58303
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
31637
1+
31671
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
56983
1+
57017

test/test_other.py

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7983,7 +7983,9 @@ def run_metadce_test(self, filename, args=[], expected_exists=[], expected_not_e
79837983
start = js.find('{', start)
79847984
self.assertNotEqual(start, -1)
79857985
relevant = js[start + 2:end - 1]
7986-
relevant = relevant.replace(' ', '').replace('"', '').replace("'", '').split(',')
7986+
relevant = relevant.replace(' ', '').replace('"', '').replace("'", '')
7987+
relevant = relevant.replace('/**@export*/', '')
7988+
relevant = relevant.split(',')
79877989
sent = [x.split(':')[0].strip() for x in relevant]
79887990
sent = [x for x in sent if x]
79897991
sent.sort()
@@ -13729,6 +13731,15 @@ def test_no_minify(self):
1372913731
content = read_file('a.out.js')
1373013732
self.assertContained(comment, content)
1373113733

13734+
def test_no_minify_and_later_closure(self):
13735+
# test that running closure after --minify=0 works
13736+
self.run_process([EMCC, test_file('hello_libcxx.cpp'), '-O2', '--minify=0'])
13737+
temp = building.closure_compiler('a.out.js',
13738+
advanced=True,
13739+
extra_closure_args=['--formatting', 'PRETTY_PRINT'])
13740+
shutil.copyfile(temp, 'closured.js')
13741+
self.assertContained('hello, world!', self.run_js('closured.js'))
13742+
1373213743
def test_table_base(self):
1373313744
create_file('test.c', r'''
1373413745
#include <stdio.h>

0 commit comments

Comments
 (0)