From 259aed65ef378664930a583782b9008c08dee952 Mon Sep 17 00:00:00 2001 From: pandamicro Date: Fri, 25 Aug 2017 15:40:35 +0800 Subject: [PATCH] Report error stack to js __errorHandler --- .../js-bindings/manual/ScriptingCore.cpp | 54 +++++++++++-------- .../js-bindings/manual/ScriptingCore.h | 2 +- 2 files changed, 34 insertions(+), 22 deletions(-) diff --git a/native/cocos/scripting/js-bindings/manual/ScriptingCore.cpp b/native/cocos/scripting/js-bindings/manual/ScriptingCore.cpp index fc0cd3d968..e11a08fdfb 100644 --- a/native/cocos/scripting/js-bindings/manual/ScriptingCore.cpp +++ b/native/cocos/scripting/js-bindings/manual/ScriptingCore.cpp @@ -446,6 +446,20 @@ on_garbage_collect(JSContext* cx, JSGCStatus status, void* data) } } +static void warningHandler(JSContext *cx, JSErrorReport *report) +{ + if (cx && report) + { + JS::RootedValue err(cx); + if (JS_GetPendingException(cx, &err)) + { + // Should clear pending exception, otherwise it will trigger infinite loop + JS_ClearPendingException(cx); + } + ScriptingCore::reportError(cx, report, err); + } +}; + // Promise support using JobQueue = JS::GCVector; @@ -680,7 +694,7 @@ void ScriptingCore::createGlobalContext() { // Waiting is allowed on the shell's main thread, for now. JS_SetFutexCanWait(_cx); JS_SetDefaultLocale(_cx, "UTF-8"); - JS::SetWarningReporter(_cx, ScriptingCore::reportError); + JS::SetWarningReporter(_cx, warningHandler); // JS_SetGCCallback(_cx, on_garbage_collect, nullptr); if (!JS::InitSelfHostedCode(_cx)) @@ -1033,23 +1047,26 @@ void ScriptingCore::cleanup() _needCleanup = false; } -void ScriptingCore::reportError(JSContext *cx, JSErrorReport *report) +void ScriptingCore::reportError(JSContext *cx, JSErrorReport *report, JS::HandleValue err) { if (cx && report) { - std::string fileName = report->filename ? report->filename : ""; + std::string fileName = report->filename ? report->filename : ""; int32_t lineno = report->lineno; std::string msg = report->message().c_str(); - -#if CC_TARGET_PLATFORM == CC_PLATFORM_ANDROID - __android_log_print(ANDROID_LOG_ERROR, "cocos js error:", "%s line:%u msg:%s", - fileName.c_str(), report->lineno, msg.c_str()); -#else - cocos2d::log("%s:%u:%s\n", fileName.c_str(), report->lineno, msg.c_str()); -#endif - // Should clear pending exception, otherwise it will trigger infinite loop - if (JS_IsExceptionPending(cx)) { - JS_ClearPendingException(cx); + CCLOGERROR("JS Exception: %s, file: %s, lineno: %u\n", msg.c_str(), fileName.c_str(), lineno); + + std::string stackStr = ""; + if (err.isObject()) + { + JS::RootedObject errObj(cx, err.toObjectOrNull()); + JS::RootedValue stack(cx); + if (JS_GetProperty(cx, errObj, "stack", &stack) && stack.isString()) + { + JS::RootedString jsstackStr(cx, stack.toString()); + stackStr = JS_EncodeStringToUTF8(cx, jsstackStr); + CCLOGERROR("Stack: %s\n", stackStr.c_str()); + } } JS::AutoValueVector valArr(cx); @@ -1059,6 +1076,8 @@ void ScriptingCore::reportError(JSContext *cx, JSErrorReport *report) valArr.append(JS::Int32Value(lineno)); std_string_to_jsval(cx, msg, &argv); valArr.append(argv); + std_string_to_jsval(cx, stackStr, &argv); + valArr.append(argv); JS::HandleValueArray args(valArr); JS::RootedValue rval(cx); @@ -1963,15 +1982,8 @@ void handlePendingException(JSContext *cx) JS::RootedObject errObj(cx, err.toObjectOrNull()); JSErrorReport *report = JS_ErrorFromException(cx, errObj); - CCLOGERROR("JS Exception: %s, file: %s, lineno: %u\n", report->message().c_str(), report->filename, report->lineno); - JS::RootedValue stack(cx); - if (JS_GetProperty(cx, errObj, "stack", &stack) && stack.isString()) - { - JS::RootedString jsstackStr(cx, stack.toString()); - char *stackStr = JS_EncodeStringToUTF8(cx, jsstackStr); - CCLOGERROR("Stack: %s\n", stackStr); - } + ScriptingCore::reportError(cx, report, err); } } diff --git a/native/cocos/scripting/js-bindings/manual/ScriptingCore.h b/native/cocos/scripting/js-bindings/manual/ScriptingCore.h index 5a70e28eda..bc966c4acf 100644 --- a/native/cocos/scripting/js-bindings/manual/ScriptingCore.h +++ b/native/cocos/scripting/js-bindings/manual/ScriptingCore.h @@ -409,7 +409,7 @@ public: * @param message @~english The error message * @param report @~english The js error report object */ - static void reportError(JSContext *cx, JSErrorReport *report); + static void reportError(JSContext *cx, JSErrorReport *report, JS::HandleValue err); /**@~english * Log something to the js context using CCLog.