Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(1179)

Side by Side Diff: src/ast-value-factory.h

Issue 314603004: Parser: Delay internalizing strings and values. (Closed) Base URL: https://v8.googlecode.com/svn/branches/bleeding_edge
Patch Set: More code review (rossberg@). Created 6 years, 6 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « src/ast.cc ('k') | src/ast-value-factory.cc » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(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) : type_(STRING) { string_ = s; }
162
163 explicit AstValue(double n) : type_(NUMBER) { number_ = n; }
164
165 AstValue(Type t, int i) : type_(t) {
166 ASSERT(type_ == SMI);
167 smi_ = i;
168 }
169
170 explicit AstValue(bool b) : type_(BOOLEAN) { bool_ = b; }
171
172 explicit AstValue(ZoneList<const AstString*>* s) : type_(STRING_ARRAY) {
173 strings_ = s;
174 }
175
176 explicit AstValue(Type t) : type_(t) {
177 ASSERT(t == AstValue::NULL_TYPE || t == AstValue::UNDEFINED ||
rossberg 2014/06/12 12:46:41 Nit: you shouldn't need the AstValue:: qualificati
marja 2014/06/12 12:48:06 Done.
178 t == AstValue::THE_HOLE);
179 }
180
181 Type type_;
182
183 // Uninternalized value.
184 union {
185 const AstString* string_;
186 double number_;
187 int smi_;
188 bool bool_;
189 ZoneList<const AstString*>* strings_;
190 };
191
192 // Internalized value (empty before internalized).
193 Handle<Object> value_;
194 };
195
196
197 // For generating string constants.
198 #define STRING_CONSTANTS(F) \
199 F(anonymous_function, "(anonymous function)") \
200 F(arguments, "arguments") \
201 F(done, "done") \
202 F(dot_for, ".for") \
203 F(dot_generator, ".generator") \
204 F(dot_generator_object, ".generator_object") \
205 F(dot_iterator, ".iterator") \
206 F(dot_module, ".module") \
207 F(dot_result, ".result") \
208 F(empty, "") \
209 F(eval, "eval") \
210 F(initialize_const_global, "initializeConstGlobal") \
211 F(initialize_var_global, "initializeVarGlobal") \
212 F(make_reference_error, "MakeReferenceError") \
213 F(make_syntax_error, "MakeSyntaxError") \
214 F(make_type_error, "MakeTypeError") \
215 F(module, "module") \
216 F(native, "native") \
217 F(next, "next") \
218 F(proto, "__proto__") \
219 F(prototype, "prototype") \
220 F(this, "this") \
221 F(use_strict, "use strict") \
222 F(value, "value")
223
224 class AstValueFactory {
225 public:
226 explicit AstValueFactory(Zone* zone)
227 : literal_chars_(0),
228 string_table_keys_(0),
229 string_table_(AstString::Compare),
230 zone_(zone),
231 isolate_(NULL) {
232 #define F(name, str) { \
233 const char* data = str; \
234 name##_string_ = GetOneByteString( \
235 Vector<const uint8_t>(reinterpret_cast<const uint8_t*>(data), \
236 strlen(data))); \
237 }
238 STRING_CONSTANTS(F)
239 #undef F
240 }
241
242 const AstString* GetOneByteString(Vector<const uint8_t> literal);
243 const AstString* GetTwoByteString(Vector<const uint16_t> literal);
244 const AstString* GetString(Handle<String> literal);
245
246 void Internalize(Isolate* isolate);
247
248 #define F(name, str) \
249 const AstString* name##_string() const { return name##_string_; }
250 STRING_CONSTANTS(F)
251 #undef F
252
253 const AstValue* NewString(const AstString* string);
254 const AstValue* NewNumber(double number);
255 const AstValue* NewSmi(int number);
256 const AstValue* NewBoolean(bool b);
257 const AstValue* NewStringList(ZoneList<const AstString*>* strings);
258 const AstValue* NewNull();
259 const AstValue* NewUndefined();
260 const AstValue* NewTheHole();
261
262 private:
263 const AstString* GetString(int hash, bool is_one_byte,
264 Vector<const byte> literal_bytes);
265
266 // All strings are copied here, one after another (no NULLs inbetween).
267 Collector<byte> literal_chars_;
268 // List of all AstStrings we have created; keys of string_table_ are pointers
269 // into AstStrings in string_table_keys_.
270 Collector<AstString> string_table_keys_;
271 HashMap string_table_;
272 // For keeping track of all AstValues we've created (so that they can be
273 // internalized later).
274 List<AstValue*> values_;
275 Zone* zone_;
276 Isolate* isolate_;
277
278 #define F(name, str) \
279 const AstString* name##_string_;
280 STRING_CONSTANTS(F)
281 #undef F
282 };
283
284 } } // namespace v8::internal
285
286 #undef STRING_CONSTANTS
287
288 #endif // V8_AST_VALUE_FACTORY_H_
OLDNEW
« no previous file with comments | « src/ast.cc ('k') | src/ast-value-factory.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698