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 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"
35 #include "JSCInlines.h"
41 const ClassInfo JSActivation::s_info = { "JSActivation", &Base::s_info, 0, CREATE_METHOD_TABLE(JSActivation) };
43 void JSActivation::visitChildren(JSCell* cell, SlotVisitor& visitor)
45 JSActivation* thisObject = jsCast<JSActivation*>(cell);
46 ASSERT_GC_OBJECT_INHERITS(thisObject, info());
47 Base::visitChildren(thisObject, visitor);
49 // No need to mark our registers if they're still in the JSStack.
50 if (!thisObject->isTornOff())
53 for (int i = 0; i < thisObject->symbolTable()->captureCount(); ++i)
54 visitor.append(&thisObject->storage()[i]);
57 inline bool JSActivation::symbolTableGet(PropertyName propertyName, PropertySlot& slot)
59 SymbolTableEntry entry = symbolTable()->inlineGet(propertyName.uid());
63 // Defend against the inspector asking for a var after it has been optimized out.
64 if (isTornOff() && !isValid(entry))
67 slot.setValue(this, DontEnum, registerAt(entry.getIndex()).get());
71 inline bool JSActivation::symbolTableGet(PropertyName propertyName, PropertyDescriptor& descriptor)
73 SymbolTableEntry entry = symbolTable()->inlineGet(propertyName.uid());
77 // Defend against the inspector asking for a var after it has been optimized out.
78 if (isTornOff() && !isValid(entry))
81 descriptor.setDescriptor(registerAt(entry.getIndex()).get(), entry.getAttributes());
85 inline bool JSActivation::symbolTablePut(ExecState* exec, PropertyName propertyName, JSValue value, bool shouldThrow)
88 ASSERT(!Heap::heap(value) || Heap::heap(value) == Heap::heap(this));
90 WriteBarrierBase<Unknown>* reg;
92 GCSafeConcurrentJITLocker locker(symbolTable()->m_lock, exec->vm().heap);
93 SymbolTable::Map::iterator iter = symbolTable()->find(locker, propertyName.uid());
94 if (iter == symbolTable()->end(locker))
96 ASSERT(!iter->value.isNull());
97 if (iter->value.isReadOnly()) {
99 throwTypeError(exec, StrictModeReadonlyPropertyWriteError);
102 // Defend against the inspector asking for a var after it has been optimized out.
103 if (isTornOff() && !isValid(iter->value))
105 if (VariableWatchpointSet* set = iter->value.watchpointSet())
106 set->invalidate(VariableWriteFireDetail(this, propertyName)); // Don't mess around - if we had found this statically, we would have invcalidated it.
107 reg = ®isterAt(iter->value.getIndex());
109 reg->set(vm, this, value);
113 void JSActivation::getOwnNonIndexPropertyNames(JSObject* object, ExecState* exec, PropertyNameArray& propertyNames, EnumerationMode mode)
115 JSActivation* thisObject = jsCast<JSActivation*>(object);
117 CallFrame* callFrame = CallFrame::create(reinterpret_cast<Register*>(thisObject->m_registers));
118 if (shouldIncludeDontEnumProperties(mode) && !thisObject->isTornOff() && (callFrame->codeBlock()->usesArguments() || callFrame->codeBlock()->usesEval()))
119 propertyNames.add(exec->propertyNames().arguments);
122 ConcurrentJITLocker locker(thisObject->symbolTable()->m_lock);
123 SymbolTable::Map::iterator end = thisObject->symbolTable()->end(locker);
124 for (SymbolTable::Map::iterator it = thisObject->symbolTable()->begin(locker); it != end; ++it) {
125 if (it->value.getAttributes() & DontEnum && !shouldIncludeDontEnumProperties(mode))
127 if (!thisObject->isValid(it->value))
129 propertyNames.add(Identifier(exec, it->key.get()));
132 // Skip the JSEnvironmentRecord implementation of getOwnNonIndexPropertyNames
133 JSObject::getOwnNonIndexPropertyNames(thisObject, exec, propertyNames, mode);
136 inline bool JSActivation::symbolTablePutWithAttributes(VM& vm, PropertyName propertyName, JSValue value, unsigned attributes)
138 ASSERT(!Heap::heap(value) || Heap::heap(value) == Heap::heap(this));
140 WriteBarrierBase<Unknown>* reg;
142 ConcurrentJITLocker locker(symbolTable()->m_lock);
143 SymbolTable::Map::iterator iter = symbolTable()->find(locker, propertyName.uid());
144 if (iter == symbolTable()->end(locker))
146 SymbolTableEntry& entry = iter->value;
147 ASSERT(!entry.isNull());
151 entry.setAttributes(attributes);
152 reg = ®isterAt(entry.getIndex());
154 reg->set(vm, this, value);
158 bool JSActivation::getOwnPropertySlot(JSObject* object, ExecState* exec, PropertyName propertyName, PropertySlot& slot)
160 JSActivation* thisObject = jsCast<JSActivation*>(object);
162 if (propertyName == exec->propertyNames().arguments) {
163 // Defend against the inspector asking for the arguments object after it has been optimized out.
164 CallFrame* callFrame = CallFrame::create(reinterpret_cast<Register*>(thisObject->m_registers));
165 if (!thisObject->isTornOff() && (callFrame->codeBlock()->usesArguments() || callFrame->codeBlock()->usesEval())) {
166 slot.setCustom(thisObject, DontEnum, argumentsGetter);
171 if (thisObject->symbolTableGet(propertyName, slot))
175 if (JSValue value = thisObject->getDirect(exec->vm(), propertyName, attributes)) {
176 slot.setValue(thisObject, attributes, value);
180 // We don't call through to JSObject because there's no way to give an
181 // activation object getter properties or a prototype.
182 ASSERT(!thisObject->hasGetterSetterProperties());
183 ASSERT(thisObject->prototype().isNull());
187 void JSActivation::put(JSCell* cell, ExecState* exec, PropertyName propertyName, JSValue value, PutPropertySlot& slot)
189 JSActivation* thisObject = jsCast<JSActivation*>(cell);
190 ASSERT(!Heap::heap(value) || Heap::heap(value) == Heap::heap(thisObject));
192 if (thisObject->symbolTablePut(exec, propertyName, value, slot.isStrictMode()))
195 // We don't call through to JSObject because __proto__ and getter/setter
196 // properties are non-standard extensions that other implementations do not
197 // expose in the activation object.
198 ASSERT(!thisObject->hasGetterSetterProperties());
199 thisObject->putOwnDataProperty(exec->vm(), propertyName, value, slot);
202 bool JSActivation::deleteProperty(JSCell* cell, ExecState* exec, PropertyName propertyName)
204 if (propertyName == exec->propertyNames().arguments)
207 return Base::deleteProperty(cell, exec, propertyName);
210 JSValue JSActivation::toThis(JSCell*, ExecState* exec, ECMAMode ecmaMode)
212 if (ecmaMode == StrictMode)
213 return jsUndefined();
214 return exec->globalThisValue();
217 EncodedJSValue JSActivation::argumentsGetter(ExecState*, JSObject* slotBase, EncodedJSValue, PropertyName)
219 JSActivation* activation = jsCast<JSActivation*>(slotBase);
220 CallFrame* callFrame = CallFrame::create(reinterpret_cast<Register*>(activation->m_registers));
221 ASSERT(!activation->isTornOff() && (callFrame->codeBlock()->usesArguments() || callFrame->codeBlock()->usesEval()));
222 if (activation->isTornOff() || !(callFrame->codeBlock()->usesArguments() || callFrame->codeBlock()->usesEval()))
223 return JSValue::encode(jsUndefined());
225 VirtualRegister argumentsRegister = callFrame->codeBlock()->argumentsRegister();
226 if (JSValue arguments = callFrame->uncheckedR(argumentsRegister.offset()).jsValue())
227 return JSValue::encode(arguments);
228 int realArgumentsRegister = unmodifiedArgumentsRegister(argumentsRegister).offset();
230 JSValue arguments = JSValue(Arguments::create(callFrame->vm(), callFrame));
231 callFrame->uncheckedR(argumentsRegister.offset()) = arguments;
232 callFrame->uncheckedR(realArgumentsRegister) = arguments;
234 ASSERT(callFrame->uncheckedR(realArgumentsRegister).jsValue().inherits(Arguments::info()));
235 return JSValue::encode(callFrame->uncheckedR(realArgumentsRegister).jsValue());