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) : is_raw_(is_raw) {} |
46 ~AstString() {} | |
46 | 47 |
47 virtual int length() const = 0; | 48 int length() const; |
48 bool IsEmpty() const { return length() == 0; } | 49 bool IsEmpty() const { return length() == 0; } |
49 | 50 |
50 // Puts the string into the V8 heap. | 51 // Puts the string into the V8 heap. |
51 virtual void Internalize(Isolate* isolate) = 0; | 52 void Internalize(Isolate* isolate); |
52 | 53 |
53 // This function can be called after internalizing. | 54 // This function can be called after internalizing. |
54 V8_INLINE Handle<String> string() const { | 55 V8_INLINE Handle<String> string() const { |
55 DCHECK(!string_.is_null()); | 56 DCHECK(!string_.is_null()); |
56 return string_; | 57 return string_; |
57 } | 58 } |
58 | 59 |
59 protected: | 60 protected: |
61 // Poor-man's virtual dispatch to AstRawString / AstConsString. But faster! | |
62 bool is_raw_; | |
Toon Verwaest
2016/08/08 09:43:24
You probably want to pack this better with fields
| |
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_; |
62 }; | 65 }; |
63 | 66 |
64 | 67 |
65 class AstRawString final : public AstString { | 68 class AstRawString final : public AstString { |
66 public: | 69 public: |
67 int length() const override { | 70 int length() const { |
68 if (is_one_byte_) | 71 if (is_one_byte_) |
69 return literal_bytes_.length(); | 72 return literal_bytes_.length(); |
70 return literal_bytes_.length() / 2; | 73 return literal_bytes_.length() / 2; |
71 } | 74 } |
72 | 75 |
73 int byte_length() const { return literal_bytes_.length(); } | 76 int byte_length() const { return literal_bytes_.length(); } |
74 | 77 |
75 void Internalize(Isolate* isolate) override; | 78 void Internalize(Isolate* isolate); |
76 | 79 |
77 bool AsArrayIndex(uint32_t* index) const; | 80 bool AsArrayIndex(uint32_t* index) const; |
78 | 81 |
79 // The string is not null-terminated, use length() to find out the length. | 82 // The string is not null-terminated, use length() to find out the length. |
80 const unsigned char* raw_data() const { | 83 const unsigned char* raw_data() const { |
81 return literal_bytes_.start(); | 84 return literal_bytes_.start(); |
82 } | 85 } |
83 bool is_one_byte() const { return is_one_byte_; } | 86 bool is_one_byte() const { return is_one_byte_; } |
84 bool IsOneByteEqualTo(const char* data) const; | 87 bool IsOneByteEqualTo(const char* data) const; |
85 uint16_t FirstCharacter() const { | 88 uint16_t FirstCharacter() const { |
86 if (is_one_byte_) | 89 if (is_one_byte_) |
87 return literal_bytes_[0]; | 90 return literal_bytes_[0]; |
88 const uint16_t* c = | 91 const uint16_t* c = |
89 reinterpret_cast<const uint16_t*>(literal_bytes_.start()); | 92 reinterpret_cast<const uint16_t*>(literal_bytes_.start()); |
90 return *c; | 93 return *c; |
91 } | 94 } |
92 | 95 |
93 // For storing AstRawStrings in a hash map. | 96 // For storing AstRawStrings in a hash map. |
94 uint32_t hash() const { | 97 uint32_t hash() const { |
95 return hash_; | 98 return hash_; |
96 } | 99 } |
97 | 100 |
98 private: | 101 private: |
99 friend class AstValueFactory; | 102 friend class AstValueFactory; |
100 friend class AstRawStringInternalizationKey; | 103 friend class AstRawStringInternalizationKey; |
101 | 104 |
102 AstRawString(bool is_one_byte, const Vector<const byte>& literal_bytes, | 105 AstRawString(bool is_one_byte, const Vector<const byte>& literal_bytes, |
103 uint32_t hash) | 106 uint32_t hash) |
104 : is_one_byte_(is_one_byte), literal_bytes_(literal_bytes), hash_(hash) {} | 107 : AstString(true), |
108 is_one_byte_(is_one_byte), | |
109 literal_bytes_(literal_bytes), | |
110 hash_(hash) {} | |
105 | 111 |
106 AstRawString() | 112 AstRawString() : AstString(true), is_one_byte_(true), hash_(0) {} |
107 : is_one_byte_(true), | |
108 hash_(0) {} | |
109 | 113 |
110 bool is_one_byte_; | 114 bool is_one_byte_; |
111 | 115 |
112 // Points to memory owned by Zone. | 116 // Points to memory owned by Zone. |
113 Vector<const byte> literal_bytes_; | 117 Vector<const byte> literal_bytes_; |
114 uint32_t hash_; | 118 uint32_t hash_; |
115 }; | 119 }; |
116 | 120 |
117 | 121 |
118 class AstConsString final : public AstString { | 122 class AstConsString final : public AstString { |
119 public: | 123 public: |
120 AstConsString(const AstString* left, const AstString* right) | 124 AstConsString(const AstString* left, const AstString* right) |
121 : length_(left->length() + right->length()), left_(left), right_(right) {} | 125 : AstString(false), |
126 length_(left->length() + right->length()), | |
127 left_(left), | |
128 right_(right) {} | |
122 | 129 |
123 int length() const override { return length_; } | 130 int length() const { return length_; } |
124 | 131 |
125 void Internalize(Isolate* isolate) override; | 132 void Internalize(Isolate* isolate); |
126 | 133 |
127 private: | 134 private: |
128 const int length_; | 135 const int length_; |
129 const AstString* left_; | 136 const AstString* left_; |
130 const AstString* right_; | 137 const AstString* right_; |
131 }; | 138 }; |
132 | 139 |
133 | 140 |
134 // AstValue is either a string, a number, a string array, a boolean, or a | 141 // AstValue is either a string, a number, a string array, a boolean, or a |
135 // special value (null, undefined, the hole). | 142 // special value (null, undefined, the hole). |
(...skipping 249 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
385 OTHER_CONSTANTS(F) | 392 OTHER_CONSTANTS(F) |
386 #undef F | 393 #undef F |
387 }; | 394 }; |
388 } // namespace internal | 395 } // namespace internal |
389 } // namespace v8 | 396 } // namespace v8 |
390 | 397 |
391 #undef STRING_CONSTANTS | 398 #undef STRING_CONSTANTS |
392 #undef OTHER_CONSTANTS | 399 #undef OTHER_CONSTANTS |
393 | 400 |
394 #endif // V8_AST_AST_VALUE_FACTORY_H_ | 401 #endif // V8_AST_AST_VALUE_FACTORY_H_ |
OLD | NEW |