Rename activation to be more in line with spec language
[WebKit-https.git] / Source / JavaScriptCore / runtime / JSLexicalEnvironment.cpp
1 /*
2  * Copyright (C) 2008, 2009, 2014 Apple Inc. All rights reserved.
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 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 "JSLexicalEnvironment.h"
31
32 #include "Arguments.h"
33 #include "Interpreter.h"
34 #include "JSFunction.h"
35 #include "JSCInlines.h"
36
37 using namespace std;
38
39 namespace JSC {
40
41 const ClassInfo JSLexicalEnvironment::s_info = { "JSLexicalEnvironment", &Base::s_info, 0, CREATE_METHOD_TABLE(JSLexicalEnvironment) };
42
43 void JSLexicalEnvironment::visitChildren(JSCell* cell, SlotVisitor& visitor)
44 {
45     JSLexicalEnvironment* thisObject = jsCast<JSLexicalEnvironment*>(cell);
46     ASSERT_GC_OBJECT_INHERITS(thisObject, info());
47     Base::visitChildren(thisObject, visitor);
48
49     // No need to mark our registers if they're still in the JSStack.
50     if (!thisObject->isTornOff())
51         return;
52
53     for (int i = 0; i < thisObject->symbolTable()->captureCount(); ++i)
54         visitor.append(&thisObject->storage()[i]);
55 }
56
57 inline bool JSLexicalEnvironment::symbolTableGet(PropertyName propertyName, PropertySlot& slot)
58 {
59     SymbolTableEntry entry = symbolTable()->inlineGet(propertyName.uid());
60     if (entry.isNull())
61         return false;
62
63     // Defend against the inspector asking for a var after it has been optimized out.
64     if (isTornOff() && !isValid(entry))
65         return false;
66
67     slot.setValue(this, DontEnum, registerAt(entry.getIndex()).get());
68     return true;
69 }
70
71 inline bool JSLexicalEnvironment::symbolTableGet(PropertyName propertyName, PropertyDescriptor& descriptor)
72 {
73     SymbolTableEntry entry = symbolTable()->inlineGet(propertyName.uid());
74     if (entry.isNull())
75         return false;
76
77     // Defend against the inspector asking for a var after it has been optimized out.
78     if (isTornOff() && !isValid(entry))
79         return false;
80
81     descriptor.setDescriptor(registerAt(entry.getIndex()).get(), entry.getAttributes());
82     return true;
83 }
84
85 inline bool JSLexicalEnvironment::symbolTablePut(ExecState* exec, PropertyName propertyName, JSValue value, bool shouldThrow)
86 {
87     VM& vm = exec->vm();
88     ASSERT(!Heap::heap(value) || Heap::heap(value) == Heap::heap(this));
89     
90     WriteBarrierBase<Unknown>* reg;
91     {
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))
95             return false;
96         ASSERT(!iter->value.isNull());
97         if (iter->value.isReadOnly()) {
98             if (shouldThrow)
99                 throwTypeError(exec, StrictModeReadonlyPropertyWriteError);
100             return true;
101         }
102         // Defend against the inspector asking for a var after it has been optimized out.
103         if (isTornOff() && !isValid(iter->value))
104             return false;
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 = &registerAt(iter->value.getIndex());
108     }
109     reg->set(vm, this, value);
110     return true;
111 }
112
113 void JSLexicalEnvironment::getOwnNonIndexPropertyNames(JSObject* object, ExecState* exec, PropertyNameArray& propertyNames, EnumerationMode mode)
114 {
115     JSLexicalEnvironment* thisObject = jsCast<JSLexicalEnvironment*>(object);
116
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);
120
121     {
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))
126                 continue;
127             if (!thisObject->isValid(it->value))
128                 continue;
129             propertyNames.add(Identifier(exec, it->key.get()));
130         }
131     }
132     // Skip the JSEnvironmentRecord implementation of getOwnNonIndexPropertyNames
133     JSObject::getOwnNonIndexPropertyNames(thisObject, exec, propertyNames, mode);
134 }
135
136 inline bool JSLexicalEnvironment::symbolTablePutWithAttributes(VM& vm, PropertyName propertyName, JSValue value, unsigned attributes)
137 {
138     ASSERT(!Heap::heap(value) || Heap::heap(value) == Heap::heap(this));
139     
140     WriteBarrierBase<Unknown>* reg;
141     {
142         ConcurrentJITLocker locker(symbolTable()->m_lock);
143         SymbolTable::Map::iterator iter = symbolTable()->find(locker, propertyName.uid());
144         if (iter == symbolTable()->end(locker))
145             return false;
146         SymbolTableEntry& entry = iter->value;
147         ASSERT(!entry.isNull());
148         if (!isValid(entry))
149             return false;
150         
151         entry.setAttributes(attributes);
152         reg = &registerAt(entry.getIndex());
153     }
154     reg->set(vm, this, value);
155     return true;
156 }
157
158 bool JSLexicalEnvironment::getOwnPropertySlot(JSObject* object, ExecState* exec, PropertyName propertyName, PropertySlot& slot)
159 {
160     JSLexicalEnvironment* thisObject = jsCast<JSLexicalEnvironment*>(object);
161
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);
167             return true;
168         }
169     }
170
171     if (thisObject->symbolTableGet(propertyName, slot))
172         return true;
173
174     unsigned attributes;
175     if (JSValue value = thisObject->getDirect(exec->vm(), propertyName, attributes)) {
176         slot.setValue(thisObject, attributes, value);
177         return true;
178     }
179
180     // We don't call through to JSObject because there's no way to give a 
181     // lexical environment object getter properties or a prototype.
182     ASSERT(!thisObject->hasGetterSetterProperties());
183     ASSERT(thisObject->prototype().isNull());
184     return false;
185 }
186
187 void JSLexicalEnvironment::put(JSCell* cell, ExecState* exec, PropertyName propertyName, JSValue value, PutPropertySlot& slot)
188 {
189     JSLexicalEnvironment* thisObject = jsCast<JSLexicalEnvironment*>(cell);
190     ASSERT(!Heap::heap(value) || Heap::heap(value) == Heap::heap(thisObject));
191
192     if (thisObject->symbolTablePut(exec, propertyName, value, slot.isStrictMode()))
193         return;
194
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 lexicalEnvironment object.
198     ASSERT(!thisObject->hasGetterSetterProperties());
199     thisObject->putOwnDataProperty(exec->vm(), propertyName, value, slot);
200 }
201
202 bool JSLexicalEnvironment::deleteProperty(JSCell* cell, ExecState* exec, PropertyName propertyName)
203 {
204     if (propertyName == exec->propertyNames().arguments)
205         return false;
206
207     return Base::deleteProperty(cell, exec, propertyName);
208 }
209
210 JSValue JSLexicalEnvironment::toThis(JSCell*, ExecState* exec, ECMAMode ecmaMode)
211 {
212     if (ecmaMode == StrictMode)
213         return jsUndefined();
214     return exec->globalThisValue();
215 }
216
217 EncodedJSValue JSLexicalEnvironment::argumentsGetter(ExecState*, JSObject* slotBase, EncodedJSValue, PropertyName)
218 {
219     JSLexicalEnvironment* lexicalEnvironment = jsCast<JSLexicalEnvironment*>(slotBase);
220     CallFrame* callFrame = CallFrame::create(reinterpret_cast<Register*>(lexicalEnvironment->m_registers));
221     ASSERT(!lexicalEnvironment->isTornOff() && (callFrame->codeBlock()->usesArguments() || callFrame->codeBlock()->usesEval()));
222     if (lexicalEnvironment->isTornOff() || !(callFrame->codeBlock()->usesArguments() || callFrame->codeBlock()->usesEval()))
223         return JSValue::encode(jsUndefined());
224
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();
229
230     JSValue arguments = JSValue(Arguments::create(callFrame->vm(), callFrame));
231     callFrame->uncheckedR(argumentsRegister.offset()) = arguments;
232     callFrame->uncheckedR(realArgumentsRegister) = arguments;
233     
234     ASSERT(callFrame->uncheckedR(realArgumentsRegister).jsValue().inherits(Arguments::info()));
235     return JSValue::encode(callFrame->uncheckedR(realArgumentsRegister).jsValue());
236 }
237
238 } // namespace JSC