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.

feat(sdk): native can report the failures only its shell can see

yhhd5d4ccb5e9c3
diff --git a/sdk/etc/index.native.api.md b/sdk/etc/index.native.api.md
index 4027019cc..179969a86 100644
--- a/sdk/etc/index.native.api.md
+++ b/sdk/etc/index.native.api.md
@@ -4358,9 +4358,11 @@ getStorageItem: (key: string) => string | null
language: (() => string) | undefined
loadImagePixels: (path: string) => Promise<ImageLoadResult>
now: (() => number) | undefined
+onContextLost: ((callback: () => void) => () => void) | undefined
onHide: ((callback: () => void) => () => void) | undefined
onMemoryWarning: ((callback: () => void) => () => void) | undefined
onShow: ((callback: () => void) => () => void) | undefined
+onUnhandledError: ((callback: (error: unknown) => void) => () => void) | undefined
rasterizeGlyph: ((request: PlatformGlyphRequest) => PlatformGlyph | null) | undefined
readCacheFile: ((key: string) => Promise<ArrayBuffer | null>) | undefined
readFile: (path: string) => Promise<ArrayBuffer>
@@ -4473,7 +4475,9 @@ name: "native"
now: () => number
onAppHide: (callback: () => void) => () => void
onAppShow: (callback: () => void) => () => void
+onContextLost: (callback: () => void) => () => void
onMemoryWarning: (callback: () => void) => () => void
+onUnhandledError: (callback: (error: unknown) => void) => () => void
rasterizeGlyph: ((request: PlatformGlyphRequest) => PlatformGlyph | null) | undefined
readCacheFile: (key: string) => Promise<ArrayBuffer | null>
readFile: (path: string) => Promise<ArrayBuffer>
diff --git a/sdk/src/platform/native/adapter.ts b/sdk/src/platform/native/adapter.ts
index 73cbd4783..388c9e083 100644
--- a/sdk/src/platform/native/adapter.ts
+++ b/sdk/src/platform/native/adapter.ts
@@ -175,6 +175,18 @@ export class NativePlatformAdapter implements PlatformAdapter {
return this.bridge_.onMemoryWarning?.(callback) ?? (() => {});
}
+ // Device loss, pushed by the shell — invisible from JS on native, since the
+ // surface lives in the host binary. A bridge without it never fires.
+ onContextLost(callback: () => void): () => void {
+ return this.bridge_.onContextLost?.(callback) ?? (() => {});
+ }
+
+ // Same shape, same reason: QuickJS has no window.onerror, so an uncaught
+ // error is only visible to the shell's exception callback.
+ onUnhandledError(callback: (error: unknown) => void): () => void {
+ return this.bridge_.onUnhandledError?.(callback) ?? (() => {});
+ }
+
getStorageItem(key: string): string | null {
return this.bridge_.getStorageItem(key);
}
diff --git a/sdk/src/platform/native/bridge.ts b/sdk/src/platform/native/bridge.ts
index 83d4dadab..0a5493e69 100644
--- a/sdk/src/platform/native/bridge.ts
+++ b/sdk/src/platform/native/bridge.ts
@@ -195,4 +195,19 @@ export interface NativeBridge {
* subscribe via the adapter's `onMemoryWarning` to drop evictable entries.
* Returns an unsubscribe. Optional. */
onMemoryWarning?(callback: () => void): () => void;
+
+ /** An error that reached the host with nobody catching it. There is no DOM on
+ * native and QuickJS has no `window.onerror`, so the shell's JS-engine
+ * exception callback is the only thing that sees these — it pushes them in,
+ * and diagnostics records them as `unhandled`. Returns an unsubscribe.
+ * Optional. */
+ onUnhandledError?(callback: (error: unknown) => void): () => void;
+
+ /** The render device was lost — the WebGPU `device.lost` promise on the Dawn
+ * side, an Android surface torn down on rotation or task-switch. The engine
+ * cannot see this from JS: the surface belongs to the host binary, so this is
+ * the shell pushing it in, the same way it pushes the two above. Diagnostics
+ * records it; nothing recovers from it yet. Returns an unsubscribe. Optional —
+ * a shell that has not wired it simply never fires. */
+ onContextLost?(callback: () => void): () => void;
}
diff --git a/sdk/src/platform/types.ts b/sdk/src/platform/types.ts
index 1ae443b0e..b8501fe6e 100644
--- a/sdk/src/platform/types.ts
+++ b/sdk/src/platform/types.ts
@@ -500,8 +500,24 @@ export interface PlatformAdapter {
* Worth its own channel because it is invisible from everywhere else: no
* error is thrown and no log is written, the frames simply stop containing
* anything. A game whose players report "it went black" has no other way to
- * find out that this is what happened. Optional — a platform whose surface
- * cannot be lost (native, node) omits it.
+ * find out that this is what happened.
+ *
+ * Who can answer this, and who cannot:
+ *
+ * web — yes. `webglcontextlost` does not bubble, but a non-bubbling event
+ * still travels the capture phase, so one window-level listener sees every
+ * canvas.
+ *
+ * native — yes, IF the shell wired it (`NativeBridge.onContextLost`). The
+ * surface belongs to the host binary and is not visible from JS at all, so
+ * it has to be pushed in, like memory pressure and foreground/background.
+ *
+ * mini-game — NO, and this is a platform limit rather than a gap here. A
+ * mini-game canvas is not a DOM element: `MiniGameCanvas` is width, height
+ * and getContext, with no listener registration and no vendor API for
+ * context loss. Nothing to duck-type for. Left unimplemented rather than
+ * approximated, because a hook that silently never fires reads as "this
+ * never happens" — which on a phone is the opposite of true.
*/
onContextLost?(callback: () => void): () => void;