Source/JavaScriptCore: Web Inspector shouldn't artificially allocate the arguments...
[WebKit-https.git] / Source / JavaScriptCore / runtime / JSActivation.h
1 /*
2  * Copyright (C) 2008, 2009 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 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 #ifndef JSActivation_h
30 #define JSActivation_h
31
32 #include "CodeBlock.h"
33 #include "CopiedSpaceInlines.h"
34 #include "JSVariableObject.h"
35 #include "Nodes.h"
36 #include "SymbolTable.h"
37
38 namespace JSC {
39
40     class Register;
41     
42     class JSActivation : public JSVariableObject {
43     private:
44         JSActivation(VM& vm, CallFrame*, SharedSymbolTable*);
45     
46     public:
47         typedef JSVariableObject Base;
48
49         static JSActivation* create(VM& vm, CallFrame* callFrame, CodeBlock* codeBlock)
50         {
51             SharedSymbolTable* symbolTable = codeBlock->symbolTable();
52             JSActivation* activation = new (
53                 NotNull,
54                 allocateCell<JSActivation>(
55                     vm.heap,
56                     allocationSize(symbolTable)
57                 )
58             ) JSActivation(vm, callFrame, symbolTable);
59             activation->finishCreation(vm);
60             return activation;
61         }
62
63         static void visitChildren(JSCell*, SlotVisitor&);
64
65         static bool getOwnPropertySlot(JSObject*, ExecState*, PropertyName, PropertySlot&);
66         static void getOwnNonIndexPropertyNames(JSObject*, ExecState*, PropertyNameArray&, EnumerationMode);
67
68         static void put(JSCell*, ExecState*, PropertyName, JSValue, PutPropertySlot&);
69
70         static bool deleteProperty(JSCell*, ExecState*, PropertyName);
71
72         static JSValue toThis(JSCell*, ExecState*, ECMAMode);
73
74         void tearOff(VM&);
75         
76         DECLARE_INFO;
77
78         static Structure* createStructure(VM& vm, JSGlobalObject* globalObject, JSValue proto) { return Structure::create(vm, globalObject, proto, TypeInfo(ActivationObjectType, StructureFlags), info()); }
79
80         WriteBarrierBase<Unknown>& registerAt(int) const;
81         bool isValidIndex(int) const;
82         bool isValid(const SymbolTableEntry&) const;
83         bool isTornOff();
84         int registersOffset();
85         static int registersOffset(SharedSymbolTable*);
86
87     protected:
88         static const unsigned StructureFlags = OverridesGetOwnPropertySlot | OverridesVisitChildren | OverridesGetPropertyNames | Base::StructureFlags;
89
90     private:
91         bool symbolTableGet(PropertyName, PropertySlot&);
92         bool symbolTableGet(PropertyName, PropertyDescriptor&);
93         bool symbolTableGet(PropertyName, PropertySlot&, bool& slotIsWriteable);
94         bool symbolTablePut(ExecState*, PropertyName, JSValue, bool shouldThrow);
95         bool symbolTablePutWithAttributes(VM&, PropertyName, JSValue, unsigned attributes);
96
97         static JSValue argumentsGetter(ExecState*, JSValue, PropertyName);
98
99         static size_t allocationSize(SharedSymbolTable*);
100         static size_t storageOffset();
101
102         WriteBarrier<Unknown>* storage(); // captureCount() number of registers.
103     };
104
105     extern int activationCount;
106     extern int allTheThingsCount;
107
108     inline JSActivation::JSActivation(VM& vm, CallFrame* callFrame, SharedSymbolTable* symbolTable)
109         : Base(
110             vm,
111             callFrame->lexicalGlobalObject()->activationStructure(),
112             callFrame->registers(),
113             callFrame->scope(),
114             symbolTable
115         )
116     {
117         WriteBarrier<Unknown>* storage = this->storage();
118         size_t captureCount = symbolTable->captureCount();
119         for (size_t i = 0; i < captureCount; ++i)
120             new(&storage[i]) WriteBarrier<Unknown>;
121     }
122
123     JSActivation* asActivation(JSValue);
124
125     inline JSActivation* asActivation(JSValue value)
126     {
127         ASSERT(asObject(value)->inherits(JSActivation::info()));
128         return jsCast<JSActivation*>(asObject(value));
129     }
130     
131     ALWAYS_INLINE JSActivation* Register::activation() const
132     {
133         return asActivation(jsValue());
134     }
135
136     inline int JSActivation::registersOffset(SharedSymbolTable* symbolTable)
137     {
138         return storageOffset() - (symbolTable->captureStart() * sizeof(WriteBarrier<Unknown>));
139     }
140
141     inline void JSActivation::tearOff(VM& vm)
142     {
143         ASSERT(!isTornOff());
144
145         WriteBarrierBase<Unknown>* dst = reinterpret_cast_ptr<WriteBarrierBase<Unknown>*>(
146             reinterpret_cast<char*>(this) + registersOffset(symbolTable()));
147         WriteBarrierBase<Unknown>* src = m_registers;
148
149         int captureEnd = symbolTable()->captureEnd();
150         for (int i = symbolTable()->captureStart(); i < captureEnd; ++i)
151             dst[i].set(vm, this, src[i].get());
152
153         m_registers = dst;
154         ASSERT(isTornOff());
155     }
156
157     inline bool JSActivation::isTornOff()
158     {
159         return m_registers == reinterpret_cast_ptr<WriteBarrierBase<Unknown>*>(
160             reinterpret_cast<char*>(this) + registersOffset(symbolTable()));
161     }
162
163     inline size_t JSActivation::storageOffset()
164     {
165         return WTF::roundUpToMultipleOf<sizeof(WriteBarrier<Unknown>)>(sizeof(JSActivation));
166     }
167
168     inline WriteBarrier<Unknown>* JSActivation::storage()
169     {
170         return reinterpret_cast_ptr<WriteBarrier<Unknown>*>(
171             reinterpret_cast<char*>(this) + storageOffset());
172     }
173
174     inline size_t JSActivation::allocationSize(SharedSymbolTable* symbolTable)
175     {
176         size_t objectSizeInBytes = WTF::roundUpToMultipleOf<sizeof(WriteBarrier<Unknown>)>(sizeof(JSActivation));
177         size_t storageSizeInBytes = symbolTable->captureCount() * sizeof(WriteBarrier<Unknown>);
178         return objectSizeInBytes + storageSizeInBytes;
179     }
180
181     inline bool JSActivation::isValidIndex(int index) const
182     {
183         if (index < symbolTable()->captureStart())
184             return false;
185         if (index >= symbolTable()->captureEnd())
186             return false;
187         return true;
188     }
189
190     inline bool JSActivation::isValid(const SymbolTableEntry& entry) const
191     {
192         return isValidIndex(entry.getIndex());
193     }
194
195     inline WriteBarrierBase<Unknown>& JSActivation::registerAt(int index) const
196     {
197         ASSERT(isValidIndex(index));
198         return Base::registerAt(index);
199     }
200
201 } // namespace JSC
202
203 #endif // JSActivation_h