]>
Commit | Line | Data |
---|---|---|
93a37866 A |
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 | ||
26 | #include "config.h" | |
27 | ||
28 | #import "JavaScriptCore.h" | |
29 | ||
30 | #if JSC_OBJC_API_ENABLED | |
31 | ||
32 | #import "APICast.h" | |
33 | #import "APIShims.h" | |
34 | #import "JSVirtualMachine.h" | |
35 | #import "JSVirtualMachineInternal.h" | |
36 | #import "JSWrapperMap.h" | |
37 | ||
38 | static NSMapTable *globalWrapperCache = 0; | |
39 | ||
40 | static Mutex& wrapperCacheLock() | |
41 | { | |
42 | DEFINE_STATIC_LOCAL(Mutex, mutex, ()); | |
43 | return mutex; | |
44 | } | |
45 | ||
46 | static void initWrapperCache() | |
47 | { | |
48 | ASSERT(!globalWrapperCache); | |
49 | NSPointerFunctionsOptions keyOptions = NSPointerFunctionsOpaqueMemory | NSPointerFunctionsOpaquePersonality; | |
50 | NSPointerFunctionsOptions valueOptions = NSPointerFunctionsWeakMemory | NSPointerFunctionsObjectPersonality; | |
51 | globalWrapperCache = [[NSMapTable alloc] initWithKeyOptions:keyOptions valueOptions:valueOptions capacity:0]; | |
52 | } | |
53 | ||
54 | static NSMapTable *wrapperCache() | |
55 | { | |
56 | if (!globalWrapperCache) | |
57 | initWrapperCache(); | |
58 | return globalWrapperCache; | |
59 | } | |
60 | ||
61 | @interface JSVMWrapperCache : NSObject | |
62 | + (void)addWrapper:(JSVirtualMachine *)wrapper forJSContextGroupRef:(JSContextGroupRef)group; | |
63 | + (JSVirtualMachine *)wrapperForJSContextGroupRef:(JSContextGroupRef)group; | |
64 | @end | |
65 | ||
66 | @implementation JSVMWrapperCache | |
67 | ||
68 | + (void)addWrapper:(JSVirtualMachine *)wrapper forJSContextGroupRef:(JSContextGroupRef)group | |
69 | { | |
70 | MutexLocker locker(wrapperCacheLock()); | |
71 | NSMapInsert(wrapperCache(), group, wrapper); | |
72 | } | |
73 | ||
74 | + (JSVirtualMachine *)wrapperForJSContextGroupRef:(JSContextGroupRef)group | |
75 | { | |
76 | MutexLocker locker(wrapperCacheLock()); | |
77 | return static_cast<JSVirtualMachine *>(NSMapGet(wrapperCache(), group)); | |
78 | } | |
79 | ||
80 | @end | |
81 | ||
82 | @implementation JSVirtualMachine { | |
83 | JSContextGroupRef m_group; | |
84 | NSMapTable *m_contextCache; | |
85 | NSMapTable *m_externalObjectGraph; | |
86 | } | |
87 | ||
88 | - (id)init | |
89 | { | |
90 | JSContextGroupRef group = JSContextGroupCreate(); | |
91 | self = [self initWithContextGroupRef:group]; | |
92 | // The extra JSContextGroupRetain is balanced here. | |
93 | JSContextGroupRelease(group); | |
94 | return self; | |
95 | } | |
96 | ||
97 | - (id)initWithContextGroupRef:(JSContextGroupRef)group | |
98 | { | |
99 | self = [super init]; | |
100 | if (!self) | |
101 | return nil; | |
102 | ||
103 | m_group = JSContextGroupRetain(group); | |
104 | ||
105 | NSPointerFunctionsOptions keyOptions = NSPointerFunctionsOpaqueMemory | NSPointerFunctionsOpaquePersonality; | |
106 | NSPointerFunctionsOptions valueOptions = NSPointerFunctionsWeakMemory | NSPointerFunctionsObjectPersonality; | |
107 | m_contextCache = [[NSMapTable alloc] initWithKeyOptions:keyOptions valueOptions:valueOptions capacity:0]; | |
108 | ||
109 | NSPointerFunctionsOptions weakIDOptions = NSPointerFunctionsWeakMemory | NSPointerFunctionsObjectPersonality; | |
110 | NSPointerFunctionsOptions strongIDOptions = NSPointerFunctionsStrongMemory | NSPointerFunctionsObjectPersonality; | |
111 | m_externalObjectGraph = [[NSMapTable alloc] initWithKeyOptions:weakIDOptions valueOptions:strongIDOptions capacity:0]; | |
112 | ||
113 | [JSVMWrapperCache addWrapper:self forJSContextGroupRef:group]; | |
114 | ||
115 | return self; | |
116 | } | |
117 | ||
118 | - (void)dealloc | |
119 | { | |
120 | JSContextGroupRelease(m_group); | |
121 | [m_contextCache release]; | |
122 | [m_externalObjectGraph release]; | |
123 | [super dealloc]; | |
124 | } | |
125 | ||
126 | static id getInternalObjcObject(id object) | |
127 | { | |
128 | if ([object isKindOfClass:[JSManagedValue class]]) { | |
129 | JSValue* value = [static_cast<JSManagedValue *>(object) value]; | |
130 | id temp = tryUnwrapObjcObject([value.context JSGlobalContextRef], [value JSValueRef]); | |
131 | if (temp) | |
132 | return temp; | |
133 | return object; | |
134 | } | |
135 | ||
136 | if ([object isKindOfClass:[JSValue class]]) { | |
137 | JSValue *value = static_cast<JSValue *>(object); | |
138 | object = tryUnwrapObjcObject([value.context JSGlobalContextRef], [value JSValueRef]); | |
139 | } | |
140 | ||
141 | return object; | |
142 | } | |
143 | ||
144 | - (void)addManagedReference:(id)object withOwner:(id)owner | |
145 | { | |
146 | object = getInternalObjcObject(object); | |
147 | owner = getInternalObjcObject(owner); | |
148 | ||
149 | if (!object || !owner) | |
150 | return; | |
151 | ||
152 | JSC::APIEntryShim shim(toJS(m_group)); | |
153 | ||
154 | NSMapTable *ownedObjects = [m_externalObjectGraph objectForKey:owner]; | |
155 | if (!ownedObjects) { | |
156 | NSPointerFunctionsOptions weakIDOptions = NSPointerFunctionsWeakMemory | NSPointerFunctionsObjectPersonality; | |
157 | NSPointerFunctionsOptions integerOptions = NSPointerFunctionsOpaqueMemory | NSPointerFunctionsIntegerPersonality; | |
158 | ownedObjects = [[NSMapTable alloc] initWithKeyOptions:weakIDOptions valueOptions:integerOptions capacity:1]; | |
159 | ||
160 | [m_externalObjectGraph setObject:ownedObjects forKey:owner]; | |
161 | [ownedObjects release]; | |
162 | } | |
163 | NSMapInsert(ownedObjects, object, reinterpret_cast<void*>(reinterpret_cast<size_t>(NSMapGet(ownedObjects, object)) + 1)); | |
164 | } | |
165 | ||
166 | - (void)removeManagedReference:(id)object withOwner:(id)owner | |
167 | { | |
168 | object = getInternalObjcObject(object); | |
169 | owner = getInternalObjcObject(owner); | |
170 | ||
171 | if (!object || !owner) | |
172 | return; | |
173 | ||
174 | JSC::APIEntryShim shim(toJS(m_group)); | |
175 | ||
176 | NSMapTable *ownedObjects = [m_externalObjectGraph objectForKey:owner]; | |
177 | if (!ownedObjects) | |
178 | return; | |
179 | ||
180 | size_t count = reinterpret_cast<size_t>(NSMapGet(ownedObjects, object)); | |
181 | if (count > 1) { | |
182 | NSMapInsert(ownedObjects, object, reinterpret_cast<void*>(count - 1)); | |
183 | return; | |
184 | } | |
185 | ||
186 | if (count == 1) | |
187 | NSMapRemove(ownedObjects, object); | |
188 | ||
189 | if (![ownedObjects count]) | |
190 | [m_externalObjectGraph removeObjectForKey:owner]; | |
191 | } | |
192 | ||
193 | @end | |
194 | ||
195 | @implementation JSVirtualMachine(Internal) | |
196 | ||
197 | JSContextGroupRef getGroupFromVirtualMachine(JSVirtualMachine *virtualMachine) | |
198 | { | |
199 | return virtualMachine->m_group; | |
200 | } | |
201 | ||
202 | + (JSVirtualMachine *)virtualMachineWithContextGroupRef:(JSContextGroupRef)group | |
203 | { | |
204 | JSVirtualMachine *virtualMachine = [JSVMWrapperCache wrapperForJSContextGroupRef:group]; | |
205 | if (!virtualMachine) | |
206 | virtualMachine = [[[JSVirtualMachine alloc] initWithContextGroupRef:group] autorelease]; | |
207 | return virtualMachine; | |
208 | } | |
209 | ||
210 | - (JSContext *)contextForGlobalContextRef:(JSGlobalContextRef)globalContext | |
211 | { | |
212 | return static_cast<JSContext *>(NSMapGet(m_contextCache, globalContext)); | |
213 | } | |
214 | ||
215 | - (void)addContext:(JSContext *)wrapper forGlobalContextRef:(JSGlobalContextRef)globalContext | |
216 | { | |
217 | NSMapInsert(m_contextCache, globalContext, wrapper); | |
218 | } | |
219 | ||
220 | - (NSMapTable *)externalObjectGraph | |
221 | { | |
222 | return m_externalObjectGraph; | |
223 | } | |
224 | ||
225 | @end | |
226 | ||
227 | void scanExternalObjectGraph(JSC::VM& vm, JSC::SlotVisitor& visitor, void* root) | |
228 | { | |
229 | @autoreleasepool { | |
230 | JSVirtualMachine *virtualMachine = [JSVMWrapperCache wrapperForJSContextGroupRef:toRef(&vm)]; | |
231 | if (!virtualMachine) | |
232 | return; | |
233 | NSMapTable *externalObjectGraph = [virtualMachine externalObjectGraph]; | |
234 | Vector<void*> stack; | |
235 | stack.append(root); | |
236 | while (!stack.isEmpty()) { | |
237 | void* nextRoot = stack.last(); | |
238 | stack.removeLast(); | |
239 | if (visitor.containsOpaqueRootTriState(nextRoot) == TrueTriState) | |
240 | continue; | |
241 | visitor.addOpaqueRoot(nextRoot); | |
242 | ||
243 | NSMapTable *ownedObjects = [externalObjectGraph objectForKey:static_cast<id>(nextRoot)]; | |
244 | id ownedObject; | |
245 | NSEnumerator *enumerator = [ownedObjects keyEnumerator]; | |
246 | while ((ownedObject = [enumerator nextObject])) { | |
247 | ASSERT(reinterpret_cast<size_t>(NSMapGet(ownedObjects, ownedObject)) == 1); | |
248 | stack.append(static_cast<void*>(ownedObject)); | |
249 | } | |
250 | } | |
251 | } | |
252 | } | |
253 | ||
254 | #endif | |
255 |