Skip to content

Commit 4158554

Browse files
authored
Fix -sDETERMINISTIC under node >= 16 (#19663)
It turns out you can't override `performance.now` on modern versions of node. Not sure if that is bug, but we have to work around it. This should make the CI green again since we updated emsdk to node 16.
1 parent 281703b commit 4158554

File tree

6 files changed

+29
-11
lines changed

6 files changed

+29
-11
lines changed

.circleci/config.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -617,6 +617,7 @@ jobs:
617617
- run-tests:
618618
title: "selected subset"
619619
test_targets: "
620+
other.test_deterministic
620621
other.test_gen_struct_info
621622
other.test_native_call_before_init
622623
other.test_node_unhandled_rejection

src/deterministic.js

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,18 @@ Math.random = () => {
99
MAGIC = Math.pow(MAGIC + 1.8912, 3) % 1;
1010
return MAGIC;
1111
};
12+
1213
var TIME = 10000;
13-
Date.now = () => TIME++;
14-
if (typeof performance == 'object') performance.now = Date.now;
14+
function deterministicNow() {
15+
return TIME++;
16+
}
17+
18+
Date.now = deterministicNow;
19+
20+
// Setting performance.now to deterministicNow doesn't work so we instead
21+
// use a helper function in parseTools (getPerformanceNow()) to call it
22+
// directly.
23+
// if (typeof performance == 'object') performance.now = Date.now;
1524

1625
Module['thisProgram'] = 'thisProgram'; // for consistency between different builds than between runs of the same build
1726

src/library.js

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2332,7 +2332,7 @@ mergeInto(LibraryManager.library, {
23322332
// Pthreads need their clocks synchronized to the execution of the main
23332333
// thread, so, when using them, make sure to adjust all timings to the
23342334
// respective time origins.
2335-
_emscripten_get_now = () => performance.timeOrigin + performance.now();
2335+
_emscripten_get_now = () => performance.timeOrigin + {{{ getPerformanceNow() }}}();
23362336
#else
23372337
#if ENVIRONMENT_MAY_BE_SHELL
23382338
if (typeof dateNow != 'undefined') {
@@ -2344,11 +2344,11 @@ mergeInto(LibraryManager.library, {
23442344
// (https://github.com/WebAudio/web-audio-api/issues/2527), so if building
23452345
// with
23462346
// Audio Worklets enabled, do a dynamic check for its presence.
2347-
if (typeof performance != 'undefined' && performance.now) {
2347+
if (typeof performance != 'undefined' && {{{ getPerformanceNow() }}}) {
23482348
#if PTHREADS
2349-
_emscripten_get_now = () => performance.timeOrigin + performance.now();
2349+
_emscripten_get_now = () => performance.timeOrigin + {{{ getPerformanceNow() }}}();
23502350
#else
2351-
_emscripten_get_now = () => performance.now();
2351+
_emscripten_get_now = () => {{{ getPerformanceNow() }}}();
23522352
#endif
23532353
} else {
23542354
_emscripten_get_now = Date.now;
@@ -2357,7 +2357,7 @@ mergeInto(LibraryManager.library, {
23572357
// Modern environment where performance.now() is supported:
23582358
// N.B. a shorter form "_emscripten_get_now = performance.now;" is
23592359
// unfortunately not allowed even in current browsers (e.g. FF Nightly 75).
2360-
_emscripten_get_now = () => performance.now();
2360+
_emscripten_get_now = () => {{{ getPerformanceNow() }}}();
23612361
#endif
23622362
#endif
23632363
`,

src/library_eventloop.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -113,10 +113,10 @@ LibraryJSEventLoop = {
113113
clearTimeout(id);
114114
},
115115

116-
emscripten_set_timeout_loop__deps: ['$callUserCallback'],
116+
emscripten_set_timeout_loop__deps: ['$callUserCallback', 'emscripten_get_now'],
117117
emscripten_set_timeout_loop: function(cb, msecs, userData) {
118118
function tick() {
119-
var t = performance.now();
119+
var t = _emscripten_get_now();
120120
var n = t + msecs;
121121
{{{ runtimeKeepalivePop() }}}
122122
callUserCallback(function() {
@@ -125,7 +125,7 @@ LibraryJSEventLoop = {
125125
// negative setTimeout as timeout of 0
126126
// (https://stackoverflow.com/questions/8430966/is-calling-settimeout-with-a-negative-delay-ok)
127127
{{{ runtimeKeepalivePush() }}}
128-
setTimeout(tick, n - performance.now());
128+
setTimeout(tick, n - _emscripten_get_now());
129129
}
130130
});
131131
}

src/library_html5.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2587,7 +2587,7 @@ var LibraryHTML5 = {
25872587
},
25882588

25892589
emscripten_performance_now: function() {
2590-
return performance.now();
2590+
return {{{ getPerformanceNow() }}}();
25912591
},
25922592

25932593
emscripten_get_device_pixel_ratio__proxy: 'sync',

src/parseTools.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1019,3 +1019,11 @@ function formattedMinNodeVersion() {
10191019
var rev = MIN_NODE_VERSION % 100
10201020
return `v${major}.${minor}.${rev}`;
10211021
}
1022+
1023+
function getPerformanceNow() {
1024+
if (DETERMINISTIC) {
1025+
return 'deterministicNow';
1026+
} else {
1027+
return 'performance.now';
1028+
}
1029+
}

0 commit comments

Comments
 (0)