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(wechat): basis module gets the wxgame shim; logger renders foreign-context errors

yhh4c7cb771c20a
diff --git a/cmake/Emscripten.cmake b/cmake/Emscripten.cmake
index 2a2d0f6cc..393032873 100644
--- a/cmake/Emscripten.cmake
+++ b/cmake/Emscripten.cmake
@@ -368,7 +368,11 @@ function(es_apply_basis_module_settings TARGET_NAME)
if(ES_BUILD_WEB OR ES_BUILD_WXGAME)
target_compile_options(${TARGET_NAME} PRIVATE ${ES_EMSCRIPTEN_COMPILE_FLAGS} -flto -fno-exceptions -fno-rtti)
- string(REPLACE ";" " " LINK_FLAGS_STR "${ES_EMSCRIPTEN_BASIS_MODULE_FLAGS}")
+ set(_BASIS_LINK_FLAGS ${ES_EMSCRIPTEN_BASIS_MODULE_FLAGS})
+ if(ES_BUILD_WXGAME)
+ list(APPEND _BASIS_LINK_FLAGS ${ES_EMSCRIPTEN_WXGAME_MODULE_EXTRA})
+ endif()
+ string(REPLACE ";" " " LINK_FLAGS_STR "${_BASIS_LINK_FLAGS}")
set_target_properties(${TARGET_NAME} PROPERTIES
SUFFIX ".js"
LINK_FLAGS "${LINK_FLAGS_STR}"
diff --git a/sdk/src/logger.ts b/sdk/src/logger.ts
index 6046ae20f..b23eccad7 100644
--- a/sdk/src/logger.ts
+++ b/sdk/src/logger.ts
@@ -24,7 +24,7 @@ export interface LogHandler {
handle(entry: LogEntry): void;
}
-class ConsoleLogHandler implements LogHandler {
+export class ConsoleLogHandler implements LogHandler {
handle(entry: LogEntry): void {
const time = new Date(entry.timestamp).toISOString().substring(11, 23);
const levelStr = LogLevel[entry.level].toUpperCase().padEnd(5);
@@ -38,8 +38,15 @@ class ConsoleLogHandler implements LogHandler {
let errorArg: Error | undefined;
if (entry.data !== undefined) {
+ const errLike = entry.data as { message?: unknown; stack?: unknown } | null;
if (entry.data instanceof Error) {
errorArg = entry.data;
+ } else if (errLike && (typeof errLike.stack === 'string' || typeof errLike.message === 'string')) {
+ // An error thrown in another JS context (WeChat sub-context,
+ // iframe realm) fails instanceof Error but still carries
+ // message/stack — JSON.stringify would render it as `{}`
+ // (Error props are non-enumerable).
+ line += ` ${String(errLike.stack ?? errLike.message)}`;
} else {
try {
line += ` ${JSON.stringify(entry.data)}`;
diff --git a/sdk/tests/logger.test.ts b/sdk/tests/logger.test.ts
index b736b0b07..eee52155e 100644
--- a/sdk/tests/logger.test.ts
+++ b/sdk/tests/logger.test.ts
@@ -1,7 +1,7 @@
// SPDX-License-Identifier: Apache-2.0
// SPDX-FileCopyrightText: Copyright (c) 2024-present ESEngine Team
import { describe, it, expect, beforeEach, vi } from 'vitest';
-import { Logger, LogLevel, type LogHandler, type LogEntry } from '../src/logger';
+import { Logger, LogLevel, ConsoleLogHandler, type LogHandler, type LogEntry } from '../src/logger';
describe('Logger', () => {
let logger: Logger;
@@ -123,6 +123,39 @@ describe('Logger', () => {
});
});
+ describe('console handler error rendering', () => {
+ it('renders a foreign-context error (fails instanceof, has stack) as text, not {}', () => {
+ const spy = vi.spyOn(console, 'warn').mockImplementation(() => {});
+ try {
+ new ConsoleLogHandler().handle({
+ timestamp: Date.now(), level: LogLevel.Warn, category: 'runtime',
+ message: 'Failed to load texture: a.png',
+ data: { message: 'boom', stack: 'FakeError: boom\n at x' },
+ });
+ expect(spy).toHaveBeenCalledTimes(1);
+ const line = spy.mock.calls[0][0] as string;
+ expect(line).toContain('FakeError: boom');
+ expect(line).not.toContain('{}');
+ } finally {
+ spy.mockRestore();
+ }
+ });
+
+ it('renders a real Error natively as a second console arg', () => {
+ const spy = vi.spyOn(console, 'error').mockImplementation(() => {});
+ try {
+ const err = new Error('kaput');
+ new ConsoleLogHandler().handle({
+ timestamp: Date.now(), level: LogLevel.Error, category: 'runtime',
+ message: 'boom', data: err,
+ });
+ expect(spy.mock.calls[0][1]).toBe(err);
+ } finally {
+ spy.mockRestore();
+ }
+ });
+ });
+
describe('timestamps', () => {
it('should include timestamp in log entry', () => {
const before = Date.now();