]> git.saurik.com Git - apple/javascriptcore.git/blob - heap/Local.h
ac7d13696a1d261f51957a7627181bc972a3b956
[apple/javascriptcore.git] / heap / Local.h
1 /*
2 * Copyright (C) 2010 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 #ifndef Local_h
27 #define Local_h
28
29 #include "Handle.h"
30 #include "JSGlobalData.h"
31
32 /*
33 A strongly referenced handle whose lifetime is temporary, limited to a given
34 LocalScope. Use Locals for local values on the stack. It is an error to
35 create a Local outside of any LocalScope.
36 */
37
38 namespace JSC {
39
40 template <typename T> class Local : public Handle<T> {
41 friend class LocalScope;
42 using Handle<T>::slot;
43
44 public:
45 typedef typename Handle<T>::ExternalType ExternalType;
46
47 Local(JSGlobalData&, ExternalType = ExternalType());
48 Local(JSGlobalData&, Handle<T>);
49 Local(const Local<T>&); // Adopting constructor. Used to return a Local to a calling function.
50
51 Local& operator=(ExternalType);
52 Local& operator=(Handle<T>);
53
54 private:
55 Local(HandleSlot, ExternalType); // Used by LocalScope::release() to move a Local to a containing scope.
56 void set(ExternalType);
57 };
58
59 template <typename T> inline Local<T>::Local(JSGlobalData& globalData, ExternalType value)
60 : Handle<T>(globalData.allocateLocalHandle())
61 {
62 set(value);
63 }
64
65 template <typename T> inline Local<T>::Local(JSGlobalData& globalData, Handle<T> other)
66 : Handle<T>(globalData.allocateLocalHandle())
67 {
68 set(other.get());
69 }
70
71 template <typename T> inline Local<T>::Local(const Local<T>& other)
72 : Handle<T>(other.slot())
73 {
74 const_cast<Local<T>&>(other).setSlot(0); // Prevent accidental sharing.
75 }
76
77 template <typename T> inline Local<T>::Local(HandleSlot slot, ExternalType value)
78 : Handle<T>(slot, value)
79 {
80 }
81
82 template <typename T> inline Local<T>& Local<T>::operator=(ExternalType value)
83 {
84 set(value);
85 return *this;
86 }
87
88 template <typename T> inline Local<T>& Local<T>::operator=(Handle<T> other)
89 {
90 set(other.get());
91 return *this;
92 }
93
94 template <typename T> inline void Local<T>::set(ExternalType externalType)
95 {
96 ASSERT(slot());
97 ASSERT(!HandleTypes<T>::toJSValue(externalType) || !HandleTypes<T>::toJSValue(externalType).isCell() || Heap::isMarked(HandleTypes<T>::toJSValue(externalType).asCell()));
98 *slot() = externalType;
99 }
100
101
102 template <typename T, unsigned inlineCapacity = 0> class LocalStack {
103 typedef typename Handle<T>::ExternalType ExternalType;
104 public:
105 LocalStack(JSGlobalData& globalData)
106 : m_globalData(&globalData)
107 , m_count(0)
108 {
109 }
110
111 ExternalType peek() const
112 {
113 ASSERT(m_count > 0);
114 return m_stack[m_count - 1].get();
115 }
116
117 ExternalType pop()
118 {
119 ASSERT(m_count > 0);
120 return m_stack[--m_count].get();
121 }
122
123 void push(ExternalType value)
124 {
125 if (m_count == m_stack.size())
126 m_stack.append(Local<T>(*m_globalData, value));
127 else
128 m_stack[m_count] = value;
129 m_count++;
130 }
131
132 bool isEmpty() const { return !m_count; }
133 unsigned size() const { return m_count; }
134
135 private:
136 RefPtr<JSGlobalData> m_globalData;
137 Vector<Local<T>, inlineCapacity> m_stack;
138 unsigned m_count;
139 };
140
141 }
142
143 namespace WTF {
144
145 template<typename T> struct VectorTraits<JSC::Local<T> > : SimpleClassVectorTraits {
146 static const bool needsDestruction = false;
147 static const bool canInitializeWithMemset = false;
148 static const bool canCompareWithMemcmp = false;
149 };
150
151 }
152
153 #endif