1/*
2 * Copyright (C) 2012 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. 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.
24 */
25
26#pragma once
27
28#include "JSCJSValue.h"
29
30namespace JSC {
31
32class WeakHandleOwner;
33
34class WeakImpl {
35public:
36 enum State {
37 Live = 0x0,
38 Dead = 0x1,
39 Finalized = 0x2,
40 Deallocated = 0x3
41 };
42
43 enum {
44 StateMask = 0x3
45 };
46
47 WeakImpl();
48 WeakImpl(JSValue, WeakHandleOwner*, void* context);
49
50 State state();
51 void setState(State);
52
53 const JSValue& jsValue();
54 static ptrdiff_t offsetOfJSValue() { return OBJECT_OFFSETOF(WeakImpl, m_jsValue); }
55 WeakHandleOwner* weakHandleOwner();
56 static ptrdiff_t offsetOfWeakHandleOwner() { return OBJECT_OFFSETOF(WeakImpl, m_weakHandleOwner); }
57 void* context();
58
59 static WeakImpl* asWeakImpl(JSValue*);
60
61private:
62 const JSValue m_jsValue;
63 WeakHandleOwner* m_weakHandleOwner;
64 void* m_context;
65};
66
67inline WeakImpl::WeakImpl()
68 : m_weakHandleOwner(0)
69 , m_context(0)
70{
71 setState(Deallocated);
72}
73
74inline WeakImpl::WeakImpl(JSValue jsValue, WeakHandleOwner* weakHandleOwner, void* context)
75 : m_jsValue(jsValue)
76 , m_weakHandleOwner(weakHandleOwner)
77 , m_context(context)
78{
79 ASSERT(state() == Live);
80 ASSERT(m_jsValue && m_jsValue.isCell());
81}
82
83inline WeakImpl::State WeakImpl::state()
84{
85 return static_cast<State>(reinterpret_cast<uintptr_t>(m_weakHandleOwner) & StateMask);
86}
87
88inline void WeakImpl::setState(WeakImpl::State state)
89{
90 ASSERT(state >= this->state());
91 m_weakHandleOwner = reinterpret_cast<WeakHandleOwner*>((reinterpret_cast<uintptr_t>(m_weakHandleOwner) & ~StateMask) | state);
92}
93
94inline const JSValue& WeakImpl::jsValue()
95{
96 return m_jsValue;
97}
98
99inline WeakHandleOwner* WeakImpl::weakHandleOwner()
100{
101 return reinterpret_cast<WeakHandleOwner*>((reinterpret_cast<uintptr_t>(m_weakHandleOwner) & ~StateMask));
102}
103
104inline void* WeakImpl::context()
105{
106 return m_context;
107}
108
109inline WeakImpl* WeakImpl::asWeakImpl(JSValue* slot)
110{
111 return reinterpret_cast_ptr<WeakImpl*>(reinterpret_cast_ptr<char*>(slot) + OBJECT_OFFSETOF(WeakImpl, m_jsValue));
112}
113
114} // namespace JSC
115