2 * Copyright (C) 2013 Apple Inc. All rights reserved.
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
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.
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.
28 #import "JavaScriptCore.h"
30 #if JSC_OBJC_API_ENABLED
33 #import "JSManagedValueInternal.h"
34 #import "JSVirtualMachine.h"
35 #import "JSVirtualMachineInternal.h"
36 #import "JSWrapperMap.h"
37 #import "SlotVisitorInlines.h"
39 #import <wtf/NeverDestroyed.h>
40 #import <wtf/spi/cocoa/NSMapTableSPI.h>
42 static NSMapTable *globalWrapperCache = 0;
44 static std::mutex& wrapperCacheMutex()
46 static NeverDestroyed<std::mutex> mutex;
51 static void initWrapperCache()
53 ASSERT(!globalWrapperCache);
54 NSPointerFunctionsOptions keyOptions = NSPointerFunctionsOpaqueMemory | NSPointerFunctionsOpaquePersonality;
55 NSPointerFunctionsOptions valueOptions = NSPointerFunctionsWeakMemory | NSPointerFunctionsObjectPersonality;
56 globalWrapperCache = [[NSMapTable alloc] initWithKeyOptions:keyOptions valueOptions:valueOptions capacity:0];
59 static NSMapTable *wrapperCache()
61 if (!globalWrapperCache)
63 return globalWrapperCache;
66 @interface JSVMWrapperCache : NSObject
67 + (void)addWrapper:(JSVirtualMachine *)wrapper forJSContextGroupRef:(JSContextGroupRef)group;
68 + (JSVirtualMachine *)wrapperForJSContextGroupRef:(JSContextGroupRef)group;
71 @implementation JSVMWrapperCache
73 + (void)addWrapper:(JSVirtualMachine *)wrapper forJSContextGroupRef:(JSContextGroupRef)group
75 std::lock_guard<std::mutex> lock(wrapperCacheMutex());
76 NSMapInsert(wrapperCache(), group, wrapper);
79 + (JSVirtualMachine *)wrapperForJSContextGroupRef:(JSContextGroupRef)group
81 std::lock_guard<std::mutex> lock(wrapperCacheMutex());
82 return static_cast<JSVirtualMachine *>(NSMapGet(wrapperCache(), group));
87 @implementation JSVirtualMachine {
88 JSContextGroupRef m_group;
89 NSMapTable *m_contextCache;
90 NSMapTable *m_externalObjectGraph;
91 NSMapTable *m_externalRememberedSet;
96 JSContextGroupRef group = JSContextGroupCreate();
97 self = [self initWithContextGroupRef:group];
98 // The extra JSContextGroupRetain is balanced here.
99 JSContextGroupRelease(group);
103 - (instancetype)initWithContextGroupRef:(JSContextGroupRef)group
109 m_group = JSContextGroupRetain(group);
111 NSPointerFunctionsOptions keyOptions = NSPointerFunctionsOpaqueMemory | NSPointerFunctionsOpaquePersonality;
112 NSPointerFunctionsOptions valueOptions = NSPointerFunctionsWeakMemory | NSPointerFunctionsObjectPersonality;
113 m_contextCache = [[NSMapTable alloc] initWithKeyOptions:keyOptions valueOptions:valueOptions capacity:0];
115 NSPointerFunctionsOptions weakIDOptions = NSPointerFunctionsWeakMemory | NSPointerFunctionsObjectPersonality;
116 NSPointerFunctionsOptions strongIDOptions = NSPointerFunctionsStrongMemory | NSPointerFunctionsObjectPersonality;
117 m_externalObjectGraph = [[NSMapTable alloc] initWithKeyOptions:weakIDOptions valueOptions:strongIDOptions capacity:0];
119 NSPointerFunctionsOptions integerOptions = NSPointerFunctionsOpaqueMemory | NSPointerFunctionsIntegerPersonality;
120 m_externalRememberedSet = [[NSMapTable alloc] initWithKeyOptions:weakIDOptions valueOptions:integerOptions capacity:0];
122 [JSVMWrapperCache addWrapper:self forJSContextGroupRef:group];
129 JSContextGroupRelease(m_group);
130 [m_contextCache release];
131 [m_externalObjectGraph release];
132 [m_externalRememberedSet release];
136 static id getInternalObjcObject(id object)
138 if ([object isKindOfClass:[JSManagedValue class]]) {
139 JSValue* value = [static_cast<JSManagedValue *>(object) value];
140 id temp = tryUnwrapObjcObject([value.context JSGlobalContextRef], [value JSValueRef]);
146 if ([object isKindOfClass:[JSValue class]]) {
147 JSValue *value = static_cast<JSValue *>(object);
148 object = tryUnwrapObjcObject([value.context JSGlobalContextRef], [value JSValueRef]);
154 - (bool)isOldExternalObject:(id)object
156 JSC::VM* vm = toJS(m_group);
157 return vm->heap.slotVisitor().containsOpaqueRoot(object);
160 - (void)addExternalRememberedObject:(id)object
162 ASSERT([self isOldExternalObject:object]);
163 [m_externalRememberedSet setObject:[NSNumber numberWithBool:true] forKey:object];
166 - (void)addManagedReference:(id)object withOwner:(id)owner
168 if ([object isKindOfClass:[JSManagedValue class]])
169 [object didAddOwner:owner];
171 object = getInternalObjcObject(object);
172 owner = getInternalObjcObject(owner);
174 if (!object || !owner)
177 JSC::JSLockHolder locker(toJS(m_group));
178 if ([self isOldExternalObject:owner] && ![self isOldExternalObject:object])
179 [self addExternalRememberedObject:owner];
181 NSMapTable *ownedObjects = [m_externalObjectGraph objectForKey:owner];
183 NSPointerFunctionsOptions weakIDOptions = NSPointerFunctionsWeakMemory | NSPointerFunctionsObjectPersonality;
184 NSPointerFunctionsOptions integerOptions = NSPointerFunctionsOpaqueMemory | NSPointerFunctionsIntegerPersonality;
185 ownedObjects = [[NSMapTable alloc] initWithKeyOptions:weakIDOptions valueOptions:integerOptions capacity:1];
187 [m_externalObjectGraph setObject:ownedObjects forKey:owner];
188 [ownedObjects release];
191 size_t count = reinterpret_cast<size_t>(NSMapGet(ownedObjects, object));
192 NSMapInsert(ownedObjects, object, reinterpret_cast<void*>(count + 1));
195 - (void)removeManagedReference:(id)object withOwner:(id)owner
197 if ([object isKindOfClass:[JSManagedValue class]])
198 [object didRemoveOwner:owner];
200 object = getInternalObjcObject(object);
201 owner = getInternalObjcObject(owner);
203 if (!object || !owner)
206 JSC::JSLockHolder locker(toJS(m_group));
208 NSMapTable *ownedObjects = [m_externalObjectGraph objectForKey:owner];
212 size_t count = reinterpret_cast<size_t>(NSMapGet(ownedObjects, object));
214 NSMapInsert(ownedObjects, object, reinterpret_cast<void*>(count - 1));
219 NSMapRemove(ownedObjects, object);
221 if (![ownedObjects count]) {
222 [m_externalObjectGraph removeObjectForKey:owner];
223 [m_externalRememberedSet removeObjectForKey:owner];
229 @implementation JSVirtualMachine(Internal)
231 JSContextGroupRef getGroupFromVirtualMachine(JSVirtualMachine *virtualMachine)
233 return virtualMachine->m_group;
236 + (JSVirtualMachine *)virtualMachineWithContextGroupRef:(JSContextGroupRef)group
238 JSVirtualMachine *virtualMachine = [JSVMWrapperCache wrapperForJSContextGroupRef:group];
240 virtualMachine = [[[JSVirtualMachine alloc] initWithContextGroupRef:group] autorelease];
241 return virtualMachine;
244 - (JSContext *)contextForGlobalContextRef:(JSGlobalContextRef)globalContext
246 return static_cast<JSContext *>(NSMapGet(m_contextCache, globalContext));
249 - (void)addContext:(JSContext *)wrapper forGlobalContextRef:(JSGlobalContextRef)globalContext
251 NSMapInsert(m_contextCache, globalContext, wrapper);
254 - (NSMapTable *)externalObjectGraph
256 return m_externalObjectGraph;
259 - (NSMapTable *)externalRememberedSet
261 return m_externalRememberedSet;
266 void scanExternalObjectGraph(JSC::VM& vm, JSC::SlotVisitor& visitor, void* root)
269 JSVirtualMachine *virtualMachine = [JSVMWrapperCache wrapperForJSContextGroupRef:toRef(&vm)];
272 NSMapTable *externalObjectGraph = [virtualMachine externalObjectGraph];
275 while (!stack.isEmpty()) {
276 void* nextRoot = stack.last();
278 if (visitor.containsOpaqueRootTriState(nextRoot) == TrueTriState)
280 visitor.addOpaqueRoot(nextRoot);
282 NSMapTable *ownedObjects = [externalObjectGraph objectForKey:static_cast<id>(nextRoot)];
283 for (id ownedObject in ownedObjects)
284 stack.append(static_cast<void*>(ownedObject));
289 void scanExternalRememberedSet(JSC::VM& vm, JSC::SlotVisitor& visitor)
292 JSVirtualMachine *virtualMachine = [JSVMWrapperCache wrapperForJSContextGroupRef:toRef(&vm)];
295 NSMapTable *externalObjectGraph = [virtualMachine externalObjectGraph];
296 NSMapTable *externalRememberedSet = [virtualMachine externalRememberedSet];
297 for (id key in externalRememberedSet) {
298 NSMapTable *ownedObjects = [externalObjectGraph objectForKey:key];
299 for (id ownedObject in ownedObjects)
300 scanExternalObjectGraph(vm, visitor, ownedObject);
302 [externalRememberedSet removeAllObjects];
306 #endif // JSC_OBJC_API_ENABLED