Skip to content

Commit

Permalink
Merge branch 'main' into fix/callFunction
Browse files Browse the repository at this point in the history
  • Loading branch information
siguangli authored Aug 8, 2023
2 parents f385074 + a4ff02e commit 2c1165a
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 6 deletions.
1 change: 1 addition & 0 deletions driver/js/include/driver/scope.h
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,7 @@ struct ClassTemplate {
string_view name;
size_t size = SIZE_OF<T>;
std::unordered_map<void*, std::shared_ptr<T>> holder_map;
std::vector<std::shared_ptr<CtxValue>> holder_ctx_values;
};

class Scope : public std::enable_shared_from_this<Scope> {
Expand Down
7 changes: 6 additions & 1 deletion driver/js/src/engine.cc
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,12 @@ Engine::Engine()

Engine::~Engine() {
FOOTSTONE_DLOG(INFO) << "~Engine";
class_template_holder_map_.clear();
for(auto& [key, template_map] : class_template_holder_map_) {
auto animation_template = std::any_cast<std::shared_ptr<ClassTemplate<CubicBezierAnimation>>>(template_map["Animation"]);
animation_template->holder_ctx_values.clear();
auto animation_set_template = std::any_cast<std::shared_ptr<ClassTemplate<AnimationSet>>>(template_map["AnimationSet"]);
animation_set_template->holder_ctx_values.clear();
}
}

void Engine::AsyncInitialize(std::shared_ptr<TaskRunner> js,
Expand Down
20 changes: 16 additions & 4 deletions driver/js/src/modules/animation_module.cc
Original file line number Diff line number Diff line change
Expand Up @@ -537,16 +537,22 @@ RegisterAnimation(const std::weak_ptr<Scope>& weak_scope) {
exception = context->CreateException("cb is not a function");
return nullptr;
}
auto cb = [weak_scope, func] { // run in js thread
std::weak_ptr<CtxValue> weak_func = func;
auto cb = [weak_scope, weak_func] { // run in js thread
auto scope = weak_scope.lock();
if (!scope) {
return;
}
auto context = scope->GetContext();
context->CallFunction(func, context->GetGlobalObject(), 0, nullptr);
auto func = weak_func.lock();
if (func) {
context->CallFunction(func, context->GetGlobalObject(), 0, nullptr);
}
};
animation->AddEventListener(StringViewUtils::ToStdString(StringViewUtils::ConvertEncoding(
event_name, string_view::Encoding::Utf8).utf8_value()), std::move(cb));
auto class_template_ptr = std::any_cast<std::shared_ptr<ClassTemplate<CubicBezierAnimation>>>(scope->GetClassTemplate("Animation"));
class_template_ptr->holder_ctx_values.emplace_back(func);
return nullptr;
};
class_template.functions.emplace_back(std::move(add_event_listener_func_def));
Expand Down Expand Up @@ -809,16 +815,22 @@ RegisterAnimationSet(const std::weak_ptr<Scope>& weak_scope) {
exception = context->CreateException("cb is not a function");
return nullptr;
}
auto cb = [weak_scope, func] {
std::weak_ptr<CtxValue> weak_func = func;
auto cb = [weak_scope, weak_func] {
auto scope = weak_scope.lock();
if (!scope) {
return;
}
auto context = scope->GetContext();
context->CallFunction(func, context->GetGlobalObject(), 0, nullptr);
auto func = weak_func.lock();
if (func) {
context->CallFunction(func, context->GetGlobalObject(), 0, nullptr);
}
};
animation_set->AddEventListener(StringViewUtils::ToStdString(StringViewUtils::ConvertEncoding(
event_name, string_view::Encoding::Utf8).utf8_value()), std::move(cb));
auto class_template_ptr = std::any_cast<std::shared_ptr<ClassTemplate<AnimationSet>>>(scope->GetClassTemplate("AnimationSet"));
class_template_ptr->holder_ctx_values.emplace_back(func);
return nullptr;
};
def.functions.emplace_back(std::move(add_event_listener_func_def));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,10 @@ void CallFunction(JNIEnv* j_env,
std::any scope_object;
auto scope_id = footstone::checked_numeric_cast<jint, uint32_t>(j_scope_id);
auto flag = hippy::global_data_holder.Find(scope_id, scope_object);
FOOTSTONE_CHECK(flag);
if (!flag) {
FOOTSTONE_LOG(ERROR) << "scope can not found, scope id = " << scope_id << "!!!";
return;
}
auto scope = std::any_cast<std::shared_ptr<Scope>>(scope_object);
JsDriverUtils::CallJs(action_name, scope,
[callback](CALLFUNCTION_CB_STATE state, const string_view& msg) {
Expand Down

0 comments on commit 2c1165a

Please sign in to comment.