-
Notifications
You must be signed in to change notification settings - Fork 3.4k
/
Copy pathlibrary_eventloop.js
183 lines (168 loc) · 5.85 KB
/
library_eventloop.js
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
/**
* @license
* Copyright 2010 The Emscripten Authors
* SPDX-License-Identifier: MIT
*/
// Implementation of functions from emscripten/eventloop.h.
LibraryJSEventLoop = {
emscripten_unwind_to_js_event_loop: function() {
throw 'unwind';
},
// Just like setImmediate but returns an i32 that can be passed back
// to wasm rather than a JS object.
$setImmediateWrapped: function(func) {
if (!setImmediateWrapped.mapping) setImmediateWrapped.mapping = [];
var id = setImmediateWrapped.mapping.length;
setImmediateWrapped.mapping[id] = setImmediate(function() {
setImmediateWrapped.mapping[id] = undefined;
func();
});
return id;
},
// Just like clearImmediate but takes an i32 rather than an object.
$clearImmediateWrapped: function(id) {
#if ASSERTIONS
assert(id);
assert(setImmediateWrapped.mapping[id]);
#endif
clearImmediate(setImmediateWrapped.mapping[id]);
setImmediateWrapped.mapping[id] = undefined;
},
$polyfillSetImmediate__deps: ['$setImmediateWrapped', '$clearImmediateWrapped'],
$polyfillSetImmediate__postset:
'var emSetImmediate;\n' +
'var emClearImmediate;\n' +
'if (typeof setImmediate != "undefined") {\n' +
'emSetImmediate = setImmediateWrapped;\n' +
'emClearImmediate = clearImmediateWrapped;\n' +
'} else if (typeof addEventListener == "function") {\n' +
'var __setImmediate_id_counter = 0;\n' +
'var __setImmediate_queue = [];\n' +
'var __setImmediate_message_id = "_si";\n' +
'function __setImmediate_cb(/** @type {Event} */e) {\n' +
'if (e.data === __setImmediate_message_id) {\n' +
'e.stopPropagation();\n' +
'__setImmediate_queue.shift()();\n' +
'++__setImmediate_id_counter;\n' +
'}\n' +
'}\n' +
'addEventListener("message", __setImmediate_cb, true);\n' +
'emSetImmediate = function(func) {\n' +
'postMessage(__setImmediate_message_id, "*");\n' +
'return __setImmediate_id_counter + __setImmediate_queue.push(func) - 1;\n' +
'}\n' +
'emClearImmediate = /**@type{function(number=)}*/(function(id) {\n' +
'var index = id - __setImmediate_id_counter;\n' +
'if (index >= 0 && index < __setImmediate_queue.length) __setImmediate_queue[index] = function() {};\n' + // must preserve the order and count of elements in the queue, so replace the pending callback with an empty function
'})\n' +
'}',
$polyfillSetImmediate: function() {
// nop, used for its postset to ensure setImmediate() polyfill is
// not duplicated between emscripten_set_immediate() and
// emscripten_set_immediate_loop() if application links to both of them.
},
emscripten_set_immediate__deps: ['$polyfillSetImmediate', '$callUserCallback',
#if !MINIMAL_RUNTIME
'$runtimeKeepalivePush', '$runtimeKeepalivePop',
#endif
],
emscripten_set_immediate: function(cb, userData) {
polyfillSetImmediate();
{{{ runtimeKeepalivePush(); }}}
return emSetImmediate(function() {
{{{ runtimeKeepalivePop(); }}}
callUserCallback(function() {
{{{ makeDynCall('vi', 'cb') }}}(userData);
});
});
},
emscripten_clear_immediate__deps: ['$polyfillSetImmediate',
#if !MINIMAL_RUNTIME
'$runtimeKeepalivePop',
#endif
],
emscripten_clear_immediate: function(id) {
{{{ runtimeKeepalivePop(); }}}
emClearImmediate(id);
},
emscripten_set_immediate_loop__deps: ['$polyfillSetImmediate', '$callUserCallback',
#if !MINIMAL_RUNTIME
'$runtimeKeepalivePush', '$runtimeKeepalivePop',
#endif
],
emscripten_set_immediate_loop: function(cb, userData) {
polyfillSetImmediate();
function tick() {
{{{ runtimeKeepalivePop(); }}}
callUserCallback(function() {
if ({{{ makeDynCall('ii', 'cb') }}}(userData)) {
{{{ runtimeKeepalivePush(); }}}
emSetImmediate(tick);
}
});
}
{{{ runtimeKeepalivePush(); }}}
return emSetImmediate(tick);
},
emscripten_set_timeout__deps: ['$callUserCallback',
#if !MINIMAL_RUNTIME
'$runtimeKeepalivePush', '$runtimeKeepalivePop',
#endif
],
emscripten_set_timeout: function(cb, msecs, userData) {
{{{ runtimeKeepalivePush() }}}
return setTimeout(function() {
{{{ runtimeKeepalivePop() }}}
callUserCallback(function() {
{{{ makeDynCall('vi', 'cb') }}}(userData);
});
}, msecs);
},
emscripten_clear_timeout: function(id) {
clearTimeout(id);
},
emscripten_set_timeout_loop__deps: ['$callUserCallback',
#if !MINIMAL_RUNTIME
'$runtimeKeepalivePush', '$runtimeKeepalivePop',
#endif
],
emscripten_set_timeout_loop: function(cb, msecs, userData) {
function tick() {
var t = performance.now();
var n = t + msecs;
{{{ runtimeKeepalivePop() }}}
callUserCallback(function() {
if ({{{ makeDynCall('idi', 'cb') }}}(t, userData)) {
// Save a little bit of code space: modern browsers should treat
// negative setTimeout as timeout of 0
// (https://stackoverflow.com/questions/8430966/is-calling-settimeout-with-a-negative-delay-ok)
{{{ runtimeKeepalivePush() }}}
setTimeout(tick, n - performance.now());
}
});
}
{{{ runtimeKeepalivePush() }}}
return setTimeout(tick, 0);
},
emscripten_set_interval__deps: ['$callUserCallback',
#if !MINIMAL_RUNTIME
'$runtimeKeepalivePush', '$runtimeKeepalivePop',
#endif
],
emscripten_set_interval: function(cb, msecs, userData) {
{{{ runtimeKeepalivePush() }}}
return setInterval(function() {
callUserCallback(function() {
{{{ makeDynCall('vi', 'cb') }}}(userData)
});
}, msecs);
},
#if !MINIMAL_RUNTIME
emscripten_clear_interval__deps: ['$runtimeKeepalivePop'],
#endif
emscripten_clear_interval: function(id) {
{{{ runtimeKeepalivePop() }}}
clearInterval(id);
},
};
mergeInto(LibraryManager.library, LibraryJSEventLoop);