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(minigame): an unset storage key is absent, not an empty string

esengine92a7bbf1db82
diff --git a/CHANGELOG.md b/CHANGELOG.md
index 5d385dae7..511d11e5d 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -16,6 +16,13 @@ published separately; it ships inside the editor.
### Fixed
+- **A mini-game reads an unset storage key as absent.** `wx.getStorageSync` answers a
+ key that was never written with `''`, and the adapter passed that up as a stored
+ value — so `Storage.getJSON` handed it to `JSON.parse`, which throws on it. A game
+ booted with one warning per JSON key it had not written yet (`Failed to parse JSON
+ for key "achievements"`), and `has()` reported keys that do not exist. `null` means
+ absent, as it does on the web.
+
- **A WeChat 分包 root carries the entry file the vendor demands.** With the phantom
roots gone, the export hit the next requirement behind them: devtools refuses to
compile a package whose subpackage root has no `game.js`, naming the missing file
diff --git a/sdk/src/platform/minigame/adapter.ts b/sdk/src/platform/minigame/adapter.ts
index 63a89ed5f..0a93814a8 100644
--- a/sdk/src/platform/minigame/adapter.ts
+++ b/sdk/src/platform/minigame/adapter.ts
@@ -452,10 +452,13 @@ export class MiniGamePlatformAdapter implements PlatformAdapter {
return this.profile_.createSocket?.(options) ?? new MiniGameSocket(options, this.g_);
}
+ /** `null` means ABSENT, as on the web — but `getStorageSync` answers an unset
+ * key with `''`, which readers above take for a stored value and fail to
+ * parse. The host cannot tell the two apart, so `''` is read as absence. */
getStorageItem(key: string): string | null {
try {
const value = this.g_.getStorageSync(key);
- return typeof value === 'string' ? value : null;
+ return typeof value === 'string' && value !== '' ? value : null;
} catch {
return null;
}
diff --git a/sdk/tests/storage.test.ts b/sdk/tests/storage.test.ts
index 1be932d3e..9fafdd282 100644
--- a/sdk/tests/storage.test.ts
+++ b/sdk/tests/storage.test.ts
@@ -177,3 +177,21 @@ describe('Storage', () => {
});
});
});
+
+describe('a mini-game host reports an unset key as an empty string', () => {
+ it('is read as absent, so every accessor answers its default', async () => {
+ // wx.getStorageSync answers `''` for a key that was never set. Taken for a
+ // stored value it reaches JSON.parse, which throws on it — the shipped
+ // symptom was a boot-time warning per JSON key a game had not written yet.
+ const { MiniGamePlatformAdapter } = await import('../src/platform/minigame/adapter');
+ const store = new Map<string, string>([['written', 'kept']]);
+ const g = {
+ getStorageSync: (k: string) => store.get(k) ?? '',
+ setStorageSync: (k: string, v: string) => { store.set(k, v); },
+ removeStorageSync: (k: string) => { store.delete(k); },
+ };
+ const adapter = new MiniGamePlatformAdapter({ id: 'wechat', hostLabel: 'WeChat', global: g } as never);
+ expect(adapter.getStorageItem('never-written')).toBeNull();
+ expect(adapter.getStorageItem('written')).toBe('kept');
+ });
+});