Skip to content

Commit b93f9de

Browse files
authored
Use arrow functions in test code. NFC (#19654)
Also, be consistent about how we add attributes to the Module object in pre-js files.
1 parent 84e9bb1 commit b93f9de

File tree

4 files changed

+135
-199
lines changed

4 files changed

+135
-199
lines changed

emcc.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3940,7 +3940,7 @@ def modularize():
39403940
if (typeof exports === 'object' && typeof module === 'object')
39413941
module.exports = %(EXPORT_NAME)s;
39423942
else if (typeof define === 'function' && define['amd'])
3943-
define([], function() { return %(EXPORT_NAME)s; });
3943+
define([], () => %(EXPORT_NAME)s);
39443944
else if (typeof exports === 'object')
39453945
exports["%(EXPORT_NAME)s"] = %(EXPORT_NAME)s;
39463946
''' % {'EXPORT_NAME': settings.EXPORT_NAME})

test/test_browser.py

Lines changed: 41 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -441,9 +441,8 @@ def make_main_two_files(path1, path2, nonexistingpath):
441441
# With FS.preloadFile
442442

443443
create_file('pre.js', '''
444-
Module.preRun = function() {
445-
FS.createPreloadedFile('/', 'someotherfile.txt', 'somefile.txt', true, false); // we need --use-preload-plugins for this.
446-
};
444+
// we need --use-preload-plugins for this.
445+
Module.preRun = () => FS.createPreloadedFile('/', 'someotherfile.txt', 'somefile.txt', true, false);
447446
''')
448447
make_main('someotherfile.txt')
449448
self.btest_exit('main.cpp', args=['--pre-js', 'pre.js', '--use-preload-plugins'])
@@ -714,7 +713,7 @@ def setup(assetLocalization):
714713
<center><canvas id='canvas' width='256' height='256'></canvas></center>
715714
<hr><div id='output'></div><hr>
716715
<script type='text/javascript'>
717-
window.onerror = function(error) {
716+
window.onerror = (error) => {
718717
window.disableErrorReporting = true;
719718
window.onerror = null;
720719
var result = error.indexOf("test.data") >= 0 ? 1 : 0;
@@ -868,7 +867,7 @@ def post_manual_reftest(self):
868867
%s
869868
870869
var windowClose = window.close;
871-
window.close = function() {
870+
window.close = () => {
872871
// wait for rafs to arrive and the screen to update before reftesting
873872
setTimeout(function() {
874873
doReftest();
@@ -936,8 +935,7 @@ def test_sdl_key(self, delay):
936935

937936
def test_sdl_key_proxy(self):
938937
create_file('pre.js', '''
939-
var Module = {};
940-
Module.postRun = function() {
938+
Module.postRun = () => {
941939
function doOne() {
942940
Module._one();
943941
setTimeout(doOne, 1000/60);
@@ -1026,7 +1024,7 @@ def post():
10261024

10271025
def test_sdl_text(self):
10281026
create_file('pre.js', '''
1029-
Module.postRun = function() {
1027+
Module.postRun = () => {
10301028
function doOne() {
10311029
Module._one();
10321030
setTimeout(doOne, 1000/60);
@@ -1173,10 +1171,8 @@ def test_sdl_joystick_1(self):
11731171
create_file('pre.js', '''
11741172
var gamepads = [];
11751173
// Spoof this function.
1176-
navigator['getGamepads'] = function() {
1177-
return gamepads;
1178-
};
1179-
window['addNewGamepad'] = function(id, numAxes, numButtons) {
1174+
navigator['getGamepads'] = () => gamepads;
1175+
window['addNewGamepad'] = (id, numAxes, numButtons) => {
11801176
var index = gamepads.length;
11811177
gamepads.push({
11821178
axes: new Array(numAxes),
@@ -1188,13 +1184,13 @@ def test_sdl_joystick_1(self):
11881184
for (i = 0; i < numAxes; i++) gamepads[index].axes[i] = 0;
11891185
for (i = 0; i < numButtons; i++) gamepads[index].buttons[i] = 0;
11901186
};
1191-
window['simulateGamepadButtonDown'] = function (index, button) {
1187+
window['simulateGamepadButtonDown'] = (index, button) => {
11921188
gamepads[index].buttons[button] = 1;
11931189
};
1194-
window['simulateGamepadButtonUp'] = function (index, button) {
1190+
window['simulateGamepadButtonUp'] = (index, button) => {
11951191
gamepads[index].buttons[button] = 0;
11961192
};
1197-
window['simulateAxisMotion'] = function (index, axis, value) {
1193+
window['simulateAxisMotion'] = (index, axis, value) => {
11981194
gamepads[index].axes[axis] = value;
11991195
};
12001196
''')
@@ -1207,10 +1203,8 @@ def test_sdl_joystick_2(self):
12071203
create_file('pre.js', '''
12081204
var gamepads = [];
12091205
// Spoof this function.
1210-
navigator['getGamepads'] = function() {
1211-
return gamepads;
1212-
};
1213-
window['addNewGamepad'] = function(id, numAxes, numButtons) {
1206+
navigator['getGamepads'] = () => gamepads;
1207+
window['addNewGamepad'] = (id, numAxes, numButtons) => {
12141208
var index = gamepads.length;
12151209
gamepads.push({
12161210
axes: new Array(numAxes),
@@ -1224,15 +1218,15 @@ def test_sdl_joystick_2(self):
12241218
for (i = 0; i < numButtons; i++) gamepads[index].buttons[i] = { pressed: false, value: 0 };
12251219
};
12261220
// FF mutates the original objects.
1227-
window['simulateGamepadButtonDown'] = function (index, button) {
1221+
window['simulateGamepadButtonDown'] = (index, button) => {
12281222
gamepads[index].buttons[button].pressed = true;
12291223
gamepads[index].buttons[button].value = 1;
12301224
};
1231-
window['simulateGamepadButtonUp'] = function (index, button) {
1225+
window['simulateGamepadButtonUp'] = (index, button) => {
12321226
gamepads[index].buttons[button].pressed = false;
12331227
gamepads[index].buttons[button].value = 0;
12341228
};
1235-
window['simulateAxisMotion'] = function (index, axis, value) {
1229+
window['simulateAxisMotion'] = (index, axis, value) => {
12361230
gamepads[index].axes[axis] = value;
12371231
};
12381232
''')
@@ -1246,10 +1240,8 @@ def test_glfw_joystick(self):
12461240
create_file('pre.js', '''
12471241
var gamepads = [];
12481242
// Spoof this function.
1249-
navigator['getGamepads'] = function() {
1250-
return gamepads;
1251-
};
1252-
window['addNewGamepad'] = function(id, numAxes, numButtons) {
1243+
navigator['getGamepads'] = () => gamepads;
1244+
window['addNewGamepad'] = (id, numAxes, numButtons) => {
12531245
var index = gamepads.length;
12541246
var gamepad = {
12551247
axes: new Array(numAxes),
@@ -1269,15 +1261,15 @@ def test_glfw_joystick(self):
12691261
window.dispatchEvent(event);
12701262
};
12711263
// FF mutates the original objects.
1272-
window['simulateGamepadButtonDown'] = function (index, button) {
1264+
window['simulateGamepadButtonDown'] = (index, button) => {
12731265
gamepads[index].buttons[button].pressed = true;
12741266
gamepads[index].buttons[button].value = 1;
12751267
};
1276-
window['simulateGamepadButtonUp'] = function (index, button) {
1268+
window['simulateGamepadButtonUp'] = (index, button) => {
12771269
gamepads[index].buttons[button].pressed = false;
12781270
gamepads[index].buttons[button].value = 0;
12791271
};
1280-
window['simulateAxisMotion'] = function (index, axis, value) {
1272+
window['simulateAxisMotion'] = (index, axis, value) => {
12811273
gamepads[index].axes[axis] = value;
12821274
};
12831275
''')
@@ -1394,7 +1386,7 @@ def test_fs_idbfs_fsync(self):
13941386
# sync from persisted state into memory before main()
13951387
self.set_setting('DEFAULT_LIBRARY_FUNCS_TO_INCLUDE', '$ccall')
13961388
create_file('pre.js', '''
1397-
Module.preRun = function() {
1389+
Module.preRun = () => {
13981390
addRunDependency('syncfs');
13991391
14001392
FS.mkdir('/working1');
@@ -1420,8 +1412,7 @@ def test_fs_workerfs_read(self):
14201412
secret = 'a' * 10
14211413
secret2 = 'b' * 10
14221414
create_file('pre.js', '''
1423-
var Module = {};
1424-
Module.preRun = function() {
1415+
Module.preRun = () => {
14251416
var blob = new Blob(['%s']);
14261417
var file = new File(['%s'], 'file.txt');
14271418
FS.mkdir('/work');
@@ -1658,7 +1649,7 @@ def test_worker(self):
16581649
Worker Test
16591650
<script>
16601651
var worker = new Worker('worker.js');
1661-
worker.onmessage = function(event) {
1652+
worker.onmessage = (event) => {
16621653
var xhr = new XMLHttpRequest();
16631654
xhr.open('GET', 'http://localhost:%s/report_result?' + event.data);
16641655
xhr.send();
@@ -1704,7 +1695,7 @@ def test_chunked_synchronous_xhr(self):
17041695
<script>
17051696
var worker = new Worker(""" + json.dumps(worker_filename) + r""");
17061697
var buffer = [];
1707-
worker.onmessage = function(event) {
1698+
worker.onmessage = (event) => {
17081699
if (event.data.channel === "stdout") {
17091700
var xhr = new XMLHttpRequest();
17101701
xhr.open('GET', 'http://localhost:%s/report_result?' + event.data.line);
@@ -1731,14 +1722,13 @@ def test_chunked_synchronous_xhr(self):
17311722
""" % self.port)
17321723

17331724
create_file('worker_prejs.js', r"""
1734-
if (typeof(Module) === "undefined") Module = {};
1735-
Module["arguments"] = ["/bigfile"];
1736-
Module["preInit"] = function() {
1737-
FS.createLazyFile('/', "bigfile", "http://localhost:11111/bogus_file_path", true, false);
1725+
Module.arguments = ["/bigfile"];
1726+
Module.preInit = () => {
1727+
FS.createLazyFile('/', "bigfile", "http://localhost:11111/bogus_file_path", true, false);
17381728
};
17391729
var doTrace = true;
1740-
Module["print"] = function(s) { self.postMessage({channel: "stdout", line: s}); };
1741-
Module["printErr"] = function(s) { self.postMessage({channel: "stderr", char: s, trace: ((doTrace && s === 10) ? new Error().stack : null)}); doTrace = false; };
1730+
Module.print = (s) => self.postMessage({channel: "stdout", line: s});
1731+
Module.printErr = (s) => { self.postMessage({channel: "stderr", char: s, trace: ((doTrace && s === 10) ? new Error().stack : null)}); doTrace = false; };
17421732
""")
17431733
self.compile_btest([test_file('checksummer.c'), '-g', '-sSMALL_XHR_CHUNKS', '-o', worker_filename,
17441734
'--pre-js', 'worker_prejs.js'])
@@ -2371,7 +2361,7 @@ def test_runtimelink(self):
23712361
def test_pre_run_deps(self):
23722362
# Adding a dependency in preRun will delay run
23732363
create_file('pre.js', '''
2374-
Module.preRun = function() {
2364+
Module.preRun = () => {
23752365
addRunDependency();
23762366
out('preRun called, added a dependency...');
23772367
setTimeout(function() {
@@ -2390,10 +2380,8 @@ def test_mem_init(self):
23902380
function myJSCallback() { // called from main()
23912381
Module._note(1);
23922382
}
2393-
Module.preRun = function() {
2394-
addOnPreMain(function() {
2395-
Module._note(2);
2396-
});
2383+
Module.preRun = () => {
2384+
addOnPreMain(() => Module._note(2));
23972385
};
23982386
''')
23992387
create_file('post.js', '''
@@ -2417,7 +2405,7 @@ def test(what, status):
24172405
xhr.responseType = 'arraybuffer';
24182406
xhr.send(null);
24192407
2420-
console.warn = function(x) {
2408+
console.warn = (x) => {
24212409
if (x.indexOf('a problem seems to have happened with Module.memoryInitializerRequest') >= 0) {
24222410
maybeReportResultToServer('got_error');
24232411
}
@@ -2504,9 +2492,7 @@ def test_runtime_misuse(self, mode):
25042492
''' % self.port
25052493

25062494
create_file('pre_runtime.js', r'''
2507-
Module.onRuntimeInitialized = function(){
2508-
myJSCallback();
2509-
};
2495+
Module.onRuntimeInitialized = () => myJSCallback();
25102496
''')
25112497

25122498
for filename, extra_args, second_code in [
@@ -2876,7 +2862,7 @@ def test_locate_file(self, args):
28762862
}
28772863
''')
28782864
create_file('data.txt', 'load me right before...')
2879-
create_file('pre.js', 'Module.locateFile = function(x) { return "sub/" + x };')
2865+
create_file('pre.js', 'Module.locateFile = (x) => "sub/" + x;')
28802866
self.run_process([FILE_PACKAGER, 'test.data', '--preload', 'data.txt'], stdout=open('data.js', 'w'))
28812867
# put pre.js first, then the file packager data, so locateFile is there for the file loading code
28822868
self.compile_btest(['src.cpp', '-O2', '-g', '--pre-js', 'pre.js', '--pre-js', 'data.js', '-o', 'page.html', '-sFORCE_FILESYSTEM', '-sWASM=' + str(wasm)] + args, reporting=Reporting.JS_ONLY)
@@ -2988,7 +2974,7 @@ def test_sdl2_image_formats(self):
29882974

29892975
def test_sdl2_key(self):
29902976
create_file('pre.js', '''
2991-
Module.postRun = function() {
2977+
Module.postRun = () => {
29922978
function doOne() {
29932979
Module._one();
29942980
setTimeout(doOne, 1000/60);
@@ -3017,7 +3003,7 @@ def test_sdl2_key(self):
30173003

30183004
def test_sdl2_text(self):
30193005
create_file('pre.js', '''
3020-
Module.postRun = function() {
3006+
Module.postRun = () => {
30213007
function doOne() {
30223008
Module._one();
30233009
setTimeout(doOne, 1000/60);
@@ -3609,7 +3595,7 @@ def test_dynamic_link(self):
36093595
temp[1] = 'x';
36103596
EM_ASM({
36113597
Module.realPrint = out;
3612-
out = function(x) {
3598+
out = (x) => {
36133599
if (!Module.printed) Module.printed = x;
36143600
Module.realPrint(x);
36153601
};
@@ -3677,7 +3663,7 @@ def do_run(src, expected_output, emcc_args):
36773663
# setup by the shell).
36783664
create_file('post.js', r'''
36793665
Module.realPrint = out;
3680-
out = function(x) {
3666+
out = (x) => {
36813667
if (!Module.printed) Module.printed = "";
36823668
Module.printed += x + '\n'; // out is passed str without last \n
36833669
Module.realPrint(x);
@@ -4379,7 +4365,7 @@ def test_binaryen_async(self):
43794365
};
43804366
}
43814367
// show stderr for the viewer's fun
4382-
err = function(x) {
4368+
err = (x) => {
43834369
out('<<< ' + x + ' >>>');
43844370
console.log(x);
43854371
};

0 commit comments

Comments
 (0)