]> git.saurik.com Git - apple/javascriptcore.git/blob - runtime/JSActivation.cpp
JavaScriptCore-1097.3.3.tar.gz
[apple/javascriptcore.git] / runtime / JSActivation.cpp
1 /*
2 * Copyright (C) 2008, 2009 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 *
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 * 3. Neither the name of Apple Computer, Inc. ("Apple") nor the names of
14 * its contributors may be used to endorse or promote products derived
15 * from this software without specific prior written permission.
16 *
17 * THIS SOFTWARE IS PROVIDED BY APPLE AND ITS CONTRIBUTORS "AS IS" AND ANY
18 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
19 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
20 * DISCLAIMED. IN NO EVENT SHALL APPLE OR ITS CONTRIBUTORS BE LIABLE FOR ANY
21 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
22 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
23 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
24 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 */
28
29 #include "config.h"
30 #include "JSActivation.h"
31
32 #include "Arguments.h"
33 #include "Interpreter.h"
34 #include "JSFunction.h"
35
36 using namespace std;
37
38 namespace JSC {
39
40 ASSERT_CLASS_FITS_IN_CELL(JSActivation);
41
42 const ClassInfo JSActivation::s_info = { "JSActivation", &Base::s_info, 0, 0, CREATE_METHOD_TABLE(JSActivation) };
43
44 JSActivation::JSActivation(CallFrame* callFrame, FunctionExecutable* functionExecutable)
45 : Base(callFrame->globalData(), callFrame->globalData().activationStructure.get(), functionExecutable->symbolTable(), callFrame->registers())
46 , m_numCapturedArgs(max(callFrame->argumentCount(), functionExecutable->parameterCount()))
47 , m_numCapturedVars(functionExecutable->capturedVariableCount())
48 , m_isTornOff(false)
49 , m_requiresDynamicChecks(functionExecutable->usesEval() && !functionExecutable->isStrictMode())
50 , m_argumentsRegister(functionExecutable->generatedBytecode().argumentsRegister())
51 {
52 }
53
54 void JSActivation::finishCreation(CallFrame* callFrame)
55 {
56 Base::finishCreation(callFrame->globalData());
57 ASSERT(inherits(&s_info));
58
59 // We have to manually ref and deref the symbol table as JSVariableObject
60 // doesn't know about SharedSymbolTable
61 static_cast<SharedSymbolTable*>(m_symbolTable)->ref();
62 callFrame->globalData().heap.addFinalizer(this, &finalize);
63 }
64
65 void JSActivation::finalize(JSCell* cell)
66 {
67 static_cast<SharedSymbolTable*>(jsCast<JSActivation*>(cell)->m_symbolTable)->deref();
68 }
69
70 void JSActivation::visitChildren(JSCell* cell, SlotVisitor& visitor)
71 {
72 JSActivation* thisObject = jsCast<JSActivation*>(cell);
73 ASSERT_GC_OBJECT_INHERITS(thisObject, &s_info);
74 COMPILE_ASSERT(StructureFlags & OverridesVisitChildren, OverridesVisitChildrenWithoutSettingFlag);
75 ASSERT(thisObject->structure()->typeInfo().overridesVisitChildren());
76 Base::visitChildren(thisObject, visitor);
77
78 // No need to mark our registers if they're still in the RegisterFile.
79 WriteBarrier<Unknown>* registerArray = thisObject->m_registerArray.get();
80 if (!registerArray)
81 return;
82
83 visitor.appendValues(registerArray, thisObject->m_numCapturedArgs);
84
85 // Skip 'this' and call frame, except for callee and scope chain.
86 int offset = CallFrame::offsetFor(thisObject->m_numCapturedArgs + 1);
87 visitor.append(registerArray + offset + RegisterFile::ScopeChain);
88 visitor.append(registerArray + offset + RegisterFile::Callee);
89
90 visitor.appendValues(registerArray + offset, thisObject->m_numCapturedVars);
91 }
92
93 inline bool JSActivation::symbolTableGet(const Identifier& propertyName, PropertySlot& slot)
94 {
95 SymbolTableEntry entry = symbolTable().inlineGet(propertyName.impl());
96 if (entry.isNull())
97 return false;
98 if (m_isTornOff && entry.getIndex() >= m_numCapturedVars)
99 return false;
100
101 slot.setValue(registerAt(entry.getIndex()).get());
102 return true;
103 }
104
105 inline bool JSActivation::symbolTablePut(ExecState* exec, const Identifier& propertyName, JSValue value, bool shouldThrow)
106 {
107 JSGlobalData& globalData = exec->globalData();
108 ASSERT(!Heap::heap(value) || Heap::heap(value) == Heap::heap(this));
109
110 SymbolTableEntry entry = symbolTable().inlineGet(propertyName.impl());
111 if (entry.isNull())
112 return false;
113 if (entry.isReadOnly()) {
114 if (shouldThrow)
115 throwTypeError(exec, StrictModeReadonlyPropertyWriteError);
116 return true;
117 }
118 if (m_isTornOff && entry.getIndex() >= m_numCapturedVars)
119 return false;
120
121 registerAt(entry.getIndex()).set(globalData, this, value);
122 return true;
123 }
124
125 void JSActivation::getOwnPropertyNames(JSObject* object, ExecState* exec, PropertyNameArray& propertyNames, EnumerationMode mode)
126 {
127 JSActivation* thisObject = jsCast<JSActivation*>(object);
128 SymbolTable::const_iterator end = thisObject->symbolTable().end();
129 for (SymbolTable::const_iterator it = thisObject->symbolTable().begin(); it != end; ++it) {
130 if (it->second.getAttributes() & DontEnum && mode != IncludeDontEnumProperties)
131 continue;
132 if (it->second.getIndex() >= thisObject->m_numCapturedVars)
133 continue;
134 propertyNames.add(Identifier(exec, it->first.get()));
135 }
136 // Skip the JSVariableObject implementation of getOwnPropertyNames
137 JSObject::getOwnPropertyNames(thisObject, exec, propertyNames, mode);
138 }
139
140 inline bool JSActivation::symbolTablePutWithAttributes(JSGlobalData& globalData, const Identifier& propertyName, JSValue value, unsigned attributes)
141 {
142 ASSERT(!Heap::heap(value) || Heap::heap(value) == Heap::heap(this));
143
144 SymbolTable::iterator iter = symbolTable().find(propertyName.impl());
145 if (iter == symbolTable().end())
146 return false;
147 SymbolTableEntry& entry = iter->second;
148 ASSERT(!entry.isNull());
149 if (entry.getIndex() >= m_numCapturedVars)
150 return false;
151
152 entry.setAttributes(attributes);
153 registerAt(entry.getIndex()).set(globalData, this, value);
154 return true;
155 }
156
157 bool JSActivation::getOwnPropertySlot(JSCell* cell, ExecState* exec, const Identifier& propertyName, PropertySlot& slot)
158 {
159 JSActivation* thisObject = jsCast<JSActivation*>(cell);
160 if (propertyName == exec->propertyNames().arguments) {
161 slot.setCustom(thisObject, thisObject->getArgumentsGetter());
162 return true;
163 }
164
165 if (thisObject->symbolTableGet(propertyName, slot))
166 return true;
167
168 if (WriteBarrierBase<Unknown>* location = thisObject->getDirectLocation(exec->globalData(), propertyName)) {
169 slot.setValue(location->get());
170 return true;
171 }
172
173 // We don't call through to JSObject because there's no way to give an
174 // activation object getter properties or a prototype.
175 ASSERT(!thisObject->hasGetterSetterProperties());
176 ASSERT(thisObject->prototype().isNull());
177 return false;
178 }
179
180 void JSActivation::put(JSCell* cell, ExecState* exec, const Identifier& propertyName, JSValue value, PutPropertySlot& slot)
181 {
182 JSActivation* thisObject = jsCast<JSActivation*>(cell);
183 ASSERT(!Heap::heap(value) || Heap::heap(value) == Heap::heap(thisObject));
184
185 if (thisObject->symbolTablePut(exec, propertyName, value, slot.isStrictMode()))
186 return;
187
188 // We don't call through to JSObject because __proto__ and getter/setter
189 // properties are non-standard extensions that other implementations do not
190 // expose in the activation object.
191 ASSERT(!thisObject->hasGetterSetterProperties());
192 thisObject->putOwnDataProperty(exec->globalData(), propertyName, value, slot);
193 }
194
195 // FIXME: Make this function honor ReadOnly (const) and DontEnum
196 void JSActivation::putDirectVirtual(JSObject* object, ExecState* exec, const Identifier& propertyName, JSValue value, unsigned attributes)
197 {
198 JSActivation* thisObject = jsCast<JSActivation*>(object);
199 ASSERT(!Heap::heap(value) || Heap::heap(value) == Heap::heap(thisObject));
200
201 if (thisObject->symbolTablePutWithAttributes(exec->globalData(), propertyName, value, attributes))
202 return;
203
204 // We don't call through to JSObject because __proto__ and getter/setter
205 // properties are non-standard extensions that other implementations do not
206 // expose in the activation object.
207 ASSERT(!thisObject->hasGetterSetterProperties());
208 JSObject::putDirectVirtual(thisObject, exec, propertyName, value, attributes);
209 }
210
211 bool JSActivation::deleteProperty(JSCell* cell, ExecState* exec, const Identifier& propertyName)
212 {
213 if (propertyName == exec->propertyNames().arguments)
214 return false;
215
216 return Base::deleteProperty(cell, exec, propertyName);
217 }
218
219 JSObject* JSActivation::toThisObject(JSCell*, ExecState* exec)
220 {
221 return exec->globalThisValue();
222 }
223
224 JSValue JSActivation::argumentsGetter(ExecState*, JSValue slotBase, const Identifier&)
225 {
226 JSActivation* activation = asActivation(slotBase);
227 CallFrame* callFrame = CallFrame::create(reinterpret_cast<Register*>(activation->m_registers));
228 int argumentsRegister = activation->m_argumentsRegister;
229 if (JSValue arguments = callFrame->uncheckedR(argumentsRegister).jsValue())
230 return arguments;
231 int realArgumentsRegister = unmodifiedArgumentsRegister(argumentsRegister);
232
233 JSValue arguments = JSValue(Arguments::create(callFrame->globalData(), callFrame));
234 callFrame->uncheckedR(argumentsRegister) = arguments;
235 callFrame->uncheckedR(realArgumentsRegister) = arguments;
236
237 ASSERT(callFrame->uncheckedR(realArgumentsRegister).jsValue().inherits(&Arguments::s_info));
238 return callFrame->uncheckedR(realArgumentsRegister).jsValue();
239 }
240
241 // These two functions serve the purpose of isolating the common case from a
242 // PIC branch.
243
244 PropertySlot::GetValueFunc JSActivation::getArgumentsGetter()
245 {
246 return argumentsGetter;
247 }
248
249 } // namespace JSC