JSVirtualMachine.mm 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256
  1. /*
  2. * Copyright (C) 2013 Apple Inc. All rights reserved.
  3. *
  4. * Redistribution and use in source and binary forms, with or without
  5. * modification, are permitted provided that the following conditions
  6. * are met:
  7. * 1. Redistributions of source code must retain the above copyright
  8. * notice, this list of conditions and the following disclaimer.
  9. * 2. Redistributions in binary form must reproduce the above copyright
  10. * notice, this list of conditions and the following disclaimer in the
  11. * documentation and/or other materials provided with the distribution.
  12. *
  13. * THIS SOFTWARE IS PROVIDED BY APPLE INC. ``AS IS'' AND ANY
  14. * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  15. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  16. * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR
  17. * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
  18. * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
  19. * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
  20. * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
  21. * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  22. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  23. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  24. */
  25. #include "config.h"
  26. #import "JavaScriptCore.h"
  27. #if JSC_OBJC_API_ENABLED
  28. #import "APICast.h"
  29. #import "APIShims.h"
  30. #import "JSVirtualMachine.h"
  31. #import "JSVirtualMachineInternal.h"
  32. #import "JSWrapperMap.h"
  33. static NSMapTable *globalWrapperCache = 0;
  34. static Mutex& wrapperCacheLock()
  35. {
  36. DEFINE_STATIC_LOCAL(Mutex, mutex, ());
  37. return mutex;
  38. }
  39. static void initWrapperCache()
  40. {
  41. ASSERT(!globalWrapperCache);
  42. NSPointerFunctionsOptions keyOptions = NSPointerFunctionsOpaqueMemory | NSPointerFunctionsOpaquePersonality;
  43. NSPointerFunctionsOptions valueOptions = NSPointerFunctionsWeakMemory | NSPointerFunctionsObjectPersonality;
  44. globalWrapperCache = [[NSMapTable alloc] initWithKeyOptions:keyOptions valueOptions:valueOptions capacity:0];
  45. }
  46. static NSMapTable *wrapperCache()
  47. {
  48. if (!globalWrapperCache)
  49. initWrapperCache();
  50. return globalWrapperCache;
  51. }
  52. @interface JSVMWrapperCache : NSObject
  53. + (void)addWrapper:(JSVirtualMachine *)wrapper forJSContextGroupRef:(JSContextGroupRef)group;
  54. + (JSVirtualMachine *)wrapperForJSContextGroupRef:(JSContextGroupRef)group;
  55. @end
  56. @implementation JSVMWrapperCache
  57. + (void)addWrapper:(JSVirtualMachine *)wrapper forJSContextGroupRef:(JSContextGroupRef)group
  58. {
  59. MutexLocker locker(wrapperCacheLock());
  60. NSMapInsert(wrapperCache(), group, wrapper);
  61. }
  62. + (JSVirtualMachine *)wrapperForJSContextGroupRef:(JSContextGroupRef)group
  63. {
  64. MutexLocker locker(wrapperCacheLock());
  65. return static_cast<JSVirtualMachine *>(NSMapGet(wrapperCache(), group));
  66. }
  67. @end
  68. @implementation JSVirtualMachine {
  69. JSContextGroupRef m_group;
  70. NSMapTable *m_contextCache;
  71. NSMapTable *m_externalObjectGraph;
  72. }
  73. - (id)init
  74. {
  75. JSContextGroupRef group = JSContextGroupCreate();
  76. self = [self initWithContextGroupRef:group];
  77. // The extra JSContextGroupRetain is balanced here.
  78. JSContextGroupRelease(group);
  79. return self;
  80. }
  81. - (id)initWithContextGroupRef:(JSContextGroupRef)group
  82. {
  83. self = [super init];
  84. if (!self)
  85. return nil;
  86. m_group = JSContextGroupRetain(group);
  87. NSPointerFunctionsOptions keyOptions = NSPointerFunctionsOpaqueMemory | NSPointerFunctionsOpaquePersonality;
  88. NSPointerFunctionsOptions valueOptions = NSPointerFunctionsWeakMemory | NSPointerFunctionsObjectPersonality;
  89. m_contextCache = [[NSMapTable alloc] initWithKeyOptions:keyOptions valueOptions:valueOptions capacity:0];
  90. NSPointerFunctionsOptions weakIDOptions = NSPointerFunctionsWeakMemory | NSPointerFunctionsObjectPersonality;
  91. NSPointerFunctionsOptions strongIDOptions = NSPointerFunctionsStrongMemory | NSPointerFunctionsObjectPersonality;
  92. m_externalObjectGraph = [[NSMapTable alloc] initWithKeyOptions:weakIDOptions valueOptions:strongIDOptions capacity:0];
  93. [JSVMWrapperCache addWrapper:self forJSContextGroupRef:group];
  94. return self;
  95. }
  96. - (void)dealloc
  97. {
  98. JSContextGroupRelease(m_group);
  99. [m_contextCache release];
  100. [m_externalObjectGraph release];
  101. [super dealloc];
  102. }
  103. static id getInternalObjcObject(id object)
  104. {
  105. if ([object isKindOfClass:[JSManagedValue class]]) {
  106. JSValue* value = [static_cast<JSManagedValue *>(object) value];
  107. id temp = tryUnwrapObjcObject([value.context JSGlobalContextRef], [value JSValueRef]);
  108. if (temp)
  109. return temp;
  110. return object;
  111. }
  112. if ([object isKindOfClass:[JSValue class]]) {
  113. JSValue *value = static_cast<JSValue *>(object);
  114. object = tryUnwrapObjcObject([value.context JSGlobalContextRef], [value JSValueRef]);
  115. }
  116. return object;
  117. }
  118. - (void)addManagedReference:(id)object withOwner:(id)owner
  119. {
  120. object = getInternalObjcObject(object);
  121. owner = getInternalObjcObject(owner);
  122. if (!object || !owner)
  123. return;
  124. JSC::APIEntryShim shim(toJS(m_group));
  125. NSMapTable *ownedObjects = [m_externalObjectGraph objectForKey:owner];
  126. if (!ownedObjects) {
  127. NSPointerFunctionsOptions weakIDOptions = NSPointerFunctionsWeakMemory | NSPointerFunctionsObjectPersonality;
  128. NSPointerFunctionsOptions integerOptions = NSPointerFunctionsOpaqueMemory | NSPointerFunctionsIntegerPersonality;
  129. ownedObjects = [[NSMapTable alloc] initWithKeyOptions:weakIDOptions valueOptions:integerOptions capacity:1];
  130. [m_externalObjectGraph setObject:ownedObjects forKey:owner];
  131. [ownedObjects release];
  132. }
  133. NSMapInsert(ownedObjects, object, reinterpret_cast<void*>(reinterpret_cast<size_t>(NSMapGet(ownedObjects, object)) + 1));
  134. }
  135. - (void)removeManagedReference:(id)object withOwner:(id)owner
  136. {
  137. object = getInternalObjcObject(object);
  138. owner = getInternalObjcObject(owner);
  139. if (!object || !owner)
  140. return;
  141. JSC::APIEntryShim shim(toJS(m_group));
  142. NSMapTable *ownedObjects = [m_externalObjectGraph objectForKey:owner];
  143. if (!ownedObjects)
  144. return;
  145. size_t count = reinterpret_cast<size_t>(NSMapGet(ownedObjects, object));
  146. if (count > 1) {
  147. NSMapInsert(ownedObjects, object, reinterpret_cast<void*>(count - 1));
  148. return;
  149. }
  150. if (count == 1)
  151. NSMapRemove(ownedObjects, object);
  152. if (![ownedObjects count])
  153. [m_externalObjectGraph removeObjectForKey:owner];
  154. }
  155. @end
  156. @implementation JSVirtualMachine(Internal)
  157. JSContextGroupRef getGroupFromVirtualMachine(JSVirtualMachine *virtualMachine)
  158. {
  159. return virtualMachine->m_group;
  160. }
  161. + (JSVirtualMachine *)virtualMachineWithContextGroupRef:(JSContextGroupRef)group
  162. {
  163. JSVirtualMachine *virtualMachine = [JSVMWrapperCache wrapperForJSContextGroupRef:group];
  164. if (!virtualMachine)
  165. virtualMachine = [[[JSVirtualMachine alloc] initWithContextGroupRef:group] autorelease];
  166. return virtualMachine;
  167. }
  168. - (JSContext *)contextForGlobalContextRef:(JSGlobalContextRef)globalContext
  169. {
  170. return static_cast<JSContext *>(NSMapGet(m_contextCache, globalContext));
  171. }
  172. - (void)addContext:(JSContext *)wrapper forGlobalContextRef:(JSGlobalContextRef)globalContext
  173. {
  174. NSMapInsert(m_contextCache, globalContext, wrapper);
  175. }
  176. - (NSMapTable *)externalObjectGraph
  177. {
  178. return m_externalObjectGraph;
  179. }
  180. @end
  181. void scanExternalObjectGraph(JSC::VM& vm, JSC::SlotVisitor& visitor, void* root)
  182. {
  183. @autoreleasepool {
  184. JSVirtualMachine *virtualMachine = [JSVMWrapperCache wrapperForJSContextGroupRef:toRef(&vm)];
  185. if (!virtualMachine)
  186. return;
  187. NSMapTable *externalObjectGraph = [virtualMachine externalObjectGraph];
  188. Vector<void*> stack;
  189. stack.append(root);
  190. while (!stack.isEmpty()) {
  191. void* nextRoot = stack.last();
  192. stack.removeLast();
  193. if (visitor.containsOpaqueRootTriState(nextRoot) == TrueTriState)
  194. continue;
  195. visitor.addOpaqueRoot(nextRoot);
  196. NSMapTable *ownedObjects = [externalObjectGraph objectForKey:static_cast<id>(nextRoot)];
  197. id ownedObject;
  198. NSEnumerator *enumerator = [ownedObjects keyEnumerator];
  199. while ((ownedObject = [enumerator nextObject])) {
  200. ASSERT(reinterpret_cast<size_t>(NSMapGet(ownedObjects, ownedObject)) == 1);
  201. stack.append(static_cast<void*>(ownedObject));
  202. }
  203. }
  204. }
  205. }
  206. #endif