]>
git.saurik.com Git - apple/javascriptcore.git/blob - runtime/ClonedArguments.cpp
2 * Copyright (C) 2015 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
7 * 1. Redistributions of source code must retain the above copyright
8 * notice, this list of conditions and the following disclaimer.
9 * 2. Redistributions in binary form must reproduce the above copyright
10 * notice, this list of conditions and the following disclaimer in the
11 * documentation and/or other materials provided with the distribution.
13 * THIS SOFTWARE IS PROVIDED BY APPLE INC. ``AS IS'' AND ANY
14 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
15 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
16 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR
17 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
18 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
19 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
20 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
21 * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
22 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
23 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 #include "ClonedArguments.h"
29 #include "GetterSetter.h"
30 #include "JSCInlines.h"
34 STATIC_ASSERT_IS_TRIVIALLY_DESTRUCTIBLE(ClonedArguments
);
36 const ClassInfo
ClonedArguments::s_info
= { "Arguments", &Base::s_info
, 0, CREATE_METHOD_TABLE(ClonedArguments
) };
38 ClonedArguments::ClonedArguments(VM
& vm
, Structure
* structure
)
39 : Base(vm
, structure
, nullptr)
43 ClonedArguments
* ClonedArguments::createEmpty(
44 VM
& vm
, Structure
* structure
, JSFunction
* callee
)
46 ClonedArguments
* result
=
47 new (NotNull
, allocateCell
<ClonedArguments
>(vm
.heap
))
48 ClonedArguments(vm
, structure
);
49 result
->finishCreation(vm
);
50 result
->m_callee
.set(vm
, result
, callee
);
54 ClonedArguments
* ClonedArguments::createEmpty(ExecState
* exec
, JSFunction
* callee
)
56 // NB. Some clients might expect that the global object of of this object is the global object
57 // of the callee. We don't do this for now, but maybe we should.
59 exec
->vm(), exec
->lexicalGlobalObject()->outOfBandArgumentsStructure(), callee
);
62 ClonedArguments
* ClonedArguments::createWithInlineFrame(ExecState
* myFrame
, ExecState
* targetFrame
, InlineCallFrame
* inlineCallFrame
, ArgumentsMode mode
)
64 VM
& vm
= myFrame
->vm();
69 callee
= jsCast
<JSFunction
*>(inlineCallFrame
->calleeRecovery
.recover(targetFrame
));
71 callee
= jsCast
<JSFunction
*>(targetFrame
->callee());
73 ClonedArguments
* result
= createEmpty(myFrame
, callee
);
75 unsigned length
= 0; // Initialize because VC needs it.
77 case ArgumentsMode::Cloned
: {
78 if (inlineCallFrame
) {
79 if (inlineCallFrame
->argumentCountRegister
.isValid())
80 length
= targetFrame
->r(inlineCallFrame
->argumentCountRegister
).unboxedInt32();
82 length
= inlineCallFrame
->arguments
.size();
85 for (unsigned i
= length
; i
--;)
86 result
->putDirectIndex(myFrame
, i
, inlineCallFrame
->arguments
[i
+ 1].recover(targetFrame
));
88 length
= targetFrame
->argumentCount();
90 for (unsigned i
= length
; i
--;)
91 result
->putDirectIndex(myFrame
, i
, targetFrame
->uncheckedArgument(i
));
96 case ArgumentsMode::FakeValues
: {
101 result
->putDirect(vm
, vm
.propertyNames
->length
, jsNumber(length
));
106 ClonedArguments
* ClonedArguments::createWithMachineFrame(ExecState
* myFrame
, ExecState
* targetFrame
, ArgumentsMode mode
)
108 return createWithInlineFrame(myFrame
, targetFrame
, nullptr, mode
);
111 ClonedArguments
* ClonedArguments::createByCopyingFrom(
112 ExecState
* exec
, Structure
* structure
, Register
* argumentStart
, unsigned length
,
116 ClonedArguments
* result
= createEmpty(vm
, structure
, callee
);
118 for (unsigned i
= length
; i
--;)
119 result
->putDirectIndex(exec
, i
, argumentStart
[i
].jsValue());
121 result
->putDirect(vm
, vm
.propertyNames
->length
, jsNumber(length
));
125 Structure
* ClonedArguments::createStructure(VM
& vm
, JSGlobalObject
* globalObject
, JSValue prototype
)
127 return Structure::create(vm
, globalObject
, prototype
, TypeInfo(ObjectType
, StructureFlags
), info());
130 bool ClonedArguments::getOwnPropertySlot(JSObject
* object
, ExecState
* exec
, PropertyName ident
, PropertySlot
& slot
)
132 ClonedArguments
* thisObject
= jsCast
<ClonedArguments
*>(object
);
135 if (ident
== vm
.propertyNames
->callee
136 || ident
== vm
.propertyNames
->caller
137 || ident
== vm
.propertyNames
->iteratorSymbol
)
138 thisObject
->materializeSpecialsIfNecessary(exec
);
140 if (Base::getOwnPropertySlot(thisObject
, exec
, ident
, slot
))
146 void ClonedArguments::getOwnPropertyNames(JSObject
* object
, ExecState
* exec
, PropertyNameArray
& array
, EnumerationMode mode
)
148 ClonedArguments
* thisObject
= jsCast
<ClonedArguments
*>(object
);
149 thisObject
->materializeSpecialsIfNecessary(exec
);
150 Base::getOwnPropertyNames(thisObject
, exec
, array
, mode
);
153 void ClonedArguments::put(JSCell
* cell
, ExecState
* exec
, PropertyName ident
, JSValue value
, PutPropertySlot
& slot
)
155 ClonedArguments
* thisObject
= jsCast
<ClonedArguments
*>(cell
);
158 if (ident
== vm
.propertyNames
->callee
159 || ident
== vm
.propertyNames
->caller
160 || ident
== vm
.propertyNames
->iteratorSymbol
) {
161 thisObject
->materializeSpecialsIfNecessary(exec
);
162 PutPropertySlot dummy
= slot
; // Shadow the given PutPropertySlot to prevent caching.
163 Base::put(thisObject
, exec
, ident
, value
, dummy
);
167 Base::put(thisObject
, exec
, ident
, value
, slot
);
170 bool ClonedArguments::deleteProperty(JSCell
* cell
, ExecState
* exec
, PropertyName ident
)
172 ClonedArguments
* thisObject
= jsCast
<ClonedArguments
*>(cell
);
175 if (ident
== vm
.propertyNames
->callee
176 || ident
== vm
.propertyNames
->caller
177 || ident
== vm
.propertyNames
->iteratorSymbol
)
178 thisObject
->materializeSpecialsIfNecessary(exec
);
180 return Base::deleteProperty(thisObject
, exec
, ident
);
183 bool ClonedArguments::defineOwnProperty(JSObject
* object
, ExecState
* exec
, PropertyName ident
, const PropertyDescriptor
& descriptor
, bool shouldThrow
)
185 ClonedArguments
* thisObject
= jsCast
<ClonedArguments
*>(object
);
188 if (ident
== vm
.propertyNames
->callee
189 || ident
== vm
.propertyNames
->caller
190 || ident
== vm
.propertyNames
->iteratorSymbol
)
191 thisObject
->materializeSpecialsIfNecessary(exec
);
193 return Base::defineOwnProperty(object
, exec
, ident
, descriptor
, shouldThrow
);
196 void ClonedArguments::materializeSpecials(ExecState
* exec
)
198 RELEASE_ASSERT(!specialsMaterialized());
201 FunctionExecutable
* executable
= jsCast
<FunctionExecutable
*>(m_callee
->executable());
202 bool isStrictMode
= executable
->isStrictMode();
205 putDirectAccessor(exec
, vm
.propertyNames
->callee
, globalObject()->throwTypeErrorGetterSetter(vm
), DontDelete
| DontEnum
| Accessor
);
206 putDirectAccessor(exec
, vm
.propertyNames
->caller
, globalObject()->throwTypeErrorGetterSetter(vm
), DontDelete
| DontEnum
| Accessor
);
208 putDirect(vm
, vm
.propertyNames
->callee
, JSValue(m_callee
.get()));
210 putDirect(vm
, vm
.propertyNames
->iteratorSymbol
, globalObject()->arrayProtoValuesFunction(), DontEnum
);
215 void ClonedArguments::materializeSpecialsIfNecessary(ExecState
* exec
)
217 if (!specialsMaterialized())
218 materializeSpecials(exec
);
221 void ClonedArguments::visitChildren(JSCell
* cell
, SlotVisitor
& visitor
)
223 ClonedArguments
* thisObject
= jsCast
<ClonedArguments
*>(cell
);
224 ASSERT_GC_OBJECT_INHERITS(thisObject
, info());
225 Base::visitChildren(thisObject
, visitor
);
226 visitor
.append(&thisObject
->m_callee
);