]> git.saurik.com Git - apple/javascriptcore.git/blame - runtime/ClonedArguments.cpp
JavaScriptCore-7601.1.46.3.tar.gz
[apple/javascriptcore.git] / runtime / ClonedArguments.cpp
CommitLineData
ed1e77d3
A
1/*
2 * Copyright (C) 2015 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 * 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.
12 *
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.
24 */
25
26#include "config.h"
27#include "ClonedArguments.h"
28
29#include "GetterSetter.h"
30#include "JSCInlines.h"
31
32namespace JSC {
33
34STATIC_ASSERT_IS_TRIVIALLY_DESTRUCTIBLE(ClonedArguments);
35
36const ClassInfo ClonedArguments::s_info = { "Arguments", &Base::s_info, 0, CREATE_METHOD_TABLE(ClonedArguments) };
37
38ClonedArguments::ClonedArguments(VM& vm, Structure* structure)
39 : Base(vm, structure, nullptr)
40{
41}
42
43ClonedArguments* ClonedArguments::createEmpty(
44 VM& vm, Structure* structure, JSFunction* callee)
45{
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);
51 return result;
52}
53
54ClonedArguments* ClonedArguments::createEmpty(ExecState* exec, JSFunction* callee)
55{
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.
58 return createEmpty(
59 exec->vm(), exec->lexicalGlobalObject()->outOfBandArgumentsStructure(), callee);
60}
61
62ClonedArguments* ClonedArguments::createWithInlineFrame(ExecState* myFrame, ExecState* targetFrame, InlineCallFrame* inlineCallFrame, ArgumentsMode mode)
63{
64 VM& vm = myFrame->vm();
65
66 JSFunction* callee;
67
68 if (inlineCallFrame)
69 callee = jsCast<JSFunction*>(inlineCallFrame->calleeRecovery.recover(targetFrame));
70 else
71 callee = jsCast<JSFunction*>(targetFrame->callee());
72
73 ClonedArguments* result = createEmpty(myFrame, callee);
74
75 unsigned length = 0; // Initialize because VC needs it.
76 switch (mode) {
77 case ArgumentsMode::Cloned: {
78 if (inlineCallFrame) {
79 if (inlineCallFrame->argumentCountRegister.isValid())
80 length = targetFrame->r(inlineCallFrame->argumentCountRegister).unboxedInt32();
81 else
82 length = inlineCallFrame->arguments.size();
83 length--;
84
85 for (unsigned i = length; i--;)
86 result->putDirectIndex(myFrame, i, inlineCallFrame->arguments[i + 1].recover(targetFrame));
87 } else {
88 length = targetFrame->argumentCount();
89
90 for (unsigned i = length; i--;)
91 result->putDirectIndex(myFrame, i, targetFrame->uncheckedArgument(i));
92 }
93 break;
94 }
95
96 case ArgumentsMode::FakeValues: {
97 length = 0;
98 break;
99 } }
100
101 result->putDirect(vm, vm.propertyNames->length, jsNumber(length));
102
103 return result;
104}
105
106ClonedArguments* ClonedArguments::createWithMachineFrame(ExecState* myFrame, ExecState* targetFrame, ArgumentsMode mode)
107{
108 return createWithInlineFrame(myFrame, targetFrame, nullptr, mode);
109}
110
111ClonedArguments* ClonedArguments::createByCopyingFrom(
112 ExecState* exec, Structure* structure, Register* argumentStart, unsigned length,
113 JSFunction* callee)
114{
115 VM& vm = exec->vm();
116 ClonedArguments* result = createEmpty(vm, structure, callee);
117
118 for (unsigned i = length; i--;)
119 result->putDirectIndex(exec, i, argumentStart[i].jsValue());
120
121 result->putDirect(vm, vm.propertyNames->length, jsNumber(length));
122 return result;
123}
124
125Structure* ClonedArguments::createStructure(VM& vm, JSGlobalObject* globalObject, JSValue prototype)
126{
127 return Structure::create(vm, globalObject, prototype, TypeInfo(ObjectType, StructureFlags), info());
128}
129
130bool ClonedArguments::getOwnPropertySlot(JSObject* object, ExecState* exec, PropertyName ident, PropertySlot& slot)
131{
132 ClonedArguments* thisObject = jsCast<ClonedArguments*>(object);
133 VM& vm = exec->vm();
134
135 if (ident == vm.propertyNames->callee
136 || ident == vm.propertyNames->caller
137 || ident == vm.propertyNames->iteratorSymbol)
138 thisObject->materializeSpecialsIfNecessary(exec);
139
140 if (Base::getOwnPropertySlot(thisObject, exec, ident, slot))
141 return true;
142
143 return false;
144}
145
146void ClonedArguments::getOwnPropertyNames(JSObject* object, ExecState* exec, PropertyNameArray& array, EnumerationMode mode)
147{
148 ClonedArguments* thisObject = jsCast<ClonedArguments*>(object);
149 thisObject->materializeSpecialsIfNecessary(exec);
150 Base::getOwnPropertyNames(thisObject, exec, array, mode);
151}
152
153void ClonedArguments::put(JSCell* cell, ExecState* exec, PropertyName ident, JSValue value, PutPropertySlot& slot)
154{
155 ClonedArguments* thisObject = jsCast<ClonedArguments*>(cell);
156 VM& vm = exec->vm();
157
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);
164 return;
165 }
166
167 Base::put(thisObject, exec, ident, value, slot);
168}
169
170bool ClonedArguments::deleteProperty(JSCell* cell, ExecState* exec, PropertyName ident)
171{
172 ClonedArguments* thisObject = jsCast<ClonedArguments*>(cell);
173 VM& vm = exec->vm();
174
175 if (ident == vm.propertyNames->callee
176 || ident == vm.propertyNames->caller
177 || ident == vm.propertyNames->iteratorSymbol)
178 thisObject->materializeSpecialsIfNecessary(exec);
179
180 return Base::deleteProperty(thisObject, exec, ident);
181}
182
183bool ClonedArguments::defineOwnProperty(JSObject* object, ExecState* exec, PropertyName ident, const PropertyDescriptor& descriptor, bool shouldThrow)
184{
185 ClonedArguments* thisObject = jsCast<ClonedArguments*>(object);
186 VM& vm = exec->vm();
187
188 if (ident == vm.propertyNames->callee
189 || ident == vm.propertyNames->caller
190 || ident == vm.propertyNames->iteratorSymbol)
191 thisObject->materializeSpecialsIfNecessary(exec);
192
193 return Base::defineOwnProperty(object, exec, ident, descriptor, shouldThrow);
194}
195
196void ClonedArguments::materializeSpecials(ExecState* exec)
197{
198 RELEASE_ASSERT(!specialsMaterialized());
199 VM& vm = exec->vm();
200
201 FunctionExecutable* executable = jsCast<FunctionExecutable*>(m_callee->executable());
202 bool isStrictMode = executable->isStrictMode();
203
204 if (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);
207 } else
208 putDirect(vm, vm.propertyNames->callee, JSValue(m_callee.get()));
209
210 putDirect(vm, vm.propertyNames->iteratorSymbol, globalObject()->arrayProtoValuesFunction(), DontEnum);
211
212 m_callee.clear();
213}
214
215void ClonedArguments::materializeSpecialsIfNecessary(ExecState* exec)
216{
217 if (!specialsMaterialized())
218 materializeSpecials(exec);
219}
220
221void ClonedArguments::visitChildren(JSCell* cell, SlotVisitor& visitor)
222{
223 ClonedArguments* thisObject = jsCast<ClonedArguments*>(cell);
224 ASSERT_GC_OBJECT_INHERITS(thisObject, info());
225 Base::visitChildren(thisObject, visitor);
226 visitor.append(&thisObject->m_callee);
227}
228
229} // namespace JSC
230