OLD | NEW |
1 // Copyright 2014 the V8 project authors. All rights reserved. | 1 // Copyright 2014 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 24 matching lines...) Expand all Loading... |
35 // AstString, AstValue and AstValueFactory are for storing strings and values | 35 // AstString, AstValue and AstValueFactory are for storing strings and values |
36 // independent of the V8 heap and internalizing them later. During parsing, | 36 // independent of the V8 heap and internalizing them later. During parsing, |
37 // AstStrings and AstValues are created and stored outside the heap, in | 37 // AstStrings and AstValues are created and stored outside the heap, in |
38 // AstValueFactory. After parsing, the strings and values are internalized | 38 // AstValueFactory. After parsing, the strings and values are internalized |
39 // (moved into the V8 heap). | 39 // (moved into the V8 heap). |
40 namespace v8 { | 40 namespace v8 { |
41 namespace internal { | 41 namespace internal { |
42 | 42 |
43 class AstString : public ZoneObject { | 43 class AstString : public ZoneObject { |
44 public: | 44 public: |
45 virtual ~AstString() {} | 45 explicit AstString(bool is_raw) |
| 46 : bit_field_(IsRawStringBits::encode(is_raw)) {} |
46 | 47 |
47 virtual int length() const = 0; | 48 ~AstString() {} |
| 49 |
| 50 int length() const; |
48 bool IsEmpty() const { return length() == 0; } | 51 bool IsEmpty() const { return length() == 0; } |
49 | 52 |
50 // Puts the string into the V8 heap. | 53 // Puts the string into the V8 heap. |
51 virtual void Internalize(Isolate* isolate) = 0; | 54 void Internalize(Isolate* isolate); |
52 | 55 |
53 // This function can be called after internalizing. | 56 // This function can be called after internalizing. |
54 V8_INLINE Handle<String> string() const { | 57 V8_INLINE Handle<String> string() const { |
55 DCHECK(!string_.is_null()); | 58 DCHECK(!string_.is_null()); |
56 return string_; | 59 return string_; |
57 } | 60 } |
58 | 61 |
59 protected: | 62 protected: |
60 // This is null until the string is internalized. | 63 // This is null until the string is internalized. |
61 Handle<String> string_; | 64 Handle<String> string_; |
| 65 // Poor-man's virtual dispatch to AstRawString / AstConsString. Takes less |
| 66 // memory. |
| 67 class IsRawStringBits : public BitField<bool, 0, 1> {}; |
| 68 int bit_field_; |
62 }; | 69 }; |
63 | 70 |
64 | 71 |
65 class AstRawString final : public AstString { | 72 class AstRawString final : public AstString { |
66 public: | 73 public: |
67 int length() const override { | 74 int length() const { |
68 if (is_one_byte_) | 75 if (is_one_byte()) return literal_bytes_.length(); |
69 return literal_bytes_.length(); | |
70 return literal_bytes_.length() / 2; | 76 return literal_bytes_.length() / 2; |
71 } | 77 } |
72 | 78 |
73 int byte_length() const { return literal_bytes_.length(); } | 79 int byte_length() const { return literal_bytes_.length(); } |
74 | 80 |
75 void Internalize(Isolate* isolate) override; | 81 void Internalize(Isolate* isolate); |
76 | 82 |
77 bool AsArrayIndex(uint32_t* index) const; | 83 bool AsArrayIndex(uint32_t* index) const; |
78 | 84 |
79 // The string is not null-terminated, use length() to find out the length. | 85 // The string is not null-terminated, use length() to find out the length. |
80 const unsigned char* raw_data() const { | 86 const unsigned char* raw_data() const { |
81 return literal_bytes_.start(); | 87 return literal_bytes_.start(); |
82 } | 88 } |
83 bool is_one_byte() const { return is_one_byte_; } | 89 |
| 90 bool is_one_byte() const { return IsOneByteBits::decode(bit_field_); } |
| 91 |
84 bool IsOneByteEqualTo(const char* data) const; | 92 bool IsOneByteEqualTo(const char* data) const; |
85 uint16_t FirstCharacter() const { | 93 uint16_t FirstCharacter() const { |
86 if (is_one_byte_) | 94 if (is_one_byte()) return literal_bytes_[0]; |
87 return literal_bytes_[0]; | |
88 const uint16_t* c = | 95 const uint16_t* c = |
89 reinterpret_cast<const uint16_t*>(literal_bytes_.start()); | 96 reinterpret_cast<const uint16_t*>(literal_bytes_.start()); |
90 return *c; | 97 return *c; |
91 } | 98 } |
92 | 99 |
93 // For storing AstRawStrings in a hash map. | 100 // For storing AstRawStrings in a hash map. |
94 uint32_t hash() const { | 101 uint32_t hash() const { |
95 return hash_; | 102 return hash_; |
96 } | 103 } |
97 | 104 |
98 private: | 105 private: |
99 friend class AstValueFactory; | 106 friend class AstValueFactory; |
100 friend class AstRawStringInternalizationKey; | 107 friend class AstRawStringInternalizationKey; |
101 | 108 |
102 AstRawString(bool is_one_byte, const Vector<const byte>& literal_bytes, | 109 AstRawString(bool is_one_byte, const Vector<const byte>& literal_bytes, |
103 uint32_t hash) | 110 uint32_t hash) |
104 : is_one_byte_(is_one_byte), literal_bytes_(literal_bytes), hash_(hash) {} | 111 : AstString(true), hash_(hash), literal_bytes_(literal_bytes) { |
| 112 bit_field_ |= IsOneByteBits::encode(is_one_byte); |
| 113 } |
105 | 114 |
106 AstRawString() | 115 AstRawString() : AstString(true), hash_(0) { |
107 : is_one_byte_(true), | 116 bit_field_ |= IsOneByteBits::encode(true); |
108 hash_(0) {} | 117 } |
109 | 118 |
110 bool is_one_byte_; | 119 class IsOneByteBits : public BitField<bool, IsRawStringBits::kNext, 1> {}; |
111 | 120 |
| 121 uint32_t hash_; |
112 // Points to memory owned by Zone. | 122 // Points to memory owned by Zone. |
113 Vector<const byte> literal_bytes_; | 123 Vector<const byte> literal_bytes_; |
114 uint32_t hash_; | |
115 }; | 124 }; |
116 | 125 |
117 | 126 |
118 class AstConsString final : public AstString { | 127 class AstConsString final : public AstString { |
119 public: | 128 public: |
120 AstConsString(const AstString* left, const AstString* right) | 129 AstConsString(const AstString* left, const AstString* right) |
121 : length_(left->length() + right->length()), left_(left), right_(right) {} | 130 : AstString(false), |
| 131 length_(left->length() + right->length()), |
| 132 left_(left), |
| 133 right_(right) {} |
122 | 134 |
123 int length() const override { return length_; } | 135 int length() const { return length_; } |
124 | 136 |
125 void Internalize(Isolate* isolate) override; | 137 void Internalize(Isolate* isolate); |
126 | 138 |
127 private: | 139 private: |
128 const int length_; | 140 const int length_; |
129 const AstString* left_; | 141 const AstString* left_; |
130 const AstString* right_; | 142 const AstString* right_; |
131 }; | 143 }; |
132 | 144 |
133 | 145 |
134 // AstValue is either a string, a number, a string array, a boolean, or a | 146 // AstValue is either a string, a number, a string array, a boolean, or a |
135 // special value (null, undefined, the hole). | 147 // special value (null, undefined, the hole). |
(...skipping 249 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
385 OTHER_CONSTANTS(F) | 397 OTHER_CONSTANTS(F) |
386 #undef F | 398 #undef F |
387 }; | 399 }; |
388 } // namespace internal | 400 } // namespace internal |
389 } // namespace v8 | 401 } // namespace v8 |
390 | 402 |
391 #undef STRING_CONSTANTS | 403 #undef STRING_CONSTANTS |
392 #undef OTHER_CONSTANTS | 404 #undef OTHER_CONSTANTS |
393 | 405 |
394 #endif // V8_AST_AST_VALUE_FACTORY_H_ | 406 #endif // V8_AST_AST_VALUE_FACTORY_H_ |
OLD | NEW |