]> git.saurik.com Git - apple/javascriptcore.git/blob - runtime/ScopedArgumentsTable.h
JavaScriptCore-7601.1.46.3.tar.gz
[apple/javascriptcore.git] / runtime / ScopedArgumentsTable.h
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 #ifndef ScopedArgumentsTable_h
27 #define ScopedArgumentsTable_h
28
29 #include "JSCell.h"
30 #include "ScopeOffset.h"
31 #include <wtf/Assertions.h>
32
33 namespace JSC {
34
35 // This class's only job is to hold onto the list of ScopeOffsets for each argument that a
36 // function has. Most of the time, the BytecodeGenerator will create one of these and it will
37 // never be modified subsequently. There is a rare case where a ScopedArguments object is created
38 // and aliases one of these and then decides to modify it; in that case we do copy-on-write. This
39 // makes sense because such modifications are so uncommon. You'd have to do something crazy like
40 // "delete arguments[i]" or some variant of defineOwnProperty.
41 class ScopedArgumentsTable final : public JSCell {
42 public:
43 typedef JSCell Base;
44 static const unsigned StructureFlags = Base::StructureFlags | StructureIsImmortal;
45
46 private:
47 ScopedArgumentsTable(VM&);
48 ~ScopedArgumentsTable();
49
50 public:
51 static ScopedArgumentsTable* create(VM&);
52 static ScopedArgumentsTable* create(VM&, uint32_t length);
53
54 static const bool needsDestruction = true;
55 static void destroy(JSCell*);
56
57 ScopedArgumentsTable* clone(VM&);
58
59 uint32_t length() const { return m_length; }
60 ScopedArgumentsTable* setLength(VM&, uint32_t newLength);
61
62 ScopeOffset get(uint32_t i) const
63 {
64 return const_cast<ScopedArgumentsTable*>(this)->at(i);
65 }
66
67 void lock()
68 {
69 m_locked = true;
70 }
71
72 ScopedArgumentsTable* set(VM&, uint32_t index, ScopeOffset);
73
74 DECLARE_INFO;
75
76 static Structure* createStructure(VM&, JSGlobalObject*, JSValue prototype);
77
78 static ptrdiff_t offsetOfLength() { return OBJECT_OFFSETOF(ScopedArgumentsTable, m_length); }
79 static ptrdiff_t offsetOfArguments() { return OBJECT_OFFSETOF(ScopedArgumentsTable, m_arguments); }
80
81 private:
82 ScopeOffset& at(uint32_t i)
83 {
84 ASSERT_WITH_SECURITY_IMPLICATION(i < m_length);
85 return m_arguments[i];
86 }
87
88 uint32_t m_length;
89 bool m_locked; // Being locked means that there are multiple references to this object and none of them expect to see the others' modifications. This means that modifications need to make a copy first.
90 std::unique_ptr<ScopeOffset[]> m_arguments;
91 };
92
93 } // namespace JSC
94
95 #endif // ScopedArgumentsTable_h
96