Adds ScriptEngine::setExceptionCallback for Spidermonkey, and fixes a memory leak after using JS_EncodeStringToUTF8.

This commit is contained in:
James Chen
2017-09-01 15:24:43 +08:00
parent cd77d4b567
commit 2b112bd7a4
2 changed files with 27 additions and 4 deletions
@@ -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
@@ -101,6 +101,9 @@ namespace se {
void clearException();
using ExceptionCallback = std::function<void(const char*)>;
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<std::function<void()>> _beforeCleanupHookArray;
std::vector<std::function<void()>> _afterCleanupHookArray;
ExceptionCallback _exceptionCallback;
// name ~> JSScript map
std::unordered_map<std::string, JS::PersistentRootedScript*> _filenameScriptMap;
};