| OLD | NEW |
| 1 // Copyright 2006-2008 the V8 project authors. All rights reserved. | 1 // Copyright 2006-2008 the V8 project authors. All rights reserved. |
| 2 // Redistribution and use in source and binary forms, with or without | 2 // Redistribution and use in source and binary forms, with or without |
| 3 // modification, are permitted provided that the following conditions are | 3 // modification, are permitted provided that the following conditions are |
| 4 // met: | 4 // met: |
| 5 // | 5 // |
| 6 // * Redistributions of source code must retain the above copyright | 6 // * Redistributions of source code must retain the above copyright |
| 7 // notice, this list of conditions and the following disclaimer. | 7 // notice, this list of conditions and the following disclaimer. |
| 8 // * Redistributions in binary form must reproduce the above | 8 // * Redistributions in binary form must reproduce the above |
| 9 // copyright notice, this list of conditions and the following | 9 // copyright notice, this list of conditions and the following |
| 10 // disclaimer in the documentation and/or other materials provided | 10 // disclaimer in the documentation and/or other materials provided |
| (...skipping 3121 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 3132 | 3132 |
| 3133 | 3133 |
| 3134 // The characteristics of a string are stored in its map. Retrieving these | 3134 // The characteristics of a string are stored in its map. Retrieving these |
| 3135 // few bits of information is moderately expensive, involving two memory | 3135 // few bits of information is moderately expensive, involving two memory |
| 3136 // loads where the second is dependent on the first. To improve efficiency | 3136 // loads where the second is dependent on the first. To improve efficiency |
| 3137 // the shape of the string is given its own class so that it can be retrieved | 3137 // the shape of the string is given its own class so that it can be retrieved |
| 3138 // once and used for several string operations. A StringShape is small enough | 3138 // once and used for several string operations. A StringShape is small enough |
| 3139 // to be passed by value and is immutable, but be aware that flattening a | 3139 // to be passed by value and is immutable, but be aware that flattening a |
| 3140 // string can potentially alter its shape. Also be aware that a GC caused by | 3140 // string can potentially alter its shape. Also be aware that a GC caused by |
| 3141 // something else can alter the shape of a string due to ConsString | 3141 // something else can alter the shape of a string due to ConsString |
| 3142 // shortcutting. | 3142 // shortcutting. Keeping these restrictions in mind has proven to be error- |
| 3143 // | 3143 // prone and so we no longer put StringShapes in variables unless there is a |
| 3144 // Most of the methods designed to interrogate a string as to its exact nature | 3144 // concrete performance benefit at that particular point in the code. |
| 3145 // have been made into methods on StringShape in order to encourage the use of | |
| 3146 // StringShape. The String class has both a length() and a length(StringShape) | |
| 3147 // operation. The former is simpler to type, but the latter is faster if you | |
| 3148 // need the StringShape for some other operation immediately before or after. | |
| 3149 class StringShape BASE_EMBEDDED { | 3145 class StringShape BASE_EMBEDDED { |
| 3150 public: | 3146 public: |
| 3151 inline explicit StringShape(String* s); | 3147 inline explicit StringShape(String* s); |
| 3152 inline explicit StringShape(Map* s); | 3148 inline explicit StringShape(Map* s); |
| 3153 inline explicit StringShape(InstanceType t); | 3149 inline explicit StringShape(InstanceType t); |
| 3154 inline bool IsAsciiRepresentation(); | 3150 inline bool IsAsciiRepresentation(); |
| 3155 inline bool IsTwoByteRepresentation(); | 3151 inline bool IsTwoByteRepresentation(); |
| 3156 inline bool IsSequential(); | 3152 inline bool IsSequential(); |
| 3157 inline bool IsExternal(); | 3153 inline bool IsExternal(); |
| 3158 inline bool IsCons(); | 3154 inline bool IsCons(); |
| (...skipping 28 matching lines...) Expand all Loading... |
| 3187 // | 3183 // |
| 3188 // Ecma-262: | 3184 // Ecma-262: |
| 3189 // 4.3.16 String Value | 3185 // 4.3.16 String Value |
| 3190 // A string value is a member of the type String and is a finite | 3186 // A string value is a member of the type String and is a finite |
| 3191 // ordered sequence of zero or more 16-bit unsigned integer values. | 3187 // ordered sequence of zero or more 16-bit unsigned integer values. |
| 3192 // | 3188 // |
| 3193 // All string values have a length field. | 3189 // All string values have a length field. |
| 3194 class String: public HeapObject { | 3190 class String: public HeapObject { |
| 3195 public: | 3191 public: |
| 3196 // Get and set the length of the string. | 3192 // Get and set the length of the string. |
| 3197 // Fast version. | |
| 3198 inline int length(StringShape shape); | |
| 3199 // Easy version. | |
| 3200 inline int length(); | 3193 inline int length(); |
| 3201 inline void set_length(int value); | 3194 inline void set_length(int value); |
| 3202 | 3195 |
| 3203 // Get and set the uninterpreted length field of the string. Notice | 3196 // Get and set the uninterpreted length field of the string. Notice |
| 3204 // that the length field is also used to cache the hash value of | 3197 // that the length field is also used to cache the hash value of |
| 3205 // strings. In order to get or set the actual length of the string | 3198 // strings. In order to get or set the actual length of the string |
| 3206 // use the length() and set_length methods. | 3199 // use the length() and set_length methods. |
| 3207 inline uint32_t length_field(); | 3200 inline uint32_t length_field(); |
| 3208 inline void set_length_field(uint32_t value); | 3201 inline void set_length_field(uint32_t value); |
| 3209 | 3202 |
| 3210 // Get and set individual two byte chars in the string. | 3203 // Get and set individual two byte chars in the string. |
| 3211 inline void Set(StringShape shape, int index, uint16_t value); | 3204 inline void Set(int index, uint16_t value); |
| 3212 // Get individual two byte char in the string. Repeated calls | 3205 // Get individual two byte char in the string. Repeated calls |
| 3213 // to this method are not efficient unless the string is flat. | 3206 // to this method are not efficient unless the string is flat. |
| 3214 inline uint16_t Get(StringShape shape, int index); | 3207 inline uint16_t Get(int index); |
| 3215 | 3208 |
| 3216 // Try to flatten the top level ConsString that is hiding behind this | 3209 // Try to flatten the top level ConsString that is hiding behind this |
| 3217 // string. This is a no-op unless the string is a ConsString or a | 3210 // string. This is a no-op unless the string is a ConsString or a |
| 3218 // SlicedString. Flatten mutates the ConsString and might return a | 3211 // SlicedString. Flatten mutates the ConsString and might return a |
| 3219 // failure. | 3212 // failure. |
| 3220 Object* TryFlatten(StringShape shape); | 3213 Object* TryFlatten(); |
| 3221 | 3214 |
| 3222 // Try to flatten the string. Checks first inline to see if it is necessary. | 3215 // Try to flatten the string. Checks first inline to see if it is necessary. |
| 3223 // Do not handle allocation failures. After calling TryFlattenIfNotFlat, the | 3216 // Do not handle allocation failures. After calling TryFlattenIfNotFlat, the |
| 3224 // string could still be a ConsString, in which case a failure is returned. | 3217 // string could still be a ConsString, in which case a failure is returned. |
| 3225 // Use FlattenString from Handles.cc to be sure to flatten. | 3218 // Use FlattenString from Handles.cc to be sure to flatten. |
| 3226 inline Object* TryFlattenIfNotFlat(StringShape shape); | 3219 inline Object* TryFlattenIfNotFlat(); |
| 3227 | 3220 |
| 3228 Vector<const char> ToAsciiVector(); | 3221 Vector<const char> ToAsciiVector(); |
| 3229 Vector<const uc16> ToUC16Vector(); | 3222 Vector<const uc16> ToUC16Vector(); |
| 3230 | 3223 |
| 3231 // Mark the string as an undetectable object. It only applies to | 3224 // Mark the string as an undetectable object. It only applies to |
| 3232 // ascii and two byte string types. | 3225 // ascii and two byte string types. |
| 3233 bool MarkAsUndetectable(); | 3226 bool MarkAsUndetectable(); |
| 3234 | 3227 |
| 3235 // Slice the string and return a substring. | 3228 // Slice the string and return a substring. |
| 3236 Object* Slice(int from, int to); | 3229 Object* Slice(int from, int to); |
| (...skipping 58 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 3295 | 3288 |
| 3296 // For use during stack traces. Performs rudimentary sanity check. | 3289 // For use during stack traces. Performs rudimentary sanity check. |
| 3297 bool LooksValid(); | 3290 bool LooksValid(); |
| 3298 | 3291 |
| 3299 // Dispatched behavior. | 3292 // Dispatched behavior. |
| 3300 void StringShortPrint(StringStream* accumulator); | 3293 void StringShortPrint(StringStream* accumulator); |
| 3301 #ifdef DEBUG | 3294 #ifdef DEBUG |
| 3302 void StringPrint(); | 3295 void StringPrint(); |
| 3303 void StringVerify(); | 3296 void StringVerify(); |
| 3304 #endif | 3297 #endif |
| 3305 inline bool IsFlat(StringShape shape); | 3298 inline bool IsFlat(); |
| 3306 | 3299 |
| 3307 // Layout description. | 3300 // Layout description. |
| 3308 static const int kLengthOffset = HeapObject::kHeaderSize; | 3301 static const int kLengthOffset = HeapObject::kHeaderSize; |
| 3309 static const int kSize = kLengthOffset + kIntSize; | 3302 static const int kSize = kLengthOffset + kIntSize; |
| 3310 | 3303 |
| 3311 // Limits on sizes of different types of strings. | 3304 // Limits on sizes of different types of strings. |
| 3312 static const int kMaxShortStringSize = 63; | 3305 static const int kMaxShortStringSize = 63; |
| 3313 static const int kMaxMediumStringSize = 16383; | 3306 static const int kMaxMediumStringSize = 16383; |
| 3314 | 3307 |
| 3315 static const int kMaxArrayIndexSize = 10; | 3308 static const int kMaxArrayIndexSize = 10; |
| (...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 3357 unsigned* offset); | 3350 unsigned* offset); |
| 3358 static const unibrow::byte* ReadBlock(String** input, | 3351 static const unibrow::byte* ReadBlock(String** input, |
| 3359 unibrow::byte* util_buffer, | 3352 unibrow::byte* util_buffer, |
| 3360 unsigned capacity, | 3353 unsigned capacity, |
| 3361 unsigned* remaining, | 3354 unsigned* remaining, |
| 3362 unsigned* offset); | 3355 unsigned* offset); |
| 3363 | 3356 |
| 3364 // Helper function for flattening strings. | 3357 // Helper function for flattening strings. |
| 3365 template <typename sinkchar> | 3358 template <typename sinkchar> |
| 3366 static void WriteToFlat(String* source, | 3359 static void WriteToFlat(String* source, |
| 3367 StringShape shape, | |
| 3368 sinkchar* sink, | 3360 sinkchar* sink, |
| 3369 int from, | 3361 int from, |
| 3370 int to); | 3362 int to); |
| 3371 | 3363 |
| 3372 protected: | 3364 protected: |
| 3373 class ReadBlockBuffer { | 3365 class ReadBlockBuffer { |
| 3374 public: | 3366 public: |
| 3375 ReadBlockBuffer(unibrow::byte* util_buffer_, | 3367 ReadBlockBuffer(unibrow::byte* util_buffer_, |
| 3376 unsigned cursor_, | 3368 unsigned cursor_, |
| 3377 unsigned capacity_, | 3369 unsigned capacity_, |
| (...skipping 20 matching lines...) Expand all Loading... |
| 3398 unsigned* offset, | 3390 unsigned* offset, |
| 3399 unsigned max_chars); | 3391 unsigned max_chars); |
| 3400 static void ReadBlockIntoBuffer(String* input, | 3392 static void ReadBlockIntoBuffer(String* input, |
| 3401 ReadBlockBuffer* buffer, | 3393 ReadBlockBuffer* buffer, |
| 3402 unsigned* offset_ptr, | 3394 unsigned* offset_ptr, |
| 3403 unsigned max_chars); | 3395 unsigned max_chars); |
| 3404 | 3396 |
| 3405 private: | 3397 private: |
| 3406 // Slow case of String::Equals. This implementation works on any strings | 3398 // Slow case of String::Equals. This implementation works on any strings |
| 3407 // but it is most efficient on strings that are almost flat. | 3399 // but it is most efficient on strings that are almost flat. |
| 3408 bool SlowEquals(StringShape this_shape, | 3400 bool SlowEquals(String* other); |
| 3409 String* other, | |
| 3410 StringShape other_shape); | |
| 3411 | 3401 |
| 3412 // Slow case of AsArrayIndex. | 3402 // Slow case of AsArrayIndex. |
| 3413 bool SlowAsArrayIndex(uint32_t* index); | 3403 bool SlowAsArrayIndex(uint32_t* index); |
| 3414 | 3404 |
| 3415 // Compute and set the hash code. | 3405 // Compute and set the hash code. |
| 3416 uint32_t ComputeAndSetHash(); | 3406 uint32_t ComputeAndSetHash(); |
| 3417 | 3407 |
| 3418 DISALLOW_IMPLICIT_CONSTRUCTORS(String); | 3408 DISALLOW_IMPLICIT_CONSTRUCTORS(String); |
| 3419 }; | 3409 }; |
| 3420 | 3410 |
| (...skipping 26 matching lines...) Expand all Loading... |
| 3447 inline Address GetCharsAddress(); | 3437 inline Address GetCharsAddress(); |
| 3448 | 3438 |
| 3449 inline char* GetChars(); | 3439 inline char* GetChars(); |
| 3450 | 3440 |
| 3451 // Casting | 3441 // Casting |
| 3452 static inline SeqAsciiString* cast(Object* obj); | 3442 static inline SeqAsciiString* cast(Object* obj); |
| 3453 | 3443 |
| 3454 // Garbage collection support. This method is called by the | 3444 // Garbage collection support. This method is called by the |
| 3455 // garbage collector to compute the actual size of an AsciiString | 3445 // garbage collector to compute the actual size of an AsciiString |
| 3456 // instance. | 3446 // instance. |
| 3457 inline int SeqAsciiStringSize(StringShape shape); | 3447 inline int SeqAsciiStringSize(InstanceType instance_type); |
| 3458 | 3448 |
| 3459 // Computes the size for an AsciiString instance of a given length. | 3449 // Computes the size for an AsciiString instance of a given length. |
| 3460 static int SizeFor(int length) { | 3450 static int SizeFor(int length) { |
| 3461 return kHeaderSize + OBJECT_SIZE_ALIGN(length * kCharSize); | 3451 return kHeaderSize + OBJECT_SIZE_ALIGN(length * kCharSize); |
| 3462 } | 3452 } |
| 3463 | 3453 |
| 3464 // Layout description. | 3454 // Layout description. |
| 3465 static const int kHeaderSize = String::kSize; | 3455 static const int kHeaderSize = String::kSize; |
| 3466 | 3456 |
| 3467 // Support for StringInputBuffer. | 3457 // Support for StringInputBuffer. |
| (...skipping 24 matching lines...) Expand all Loading... |
| 3492 | 3482 |
| 3493 // For regexp code. | 3483 // For regexp code. |
| 3494 const uint16_t* SeqTwoByteStringGetData(unsigned start); | 3484 const uint16_t* SeqTwoByteStringGetData(unsigned start); |
| 3495 | 3485 |
| 3496 // Casting | 3486 // Casting |
| 3497 static inline SeqTwoByteString* cast(Object* obj); | 3487 static inline SeqTwoByteString* cast(Object* obj); |
| 3498 | 3488 |
| 3499 // Garbage collection support. This method is called by the | 3489 // Garbage collection support. This method is called by the |
| 3500 // garbage collector to compute the actual size of a TwoByteString | 3490 // garbage collector to compute the actual size of a TwoByteString |
| 3501 // instance. | 3491 // instance. |
| 3502 inline int SeqTwoByteStringSize(StringShape shape); | 3492 inline int SeqTwoByteStringSize(InstanceType instance_type); |
| 3503 | 3493 |
| 3504 // Computes the size for a TwoByteString instance of a given length. | 3494 // Computes the size for a TwoByteString instance of a given length. |
| 3505 static int SizeFor(int length) { | 3495 static int SizeFor(int length) { |
| 3506 return kHeaderSize + OBJECT_SIZE_ALIGN(length * kShortSize); | 3496 return kHeaderSize + OBJECT_SIZE_ALIGN(length * kShortSize); |
| 3507 } | 3497 } |
| 3508 | 3498 |
| 3509 // Layout description. | 3499 // Layout description. |
| 3510 static const int kHeaderSize = String::kSize; | 3500 static const int kHeaderSize = String::kSize; |
| 3511 | 3501 |
| 3512 // Support for StringInputBuffer. | 3502 // Support for StringInputBuffer. |
| (...skipping 805 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 4318 } else { | 4308 } else { |
| 4319 value &= ~(1 << bit_position); | 4309 value &= ~(1 << bit_position); |
| 4320 } | 4310 } |
| 4321 return value; | 4311 return value; |
| 4322 } | 4312 } |
| 4323 }; | 4313 }; |
| 4324 | 4314 |
| 4325 } } // namespace v8::internal | 4315 } } // namespace v8::internal |
| 4326 | 4316 |
| 4327 #endif // V8_OBJECTS_H_ | 4317 #endif // V8_OBJECTS_H_ |
| OLD | NEW |