5
5
*/
6
6
7
7
/*
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
18
18
19
19
#if ENVIRONMENT_MAY_BE_NODE
20
20
if ( ! ENVIRONMENT_IS_NODE ) {
@@ -24,7 +24,7 @@ function FPSTracker(text) {
24
24
var last = 0 ;
25
25
var mean = 0 ;
26
26
var counter = 0 ;
27
- this . tick = function ( ) {
27
+ this . tick = ( ) => {
28
28
var now = Date . now ( ) ;
29
29
if ( last > 0 ) {
30
30
var diff = now - last ;
@@ -47,9 +47,9 @@ var KeyboardEvent = {
47
47
} ;
48
48
49
49
function PropertyBag ( ) {
50
- this . addProperty = function ( ) { } ;
51
- this . removeProperty = function ( ) { } ;
52
- this . setProperty = function ( ) { } ;
50
+ this . addProperty = ( ) => { } ;
51
+ this . removeProperty = ( ) => { } ;
52
+ this . setProperty = ( ) => { } ;
53
53
} ;
54
54
55
55
var IndexedObjects = {
@@ -77,44 +77,42 @@ function EventListener() {
77
77
list . splice ( me , 1 ) ;
78
78
} ;
79
79
80
- this . fireEvent = function fireEvent ( event ) {
81
- event . preventDefault = function ( ) { } ;
80
+ this . fireEvent = function ( event ) {
81
+ event . preventDefault = ( ) => { } ;
82
82
83
83
if ( event . type in this . listeners ) {
84
84
this . listeners [ event . type ] . forEach ( ( listener ) => listener ( event ) ) ;
85
85
}
86
- } ;
86
+ }
87
87
}
88
88
89
89
function Image ( ) {
90
90
IndexedObjects . add ( this ) ;
91
91
EventListener . call ( this ) ;
92
92
var src = '' ;
93
93
Object . defineProperty ( this , 'src' , {
94
- set : function ( value ) {
94
+ set : ( value ) => {
95
95
src = value ;
96
96
assert ( this . id ) ;
97
97
postMessage ( { target : 'Image' , method : 'src' , src, id : this . id } ) ;
98
98
} ,
99
- get : function ( ) {
100
- return src ;
101
- }
99
+ get : ( ) => src
102
100
} ) ;
103
101
}
104
- Image . prototype . onload = function ( ) { } ;
105
- Image . prototype . onerror = function ( ) { } ;
102
+ Image . prototype . onload = ( ) => { } ;
103
+ Image . prototype . onerror = ( ) => { } ;
106
104
107
105
var HTMLImageElement = Image ;
108
106
109
107
var window = this ;
110
108
var windowExtra = new EventListener ( ) ;
111
109
for ( var x in windowExtra ) window [ x ] = windowExtra [ x ] ;
112
110
113
- window . close = function window_close ( ) {
111
+ window . close = ( ) => {
114
112
postMessage ( { target : 'window' , method : 'close' } ) ;
115
113
} ;
116
114
117
- window . alert = function ( text ) {
115
+ window . alert = ( text ) => {
118
116
err ( 'alert forever: ' + text ) ;
119
117
while ( 1 ) { } ;
120
118
} ;
@@ -145,11 +143,11 @@ var webGLWorker = new WebGLWorker();
145
143
146
144
var document = new EventListener ( ) ;
147
145
148
- document . createElement = function document_createElement ( what ) {
146
+ document . createElement = ( what ) => {
149
147
switch ( what ) {
150
148
case 'canvas' : {
151
149
var canvas = new EventListener ( ) ;
152
- canvas . ensureData = function canvas_ensureData ( ) {
150
+ canvas . ensureData = ( ) => {
153
151
if ( ! canvas . data || canvas . data . width !== canvas . width || canvas . data . height !== canvas . height ) {
154
152
canvas . data = {
155
153
width : canvas . width ,
@@ -161,13 +159,13 @@ document.createElement = function document_createElement(what) {
161
159
}
162
160
}
163
161
} ;
164
- canvas . getContext = function canvas_getContext ( type , attributes ) {
162
+ canvas . getContext = ( type , attributes ) => {
165
163
if ( canvas === Module [ 'canvas' ] ) {
166
164
postMessage ( { target : 'canvas' , op : 'getContext' , type, attributes } ) ;
167
165
}
168
166
if ( type === '2d' ) {
169
167
return {
170
- getImageData : function ( x , y , w , h ) {
168
+ getImageData : ( x , y , w , h ) => {
171
169
assert ( x == 0 && y == 0 && w == canvas . width && h == canvas . height ) ;
172
170
canvas . ensureData ( ) ;
173
171
return {
@@ -176,15 +174,15 @@ document.createElement = function document_createElement(what) {
176
174
data : new Uint8Array ( canvas . data . data ) // TODO: can we avoid this copy?
177
175
} ;
178
176
} ,
179
- putImageData : function ( image , x , y ) {
177
+ putImageData : ( image , x , y ) => {
180
178
canvas . ensureData ( ) ;
181
179
assert ( x == 0 && y == 0 && image . width == canvas . width && image . height == canvas . height ) ;
182
180
canvas . data . data . set ( image . data ) ; // TODO: can we avoid this copy?
183
181
if ( canvas === Module [ 'canvas' ] ) {
184
182
postMessage ( { target : 'canvas' , op : 'render' , image : canvas . data } ) ;
185
183
}
186
184
} ,
187
- drawImage : function ( image , x , y , w , h , ox , oy , ow , oh ) {
185
+ drawImage : ( image , x , y , w , h , ox , oy , ow , oh ) => {
188
186
assert ( ! x && ! y && ! ox && ! oy ) ;
189
187
assert ( w === ow && h === oh ) ;
190
188
assert ( canvas . width === w || w === undefined ) ;
@@ -202,7 +200,7 @@ document.createElement = function document_createElement(what) {
202
200
}
203
201
} ;
204
202
canvas . boundingClientRect = { } ;
205
- canvas . getBoundingClientRect = function canvas_getBoundingClientRect ( ) {
203
+ canvas . getBoundingClientRect = ( ) => {
206
204
return {
207
205
width : canvas . boundingClientRect . width ,
208
206
height : canvas . boundingClientRect . height ,
@@ -213,69 +211,68 @@ document.createElement = function document_createElement(what) {
213
211
} ;
214
212
} ;
215
213
canvas . style = new PropertyBag ( ) ;
216
- canvas . exitPointerLock = function ( ) { } ;
214
+ canvas . exitPointerLock = ( ) => { } ;
217
215
218
216
canvas . width_ = canvas . width_ || 0 ;
219
217
canvas . height_ = canvas . height_ || 0 ;
220
218
Object . defineProperty ( canvas , 'width' , {
221
- set : function ( value ) {
219
+ set : ( value ) => {
222
220
canvas . width_ = value ;
223
221
if ( canvas === Module [ 'canvas' ] ) {
224
222
postMessage ( { target : 'canvas' , op : 'resize' , width : canvas . width_ , height : canvas . height_ } ) ;
225
223
}
226
224
} ,
227
- get : function ( ) {
225
+ get : ( ) => {
228
226
return canvas . width_ ;
229
227
}
230
228
} ) ;
231
229
Object . defineProperty ( canvas , 'height' , {
232
- set : function ( value ) {
230
+ set : ( value ) => {
233
231
canvas . height_ = value ;
234
232
if ( canvas === Module [ 'canvas' ] ) {
235
233
postMessage ( { target : 'canvas' , op : 'resize' , width : canvas . width_ , height : canvas . height_ } ) ;
236
234
}
237
235
} ,
238
- get : function ( ) {
236
+ get : ( ) => {
239
237
return canvas . height_ ;
240
238
}
241
239
} ) ;
242
240
243
241
var style = {
244
242
parentCanvas : canvas ,
245
- removeProperty : function ( ) { } ,
246
- setProperty : function ( ) { } ,
243
+ removeProperty : ( ) => { } ,
244
+ setProperty : ( ) => { } ,
247
245
} ;
248
246
249
247
Object . defineProperty ( style , 'cursor' , {
250
- set : function ( value ) {
248
+ set : ( value ) => {
251
249
if ( ! style . cursor_ || style . cursor_ !== value ) {
252
250
style . cursor_ = value ;
253
251
if ( style . parentCanvas === Module [ 'canvas' ] ) {
254
252
postMessage ( { target : 'canvas' , op : 'setObjectProperty' , object : 'style' , property : 'cursor' , value : style . cursor_ } ) ;
255
253
}
256
254
}
257
255
} ,
258
- get : function ( ) {
259
- return style . cursor_ ;
260
- }
256
+ get : ( ) => style . cursor ,
261
257
} ) ;
262
258
263
259
canvas . style = style ;
264
-
265
260
return canvas ;
266
261
}
267
- default : throw 'document.createElement ' + what ;
262
+ default : {
263
+ throw 'document.createElement ' + what ;
264
+ }
268
265
}
269
266
} ;
270
267
271
- document . getElementById = function ( id ) {
268
+ document . getElementById = ( id ) => {
272
269
if ( id === 'canvas' || id === 'application-canvas' ) {
273
270
return Module . canvas ;
274
271
}
275
272
throw 'document.getElementById failed on ' + id ;
276
273
} ;
277
274
278
- document . querySelector = function ( id ) {
275
+ document . querySelector = ( id ) => {
279
276
if ( id === '#canvas' || id === '#application-canvas' || id === 'canvas' || id === 'application-canvas' ) {
280
277
return Module . canvas ;
281
278
}
@@ -304,26 +301,24 @@ Object.defineProperty(Audio.prototype, 'src', {
304
301
} ,
305
302
} ) ;
306
303
307
- Audio . prototype . play = function ( ) { } ;
308
- Audio . prototype . pause = function ( ) { } ;
304
+ Audio . prototype . play = ( ) => { } ;
305
+ Audio . prototype . pause = ( ) => { } ;
309
306
310
- Audio . prototype . cloneNode = function ( ) {
311
- return new Audio ;
312
- }
307
+ Audio . prototype . cloneNode = ( ) => new Audio ;
313
308
314
309
function AudioContext ( ) {
315
310
warnOnce ( 'faking WebAudio elements, no actual sound will play' ) ;
316
- function makeNode ( ) {
311
+ var makeNode = ( ) => {
317
312
return {
318
- connect : function ( ) { } ,
319
- disconnect : function ( ) { } ,
313
+ connect : ( ) => { } ,
314
+ disconnect : ( ) => { } ,
320
315
}
321
- }
316
+ } ;
322
317
this . listener = {
323
- setPosition : function ( ) { } ,
324
- setOrientation : function ( ) { } ,
318
+ setPosition : ( ) => { } ,
319
+ setOrientation : ( ) => { } ,
325
320
} ;
326
- this . decodeAudioData = function ( ) { } ; // ignore callbacks
321
+ this . decodeAudioData = ( ) => { } ; // ignore callbacks
327
322
this . createBuffer = makeNode ;
328
323
this . createBufferSource = makeNode ;
329
324
this . createGain = makeNode ;
@@ -339,11 +334,11 @@ Module.canvas = document.createElement('canvas');
339
334
340
335
Module . setStatus = ( ) => { } ;
341
336
342
- out = function ( x ) {
337
+ out = ( x ) => {
343
338
//dump('OUT: ' + x + '\n');
344
339
postMessage ( { target : 'stdout' , content : x } ) ;
345
340
} ;
346
- err = function ( x ) {
341
+ err = ( x ) => {
347
342
//dump('ERR: ' + x + '\n');
348
343
postMessage ( { target : 'stderr' , content : x } ) ;
349
344
} ;
@@ -354,7 +349,7 @@ var frameId = 0;
354
349
var clientFrameId = 0 ;
355
350
356
351
var postMainLoop = Module [ 'postMainLoop' ] ;
357
- Module [ 'postMainLoop' ] = function ( ) {
352
+ Module [ 'postMainLoop' ] = ( ) => {
358
353
if ( postMainLoop ) postMainLoop ( ) ;
359
354
// frame complete, send a frame id
360
355
postMessage ( { target : 'tick' , id : frameId ++ } ) ;
0 commit comments