]> git.saurik.com Git - apple/javascriptcore.git/blob - runtime/ArgList.h
JavaScriptCore-1097.13.tar.gz
[apple/javascriptcore.git] / runtime / ArgList.h
1 /*
2 * Copyright (C) 1999-2001 Harri Porten (porten@kde.org)
3 * Copyright (C) 2003, 2007, 2008, 2009 Apple Inc. All rights reserved.
4 *
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Library General Public
7 * License as published by the Free Software Foundation; either
8 * version 2 of the License, or (at your option) any later version.
9 *
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Library General Public License for more details.
14 *
15 * You should have received a copy of the GNU Library General Public License
16 * along with this library; see the file COPYING.LIB. If not, write to
17 * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
18 * Boston, MA 02110-1301, USA.
19 *
20 */
21
22 #ifndef ArgList_h
23 #define ArgList_h
24
25 #include "CallFrame.h"
26 #include "Register.h"
27 #include "WriteBarrier.h"
28 #include <wtf/HashSet.h>
29 #include <wtf/Vector.h>
30
31 namespace JSC {
32
33 class SlotVisitor;
34
35 class MarkedArgumentBuffer {
36 WTF_MAKE_NONCOPYABLE(MarkedArgumentBuffer);
37 friend class JSGlobalData;
38 friend class ArgList;
39
40 private:
41 static const size_t inlineCapacity = 8;
42 typedef Vector<Register, inlineCapacity> VectorType;
43 typedef HashSet<MarkedArgumentBuffer*> ListSet;
44
45 public:
46 // Constructor for a read-write list, to which you may append values.
47 // FIXME: Remove all clients of this API, then remove this API.
48 MarkedArgumentBuffer()
49 : m_size(0)
50 , m_capacity(inlineCapacity)
51 , m_buffer(&m_inlineBuffer[m_capacity - 1])
52 , m_markSet(0)
53 {
54 }
55
56 ~MarkedArgumentBuffer()
57 {
58 if (m_markSet)
59 m_markSet->remove(this);
60
61 if (EncodedJSValue* base = mallocBase())
62 delete [] base;
63 }
64
65 size_t size() const { return m_size; }
66 bool isEmpty() const { return !m_size; }
67
68 JSValue at(int i) const
69 {
70 if (i >= m_size)
71 return jsUndefined();
72
73 return JSValue::decode(slotFor(i));
74 }
75
76 void clear()
77 {
78 m_size = 0;
79 }
80
81 void append(JSValue v)
82 {
83 if (m_size >= m_capacity)
84 return slowAppend(v);
85
86 slotFor(m_size) = JSValue::encode(v);
87 ++m_size;
88 }
89
90 void removeLast()
91 {
92 ASSERT(m_size);
93 m_size--;
94 }
95
96 JSValue last()
97 {
98 ASSERT(m_size);
99 return JSValue::decode(slotFor(m_size - 1));
100 }
101
102 static void markLists(HeapRootVisitor&, ListSet&);
103
104 private:
105 JS_EXPORT_PRIVATE void slowAppend(JSValue);
106
107 EncodedJSValue& slotFor(int item) const
108 {
109 return m_buffer[-item];
110 }
111
112 EncodedJSValue* mallocBase()
113 {
114 if (m_capacity == static_cast<int>(inlineCapacity))
115 return 0;
116 return &slotFor(m_capacity - 1);
117 }
118
119 int m_size;
120 int m_capacity;
121 EncodedJSValue m_inlineBuffer[inlineCapacity];
122 EncodedJSValue* m_buffer;
123 ListSet* m_markSet;
124
125 private:
126 // Prohibits new / delete, which would break GC.
127 void* operator new(size_t size)
128 {
129 return fastMalloc(size);
130 }
131 void operator delete(void* p)
132 {
133 fastFree(p);
134 }
135
136 void* operator new[](size_t);
137 void operator delete[](void*);
138
139 void* operator new(size_t, void*);
140 void operator delete(void*, size_t);
141 };
142
143 class ArgList {
144 friend class JIT;
145 public:
146 ArgList()
147 : m_args(0)
148 , m_argCount(0)
149 {
150 }
151
152 ArgList(ExecState* exec)
153 : m_args(reinterpret_cast<JSValue*>(&exec[CallFrame::argumentOffset(0)]))
154 , m_argCount(exec->argumentCount())
155 {
156 }
157
158 ArgList(const MarkedArgumentBuffer& args)
159 : m_args(reinterpret_cast<JSValue*>(args.m_buffer))
160 , m_argCount(args.size())
161 {
162 }
163
164 JSValue at(int i) const
165 {
166 if (i >= m_argCount)
167 return jsUndefined();
168 return m_args[-i];
169 }
170
171 bool isEmpty() const { return !m_argCount; }
172 size_t size() const { return m_argCount; }
173
174 JS_EXPORT_PRIVATE void getSlice(int startIndex, ArgList& result) const;
175
176 private:
177 JSValue* m_args;
178 int m_argCount;
179 };
180
181 } // namespace JSC
182
183 #endif // ArgList_h