]> git.saurik.com Git - apple/javascriptcore.git/blame - runtime/JSActivation.cpp
JavaScriptCore-1097.3.3.tar.gz
[apple/javascriptcore.git] / runtime / JSActivation.cpp
CommitLineData
9dae56ea 1/*
f9bf01c6 2 * Copyright (C) 2008, 2009 Apple Inc. All rights reserved.
9dae56ea
A
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
6fe7ccc8
A
36using namespace std;
37
9dae56ea
A
38namespace JSC {
39
40ASSERT_CLASS_FITS_IN_CELL(JSActivation);
41
6fe7ccc8 42const ClassInfo JSActivation::s_info = { "JSActivation", &Base::s_info, 0, 0, CREATE_METHOD_TABLE(JSActivation) };
14957cd0
A
43
44JSActivation::JSActivation(CallFrame* callFrame, FunctionExecutable* functionExecutable)
45 : Base(callFrame->globalData(), callFrame->globalData().activationStructure.get(), functionExecutable->symbolTable(), callFrame->registers())
6fe7ccc8 46 , m_numCapturedArgs(max(callFrame->argumentCount(), functionExecutable->parameterCount()))
14957cd0 47 , m_numCapturedVars(functionExecutable->capturedVariableCount())
6fe7ccc8
A
48 , m_isTornOff(false)
49 , m_requiresDynamicChecks(functionExecutable->usesEval() && !functionExecutable->isStrictMode())
14957cd0 50 , m_argumentsRegister(functionExecutable->generatedBytecode().argumentsRegister())
9dae56ea 51{
6fe7ccc8
A
52}
53
54void JSActivation::finishCreation(CallFrame* callFrame)
55{
56 Base::finishCreation(callFrame->globalData());
14957cd0
A
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();
6fe7ccc8 62 callFrame->globalData().heap.addFinalizer(this, &finalize);
9dae56ea
A
63}
64
6fe7ccc8 65void JSActivation::finalize(JSCell* cell)
9dae56ea 66{
6fe7ccc8 67 static_cast<SharedSymbolTable*>(jsCast<JSActivation*>(cell)->m_symbolTable)->deref();
9dae56ea
A
68}
69
6fe7ccc8 70void JSActivation::visitChildren(JSCell* cell, SlotVisitor& visitor)
9dae56ea 71{
6fe7ccc8
A
72 JSActivation* thisObject = jsCast<JSActivation*>(cell);
73 ASSERT_GC_OBJECT_INHERITS(thisObject, &s_info);
14957cd0 74 COMPILE_ASSERT(StructureFlags & OverridesVisitChildren, OverridesVisitChildrenWithoutSettingFlag);
6fe7ccc8
A
75 ASSERT(thisObject->structure()->typeInfo().overridesVisitChildren());
76 Base::visitChildren(thisObject, visitor);
9dae56ea 77
14957cd0 78 // No need to mark our registers if they're still in the RegisterFile.
6fe7ccc8 79 WriteBarrier<Unknown>* registerArray = thisObject->m_registerArray.get();
9dae56ea
A
80 if (!registerArray)
81 return;
6fe7ccc8
A
82
83 visitor.appendValues(registerArray, thisObject->m_numCapturedArgs);
9dae56ea 84
6fe7ccc8
A
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);
14957cd0 91}
9dae56ea 92
14957cd0
A
93inline bool JSActivation::symbolTableGet(const Identifier& propertyName, PropertySlot& slot)
94{
95 SymbolTableEntry entry = symbolTable().inlineGet(propertyName.impl());
96 if (entry.isNull())
97 return false;
6fe7ccc8 98 if (m_isTornOff && entry.getIndex() >= m_numCapturedVars)
14957cd0 99 return false;
9dae56ea 100
14957cd0
A
101 slot.setValue(registerAt(entry.getIndex()).get());
102 return true;
9dae56ea
A
103}
104
6fe7ccc8 105inline bool JSActivation::symbolTablePut(ExecState* exec, const Identifier& propertyName, JSValue value, bool shouldThrow)
9dae56ea 106{
6fe7ccc8 107 JSGlobalData& globalData = exec->globalData();
14957cd0
A
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;
6fe7ccc8
A
113 if (entry.isReadOnly()) {
114 if (shouldThrow)
115 throwTypeError(exec, StrictModeReadonlyPropertyWriteError);
9dae56ea 116 return true;
6fe7ccc8
A
117 }
118 if (m_isTornOff && entry.getIndex() >= m_numCapturedVars)
14957cd0 119 return false;
9dae56ea 120
14957cd0
A
121 registerAt(entry.getIndex()).set(globalData, this, value);
122 return true;
123}
124
6fe7ccc8 125void JSActivation::getOwnPropertyNames(JSObject* object, ExecState* exec, PropertyNameArray& propertyNames, EnumerationMode mode)
14957cd0 126{
6fe7ccc8
A
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) {
14957cd0
A
130 if (it->second.getAttributes() & DontEnum && mode != IncludeDontEnumProperties)
131 continue;
6fe7ccc8 132 if (it->second.getIndex() >= thisObject->m_numCapturedVars)
14957cd0
A
133 continue;
134 propertyNames.add(Identifier(exec, it->first.get()));
9dae56ea 135 }
14957cd0 136 // Skip the JSVariableObject implementation of getOwnPropertyNames
6fe7ccc8 137 JSObject::getOwnPropertyNames(thisObject, exec, propertyNames, mode);
14957cd0
A
138}
139
140inline 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}
9dae56ea 156
6fe7ccc8 157bool JSActivation::getOwnPropertySlot(JSCell* cell, ExecState* exec, const Identifier& propertyName, PropertySlot& slot)
14957cd0 158{
6fe7ccc8 159 JSActivation* thisObject = jsCast<JSActivation*>(cell);
9dae56ea 160 if (propertyName == exec->propertyNames().arguments) {
6fe7ccc8 161 slot.setCustom(thisObject, thisObject->getArgumentsGetter());
9dae56ea
A
162 return true;
163 }
164
6fe7ccc8 165 if (thisObject->symbolTableGet(propertyName, slot))
14957cd0
A
166 return true;
167
6fe7ccc8 168 if (WriteBarrierBase<Unknown>* location = thisObject->getDirectLocation(exec->globalData(), propertyName)) {
14957cd0
A
169 slot.setValue(location->get());
170 return true;
171 }
172
9dae56ea
A
173 // We don't call through to JSObject because there's no way to give an
174 // activation object getter properties or a prototype.
6fe7ccc8
A
175 ASSERT(!thisObject->hasGetterSetterProperties());
176 ASSERT(thisObject->prototype().isNull());
9dae56ea
A
177 return false;
178}
179
6fe7ccc8 180void JSActivation::put(JSCell* cell, ExecState* exec, const Identifier& propertyName, JSValue value, PutPropertySlot& slot)
9dae56ea 181{
6fe7ccc8
A
182 JSActivation* thisObject = jsCast<JSActivation*>(cell);
183 ASSERT(!Heap::heap(value) || Heap::heap(value) == Heap::heap(thisObject));
9dae56ea 184
6fe7ccc8 185 if (thisObject->symbolTablePut(exec, propertyName, value, slot.isStrictMode()))
9dae56ea
A
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.
6fe7ccc8
A
191 ASSERT(!thisObject->hasGetterSetterProperties());
192 thisObject->putOwnDataProperty(exec->globalData(), propertyName, value, slot);
9dae56ea
A
193}
194
195// FIXME: Make this function honor ReadOnly (const) and DontEnum
6fe7ccc8 196void JSActivation::putDirectVirtual(JSObject* object, ExecState* exec, const Identifier& propertyName, JSValue value, unsigned attributes)
9dae56ea 197{
6fe7ccc8
A
198 JSActivation* thisObject = jsCast<JSActivation*>(object);
199 ASSERT(!Heap::heap(value) || Heap::heap(value) == Heap::heap(thisObject));
9dae56ea 200
6fe7ccc8 201 if (thisObject->symbolTablePutWithAttributes(exec->globalData(), propertyName, value, attributes))
9dae56ea
A
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.
6fe7ccc8
A
207 ASSERT(!thisObject->hasGetterSetterProperties());
208 JSObject::putDirectVirtual(thisObject, exec, propertyName, value, attributes);
9dae56ea
A
209}
210
6fe7ccc8 211bool JSActivation::deleteProperty(JSCell* cell, ExecState* exec, const Identifier& propertyName)
9dae56ea
A
212{
213 if (propertyName == exec->propertyNames().arguments)
214 return false;
215
6fe7ccc8 216 return Base::deleteProperty(cell, exec, propertyName);
9dae56ea
A
217}
218
6fe7ccc8 219JSObject* JSActivation::toThisObject(JSCell*, ExecState* exec)
9dae56ea
A
220{
221 return exec->globalThisValue();
222}
223
14957cd0 224JSValue JSActivation::argumentsGetter(ExecState*, JSValue slotBase, const Identifier&)
9dae56ea 225{
4e4e5a6f 226 JSActivation* activation = asActivation(slotBase);
14957cd0
A
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
6fe7ccc8 233 JSValue arguments = JSValue(Arguments::create(callFrame->globalData(), callFrame));
14957cd0
A
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();
9dae56ea
A
239}
240
241// These two functions serve the purpose of isolating the common case from a
242// PIC branch.
243
244PropertySlot::GetValueFunc JSActivation::getArgumentsGetter()
245{
246 return argumentsGetter;
247}
248
249} // namespace JSC