Skip to content

Commit 7c092b7

Browse files
committed
Centralize nodejs performace polyfill. NFC
1 parent 893e0a9 commit 7c092b7

File tree

5 files changed

+18
-23
lines changed

5 files changed

+18
-23
lines changed

src/library.js

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1429,13 +1429,6 @@ addToLibrary({
14291429
emscripten_random: () => Math.random(),
14301430

14311431
emscripten_get_now: `;
1432-
#if ENVIRONMENT_MAY_BE_NODE && MIN_NODE_VERSION < 160000
1433-
// The performance global was added to node in v16.0.0:
1434-
// https://nodejs.org/api/globals.html#performance
1435-
if (ENVIRONMENT_IS_NODE) {
1436-
global.performance = require('perf_hooks').performance;
1437-
}
1438-
#endif
14391432
#if PTHREADS && !AUDIO_WORKLET
14401433
// Pthreads need their clocks synchronized to the execution of the main
14411434
// thread, so, when using them, make sure to adjust all timings to the

src/preamble.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,6 @@
1414
// An online HTML version (which may be of a different version of Emscripten)
1515
// is up at http://kripken.github.io/emscripten-site/docs/api_reference/preamble.js.html
1616

17-
#if PTHREADS
18-
#include "runtime_pthread.js"
19-
#endif
20-
2117
#if RELOCATABLE
2218
{{{ makeModuleReceiveWithVar('dynamicLibraries', undefined, '[]', true) }}}
2319
#endif
@@ -152,6 +148,10 @@ var HEAP_DATA_VIEW;
152148

153149
#include "runtime_shared.js"
154150

151+
#if PTHREADS
152+
#include "runtime_pthread.js"
153+
#endif
154+
155155
#if ASSERTIONS
156156
assert(!Module['STACK_SIZE'], 'STACK_SIZE can no longer be set at runtime. Use -sSTACK_SIZE at link time')
157157
#endif

src/preamble_minimal.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,6 @@
1212
#include "runtime_asan.js"
1313
#endif
1414

15-
#if PTHREADS
16-
#include "runtime_pthread.js"
17-
#endif
18-
1915
#if ASSERTIONS
2016
/** @type {function(*, string=)} */
2117
function assert(condition, text) {
@@ -60,6 +56,10 @@ var HEAP8, HEAP16, HEAP32, HEAPU8, HEAPU16, HEAPU32, HEAPF32, HEAPF64,
6056

6157
#include "runtime_shared.js"
6258

59+
#if PTHREADS
60+
#include "runtime_pthread.js"
61+
#endif
62+
6363
#if IMPORTED_MEMORY
6464
#if PTHREADS
6565
if (!ENVIRONMENT_IS_PTHREAD) {

src/runtime_shared.js

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,3 +62,13 @@ var toIndexType = (function() {
6262
return (i) => bigintMemoryBounds ? BigInt(i) : i;
6363
})();
6464
#endif
65+
66+
#if ENVIRONMENT_MAY_BE_NODE && MIN_NODE_VERSION < 160000
67+
// The performance global was added to node in v16.0.0:
68+
// https://nodejs.org/api/globals.html#performance
69+
if (ENVIRONMENT_IS_NODE) {
70+
// This is needed for emscripten_get_now and for which emscripten pthreads
71+
// support which depends on it for good timing.
72+
globalThis.performance ??= require('perf_hooks').performance;
73+
}
74+
#endif

src/shell.js

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -428,14 +428,6 @@ if (!ENVIRONMENT_IS_AUDIO_WORKLET)
428428
}
429429

430430
#if ENVIRONMENT_MAY_BE_NODE && PTHREADS
431-
if (ENVIRONMENT_IS_NODE) {
432-
// Polyfill the performance object, which emscripten pthreads support
433-
// depends on for good timing.
434-
if (typeof performance == 'undefined') {
435-
global.performance = require('perf_hooks').performance;
436-
}
437-
}
438-
439431
// Set up the out() and err() hooks, which are how we can print to stdout or
440432
// stderr, respectively.
441433
// Normally just binding console.log/console.error here works fine, but

0 commit comments

Comments
 (0)