From bac054d06652f0d7fc024f22179efd7b7cdabb65 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=B1=9F=E6=88=98?= <40414978+PatriceJiang@users.noreply.github.com> Date: Wed, 2 Jun 2021 09:41:41 +0800 Subject: [PATCH] [3.3] reflection.callStaticlMethod() throw exception (#3589) * add exceptions * linter --- .../bindings/jswrapper/v8/ScriptEngine.cpp | 19 ++++++--- .../bindings/jswrapper/v8/ScriptEngine.h | 9 ++++- .../bindings/manual/JavaScriptJavaBridge.cpp | 40 +++++++++++++++++-- 3 files changed, 56 insertions(+), 12 deletions(-) diff --git a/native/cocos/bindings/jswrapper/v8/ScriptEngine.cpp b/native/cocos/bindings/jswrapper/v8/ScriptEngine.cpp index cc692250e8..ffce34b0be 100644 --- a/native/cocos/bindings/jswrapper/v8/ScriptEngine.cpp +++ b/native/cocos/bindings/jswrapper/v8/ScriptEngine.cpp @@ -705,7 +705,7 @@ void ScriptEngine::garbageCollect() { SE_LOGD("GC end ..., (js->native map) size: %d, all objects: %d\n", (int)NativePtrToObjectMap::size(), objSize); } -bool ScriptEngine::isGarbageCollecting() { +bool ScriptEngine::isGarbageCollecting() const { return _isGarbageCollecting; } @@ -726,7 +726,7 @@ bool ScriptEngine::evalString(const char *script, ssize_t length /* = -1 */, Val assert(script != nullptr); if (length < 0) { - length = strlen(script); + length = static_cast(strlen(script)); } if (fileName == nullptr) { @@ -814,8 +814,8 @@ const ScriptEngine::FileOperationDelegate &ScriptEngine::getFileOperationDelegat } bool ScriptEngine::saveByteCodeToFile(const std::string &path, const std::string &pathBc) { - bool success = false; - auto fu = cc::FileUtils::getInstance(); + bool success = false; + auto *fu = cc::FileUtils::getInstance(); if (pathBc.length() > 3 && pathBc.substr(pathBc.length() - 3) != ".bc") { SE_LOGE("ScriptEngine::generateByteCode bytecode file path should endwith \".bc\"\n"); @@ -871,7 +871,7 @@ bool ScriptEngine::saveByteCodeToFile(const std::string &path, const std::string } bool ScriptEngine::runByteCodeFile(const std::string &pathBc, Value *ret /* = nullptr */) { - auto fu = cc::FileUtils::getInstance(); + auto *fu = cc::FileUtils::getInstance(); cc::Data cachedData; fu->getContents(pathBc, &cachedData); @@ -973,7 +973,7 @@ bool ScriptEngine::runScript(const std::string &path, Value *ret /* = nullptr */ std::string scriptBuffer = _fileOperationDelegate.onGetStringFromFile(path); if (!scriptBuffer.empty()) { - return evalString(scriptBuffer.c_str(), scriptBuffer.length(), ret, path.c_str()); + return evalString(scriptBuffer.c_str(), static_cast(scriptBuffer.length()), ret, path.c_str()); } SE_LOGE("ScriptEngine::runScript script %s, buffer is empty!\n", path.c_str()); @@ -984,6 +984,13 @@ void ScriptEngine::clearException() { //IDEA: } +void ScriptEngine::throwException(const std::string &errorMessage) { + v8::HandleScope scope(_isolate); + v8::Local message = v8::String::NewFromUtf8(_isolate, errorMessage.data()).ToLocalChecked(); + v8::Local error = v8::Exception::Error(message); + _isolate->ThrowException(error); +} + void ScriptEngine::setExceptionCallback(const ExceptionCallback &cb) { _nativeExceptionCallback = cb; } diff --git a/native/cocos/bindings/jswrapper/v8/ScriptEngine.h b/native/cocos/bindings/jswrapper/v8/ScriptEngine.h index ab016a940b..1ce9e007be 100644 --- a/native/cocos/bindings/jswrapper/v8/ScriptEngine.h +++ b/native/cocos/bindings/jswrapper/v8/ScriptEngine.h @@ -233,7 +233,7 @@ public: * @brief Tests whether script engine is doing garbage collection. * @return true if it's in garbage collection, otherwise false. */ - bool isGarbageCollecting(); + bool isGarbageCollecting() const; /** * @brief Performs a JavaScript garbage collection. @@ -244,7 +244,7 @@ public: * @brief Tests whether script engine is being cleaned up. * @return true if it's in cleaning up, otherwise false. */ - bool isInCleanup() { return _isInCleanup; } + bool isInCleanup() const { return _isInCleanup; } /** * @brief Tests whether script engine is valid. @@ -252,6 +252,11 @@ public: */ bool isValid() const; + /** + * @brief Throw JS exception + */ + void throwException(const std::string &errorMessage); + /** * @brief Clears all exceptions. */ diff --git a/native/cocos/bindings/manual/JavaScriptJavaBridge.cpp b/native/cocos/bindings/manual/JavaScriptJavaBridge.cpp index a0e44075f9..7c19a40ebf 100644 --- a/native/cocos/bindings/manual/JavaScriptJavaBridge.cpp +++ b/native/cocos/bindings/manual/JavaScriptJavaBridge.cpp @@ -122,14 +122,42 @@ public: } ~CallInfo(); - bool isValid() { + bool isValid() const { return _mValid; } - int getErrorCode() { + int getErrorCode() const { return _mError; } + void tryThrowJSException() const { + if (_mError != JSJ_ERR_OK) { + se::ScriptEngine::getInstance()->throwException(getErrorMessage()); + } + } + + const char *getErrorMessage() const { + switch (_mError) { + case JSJ_ERR_TYPE_NOT_SUPPORT: + return "argument type is not supported"; + case JSJ_ERR_INVALID_SIGNATURES: + return "invalid signature"; + case JSJ_ERR_METHOD_NOT_FOUND: + return "method not found"; + case JSJ_ERR_EXCEPTION_OCCURRED: + return "excpected occurred"; + case JSJ_ERR_VM_THREAD_DETACHED: + return "vm thread detached"; + case JSJ_ERR_VM_FAILURE: + return "vm failure"; + case JSJ_ERR_CLASS_NOT_FOUND: + return "class not found"; + case JSJ_ERR_OK: + default: + return "NOERROR"; + } + } + JNIEnv *getEnv() { return _mEnv; } @@ -138,7 +166,7 @@ public: return _mArgumentsType.at(index); } - int getArgumentsCount() { + int getArgumentsCount() const { return _mArgumentsCount; } @@ -409,7 +437,7 @@ bool JavaScriptJavaBridge::convertReturnValue(ReturnValue retValue, ValueType ty ret->setInt32(retValue.intValue); break; case JavaScriptJavaBridge::ValueType::LONG: - ret->setLong(retValue.longValue); + ret->setLong(retValue.longValue); //NOLINT(bugprone-narrowing-conversions) break; case JavaScriptJavaBridge::ValueType::FLOAT: ret->setFloat(retValue.floatValue); @@ -471,12 +499,14 @@ static bool JavaScriptJavaBridge_callStaticMethod(se::State &s) { //NOLINT(reada ok = call.execute(); int errorCode = call.getErrorCode(); if (!ok || errorCode < 0) { + call.tryThrowJSException(); SE_REPORT_ERROR("call result code: %d", call.getErrorCode()); return false; } JavaScriptJavaBridge::convertReturnValue(call.getReturnValue(), call.getReturnValueType(), &s.rval()); return true; } + call.tryThrowJSException(); SE_REPORT_ERROR("JavaScriptJavaBridge::CallInfo isn't valid!"); return false; } @@ -550,6 +580,7 @@ static bool JavaScriptJavaBridge_callStaticMethod(se::State &s) { //NOLINT(reada delete[] jargs; int errorCode = call.getErrorCode(); if (!ok || errorCode < 0) { + call.tryThrowJSException(); SE_REPORT_ERROR("js_JSJavaBridge : call result code: %d", errorCode); return false; } @@ -557,6 +588,7 @@ static bool JavaScriptJavaBridge_callStaticMethod(se::State &s) { //NOLINT(reada JavaScriptJavaBridge::convertReturnValue(call.getReturnValue(), call.getReturnValueType(), &s.rval()); return true; } + call.tryThrowJSException(); SE_REPORT_ERROR("call valid: %d, call.getArgumentsCount()= %d", call.isValid(), call.getArgumentsCount()); return false; }