Repository navigation
Expand file tree
/
Copy pathArguments.h
More file actions
387 lines (321 loc) · 15.2 KB
/
Copy pathArguments.h
File metadata and controls
387 lines (321 loc) · 15.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
/*
* Copyright (C) 1999-2000 Harri Porten (porten@kde.org)
* Copyright (C) 2003, 2006, 2007, 2008, 2009, 2014 Apple Inc. All rights reserved.
* Copyright (C) 2007 Cameron Zwarich (cwzwarich@uwaterloo.ca)
* Copyright (C) 2007 Maks Orlovich
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Library General Public
* License as published by the Free Software Foundation; either
* version 2 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Library General Public License for more details.
*
* You should have received a copy of the GNU Library General Public License
* along with this library; see the file COPYING.LIB. If not, write to
* the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
* Boston, MA 02110-1301, USA.
*
*/
#ifndef Arguments_h
#define Arguments_h
#include "CodeOrigin.h"
#include "JSFunction.h"
#include "JSGlobalObject.h"
#include "JSLexicalEnvironment.h"
#include "Interpreter.h"
#include "ObjectConstructor.h"
#include "WriteBarrierInlines.h"
#include <wtf/StdLibExtras.h>
namespace JSC {
enum ArgumentsMode {
NormalArgumentsCreationMode,
ClonedArgumentsCreationMode,
FakeArgumentValuesCreationMode
};
class Arguments : public JSNonFinalObject {
friend class JIT;
friend class JSArgumentsIterator;
public:
typedef JSNonFinalObject Base;
static Arguments* create(VM& vm, CallFrame* callFrame, JSLexicalEnvironment* lexicalEnvironment, ArgumentsMode mode = NormalArgumentsCreationMode)
{
Arguments* arguments = new (NotNull, allocateCell<Arguments>(vm.heap, offsetOfInlineRegisterArray() + registerArraySizeInBytes(callFrame))) Arguments(callFrame);
arguments->finishCreation(callFrame, lexicalEnvironment, mode);
return arguments;
}
static Arguments* create(VM& vm, CallFrame* callFrame, InlineCallFrame* inlineCallFrame, ArgumentsMode mode = NormalArgumentsCreationMode)
{
Arguments* arguments = new (NotNull, allocateCell<Arguments>(vm.heap, offsetOfInlineRegisterArray() + registerArraySizeInBytes(inlineCallFrame))) Arguments(callFrame);
arguments->finishCreation(callFrame, inlineCallFrame, mode);
return arguments;
}
enum { MaxArguments = 0x10000 };
private:
enum NoParametersType { NoParameters };
Arguments(CallFrame*);
Arguments(CallFrame*, NoParametersType);
public:
DECLARE_INFO;
static void visitChildren(JSCell*, SlotVisitor&);
static void copyBackingStore(JSCell*, CopyVisitor&, CopyToken);
void fillArgList(ExecState*, MarkedArgumentBuffer&);
uint32_t length(ExecState* exec) const
{
if (UNLIKELY(m_overrodeLength))
return get(exec, exec->propertyNames().length).toUInt32(exec);
return m_numArguments;
}
void copyToArguments(ExecState*, CallFrame*, uint32_t copyLength, int32_t firstArgumentOffset);
void tearOff(CallFrame*);
void tearOff(CallFrame*, InlineCallFrame*);
void tearOffForCloning(CallFrame*);
void tearOffForCloning(CallFrame*, InlineCallFrame*);
bool isTornOff() const { return m_registers == (®isterArray() - CallFrame::offsetFor(1) - 1); }
static Structure* createStructure(VM& vm, JSGlobalObject* globalObject, JSValue prototype)
{
return Structure::create(vm, globalObject, prototype, TypeInfo(ArgumentsType, StructureFlags), info());
}
static ptrdiff_t offsetOfActivation() { return OBJECT_OFFSETOF(Arguments, m_lexicalEnvironment); }
static ptrdiff_t offsetOfNumArguments() { return OBJECT_OFFSETOF(Arguments, m_numArguments); }
static ptrdiff_t offsetOfOverrodeLength() { return OBJECT_OFFSETOF(Arguments, m_overrodeLength); }
static ptrdiff_t offsetOfIsStrictMode() { return OBJECT_OFFSETOF(Arguments, m_isStrictMode); }
static ptrdiff_t offsetOfRegisters() { return OBJECT_OFFSETOF(Arguments, m_registers); }
static ptrdiff_t offsetOfInlineRegisterArray() { return WTF::roundUpToMultipleOf<8>(sizeof(Arguments)); }
static ptrdiff_t offsetOfSlowArgumentData() { return OBJECT_OFFSETOF(Arguments, m_slowArgumentData); }
static ptrdiff_t offsetOfCallee() { return OBJECT_OFFSETOF(Arguments, m_callee); }
protected:
static const unsigned StructureFlags = OverridesGetOwnPropertySlot | InterceptsGetOwnPropertySlotByIndexEvenWhenLengthIsNotZero | OverridesGetPropertyNames | JSObject::StructureFlags;
void finishCreation(CallFrame*, JSLexicalEnvironment*, ArgumentsMode);
void finishCreation(CallFrame*, InlineCallFrame*, ArgumentsMode);
private:
static bool getOwnPropertySlot(JSObject*, ExecState*, PropertyName, PropertySlot&);
static bool getOwnPropertySlotByIndex(JSObject*, ExecState*, unsigned propertyName, PropertySlot&);
static void getOwnPropertyNames(JSObject*, ExecState*, PropertyNameArray&, EnumerationMode);
static void put(JSCell*, ExecState*, PropertyName, JSValue, PutPropertySlot&);
static void putByIndex(JSCell*, ExecState*, unsigned propertyName, JSValue, bool shouldThrow);
static bool deleteProperty(JSCell*, ExecState*, PropertyName);
static bool deletePropertyByIndex(JSCell*, ExecState*, unsigned propertyName);
static bool defineOwnProperty(JSObject*, ExecState*, PropertyName, const PropertyDescriptor&, bool shouldThrow);
void createStrictModeCallerIfNecessary(ExecState*);
void createStrictModeCalleeIfNecessary(ExecState*);
static size_t registerArraySizeInBytes(CallFrame* callFrame) { return sizeof(WriteBarrier<Unknown>) * callFrame->argumentCount(); }
static size_t registerArraySizeInBytes(InlineCallFrame* inlineCallFrame) { return sizeof(WriteBarrier<Unknown>) * (inlineCallFrame->arguments.size() - 1); }
bool isArgument(size_t);
bool trySetArgument(VM&, size_t argument, JSValue);
JSValue tryGetArgument(size_t argument);
bool isDeletedArgument(size_t);
bool tryDeleteArgument(VM&, size_t);
WriteBarrierBase<Unknown>& argument(size_t);
void allocateSlowArguments(VM&);
void init(CallFrame*);
WriteBarrier<JSLexicalEnvironment> m_lexicalEnvironment;
unsigned m_numArguments;
// We make these full byte booleans to make them easy to test from the JIT,
// and because even if they were single-bit booleans we still wouldn't save
// any space.
bool m_overrodeLength;
bool m_overrodeCallee;
bool m_overrodeCaller;
bool m_isStrictMode;
WriteBarrierBase<Unknown>* m_registers;
WriteBarrier<Unknown>& registerArray() { return *reinterpret_cast<WriteBarrier<Unknown>*>(reinterpret_cast<char*>(this) + offsetOfInlineRegisterArray()); }
const WriteBarrier<Unknown>& registerArray() const { return *reinterpret_cast<const WriteBarrier<Unknown>*>(reinterpret_cast<const char*>(this) + offsetOfInlineRegisterArray()); }
public:
struct SlowArgumentData {
public:
SlowArgumentData()
: m_bytecodeToMachineCaptureOffset(0)
{
}
SlowArgument* slowArguments()
{
return reinterpret_cast<SlowArgument*>(WTF::roundUpToMultipleOf<8>(reinterpret_cast<size_t>(this + 1)));
}
int bytecodeToMachineCaptureOffset() const { return m_bytecodeToMachineCaptureOffset; }
void setBytecodeToMachineCaptureOffset(int newOffset) { m_bytecodeToMachineCaptureOffset = newOffset; }
static size_t sizeForNumArguments(unsigned numArguments)
{
return WTF::roundUpToMultipleOf<8>(sizeof(SlowArgumentData)) + sizeof(SlowArgument) * numArguments;
}
private:
int m_bytecodeToMachineCaptureOffset; // Add this if you have a bytecode offset into captured registers and you want the machine offset instead. Subtract if you want to do the opposite.
};
private:
CopyWriteBarrier<SlowArgumentData> m_slowArgumentData;
WriteBarrier<JSFunction> m_callee;
};
Arguments* asArguments(JSValue);
inline Arguments* asArguments(JSValue value)
{
ASSERT(asObject(value)->inherits(Arguments::info()));
return static_cast<Arguments*>(asObject(value));
}
inline Arguments::Arguments(CallFrame* callFrame)
: Base(callFrame->vm(), callFrame->lexicalGlobalObject()->argumentsStructure())
{
}
inline Arguments::Arguments(CallFrame* callFrame, NoParametersType)
: Base(callFrame->vm(), callFrame->lexicalGlobalObject()->argumentsStructure())
{
}
inline void Arguments::allocateSlowArguments(VM& vm)
{
if (!!m_slowArgumentData)
return;
void* backingStore;
if (!vm.heap.tryAllocateStorage(this, SlowArgumentData::sizeForNumArguments(m_numArguments), &backingStore))
RELEASE_ASSERT_NOT_REACHED();
m_slowArgumentData.set(vm, this, static_cast<SlowArgumentData*>(backingStore));
for (size_t i = 0; i < m_numArguments; ++i) {
ASSERT(m_slowArgumentData->slowArguments()[i].status == SlowArgument::Normal);
m_slowArgumentData->slowArguments()[i].index = CallFrame::argumentOffset(i);
}
}
inline bool Arguments::tryDeleteArgument(VM& vm, size_t argument)
{
if (!isArgument(argument))
return false;
allocateSlowArguments(vm);
m_slowArgumentData->slowArguments()[argument].status = SlowArgument::Deleted;
return true;
}
inline bool Arguments::trySetArgument(VM& vm, size_t argument, JSValue value)
{
if (!isArgument(argument))
return false;
this->argument(argument).set(vm, this, value);
return true;
}
inline JSValue Arguments::tryGetArgument(size_t argument)
{
if (!isArgument(argument))
return JSValue();
return this->argument(argument).get();
}
inline bool Arguments::isDeletedArgument(size_t argument)
{
if (argument >= m_numArguments)
return false;
if (!m_slowArgumentData)
return false;
if (m_slowArgumentData->slowArguments()[argument].status != SlowArgument::Deleted)
return false;
return true;
}
inline bool Arguments::isArgument(size_t argument)
{
if (argument >= m_numArguments)
return false;
if (m_slowArgumentData && m_slowArgumentData->slowArguments()[argument].status == SlowArgument::Deleted)
return false;
return true;
}
inline WriteBarrierBase<Unknown>& Arguments::argument(size_t argument)
{
ASSERT(isArgument(argument));
if (!m_slowArgumentData)
return m_registers[CallFrame::argumentOffset(argument)];
int index = m_slowArgumentData->slowArguments()[argument].index;
if (m_slowArgumentData->slowArguments()[argument].status != SlowArgument::Captured)
return m_registers[index];
RELEASE_ASSERT(m_lexicalEnvironment);
return m_lexicalEnvironment->registerAt(index - m_slowArgumentData->bytecodeToMachineCaptureOffset());
}
inline void Arguments::finishCreation(CallFrame* callFrame, JSLexicalEnvironment* lexicalEnvironment, ArgumentsMode mode)
{
Base::finishCreation(callFrame->vm());
ASSERT(inherits(info()));
JSFunction* callee = jsCast<JSFunction*>(callFrame->callee());
m_callee.set(callFrame->vm(), this, callee);
m_overrodeLength = false;
m_overrodeCallee = false;
m_overrodeCaller = false;
m_isStrictMode = callFrame->codeBlock()->isStrictMode();
switch (mode) {
case NormalArgumentsCreationMode: {
m_numArguments = callFrame->argumentCount();
m_registers = reinterpret_cast<WriteBarrierBase<Unknown>*>(callFrame->registers());
CodeBlock* codeBlock = callFrame->codeBlock();
if (codeBlock->hasSlowArguments()) {
SymbolTable* symbolTable = codeBlock->symbolTable();
const SlowArgument* slowArguments = codeBlock->machineSlowArguments();
allocateSlowArguments(callFrame->vm());
size_t count = std::min<unsigned>(m_numArguments, symbolTable->parameterCount());
for (size_t i = 0; i < count; ++i)
m_slowArgumentData->slowArguments()[i] = slowArguments[i];
m_slowArgumentData->setBytecodeToMachineCaptureOffset(
codeBlock->framePointerOffsetToGetActivationRegisters());
}
if (codeBlock->needsActivation()) {
RELEASE_ASSERT(lexicalEnvironment && lexicalEnvironment == callFrame->lexicalEnvironment());
m_lexicalEnvironment.set(callFrame->vm(), this, lexicalEnvironment);
}
// The bytecode generator omits op_tear_off_lexical_environment in cases of no
// declared parameters, so we need to tear off immediately.
if (m_isStrictMode || !callee->jsExecutable()->parameterCount())
tearOff(callFrame);
break;
}
case ClonedArgumentsCreationMode: {
m_numArguments = callFrame->argumentCount();
m_registers = reinterpret_cast<WriteBarrierBase<Unknown>*>(callFrame->registers());
tearOffForCloning(callFrame);
break;
}
case FakeArgumentValuesCreationMode: {
m_numArguments = 0;
m_registers = nullptr;
tearOff(callFrame);
break;
}
}
}
inline void Arguments::finishCreation(CallFrame* callFrame, InlineCallFrame* inlineCallFrame, ArgumentsMode mode)
{
Base::finishCreation(callFrame->vm());
ASSERT(inherits(info()));
JSFunction* callee = inlineCallFrame->calleeForCallFrame(callFrame);
m_callee.set(callFrame->vm(), this, callee);
m_overrodeLength = false;
m_overrodeCallee = false;
m_overrodeCaller = false;
m_isStrictMode = jsCast<FunctionExecutable*>(inlineCallFrame->executable.get())->isStrictMode();
switch (mode) {
case NormalArgumentsCreationMode: {
m_numArguments = inlineCallFrame->arguments.size() - 1;
if (m_numArguments) {
int offsetForArgumentOne = inlineCallFrame->arguments[1].virtualRegister().offset();
m_registers = reinterpret_cast<WriteBarrierBase<Unknown>*>(callFrame->registers()) + offsetForArgumentOne - virtualRegisterForArgument(1).offset();
} else
m_registers = 0;
ASSERT(!jsCast<FunctionExecutable*>(inlineCallFrame->executable.get())->symbolTable(inlineCallFrame->specializationKind())->slowArguments());
// The bytecode generator omits op_tear_off_lexical_environment in cases of no
// declared parameters, so we need to tear off immediately.
if (m_isStrictMode || !callee->jsExecutable()->parameterCount())
tearOff(callFrame, inlineCallFrame);
break;
}
case ClonedArgumentsCreationMode: {
m_numArguments = inlineCallFrame->arguments.size() - 1;
if (m_numArguments) {
int offsetForArgumentOne = inlineCallFrame->arguments[1].virtualRegister().offset();
m_registers = reinterpret_cast<WriteBarrierBase<Unknown>*>(callFrame->registers()) + offsetForArgumentOne - virtualRegisterForArgument(1).offset();
} else
m_registers = 0;
ASSERT(!jsCast<FunctionExecutable*>(inlineCallFrame->executable.get())->symbolTable(inlineCallFrame->specializationKind())->slowArguments());
tearOffForCloning(callFrame, inlineCallFrame);
break;
}
case FakeArgumentValuesCreationMode: {
m_numArguments = 0;
m_registers = nullptr;
tearOff(callFrame);
break;
} }
}
} // namespace JSC
#endif // Arguments_h