|
10 | 10 | #include "Helpers.h" |
11 | 11 | #include "Caches.h" |
12 | 12 | #include "Interop.h" |
| 13 | +#include "Runtime.h" |
13 | 14 |
|
14 | 15 | using namespace v8; |
15 | 16 |
|
|
254 | 255 | /// in order to make both of them destroyable/GC-able. When the JavaScript object is GC-ed we release the native counterpart as well. |
255 | 256 | void (*retain)(id, SEL) = (void (*)(id, SEL))FindNotOverridenMethod(extendedClass, @selector(retain)); |
256 | 257 | IMP newRetain = imp_implementationWithBlock(^(id self) { |
257 | | - if ([self retainCount] == 1) { |
258 | | - auto it = cache->Instances.find(self); |
259 | | - if (it != cache->Instances.end()) { |
260 | | - v8::Locker locker(isolate); |
261 | | - Isolate::Scope isolate_scope(isolate); |
262 | | - HandleScope handle_scope(isolate); |
263 | | - Local<Value> value = it->second->Get(isolate); |
264 | | - BaseDataWrapper* wrapper = tns::GetValue(isolate, value); |
265 | | - if (wrapper != nullptr && wrapper->Type() == WrapperType::ObjCObject) { |
266 | | - ObjCDataWrapper* objcWrapper = static_cast<ObjCDataWrapper*>(wrapper); |
267 | | - objcWrapper->GcProtect(); |
268 | | - } |
269 | | - } |
270 | | - } |
271 | | - |
272 | | - return retain(self, @selector(retain)); |
| 258 | + if ([self retainCount] == 1) { |
| 259 | + auto it = cache->Instances.find(self); |
| 260 | + if (it != cache->Instances.end()) { |
| 261 | + v8::Locker locker(isolate); |
| 262 | + Isolate::Scope isolate_scope(isolate); |
| 263 | + HandleScope handle_scope(isolate); |
| 264 | + Local<Value> value = it->second->Get(isolate); |
| 265 | + BaseDataWrapper* wrapper = tns::GetValue(isolate, value); |
| 266 | + if (wrapper != nullptr && wrapper->Type() == WrapperType::ObjCObject) { |
| 267 | + ObjCDataWrapper* objcWrapper = static_cast<ObjCDataWrapper*>(wrapper); |
| 268 | + objcWrapper->GcProtect(); |
| 269 | + } |
| 270 | + } |
| 271 | + } |
| 272 | + |
| 273 | + return retain(self, @selector(retain)); |
273 | 274 | }); |
274 | 275 | class_addMethod(extendedClass, @selector(retain), newRetain, "@@:"); |
275 | 276 |
|
276 | 277 | void (*release)(id, SEL) = (void (*)(id, SEL))FindNotOverridenMethod(extendedClass, @selector(release)); |
277 | 278 | IMP newRelease = imp_implementationWithBlock(^(id self) { |
278 | | - if ([self retainCount] == 2) { |
279 | | - auto it = cache->Instances.find(self); |
280 | | - if (it != cache->Instances.end()) { |
281 | | - v8::Locker locker(isolate); |
282 | | - Isolate::Scope isolate_scope(isolate); |
283 | | - HandleScope handle_scope(isolate); |
284 | | - if (it->second != nullptr) { |
285 | | - Local<Value> value = it->second->Get(isolate); |
286 | | - BaseDataWrapper* wrapper = tns::GetValue(isolate, value); |
287 | | - if (wrapper != nullptr && wrapper->Type() == WrapperType::ObjCObject) { |
288 | | - ObjCDataWrapper* objcWrapper = static_cast<ObjCDataWrapper*>(wrapper); |
289 | | - objcWrapper->GcUnprotect(); |
290 | | - } |
291 | | - } |
292 | | - } |
293 | | - } |
294 | | - |
295 | | - release(self, @selector(release)); |
| 279 | + if (!Runtime::IsAlive(isolate)) { |
| 280 | + return; |
| 281 | + } |
| 282 | + |
| 283 | + if ([self retainCount] == 2) { |
| 284 | + auto it = cache->Instances.find(self); |
| 285 | + if (it != cache->Instances.end()) { |
| 286 | + v8::Locker locker(isolate); |
| 287 | + Isolate::Scope isolate_scope(isolate); |
| 288 | + HandleScope handle_scope(isolate); |
| 289 | + if (it->second != nullptr) { |
| 290 | + Local<Value> value = it->second->Get(isolate); |
| 291 | + BaseDataWrapper* wrapper = tns::GetValue(isolate, value); |
| 292 | + if (wrapper != nullptr && wrapper->Type() == WrapperType::ObjCObject) { |
| 293 | + ObjCDataWrapper* objcWrapper = static_cast<ObjCDataWrapper*>(wrapper); |
| 294 | + objcWrapper->GcUnprotect(); |
| 295 | + } |
| 296 | + } |
| 297 | + } |
| 298 | + } |
| 299 | + |
| 300 | + release(self, @selector(release)); |
296 | 301 | }); |
297 | 302 | class_addMethod(extendedClass, @selector(release), newRelease, "v@:"); |
298 | 303 |
|
|
0 commit comments