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"
38 ASSERT_CLASS_FITS_IN_CELL(JSActivation);
40 const ClassInfo JSActivation::s_info = { "JSActivation", &Base::s_info, 0, 0, CREATE_METHOD_TABLE(JSActivation) };
42 JSActivation::JSActivation(CallFrame* callFrame, FunctionExecutable* functionExecutable)
43 : Base(callFrame->globalData(), callFrame->globalData().activationStructure.get(), functionExecutable->symbolTable(), callFrame->registers())
44 , m_numParametersMinusThis(static_cast<int>(functionExecutable->parameterCount()))
45 , m_numCapturedVars(functionExecutable->capturedVariableCount())
46 , m_requiresDynamicChecks(functionExecutable->usesEval())
47 , m_argumentsRegister(functionExecutable->generatedBytecode().argumentsRegister())
51 void JSActivation::finishCreation(CallFrame* callFrame)
53 Base::finishCreation(callFrame->globalData());
54 ASSERT(inherits(&s_info));
56 // We have to manually ref and deref the symbol table as JSVariableObject
57 // doesn't know about SharedSymbolTable
58 static_cast<SharedSymbolTable*>(m_symbolTable)->ref();
59 callFrame->globalData().heap.addFinalizer(this, &finalize);
62 void JSActivation::finalize(JSCell* cell)
64 static_cast<SharedSymbolTable*>(jsCast<JSActivation*>(cell)->m_symbolTable)->deref();
67 void JSActivation::visitChildren(JSCell* cell, SlotVisitor& visitor)
69 JSActivation* thisObject = jsCast<JSActivation*>(cell);
70 ASSERT_GC_OBJECT_INHERITS(thisObject, &s_info);
71 COMPILE_ASSERT(StructureFlags & OverridesVisitChildren, OverridesVisitChildrenWithoutSettingFlag);
72 ASSERT(thisObject->structure()->typeInfo().overridesVisitChildren());
73 Base::visitChildren(thisObject, visitor);
75 // No need to mark our registers if they're still in the RegisterFile.
76 WriteBarrier<Unknown>* registerArray = thisObject->m_registerArray.get();
80 visitor.appendValues(registerArray, thisObject->m_numParametersMinusThis);
82 // Skip the call frame, which sits between the parameters and vars.
83 visitor.appendValues(registerArray + thisObject->m_numParametersMinusThis + RegisterFile::CallFrameHeaderSize, thisObject->m_numCapturedVars);
86 inline bool JSActivation::symbolTableGet(const Identifier& propertyName, PropertySlot& slot)
88 SymbolTableEntry entry = symbolTable().inlineGet(propertyName.impl());
91 if (entry.getIndex() >= m_numCapturedVars)
94 slot.setValue(registerAt(entry.getIndex()).get());
98 inline bool JSActivation::symbolTablePut(JSGlobalData& globalData, const Identifier& propertyName, JSValue value)
100 ASSERT(!Heap::heap(value) || Heap::heap(value) == Heap::heap(this));
102 SymbolTableEntry entry = symbolTable().inlineGet(propertyName.impl());
105 if (entry.isReadOnly())
107 if (entry.getIndex() >= m_numCapturedVars)
110 registerAt(entry.getIndex()).set(globalData, this, value);
114 void JSActivation::getOwnPropertyNames(JSObject* object, ExecState* exec, PropertyNameArray& propertyNames, EnumerationMode mode)
116 JSActivation* thisObject = jsCast<JSActivation*>(object);
117 SymbolTable::const_iterator end = thisObject->symbolTable().end();
118 for (SymbolTable::const_iterator it = thisObject->symbolTable().begin(); it != end; ++it) {
119 if (it->second.getAttributes() & DontEnum && mode != IncludeDontEnumProperties)
121 if (it->second.getIndex() >= thisObject->m_numCapturedVars)
123 propertyNames.add(Identifier(exec, it->first.get()));
125 // Skip the JSVariableObject implementation of getOwnPropertyNames
126 JSObject::getOwnPropertyNames(thisObject, exec, propertyNames, mode);
129 inline bool JSActivation::symbolTablePutWithAttributes(JSGlobalData& globalData, const Identifier& propertyName, JSValue value, unsigned attributes)
131 ASSERT(!Heap::heap(value) || Heap::heap(value) == Heap::heap(this));
133 SymbolTable::iterator iter = symbolTable().find(propertyName.impl());
134 if (iter == symbolTable().end())
136 SymbolTableEntry& entry = iter->second;
137 ASSERT(!entry.isNull());
138 if (entry.getIndex() >= m_numCapturedVars)
141 entry.setAttributes(attributes);
142 registerAt(entry.getIndex()).set(globalData, this, value);
146 bool JSActivation::getOwnPropertySlot(JSCell* cell, ExecState* exec, const Identifier& propertyName, PropertySlot& slot)
148 JSActivation* thisObject = jsCast<JSActivation*>(cell);
149 if (propertyName == exec->propertyNames().arguments) {
150 slot.setCustom(thisObject, thisObject->getArgumentsGetter());
154 if (thisObject->symbolTableGet(propertyName, slot))
157 if (WriteBarrierBase<Unknown>* location = thisObject->getDirectLocation(exec->globalData(), propertyName)) {
158 slot.setValue(location->get());
162 // We don't call through to JSObject because there's no way to give an
163 // activation object getter properties or a prototype.
164 ASSERT(!thisObject->hasGetterSetterProperties());
165 ASSERT(thisObject->prototype().isNull());
169 void JSActivation::put(JSCell* cell, ExecState* exec, const Identifier& propertyName, JSValue value, PutPropertySlot& slot)
171 JSActivation* thisObject = jsCast<JSActivation*>(cell);
172 ASSERT(!Heap::heap(value) || Heap::heap(value) == Heap::heap(thisObject));
174 if (thisObject->symbolTablePut(exec->globalData(), propertyName, value))
177 // We don't call through to JSObject because __proto__ and getter/setter
178 // properties are non-standard extensions that other implementations do not
179 // expose in the activation object.
180 ASSERT(!thisObject->hasGetterSetterProperties());
181 thisObject->putDirect(exec->globalData(), propertyName, value, 0, true, slot);
184 // FIXME: Make this function honor ReadOnly (const) and DontEnum
185 void JSActivation::putWithAttributes(JSObject* object, ExecState* exec, const Identifier& propertyName, JSValue value, unsigned attributes)
187 JSActivation* thisObject = jsCast<JSActivation*>(object);
188 ASSERT(!Heap::heap(value) || Heap::heap(value) == Heap::heap(thisObject));
190 if (thisObject->symbolTablePutWithAttributes(exec->globalData(), propertyName, value, attributes))
193 // We don't call through to JSObject because __proto__ and getter/setter
194 // properties are non-standard extensions that other implementations do not
195 // expose in the activation object.
196 ASSERT(!thisObject->hasGetterSetterProperties());
197 JSObject::putWithAttributes(thisObject, exec, propertyName, value, attributes);
200 bool JSActivation::deleteProperty(JSCell* cell, ExecState* exec, const Identifier& propertyName)
202 if (propertyName == exec->propertyNames().arguments)
205 return Base::deleteProperty(cell, exec, propertyName);
208 JSObject* JSActivation::toThisObject(JSCell*, ExecState* exec)
210 return exec->globalThisValue();
213 JSValue JSActivation::argumentsGetter(ExecState*, JSValue slotBase, const Identifier&)
215 JSActivation* activation = asActivation(slotBase);
216 CallFrame* callFrame = CallFrame::create(reinterpret_cast<Register*>(activation->m_registers));
217 int argumentsRegister = activation->m_argumentsRegister;
218 if (JSValue arguments = callFrame->uncheckedR(argumentsRegister).jsValue())
220 int realArgumentsRegister = unmodifiedArgumentsRegister(argumentsRegister);
222 JSValue arguments = JSValue(Arguments::create(callFrame->globalData(), callFrame));
223 callFrame->uncheckedR(argumentsRegister) = arguments;
224 callFrame->uncheckedR(realArgumentsRegister) = arguments;
226 ASSERT(callFrame->uncheckedR(realArgumentsRegister).jsValue().inherits(&Arguments::s_info));
227 return callFrame->uncheckedR(realArgumentsRegister).jsValue();
230 // These two functions serve the purpose of isolating the common case from a
233 PropertySlot::GetValueFunc JSActivation::getArgumentsGetter()
235 return argumentsGetter;