Skip to content

Commit e6cb215

Browse files
committed
lib: fix TypeScript support check in jitless mode
WebAssembly is disabled when Node.js is run with --jitless. The internal TypeScript stripper relies on WebAssembly, and previously failed obscurely with a ReferenceError in this mode. This commit adds an explicit check for WebAssembly support when loading the TypeScript parser. If WebAssembly is unavailable, it now throws a descriptive ERR_WEBASSEMBLY_NOT_SUPPORTED error. Fixes: #61353
1 parent 1409ac4 commit e6cb215

File tree

4 files changed

+25
-0
lines changed

4 files changed

+25
-0
lines changed

doc/api/errors.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3350,6 +3350,14 @@ The WASI instance has already started.
33503350

33513351
The WASI instance has not been started.
33523352

3353+
<a id="ERR_WEBASSEMBLY_NOT_SUPPORTED"></a>
3354+
3355+
### `ERR_WEBASSEMBLY_NOT_SUPPORTED`
3356+
3357+
A feature requiring WebAssembly was used, but WebAssembly is not supported or
3358+
has been disabled in the current environment (for example, when running with
3359+
`--jitless`).
3360+
33533361
<a id="ERR_WEBASSEMBLY_RESPONSE"></a>
33543362

33553363
### `ERR_WEBASSEMBLY_RESPONSE`

lib/internal/errors.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1905,6 +1905,7 @@ E('ERR_VM_MODULE_NOT_MODULE',
19051905
'Provided module is not an instance of Module', Error);
19061906
E('ERR_VM_MODULE_STATUS', 'Module status %s', Error);
19071907
E('ERR_WASI_ALREADY_STARTED', 'WASI instance has already started', Error);
1908+
E('ERR_WEBASSEMBLY_NOT_SUPPORTED', 'WebAssembly is not supported', Error);
19081909
E('ERR_WEBASSEMBLY_RESPONSE', 'WebAssembly response %s', TypeError);
19091910
E('ERR_WORKER_INIT_FAILED', 'Worker initialization failure: %s', Error);
19101911
E('ERR_WORKER_INVALID_EXEC_ARGV', (errors, msg = 'invalid execArgv flags') =>

lib/internal/modules/typescript.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
const {
44
ObjectPrototypeHasOwnProperty,
5+
globalThis: { WebAssembly },
56
} = primordials;
67
const {
78
validateBoolean,
@@ -18,6 +19,7 @@ const {
1819
ERR_INVALID_TYPESCRIPT_SYNTAX,
1920
ERR_UNSUPPORTED_NODE_MODULES_TYPE_STRIPPING,
2021
ERR_UNSUPPORTED_TYPESCRIPT_SYNTAX,
22+
ERR_WEBASSEMBLY_NOT_SUPPORTED,
2123
} = require('internal/errors').codes;
2224
const { getOptionValue } = require('internal/options');
2325
const assert = require('internal/assert');
@@ -44,6 +46,9 @@ const getTypeScriptParsingMode = getLazy(() =>
4446
*/
4547
const loadTypeScriptParser = getLazy(() => {
4648
assertTypeScript();
49+
if (WebAssembly === undefined) {
50+
throw new ERR_WEBASSEMBLY_NOT_SUPPORTED();
51+
}
4752
const amaro = require('internal/deps/amaro/dist/index');
4853
return amaro.transformSync;
4954
});

test/es-module/test-typescript.mjs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -342,3 +342,14 @@ test('check transform types warning', async () => {
342342
assert.match(result.stdout, /Hello, TypeScript!/);
343343
assert.strictEqual(result.code, 0);
344344
});
345+
346+
test('expect error when executing a TypeScript file with --jitless', async () => {
347+
const result = await spawnPromisified(process.execPath, [
348+
'--jitless',
349+
fixtures.path('typescript/ts/test-typescript.ts'),
350+
]);
351+
352+
assert.match(result.stderr, /ERR_WEBASSEMBLY_NOT_SUPPORTED/);
353+
assert.match(result.stderr, /WebAssembly is not supported/);
354+
assert.strictEqual(result.code, 1);
355+
});

0 commit comments

Comments
 (0)