1/*
2 * Copyright (C) 2015-2018 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 "ScopedArguments.h"
28
29#include "GenericArgumentsInlines.h"
30#include "JSCInlines.h"
31
32namespace JSC {
33
34STATIC_ASSERT_IS_TRIVIALLY_DESTRUCTIBLE(ScopedArguments);
35
36const ClassInfo ScopedArguments::s_info = { "Arguments", &Base::s_info, nullptr, nullptr, CREATE_METHOD_TABLE(ScopedArguments) };
37
38ScopedArguments::ScopedArguments(VM& vm, Structure* structure, WriteBarrier<Unknown>* storage, unsigned totalLength)
39 : GenericArguments(vm, structure)
40 , m_totalLength(totalLength)
41{
42 if (storage)
43 m_storage.set(vm, this, storage);
44}
45
46void ScopedArguments::finishCreation(VM& vm, JSFunction* callee, ScopedArgumentsTable* table, JSLexicalEnvironment* scope)
47{
48 Base::finishCreation(vm);
49 m_callee.set(vm, this, callee);
50 m_table.set(vm, this, table);
51 m_scope.set(vm, this, scope);
52}
53
54ScopedArguments* ScopedArguments::createUninitialized(VM& vm, Structure* structure, JSFunction* callee, ScopedArgumentsTable* table, JSLexicalEnvironment* scope, unsigned totalLength)
55{
56 WriteBarrier<Unknown>* storage = nullptr;
57 if (totalLength > table->length()) {
58 Checked<unsigned> overflowLength = totalLength - table->length();
59 storage = static_cast<WriteBarrier<Unknown>*>(vm.jsValueGigacageAuxiliarySpace.allocateNonVirtual(vm, (overflowLength * sizeof(WriteBarrier<Unknown>)).unsafeGet(), nullptr, AllocationFailureMode::Assert));
60 }
61
62 ScopedArguments* result = new (
63 NotNull,
64 allocateCell<ScopedArguments>(vm.heap))
65 ScopedArguments(vm, structure, storage, totalLength);
66 result->finishCreation(vm, callee, table, scope);
67 return result;
68}
69
70ScopedArguments* ScopedArguments::create(VM& vm, Structure* structure, JSFunction* callee, ScopedArgumentsTable* table, JSLexicalEnvironment* scope, unsigned totalLength)
71{
72 ScopedArguments* result =
73 createUninitialized(vm, structure, callee, table, scope, totalLength);
74
75 unsigned namedLength = table->length();
76 for (unsigned i = namedLength; i < totalLength; ++i)
77 result->storage()[i - namedLength].clear();
78
79 return result;
80}
81
82ScopedArguments* ScopedArguments::createByCopying(JSGlobalObject* globalObject, CallFrame* callFrame, ScopedArgumentsTable* table, JSLexicalEnvironment* scope)
83{
84 return createByCopyingFrom(
85 globalObject->vm(), globalObject->scopedArgumentsStructure(),
86 callFrame->registers() + CallFrame::argumentOffset(0), callFrame->argumentCount(),
87 jsCast<JSFunction*>(callFrame->jsCallee()), table, scope);
88}
89
90ScopedArguments* ScopedArguments::createByCopyingFrom(VM& vm, Structure* structure, Register* argumentsStart, unsigned totalLength, JSFunction* callee, ScopedArgumentsTable* table, JSLexicalEnvironment* scope)
91{
92 ScopedArguments* result =
93 createUninitialized(vm, structure, callee, table, scope, totalLength);
94
95 unsigned namedLength = table->length();
96 for (unsigned i = namedLength; i < totalLength; ++i)
97 result->storage()[i - namedLength].set(vm, result, argumentsStart[i].jsValue());
98
99 return result;
100}
101
102void ScopedArguments::visitChildren(JSCell* cell, SlotVisitor& visitor)
103{
104 ScopedArguments* thisObject = static_cast<ScopedArguments*>(cell);
105 ASSERT_GC_OBJECT_INHERITS(thisObject, info());
106 Base::visitChildren(thisObject, visitor);
107
108 visitor.append(thisObject->m_callee);
109 visitor.append(thisObject->m_table);
110 visitor.append(thisObject->m_scope);
111
112 if (WriteBarrier<Unknown>* storage = thisObject->m_storage.get()) {
113 visitor.markAuxiliary(storage);
114 if (thisObject->m_totalLength > thisObject->m_table->length())
115 visitor.appendValues(storage, thisObject->m_totalLength - thisObject->m_table->length());
116 }
117}
118
119Structure* ScopedArguments::createStructure(VM& vm, JSGlobalObject* globalObject, JSValue prototype)
120{
121 return Structure::create(vm, globalObject, prototype, TypeInfo(ScopedArgumentsType, StructureFlags), info());
122}
123
124void ScopedArguments::overrideThings(VM& vm)
125{
126 RELEASE_ASSERT(!m_overrodeThings);
127
128 putDirect(vm, vm.propertyNames->length, jsNumber(m_table->length()), static_cast<unsigned>(PropertyAttribute::DontEnum));
129 putDirect(vm, vm.propertyNames->callee, m_callee.get(), static_cast<unsigned>(PropertyAttribute::DontEnum));
130 putDirect(vm, vm.propertyNames->iteratorSymbol, globalObject(vm)->arrayProtoValuesFunction(), static_cast<unsigned>(PropertyAttribute::DontEnum));
131
132 m_overrodeThings = true;
133}
134
135void ScopedArguments::overrideThingsIfNecessary(VM& vm)
136{
137 if (!m_overrodeThings)
138 overrideThings(vm);
139}
140
141void ScopedArguments::unmapArgument(VM& vm, uint32_t i)
142{
143 ASSERT_WITH_SECURITY_IMPLICATION(i < m_totalLength);
144 unsigned namedLength = m_table->length();
145 if (i < namedLength)
146 m_table.set(vm, this, m_table->set(vm, i, ScopeOffset()));
147 else
148 storage()[i - namedLength].clear();
149}
150
151void ScopedArguments::copyToArguments(JSGlobalObject* globalObject, CallFrame* callFrame, VirtualRegister firstElementDest, unsigned offset, unsigned length)
152{
153 GenericArguments::copyToArguments(globalObject, callFrame, firstElementDest, offset, length);
154}
155
156} // namespace JSC
157
158