From 2b112bd7a45ed4a06a68c55e3a4ecee6e0801e87 Mon Sep 17 00:00:00 2001 From: James Chen Date: Fri, 1 Sep 2017 15:24:43 +0800 Subject: [PATCH] Adds ScriptEngine::setExceptionCallback for Spidermonkey, and fixes a memory leak after using JS_EncodeStringToUTF8. --- .../js-bindings/jswrapper/sm/ScriptEngine.cpp | 27 ++++++++++++++++--- .../js-bindings/jswrapper/sm/ScriptEngine.hpp | 4 +++ 2 files changed, 27 insertions(+), 4 deletions(-) diff --git a/native/cocos/scripting/js-bindings/jswrapper/sm/ScriptEngine.cpp b/native/cocos/scripting/js-bindings/jswrapper/sm/ScriptEngine.cpp index 0325226deb..e9d06e53a0 100644 --- a/native/cocos/scripting/js-bindings/jswrapper/sm/ScriptEngine.cpp +++ b/native/cocos/scripting/js-bindings/jswrapper/sm/ScriptEngine.cpp @@ -66,7 +66,7 @@ namespace se { LOGD("JS: %s\n", buffer); - JS_free(cx, (void*)buffer); + JS_free(cx, buffer); } } args.rval().setUndefined(); @@ -230,6 +230,7 @@ namespace se { , _isValid(false) , _isInCleanup(false) , _nodeEventListener(nullptr) + , _exceptionCallback(nullptr) { bool ok = JS_Init(); assert(ok); @@ -719,18 +720,36 @@ namespace se { JS::RootedObject exceptionObj(_cx, exceptionValue.toObjectOrNull()); JSErrorReport* report = JS_ErrorFromException(_cx, exceptionObj); const char* fileName = report->filename != nullptr ? report->filename : "(no filename)"; - LOGE("JS ERROR: %s, file: %s, lineno: %u\n", report->message().c_str(), fileName, report->lineno); + std::string exceptionStr = report->message().c_str(); + exceptionStr += ", file: "; + exceptionStr += fileName; + char lineBuf[50] = {0}; + snprintf(lineBuf, sizeof(lineBuf), "%u", report->lineno); + exceptionStr += lineBuf; JS::RootedValue stack(_cx); if (JS_GetProperty(_cx, exceptionObj, "stack", &stack) && stack.isString()) { JS::RootedString jsstackStr(_cx, stack.toString()); - char *stackStr = JS_EncodeStringToUTF8(_cx, jsstackStr); - LOGE("Stack: %s\n", stackStr); + char* stackStr = JS_EncodeStringToUTF8(_cx, jsstackStr); + exceptionStr += "\nSTACK:\n"; + exceptionStr += stackStr; + JS_free(_cx, stackStr); + } + + LOGE("ERROR: %s\n", exceptionStr.c_str()); + if (_exceptionCallback != nullptr) + { + _exceptionCallback(exceptionStr.c_str()); } } } + void ScriptEngine::setExceptionCallback(const ExceptionCallback& cb) + { + _exceptionCallback = cb; + } + } // namespace se { #endif // SCRIPT_ENGINE_SM diff --git a/native/cocos/scripting/js-bindings/jswrapper/sm/ScriptEngine.hpp b/native/cocos/scripting/js-bindings/jswrapper/sm/ScriptEngine.hpp index f5aca3ce91..25b6253450 100644 --- a/native/cocos/scripting/js-bindings/jswrapper/sm/ScriptEngine.hpp +++ b/native/cocos/scripting/js-bindings/jswrapper/sm/ScriptEngine.hpp @@ -101,6 +101,9 @@ namespace se { void clearException(); + using ExceptionCallback = std::function; + void setExceptionCallback(const ExceptionCallback& cb); + const std::chrono::steady_clock::time_point& getStartTime() const { return _startTime; } void _retainScriptObject(void* owner, void* target); @@ -147,6 +150,7 @@ namespace se { std::vector> _beforeCleanupHookArray; std::vector> _afterCleanupHookArray; + ExceptionCallback _exceptionCallback; // name ~> JSScript map std::unordered_map _filenameScriptMap; };