Skip to content

Commit 2f593a8

Browse files
authored
Use parseArgs in JS entry points. NFC (#23520)
1 parent 71761f5 commit 2f593a8

File tree

10 files changed

+51
-36
lines changed

10 files changed

+51
-36
lines changed

.circleci/config.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -755,9 +755,9 @@ jobs:
755755
# version.
756756
# Keep this in sync with MINIMUM_NODE_VERSION in tools/shared.py.
757757
- install-node-version:
758-
node_version: "18.0.0"
758+
node_version: "18.3.0"
759759
- run:
760-
name: configure compiler to use 18.0.0
760+
name: configure compiler to use 18.3.0
761761
command: echo "NODE_JS = '$(which node)'" >> ~/emsdk/.emscripten
762762
- install-node-canary
763763
- run-tests:

ChangeLog.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ See docs/process.md for more on how version tagging works.
3535
4.0.1 - 01/17/25
3636
----------------
3737
- The minimum version of node required to run emscripten was bumped from v16.20
38-
to v18. Version 4.0 was mistakenly shipped with a change that required v20,
38+
to v18.3. Version 4.0 was mistakenly shipped with a change that required v20,
3939
but that was reverted. (#23410)
4040
- `emscripten_webgl_create_context` now displays a warning message when there is
4141
a conflict between the `majorVersion` requested and the WebGL support defined

src/compiler.mjs

Lines changed: 20 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -5,32 +5,36 @@
55
* SPDX-License-Identifier: MIT
66
*/
77

8-
// LLVM => JavaScript compiler, main entry point
8+
// JavaScript compiler, main entry point
99

10-
import {
11-
Benchmarker,
12-
applySettings,
13-
assert,
14-
loadDefaultSettings,
15-
printErr,
16-
read,
17-
} from './utility.mjs';
10+
import {Benchmarker, applySettings, loadDefaultSettings, printErr, read} from './utility.mjs';
11+
12+
import assert from 'node:assert';
13+
import {parseArgs} from 'node:util';
1814

1915
loadDefaultSettings();
2016

21-
const argv = process.argv.slice(2);
22-
const symbolsOnlyArg = argv.indexOf('--symbols-only');
23-
if (symbolsOnlyArg != -1) {
24-
argv.splice(symbolsOnlyArg, 1);
17+
const options = {
18+
help: {type: 'boolean', short: 'h'},
19+
'symbols-only': {type: 'boolean'},
20+
};
21+
const {values, positionals} = parseArgs({options, allowPositionals: true});
22+
23+
if (values.help) {
24+
console.log(`\
25+
Main entry point for JS compiler
26+
27+
Usage: compiler.mjs <settings.json> [-o out.js] [--symbols-only]`);
28+
process.exit(0);
2529
}
2630

2731
// Load settings from JSON passed on the command line
28-
const settingsFile = argv[0];
29-
assert(settingsFile);
32+
const settingsFile = positionals[0];
33+
assert(settingsFile, 'settings file not specified');
3034
const user_settings = JSON.parse(read(settingsFile));
3135
applySettings(user_settings);
3236

33-
export const symbolsOnly = symbolsOnlyArg != -1;
37+
export const symbolsOnly = values['symbols-only'];
3438

3539
// In case compiler.mjs is run directly (as in gen_sig_info)
3640
// ALL_INCOMING_MODULE_JS_API might not be populated yet.

src/jsifier.mjs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
// Convert analyzed data to javascript. Everything has already been calculated
88
// before this stage, which just does the final conversion to JavaScript.
99

10+
import assert from 'node:assert';
1011
import {
1112
ATEXITS,
1213
ATINITS,
@@ -21,7 +22,6 @@ import {
2122
} from './parseTools.mjs';
2223
import {
2324
addToCompileTimeContext,
24-
assert,
2525
error,
2626
errorOccured,
2727
isDecorator,

src/modules.mjs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,10 @@
77
import * as path from 'node:path';
88
import * as fs from 'node:fs';
99
import {fileURLToPath} from 'node:url';
10+
import assert from 'node:assert';
1011

1112
import {
1213
isDecorator,
13-
assert,
1414
isJsOnlySymbol,
1515
error,
1616
read,

src/parseTools.mjs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,10 @@
1010

1111
import * as path from 'node:path';
1212
import {existsSync} from 'node:fs';
13+
import assert from 'node:assert';
1314

1415
import {
1516
addToCompileTimeContext,
16-
assert,
1717
error,
1818
printErr,
1919
read,

src/utility.mjs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,6 @@ import * as fs from 'node:fs';
1313
import * as vm from 'node:vm';
1414
import assert from 'node:assert';
1515

16-
export {assert};
17-
1816
export function safeQuote(x) {
1917
return x.replace(/"/g, '\\"').replace(/'/g, "\\'");
2018
}

test/test_sanity.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -287,8 +287,8 @@ def test_node(self):
287287
('v4.1.0', False),
288288
('v10.18.0', False),
289289
('v16.20.0', False),
290-
('v18.0.0', True),
291-
('v18.0.1-pre', True),
290+
('v18.3.0', True),
291+
('v18.3.1-pre', True),
292292
('cheez', False)):
293293
print(version, succeed)
294294
delete_file(SANITY_FILE)

tools/preprocessor.mjs

Lines changed: 19 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -12,16 +12,28 @@
1212
// file with modified settings and supply the filename here.
1313
// input file This is the file that will be processed by the preprocessor
1414

15-
import assert from 'assert';
15+
import assert from 'node:assert';
16+
import {parseArgs} from 'node:util';
1617

1718
import {loadSettingsFile} from '../src/utility.mjs';
1819

19-
const args = process.argv.slice(2);
20+
const options = {
21+
'expand-macros': {type: 'boolean'},
22+
help: {type: 'boolean', short: 'h'},
23+
};
24+
const {values, positionals} = parseArgs({options, allowPositionals: true});
2025

21-
assert(args.length >= 2, 'Script requires 2 arguments');
22-
const settingsFile = args[0];
23-
const inputFile = args[1];
24-
const expandMacros = args.includes('--expandMacros');
26+
if (values.help) {
27+
console.log(`\
28+
Run JS preprocessor / macro processor on an input file
29+
30+
Usage: preprocessor.mjs <settings.json> <input-file> [--expand-macros]`);
31+
process.exit(0);
32+
}
33+
34+
assert(positionals.length == 2, 'Script requires 2 arguments');
35+
const settingsFile = positionals[0];
36+
const inputFile = positionals[1];
2537

2638
loadSettingsFile(settingsFile);
2739

@@ -32,7 +44,7 @@ const parseTools = await import('../src/parseTools.mjs');
3244
await import('../src/modules.mjs');
3345

3446
let output = parseTools.preprocess(inputFile);
35-
if (expandMacros) {
47+
if (values['expand-macros']) {
3648
output = parseTools.processMacros(output, inputFile);
3749
}
3850
process.stdout.write(output);

tools/shared.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -57,8 +57,9 @@
5757
# distinct from the minimum version required to execute the generated code
5858
# (settings.MIN_NODE_VERSION).
5959
# This is currently set to v18 since this is the version of node available
60-
# in debian/stable (bookworm).
61-
MINIMUM_NODE_VERSION = (18, 0, 0)
60+
# in debian/stable (bookworm). We need at least v18.3.0 because we make
61+
# use of util.parseArg which was added in v18.3.0.
62+
MINIMUM_NODE_VERSION = (18, 3, 0)
6263
EXPECTED_LLVM_VERSION = 21
6364

6465
# These get set by setup_temp_dirs
@@ -741,7 +742,7 @@ def read_and_preprocess(filename, expand_macros=False):
741742
stdout = os.path.join(temp_dir, 'stdout')
742743
args = [settings_file, filename]
743744
if expand_macros:
744-
args += ['--expandMacros']
745+
args += ['--expand-macros']
745746

746747
run_js_tool(path_from_root('tools/preprocessor.mjs'), args, stdout=open(stdout, 'w'), cwd=dirname)
747748
out = utils.read_file(stdout)

0 commit comments

Comments
 (0)