Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(ios): fix object_data_map crash when jsc_vm and jsc_ctx released … #4017

Merged
merged 1 commit into from
Sep 4, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions driver/js/include/driver/napi/jsc/jsc_ctx.h
Original file line number Diff line number Diff line change
Expand Up @@ -212,6 +212,8 @@ class JSCCtx : public Ctx {
bool is_exception_handled_;
std::unordered_map<string_view, std::shared_ptr<ClassDefinition>> class_definition_map_;
std::weak_ptr<VM> vm_;

std::unordered_map<JSClassRef, std::unique_ptr<ConstructorData>> constructor_data_holder_;
};

inline footstone::string_view ToStrView(JSStringRef str) {
Expand Down
1 change: 0 additions & 1 deletion driver/js/include/driver/vm/jsc/jsc_vm.h
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,6 @@ class JSCVM : public VM, public std::enable_shared_from_this<JSCVM> {
JSContextGroupRelease(vm_);
}

std::unordered_map<void*, std::unordered_map<JSClassRef, std::unique_ptr<ConstructorData>>> constructor_data_holder_;
JSContextGroupRef vm_;

static void SaveConstructorDataPtr(void* ptr);
Expand Down
29 changes: 4 additions & 25 deletions driver/js/src/napi/jsc/jsc_ctx.cc
Original file line number Diff line number Diff line change
Expand Up @@ -65,11 +65,7 @@ JSCCtx::JSCCtx(JSContextGroupRef group, std::weak_ptr<VM> vm): vm_(vm) {

JSCCtx::~JSCCtx() {
JSGlobalContextRelease(context_);
auto vm = vm_.lock();
FOOTSTONE_CHECK(vm);
auto jsc_vm = std::static_pointer_cast<JSCVM>(vm);
auto& holder = jsc_vm->constructor_data_holder_[this];
for (auto& [key, item] : holder) {
for (auto& [key, item] : constructor_data_holder_) {
item->prototype = nullptr;
JSCVM::ClearConstructorDataPtr(item.get());
}
Expand Down Expand Up @@ -969,30 +965,13 @@ void* JSCCtx::GetPrivateData(const std::shared_ptr<CtxValue>& object) {
}

void JSCCtx::SaveConstructorData(std::unique_ptr<ConstructorData> constructor_data) {
auto vm = vm_.lock();
FOOTSTONE_CHECK(vm);
auto jsc_vm = std::static_pointer_cast<JSCVM>(vm);
auto& holder = jsc_vm->constructor_data_holder_;
auto it = holder.find(this);
if (it == holder.end()) {
holder[this] = std::unordered_map<JSClassRef, std::unique_ptr<ConstructorData>>{};
}
JSCVM::SaveConstructorDataPtr(constructor_data.get());
holder[this][constructor_data->class_ref] = std::move(constructor_data);
constructor_data_holder_[constructor_data->class_ref] = std::move(constructor_data);
}

std::shared_ptr<JSCCtxValue> JSCCtx::GetClassPrototype(JSClassRef ref) {
auto vm = vm_.lock();
FOOTSTONE_CHECK(vm);
auto jsc_vm = std::static_pointer_cast<JSCVM>(vm);
auto& holder = jsc_vm->constructor_data_holder_;
auto it = holder.find(this);
if (it == holder.end()) {
return nullptr;
}
auto& class_map = it->second;
auto iterator = class_map.find(ref);
if (iterator == class_map.end()) {
auto iterator = constructor_data_holder_.find(ref);
if (iterator == constructor_data_holder_.end()) {
return nullptr;
}
return iterator->second->prototype;
Expand Down
Loading