var window = {
location: {
toString: function() {
return '%s';
},
search: '?%s',
},
fakeNow: 0, // we don't use Date.now()
rafs: [],
timeouts: [],
uid: 0,
requestAnimationFrame: function(func) {
func.uid = window.uid++;
print('adding raf ' + func.uid);
window.rafs.push(func);
},
setTimeout: function(func, ms) {
func.uid = window.uid++;
print('adding timeout ' + func.uid);
window.timeouts.push({
func: func,
when: window.fakeNow + (ms || 0)
});
window.timeouts.sort(function(x, y) { return y.when - x.when });
},
onIdle: %s,
runEventLoop: function() {
// run forever until an exception stops this replay
var iter = 0;
while (1) {
var start = Recorder.dnow();
print('event loop: ' + (iter++));
if (window.rafs.length == 0 && window.timeouts.length == 0) {
if (window.onIdle) {
window.onIdle();
} else {
throw 'main loop is idle!';
}
}
// rafs
var currRafs = window.rafs;
window.rafs = [];
for (var i = 0; i < currRafs.length; i++) {
var raf = currRafs[i];
print('calling raf: ' + raf.uid);// + ': ' + raf.toString().substring(0, 50));
raf();
}
// timeouts
var now = window.fakeNow;
var timeouts = window.timeouts;
window.timeouts = [];
while (timeouts.length && timeouts[timeouts.length-1].when <= now) {
var timeout = timeouts.pop();
print('calling timeout: ' + timeout.func.uid);// + ': ' + timeout.func.toString().substring(0, 50));
timeout.func();
}
// increment 'time'
window.fakeNow += 16.666;
print('main event loop iteration took ' + (Recorder.dnow() - start) + ' ms');
}
},
URL: {
createObjectURL: function(x) {
return x; // the blob itself is returned
},
revokeObjectURL: function(x) {},
},
};
var setTimeout = window.setTimeout;
var document = {
eventListeners: {},
addEventListener: function(id, func) {
var listeners = this.eventListeners[id];
if (!listeners) {
listeners = this.eventListeners[id] = [];
}
listeners.push(func);
},
callEventListeners: function(id) {
var listeners = this.eventListeners[id];
if (listeners) {
listeners.forEach(function(listener) { listener() });
}
},
getElementById: function(id) {
switch(id) {
case 'canvas': {
if (this.canvas) return this.canvas;
return this.canvas = {
getContext: function(which) {
switch(which) {
case 'experimental-webgl': {
return {
/* ClearBufferMask */
DEPTH_BUFFER_BIT : 0x00000100,
STENCIL_BUFFER_BIT : 0x00000400,
COLOR_BUFFER_BIT : 0x00004000,
/* BeginMode */
POINTS : 0x0000,
LINES : 0x0001,
LINE_LOOP : 0x0002,
LINE_STRIP : 0x0003,
TRIANGLES : 0x0004,
TRIANGLE_STRIP : 0x0005,
TRIANGLE_FAN : 0x0006,
/* AlphaFunction (not supported in ES20) */
/* NEVER */
/* LESS */
/* EQUAL */
/* LEQUAL */
/* GREATER */
/* NOTEQUAL */
/* GEQUAL */
/* ALWAYS */
/* BlendingFactorDest */
ZERO : 0,
ONE : 1,
SRC_COLOR : 0x0300,
ONE_MINUS_SRC_COLOR : 0x0301,
SRC_ALPHA : 0x0302,
ONE_MINUS_SRC_ALPHA : 0x0303,
DST_ALPHA : 0x0304,
ONE_MINUS_DST_ALPHA : 0x0305,
/* BlendingFactorSrc */
/* ZERO */
/* ONE */
DST_COLOR : 0x0306,
ONE_MINUS_DST_COLOR : 0x0307,
SRC_ALPHA_SATURATE : 0x0308,
/* SRC_ALPHA */
/* ONE_MINUS_SRC_ALPHA */
/* DST_ALPHA */
/* ONE_MINUS_DST_ALPHA */
/* BlendEquationSeparate */
FUNC_ADD : 0x8006,
BLEND_EQUATION