Skip to content

Commit

Permalink
feat(core): refactor napi and jsi
Browse files Browse the repository at this point in the history
  • Loading branch information
churchill-zhang authored and zoomchan-cxj committed Mar 16, 2023
1 parent 8ce3f00 commit 8d7d6b8
Show file tree
Hide file tree
Showing 85 changed files with 3,622 additions and 4,029 deletions.
1 change: 0 additions & 1 deletion android/sdk/src/main/jni/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,6 @@ set(SOURCE_SET
src/bridge/java2js.cc
src/bridge/js2java.cc
src/bridge/runtime.cc
src/bridge/serializer.cc
src/jni/convert_utils.cc
src/jni/exception_handler.cc
src/jni/java_turbo_module.cc
Expand Down
2 changes: 1 addition & 1 deletion android/sdk/src/main/jni/include/bridge/js2java.h
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
namespace hippy {
namespace bridge {

void CallJava(hippy::napi::CBDataTuple* data);
void CallJava(const hippy::napi::CallbackInfo& info, int32_t runtime_id);

} // namespace bridge
} // namespace hippy
15 changes: 8 additions & 7 deletions android/sdk/src/main/jni/include/bridge/runtime.h
Original file line number Diff line number Diff line change
Expand Up @@ -29,13 +29,14 @@
#include <memory>

#include "core/core.h"
#include "jni/turbo_module_runtime.h"
#include "jni/java_turbo_module.h"
#include "jni/scoped_java_ref.h"
#include "v8/interrupt_queue.h"

class Runtime {
public:
using Bridge = hippy::Bridge;
using CtxValue = hippy::napi::CtxValue;
#ifndef V8_WITHOUT_INSPECTOR
using V8InspectorContext = hippy::inspector::V8InspectorContext;
#endif
Expand Down Expand Up @@ -65,12 +66,12 @@ class Runtime {
}
inline std::shared_ptr<V8InspectorContext> GetInspectorContext() { return inspector_context_; }
#endif
inline std::shared_ptr<TurboModuleRuntime> GetTurboModuleRuntime() {
return turbo_module_runtime_;
inline std::shared_ptr<JavaRef> GetTurboManager() {
return turbo_manager_;
}
inline void SetTurboModuleRuntime(
std::shared_ptr<TurboModuleRuntime> turbo_module_runtime) {
turbo_module_runtime_ = turbo_module_runtime;

inline void SetTurboModuleManager(std::shared_ptr<JavaRef> turbo_manager) {
turbo_manager_ = turbo_manager;
}

inline std::any GetData(uint8_t slot) {
Expand Down Expand Up @@ -111,11 +112,11 @@ class Runtime {
std::shared_ptr<Scope> scope_;
std::shared_ptr<hippy::napi::CtxValue> bridge_func_;
int32_t id_;
std::shared_ptr<TurboModuleRuntime> turbo_module_runtime_;
std::unordered_map<uint32_t, std::any> slot_;
std::shared_ptr<hippy::InterruptQueue> interrupt_queue_;
std::function<size_t(void*, size_t, size_t)> near_heap_limit_cb_;
#ifndef V8_WITHOUT_INSPECTOR
std::shared_ptr<V8InspectorContext> inspector_context_;
#endif
std::shared_ptr<JavaRef> turbo_manager_;
};
2 changes: 1 addition & 1 deletion android/sdk/src/main/jni/include/hippy.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
#include "bridge/java2js.h"
#include "bridge/js2java.h"
#include "bridge/runtime.h"
#include "bridge/serializer.h"
#include "core/vm/v8/serializer.h"
#include "jni/exception_handler.h"
#include "jni/jni_env.h"
#include "jni/jni_register.h"
Expand Down
31 changes: 16 additions & 15 deletions android/sdk/src/main/jni/include/jni/convert_utils.h
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,6 @@ class ConvertUtils {
public:
using Ctx = hippy::napi::Ctx;
using CtxValue = hippy::napi::CtxValue;
using TurboEnv = hippy::napi::TurboEnv;

static bool Init();

Expand All @@ -60,50 +59,51 @@ class ConvertUtils {
const std::string &method_signature);

static std::tuple<bool, std::string, std::shared_ptr<JNIArgs>> ConvertJSIArgsToJNIArgs(
TurboEnv &turbo_env,
const std::shared_ptr<Ctx>& ctx,
const std::string &module_name,
const std::string &method_name,
const std::vector<std::string> &method_arg_types,
const std::vector<std::shared_ptr<CtxValue>> &arg_values);

static std::tuple<bool, std::string, std::shared_ptr<CtxValue>> ConvertMethodResultToJSValue(
TurboEnv &turbo_env,
const jobject &obj,
const std::shared_ptr<Ctx>& ctx,
const std::shared_ptr<JavaRef> &obj,
const MethodInfo &method_info,
const jvalue *args);
const jvalue *args,
const std::shared_ptr<Scope>& scope);

static std::tuple<bool, std::string, jobject> ToJObject(TurboEnv &turbo_env,
static std::tuple<bool, std::string, jobject> ToJObject(const std::shared_ptr<Ctx>& ctx,
const std::shared_ptr<CtxValue> &value);

static std::tuple<bool, std::string, jobject> ToHippyMap(
TurboEnv &turbo_env,
const std::shared_ptr<Ctx>& ctx,
const std::shared_ptr<CtxValue> &value);

static std::tuple<bool, std::string, jobject> ToHippyArray(
TurboEnv &turbo_env,
const std::shared_ptr<Ctx>& ctx,
const std::shared_ptr<CtxValue> &value);

static std::tuple<bool, std::string, std::shared_ptr<CtxValue>> ToJsValueInArray(
TurboEnv &turbo_env,
const std::shared_ptr<Ctx>& ctx,
jobject array,
int index);

static std::tuple<bool, std::string, std::shared_ptr<CtxValue>> ToJsArray(
TurboEnv &turbo_env,
const std::shared_ptr<Ctx>& ctx,
jobject array);

static std::tuple<bool, std::string, std::shared_ptr<CtxValue>> ToJsMap(
TurboEnv &turbo_env,
const std::shared_ptr<Ctx>& ctx,
jobject map);

static std::tuple<bool, std::string, bool> HandleBasicType(
TurboEnv &turbo_env,
const std::shared_ptr<Ctx>& ctx,
const std::string &type,
jvalue &j_args,
const std::shared_ptr<CtxValue> &value);

static std::tuple<bool, std::string, bool> HandleObjectType(
TurboEnv &turbo_env,
const std::shared_ptr<Ctx>& ctx,
const std::string &module_name,
const std::string &method_name,
const std::string &type,
Expand All @@ -115,9 +115,10 @@ class ConvertUtils {
const std::string &method_map_str);

static std::shared_ptr<CtxValue> ToHostObject(
TurboEnv &turbo_env,
const std::shared_ptr<Ctx>& ctx,
jobject &j_obj,
std::string name);
std::string name,
std::shared_ptr<Scope> scope);
};

static jclass hippy_array_clazz;
Expand Down
49 changes: 36 additions & 13 deletions android/sdk/src/main/jni/include/jni/java_turbo_module.h
Original file line number Diff line number Diff line change
Expand Up @@ -31,31 +31,54 @@
#include "convert_utils.h"
#include "scoped_java_ref.h"

class JavaTurboModule : public hippy::napi::HippyTurboModule {
class JavaTurboModule {
public:
JavaTurboModule(const std::string &name, std::shared_ptr<JavaRef> &impl);
using Ctx = hippy::napi::Ctx;
using CtxValue = hippy::napi::CtxValue;
using FuncWrapper = hippy::napi::FuncWrapper;
using PropertyDescriptor = hippy::napi::PropertyDescriptor;

~JavaTurboModule();
struct TurboWrapper {
JavaTurboModule* module;
std::shared_ptr<CtxValue> name;
std::unique_ptr<FuncWrapper> func_wrapper;

TurboWrapper(JavaTurboModule* module, const std::shared_ptr<CtxValue>& name) {
this->module = module;
this->name = name;
this->func_wrapper = nullptr;
}

void SetFunctionWrapper(std::unique_ptr<FuncWrapper> wrapper) {
func_wrapper = std::move(wrapper);
}
};

JavaTurboModule(const std::string& name, std::shared_ptr<JavaRef>& impl, const std::shared_ptr<Ctx>& ctx);

std::shared_ptr<JavaRef> impl_;

std::shared_ptr<JavaRef> impl_j_clazz_;

std::string name;

std::unique_ptr<hippy::napi::FuncWrapper> wrapper_holder_;

// methodName, signature
std::unordered_map<std::string, MethodInfo> method_map_;

virtual std::shared_ptr<hippy::napi::CtxValue> InvokeJavaMethod(
hippy::napi::TurboEnv &turbo_env,
const std::shared_ptr<hippy::napi::CtxValue> &prop_name,
const std::shared_ptr<hippy::napi::CtxValue> &this_val,
const std::shared_ptr<hippy::napi::CtxValue> *args,
size_t count);
std::shared_ptr<CtxValue> constructor;
std::unique_ptr<FuncWrapper> constructor_wrapper;
std::unordered_map<std::shared_ptr<CtxValue>, std::unique_ptr<TurboWrapper>> turbo_wrapper_map;
std::unordered_map<std::shared_ptr<CtxValue>, std::shared_ptr<CtxValue>> func_map;
std::shared_ptr<PropertyDescriptor> properties[1];

void InitPropertyMap();
std::shared_ptr<CtxValue> InvokeJavaMethod(
const std::shared_ptr<CtxValue>& prop_name,
const hippy::napi::CallbackInfo& info,
void* data);

virtual std::shared_ptr<hippy::napi::CtxValue> Get(
hippy::napi::TurboEnv &,
const std::shared_ptr<hippy::napi::CtxValue> &prop_name) override;
void InitPropertyMap();

static void Init();

Expand Down
59 changes: 0 additions & 59 deletions android/sdk/src/main/jni/include/jni/turbo_module_runtime.h

This file was deleted.

0 comments on commit 8d7d6b8

Please sign in to comment.