Skip to content

Commit

Permalink
refactor(core): refactor dynamic jni
Browse files Browse the repository at this point in the history
  • Loading branch information
churchill-zhang committed Apr 12, 2021
1 parent b6a46f1 commit fdbac85
Show file tree
Hide file tree
Showing 28 changed files with 910 additions and 586 deletions.
Binary file modified android/sdk/libs/arm64-v8a/libhippybridge.so
100755 → 100644
Binary file not shown.
Binary file removed android/sdk/libs/armeabi-v7a/libhippybridge.so
Binary file not shown.
5 changes: 4 additions & 1 deletion android/sdk/src/main/jni/HippyAndroidSdk.vcxproj
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
</ItemGroup>
<ItemGroup>
<None Include=".clang-format" />
<None Include="cpp.hint" />
</ItemGroup>
<ItemGroup>
<ClCompile Include="src\inspector\v8_channel_impl.cc" />
Expand All @@ -28,6 +29,7 @@
<ClCompile Include="src\jni\hippy_buffer.cc" />
<ClCompile Include="src\jni\jni_env.cc" />
<ClCompile Include="src\jni\jni_utils.cc" />
<ClCompile Include="src\jni\jni_register.cc" />
<ClCompile Include="src\jni\runtime.cc" />
<ClCompile Include="src\jni\scoped_java_ref.cc" />
<ClCompile Include="src\jni\uri.cc" />
Expand All @@ -44,6 +46,7 @@
<ClInclude Include="include\jni\hippy_buffer.h" />
<ClInclude Include="include\jni\jni_env.h" />
<ClInclude Include="include\jni\jni_utils.h" />
<ClInclude Include="include\jni\jni_register.h" />
<ClInclude Include="include\jni\runtime.h" />
<ClInclude Include="include\jni\scoped_java_ref.h" />
<ClInclude Include="include\jni\uri.h" />
Expand Down Expand Up @@ -208,4 +211,4 @@
</ItemDefinitionGroup>
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" />
<ImportGroup Label="ExtensionTargets" />
</Project>
</Project>
55 changes: 55 additions & 0 deletions android/sdk/src/main/jni/include/bridge/entry.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
/*
*
* Tencent is pleased to support the open source community by making
* Hippy available.
*
* Copyright (C) 2019 THL A29 Limited, a Tencent company.
* All rights reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*/

#pragma once

#include <jni.h>

namespace hippy {
namespace bridge {

jlong InitInstance(JNIEnv* j_env,
jobject j_object,
jbyteArray j_global_config,
jboolean j_single_thread_mode,
jboolean j_bridge_param_json,
jboolean j_is_dev_module,
jobject j_callback,
jlong j_group_id);

void DestroyInstance(JNIEnv* j_env,
jobject j_object,
jlong j_runtime_id,
jboolean j_single_thread_mode,
jobject j_callback);

jboolean RunScriptFromUri(JNIEnv* j_env,
jobject j_obj,
jstring j_uri,
jobject j_aasset_manager,
jboolean j_can_use_code_cache,
jstring j_code_cache_dir,
jlong j_runtime_id,
jobject j_cb);

} // namespace bridge
} // namespace hippy
40 changes: 40 additions & 0 deletions android/sdk/src/main/jni/include/bridge/java2js.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
/*
*
* Tencent is pleased to support the open source community by making
* Hippy available.
*
* Copyright (C) 2019 THL A29 Limited, a Tencent company.
* All rights reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*/

#pragma once

#include <jni.h>

namespace hippy {
namespace bridge {

void CallFunction(JNIEnv* j_env,
jobject j_obj,
jstring j_action,
jbyteArray j_params,
jint j_offset,
jint j_length,
jlong j_runtime_id,
jobject j_callback);

} // namespace bridge
} // namespace hippy
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,15 @@

#pragma once

#define JNI_NATIVE_METHOD(name, signature, pointer) \
{ \
const_cast<char *>(name), const_cast<char *>(signature), \
reinterpret_cast<void *>(pointer) \
}
#include <jni.h>

#include "core/core.h"

namespace hippy {
namespace bridge {

void CallJava(hippy::napi::CBDataTuple* data);
void CallJavaMethod(jobject obj, jlong value);

} // namespace bridge
} // namespace hippy
7 changes: 5 additions & 2 deletions android/sdk/src/main/jni/include/hippy.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,16 @@

#pragma once

#include "bridge/entry.h"
#include "bridge/java2js.h"
#include "bridge/js2java.h"
#include "bridge/runtime.h"
#include "inspector/v8_channel_impl.h"
#include "inspector/v8_inspector_client_impl.h"
#include "jni/exception_handler.h"
#include "jni/jni_env.h"
#include "jni/jni_register.h"
#include "jni/jni_utils.h"
#include "jni/register.h"
#include "jni/runtime.h"
#include "jni/scoped_java_ref.h"
#include "loader/adr_loader.h"
#include "v8/v8.h"
7 changes: 6 additions & 1 deletion android/sdk/src/main/jni/include/inspector/v8_channel_impl.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,13 @@

#include <memory>

#include "jni/jni_env.h"
#include "jni/jni_utils.h"
#include "jni/scoped_java_ref.h"
#include "v8/v8-inspector.h"

class JNIEnvironment;
namespace hippy {
namespace inspector {

class V8ChannelImpl : public v8_inspector::V8Inspector::Channel {
public:
Expand All @@ -54,3 +56,6 @@ class V8ChannelImpl : public v8_inspector::V8Inspector::Channel {
friend class V8InspectorClientImpl;
std::shared_ptr<JavaRef> bridge_;
};

} // namespace inspector
} // namespace hippy
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,9 @@
#include "jni/scoped_java_ref.h"
#include "v8_channel_impl.h"

namespace hippy {
namespace inspector {

class V8InspectorClientImpl : public v8_inspector::V8InspectorClient {
public:
explicit V8InspectorClientImpl(std::shared_ptr<Scope> scope);
Expand Down Expand Up @@ -100,3 +103,6 @@ class V8InspectorClientImpl : public v8_inspector::V8InspectorClient {
std::unique_ptr<V8ChannelImpl> channel_;
std::unique_ptr<v8_inspector::V8InspectorSession> session_;
};

} // namespace inspector
} // namespace hippy
2 changes: 1 addition & 1 deletion android/sdk/src/main/jni/include/jni/exception_handler.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
#include <iostream>
#include <sstream>

#include "jni/runtime.h"
#include "bridge/runtime.h"

class JNIEnvironment;

Expand Down
36 changes: 17 additions & 19 deletions android/sdk/src/main/jni/include/jni/jni_env.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,37 +24,35 @@

#include <jni.h>

#include "core/core.h"
#include <memory>
#include <mutex>

class JNIEnvironment {
public:
struct JemthodID_Wrapper {
JemthodID_Wrapper() {
call_natives_method_id = nullptr;
report_exception_method_id = nullptr;
inspector_channel_method_id = nullptr;
fetch_resource_method_id = nullptr;
}

jmethodID call_natives_method_id;
jmethodID report_exception_method_id;
jmethodID inspector_channel_method_id;
jmethodID fetch_resource_method_id;
jmethodID call_natives_method_id = nullptr;
jmethodID report_exception_method_id = nullptr;
jmethodID inspector_channel_method_id = nullptr;
jmethodID fetch_resource_method_id = nullptr;
};

public:
static std::shared_ptr<JNIEnvironment> GetInstance();
static bool ClearJEnvException(JNIEnv* env);
static void DestroyInstance();

JNIEnvironment() = default;
~JNIEnvironment() = default;

inline JemthodID_Wrapper GetMethods() { return wrapper_; }
void init(JavaVM* vm, JNIEnv* env);
JNIEnv* AttachCurrentThread();
void DetachCurrentThread();

static bool ClearJEnvException(JNIEnv* env);
static JNIEnvironment* GetInstance();
static void DestroyInstance();
static JNIEnv* AttachCurrentThread();
static void DetachCurrentThread();
private:
static std::shared_ptr<JNIEnvironment> instance_;
static std::mutex mutex_;

public:
JavaVM* jvm_;
JavaVM* j_vm_;
JemthodID_Wrapper wrapper_;
};
83 changes: 83 additions & 0 deletions android/sdk/src/main/jni/include/jni/jni_register.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
/*
*
* Tencent is pleased to support the open source community by making
* Hippy available.
*
* Copyright (C) 2019 THL A29 Limited, a Tencent company.
* All rights reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*/

#pragma once

#include <jni.h>

#include <string>

#include "core/core.h"

class JNIRegisterData {
public:
JNIRegisterData(const char *name, const char *sign, void *pointer);
JNINativeMethod ToJNINativeMethod();

private:
std::string name_;
std::string sign_;
void *pointer_;
};

class JNIRegister {
public:
static std::unique_ptr<JNIRegister> &GetInstance();
static bool RegisterMethods(JNIEnv *j_env);

JNIRegister() = default;
bool RegisterJNIModule(const char *module,
const char *name,
const char *signature,
void *pointer) {
auto it = jni_modules_.find(module);
if (it != jni_modules_.end()) {
jni_modules_[module].push_back({name, signature, pointer});
} else {
jni_modules_[module] = {{name, signature, pointer}};
}
return true;
}

const std::unordered_map<std::string, std::vector<JNIRegisterData>>
&GetJniModules() {
return jni_modules_;
}

private:
std::unordered_map<std::string, std::vector<JNIRegisterData>> jni_modules_;

DISALLOW_COPY_AND_ASSIGN(JNIRegister);
};

#define REGISTER_JNI_INTERNAL(clazz, name, signature, pointer, key) \
auto __REGISTER_JNI_##key = []() { \
JNIRegister::GetInstance()->RegisterJNIModule( \
clazz, name, signature, reinterpret_cast<void *>(pointer)); \
return 0; \
}();

#define REGISTER_JNI_TEMP(clazz, name, signature, pointer, key) \
REGISTER_JNI_INTERNAL(clazz, name, signature, pointer, pointer##key)

#define REGISTER_JNI(clazz, name, signature, pointer) \
REGISTER_JNI_TEMP(clazz, name, signature, pointer, __COUNTER__)
Loading

0 comments on commit fdbac85

Please sign in to comment.