Skip to content

Commit a303ebd

Browse files
authored
Cleanup proxyWorker.js and use arrow functions. NFC (#19604)
1 parent 324224f commit a303ebd

File tree

2 files changed

+67
-63
lines changed

2 files changed

+67
-63
lines changed

src/proxyClient.js

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,16 @@
44
* SPDX-License-Identifier: MIT
55
*/
66

7-
// proxy to/from worker
7+
/*
8+
* Proxy events/work to/from an emscripen worker built
9+
* with PROXY_TO_WORKER. This code runs on the main
10+
* thread and is not part of the main emscripten output
11+
* file.
12+
*/
13+
14+
#if !PROXY_TO_WORKER
15+
#error "proxyClient.js should only be included in PROXY_TO_WORKER mode"
16+
#endif
817

918
#if ENVIRONMENT_MAY_BE_NODE
1019
var ENVIRONMENT_IS_NODE = typeof process == 'object' && typeof process.versions == 'object' && typeof process.versions.node == 'string';

src/proxyWorker.js

Lines changed: 57 additions & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -5,16 +5,16 @@
55
*/
66

77
/*
8-
function proxify(object, nick) {
9-
return new Proxy(object, {
10-
get: function(target, name) {
11-
var ret = target[name];
12-
if (ret === undefined) console.log('PROXY ' + [nick, target, name, ret, typeof ret]);
13-
return ret;
14-
}
15-
});
16-
}
17-
*/
8+
* Implements the server/worker side of proxyClient.js.
9+
* This code gets included in the main emscripten output
10+
* when PROXY_TO_WORKER is used. The resulting code then
11+
* needs to be run in a worker and receive events from
12+
* proxyClient.js running on the main thread.
13+
*/
14+
15+
#if !PROXY_TO_WORKER
16+
#error "proxyClient.js should only be included in PROXY_TO_WORKER mode"
17+
#endif
1818

1919
#if ENVIRONMENT_MAY_BE_NODE
2020
if (!ENVIRONMENT_IS_NODE) {
@@ -24,7 +24,7 @@ function FPSTracker(text) {
2424
var last = 0;
2525
var mean = 0;
2626
var counter = 0;
27-
this.tick = function() {
27+
this.tick = () => {
2828
var now = Date.now();
2929
if (last > 0) {
3030
var diff = now - last;
@@ -47,9 +47,9 @@ var KeyboardEvent = {
4747
};
4848

4949
function PropertyBag() {
50-
this.addProperty = function(){};
51-
this.removeProperty = function(){};
52-
this.setProperty = function(){};
50+
this.addProperty = () => {};
51+
this.removeProperty = () => {};
52+
this.setProperty = () => {};
5353
};
5454

5555
var IndexedObjects = {
@@ -77,44 +77,42 @@ function EventListener() {
7777
list.splice(me, 1);
7878
};
7979

80-
this.fireEvent = function fireEvent(event) {
81-
event.preventDefault = function(){};
80+
this.fireEvent = function(event) {
81+
event.preventDefault = () => {};
8282

8383
if (event.type in this.listeners) {
8484
this.listeners[event.type].forEach((listener) => listener(event));
8585
}
86-
};
86+
}
8787
}
8888

8989
function Image() {
9090
IndexedObjects.add(this);
9191
EventListener.call(this);
9292
var src = '';
9393
Object.defineProperty(this, 'src', {
94-
set: function(value) {
94+
set: (value) => {
9595
src = value;
9696
assert(this.id);
9797
postMessage({ target: 'Image', method: 'src', src, id: this.id });
9898
},
99-
get: function() {
100-
return src;
101-
}
99+
get: () => src
102100
});
103101
}
104-
Image.prototype.onload = function(){};
105-
Image.prototype.onerror = function(){};
102+
Image.prototype.onload = () => {};
103+
Image.prototype.onerror = () => {};
106104

107105
var HTMLImageElement = Image;
108106

109107
var window = this;
110108
var windowExtra = new EventListener();
111109
for (var x in windowExtra) window[x] = windowExtra[x];
112110

113-
window.close = function window_close() {
111+
window.close = () => {
114112
postMessage({ target: 'window', method: 'close' });
115113
};
116114

117-
window.alert = function(text) {
115+
window.alert = (text) => {
118116
err('alert forever: ' + text);
119117
while (1) {};
120118
};
@@ -145,11 +143,11 @@ var webGLWorker = new WebGLWorker();
145143

146144
var document = new EventListener();
147145

148-
document.createElement = function document_createElement(what) {
146+
document.createElement = (what) => {
149147
switch (what) {
150148
case 'canvas': {
151149
var canvas = new EventListener();
152-
canvas.ensureData = function canvas_ensureData() {
150+
canvas.ensureData = () => {
153151
if (!canvas.data || canvas.data.width !== canvas.width || canvas.data.height !== canvas.height) {
154152
canvas.data = {
155153
width: canvas.width,
@@ -161,13 +159,13 @@ document.createElement = function document_createElement(what) {
161159
}
162160
}
163161
};
164-
canvas.getContext = function canvas_getContext(type, attributes) {
162+
canvas.getContext = (type, attributes) => {
165163
if (canvas === Module['canvas']) {
166164
postMessage({ target: 'canvas', op: 'getContext', type, attributes });
167165
}
168166
if (type === '2d') {
169167
return {
170-
getImageData: function(x, y, w, h) {
168+
getImageData: (x, y, w, h) => {
171169
assert(x == 0 && y == 0 && w == canvas.width && h == canvas.height);
172170
canvas.ensureData();
173171
return {
@@ -176,15 +174,15 @@ document.createElement = function document_createElement(what) {
176174
data: new Uint8Array(canvas.data.data) // TODO: can we avoid this copy?
177175
};
178176
},
179-
putImageData: function(image, x, y) {
177+
putImageData: (image, x, y) => {
180178
canvas.ensureData();
181179
assert(x == 0 && y == 0 && image.width == canvas.width && image.height == canvas.height);
182180
canvas.data.data.set(image.data); // TODO: can we avoid this copy?
183181
if (canvas === Module['canvas']) {
184182
postMessage({ target: 'canvas', op: 'render', image: canvas.data });
185183
}
186184
},
187-
drawImage: function(image, x, y, w, h, ox, oy, ow, oh) {
185+
drawImage: (image, x, y, w, h, ox, oy, ow, oh) => {
188186
assert (!x && !y && !ox && !oy);
189187
assert(w === ow && h === oh);
190188
assert(canvas.width === w || w === undefined);
@@ -202,7 +200,7 @@ document.createElement = function document_createElement(what) {
202200
}
203201
};
204202
canvas.boundingClientRect = {};
205-
canvas.getBoundingClientRect = function canvas_getBoundingClientRect() {
203+
canvas.getBoundingClientRect = () => {
206204
return {
207205
width: canvas.boundingClientRect.width,
208206
height: canvas.boundingClientRect.height,
@@ -213,69 +211,68 @@ document.createElement = function document_createElement(what) {
213211
};
214212
};
215213
canvas.style = new PropertyBag();
216-
canvas.exitPointerLock = function(){};
214+
canvas.exitPointerLock = () => {};
217215

218216
canvas.width_ = canvas.width_ || 0;
219217
canvas.height_ = canvas.height_ || 0;
220218
Object.defineProperty(canvas, 'width', {
221-
set: function(value) {
219+
set: (value) => {
222220
canvas.width_ = value;
223221
if (canvas === Module['canvas']) {
224222
postMessage({ target: 'canvas', op: 'resize', width: canvas.width_, height: canvas.height_ });
225223
}
226224
},
227-
get: function() {
225+
get: () => {
228226
return canvas.width_;
229227
}
230228
});
231229
Object.defineProperty(canvas, 'height', {
232-
set: function(value) {
230+
set: (value) => {
233231
canvas.height_ = value;
234232
if (canvas === Module['canvas']) {
235233
postMessage({ target: 'canvas', op: 'resize', width: canvas.width_, height: canvas.height_ });
236234
}
237235
},
238-
get: function() {
236+
get: () => {
239237
return canvas.height_;
240238
}
241239
});
242240

243241
var style = {
244242
parentCanvas: canvas,
245-
removeProperty: function(){},
246-
setProperty: function(){},
243+
removeProperty: () => {},
244+
setProperty: () => {},
247245
};
248246

249247
Object.defineProperty(style, 'cursor', {
250-
set: function(value) {
248+
set: (value) => {
251249
if (!style.cursor_ || style.cursor_ !== value) {
252250
style.cursor_ = value;
253251
if (style.parentCanvas === Module['canvas']) {
254252
postMessage({ target: 'canvas', op: 'setObjectProperty', object: 'style', property: 'cursor', value: style.cursor_ });
255253
}
256254
}
257255
},
258-
get: function() {
259-
return style.cursor_;
260-
}
256+
get: () => style.cursor,
261257
});
262258

263259
canvas.style = style;
264-
265260
return canvas;
266261
}
267-
default: throw 'document.createElement ' + what;
262+
default: {
263+
throw 'document.createElement ' + what;
264+
}
268265
}
269266
};
270267

271-
document.getElementById = function(id) {
268+
document.getElementById = (id) => {
272269
if (id === 'canvas' || id === 'application-canvas') {
273270
return Module.canvas;
274271
}
275272
throw 'document.getElementById failed on ' + id;
276273
};
277274

278-
document.querySelector = function(id) {
275+
document.querySelector = (id) => {
279276
if (id === '#canvas' || id === '#application-canvas' || id === 'canvas' || id === 'application-canvas') {
280277
return Module.canvas;
281278
}
@@ -304,26 +301,24 @@ Object.defineProperty(Audio.prototype, 'src', {
304301
},
305302
});
306303

307-
Audio.prototype.play = function(){};
308-
Audio.prototype.pause = function(){};
304+
Audio.prototype.play = () => {};
305+
Audio.prototype.pause = () => {};
309306

310-
Audio.prototype.cloneNode = function() {
311-
return new Audio;
312-
}
307+
Audio.prototype.cloneNode = () => new Audio;
313308

314309
function AudioContext() {
315310
warnOnce('faking WebAudio elements, no actual sound will play');
316-
function makeNode() {
311+
var makeNode = () => {
317312
return {
318-
connect: function(){},
319-
disconnect: function(){},
313+
connect: () => {},
314+
disconnect: () => {},
320315
}
321-
}
316+
};
322317
this.listener = {
323-
setPosition: function() {},
324-
setOrientation: function() {},
318+
setPosition: () => {},
319+
setOrientation: () => {},
325320
};
326-
this.decodeAudioData = function() {}; // ignore callbacks
321+
this.decodeAudioData = () => {}; // ignore callbacks
327322
this.createBuffer = makeNode;
328323
this.createBufferSource = makeNode;
329324
this.createGain = makeNode;
@@ -339,11 +334,11 @@ Module.canvas = document.createElement('canvas');
339334

340335
Module.setStatus = () => {};
341336

342-
out = function(x) {
337+
out = (x) => {
343338
//dump('OUT: ' + x + '\n');
344339
postMessage({ target: 'stdout', content: x });
345340
};
346-
err = function(x) {
341+
err = (x) => {
347342
//dump('ERR: ' + x + '\n');
348343
postMessage({ target: 'stderr', content: x });
349344
};
@@ -354,7 +349,7 @@ var frameId = 0;
354349
var clientFrameId = 0;
355350

356351
var postMainLoop = Module['postMainLoop'];
357-
Module['postMainLoop'] = function() {
352+
Module['postMainLoop'] = () => {
358353
if (postMainLoop) postMainLoop();
359354
// frame complete, send a frame id
360355
postMessage({ target: 'tick', id: frameId++ });

0 commit comments

Comments
 (0)