Skip to content

Commit

Permalink
fix(ios): fix object_data_map crash when jsc_vm and jsc_ctx released …
Browse files Browse the repository at this point in the history
…in 2 threads
  • Loading branch information
etkmao committed Sep 4, 2024
1 parent 90fdf95 commit 8c871e9
Show file tree
Hide file tree
Showing 3 changed files with 6 additions and 26 deletions.
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

0 comments on commit 8c871e9

Please sign in to comment.