Skip to content

Commit

Permalink
fix(jsc): fix JSCCtxValue crash after destroy engine
Browse files Browse the repository at this point in the history
  • Loading branch information
etkmao authored and zealotchen0 committed Aug 9, 2023
1 parent 5dc53d9 commit 6bef64a
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 5 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

0 comments on commit 6bef64a

Please sign in to comment.