Skip to content

Commit

Permalink
feat(android): add UnloadInstance jni C impl
Browse files Browse the repository at this point in the history
  • Loading branch information
siguangli committed Jul 18, 2022
1 parent a1099ee commit cc04797
Show file tree
Hide file tree
Showing 8 changed files with 66 additions and 48 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -384,12 +384,10 @@ public void destroyModule(ViewGroup rootView) {
mDevSupportManager.detachFromHost(((ContextWrapper) context).getBaseContext());
}
}

if (mEngineContext != null) {
if (mEngineContext.getBridgeManager() != null) {
mEngineContext.getBridgeManager().destroyInstance(rootView.getId());
}
mEngineContext.onInstanceDestroy(rootView.getId());
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@
import com.tencent.mtt.hippy.modules.javascriptmodules.HippyJavaScriptModuleInvocationHandler;
import com.tencent.mtt.hippy.modules.nativemodules.HippyNativeModuleBase;
import com.tencent.mtt.hippy.modules.nativemodules.HippyNativeModuleInfo;
import com.tencent.mtt.hippy.runtime.builtins.JSValue;
import com.tencent.mtt.hippy.runtime.builtins.array.JSDenseArray;
import com.tencent.mtt.hippy.serialization.PrimitiveValueDeserializer;
import com.tencent.mtt.hippy.serialization.compatible.Deserializer;
import com.tencent.mtt.hippy.serialization.nio.reader.BinaryReader;
Expand Down Expand Up @@ -302,8 +304,8 @@ private void doDeserialization(@NonNull Message from) {
params = (HippyCallNativeParams) from.obj;
if (params.moduleName != null && params.moduleName.equals(REMOVE_ROOT_VIEW_MODULE_NAME)
&& params.moduleFunc.equals(REMOVE_ROOT_VIEW_FUNC_NAME)) {
params.paramsValue = bytesToArgument(params.paramsBuffer, false);
Message to = handler.obtainMessage(MSG_CODE_REMOVE_ROOT_VIEW, params);
params.paramsValue = bytesToArgument(params.paramsBuffer, true);
Message to = handler.obtainMessage(MSG_CODE_REMOVE_ROOT_VIEW, params.paramsValue);
handler.sendMessage(to);
return;
}
Expand Down Expand Up @@ -400,13 +402,15 @@ private void onCallNativeFinished(@Nullable HippyCallNativeParams params) {
adapter.onCallNativeFinished(mContext.getComponentName(), params);
}

private void removeRootView(@NonNull final HippyCallNativeParams params) {
private void removeRootView(@NonNull final JSDenseArray roots) {
UIThreadUtils.runOnUiThread(new Runnable() {
@Override
public void run() {
if (params.paramsValue instanceof Integer
|| params.paramsValue instanceof Long) {
mContext.removeRootView((Integer) params.paramsValue);
if (roots.size() > 0) {
Object valueObj = roots.get(0);
if (valueObj instanceof Integer) {
mContext.removeRootView((Integer) valueObj);
}
}
}
});
Expand Down Expand Up @@ -449,7 +453,7 @@ public boolean handleMessage(Message msg) {
onDestroy();
break;
case MSG_CODE_REMOVE_ROOT_VIEW:
removeRootView((HippyCallNativeParams) msg.obj);
removeRootView((JSDenseArray) msg.obj);
break;
default:
break;
Expand Down
4 changes: 2 additions & 2 deletions driver/js/android/src/main/jni/include/bridge/entry.h
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ jlong InitInstance(JNIEnv* j_env,
jstring j_data_dir,
jstring j_ws_url);

void DestroyBridge(JNIEnv* j_env,
void DestroyInstance(JNIEnv* j_env,
jobject j_object,
jlong j_runtime_id,
jboolean j_single_thread_mode,
Expand Down Expand Up @@ -106,7 +106,7 @@ void LoadInstance(JNIEnv* j_env,
jint j_offset,
jint j_length);

void DestroyInstance(JNIEnv* j_env,
void UnloadInstance(JNIEnv* j_env,
__unused jobject j_obj,
jlong j_runtime_id,
jbyteArray j_byte_array,
Expand Down
12 changes: 7 additions & 5 deletions driver/js/android/src/main/jni/src/bridge/entry.cc
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ REGISTER_JNI("com/tencent/mtt/hippy/bridge/HippyBridgeImpl", // NOLINT(cert-err5
REGISTER_JNI("com/tencent/mtt/hippy/bridge/HippyBridgeImpl", // NOLINT(cert-err58-cpp)
"destroy",
"(JZZLcom/tencent/mtt/hippy/bridge/NativeCallback;)V",
DestroyBridge)
DestroyInstance)

REGISTER_JNI("com/tencent/link_supplier/Linker", // NOLINT(cert-err58-cpp)
"doBind",
Expand Down Expand Up @@ -133,7 +133,7 @@ REGISTER_JNI( // NOLINT(cert-err58-cpp)
"com/tencent/mtt/hippy/bridge/HippyBridgeImpl",
"destroyInstance",
"(J[BII)V",
DestroyInstance)
UnloadInstance)

using unicode_string_view = footstone::stringview::unicode_string_view;
using TaskRunner = footstone::runner::TaskRunner;
Expand Down Expand Up @@ -474,7 +474,7 @@ jlong InitInstance(JNIEnv* j_env,
return static_cast<jlong>(runtime_id);
}

void DestroyBridge(__unused JNIEnv* j_env,
void DestroyInstance(__unused JNIEnv* j_env,
__unused jobject j_object,
jlong j_runtime_id,
__unused jboolean j_single_thread_mode,
Expand Down Expand Up @@ -502,13 +502,15 @@ void LoadInstance(JNIEnv* j_env,
std::move(buffer_data));
}

void DestroyInstance(JNIEnv* j_env,
void UnloadInstance(JNIEnv* j_env,
__unused jobject j_obj,
jlong j_runtime_id,
jbyteArray j_byte_array,
jint j_offset,
jint j_length) {

auto buffer_data = JniUtils::AppendJavaByteArrayToBytes(j_env, j_byte_array, j_offset, j_length);
V8BridgeUtils::UnloadInstance(footstone::check::checked_numeric_cast<jlong, int32_t>(j_runtime_id),
std::move(buffer_data));
}


Expand Down
3 changes: 1 addition & 2 deletions driver/js/core/include/core/runtime/v8/v8_bridge_utils.h
Original file line number Diff line number Diff line change
Expand Up @@ -97,8 +97,7 @@ class V8BridgeUtils {
bool,
bytes)>& cb);
static void LoadInstance(int32_t runtime_id, bytes&& buffer_data);
static void UnloadInstance(int32_t runtime_id,
std::function<void(CALL_FUNCTION_CB_STATE, unicode_string_view)> cb);
static void UnloadInstance(int32_t runtime_id, bytes&& buffer_data);
private:
static std::function<void(std::shared_ptr<Runtime>,
unicode_string_view,
Expand Down
2 changes: 2 additions & 0 deletions driver/js/core/include/core/scope.h
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,8 @@ class Scope : public std::enable_shared_from_this<Scope> {

void LoadInstance(const std::shared_ptr<HippyValue>& value);

void UnloadInstance(const std::shared_ptr<HippyValue>& value);

inline std::shared_ptr<TaskRunner> GetTaskRunner() {
return engine_->GetJsTaskRunner();
}
Expand Down
47 changes: 17 additions & 30 deletions driver/js/core/src/runtime/v8/v8_bridge_utils.cc
Original file line number Diff line number Diff line change
Expand Up @@ -646,45 +646,32 @@ void V8BridgeUtils::LoadInstance(int32_t runtime_id, bytes&& buffer_data) {
runner->PostTask(std::move(callback));
}

void V8BridgeUtils::UnloadInstance(
int32_t runtime_id,
std::function<void(CALL_FUNCTION_CB_STATE, unicode_string_view)> cb) {
FOOTSTONE_DLOG(INFO) << "Destroy instance runtime_id = " << runtime_id;
void V8BridgeUtils::UnloadInstance(int32_t runtime_id, bytes&& buffer_data) {
FOOTSTONE_DLOG(INFO) << "UnloadInstance instance runtime_id = " << runtime_id;
std::shared_ptr<Runtime> runtime = Runtime::Find(runtime_id);
if (!runtime) {
FOOTSTONE_DLOG(WARNING) << "Destroy instance failed runtime_id invalid";
FOOTSTONE_DLOG(WARNING) << "UnloadInstance instance failed runtime_id invalid";
return;
}
std::shared_ptr<TaskRunner> runner = runtime->GetEngine()->GetJsTaskRunner();
auto callback = [runtime, cb = std::move(cb)] {
std::shared_ptr<Scope> scope = runtime->GetScope();
auto runner = runtime->GetEngine()->GetJsTaskRunner();
std::weak_ptr<Scope> weak_scope = runtime->GetScope();
auto callback = [weak_scope, buffer_data_ = std::move(buffer_data)] {
std::shared_ptr<Scope> scope = weak_scope.lock();
if (!scope) {
FOOTSTONE_DLOG(WARNING) << "Destroy instance invalid";
return;
}
std::shared_ptr<Ctx> context = scope->GetContext();
if (!runtime->GetBridgeFunc()) {
FOOTSTONE_DLOG(INFO) << "init bridge func";
unicode_string_view name(kHippyBridgeName);
std::shared_ptr<CtxValue> fn = context->GetJsFn(name);
bool is_fn = context->IsFunction(fn);
FOOTSTONE_DLOG(INFO) << "is_fn = " << is_fn;

if (!is_fn) {
cb(CALL_FUNCTION_CB_STATE::NO_METHOD_ERROR, u"hippyBridge not find");
return;
} else {
runtime->SetBridgeFunc(fn);
}
Deserializer deserializer(
reinterpret_cast<const uint8_t*>(buffer_data_.c_str()),
buffer_data_.length());
HippyValue value;
deserializer.ReadHeader();
auto ret = deserializer.ReadValue(value);
if (ret) {
scope->UnloadInstance(std::make_shared<HippyValue>(std::move(value)));
} else {
scope->GetContext()->ThrowException("LoadInstance param error");
}
std::shared_ptr<CtxValue> action_value = context->CreateString(u"destroyInstance");
std::shared_ptr<CtxValue> params = context->CreateNull();

std::shared_ptr<CtxValue> argv[] = {action_value, params};
context->CallFunction(runtime->GetBridgeFunc(), 2, argv);
cb(CALL_FUNCTION_CB_STATE::SUCCESS, "");
};

runner->PostTask(std::move(callback));
}

Expand Down
26 changes: 26 additions & 0 deletions driver/js/core/src/scope.cc
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ using DomNode = hippy::dom::DomNode;

constexpr char kDeallocFuncName[] = "HippyDealloc";
constexpr char kLoadInstanceFuncName[] = "__loadInstance__";
constexpr char kUnLoadInstanceFuncName[] = "__unloadInstance__";
constexpr char kHippyBootstrapJSName[] = "bootstrap.js";
#ifdef ENABLE_INSPECTOR
constexpr char kHippyModuleName[] = "name";
Expand Down Expand Up @@ -409,3 +410,28 @@ void Scope::LoadInstance(const std::shared_ptr<HippyValue>& value) {
runner->PostTask(std::move(cb));
}
}

void Scope::UnloadInstance(const std::shared_ptr<HippyValue>& value) {
std::weak_ptr<Ctx> weak_context = context_;
auto cb = [weak_context, value]() mutable {
std::shared_ptr<Ctx> context = weak_context.lock();
if (context) {
std::shared_ptr<CtxValue> fn = context->GetJsFn(kUnLoadInstanceFuncName);
bool is_fn = context->IsFunction(fn);
FOOTSTONE_DCHECK(is_fn);
if (is_fn) {
auto param = context->CreateCtxValue(value);
std::shared_ptr<CtxValue> argv[] = {param};
context->CallFunction(fn, 1, argv);
} else {
context->ThrowException("Application entry not found");
}
}
};
auto runner = engine_->GetJsTaskRunner();
if (footstone::Worker::IsTaskRunning() && runner == footstone::runner::TaskRunner::GetCurrentTaskRunner()) {
cb();
} else {
runner->PostTask(std::move(cb));
}
}

0 comments on commit cc04797

Please sign in to comment.