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(particles): refresh LUT heap views after allocation

yhhe55c9e299d10
diff --git a/CHANGELOG.md b/CHANGELOG.md
index 4dfc48865..37bb41967 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -16,6 +16,8 @@ published separately; it ships inside the editor.
### Fixed
+- **Particle gradient uploads survive WASM memory growth.** Color, size, and speed LUT uploads read the current heap after allocating their staging memory, preventing detached-buffer errors when a large scene causes memory expansion. LUTs borrowed from the heap are preserved before allocation; independent arrays avoid this copy. Staging memory is still released if the native setter throws.
+
- **Web focus loss cancels held pointer input and pending edges.** Blurring or hiding the game and replacing its input target releases host key, mouse, and touch tracking, clears stale render/fixed-step edges, and cancels UI presses without clicking them. The next touch can become the primary pointer, and late releases from the cancelled gesture are ignored.
- **Mini-game audio keeps playback ownership through device suspension.** Device-paused voices remain live for AudioSource completion, bus gain updates, and priority budgets. New play requests and manual resumes wait for device recovery; manual pauses withdraw automatic recovery, while stopped or despawned voices never restart.
diff --git a/sdk/src/particle/ParticleAPI.ts b/sdk/src/particle/ParticleAPI.ts
index 1df97b1c1..0dae80ed9 100644
--- a/sdk/src/particle/ParticleAPI.ts
+++ b/sdk/src/particle/ParticleAPI.ts
@@ -83,12 +83,15 @@ export class ParticleAPI {
fn(entity as number, 0, 0);
return;
}
- const { _malloc: malloc, _free: free, HEAPF32: heap } = this.module_;
- if (!malloc || !free || !heap) return; // a core with no heap marshals nothing
- const ptr = malloc(lut.length * 4);
+ const { _malloc: malloc, _free: free } = this.module_;
+ if (!malloc || !free || !this.module_.HEAPF32) return; // a core with no heap marshals nothing
+ // A caller may borrow the same heap that allocation is about to detach.
+ const source = lut.buffer === this.module_.HEAPF32.buffer ? lut.slice() : lut;
+ const ptr = malloc(source.length * 4);
try {
- heap.set(lut, ptr / 4);
- fn(entity as number, ptr, lut.length / stride);
+ // Allocation can grow WASM memory and detach the previous heap view.
+ this.module_.HEAPF32!.set(source, ptr / 4);
+ fn(entity as number, ptr, source.length / stride);
} finally {
free(ptr);
}
diff --git a/sdk/tests/particle-lut-memory.test.ts b/sdk/tests/particle-lut-memory.test.ts
new file mode 100644
index 000000000..c46fdb1bf
--- /dev/null
+++ b/sdk/tests/particle-lut-memory.test.ts
@@ -0,0 +1,47 @@
+// SPDX-License-Identifier: Apache-2.0
+// SPDX-FileCopyrightText: Copyright (c) 2024-present ESEngine Team
+import { expect, it, vi } from 'vitest';
+import { ParticleAPI } from '../src/particle/ParticleAPI';
+import type { EngineApi } from '../src/ecs/bridge/engineApi';
+import type { CppRegistry } from '../src/wasm';
+import type { Entity } from '../src/types';
+
+it.each([
+ ['setColorLut', 'particle_set_color_lut', 4],
+ ['setSizeLut', 'particle_set_size_lut', 1],
+ ['setSpeedLut', 'particle_set_speed_lut', 1],
+] as const)('%s uploads to the current heap after allocation grows WASM memory', (method, entry, stride) => {
+ const memory = new WebAssembly.Memory({ initial: 1, maximum: 2 });
+ const original = new Float32Array(memory.buffer), lut = new Float32Array(stride * 2).fill(.5);
+ let uploaded: number[] = [];
+ const fn = vi.fn((_entity: number, ptr: number, count: number) => {
+ uploaded = Array.from(new Float32Array(memory.buffer, ptr, count * stride));
+ });
+ const free = vi.fn();
+ const module = { _malloc: vi.fn(() => { memory.grow(1); return 16; }), _free: free,
+ get HEAPF32() { return new Float32Array(memory.buffer); }, [entry]: fn } as unknown as EngineApi;
+ const api = new ParticleAPI(module, {} as CppRegistry);
+ api[method](1 as Entity, lut);
+ expect(original.byteLength).toBe(0); expect(uploaded).toEqual(Array.from(lut));
+ expect(fn).toHaveBeenCalledWith(1, 16, 2); expect(free).toHaveBeenCalledWith(16);
+});
+it('frees upload memory when the native LUT setter throws', () => {
+ const free = vi.fn(), failure = Error('LUT rejected');
+ const module = { _malloc: vi.fn(() => 16), _free: free, HEAPF32: new Float32Array(32),
+ particle_set_size_lut: vi.fn(() => { throw failure; }) } as unknown as EngineApi;
+ const api = new ParticleAPI(module, {} as CppRegistry);
+ expect(() => api.setSizeLut(1 as Entity, new Float32Array([1, .5]))).toThrow(failure);
+ expect(free).toHaveBeenCalledOnce();
+});
+it('preserves a LUT borrowed from the heap before allocation detaches its source', () => {
+ const memory = new WebAssembly.Memory({ initial: 1, maximum: 2 });
+ const lut = new Float32Array(memory.buffer, 0, 3); lut.set([1, .5, 0]);
+ let uploaded: number[] = [];
+ const fn = vi.fn((_entity: number, ptr: number, count: number) => {
+ uploaded = Array.from(new Float32Array(memory.buffer, ptr, count));
+ });
+ const module = { _malloc: vi.fn(() => { memory.grow(1); return 16; }), _free: vi.fn(),
+ get HEAPF32() { return new Float32Array(memory.buffer); }, particle_set_size_lut: fn } as unknown as EngineApi;
+ new ParticleAPI(module, {} as CppRegistry).setSizeLut(1 as Entity, lut);
+ expect(uploaded).toEqual([1, .5, 0]); expect(fn).toHaveBeenCalledWith(1, 16, 3);
+});
diff --git a/tools/releaseNotes.mjs b/tools/releaseNotes.mjs
index 01b6bc597..8470a84b1 100644
--- a/tools/releaseNotes.mjs
+++ b/tools/releaseNotes.mjs
@@ -28,6 +28,8 @@ export const NOTES_FLOOR = 'v0.65.0';
* the reader recognises anyway.
*/
export const NOTED = {
+ 'fix(particles): refresh LUT heap views after allocation':
+ { note: 'Particle gradient uploads survive WASM memory growth.' },
'fix(input): cancel held web input on focus loss':
{ note: 'Web focus loss cancels held pointer input and pending edges.' },
'fix(audio): retain mini-game playback intent during device suspension':