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>
41 static NSMapTable *globalWrapperCache = 0;
43 static std::mutex& wrapperCacheMutex()
45 static NeverDestroyed<std::mutex> mutex;
50 static void initWrapperCache()
52 ASSERT(!globalWrapperCache);
53 NSPointerFunctionsOptions keyOptions = NSPointerFunctionsOpaqueMemory | NSPointerFunctionsOpaquePersonality;
54 NSPointerFunctionsOptions valueOptions = NSPointerFunctionsWeakMemory | NSPointerFunctionsObjectPersonality;
55 globalWrapperCache = [[NSMapTable alloc] initWithKeyOptions:keyOptions valueOptions:valueOptions capacity:0];
58 static NSMapTable *wrapperCache()
60 if (!globalWrapperCache)
62 return globalWrapperCache;
65 @interface JSVMWrapperCache : NSObject
66 + (void)addWrapper:(JSVirtualMachine *)wrapper forJSContextGroupRef:(JSContextGroupRef)group;
67 + (JSVirtualMachine *)wrapperForJSContextGroupRef:(JSContextGroupRef)group;
70 @implementation JSVMWrapperCache
72 + (void)addWrapper:(JSVirtualMachine *)wrapper forJSContextGroupRef:(JSContextGroupRef)group
74 std::lock_guard<std::mutex> lock(wrapperCacheMutex());
75 NSMapInsert(wrapperCache(), group, wrapper);
78 + (JSVirtualMachine *)wrapperForJSContextGroupRef:(JSContextGroupRef)group
80 std::lock_guard<std::mutex> lock(wrapperCacheMutex());
81 return static_cast<JSVirtualMachine *>(NSMapGet(wrapperCache(), group));
86 @implementation JSVirtualMachine {
87 JSContextGroupRef m_group;
88 NSMapTable *m_contextCache;
89 NSMapTable *m_externalObjectGraph;
90 NSMapTable *m_externalRememberedSet;
95 JSContextGroupRef group = JSContextGroupCreate();
96 self = [self initWithContextGroupRef:group];
97 // The extra JSContextGroupRetain is balanced here.
98 JSContextGroupRelease(group);
102 - (instancetype)initWithContextGroupRef:(JSContextGroupRef)group
108 m_group = JSContextGroupRetain(group);
110 NSPointerFunctionsOptions keyOptions = NSPointerFunctionsOpaqueMemory | NSPointerFunctionsOpaquePersonality;
111 NSPointerFunctionsOptions valueOptions = NSPointerFunctionsWeakMemory | NSPointerFunctionsObjectPersonality;
112 m_contextCache = [[NSMapTable alloc] initWithKeyOptions:keyOptions valueOptions:valueOptions capacity:0];
114 NSPointerFunctionsOptions weakIDOptions = NSPointerFunctionsWeakMemory | NSPointerFunctionsObjectPersonality;
115 NSPointerFunctionsOptions strongIDOptions = NSPointerFunctionsStrongMemory | NSPointerFunctionsObjectPersonality;
116 m_externalObjectGraph = [[NSMapTable alloc] initWithKeyOptions:weakIDOptions valueOptions:strongIDOptions capacity:0];
118 NSPointerFunctionsOptions integerOptions = NSPointerFunctionsOpaqueMemory | NSPointerFunctionsIntegerPersonality;
119 m_externalRememberedSet = [[NSMapTable alloc] initWithKeyOptions:weakIDOptions valueOptions:integerOptions capacity:0];
121 [JSVMWrapperCache addWrapper:self forJSContextGroupRef:group];
128 JSContextGroupRelease(m_group);
129 [m_contextCache release];
130 [m_externalObjectGraph release];
131 [m_externalRememberedSet release];
135 static id getInternalObjcObject(id object)
137 if ([object isKindOfClass:[JSManagedValue class]]) {
138 JSValue* value = [static_cast<JSManagedValue *>(object) value];
139 id temp = tryUnwrapObjcObject([value.context JSGlobalContextRef], [value JSValueRef]);
145 if ([object isKindOfClass:[JSValue class]]) {
146 JSValue *value = static_cast<JSValue *>(object);
147 object = tryUnwrapObjcObject([value.context JSGlobalContextRef], [value JSValueRef]);
153 - (bool)isOldExternalObject:(id)object
155 JSC::VM* vm = toJS(m_group);
156 return vm->heap.slotVisitor().containsOpaqueRoot(object);
159 - (void)addExternalRememberedObject:(id)object
161 ASSERT([self isOldExternalObject:object]);
162 [m_externalRememberedSet setObject:[NSNumber numberWithBool:true] forKey:object];
165 - (void)addManagedReference:(id)object withOwner:(id)owner
167 if ([object isKindOfClass:[JSManagedValue class]])
168 [object didAddOwner:owner];
170 object = getInternalObjcObject(object);
171 owner = getInternalObjcObject(owner);
173 if (!object || !owner)
176 JSC::JSLockHolder locker(toJS(m_group));
177 if ([self isOldExternalObject:owner] && ![self isOldExternalObject:object])
178 [self addExternalRememberedObject:owner];
180 NSMapTable *ownedObjects = [m_externalObjectGraph objectForKey:owner];
182 NSPointerFunctionsOptions weakIDOptions = NSPointerFunctionsWeakMemory | NSPointerFunctionsObjectPersonality;
183 NSPointerFunctionsOptions integerOptions = NSPointerFunctionsOpaqueMemory | NSPointerFunctionsIntegerPersonality;
184 ownedObjects = [[NSMapTable alloc] initWithKeyOptions:weakIDOptions valueOptions:integerOptions capacity:1];
186 [m_externalObjectGraph setObject:ownedObjects forKey:owner];
187 [ownedObjects release];
190 size_t count = reinterpret_cast<size_t>(NSMapGet(ownedObjects, object));
191 NSMapInsert(ownedObjects, object, reinterpret_cast<void*>(count + 1));
194 - (void)removeManagedReference:(id)object withOwner:(id)owner
196 if ([object isKindOfClass:[JSManagedValue class]])
197 [object didRemoveOwner:owner];
199 object = getInternalObjcObject(object);
200 owner = getInternalObjcObject(owner);
202 if (!object || !owner)
205 JSC::JSLockHolder locker(toJS(m_group));
207 NSMapTable *ownedObjects = [m_externalObjectGraph objectForKey:owner];
211 size_t count = reinterpret_cast<size_t>(NSMapGet(ownedObjects, object));
213 NSMapInsert(ownedObjects, object, reinterpret_cast<void*>(count - 1));
218 NSMapRemove(ownedObjects, object);
220 if (![ownedObjects count]) {
221 [m_externalObjectGraph removeObjectForKey:owner];
222 [m_externalRememberedSet removeObjectForKey:owner];
228 @implementation JSVirtualMachine(Internal)
230 JSContextGroupRef getGroupFromVirtualMachine(JSVirtualMachine *virtualMachine)
232 return virtualMachine->m_group;
235 + (JSVirtualMachine *)virtualMachineWithContextGroupRef:(JSContextGroupRef)group
237 JSVirtualMachine *virtualMachine = [JSVMWrapperCache wrapperForJSContextGroupRef:group];
239 virtualMachine = [[[JSVirtualMachine alloc] initWithContextGroupRef:group] autorelease];
240 return virtualMachine;
243 - (JSContext *)contextForGlobalContextRef:(JSGlobalContextRef)globalContext
245 return static_cast<JSContext *>(NSMapGet(m_contextCache, globalContext));
248 - (void)addContext:(JSContext *)wrapper forGlobalContextRef:(JSGlobalContextRef)globalContext
250 NSMapInsert(m_contextCache, globalContext, wrapper);
253 - (NSMapTable *)externalObjectGraph
255 return m_externalObjectGraph;
258 - (NSMapTable *)externalRememberedSet
260 return m_externalRememberedSet;
265 void scanExternalObjectGraph(JSC::VM& vm, JSC::SlotVisitor& visitor, void* root)
268 JSVirtualMachine *virtualMachine = [JSVMWrapperCache wrapperForJSContextGroupRef:toRef(&vm)];
271 NSMapTable *externalObjectGraph = [virtualMachine externalObjectGraph];
274 while (!stack.isEmpty()) {
275 void* nextRoot = stack.last();
277 if (visitor.containsOpaqueRootTriState(nextRoot) == TrueTriState)
279 visitor.addOpaqueRoot(nextRoot);
281 NSMapTable *ownedObjects = [externalObjectGraph objectForKey:static_cast<id>(nextRoot)];
282 for (id ownedObject in ownedObjects)
283 stack.append(static_cast<void*>(ownedObject));
288 void scanExternalRememberedSet(JSC::VM& vm, JSC::SlotVisitor& visitor)
291 JSVirtualMachine *virtualMachine = [JSVMWrapperCache wrapperForJSContextGroupRef:toRef(&vm)];
294 NSMapTable *externalObjectGraph = [virtualMachine externalObjectGraph];
295 NSMapTable *externalRememberedSet = [virtualMachine externalRememberedSet];
296 for (id key in externalRememberedSet) {
297 NSMapTable *ownedObjects = [externalObjectGraph objectForKey:key];
298 for (id ownedObject in ownedObjects)
299 scanExternalObjectGraph(vm, visitor, ownedObject);
301 [externalRememberedSet removeAllObjects];
305 #endif // JSC_OBJC_API_ENABLED