Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 1 // Copyright 2014 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 #ifndef V8_AST_VALUE_FACTORY_H_ | |
| 29 #define V8_AST_VALUE_FACTORY_H_ | |
| 30 | |
| 31 #include "src/api.h" | |
| 32 #include "src/hashmap.h" | |
| 33 #include "src/utils.h" | |
| 34 | |
| 35 // AstString, AstValue and AstValueFactory are for storing strings and values | |
| 36 // independent of the V8 heap and internalizing them later. During parsing, | |
| 37 // AstStrings and AstValues are created and stored outside the heap, in | |
| 38 // AstValueFactory. After parsing, the strings and values are internalized | |
| 39 // (moved into the V8 heap). | |
| 40 namespace v8 { | |
| 41 namespace internal { | |
| 42 | |
| 43 class AstString { | |
| 44 public: | |
| 45 AstString(bool i, Vector<const byte> lb, int h) | |
| 46 : is_one_byte_(i), | |
| 47 literal_bytes_(lb), | |
| 48 hash_(h) {} | |
| 49 | |
| 50 AstString() | |
| 51 : is_one_byte_(true), | |
| 52 hash_(0) {} | |
| 53 | |
| 54 bool AsArrayIndex(uint32_t* index) const; | |
| 55 | |
| 56 // The string is not null-terminated, use length() to find out the length. | |
| 57 const unsigned char* raw_data() const { return literal_bytes_.start(); } | |
| 58 int length() const { | |
| 59 if (is_one_byte_) | |
| 60 return literal_bytes_.length(); | |
| 61 return literal_bytes_.length() / 2; | |
| 62 } | |
| 63 bool is_one_byte() const { return is_one_byte_; } | |
| 64 bool IsEmpty() const { return literal_bytes_.length() == 0; } | |
| 65 bool IsOneByteEqualTo(const char* data) const; | |
| 66 uint16_t FirstCharacter() const { | |
| 67 if (is_one_byte_) | |
| 68 return literal_bytes_[0]; | |
| 69 const uint16_t* c = | |
| 70 reinterpret_cast<const uint16_t*>(literal_bytes_.start()); | |
| 71 return *c; | |
| 72 } | |
| 73 | |
| 74 // Puts the string into the V8 heap. | |
| 75 void Internalize(Isolate* isolate); | |
| 76 | |
| 77 // This function can be called after internalizing. | |
| 78 V8_INLINE Handle<String> string() const { | |
| 79 ASSERT(!string_.is_null()); | |
| 80 return string_; | |
| 81 } | |
| 82 | |
| 83 // For storing AstStrings in a hash map. | |
| 84 int hash() const { return hash_; } | |
| 85 static bool Compare(void* a, void* b); | |
| 86 | |
| 87 private: | |
| 88 friend class AstValueFactory; | |
| 89 | |
| 90 bool is_one_byte_; | |
| 91 // Weak. Points to memory owned by AstValueFactory. | |
| 92 Vector<const byte> literal_bytes_; | |
| 93 int hash_; | |
| 94 | |
| 95 // This is null until the string is internalized. | |
| 96 Handle<String> string_; | |
| 97 }; | |
| 98 | |
| 99 | |
| 100 // AstValue is either a string, a number, a string array, a boolean, or a | |
| 101 // special value (null, undefined, the hole). | |
| 102 class AstValue : public ZoneObject { | |
| 103 public: | |
| 104 bool IsString() const { | |
| 105 return type_ == STRING; | |
| 106 } | |
| 107 | |
| 108 bool IsNumber() const { | |
| 109 return type_ == NUMBER || type_ == SMI; | |
| 110 } | |
| 111 | |
| 112 const AstString* AsString() const { | |
| 113 if (type_ == STRING) | |
| 114 return string_; | |
| 115 UNREACHABLE(); | |
| 116 return 0; | |
| 117 } | |
| 118 | |
| 119 double AsNumber() const { | |
| 120 if (type_ == NUMBER) | |
| 121 return number_; | |
| 122 if (type_ == SMI) | |
| 123 return smi_; | |
| 124 UNREACHABLE(); | |
| 125 return 0; | |
| 126 } | |
| 127 | |
| 128 bool EqualsString(const AstString* string) const { | |
| 129 return type_ == STRING && string_ == string; | |
| 130 } | |
| 131 | |
| 132 bool IsPropertyName() const; | |
| 133 | |
| 134 bool BooleanValue() const; | |
| 135 | |
| 136 void Internalize(Isolate* isolate); | |
| 137 | |
| 138 // Can be called after Internalize has been called. | |
| 139 V8_INLINE Handle<Object> value() const { | |
| 140 if (type_ == STRING) { | |
| 141 return string_->string(); | |
| 142 } | |
| 143 ASSERT(!value_.is_null()); | |
| 144 return value_; | |
| 145 } | |
| 146 | |
| 147 private: | |
| 148 friend class AstValueFactory; | |
| 149 | |
| 150 enum Type { | |
| 151 STRING, | |
| 152 NUMBER, | |
| 153 SMI, | |
| 154 BOOLEAN, | |
| 155 STRING_ARRAY, | |
| 156 NULL_TYPE, | |
| 157 UNDEFINED, | |
| 158 THE_HOLE | |
| 159 }; | |
| 160 | |
| 161 explicit AstValue(const AstString* s) : | |
| 162 type_(STRING) { | |
| 163 string_ = s; | |
| 164 } | |
| 165 | |
| 166 explicit AstValue(double n) : | |
| 167 type_(NUMBER) { | |
| 168 number_ = n; | |
| 169 } | |
| 170 | |
| 171 AstValue(Type t, int i) : | |
| 172 type_(t) { | |
| 173 ASSERT(type_ == SMI); | |
| 174 smi_ = i; | |
| 175 } | |
| 176 | |
| 177 explicit AstValue(bool b) : | |
| 178 type_(BOOLEAN) { | |
| 179 bool_ = b; | |
| 180 } | |
| 181 | |
| 182 explicit AstValue(ZoneList<const AstString*>* s) : | |
| 183 type_(STRING_ARRAY) { | |
| 184 strings_ = s; | |
| 185 } | |
| 186 | |
| 187 explicit AstValue(Type t) : | |
| 188 type_(t) {} | |
|
rossberg
2014/06/12 12:20:28
Perhaps ASSERT that t in {NULL, UNDEFINED, HOLE}.
marja
2014/06/12 12:35:03
Done.
| |
| 189 | |
| 190 Type type_; | |
| 191 | |
| 192 // Uninternalized value. | |
| 193 union { | |
| 194 const AstString* string_; | |
| 195 double number_; | |
| 196 int smi_; | |
| 197 bool bool_; | |
| 198 ZoneList<const AstString*>* strings_; | |
| 199 }; | |
| 200 | |
| 201 // Internalized value (empty before internalized). | |
| 202 Handle<Object> value_; | |
| 203 }; | |
| 204 | |
| 205 | |
| 206 // For generating string constants. | |
| 207 #define STRING_CONSTANTS(F) \ | |
| 208 F(anonymous_function, "(anonymous function)") \ | |
| 209 F(arguments, "arguments") \ | |
| 210 F(done, "done") \ | |
| 211 F(dot_for, ".for") \ | |
| 212 F(dot_generator, ".generator") \ | |
| 213 F(dot_generator_object, ".generator_object") \ | |
| 214 F(dot_iterator, ".iterator") \ | |
| 215 F(dot_module, ".module") \ | |
| 216 F(dot_result, ".result") \ | |
| 217 F(empty, "") \ | |
| 218 F(eval, "eval") \ | |
| 219 F(initialize_const_global, "initializeConstGlobal") \ | |
| 220 F(initialize_var_global, "initializeVarGlobal") \ | |
| 221 F(make_reference_error, "MakeReferenceError") \ | |
| 222 F(make_syntax_error, "MakeSyntaxError") \ | |
| 223 F(make_type_error, "MakeTypeError") \ | |
| 224 F(module, "module") \ | |
| 225 F(native, "native") \ | |
| 226 F(next, "next") \ | |
| 227 F(proto, "__proto__") \ | |
| 228 F(prototype, "prototype") \ | |
| 229 F(this, "this") \ | |
| 230 F(use_strict, "use strict") \ | |
| 231 F(value, "value") | |
| 232 | |
| 233 class AstValueFactory { | |
| 234 public: | |
| 235 explicit AstValueFactory(Zone* zone) | |
| 236 : literal_chars_(0), | |
| 237 string_table_keys_(0), | |
| 238 string_table_(AstString::Compare), | |
| 239 zone_(zone), | |
| 240 isolate_(NULL) { | |
| 241 #define F(name, str) { \ | |
| 242 const char* data = str; \ | |
| 243 name##_string_ = GetOneByteString( \ | |
| 244 Vector<const uint8_t>(reinterpret_cast<const uint8_t*>(data), \ | |
| 245 strlen(data))); \ | |
| 246 } | |
| 247 STRING_CONSTANTS(F) | |
| 248 #undef F | |
| 249 } | |
| 250 | |
| 251 const AstString* GetOneByteString(Vector<const uint8_t> literal); | |
| 252 const AstString* GetTwoByteString(Vector<const uint16_t> literal); | |
| 253 const AstString* GetString(Handle<String> literal); | |
| 254 | |
| 255 void Internalize(Isolate* isolate); | |
| 256 | |
| 257 #define F(name, str) \ | |
| 258 const AstString* name##_string() const { return name##_string_; } | |
| 259 STRING_CONSTANTS(F) | |
| 260 #undef F | |
| 261 | |
| 262 const AstValue* NewString(const AstString* string); | |
| 263 const AstValue* NewNumber(double number); | |
| 264 const AstValue* NewSmi(int number); | |
| 265 const AstValue* NewBoolean(bool b); | |
| 266 const AstValue* NewStringList(ZoneList<const AstString*>* strings); | |
| 267 const AstValue* NewNull(); | |
| 268 const AstValue* NewUndefined(); | |
| 269 const AstValue* NewTheHole(); | |
| 270 | |
| 271 private: | |
| 272 const AstString* GetString(int hash, bool is_one_byte, | |
| 273 Vector<const byte> literal_bytes); | |
| 274 | |
| 275 // All strings are copied here, one after another (no NULLs inbetween). | |
| 276 Collector<byte> literal_chars_; | |
| 277 // List of all AstStrings we have created; keys of string_table_ are pointers | |
| 278 // into AstStrings in string_table_keys_. | |
| 279 Collector<AstString> string_table_keys_; | |
| 280 HashMap string_table_; | |
| 281 // For keeping track of all AstValues we've created (so that they can be | |
| 282 // internalized later). | |
| 283 List<AstValue*> values_; | |
| 284 Zone* zone_; | |
| 285 Isolate* isolate_; | |
| 286 | |
| 287 #define F(name, str) \ | |
| 288 const AstString* name##_string_; | |
| 289 STRING_CONSTANTS(F) | |
| 290 #undef F | |
| 291 }; | |
| 292 | |
| 293 } } // namespace v8::internal | |
| 294 | |
| 295 #undef STRING_CONSTANTS | |
| 296 | |
| 297 #endif // V8_AST_VALUE_FACTORY_H_ | |
| OLD | NEW |