[3.3] reflection.callStaticlMethod() throw exception (#3589)

* add exceptions

* linter
This commit is contained in:
江战
2021-06-02 09:41:41 +08:00
committed by GitHub
parent 441e739c8e
commit bac054d066
3 changed files with 56 additions and 12 deletions
@@ -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<ssize_t>(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<ssize_t>(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<v8::String> message = v8::String::NewFromUtf8(_isolate, errorMessage.data()).ToLocalChecked();
v8::Local<v8::Value> error = v8::Exception::Error(message);
_isolate->ThrowException(error);
}
void ScriptEngine::setExceptionCallback(const ExceptionCallback &cb) {
_nativeExceptionCallback = cb;
}
@@ -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.
*/
@@ -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;
}