Skip to content

Commit 5998b65

Browse files
authored
Use the same json interchange format preprocessor.mjs as compiler.mjs. NFC (emscripten-core#23647)
I'm not sure why used `.js` in once case and `.json` in another. The other nice thing about this change is that it has the preprocessor read the default settings (just like compiler.mjs) so the json file can contain just the changes from the default.
1 parent fb7ad9d commit 5998b65

File tree

3 files changed

+29
-28
lines changed

3 files changed

+29
-28
lines changed

tools/compiler.mjs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,8 +39,8 @@ Usage: compiler.mjs <settings.json> [-o out.js] [--symbols-only]`);
3939
// Load settings from JSON passed on the command line
4040
const settingsFile = positionals[0];
4141
assert(settingsFile, 'settings file not specified');
42-
const user_settings = JSON.parse(readFile(settingsFile));
43-
applySettings(user_settings);
42+
const userSettings = JSON.parse(readFile(settingsFile));
43+
applySettings(userSettings);
4444

4545
export const symbolsOnly = values['symbols-only'];
4646

tools/preprocessor.mjs

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
import assert from 'node:assert';
1616
import {parseArgs} from 'node:util';
1717

18-
import {loadSettingsFile} from '../src/utility.mjs';
18+
import {readFile, loadDefaultSettings, applySettings} from '../src/utility.mjs';
1919

2020
const options = {
2121
'expand-macros': {type: 'boolean'},
@@ -31,11 +31,17 @@ Usage: preprocessor.mjs <settings.json> <input-file> [--expand-macros]`);
3131
process.exit(0);
3232
}
3333

34+
loadDefaultSettings();
35+
3436
assert(positionals.length == 2, 'Script requires 2 arguments');
37+
38+
// Load settings from JSON passed on the command line
3539
const settingsFile = positionals[0];
36-
const inputFile = positionals[1];
40+
assert(settingsFile, 'settings file not specified');
41+
const userSettings = JSON.parse(readFile(settingsFile));
42+
applySettings(userSettings);
3743

38-
loadSettingsFile(settingsFile);
44+
const inputFile = positionals[1];
3945

4046
// We can't use static import statements here because several of these
4147
// file depend on having the settings defined in the global scope (which

tools/shared.py

Lines changed: 18 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -723,29 +723,24 @@ def read_and_preprocess(filename, expand_macros=False):
723723
temp_dir = get_emscripten_temp_dir()
724724
# Create a settings file with the current settings to pass to the JS preprocessor
725725

726-
settings_str = ''
727-
for key, value in settings.external_dict().items():
728-
assert key == key.upper() # should only ever be uppercase keys in settings
729-
jsoned = json.dumps(value, sort_keys=True)
730-
settings_str += f'var {key} = {jsoned};\n'
731-
732-
settings_file = os.path.join(temp_dir, 'settings.js')
733-
utils.write_file(settings_file, settings_str)
734-
735-
# Run the JS preprocessor
736-
# N.B. We can't use the default stdout=PIPE here as it only allows 64K of output before it hangs
737-
# and shell.html is bigger than that!
738-
# See https://thraxil.org/users/anders/posts/2008/03/13/Subprocess-Hanging-PIPE-is-your-enemy/
739-
dirname, filename = os.path.split(filename)
740-
if not dirname:
741-
dirname = None
742-
stdout = os.path.join(temp_dir, 'stdout')
743-
args = [settings_file, filename]
744-
if expand_macros:
745-
args += ['--expand-macros']
746-
747-
run_js_tool(path_from_root('tools/preprocessor.mjs'), args, stdout=open(stdout, 'w'), cwd=dirname)
748-
out = utils.read_file(stdout)
726+
with get_temp_files().get_file('.json') as settings_file:
727+
with open(settings_file, 'w') as s:
728+
json.dump(settings.external_dict(), s, sort_keys=True, indent=2)
729+
730+
# Run the JS preprocessor
731+
# N.B. We can't use the default stdout=PIPE here as it only allows 64K of output before it hangs
732+
# and shell.html is bigger than that!
733+
# See https://thraxil.org/users/anders/posts/2008/03/13/Subprocess-Hanging-PIPE-is-your-enemy/
734+
dirname, filename = os.path.split(filename)
735+
if not dirname:
736+
dirname = None
737+
stdout = os.path.join(temp_dir, 'stdout')
738+
args = [settings_file, filename]
739+
if expand_macros:
740+
args += ['--expand-macros']
741+
742+
run_js_tool(path_from_root('tools/preprocessor.mjs'), args, stdout=open(stdout, 'w'), cwd=dirname)
743+
out = utils.read_file(stdout)
749744

750745
return out
751746

0 commit comments

Comments
 (0)