Skip to content

Commit 6cc710d

Browse files
committedJan 31, 2024
Re-enable preprocessing of pre/post JS file. NFC
This change got reverted in #19006 so this time we make the preprocessing optional. This is useful as it allows things like `{{{ POINTER_SIZE }}}` and `{{{ makeGetValue(..) }}}` to be used in pre/post JS files, just like they can be in JS library files. This change allows threadprofiler.js to be fixed such that it works under wasm64. Fixes: #21226
1 parent 876ae7c commit 6cc710d

8 files changed

+61
-23
lines changed
 

‎ChangeLog.md

+6
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,12 @@ See docs/process.md for more on how version tagging works.
2020

2121
3.1.54 (in development)
2222
-----------------------
23+
- `--pre-js` and `--post-js` files can now opt into being run through the JS
24+
preprocessor. This change was oringally landed in #18525, but it got reverted
25+
in #19006. Now it requires explict opt-in by add `'use preprocessor';` to
26+
the JS file. This is useful as it allows things like `{{{ POINTER_SIZE }}}`
27+
and `{{{ makeGetValue(..) }}}` to be used in pre/post JS files, just
28+
like they can be in JS library files. (#21227)
2329

2430
3.1.53 - 01/29/24
2531
-----------------

‎src/generated_struct_info32.json

+1
Original file line numberDiff line numberDiff line change
@@ -223,6 +223,7 @@
223223
"EM_PROMISE_MATCH": 1,
224224
"EM_PROMISE_MATCH_RELEASE": 2,
225225
"EM_PROMISE_REJECT": 3,
226+
"EM_THREAD_STATUS_NUMFIELDS": 7,
226227
"EM_TIMING_RAF": 1,
227228
"EM_TIMING_SETIMMEDIATE": 2,
228229
"EM_TIMING_SETTIMEOUT": 0,

‎src/generated_struct_info64.json

+1
Original file line numberDiff line numberDiff line change
@@ -223,6 +223,7 @@
223223
"EM_PROMISE_MATCH": 1,
224224
"EM_PROMISE_MATCH_RELEASE": 2,
225225
"EM_PROMISE_REJECT": 3,
226+
"EM_THREAD_STATUS_NUMFIELDS": 7,
226227
"EM_TIMING_RAF": 1,
227228
"EM_TIMING_SETIMMEDIATE": 2,
228229
"EM_TIMING_SETTIMEOUT": 0,

‎src/jsifier.js

+23-9
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,22 @@ function getTransitiveDeps(symbol) {
9797
return Array.from(transitiveDeps);
9898
}
9999

100+
function shouldPreprocess(fileName) {
101+
return read(fileName).match(/'use preprocessor';\n/) != null;
102+
}
103+
104+
function preJS() {
105+
let result = '';
106+
for (const fileName of PRE_JS_FILES) {
107+
if (shouldPreprocess(fileName)) {
108+
result += processMacros(preprocess(fileName));
109+
} else {
110+
result += read(fileName);
111+
}
112+
}
113+
return result;
114+
}
115+
100116
function runJSify() {
101117
const libraryItems = [];
102118
const symbolDeps = {};
@@ -583,15 +599,13 @@ function(${args}) {
583599
libraryItems.push(JS);
584600
}
585601

586-
function includeFile(fileName) {
602+
function includeFile(fileName, needsPreprocess = true) {
587603
print(`// include: ${fileName}`);
588-
print(processMacros(preprocess(fileName)));
589-
print(`// end include: ${fileName}`);
590-
}
591-
592-
function includeFileRaw(fileName) {
593-
print(`// include: ${fileName}`);
594-
print(read(fileName));
604+
if (needsPreprocess) {
605+
print(processMacros(preprocess(fileName)));
606+
} else {
607+
print(read(fileName));
608+
}
595609
print(`// end include: ${fileName}`);
596610
}
597611

@@ -653,7 +667,7 @@ var proxiedFunctionTable = [
653667
includeFile(postFile);
654668

655669
for (const fileName of POST_JS_FILES) {
656-
includeFileRaw(fileName);
670+
includeFile(fileName, shouldPreprocess(fileName));
657671
}
658672

659673
print('//FORWARDED_DATA:' + JSON.stringify({

‎src/parseTools.js

-8
Original file line numberDiff line numberDiff line change
@@ -1022,14 +1022,6 @@ function getEntryFunction() {
10221022
return `_${entryFunction}`;
10231023
}
10241024

1025-
function preJS() {
1026-
let result = '';
1027-
for (const fileName of PRE_JS_FILES) {
1028-
result += read(fileName);
1029-
}
1030-
return result;
1031-
}
1032-
10331025
function formattedMinNodeVersion() {
10341026
var major = MIN_NODE_VERSION / 10000
10351027
var minor = (MIN_NODE_VERSION / 100) % 100

‎src/struct_info_internal.json

+6
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,12 @@
2424
"SIGCANCEL"
2525
]
2626
},
27+
{
28+
"file": "threading_internal.h",
29+
"defines": [
30+
"EM_THREAD_STATUS_NUMFIELDS"
31+
]
32+
},
2733
{
2834
"file": "dynlink.h",
2935
"structs": {

‎src/threadprofiler.js

+7-6
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
'use preprocessor';
2+
13
/**
24
* @license
35
* Copyright 2015 The Emscripten Authors
@@ -41,7 +43,6 @@ var emscriptenThreadProfiler = {
4143
}
4244
for (var i = 0; i < threads.length; ++i) {
4345
var threadPtr = threads[i];
44-
var profilerBlock = Atomics.load(HEAPU32, (threadPtr + 8 /* {{{ C_STRUCTS.pthread.profilerBlock }}}*/) >> 2);
4546
var threadName = PThread.getThreadName(threadPtr);
4647
if (threadName) {
4748
threadName = `"${threadName}" (${ptrToString(threadPtr)})`;
@@ -69,7 +70,7 @@ var emscriptenThreadProfiler = {
6970

7071
for (var i = 0; i < threads.length; ++i) {
7172
var threadPtr = threads[i];
72-
var profilerBlock = Atomics.load(HEAPU32, (threadPtr + 8 /* {{{ C_STRUCTS.pthread.profilerBlock }}}*/) >> 2);
73+
var profilerBlock = Atomics.load({{{ getHeapForType('*') }}}, {{{ getHeapOffset('threadPtr + ' + C_STRUCTS.pthread.profilerBlock, '*') }}});
7374
var threadName = PThread.getThreadName(threadPtr);
7475
if (threadName) {
7576
threadName = `"${threadName}" (${ptrToString(threadPtr)})`;
@@ -81,11 +82,11 @@ var emscriptenThreadProfiler = {
8182

8283
var threadTimesInStatus = [];
8384
var totalTime = 0;
84-
var offset = profilerBlock + 16/*C_STRUCTS.thread_profiler_block.timeSpentInStatus*/;
85-
for (var j = 0; j < 7/*EM_THREAD_STATUS_NUMFIELDS*/; ++j, offset += 8) {
86-
threadTimesInStatus.push(Number(getValue(offset, 'double')));
85+
var offset = profilerBlock + {{{ C_STRUCTS.thread_profiler_block.timeSpentInStatus }}};
86+
for (var j = 0; j < {{{ cDefs.EM_THREAD_STATUS_NUMFIELDS }}}; ++j, offset += 8) {
87+
threadTimesInStatus.push({{{ makeGetValue('offset', 0, 'double') }}});
8788
totalTime += threadTimesInStatus[j];
88-
setValue(offset, 0, 'double');
89+
{{{ makeSetValue('offset', 0, 0, 'double') }}};
8990
}
9091
var recent = '';
9192
if (threadTimesInStatus[1] > 0) recent += (threadTimesInStatus[1] / totalTime * 100.0).toFixed(1) + '% running. ';

‎test/test_other.py

+17
Original file line numberDiff line numberDiff line change
@@ -14434,3 +14434,20 @@ def test_uuid(self):
1443414434
def test_wasm64_no_asan(self):
1443514435
err = self.expect_fail([EMCC, test_file('hello_world.c'), '-sMEMORY64', '-fsanitize=address'])
1443614436
self.assertContained('error: MEMORY64 does not yet work with ASAN', err)
14437+
14438+
def test_js_preprocess_pre_post(self):
14439+
create_file('pre.js', '''
14440+
'use preprocessor';
14441+
#if ASSERTIONS
14442+
console.log('assertions enabled')
14443+
#else
14444+
console.log('assertions disabled')
14445+
#endif
14446+
''')
14447+
create_file('post.js', '''
14448+
'use preprocessor';
14449+
console.log({{{ POINTER_SIZE }}});
14450+
''')
14451+
self.emcc_args += ['--pre-js', 'pre.js', '--post-js', 'post.js']
14452+
self.do_runf(test_file('hello_world.c'), 'assertions enabled\n4')
14453+
self.do_runf(test_file('hello_world.c'), 'assertions disabled\n4', emcc_args=['-sASSERTIONS=0'])

0 commit comments

Comments
 (0)