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