Errors optimizing with Closure Compiler (Emscripten)

Compiling an application using recent Emscripten and FMOD 2.02.15 upstream/w32/fmod_wasm.a with the --closure=1 option to optimize JavaScript I get a lot of errors that seem to originate from FMOD’s JavaScript code:

building:ERROR: Closure compiler run failed:

building:ERROR: Application.js:1001:2: ERROR - [JSC_UNDEFINED_VARIABLE] variable contextForCheck is undeclared
  1001|   contextForCheck = new (window.AudioContext || window.webkitAudioContext)();
          ^^^^^^^^^^^^^^^

Application.js:1005:2: ERROR - [JSC_UNDEFINED_VARIABLE] variable retValue is undeclared
  1005|   retValue = 0;
          ^^^^^^^^

Application.js:1016:2: ERROR - [JSC_UNDEFINED_VARIABLE] variable infocontext is undeclared
  1016|   infocontext = new (window.AudioContext || window.webkitAudioContext)();
          ^^^^^^^^^^^

Application.js:1020:2: ERROR - [JSC_UNDEFINED_VARIABLE] variable inforate is undeclared
  1020|   inforate = infocontext.sampleRate;
          ^^^^^^^^

Application.js:1026:2: ERROR - [JSC_UNDEFINED_VARIABLE] variable mInputRegistered is undeclared
  1026|   mInputRegistered = false;
          ^^^^^^^^^^^^^^^^

Application.js:1029:2: ERROR - [JSC_UNDEFINED_VARIABLE] variable context is undeclared
  1029|   context = new (window.AudioContext || window.webkitAudioContext)();
          ^^^^^^^

Application.js:1033:2: ERROR - [JSC_UNDEFINED_VARIABLE] variable FMOD_JS_MixFunction is undeclared
  1033|   FMOD_JS_MixFunction = Module.cwrap("FMOD_JS_MixFunction", "void", [ "number" ]);
          ^^^^^^^^^^^^^^^^^^^

Application.js:1037:2: ERROR - [JSC_UNDEFINED_VARIABLE] variable _as_script_node is undeclared
  1037|   _as_script_node = context.createScriptProcessor($1, 0, $0);
          ^^^^^^^^^^^^^^^

Application.js:1040:38: ERROR - [JSC_UNDEFINED_VARIABLE] variable OutputWebAudio_resumeAudio is undeclared
  1040|   window.addEventListener("touchend", OutputWebAudio_resumeAudio, false);
                                              ^^^^^^^^^^^^^^^^^^^^^^^^^^

Application.js:1054:3: ERROR - [JSC_UNDEFINED_VARIABLE] variable _as_output_buffer is undeclared
  1054|    _as_output_buffer = audioProcessingEvent.outputBuffer;
           ^^^^^^^^^^^^^^^^^

Application.js:1087:2: ERROR - [JSC_UNDEFINED_VARIABLE] variable mWorkletNode is undeclared
  1087|   mWorkletNode = null;
          ^^^^^^^^^^^^

Application.js:1088:2: ERROR - [JSC_UNDEFINED_VARIABLE] variable mModulePolling is undeclared
  1088|   mModulePolling = false;
          ^^^^^^^^^^^^^^

Application.js:1089:2: ERROR - [JSC_UNDEFINED_VARIABLE] variable mModuleLoading is undeclared
  1089|   mModuleLoading = false;
          ^^^^^^^^^^^^^^

Application.js:1090:2: ERROR - [JSC_UNDEFINED_VARIABLE] variable mStartInterval is undeclared
  1090|   mStartInterval = null;
          ^^^^^^^^^^^^^^

Application.js:1091:2: ERROR - [JSC_UNDEFINED_VARIABLE] variable mStopInterval is undeclared
  1091|   mStopInterval = null;
          ^^^^^^^^^^^^^

Application.js:1092:2: ERROR - [JSC_UNDEFINED_VARIABLE] variable mSuspendInterval is undeclared
  1092|   mSuspendInterval = null;
          ^^^^^^^^^^^^^^^^

Application.js:1093:2: ERROR - [JSC_UNDEFINED_VARIABLE] variable mResumeInterval is undeclared
  1093|   mResumeInterval = null;
          ^^^^^^^^^^^^^^^

Application.js:1094:2: ERROR - [JSC_UNDEFINED_VARIABLE] variable mWorkletNodeConnected is undeclared
  1094|   mWorkletNodeConnected = false;
          ^^^^^^^^^^^^^^^^^^^^^

Application.js:1096:2: ERROR - [JSC_UNDEFINED_VARIABLE] variable mContext is undeclared
  1096|   mContext = new (window.AudioContext || window.webkitAudioContext)();
          ^^^^^^^^

Application.js:1106:3: ERROR - [JSC_UNDEFINED_VARIABLE] variable mAddModuleRef is undeclared
  1106|    mAddModuleRef = 0;
           ^^^^^^^^^^^^^

Application.js:1114:2: ERROR - [JSC_UNDEFINED_VARIABLE] variable FMOD_JS_MixerSlowpathFunction is undeclared
  1114|   FMOD_JS_MixerSlowpathFunction = Module.cwrap("FMOD_JS_MixerSlowpathFunction", "void", []);
          ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

Application.js:1115:2: ERROR - [JSC_UNDEFINED_VARIABLE] variable FMOD_JS_MixerFastpathFunction is undeclared
  1115|   FMOD_JS_MixerFastpathFunction = Module.cwrap("FMOD_JS_MixerFastpathFunction", "void", [ "number" ]);
          ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

Application.js:1119:2: ERROR - [JSC_UNDEFINED_VARIABLE] variable mSpeakerChannelCount is undeclared
  1119|   mSpeakerChannelCount = $0;
          ^^^^^^^^^^^^^^^^^^^^

Application.js:1121:2: ERROR - [JSC_UNDEFINED_VARIABLE] variable mUrl is undeclared
  1121|   mUrl = null;
          ^^^^

Application.js:1122:2: ERROR - [JSC_UNDEFINED_VARIABLE] variable mOutputData is undeclared
  1122|   mOutputData = null;
          ^^^^^^^^^^^

Application.js:1123:2: ERROR - [JSC_UNDEFINED_VARIABLE] variable mSharedArrayBuffers is undeclared
  1123|   mSharedArrayBuffers = false;
          ^^^^^^^^^^^^^^^^^^^

Application.js:1146:38: ERROR - [JSC_UNDEFINED_VARIABLE] variable OutputAudioWorklet_resumeAudio is undeclared
  1146|   window.addEventListener("touchend", OutputAudioWorklet_resumeAudio, false);
                                              ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

Application.js:1180:19: ERROR - [JSC_UNDEFINED_VARIABLE] variable waitForAudioWorklet is undeclared
  1180|   mStartInterval = waitForAudioWorklet(function() {
                           ^^^^^^^^^^^^^^^^^^^

28 error(s), 0 warning(s)

I think these errors stem from missing var keywords. As this code is integrated into the .a library file I can’t change it. Can this be fixed?

Hi,

This appears to be caused by a known issue wherein FMOD JS doesn’t respect strict mode. Unfortunately, all I can recommend is that you not use closure compiler optimizations, and if you’re not already, consider using either the -Os or -Oz optimization flags for reduced code size.