Skip to content

Commit 1eed2ff

Browse files
authored
The previous PR that added exception guards to Gamepad API created a subtle possible bug that the Gamepad API would start to be sampled twice in emscripten_sample_gamepad_data() function. Revise the PR so that the function emscripten_sample_gamepad_data() only calls navigator.getGamepads() exactly once. (#20915)
1 parent 0de50a2 commit 1eed2ff

File tree

1 file changed

+16
-21
lines changed

1 file changed

+16
-21
lines changed

src/library_html5.js

Lines changed: 16 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -2066,39 +2066,34 @@ var LibraryHTML5 = {
20662066
return JSEvents.registerOrRemoveHandler(eventHandler);
20672067
},
20682068

2069-
$disableGamepadApiIfItThrows__docs: '/** @suppress {checkTypes} */',
2070-
$disableGamepadApiIfItThrows: () => {
2071-
try {
2072-
navigator.getGamepads();
2073-
} catch(e) {
2074-
#if ASSERTIONS
2075-
err(`navigator.getGamepads() exists, but failed to execute with exception ${e}. Disabling Gamepad access.`);
2076-
#endif
2077-
navigator.getGamepads = null; // Disable getGamepads() so that other functions will not attempt to use it.
2078-
return 1;
2079-
}
2080-
},
2081-
20822069
emscripten_set_gamepadconnected_callback_on_thread__proxy: 'sync',
2083-
emscripten_set_gamepadconnected_callback_on_thread__deps: ['$registerGamepadEventCallback', '$disableGamepadApiIfItThrows'],
2070+
emscripten_set_gamepadconnected_callback_on_thread__deps: ['$registerGamepadEventCallback', 'emscripten_sample_gamepad_data'],
20842071
emscripten_set_gamepadconnected_callback_on_thread: (userData, useCapture, callbackfunc, targetThread) => {
2085-
if (!navigator.getGamepads || disableGamepadApiIfItThrows()) return {{{ cDefs.EMSCRIPTEN_RESULT_NOT_SUPPORTED }}};
2072+
if (_emscripten_sample_gamepad_data()) return {{{ cDefs.EMSCRIPTEN_RESULT_NOT_SUPPORTED }}};
20862073
return registerGamepadEventCallback({{{ cDefs.EMSCRIPTEN_EVENT_TARGET_WINDOW }}}, userData, useCapture, callbackfunc, {{{ cDefs.EMSCRIPTEN_EVENT_GAMEPADCONNECTED }}}, "gamepadconnected", targetThread);
20872074
},
20882075

20892076
emscripten_set_gamepaddisconnected_callback_on_thread__proxy: 'sync',
2090-
emscripten_set_gamepaddisconnected_callback_on_thread__deps: ['$registerGamepadEventCallback', '$disableGamepadApiIfItThrows'],
2077+
emscripten_set_gamepaddisconnected_callback_on_thread__deps: ['$registerGamepadEventCallback', 'emscripten_sample_gamepad_data'],
20912078
emscripten_set_gamepaddisconnected_callback_on_thread: (userData, useCapture, callbackfunc, targetThread) => {
2092-
if (!navigator.getGamepads || disableGamepadApiIfItThrows()) return {{{ cDefs.EMSCRIPTEN_RESULT_NOT_SUPPORTED }}};
2079+
if (_emscripten_sample_gamepad_data()) return {{{ cDefs.EMSCRIPTEN_RESULT_NOT_SUPPORTED }}};
20932080
return registerGamepadEventCallback({{{ cDefs.EMSCRIPTEN_EVENT_TARGET_WINDOW }}}, userData, useCapture, callbackfunc, {{{ cDefs.EMSCRIPTEN_EVENT_GAMEPADDISCONNECTED }}}, "gamepaddisconnected", targetThread);
20942081
},
20952082

2083+
emscripten_sample_gamepad_data__docs: '/** @suppress {checkTypes} */', // We assign null to navigator.getGamepads, which Closure would like to complain about.
20962084
emscripten_sample_gamepad_data__proxy: 'sync',
2097-
emscripten_sample_gamepad_data__deps: ['$JSEvents', '$disableGamepadApiIfItThrows'],
2085+
emscripten_sample_gamepad_data__deps: ['$JSEvents'],
20982086
emscripten_sample_gamepad_data: () => {
2099-
if (!navigator.getGamepads || disableGamepadApiIfItThrows()) return {{{ cDefs.EMSCRIPTEN_RESULT_NOT_SUPPORTED }}};
2100-
return (JSEvents.lastGamepadState = navigator.getGamepads())
2101-
? {{{ cDefs.EMSCRIPTEN_RESULT_SUCCESS }}} : {{{ cDefs.EMSCRIPTEN_RESULT_NOT_SUPPORTED }}};
2087+
try {
2088+
if (navigator.getGamepads) return (JSEvents.lastGamepadState = navigator.getGamepads())
2089+
? {{{ cDefs.EMSCRIPTEN_RESULT_SUCCESS }}} : {{{ cDefs.EMSCRIPTEN_RESULT_NOT_SUPPORTED }}};
2090+
} catch(e) {
2091+
#if ASSERTIONS
2092+
err(`navigator.getGamepads() exists, but failed to execute with exception ${e}. Disabling Gamepad access.`);
2093+
#endif
2094+
navigator.getGamepads = null; // Disable getGamepads() so that it won't be attempted to be used again.
2095+
}
2096+
return {{{ cDefs.EMSCRIPTEN_RESULT_NOT_SUPPORTED }}};
21022097
},
21032098

21042099
emscripten_get_num_gamepads__proxy: 'sync',

0 commit comments

Comments
 (0)