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. AND ITS CONTRIBUTORS ``AS IS''
14 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
15 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
16 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS
17 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
18 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
19 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
20 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
21 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
22 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
23 * THE POSSIBILITY OF SUCH DAMAGE.
28 #import "JSManagedValue.h"
30 #if JSC_OBJC_API_ENABLED
34 #import "JSContextInternal.h"
35 #import "JSValueInternal.h"
37 #import "WeakHandleOwner.h"
38 #import "ObjcRuntimeExtras.h"
39 #import "JSCInlines.h"
41 class JSManagedValueHandleOwner : public JSC::WeakHandleOwner {
43 virtual void finalize(JSC::Handle<JSC::Unknown>, void* context) override;
44 virtual bool isReachableFromOpaqueRoots(JSC::Handle<JSC::Unknown>, void* context, JSC::SlotVisitor&) override;
47 static JSManagedValueHandleOwner* managedValueHandleOwner()
49 DEPRECATED_DEFINE_STATIC_LOCAL(JSManagedValueHandleOwner, jsManagedValueHandleOwner, ());
50 return &jsManagedValueHandleOwner;
71 u.m_primitive = JSC::JSValue();
80 RELEASE_ASSERT_NOT_REACHED();
89 return !u.m_primitive;
95 RELEASE_ASSERT_NOT_REACHED();
98 bool isSet() const { return m_tag != NotSet; }
99 bool isPrimitive() const { return m_tag == Primitive; }
100 bool isObject() const { return m_tag == Object; }
101 bool isString() const { return m_tag == String; }
103 void setPrimitive(JSC::JSValue primitive)
106 ASSERT(!u.m_primitive);
107 ASSERT(primitive.isPrimitive());
109 u.m_primitive = primitive;
112 void setObject(JSC::JSObject* object, void* context)
117 JSC::Weak<JSC::JSObject> weak(object, managedValueHandleOwner(), context);
118 u.m_object.swap(weak);
121 void setString(JSC::JSString* string, void* context)
126 JSC::Weak<JSC::JSString> weak(string, managedValueHandleOwner(), context);
127 u.m_string.swap(weak);
130 JSC::JSObject* object()
133 return u.m_object.get();
136 JSC::JSValue primitive()
138 ASSERT(isPrimitive());
139 return u.m_primitive;
142 JSC::JSString* string()
145 return u.m_string.get();
149 enum WeakTypeTag { NotSet, Primitive, Object, String };
151 union WeakValueUnion {
154 : m_primitive(JSC::JSValue())
160 ASSERT(!m_primitive);
163 JSC::JSValue m_primitive;
164 JSC::Weak<JSC::JSObject> m_object;
165 JSC::Weak<JSC::JSString> m_string;
169 @implementation JSManagedValue {
170 JSC::Weak<JSC::JSGlobalObject> m_globalObject;
171 RefPtr<JSC::JSLock> m_lock;
172 WeakValueRef m_weakValue;
173 NSMapTable *m_owners;
176 + (JSManagedValue *)managedValueWithValue:(JSValue *)value
178 return [[[self alloc] initWithValue:value] autorelease];
181 + (JSManagedValue *)managedValueWithValue:(JSValue *)value andOwner:(id)owner
183 JSManagedValue *managedValue = [[self alloc] initWithValue:value];
184 [value.context.virtualMachine addManagedReference:managedValue withOwner:owner];
185 return [managedValue autorelease];
190 return [self initWithValue:nil];
193 - (instancetype)initWithValue:(JSValue *)value
202 JSC::ExecState* exec = toJS([value.context JSGlobalContextRef]);
203 JSC::JSGlobalObject* globalObject = exec->lexicalGlobalObject();
204 JSC::Weak<JSC::JSGlobalObject> weak(globalObject, managedValueHandleOwner(), self);
205 m_globalObject.swap(weak);
207 m_lock = &exec->vm().apiLock();
209 NSPointerFunctionsOptions weakIDOptions = NSPointerFunctionsWeakMemory | NSPointerFunctionsObjectPersonality;
210 NSPointerFunctionsOptions integerOptions = NSPointerFunctionsOpaqueMemory | NSPointerFunctionsIntegerPersonality;
211 m_owners = [[NSMapTable alloc] initWithKeyOptions:weakIDOptions valueOptions:integerOptions capacity:1];
213 JSC::JSValue jsValue = toJS(exec, [value JSValueRef]);
214 if (jsValue.isObject())
215 m_weakValue.setObject(JSC::jsCast<JSC::JSObject*>(jsValue.asCell()), self);
216 else if (jsValue.isString())
217 m_weakValue.setString(JSC::jsCast<JSC::JSString*>(jsValue.asCell()), self);
219 m_weakValue.setPrimitive(jsValue);
225 JSVirtualMachine *virtualMachine = [[[self value] context] virtualMachine];
226 if (virtualMachine) {
227 NSMapTable *copy = [m_owners copy];
228 for (id owner in [copy keyEnumerator]) {
229 size_t count = reinterpret_cast<size_t>(NSMapGet(m_owners, owner));
231 [virtualMachine removeManagedReference:self withOwner:owner];
236 [self disconnectValue];
241 - (void)didAddOwner:(id)owner
243 size_t count = reinterpret_cast<size_t>(NSMapGet(m_owners, owner));
244 NSMapInsert(m_owners, owner, reinterpret_cast<void*>(count + 1));
247 - (void)didRemoveOwner:(id)owner
249 size_t count = reinterpret_cast<size_t>(NSMapGet(m_owners, owner));
255 NSMapRemove(m_owners, owner);
259 NSMapInsert(m_owners, owner, reinterpret_cast<void*>(count - 1));
264 WTF::Locker<JSC::JSLock> locker(m_lock.get());
268 JSC::JSLockHolder apiLocker(m_lock->vm());
271 if (m_weakValue.isClear())
273 JSC::ExecState* exec = m_globalObject->globalExec();
274 JSContext *context = [JSContext contextWithJSGlobalContextRef:toGlobalRef(exec)];
276 if (m_weakValue.isPrimitive())
277 value = m_weakValue.primitive();
278 else if (m_weakValue.isString())
279 value = m_weakValue.string();
281 value = m_weakValue.object();
282 return [JSValue valueWithJSValueRef:toRef(exec, value) inContext:context];
285 - (void)disconnectValue
287 m_globalObject.clear();
293 @interface JSManagedValue (PrivateMethods)
294 - (void)disconnectValue;
297 bool JSManagedValueHandleOwner::isReachableFromOpaqueRoots(JSC::Handle<JSC::Unknown>, void* context, JSC::SlotVisitor& visitor)
299 JSManagedValue *managedValue = static_cast<JSManagedValue *>(context);
300 return visitor.containsOpaqueRoot(managedValue);
303 void JSManagedValueHandleOwner::finalize(JSC::Handle<JSC::Unknown>, void* context)
305 JSManagedValue *managedValue = static_cast<JSManagedValue *>(context);
306 [managedValue disconnectValue];
309 #endif // JSC_OBJC_API_ENABLED