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 : | |
|
Lasse Reichstein
2011/08/01 09:35:54
I really, really dislike adding "Impl" to a class
danno
2011/08/02 13:37:46
Done.
| |
| 45 // public ElementsHandlerImpl<SomeElementsHandlerImpl, | |
| 46 // BackingStoreClass> { | |
| 47 // ... | |
| 48 // } | |
| 49 // | |
| 50 // This is an example of Curiously recurring template pattern | |
|
Lasse Reichstein
2011/08/01 09:35:54
Either don't capitalize "Curiously" (and prefix it
danno
2011/08/02 13:37:46
Done.
| |
| 51 // (see http://en.wikipedia.org/wiki/Curiously_recurring_template_pattern). | |
| 52 // We use CRTP to guarantee aggressive compile time optimizations (i.e. | |
| 53 // inlining and specialization of SomeElementsHandlerImpl methods). | |
| 54 template <typename ConcreteHandlerImpl, typename BackingStoreClass> | |
| 55 class ElementsHandlerImpl { | |
| 56 public: | |
| 57 inline static bool GetWithReceiver(JSObject* obj, | |
| 58 Object* receiver, | |
| 59 uint32_t index, | |
| 60 MaybeObject** result) { | |
|
Lasse Reichstein
2011/08/01 09:35:54
Why MaybeObject as result, if you always return an
danno
2011/08/02 13:37:46
The FixedDoubleArray handler (and some of the exte
| |
| 61 if (index < ConcreteHandlerImpl::GetLength(obj)) { | |
| 62 BackingStoreClass* backing_store = | |
| 63 ConcreteHandlerImpl::GetBackingStore(obj); | |
| 64 MaybeObject* value = backing_store->get(index); | |
| 65 if (!value->IsTheHole()) { | |
| 66 *result = value; | |
| 67 return true; | |
| 68 }; | |
| 69 } | |
| 70 return false; | |
| 71 } | |
| 72 | |
| 73 protected: | |
| 74 inline static BackingStoreClass* GetBackingStore(JSObject* obj) { | |
| 75 return BackingStoreClass::cast(obj->elements()); | |
| 76 } | |
| 77 | |
| 78 inline static uint32_t GetLength(JSObject* obj) { | |
| 79 return ConcreteHandlerImpl::GetBackingStore(obj)->length(); | |
| 80 } | |
| 81 | |
| 82 private: | |
| 83 DISALLOW_IMPLICIT_CONSTRUCTORS(ElementsHandlerImpl); | |
| 84 }; | |
| 85 | |
| 86 | |
| 87 class FastElementsHandlerImpl | |
| 88 : public ElementsHandlerImpl<FastElementsHandlerImpl, FixedArray> { | |
| 89 }; | |
| 90 | |
| 91 | |
| 92 class FastDoubleElementsHandlerImpl | |
| 93 : public ElementsHandlerImpl<FastDoubleElementsHandlerImpl, | |
| 94 FixedDoubleArray> { | |
| 95 }; | |
| 96 | |
| 97 | |
| 98 // Super class for all external element arrays, whose handler implementation is | |
| 99 // identical except for specialization. | |
|
Lasse Reichstein
2011/08/01 09:35:54
You specialize the only real function, so it's pre
danno
2011/08/02 13:37:46
Done.
| |
| 100 template<typename ConcreteExternalElementHandlerImpl, | |
| 101 typename ExternalArray> | |
| 102 class ExternalElementsHandlerImpl | |
| 103 : public ElementsHandlerImpl<ConcreteExternalElementHandlerImpl, | |
| 104 ExternalArray> { | |
| 105 public: | |
| 106 inline static bool GetWithReceiver(JSObject* obj, | |
| 107 Object* receiver, | |
| 108 uint32_t index, | |
| 109 MaybeObject** result) { | |
| 110 if (index < ConcreteExternalElementHandlerImpl::GetLength(obj)) { | |
| 111 ExternalArray* backing_store = | |
| 112 ConcreteExternalElementHandlerImpl::GetBackingStore(obj); | |
| 113 *result = backing_store->get(index); | |
| 114 } else { | |
| 115 *result = obj->GetHeap()->undefined_value(); | |
| 116 } | |
| 117 return true; | |
| 118 } | |
| 119 }; | |
| 120 | |
| 121 | |
| 122 class ExternalByteElementsHandlerImpl | |
| 123 : public ExternalElementsHandlerImpl<ExternalByteElementsHandlerImpl, | |
| 124 ExternalByteArray> { | |
| 125 }; | |
| 126 | |
| 127 | |
| 128 class ExternalUnsignedByteElementsHandlerImpl | |
| 129 : public ExternalElementsHandlerImpl< | |
| 130 ExternalUnsignedByteElementsHandlerImpl, ExternalUnsignedByteArray> { | |
| 131 }; | |
| 132 | |
| 133 | |
| 134 class ExternalShortElementsHandlerImpl | |
| 135 : public ExternalElementsHandlerImpl<ExternalShortElementsHandlerImpl, | |
| 136 ExternalShortArray> { | |
| 137 }; | |
| 138 | |
| 139 | |
| 140 class ExternalUnsignedShortElementsHandlerImpl | |
| 141 : public ExternalElementsHandlerImpl< | |
| 142 ExternalUnsignedShortElementsHandlerImpl, ExternalUnsignedShortArray> { | |
| 143 }; | |
| 144 | |
| 145 | |
| 146 class ExternalIntElementsHandlerImpl | |
| 147 : public ExternalElementsHandlerImpl<ExternalIntElementsHandlerImpl, | |
| 148 ExternalIntArray> { | |
| 149 }; | |
| 150 | |
| 151 | |
| 152 class ExternalUnsignedIntElementsHandlerImpl | |
| 153 : public ExternalElementsHandlerImpl<ExternalUnsignedIntElementsHandlerImpl, | |
| 154 ExternalUnsignedIntArray> { | |
| 155 }; | |
| 156 | |
| 157 | |
| 158 class ExternalFloatElementsHandlerImpl | |
| 159 : public ExternalElementsHandlerImpl<ExternalFloatElementsHandlerImpl, | |
| 160 ExternalFloatArray> { | |
| 161 }; | |
| 162 | |
| 163 | |
| 164 class ExternalDoubleElementsHandlerImpl | |
| 165 : public ExternalElementsHandlerImpl<ExternalDoubleElementsHandlerImpl, | |
| 166 ExternalDoubleArray> { | |
| 167 }; | |
| 168 | |
| 169 | |
| 170 class PixelElementsHandlerImpl | |
| 171 : public ExternalElementsHandlerImpl<PixelElementsHandlerImpl, | |
| 172 ExternalPixelArray> { | |
| 173 }; | |
| 174 | |
| 175 | |
| 176 class DictionaryElementsHandlerImpl | |
| 177 : public ElementsHandlerImpl<DictionaryElementsHandlerImpl, | |
| 178 NumberDictionary> { | |
| 179 public: | |
| 180 inline static bool GetNumberDictionaryElement( | |
|
Rico
2011/08/01 07:33:27
Do we use this function outside this class, otherw
danno
2011/08/02 13:37:46
Yes, the NonStrictArgument accessor uses this.
On
| |
| 181 JSObject* obj, | |
| 182 Object* receiver, | |
| 183 NumberDictionary* backing_store, | |
| 184 uint32_t index, | |
| 185 MaybeObject** result) { | |
| 186 int entry = backing_store->FindEntry(index); | |
| 187 if (entry != NumberDictionary::kNotFound) { | |
| 188 Object* element = backing_store->ValueAt(entry); | |
| 189 PropertyDetails details = backing_store->DetailsAt(entry); | |
| 190 if (details.type() == CALLBACKS) { | |
| 191 *result = obj->GetElementWithCallback(receiver, | |
| 192 element, | |
| 193 index, | |
| 194 obj); | |
| 195 } else { | |
| 196 *result = element; | |
| 197 } | |
| 198 return true; | |
| 199 } | |
| 200 return false; | |
| 201 } | |
| 202 | |
| 203 inline static bool GetWithReceiver(JSObject* obj, | |
| 204 Object* receiver, | |
| 205 uint32_t index, | |
| 206 MaybeObject** result) { | |
| 207 NumberDictionary* backing_store = GetBackingStore(obj); | |
| 208 return GetNumberDictionaryElement(obj, | |
| 209 receiver, | |
| 210 backing_store, | |
| 211 index, | |
| 212 result); | |
| 213 } | |
| 214 | |
| 215 protected: | |
| 216 inline static NumberDictionary* GetBackingStore(JSObject* obj) { | |
| 217 return NumberDictionary::cast(obj->element_dictionary()); | |
|
Lasse Reichstein
2011/08/01 09:35:54
Why specialize this?
The element_dictionary metho
danno
2011/08/02 13:37:46
Done.
| |
| 218 } | |
| 219 }; | |
| 220 | |
| 221 | |
| 222 class NonStrictArgumentsElementsHandlerImpl | |
| 223 : public ElementsHandlerImpl<NonStrictArgumentsElementsHandlerImpl, | |
| 224 FixedArray> { | |
| 225 public: | |
| 226 inline static bool GetWithReceiver(JSObject* obj, | |
| 227 Object* receiver, | |
| 228 uint32_t index, | |
| 229 MaybeObject** result) { | |
| 230 FixedArray* parameter_map = GetBackingStore(obj); | |
| 231 uint32_t length = parameter_map->length(); | |
| 232 Object* probe = | |
| 233 (index < length - 2) ? parameter_map->get(index + 2) : NULL; | |
| 234 if (probe != NULL && !probe->IsTheHole()) { | |
| 235 Context* context = Context::cast(parameter_map->get(0)); | |
| 236 int context_index = Smi::cast(probe)->value(); | |
| 237 *result = context->get(context_index); | |
| 238 ASSERT(!context->get(context_index)->IsTheHole()); | |
| 239 return true; | |
| 240 } else { | |
| 241 // Object is not mapped, defer to the arguments. | |
| 242 FixedArray* arguments = FixedArray::cast(parameter_map->get(1)); | |
| 243 if (arguments->IsDictionary()) { | |
| 244 return DictionaryElementsHandlerImpl::GetNumberDictionaryElement( | |
| 245 obj, | |
| 246 receiver, | |
| 247 NumberDictionary::cast(arguments), | |
| 248 index, | |
| 249 result); | |
| 250 } else if (index < static_cast<uint32_t>(arguments->length())) { | |
| 251 Object* value = arguments->get(index); | |
| 252 if (!value->IsTheHole()) { | |
| 253 *result = value; | |
| 254 return true; | |
| 255 } | |
| 256 } | |
| 257 } | |
| 258 return false; | |
| 259 } | |
| 260 }; | |
| 261 | |
| 262 | |
| 263 template<typename Impl> | |
| 264 class ConcreteElementsHandler : public ElementsHandler { | |
|
Lasse Reichstein
2011/08/01 09:35:54
"Concrete" sounds like a weasel-word too. And it's
danno
2011/08/02 13:37:46
Fixed. I prefer the classes, because they later wi
| |
| 265 public: | |
| 266 ConcreteElementsHandler() {} | |
| 267 | |
| 268 virtual bool GetWithReceiver(JSObject* obj, | |
| 269 Object* receiver, | |
| 270 uint32_t index, | |
| 271 MaybeObject** result) { | |
| 272 return Impl::GetWithReceiver(obj, | |
| 273 receiver, | |
| 274 index, | |
| 275 result); | |
|
Lasse Reichstein
2011/08/01 09:35:54
Why is this split into ElementsHandler and Element
danno
2011/08/02 13:37:46
Done.
| |
| 276 } | |
| 277 | |
| 278 private: | |
| 279 DISALLOW_COPY_AND_ASSIGN(ConcreteElementsHandler); | |
| 280 }; | |
| 281 | |
| 282 | |
| 283 struct ConcreteElementHandlers { | |
| 284 ConcreteElementsHandler<FastElementsHandlerImpl> | |
| 285 fast_elements_handler; | |
| 286 ConcreteElementsHandler<FastDoubleElementsHandlerImpl> | |
| 287 fast_double_elements_handler; | |
| 288 ConcreteElementsHandler<DictionaryElementsHandlerImpl> | |
| 289 dictionary_elements_handler; | |
| 290 ConcreteElementsHandler<NonStrictArgumentsElementsHandlerImpl> | |
| 291 non_strict_arguments_elements_handler; | |
| 292 ConcreteElementsHandler<ExternalByteElementsHandlerImpl> | |
| 293 byte_elements_handler; | |
| 294 ConcreteElementsHandler<ExternalUnsignedByteElementsHandlerImpl> | |
| 295 unsigned_byte_elements_handler; | |
| 296 ConcreteElementsHandler<ExternalShortElementsHandlerImpl> | |
| 297 short_elements_handler; | |
| 298 ConcreteElementsHandler<ExternalUnsignedShortElementsHandlerImpl> | |
| 299 unsigned_short_elements_handler; | |
| 300 ConcreteElementsHandler<ExternalIntElementsHandlerImpl> | |
| 301 int_elements_handler; | |
| 302 ConcreteElementsHandler<ExternalUnsignedIntElementsHandlerImpl> | |
| 303 unsigned_int_elements_handler; | |
| 304 ConcreteElementsHandler<ExternalFloatElementsHandlerImpl> | |
| 305 float_elements_handler; | |
| 306 ConcreteElementsHandler<ExternalDoubleElementsHandlerImpl> | |
| 307 double_elements_handler; | |
| 308 ConcreteElementsHandler<PixelElementsHandlerImpl> | |
| 309 pixel_elements_handler; | |
| 310 } element_handlers; | |
|
Lasse Reichstein
2011/08/01 09:35:54
Seems like a big static initializer. Might upset t
danno
2011/08/02 13:37:46
Done.
| |
| 311 | |
| 312 | |
| 313 ElementsHandler* ElementsHandler::elements_handler_table_[] = { | |
| 314 &element_handlers.fast_elements_handler, | |
| 315 &element_handlers.fast_double_elements_handler, | |
| 316 &element_handlers.dictionary_elements_handler, | |
| 317 &element_handlers.non_strict_arguments_elements_handler, | |
| 318 &element_handlers.byte_elements_handler, | |
| 319 &element_handlers.unsigned_byte_elements_handler, | |
| 320 &element_handlers.short_elements_handler, | |
| 321 &element_handlers.unsigned_short_elements_handler, | |
| 322 &element_handlers.int_elements_handler, | |
| 323 &element_handlers.unsigned_int_elements_handler, | |
| 324 &element_handlers.float_elements_handler, | |
| 325 &element_handlers.double_elements_handler, | |
| 326 &element_handlers.pixel_elements_handler | |
| 327 }; | |
| 328 | |
| 329 | |
| 330 } } // namespace v8::internal | |
| OLD | NEW |