1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
|
//"use strict";
// Utilities for browser environments
mergeInto(LibraryManager.library, {
$Browser__postset: 'Module["requestFullScreen"] = function(doLockPointer, doResizeCanvas) { Browser.requestFullScreen(doLockPointer, doResizeCanvas) };\n' + // exports
'Module["requestAnimationFrame"] = function(func) { Browser.requestAnimationFrame(func) };\n' +
'Module["pauseMainLoop"] = function() { Browser.mainLoop.pause() };\n' +
'Module["resumeMainLoop"] = function() { Browser.mainLoop.resume() };\n',
$Browser: {
mainLoop: {
scheduler: null,
shouldPause: false,
paused: false,
queue: [],
pause: function() {
Browser.mainLoop.shouldPause = true;
},
resume: function() {
if (Browser.mainLoop.paused) {
Browser.mainLoop.paused = false;
Browser.mainLoop.scheduler();
}
Browser.mainLoop.shouldPause = false;
},
updateStatus: function() {
if (Module['setStatus']) {
var message = Module['statusMessage'] || 'Please wait...';
var remaining = Browser.mainLoop.remainingBlockers;
var expected = Browser.mainLoop.expectedBlockers;
if (remaining) {
if (remaining < expected) {
Module['setStatus'](message + ' (' + (expected - remaining) + '/' + expected + ')');
} else {
Module['setStatus'](message);
}
} else {
Module['setStatus']('');
}
}
}
},
pointerLock: false,
moduleContextCreatedCallbacks: [],
workers: [],
ensureObjects: function() {
if (Browser.ensured) return;
Browser.ensured = true;
try {
new Blob();
Browser.hasBlobConstructor = true;
} catch(e) {
Browser.hasBlobConstructor = false;
console.log("warning: no blob constructor, cannot create blobs with mimetypes");
}
Browser.BlobBuilder = typeof MozBlobBuilder != "undefined" ? MozBlobBuilder : (typeof WebKitBlobBuilder != "undefined" ? WebKitBlobBuilder : (!Browser.hasBlobConstructor ? console.log("warning: no BlobBuilder") : null));
Browser.URLObject = typeof window != "undefined" ? (window.URL ? window.URL : window.webkitURL) : console.log("warning: cannot create object URLs");
// Support for plugins that can process preloaded files. You can add more of these to
// your app by creating and appending to Module.preloadPlugins.
//
// Each plugin is asked if it can handle a file based on the file's name. If it can,
// it is given the file's raw data. When it is done, it calls a callback with the file's
// (possibly modified) data. For example, a plugin might decompress a file, or it
// might create some side data structure for use later (like an Image element, etc.).
function getMimetype(name) {
return {
'jpg': 'image/jpeg',
'jpeg': 'image/jpeg',
'png': 'image/png',
'bmp': 'image/bmp',
'ogg': 'audio/ogg',
'wav': 'audio/wav',
'mp3': 'audio/mpeg'
}[name.substr(-3)];
return ret;
}
if (!Module["preloadPlugins"]) Module["preloadPlugins"] = [];
var imagePlugin = {};
imagePlugin['canHandle'] = function(name) {
return !Module.noImageDecoding && /\.(jpg|jpeg|png|bmp)$/.exec(name);
};
imagePlugin['handle'] = function(byteArray, name, onload, onerror) {
var b = null;
if (Browser.hasBlobConstructor) {
try {
b = new Blob([byteArray], { type: getMimetype(name) });
} catch(e) {
Runtime.warnOnce('Blob constructor present but fails: ' + e + '; falling back to blob builder');
}
}
if (!b) {
var bb = new Browser.BlobBuilder();
bb.append((new Uint8Array(byteArray)).buffer); // we need to pass a buffer, and must copy the array to get the right data range
b = bb.getBlob();
}
var url = Browser.URLObject.createObjectURL(b);
#if ASSERTIONS
assert(typeof url == 'string', 'createObjectURL must return a url as a string');
#endif
var img = new Image();
img.onload = function() {
assert(img.complete, 'Image ' + name + ' could not be decoded');
var canvas = document.createElement('canvas');
canvas.width = img.width;
canvas.height = img.height;
var ctx = canvas.getContext('2d');
ctx.drawImage(img, 0, 0);
Module["preloadedImages"][name] = canvas;
Browser.URLObject.revokeObjectURL(url);
if (onload) onload(byteArray);
};
img.onerror = function(event) {
console.log('Image ' + url + ' could not be decoded');
if (onerror) onerror();
};
img.src = url;
};
Module['preloadPlugins'].push(imagePlugin);
var audioPlugin = {};
audioPlugin['canHandle'] = function(name) {
return !Module.noAudioDecoding && name.substr(-4) in { '.ogg': 1, '.wav': 1, '.mp3': 1 };
};
audioPlugin['handle'] = function(byteArray, name, onload, onerror) {
var done = false;
function finish(audio) {
if (done) return;
done = true;
Module["preloadedAudios"][name] = audio;
if (onload) onload(byteArray);
}
function fail() {
if (done) return;
done = true;
Module["preloadedAudios"][name] = new Audio(); // empty shim
if (onerror) onerror();
}
if (Browser.hasBlobConstructor) {
try {
var b = new Blob([byteArray], { type: getMimetype(name) });
} catch(e) {
return fail();
}
var url = Browser.URLObject.createObjectURL(b); // XXX we never revoke this!
#if ASSERTIONS
assert(typeof url == 'string', 'createObjectURL must return a url as a string');
#endif
var audio = new Audio();
audio.addEventListener('canplaythrough', function() { finish(audio) }, false); // use addEventListener due to chromium bug 124926
audio.onerror = function(event) {
if (done) return;
console.log('warning: browser could not fully decode audio ' + name + ', trying slower base64 approach');
function encode64(data) {
var BASE = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/';
var PAD = '=';
var ret = '';
var leftchar = 0;
var leftbits = 0;
for (var i = 0; i < data.length; i++) {
leftchar = (leftchar << 8) | data[i];
leftbits += 8;
while (leftbits >= 6) {
var curr = (leftchar >> (leftbits-6)) & 0x3f;
leftbits -= 6;
ret += BASE[curr];
}
}
if (leftbits == 2) {
ret += BASE[(leftchar&3) << 4];
ret += PAD + PAD;
} else if (leftbits == 4) {
ret += BASE[(leftchar&0xf) << 2];
ret += PAD;
}
return ret;
}
audio.src = 'data:audio/x-' + name.substr(-3) + ';base64,' + encode64(byteArray);
finish(audio); // we don't wait for confirmation this worked - but it's worth trying
};
audio.src = url;
// workaround for chrome bug 124926 - we do not always get oncanplaythrough or onerror
setTimeout(function() {
finish(audio); // try to use it even though it is not necessarily ready to play
}, 10000);
} else {
return fail();
}
};
Module['preloadPlugins'].push(audioPlugin);
},
createContext: function(canvas, useWebGL, setInModule) {
#if !USE_TYPED_ARRAYS
if (useWebGL) {
Module.print('(USE_TYPED_ARRAYS needs to be enabled for WebGL)');
return null;
}
#endif
var ctx;
try {
if (useWebGL) {
ctx = canvas.getContext('experimental-webgl', {
alpha: false,
#if GL_TESTING
preserveDrawingBuffer: true
#endif
});
} else {
ctx = canvas.getContext('2d');
}
if (!ctx) throw ':(';
} catch (e) {
Module.print('Could not create canvas - ' + e);
return null;
}
if (useWebGL) {
#if GL_DEBUG
// Useful to debug native webgl apps: var Module = { printErr: function(x) { console.log(x) } };
var tempCtx = ctx;
var wrapper = {};
for (var prop in tempCtx) {
(function(prop) {
switch (typeof tempCtx[prop]) {
case 'function': {
wrapper[prop] = function() {
if (GL.debug) {
var printArgs = Array.prototype.slice.call(arguments).map(Runtime.prettyPrint);
Module.printErr('[gl_f:' + prop + ':' + printArgs + ']');
}
var ret = tempCtx[prop].apply(tempCtx, arguments);
if (GL.debug && typeof ret != 'undefined') {
Module.printErr('[ gl:' + prop + ':return:' + Runtime.prettyPrint(ret) + ']');
}
return ret;
}
break;
}
case 'number': case 'string': {
wrapper.__defineGetter__(prop, function() {
//Module.printErr('[gl_g:' + prop + ':' + tempCtx[prop] + ']');
return tempCtx[prop];
});
wrapper.__defineSetter__(prop, function(value) {
if (GL.debug) {
Module.printErr('[gl_s:' + prop + ':' + value + ']');
}
tempCtx[prop] = value;
});
break;
}
}
})(prop);
}
ctx = wrapper;
#endif
// Set the background of the WebGL canvas to black
canvas.style.backgroundColor = "black";
// Warn on context loss
canvas.addEventListener('webglcontextlost', function(event) {
alert('WebGL context lost. You will need to reload the page.');
}, false);
}
if (setInModule) {
Module.ctx = ctx;
Module.useWebGL = useWebGL;
Browser.moduleContextCreatedCallbacks.forEach(function(callback) { callback() });
}
return ctx;
},
destroyContext: function(canvas, useWebGL, setInModule) {},
storedFullScreenHandler: null,
storedPointerLockHandler: null,
requestFullScreen: function(doLockPointer, doResizeCanvas) {
if(typeof(doLockPointer)==='undefined') doLockPointer = true;
if(typeof(doResizeCanvas)==='undefined') doResizeCanvas = false;
var canvas = Module['canvas'];
function fullScreenChange() {
var isFullScreen = false;
if ((document['webkitFullScreenElement'] || document['webkitFullscreenElement'] ||
document['mozFullScreenElement'] || document['mozFullscreenElement'] ||
document['fullScreenElement'] || document['fullscreenElement']) === canvas) {
canvas.requestPointerLock = canvas['requestPointerLock'] ||
canvas['mozRequestPointerLock'] ||
canvas['webkitRequestPointerLock'];
canvas.exitPointerLock = document['exitPointerLock'] ||
document['mozExitPointerLock'] ||
document['webkitExitPointerLock'];
canvas.exitPointerLock = canvas.exitPointerLock.bind( document );
canvas.cancelFullScreen = document['cancelFullScreen'] ||
document['mozCancelFullScreen'] ||
document['webkitCancelFullScreen'];
canvas.cancelFullScreen = canvas.cancelFullScreen.bind( document );
if (doLockPointer) canvas.requestPointerLock();
isFullScreen = true;
if(doResizeCanvas) Browser.setFullScreenCanvasSize();
} else if(doResizeCanvas){
Browser.setWindowedCanvasSize();
}
if (Module['onFullScreen']) Module['onFullScreen'](isFullScreen);
}
function pointerLockChange() {
Browser.pointerLock = document['pointerLockElement'] === canvas ||
document['mozPointerLockElement'] === canvas ||
document['webkitPointerLockElement'] === canvas;
}
if( !(this.storedFullScreenHandler == null) ){
document.removeEventListener('fullscreenchange', this.storedFullScreenHandler, false);
document.removeEventListener('mozfullscreenchange', this.storedFullScreenHandler, false);
document.removeEventListener('webkitfullscreenchange', this.storedFullScreenHandler, false);
this.storedFullScreenHandler = null;
}
if( !(this.storedPointerLockHandler == null) ){
document.addEventListener('pointerlockchange', this.storedPointerLockHandler, false);
document.addEventListener('mozpointerlockchange', this.storedPointerLockHandler, false);
document.addEventListener('webkitpointerlockchange', this.storedPointerLockHandler, false);
this.storedPointerLockHandler = null;
}
this.storedFullScreenHandler = fullScreenChange;
document.addEventListener('fullscreenchange', this.storedFullScreenHandler, false);
document.addEventListener('mozfullscreenchange', this.storedFullScreenHandler, false);
document.addEventListener('webkitfullscreenchange', this.storedFullScreenHandler, false);
this.storedPointerLockHandler = pointerLockChange;
document.addEventListener('pointerlockchange', this.storedPointerLockHandler, false);
document.addEventListener('mozpointerlockchange', this.storedPointerLockHandler, false);
document.addEventListener('webkitpointerlockchange', this.storedPointerLockHandler, false);
canvas.requestFullScreen = canvas['requestFullScreen'] ||
canvas['mozRequestFullScreen'] ||
(canvas['webkitRequestFullScreen'] ? function() { canvas['webkitRequestFullScreen'](Element['ALLOW_KEYBOARD_INPUT']) } : null);
canvas.requestFullScreen();
},
requestAnimationFrame: function(func) {
if (!window.requestAnimationFrame) {
window.requestAnimationFrame = window['requestAnimationFrame'] ||
window['mozRequestAnimationFrame'] ||
window['webkitRequestAnimationFrame'] ||
window['msRequestAnimationFrame'] ||
window['oRequestAnimationFrame'] ||
window['setTimeout'];
}
window.requestAnimationFrame(func);
},
getMovementX: function(event) {
return event['movementX'] ||
event['mozMovementX'] ||
event['webkitMovementX'] ||
0;
},
getMovementY: function(event) {
return event['movementY'] ||
event['mozMovementY'] ||
event['webkitMovementY'] ||
0;
},
xhrLoad: function(url, onload, onerror) {
var xhr = new XMLHttpRequest();
xhr.open('GET', url, true);
xhr.responseType = 'arraybuffer';
xhr.onload = function() {
if (xhr.status == 200) {
onload(xhr.response);
} else {
onerror();
}
};
xhr.onerror = onerror;
xhr.send(null);
},
asyncLoad: function(url, onload, onerror, noRunDep) {
Browser.xhrLoad(url, function(arrayBuffer) {
assert(arrayBuffer, 'Loading data file "' + url + '" failed (no arrayBuffer).');
onload(new Uint8Array(arrayBuffer));
if (!noRunDep) removeRunDependency('al ' + url);
}, function(event) {
if (onerror) {
onerror();
} else {
throw 'Loading data file "' + url + '" failed.';
}
});
if (!noRunDep) addRunDependency('al ' + url);
},
resizeListeners: [],
updateResizeListeners: function() {
var canvas = Module['canvas'];
Browser.resizeListeners.forEach(function(listener) {
listener(canvas.width, canvas.height);
});
},
setCanvasSize: function(width, height, noUpdates) {
var canvas = Module['canvas'];
canvas.width = width;
canvas.height = height;
if (!noUpdates) Browser.updateResizeListeners();
},
windowedWidth: 0,
windowedHeight: 0,
setFullScreenCanvasSize: function() {
var canvas = Module['canvas'];
this.windowedWidth = canvas.width;
this.windowedHeight = canvas.height;
canvas.width = screen.width;
canvas.height = screen.height;
var flags = {{{ makeGetValue('SDL.screen+Runtime.QUANTUM_SIZE*0', '0', 'i32', 0, 1) }}};
flags = flags | 0x00800000;
{{{ makeSetValue('SDL.screen+Runtime.QUANTUM_SIZE*0', '0', 'flags', 'i32') }}}
Browser.updateResizeListeners();
},
setWindowedCanvasSize: function() {
var canvas = Module['canvas'];
canvas.width = this.windowedWidth;
canvas.height = this.windowedHeight;
var flags = {{{ makeGetValue('SDL.screen+Runtime.QUANTUM_SIZE*0', '0', 'i32', 0, 1) }}};
flags = flags & ~0x00800000;
{{{ makeSetValue('SDL.screen+Runtime.QUANTUM_SIZE*0', '0', 'flags', 'i32') }}}
Browser.updateResizeListeners();
}
},
emscripten_async_wget: function(url, file, onload, onerror) {
var _url = Pointer_stringify(url);
var _file = Pointer_stringify(file);
var index = _file.lastIndexOf('/');
FS.createPreloadedFile(
_file.substr(0, index),
_file.substr(index +1),
_url, true, true,
function() {
if (onload) Runtime.dynCall('vi', onload, [file]);
},
function() {
if (onerror) Runtime.dynCall('vi', onerror, [file]);
}
);
},
emscripten_async_wget_data: function(url, arg, onload, onerror) {
Browser.asyncLoad(Pointer_stringify(url), function(byteArray) {
var buffer = _malloc(byteArray.length);
HEAPU8.set(byteArray, buffer);
Runtime.dynCall('viii', onload, [arg, buffer, byteArray.length]);
_free(buffer);
}, function() {
if (onerror) Runtime.dynCall('vi', onerror, [arg]);
}, true /* no need for run dependency, this is async but will not do any prepare etc. step */ );
},
emscripten_async_wget2: function(url, file, request, param, arg, onload, onerror, onprogress) {
var _url = Pointer_stringify(url);
var _file = Pointer_stringify(file);
var _request = Pointer_stringify(request);
var _param = Pointer_stringify(param);
var index = _file.lastIndexOf('/');
var http = new XMLHttpRequest();
http.open(_request, _url, true);
http.responseType = 'arraybuffer';
// LOAD
http.onload = function(e) {
if (http.status == 200) {
FS.createDataFile( _file.substr(0, index), _file.substr(index + 1), new Uint8Array(http.response), true, true);
if (onload) Runtime.dynCall('vii', onload, [arg, file]);
} else {
if (onerror) Runtime.dynCall('vii', onerror, [arg, http.status]);
}
};
// ERROR
http.onerror = function(e) {
if (onerror) Runtime.dynCall('vii', onerror, [arg, http.status]);
};
// PROGRESS
http.onprogress = function(e) {
var percentComplete = (e.position / e.totalSize)*100;
if (onprogress) Runtime.dynCall('vii', onprogress, [arg, percentComplete]);
};
// Useful because the browser can limit the number of redirection
try {
if (http.channel instanceof Ci.nsIHttpChannel)
http.channel.redirectionLimit = 0;
} catch (ex) { /* whatever */ }
if (_request == "POST") {
//Send the proper header information along with the request
http.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
http.setRequestHeader("Content-length", _param.length);
http.setRequestHeader("Connection", "close");
http.send(_param);
} else {
http.send(null);
}
},
emscripten_async_prepare: function(file, onload, onerror) {
var _file = Pointer_stringify(file);
var data = FS.analyzePath(_file);
if (!data.exists) return -1;
var index = _file.lastIndexOf('/');
FS.createPreloadedFile(
_file.substr(0, index),
_file.substr(index +1),
new Uint8Array(data.object.contents), true, true,
function() {
if (onload) Runtime.dynCall('vi', onload, [file]);
},
function() {
if (onerror) Runtime.dynCall('vi', onerror, [file]);
},
true // don'tCreateFile - it's already there
);
return 0;
},
emscripten_async_prepare_data: function(data, size, suffix, arg, onload, onerror) {
var _suffix = Pointer_stringify(suffix);
if (!Browser.asyncPrepareDataCounter) Browser.asyncPrepareDataCounter = 0;
var name = 'prepare_data_' + (Browser.asyncPrepareDataCounter++) + '.' + _suffix;
var cname = _malloc(name.length+1);
writeStringToMemory(name, cname);
FS.createPreloadedFile(
'',
name,
{{{ makeHEAPView('U8', 'data', 'data + size') }}},
true, true,
function() {
if (onload) Runtime.dynCall('vii', onload, [arg, cname]);
},
function() {
if (onerror) Runtime.dynCall('vi', onerror, [arg]);
},
true // don'tCreateFile - it's already there
);
return 0;
},
emscripten_async_run_script__deps: ['emscripten_run_script'],
emscripten_async_run_script: function(script, millis) {
Module['noExitRuntime'] = true;
// TODO: cache these to avoid generating garbage
setTimeout(function() {
_emscripten_run_script(script);
}, millis);
},
emscripten_set_main_loop: function(func, fps, simulateInfiniteLoop) {
Module['noExitRuntime'] = true;
Browser.mainLoop.runner = function() {
if (Browser.mainLoop.queue.length > 0) {
var start = Date.now();
var blocker = Browser.mainLoop.queue.shift();
blocker.func(blocker.arg);
if (Browser.mainLoop.remainingBlockers) {
var remaining = Browser.mainLoop.remainingBlockers;
var next = remaining%1 == 0 ? remaining-1 : Math.floor(remaining);
if (blocker.counted) {
Browser.mainLoop.remainingBlockers = next;
} else {
// not counted, but move the progress along a tiny bit
next = next + 0.5; // do not steal all the next one's progress
Browser.mainLoop.remainingBlockers = (8*remaining + next)/9;
}
}
console.log('main loop blocker "' + blocker.name + '" took ' + (Date.now() - start) + ' ms'); //, left: ' + Browser.mainLoop.remainingBlockers);
Browser.mainLoop.updateStatus();
setTimeout(Browser.mainLoop.runner, 0);
return;
}
if (Browser.mainLoop.shouldPause) {
// catch pauses from non-main loop sources
Browser.mainLoop.paused = true;
Browser.mainLoop.shouldPause = false;
return;
}
if (Module['preMainLoop']) {
Module['preMainLoop']();
}
Runtime.dynCall('v', func);
if (Module['postMainLoop']) {
Module['postMainLoop']();
}
if (Browser.mainLoop.shouldPause) {
// catch pauses from the main loop itself
Browser.mainLoop.paused = true;
Browser.mainLoop.shouldPause = false;
return;
}
Browser.mainLoop.scheduler();
}
if (fps && fps > 0) {
Browser.mainLoop.scheduler = function() {
setTimeout(Browser.mainLoop.runner, 1000/fps); // doing this each time means that on exception, we stop
}
} else {
Browser.mainLoop.scheduler = function() {
Browser.requestAnimationFrame(Browser.mainLoop.runner);
}
}
Browser.mainLoop.scheduler();
if (simulateInfiniteLoop) {
throw 'emscripten_set_main_loop simulating infinite loop by throwing so we get right into the JS event loop';
}
},
emscripten_cancel_main_loop: function() {
Browser.mainLoop.scheduler = null;
Browser.mainLoop.shouldPause = true;
},
emscripten_pause_main_loop: function() {
Browser.mainLoop.pause();
},
emscripten_resume_main_loop: function() {
Browser.mainLoop.resume();
},
_emscripten_push_main_loop_blocker: function(func, arg, name) {
Browser.mainLoop.queue.push({ func: function() {
Runtime.dynCall('vi', func, [arg]);
}, name: Pointer_stringify(name), counted: true });
Browser.mainLoop.updateStatus();
},
_emscripten_push_uncounted_main_loop_blocker: function(func, arg, name) {
Browser.mainLoop.queue.push({ func: function() {
Runtime.dynCall('vi', func, [arg]);
}, name: Pointer_stringify(name), counted: false });
Browser.mainLoop.updateStatus();
},
emscripten_set_main_loop_expected_blockers: function(num) {
Browser.mainLoop.expectedBlockers = num;
Browser.mainLoop.remainingBlockers = num;
Browser.mainLoop.updateStatus();
},
emscripten_async_call: function(func, arg, millis) {
Module['noExitRuntime'] = true;
function wrapper() {
Runtime.getFuncWrapper(func, 'vi')(arg);
}
if (millis >= 0) {
setTimeout(wrapper, millis);
} else {
Browser.requestAnimationFrame(wrapper);
}
},
emscripten_hide_mouse: function() {
var styleSheet = document.styleSheets[0];
var rules = styleSheet.cssRules;
for (var i = 0; i < rules.length; i++) {
if (rules[i].cssText.substr(0, 5) == 'canvas') {
styleSheet.deleteRule(i);
i--;
}
}
styleSheet.insertRule('canvas.emscripten { border: 1px solid black; cursor: none; }', 0);
},
emscripten_set_canvas_size: function(width, height) {
Browser.setCanvasSize(width, height);
},
emscripten_get_now: function() {
if (window['performance'] && window['performance']['now']) {
return window['performance']['now']();
} else {
return Date.now();
}
},
emscripten_create_worker: function(url) {
url = Pointer_stringify(url);
var id = Browser.workers.length;
var info = {
worker: new Worker(url),
callbacks: [],
awaited: 0,
buffer: 0,
bufferSize: 0
};
info.worker.onmessage = function(msg) {
var info = Browser.workers[id];
if (!info) return; // worker was destroyed meanwhile
var callbackId = msg.data['callbackId'];
var callbackInfo = info.callbacks[callbackId];
if (!callbackInfo) return; // no callback or callback removed meanwhile
info.awaited--;
info.callbacks[callbackId] = null; // TODO: reuse callbackIds, compress this
var data = msg.data['data'];
if (data) {
if (!data.byteLength) data = new Uint8Array(data);
if (!info.buffer || info.bufferSize < data.length) {
if (info.buffer) _free(info.buffer);
info.bufferSize = data.length;
info.buffer = _malloc(data.length);
}
HEAPU8.set(data, info.buffer);
callbackInfo.func(info.buffer, data.length, callbackInfo.arg);
} else {
callbackInfo.func(0, 0, callbackInfo.arg);
}
};
Browser.workers.push(info);
return id;
},
emscripten_destroy_worker: function(id) {
var info = Browser.workers[id];
info.worker.terminate();
if (info.buffer) _free(info.buffer);
Browser.workers[id] = null;
},
emscripten_call_worker: function(id, funcName, data, size, callback, arg) {
funcName = Pointer_stringify(funcName);
var info = Browser.workers[id];
var callbackId = -1;
if (callback) {
callbackId = info.callbacks.length;
info.callbacks.push({
func: Runtime.getFuncWrapper(callback, 'viii'),
arg: arg
});
info.awaited++;
}
info.worker.postMessage({
'funcName': funcName,
'callbackId': callbackId,
'data': data ? new Uint8Array({{{ makeHEAPView('U8', 'data', 'data + size') }}}) : 0 // XXX copy to a new typed array as a workaround for chrome bug 169705
});
},
emscripten_worker_respond: function(data, size) {
if (!inWorkerCall) throw 'not in worker call!';
if (workerResponded) throw 'already responded!';
workerResponded = true;
postMessage({
'callbackId': workerCallbackId,
'data': data ? new Uint8Array({{{ makeHEAPView('U8', 'data', 'data + size') }}}) : 0 // XXX copy to a new typed array as a workaround for chrome bug 169705
});
},
emscripten_get_worker_queue_size: function(id) {
var info = Browser.workers[id];
if (!info) return -1;
return info.awaited;
}
});
/* Useful stuff for browser debugging
function slowLog(label, text) {
if (!slowLog.labels) slowLog.labels = {};
if (!slowLog.labels[label]) slowLog.labels[label] = 0;
var now = Date.now();
if (now - slowLog.labels[label] > 1000) {
Module.print(label + ': ' + text);
slowLog.labels[label] = now;
}
}
*/
|