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

Side by Side Diff: src/lexer/experimental-scanner.cc

Issue 140913009: Experimental lexer: fix internalization and allocation of literals. (Closed) Base URL: https://v8.googlecode.com/svn/branches/experimental/parser
Patch Set: Add comments Created 6 years, 11 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/lexer/experimental-scanner.h ('k') | src/objects.cc » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright 2013 the V8 project authors. All rights reserved. 1 // Copyright 2013 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 35 matching lines...) Expand 10 before | Expand all | Expand 10 after
46 46
47 47
48 template<> 48 template<>
49 const int8_t* ExperimentalScanner<int8_t>::GetNewBufferBasedOnHandle() const { 49 const int8_t* ExperimentalScanner<int8_t>::GetNewBufferBasedOnHandle() const {
50 String::FlatContent content = source_handle_->GetFlatContent(); 50 String::FlatContent content = source_handle_->GetFlatContent();
51 return reinterpret_cast<const int8_t*>(content.ToOneByteVector().start()); 51 return reinterpret_cast<const int8_t*>(content.ToOneByteVector().start());
52 } 52 }
53 53
54 54
55 template<> 55 template<>
56 bool ExperimentalScanner<uint8_t>::IsSubstringOfSource(const TokenDesc& token) {
57 return !token.has_escapes;
58 }
59
60
61 template<>
62 bool ExperimentalScanner<uint16_t>::IsSubstringOfSource(
63 const TokenDesc& token) {
64 if (token.has_escapes) return false;
65 const uint16_t* start = buffer_ + token.beg_pos;
66 const uint16_t* end = buffer_ + token.end_pos;
67 for (const uint16_t* cursor = start; cursor != end; ++cursor) {
68 if (*cursor >= unibrow::Latin1::kMaxChar) return true;
69 }
70 return false;
71 }
72
73
74 template<>
75 bool ExperimentalScanner<int8_t>::IsSubstringOfSource(const TokenDesc& token) {
76 // FIXME: implement.
77 UNREACHABLE();
78 return false;
79 }
80
81
82 template<>
56 bool ExperimentalScanner<uint8_t>::FillLiteral( 83 bool ExperimentalScanner<uint8_t>::FillLiteral(
57 const TokenDesc& token, LiteralDesc* literal) { 84 const TokenDesc& token, LiteralDesc* literal) {
58 literal->beg_pos = token.beg_pos; 85 literal->beg_pos = token.beg_pos;
59 const uint8_t* start = buffer_ + token.beg_pos; 86 const uint8_t* start = buffer_ + token.beg_pos;
60 const uint8_t* end = buffer_ + token.end_pos; 87 const uint8_t* end = buffer_ + token.end_pos;
61 if (token.token == Token::STRING) { 88 if (token.token == Token::STRING) {
62 ++start; 89 ++start;
63 --end; 90 --end;
64 } 91 }
65 if (!token.has_escapes) { 92 if (IsSubstringOfSource(token)) {
66 literal->is_ascii = true; 93 literal->is_ascii = true;
94 literal->is_in_buffer = false;
95 literal->offset = start - buffer_;
67 literal->length = end - start; 96 literal->length = end - start;
68 literal->ascii_string = Vector<const char>( 97 literal->ascii_string = Vector<const char>(
69 reinterpret_cast<const char*>(start), literal->length); 98 reinterpret_cast<const char*>(start), literal->length);
70 return true; 99 return true;
71 } 100 }
72 literal->buffer.Reset(); 101 return CopyToLiteralBuffer(start, end, token, literal);
73 for (const uint8_t* cursor = start; cursor != end;) {
74 if (*cursor != '\\') {
75 literal->buffer.AddChar(*cursor++);
76 } else if (token.token == Token::IDENTIFIER) {
77 uc32 c;
78 cursor = ScanIdentifierUnicodeEscape(cursor, end, &c);
79 ASSERT(cursor != NULL);
80 if (cursor == NULL) return false;
81 literal->buffer.AddChar(c);
82 } else {
83 cursor = ScanEscape(cursor, end, &literal->buffer);
84 ASSERT(cursor != NULL);
85 if (cursor == NULL) return false;
86 }
87 }
88 literal->is_ascii = literal->buffer.is_ascii();
89 literal->length = literal->buffer.length();
90 if (literal->is_ascii) {
91 literal->ascii_string = literal->buffer.ascii_literal();
92 } else {
93 literal->utf16_string = literal->buffer.utf16_literal();
94 }
95 return true;
96 } 102 }
97 103
98 104
99 template<> 105 template<>
100 bool ExperimentalScanner<uint16_t>::FillLiteral( 106 bool ExperimentalScanner<uint16_t>::FillLiteral(
101 const TokenDesc& token, LiteralDesc* literal) { 107 const TokenDesc& token, LiteralDesc* literal) {
102 literal->beg_pos = token.beg_pos; 108 literal->beg_pos = token.beg_pos;
103 const uint16_t* start = buffer_ + token.beg_pos; 109 const uint16_t* start = buffer_ + token.beg_pos;
104 const uint16_t* end = buffer_ + token.end_pos; 110 const uint16_t* end = buffer_ + token.end_pos;
105 if (token.token == Token::STRING) { 111 if (token.token == Token::STRING) {
106 ++start; 112 ++start;
107 --end; 113 --end;
108 } 114 }
109 if (!token.has_escapes) { 115 if (IsSubstringOfSource(token)) {
110 // UTF-16 can also contain only one byte chars. Note that is_ascii here 116 literal->is_ascii = false;
111 // means is_onebyte. 117 literal->is_in_buffer = false;
112 literal->is_ascii = true; 118 literal->offset = start - buffer_;
113 literal->buffer.Reset();
114 for (const uint16_t* cursor = start; cursor != end; ++cursor) {
115 if (*cursor >= unibrow::Latin1::kMaxChar) {
116 literal->is_ascii = false;
117 break;
118 }
119 literal->buffer.AddChar(*cursor);
120 }
121 literal->length = end - start; 119 literal->length = end - start;
122 if (literal->is_ascii) { 120 literal->utf16_string = Vector<const uint16_t>(start, literal->length);
123 literal->ascii_string = literal->buffer.ascii_literal();
124 } else {
125 literal->buffer.Reset();
126 literal->utf16_string = Vector<const uint16_t>(start, literal->length);
127 }
128 return true; 121 return true;
129 } 122 }
123 return CopyToLiteralBuffer(start, end, token, literal);
124 }
125
126
127 template<>
128 bool ExperimentalScanner<int8_t>::FillLiteral(
129 const TokenDesc& token, LiteralDesc* literal) {
130 // FIXME: implement.
131 UNREACHABLE();
132 return false;
133 }
134
135
136 template<class Char>
137 bool ExperimentalScanner<Char>::CopyToLiteralBuffer(const Char* start,
138 const Char* end,
139 const TokenDesc& token,
140 LiteralDesc* literal) {
130 literal->buffer.Reset(); 141 literal->buffer.Reset();
131 for (const uint16_t* cursor = start; cursor != end;) { 142 if (token.has_escapes) {
132 if (*cursor != '\\') { 143 for (const Char* cursor = start; cursor != end;) {
133 literal->buffer.AddChar(*cursor++); 144 if (*cursor != '\\') {
134 } else if (token.token == Token::IDENTIFIER) { 145 literal->buffer.AddChar(*cursor++);
135 uc32 c; 146 } else if (token.token == Token::IDENTIFIER) {
136 cursor = ScanIdentifierUnicodeEscape(cursor, end, &c); 147 uc32 c;
137 ASSERT(cursor != NULL); 148 cursor = ScanIdentifierUnicodeEscape(cursor, end, &c);
138 if (cursor == NULL) return false; 149 ASSERT(cursor != NULL);
139 literal->buffer.AddChar(c); 150 if (cursor == NULL) return false;
140 } else { 151 literal->buffer.AddChar(c);
141 cursor = ScanEscape(cursor, end, &literal->buffer); 152 } else {
142 ASSERT(cursor != NULL); 153 cursor = ScanEscape(cursor, end, &literal->buffer);
143 if (cursor == NULL) return false; 154 ASSERT(cursor != NULL);
155 if (cursor == NULL) return false;
156 }
157 }
158 } else {
159 for (const Char* cursor = start; cursor != end;) {
160 literal->buffer.AddChar(*cursor++);
144 } 161 }
145 } 162 }
146 literal->is_ascii = literal->buffer.is_ascii(); 163 literal->is_ascii = literal->buffer.is_ascii();
164 literal->is_in_buffer = true;
147 literal->length = literal->buffer.length(); 165 literal->length = literal->buffer.length();
148 if (literal->is_ascii) { 166 if (literal->is_ascii) {
149 literal->ascii_string = literal->buffer.ascii_literal(); 167 literal->ascii_string = literal->buffer.ascii_literal();
150 } else { 168 } else {
151 literal->utf16_string = literal->buffer.utf16_literal(); 169 literal->utf16_string = literal->buffer.utf16_literal();
152 } 170 }
153 return true; 171 return true;
154 } 172 }
155 173
156 template<> 174
157 bool ExperimentalScanner<int8_t>::FillLiteral( 175 template<class Char>
158 const TokenDesc& token, LiteralDesc* literal) { 176 Handle<String> ExperimentalScanner<Char>::InternalizeLiteral(
159 // FIXME: implement. 177 LiteralDesc* literal) {
160 return false; 178 Factory* factory = isolate_->factory();
179 if (literal->is_in_buffer) {
180 return literal->is_ascii
181 ? factory->InternalizeOneByteString(
182 Vector<const uint8_t>::cast(literal->ascii_string))
183 : factory->InternalizeTwoByteString(literal->utf16_string);
184 }
185 if (sizeof(Char) == 1) {
186 SubStringKey<uint8_t> key(
187 source_handle_, literal->offset, literal->length);
188 return factory->InternalizeStringWithKey(&key);
189 } else {
190 SubStringKey<uint16_t> key(
191 source_handle_, literal->offset, literal->length);
192 return factory->InternalizeStringWithKey(&key);
193 }
161 } 194 }
162 195
163 196
197 template<>
198 Handle<String> ExperimentalScanner<uint8_t>::AllocateLiteral(
199 LiteralDesc* literal, PretenureFlag pretenured) {
200 Factory* factory = isolate_->factory();
201 if (literal->is_in_buffer) {
202 return literal->is_ascii
203 ? factory->NewStringFromAscii(literal->ascii_string, pretenured)
204 : factory->NewStringFromTwoByte(literal->utf16_string, pretenured);
205 }
206 int from = literal->offset;
207 int length = literal->length;
208 // Save the offset and the length before allocating the string as it may
209 // cause a GC, invalidate the literal, and move the source.
210 Handle<String> result = factory->NewRawOneByteString(length, pretenured);
211 uint8_t* chars = SeqOneByteString::cast(*result)->GetChars();
212 String::WriteToFlat(*source_handle_, chars, from, from + length);
213 return result;
164 } 214 }
215
216
217 template<>
218 Handle<String> ExperimentalScanner<uint16_t>::AllocateLiteral(
219 LiteralDesc* literal, PretenureFlag pretenured) {
220 Factory* factory = isolate_->factory();
221 if (literal->is_in_buffer) {
222 return literal->is_ascii
223 ? factory->NewStringFromAscii(literal->ascii_string, pretenured)
224 : factory->NewStringFromTwoByte(literal->utf16_string, pretenured);
225 }
226 // Save the offset and the length before allocating the string as it may
227 // cause a GC, invalidate the literal, and move the source.
228 int from = literal->offset;
229 int length = literal->length;
230 Handle<String> result = factory->NewRawTwoByteString(length, pretenured);
231 uint16_t* chars = SeqTwoByteString::cast(*result)->GetChars();
232 String::WriteToFlat(*source_handle_, chars, from, from + length);
233 return result;
165 } 234 }
235
236
237 template<>
238 Handle<String> ExperimentalScanner<int8_t>::AllocateLiteral(
239 LiteralDesc* literal, PretenureFlag pretenured) {
240 // FIXME: implement
241 UNREACHABLE();
242 return Handle<String>();
243 }
244
245 template class ExperimentalScanner<uint8_t>;
246 template class ExperimentalScanner<uint16_t>;
247 template class ExperimentalScanner<int8_t>;
248
249 } } // v8::internal
OLDNEW
« no previous file with comments | « src/lexer/experimental-scanner.h ('k') | src/objects.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698