Changeset 247346 in webkit
- Timestamp:
- Jul 11, 2019 2:42:22 AM (5 years ago)
- Location:
- trunk/Source/JavaScriptCore
- Files:
-
- 6 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/Source/JavaScriptCore/API/JSContext.mm
r244323 r247346 86 86 87 87 [self ensureWrapperMap]; 88 89 toJSGlobalObject(m_context)->setAPIWrapper((__bridge void*)self); 88 [m_virtualMachine addContext:self forGlobalContextRef:m_context]; 90 89 91 90 return self; … … 94 93 - (void)dealloc 95 94 { 96 toJSGlobalObject(m_context)->setAPIWrapper((__bridge void*)nil);97 95 m_exception.clear(); 98 96 JSGlobalContextRelease(m_context); … … 311 309 }; 312 310 313 toJSGlobalObject(m_context)->setAPIWrapper((__bridge void*)self);311 [m_virtualMachine addContext:self forGlobalContextRef:m_context]; 314 312 315 313 return self; … … 361 359 - (JSWrapperMap *)wrapperMap 362 360 { 363 return toJS GlobalObject(m_context)->wrapperMap();361 return toJS(m_context)->lexicalGlobalObject()->wrapperMap(); 364 362 } 365 363 … … 372 370 + (JSContext *)contextWithJSGlobalContextRef:(JSGlobalContextRef)globalContext 373 371 { 374 JSContext *context = (__bridge JSContext *)toJSGlobalObject(globalContext)->apiWrapper(); 372 JSVirtualMachine *virtualMachine = [JSVirtualMachine virtualMachineWithContextGroupRef:toRef(&toJS(globalContext)->vm())]; 373 JSContext *context = [virtualMachine contextForGlobalContextRef:globalContext]; 375 374 if (!context) 376 375 context = [[[JSContext alloc] initWithGlobalContextRef:globalContext] autorelease]; -
trunk/Source/JavaScriptCore/API/JSVirtualMachine.mm
r246578 r247346 42 42 #import <wtf/Lock.h> 43 43 44 static NSMapTable *globalWrapperCache = 0; 45 46 static Lock wrapperCacheMutex; 47 48 static void initWrapperCache() 49 { 50 ASSERT(!globalWrapperCache); 51 NSPointerFunctionsOptions keyOptions = NSPointerFunctionsOpaqueMemory | NSPointerFunctionsOpaquePersonality; 52 NSPointerFunctionsOptions valueOptions = NSPointerFunctionsWeakMemory | NSPointerFunctionsObjectPersonality; 53 globalWrapperCache = [[NSMapTable alloc] initWithKeyOptions:keyOptions valueOptions:valueOptions capacity:0]; 54 } 55 56 static NSMapTable *wrapperCache() 57 { 58 if (!globalWrapperCache) 59 initWrapperCache(); 60 return globalWrapperCache; 61 } 62 63 @interface JSVMWrapperCache : NSObject 64 + (void)addWrapper:(JSVirtualMachine *)wrapper forJSContextGroupRef:(JSContextGroupRef)group; 65 + (JSVirtualMachine *)wrapperForJSContextGroupRef:(JSContextGroupRef)group; 66 @end 67 68 @implementation JSVMWrapperCache 69 70 + (void)addWrapper:(JSVirtualMachine *)wrapper forJSContextGroupRef:(JSContextGroupRef)group 71 { 72 std::lock_guard<Lock> lock(wrapperCacheMutex); 73 NSMapInsert(wrapperCache(), group, (__bridge void*)wrapper); 74 } 75 76 + (JSVirtualMachine *)wrapperForJSContextGroupRef:(JSContextGroupRef)group 77 { 78 std::lock_guard<Lock> lock(wrapperCacheMutex); 79 return (__bridge JSVirtualMachine *)NSMapGet(wrapperCache(), group); 80 } 81 82 @end 83 44 84 @implementation JSVirtualMachine { 45 85 JSContextGroupRef m_group; 46 86 Lock m_externalDataMutex; 87 NSMapTable *m_contextCache; 47 88 NSMapTable *m_externalObjectGraph; 48 89 NSMapTable *m_externalRememberedSet; … … 66 107 m_group = JSContextGroupRetain(group); 67 108 109 NSPointerFunctionsOptions keyOptions = NSPointerFunctionsOpaqueMemory | NSPointerFunctionsOpaquePersonality; 110 NSPointerFunctionsOptions valueOptions = NSPointerFunctionsWeakMemory | NSPointerFunctionsObjectPersonality; 111 m_contextCache = [[NSMapTable alloc] initWithKeyOptions:keyOptions valueOptions:valueOptions capacity:0]; 112 68 113 NSPointerFunctionsOptions weakIDOptions = NSPointerFunctionsWeakMemory | NSPointerFunctionsObjectPersonality; 69 114 NSPointerFunctionsOptions strongIDOptions = NSPointerFunctionsStrongMemory | NSPointerFunctionsObjectPersonality; … … 72 117 NSPointerFunctionsOptions integerOptions = NSPointerFunctionsOpaqueMemory | NSPointerFunctionsIntegerPersonality; 73 118 m_externalRememberedSet = [[NSMapTable alloc] initWithKeyOptions:weakIDOptions valueOptions:integerOptions capacity:0]; 74 75 toJS(group)->m_apiWrapper = (__bridge void*)self;119 120 [JSVMWrapperCache addWrapper:self forJSContextGroupRef:group]; 76 121 77 122 return self; … … 80 125 - (void)dealloc 81 126 { 82 toJS(m_group)->m_apiWrapper = (__bridge void*)nil;83 127 JSContextGroupRelease(m_group); 128 [m_contextCache release]; 84 129 [m_externalObjectGraph release]; 85 130 [m_externalRememberedSet release]; … … 193 238 + (JSVirtualMachine *)virtualMachineWithContextGroupRef:(JSContextGroupRef)group 194 239 { 195 auto* vm = toJS(group); 196 JSVirtualMachine *virtualMachine = (__bridge JSVirtualMachine *)vm->m_apiWrapper; 240 JSVirtualMachine *virtualMachine = [JSVMWrapperCache wrapperForJSContextGroupRef:group]; 197 241 if (!virtualMachine) 198 242 virtualMachine = [[[JSVirtualMachine alloc] initWithContextGroupRef:group] autorelease]; 199 243 return virtualMachine; 244 } 245 246 - (JSContext *)contextForGlobalContextRef:(JSGlobalContextRef)globalContext 247 { 248 return (__bridge JSContext *)NSMapGet(m_contextCache, globalContext); 249 } 250 251 - (void)addContext:(JSContext *)wrapper forGlobalContextRef:(JSGlobalContextRef)globalContext 252 { 253 NSMapInsert(m_contextCache, globalContext, (__bridge void*)wrapper); 200 254 } 201 255 … … 264 318 { 265 319 @autoreleasepool { 266 JSVirtualMachine *virtualMachine = (__bridge JSVirtualMachine *)vm.m_apiWrapper;320 JSVirtualMachine *virtualMachine = [JSVMWrapperCache wrapperForJSContextGroupRef:toRef(&vm)]; 267 321 if (!virtualMachine) 268 322 return; … … 302 356 { 303 357 @autoreleasepool { 304 JSVirtualMachine *virtualMachine = (__bridge JSVirtualMachine *)vm.m_apiWrapper;358 JSVirtualMachine *virtualMachine = [JSVMWrapperCache wrapperForJSContextGroupRef:toRef(&vm)]; 305 359 if (!virtualMachine) 306 360 return; -
trunk/Source/JavaScriptCore/API/JSVirtualMachineInternal.h
r246578 r247346 45 45 + (JSVirtualMachine *)virtualMachineWithContextGroupRef:(JSContextGroupRef)group; 46 46 47 - (JSContext *)contextForGlobalContextRef:(JSGlobalContextRef)globalContext; 48 - (void)addContext:(JSContext *)wrapper forGlobalContextRef:(JSGlobalContextRef)globalContext; 47 49 - (JSC::VM&)vm; 48 50 -
trunk/Source/JavaScriptCore/ChangeLog
r247296 r247346 1 2019-07-11 Yusuke Suzuki <ysuzuki@apple.com> 2 3 Unreviewed, revert r243617. 4 https://bugs.webkit.org/show_bug.cgi?id=196341 5 6 Mark pointed out that JSVirtualMachine can be gone in the other thread while we are executing GC constraint-solving. 7 This patch does not account that JavaScriptCore.framework is multi-thread safe: JSVirtualMachine wrapper can be destroyed, 8 and [JSVirtualMachine dealloc] can be executed in any threads while the VM is retained and used in the other thread (e.g. 9 destroyed from AutoReleasePool in some thread). 10 11 * API/JSContext.mm: 12 (-[JSContext initWithVirtualMachine:]): 13 (-[JSContext dealloc]): 14 (-[JSContext initWithGlobalContextRef:]): 15 (-[JSContext wrapperMap]): 16 (+[JSContext contextWithJSGlobalContextRef:]): 17 * API/JSVirtualMachine.mm: 18 (initWrapperCache): 19 (wrapperCache): 20 (+[JSVMWrapperCache addWrapper:forJSContextGroupRef:]): 21 (+[JSVMWrapperCache wrapperForJSContextGroupRef:]): 22 (-[JSVirtualMachine initWithContextGroupRef:]): 23 (-[JSVirtualMachine dealloc]): 24 (+[JSVirtualMachine virtualMachineWithContextGroupRef:]): 25 (-[JSVirtualMachine contextForGlobalContextRef:]): 26 (-[JSVirtualMachine addContext:forGlobalContextRef:]): 27 (scanExternalObjectGraph): 28 (scanExternalRememberedSet): 29 * API/JSVirtualMachineInternal.h: 30 * runtime/JSGlobalObject.h: 31 (JSC::JSGlobalObject::setWrapperMap): 32 (JSC::JSGlobalObject::setAPIWrapper): Deleted. 33 (JSC::JSGlobalObject::apiWrapper const): Deleted. 34 * runtime/VM.h: 35 1 36 2019-07-10 Tadeu Zagallo <tzagallo@apple.com> 2 37 -
trunk/Source/JavaScriptCore/runtime/JSGlobalObject.h
r246565 r247346 1011 1011 JSWrapperMap* wrapperMap() const { return m_wrapperMap.get(); } 1012 1012 void setWrapperMap(JSWrapperMap* map) { m_wrapperMap = map; } 1013 void setAPIWrapper(void* apiWrapper) { m_apiWrapper = apiWrapper; }1014 void* apiWrapper() const { return m_apiWrapper; }1015 1013 #endif 1016 1014 #ifdef JSC_GLIB_API_ENABLED … … 1055 1053 #if JSC_OBJC_API_ENABLED 1056 1054 RetainPtr<JSWrapperMap> m_wrapperMap; 1057 void* m_apiWrapper { nullptr };1058 1055 #endif 1059 1056 #ifdef JSC_GLIB_API_ENABLED -
trunk/Source/JavaScriptCore/runtime/VM.h
r246572 r247346 834 834 #endif 835 835 836 #if JSC_OBJC_API_ENABLED837 void* m_apiWrapper { nullptr };838 #endif839 840 836 JS_EXPORT_PRIVATE void resetDateCache(); 841 837
Note: See TracChangeset
for help on using the changeset viewer.