Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 1 // Copyright 2011 the V8 project authors. All rights reserved. | |
| 2 // Redistribution and use in source and binary forms, with or without | |
| 3 // modification, are permitted provided that the following conditions are | |
| 4 // met: | |
| 5 // | |
| 6 // * Redistributions of source code must retain the above copyright | |
| 7 // notice, this list of conditions and the following disclaimer. | |
| 8 // * Redistributions in binary form must reproduce the above | |
| 9 // copyright notice, this list of conditions and the following | |
| 10 // disclaimer in the documentation and/or other materials provided | |
| 11 // with the distribution. | |
| 12 // * Neither the name of Google Inc. nor the names of its | |
| 13 // contributors may be used to endorse or promote products derived | |
| 14 // from this software without specific prior written permission. | |
| 15 // | |
| 16 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS | |
| 17 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT | |
| 18 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR | |
| 19 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT | |
| 20 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, | |
| 21 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT | |
| 22 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, | |
| 23 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY | |
| 24 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT | |
| 25 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE | |
| 26 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | |
| 27 | |
| 28 #include "v8.h" | |
| 29 | |
| 30 #include "objects.h" | |
| 31 #include "elements.h" | |
| 32 | |
| 33 namespace v8 { | |
| 34 namespace internal { | |
| 35 | |
| 36 | |
| 37 // Base class for element handler implementations. Contains the | |
| 38 // the common logic for objects with different ElementsKinds. | |
| 39 // Subclasses must specialize method for which the element | |
| 40 // implementation differs from the base class implementation. | |
| 41 // | |
| 42 // This class is intended to be used in the following way: | |
| 43 // | |
| 44 // class SomeElementsHandlerImpl : | |
| 45 // public ElementsHandlerImpl<SomeElementsHandlerImpl> { | |
|
Jakob Kummerow
2011/07/30 10:29:21
nit: second template argument (BackingStoreClass)
danno
2011/07/30 13:10:10
Done.
| |
| 46 // ... | |
| 47 // } | |
| 48 // | |
| 49 // This is an example of Curiously recurring template pattern | |
| 50 // (see http://en.wikipedia.org/wiki/Curiously_recurring_template_pattern). | |
| 51 // We use CRTP to guarantee aggressive compile time optimizations (i.e. | |
| 52 // inlining and specialization of SomeElementsHandlerImpl methods). | |
| 53 template <typename ConcreteHandlerImpl, typename BackingStoreClass> | |
| 54 class ElementsHandlerImpl { | |
| 55 public: | |
| 56 static MaybeObject* GetWithReceiver(JSObject* obj, | |
| 57 Object* receiver, | |
| 58 uint32_t index) { | |
| 59 if (index < ConcreteHandlerImpl::GetLength(obj)) { | |
| 60 BackingStoreClass* backing_store = | |
| 61 ElementsHandlerImpl::GetBackingStore(obj); | |
| 62 MaybeObject* result = backing_store->get(index); | |
| 63 if (!result->IsTheHole()) return result; | |
| 64 } | |
| 65 | |
| 66 Object* pt = obj->GetPrototype(); | |
| 67 Heap* heap = obj->GetHeap(); | |
| 68 if (pt == heap->null_value()) return heap->undefined_value(); | |
| 69 return pt->GetElementWithReceiver(receiver, index); | |
| 70 } | |
| 71 | |
| 72 protected: | |
| 73 static BackingStoreClass* GetBackingStore(JSObject* obj) { | |
| 74 return BackingStoreClass::cast(obj->elements()); | |
| 75 } | |
| 76 | |
| 77 static uint32_t GetLength(JSObject* obj) { | |
| 78 return ConcreteHandlerImpl::GetBackingStore(obj)->length(); | |
| 79 } | |
| 80 | |
| 81 private: | |
| 82 DISALLOW_IMPLICIT_CONSTRUCTORS(ElementsHandlerImpl); | |
| 83 }; | |
| 84 | |
| 85 | |
| 86 class FastElementsHandlerImpl | |
| 87 : public ElementsHandlerImpl< FastElementsHandlerImpl, FixedArray> { | |
|
Jakob Kummerow
2011/07/30 10:29:21
nit: extra space
danno
2011/07/30 13:10:10
Done.
| |
| 88 }; | |
| 89 | |
| 90 | |
| 91 class FastDoubleElementsHandlerImpl | |
| 92 : public ElementsHandlerImpl<FastDoubleElementsHandlerImpl, | |
| 93 FixedDoubleArray> { | |
| 94 }; | |
| 95 | |
| 96 | |
| 97 // Super class for all external element arrays, whose handler implementation is | |
| 98 // identical except for specialization. | |
| 99 template<typename ConcreteExternalElementHandlerImpl, | |
| 100 typename ExternalArray> | |
| 101 class ExternalElementsHandlerImpl | |
| 102 : public ElementsHandlerImpl<ConcreteExternalElementHandlerImpl, | |
| 103 ExternalArray> { | |
| 104 }; | |
| 105 | |
| 106 | |
| 107 class ExternalByteElementsHandlerImpl | |
| 108 : public ExternalElementsHandlerImpl<ExternalByteElementsHandlerImpl, | |
| 109 ExternalByteArray> { | |
| 110 }; | |
| 111 | |
| 112 | |
| 113 class ExternalUnsignedByteElementsHandlerImpl | |
| 114 : public ExternalElementsHandlerImpl< | |
| 115 ExternalUnsignedByteElementsHandlerImpl, ExternalUnsignedByteArray> { | |
| 116 }; | |
| 117 | |
| 118 | |
| 119 class ExternalShortElementsHandlerImpl | |
| 120 : public ExternalElementsHandlerImpl<ExternalShortElementsHandlerImpl, | |
| 121 ExternalShortArray> { | |
| 122 }; | |
| 123 | |
| 124 | |
| 125 class ExternalUnsignedShortElementsHandlerImpl | |
| 126 : public ExternalElementsHandlerImpl< | |
| 127 ExternalUnsignedShortElementsHandlerImpl, ExternalUnsignedShortArray> { | |
| 128 }; | |
| 129 | |
| 130 | |
| 131 class ExternalIntElementsHandlerImpl | |
| 132 : public ExternalElementsHandlerImpl<ExternalIntElementsHandlerImpl, | |
| 133 ExternalIntArray> { | |
| 134 }; | |
| 135 | |
| 136 | |
| 137 class ExternalUnsignedIntElementsHandlerImpl | |
| 138 : public ExternalElementsHandlerImpl<ExternalUnsignedIntElementsHandlerImpl, | |
| 139 ExternalUnsignedIntArray> { | |
| 140 }; | |
| 141 | |
| 142 | |
| 143 class ExternalFloatElementsHandlerImpl | |
| 144 : public ExternalElementsHandlerImpl<ExternalFloatElementsHandlerImpl, | |
| 145 ExternalFloatArray> { | |
| 146 }; | |
| 147 | |
| 148 | |
| 149 class ExternalDoubleElementsHandlerImpl | |
| 150 : public ExternalElementsHandlerImpl<ExternalDoubleElementsHandlerImpl, | |
| 151 ExternalDoubleArray> { | |
| 152 }; | |
| 153 | |
| 154 | |
| 155 class PixelElementsHandlerImpl | |
| 156 : public ExternalElementsHandlerImpl<PixelElementsHandlerImpl, | |
| 157 ExternalPixelArray> { | |
| 158 }; | |
| 159 | |
| 160 | |
| 161 class DictionaryElementsHandlerImpl | |
| 162 : public ElementsHandlerImpl<DictionaryElementsHandlerImpl, | |
| 163 NumberDictionary> { | |
| 164 public: | |
| 165 static MaybeObject* GetNumberDictionaryElement( | |
| 166 JSObject* obj, | |
| 167 Object* receiver, | |
| 168 NumberDictionary* backing_store, | |
| 169 uint32_t index) { | |
| 170 int entry = backing_store->FindEntry(index); | |
| 171 if (entry != NumberDictionary::kNotFound) { | |
| 172 Object* element = backing_store->ValueAt(entry); | |
| 173 PropertyDetails details = backing_store->DetailsAt(entry); | |
| 174 if (details.type() == CALLBACKS) { | |
| 175 return obj->GetElementWithCallback(receiver, | |
| 176 element, | |
| 177 index, | |
| 178 obj); | |
| 179 } | |
| 180 return element; | |
| 181 } | |
| 182 return obj->GetHeap()->the_hole_value(); | |
| 183 } | |
| 184 | |
| 185 static MaybeObject* GetWithReceiver(JSObject* obj, | |
| 186 Object* receiver, | |
| 187 uint32_t index) { | |
| 188 NumberDictionary* backing_store = GetBackingStore(obj); | |
| 189 return GetNumberDictionaryElement(obj, receiver, backing_store, index); | |
| 190 } | |
| 191 | |
| 192 protected: | |
| 193 static NumberDictionary* GetBackingStore(JSObject* obj) { | |
| 194 return NumberDictionary::cast(obj->element_dictionary()); | |
| 195 } | |
| 196 }; | |
| 197 | |
| 198 | |
| 199 class NonStrictArgumentsElementsHandlerImpl | |
| 200 : public ElementsHandlerImpl<NonStrictArgumentsElementsHandlerImpl, | |
| 201 FixedArray> { | |
| 202 public: | |
| 203 static MaybeObject* GetWithReceiver(JSObject* obj, | |
| 204 Object* receiver, | |
| 205 uint32_t index) { | |
| 206 FixedArray* parameter_map = GetBackingStore(obj); | |
| 207 uint32_t length = parameter_map->length(); | |
| 208 Object* probe = | |
| 209 (index < length - 2) ? parameter_map->get(index + 2) : NULL; | |
| 210 if (probe != NULL && !probe->IsTheHole()) { | |
| 211 Context* context = Context::cast(parameter_map->get(0)); | |
| 212 int context_index = Smi::cast(probe)->value(); | |
| 213 ASSERT(!context->get(context_index)->IsTheHole()); | |
| 214 return context->get(context_index); | |
| 215 } else { | |
| 216 // Object is not mapped, defer to the arguments. | |
| 217 FixedArray* arguments = FixedArray::cast(parameter_map->get(1)); | |
| 218 if (arguments->IsDictionary()) { | |
| 219 return DictionaryElementsHandlerImpl::GetNumberDictionaryElement( | |
| 220 obj, | |
| 221 receiver, | |
| 222 NumberDictionary::cast(arguments), | |
| 223 index); | |
| 224 } else if (index < static_cast<uint32_t>(arguments->length())) { | |
| 225 return arguments->get(index); | |
| 226 } | |
| 227 } | |
| 228 return obj->GetHeap()->the_hole_value(); | |
| 229 } | |
| 230 }; | |
| 231 | |
| 232 | |
| 233 template<typename Impl> | |
| 234 class ConcreteElementsHandler : public ElementsHandler { | |
| 235 public: | |
| 236 ConcreteElementsHandler() {} | |
| 237 | |
| 238 virtual MaybeObject* GetWithReceiver(JSObject* obj, | |
| 239 Object* receiver, | |
| 240 uint32_t index) { | |
| 241 return Impl::GetWithReceiver(obj, | |
| 242 receiver, | |
| 243 index); | |
| 244 } | |
| 245 private: | |
| 246 DISALLOW_COPY_AND_ASSIGN(ConcreteElementsHandler); | |
| 247 }; | |
| 248 | |
| 249 | |
| 250 struct ConcreteElementHandlers { | |
|
Jakob Kummerow
2011/07/30 10:29:21
nit: for consistency, change order to match elemen
danno
2011/07/30 13:10:10
Done.
| |
| 251 ConcreteElementsHandler<FastElementsHandlerImpl> | |
| 252 fast_elements_handler; | |
| 253 ConcreteElementsHandler<FastDoubleElementsHandlerImpl> | |
| 254 fast_double_elements_handler; | |
| 255 ConcreteElementsHandler<ExternalByteElementsHandlerImpl> | |
| 256 byte_elements_handler; | |
| 257 ConcreteElementsHandler<ExternalUnsignedByteElementsHandlerImpl> | |
| 258 unsigned_byte_elements_handler; | |
| 259 ConcreteElementsHandler<ExternalShortElementsHandlerImpl> | |
| 260 short_elements_handler; | |
| 261 ConcreteElementsHandler<ExternalUnsignedShortElementsHandlerImpl> | |
| 262 unsigned_short_elements_handler; | |
| 263 ConcreteElementsHandler<ExternalIntElementsHandlerImpl> | |
| 264 int_elements_handler; | |
| 265 ConcreteElementsHandler<ExternalUnsignedIntElementsHandlerImpl> | |
| 266 unsigned_int_elements_handler; | |
| 267 ConcreteElementsHandler<ExternalFloatElementsHandlerImpl> | |
| 268 float_elements_handler; | |
| 269 ConcreteElementsHandler<ExternalDoubleElementsHandlerImpl> | |
| 270 double_elements_handler; | |
| 271 ConcreteElementsHandler<PixelElementsHandlerImpl> | |
| 272 pixel_elements_handler; | |
| 273 ConcreteElementsHandler<DictionaryElementsHandlerImpl> | |
| 274 dictionary_elements_handler; | |
| 275 ConcreteElementsHandler<NonStrictArgumentsElementsHandlerImpl> | |
| 276 non_strict_arguments_elements_handler; | |
| 277 } element_handlers; | |
| 278 | |
| 279 | |
| 280 ElementsHandler* ElementsHandler::elements_handler_table_[] = { | |
| 281 &element_handlers.fast_elements_handler, | |
| 282 &element_handlers.fast_double_elements_handler, | |
| 283 &element_handlers.dictionary_elements_handler, | |
| 284 &element_handlers.non_strict_arguments_elements_handler, | |
| 285 &element_handlers.byte_elements_handler, | |
| 286 &element_handlers.unsigned_byte_elements_handler, | |
| 287 &element_handlers.short_elements_handler, | |
| 288 &element_handlers.unsigned_short_elements_handler, | |
| 289 &element_handlers.int_elements_handler, | |
| 290 &element_handlers.unsigned_int_elements_handler, | |
| 291 &element_handlers.float_elements_handler, | |
| 292 &element_handlers.double_elements_handler, | |
| 293 &element_handlers.pixel_elements_handler | |
| 294 }; | |
| 295 | |
| 296 | |
| 297 } } // namespace v8::internal | |
| OLD | NEW |