From 7dbc12439cfac6d0344f47fb06f4e056342179c7 Mon Sep 17 00:00:00 2001 From: etkmao Date: Wed, 4 Sep 2024 20:22:04 +0800 Subject: [PATCH] fix(ios): fix object_data_map crash when jsc_vm and jsc_ctx released --- driver/js/include/driver/napi/jsc/jsc_ctx.h | 2 ++ driver/js/include/driver/vm/jsc/jsc_vm.h | 1 - driver/js/src/napi/jsc/jsc_ctx.cc | 29 +++------------------ 3 files changed, 6 insertions(+), 26 deletions(-) diff --git a/driver/js/include/driver/napi/jsc/jsc_ctx.h b/driver/js/include/driver/napi/jsc/jsc_ctx.h index d8d6e12e74f..d33210c3066 100644 --- a/driver/js/include/driver/napi/jsc/jsc_ctx.h +++ b/driver/js/include/driver/napi/jsc/jsc_ctx.h @@ -212,6 +212,8 @@ class JSCCtx : public Ctx { bool is_exception_handled_; std::unordered_map> class_definition_map_; std::weak_ptr vm_; + + std::unordered_map> constructor_data_holder_; }; inline footstone::string_view ToStrView(JSStringRef str) { diff --git a/driver/js/include/driver/vm/jsc/jsc_vm.h b/driver/js/include/driver/vm/jsc/jsc_vm.h index d235163e76a..2d2f25896dd 100644 --- a/driver/js/include/driver/vm/jsc/jsc_vm.h +++ b/driver/js/include/driver/vm/jsc/jsc_vm.h @@ -46,7 +46,6 @@ class JSCVM : public VM, public std::enable_shared_from_this { JSContextGroupRelease(vm_); } - std::unordered_map>> constructor_data_holder_; JSContextGroupRef vm_; static void SaveConstructorDataPtr(void* ptr); diff --git a/driver/js/src/napi/jsc/jsc_ctx.cc b/driver/js/src/napi/jsc/jsc_ctx.cc index dd88e02b860..d01cb4e7590 100644 --- a/driver/js/src/napi/jsc/jsc_ctx.cc +++ b/driver/js/src/napi/jsc/jsc_ctx.cc @@ -65,11 +65,7 @@ JSCCtx::JSCCtx(JSContextGroupRef group, std::weak_ptr vm): vm_(vm) { JSCCtx::~JSCCtx() { JSGlobalContextRelease(context_); - auto vm = vm_.lock(); - FOOTSTONE_CHECK(vm); - auto jsc_vm = std::static_pointer_cast(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()); } @@ -969,30 +965,13 @@ void* JSCCtx::GetPrivateData(const std::shared_ptr& object) { } void JSCCtx::SaveConstructorData(std::unique_ptr constructor_data) { - auto vm = vm_.lock(); - FOOTSTONE_CHECK(vm); - auto jsc_vm = std::static_pointer_cast(vm); - auto& holder = jsc_vm->constructor_data_holder_; - auto it = holder.find(this); - if (it == holder.end()) { - holder[this] = std::unordered_map>{}; - } 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 JSCCtx::GetClassPrototype(JSClassRef ref) { - auto vm = vm_.lock(); - FOOTSTONE_CHECK(vm); - auto jsc_vm = std::static_pointer_cast(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;