2 * Copyright (C) 2008, 2009 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
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.
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.
30 #include "JSActivation.h"
32 #include "Arguments.h"
33 #include "Interpreter.h"
34 #include "JSFunction.h"
40 const ClassInfo JSActivation::s_info = { "JSActivation", &Base::s_info, 0, 0, CREATE_METHOD_TABLE(JSActivation) };
42 void JSActivation::visitChildren(JSCell* cell, SlotVisitor& visitor)
44 JSActivation* thisObject = jsCast<JSActivation*>(cell);
45 ASSERT_GC_OBJECT_INHERITS(thisObject, &s_info);
46 COMPILE_ASSERT(StructureFlags & OverridesVisitChildren, OverridesVisitChildrenWithoutSettingFlag);
47 ASSERT(thisObject->structure()->typeInfo().overridesVisitChildren());
48 Base::visitChildren(thisObject, visitor);
50 // No need to mark our registers if they're still in the JSStack.
51 if (!thisObject->isTornOff())
54 for (int i = 0; i < thisObject->symbolTable()->captureCount(); ++i)
55 visitor.append(&thisObject->storage()[i]);
58 inline bool JSActivation::symbolTableGet(PropertyName propertyName, PropertySlot& slot)
60 SymbolTableEntry entry = symbolTable()->inlineGet(propertyName.publicName());
64 // Defend against the inspector asking for a var after it has been optimized out.
65 if (isTornOff() && !isValid(entry))
68 slot.setValue(registerAt(entry.getIndex()).get());
72 inline bool JSActivation::symbolTableGet(PropertyName propertyName, PropertyDescriptor& descriptor)
74 SymbolTableEntry entry = symbolTable()->inlineGet(propertyName.publicName());
78 // Defend against the inspector asking for a var after it has been optimized out.
79 if (isTornOff() && !isValid(entry))
82 descriptor.setDescriptor(registerAt(entry.getIndex()).get(), entry.getAttributes());
86 inline bool JSActivation::symbolTablePut(ExecState* exec, PropertyName propertyName, JSValue value, bool shouldThrow)
88 JSGlobalData& globalData = exec->globalData();
89 ASSERT(!Heap::heap(value) || Heap::heap(value) == Heap::heap(this));
91 SymbolTableEntry entry = symbolTable()->inlineGet(propertyName.publicName());
94 if (entry.isReadOnly()) {
96 throwTypeError(exec, StrictModeReadonlyPropertyWriteError);
100 // Defend against the inspector asking for a var after it has been optimized out.
101 if (isTornOff() && !isValid(entry))
104 registerAt(entry.getIndex()).set(globalData, this, value);
108 void JSActivation::getOwnNonIndexPropertyNames(JSObject* object, ExecState* exec, PropertyNameArray& propertyNames, EnumerationMode mode)
110 JSActivation* thisObject = jsCast<JSActivation*>(object);
112 if (mode == IncludeDontEnumProperties && !thisObject->isTornOff())
113 propertyNames.add(exec->propertyNames().arguments);
115 SymbolTable::const_iterator end = thisObject->symbolTable()->end();
116 for (SymbolTable::const_iterator it = thisObject->symbolTable()->begin(); it != end; ++it) {
117 if (it->value.getAttributes() & DontEnum && mode != IncludeDontEnumProperties)
119 if (!thisObject->isValid(it->value))
121 propertyNames.add(Identifier(exec, it->key.get()));
123 // Skip the JSVariableObject implementation of getOwnNonIndexPropertyNames
124 JSObject::getOwnNonIndexPropertyNames(thisObject, exec, propertyNames, mode);
127 inline bool JSActivation::symbolTablePutWithAttributes(JSGlobalData& globalData, PropertyName propertyName, JSValue value, unsigned attributes)
129 ASSERT(!Heap::heap(value) || Heap::heap(value) == Heap::heap(this));
131 SymbolTable::iterator iter = symbolTable()->find(propertyName.publicName());
132 if (iter == symbolTable()->end())
134 SymbolTableEntry& entry = iter->value;
135 ASSERT(!entry.isNull());
139 entry.setAttributes(attributes);
140 registerAt(entry.getIndex()).set(globalData, this, value);
144 bool JSActivation::getOwnPropertySlot(JSCell* cell, ExecState* exec, PropertyName propertyName, PropertySlot& slot)
146 JSActivation* thisObject = jsCast<JSActivation*>(cell);
148 if (propertyName == exec->propertyNames().arguments) {
149 // Defend against the inspector asking for the arguments object after it has been optimized out.
150 if (!thisObject->isTornOff()) {
151 slot.setCustom(thisObject, thisObject->getArgumentsGetter());
156 if (thisObject->symbolTableGet(propertyName, slot))
159 if (WriteBarrierBase<Unknown>* location = thisObject->getDirectLocation(exec->globalData(), propertyName)) {
160 slot.setValue(location->get());
164 // We don't call through to JSObject because there's no way to give an
165 // activation object getter properties or a prototype.
166 ASSERT(!thisObject->hasGetterSetterProperties());
167 ASSERT(thisObject->prototype().isNull());
171 bool JSActivation::getOwnPropertyDescriptor(JSObject* object, ExecState* exec, PropertyName propertyName, PropertyDescriptor& descriptor)
173 JSActivation* thisObject = jsCast<JSActivation*>(object);
175 if (propertyName == exec->propertyNames().arguments) {
176 // Defend against the inspector asking for the arguments object after it has been optimized out.
177 if (!thisObject->isTornOff()) {
179 JSActivation::getOwnPropertySlot(thisObject, exec, propertyName, slot);
180 descriptor.setDescriptor(slot.getValue(exec, propertyName), DontEnum);
185 if (thisObject->symbolTableGet(propertyName, descriptor))
188 return Base::getOwnPropertyDescriptor(object, exec, propertyName, descriptor);
191 void JSActivation::put(JSCell* cell, ExecState* exec, PropertyName propertyName, JSValue value, PutPropertySlot& slot)
193 JSActivation* thisObject = jsCast<JSActivation*>(cell);
194 ASSERT(!Heap::heap(value) || Heap::heap(value) == Heap::heap(thisObject));
196 if (thisObject->symbolTablePut(exec, propertyName, value, slot.isStrictMode()))
199 // We don't call through to JSObject because __proto__ and getter/setter
200 // properties are non-standard extensions that other implementations do not
201 // expose in the activation object.
202 ASSERT(!thisObject->hasGetterSetterProperties());
203 thisObject->putOwnDataProperty(exec->globalData(), propertyName, value, slot);
206 // FIXME: Make this function honor ReadOnly (const) and DontEnum
207 void JSActivation::putDirectVirtual(JSObject* object, ExecState* exec, PropertyName propertyName, JSValue value, unsigned attributes)
209 JSActivation* thisObject = jsCast<JSActivation*>(object);
210 ASSERT(!Heap::heap(value) || Heap::heap(value) == Heap::heap(thisObject));
212 if (thisObject->symbolTablePutWithAttributes(exec->globalData(), propertyName, value, attributes))
215 // We don't call through to JSObject because __proto__ and getter/setter
216 // properties are non-standard extensions that other implementations do not
217 // expose in the activation object.
218 ASSERT(!thisObject->hasGetterSetterProperties());
219 JSObject::putDirectVirtual(thisObject, exec, propertyName, value, attributes);
222 bool JSActivation::deleteProperty(JSCell* cell, ExecState* exec, PropertyName propertyName)
224 if (propertyName == exec->propertyNames().arguments)
227 return Base::deleteProperty(cell, exec, propertyName);
230 JSObject* JSActivation::toThisObject(JSCell*, ExecState* exec)
232 return exec->globalThisValue();
235 JSValue JSActivation::argumentsGetter(ExecState*, JSValue slotBase, PropertyName)
237 JSActivation* activation = jsCast<JSActivation*>(slotBase);
238 if (activation->isTornOff())
239 return jsUndefined();
241 CallFrame* callFrame = CallFrame::create(reinterpret_cast<Register*>(activation->m_registers));
242 int argumentsRegister = callFrame->codeBlock()->argumentsRegister();
243 if (JSValue arguments = callFrame->uncheckedR(argumentsRegister).jsValue())
245 int realArgumentsRegister = unmodifiedArgumentsRegister(argumentsRegister);
247 JSValue arguments = JSValue(Arguments::create(callFrame->globalData(), callFrame));
248 callFrame->uncheckedR(argumentsRegister) = arguments;
249 callFrame->uncheckedR(realArgumentsRegister) = arguments;
251 ASSERT(callFrame->uncheckedR(realArgumentsRegister).jsValue().inherits(&Arguments::s_info));
252 return callFrame->uncheckedR(realArgumentsRegister).jsValue();
255 // These two functions serve the purpose of isolating the common case from a
258 PropertySlot::GetValueFunc JSActivation::getArgumentsGetter()
260 return argumentsGetter;