Implement more logic in native (#4131)

Co-authored-by: bofeng.song <bofeng.song@chukong-inc.com>
Co-authored-by: dumganhar <dumganhar@gmail.com>
Co-authored-by: mmyduckx <308303735@qq.com>
Co-authored-by: Patrice Jiang <397136899@qq.com>
Co-authored-by: 江战 <40414978+PatriceJiang@users.noreply.github.com>
This commit is contained in:
minggo
2022-01-07 18:39:40 +08:00
committed by GitHub
parent 38b03565f1
commit 5410a48a58
614 changed files with 98121 additions and 23824 deletions
@@ -24,9 +24,9 @@
****************************************************************************/
#include "cocos/bindings/manual/JavaScriptJavaBridge.h"
#include "cocos/application/ApplicationManager.h"
#include "cocos/base/UTF8.h"
#include "cocos/bindings/manual/jsb_conversions.h"
#include "cocos/application/ApplicationManager.h"
#if CC_PLATFORM == CC_PLATFORM_ANDROID
#include <android/log.h>
@@ -44,7 +44,7 @@
#ifndef ORG_JAVABRIDGE_CLASS_NAME
#define ORG_JAVABRIDGE_CLASS_NAME com_cocos_lib_CocosJavascriptJavaBridge
#endif
#define JNI_JSJAVABRIDGE(FUNC) JNI_METHOD1(ORG_JAVABRIDGE_CLASS_NAME, FUNC)
#define JNI_JSJAVABRIDGE(FUNC) JNI_METHOD1(ORG_JAVABRIDGE_CLASS_NAME, FUNC)
#define JSJ_ERR_OK (0)
#define JSJ_ERR_TYPE_NOT_SUPPORT (-1)
#define JSJ_ERR_INVALID_SIGNATURES (-2)
@@ -81,10 +81,9 @@ public:
class CallInfo {
public:
CallInfo(const char *className, const char *methodName, const char *methodSig)
: _mClassName(className),
_mMethodName(methodName),
_mMethodSig(methodSig)
{
: _mClassName(className),
_mMethodName(methodName),
_mMethodSig(methodSig) {
memset(&_mRet, 0, sizeof(_mRet));
_mValid = validateMethodSig() && getMethodInfo();
}
@@ -126,7 +125,7 @@ public:
}
}
JNIEnv *getEnv() const{
JNIEnv *getEnv() const {
return _mEnv;
}
@@ -173,17 +172,17 @@ public:
};
static bool convertReturnValue(ReturnValue retValue, ValueType type, se::Value *ret);
};
using JsCallback = std::function<void(const std::string&, const std::string&)>;
class ScriptNativeBridge{
using JsCallback = std::function<void(const std::string &, const std::string &)>;
class ScriptNativeBridge {
public:
void callByNative(const std::string& arg0, const std::string& arg1);
inline void setCallback(const JsCallback& cb){
void callByNative(const std::string &arg0, const std::string &arg1);
inline void setCallback(const JsCallback &cb) {
_callback = cb;
}
static ScriptNativeBridge* bridgeCxxInstance;
se::Value jsCb;
static ScriptNativeBridge *bridgeCxxInstance;
se::Value jsCb;
private:
JsCallback _callback{nullptr}; // NOLINT(readability-identifier-naming)
};
@@ -216,7 +215,7 @@ Java_com_cocos_lib_JsbBridge_nativeSendToScript(JNIEnv *env, jclass clazz, jstri
}
} // extern "C"
ScriptNativeBridge* ScriptNativeBridge::bridgeCxxInstance{nullptr};
ScriptNativeBridge *ScriptNativeBridge::bridgeCxxInstance{nullptr};
JavaScriptJavaBridge::CallInfo::~CallInfo() {
if (_mReturnType == ValueType::STRING && _mRet.stringValue) {
@@ -477,9 +476,7 @@ bool JavaScriptJavaBridge::convertReturnValue(ReturnValue retValue, ValueType ty
se::Class *__jsb_JavaScriptJavaBridge_class = nullptr; // NOLINT
static bool JavaScriptJavaBridge_finalize(se::State &s) { //NOLINT(readability-identifier-naming)
auto *cobj = static_cast<JavaScriptJavaBridge *>(s.nativeThisObject());
delete cobj;
static bool JavaScriptJavaBridge_finalize(se::State &s) { //NOLINT(readability-identifier-naming, misc-unused-parameters)
return true;
}
SE_BIND_FINALIZE_FUNC(JavaScriptJavaBridge_finalize)
@@ -500,13 +497,13 @@ static bool JavaScriptJavaBridge_callStaticMethod(se::State &s) { //NOLINT(reada
std::string clsName;
std::string methodName;
std::string methodSig;
ok = seval_to_std_string(args[0], &clsName);
ok = sevalue_to_native(args[0], &clsName);
SE_PRECONDITION2(ok, false, "Converting class name failed!");
ok = seval_to_std_string(args[1], &methodName);
ok = sevalue_to_native(args[1], &methodName);
SE_PRECONDITION2(ok, false, "Converting method name failed!");
ok = seval_to_std_string(args[2], &methodSig);
ok = sevalue_to_native(args[2], &methodSig);
SE_PRECONDITION2(ok, false, "Converting method signature failed!");
JavaScriptJavaBridge::CallInfo call(clsName.c_str(), methodName.c_str(), methodSig.c_str());
@@ -531,13 +528,13 @@ static bool JavaScriptJavaBridge_callStaticMethod(se::State &s) { //NOLINT(reada
std::string clsName;
std::string methodName;
std::string methodSig;
ok = seval_to_std_string(args[0], &clsName);
ok = sevalue_to_native(args[0], &clsName);
SE_PRECONDITION2(ok, false, "Converting class name failed!");
ok = seval_to_std_string(args[1], &methodName);
ok = sevalue_to_native(args[1], &methodName);
SE_PRECONDITION2(ok, false, "Converting method name failed!");
ok = seval_to_std_string(args[2], &methodSig);
ok = sevalue_to_native(args[2], &methodSig);
SE_PRECONDITION2(ok, false, "Converting method signature failed!");
JavaScriptJavaBridge::CallInfo call(clsName.c_str(), methodName.c_str(), methodSig.c_str());
@@ -550,7 +547,7 @@ static bool JavaScriptJavaBridge_callStaticMethod(se::State &s) { //NOLINT(reada
switch (call.argumentTypeAtIndex(i)) {
case JavaScriptJavaBridge::ValueType::INTEGER: {
int integer = 0;
seval_to_int32(args[index], &integer);
sevalue_to_native(args[index], &integer);
jargs[i].i = integer;
break;
}
@@ -562,7 +559,7 @@ static bool JavaScriptJavaBridge_callStaticMethod(se::State &s) { //NOLINT(reada
}
case JavaScriptJavaBridge::ValueType::FLOAT: {
float floatNumber = 0.0F;
seval_to_float(args[index], &floatNumber);
sevalue_to_native(args[index], &floatNumber);
jargs[i].f = floatNumber;
break;
}
@@ -576,7 +573,7 @@ static bool JavaScriptJavaBridge_callStaticMethod(se::State &s) { //NOLINT(reada
jargs[i].l = nullptr;
} else {
std::string str;
seval_to_std_string(args[index], &str);
sevalue_to_native(args[index], &str);
jargs[i].l = call.getEnv()->NewStringUTF(str.c_str());
toReleaseObjects.push_back(jargs[i].l);
}
@@ -612,8 +609,7 @@ static bool JavaScriptJavaBridge_callStaticMethod(se::State &s) { //NOLINT(reada
}
SE_BIND_FUNC(JavaScriptJavaBridge_callStaticMethod)
static bool ScriptNativeBridge_getCallback(se::State &s){ //NOLINT(readability-identifier-naming)
static bool ScriptNativeBridge_getCallback(se::State &s) { //NOLINT(readability-identifier-naming)
auto *cobj = static_cast<ScriptNativeBridge *>(s.nativeThisObject());
assert(cobj == ScriptNativeBridge::bridgeCxxInstance);
s.rval() = cobj->jsCb;
@@ -622,31 +618,30 @@ static bool ScriptNativeBridge_getCallback(se::State &s){ //NOLINT(readability-i
}
SE_BIND_PROP_GET(ScriptNativeBridge_getCallback)
static bool ScriptNativeBridge_setCallback(se::State &s){ //NOLINT(readability-identifier-naming)
static bool ScriptNativeBridge_setCallback(se::State &s) { //NOLINT(readability-identifier-naming)
auto *cobj = static_cast<ScriptNativeBridge *>(s.nativeThisObject());
assert(cobj == ScriptNativeBridge::bridgeCxxInstance);
const auto &args = s.args();
se::Value jsFunc = args[0];
cobj->jsCb = jsFunc;
if(jsFunc.isNullOrUndefined())
{
const auto &args = s.args();
se::Value jsFunc = args[0];
cobj->jsCb = jsFunc;
if (jsFunc.isNullOrUndefined()) {
cobj->setCallback(nullptr);
}
else{
} else {
assert(jsFunc.isObject() && jsFunc.toObject()->isFunction());
s.thisObject()->attachObject(jsFunc.toObject());
cobj->setCallback([jsFunc](const std::string& arg0, const std::string& arg1){
cobj->setCallback([jsFunc](const std::string &arg0, const std::string &arg1) {
se::AutoHandleScope hs;
se::ValueArray args;
se::ValueArray args;
args.push_back(se::Value(arg0));
if(!arg1.empty()) {
if (!arg1.empty()) {
args.push_back(se::Value(arg1));
}
jsFunc.toObject()->call(args, nullptr);
});
}
return true;
}SE_BIND_PROP_SET(ScriptNativeBridge_setCallback)
}
SE_BIND_PROP_SET(ScriptNativeBridge_setCallback)
static bool ScriptNativeBridge_sendToNative(se::State &s) { //NOLINT(readability-identifier-naming)
const auto &args = s.args();
@@ -654,11 +649,11 @@ static bool ScriptNativeBridge_sendToNative(se::State &s) { //NOLINT(readability
if (argc >= 1 && argc < 3) {
bool ok = false;
std::string arg0;
ok = seval_to_std_string(args[0], &arg0);
ok = sevalue_to_native(args[0], &arg0);
SE_PRECONDITION2(ok, false, "Converting arg0 failed!");
std::string arg1;
if (argc == 2) {
ok = seval_to_std_string(args[1], &arg1);
ok = sevalue_to_native(args[1], &arg1);
SE_PRECONDITION2(ok, false, "Converting arg1 failed!");
}
callPlatformStringMethod(arg0, arg1);
@@ -716,12 +711,10 @@ bool register_script_native_bridge(se::Object *obj) { //NOLINT(readability-ident
return true;
}
void callPlatformStringMethod(const std::string &arg0, const std::string &arg1) {
cc::JniHelper::callStaticVoidMethod(
"com/cocos/lib/JsbBridge", "callByScript", arg0, arg1);
cc::JniHelper::callStaticVoidMethod(
"com/cocos/lib/JsbBridge", "callByScript", arg0, arg1);
}
void ScriptNativeBridge::callByNative(const std::string& arg0, const std::string& arg1){
void ScriptNativeBridge::callByNative(const std::string &arg0, const std::string &arg1) {
_callback(arg0, arg1);
}