| 1 | /* |
| 2 | * Copyright (C) 2012, 2014 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 | #pragma once |
| 27 | |
| 28 | #include "PropertyStorage.h" |
| 29 | |
| 30 | namespace JSC { |
| 31 | |
| 32 | class ArrayBuffer; |
| 33 | class Butterfly; |
| 34 | class ; |
| 35 | class Structure; |
| 36 | struct ArrayStorage; |
| 37 | |
| 38 | class { |
| 39 | public: |
| 40 | // Define the maximum storage vector length to be 2^32 / sizeof(JSValue) / 2 to ensure that |
| 41 | // there is no risk of overflow. |
| 42 | enum { = 0x10000000 }; |
| 43 | |
| 44 | static ptrdiff_t () { return -static_cast<ptrdiff_t>(sizeof(IndexingHeader)); } |
| 45 | |
| 46 | static ptrdiff_t () { return OBJECT_OFFSETOF(IndexingHeader, u.typedArray.buffer); } |
| 47 | static ptrdiff_t () { return OBJECT_OFFSETOF(IndexingHeader, u.lengths.publicLength); } |
| 48 | static ptrdiff_t () { return OBJECT_OFFSETOF(IndexingHeader, u.lengths.vectorLength); } |
| 49 | |
| 50 | () |
| 51 | { |
| 52 | u.lengths.publicLength = 0; |
| 53 | u.lengths.vectorLength = 0; |
| 54 | } |
| 55 | |
| 56 | uint32_t () const { return u.lengths.vectorLength; } |
| 57 | |
| 58 | void (uint32_t length) |
| 59 | { |
| 60 | RELEASE_ASSERT(length <= maximumLength); |
| 61 | u.lengths.vectorLength = length; |
| 62 | } |
| 63 | |
| 64 | uint32_t () const { return u.lengths.publicLength; } |
| 65 | void (uint32_t auxWord) { u.lengths.publicLength = auxWord; } |
| 66 | |
| 67 | ArrayBuffer* () { return u.typedArray.buffer; } |
| 68 | void (ArrayBuffer* buffer) { u.typedArray.buffer = buffer; } |
| 69 | |
| 70 | static IndexingHeader* (Butterfly* butterfly) |
| 71 | { |
| 72 | return reinterpret_cast<IndexingHeader*>(butterfly) - 1; |
| 73 | } |
| 74 | |
| 75 | static const IndexingHeader* (const Butterfly* butterfly) |
| 76 | { |
| 77 | return reinterpret_cast<const IndexingHeader*>(butterfly) - 1; |
| 78 | } |
| 79 | |
| 80 | static IndexingHeader* (ArrayStorage* arrayStorage) |
| 81 | { |
| 82 | return const_cast<IndexingHeader*>(from(const_cast<const ArrayStorage*>(arrayStorage))); |
| 83 | } |
| 84 | |
| 85 | static const IndexingHeader* (const ArrayStorage* arrayStorage) |
| 86 | { |
| 87 | return reinterpret_cast<const IndexingHeader*>(arrayStorage) - 1; |
| 88 | } |
| 89 | |
| 90 | static IndexingHeader* (PropertyStorage propertyStorage) |
| 91 | { |
| 92 | return reinterpret_cast<IndexingHeader*>(propertyStorage); |
| 93 | } |
| 94 | |
| 95 | PropertyStorage () |
| 96 | { |
| 97 | return reinterpret_cast_ptr<PropertyStorage>(this); |
| 98 | } |
| 99 | |
| 100 | ConstPropertyStorage () const |
| 101 | { |
| 102 | return reinterpret_cast_ptr<ConstPropertyStorage>(this); |
| 103 | } |
| 104 | |
| 105 | ArrayStorage* () |
| 106 | { |
| 107 | return reinterpret_cast<ArrayStorage*>(this + 1); |
| 108 | } |
| 109 | |
| 110 | Butterfly* () |
| 111 | { |
| 112 | return reinterpret_cast<Butterfly*>(this + 1); |
| 113 | } |
| 114 | |
| 115 | // These methods are not standalone in the sense that they cannot be |
| 116 | // used on a copy of the IndexingHeader. |
| 117 | size_t (Structure*); |
| 118 | size_t (Structure*); |
| 119 | |
| 120 | private: |
| 121 | friend class LLIntOffsetsExtractor; |
| 122 | |
| 123 | union { |
| 124 | struct { |
| 125 | // FIXME: vectorLength should be least significant, so that it's really hard to craft a pointer by |
| 126 | // mucking with the butterfly. |
| 127 | // https://bugs.webkit.org/show_bug.cgi?id=174927 |
| 128 | uint32_t ; // The meaning of this field depends on the array type, but for all JSArrays we rely on this being the publicly visible length (array.length). |
| 129 | uint32_t ; // The length of the indexed property storage. The actual size of the storage depends on this, and the type. |
| 130 | } ; |
| 131 | |
| 132 | struct { |
| 133 | ArrayBuffer* ; |
| 134 | } ; |
| 135 | } ; |
| 136 | }; |
| 137 | |
| 138 | } // namespace JSC |
| 139 | |