Skip to content

A fast 2D and 3D game engine — TypeScript SDK, C++/WebAssembly core, visual editor. Ship one project to web, desktop, WeChat MiniGames, playable ads, and native Android / iOS.

fix: bound binaryen workers on windows

yhh2ccf37b4a070
diff --git a/CONTRIBUTING.md b/CONTRIBUTING.md
index 00e5c93f8..bc5fc0567 100644
--- a/CONTRIBUTING.md
+++ b/CONTRIBUTING.md
@@ -67,6 +67,20 @@ cd desktop && npm run dev
## Making Changes
+### Windows WASM optimizer workers
+
+The build tools default to `BINARYEN_CORES=4` on Windows to avoid crashes in the
+bundled Binaryen optimizer's default all-CPU worker pool. Release optimization
+flags, including `-O3`, remain unchanged. Explicit `BINARYEN_CORES` values take
+precedence; other platforms keep Binaryen's default behavior.
+
+When invoking Emscripten or CMake directly outside the build tools, set the same
+environment variable in PowerShell before building:
+
+```powershell
+$env:BINARYEN_CORES = '4'
+```
+
### Branch Naming
Create a branch from `master` with a descriptive name:
diff --git a/build-tools/utils/emscripten.js b/build-tools/utils/emscripten.js
index 676f29076..53fd3f773 100644
--- a/build-tools/utils/emscripten.js
+++ b/build-tools/utils/emscripten.js
@@ -103,6 +103,15 @@ export function proveEmcc(suite) {
let emsdkActivated = false;
+// Bound Binaryen workers on Windows; preserve explicit environment overrides.
+export function withBinaryenWorkers(env, platform = process.platform) {
+ const result = { ...env };
+ if (platform === 'win32' && result.BINARYEN_CORES === undefined) {
+ result.BINARYEN_CORES = '4';
+ }
+ return result;
+}
+
/**
* Locate an activated emsdk and inject it into this process's environment so
* `emcc`/`em++`/`emcmake`/`wasm-opt` resolve without the caller having to source
@@ -111,6 +120,8 @@ let emsdkActivated = false;
* active (either pre-existing on PATH or freshly discovered).
*/
export function ensureEmscriptenEnv() {
+ const workers = withBinaryenWorkers(process.env).BINARYEN_CORES;
+ if (workers !== undefined) process.env.BINARYEN_CORES = workers;
if (emsdkActivated) return true;
let root = null;
@@ -255,7 +266,7 @@ export function runCommand(command, args, options = {}) {
const executable = shell ? `chcp 65001>nul && ${quoted(command)}` : command;
const proc = spawn(executable, args.map(quoted), {
cwd,
- env: { ...process.env, ...env },
+ env: withBinaryenWorkers({ ...process.env, ...env }),
stdio: silent ? 'pipe' : 'inherit',
shell,
});
diff --git a/tools/tests/emscripten-workers.test.ts b/tools/tests/emscripten-workers.test.ts
new file mode 100644
index 000000000..fab202368
--- /dev/null
+++ b/tools/tests/emscripten-workers.test.ts
@@ -0,0 +1,29 @@
+// SPDX-License-Identifier: Apache-2.0
+// SPDX-FileCopyrightText: Copyright (c) 2024-present ESEngine Team
+import { expect, it } from 'vitest';
+import { runCommand, withBinaryenWorkers } from '../../build-tools/utils/emscripten.js';
+
+it('bounds Windows workers while preserving explicit settings and other platforms', () => {
+ const original = { PATH: 'toolchain' };
+ expect(withBinaryenWorkers(original, 'win32')).toEqual({ ...original, BINARYEN_CORES: '4' });
+ expect(original).toEqual({ PATH: 'toolchain' });
+ for (const platform of ['win32', 'linux', 'darwin']) {
+ expect(withBinaryenWorkers({ BINARYEN_CORES: '1' }, platform).BINARYEN_CORES).toBe('1');
+ }
+ expect(withBinaryenWorkers(original, 'linux')).toEqual(original);
+ expect(withBinaryenWorkers(original, 'darwin')).toEqual(original);
+});
+
+it('passes per-command worker overrides to the actual child process', async () => {
+ const result = await runCommand(process.execPath, [
+ '-p', 'process.env.BINARYEN_CORES',
+ ], { silent: true, env: { BINARYEN_CORES: '2' } });
+ expect(result.stdout.trim()).toBe('2');
+});
+
+it('applies the platform default to the actual child process', async () => {
+ const result = await runCommand(process.execPath, [
+ '-p', 'process.env.BINARYEN_CORES',
+ ], { silent: true, env: { BINARYEN_CORES: undefined } });
+ expect(result.stdout.trim()).toBe(process.platform === 'win32' ? '4' : 'undefined');
+});