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 };
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())
49 ASSERT(inherits(&s_info
));
51 // We have to manually ref and deref the symbol table as JSVariableObject
52 // doesn't know about SharedSymbolTable
53 static_cast<SharedSymbolTable
*>(m_symbolTable
)->ref();
56 JSActivation::~JSActivation()
58 static_cast<SharedSymbolTable
*>(m_symbolTable
)->deref();
61 void JSActivation::visitChildren(SlotVisitor
& visitor
)
63 ASSERT_GC_OBJECT_INHERITS(this, &s_info
);
64 COMPILE_ASSERT(StructureFlags
& OverridesVisitChildren
, OverridesVisitChildrenWithoutSettingFlag
);
65 ASSERT(structure()->typeInfo().overridesVisitChildren());
66 Base::visitChildren(visitor
);
68 // No need to mark our registers if they're still in the RegisterFile.
69 WriteBarrier
<Unknown
>* registerArray
= m_registerArray
.get();
73 visitor
.appendValues(registerArray
, m_numParametersMinusThis
);
75 // Skip the call frame, which sits between the parameters and vars.
76 visitor
.appendValues(registerArray
+ m_numParametersMinusThis
+ RegisterFile::CallFrameHeaderSize
, m_numCapturedVars
, MayContainNullValues
);
79 inline bool JSActivation::symbolTableGet(const Identifier
& propertyName
, PropertySlot
& slot
)
81 SymbolTableEntry entry
= symbolTable().inlineGet(propertyName
.impl());
84 if (entry
.getIndex() >= m_numCapturedVars
)
87 slot
.setValue(registerAt(entry
.getIndex()).get());
91 inline bool JSActivation::symbolTablePut(JSGlobalData
& globalData
, const Identifier
& propertyName
, JSValue value
)
93 ASSERT(!Heap::heap(value
) || Heap::heap(value
) == Heap::heap(this));
95 SymbolTableEntry entry
= symbolTable().inlineGet(propertyName
.impl());
98 if (entry
.isReadOnly())
100 if (entry
.getIndex() >= m_numCapturedVars
)
103 registerAt(entry
.getIndex()).set(globalData
, this, value
);
107 void JSActivation::getOwnPropertyNames(ExecState
* exec
, PropertyNameArray
& propertyNames
, EnumerationMode mode
)
109 SymbolTable::const_iterator end
= symbolTable().end();
110 for (SymbolTable::const_iterator it
= symbolTable().begin(); it
!= end
; ++it
) {
111 if (it
->second
.getAttributes() & DontEnum
&& mode
!= IncludeDontEnumProperties
)
113 if (it
->second
.getIndex() >= m_numCapturedVars
)
115 propertyNames
.add(Identifier(exec
, it
->first
.get()));
117 // Skip the JSVariableObject implementation of getOwnPropertyNames
118 JSObject::getOwnPropertyNames(exec
, propertyNames
, mode
);
121 inline bool JSActivation::symbolTablePutWithAttributes(JSGlobalData
& globalData
, const Identifier
& propertyName
, JSValue value
, unsigned attributes
)
123 ASSERT(!Heap::heap(value
) || Heap::heap(value
) == Heap::heap(this));
125 SymbolTable::iterator iter
= symbolTable().find(propertyName
.impl());
126 if (iter
== symbolTable().end())
128 SymbolTableEntry
& entry
= iter
->second
;
129 ASSERT(!entry
.isNull());
130 if (entry
.getIndex() >= m_numCapturedVars
)
133 entry
.setAttributes(attributes
);
134 registerAt(entry
.getIndex()).set(globalData
, this, value
);
138 bool JSActivation::getOwnPropertySlot(ExecState
* exec
, const Identifier
& propertyName
, PropertySlot
& slot
)
140 if (propertyName
== exec
->propertyNames().arguments
) {
141 slot
.setCustom(this, getArgumentsGetter());
145 if (symbolTableGet(propertyName
, slot
))
148 if (WriteBarrierBase
<Unknown
>* location
= getDirectLocation(exec
->globalData(), propertyName
)) {
149 slot
.setValue(location
->get());
153 // We don't call through to JSObject because there's no way to give an
154 // activation object getter properties or a prototype.
155 ASSERT(!hasGetterSetterProperties());
156 ASSERT(prototype().isNull());
160 void JSActivation::put(ExecState
* exec
, const Identifier
& propertyName
, JSValue value
, PutPropertySlot
& slot
)
162 ASSERT(!Heap::heap(value
) || Heap::heap(value
) == Heap::heap(this));
164 if (symbolTablePut(exec
->globalData(), propertyName
, value
))
167 // We don't call through to JSObject because __proto__ and getter/setter
168 // properties are non-standard extensions that other implementations do not
169 // expose in the activation object.
170 ASSERT(!hasGetterSetterProperties());
171 putDirect(exec
->globalData(), propertyName
, value
, 0, true, slot
);
174 // FIXME: Make this function honor ReadOnly (const) and DontEnum
175 void JSActivation::putWithAttributes(ExecState
* exec
, const Identifier
& propertyName
, JSValue value
, unsigned attributes
)
177 ASSERT(!Heap::heap(value
) || Heap::heap(value
) == Heap::heap(this));
179 if (symbolTablePutWithAttributes(exec
->globalData(), propertyName
, value
, attributes
))
182 // We don't call through to JSObject because __proto__ and getter/setter
183 // properties are non-standard extensions that other implementations do not
184 // expose in the activation object.
185 ASSERT(!hasGetterSetterProperties());
186 PutPropertySlot slot
;
187 JSObject::putWithAttributes(exec
, propertyName
, value
, attributes
, true, slot
);
190 bool JSActivation::deleteProperty(ExecState
* exec
, const Identifier
& propertyName
)
192 if (propertyName
== exec
->propertyNames().arguments
)
195 return Base::deleteProperty(exec
, propertyName
);
198 JSObject
* JSActivation::toThisObject(ExecState
* exec
) const
200 return exec
->globalThisValue();
203 JSValue
JSActivation::toStrictThisObject(ExecState
*) const
208 bool JSActivation::isDynamicScope(bool& requiresDynamicChecks
) const
210 requiresDynamicChecks
= m_requiresDynamicChecks
;
214 JSValue
JSActivation::argumentsGetter(ExecState
*, JSValue slotBase
, const Identifier
&)
216 JSActivation
* activation
= asActivation(slotBase
);
217 CallFrame
* callFrame
= CallFrame::create(reinterpret_cast<Register
*>(activation
->m_registers
));
218 int argumentsRegister
= activation
->m_argumentsRegister
;
219 if (JSValue arguments
= callFrame
->uncheckedR(argumentsRegister
).jsValue())
221 int realArgumentsRegister
= unmodifiedArgumentsRegister(argumentsRegister
);
223 JSValue arguments
= JSValue(new (callFrame
) Arguments(callFrame
));
224 callFrame
->uncheckedR(argumentsRegister
) = arguments
;
225 callFrame
->uncheckedR(realArgumentsRegister
) = arguments
;
227 ASSERT(callFrame
->uncheckedR(realArgumentsRegister
).jsValue().inherits(&Arguments::s_info
));
228 return callFrame
->uncheckedR(realArgumentsRegister
).jsValue();
231 // These two functions serve the purpose of isolating the common case from a
234 PropertySlot::GetValueFunc
JSActivation::getArgumentsGetter()
236 return argumentsGetter
;