Skip to content

Fix -sDETERMINISTIC under node >= 16 #19663

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jun 21, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .circleci/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -617,6 +617,7 @@ jobs:
- run-tests:
title: "selected subset"
test_targets: "
other.test_deterministic
other.test_gen_struct_info
other.test_native_call_before_init
other.test_node_unhandled_rejection
Expand Down
13 changes: 11 additions & 2 deletions src/deterministic.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,18 @@ Math.random = () => {
MAGIC = Math.pow(MAGIC + 1.8912, 3) % 1;
return MAGIC;
};

var TIME = 10000;
Date.now = () => TIME++;
if (typeof performance == 'object') performance.now = Date.now;
function deterministicNow() {
return TIME++;
}

Date.now = deterministicNow;

// Setting performance.now to deterministicNow doesn't work so we instead
// use a helper function in parseTools (getPerformanceNow()) to call it
// directly.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe link to this PR as a source for the issue?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure thats really necessary.. isn't that what git blame is for?

// if (typeof performance == 'object') performance.now = Date.now;

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

Expand Down
10 changes: 5 additions & 5 deletions src/library.js
Original file line number Diff line number Diff line change
Expand Up @@ -2332,7 +2332,7 @@ mergeInto(LibraryManager.library, {
// Pthreads need their clocks synchronized to the execution of the main
// thread, so, when using them, make sure to adjust all timings to the
// respective time origins.
_emscripten_get_now = () => performance.timeOrigin + performance.now();
_emscripten_get_now = () => performance.timeOrigin + {{{ getPerformanceNow() }}}();
#else
#if ENVIRONMENT_MAY_BE_SHELL
if (typeof dateNow != 'undefined') {
Expand All @@ -2344,11 +2344,11 @@ mergeInto(LibraryManager.library, {
// (https://github.com/WebAudio/web-audio-api/issues/2527), so if building
// with
// Audio Worklets enabled, do a dynamic check for its presence.
if (typeof performance != 'undefined' && performance.now) {
if (typeof performance != 'undefined' && {{{ getPerformanceNow() }}}) {
#if PTHREADS
_emscripten_get_now = () => performance.timeOrigin + performance.now();
_emscripten_get_now = () => performance.timeOrigin + {{{ getPerformanceNow() }}}();
#else
_emscripten_get_now = () => performance.now();
_emscripten_get_now = () => {{{ getPerformanceNow() }}}();
#endif
} else {
_emscripten_get_now = Date.now;
Expand All @@ -2357,7 +2357,7 @@ mergeInto(LibraryManager.library, {
// Modern environment where performance.now() is supported:
// N.B. a shorter form "_emscripten_get_now = performance.now;" is
// unfortunately not allowed even in current browsers (e.g. FF Nightly 75).
_emscripten_get_now = () => performance.now();
_emscripten_get_now = () => {{{ getPerformanceNow() }}}();
#endif
#endif
`,
Expand Down
6 changes: 3 additions & 3 deletions src/library_eventloop.js
Original file line number Diff line number Diff line change
Expand Up @@ -113,10 +113,10 @@ LibraryJSEventLoop = {
clearTimeout(id);
},

emscripten_set_timeout_loop__deps: ['$callUserCallback'],
emscripten_set_timeout_loop__deps: ['$callUserCallback', 'emscripten_get_now'],
emscripten_set_timeout_loop: function(cb, msecs, userData) {
function tick() {
var t = performance.now();
var t = _emscripten_get_now();
var n = t + msecs;
{{{ runtimeKeepalivePop() }}}
callUserCallback(function() {
Expand All @@ -125,7 +125,7 @@ LibraryJSEventLoop = {
// 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());
setTimeout(tick, n - _emscripten_get_now());
}
});
}
Expand Down
2 changes: 1 addition & 1 deletion src/library_html5.js
Original file line number Diff line number Diff line change
Expand Up @@ -2587,7 +2587,7 @@ var LibraryHTML5 = {
},

emscripten_performance_now: function() {
return performance.now();
return {{{ getPerformanceNow() }}}();
},

emscripten_get_device_pixel_ratio__proxy: 'sync',
Expand Down
8 changes: 8 additions & 0 deletions src/parseTools.js
Original file line number Diff line number Diff line change
Expand Up @@ -1019,3 +1019,11 @@ function formattedMinNodeVersion() {
var rev = MIN_NODE_VERSION % 100
return `v${major}.${minor}.${rev}`;
}

function getPerformanceNow() {
if (DETERMINISTIC) {
return 'deterministicNow';
} else {
return 'performance.now';
}
}