Skip to content

Commit 01c4612

Browse files
authored
Add an internal setting to track whether closure might be run (#20208)
This new internal setting is true if we run it, or if the user might run it. Use that new setting to gate when to add closure annotations, which should fix remaining issues with closure in emcc working but not after it.
1 parent b725501 commit 01c4612

File tree

7 files changed

+23
-12
lines changed

7 files changed

+23
-12
lines changed

emcc.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2972,6 +2972,15 @@ def get_full_import_name(name):
29722972

29732973
settings.MINIFY_WHITESPACE = settings.OPT_LEVEL >= 2 and settings.DEBUG_LEVEL == 0 and not options.no_minify
29742974

2975+
# Closure might be run if we run it ourselves, or if whitespace is not being
2976+
# minifed. In the latter case we keep both whitespace and comments, and the
2977+
# purpose of the comments might be closure compiler, so also perform all
2978+
# adjustments necessary to ensure that works (which amounts to a few more
2979+
# comments; adding some more of them is not an issue in such a build which
2980+
# includes all comments and whitespace anyhow).
2981+
if settings.USE_CLOSURE_COMPILER or not settings.MINIFY_WHITESPACE:
2982+
settings.MAYBE_CLOSURE_COMPILER = 1
2983+
29752984
return target, wasm_target
29762985

29772986

emscripten.py

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

730730
sorted_items = sorted(send_items_map.items())
731731
prefix = ''
732-
if settings.USE_CLOSURE_COMPILER or not settings.MINIFY_WHITESPACE:
732+
if settings.MAYBE_CLOSURE_COMPILER:
733733
# 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).
734+
# object.
738735
prefix = '/** @export */\n '
739736
return '{\n ' + ',\n '.join(f'{prefix}{k}: {v}' for k, v in sorted_items) + '\n}'
740737

src/library_browser.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -902,7 +902,7 @@ var LibraryBrowser = {
902902
Browser.mainLoop.func = browserIterationFunc;
903903
Browser.mainLoop.arg = arg;
904904

905-
#if USE_CLOSURE_COMPILER
905+
#if MAYBE_CLOSURE_COMPILER
906906
// Closure compiler bug(?): Closure does not see that the assignment
907907
// var thisMainLoopId = Browser.mainLoop.currentlyRunningMainloop
908908
// is a value copy of a number (even with the JSDoc @type annotation)

src/library_glemu.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ var LibraryGLEmulation = {
88
// GL emulation: provides misc. functionality not present in OpenGL ES 2.0 or WebGL
99
$GLEmulation__deps: ['$GLImmediateSetup', 'glEnable', 'glDisable', 'glIsEnabled', 'glGetBooleanv', 'glGetIntegerv', 'glGetString', 'glCreateShader', 'glShaderSource', 'glCompileShader', 'glAttachShader', 'glDetachShader', 'glUseProgram', 'glDeleteProgram', 'glBindAttribLocation', 'glLinkProgram', 'glBindBuffer', 'glGetFloatv', 'glHint', 'glEnableVertexAttribArray', 'glDisableVertexAttribArray', 'glVertexAttribPointer', 'glActiveTexture', '$stringToNewUTF8', '$ptrToString'],
1010
$GLEmulation__postset:
11-
#if USE_CLOSURE_COMPILER
11+
#if MAYBE_CLOSURE_COMPILER
1212
// Forward declare GL functions that are overridden by GLEmulation here to appease Closure compiler.
1313
'/**@suppress {duplicate, undefinedVars}*/var _emscripten_glDrawArrays;' +
1414
'/**@suppress {duplicate, undefinedVars}*/var _emscripten_glDrawElements;' +

src/library_pthread.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,7 @@ var LibraryPThread = {
119119
},
120120

121121
initWorker() {
122-
#if USE_CLOSURE_COMPILER
122+
#if MAYBE_CLOSURE_COMPILER
123123
// worker.js is not compiled together with us, and must access certain
124124
// things.
125125
PThread['receiveObjectTransfer'] = PThread.receiveObjectTransfer;

src/settings_internal.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -238,6 +238,11 @@ var HAS_MAIN = false;
238238
// Set to true if we are linking as C++ and including C++ stdlibs
239239
var LINK_AS_CXX = false;
240240

241+
// Set when closure compiler may be run: Either emcc will run it, or the user
242+
// might run it after emcc. Either way, some JS changes and annotations must be
243+
// emitted in that case for closure compiler.
244+
var MAYBE_CLOSURE_COMPILER = false;
245+
241246
// Set when some minimum browser version triggers doesn't support the
242247
// minimum set of ES6 features. This triggers transpilation to ES5
243248
// using closure compiler.

tools/building.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -337,9 +337,9 @@ def acorn_optimizer(filename, passes, extra_info=None, return_output=False):
337337
f.write('// EXTRA_INFO: ' + extra_info)
338338
filename = temp
339339
cmd = config.NODE_JS + [optimizer, filename] + passes
340-
# Keep JS code comments intact through the acorn optimization pass so that JSDoc comments
341-
# will be carried over to a later Closure run.
342-
if settings.USE_CLOSURE_COMPILER or not settings.MINIFY_WHITESPACE:
340+
# Keep JS code comments intact through the acorn optimization pass so that
341+
# JSDoc comments will be carried over to a later Closure run.
342+
if settings.MAYBE_CLOSURE_COMPILER:
343343
cmd += ['--closureFriendly']
344344
if settings.EXPORT_ES6:
345345
cmd += ['--exportES6']
@@ -681,7 +681,7 @@ def minify_wasm_js(js_file, wasm_file, expensive_optimizations, debug_info):
681681
if not settings.LINKABLE:
682682
passes.append('JSDCE' if not expensive_optimizations else 'AJSDCE')
683683
# Don't minify if we are going to run closure compiler afterwards
684-
minify = settings.MINIFY_WHITESPACE and not settings.USE_CLOSURE_COMPILER
684+
minify = settings.MINIFY_WHITESPACE and not settings.MAYBE_CLOSURE_COMPILER
685685
if minify:
686686
passes.append('minifyWhitespace')
687687
if passes:

0 commit comments

Comments
 (0)