Ben Murdoch | 8b112d2 | 2011-06-08 16:22:53 +0100 | [diff] [blame] | 1 | // Copyright 2011 the V8 project authors. All rights reserved. |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 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 | #include "v8.h" |
| 29 | |
| 30 | #include "api.h" |
Ben Murdoch | 257744e | 2011-11-30 15:57:28 +0000 | [diff] [blame] | 31 | #include "ast-inl.h" |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 32 | #include "bootstrapper.h" |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 33 | #include "codegen.h" |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 34 | #include "compiler.h" |
Kristian Monsen | 80d68ea | 2010-09-08 11:05:35 +0100 | [diff] [blame] | 35 | #include "func-name-inferrer.h" |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 36 | #include "messages.h" |
Kristian Monsen | 25f6136 | 2010-05-21 11:50:48 +0100 | [diff] [blame] | 37 | #include "parser.h" |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 38 | #include "platform.h" |
Teng-Hui Zhu | 3e5fa29 | 2010-11-09 16:16:48 -0800 | [diff] [blame] | 39 | #include "preparser.h" |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 40 | #include "runtime.h" |
Ben Murdoch | 3bec4d2 | 2010-07-22 14:51:16 +0100 | [diff] [blame] | 41 | #include "scopeinfo.h" |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 42 | #include "string-stream.h" |
| 43 | |
| 44 | namespace v8 { |
| 45 | namespace internal { |
| 46 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 47 | // PositionStack is used for on-stack allocation of token positions for |
| 48 | // new expressions. Please look at ParseNewExpression. |
| 49 | |
| 50 | class PositionStack { |
| 51 | public: |
| 52 | explicit PositionStack(bool* ok) : top_(NULL), ok_(ok) {} |
| 53 | ~PositionStack() { ASSERT(!*ok_ || is_empty()); } |
| 54 | |
| 55 | class Element { |
| 56 | public: |
| 57 | Element(PositionStack* stack, int value) { |
| 58 | previous_ = stack->top(); |
| 59 | value_ = value; |
| 60 | stack->set_top(this); |
| 61 | } |
| 62 | |
| 63 | private: |
| 64 | Element* previous() { return previous_; } |
| 65 | int value() { return value_; } |
| 66 | friend class PositionStack; |
| 67 | Element* previous_; |
| 68 | int value_; |
| 69 | }; |
| 70 | |
| 71 | bool is_empty() { return top_ == NULL; } |
| 72 | int pop() { |
| 73 | ASSERT(!is_empty()); |
| 74 | int result = top_->value(); |
| 75 | top_ = top_->previous(); |
| 76 | return result; |
| 77 | } |
| 78 | |
| 79 | private: |
| 80 | Element* top() { return top_; } |
| 81 | void set_top(Element* value) { top_ = value; } |
| 82 | Element* top_; |
| 83 | bool* ok_; |
| 84 | }; |
| 85 | |
| 86 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 87 | RegExpBuilder::RegExpBuilder() |
Ben Murdoch | 8b112d2 | 2011-06-08 16:22:53 +0100 | [diff] [blame] | 88 | : zone_(Isolate::Current()->zone()), |
| 89 | pending_empty_(false), |
| 90 | characters_(NULL), |
| 91 | terms_(), |
| 92 | alternatives_() |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 93 | #ifdef DEBUG |
Ben Murdoch | 8b112d2 | 2011-06-08 16:22:53 +0100 | [diff] [blame] | 94 | , last_added_(ADD_NONE) |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 95 | #endif |
| 96 | {} |
| 97 | |
| 98 | |
| 99 | void RegExpBuilder::FlushCharacters() { |
| 100 | pending_empty_ = false; |
| 101 | if (characters_ != NULL) { |
Ben Murdoch | 8b112d2 | 2011-06-08 16:22:53 +0100 | [diff] [blame] | 102 | RegExpTree* atom = new(zone()) RegExpAtom(characters_->ToConstVector()); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 103 | characters_ = NULL; |
| 104 | text_.Add(atom); |
| 105 | LAST(ADD_ATOM); |
| 106 | } |
| 107 | } |
| 108 | |
| 109 | |
| 110 | void RegExpBuilder::FlushText() { |
| 111 | FlushCharacters(); |
| 112 | int num_text = text_.length(); |
| 113 | if (num_text == 0) { |
| 114 | return; |
| 115 | } else if (num_text == 1) { |
| 116 | terms_.Add(text_.last()); |
| 117 | } else { |
Ben Murdoch | 8b112d2 | 2011-06-08 16:22:53 +0100 | [diff] [blame] | 118 | RegExpText* text = new(zone()) RegExpText(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 119 | for (int i = 0; i < num_text; i++) |
| 120 | text_.Get(i)->AppendToText(text); |
| 121 | terms_.Add(text); |
| 122 | } |
| 123 | text_.Clear(); |
| 124 | } |
| 125 | |
| 126 | |
| 127 | void RegExpBuilder::AddCharacter(uc16 c) { |
| 128 | pending_empty_ = false; |
| 129 | if (characters_ == NULL) { |
Ben Murdoch | 257744e | 2011-11-30 15:57:28 +0000 | [diff] [blame] | 130 | characters_ = new(zone()) ZoneList<uc16>(4); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 131 | } |
| 132 | characters_->Add(c); |
| 133 | LAST(ADD_CHAR); |
| 134 | } |
| 135 | |
| 136 | |
| 137 | void RegExpBuilder::AddEmpty() { |
| 138 | pending_empty_ = true; |
| 139 | } |
| 140 | |
| 141 | |
| 142 | void RegExpBuilder::AddAtom(RegExpTree* term) { |
| 143 | if (term->IsEmpty()) { |
| 144 | AddEmpty(); |
| 145 | return; |
| 146 | } |
| 147 | if (term->IsTextElement()) { |
| 148 | FlushCharacters(); |
| 149 | text_.Add(term); |
| 150 | } else { |
| 151 | FlushText(); |
| 152 | terms_.Add(term); |
| 153 | } |
| 154 | LAST(ADD_ATOM); |
| 155 | } |
| 156 | |
| 157 | |
| 158 | void RegExpBuilder::AddAssertion(RegExpTree* assert) { |
| 159 | FlushText(); |
| 160 | terms_.Add(assert); |
| 161 | LAST(ADD_ASSERT); |
| 162 | } |
| 163 | |
| 164 | |
| 165 | void RegExpBuilder::NewAlternative() { |
| 166 | FlushTerms(); |
| 167 | } |
| 168 | |
| 169 | |
| 170 | void RegExpBuilder::FlushTerms() { |
| 171 | FlushText(); |
| 172 | int num_terms = terms_.length(); |
| 173 | RegExpTree* alternative; |
| 174 | if (num_terms == 0) { |
| 175 | alternative = RegExpEmpty::GetInstance(); |
| 176 | } else if (num_terms == 1) { |
| 177 | alternative = terms_.last(); |
| 178 | } else { |
Ben Murdoch | 8b112d2 | 2011-06-08 16:22:53 +0100 | [diff] [blame] | 179 | alternative = new(zone()) RegExpAlternative(terms_.GetList()); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 180 | } |
| 181 | alternatives_.Add(alternative); |
| 182 | terms_.Clear(); |
| 183 | LAST(ADD_NONE); |
| 184 | } |
| 185 | |
| 186 | |
| 187 | RegExpTree* RegExpBuilder::ToRegExp() { |
| 188 | FlushTerms(); |
| 189 | int num_alternatives = alternatives_.length(); |
| 190 | if (num_alternatives == 0) { |
| 191 | return RegExpEmpty::GetInstance(); |
| 192 | } |
| 193 | if (num_alternatives == 1) { |
| 194 | return alternatives_.last(); |
| 195 | } |
Ben Murdoch | 8b112d2 | 2011-06-08 16:22:53 +0100 | [diff] [blame] | 196 | return new(zone()) RegExpDisjunction(alternatives_.GetList()); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 197 | } |
| 198 | |
| 199 | |
Leon Clarke | e46be81 | 2010-01-19 14:06:41 +0000 | [diff] [blame] | 200 | void RegExpBuilder::AddQuantifierToAtom(int min, |
| 201 | int max, |
| 202 | RegExpQuantifier::Type type) { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 203 | if (pending_empty_) { |
| 204 | pending_empty_ = false; |
| 205 | return; |
| 206 | } |
| 207 | RegExpTree* atom; |
| 208 | if (characters_ != NULL) { |
| 209 | ASSERT(last_added_ == ADD_CHAR); |
| 210 | // Last atom was character. |
| 211 | Vector<const uc16> char_vector = characters_->ToConstVector(); |
| 212 | int num_chars = char_vector.length(); |
| 213 | if (num_chars > 1) { |
| 214 | Vector<const uc16> prefix = char_vector.SubVector(0, num_chars - 1); |
Ben Murdoch | 8b112d2 | 2011-06-08 16:22:53 +0100 | [diff] [blame] | 215 | text_.Add(new(zone()) RegExpAtom(prefix)); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 216 | char_vector = char_vector.SubVector(num_chars - 1, num_chars); |
| 217 | } |
| 218 | characters_ = NULL; |
Ben Murdoch | 8b112d2 | 2011-06-08 16:22:53 +0100 | [diff] [blame] | 219 | atom = new(zone()) RegExpAtom(char_vector); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 220 | FlushText(); |
| 221 | } else if (text_.length() > 0) { |
| 222 | ASSERT(last_added_ == ADD_ATOM); |
| 223 | atom = text_.RemoveLast(); |
| 224 | FlushText(); |
| 225 | } else if (terms_.length() > 0) { |
| 226 | ASSERT(last_added_ == ADD_ATOM); |
| 227 | atom = terms_.RemoveLast(); |
| 228 | if (atom->max_match() == 0) { |
| 229 | // Guaranteed to only match an empty string. |
| 230 | LAST(ADD_TERM); |
| 231 | if (min == 0) { |
| 232 | return; |
| 233 | } |
| 234 | terms_.Add(atom); |
| 235 | return; |
| 236 | } |
| 237 | } else { |
| 238 | // Only call immediately after adding an atom or character! |
| 239 | UNREACHABLE(); |
| 240 | return; |
| 241 | } |
Ben Murdoch | 8b112d2 | 2011-06-08 16:22:53 +0100 | [diff] [blame] | 242 | terms_.Add(new(zone()) RegExpQuantifier(min, max, type, atom)); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 243 | LAST(ADD_TERM); |
| 244 | } |
| 245 | |
| 246 | |
Steve Block | 9fac840 | 2011-05-12 15:51:54 +0100 | [diff] [blame] | 247 | Handle<String> Parser::LookupSymbol(int symbol_id) { |
Teng-Hui Zhu | 3e5fa29 | 2010-11-09 16:16:48 -0800 | [diff] [blame] | 248 | // Length of symbol cache is the number of identified symbols. |
| 249 | // If we are larger than that, or negative, it's not a cached symbol. |
| 250 | // This might also happen if there is no preparser symbol data, even |
| 251 | // if there is some preparser data. |
| 252 | if (static_cast<unsigned>(symbol_id) |
| 253 | >= static_cast<unsigned>(symbol_cache_.length())) { |
Steve Block | 9fac840 | 2011-05-12 15:51:54 +0100 | [diff] [blame] | 254 | if (scanner().is_literal_ascii()) { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 255 | return isolate()->factory()->LookupAsciiSymbol( |
| 256 | scanner().literal_ascii_string()); |
Steve Block | 9fac840 | 2011-05-12 15:51:54 +0100 | [diff] [blame] | 257 | } else { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 258 | return isolate()->factory()->LookupTwoByteSymbol( |
| 259 | scanner().literal_uc16_string()); |
Steve Block | 9fac840 | 2011-05-12 15:51:54 +0100 | [diff] [blame] | 260 | } |
Teng-Hui Zhu | 3e5fa29 | 2010-11-09 16:16:48 -0800 | [diff] [blame] | 261 | } |
Steve Block | 9fac840 | 2011-05-12 15:51:54 +0100 | [diff] [blame] | 262 | return LookupCachedSymbol(symbol_id); |
Teng-Hui Zhu | 3e5fa29 | 2010-11-09 16:16:48 -0800 | [diff] [blame] | 263 | } |
| 264 | |
| 265 | |
Steve Block | 9fac840 | 2011-05-12 15:51:54 +0100 | [diff] [blame] | 266 | Handle<String> Parser::LookupCachedSymbol(int symbol_id) { |
Teng-Hui Zhu | 3e5fa29 | 2010-11-09 16:16:48 -0800 | [diff] [blame] | 267 | // Make sure the cache is large enough to hold the symbol identifier. |
| 268 | if (symbol_cache_.length() <= symbol_id) { |
| 269 | // Increase length to index + 1. |
| 270 | symbol_cache_.AddBlock(Handle<String>::null(), |
| 271 | symbol_id + 1 - symbol_cache_.length()); |
| 272 | } |
| 273 | Handle<String> result = symbol_cache_.at(symbol_id); |
| 274 | if (result.is_null()) { |
Steve Block | 9fac840 | 2011-05-12 15:51:54 +0100 | [diff] [blame] | 275 | if (scanner().is_literal_ascii()) { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 276 | result = isolate()->factory()->LookupAsciiSymbol( |
| 277 | scanner().literal_ascii_string()); |
Steve Block | 9fac840 | 2011-05-12 15:51:54 +0100 | [diff] [blame] | 278 | } else { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 279 | result = isolate()->factory()->LookupTwoByteSymbol( |
| 280 | scanner().literal_uc16_string()); |
Steve Block | 9fac840 | 2011-05-12 15:51:54 +0100 | [diff] [blame] | 281 | } |
Teng-Hui Zhu | 3e5fa29 | 2010-11-09 16:16:48 -0800 | [diff] [blame] | 282 | symbol_cache_.at(symbol_id) = result; |
Iain Merrick | 9ac36c9 | 2010-09-13 15:29:50 +0100 | [diff] [blame] | 283 | return result; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 284 | } |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 285 | isolate()->counters()->total_preparse_symbols_skipped()->Increment(); |
Teng-Hui Zhu | 3e5fa29 | 2010-11-09 16:16:48 -0800 | [diff] [blame] | 286 | return result; |
| 287 | } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 288 | |
Teng-Hui Zhu | 3e5fa29 | 2010-11-09 16:16:48 -0800 | [diff] [blame] | 289 | |
Kristian Monsen | 80d68ea | 2010-09-08 11:05:35 +0100 | [diff] [blame] | 290 | FunctionEntry ScriptDataImpl::GetFunctionEntry(int start) { |
| 291 | // The current pre-data entry must be a FunctionEntry with the given |
| 292 | // start position. |
Iain Merrick | 9ac36c9 | 2010-09-13 15:29:50 +0100 | [diff] [blame] | 293 | if ((function_index_ + FunctionEntry::kSize <= store_.length()) |
| 294 | && (static_cast<int>(store_[function_index_]) == start)) { |
| 295 | int index = function_index_; |
| 296 | function_index_ += FunctionEntry::kSize; |
Kristian Monsen | 80d68ea | 2010-09-08 11:05:35 +0100 | [diff] [blame] | 297 | return FunctionEntry(store_.SubVector(index, |
| 298 | index + FunctionEntry::kSize)); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 299 | } |
| 300 | return FunctionEntry(); |
| 301 | } |
| 302 | |
| 303 | |
Kristian Monsen | 0d5e116 | 2010-09-30 15:31:59 +0100 | [diff] [blame] | 304 | int ScriptDataImpl::GetSymbolIdentifier() { |
| 305 | return ReadNumber(&symbol_data_); |
Iain Merrick | 9ac36c9 | 2010-09-13 15:29:50 +0100 | [diff] [blame] | 306 | } |
| 307 | |
| 308 | |
Iain Merrick | 9ac36c9 | 2010-09-13 15:29:50 +0100 | [diff] [blame] | 309 | bool ScriptDataImpl::SanityCheck() { |
| 310 | // Check that the header data is valid and doesn't specify |
| 311 | // point to positions outside the store. |
Shimeng (Simon) Wang | 8a31eba | 2010-12-06 19:01:33 -0800 | [diff] [blame] | 312 | if (store_.length() < PreparseDataConstants::kHeaderSize) return false; |
| 313 | if (magic() != PreparseDataConstants::kMagicNumber) return false; |
| 314 | if (version() != PreparseDataConstants::kCurrentVersion) return false; |
Iain Merrick | 9ac36c9 | 2010-09-13 15:29:50 +0100 | [diff] [blame] | 315 | if (has_error()) { |
| 316 | // Extra sane sanity check for error message encoding. |
Shimeng (Simon) Wang | 8a31eba | 2010-12-06 19:01:33 -0800 | [diff] [blame] | 317 | if (store_.length() <= PreparseDataConstants::kHeaderSize |
| 318 | + PreparseDataConstants::kMessageTextPos) { |
| 319 | return false; |
| 320 | } |
| 321 | if (Read(PreparseDataConstants::kMessageStartPos) > |
| 322 | Read(PreparseDataConstants::kMessageEndPos)) { |
| 323 | return false; |
| 324 | } |
| 325 | unsigned arg_count = Read(PreparseDataConstants::kMessageArgCountPos); |
| 326 | int pos = PreparseDataConstants::kMessageTextPos; |
Iain Merrick | 9ac36c9 | 2010-09-13 15:29:50 +0100 | [diff] [blame] | 327 | for (unsigned int i = 0; i <= arg_count; i++) { |
Shimeng (Simon) Wang | 8a31eba | 2010-12-06 19:01:33 -0800 | [diff] [blame] | 328 | if (store_.length() <= PreparseDataConstants::kHeaderSize + pos) { |
| 329 | return false; |
| 330 | } |
Iain Merrick | 9ac36c9 | 2010-09-13 15:29:50 +0100 | [diff] [blame] | 331 | int length = static_cast<int>(Read(pos)); |
| 332 | if (length < 0) return false; |
| 333 | pos += 1 + length; |
| 334 | } |
Shimeng (Simon) Wang | 8a31eba | 2010-12-06 19:01:33 -0800 | [diff] [blame] | 335 | if (store_.length() < PreparseDataConstants::kHeaderSize + pos) { |
| 336 | return false; |
| 337 | } |
Iain Merrick | 9ac36c9 | 2010-09-13 15:29:50 +0100 | [diff] [blame] | 338 | return true; |
| 339 | } |
| 340 | // Check that the space allocated for function entries is sane. |
| 341 | int functions_size = |
Shimeng (Simon) Wang | 8a31eba | 2010-12-06 19:01:33 -0800 | [diff] [blame] | 342 | static_cast<int>(store_[PreparseDataConstants::kFunctionsSizeOffset]); |
Iain Merrick | 9ac36c9 | 2010-09-13 15:29:50 +0100 | [diff] [blame] | 343 | if (functions_size < 0) return false; |
| 344 | if (functions_size % FunctionEntry::kSize != 0) return false; |
| 345 | // Check that the count of symbols is non-negative. |
| 346 | int symbol_count = |
Shimeng (Simon) Wang | 8a31eba | 2010-12-06 19:01:33 -0800 | [diff] [blame] | 347 | static_cast<int>(store_[PreparseDataConstants::kSymbolCountOffset]); |
Iain Merrick | 9ac36c9 | 2010-09-13 15:29:50 +0100 | [diff] [blame] | 348 | if (symbol_count < 0) return false; |
Kristian Monsen | 0d5e116 | 2010-09-30 15:31:59 +0100 | [diff] [blame] | 349 | // Check that the total size has room for header and function entries. |
Iain Merrick | 9ac36c9 | 2010-09-13 15:29:50 +0100 | [diff] [blame] | 350 | int minimum_size = |
Shimeng (Simon) Wang | 8a31eba | 2010-12-06 19:01:33 -0800 | [diff] [blame] | 351 | PreparseDataConstants::kHeaderSize + functions_size; |
Iain Merrick | 9ac36c9 | 2010-09-13 15:29:50 +0100 | [diff] [blame] | 352 | if (store_.length() < minimum_size) return false; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 353 | return true; |
| 354 | } |
| 355 | |
| 356 | |
Steve Block | 5915150 | 2010-09-22 15:07:15 +0100 | [diff] [blame] | 357 | |
Kristian Monsen | 80d68ea | 2010-09-08 11:05:35 +0100 | [diff] [blame] | 358 | const char* ScriptDataImpl::ReadString(unsigned* start, int* chars) { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 359 | int length = start[0]; |
| 360 | char* result = NewArray<char>(length + 1); |
Kristian Monsen | 80d68ea | 2010-09-08 11:05:35 +0100 | [diff] [blame] | 361 | for (int i = 0; i < length; i++) { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 362 | result[i] = start[i + 1]; |
Kristian Monsen | 80d68ea | 2010-09-08 11:05:35 +0100 | [diff] [blame] | 363 | } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 364 | result[length] = '\0'; |
| 365 | if (chars != NULL) *chars = length; |
| 366 | return result; |
| 367 | } |
| 368 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 369 | Scanner::Location ScriptDataImpl::MessageLocation() { |
Shimeng (Simon) Wang | 8a31eba | 2010-12-06 19:01:33 -0800 | [diff] [blame] | 370 | int beg_pos = Read(PreparseDataConstants::kMessageStartPos); |
| 371 | int end_pos = Read(PreparseDataConstants::kMessageEndPos); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 372 | return Scanner::Location(beg_pos, end_pos); |
| 373 | } |
| 374 | |
| 375 | |
| 376 | const char* ScriptDataImpl::BuildMessage() { |
Shimeng (Simon) Wang | 8a31eba | 2010-12-06 19:01:33 -0800 | [diff] [blame] | 377 | unsigned* start = ReadAddress(PreparseDataConstants::kMessageTextPos); |
Kristian Monsen | 80d68ea | 2010-09-08 11:05:35 +0100 | [diff] [blame] | 378 | return ReadString(start, NULL); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 379 | } |
| 380 | |
| 381 | |
| 382 | Vector<const char*> ScriptDataImpl::BuildArgs() { |
Shimeng (Simon) Wang | 8a31eba | 2010-12-06 19:01:33 -0800 | [diff] [blame] | 383 | int arg_count = Read(PreparseDataConstants::kMessageArgCountPos); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 384 | const char** array = NewArray<const char*>(arg_count); |
Kristian Monsen | 0d5e116 | 2010-09-30 15:31:59 +0100 | [diff] [blame] | 385 | // Position after text found by skipping past length field and |
| 386 | // length field content words. |
Shimeng (Simon) Wang | 8a31eba | 2010-12-06 19:01:33 -0800 | [diff] [blame] | 387 | int pos = PreparseDataConstants::kMessageTextPos + 1 |
| 388 | + Read(PreparseDataConstants::kMessageTextPos); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 389 | for (int i = 0; i < arg_count; i++) { |
| 390 | int count = 0; |
Kristian Monsen | 80d68ea | 2010-09-08 11:05:35 +0100 | [diff] [blame] | 391 | array[i] = ReadString(ReadAddress(pos), &count); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 392 | pos += count + 1; |
| 393 | } |
| 394 | return Vector<const char*>(array, arg_count); |
| 395 | } |
| 396 | |
| 397 | |
| 398 | unsigned ScriptDataImpl::Read(int position) { |
Shimeng (Simon) Wang | 8a31eba | 2010-12-06 19:01:33 -0800 | [diff] [blame] | 399 | return store_[PreparseDataConstants::kHeaderSize + position]; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 400 | } |
| 401 | |
| 402 | |
| 403 | unsigned* ScriptDataImpl::ReadAddress(int position) { |
Shimeng (Simon) Wang | 8a31eba | 2010-12-06 19:01:33 -0800 | [diff] [blame] | 404 | return &store_[PreparseDataConstants::kHeaderSize + position]; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 405 | } |
| 406 | |
| 407 | |
Teng-Hui Zhu | 3e5fa29 | 2010-11-09 16:16:48 -0800 | [diff] [blame] | 408 | Scope* Parser::NewScope(Scope* parent, Scope::Type type, bool inside_with) { |
Ben Murdoch | 8b112d2 | 2011-06-08 16:22:53 +0100 | [diff] [blame] | 409 | Scope* result = new(zone()) Scope(parent, type); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 410 | result->Initialize(inside_with); |
| 411 | return result; |
| 412 | } |
| 413 | |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame^] | 414 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 415 | // ---------------------------------------------------------------------------- |
| 416 | // Target is a support class to facilitate manipulation of the |
| 417 | // Parser's target_stack_ (the stack of potential 'break' and |
| 418 | // 'continue' statement targets). Upon construction, a new target is |
| 419 | // added; it is removed upon destruction. |
| 420 | |
| 421 | class Target BASE_EMBEDDED { |
| 422 | public: |
Teng-Hui Zhu | 3e5fa29 | 2010-11-09 16:16:48 -0800 | [diff] [blame] | 423 | Target(Target** variable, AstNode* node) |
| 424 | : variable_(variable), node_(node), previous_(*variable) { |
| 425 | *variable = this; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 426 | } |
| 427 | |
| 428 | ~Target() { |
Teng-Hui Zhu | 3e5fa29 | 2010-11-09 16:16:48 -0800 | [diff] [blame] | 429 | *variable_ = previous_; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 430 | } |
| 431 | |
| 432 | Target* previous() { return previous_; } |
| 433 | AstNode* node() { return node_; } |
| 434 | |
| 435 | private: |
Teng-Hui Zhu | 3e5fa29 | 2010-11-09 16:16:48 -0800 | [diff] [blame] | 436 | Target** variable_; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 437 | AstNode* node_; |
| 438 | Target* previous_; |
| 439 | }; |
| 440 | |
| 441 | |
| 442 | class TargetScope BASE_EMBEDDED { |
| 443 | public: |
Teng-Hui Zhu | 3e5fa29 | 2010-11-09 16:16:48 -0800 | [diff] [blame] | 444 | explicit TargetScope(Target** variable) |
| 445 | : variable_(variable), previous_(*variable) { |
| 446 | *variable = NULL; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 447 | } |
| 448 | |
| 449 | ~TargetScope() { |
Teng-Hui Zhu | 3e5fa29 | 2010-11-09 16:16:48 -0800 | [diff] [blame] | 450 | *variable_ = previous_; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 451 | } |
| 452 | |
| 453 | private: |
Teng-Hui Zhu | 3e5fa29 | 2010-11-09 16:16:48 -0800 | [diff] [blame] | 454 | Target** variable_; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 455 | Target* previous_; |
| 456 | }; |
| 457 | |
| 458 | |
| 459 | // ---------------------------------------------------------------------------- |
| 460 | // LexicalScope is a support class to facilitate manipulation of the |
| 461 | // Parser's scope stack. The constructor sets the parser's top scope |
| 462 | // to the incoming scope, and the destructor resets it. |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 463 | // |
Ben Murdoch | 8b112d2 | 2011-06-08 16:22:53 +0100 | [diff] [blame] | 464 | // Additionally, it stores transient information used during parsing. |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 465 | // These scopes are not kept around after parsing or referenced by syntax |
| 466 | // trees so they can be stack-allocated and hence used by the pre-parser. |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 467 | |
| 468 | class LexicalScope BASE_EMBEDDED { |
| 469 | public: |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 470 | LexicalScope(Parser* parser, Scope* scope, Isolate* isolate); |
| 471 | ~LexicalScope(); |
| 472 | |
| 473 | int NextMaterializedLiteralIndex() { |
| 474 | int next_index = |
| 475 | materialized_literal_count_ + JSFunction::kLiteralsPrefixSize; |
| 476 | materialized_literal_count_++; |
| 477 | return next_index; |
| 478 | } |
| 479 | int materialized_literal_count() { return materialized_literal_count_; } |
| 480 | |
| 481 | void SetThisPropertyAssignmentInfo( |
| 482 | bool only_simple_this_property_assignments, |
| 483 | Handle<FixedArray> this_property_assignments) { |
| 484 | only_simple_this_property_assignments_ = |
| 485 | only_simple_this_property_assignments; |
| 486 | this_property_assignments_ = this_property_assignments; |
| 487 | } |
| 488 | bool only_simple_this_property_assignments() { |
| 489 | return only_simple_this_property_assignments_; |
| 490 | } |
| 491 | Handle<FixedArray> this_property_assignments() { |
| 492 | return this_property_assignments_; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 493 | } |
| 494 | |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 495 | void AddProperty() { expected_property_count_++; } |
| 496 | int expected_property_count() { return expected_property_count_; } |
| 497 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 498 | private: |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 499 | // Captures the number of literals that need materialization in the |
| 500 | // function. Includes regexp literals, and boilerplate for object |
| 501 | // and array literals. |
| 502 | int materialized_literal_count_; |
| 503 | |
| 504 | // Properties count estimation. |
| 505 | int expected_property_count_; |
| 506 | |
| 507 | // Keeps track of assignments to properties of this. Used for |
| 508 | // optimizing constructors. |
| 509 | bool only_simple_this_property_assignments_; |
| 510 | Handle<FixedArray> this_property_assignments_; |
| 511 | |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 512 | // Bookkeeping |
| 513 | Parser* parser_; |
| 514 | // Previous values |
| 515 | LexicalScope* lexical_scope_parent_; |
| 516 | Scope* previous_scope_; |
| 517 | int previous_with_nesting_level_; |
Ben Murdoch | 8b112d2 | 2011-06-08 16:22:53 +0100 | [diff] [blame] | 518 | unsigned previous_ast_node_id_; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 519 | }; |
| 520 | |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 521 | |
| 522 | LexicalScope::LexicalScope(Parser* parser, Scope* scope, Isolate* isolate) |
| 523 | : materialized_literal_count_(0), |
| 524 | expected_property_count_(0), |
| 525 | only_simple_this_property_assignments_(false), |
| 526 | this_property_assignments_(isolate->factory()->empty_fixed_array()), |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 527 | parser_(parser), |
| 528 | lexical_scope_parent_(parser->lexical_scope_), |
| 529 | previous_scope_(parser->top_scope_), |
Ben Murdoch | 8b112d2 | 2011-06-08 16:22:53 +0100 | [diff] [blame] | 530 | previous_with_nesting_level_(parser->with_nesting_level_), |
| 531 | previous_ast_node_id_(isolate->ast_node_id()) { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 532 | parser->top_scope_ = scope; |
| 533 | parser->lexical_scope_ = this; |
| 534 | parser->with_nesting_level_ = 0; |
Ben Murdoch | 8b112d2 | 2011-06-08 16:22:53 +0100 | [diff] [blame] | 535 | isolate->set_ast_node_id(AstNode::kFunctionEntryId + 1); |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 536 | } |
| 537 | |
| 538 | |
| 539 | LexicalScope::~LexicalScope() { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 540 | parser_->top_scope_ = previous_scope_; |
| 541 | parser_->lexical_scope_ = lexical_scope_parent_; |
| 542 | parser_->with_nesting_level_ = previous_with_nesting_level_; |
Ben Murdoch | 8b112d2 | 2011-06-08 16:22:53 +0100 | [diff] [blame] | 543 | parser_->isolate()->set_ast_node_id(previous_ast_node_id_); |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 544 | } |
| 545 | |
| 546 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 547 | // ---------------------------------------------------------------------------- |
| 548 | // The CHECK_OK macro is a convenient macro to enforce error |
| 549 | // handling for functions that may fail (by returning !*ok). |
| 550 | // |
| 551 | // CAUTION: This macro appends extra statements after a call, |
| 552 | // thus it must never be used where only a single statement |
| 553 | // is correct (e.g. an if statement branch w/o braces)! |
| 554 | |
| 555 | #define CHECK_OK ok); \ |
| 556 | if (!*ok) return NULL; \ |
| 557 | ((void)0 |
| 558 | #define DUMMY ) // to make indentation work |
| 559 | #undef DUMMY |
| 560 | |
| 561 | #define CHECK_FAILED /**/); \ |
| 562 | if (failed_) return NULL; \ |
| 563 | ((void)0 |
| 564 | #define DUMMY ) // to make indentation work |
| 565 | #undef DUMMY |
| 566 | |
| 567 | // ---------------------------------------------------------------------------- |
| 568 | // Implementation of Parser |
| 569 | |
| 570 | Parser::Parser(Handle<Script> script, |
| 571 | bool allow_natives_syntax, |
| 572 | v8::Extension* extension, |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 573 | ScriptDataImpl* pre_data) |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 574 | : isolate_(script->GetIsolate()), |
| 575 | symbol_cache_(pre_data ? pre_data->symbol_count() : 0), |
Teng-Hui Zhu | 3e5fa29 | 2010-11-09 16:16:48 -0800 | [diff] [blame] | 576 | script_(script), |
Ben Murdoch | 8b112d2 | 2011-06-08 16:22:53 +0100 | [diff] [blame] | 577 | scanner_(isolate_->unicode_cache()), |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 578 | top_scope_(NULL), |
| 579 | with_nesting_level_(0), |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 580 | lexical_scope_(NULL), |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 581 | target_stack_(NULL), |
| 582 | allow_natives_syntax_(allow_natives_syntax), |
| 583 | extension_(extension), |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 584 | pre_data_(pre_data), |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 585 | fni_(NULL), |
Ben Murdoch | b8e0da2 | 2011-05-16 14:20:40 +0100 | [diff] [blame] | 586 | stack_overflow_(false), |
| 587 | parenthesized_function_(false) { |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 588 | AstNode::ResetIds(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 589 | } |
| 590 | |
| 591 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 592 | FunctionLiteral* Parser::ParseProgram(Handle<String> source, |
Steve Block | 1e0659c | 2011-05-24 12:43:12 +0100 | [diff] [blame] | 593 | bool in_global_context, |
| 594 | StrictModeFlag strict_mode) { |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame^] | 595 | ZoneScope zone_scope(isolate(), DONT_DELETE_ON_EXIT); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 596 | |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 597 | HistogramTimerScope timer(isolate()->counters()->parse()); |
| 598 | isolate()->counters()->total_parse_size()->Increment(source->length()); |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame^] | 599 | fni_ = new(zone()) FuncNameInferrer(isolate()); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 600 | |
| 601 | // Initialize parser state. |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 602 | source->TryFlatten(); |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 603 | if (source->IsExternalTwoByteString()) { |
| 604 | // Notice that the stream is destroyed at the end of the branch block. |
| 605 | // The last line of the blocks can't be moved outside, even though they're |
| 606 | // identical calls. |
| 607 | ExternalTwoByteStringUC16CharacterStream stream( |
| 608 | Handle<ExternalTwoByteString>::cast(source), 0, source->length()); |
Steve Block | 9fac840 | 2011-05-12 15:51:54 +0100 | [diff] [blame] | 609 | scanner_.Initialize(&stream); |
Steve Block | 1e0659c | 2011-05-24 12:43:12 +0100 | [diff] [blame] | 610 | return DoParseProgram(source, in_global_context, strict_mode, &zone_scope); |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 611 | } else { |
| 612 | GenericStringUC16CharacterStream stream(source, 0, source->length()); |
Steve Block | 9fac840 | 2011-05-12 15:51:54 +0100 | [diff] [blame] | 613 | scanner_.Initialize(&stream); |
Steve Block | 1e0659c | 2011-05-24 12:43:12 +0100 | [diff] [blame] | 614 | return DoParseProgram(source, in_global_context, strict_mode, &zone_scope); |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 615 | } |
| 616 | } |
| 617 | |
| 618 | |
| 619 | FunctionLiteral* Parser::DoParseProgram(Handle<String> source, |
| 620 | bool in_global_context, |
Steve Block | 1e0659c | 2011-05-24 12:43:12 +0100 | [diff] [blame] | 621 | StrictModeFlag strict_mode, |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 622 | ZoneScope* zone_scope) { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 623 | ASSERT(target_stack_ == NULL); |
Kristian Monsen | 0d5e116 | 2010-09-30 15:31:59 +0100 | [diff] [blame] | 624 | if (pre_data_ != NULL) pre_data_->Initialize(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 625 | |
| 626 | // Compute the parsing mode. |
| 627 | mode_ = FLAG_lazy ? PARSE_LAZILY : PARSE_EAGERLY; |
| 628 | if (allow_natives_syntax_ || extension_ != NULL) mode_ = PARSE_EAGERLY; |
| 629 | |
| 630 | Scope::Type type = |
| 631 | in_global_context |
| 632 | ? Scope::GLOBAL_SCOPE |
| 633 | : Scope::EVAL_SCOPE; |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 634 | Handle<String> no_name = isolate()->factory()->empty_symbol(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 635 | |
| 636 | FunctionLiteral* result = NULL; |
Teng-Hui Zhu | 3e5fa29 | 2010-11-09 16:16:48 -0800 | [diff] [blame] | 637 | { Scope* scope = NewScope(top_scope_, type, inside_with()); |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 638 | LexicalScope lexical_scope(this, scope, isolate()); |
Steve Block | 1e0659c | 2011-05-24 12:43:12 +0100 | [diff] [blame] | 639 | if (strict_mode == kStrictMode) { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 640 | top_scope_->EnableStrictMode(); |
Steve Block | 1e0659c | 2011-05-24 12:43:12 +0100 | [diff] [blame] | 641 | } |
Ben Murdoch | 257744e | 2011-11-30 15:57:28 +0000 | [diff] [blame] | 642 | ZoneList<Statement*>* body = new(zone()) ZoneList<Statement*>(16); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 643 | bool ok = true; |
Steve Block | 1e0659c | 2011-05-24 12:43:12 +0100 | [diff] [blame] | 644 | int beg_loc = scanner().location().beg_pos; |
Teng-Hui Zhu | 3e5fa29 | 2010-11-09 16:16:48 -0800 | [diff] [blame] | 645 | ParseSourceElements(body, Token::EOS, &ok); |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 646 | if (ok && top_scope_->is_strict_mode()) { |
Steve Block | 1e0659c | 2011-05-24 12:43:12 +0100 | [diff] [blame] | 647 | CheckOctalLiteral(beg_loc, scanner().location().end_pos, &ok); |
| 648 | } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 649 | if (ok) { |
Ben Murdoch | 8b112d2 | 2011-06-08 16:22:53 +0100 | [diff] [blame] | 650 | result = new(zone()) FunctionLiteral( |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame^] | 651 | isolate(), |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 652 | no_name, |
| 653 | top_scope_, |
Teng-Hui Zhu | 3e5fa29 | 2010-11-09 16:16:48 -0800 | [diff] [blame] | 654 | body, |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 655 | lexical_scope.materialized_literal_count(), |
| 656 | lexical_scope.expected_property_count(), |
| 657 | lexical_scope.only_simple_this_property_assignments(), |
| 658 | lexical_scope.this_property_assignments(), |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 659 | 0, |
| 660 | 0, |
| 661 | source->length(), |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame^] | 662 | FunctionLiteral::ANONYMOUS_EXPRESSION, |
| 663 | false); // Does not have duplicate parameters. |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 664 | } else if (stack_overflow_) { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 665 | isolate()->StackOverflow(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 666 | } |
| 667 | } |
| 668 | |
| 669 | // Make sure the target stack is empty. |
| 670 | ASSERT(target_stack_ == NULL); |
| 671 | |
| 672 | // If there was a syntax error we have to get rid of the AST |
| 673 | // and it is not safe to do so before the scope has been deleted. |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 674 | if (result == NULL) zone_scope->DeleteOnExit(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 675 | return result; |
| 676 | } |
| 677 | |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 678 | FunctionLiteral* Parser::ParseLazy(CompilationInfo* info) { |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame^] | 679 | ZoneScope zone_scope(isolate(), DONT_DELETE_ON_EXIT); |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 680 | HistogramTimerScope timer(isolate()->counters()->parse_lazy()); |
Ben Murdoch | f87a203 | 2010-10-22 12:50:53 +0100 | [diff] [blame] | 681 | Handle<String> source(String::cast(script_->source())); |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 682 | isolate()->counters()->total_parse_size()->Increment(source->length()); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 683 | |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 684 | Handle<SharedFunctionInfo> shared_info = info->shared_info(); |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 685 | // Initialize parser state. |
| 686 | source->TryFlatten(); |
| 687 | if (source->IsExternalTwoByteString()) { |
| 688 | ExternalTwoByteStringUC16CharacterStream stream( |
| 689 | Handle<ExternalTwoByteString>::cast(source), |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 690 | shared_info->start_position(), |
| 691 | shared_info->end_position()); |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 692 | FunctionLiteral* result = ParseLazy(info, &stream, &zone_scope); |
| 693 | return result; |
| 694 | } else { |
| 695 | GenericStringUC16CharacterStream stream(source, |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 696 | shared_info->start_position(), |
| 697 | shared_info->end_position()); |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 698 | FunctionLiteral* result = ParseLazy(info, &stream, &zone_scope); |
| 699 | return result; |
| 700 | } |
| 701 | } |
| 702 | |
| 703 | |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 704 | FunctionLiteral* Parser::ParseLazy(CompilationInfo* info, |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 705 | UC16CharacterStream* source, |
| 706 | ZoneScope* zone_scope) { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 707 | Handle<SharedFunctionInfo> shared_info = info->shared_info(); |
Steve Block | 9fac840 | 2011-05-12 15:51:54 +0100 | [diff] [blame] | 708 | scanner_.Initialize(source); |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 709 | ASSERT(target_stack_ == NULL); |
| 710 | |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 711 | Handle<String> name(String::cast(shared_info->name())); |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame^] | 712 | fni_ = new(zone()) FuncNameInferrer(isolate()); |
Kristian Monsen | 80d68ea | 2010-09-08 11:05:35 +0100 | [diff] [blame] | 713 | fni_->PushEnclosingName(name); |
| 714 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 715 | mode_ = PARSE_EAGERLY; |
| 716 | |
| 717 | // Place holder for the result. |
| 718 | FunctionLiteral* result = NULL; |
| 719 | |
| 720 | { |
| 721 | // Parse the function literal. |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 722 | Scope* scope = NewScope(top_scope_, Scope::GLOBAL_SCOPE, inside_with()); |
| 723 | if (!info->closure().is_null()) { |
| 724 | scope = Scope::DeserializeScopeChain(info, scope); |
| 725 | } |
| 726 | LexicalScope lexical_scope(this, scope, isolate()); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 727 | |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 728 | if (shared_info->strict_mode()) { |
| 729 | top_scope_->EnableStrictMode(); |
Steve Block | 1e0659c | 2011-05-24 12:43:12 +0100 | [diff] [blame] | 730 | } |
| 731 | |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame^] | 732 | FunctionLiteral::Type type = shared_info->is_expression() |
| 733 | ? (shared_info->is_anonymous() |
| 734 | ? FunctionLiteral::ANONYMOUS_EXPRESSION |
| 735 | : FunctionLiteral::NAMED_EXPRESSION) |
| 736 | : FunctionLiteral::DECLARATION; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 737 | bool ok = true; |
Steve Block | 1e0659c | 2011-05-24 12:43:12 +0100 | [diff] [blame] | 738 | result = ParseFunctionLiteral(name, |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame^] | 739 | false, // Strict mode name already checked. |
| 740 | RelocInfo::kNoPosition, |
| 741 | type, |
| 742 | &ok); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 743 | // Make sure the results agree. |
| 744 | ASSERT(ok == (result != NULL)); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 745 | } |
| 746 | |
| 747 | // Make sure the target stack is empty. |
| 748 | ASSERT(target_stack_ == NULL); |
| 749 | |
| 750 | // If there was a stack overflow we have to get rid of AST and it is |
| 751 | // not safe to do before scope has been deleted. |
| 752 | if (result == NULL) { |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 753 | zone_scope->DeleteOnExit(); |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 754 | if (stack_overflow_) isolate()->StackOverflow(); |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 755 | } else { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 756 | Handle<String> inferred_name(shared_info->inferred_name()); |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 757 | result->set_inferred_name(inferred_name); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 758 | } |
| 759 | return result; |
| 760 | } |
| 761 | |
Ben Murdoch | f87a203 | 2010-10-22 12:50:53 +0100 | [diff] [blame] | 762 | |
Teng-Hui Zhu | 3e5fa29 | 2010-11-09 16:16:48 -0800 | [diff] [blame] | 763 | Handle<String> Parser::GetSymbol(bool* ok) { |
| 764 | int symbol_id = -1; |
| 765 | if (pre_data() != NULL) { |
| 766 | symbol_id = pre_data()->GetSymbolIdentifier(); |
Leon Clarke | 4515c47 | 2010-02-03 11:58:03 +0000 | [diff] [blame] | 767 | } |
Steve Block | 9fac840 | 2011-05-12 15:51:54 +0100 | [diff] [blame] | 768 | return LookupSymbol(symbol_id); |
Leon Clarke | 4515c47 | 2010-02-03 11:58:03 +0000 | [diff] [blame] | 769 | } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 770 | |
Teng-Hui Zhu | 3e5fa29 | 2010-11-09 16:16:48 -0800 | [diff] [blame] | 771 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 772 | void Parser::ReportMessage(const char* type, Vector<const char*> args) { |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 773 | Scanner::Location source_location = scanner().location(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 774 | ReportMessageAt(source_location, type, args); |
| 775 | } |
| 776 | |
| 777 | |
Teng-Hui Zhu | 3e5fa29 | 2010-11-09 16:16:48 -0800 | [diff] [blame] | 778 | void Parser::ReportMessageAt(Scanner::Location source_location, |
| 779 | const char* type, |
| 780 | Vector<const char*> args) { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 781 | MessageLocation location(script_, |
Ben Murdoch | b8e0da2 | 2011-05-16 14:20:40 +0100 | [diff] [blame] | 782 | source_location.beg_pos, |
| 783 | source_location.end_pos); |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 784 | Factory* factory = isolate()->factory(); |
| 785 | Handle<FixedArray> elements = factory->NewFixedArray(args.length()); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 786 | for (int i = 0; i < args.length(); i++) { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 787 | Handle<String> arg_string = factory->NewStringFromUtf8(CStrVector(args[i])); |
Ben Murdoch | e0cee9b | 2011-05-25 10:26:03 +0100 | [diff] [blame] | 788 | elements->set(i, *arg_string); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 789 | } |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 790 | Handle<JSArray> array = factory->NewJSArrayWithElements(elements); |
| 791 | Handle<Object> result = factory->NewSyntaxError(type, array); |
| 792 | isolate()->Throw(*result, &location); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 793 | } |
| 794 | |
| 795 | |
Ben Murdoch | b8e0da2 | 2011-05-16 14:20:40 +0100 | [diff] [blame] | 796 | void Parser::ReportMessageAt(Scanner::Location source_location, |
| 797 | const char* type, |
| 798 | Vector<Handle<String> > args) { |
| 799 | MessageLocation location(script_, |
| 800 | source_location.beg_pos, |
| 801 | source_location.end_pos); |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 802 | Factory* factory = isolate()->factory(); |
| 803 | Handle<FixedArray> elements = factory->NewFixedArray(args.length()); |
Ben Murdoch | b8e0da2 | 2011-05-16 14:20:40 +0100 | [diff] [blame] | 804 | for (int i = 0; i < args.length(); i++) { |
Ben Murdoch | e0cee9b | 2011-05-25 10:26:03 +0100 | [diff] [blame] | 805 | elements->set(i, *args[i]); |
Ben Murdoch | b8e0da2 | 2011-05-16 14:20:40 +0100 | [diff] [blame] | 806 | } |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 807 | Handle<JSArray> array = factory->NewJSArrayWithElements(elements); |
| 808 | Handle<Object> result = factory->NewSyntaxError(type, array); |
| 809 | isolate()->Throw(*result, &location); |
Ben Murdoch | b8e0da2 | 2011-05-16 14:20:40 +0100 | [diff] [blame] | 810 | } |
| 811 | |
| 812 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 813 | // Base class containing common code for the different finder classes used by |
| 814 | // the parser. |
| 815 | class ParserFinder { |
| 816 | protected: |
| 817 | ParserFinder() {} |
| 818 | static Assignment* AsAssignment(Statement* stat) { |
| 819 | if (stat == NULL) return NULL; |
| 820 | ExpressionStatement* exp_stat = stat->AsExpressionStatement(); |
| 821 | if (exp_stat == NULL) return NULL; |
| 822 | return exp_stat->expression()->AsAssignment(); |
| 823 | } |
| 824 | }; |
| 825 | |
| 826 | |
| 827 | // An InitializationBlockFinder finds and marks sequences of statements of the |
Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 828 | // form expr.a = ...; expr.b = ...; etc. |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 829 | class InitializationBlockFinder : public ParserFinder { |
| 830 | public: |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame^] | 831 | // We find and mark the initialization blocks in top level |
| 832 | // non-looping code only. This is because the optimization prevents |
| 833 | // reuse of the map transitions, so it should be used only for code |
| 834 | // that will only be run once. |
| 835 | InitializationBlockFinder(Scope* top_scope, Target* target) |
| 836 | : enabled_(top_scope->DeclarationScope()->is_global_scope() && |
| 837 | !IsLoopTarget(target)), |
| 838 | first_in_block_(NULL), |
| 839 | last_in_block_(NULL), |
| 840 | block_size_(0) {} |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 841 | |
| 842 | ~InitializationBlockFinder() { |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame^] | 843 | if (!enabled_) return; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 844 | if (InBlock()) EndBlock(); |
| 845 | } |
| 846 | |
| 847 | void Update(Statement* stat) { |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame^] | 848 | if (!enabled_) return; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 849 | Assignment* assignment = AsAssignment(stat); |
| 850 | if (InBlock()) { |
| 851 | if (BlockContinues(assignment)) { |
| 852 | UpdateBlock(assignment); |
| 853 | } else { |
| 854 | EndBlock(); |
| 855 | } |
| 856 | } |
| 857 | if (!InBlock() && (assignment != NULL) && |
| 858 | (assignment->op() == Token::ASSIGN)) { |
| 859 | StartBlock(assignment); |
| 860 | } |
| 861 | } |
| 862 | |
| 863 | private: |
Teng-Hui Zhu | 3e5fa29 | 2010-11-09 16:16:48 -0800 | [diff] [blame] | 864 | // The minimum number of contiguous assignment that will |
| 865 | // be treated as an initialization block. Benchmarks show that |
| 866 | // the overhead exceeds the savings below this limit. |
| 867 | static const int kMinInitializationBlock = 3; |
| 868 | |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame^] | 869 | static bool IsLoopTarget(Target* target) { |
| 870 | while (target != NULL) { |
| 871 | if (target->node()->AsIterationStatement() != NULL) return true; |
| 872 | target = target->previous(); |
| 873 | } |
| 874 | return false; |
| 875 | } |
| 876 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 877 | // Returns true if the expressions appear to denote the same object. |
| 878 | // In the context of initialization blocks, we only consider expressions |
Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 879 | // of the form 'expr.x' or expr["x"]. |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 880 | static bool SameObject(Expression* e1, Expression* e2) { |
| 881 | VariableProxy* v1 = e1->AsVariableProxy(); |
| 882 | VariableProxy* v2 = e2->AsVariableProxy(); |
| 883 | if (v1 != NULL && v2 != NULL) { |
| 884 | return v1->name()->Equals(*v2->name()); |
| 885 | } |
| 886 | Property* p1 = e1->AsProperty(); |
| 887 | Property* p2 = e2->AsProperty(); |
| 888 | if ((p1 == NULL) || (p2 == NULL)) return false; |
| 889 | Literal* key1 = p1->key()->AsLiteral(); |
| 890 | Literal* key2 = p2->key()->AsLiteral(); |
| 891 | if ((key1 == NULL) || (key2 == NULL)) return false; |
| 892 | if (!key1->handle()->IsString() || !key2->handle()->IsString()) { |
| 893 | return false; |
| 894 | } |
| 895 | String* name1 = String::cast(*key1->handle()); |
| 896 | String* name2 = String::cast(*key2->handle()); |
| 897 | if (!name1->Equals(name2)) return false; |
| 898 | return SameObject(p1->obj(), p2->obj()); |
| 899 | } |
| 900 | |
| 901 | // Returns true if the expressions appear to denote different properties |
| 902 | // of the same object. |
| 903 | static bool PropertyOfSameObject(Expression* e1, Expression* e2) { |
| 904 | Property* p1 = e1->AsProperty(); |
| 905 | Property* p2 = e2->AsProperty(); |
| 906 | if ((p1 == NULL) || (p2 == NULL)) return false; |
| 907 | return SameObject(p1->obj(), p2->obj()); |
| 908 | } |
| 909 | |
| 910 | bool BlockContinues(Assignment* assignment) { |
| 911 | if ((assignment == NULL) || (first_in_block_ == NULL)) return false; |
| 912 | if (assignment->op() != Token::ASSIGN) return false; |
| 913 | return PropertyOfSameObject(first_in_block_->target(), |
| 914 | assignment->target()); |
| 915 | } |
| 916 | |
| 917 | void StartBlock(Assignment* assignment) { |
| 918 | first_in_block_ = assignment; |
| 919 | last_in_block_ = assignment; |
| 920 | block_size_ = 1; |
| 921 | } |
| 922 | |
| 923 | void UpdateBlock(Assignment* assignment) { |
| 924 | last_in_block_ = assignment; |
| 925 | ++block_size_; |
| 926 | } |
| 927 | |
| 928 | void EndBlock() { |
Teng-Hui Zhu | 3e5fa29 | 2010-11-09 16:16:48 -0800 | [diff] [blame] | 929 | if (block_size_ >= kMinInitializationBlock) { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 930 | first_in_block_->mark_block_start(); |
| 931 | last_in_block_->mark_block_end(); |
| 932 | } |
| 933 | last_in_block_ = first_in_block_ = NULL; |
| 934 | block_size_ = 0; |
| 935 | } |
| 936 | |
| 937 | bool InBlock() { return first_in_block_ != NULL; } |
| 938 | |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame^] | 939 | const bool enabled_; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 940 | Assignment* first_in_block_; |
| 941 | Assignment* last_in_block_; |
| 942 | int block_size_; |
| 943 | |
| 944 | DISALLOW_COPY_AND_ASSIGN(InitializationBlockFinder); |
| 945 | }; |
| 946 | |
| 947 | |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame^] | 948 | // A ThisNamedPropertyAssignmentFinder finds and marks statements of the form |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 949 | // this.x = ...;, where x is a named property. It also determines whether a |
| 950 | // function contains only assignments of this type. |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame^] | 951 | class ThisNamedPropertyAssignmentFinder : public ParserFinder { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 952 | public: |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame^] | 953 | explicit ThisNamedPropertyAssignmentFinder(Isolate* isolate) |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 954 | : isolate_(isolate), |
| 955 | only_simple_this_property_assignments_(true), |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame^] | 956 | names_(0), |
| 957 | assigned_arguments_(0), |
| 958 | assigned_constants_(0) { |
| 959 | } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 960 | |
| 961 | void Update(Scope* scope, Statement* stat) { |
Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 962 | // Bail out if function already has property assignment that are |
| 963 | // not simple this property assignments. |
| 964 | if (!only_simple_this_property_assignments_) { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 965 | return; |
| 966 | } |
| 967 | |
| 968 | // Check whether this statement is of the form this.x = ...; |
| 969 | Assignment* assignment = AsAssignment(stat); |
| 970 | if (IsThisPropertyAssignment(assignment)) { |
| 971 | HandleThisPropertyAssignment(scope, assignment); |
| 972 | } else { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 973 | only_simple_this_property_assignments_ = false; |
| 974 | } |
| 975 | } |
| 976 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 977 | // Returns whether only statements of the form this.x = y; where y is either a |
| 978 | // constant or a function argument was encountered. |
| 979 | bool only_simple_this_property_assignments() { |
| 980 | return only_simple_this_property_assignments_; |
| 981 | } |
| 982 | |
| 983 | // Returns a fixed array containing three elements for each assignment of the |
| 984 | // form this.x = y; |
| 985 | Handle<FixedArray> GetThisPropertyAssignments() { |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame^] | 986 | if (names_.is_empty()) { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 987 | return isolate_->factory()->empty_fixed_array(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 988 | } |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame^] | 989 | ASSERT_EQ(names_.length(), assigned_arguments_.length()); |
| 990 | ASSERT_EQ(names_.length(), assigned_constants_.length()); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 991 | Handle<FixedArray> assignments = |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame^] | 992 | isolate_->factory()->NewFixedArray(names_.length() * 3); |
| 993 | for (int i = 0; i < names_.length(); ++i) { |
| 994 | assignments->set(i * 3, *names_[i]); |
| 995 | assignments->set(i * 3 + 1, Smi::FromInt(assigned_arguments_[i])); |
| 996 | assignments->set(i * 3 + 2, *assigned_constants_[i]); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 997 | } |
| 998 | return assignments; |
| 999 | } |
| 1000 | |
| 1001 | private: |
| 1002 | bool IsThisPropertyAssignment(Assignment* assignment) { |
| 1003 | if (assignment != NULL) { |
| 1004 | Property* property = assignment->target()->AsProperty(); |
| 1005 | return assignment->op() == Token::ASSIGN |
| 1006 | && property != NULL |
| 1007 | && property->obj()->AsVariableProxy() != NULL |
| 1008 | && property->obj()->AsVariableProxy()->is_this(); |
| 1009 | } |
| 1010 | return false; |
| 1011 | } |
| 1012 | |
| 1013 | void HandleThisPropertyAssignment(Scope* scope, Assignment* assignment) { |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 1014 | // Check that the property assigned to is a named property, which is not |
| 1015 | // __proto__. |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1016 | Property* property = assignment->target()->AsProperty(); |
| 1017 | ASSERT(property != NULL); |
| 1018 | Literal* literal = property->key()->AsLiteral(); |
| 1019 | uint32_t dummy; |
| 1020 | if (literal != NULL && |
| 1021 | literal->handle()->IsString() && |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 1022 | !String::cast(*(literal->handle()))->Equals( |
| 1023 | isolate_->heap()->Proto_symbol()) && |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1024 | !String::cast(*(literal->handle()))->AsArrayIndex(&dummy)) { |
| 1025 | Handle<String> key = Handle<String>::cast(literal->handle()); |
| 1026 | |
| 1027 | // Check whether the value assigned is either a constant or matches the |
| 1028 | // name of one of the arguments to the function. |
| 1029 | if (assignment->value()->AsLiteral() != NULL) { |
| 1030 | // Constant assigned. |
| 1031 | Literal* literal = assignment->value()->AsLiteral(); |
| 1032 | AssignmentFromConstant(key, literal->handle()); |
Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 1033 | return; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1034 | } else if (assignment->value()->AsVariableProxy() != NULL) { |
| 1035 | // Variable assigned. |
| 1036 | Handle<String> name = |
| 1037 | assignment->value()->AsVariableProxy()->name(); |
| 1038 | // Check whether the variable assigned matches an argument name. |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1039 | for (int i = 0; i < scope->num_parameters(); i++) { |
| 1040 | if (*scope->parameter(i)->name() == *name) { |
| 1041 | // Assigned from function argument. |
Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 1042 | AssignmentFromParameter(key, i); |
| 1043 | return; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1044 | } |
| 1045 | } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1046 | } |
| 1047 | } |
Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 1048 | // It is not a simple "this.x = value;" assignment with a constant |
| 1049 | // or parameter value. |
| 1050 | AssignmentFromSomethingElse(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1051 | } |
| 1052 | |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame^] | 1053 | |
| 1054 | |
| 1055 | |
| 1056 | // We will potentially reorder the property assignments, so they must be |
| 1057 | // simple enough that the ordering does not matter. |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1058 | void AssignmentFromParameter(Handle<String> name, int index) { |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame^] | 1059 | EnsureInitialized(); |
| 1060 | for (int i = 0; i < names_.length(); ++i) { |
| 1061 | if (name->Equals(*names_[i])) { |
| 1062 | assigned_arguments_[i] = index; |
| 1063 | assigned_constants_[i] = isolate_->factory()->undefined_value(); |
| 1064 | return; |
| 1065 | } |
| 1066 | } |
| 1067 | names_.Add(name); |
| 1068 | assigned_arguments_.Add(index); |
| 1069 | assigned_constants_.Add(isolate_->factory()->undefined_value()); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1070 | } |
| 1071 | |
| 1072 | void AssignmentFromConstant(Handle<String> name, Handle<Object> value) { |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame^] | 1073 | EnsureInitialized(); |
| 1074 | for (int i = 0; i < names_.length(); ++i) { |
| 1075 | if (name->Equals(*names_[i])) { |
| 1076 | assigned_arguments_[i] = -1; |
| 1077 | assigned_constants_[i] = value; |
| 1078 | return; |
| 1079 | } |
| 1080 | } |
| 1081 | names_.Add(name); |
| 1082 | assigned_arguments_.Add(-1); |
| 1083 | assigned_constants_.Add(value); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1084 | } |
| 1085 | |
Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 1086 | void AssignmentFromSomethingElse() { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1087 | // The this assignment is not a simple one. |
| 1088 | only_simple_this_property_assignments_ = false; |
| 1089 | } |
| 1090 | |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame^] | 1091 | void EnsureInitialized() { |
| 1092 | if (names_.capacity() == 0) { |
| 1093 | ASSERT(assigned_arguments_.capacity() == 0); |
| 1094 | ASSERT(assigned_constants_.capacity() == 0); |
| 1095 | names_.Initialize(4); |
| 1096 | assigned_arguments_.Initialize(4); |
| 1097 | assigned_constants_.Initialize(4); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1098 | } |
| 1099 | } |
| 1100 | |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 1101 | Isolate* isolate_; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1102 | bool only_simple_this_property_assignments_; |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame^] | 1103 | ZoneStringList names_; |
| 1104 | ZoneList<int> assigned_arguments_; |
| 1105 | ZoneObjectList assigned_constants_; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1106 | }; |
| 1107 | |
| 1108 | |
Teng-Hui Zhu | 3e5fa29 | 2010-11-09 16:16:48 -0800 | [diff] [blame] | 1109 | void* Parser::ParseSourceElements(ZoneList<Statement*>* processor, |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1110 | int end_token, |
| 1111 | bool* ok) { |
| 1112 | // SourceElements :: |
| 1113 | // (Statement)* <end_token> |
| 1114 | |
| 1115 | // Allocate a target stack to use for this set of source |
| 1116 | // elements. This way, all scripts and functions get their own |
| 1117 | // target stack thus avoiding illegal breaks and continues across |
| 1118 | // functions. |
Teng-Hui Zhu | 3e5fa29 | 2010-11-09 16:16:48 -0800 | [diff] [blame] | 1119 | TargetScope scope(&this->target_stack_); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1120 | |
| 1121 | ASSERT(processor != NULL); |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame^] | 1122 | InitializationBlockFinder block_finder(top_scope_, target_stack_); |
| 1123 | ThisNamedPropertyAssignmentFinder this_property_assignment_finder(isolate()); |
Steve Block | 1e0659c | 2011-05-24 12:43:12 +0100 | [diff] [blame] | 1124 | bool directive_prologue = true; // Parsing directive prologue. |
| 1125 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1126 | while (peek() != end_token) { |
Steve Block | 1e0659c | 2011-05-24 12:43:12 +0100 | [diff] [blame] | 1127 | if (directive_prologue && peek() != Token::STRING) { |
| 1128 | directive_prologue = false; |
| 1129 | } |
| 1130 | |
| 1131 | Scanner::Location token_loc = scanner().peek_location(); |
Ben Murdoch | e0cee9b | 2011-05-25 10:26:03 +0100 | [diff] [blame] | 1132 | |
| 1133 | Statement* stat; |
| 1134 | if (peek() == Token::FUNCTION) { |
| 1135 | // FunctionDeclaration is only allowed in the context of SourceElements |
| 1136 | // (Ecma 262 5th Edition, clause 14): |
| 1137 | // SourceElement: |
| 1138 | // Statement |
| 1139 | // FunctionDeclaration |
| 1140 | // Common language extension is to allow function declaration in place |
| 1141 | // of any statement. This language extension is disabled in strict mode. |
| 1142 | stat = ParseFunctionDeclaration(CHECK_OK); |
| 1143 | } else { |
| 1144 | stat = ParseStatement(NULL, CHECK_OK); |
| 1145 | } |
Steve Block | 1e0659c | 2011-05-24 12:43:12 +0100 | [diff] [blame] | 1146 | |
| 1147 | if (stat == NULL || stat->IsEmpty()) { |
| 1148 | directive_prologue = false; // End of directive prologue. |
| 1149 | continue; |
| 1150 | } |
| 1151 | |
| 1152 | if (directive_prologue) { |
| 1153 | // A shot at a directive. |
| 1154 | ExpressionStatement *e_stat; |
| 1155 | Literal *literal; |
| 1156 | // Still processing directive prologue? |
| 1157 | if ((e_stat = stat->AsExpressionStatement()) != NULL && |
| 1158 | (literal = e_stat->expression()->AsLiteral()) != NULL && |
| 1159 | literal->handle()->IsString()) { |
| 1160 | Handle<String> directive = Handle<String>::cast(literal->handle()); |
| 1161 | |
| 1162 | // Check "use strict" directive (ES5 14.1). |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 1163 | if (!top_scope_->is_strict_mode() && |
| 1164 | directive->Equals(isolate()->heap()->use_strict()) && |
Steve Block | 1e0659c | 2011-05-24 12:43:12 +0100 | [diff] [blame] | 1165 | token_loc.end_pos - token_loc.beg_pos == |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 1166 | isolate()->heap()->use_strict()->length() + 2) { |
| 1167 | top_scope_->EnableStrictMode(); |
Steve Block | 1e0659c | 2011-05-24 12:43:12 +0100 | [diff] [blame] | 1168 | // "use strict" is the only directive for now. |
| 1169 | directive_prologue = false; |
| 1170 | } |
| 1171 | } else { |
| 1172 | // End of the directive prologue. |
| 1173 | directive_prologue = false; |
| 1174 | } |
| 1175 | } |
| 1176 | |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame^] | 1177 | block_finder.Update(stat); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1178 | // Find and mark all assignments to named properties in this (this.x =) |
| 1179 | if (top_scope_->is_function_scope()) { |
| 1180 | this_property_assignment_finder.Update(top_scope_, stat); |
| 1181 | } |
| 1182 | processor->Add(stat); |
| 1183 | } |
| 1184 | |
| 1185 | // Propagate the collected information on this property assignments. |
Teng-Hui Zhu | 3e5fa29 | 2010-11-09 16:16:48 -0800 | [diff] [blame] | 1186 | if (top_scope_->is_function_scope()) { |
Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 1187 | bool only_simple_this_property_assignments = |
Andrei Popescu | 402d937 | 2010-02-26 13:31:12 +0000 | [diff] [blame] | 1188 | this_property_assignment_finder.only_simple_this_property_assignments() |
| 1189 | && top_scope_->declarations()->length() == 0; |
Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 1190 | if (only_simple_this_property_assignments) { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 1191 | lexical_scope_->SetThisPropertyAssignmentInfo( |
Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 1192 | only_simple_this_property_assignments, |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1193 | this_property_assignment_finder.GetThisPropertyAssignments()); |
| 1194 | } |
| 1195 | } |
| 1196 | return 0; |
| 1197 | } |
| 1198 | |
| 1199 | |
| 1200 | Statement* Parser::ParseStatement(ZoneStringList* labels, bool* ok) { |
| 1201 | // Statement :: |
| 1202 | // Block |
| 1203 | // VariableStatement |
| 1204 | // EmptyStatement |
| 1205 | // ExpressionStatement |
| 1206 | // IfStatement |
| 1207 | // IterationStatement |
| 1208 | // ContinueStatement |
| 1209 | // BreakStatement |
| 1210 | // ReturnStatement |
| 1211 | // WithStatement |
| 1212 | // LabelledStatement |
| 1213 | // SwitchStatement |
| 1214 | // ThrowStatement |
| 1215 | // TryStatement |
| 1216 | // DebuggerStatement |
| 1217 | |
| 1218 | // Note: Since labels can only be used by 'break' and 'continue' |
| 1219 | // statements, which themselves are only valid within blocks, |
| 1220 | // iterations or 'switch' statements (i.e., BreakableStatements), |
| 1221 | // labels can be simply ignored in all other cases; except for |
| 1222 | // trivial labeled break statements 'label: break label' which is |
| 1223 | // parsed into an empty statement. |
| 1224 | |
| 1225 | // Keep the source position of the statement |
| 1226 | int statement_pos = scanner().peek_location().beg_pos; |
| 1227 | Statement* stmt = NULL; |
| 1228 | switch (peek()) { |
| 1229 | case Token::LBRACE: |
| 1230 | return ParseBlock(labels, ok); |
| 1231 | |
| 1232 | case Token::CONST: // fall through |
| 1233 | case Token::VAR: |
| 1234 | stmt = ParseVariableStatement(ok); |
| 1235 | break; |
| 1236 | |
| 1237 | case Token::SEMICOLON: |
| 1238 | Next(); |
Teng-Hui Zhu | 3e5fa29 | 2010-11-09 16:16:48 -0800 | [diff] [blame] | 1239 | return EmptyStatement(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1240 | |
| 1241 | case Token::IF: |
| 1242 | stmt = ParseIfStatement(labels, ok); |
| 1243 | break; |
| 1244 | |
| 1245 | case Token::DO: |
Steve Block | 3ce2e20 | 2009-11-05 08:53:23 +0000 | [diff] [blame] | 1246 | stmt = ParseDoWhileStatement(labels, ok); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1247 | break; |
| 1248 | |
| 1249 | case Token::WHILE: |
| 1250 | stmt = ParseWhileStatement(labels, ok); |
| 1251 | break; |
| 1252 | |
| 1253 | case Token::FOR: |
| 1254 | stmt = ParseForStatement(labels, ok); |
| 1255 | break; |
| 1256 | |
| 1257 | case Token::CONTINUE: |
| 1258 | stmt = ParseContinueStatement(ok); |
| 1259 | break; |
| 1260 | |
| 1261 | case Token::BREAK: |
| 1262 | stmt = ParseBreakStatement(labels, ok); |
| 1263 | break; |
| 1264 | |
| 1265 | case Token::RETURN: |
| 1266 | stmt = ParseReturnStatement(ok); |
| 1267 | break; |
| 1268 | |
| 1269 | case Token::WITH: |
| 1270 | stmt = ParseWithStatement(labels, ok); |
| 1271 | break; |
| 1272 | |
| 1273 | case Token::SWITCH: |
| 1274 | stmt = ParseSwitchStatement(labels, ok); |
| 1275 | break; |
| 1276 | |
| 1277 | case Token::THROW: |
| 1278 | stmt = ParseThrowStatement(ok); |
| 1279 | break; |
| 1280 | |
| 1281 | case Token::TRY: { |
| 1282 | // NOTE: It is somewhat complicated to have labels on |
| 1283 | // try-statements. When breaking out of a try-finally statement, |
| 1284 | // one must take great care not to treat it as a |
| 1285 | // fall-through. It is much easier just to wrap the entire |
| 1286 | // try-statement in a statement block and put the labels there |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame^] | 1287 | Block* result = new(zone()) Block(isolate(), labels, 1, false); |
Teng-Hui Zhu | 3e5fa29 | 2010-11-09 16:16:48 -0800 | [diff] [blame] | 1288 | Target target(&this->target_stack_, result); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1289 | TryStatement* statement = ParseTryStatement(CHECK_OK); |
| 1290 | if (statement) { |
| 1291 | statement->set_statement_pos(statement_pos); |
| 1292 | } |
| 1293 | if (result) result->AddStatement(statement); |
| 1294 | return result; |
| 1295 | } |
| 1296 | |
Ben Murdoch | e0cee9b | 2011-05-25 10:26:03 +0100 | [diff] [blame] | 1297 | case Token::FUNCTION: { |
| 1298 | // In strict mode, FunctionDeclaration is only allowed in the context |
| 1299 | // of SourceElements. |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 1300 | if (top_scope_->is_strict_mode()) { |
Ben Murdoch | e0cee9b | 2011-05-25 10:26:03 +0100 | [diff] [blame] | 1301 | ReportMessageAt(scanner().peek_location(), "strict_function", |
| 1302 | Vector<const char*>::empty()); |
| 1303 | *ok = false; |
| 1304 | return NULL; |
| 1305 | } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1306 | return ParseFunctionDeclaration(ok); |
Ben Murdoch | e0cee9b | 2011-05-25 10:26:03 +0100 | [diff] [blame] | 1307 | } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1308 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1309 | case Token::DEBUGGER: |
| 1310 | stmt = ParseDebuggerStatement(ok); |
| 1311 | break; |
| 1312 | |
| 1313 | default: |
| 1314 | stmt = ParseExpressionOrLabelledStatement(labels, ok); |
| 1315 | } |
| 1316 | |
| 1317 | // Store the source position of the statement |
| 1318 | if (stmt != NULL) stmt->set_statement_pos(statement_pos); |
| 1319 | return stmt; |
| 1320 | } |
| 1321 | |
| 1322 | |
Teng-Hui Zhu | 3e5fa29 | 2010-11-09 16:16:48 -0800 | [diff] [blame] | 1323 | VariableProxy* Parser::Declare(Handle<String> name, |
| 1324 | Variable::Mode mode, |
| 1325 | FunctionLiteral* fun, |
| 1326 | bool resolve, |
| 1327 | bool* ok) { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1328 | Variable* var = NULL; |
| 1329 | // If we are inside a function, a declaration of a variable |
| 1330 | // is a truly local variable, and the scope of the variable |
| 1331 | // is always the function scope. |
| 1332 | |
| 1333 | // If a function scope exists, then we can statically declare this |
| 1334 | // variable and also set its mode. In any case, a Declaration node |
| 1335 | // will be added to the scope so that the declaration can be added |
| 1336 | // to the corresponding activation frame at runtime if necessary. |
| 1337 | // For instance declarations inside an eval scope need to be added |
| 1338 | // to the calling function context. |
Ben Murdoch | 257744e | 2011-11-30 15:57:28 +0000 | [diff] [blame] | 1339 | // Similarly, strict mode eval scope does not leak variable declarations to |
| 1340 | // the caller's scope so we declare all locals, too. |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame^] | 1341 | Scope* declaration_scope = top_scope_->DeclarationScope(); |
| 1342 | if (declaration_scope->is_function_scope() || |
| 1343 | declaration_scope->is_strict_mode_eval_scope()) { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1344 | // Declare the variable in the function scope. |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame^] | 1345 | var = declaration_scope->LocalLookup(name); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1346 | if (var == NULL) { |
| 1347 | // Declare the name. |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame^] | 1348 | var = declaration_scope->DeclareLocal(name, mode); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1349 | } else { |
| 1350 | // The name was declared before; check for conflicting |
| 1351 | // re-declarations. If the previous declaration was a const or the |
| 1352 | // current declaration is a const then we have a conflict. There is |
| 1353 | // similar code in runtime.cc in the Declare functions. |
| 1354 | if ((mode == Variable::CONST) || (var->mode() == Variable::CONST)) { |
| 1355 | // We only have vars and consts in declarations. |
| 1356 | ASSERT(var->mode() == Variable::VAR || |
| 1357 | var->mode() == Variable::CONST); |
| 1358 | const char* type = (var->mode() == Variable::VAR) ? "var" : "const"; |
| 1359 | Handle<String> type_string = |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 1360 | isolate()->factory()->NewStringFromUtf8(CStrVector(type), TENURED); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1361 | Expression* expression = |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 1362 | NewThrowTypeError(isolate()->factory()->redeclaration_symbol(), |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1363 | type_string, name); |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame^] | 1364 | declaration_scope->SetIllegalRedeclaration(expression); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1365 | } |
| 1366 | } |
| 1367 | } |
| 1368 | |
| 1369 | // We add a declaration node for every declaration. The compiler |
| 1370 | // will only generate code if necessary. In particular, declarations |
| 1371 | // for inner local variables that do not represent functions won't |
| 1372 | // result in any generated code. |
| 1373 | // |
| 1374 | // Note that we always add an unresolved proxy even if it's not |
| 1375 | // used, simply because we don't know in this method (w/o extra |
| 1376 | // parameters) if the proxy is needed or not. The proxy will be |
| 1377 | // bound during variable resolution time unless it was pre-bound |
| 1378 | // below. |
| 1379 | // |
| 1380 | // WARNING: This will lead to multiple declaration nodes for the |
| 1381 | // same variable if it is declared several times. This is not a |
| 1382 | // semantic issue as long as we keep the source order, but it may be |
| 1383 | // a performance issue since it may lead to repeated |
| 1384 | // Runtime::DeclareContextSlot() calls. |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame^] | 1385 | VariableProxy* proxy = declaration_scope->NewUnresolved(name, false); |
| 1386 | declaration_scope->AddDeclaration(new(zone()) Declaration(proxy, mode, fun)); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1387 | |
| 1388 | // For global const variables we bind the proxy to a variable. |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame^] | 1389 | if (mode == Variable::CONST && declaration_scope->is_global_scope()) { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1390 | ASSERT(resolve); // should be set by all callers |
| 1391 | Variable::Kind kind = Variable::NORMAL; |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame^] | 1392 | var = new(zone()) Variable(declaration_scope, |
| 1393 | name, |
| 1394 | Variable::CONST, |
| 1395 | true, |
| 1396 | kind); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1397 | } |
| 1398 | |
| 1399 | // If requested and we have a local variable, bind the proxy to the variable |
| 1400 | // at parse-time. This is used for functions (and consts) declared inside |
| 1401 | // statements: the corresponding function (or const) variable must be in the |
| 1402 | // function scope and not a statement-local scope, e.g. as provided with a |
| 1403 | // 'with' statement: |
| 1404 | // |
| 1405 | // with (obj) { |
| 1406 | // function f() {} |
| 1407 | // } |
| 1408 | // |
| 1409 | // which is translated into: |
| 1410 | // |
| 1411 | // with (obj) { |
| 1412 | // // in this case this is not: 'var f; f = function () {};' |
| 1413 | // var f = function () {}; |
| 1414 | // } |
| 1415 | // |
| 1416 | // Note that if 'f' is accessed from inside the 'with' statement, it |
| 1417 | // will be allocated in the context (because we must be able to look |
| 1418 | // it up dynamically) but it will also be accessed statically, i.e., |
| 1419 | // with a context slot index and a context chain length for this |
| 1420 | // initialization code. Thus, inside the 'with' statement, we need |
| 1421 | // both access to the static and the dynamic context chain; the |
| 1422 | // runtime needs to provide both. |
| 1423 | if (resolve && var != NULL) proxy->BindTo(var); |
| 1424 | |
| 1425 | return proxy; |
| 1426 | } |
| 1427 | |
| 1428 | |
| 1429 | // Language extension which is only enabled for source files loaded |
| 1430 | // through the API's extension mechanism. A native function |
| 1431 | // declaration is resolved by looking up the function through a |
| 1432 | // callback provided by the extension. |
| 1433 | Statement* Parser::ParseNativeDeclaration(bool* ok) { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1434 | Expect(Token::FUNCTION, CHECK_OK); |
| 1435 | Handle<String> name = ParseIdentifier(CHECK_OK); |
| 1436 | Expect(Token::LPAREN, CHECK_OK); |
| 1437 | bool done = (peek() == Token::RPAREN); |
| 1438 | while (!done) { |
| 1439 | ParseIdentifier(CHECK_OK); |
| 1440 | done = (peek() == Token::RPAREN); |
Teng-Hui Zhu | 3e5fa29 | 2010-11-09 16:16:48 -0800 | [diff] [blame] | 1441 | if (!done) { |
| 1442 | Expect(Token::COMMA, CHECK_OK); |
| 1443 | } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1444 | } |
| 1445 | Expect(Token::RPAREN, CHECK_OK); |
| 1446 | Expect(Token::SEMICOLON, CHECK_OK); |
| 1447 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1448 | // Make sure that the function containing the native declaration |
| 1449 | // isn't lazily compiled. The extension structures are only |
| 1450 | // accessible while parsing the first time not when reparsing |
| 1451 | // because of lazy compilation. |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame^] | 1452 | top_scope_->DeclarationScope()->ForceEagerCompilation(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1453 | |
| 1454 | // Compute the function template for the native function. |
| 1455 | v8::Handle<v8::FunctionTemplate> fun_template = |
| 1456 | extension_->GetNativeFunction(v8::Utils::ToLocal(name)); |
| 1457 | ASSERT(!fun_template.IsEmpty()); |
| 1458 | |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 1459 | // Instantiate the function and create a shared function info from it. |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1460 | Handle<JSFunction> fun = Utils::OpenHandle(*fun_template->GetFunction()); |
| 1461 | const int literals = fun->NumberOfLiterals(); |
| 1462 | Handle<Code> code = Handle<Code>(fun->shared()->code()); |
Leon Clarke | e46be81 | 2010-01-19 14:06:41 +0000 | [diff] [blame] | 1463 | Handle<Code> construct_stub = Handle<Code>(fun->shared()->construct_stub()); |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 1464 | Handle<SharedFunctionInfo> shared = |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 1465 | isolate()->factory()->NewSharedFunctionInfo(name, literals, code, |
Ben Murdoch | 3bec4d2 | 2010-07-22 14:51:16 +0100 | [diff] [blame] | 1466 | Handle<SerializedScopeInfo>(fun->shared()->scope_info())); |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 1467 | shared->set_construct_stub(*construct_stub); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1468 | |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 1469 | // Copy the function data to the shared function info. |
| 1470 | shared->set_function_data(fun->shared()->function_data()); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1471 | int parameters = fun->shared()->formal_parameter_count(); |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 1472 | shared->set_formal_parameter_count(parameters); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1473 | |
| 1474 | // TODO(1240846): It's weird that native function declarations are |
| 1475 | // introduced dynamically when we meet their declarations, whereas |
| 1476 | // other functions are setup when entering the surrounding scope. |
Ben Murdoch | 8b112d2 | 2011-06-08 16:22:53 +0100 | [diff] [blame] | 1477 | SharedFunctionInfoLiteral* lit = |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame^] | 1478 | new(zone()) SharedFunctionInfoLiteral(isolate(), shared); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1479 | VariableProxy* var = Declare(name, Variable::VAR, NULL, true, CHECK_OK); |
Ben Murdoch | 8b112d2 | 2011-06-08 16:22:53 +0100 | [diff] [blame] | 1480 | return new(zone()) ExpressionStatement(new(zone()) Assignment( |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame^] | 1481 | isolate(), Token::INIT_VAR, var, lit, RelocInfo::kNoPosition)); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1482 | } |
| 1483 | |
| 1484 | |
| 1485 | Statement* Parser::ParseFunctionDeclaration(bool* ok) { |
| 1486 | // FunctionDeclaration :: |
| 1487 | // 'function' Identifier '(' FormalParameterListopt ')' '{' FunctionBody '}' |
| 1488 | Expect(Token::FUNCTION, CHECK_OK); |
| 1489 | int function_token_position = scanner().location().beg_pos; |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame^] | 1490 | bool is_strict_reserved = false; |
| 1491 | Handle<String> name = ParseIdentifierOrStrictReservedWord( |
| 1492 | &is_strict_reserved, CHECK_OK); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1493 | FunctionLiteral* fun = ParseFunctionLiteral(name, |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame^] | 1494 | is_strict_reserved, |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1495 | function_token_position, |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame^] | 1496 | FunctionLiteral::DECLARATION, |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1497 | CHECK_OK); |
| 1498 | // Even if we're not at the top-level of the global or a function |
| 1499 | // scope, we treat is as such and introduce the function with it's |
| 1500 | // initial value upon entering the corresponding scope. |
| 1501 | Declare(name, Variable::VAR, fun, true, CHECK_OK); |
Teng-Hui Zhu | 3e5fa29 | 2010-11-09 16:16:48 -0800 | [diff] [blame] | 1502 | return EmptyStatement(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1503 | } |
| 1504 | |
| 1505 | |
| 1506 | Block* Parser::ParseBlock(ZoneStringList* labels, bool* ok) { |
| 1507 | // Block :: |
| 1508 | // '{' Statement* '}' |
| 1509 | |
| 1510 | // Note that a Block does not introduce a new execution scope! |
| 1511 | // (ECMA-262, 3rd, 12.2) |
| 1512 | // |
| 1513 | // Construct block expecting 16 statements. |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame^] | 1514 | Block* result = new(zone()) Block(isolate(), labels, 16, false); |
Teng-Hui Zhu | 3e5fa29 | 2010-11-09 16:16:48 -0800 | [diff] [blame] | 1515 | Target target(&this->target_stack_, result); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1516 | Expect(Token::LBRACE, CHECK_OK); |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame^] | 1517 | InitializationBlockFinder block_finder(top_scope_, target_stack_); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1518 | while (peek() != Token::RBRACE) { |
| 1519 | Statement* stat = ParseStatement(NULL, CHECK_OK); |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame^] | 1520 | if (stat && !stat->IsEmpty()) { |
| 1521 | result->AddStatement(stat); |
| 1522 | block_finder.Update(stat); |
| 1523 | } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1524 | } |
| 1525 | Expect(Token::RBRACE, CHECK_OK); |
| 1526 | return result; |
| 1527 | } |
| 1528 | |
| 1529 | |
| 1530 | Block* Parser::ParseVariableStatement(bool* ok) { |
| 1531 | // VariableStatement :: |
| 1532 | // VariableDeclarations ';' |
| 1533 | |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame^] | 1534 | Handle<String> ignore; |
| 1535 | Block* result = ParseVariableDeclarations(true, &ignore, CHECK_OK); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1536 | ExpectSemicolon(CHECK_OK); |
| 1537 | return result; |
| 1538 | } |
| 1539 | |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 1540 | |
| 1541 | bool Parser::IsEvalOrArguments(Handle<String> string) { |
| 1542 | return string.is_identical_to(isolate()->factory()->eval_symbol()) || |
| 1543 | string.is_identical_to(isolate()->factory()->arguments_symbol()); |
Steve Block | 1e0659c | 2011-05-24 12:43:12 +0100 | [diff] [blame] | 1544 | } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1545 | |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 1546 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1547 | // If the variable declaration declares exactly one non-const |
| 1548 | // variable, then *var is set to that variable. In all other cases, |
| 1549 | // *var is untouched; in particular, it is the caller's responsibility |
| 1550 | // to initialize it properly. This mechanism is used for the parsing |
| 1551 | // of 'for-in' loops. |
| 1552 | Block* Parser::ParseVariableDeclarations(bool accept_IN, |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame^] | 1553 | Handle<String>* out, |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1554 | bool* ok) { |
| 1555 | // VariableDeclarations :: |
| 1556 | // ('var' | 'const') (Identifier ('=' AssignmentExpression)?)+[','] |
| 1557 | |
| 1558 | Variable::Mode mode = Variable::VAR; |
| 1559 | bool is_const = false; |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame^] | 1560 | Scope* declaration_scope = top_scope_->DeclarationScope(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1561 | if (peek() == Token::VAR) { |
| 1562 | Consume(Token::VAR); |
| 1563 | } else if (peek() == Token::CONST) { |
| 1564 | Consume(Token::CONST); |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame^] | 1565 | if (declaration_scope->is_strict_mode()) { |
Ben Murdoch | e0cee9b | 2011-05-25 10:26:03 +0100 | [diff] [blame] | 1566 | ReportMessage("strict_const", Vector<const char*>::empty()); |
| 1567 | *ok = false; |
| 1568 | return NULL; |
| 1569 | } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1570 | mode = Variable::CONST; |
| 1571 | is_const = true; |
| 1572 | } else { |
| 1573 | UNREACHABLE(); // by current callers |
| 1574 | } |
| 1575 | |
| 1576 | // The scope of a variable/const declared anywhere inside a function |
| 1577 | // is the entire function (ECMA-262, 3rd, 10.1.3, and 12.2). Thus we can |
| 1578 | // transform a source-level variable/const declaration into a (Function) |
| 1579 | // Scope declaration, and rewrite the source-level initialization into an |
| 1580 | // assignment statement. We use a block to collect multiple assignments. |
| 1581 | // |
| 1582 | // We mark the block as initializer block because we don't want the |
| 1583 | // rewriter to add a '.result' assignment to such a block (to get compliant |
| 1584 | // behavior for code such as print(eval('var x = 7')), and for cosmetic |
| 1585 | // reasons when pretty-printing. Also, unless an assignment (initialization) |
| 1586 | // is inside an initializer block, it is ignored. |
| 1587 | // |
| 1588 | // Create new block with one expected declaration. |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame^] | 1589 | Block* block = new(zone()) Block(isolate(), NULL, 1, true); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1590 | int nvars = 0; // the number of variables declared |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame^] | 1591 | Handle<String> name; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1592 | do { |
Kristian Monsen | 80d68ea | 2010-09-08 11:05:35 +0100 | [diff] [blame] | 1593 | if (fni_ != NULL) fni_->Enter(); |
| 1594 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1595 | // Parse variable name. |
| 1596 | if (nvars > 0) Consume(Token::COMMA); |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame^] | 1597 | name = ParseIdentifier(CHECK_OK); |
Kristian Monsen | 80d68ea | 2010-09-08 11:05:35 +0100 | [diff] [blame] | 1598 | if (fni_ != NULL) fni_->PushVariableName(name); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1599 | |
Steve Block | 1e0659c | 2011-05-24 12:43:12 +0100 | [diff] [blame] | 1600 | // Strict mode variables may not be named eval or arguments |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame^] | 1601 | if (declaration_scope->is_strict_mode() && IsEvalOrArguments(name)) { |
Steve Block | 1e0659c | 2011-05-24 12:43:12 +0100 | [diff] [blame] | 1602 | ReportMessage("strict_var_name", Vector<const char*>::empty()); |
| 1603 | *ok = false; |
| 1604 | return NULL; |
| 1605 | } |
| 1606 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1607 | // Declare variable. |
| 1608 | // Note that we *always* must treat the initial value via a separate init |
| 1609 | // assignment for variables and constants because the value must be assigned |
| 1610 | // when the variable is encountered in the source. But the variable/constant |
| 1611 | // is declared (and set to 'undefined') upon entering the function within |
| 1612 | // which the variable or constant is declared. Only function variables have |
| 1613 | // an initial value in the declaration (because they are initialized upon |
| 1614 | // entering the function). |
| 1615 | // |
| 1616 | // If we have a const declaration, in an inner scope, the proxy is always |
| 1617 | // bound to the declared variable (independent of possibly surrounding with |
| 1618 | // statements). |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame^] | 1619 | Declare(name, mode, NULL, is_const /* always bound for CONST! */, |
| 1620 | CHECK_OK); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1621 | nvars++; |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame^] | 1622 | if (declaration_scope->num_var_or_const() > kMaxNumFunctionLocals) { |
Steve Block | 053d10c | 2011-06-13 19:13:29 +0100 | [diff] [blame] | 1623 | ReportMessageAt(scanner().location(), "too_many_variables", |
| 1624 | Vector<const char*>::empty()); |
| 1625 | *ok = false; |
| 1626 | return NULL; |
| 1627 | } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1628 | |
| 1629 | // Parse initialization expression if present and/or needed. A |
| 1630 | // declaration of the form: |
| 1631 | // |
| 1632 | // var v = x; |
| 1633 | // |
| 1634 | // is syntactic sugar for: |
| 1635 | // |
| 1636 | // var v; v = x; |
| 1637 | // |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame^] | 1638 | // In particular, we need to re-lookup 'v' (in top_scope_, not |
| 1639 | // declaration_scope) as it may be a different 'v' than the 'v' in the |
| 1640 | // declaration (e.g., if we are inside a 'with' statement or 'catch' |
| 1641 | // block). |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1642 | // |
| 1643 | // However, note that const declarations are different! A const |
| 1644 | // declaration of the form: |
| 1645 | // |
| 1646 | // const c = x; |
| 1647 | // |
| 1648 | // is *not* syntactic sugar for: |
| 1649 | // |
| 1650 | // const c; c = x; |
| 1651 | // |
| 1652 | // The "variable" c initialized to x is the same as the declared |
| 1653 | // one - there is no re-lookup (see the last parameter of the |
| 1654 | // Declare() call above). |
| 1655 | |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame^] | 1656 | Scope* initialization_scope = is_const ? declaration_scope : top_scope_; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1657 | Expression* value = NULL; |
| 1658 | int position = -1; |
| 1659 | if (peek() == Token::ASSIGN) { |
| 1660 | Expect(Token::ASSIGN, CHECK_OK); |
| 1661 | position = scanner().location().beg_pos; |
| 1662 | value = ParseAssignmentExpression(accept_IN, CHECK_OK); |
Kristian Monsen | 80d68ea | 2010-09-08 11:05:35 +0100 | [diff] [blame] | 1663 | // Don't infer if it is "a = function(){...}();"-like expression. |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame^] | 1664 | if (fni_ != NULL && |
| 1665 | value->AsCall() == NULL && |
| 1666 | value->AsCallNew() == NULL) { |
| 1667 | fni_->Infer(); |
| 1668 | } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1669 | } |
| 1670 | |
| 1671 | // Make sure that 'const c' actually initializes 'c' to undefined |
| 1672 | // even though it seems like a stupid thing to do. |
| 1673 | if (value == NULL && is_const) { |
| 1674 | value = GetLiteralUndefined(); |
| 1675 | } |
| 1676 | |
| 1677 | // Global variable declarations must be compiled in a specific |
| 1678 | // way. When the script containing the global variable declaration |
| 1679 | // is entered, the global variable must be declared, so that if it |
| 1680 | // doesn't exist (not even in a prototype of the global object) it |
| 1681 | // gets created with an initial undefined value. This is handled |
| 1682 | // by the declarations part of the function representing the |
| 1683 | // top-level global code; see Runtime::DeclareGlobalVariable. If |
| 1684 | // it already exists (in the object or in a prototype), it is |
| 1685 | // *not* touched until the variable declaration statement is |
| 1686 | // executed. |
| 1687 | // |
| 1688 | // Executing the variable declaration statement will always |
| 1689 | // guarantee to give the global object a "local" variable; a |
| 1690 | // variable defined in the global object and not in any |
| 1691 | // prototype. This way, global variable declarations can shadow |
| 1692 | // properties in the prototype chain, but only after the variable |
| 1693 | // declaration statement has been executed. This is important in |
| 1694 | // browsers where the global object (window) has lots of |
| 1695 | // properties defined in prototype objects. |
| 1696 | |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame^] | 1697 | if (initialization_scope->is_global_scope()) { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1698 | // Compute the arguments for the runtime call. |
Ben Murdoch | 257744e | 2011-11-30 15:57:28 +0000 | [diff] [blame] | 1699 | ZoneList<Expression*>* arguments = new(zone()) ZoneList<Expression*>(3); |
Ben Murdoch | 8b112d2 | 2011-06-08 16:22:53 +0100 | [diff] [blame] | 1700 | // We have at least 1 parameter. |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame^] | 1701 | arguments->Add(NewLiteral(name)); |
Ben Murdoch | e0cee9b | 2011-05-25 10:26:03 +0100 | [diff] [blame] | 1702 | CallRuntime* initialize; |
| 1703 | |
| 1704 | if (is_const) { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1705 | arguments->Add(value); |
| 1706 | value = NULL; // zap the value to avoid the unnecessary assignment |
Ben Murdoch | e0cee9b | 2011-05-25 10:26:03 +0100 | [diff] [blame] | 1707 | |
| 1708 | // Construct the call to Runtime_InitializeConstGlobal |
| 1709 | // and add it to the initialization statement block. |
| 1710 | // Note that the function does different things depending on |
| 1711 | // the number of arguments (1 or 2). |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1712 | initialize = |
Ben Murdoch | 8b112d2 | 2011-06-08 16:22:53 +0100 | [diff] [blame] | 1713 | new(zone()) CallRuntime( |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame^] | 1714 | isolate(), |
| 1715 | isolate()->factory()->InitializeConstGlobal_symbol(), |
| 1716 | Runtime::FunctionForId(Runtime::kInitializeConstGlobal), |
| 1717 | arguments); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1718 | } else { |
Ben Murdoch | e0cee9b | 2011-05-25 10:26:03 +0100 | [diff] [blame] | 1719 | // Add strict mode. |
| 1720 | // We may want to pass singleton to avoid Literal allocations. |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame^] | 1721 | StrictModeFlag flag = initialization_scope->is_strict_mode() |
| 1722 | ? kStrictMode |
| 1723 | : kNonStrictMode; |
| 1724 | arguments->Add(NewNumberLiteral(flag)); |
Ben Murdoch | e0cee9b | 2011-05-25 10:26:03 +0100 | [diff] [blame] | 1725 | |
| 1726 | // Be careful not to assign a value to the global variable if |
| 1727 | // we're in a with. The initialization value should not |
| 1728 | // necessarily be stored in the global object in that case, |
| 1729 | // which is why we need to generate a separate assignment node. |
| 1730 | if (value != NULL && !inside_with()) { |
| 1731 | arguments->Add(value); |
| 1732 | value = NULL; // zap the value to avoid the unnecessary assignment |
| 1733 | } |
| 1734 | |
| 1735 | // Construct the call to Runtime_InitializeVarGlobal |
| 1736 | // and add it to the initialization statement block. |
| 1737 | // Note that the function does different things depending on |
| 1738 | // the number of arguments (2 or 3). |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1739 | initialize = |
Ben Murdoch | 8b112d2 | 2011-06-08 16:22:53 +0100 | [diff] [blame] | 1740 | new(zone()) CallRuntime( |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame^] | 1741 | isolate(), |
| 1742 | isolate()->factory()->InitializeVarGlobal_symbol(), |
| 1743 | Runtime::FunctionForId(Runtime::kInitializeVarGlobal), |
| 1744 | arguments); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1745 | } |
Ben Murdoch | e0cee9b | 2011-05-25 10:26:03 +0100 | [diff] [blame] | 1746 | |
Ben Murdoch | 8b112d2 | 2011-06-08 16:22:53 +0100 | [diff] [blame] | 1747 | block->AddStatement(new(zone()) ExpressionStatement(initialize)); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1748 | } |
| 1749 | |
| 1750 | // Add an assignment node to the initialization statement block if |
| 1751 | // we still have a pending initialization value. We must distinguish |
| 1752 | // between variables and constants: Variable initializations are simply |
| 1753 | // assignments (with all the consequences if they are inside a 'with' |
| 1754 | // statement - they may change a 'with' object property). Constant |
| 1755 | // initializations always assign to the declared constant which is |
| 1756 | // always at the function scope level. This is only relevant for |
| 1757 | // dynamically looked-up variables and constants (the start context |
| 1758 | // for constant lookups is always the function context, while it is |
| 1759 | // the top context for variables). Sigh... |
| 1760 | if (value != NULL) { |
| 1761 | Token::Value op = (is_const ? Token::INIT_CONST : Token::INIT_VAR); |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame^] | 1762 | bool in_with = is_const ? false : inside_with(); |
| 1763 | VariableProxy* proxy = |
| 1764 | initialization_scope->NewUnresolved(name, in_with); |
Ben Murdoch | 8b112d2 | 2011-06-08 16:22:53 +0100 | [diff] [blame] | 1765 | Assignment* assignment = |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame^] | 1766 | new(zone()) Assignment(isolate(), op, proxy, value, position); |
Ben Murdoch | 8b112d2 | 2011-06-08 16:22:53 +0100 | [diff] [blame] | 1767 | if (block) { |
| 1768 | block->AddStatement(new(zone()) ExpressionStatement(assignment)); |
| 1769 | } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1770 | } |
Kristian Monsen | 80d68ea | 2010-09-08 11:05:35 +0100 | [diff] [blame] | 1771 | |
| 1772 | if (fni_ != NULL) fni_->Leave(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1773 | } while (peek() == Token::COMMA); |
| 1774 | |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame^] | 1775 | // If there was a single non-const declaration, return it in the output |
| 1776 | // parameter for possible use by for/in. |
| 1777 | if (nvars == 1 && !is_const) { |
| 1778 | *out = name; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1779 | } |
| 1780 | |
| 1781 | return block; |
| 1782 | } |
| 1783 | |
| 1784 | |
| 1785 | static bool ContainsLabel(ZoneStringList* labels, Handle<String> label) { |
| 1786 | ASSERT(!label.is_null()); |
| 1787 | if (labels != NULL) |
| 1788 | for (int i = labels->length(); i-- > 0; ) |
| 1789 | if (labels->at(i).is_identical_to(label)) |
| 1790 | return true; |
| 1791 | |
| 1792 | return false; |
| 1793 | } |
| 1794 | |
| 1795 | |
| 1796 | Statement* Parser::ParseExpressionOrLabelledStatement(ZoneStringList* labels, |
| 1797 | bool* ok) { |
| 1798 | // ExpressionStatement | LabelledStatement :: |
| 1799 | // Expression ';' |
| 1800 | // Identifier ':' Statement |
Steve Block | 1e0659c | 2011-05-24 12:43:12 +0100 | [diff] [blame] | 1801 | bool starts_with_idenfifier = peek_any_identifier(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1802 | Expression* expr = ParseExpression(true, CHECK_OK); |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame^] | 1803 | if (peek() == Token::COLON && starts_with_idenfifier && expr != NULL && |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1804 | expr->AsVariableProxy() != NULL && |
| 1805 | !expr->AsVariableProxy()->is_this()) { |
Shimeng (Simon) Wang | 8a31eba | 2010-12-06 19:01:33 -0800 | [diff] [blame] | 1806 | // Expression is a single identifier, and not, e.g., a parenthesized |
| 1807 | // identifier. |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1808 | VariableProxy* var = expr->AsVariableProxy(); |
| 1809 | Handle<String> label = var->name(); |
| 1810 | // TODO(1240780): We don't check for redeclaration of labels |
| 1811 | // during preparsing since keeping track of the set of active |
| 1812 | // labels requires nontrivial changes to the way scopes are |
| 1813 | // structured. However, these are probably changes we want to |
| 1814 | // make later anyway so we should go back and fix this then. |
Teng-Hui Zhu | 3e5fa29 | 2010-11-09 16:16:48 -0800 | [diff] [blame] | 1815 | if (ContainsLabel(labels, label) || TargetStackContainsLabel(label)) { |
| 1816 | SmartPointer<char> c_string = label->ToCString(DISALLOW_NULLS); |
| 1817 | const char* elms[2] = { "Label", *c_string }; |
| 1818 | Vector<const char*> args(elms, 2); |
| 1819 | ReportMessage("redeclaration", args); |
| 1820 | *ok = false; |
| 1821 | return NULL; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1822 | } |
Ben Murdoch | 257744e | 2011-11-30 15:57:28 +0000 | [diff] [blame] | 1823 | if (labels == NULL) labels = new(zone()) ZoneStringList(4); |
Teng-Hui Zhu | 3e5fa29 | 2010-11-09 16:16:48 -0800 | [diff] [blame] | 1824 | labels->Add(label); |
| 1825 | // Remove the "ghost" variable that turned out to be a label |
| 1826 | // from the top scope. This way, we don't try to resolve it |
| 1827 | // during the scope processing. |
| 1828 | top_scope_->RemoveUnresolved(var); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1829 | Expect(Token::COLON, CHECK_OK); |
| 1830 | return ParseStatement(labels, ok); |
| 1831 | } |
| 1832 | |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame^] | 1833 | // If we have an extension, we allow a native function declaration. |
| 1834 | // A native function declaration starts with "native function" with |
| 1835 | // no line-terminator between the two words. |
| 1836 | if (extension_ != NULL && |
| 1837 | peek() == Token::FUNCTION && |
| 1838 | !scanner().HasAnyLineTerminatorBeforeNext() && |
| 1839 | expr != NULL && |
| 1840 | expr->AsVariableProxy() != NULL && |
| 1841 | expr->AsVariableProxy()->name()->Equals( |
| 1842 | isolate()->heap()->native_symbol()) && |
| 1843 | !scanner().literal_contains_escapes()) { |
| 1844 | return ParseNativeDeclaration(ok); |
| 1845 | } |
| 1846 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1847 | // Parsed expression statement. |
| 1848 | ExpectSemicolon(CHECK_OK); |
Ben Murdoch | 8b112d2 | 2011-06-08 16:22:53 +0100 | [diff] [blame] | 1849 | return new(zone()) ExpressionStatement(expr); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1850 | } |
| 1851 | |
| 1852 | |
| 1853 | IfStatement* Parser::ParseIfStatement(ZoneStringList* labels, bool* ok) { |
| 1854 | // IfStatement :: |
| 1855 | // 'if' '(' Expression ')' Statement ('else' Statement)? |
| 1856 | |
| 1857 | Expect(Token::IF, CHECK_OK); |
| 1858 | Expect(Token::LPAREN, CHECK_OK); |
| 1859 | Expression* condition = ParseExpression(true, CHECK_OK); |
| 1860 | Expect(Token::RPAREN, CHECK_OK); |
| 1861 | Statement* then_statement = ParseStatement(labels, CHECK_OK); |
| 1862 | Statement* else_statement = NULL; |
| 1863 | if (peek() == Token::ELSE) { |
| 1864 | Next(); |
| 1865 | else_statement = ParseStatement(labels, CHECK_OK); |
Teng-Hui Zhu | 3e5fa29 | 2010-11-09 16:16:48 -0800 | [diff] [blame] | 1866 | } else { |
| 1867 | else_statement = EmptyStatement(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1868 | } |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame^] | 1869 | return new(zone()) IfStatement( |
| 1870 | isolate(), condition, then_statement, else_statement); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1871 | } |
| 1872 | |
| 1873 | |
| 1874 | Statement* Parser::ParseContinueStatement(bool* ok) { |
| 1875 | // ContinueStatement :: |
| 1876 | // 'continue' Identifier? ';' |
| 1877 | |
| 1878 | Expect(Token::CONTINUE, CHECK_OK); |
| 1879 | Handle<String> label = Handle<String>::null(); |
| 1880 | Token::Value tok = peek(); |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame^] | 1881 | if (!scanner().HasAnyLineTerminatorBeforeNext() && |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1882 | tok != Token::SEMICOLON && tok != Token::RBRACE && tok != Token::EOS) { |
| 1883 | label = ParseIdentifier(CHECK_OK); |
| 1884 | } |
| 1885 | IterationStatement* target = NULL; |
Teng-Hui Zhu | 3e5fa29 | 2010-11-09 16:16:48 -0800 | [diff] [blame] | 1886 | target = LookupContinueTarget(label, CHECK_OK); |
| 1887 | if (target == NULL) { |
Ben Murdoch | b8e0da2 | 2011-05-16 14:20:40 +0100 | [diff] [blame] | 1888 | // Illegal continue statement. |
| 1889 | const char* message = "illegal_continue"; |
| 1890 | Vector<Handle<String> > args; |
| 1891 | if (!label.is_null()) { |
| 1892 | message = "unknown_label"; |
| 1893 | args = Vector<Handle<String> >(&label, 1); |
| 1894 | } |
| 1895 | ReportMessageAt(scanner().location(), message, args); |
| 1896 | *ok = false; |
| 1897 | return NULL; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1898 | } |
| 1899 | ExpectSemicolon(CHECK_OK); |
Ben Murdoch | 8b112d2 | 2011-06-08 16:22:53 +0100 | [diff] [blame] | 1900 | return new(zone()) ContinueStatement(target); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1901 | } |
| 1902 | |
| 1903 | |
| 1904 | Statement* Parser::ParseBreakStatement(ZoneStringList* labels, bool* ok) { |
| 1905 | // BreakStatement :: |
| 1906 | // 'break' Identifier? ';' |
| 1907 | |
| 1908 | Expect(Token::BREAK, CHECK_OK); |
| 1909 | Handle<String> label; |
| 1910 | Token::Value tok = peek(); |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame^] | 1911 | if (!scanner().HasAnyLineTerminatorBeforeNext() && |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1912 | tok != Token::SEMICOLON && tok != Token::RBRACE && tok != Token::EOS) { |
| 1913 | label = ParseIdentifier(CHECK_OK); |
| 1914 | } |
| 1915 | // Parse labeled break statements that target themselves into |
| 1916 | // empty statements, e.g. 'l1: l2: l3: break l2;' |
| 1917 | if (!label.is_null() && ContainsLabel(labels, label)) { |
Teng-Hui Zhu | 3e5fa29 | 2010-11-09 16:16:48 -0800 | [diff] [blame] | 1918 | return EmptyStatement(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1919 | } |
| 1920 | BreakableStatement* target = NULL; |
Teng-Hui Zhu | 3e5fa29 | 2010-11-09 16:16:48 -0800 | [diff] [blame] | 1921 | target = LookupBreakTarget(label, CHECK_OK); |
| 1922 | if (target == NULL) { |
Ben Murdoch | b8e0da2 | 2011-05-16 14:20:40 +0100 | [diff] [blame] | 1923 | // Illegal break statement. |
| 1924 | const char* message = "illegal_break"; |
| 1925 | Vector<Handle<String> > args; |
| 1926 | if (!label.is_null()) { |
| 1927 | message = "unknown_label"; |
| 1928 | args = Vector<Handle<String> >(&label, 1); |
| 1929 | } |
| 1930 | ReportMessageAt(scanner().location(), message, args); |
| 1931 | *ok = false; |
| 1932 | return NULL; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1933 | } |
| 1934 | ExpectSemicolon(CHECK_OK); |
Ben Murdoch | 8b112d2 | 2011-06-08 16:22:53 +0100 | [diff] [blame] | 1935 | return new(zone()) BreakStatement(target); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1936 | } |
| 1937 | |
| 1938 | |
| 1939 | Statement* Parser::ParseReturnStatement(bool* ok) { |
| 1940 | // ReturnStatement :: |
| 1941 | // 'return' Expression? ';' |
| 1942 | |
| 1943 | // Consume the return token. It is necessary to do the before |
| 1944 | // reporting any errors on it, because of the way errors are |
| 1945 | // reported (underlining). |
| 1946 | Expect(Token::RETURN, CHECK_OK); |
| 1947 | |
| 1948 | // An ECMAScript program is considered syntactically incorrect if it |
| 1949 | // contains a return statement that is not within the body of a |
| 1950 | // function. See ECMA-262, section 12.9, page 67. |
| 1951 | // |
| 1952 | // To be consistent with KJS we report the syntax error at runtime. |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame^] | 1953 | Scope* declaration_scope = top_scope_->DeclarationScope(); |
| 1954 | if (declaration_scope->is_global_scope() || |
| 1955 | declaration_scope->is_eval_scope()) { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 1956 | Handle<String> type = isolate()->factory()->illegal_return_symbol(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1957 | Expression* throw_error = NewThrowSyntaxError(type, Handle<Object>::null()); |
Ben Murdoch | 8b112d2 | 2011-06-08 16:22:53 +0100 | [diff] [blame] | 1958 | return new(zone()) ExpressionStatement(throw_error); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1959 | } |
| 1960 | |
| 1961 | Token::Value tok = peek(); |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame^] | 1962 | if (scanner().HasAnyLineTerminatorBeforeNext() || |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1963 | tok == Token::SEMICOLON || |
| 1964 | tok == Token::RBRACE || |
| 1965 | tok == Token::EOS) { |
| 1966 | ExpectSemicolon(CHECK_OK); |
Ben Murdoch | 8b112d2 | 2011-06-08 16:22:53 +0100 | [diff] [blame] | 1967 | return new(zone()) ReturnStatement(GetLiteralUndefined()); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1968 | } |
| 1969 | |
| 1970 | Expression* expr = ParseExpression(true, CHECK_OK); |
| 1971 | ExpectSemicolon(CHECK_OK); |
Ben Murdoch | 8b112d2 | 2011-06-08 16:22:53 +0100 | [diff] [blame] | 1972 | return new(zone()) ReturnStatement(expr); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1973 | } |
| 1974 | |
| 1975 | |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame^] | 1976 | Block* Parser::WithHelper(Expression* obj, ZoneStringList* labels, bool* ok) { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1977 | // Parse the statement and collect escaping labels. |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame^] | 1978 | TargetCollector collector; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1979 | Statement* stat; |
Teng-Hui Zhu | 3e5fa29 | 2010-11-09 16:16:48 -0800 | [diff] [blame] | 1980 | { Target target(&this->target_stack_, &collector); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1981 | with_nesting_level_++; |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame^] | 1982 | top_scope_->DeclarationScope()->RecordWithStatement(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1983 | stat = ParseStatement(labels, CHECK_OK); |
| 1984 | with_nesting_level_--; |
| 1985 | } |
| 1986 | // Create resulting block with two statements. |
| 1987 | // 1: Evaluate the with expression. |
| 1988 | // 2: The try-finally block evaluating the body. |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame^] | 1989 | Block* result = new(zone()) Block(isolate(), NULL, 2, false); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1990 | |
| 1991 | if (result != NULL) { |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame^] | 1992 | result->AddStatement(new(zone()) EnterWithContextStatement(obj)); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1993 | |
| 1994 | // Create body block. |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame^] | 1995 | Block* body = new(zone()) Block(isolate(), NULL, 1, false); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1996 | body->AddStatement(stat); |
| 1997 | |
| 1998 | // Create exit block. |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame^] | 1999 | Block* exit = new(zone()) Block(isolate(), NULL, 1, false); |
| 2000 | exit->AddStatement(new(zone()) ExitContextStatement()); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2001 | |
| 2002 | // Return a try-finally statement. |
Ben Murdoch | 8b112d2 | 2011-06-08 16:22:53 +0100 | [diff] [blame] | 2003 | TryFinallyStatement* wrapper = new(zone()) TryFinallyStatement(body, exit); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2004 | wrapper->set_escaping_targets(collector.targets()); |
| 2005 | result->AddStatement(wrapper); |
| 2006 | } |
| 2007 | return result; |
| 2008 | } |
| 2009 | |
| 2010 | |
| 2011 | Statement* Parser::ParseWithStatement(ZoneStringList* labels, bool* ok) { |
| 2012 | // WithStatement :: |
| 2013 | // 'with' '(' Expression ')' Statement |
| 2014 | |
| 2015 | Expect(Token::WITH, CHECK_OK); |
Steve Block | 1e0659c | 2011-05-24 12:43:12 +0100 | [diff] [blame] | 2016 | |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 2017 | if (top_scope_->is_strict_mode()) { |
Steve Block | 1e0659c | 2011-05-24 12:43:12 +0100 | [diff] [blame] | 2018 | ReportMessage("strict_mode_with", Vector<const char*>::empty()); |
| 2019 | *ok = false; |
| 2020 | return NULL; |
| 2021 | } |
| 2022 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2023 | Expect(Token::LPAREN, CHECK_OK); |
| 2024 | Expression* expr = ParseExpression(true, CHECK_OK); |
| 2025 | Expect(Token::RPAREN, CHECK_OK); |
| 2026 | |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame^] | 2027 | return WithHelper(expr, labels, CHECK_OK); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2028 | } |
| 2029 | |
| 2030 | |
| 2031 | CaseClause* Parser::ParseCaseClause(bool* default_seen_ptr, bool* ok) { |
| 2032 | // CaseClause :: |
| 2033 | // 'case' Expression ':' Statement* |
| 2034 | // 'default' ':' Statement* |
| 2035 | |
| 2036 | Expression* label = NULL; // NULL expression indicates default case |
| 2037 | if (peek() == Token::CASE) { |
| 2038 | Expect(Token::CASE, CHECK_OK); |
| 2039 | label = ParseExpression(true, CHECK_OK); |
| 2040 | } else { |
| 2041 | Expect(Token::DEFAULT, CHECK_OK); |
| 2042 | if (*default_seen_ptr) { |
| 2043 | ReportMessage("multiple_defaults_in_switch", |
| 2044 | Vector<const char*>::empty()); |
| 2045 | *ok = false; |
| 2046 | return NULL; |
| 2047 | } |
| 2048 | *default_seen_ptr = true; |
| 2049 | } |
| 2050 | Expect(Token::COLON, CHECK_OK); |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 2051 | int pos = scanner().location().beg_pos; |
Ben Murdoch | 257744e | 2011-11-30 15:57:28 +0000 | [diff] [blame] | 2052 | ZoneList<Statement*>* statements = new(zone()) ZoneList<Statement*>(5); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2053 | while (peek() != Token::CASE && |
| 2054 | peek() != Token::DEFAULT && |
| 2055 | peek() != Token::RBRACE) { |
| 2056 | Statement* stat = ParseStatement(NULL, CHECK_OK); |
Teng-Hui Zhu | 3e5fa29 | 2010-11-09 16:16:48 -0800 | [diff] [blame] | 2057 | statements->Add(stat); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2058 | } |
| 2059 | |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame^] | 2060 | return new(zone()) CaseClause(isolate(), label, statements, pos); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2061 | } |
| 2062 | |
| 2063 | |
| 2064 | SwitchStatement* Parser::ParseSwitchStatement(ZoneStringList* labels, |
| 2065 | bool* ok) { |
| 2066 | // SwitchStatement :: |
| 2067 | // 'switch' '(' Expression ')' '{' CaseClause* '}' |
| 2068 | |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame^] | 2069 | SwitchStatement* statement = new(zone()) SwitchStatement(isolate(), labels); |
Teng-Hui Zhu | 3e5fa29 | 2010-11-09 16:16:48 -0800 | [diff] [blame] | 2070 | Target target(&this->target_stack_, statement); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2071 | |
| 2072 | Expect(Token::SWITCH, CHECK_OK); |
| 2073 | Expect(Token::LPAREN, CHECK_OK); |
| 2074 | Expression* tag = ParseExpression(true, CHECK_OK); |
| 2075 | Expect(Token::RPAREN, CHECK_OK); |
| 2076 | |
| 2077 | bool default_seen = false; |
Ben Murdoch | 257744e | 2011-11-30 15:57:28 +0000 | [diff] [blame] | 2078 | ZoneList<CaseClause*>* cases = new(zone()) ZoneList<CaseClause*>(4); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2079 | Expect(Token::LBRACE, CHECK_OK); |
| 2080 | while (peek() != Token::RBRACE) { |
| 2081 | CaseClause* clause = ParseCaseClause(&default_seen, CHECK_OK); |
Teng-Hui Zhu | 3e5fa29 | 2010-11-09 16:16:48 -0800 | [diff] [blame] | 2082 | cases->Add(clause); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2083 | } |
| 2084 | Expect(Token::RBRACE, CHECK_OK); |
| 2085 | |
Teng-Hui Zhu | 3e5fa29 | 2010-11-09 16:16:48 -0800 | [diff] [blame] | 2086 | if (statement) statement->Initialize(tag, cases); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2087 | return statement; |
| 2088 | } |
| 2089 | |
| 2090 | |
| 2091 | Statement* Parser::ParseThrowStatement(bool* ok) { |
| 2092 | // ThrowStatement :: |
| 2093 | // 'throw' Expression ';' |
| 2094 | |
| 2095 | Expect(Token::THROW, CHECK_OK); |
| 2096 | int pos = scanner().location().beg_pos; |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame^] | 2097 | if (scanner().HasAnyLineTerminatorBeforeNext()) { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2098 | ReportMessage("newline_after_throw", Vector<const char*>::empty()); |
| 2099 | *ok = false; |
| 2100 | return NULL; |
| 2101 | } |
| 2102 | Expression* exception = ParseExpression(true, CHECK_OK); |
| 2103 | ExpectSemicolon(CHECK_OK); |
| 2104 | |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame^] | 2105 | return new(zone()) ExpressionStatement( |
| 2106 | new(zone()) Throw(isolate(), exception, pos)); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2107 | } |
| 2108 | |
| 2109 | |
| 2110 | TryStatement* Parser::ParseTryStatement(bool* ok) { |
| 2111 | // TryStatement :: |
| 2112 | // 'try' Block Catch |
| 2113 | // 'try' Block Finally |
| 2114 | // 'try' Block Catch Finally |
| 2115 | // |
| 2116 | // Catch :: |
| 2117 | // 'catch' '(' Identifier ')' Block |
| 2118 | // |
| 2119 | // Finally :: |
| 2120 | // 'finally' Block |
| 2121 | |
| 2122 | Expect(Token::TRY, CHECK_OK); |
| 2123 | |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame^] | 2124 | TargetCollector try_collector; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2125 | Block* try_block; |
| 2126 | |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame^] | 2127 | { Target target(&this->target_stack_, &try_collector); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2128 | try_block = ParseBlock(NULL, CHECK_OK); |
| 2129 | } |
| 2130 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2131 | Token::Value tok = peek(); |
| 2132 | if (tok != Token::CATCH && tok != Token::FINALLY) { |
| 2133 | ReportMessage("no_catch_or_finally", Vector<const char*>::empty()); |
| 2134 | *ok = false; |
| 2135 | return NULL; |
| 2136 | } |
| 2137 | |
| 2138 | // If we can break out from the catch block and there is a finally block, |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame^] | 2139 | // then we will need to collect escaping targets from the catch |
| 2140 | // block. Since we don't know yet if there will be a finally block, we |
| 2141 | // always collect the targets. |
| 2142 | TargetCollector catch_collector; |
| 2143 | Scope* catch_scope = NULL; |
| 2144 | Variable* catch_variable = NULL; |
| 2145 | Block* catch_block = NULL; |
| 2146 | Handle<String> name; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2147 | if (tok == Token::CATCH) { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2148 | Consume(Token::CATCH); |
| 2149 | |
| 2150 | Expect(Token::LPAREN, CHECK_OK); |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame^] | 2151 | name = ParseIdentifier(CHECK_OK); |
Steve Block | 1e0659c | 2011-05-24 12:43:12 +0100 | [diff] [blame] | 2152 | |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 2153 | if (top_scope_->is_strict_mode() && IsEvalOrArguments(name)) { |
Steve Block | 1e0659c | 2011-05-24 12:43:12 +0100 | [diff] [blame] | 2154 | ReportMessage("strict_catch_variable", Vector<const char*>::empty()); |
| 2155 | *ok = false; |
| 2156 | return NULL; |
| 2157 | } |
| 2158 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2159 | Expect(Token::RPAREN, CHECK_OK); |
| 2160 | |
| 2161 | if (peek() == Token::LBRACE) { |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame^] | 2162 | // Rewrite the catch body B to a single statement block |
| 2163 | // { try B finally { PopContext }}. |
| 2164 | Block* inner_body; |
| 2165 | // We need to collect escapes from the body for both the inner |
| 2166 | // try/finally used to pop the catch context and any possible outer |
| 2167 | // try/finally. |
| 2168 | TargetCollector inner_collector; |
Teng-Hui Zhu | 3e5fa29 | 2010-11-09 16:16:48 -0800 | [diff] [blame] | 2169 | { Target target(&this->target_stack_, &catch_collector); |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame^] | 2170 | { Target target(&this->target_stack_, &inner_collector); |
| 2171 | catch_scope = NewScope(top_scope_, Scope::CATCH_SCOPE, inside_with()); |
| 2172 | if (top_scope_->is_strict_mode()) { |
| 2173 | catch_scope->EnableStrictMode(); |
| 2174 | } |
| 2175 | catch_variable = catch_scope->DeclareLocal(name, Variable::VAR); |
| 2176 | |
| 2177 | Scope* saved_scope = top_scope_; |
| 2178 | top_scope_ = catch_scope; |
| 2179 | inner_body = ParseBlock(NULL, CHECK_OK); |
| 2180 | top_scope_ = saved_scope; |
| 2181 | } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2182 | } |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame^] | 2183 | |
| 2184 | // Create exit block. |
| 2185 | Block* inner_finally = new(zone()) Block(isolate(), NULL, 1, false); |
| 2186 | inner_finally->AddStatement(new(zone()) ExitContextStatement()); |
| 2187 | |
| 2188 | // Create a try/finally statement. |
| 2189 | TryFinallyStatement* inner_try_finally = |
| 2190 | new(zone()) TryFinallyStatement(inner_body, inner_finally); |
| 2191 | inner_try_finally->set_escaping_targets(inner_collector.targets()); |
| 2192 | |
| 2193 | catch_block = new(zone()) Block(isolate(), NULL, 1, false); |
| 2194 | catch_block->AddStatement(inner_try_finally); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2195 | } else { |
| 2196 | Expect(Token::LBRACE, CHECK_OK); |
| 2197 | } |
| 2198 | |
| 2199 | tok = peek(); |
| 2200 | } |
| 2201 | |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame^] | 2202 | Block* finally_block = NULL; |
| 2203 | if (tok == Token::FINALLY || catch_block == NULL) { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2204 | Consume(Token::FINALLY); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2205 | finally_block = ParseBlock(NULL, CHECK_OK); |
| 2206 | } |
| 2207 | |
| 2208 | // Simplify the AST nodes by converting: |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame^] | 2209 | // 'try B0 catch B1 finally B2' |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2210 | // to: |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame^] | 2211 | // 'try { try B0 catch B1 } finally B2' |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2212 | |
Teng-Hui Zhu | 3e5fa29 | 2010-11-09 16:16:48 -0800 | [diff] [blame] | 2213 | if (catch_block != NULL && finally_block != NULL) { |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame^] | 2214 | // If we have both, create an inner try/catch. |
| 2215 | ASSERT(catch_scope != NULL && catch_variable != NULL); |
Steve Block | 3ce2e20 | 2009-11-05 08:53:23 +0000 | [diff] [blame] | 2216 | TryCatchStatement* statement = |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame^] | 2217 | new(zone()) TryCatchStatement(try_block, |
| 2218 | catch_scope, |
| 2219 | catch_variable, |
| 2220 | catch_block); |
| 2221 | statement->set_escaping_targets(try_collector.targets()); |
| 2222 | try_block = new(zone()) Block(isolate(), NULL, 1, false); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2223 | try_block->AddStatement(statement); |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame^] | 2224 | catch_block = NULL; // Clear to indicate it's been handled. |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2225 | } |
| 2226 | |
| 2227 | TryStatement* result = NULL; |
Teng-Hui Zhu | 3e5fa29 | 2010-11-09 16:16:48 -0800 | [diff] [blame] | 2228 | if (catch_block != NULL) { |
| 2229 | ASSERT(finally_block == NULL); |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame^] | 2230 | ASSERT(catch_scope != NULL && catch_variable != NULL); |
Ben Murdoch | 8b112d2 | 2011-06-08 16:22:53 +0100 | [diff] [blame] | 2231 | result = |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame^] | 2232 | new(zone()) TryCatchStatement(try_block, |
| 2233 | catch_scope, |
| 2234 | catch_variable, |
| 2235 | catch_block); |
Teng-Hui Zhu | 3e5fa29 | 2010-11-09 16:16:48 -0800 | [diff] [blame] | 2236 | } else { |
| 2237 | ASSERT(finally_block != NULL); |
Ben Murdoch | 8b112d2 | 2011-06-08 16:22:53 +0100 | [diff] [blame] | 2238 | result = new(zone()) TryFinallyStatement(try_block, finally_block); |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame^] | 2239 | // Combine the jump targets of the try block and the possible catch block. |
| 2240 | try_collector.targets()->AddAll(*catch_collector.targets()); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2241 | } |
| 2242 | |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame^] | 2243 | result->set_escaping_targets(try_collector.targets()); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2244 | return result; |
| 2245 | } |
| 2246 | |
| 2247 | |
Steve Block | 3ce2e20 | 2009-11-05 08:53:23 +0000 | [diff] [blame] | 2248 | DoWhileStatement* Parser::ParseDoWhileStatement(ZoneStringList* labels, |
| 2249 | bool* ok) { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2250 | // DoStatement :: |
| 2251 | // 'do' Statement 'while' '(' Expression ')' ';' |
| 2252 | |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame^] | 2253 | DoWhileStatement* loop = new(zone()) DoWhileStatement(isolate(), labels); |
Teng-Hui Zhu | 3e5fa29 | 2010-11-09 16:16:48 -0800 | [diff] [blame] | 2254 | Target target(&this->target_stack_, loop); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2255 | |
| 2256 | Expect(Token::DO, CHECK_OK); |
| 2257 | Statement* body = ParseStatement(NULL, CHECK_OK); |
| 2258 | Expect(Token::WHILE, CHECK_OK); |
| 2259 | Expect(Token::LPAREN, CHECK_OK); |
Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 2260 | |
| 2261 | if (loop != NULL) { |
| 2262 | int position = scanner().location().beg_pos; |
| 2263 | loop->set_condition_position(position); |
| 2264 | } |
| 2265 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2266 | Expression* cond = ParseExpression(true, CHECK_OK); |
| 2267 | Expect(Token::RPAREN, CHECK_OK); |
| 2268 | |
| 2269 | // Allow do-statements to be terminated with and without |
| 2270 | // semi-colons. This allows code such as 'do;while(0)return' to |
| 2271 | // parse, which would not be the case if we had used the |
| 2272 | // ExpectSemicolon() functionality here. |
| 2273 | if (peek() == Token::SEMICOLON) Consume(Token::SEMICOLON); |
| 2274 | |
Steve Block | 3ce2e20 | 2009-11-05 08:53:23 +0000 | [diff] [blame] | 2275 | if (loop != NULL) loop->Initialize(cond, body); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2276 | return loop; |
| 2277 | } |
| 2278 | |
| 2279 | |
Steve Block | 3ce2e20 | 2009-11-05 08:53:23 +0000 | [diff] [blame] | 2280 | WhileStatement* Parser::ParseWhileStatement(ZoneStringList* labels, bool* ok) { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2281 | // WhileStatement :: |
| 2282 | // 'while' '(' Expression ')' Statement |
| 2283 | |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame^] | 2284 | WhileStatement* loop = new(zone()) WhileStatement(isolate(), labels); |
Teng-Hui Zhu | 3e5fa29 | 2010-11-09 16:16:48 -0800 | [diff] [blame] | 2285 | Target target(&this->target_stack_, loop); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2286 | |
| 2287 | Expect(Token::WHILE, CHECK_OK); |
| 2288 | Expect(Token::LPAREN, CHECK_OK); |
| 2289 | Expression* cond = ParseExpression(true, CHECK_OK); |
| 2290 | Expect(Token::RPAREN, CHECK_OK); |
| 2291 | Statement* body = ParseStatement(NULL, CHECK_OK); |
| 2292 | |
Steve Block | 3ce2e20 | 2009-11-05 08:53:23 +0000 | [diff] [blame] | 2293 | if (loop != NULL) loop->Initialize(cond, body); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2294 | return loop; |
| 2295 | } |
| 2296 | |
| 2297 | |
| 2298 | Statement* Parser::ParseForStatement(ZoneStringList* labels, bool* ok) { |
| 2299 | // ForStatement :: |
| 2300 | // 'for' '(' Expression? ';' Expression? ';' Expression? ')' Statement |
| 2301 | |
| 2302 | Statement* init = NULL; |
| 2303 | |
| 2304 | Expect(Token::FOR, CHECK_OK); |
| 2305 | Expect(Token::LPAREN, CHECK_OK); |
| 2306 | if (peek() != Token::SEMICOLON) { |
| 2307 | if (peek() == Token::VAR || peek() == Token::CONST) { |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame^] | 2308 | Handle<String> name; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2309 | Block* variable_statement = |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame^] | 2310 | ParseVariableDeclarations(false, &name, CHECK_OK); |
| 2311 | |
| 2312 | if (peek() == Token::IN && !name.is_null()) { |
| 2313 | VariableProxy* each = top_scope_->NewUnresolved(name, inside_with()); |
| 2314 | ForInStatement* loop = new(zone()) ForInStatement(isolate(), labels); |
Teng-Hui Zhu | 3e5fa29 | 2010-11-09 16:16:48 -0800 | [diff] [blame] | 2315 | Target target(&this->target_stack_, loop); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2316 | |
| 2317 | Expect(Token::IN, CHECK_OK); |
| 2318 | Expression* enumerable = ParseExpression(true, CHECK_OK); |
| 2319 | Expect(Token::RPAREN, CHECK_OK); |
| 2320 | |
| 2321 | Statement* body = ParseStatement(NULL, CHECK_OK); |
Teng-Hui Zhu | 3e5fa29 | 2010-11-09 16:16:48 -0800 | [diff] [blame] | 2322 | loop->Initialize(each, enumerable, body); |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame^] | 2323 | Block* result = new(zone()) Block(isolate(), NULL, 2, false); |
Teng-Hui Zhu | 3e5fa29 | 2010-11-09 16:16:48 -0800 | [diff] [blame] | 2324 | result->AddStatement(variable_statement); |
| 2325 | result->AddStatement(loop); |
| 2326 | // Parsed for-in loop w/ variable/const declaration. |
| 2327 | return result; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2328 | } else { |
| 2329 | init = variable_statement; |
| 2330 | } |
| 2331 | |
| 2332 | } else { |
| 2333 | Expression* expression = ParseExpression(false, CHECK_OK); |
| 2334 | if (peek() == Token::IN) { |
| 2335 | // Signal a reference error if the expression is an invalid |
| 2336 | // left-hand side expression. We could report this as a syntax |
| 2337 | // error here but for compatibility with JSC we choose to report |
| 2338 | // the error at runtime. |
| 2339 | if (expression == NULL || !expression->IsValidLeftHandSide()) { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 2340 | Handle<String> type = |
| 2341 | isolate()->factory()->invalid_lhs_in_for_in_symbol(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2342 | expression = NewThrowReferenceError(type); |
| 2343 | } |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame^] | 2344 | ForInStatement* loop = new(zone()) ForInStatement(isolate(), labels); |
Teng-Hui Zhu | 3e5fa29 | 2010-11-09 16:16:48 -0800 | [diff] [blame] | 2345 | Target target(&this->target_stack_, loop); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2346 | |
| 2347 | Expect(Token::IN, CHECK_OK); |
| 2348 | Expression* enumerable = ParseExpression(true, CHECK_OK); |
| 2349 | Expect(Token::RPAREN, CHECK_OK); |
| 2350 | |
| 2351 | Statement* body = ParseStatement(NULL, CHECK_OK); |
| 2352 | if (loop) loop->Initialize(expression, enumerable, body); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2353 | // Parsed for-in loop. |
| 2354 | return loop; |
| 2355 | |
| 2356 | } else { |
Ben Murdoch | 8b112d2 | 2011-06-08 16:22:53 +0100 | [diff] [blame] | 2357 | init = new(zone()) ExpressionStatement(expression); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2358 | } |
| 2359 | } |
| 2360 | } |
| 2361 | |
| 2362 | // Standard 'for' loop |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame^] | 2363 | ForStatement* loop = new(zone()) ForStatement(isolate(), labels); |
Teng-Hui Zhu | 3e5fa29 | 2010-11-09 16:16:48 -0800 | [diff] [blame] | 2364 | Target target(&this->target_stack_, loop); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2365 | |
| 2366 | // Parsed initializer at this point. |
| 2367 | Expect(Token::SEMICOLON, CHECK_OK); |
| 2368 | |
| 2369 | Expression* cond = NULL; |
| 2370 | if (peek() != Token::SEMICOLON) { |
| 2371 | cond = ParseExpression(true, CHECK_OK); |
| 2372 | } |
| 2373 | Expect(Token::SEMICOLON, CHECK_OK); |
| 2374 | |
| 2375 | Statement* next = NULL; |
| 2376 | if (peek() != Token::RPAREN) { |
| 2377 | Expression* exp = ParseExpression(true, CHECK_OK); |
Ben Murdoch | 8b112d2 | 2011-06-08 16:22:53 +0100 | [diff] [blame] | 2378 | next = new(zone()) ExpressionStatement(exp); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2379 | } |
| 2380 | Expect(Token::RPAREN, CHECK_OK); |
| 2381 | |
| 2382 | Statement* body = ParseStatement(NULL, CHECK_OK); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2383 | if (loop) loop->Initialize(init, cond, next, body); |
| 2384 | return loop; |
| 2385 | } |
| 2386 | |
| 2387 | |
| 2388 | // Precedence = 1 |
| 2389 | Expression* Parser::ParseExpression(bool accept_IN, bool* ok) { |
| 2390 | // Expression :: |
| 2391 | // AssignmentExpression |
| 2392 | // Expression ',' AssignmentExpression |
| 2393 | |
| 2394 | Expression* result = ParseAssignmentExpression(accept_IN, CHECK_OK); |
| 2395 | while (peek() == Token::COMMA) { |
| 2396 | Expect(Token::COMMA, CHECK_OK); |
Kristian Monsen | 80d68ea | 2010-09-08 11:05:35 +0100 | [diff] [blame] | 2397 | int position = scanner().location().beg_pos; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2398 | Expression* right = ParseAssignmentExpression(accept_IN, CHECK_OK); |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame^] | 2399 | result = new(zone()) BinaryOperation( |
| 2400 | isolate(), Token::COMMA, result, right, position); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2401 | } |
| 2402 | return result; |
| 2403 | } |
| 2404 | |
| 2405 | |
| 2406 | // Precedence = 2 |
| 2407 | Expression* Parser::ParseAssignmentExpression(bool accept_IN, bool* ok) { |
| 2408 | // AssignmentExpression :: |
| 2409 | // ConditionalExpression |
| 2410 | // LeftHandSideExpression AssignmentOperator AssignmentExpression |
| 2411 | |
Kristian Monsen | 80d68ea | 2010-09-08 11:05:35 +0100 | [diff] [blame] | 2412 | if (fni_ != NULL) fni_->Enter(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2413 | Expression* expression = ParseConditionalExpression(accept_IN, CHECK_OK); |
| 2414 | |
| 2415 | if (!Token::IsAssignmentOp(peek())) { |
Kristian Monsen | 80d68ea | 2010-09-08 11:05:35 +0100 | [diff] [blame] | 2416 | if (fni_ != NULL) fni_->Leave(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2417 | // Parsed conditional expression only (no assignment). |
| 2418 | return expression; |
| 2419 | } |
| 2420 | |
| 2421 | // Signal a reference error if the expression is an invalid left-hand |
| 2422 | // side expression. We could report this as a syntax error here but |
| 2423 | // for compatibility with JSC we choose to report the error at |
| 2424 | // runtime. |
| 2425 | if (expression == NULL || !expression->IsValidLeftHandSide()) { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 2426 | Handle<String> type = |
| 2427 | isolate()->factory()->invalid_lhs_in_assignment_symbol(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2428 | expression = NewThrowReferenceError(type); |
| 2429 | } |
| 2430 | |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 2431 | if (top_scope_->is_strict_mode()) { |
Steve Block | 1e0659c | 2011-05-24 12:43:12 +0100 | [diff] [blame] | 2432 | // Assignment to eval or arguments is disallowed in strict mode. |
| 2433 | CheckStrictModeLValue(expression, "strict_lhs_assignment", CHECK_OK); |
| 2434 | } |
| 2435 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2436 | Token::Value op = Next(); // Get assignment operator. |
| 2437 | int pos = scanner().location().beg_pos; |
| 2438 | Expression* right = ParseAssignmentExpression(accept_IN, CHECK_OK); |
| 2439 | |
| 2440 | // TODO(1231235): We try to estimate the set of properties set by |
| 2441 | // constructors. We define a new property whenever there is an |
| 2442 | // assignment to a property of 'this'. We should probably only add |
| 2443 | // properties if we haven't seen them before. Otherwise we'll |
| 2444 | // probably overestimate the number of properties. |
| 2445 | Property* property = expression ? expression->AsProperty() : NULL; |
| 2446 | if (op == Token::ASSIGN && |
| 2447 | property != NULL && |
| 2448 | property->obj()->AsVariableProxy() != NULL && |
| 2449 | property->obj()->AsVariableProxy()->is_this()) { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 2450 | lexical_scope_->AddProperty(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2451 | } |
| 2452 | |
Shimeng (Simon) Wang | 8a31eba | 2010-12-06 19:01:33 -0800 | [diff] [blame] | 2453 | // If we assign a function literal to a property we pretenure the |
| 2454 | // literal so it can be added as a constant function property. |
| 2455 | if (property != NULL && right->AsFunctionLiteral() != NULL) { |
| 2456 | right->AsFunctionLiteral()->set_pretenure(true); |
| 2457 | } |
| 2458 | |
Kristian Monsen | 80d68ea | 2010-09-08 11:05:35 +0100 | [diff] [blame] | 2459 | if (fni_ != NULL) { |
| 2460 | // Check if the right hand side is a call to avoid inferring a |
| 2461 | // name if we're dealing with "a = function(){...}();"-like |
| 2462 | // expression. |
| 2463 | if ((op == Token::INIT_VAR |
| 2464 | || op == Token::INIT_CONST |
| 2465 | || op == Token::ASSIGN) |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame^] | 2466 | && (right->AsCall() == NULL && right->AsCallNew() == NULL)) { |
Kristian Monsen | 80d68ea | 2010-09-08 11:05:35 +0100 | [diff] [blame] | 2467 | fni_->Infer(); |
| 2468 | } |
| 2469 | fni_->Leave(); |
| 2470 | } |
| 2471 | |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame^] | 2472 | return new(zone()) Assignment(isolate(), op, expression, right, pos); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2473 | } |
| 2474 | |
| 2475 | |
| 2476 | // Precedence = 3 |
| 2477 | Expression* Parser::ParseConditionalExpression(bool accept_IN, bool* ok) { |
| 2478 | // ConditionalExpression :: |
| 2479 | // LogicalOrExpression |
| 2480 | // LogicalOrExpression '?' AssignmentExpression ':' AssignmentExpression |
| 2481 | |
| 2482 | // We start using the binary expression parser for prec >= 4 only! |
| 2483 | Expression* expression = ParseBinaryExpression(4, accept_IN, CHECK_OK); |
| 2484 | if (peek() != Token::CONDITIONAL) return expression; |
| 2485 | Consume(Token::CONDITIONAL); |
| 2486 | // In parsing the first assignment expression in conditional |
| 2487 | // expressions we always accept the 'in' keyword; see ECMA-262, |
| 2488 | // section 11.12, page 58. |
Ben Murdoch | 7f4d5bd | 2010-06-15 11:15:29 +0100 | [diff] [blame] | 2489 | int left_position = scanner().peek_location().beg_pos; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2490 | Expression* left = ParseAssignmentExpression(true, CHECK_OK); |
| 2491 | Expect(Token::COLON, CHECK_OK); |
Ben Murdoch | 7f4d5bd | 2010-06-15 11:15:29 +0100 | [diff] [blame] | 2492 | int right_position = scanner().peek_location().beg_pos; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2493 | Expression* right = ParseAssignmentExpression(accept_IN, CHECK_OK); |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame^] | 2494 | return new(zone()) Conditional( |
| 2495 | isolate(), expression, left, right, left_position, right_position); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2496 | } |
| 2497 | |
| 2498 | |
| 2499 | static int Precedence(Token::Value tok, bool accept_IN) { |
| 2500 | if (tok == Token::IN && !accept_IN) |
| 2501 | return 0; // 0 precedence will terminate binary expression parsing |
| 2502 | |
| 2503 | return Token::Precedence(tok); |
| 2504 | } |
| 2505 | |
| 2506 | |
| 2507 | // Precedence >= 4 |
| 2508 | Expression* Parser::ParseBinaryExpression(int prec, bool accept_IN, bool* ok) { |
| 2509 | ASSERT(prec >= 4); |
| 2510 | Expression* x = ParseUnaryExpression(CHECK_OK); |
| 2511 | for (int prec1 = Precedence(peek(), accept_IN); prec1 >= prec; prec1--) { |
| 2512 | // prec1 >= 4 |
| 2513 | while (Precedence(peek(), accept_IN) == prec1) { |
| 2514 | Token::Value op = Next(); |
Kristian Monsen | 80d68ea | 2010-09-08 11:05:35 +0100 | [diff] [blame] | 2515 | int position = scanner().location().beg_pos; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2516 | Expression* y = ParseBinaryExpression(prec1 + 1, accept_IN, CHECK_OK); |
| 2517 | |
| 2518 | // Compute some expressions involving only number literals. |
| 2519 | if (x && x->AsLiteral() && x->AsLiteral()->handle()->IsNumber() && |
| 2520 | y && y->AsLiteral() && y->AsLiteral()->handle()->IsNumber()) { |
| 2521 | double x_val = x->AsLiteral()->handle()->Number(); |
| 2522 | double y_val = y->AsLiteral()->handle()->Number(); |
| 2523 | |
| 2524 | switch (op) { |
| 2525 | case Token::ADD: |
| 2526 | x = NewNumberLiteral(x_val + y_val); |
| 2527 | continue; |
| 2528 | case Token::SUB: |
| 2529 | x = NewNumberLiteral(x_val - y_val); |
| 2530 | continue; |
| 2531 | case Token::MUL: |
| 2532 | x = NewNumberLiteral(x_val * y_val); |
| 2533 | continue; |
| 2534 | case Token::DIV: |
| 2535 | x = NewNumberLiteral(x_val / y_val); |
| 2536 | continue; |
| 2537 | case Token::BIT_OR: |
| 2538 | x = NewNumberLiteral(DoubleToInt32(x_val) | DoubleToInt32(y_val)); |
| 2539 | continue; |
| 2540 | case Token::BIT_AND: |
| 2541 | x = NewNumberLiteral(DoubleToInt32(x_val) & DoubleToInt32(y_val)); |
| 2542 | continue; |
| 2543 | case Token::BIT_XOR: |
| 2544 | x = NewNumberLiteral(DoubleToInt32(x_val) ^ DoubleToInt32(y_val)); |
| 2545 | continue; |
| 2546 | case Token::SHL: { |
| 2547 | int value = DoubleToInt32(x_val) << (DoubleToInt32(y_val) & 0x1f); |
| 2548 | x = NewNumberLiteral(value); |
| 2549 | continue; |
| 2550 | } |
| 2551 | case Token::SHR: { |
| 2552 | uint32_t shift = DoubleToInt32(y_val) & 0x1f; |
| 2553 | uint32_t value = DoubleToUint32(x_val) >> shift; |
| 2554 | x = NewNumberLiteral(value); |
| 2555 | continue; |
| 2556 | } |
| 2557 | case Token::SAR: { |
| 2558 | uint32_t shift = DoubleToInt32(y_val) & 0x1f; |
| 2559 | int value = ArithmeticShiftRight(DoubleToInt32(x_val), shift); |
| 2560 | x = NewNumberLiteral(value); |
| 2561 | continue; |
| 2562 | } |
| 2563 | default: |
| 2564 | break; |
| 2565 | } |
| 2566 | } |
| 2567 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2568 | // For now we distinguish between comparisons and other binary |
| 2569 | // operations. (We could combine the two and get rid of this |
Kristian Monsen | 80d68ea | 2010-09-08 11:05:35 +0100 | [diff] [blame] | 2570 | // code and AST node eventually.) |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2571 | if (Token::IsCompareOp(op)) { |
| 2572 | // We have a comparison. |
| 2573 | Token::Value cmp = op; |
| 2574 | switch (op) { |
| 2575 | case Token::NE: cmp = Token::EQ; break; |
| 2576 | case Token::NE_STRICT: cmp = Token::EQ_STRICT; break; |
| 2577 | default: break; |
| 2578 | } |
Kristian Monsen | 80d68ea | 2010-09-08 11:05:35 +0100 | [diff] [blame] | 2579 | x = NewCompareNode(cmp, x, y, position); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2580 | if (cmp != op) { |
| 2581 | // The comparison was negated - add a NOT. |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame^] | 2582 | x = new(zone()) UnaryOperation(isolate(), Token::NOT, x, position); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2583 | } |
| 2584 | |
| 2585 | } else { |
| 2586 | // We have a "normal" binary operation. |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame^] | 2587 | x = new(zone()) BinaryOperation(isolate(), op, x, y, position); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2588 | } |
| 2589 | } |
| 2590 | } |
| 2591 | return x; |
| 2592 | } |
| 2593 | |
| 2594 | |
Kristian Monsen | 80d68ea | 2010-09-08 11:05:35 +0100 | [diff] [blame] | 2595 | Expression* Parser::NewCompareNode(Token::Value op, |
| 2596 | Expression* x, |
| 2597 | Expression* y, |
| 2598 | int position) { |
| 2599 | ASSERT(op != Token::NE && op != Token::NE_STRICT); |
Teng-Hui Zhu | 3e5fa29 | 2010-11-09 16:16:48 -0800 | [diff] [blame] | 2600 | if (op == Token::EQ || op == Token::EQ_STRICT) { |
Kristian Monsen | 80d68ea | 2010-09-08 11:05:35 +0100 | [diff] [blame] | 2601 | bool is_strict = (op == Token::EQ_STRICT); |
| 2602 | Literal* x_literal = x->AsLiteral(); |
| 2603 | if (x_literal != NULL && x_literal->IsNull()) { |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame^] | 2604 | return new(zone()) CompareToNull(isolate(), is_strict, y); |
Kristian Monsen | 80d68ea | 2010-09-08 11:05:35 +0100 | [diff] [blame] | 2605 | } |
| 2606 | |
| 2607 | Literal* y_literal = y->AsLiteral(); |
| 2608 | if (y_literal != NULL && y_literal->IsNull()) { |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame^] | 2609 | return new(zone()) CompareToNull(isolate(), is_strict, x); |
Kristian Monsen | 80d68ea | 2010-09-08 11:05:35 +0100 | [diff] [blame] | 2610 | } |
| 2611 | } |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame^] | 2612 | return new(zone()) CompareOperation(isolate(), op, x, y, position); |
Kristian Monsen | 80d68ea | 2010-09-08 11:05:35 +0100 | [diff] [blame] | 2613 | } |
| 2614 | |
| 2615 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2616 | Expression* Parser::ParseUnaryExpression(bool* ok) { |
| 2617 | // UnaryExpression :: |
| 2618 | // PostfixExpression |
| 2619 | // 'delete' UnaryExpression |
| 2620 | // 'void' UnaryExpression |
| 2621 | // 'typeof' UnaryExpression |
| 2622 | // '++' UnaryExpression |
| 2623 | // '--' UnaryExpression |
| 2624 | // '+' UnaryExpression |
| 2625 | // '-' UnaryExpression |
| 2626 | // '~' UnaryExpression |
| 2627 | // '!' UnaryExpression |
| 2628 | |
| 2629 | Token::Value op = peek(); |
| 2630 | if (Token::IsUnaryOp(op)) { |
| 2631 | op = Next(); |
Ben Murdoch | 257744e | 2011-11-30 15:57:28 +0000 | [diff] [blame] | 2632 | int position = scanner().location().beg_pos; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2633 | Expression* expression = ParseUnaryExpression(CHECK_OK); |
| 2634 | |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame^] | 2635 | if (expression != NULL && (expression->AsLiteral() != NULL)) { |
| 2636 | Handle<Object> literal = expression->AsLiteral()->handle(); |
| 2637 | if (op == Token::NOT) { |
| 2638 | // Convert the literal to a boolean condition and negate it. |
| 2639 | bool condition = literal->ToBoolean()->IsTrue(); |
| 2640 | Handle<Object> result(isolate()->heap()->ToBoolean(!condition)); |
| 2641 | return NewLiteral(result); |
| 2642 | } else if (literal->IsNumber()) { |
| 2643 | // Compute some expressions involving only number literals. |
| 2644 | double value = literal->Number(); |
| 2645 | switch (op) { |
| 2646 | case Token::ADD: |
| 2647 | return expression; |
| 2648 | case Token::SUB: |
| 2649 | return NewNumberLiteral(-value); |
| 2650 | case Token::BIT_NOT: |
| 2651 | return NewNumberLiteral(~DoubleToInt32(value)); |
| 2652 | default: |
| 2653 | break; |
| 2654 | } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2655 | } |
| 2656 | } |
| 2657 | |
Ben Murdoch | e0cee9b | 2011-05-25 10:26:03 +0100 | [diff] [blame] | 2658 | // "delete identifier" is a syntax error in strict mode. |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 2659 | if (op == Token::DELETE && top_scope_->is_strict_mode()) { |
Ben Murdoch | e0cee9b | 2011-05-25 10:26:03 +0100 | [diff] [blame] | 2660 | VariableProxy* operand = expression->AsVariableProxy(); |
| 2661 | if (operand != NULL && !operand->is_this()) { |
| 2662 | ReportMessage("strict_delete", Vector<const char*>::empty()); |
| 2663 | *ok = false; |
| 2664 | return NULL; |
| 2665 | } |
| 2666 | } |
| 2667 | |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame^] | 2668 | return new(zone()) UnaryOperation(isolate(), op, expression, position); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2669 | |
| 2670 | } else if (Token::IsCountOp(op)) { |
| 2671 | op = Next(); |
| 2672 | Expression* expression = ParseUnaryExpression(CHECK_OK); |
| 2673 | // Signal a reference error if the expression is an invalid |
| 2674 | // left-hand side expression. We could report this as a syntax |
| 2675 | // error here but for compatibility with JSC we choose to report the |
| 2676 | // error at runtime. |
| 2677 | if (expression == NULL || !expression->IsValidLeftHandSide()) { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 2678 | Handle<String> type = |
| 2679 | isolate()->factory()->invalid_lhs_in_prefix_op_symbol(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2680 | expression = NewThrowReferenceError(type); |
| 2681 | } |
Steve Block | 1e0659c | 2011-05-24 12:43:12 +0100 | [diff] [blame] | 2682 | |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 2683 | if (top_scope_->is_strict_mode()) { |
Steve Block | 1e0659c | 2011-05-24 12:43:12 +0100 | [diff] [blame] | 2684 | // Prefix expression operand in strict mode may not be eval or arguments. |
| 2685 | CheckStrictModeLValue(expression, "strict_lhs_prefix", CHECK_OK); |
| 2686 | } |
| 2687 | |
Kristian Monsen | 80d68ea | 2010-09-08 11:05:35 +0100 | [diff] [blame] | 2688 | int position = scanner().location().beg_pos; |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame^] | 2689 | return new(zone()) CountOperation(isolate(), |
| 2690 | op, |
Ben Murdoch | 8b112d2 | 2011-06-08 16:22:53 +0100 | [diff] [blame] | 2691 | true /* prefix */, |
| 2692 | expression, |
| 2693 | position); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2694 | |
| 2695 | } else { |
| 2696 | return ParsePostfixExpression(ok); |
| 2697 | } |
| 2698 | } |
| 2699 | |
| 2700 | |
| 2701 | Expression* Parser::ParsePostfixExpression(bool* ok) { |
| 2702 | // PostfixExpression :: |
| 2703 | // LeftHandSideExpression ('++' | '--')? |
| 2704 | |
| 2705 | Expression* expression = ParseLeftHandSideExpression(CHECK_OK); |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame^] | 2706 | if (!scanner().HasAnyLineTerminatorBeforeNext() && |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 2707 | Token::IsCountOp(peek())) { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2708 | // Signal a reference error if the expression is an invalid |
| 2709 | // left-hand side expression. We could report this as a syntax |
| 2710 | // error here but for compatibility with JSC we choose to report the |
| 2711 | // error at runtime. |
| 2712 | if (expression == NULL || !expression->IsValidLeftHandSide()) { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 2713 | Handle<String> type = |
| 2714 | isolate()->factory()->invalid_lhs_in_postfix_op_symbol(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2715 | expression = NewThrowReferenceError(type); |
| 2716 | } |
Steve Block | 1e0659c | 2011-05-24 12:43:12 +0100 | [diff] [blame] | 2717 | |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 2718 | if (top_scope_->is_strict_mode()) { |
Steve Block | 1e0659c | 2011-05-24 12:43:12 +0100 | [diff] [blame] | 2719 | // Postfix expression operand in strict mode may not be eval or arguments. |
| 2720 | CheckStrictModeLValue(expression, "strict_lhs_prefix", CHECK_OK); |
| 2721 | } |
| 2722 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2723 | Token::Value next = Next(); |
Kristian Monsen | 80d68ea | 2010-09-08 11:05:35 +0100 | [diff] [blame] | 2724 | int position = scanner().location().beg_pos; |
Ben Murdoch | 8b112d2 | 2011-06-08 16:22:53 +0100 | [diff] [blame] | 2725 | expression = |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame^] | 2726 | new(zone()) CountOperation(isolate(), |
| 2727 | next, |
Ben Murdoch | 8b112d2 | 2011-06-08 16:22:53 +0100 | [diff] [blame] | 2728 | false /* postfix */, |
| 2729 | expression, |
| 2730 | position); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2731 | } |
| 2732 | return expression; |
| 2733 | } |
| 2734 | |
| 2735 | |
| 2736 | Expression* Parser::ParseLeftHandSideExpression(bool* ok) { |
| 2737 | // LeftHandSideExpression :: |
| 2738 | // (NewExpression | MemberExpression) ... |
| 2739 | |
| 2740 | Expression* result; |
| 2741 | if (peek() == Token::NEW) { |
| 2742 | result = ParseNewExpression(CHECK_OK); |
| 2743 | } else { |
| 2744 | result = ParseMemberExpression(CHECK_OK); |
| 2745 | } |
| 2746 | |
| 2747 | while (true) { |
| 2748 | switch (peek()) { |
| 2749 | case Token::LBRACK: { |
| 2750 | Consume(Token::LBRACK); |
| 2751 | int pos = scanner().location().beg_pos; |
| 2752 | Expression* index = ParseExpression(true, CHECK_OK); |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame^] | 2753 | result = new(zone()) Property(isolate(), result, index, pos); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2754 | Expect(Token::RBRACK, CHECK_OK); |
| 2755 | break; |
| 2756 | } |
| 2757 | |
| 2758 | case Token::LPAREN: { |
| 2759 | int pos = scanner().location().beg_pos; |
| 2760 | ZoneList<Expression*>* args = ParseArguments(CHECK_OK); |
| 2761 | |
| 2762 | // Keep track of eval() calls since they disable all local variable |
| 2763 | // optimizations. |
| 2764 | // The calls that need special treatment are the |
| 2765 | // direct (i.e. not aliased) eval calls. These calls are all of the |
| 2766 | // form eval(...) with no explicit receiver object where eval is not |
Ben Murdoch | b8e0da2 | 2011-05-16 14:20:40 +0100 | [diff] [blame] | 2767 | // declared in the current scope chain. |
| 2768 | // These calls are marked as potentially direct eval calls. Whether |
| 2769 | // they are actually direct calls to eval is determined at run time. |
| 2770 | // TODO(994): In ES5, it doesn't matter if the "eval" var is declared |
| 2771 | // in the local scope chain. It only matters that it's called "eval", |
| 2772 | // is called without a receiver and it refers to the original eval |
| 2773 | // function. |
Teng-Hui Zhu | 3e5fa29 | 2010-11-09 16:16:48 -0800 | [diff] [blame] | 2774 | VariableProxy* callee = result->AsVariableProxy(); |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 2775 | if (callee != NULL && |
| 2776 | callee->IsVariable(isolate()->factory()->eval_symbol())) { |
Teng-Hui Zhu | 3e5fa29 | 2010-11-09 16:16:48 -0800 | [diff] [blame] | 2777 | Handle<String> name = callee->name(); |
| 2778 | Variable* var = top_scope_->Lookup(name); |
| 2779 | if (var == NULL) { |
| 2780 | top_scope_->RecordEvalCall(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2781 | } |
| 2782 | } |
Teng-Hui Zhu | 3e5fa29 | 2010-11-09 16:16:48 -0800 | [diff] [blame] | 2783 | result = NewCall(result, args, pos); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2784 | break; |
| 2785 | } |
| 2786 | |
| 2787 | case Token::PERIOD: { |
| 2788 | Consume(Token::PERIOD); |
| 2789 | int pos = scanner().location().beg_pos; |
Ben Murdoch | bb769b2 | 2010-08-11 14:56:33 +0100 | [diff] [blame] | 2790 | Handle<String> name = ParseIdentifierName(CHECK_OK); |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame^] | 2791 | result = new(zone()) Property(isolate(), |
| 2792 | result, |
| 2793 | NewLiteral(name), |
| 2794 | pos); |
Kristian Monsen | 80d68ea | 2010-09-08 11:05:35 +0100 | [diff] [blame] | 2795 | if (fni_ != NULL) fni_->PushLiteralName(name); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2796 | break; |
| 2797 | } |
| 2798 | |
| 2799 | default: |
| 2800 | return result; |
| 2801 | } |
| 2802 | } |
| 2803 | } |
| 2804 | |
| 2805 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2806 | Expression* Parser::ParseNewPrefix(PositionStack* stack, bool* ok) { |
| 2807 | // NewExpression :: |
| 2808 | // ('new')+ MemberExpression |
| 2809 | |
| 2810 | // The grammar for new expressions is pretty warped. The keyword |
| 2811 | // 'new' can either be a part of the new expression (where it isn't |
| 2812 | // followed by an argument list) or a part of the member expression, |
| 2813 | // where it must be followed by an argument list. To accommodate |
| 2814 | // this, we parse the 'new' keywords greedily and keep track of how |
| 2815 | // many we have parsed. This information is then passed on to the |
| 2816 | // member expression parser, which is only allowed to match argument |
| 2817 | // lists as long as it has 'new' prefixes left |
| 2818 | Expect(Token::NEW, CHECK_OK); |
| 2819 | PositionStack::Element pos(stack, scanner().location().beg_pos); |
| 2820 | |
| 2821 | Expression* result; |
| 2822 | if (peek() == Token::NEW) { |
| 2823 | result = ParseNewPrefix(stack, CHECK_OK); |
| 2824 | } else { |
| 2825 | result = ParseMemberWithNewPrefixesExpression(stack, CHECK_OK); |
| 2826 | } |
| 2827 | |
| 2828 | if (!stack->is_empty()) { |
| 2829 | int last = stack->pop(); |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame^] | 2830 | result = new(zone()) CallNew(isolate(), |
| 2831 | result, |
Ben Murdoch | 257744e | 2011-11-30 15:57:28 +0000 | [diff] [blame] | 2832 | new(zone()) ZoneList<Expression*>(0), |
| 2833 | last); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2834 | } |
| 2835 | return result; |
| 2836 | } |
| 2837 | |
| 2838 | |
| 2839 | Expression* Parser::ParseNewExpression(bool* ok) { |
| 2840 | PositionStack stack(ok); |
| 2841 | return ParseNewPrefix(&stack, ok); |
| 2842 | } |
| 2843 | |
| 2844 | |
| 2845 | Expression* Parser::ParseMemberExpression(bool* ok) { |
| 2846 | return ParseMemberWithNewPrefixesExpression(NULL, ok); |
| 2847 | } |
| 2848 | |
| 2849 | |
| 2850 | Expression* Parser::ParseMemberWithNewPrefixesExpression(PositionStack* stack, |
| 2851 | bool* ok) { |
| 2852 | // MemberExpression :: |
| 2853 | // (PrimaryExpression | FunctionLiteral) |
| 2854 | // ('[' Expression ']' | '.' Identifier | Arguments)* |
| 2855 | |
| 2856 | // Parse the initial primary or function expression. |
| 2857 | Expression* result = NULL; |
| 2858 | if (peek() == Token::FUNCTION) { |
| 2859 | Expect(Token::FUNCTION, CHECK_OK); |
| 2860 | int function_token_position = scanner().location().beg_pos; |
| 2861 | Handle<String> name; |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame^] | 2862 | bool is_strict_reserved_name = false; |
Steve Block | 1e0659c | 2011-05-24 12:43:12 +0100 | [diff] [blame] | 2863 | if (peek_any_identifier()) { |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame^] | 2864 | name = ParseIdentifierOrStrictReservedWord(&is_strict_reserved_name, |
| 2865 | CHECK_OK); |
Steve Block | 1e0659c | 2011-05-24 12:43:12 +0100 | [diff] [blame] | 2866 | } |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame^] | 2867 | FunctionLiteral::Type type = name.is_null() |
| 2868 | ? FunctionLiteral::ANONYMOUS_EXPRESSION |
| 2869 | : FunctionLiteral::NAMED_EXPRESSION; |
| 2870 | result = ParseFunctionLiteral(name, |
| 2871 | is_strict_reserved_name, |
| 2872 | function_token_position, |
| 2873 | type, |
| 2874 | CHECK_OK); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2875 | } else { |
| 2876 | result = ParsePrimaryExpression(CHECK_OK); |
| 2877 | } |
| 2878 | |
| 2879 | while (true) { |
| 2880 | switch (peek()) { |
| 2881 | case Token::LBRACK: { |
| 2882 | Consume(Token::LBRACK); |
| 2883 | int pos = scanner().location().beg_pos; |
| 2884 | Expression* index = ParseExpression(true, CHECK_OK); |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame^] | 2885 | result = new(zone()) Property(isolate(), result, index, pos); |
| 2886 | if (fni_ != NULL) { |
| 2887 | if (index->IsPropertyName()) { |
| 2888 | fni_->PushLiteralName(index->AsLiteral()->AsPropertyName()); |
| 2889 | } else { |
| 2890 | fni_->PushLiteralName( |
| 2891 | isolate()->factory()->anonymous_function_symbol()); |
| 2892 | } |
| 2893 | } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2894 | Expect(Token::RBRACK, CHECK_OK); |
| 2895 | break; |
| 2896 | } |
| 2897 | case Token::PERIOD: { |
| 2898 | Consume(Token::PERIOD); |
| 2899 | int pos = scanner().location().beg_pos; |
Ben Murdoch | bb769b2 | 2010-08-11 14:56:33 +0100 | [diff] [blame] | 2900 | Handle<String> name = ParseIdentifierName(CHECK_OK); |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame^] | 2901 | result = new(zone()) Property(isolate(), |
| 2902 | result, |
| 2903 | NewLiteral(name), |
| 2904 | pos); |
Kristian Monsen | 80d68ea | 2010-09-08 11:05:35 +0100 | [diff] [blame] | 2905 | if (fni_ != NULL) fni_->PushLiteralName(name); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2906 | break; |
| 2907 | } |
| 2908 | case Token::LPAREN: { |
| 2909 | if ((stack == NULL) || stack->is_empty()) return result; |
| 2910 | // Consume one of the new prefixes (already parsed). |
| 2911 | ZoneList<Expression*>* args = ParseArguments(CHECK_OK); |
| 2912 | int last = stack->pop(); |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame^] | 2913 | result = new(zone()) CallNew(isolate(), result, args, last); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2914 | break; |
| 2915 | } |
| 2916 | default: |
| 2917 | return result; |
| 2918 | } |
| 2919 | } |
| 2920 | } |
| 2921 | |
| 2922 | |
| 2923 | DebuggerStatement* Parser::ParseDebuggerStatement(bool* ok) { |
| 2924 | // In ECMA-262 'debugger' is defined as a reserved keyword. In some browser |
| 2925 | // contexts this is used as a statement which invokes the debugger as i a |
| 2926 | // break point is present. |
| 2927 | // DebuggerStatement :: |
| 2928 | // 'debugger' ';' |
| 2929 | |
| 2930 | Expect(Token::DEBUGGER, CHECK_OK); |
| 2931 | ExpectSemicolon(CHECK_OK); |
Ben Murdoch | 8b112d2 | 2011-06-08 16:22:53 +0100 | [diff] [blame] | 2932 | return new(zone()) DebuggerStatement(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2933 | } |
| 2934 | |
| 2935 | |
| 2936 | void Parser::ReportUnexpectedToken(Token::Value token) { |
| 2937 | // We don't report stack overflows here, to avoid increasing the |
| 2938 | // stack depth even further. Instead we report it after parsing is |
Leon Clarke | 4515c47 | 2010-02-03 11:58:03 +0000 | [diff] [blame] | 2939 | // over, in ParseProgram/ParseJson. |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 2940 | if (token == Token::ILLEGAL && stack_overflow_) return; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2941 | // Four of the tokens are treated specially |
| 2942 | switch (token) { |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 2943 | case Token::EOS: |
| 2944 | return ReportMessage("unexpected_eos", Vector<const char*>::empty()); |
| 2945 | case Token::NUMBER: |
| 2946 | return ReportMessage("unexpected_token_number", |
| 2947 | Vector<const char*>::empty()); |
| 2948 | case Token::STRING: |
| 2949 | return ReportMessage("unexpected_token_string", |
| 2950 | Vector<const char*>::empty()); |
| 2951 | case Token::IDENTIFIER: |
| 2952 | return ReportMessage("unexpected_token_identifier", |
| 2953 | Vector<const char*>::empty()); |
Steve Block | 1e0659c | 2011-05-24 12:43:12 +0100 | [diff] [blame] | 2954 | case Token::FUTURE_RESERVED_WORD: |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame^] | 2955 | return ReportMessage("unexpected_reserved", |
| 2956 | Vector<const char*>::empty()); |
| 2957 | case Token::FUTURE_STRICT_RESERVED_WORD: |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 2958 | return ReportMessage(top_scope_->is_strict_mode() ? |
Steve Block | 1e0659c | 2011-05-24 12:43:12 +0100 | [diff] [blame] | 2959 | "unexpected_strict_reserved" : |
| 2960 | "unexpected_token_identifier", |
| 2961 | Vector<const char*>::empty()); |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 2962 | default: |
| 2963 | const char* name = Token::String(token); |
| 2964 | ASSERT(name != NULL); |
| 2965 | ReportMessage("unexpected_token", Vector<const char*>(&name, 1)); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2966 | } |
| 2967 | } |
| 2968 | |
| 2969 | |
Leon Clarke | ac95265 | 2010-07-15 11:15:24 +0100 | [diff] [blame] | 2970 | void Parser::ReportInvalidPreparseData(Handle<String> name, bool* ok) { |
| 2971 | SmartPointer<char> name_string = name->ToCString(DISALLOW_NULLS); |
| 2972 | const char* element[1] = { *name_string }; |
| 2973 | ReportMessage("invalid_preparser_data", |
| 2974 | Vector<const char*>(element, 1)); |
| 2975 | *ok = false; |
| 2976 | } |
| 2977 | |
| 2978 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2979 | Expression* Parser::ParsePrimaryExpression(bool* ok) { |
| 2980 | // PrimaryExpression :: |
| 2981 | // 'this' |
| 2982 | // 'null' |
| 2983 | // 'true' |
| 2984 | // 'false' |
| 2985 | // Identifier |
| 2986 | // Number |
| 2987 | // String |
| 2988 | // ArrayLiteral |
| 2989 | // ObjectLiteral |
| 2990 | // RegExpLiteral |
| 2991 | // '(' Expression ')' |
| 2992 | |
| 2993 | Expression* result = NULL; |
| 2994 | switch (peek()) { |
| 2995 | case Token::THIS: { |
| 2996 | Consume(Token::THIS); |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame^] | 2997 | result = new(zone()) VariableProxy(isolate(), top_scope_->receiver()); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2998 | break; |
| 2999 | } |
| 3000 | |
| 3001 | case Token::NULL_LITERAL: |
| 3002 | Consume(Token::NULL_LITERAL); |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame^] | 3003 | result = new(zone()) Literal( |
| 3004 | isolate(), isolate()->factory()->null_value()); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 3005 | break; |
| 3006 | |
| 3007 | case Token::TRUE_LITERAL: |
| 3008 | Consume(Token::TRUE_LITERAL); |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame^] | 3009 | result = new(zone()) Literal( |
| 3010 | isolate(), isolate()->factory()->true_value()); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 3011 | break; |
| 3012 | |
| 3013 | case Token::FALSE_LITERAL: |
| 3014 | Consume(Token::FALSE_LITERAL); |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame^] | 3015 | result = new(zone()) Literal( |
| 3016 | isolate(), isolate()->factory()->false_value()); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 3017 | break; |
| 3018 | |
Steve Block | 1e0659c | 2011-05-24 12:43:12 +0100 | [diff] [blame] | 3019 | case Token::IDENTIFIER: |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame^] | 3020 | case Token::FUTURE_STRICT_RESERVED_WORD: { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 3021 | Handle<String> name = ParseIdentifier(CHECK_OK); |
Kristian Monsen | 80d68ea | 2010-09-08 11:05:35 +0100 | [diff] [blame] | 3022 | if (fni_ != NULL) fni_->PushVariableName(name); |
Ben Murdoch | 8b112d2 | 2011-06-08 16:22:53 +0100 | [diff] [blame] | 3023 | result = top_scope_->NewUnresolved(name, |
| 3024 | inside_with(), |
| 3025 | scanner().location().beg_pos); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 3026 | break; |
| 3027 | } |
| 3028 | |
| 3029 | case Token::NUMBER: { |
| 3030 | Consume(Token::NUMBER); |
Steve Block | 9fac840 | 2011-05-12 15:51:54 +0100 | [diff] [blame] | 3031 | ASSERT(scanner().is_literal_ascii()); |
Ben Murdoch | 8b112d2 | 2011-06-08 16:22:53 +0100 | [diff] [blame] | 3032 | double value = StringToDouble(isolate()->unicode_cache(), |
| 3033 | scanner().literal_ascii_string(), |
Steve Block | 9fac840 | 2011-05-12 15:51:54 +0100 | [diff] [blame] | 3034 | ALLOW_HEX | ALLOW_OCTALS); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 3035 | result = NewNumberLiteral(value); |
| 3036 | break; |
| 3037 | } |
| 3038 | |
| 3039 | case Token::STRING: { |
| 3040 | Consume(Token::STRING); |
Iain Merrick | 9ac36c9 | 2010-09-13 15:29:50 +0100 | [diff] [blame] | 3041 | Handle<String> symbol = GetSymbol(CHECK_OK); |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame^] | 3042 | result = NewLiteral(symbol); |
Kristian Monsen | 80d68ea | 2010-09-08 11:05:35 +0100 | [diff] [blame] | 3043 | if (fni_ != NULL) fni_->PushLiteralName(symbol); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 3044 | break; |
| 3045 | } |
| 3046 | |
| 3047 | case Token::ASSIGN_DIV: |
| 3048 | result = ParseRegExpLiteral(true, CHECK_OK); |
| 3049 | break; |
| 3050 | |
| 3051 | case Token::DIV: |
| 3052 | result = ParseRegExpLiteral(false, CHECK_OK); |
| 3053 | break; |
| 3054 | |
| 3055 | case Token::LBRACK: |
| 3056 | result = ParseArrayLiteral(CHECK_OK); |
| 3057 | break; |
| 3058 | |
| 3059 | case Token::LBRACE: |
| 3060 | result = ParseObjectLiteral(CHECK_OK); |
| 3061 | break; |
| 3062 | |
| 3063 | case Token::LPAREN: |
| 3064 | Consume(Token::LPAREN); |
Ben Murdoch | b8e0da2 | 2011-05-16 14:20:40 +0100 | [diff] [blame] | 3065 | // Heuristically try to detect immediately called functions before |
| 3066 | // seeing the call parentheses. |
| 3067 | parenthesized_function_ = (peek() == Token::FUNCTION); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 3068 | result = ParseExpression(true, CHECK_OK); |
| 3069 | Expect(Token::RPAREN, CHECK_OK); |
| 3070 | break; |
| 3071 | |
| 3072 | case Token::MOD: |
| 3073 | if (allow_natives_syntax_ || extension_ != NULL) { |
| 3074 | result = ParseV8Intrinsic(CHECK_OK); |
| 3075 | break; |
| 3076 | } |
| 3077 | // If we're not allowing special syntax we fall-through to the |
| 3078 | // default case. |
| 3079 | |
| 3080 | default: { |
Ben Murdoch | bb769b2 | 2010-08-11 14:56:33 +0100 | [diff] [blame] | 3081 | Token::Value tok = Next(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 3082 | ReportUnexpectedToken(tok); |
| 3083 | *ok = false; |
| 3084 | return NULL; |
| 3085 | } |
| 3086 | } |
| 3087 | |
| 3088 | return result; |
| 3089 | } |
| 3090 | |
| 3091 | |
Leon Clarke | 4515c47 | 2010-02-03 11:58:03 +0000 | [diff] [blame] | 3092 | void Parser::BuildArrayLiteralBoilerplateLiterals(ZoneList<Expression*>* values, |
| 3093 | Handle<FixedArray> literals, |
| 3094 | bool* is_simple, |
| 3095 | int* depth) { |
| 3096 | // Fill in the literals. |
| 3097 | // Accumulate output values in local variables. |
| 3098 | bool is_simple_acc = true; |
| 3099 | int depth_acc = 1; |
| 3100 | for (int i = 0; i < values->length(); i++) { |
| 3101 | MaterializedLiteral* m_literal = values->at(i)->AsMaterializedLiteral(); |
| 3102 | if (m_literal != NULL && m_literal->depth() >= depth_acc) { |
| 3103 | depth_acc = m_literal->depth() + 1; |
| 3104 | } |
| 3105 | Handle<Object> boilerplate_value = GetBoilerplateValue(values->at(i)); |
| 3106 | if (boilerplate_value->IsUndefined()) { |
| 3107 | literals->set_the_hole(i); |
| 3108 | is_simple_acc = false; |
| 3109 | } else { |
| 3110 | literals->set(i, *boilerplate_value); |
| 3111 | } |
| 3112 | } |
| 3113 | |
| 3114 | *is_simple = is_simple_acc; |
| 3115 | *depth = depth_acc; |
| 3116 | } |
| 3117 | |
| 3118 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 3119 | Expression* Parser::ParseArrayLiteral(bool* ok) { |
| 3120 | // ArrayLiteral :: |
| 3121 | // '[' Expression? (',' Expression?)* ']' |
| 3122 | |
Ben Murdoch | 257744e | 2011-11-30 15:57:28 +0000 | [diff] [blame] | 3123 | ZoneList<Expression*>* values = new(zone()) ZoneList<Expression*>(4); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 3124 | Expect(Token::LBRACK, CHECK_OK); |
| 3125 | while (peek() != Token::RBRACK) { |
| 3126 | Expression* elem; |
| 3127 | if (peek() == Token::COMMA) { |
| 3128 | elem = GetLiteralTheHole(); |
| 3129 | } else { |
| 3130 | elem = ParseAssignmentExpression(true, CHECK_OK); |
| 3131 | } |
Teng-Hui Zhu | 3e5fa29 | 2010-11-09 16:16:48 -0800 | [diff] [blame] | 3132 | values->Add(elem); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 3133 | if (peek() != Token::RBRACK) { |
| 3134 | Expect(Token::COMMA, CHECK_OK); |
| 3135 | } |
| 3136 | } |
| 3137 | Expect(Token::RBRACK, CHECK_OK); |
| 3138 | |
| 3139 | // Update the scope information before the pre-parsing bailout. |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 3140 | int literal_index = lexical_scope_->NextMaterializedLiteralIndex(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 3141 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 3142 | // Allocate a fixed array with all the literals. |
| 3143 | Handle<FixedArray> literals = |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 3144 | isolate()->factory()->NewFixedArray(values->length(), TENURED); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 3145 | |
| 3146 | // Fill in the literals. |
| 3147 | bool is_simple = true; |
| 3148 | int depth = 1; |
Teng-Hui Zhu | 3e5fa29 | 2010-11-09 16:16:48 -0800 | [diff] [blame] | 3149 | for (int i = 0, n = values->length(); i < n; i++) { |
| 3150 | MaterializedLiteral* m_literal = values->at(i)->AsMaterializedLiteral(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 3151 | if (m_literal != NULL && m_literal->depth() + 1 > depth) { |
| 3152 | depth = m_literal->depth() + 1; |
| 3153 | } |
Teng-Hui Zhu | 3e5fa29 | 2010-11-09 16:16:48 -0800 | [diff] [blame] | 3154 | Handle<Object> boilerplate_value = GetBoilerplateValue(values->at(i)); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 3155 | if (boilerplate_value->IsUndefined()) { |
| 3156 | literals->set_the_hole(i); |
| 3157 | is_simple = false; |
| 3158 | } else { |
| 3159 | literals->set(i, *boilerplate_value); |
| 3160 | } |
| 3161 | } |
| 3162 | |
Iain Merrick | 7568138 | 2010-08-19 15:07:18 +0100 | [diff] [blame] | 3163 | // Simple and shallow arrays can be lazily copied, we transform the |
| 3164 | // elements array to a copy-on-write array. |
Teng-Hui Zhu | 3e5fa29 | 2010-11-09 16:16:48 -0800 | [diff] [blame] | 3165 | if (is_simple && depth == 1 && values->length() > 0) { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 3166 | literals->set_map(isolate()->heap()->fixed_cow_array_map()); |
Iain Merrick | 7568138 | 2010-08-19 15:07:18 +0100 | [diff] [blame] | 3167 | } |
| 3168 | |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame^] | 3169 | return new(zone()) ArrayLiteral( |
| 3170 | isolate(), literals, values, literal_index, is_simple, depth); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 3171 | } |
| 3172 | |
| 3173 | |
| 3174 | bool Parser::IsBoilerplateProperty(ObjectLiteral::Property* property) { |
| 3175 | return property != NULL && |
| 3176 | property->kind() != ObjectLiteral::Property::PROTOTYPE; |
| 3177 | } |
| 3178 | |
| 3179 | |
| 3180 | bool CompileTimeValue::IsCompileTimeValue(Expression* expression) { |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 3181 | if (expression->AsLiteral() != NULL) return true; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 3182 | MaterializedLiteral* lit = expression->AsMaterializedLiteral(); |
| 3183 | return lit != NULL && lit->is_simple(); |
| 3184 | } |
| 3185 | |
Iain Merrick | 7568138 | 2010-08-19 15:07:18 +0100 | [diff] [blame] | 3186 | |
| 3187 | bool CompileTimeValue::ArrayLiteralElementNeedsInitialization( |
| 3188 | Expression* value) { |
| 3189 | // If value is a literal the property value is already set in the |
| 3190 | // boilerplate object. |
| 3191 | if (value->AsLiteral() != NULL) return false; |
| 3192 | // If value is a materialized literal the property value is already set |
| 3193 | // in the boilerplate object if it is simple. |
| 3194 | if (CompileTimeValue::IsCompileTimeValue(value)) return false; |
| 3195 | return true; |
| 3196 | } |
| 3197 | |
| 3198 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 3199 | Handle<FixedArray> CompileTimeValue::GetValue(Expression* expression) { |
| 3200 | ASSERT(IsCompileTimeValue(expression)); |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 3201 | Handle<FixedArray> result = FACTORY->NewFixedArray(2, TENURED); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 3202 | ObjectLiteral* object_literal = expression->AsObjectLiteral(); |
| 3203 | if (object_literal != NULL) { |
| 3204 | ASSERT(object_literal->is_simple()); |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 3205 | if (object_literal->fast_elements()) { |
| 3206 | result->set(kTypeSlot, Smi::FromInt(OBJECT_LITERAL_FAST_ELEMENTS)); |
| 3207 | } else { |
| 3208 | result->set(kTypeSlot, Smi::FromInt(OBJECT_LITERAL_SLOW_ELEMENTS)); |
| 3209 | } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 3210 | result->set(kElementsSlot, *object_literal->constant_properties()); |
| 3211 | } else { |
| 3212 | ArrayLiteral* array_literal = expression->AsArrayLiteral(); |
| 3213 | ASSERT(array_literal != NULL && array_literal->is_simple()); |
| 3214 | result->set(kTypeSlot, Smi::FromInt(ARRAY_LITERAL)); |
Leon Clarke | e46be81 | 2010-01-19 14:06:41 +0000 | [diff] [blame] | 3215 | result->set(kElementsSlot, *array_literal->constant_elements()); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 3216 | } |
| 3217 | return result; |
| 3218 | } |
| 3219 | |
| 3220 | |
| 3221 | CompileTimeValue::Type CompileTimeValue::GetType(Handle<FixedArray> value) { |
| 3222 | Smi* type_value = Smi::cast(value->get(kTypeSlot)); |
| 3223 | return static_cast<Type>(type_value->value()); |
| 3224 | } |
| 3225 | |
| 3226 | |
| 3227 | Handle<FixedArray> CompileTimeValue::GetElements(Handle<FixedArray> value) { |
| 3228 | return Handle<FixedArray>(FixedArray::cast(value->get(kElementsSlot))); |
| 3229 | } |
| 3230 | |
| 3231 | |
| 3232 | Handle<Object> Parser::GetBoilerplateValue(Expression* expression) { |
| 3233 | if (expression->AsLiteral() != NULL) { |
| 3234 | return expression->AsLiteral()->handle(); |
| 3235 | } |
| 3236 | if (CompileTimeValue::IsCompileTimeValue(expression)) { |
| 3237 | return CompileTimeValue::GetValue(expression); |
| 3238 | } |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 3239 | return isolate()->factory()->undefined_value(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 3240 | } |
| 3241 | |
Steve Block | 1e0659c | 2011-05-24 12:43:12 +0100 | [diff] [blame] | 3242 | // Defined in ast.cc |
| 3243 | bool IsEqualString(void* first, void* second); |
| 3244 | bool IsEqualNumber(void* first, void* second); |
| 3245 | |
| 3246 | |
| 3247 | // Validation per 11.1.5 Object Initialiser |
| 3248 | class ObjectLiteralPropertyChecker { |
| 3249 | public: |
| 3250 | ObjectLiteralPropertyChecker(Parser* parser, bool strict) : |
| 3251 | props(&IsEqualString), |
| 3252 | elems(&IsEqualNumber), |
| 3253 | parser_(parser), |
| 3254 | strict_(strict) { |
| 3255 | } |
| 3256 | |
| 3257 | void CheckProperty( |
| 3258 | ObjectLiteral::Property* property, |
| 3259 | Scanner::Location loc, |
| 3260 | bool* ok); |
| 3261 | |
| 3262 | private: |
| 3263 | enum PropertyKind { |
| 3264 | kGetAccessor = 0x01, |
| 3265 | kSetAccessor = 0x02, |
| 3266 | kAccessor = kGetAccessor | kSetAccessor, |
| 3267 | kData = 0x04 |
| 3268 | }; |
| 3269 | |
| 3270 | static intptr_t GetPropertyKind(ObjectLiteral::Property* property) { |
| 3271 | switch (property->kind()) { |
| 3272 | case ObjectLiteral::Property::GETTER: |
| 3273 | return kGetAccessor; |
| 3274 | case ObjectLiteral::Property::SETTER: |
| 3275 | return kSetAccessor; |
| 3276 | default: |
| 3277 | return kData; |
| 3278 | } |
| 3279 | } |
| 3280 | |
| 3281 | HashMap props; |
| 3282 | HashMap elems; |
| 3283 | Parser* parser_; |
| 3284 | bool strict_; |
| 3285 | }; |
| 3286 | |
| 3287 | |
| 3288 | void ObjectLiteralPropertyChecker::CheckProperty( |
| 3289 | ObjectLiteral::Property* property, |
| 3290 | Scanner::Location loc, |
| 3291 | bool* ok) { |
| 3292 | |
| 3293 | ASSERT(property != NULL); |
| 3294 | |
| 3295 | Literal *lit = property->key(); |
| 3296 | Handle<Object> handle = lit->handle(); |
| 3297 | |
| 3298 | uint32_t hash; |
| 3299 | HashMap* map; |
| 3300 | void* key; |
| 3301 | |
| 3302 | if (handle->IsSymbol()) { |
| 3303 | Handle<String> name(String::cast(*handle)); |
| 3304 | if (name->AsArrayIndex(&hash)) { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 3305 | Handle<Object> key_handle = FACTORY->NewNumberFromUint(hash); |
Steve Block | 1e0659c | 2011-05-24 12:43:12 +0100 | [diff] [blame] | 3306 | key = key_handle.location(); |
| 3307 | map = &elems; |
| 3308 | } else { |
| 3309 | key = handle.location(); |
| 3310 | hash = name->Hash(); |
| 3311 | map = &props; |
| 3312 | } |
| 3313 | } else if (handle->ToArrayIndex(&hash)) { |
| 3314 | key = handle.location(); |
| 3315 | map = &elems; |
| 3316 | } else { |
| 3317 | ASSERT(handle->IsNumber()); |
| 3318 | double num = handle->Number(); |
| 3319 | char arr[100]; |
| 3320 | Vector<char> buffer(arr, ARRAY_SIZE(arr)); |
| 3321 | const char* str = DoubleToCString(num, buffer); |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 3322 | Handle<String> name = FACTORY->NewStringFromAscii(CStrVector(str)); |
Steve Block | 1e0659c | 2011-05-24 12:43:12 +0100 | [diff] [blame] | 3323 | key = name.location(); |
| 3324 | hash = name->Hash(); |
| 3325 | map = &props; |
| 3326 | } |
| 3327 | |
| 3328 | // Lookup property previously defined, if any. |
| 3329 | HashMap::Entry* entry = map->Lookup(key, hash, true); |
| 3330 | intptr_t prev = reinterpret_cast<intptr_t> (entry->value); |
| 3331 | intptr_t curr = GetPropertyKind(property); |
| 3332 | |
| 3333 | // Duplicate data properties are illegal in strict mode. |
| 3334 | if (strict_ && (curr & prev & kData) != 0) { |
| 3335 | parser_->ReportMessageAt(loc, "strict_duplicate_property", |
| 3336 | Vector<const char*>::empty()); |
| 3337 | *ok = false; |
| 3338 | return; |
| 3339 | } |
| 3340 | // Data property conflicting with an accessor. |
| 3341 | if (((curr & kData) && (prev & kAccessor)) || |
| 3342 | ((prev & kData) && (curr & kAccessor))) { |
| 3343 | parser_->ReportMessageAt(loc, "accessor_data_property", |
| 3344 | Vector<const char*>::empty()); |
| 3345 | *ok = false; |
| 3346 | return; |
| 3347 | } |
| 3348 | // Two accessors of the same type conflicting |
| 3349 | if ((curr & prev & kAccessor) != 0) { |
| 3350 | parser_->ReportMessageAt(loc, "accessor_get_set", |
| 3351 | Vector<const char*>::empty()); |
| 3352 | *ok = false; |
| 3353 | return; |
| 3354 | } |
| 3355 | |
| 3356 | // Update map |
| 3357 | entry->value = reinterpret_cast<void*> (prev | curr); |
| 3358 | *ok = true; |
| 3359 | } |
| 3360 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 3361 | |
Leon Clarke | 4515c47 | 2010-02-03 11:58:03 +0000 | [diff] [blame] | 3362 | void Parser::BuildObjectLiteralConstantProperties( |
| 3363 | ZoneList<ObjectLiteral::Property*>* properties, |
| 3364 | Handle<FixedArray> constant_properties, |
| 3365 | bool* is_simple, |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 3366 | bool* fast_elements, |
Leon Clarke | 4515c47 | 2010-02-03 11:58:03 +0000 | [diff] [blame] | 3367 | int* depth) { |
| 3368 | int position = 0; |
| 3369 | // Accumulate the value in local variables and store it at the end. |
| 3370 | bool is_simple_acc = true; |
| 3371 | int depth_acc = 1; |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 3372 | uint32_t max_element_index = 0; |
| 3373 | uint32_t elements = 0; |
Leon Clarke | 4515c47 | 2010-02-03 11:58:03 +0000 | [diff] [blame] | 3374 | for (int i = 0; i < properties->length(); i++) { |
| 3375 | ObjectLiteral::Property* property = properties->at(i); |
| 3376 | if (!IsBoilerplateProperty(property)) { |
| 3377 | is_simple_acc = false; |
| 3378 | continue; |
| 3379 | } |
| 3380 | MaterializedLiteral* m_literal = property->value()->AsMaterializedLiteral(); |
| 3381 | if (m_literal != NULL && m_literal->depth() >= depth_acc) { |
| 3382 | depth_acc = m_literal->depth() + 1; |
| 3383 | } |
| 3384 | |
| 3385 | // Add CONSTANT and COMPUTED properties to boilerplate. Use undefined |
| 3386 | // value for COMPUTED properties, the real value is filled in at |
| 3387 | // runtime. The enumeration order is maintained. |
| 3388 | Handle<Object> key = property->key()->handle(); |
| 3389 | Handle<Object> value = GetBoilerplateValue(property->value()); |
| 3390 | is_simple_acc = is_simple_acc && !value->IsUndefined(); |
| 3391 | |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 3392 | // Keep track of the number of elements in the object literal and |
| 3393 | // the largest element index. If the largest element index is |
| 3394 | // much larger than the number of elements, creating an object |
| 3395 | // literal with fast elements will be a waste of space. |
| 3396 | uint32_t element_index = 0; |
| 3397 | if (key->IsString() |
| 3398 | && Handle<String>::cast(key)->AsArrayIndex(&element_index) |
| 3399 | && element_index > max_element_index) { |
| 3400 | max_element_index = element_index; |
| 3401 | elements++; |
| 3402 | } else if (key->IsSmi()) { |
| 3403 | int key_value = Smi::cast(*key)->value(); |
| 3404 | if (key_value > 0 |
| 3405 | && static_cast<uint32_t>(key_value) > max_element_index) { |
| 3406 | max_element_index = key_value; |
| 3407 | } |
| 3408 | elements++; |
| 3409 | } |
| 3410 | |
Leon Clarke | 4515c47 | 2010-02-03 11:58:03 +0000 | [diff] [blame] | 3411 | // Add name, value pair to the fixed array. |
| 3412 | constant_properties->set(position++, *key); |
| 3413 | constant_properties->set(position++, *value); |
| 3414 | } |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 3415 | *fast_elements = |
| 3416 | (max_element_index <= 32) || ((2 * elements) >= max_element_index); |
Leon Clarke | 4515c47 | 2010-02-03 11:58:03 +0000 | [diff] [blame] | 3417 | *is_simple = is_simple_acc; |
| 3418 | *depth = depth_acc; |
| 3419 | } |
| 3420 | |
| 3421 | |
Ben Murdoch | bb769b2 | 2010-08-11 14:56:33 +0100 | [diff] [blame] | 3422 | ObjectLiteral::Property* Parser::ParseObjectLiteralGetSet(bool is_getter, |
| 3423 | bool* ok) { |
| 3424 | // Special handling of getter and setter syntax: |
| 3425 | // { ... , get foo() { ... }, ... , set foo(v) { ... v ... } , ... } |
| 3426 | // We have already read the "get" or "set" keyword. |
| 3427 | Token::Value next = Next(); |
Steve Block | 9fac840 | 2011-05-12 15:51:54 +0100 | [diff] [blame] | 3428 | bool is_keyword = Token::IsKeyword(next); |
| 3429 | if (next == Token::IDENTIFIER || next == Token::NUMBER || |
Steve Block | 1e0659c | 2011-05-24 12:43:12 +0100 | [diff] [blame] | 3430 | next == Token::FUTURE_RESERVED_WORD || |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame^] | 3431 | next == Token::FUTURE_STRICT_RESERVED_WORD || |
Steve Block | 9fac840 | 2011-05-12 15:51:54 +0100 | [diff] [blame] | 3432 | next == Token::STRING || is_keyword) { |
| 3433 | Handle<String> name; |
| 3434 | if (is_keyword) { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 3435 | name = isolate_->factory()->LookupAsciiSymbol(Token::String(next)); |
Steve Block | 9fac840 | 2011-05-12 15:51:54 +0100 | [diff] [blame] | 3436 | } else { |
| 3437 | name = GetSymbol(CHECK_OK); |
| 3438 | } |
Ben Murdoch | bb769b2 | 2010-08-11 14:56:33 +0100 | [diff] [blame] | 3439 | FunctionLiteral* value = |
| 3440 | ParseFunctionLiteral(name, |
Steve Block | 1e0659c | 2011-05-24 12:43:12 +0100 | [diff] [blame] | 3441 | false, // reserved words are allowed here |
Ben Murdoch | bb769b2 | 2010-08-11 14:56:33 +0100 | [diff] [blame] | 3442 | RelocInfo::kNoPosition, |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame^] | 3443 | FunctionLiteral::ANONYMOUS_EXPRESSION, |
Ben Murdoch | bb769b2 | 2010-08-11 14:56:33 +0100 | [diff] [blame] | 3444 | CHECK_OK); |
Steve Block | 9fac840 | 2011-05-12 15:51:54 +0100 | [diff] [blame] | 3445 | // Allow any number of parameters for compatiabilty with JSC. |
| 3446 | // Specification only allows zero parameters for get and one for set. |
Ben Murdoch | bb769b2 | 2010-08-11 14:56:33 +0100 | [diff] [blame] | 3447 | ObjectLiteral::Property* property = |
Ben Murdoch | 8b112d2 | 2011-06-08 16:22:53 +0100 | [diff] [blame] | 3448 | new(zone()) ObjectLiteral::Property(is_getter, value); |
Ben Murdoch | bb769b2 | 2010-08-11 14:56:33 +0100 | [diff] [blame] | 3449 | return property; |
| 3450 | } else { |
| 3451 | ReportUnexpectedToken(next); |
| 3452 | *ok = false; |
| 3453 | return NULL; |
| 3454 | } |
| 3455 | } |
| 3456 | |
| 3457 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 3458 | Expression* Parser::ParseObjectLiteral(bool* ok) { |
| 3459 | // ObjectLiteral :: |
| 3460 | // '{' ( |
Ben Murdoch | bb769b2 | 2010-08-11 14:56:33 +0100 | [diff] [blame] | 3461 | // ((IdentifierName | String | Number) ':' AssignmentExpression) |
| 3462 | // | (('get' | 'set') (IdentifierName | String | Number) FunctionLiteral) |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 3463 | // )*[','] '}' |
| 3464 | |
Teng-Hui Zhu | 3e5fa29 | 2010-11-09 16:16:48 -0800 | [diff] [blame] | 3465 | ZoneList<ObjectLiteral::Property*>* properties = |
Ben Murdoch | 257744e | 2011-11-30 15:57:28 +0000 | [diff] [blame] | 3466 | new(zone()) ZoneList<ObjectLiteral::Property*>(4); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 3467 | int number_of_boilerplate_properties = 0; |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 3468 | bool has_function = false; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 3469 | |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 3470 | ObjectLiteralPropertyChecker checker(this, top_scope_->is_strict_mode()); |
Steve Block | 1e0659c | 2011-05-24 12:43:12 +0100 | [diff] [blame] | 3471 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 3472 | Expect(Token::LBRACE, CHECK_OK); |
Steve Block | 1e0659c | 2011-05-24 12:43:12 +0100 | [diff] [blame] | 3473 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 3474 | while (peek() != Token::RBRACE) { |
Kristian Monsen | 80d68ea | 2010-09-08 11:05:35 +0100 | [diff] [blame] | 3475 | if (fni_ != NULL) fni_->Enter(); |
| 3476 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 3477 | Literal* key = NULL; |
Ben Murdoch | bb769b2 | 2010-08-11 14:56:33 +0100 | [diff] [blame] | 3478 | Token::Value next = peek(); |
Steve Block | 1e0659c | 2011-05-24 12:43:12 +0100 | [diff] [blame] | 3479 | |
| 3480 | // Location of the property name token |
| 3481 | Scanner::Location loc = scanner().peek_location(); |
| 3482 | |
Ben Murdoch | bb769b2 | 2010-08-11 14:56:33 +0100 | [diff] [blame] | 3483 | switch (next) { |
Steve Block | 1e0659c | 2011-05-24 12:43:12 +0100 | [diff] [blame] | 3484 | case Token::FUTURE_RESERVED_WORD: |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame^] | 3485 | case Token::FUTURE_STRICT_RESERVED_WORD: |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 3486 | case Token::IDENTIFIER: { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 3487 | bool is_getter = false; |
| 3488 | bool is_setter = false; |
| 3489 | Handle<String> id = |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame^] | 3490 | ParseIdentifierNameOrGetOrSet(&is_getter, &is_setter, CHECK_OK); |
Kristian Monsen | 80d68ea | 2010-09-08 11:05:35 +0100 | [diff] [blame] | 3491 | if (fni_ != NULL) fni_->PushLiteralName(id); |
| 3492 | |
Ben Murdoch | bb769b2 | 2010-08-11 14:56:33 +0100 | [diff] [blame] | 3493 | if ((is_getter || is_setter) && peek() != Token::COLON) { |
Steve Block | 1e0659c | 2011-05-24 12:43:12 +0100 | [diff] [blame] | 3494 | // Update loc to point to the identifier |
| 3495 | loc = scanner().peek_location(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 3496 | ObjectLiteral::Property* property = |
Ben Murdoch | bb769b2 | 2010-08-11 14:56:33 +0100 | [diff] [blame] | 3497 | ParseObjectLiteralGetSet(is_getter, CHECK_OK); |
| 3498 | if (IsBoilerplateProperty(property)) { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 3499 | number_of_boilerplate_properties++; |
Ben Murdoch | bb769b2 | 2010-08-11 14:56:33 +0100 | [diff] [blame] | 3500 | } |
Steve Block | 1e0659c | 2011-05-24 12:43:12 +0100 | [diff] [blame] | 3501 | // Validate the property. |
| 3502 | checker.CheckProperty(property, loc, CHECK_OK); |
Teng-Hui Zhu | 3e5fa29 | 2010-11-09 16:16:48 -0800 | [diff] [blame] | 3503 | properties->Add(property); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 3504 | if (peek() != Token::RBRACE) Expect(Token::COMMA, CHECK_OK); |
Kristian Monsen | 80d68ea | 2010-09-08 11:05:35 +0100 | [diff] [blame] | 3505 | |
| 3506 | if (fni_ != NULL) { |
| 3507 | fni_->Infer(); |
| 3508 | fni_->Leave(); |
| 3509 | } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 3510 | continue; // restart the while |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 3511 | } |
Ben Murdoch | bb769b2 | 2010-08-11 14:56:33 +0100 | [diff] [blame] | 3512 | // Failed to parse as get/set property, so it's just a property |
| 3513 | // called "get" or "set". |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame^] | 3514 | key = NewLiteral(id); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 3515 | break; |
| 3516 | } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 3517 | case Token::STRING: { |
| 3518 | Consume(Token::STRING); |
Iain Merrick | 9ac36c9 | 2010-09-13 15:29:50 +0100 | [diff] [blame] | 3519 | Handle<String> string = GetSymbol(CHECK_OK); |
Kristian Monsen | 80d68ea | 2010-09-08 11:05:35 +0100 | [diff] [blame] | 3520 | if (fni_ != NULL) fni_->PushLiteralName(string); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 3521 | uint32_t index; |
Iain Merrick | 7568138 | 2010-08-19 15:07:18 +0100 | [diff] [blame] | 3522 | if (!string.is_null() && string->AsArrayIndex(&index)) { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 3523 | key = NewNumberLiteral(index); |
Ben Murdoch | bb769b2 | 2010-08-11 14:56:33 +0100 | [diff] [blame] | 3524 | break; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 3525 | } |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame^] | 3526 | key = NewLiteral(string); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 3527 | break; |
| 3528 | } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 3529 | case Token::NUMBER: { |
| 3530 | Consume(Token::NUMBER); |
Steve Block | 9fac840 | 2011-05-12 15:51:54 +0100 | [diff] [blame] | 3531 | ASSERT(scanner().is_literal_ascii()); |
Ben Murdoch | 8b112d2 | 2011-06-08 16:22:53 +0100 | [diff] [blame] | 3532 | double value = StringToDouble(isolate()->unicode_cache(), |
| 3533 | scanner().literal_ascii_string(), |
Steve Block | 9fac840 | 2011-05-12 15:51:54 +0100 | [diff] [blame] | 3534 | ALLOW_HEX | ALLOW_OCTALS); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 3535 | key = NewNumberLiteral(value); |
| 3536 | break; |
| 3537 | } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 3538 | default: |
Ben Murdoch | bb769b2 | 2010-08-11 14:56:33 +0100 | [diff] [blame] | 3539 | if (Token::IsKeyword(next)) { |
| 3540 | Consume(next); |
Iain Merrick | 9ac36c9 | 2010-09-13 15:29:50 +0100 | [diff] [blame] | 3541 | Handle<String> string = GetSymbol(CHECK_OK); |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame^] | 3542 | key = NewLiteral(string); |
Ben Murdoch | bb769b2 | 2010-08-11 14:56:33 +0100 | [diff] [blame] | 3543 | } else { |
| 3544 | // Unexpected token. |
| 3545 | Token::Value next = Next(); |
| 3546 | ReportUnexpectedToken(next); |
| 3547 | *ok = false; |
| 3548 | return NULL; |
| 3549 | } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 3550 | } |
| 3551 | |
| 3552 | Expect(Token::COLON, CHECK_OK); |
| 3553 | Expression* value = ParseAssignmentExpression(true, CHECK_OK); |
| 3554 | |
| 3555 | ObjectLiteral::Property* property = |
Ben Murdoch | 8b112d2 | 2011-06-08 16:22:53 +0100 | [diff] [blame] | 3556 | new(zone()) ObjectLiteral::Property(key, value); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 3557 | |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 3558 | // Mark object literals that contain function literals and pretenure the |
| 3559 | // literal so it can be added as a constant function property. |
| 3560 | if (value->AsFunctionLiteral() != NULL) { |
| 3561 | has_function = true; |
| 3562 | value->AsFunctionLiteral()->set_pretenure(true); |
| 3563 | } |
| 3564 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 3565 | // Count CONSTANT or COMPUTED properties to maintain the enumeration order. |
| 3566 | if (IsBoilerplateProperty(property)) number_of_boilerplate_properties++; |
Steve Block | 1e0659c | 2011-05-24 12:43:12 +0100 | [diff] [blame] | 3567 | // Validate the property |
| 3568 | checker.CheckProperty(property, loc, CHECK_OK); |
Teng-Hui Zhu | 3e5fa29 | 2010-11-09 16:16:48 -0800 | [diff] [blame] | 3569 | properties->Add(property); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 3570 | |
| 3571 | // TODO(1240767): Consider allowing trailing comma. |
| 3572 | if (peek() != Token::RBRACE) Expect(Token::COMMA, CHECK_OK); |
Kristian Monsen | 80d68ea | 2010-09-08 11:05:35 +0100 | [diff] [blame] | 3573 | |
| 3574 | if (fni_ != NULL) { |
| 3575 | fni_->Infer(); |
| 3576 | fni_->Leave(); |
| 3577 | } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 3578 | } |
| 3579 | Expect(Token::RBRACE, CHECK_OK); |
Steve Block | 1e0659c | 2011-05-24 12:43:12 +0100 | [diff] [blame] | 3580 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 3581 | // Computation of literal_index must happen before pre parse bailout. |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 3582 | int literal_index = lexical_scope_->NextMaterializedLiteralIndex(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 3583 | |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 3584 | Handle<FixedArray> constant_properties = isolate()->factory()->NewFixedArray( |
| 3585 | number_of_boilerplate_properties * 2, TENURED); |
Leon Clarke | 4515c47 | 2010-02-03 11:58:03 +0000 | [diff] [blame] | 3586 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 3587 | bool is_simple = true; |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 3588 | bool fast_elements = true; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 3589 | int depth = 1; |
Teng-Hui Zhu | 3e5fa29 | 2010-11-09 16:16:48 -0800 | [diff] [blame] | 3590 | BuildObjectLiteralConstantProperties(properties, |
Leon Clarke | 4515c47 | 2010-02-03 11:58:03 +0000 | [diff] [blame] | 3591 | constant_properties, |
| 3592 | &is_simple, |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 3593 | &fast_elements, |
Leon Clarke | 4515c47 | 2010-02-03 11:58:03 +0000 | [diff] [blame] | 3594 | &depth); |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame^] | 3595 | return new(zone()) ObjectLiteral(isolate(), |
| 3596 | constant_properties, |
| 3597 | properties, |
| 3598 | literal_index, |
| 3599 | is_simple, |
| 3600 | fast_elements, |
| 3601 | depth, |
| 3602 | has_function); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 3603 | } |
| 3604 | |
| 3605 | |
| 3606 | Expression* Parser::ParseRegExpLiteral(bool seen_equal, bool* ok) { |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 3607 | if (!scanner().ScanRegExpPattern(seen_equal)) { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 3608 | Next(); |
| 3609 | ReportMessage("unterminated_regexp", Vector<const char*>::empty()); |
| 3610 | *ok = false; |
| 3611 | return NULL; |
| 3612 | } |
| 3613 | |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 3614 | int literal_index = lexical_scope_->NextMaterializedLiteralIndex(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 3615 | |
Steve Block | 9fac840 | 2011-05-12 15:51:54 +0100 | [diff] [blame] | 3616 | Handle<String> js_pattern = NextLiteralString(TENURED); |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 3617 | scanner().ScanRegExpFlags(); |
Steve Block | 9fac840 | 2011-05-12 15:51:54 +0100 | [diff] [blame] | 3618 | Handle<String> js_flags = NextLiteralString(TENURED); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 3619 | Next(); |
| 3620 | |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame^] | 3621 | return new(zone()) RegExpLiteral( |
| 3622 | isolate(), js_pattern, js_flags, literal_index); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 3623 | } |
| 3624 | |
| 3625 | |
| 3626 | ZoneList<Expression*>* Parser::ParseArguments(bool* ok) { |
| 3627 | // Arguments :: |
| 3628 | // '(' (AssignmentExpression)*[','] ')' |
| 3629 | |
Ben Murdoch | 257744e | 2011-11-30 15:57:28 +0000 | [diff] [blame] | 3630 | ZoneList<Expression*>* result = new(zone()) ZoneList<Expression*>(4); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 3631 | Expect(Token::LPAREN, CHECK_OK); |
| 3632 | bool done = (peek() == Token::RPAREN); |
| 3633 | while (!done) { |
| 3634 | Expression* argument = ParseAssignmentExpression(true, CHECK_OK); |
Teng-Hui Zhu | 3e5fa29 | 2010-11-09 16:16:48 -0800 | [diff] [blame] | 3635 | result->Add(argument); |
Steve Block | 053d10c | 2011-06-13 19:13:29 +0100 | [diff] [blame] | 3636 | if (result->length() > kMaxNumFunctionParameters) { |
| 3637 | ReportMessageAt(scanner().location(), "too_many_arguments", |
| 3638 | Vector<const char*>::empty()); |
| 3639 | *ok = false; |
| 3640 | return NULL; |
| 3641 | } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 3642 | done = (peek() == Token::RPAREN); |
| 3643 | if (!done) Expect(Token::COMMA, CHECK_OK); |
| 3644 | } |
| 3645 | Expect(Token::RPAREN, CHECK_OK); |
Teng-Hui Zhu | 3e5fa29 | 2010-11-09 16:16:48 -0800 | [diff] [blame] | 3646 | return result; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 3647 | } |
| 3648 | |
| 3649 | |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame^] | 3650 | FunctionLiteral* Parser::ParseFunctionLiteral(Handle<String> function_name, |
| 3651 | bool name_is_strict_reserved, |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 3652 | int function_token_position, |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame^] | 3653 | FunctionLiteral::Type type, |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 3654 | bool* ok) { |
| 3655 | // Function :: |
| 3656 | // '(' FormalParameterList? ')' '{' FunctionBody '}' |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 3657 | |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame^] | 3658 | // Anonymous functions were passed either the empty symbol or a null |
| 3659 | // handle as the function name. Remember if we were passed a non-empty |
| 3660 | // handle to decide whether to invoke function name inference. |
| 3661 | bool should_infer_name = function_name.is_null(); |
| 3662 | |
| 3663 | // We want a non-null handle as the function name. |
| 3664 | if (should_infer_name) { |
| 3665 | function_name = isolate()->factory()->empty_symbol(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 3666 | } |
| 3667 | |
| 3668 | int num_parameters = 0; |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame^] | 3669 | // Function declarations are hoisted. |
| 3670 | Scope* scope = (type == FunctionLiteral::DECLARATION) |
| 3671 | ? NewScope(top_scope_->DeclarationScope(), Scope::FUNCTION_SCOPE, false) |
| 3672 | : NewScope(top_scope_, Scope::FUNCTION_SCOPE, inside_with()); |
Ben Murdoch | 257744e | 2011-11-30 15:57:28 +0000 | [diff] [blame] | 3673 | ZoneList<Statement*>* body = new(zone()) ZoneList<Statement*>(8); |
Ben Murdoch | 8b112d2 | 2011-06-08 16:22:53 +0100 | [diff] [blame] | 3674 | int materialized_literal_count; |
| 3675 | int expected_property_count; |
| 3676 | int start_pos; |
| 3677 | int end_pos; |
| 3678 | bool only_simple_this_property_assignments; |
| 3679 | Handle<FixedArray> this_property_assignments; |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame^] | 3680 | bool has_duplicate_parameters = false; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 3681 | // Parse function body. |
Ben Murdoch | 8b112d2 | 2011-06-08 16:22:53 +0100 | [diff] [blame] | 3682 | { LexicalScope lexical_scope(this, scope, isolate()); |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame^] | 3683 | top_scope_->SetScopeName(function_name); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 3684 | |
| 3685 | // FormalParameterList :: |
| 3686 | // '(' (Identifier)*[','] ')' |
| 3687 | Expect(Token::LPAREN, CHECK_OK); |
Ben Murdoch | 8b112d2 | 2011-06-08 16:22:53 +0100 | [diff] [blame] | 3688 | start_pos = scanner().location().beg_pos; |
Ben Murdoch | 257744e | 2011-11-30 15:57:28 +0000 | [diff] [blame] | 3689 | Scanner::Location name_loc = Scanner::Location::invalid(); |
| 3690 | Scanner::Location dupe_loc = Scanner::Location::invalid(); |
| 3691 | Scanner::Location reserved_loc = Scanner::Location::invalid(); |
Steve Block | 1e0659c | 2011-05-24 12:43:12 +0100 | [diff] [blame] | 3692 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 3693 | bool done = (peek() == Token::RPAREN); |
| 3694 | while (!done) { |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame^] | 3695 | bool is_strict_reserved = false; |
Steve Block | 1e0659c | 2011-05-24 12:43:12 +0100 | [diff] [blame] | 3696 | Handle<String> param_name = |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame^] | 3697 | ParseIdentifierOrStrictReservedWord(&is_strict_reserved, |
| 3698 | CHECK_OK); |
Steve Block | 1e0659c | 2011-05-24 12:43:12 +0100 | [diff] [blame] | 3699 | |
| 3700 | // Store locations for possible future error reports. |
| 3701 | if (!name_loc.IsValid() && IsEvalOrArguments(param_name)) { |
| 3702 | name_loc = scanner().location(); |
| 3703 | } |
| 3704 | if (!dupe_loc.IsValid() && top_scope_->IsDeclared(param_name)) { |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame^] | 3705 | has_duplicate_parameters = true; |
Steve Block | 1e0659c | 2011-05-24 12:43:12 +0100 | [diff] [blame] | 3706 | dupe_loc = scanner().location(); |
| 3707 | } |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame^] | 3708 | if (!reserved_loc.IsValid() && is_strict_reserved) { |
Steve Block | 1e0659c | 2011-05-24 12:43:12 +0100 | [diff] [blame] | 3709 | reserved_loc = scanner().location(); |
| 3710 | } |
| 3711 | |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame^] | 3712 | top_scope_->DeclareParameter(param_name); |
Teng-Hui Zhu | 3e5fa29 | 2010-11-09 16:16:48 -0800 | [diff] [blame] | 3713 | num_parameters++; |
Steve Block | 1e0659c | 2011-05-24 12:43:12 +0100 | [diff] [blame] | 3714 | if (num_parameters > kMaxNumFunctionParameters) { |
| 3715 | ReportMessageAt(scanner().location(), "too_many_parameters", |
| 3716 | Vector<const char*>::empty()); |
| 3717 | *ok = false; |
| 3718 | return NULL; |
| 3719 | } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 3720 | done = (peek() == Token::RPAREN); |
| 3721 | if (!done) Expect(Token::COMMA, CHECK_OK); |
| 3722 | } |
| 3723 | Expect(Token::RPAREN, CHECK_OK); |
| 3724 | |
| 3725 | Expect(Token::LBRACE, CHECK_OK); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 3726 | |
| 3727 | // If we have a named function expression, we add a local variable |
| 3728 | // declaration to the body of the function with the name of the |
| 3729 | // function and let it refer to the function itself (closure). |
| 3730 | // NOTE: We create a proxy and resolve it here so that in the |
| 3731 | // future we can change the AST to only refer to VariableProxies |
| 3732 | // instead of Variables and Proxis as is the case now. |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame^] | 3733 | if (type == FunctionLiteral::NAMED_EXPRESSION) { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 3734 | Variable* fvar = top_scope_->DeclareFunctionVar(function_name); |
| 3735 | VariableProxy* fproxy = |
| 3736 | top_scope_->NewUnresolved(function_name, inside_with()); |
| 3737 | fproxy->BindTo(fvar); |
Ben Murdoch | 8b112d2 | 2011-06-08 16:22:53 +0100 | [diff] [blame] | 3738 | body->Add(new(zone()) ExpressionStatement( |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame^] | 3739 | new(zone()) Assignment(isolate(), |
| 3740 | Token::INIT_CONST, |
| 3741 | fproxy, |
| 3742 | new(zone()) ThisFunction(isolate()), |
| 3743 | RelocInfo::kNoPosition))); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 3744 | } |
| 3745 | |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame^] | 3746 | // Determine if the function will be lazily compiled. The mode can only |
| 3747 | // be PARSE_LAZILY if the --lazy flag is true. We will not lazily |
| 3748 | // compile if we do not have preparser data for the function. |
Ben Murdoch | b8e0da2 | 2011-05-16 14:20:40 +0100 | [diff] [blame] | 3749 | bool is_lazily_compiled = (mode() == PARSE_LAZILY && |
| 3750 | top_scope_->outer_scope()->is_global_scope() && |
| 3751 | top_scope_->HasTrivialOuterContext() && |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame^] | 3752 | !parenthesized_function_ && |
| 3753 | pre_data() != NULL); |
Ben Murdoch | b8e0da2 | 2011-05-16 14:20:40 +0100 | [diff] [blame] | 3754 | parenthesized_function_ = false; // The bit was set for this function only. |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 3755 | |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame^] | 3756 | if (is_lazily_compiled) { |
| 3757 | int function_block_pos = scanner().location().beg_pos; |
Kristian Monsen | 80d68ea | 2010-09-08 11:05:35 +0100 | [diff] [blame] | 3758 | FunctionEntry entry = pre_data()->GetFunctionEntry(function_block_pos); |
Leon Clarke | ac95265 | 2010-07-15 11:15:24 +0100 | [diff] [blame] | 3759 | if (!entry.is_valid()) { |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame^] | 3760 | // There is no preparser data for the function, we will not lazily |
| 3761 | // compile after all. |
| 3762 | is_lazily_compiled = false; |
| 3763 | } else { |
| 3764 | end_pos = entry.end_pos(); |
| 3765 | if (end_pos <= function_block_pos) { |
| 3766 | // End position greater than end of stream is safe, and hard to check. |
| 3767 | ReportInvalidPreparseData(function_name, CHECK_OK); |
| 3768 | } |
| 3769 | isolate()->counters()->total_preparse_skipped()->Increment( |
| 3770 | end_pos - function_block_pos); |
| 3771 | // Seek to position just before terminal '}'. |
| 3772 | scanner().SeekForward(end_pos - 1); |
| 3773 | materialized_literal_count = entry.literal_count(); |
| 3774 | expected_property_count = entry.property_count(); |
| 3775 | if (entry.strict_mode()) top_scope_->EnableStrictMode(); |
| 3776 | only_simple_this_property_assignments = false; |
| 3777 | this_property_assignments = isolate()->factory()->empty_fixed_array(); |
| 3778 | Expect(Token::RBRACE, CHECK_OK); |
Leon Clarke | ac95265 | 2010-07-15 11:15:24 +0100 | [diff] [blame] | 3779 | } |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame^] | 3780 | } |
| 3781 | |
| 3782 | if (!is_lazily_compiled) { |
Teng-Hui Zhu | 3e5fa29 | 2010-11-09 16:16:48 -0800 | [diff] [blame] | 3783 | ParseSourceElements(body, Token::RBRACE, CHECK_OK); |
| 3784 | |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 3785 | materialized_literal_count = lexical_scope.materialized_literal_count(); |
| 3786 | expected_property_count = lexical_scope.expected_property_count(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 3787 | only_simple_this_property_assignments = |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 3788 | lexical_scope.only_simple_this_property_assignments(); |
| 3789 | this_property_assignments = lexical_scope.this_property_assignments(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 3790 | |
Kristian Monsen | 80d68ea | 2010-09-08 11:05:35 +0100 | [diff] [blame] | 3791 | Expect(Token::RBRACE, CHECK_OK); |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 3792 | end_pos = scanner().location().end_pos; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 3793 | } |
| 3794 | |
Steve Block | 1e0659c | 2011-05-24 12:43:12 +0100 | [diff] [blame] | 3795 | // Validate strict mode. |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 3796 | if (top_scope_->is_strict_mode()) { |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame^] | 3797 | if (IsEvalOrArguments(function_name)) { |
Steve Block | 1e0659c | 2011-05-24 12:43:12 +0100 | [diff] [blame] | 3798 | int position = function_token_position != RelocInfo::kNoPosition |
| 3799 | ? function_token_position |
| 3800 | : (start_pos > 0 ? start_pos - 1 : start_pos); |
| 3801 | Scanner::Location location = Scanner::Location(position, start_pos); |
| 3802 | ReportMessageAt(location, |
| 3803 | "strict_function_name", Vector<const char*>::empty()); |
| 3804 | *ok = false; |
| 3805 | return NULL; |
| 3806 | } |
| 3807 | if (name_loc.IsValid()) { |
| 3808 | ReportMessageAt(name_loc, "strict_param_name", |
| 3809 | Vector<const char*>::empty()); |
| 3810 | *ok = false; |
| 3811 | return NULL; |
| 3812 | } |
| 3813 | if (dupe_loc.IsValid()) { |
| 3814 | ReportMessageAt(dupe_loc, "strict_param_dupe", |
| 3815 | Vector<const char*>::empty()); |
| 3816 | *ok = false; |
| 3817 | return NULL; |
| 3818 | } |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame^] | 3819 | if (name_is_strict_reserved) { |
Steve Block | 1e0659c | 2011-05-24 12:43:12 +0100 | [diff] [blame] | 3820 | int position = function_token_position != RelocInfo::kNoPosition |
| 3821 | ? function_token_position |
| 3822 | : (start_pos > 0 ? start_pos - 1 : start_pos); |
| 3823 | Scanner::Location location = Scanner::Location(position, start_pos); |
| 3824 | ReportMessageAt(location, "strict_reserved_word", |
| 3825 | Vector<const char*>::empty()); |
| 3826 | *ok = false; |
| 3827 | return NULL; |
| 3828 | } |
| 3829 | if (reserved_loc.IsValid()) { |
| 3830 | ReportMessageAt(reserved_loc, "strict_reserved_word", |
| 3831 | Vector<const char*>::empty()); |
| 3832 | *ok = false; |
| 3833 | return NULL; |
| 3834 | } |
| 3835 | CheckOctalLiteral(start_pos, end_pos, CHECK_OK); |
| 3836 | } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 3837 | } |
Ben Murdoch | 8b112d2 | 2011-06-08 16:22:53 +0100 | [diff] [blame] | 3838 | |
| 3839 | FunctionLiteral* function_literal = |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame^] | 3840 | new(zone()) FunctionLiteral(isolate(), |
| 3841 | function_name, |
Ben Murdoch | 8b112d2 | 2011-06-08 16:22:53 +0100 | [diff] [blame] | 3842 | scope, |
| 3843 | body, |
| 3844 | materialized_literal_count, |
| 3845 | expected_property_count, |
| 3846 | only_simple_this_property_assignments, |
| 3847 | this_property_assignments, |
| 3848 | num_parameters, |
| 3849 | start_pos, |
| 3850 | end_pos, |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame^] | 3851 | type, |
| 3852 | has_duplicate_parameters); |
Ben Murdoch | 8b112d2 | 2011-06-08 16:22:53 +0100 | [diff] [blame] | 3853 | function_literal->set_function_token_position(function_token_position); |
| 3854 | |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame^] | 3855 | if (fni_ != NULL && should_infer_name) fni_->AddFunction(function_literal); |
Ben Murdoch | 8b112d2 | 2011-06-08 16:22:53 +0100 | [diff] [blame] | 3856 | return function_literal; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 3857 | } |
| 3858 | |
| 3859 | |
| 3860 | Expression* Parser::ParseV8Intrinsic(bool* ok) { |
| 3861 | // CallRuntime :: |
| 3862 | // '%' Identifier Arguments |
| 3863 | |
| 3864 | Expect(Token::MOD, CHECK_OK); |
| 3865 | Handle<String> name = ParseIdentifier(CHECK_OK); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 3866 | ZoneList<Expression*>* args = ParseArguments(CHECK_OK); |
Kristian Monsen | 0d5e116 | 2010-09-30 15:31:59 +0100 | [diff] [blame] | 3867 | |
| 3868 | if (extension_ != NULL) { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 3869 | // The extension structures are only accessible while parsing the |
| 3870 | // very first time not when reparsing because of lazy compilation. |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame^] | 3871 | top_scope_->DeclarationScope()->ForceEagerCompilation(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 3872 | } |
| 3873 | |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 3874 | const Runtime::Function* function = Runtime::FunctionForSymbol(name); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 3875 | |
Kristian Monsen | 0d5e116 | 2010-09-30 15:31:59 +0100 | [diff] [blame] | 3876 | // Check for built-in IS_VAR macro. |
| 3877 | if (function != NULL && |
| 3878 | function->intrinsic_type == Runtime::RUNTIME && |
| 3879 | function->function_id == Runtime::kIS_VAR) { |
| 3880 | // %IS_VAR(x) evaluates to x if x is a variable, |
| 3881 | // leads to a parse error otherwise. Could be implemented as an |
| 3882 | // inline function %_IS_VAR(x) to eliminate this special case. |
| 3883 | if (args->length() == 1 && args->at(0)->AsVariableProxy() != NULL) { |
| 3884 | return args->at(0); |
| 3885 | } else { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 3886 | ReportMessage("unable_to_parse", Vector<const char*>::empty()); |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 3887 | *ok = false; |
| 3888 | return NULL; |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 3889 | } |
| 3890 | } |
| 3891 | |
Kristian Monsen | 0d5e116 | 2010-09-30 15:31:59 +0100 | [diff] [blame] | 3892 | // Check that the expected number of arguments are being passed. |
| 3893 | if (function != NULL && |
| 3894 | function->nargs != -1 && |
| 3895 | function->nargs != args->length()) { |
| 3896 | ReportMessage("illegal_access", Vector<const char*>::empty()); |
| 3897 | *ok = false; |
| 3898 | return NULL; |
| 3899 | } |
| 3900 | |
| 3901 | // We have a valid intrinsics call or a call to a builtin. |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame^] | 3902 | return new(zone()) CallRuntime(isolate(), name, function, args); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 3903 | } |
| 3904 | |
| 3905 | |
Steve Block | 1e0659c | 2011-05-24 12:43:12 +0100 | [diff] [blame] | 3906 | bool Parser::peek_any_identifier() { |
| 3907 | Token::Value next = peek(); |
| 3908 | return next == Token::IDENTIFIER || |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame^] | 3909 | next == Token::FUTURE_RESERVED_WORD || |
| 3910 | next == Token::FUTURE_STRICT_RESERVED_WORD; |
Steve Block | 1e0659c | 2011-05-24 12:43:12 +0100 | [diff] [blame] | 3911 | } |
| 3912 | |
| 3913 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 3914 | void Parser::Consume(Token::Value token) { |
| 3915 | Token::Value next = Next(); |
| 3916 | USE(next); |
| 3917 | USE(token); |
| 3918 | ASSERT(next == token); |
| 3919 | } |
| 3920 | |
| 3921 | |
| 3922 | void Parser::Expect(Token::Value token, bool* ok) { |
| 3923 | Token::Value next = Next(); |
| 3924 | if (next == token) return; |
| 3925 | ReportUnexpectedToken(next); |
| 3926 | *ok = false; |
| 3927 | } |
| 3928 | |
| 3929 | |
Leon Clarke | 4515c47 | 2010-02-03 11:58:03 +0000 | [diff] [blame] | 3930 | bool Parser::Check(Token::Value token) { |
| 3931 | Token::Value next = peek(); |
| 3932 | if (next == token) { |
| 3933 | Consume(next); |
| 3934 | return true; |
| 3935 | } |
| 3936 | return false; |
| 3937 | } |
| 3938 | |
| 3939 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 3940 | void Parser::ExpectSemicolon(bool* ok) { |
| 3941 | // Check for automatic semicolon insertion according to |
| 3942 | // the rules given in ECMA-262, section 7.9, page 21. |
| 3943 | Token::Value tok = peek(); |
| 3944 | if (tok == Token::SEMICOLON) { |
| 3945 | Next(); |
| 3946 | return; |
| 3947 | } |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame^] | 3948 | if (scanner().HasAnyLineTerminatorBeforeNext() || |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 3949 | tok == Token::RBRACE || |
| 3950 | tok == Token::EOS) { |
| 3951 | return; |
| 3952 | } |
| 3953 | Expect(Token::SEMICOLON, ok); |
| 3954 | } |
| 3955 | |
| 3956 | |
| 3957 | Literal* Parser::GetLiteralUndefined() { |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame^] | 3958 | return NewLiteral(isolate()->factory()->undefined_value()); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 3959 | } |
| 3960 | |
| 3961 | |
| 3962 | Literal* Parser::GetLiteralTheHole() { |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame^] | 3963 | return NewLiteral(isolate()->factory()->the_hole_value()); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 3964 | } |
| 3965 | |
| 3966 | |
| 3967 | Literal* Parser::GetLiteralNumber(double value) { |
| 3968 | return NewNumberLiteral(value); |
| 3969 | } |
| 3970 | |
| 3971 | |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame^] | 3972 | // Parses and identifier that is valid for the current scope, in particular it |
| 3973 | // fails on strict mode future reserved keywords in a strict scope. |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 3974 | Handle<String> Parser::ParseIdentifier(bool* ok) { |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame^] | 3975 | if (top_scope_->is_strict_mode()) { |
| 3976 | Expect(Token::IDENTIFIER, ok); |
| 3977 | } else if (!Check(Token::IDENTIFIER)) { |
| 3978 | Expect(Token::FUTURE_STRICT_RESERVED_WORD, ok); |
| 3979 | } |
| 3980 | if (!*ok) return Handle<String>(); |
| 3981 | return GetSymbol(ok); |
Steve Block | 1e0659c | 2011-05-24 12:43:12 +0100 | [diff] [blame] | 3982 | } |
| 3983 | |
| 3984 | |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame^] | 3985 | // Parses and identifier or a strict mode future reserved word, and indicate |
| 3986 | // whether it is strict mode future reserved. |
| 3987 | Handle<String> Parser::ParseIdentifierOrStrictReservedWord( |
| 3988 | bool* is_strict_reserved, bool* ok) { |
| 3989 | *is_strict_reserved = false; |
| 3990 | if (!Check(Token::IDENTIFIER)) { |
| 3991 | Expect(Token::FUTURE_STRICT_RESERVED_WORD, ok); |
| 3992 | *is_strict_reserved = true; |
Steve Block | 1e0659c | 2011-05-24 12:43:12 +0100 | [diff] [blame] | 3993 | } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 3994 | if (!*ok) return Handle<String>(); |
Iain Merrick | 9ac36c9 | 2010-09-13 15:29:50 +0100 | [diff] [blame] | 3995 | return GetSymbol(ok); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 3996 | } |
| 3997 | |
Ben Murdoch | bb769b2 | 2010-08-11 14:56:33 +0100 | [diff] [blame] | 3998 | |
| 3999 | Handle<String> Parser::ParseIdentifierName(bool* ok) { |
| 4000 | Token::Value next = Next(); |
Steve Block | 1e0659c | 2011-05-24 12:43:12 +0100 | [diff] [blame] | 4001 | if (next != Token::IDENTIFIER && |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame^] | 4002 | next != Token::FUTURE_RESERVED_WORD && |
| 4003 | next != Token::FUTURE_STRICT_RESERVED_WORD && |
| 4004 | !Token::IsKeyword(next)) { |
Ben Murdoch | bb769b2 | 2010-08-11 14:56:33 +0100 | [diff] [blame] | 4005 | ReportUnexpectedToken(next); |
| 4006 | *ok = false; |
| 4007 | return Handle<String>(); |
| 4008 | } |
Iain Merrick | 9ac36c9 | 2010-09-13 15:29:50 +0100 | [diff] [blame] | 4009 | return GetSymbol(ok); |
Ben Murdoch | bb769b2 | 2010-08-11 14:56:33 +0100 | [diff] [blame] | 4010 | } |
| 4011 | |
| 4012 | |
Steve Block | 1e0659c | 2011-05-24 12:43:12 +0100 | [diff] [blame] | 4013 | // Checks LHS expression for assignment and prefix/postfix increment/decrement |
| 4014 | // in strict mode. |
| 4015 | void Parser::CheckStrictModeLValue(Expression* expression, |
| 4016 | const char* error, |
| 4017 | bool* ok) { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 4018 | ASSERT(top_scope_->is_strict_mode()); |
Steve Block | 1e0659c | 2011-05-24 12:43:12 +0100 | [diff] [blame] | 4019 | VariableProxy* lhs = expression != NULL |
| 4020 | ? expression->AsVariableProxy() |
| 4021 | : NULL; |
| 4022 | |
| 4023 | if (lhs != NULL && !lhs->is_this() && IsEvalOrArguments(lhs->name())) { |
| 4024 | ReportMessage(error, Vector<const char*>::empty()); |
| 4025 | *ok = false; |
| 4026 | } |
| 4027 | } |
| 4028 | |
| 4029 | |
Ben Murdoch | 257744e | 2011-11-30 15:57:28 +0000 | [diff] [blame] | 4030 | // Checks whether an octal literal was last seen between beg_pos and end_pos. |
| 4031 | // If so, reports an error. Only called for strict mode. |
Steve Block | 1e0659c | 2011-05-24 12:43:12 +0100 | [diff] [blame] | 4032 | void Parser::CheckOctalLiteral(int beg_pos, int end_pos, bool* ok) { |
Ben Murdoch | 257744e | 2011-11-30 15:57:28 +0000 | [diff] [blame] | 4033 | Scanner::Location octal = scanner().octal_position(); |
| 4034 | if (octal.IsValid() && |
| 4035 | beg_pos <= octal.beg_pos && |
| 4036 | octal.end_pos <= end_pos) { |
| 4037 | ReportMessageAt(octal, "strict_octal_literal", |
Steve Block | 1e0659c | 2011-05-24 12:43:12 +0100 | [diff] [blame] | 4038 | Vector<const char*>::empty()); |
| 4039 | scanner().clear_octal_position(); |
| 4040 | *ok = false; |
| 4041 | } |
| 4042 | } |
| 4043 | |
| 4044 | |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame^] | 4045 | // This function reads an identifier name and determines whether or not it |
Steve Block | 1e0659c | 2011-05-24 12:43:12 +0100 | [diff] [blame] | 4046 | // is 'get' or 'set'. |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame^] | 4047 | Handle<String> Parser::ParseIdentifierNameOrGetOrSet(bool* is_get, |
| 4048 | bool* is_set, |
| 4049 | bool* ok) { |
| 4050 | Handle<String> result = ParseIdentifierName(ok); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4051 | if (!*ok) return Handle<String>(); |
Steve Block | 9fac840 | 2011-05-12 15:51:54 +0100 | [diff] [blame] | 4052 | if (scanner().is_literal_ascii() && scanner().literal_length() == 3) { |
| 4053 | const char* token = scanner().literal_ascii_string().start(); |
| 4054 | *is_get = strncmp(token, "get", 3) == 0; |
| 4055 | *is_set = !*is_get && strncmp(token, "set", 3) == 0; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4056 | } |
Steve Block | 1e0659c | 2011-05-24 12:43:12 +0100 | [diff] [blame] | 4057 | return result; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4058 | } |
| 4059 | |
| 4060 | |
| 4061 | // ---------------------------------------------------------------------------- |
| 4062 | // Parser support |
| 4063 | |
| 4064 | |
| 4065 | bool Parser::TargetStackContainsLabel(Handle<String> label) { |
| 4066 | for (Target* t = target_stack_; t != NULL; t = t->previous()) { |
| 4067 | BreakableStatement* stat = t->node()->AsBreakableStatement(); |
| 4068 | if (stat != NULL && ContainsLabel(stat->labels(), label)) |
| 4069 | return true; |
| 4070 | } |
| 4071 | return false; |
| 4072 | } |
| 4073 | |
| 4074 | |
| 4075 | BreakableStatement* Parser::LookupBreakTarget(Handle<String> label, bool* ok) { |
| 4076 | bool anonymous = label.is_null(); |
| 4077 | for (Target* t = target_stack_; t != NULL; t = t->previous()) { |
| 4078 | BreakableStatement* stat = t->node()->AsBreakableStatement(); |
| 4079 | if (stat == NULL) continue; |
| 4080 | if ((anonymous && stat->is_target_for_anonymous()) || |
| 4081 | (!anonymous && ContainsLabel(stat->labels(), label))) { |
| 4082 | RegisterTargetUse(stat->break_target(), t->previous()); |
| 4083 | return stat; |
| 4084 | } |
| 4085 | } |
| 4086 | return NULL; |
| 4087 | } |
| 4088 | |
| 4089 | |
| 4090 | IterationStatement* Parser::LookupContinueTarget(Handle<String> label, |
| 4091 | bool* ok) { |
| 4092 | bool anonymous = label.is_null(); |
| 4093 | for (Target* t = target_stack_; t != NULL; t = t->previous()) { |
| 4094 | IterationStatement* stat = t->node()->AsIterationStatement(); |
| 4095 | if (stat == NULL) continue; |
| 4096 | |
| 4097 | ASSERT(stat->is_target_for_anonymous()); |
| 4098 | if (anonymous || ContainsLabel(stat->labels(), label)) { |
| 4099 | RegisterTargetUse(stat->continue_target(), t->previous()); |
| 4100 | return stat; |
| 4101 | } |
| 4102 | } |
| 4103 | return NULL; |
| 4104 | } |
| 4105 | |
| 4106 | |
Ben Murdoch | 8b112d2 | 2011-06-08 16:22:53 +0100 | [diff] [blame] | 4107 | void Parser::RegisterTargetUse(Label* target, Target* stop) { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4108 | // Register that a break target found at the given stop in the |
| 4109 | // target stack has been used from the top of the target stack. Add |
| 4110 | // the break target to any TargetCollectors passed on the stack. |
| 4111 | for (Target* t = target_stack_; t != stop; t = t->previous()) { |
| 4112 | TargetCollector* collector = t->node()->AsTargetCollector(); |
| 4113 | if (collector != NULL) collector->AddTarget(target); |
| 4114 | } |
| 4115 | } |
| 4116 | |
| 4117 | |
| 4118 | Literal* Parser::NewNumberLiteral(double number) { |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame^] | 4119 | return NewLiteral(isolate()->factory()->NewNumber(number, TENURED)); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4120 | } |
| 4121 | |
| 4122 | |
| 4123 | Expression* Parser::NewThrowReferenceError(Handle<String> type) { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 4124 | return NewThrowError(isolate()->factory()->MakeReferenceError_symbol(), |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4125 | type, HandleVector<Object>(NULL, 0)); |
| 4126 | } |
| 4127 | |
| 4128 | |
| 4129 | Expression* Parser::NewThrowSyntaxError(Handle<String> type, |
| 4130 | Handle<Object> first) { |
| 4131 | int argc = first.is_null() ? 0 : 1; |
| 4132 | Vector< Handle<Object> > arguments = HandleVector<Object>(&first, argc); |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 4133 | return NewThrowError( |
| 4134 | isolate()->factory()->MakeSyntaxError_symbol(), type, arguments); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4135 | } |
| 4136 | |
| 4137 | |
| 4138 | Expression* Parser::NewThrowTypeError(Handle<String> type, |
| 4139 | Handle<Object> first, |
| 4140 | Handle<Object> second) { |
| 4141 | ASSERT(!first.is_null() && !second.is_null()); |
| 4142 | Handle<Object> elements[] = { first, second }; |
| 4143 | Vector< Handle<Object> > arguments = |
| 4144 | HandleVector<Object>(elements, ARRAY_SIZE(elements)); |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 4145 | return NewThrowError( |
| 4146 | isolate()->factory()->MakeTypeError_symbol(), type, arguments); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4147 | } |
| 4148 | |
| 4149 | |
| 4150 | Expression* Parser::NewThrowError(Handle<String> constructor, |
| 4151 | Handle<String> type, |
| 4152 | Vector< Handle<Object> > arguments) { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4153 | int argc = arguments.length(); |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 4154 | Handle<FixedArray> elements = isolate()->factory()->NewFixedArray(argc, |
| 4155 | TENURED); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4156 | for (int i = 0; i < argc; i++) { |
| 4157 | Handle<Object> element = arguments[i]; |
| 4158 | if (!element.is_null()) { |
Steve Block | 1e0659c | 2011-05-24 12:43:12 +0100 | [diff] [blame] | 4159 | elements->set(i, *element); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4160 | } |
| 4161 | } |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 4162 | Handle<JSArray> array = isolate()->factory()->NewJSArrayWithElements(elements, |
| 4163 | TENURED); |
Steve Block | 1e0659c | 2011-05-24 12:43:12 +0100 | [diff] [blame] | 4164 | |
Ben Murdoch | 257744e | 2011-11-30 15:57:28 +0000 | [diff] [blame] | 4165 | ZoneList<Expression*>* args = new(zone()) ZoneList<Expression*>(2); |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame^] | 4166 | args->Add(NewLiteral(type)); |
| 4167 | args->Add(NewLiteral(array)); |
| 4168 | CallRuntime* call_constructor = new(zone()) CallRuntime(isolate(), |
| 4169 | constructor, |
| 4170 | NULL, |
| 4171 | args); |
| 4172 | return new(zone()) Throw(isolate(), |
| 4173 | call_constructor, |
| 4174 | scanner().location().beg_pos); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4175 | } |
| 4176 | |
Leon Clarke | 4515c47 | 2010-02-03 11:58:03 +0000 | [diff] [blame] | 4177 | // ---------------------------------------------------------------------------- |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4178 | // Regular expressions |
| 4179 | |
| 4180 | |
| 4181 | RegExpParser::RegExpParser(FlatStringReader* in, |
| 4182 | Handle<String>* error, |
| 4183 | bool multiline) |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 4184 | : isolate_(Isolate::Current()), |
| 4185 | error_(error), |
| 4186 | captures_(NULL), |
| 4187 | in_(in), |
| 4188 | current_(kEndMarker), |
| 4189 | next_pos_(0), |
| 4190 | capture_count_(0), |
| 4191 | has_more_(true), |
| 4192 | multiline_(multiline), |
| 4193 | simple_(false), |
| 4194 | contains_anchor_(false), |
| 4195 | is_scanned_for_captures_(false), |
| 4196 | failed_(false) { |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 4197 | Advance(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4198 | } |
| 4199 | |
| 4200 | |
| 4201 | uc32 RegExpParser::Next() { |
| 4202 | if (has_next()) { |
| 4203 | return in()->Get(next_pos_); |
| 4204 | } else { |
| 4205 | return kEndMarker; |
| 4206 | } |
| 4207 | } |
| 4208 | |
| 4209 | |
| 4210 | void RegExpParser::Advance() { |
| 4211 | if (next_pos_ < in()->length()) { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 4212 | StackLimitCheck check(isolate()); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4213 | if (check.HasOverflowed()) { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 4214 | ReportError(CStrVector(Isolate::kStackOverflowMessage)); |
| 4215 | } else if (isolate()->zone()->excess_allocation()) { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4216 | ReportError(CStrVector("Regular expression too large")); |
| 4217 | } else { |
| 4218 | current_ = in()->Get(next_pos_); |
| 4219 | next_pos_++; |
| 4220 | } |
| 4221 | } else { |
| 4222 | current_ = kEndMarker; |
| 4223 | has_more_ = false; |
| 4224 | } |
| 4225 | } |
| 4226 | |
| 4227 | |
| 4228 | void RegExpParser::Reset(int pos) { |
| 4229 | next_pos_ = pos; |
| 4230 | Advance(); |
| 4231 | } |
| 4232 | |
| 4233 | |
| 4234 | void RegExpParser::Advance(int dist) { |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 4235 | next_pos_ += dist - 1; |
| 4236 | Advance(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4237 | } |
| 4238 | |
| 4239 | |
| 4240 | bool RegExpParser::simple() { |
| 4241 | return simple_; |
| 4242 | } |
| 4243 | |
| 4244 | RegExpTree* RegExpParser::ReportError(Vector<const char> message) { |
| 4245 | failed_ = true; |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 4246 | *error_ = isolate()->factory()->NewStringFromAscii(message, NOT_TENURED); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4247 | // Zip to the end to make sure the no more input is read. |
| 4248 | current_ = kEndMarker; |
| 4249 | next_pos_ = in()->length(); |
| 4250 | return NULL; |
| 4251 | } |
| 4252 | |
| 4253 | |
| 4254 | // Pattern :: |
| 4255 | // Disjunction |
| 4256 | RegExpTree* RegExpParser::ParsePattern() { |
| 4257 | RegExpTree* result = ParseDisjunction(CHECK_FAILED); |
| 4258 | ASSERT(!has_more()); |
| 4259 | // If the result of parsing is a literal string atom, and it has the |
| 4260 | // same length as the input, then the atom is identical to the input. |
| 4261 | if (result->IsAtom() && result->AsAtom()->length() == in()->length()) { |
| 4262 | simple_ = true; |
| 4263 | } |
| 4264 | return result; |
| 4265 | } |
| 4266 | |
| 4267 | |
| 4268 | // Disjunction :: |
| 4269 | // Alternative |
| 4270 | // Alternative | Disjunction |
| 4271 | // Alternative :: |
| 4272 | // [empty] |
| 4273 | // Term Alternative |
| 4274 | // Term :: |
| 4275 | // Assertion |
| 4276 | // Atom |
| 4277 | // Atom Quantifier |
| 4278 | RegExpTree* RegExpParser::ParseDisjunction() { |
| 4279 | // Used to store current state while parsing subexpressions. |
| 4280 | RegExpParserState initial_state(NULL, INITIAL, 0); |
| 4281 | RegExpParserState* stored_state = &initial_state; |
| 4282 | // Cache the builder in a local variable for quick access. |
| 4283 | RegExpBuilder* builder = initial_state.builder(); |
| 4284 | while (true) { |
| 4285 | switch (current()) { |
| 4286 | case kEndMarker: |
| 4287 | if (stored_state->IsSubexpression()) { |
| 4288 | // Inside a parenthesized group when hitting end of input. |
| 4289 | ReportError(CStrVector("Unterminated group") CHECK_FAILED); |
| 4290 | } |
| 4291 | ASSERT_EQ(INITIAL, stored_state->group_type()); |
| 4292 | // Parsing completed successfully. |
| 4293 | return builder->ToRegExp(); |
| 4294 | case ')': { |
| 4295 | if (!stored_state->IsSubexpression()) { |
| 4296 | ReportError(CStrVector("Unmatched ')'") CHECK_FAILED); |
| 4297 | } |
| 4298 | ASSERT_NE(INITIAL, stored_state->group_type()); |
| 4299 | |
| 4300 | Advance(); |
| 4301 | // End disjunction parsing and convert builder content to new single |
| 4302 | // regexp atom. |
| 4303 | RegExpTree* body = builder->ToRegExp(); |
| 4304 | |
| 4305 | int end_capture_index = captures_started(); |
| 4306 | |
| 4307 | int capture_index = stored_state->capture_index(); |
| 4308 | SubexpressionType type = stored_state->group_type(); |
| 4309 | |
| 4310 | // Restore previous state. |
| 4311 | stored_state = stored_state->previous_state(); |
| 4312 | builder = stored_state->builder(); |
| 4313 | |
| 4314 | // Build result of subexpression. |
| 4315 | if (type == CAPTURE) { |
Ben Murdoch | 8b112d2 | 2011-06-08 16:22:53 +0100 | [diff] [blame] | 4316 | RegExpCapture* capture = new(zone()) RegExpCapture(body, capture_index); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4317 | captures_->at(capture_index - 1) = capture; |
| 4318 | body = capture; |
| 4319 | } else if (type != GROUPING) { |
| 4320 | ASSERT(type == POSITIVE_LOOKAHEAD || type == NEGATIVE_LOOKAHEAD); |
| 4321 | bool is_positive = (type == POSITIVE_LOOKAHEAD); |
Ben Murdoch | 8b112d2 | 2011-06-08 16:22:53 +0100 | [diff] [blame] | 4322 | body = new(zone()) RegExpLookahead(body, |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4323 | is_positive, |
| 4324 | end_capture_index - capture_index, |
| 4325 | capture_index); |
| 4326 | } |
| 4327 | builder->AddAtom(body); |
Ben Murdoch | e0cee9b | 2011-05-25 10:26:03 +0100 | [diff] [blame] | 4328 | // For compatability with JSC and ES3, we allow quantifiers after |
| 4329 | // lookaheads, and break in all cases. |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4330 | break; |
| 4331 | } |
| 4332 | case '|': { |
| 4333 | Advance(); |
| 4334 | builder->NewAlternative(); |
| 4335 | continue; |
| 4336 | } |
| 4337 | case '*': |
| 4338 | case '+': |
| 4339 | case '?': |
| 4340 | return ReportError(CStrVector("Nothing to repeat")); |
| 4341 | case '^': { |
| 4342 | Advance(); |
| 4343 | if (multiline_) { |
| 4344 | builder->AddAssertion( |
Ben Murdoch | 8b112d2 | 2011-06-08 16:22:53 +0100 | [diff] [blame] | 4345 | new(zone()) RegExpAssertion(RegExpAssertion::START_OF_LINE)); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4346 | } else { |
| 4347 | builder->AddAssertion( |
Ben Murdoch | 8b112d2 | 2011-06-08 16:22:53 +0100 | [diff] [blame] | 4348 | new(zone()) RegExpAssertion(RegExpAssertion::START_OF_INPUT)); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4349 | set_contains_anchor(); |
| 4350 | } |
| 4351 | continue; |
| 4352 | } |
| 4353 | case '$': { |
| 4354 | Advance(); |
| 4355 | RegExpAssertion::Type type = |
| 4356 | multiline_ ? RegExpAssertion::END_OF_LINE : |
| 4357 | RegExpAssertion::END_OF_INPUT; |
Ben Murdoch | 8b112d2 | 2011-06-08 16:22:53 +0100 | [diff] [blame] | 4358 | builder->AddAssertion(new(zone()) RegExpAssertion(type)); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4359 | continue; |
| 4360 | } |
| 4361 | case '.': { |
| 4362 | Advance(); |
| 4363 | // everything except \x0a, \x0d, \u2028 and \u2029 |
Ben Murdoch | 257744e | 2011-11-30 15:57:28 +0000 | [diff] [blame] | 4364 | ZoneList<CharacterRange>* ranges = |
| 4365 | new(zone()) ZoneList<CharacterRange>(2); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4366 | CharacterRange::AddClassEscape('.', ranges); |
Ben Murdoch | 8b112d2 | 2011-06-08 16:22:53 +0100 | [diff] [blame] | 4367 | RegExpTree* atom = new(zone()) RegExpCharacterClass(ranges, false); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4368 | builder->AddAtom(atom); |
| 4369 | break; |
| 4370 | } |
| 4371 | case '(': { |
| 4372 | SubexpressionType type = CAPTURE; |
| 4373 | Advance(); |
| 4374 | if (current() == '?') { |
| 4375 | switch (Next()) { |
| 4376 | case ':': |
| 4377 | type = GROUPING; |
| 4378 | break; |
| 4379 | case '=': |
| 4380 | type = POSITIVE_LOOKAHEAD; |
| 4381 | break; |
| 4382 | case '!': |
| 4383 | type = NEGATIVE_LOOKAHEAD; |
| 4384 | break; |
| 4385 | default: |
| 4386 | ReportError(CStrVector("Invalid group") CHECK_FAILED); |
| 4387 | break; |
| 4388 | } |
| 4389 | Advance(2); |
| 4390 | } else { |
| 4391 | if (captures_ == NULL) { |
Ben Murdoch | 257744e | 2011-11-30 15:57:28 +0000 | [diff] [blame] | 4392 | captures_ = new(zone()) ZoneList<RegExpCapture*>(2); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4393 | } |
| 4394 | if (captures_started() >= kMaxCaptures) { |
| 4395 | ReportError(CStrVector("Too many captures") CHECK_FAILED); |
| 4396 | } |
| 4397 | captures_->Add(NULL); |
| 4398 | } |
| 4399 | // Store current state and begin new disjunction parsing. |
Ben Murdoch | 8b112d2 | 2011-06-08 16:22:53 +0100 | [diff] [blame] | 4400 | stored_state = new(zone()) RegExpParserState(stored_state, |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4401 | type, |
| 4402 | captures_started()); |
| 4403 | builder = stored_state->builder(); |
Ben Murdoch | e0cee9b | 2011-05-25 10:26:03 +0100 | [diff] [blame] | 4404 | continue; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4405 | } |
| 4406 | case '[': { |
| 4407 | RegExpTree* atom = ParseCharacterClass(CHECK_FAILED); |
| 4408 | builder->AddAtom(atom); |
| 4409 | break; |
| 4410 | } |
| 4411 | // Atom :: |
| 4412 | // \ AtomEscape |
| 4413 | case '\\': |
| 4414 | switch (Next()) { |
| 4415 | case kEndMarker: |
| 4416 | return ReportError(CStrVector("\\ at end of pattern")); |
| 4417 | case 'b': |
| 4418 | Advance(2); |
| 4419 | builder->AddAssertion( |
Ben Murdoch | 8b112d2 | 2011-06-08 16:22:53 +0100 | [diff] [blame] | 4420 | new(zone()) RegExpAssertion(RegExpAssertion::BOUNDARY)); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4421 | continue; |
| 4422 | case 'B': |
| 4423 | Advance(2); |
| 4424 | builder->AddAssertion( |
Ben Murdoch | 8b112d2 | 2011-06-08 16:22:53 +0100 | [diff] [blame] | 4425 | new(zone()) RegExpAssertion(RegExpAssertion::NON_BOUNDARY)); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4426 | continue; |
Ben Murdoch | e0cee9b | 2011-05-25 10:26:03 +0100 | [diff] [blame] | 4427 | // AtomEscape :: |
| 4428 | // CharacterClassEscape |
| 4429 | // |
| 4430 | // CharacterClassEscape :: one of |
| 4431 | // d D s S w W |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4432 | case 'd': case 'D': case 's': case 'S': case 'w': case 'W': { |
| 4433 | uc32 c = Next(); |
| 4434 | Advance(2); |
Ben Murdoch | 257744e | 2011-11-30 15:57:28 +0000 | [diff] [blame] | 4435 | ZoneList<CharacterRange>* ranges = |
| 4436 | new(zone()) ZoneList<CharacterRange>(2); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4437 | CharacterRange::AddClassEscape(c, ranges); |
Ben Murdoch | 8b112d2 | 2011-06-08 16:22:53 +0100 | [diff] [blame] | 4438 | RegExpTree* atom = new(zone()) RegExpCharacterClass(ranges, false); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4439 | builder->AddAtom(atom); |
| 4440 | break; |
| 4441 | } |
| 4442 | case '1': case '2': case '3': case '4': case '5': case '6': |
| 4443 | case '7': case '8': case '9': { |
| 4444 | int index = 0; |
| 4445 | if (ParseBackReferenceIndex(&index)) { |
| 4446 | RegExpCapture* capture = NULL; |
| 4447 | if (captures_ != NULL && index <= captures_->length()) { |
| 4448 | capture = captures_->at(index - 1); |
| 4449 | } |
| 4450 | if (capture == NULL) { |
| 4451 | builder->AddEmpty(); |
| 4452 | break; |
| 4453 | } |
Ben Murdoch | 8b112d2 | 2011-06-08 16:22:53 +0100 | [diff] [blame] | 4454 | RegExpTree* atom = new(zone()) RegExpBackReference(capture); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4455 | builder->AddAtom(atom); |
| 4456 | break; |
| 4457 | } |
| 4458 | uc32 first_digit = Next(); |
| 4459 | if (first_digit == '8' || first_digit == '9') { |
| 4460 | // Treat as identity escape |
| 4461 | builder->AddCharacter(first_digit); |
| 4462 | Advance(2); |
| 4463 | break; |
| 4464 | } |
| 4465 | } |
| 4466 | // FALLTHROUGH |
| 4467 | case '0': { |
| 4468 | Advance(); |
| 4469 | uc32 octal = ParseOctalLiteral(); |
| 4470 | builder->AddCharacter(octal); |
| 4471 | break; |
| 4472 | } |
| 4473 | // ControlEscape :: one of |
| 4474 | // f n r t v |
| 4475 | case 'f': |
| 4476 | Advance(2); |
| 4477 | builder->AddCharacter('\f'); |
| 4478 | break; |
| 4479 | case 'n': |
| 4480 | Advance(2); |
| 4481 | builder->AddCharacter('\n'); |
| 4482 | break; |
| 4483 | case 'r': |
| 4484 | Advance(2); |
| 4485 | builder->AddCharacter('\r'); |
| 4486 | break; |
| 4487 | case 't': |
| 4488 | Advance(2); |
| 4489 | builder->AddCharacter('\t'); |
| 4490 | break; |
| 4491 | case 'v': |
| 4492 | Advance(2); |
| 4493 | builder->AddCharacter('\v'); |
| 4494 | break; |
| 4495 | case 'c': { |
Ben Murdoch | 086aeea | 2011-05-13 15:57:08 +0100 | [diff] [blame] | 4496 | Advance(); |
| 4497 | uc32 controlLetter = Next(); |
| 4498 | // Special case if it is an ASCII letter. |
| 4499 | // Convert lower case letters to uppercase. |
| 4500 | uc32 letter = controlLetter & ~('a' ^ 'A'); |
| 4501 | if (letter < 'A' || 'Z' < letter) { |
| 4502 | // controlLetter is not in range 'A'-'Z' or 'a'-'z'. |
| 4503 | // This is outside the specification. We match JSC in |
| 4504 | // reading the backslash as a literal character instead |
| 4505 | // of as starting an escape. |
| 4506 | builder->AddCharacter('\\'); |
| 4507 | } else { |
| 4508 | Advance(2); |
| 4509 | builder->AddCharacter(controlLetter & 0x1f); |
| 4510 | } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4511 | break; |
| 4512 | } |
| 4513 | case 'x': { |
| 4514 | Advance(2); |
| 4515 | uc32 value; |
| 4516 | if (ParseHexEscape(2, &value)) { |
| 4517 | builder->AddCharacter(value); |
| 4518 | } else { |
| 4519 | builder->AddCharacter('x'); |
| 4520 | } |
| 4521 | break; |
| 4522 | } |
| 4523 | case 'u': { |
| 4524 | Advance(2); |
| 4525 | uc32 value; |
| 4526 | if (ParseHexEscape(4, &value)) { |
| 4527 | builder->AddCharacter(value); |
| 4528 | } else { |
| 4529 | builder->AddCharacter('u'); |
| 4530 | } |
| 4531 | break; |
| 4532 | } |
| 4533 | default: |
| 4534 | // Identity escape. |
| 4535 | builder->AddCharacter(Next()); |
| 4536 | Advance(2); |
| 4537 | break; |
| 4538 | } |
| 4539 | break; |
| 4540 | case '{': { |
| 4541 | int dummy; |
| 4542 | if (ParseIntervalQuantifier(&dummy, &dummy)) { |
| 4543 | ReportError(CStrVector("Nothing to repeat") CHECK_FAILED); |
| 4544 | } |
| 4545 | // fallthrough |
| 4546 | } |
| 4547 | default: |
| 4548 | builder->AddCharacter(current()); |
| 4549 | Advance(); |
| 4550 | break; |
| 4551 | } // end switch(current()) |
| 4552 | |
| 4553 | int min; |
| 4554 | int max; |
| 4555 | switch (current()) { |
| 4556 | // QuantifierPrefix :: |
| 4557 | // * |
| 4558 | // + |
| 4559 | // ? |
| 4560 | // { |
| 4561 | case '*': |
| 4562 | min = 0; |
| 4563 | max = RegExpTree::kInfinity; |
| 4564 | Advance(); |
| 4565 | break; |
| 4566 | case '+': |
| 4567 | min = 1; |
| 4568 | max = RegExpTree::kInfinity; |
| 4569 | Advance(); |
| 4570 | break; |
| 4571 | case '?': |
| 4572 | min = 0; |
| 4573 | max = 1; |
| 4574 | Advance(); |
| 4575 | break; |
| 4576 | case '{': |
| 4577 | if (ParseIntervalQuantifier(&min, &max)) { |
| 4578 | if (max < min) { |
| 4579 | ReportError(CStrVector("numbers out of order in {} quantifier.") |
| 4580 | CHECK_FAILED); |
| 4581 | } |
| 4582 | break; |
| 4583 | } else { |
| 4584 | continue; |
| 4585 | } |
| 4586 | default: |
| 4587 | continue; |
| 4588 | } |
Leon Clarke | e46be81 | 2010-01-19 14:06:41 +0000 | [diff] [blame] | 4589 | RegExpQuantifier::Type type = RegExpQuantifier::GREEDY; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4590 | if (current() == '?') { |
Leon Clarke | e46be81 | 2010-01-19 14:06:41 +0000 | [diff] [blame] | 4591 | type = RegExpQuantifier::NON_GREEDY; |
| 4592 | Advance(); |
| 4593 | } else if (FLAG_regexp_possessive_quantifier && current() == '+') { |
| 4594 | // FLAG_regexp_possessive_quantifier is a debug-only flag. |
| 4595 | type = RegExpQuantifier::POSSESSIVE; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4596 | Advance(); |
| 4597 | } |
Leon Clarke | e46be81 | 2010-01-19 14:06:41 +0000 | [diff] [blame] | 4598 | builder->AddQuantifierToAtom(min, max, type); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4599 | } |
| 4600 | } |
| 4601 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4602 | |
| 4603 | #ifdef DEBUG |
| 4604 | // Currently only used in an ASSERT. |
| 4605 | static bool IsSpecialClassEscape(uc32 c) { |
| 4606 | switch (c) { |
| 4607 | case 'd': case 'D': |
| 4608 | case 's': case 'S': |
| 4609 | case 'w': case 'W': |
| 4610 | return true; |
| 4611 | default: |
| 4612 | return false; |
| 4613 | } |
| 4614 | } |
| 4615 | #endif |
| 4616 | |
| 4617 | |
| 4618 | // In order to know whether an escape is a backreference or not we have to scan |
| 4619 | // the entire regexp and find the number of capturing parentheses. However we |
| 4620 | // don't want to scan the regexp twice unless it is necessary. This mini-parser |
| 4621 | // is called when needed. It can see the difference between capturing and |
| 4622 | // noncapturing parentheses and can skip character classes and backslash-escaped |
| 4623 | // characters. |
| 4624 | void RegExpParser::ScanForCaptures() { |
| 4625 | // Start with captures started previous to current position |
| 4626 | int capture_count = captures_started(); |
| 4627 | // Add count of captures after this position. |
| 4628 | int n; |
| 4629 | while ((n = current()) != kEndMarker) { |
| 4630 | Advance(); |
| 4631 | switch (n) { |
| 4632 | case '\\': |
| 4633 | Advance(); |
| 4634 | break; |
| 4635 | case '[': { |
| 4636 | int c; |
| 4637 | while ((c = current()) != kEndMarker) { |
| 4638 | Advance(); |
| 4639 | if (c == '\\') { |
| 4640 | Advance(); |
| 4641 | } else { |
| 4642 | if (c == ']') break; |
| 4643 | } |
| 4644 | } |
| 4645 | break; |
| 4646 | } |
| 4647 | case '(': |
| 4648 | if (current() != '?') capture_count++; |
| 4649 | break; |
| 4650 | } |
| 4651 | } |
| 4652 | capture_count_ = capture_count; |
| 4653 | is_scanned_for_captures_ = true; |
| 4654 | } |
| 4655 | |
| 4656 | |
| 4657 | bool RegExpParser::ParseBackReferenceIndex(int* index_out) { |
| 4658 | ASSERT_EQ('\\', current()); |
| 4659 | ASSERT('1' <= Next() && Next() <= '9'); |
| 4660 | // Try to parse a decimal literal that is no greater than the total number |
| 4661 | // of left capturing parentheses in the input. |
| 4662 | int start = position(); |
| 4663 | int value = Next() - '0'; |
| 4664 | Advance(2); |
| 4665 | while (true) { |
| 4666 | uc32 c = current(); |
| 4667 | if (IsDecimalDigit(c)) { |
| 4668 | value = 10 * value + (c - '0'); |
| 4669 | if (value > kMaxCaptures) { |
| 4670 | Reset(start); |
| 4671 | return false; |
| 4672 | } |
| 4673 | Advance(); |
| 4674 | } else { |
| 4675 | break; |
| 4676 | } |
| 4677 | } |
| 4678 | if (value > captures_started()) { |
| 4679 | if (!is_scanned_for_captures_) { |
| 4680 | int saved_position = position(); |
| 4681 | ScanForCaptures(); |
| 4682 | Reset(saved_position); |
| 4683 | } |
| 4684 | if (value > capture_count_) { |
| 4685 | Reset(start); |
| 4686 | return false; |
| 4687 | } |
| 4688 | } |
| 4689 | *index_out = value; |
| 4690 | return true; |
| 4691 | } |
| 4692 | |
| 4693 | |
| 4694 | // QuantifierPrefix :: |
| 4695 | // { DecimalDigits } |
| 4696 | // { DecimalDigits , } |
| 4697 | // { DecimalDigits , DecimalDigits } |
| 4698 | // |
| 4699 | // Returns true if parsing succeeds, and set the min_out and max_out |
| 4700 | // values. Values are truncated to RegExpTree::kInfinity if they overflow. |
| 4701 | bool RegExpParser::ParseIntervalQuantifier(int* min_out, int* max_out) { |
| 4702 | ASSERT_EQ(current(), '{'); |
| 4703 | int start = position(); |
| 4704 | Advance(); |
| 4705 | int min = 0; |
| 4706 | if (!IsDecimalDigit(current())) { |
| 4707 | Reset(start); |
| 4708 | return false; |
| 4709 | } |
| 4710 | while (IsDecimalDigit(current())) { |
| 4711 | int next = current() - '0'; |
| 4712 | if (min > (RegExpTree::kInfinity - next) / 10) { |
| 4713 | // Overflow. Skip past remaining decimal digits and return -1. |
| 4714 | do { |
| 4715 | Advance(); |
| 4716 | } while (IsDecimalDigit(current())); |
| 4717 | min = RegExpTree::kInfinity; |
| 4718 | break; |
| 4719 | } |
| 4720 | min = 10 * min + next; |
| 4721 | Advance(); |
| 4722 | } |
| 4723 | int max = 0; |
| 4724 | if (current() == '}') { |
| 4725 | max = min; |
| 4726 | Advance(); |
| 4727 | } else if (current() == ',') { |
| 4728 | Advance(); |
| 4729 | if (current() == '}') { |
| 4730 | max = RegExpTree::kInfinity; |
| 4731 | Advance(); |
| 4732 | } else { |
| 4733 | while (IsDecimalDigit(current())) { |
| 4734 | int next = current() - '0'; |
| 4735 | if (max > (RegExpTree::kInfinity - next) / 10) { |
| 4736 | do { |
| 4737 | Advance(); |
| 4738 | } while (IsDecimalDigit(current())); |
| 4739 | max = RegExpTree::kInfinity; |
| 4740 | break; |
| 4741 | } |
| 4742 | max = 10 * max + next; |
| 4743 | Advance(); |
| 4744 | } |
| 4745 | if (current() != '}') { |
| 4746 | Reset(start); |
| 4747 | return false; |
| 4748 | } |
| 4749 | Advance(); |
| 4750 | } |
| 4751 | } else { |
| 4752 | Reset(start); |
| 4753 | return false; |
| 4754 | } |
| 4755 | *min_out = min; |
| 4756 | *max_out = max; |
| 4757 | return true; |
| 4758 | } |
| 4759 | |
| 4760 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4761 | uc32 RegExpParser::ParseOctalLiteral() { |
| 4762 | ASSERT('0' <= current() && current() <= '7'); |
| 4763 | // For compatibility with some other browsers (not all), we parse |
| 4764 | // up to three octal digits with a value below 256. |
| 4765 | uc32 value = current() - '0'; |
| 4766 | Advance(); |
| 4767 | if ('0' <= current() && current() <= '7') { |
| 4768 | value = value * 8 + current() - '0'; |
| 4769 | Advance(); |
| 4770 | if (value < 32 && '0' <= current() && current() <= '7') { |
| 4771 | value = value * 8 + current() - '0'; |
| 4772 | Advance(); |
| 4773 | } |
| 4774 | } |
| 4775 | return value; |
| 4776 | } |
| 4777 | |
| 4778 | |
| 4779 | bool RegExpParser::ParseHexEscape(int length, uc32 *value) { |
| 4780 | int start = position(); |
| 4781 | uc32 val = 0; |
| 4782 | bool done = false; |
| 4783 | for (int i = 0; !done; i++) { |
| 4784 | uc32 c = current(); |
| 4785 | int d = HexValue(c); |
| 4786 | if (d < 0) { |
| 4787 | Reset(start); |
| 4788 | return false; |
| 4789 | } |
| 4790 | val = val * 16 + d; |
| 4791 | Advance(); |
| 4792 | if (i == length - 1) { |
| 4793 | done = true; |
| 4794 | } |
| 4795 | } |
| 4796 | *value = val; |
| 4797 | return true; |
| 4798 | } |
| 4799 | |
| 4800 | |
| 4801 | uc32 RegExpParser::ParseClassCharacterEscape() { |
| 4802 | ASSERT(current() == '\\'); |
| 4803 | ASSERT(has_next() && !IsSpecialClassEscape(Next())); |
| 4804 | Advance(); |
| 4805 | switch (current()) { |
| 4806 | case 'b': |
| 4807 | Advance(); |
| 4808 | return '\b'; |
| 4809 | // ControlEscape :: one of |
| 4810 | // f n r t v |
| 4811 | case 'f': |
| 4812 | Advance(); |
| 4813 | return '\f'; |
| 4814 | case 'n': |
| 4815 | Advance(); |
| 4816 | return '\n'; |
| 4817 | case 'r': |
| 4818 | Advance(); |
| 4819 | return '\r'; |
| 4820 | case 't': |
| 4821 | Advance(); |
| 4822 | return '\t'; |
| 4823 | case 'v': |
| 4824 | Advance(); |
| 4825 | return '\v'; |
Ben Murdoch | 086aeea | 2011-05-13 15:57:08 +0100 | [diff] [blame] | 4826 | case 'c': { |
| 4827 | uc32 controlLetter = Next(); |
| 4828 | uc32 letter = controlLetter & ~('A' ^ 'a'); |
| 4829 | // For compatibility with JSC, inside a character class |
| 4830 | // we also accept digits and underscore as control characters. |
| 4831 | if ((controlLetter >= '0' && controlLetter <= '9') || |
| 4832 | controlLetter == '_' || |
| 4833 | (letter >= 'A' && letter <= 'Z')) { |
| 4834 | Advance(2); |
| 4835 | // Control letters mapped to ASCII control characters in the range |
| 4836 | // 0x00-0x1f. |
| 4837 | return controlLetter & 0x1f; |
| 4838 | } |
| 4839 | // We match JSC in reading the backslash as a literal |
| 4840 | // character instead of as starting an escape. |
| 4841 | return '\\'; |
| 4842 | } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4843 | case '0': case '1': case '2': case '3': case '4': case '5': |
| 4844 | case '6': case '7': |
| 4845 | // For compatibility, we interpret a decimal escape that isn't |
| 4846 | // a back reference (and therefore either \0 or not valid according |
| 4847 | // to the specification) as a 1..3 digit octal character code. |
| 4848 | return ParseOctalLiteral(); |
| 4849 | case 'x': { |
| 4850 | Advance(); |
| 4851 | uc32 value; |
| 4852 | if (ParseHexEscape(2, &value)) { |
| 4853 | return value; |
| 4854 | } |
| 4855 | // If \x is not followed by a two-digit hexadecimal, treat it |
| 4856 | // as an identity escape. |
| 4857 | return 'x'; |
| 4858 | } |
| 4859 | case 'u': { |
| 4860 | Advance(); |
| 4861 | uc32 value; |
| 4862 | if (ParseHexEscape(4, &value)) { |
| 4863 | return value; |
| 4864 | } |
| 4865 | // If \u is not followed by a four-digit hexadecimal, treat it |
| 4866 | // as an identity escape. |
| 4867 | return 'u'; |
| 4868 | } |
| 4869 | default: { |
| 4870 | // Extended identity escape. We accept any character that hasn't |
| 4871 | // been matched by a more specific case, not just the subset required |
| 4872 | // by the ECMAScript specification. |
| 4873 | uc32 result = current(); |
| 4874 | Advance(); |
| 4875 | return result; |
| 4876 | } |
| 4877 | } |
| 4878 | return 0; |
| 4879 | } |
| 4880 | |
| 4881 | |
| 4882 | CharacterRange RegExpParser::ParseClassAtom(uc16* char_class) { |
| 4883 | ASSERT_EQ(0, *char_class); |
| 4884 | uc32 first = current(); |
| 4885 | if (first == '\\') { |
| 4886 | switch (Next()) { |
| 4887 | case 'w': case 'W': case 'd': case 'D': case 's': case 'S': { |
| 4888 | *char_class = Next(); |
| 4889 | Advance(2); |
| 4890 | return CharacterRange::Singleton(0); // Return dummy value. |
| 4891 | } |
| 4892 | case kEndMarker: |
| 4893 | return ReportError(CStrVector("\\ at end of pattern")); |
| 4894 | default: |
| 4895 | uc32 c = ParseClassCharacterEscape(CHECK_FAILED); |
| 4896 | return CharacterRange::Singleton(c); |
| 4897 | } |
| 4898 | } else { |
| 4899 | Advance(); |
| 4900 | return CharacterRange::Singleton(first); |
| 4901 | } |
| 4902 | } |
| 4903 | |
| 4904 | |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 4905 | static const uc16 kNoCharClass = 0; |
| 4906 | |
| 4907 | // Adds range or pre-defined character class to character ranges. |
| 4908 | // If char_class is not kInvalidClass, it's interpreted as a class |
| 4909 | // escape (i.e., 's' means whitespace, from '\s'). |
| 4910 | static inline void AddRangeOrEscape(ZoneList<CharacterRange>* ranges, |
| 4911 | uc16 char_class, |
| 4912 | CharacterRange range) { |
| 4913 | if (char_class != kNoCharClass) { |
| 4914 | CharacterRange::AddClassEscape(char_class, ranges); |
| 4915 | } else { |
| 4916 | ranges->Add(range); |
| 4917 | } |
| 4918 | } |
| 4919 | |
| 4920 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4921 | RegExpTree* RegExpParser::ParseCharacterClass() { |
| 4922 | static const char* kUnterminated = "Unterminated character class"; |
| 4923 | static const char* kRangeOutOfOrder = "Range out of order in character class"; |
| 4924 | |
| 4925 | ASSERT_EQ(current(), '['); |
| 4926 | Advance(); |
| 4927 | bool is_negated = false; |
| 4928 | if (current() == '^') { |
| 4929 | is_negated = true; |
| 4930 | Advance(); |
| 4931 | } |
Ben Murdoch | 257744e | 2011-11-30 15:57:28 +0000 | [diff] [blame] | 4932 | ZoneList<CharacterRange>* ranges = new(zone()) ZoneList<CharacterRange>(2); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4933 | while (has_more() && current() != ']') { |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 4934 | uc16 char_class = kNoCharClass; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4935 | CharacterRange first = ParseClassAtom(&char_class CHECK_FAILED); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4936 | if (current() == '-') { |
| 4937 | Advance(); |
| 4938 | if (current() == kEndMarker) { |
| 4939 | // If we reach the end we break out of the loop and let the |
| 4940 | // following code report an error. |
| 4941 | break; |
| 4942 | } else if (current() == ']') { |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 4943 | AddRangeOrEscape(ranges, char_class, first); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4944 | ranges->Add(CharacterRange::Singleton('-')); |
| 4945 | break; |
| 4946 | } |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 4947 | uc16 char_class_2 = kNoCharClass; |
| 4948 | CharacterRange next = ParseClassAtom(&char_class_2 CHECK_FAILED); |
| 4949 | if (char_class != kNoCharClass || char_class_2 != kNoCharClass) { |
| 4950 | // Either end is an escaped character class. Treat the '-' verbatim. |
| 4951 | AddRangeOrEscape(ranges, char_class, first); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4952 | ranges->Add(CharacterRange::Singleton('-')); |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 4953 | AddRangeOrEscape(ranges, char_class_2, next); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4954 | continue; |
| 4955 | } |
| 4956 | if (first.from() > next.to()) { |
| 4957 | return ReportError(CStrVector(kRangeOutOfOrder) CHECK_FAILED); |
| 4958 | } |
| 4959 | ranges->Add(CharacterRange::Range(first.from(), next.to())); |
| 4960 | } else { |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 4961 | AddRangeOrEscape(ranges, char_class, first); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4962 | } |
| 4963 | } |
| 4964 | if (!has_more()) { |
| 4965 | return ReportError(CStrVector(kUnterminated) CHECK_FAILED); |
| 4966 | } |
| 4967 | Advance(); |
| 4968 | if (ranges->length() == 0) { |
| 4969 | ranges->Add(CharacterRange::Everything()); |
| 4970 | is_negated = !is_negated; |
| 4971 | } |
Ben Murdoch | 8b112d2 | 2011-06-08 16:22:53 +0100 | [diff] [blame] | 4972 | return new(zone()) RegExpCharacterClass(ranges, is_negated); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4973 | } |
| 4974 | |
| 4975 | |
| 4976 | // ---------------------------------------------------------------------------- |
| 4977 | // The Parser interface. |
| 4978 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4979 | ParserMessage::~ParserMessage() { |
| 4980 | for (int i = 0; i < args().length(); i++) |
| 4981 | DeleteArray(args()[i]); |
| 4982 | DeleteArray(args().start()); |
| 4983 | } |
| 4984 | |
| 4985 | |
| 4986 | ScriptDataImpl::~ScriptDataImpl() { |
Iain Merrick | 9ac36c9 | 2010-09-13 15:29:50 +0100 | [diff] [blame] | 4987 | if (owns_store_) store_.Dispose(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4988 | } |
| 4989 | |
| 4990 | |
| 4991 | int ScriptDataImpl::Length() { |
Leon Clarke | f7060e2 | 2010-06-03 12:02:55 +0100 | [diff] [blame] | 4992 | return store_.length() * sizeof(unsigned); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4993 | } |
| 4994 | |
| 4995 | |
Leon Clarke | f7060e2 | 2010-06-03 12:02:55 +0100 | [diff] [blame] | 4996 | const char* ScriptDataImpl::Data() { |
| 4997 | return reinterpret_cast<const char*>(store_.start()); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4998 | } |
| 4999 | |
| 5000 | |
Leon Clarke | e46be81 | 2010-01-19 14:06:41 +0000 | [diff] [blame] | 5001 | bool ScriptDataImpl::HasError() { |
| 5002 | return has_error(); |
| 5003 | } |
| 5004 | |
| 5005 | |
Kristian Monsen | 0d5e116 | 2010-09-30 15:31:59 +0100 | [diff] [blame] | 5006 | void ScriptDataImpl::Initialize() { |
| 5007 | // Prepares state for use. |
Shimeng (Simon) Wang | 8a31eba | 2010-12-06 19:01:33 -0800 | [diff] [blame] | 5008 | if (store_.length() >= PreparseDataConstants::kHeaderSize) { |
| 5009 | function_index_ = PreparseDataConstants::kHeaderSize; |
| 5010 | int symbol_data_offset = PreparseDataConstants::kHeaderSize |
| 5011 | + store_[PreparseDataConstants::kFunctionsSizeOffset]; |
Kristian Monsen | 0d5e116 | 2010-09-30 15:31:59 +0100 | [diff] [blame] | 5012 | if (store_.length() > symbol_data_offset) { |
| 5013 | symbol_data_ = reinterpret_cast<byte*>(&store_[symbol_data_offset]); |
| 5014 | } else { |
| 5015 | // Partial preparse causes no symbol information. |
| 5016 | symbol_data_ = reinterpret_cast<byte*>(&store_[0] + store_.length()); |
| 5017 | } |
| 5018 | symbol_data_end_ = reinterpret_cast<byte*>(&store_[0] + store_.length()); |
| 5019 | } |
| 5020 | } |
| 5021 | |
| 5022 | |
| 5023 | int ScriptDataImpl::ReadNumber(byte** source) { |
| 5024 | // Reads a number from symbol_data_ in base 128. The most significant |
| 5025 | // bit marks that there are more digits. |
| 5026 | // If the first byte is 0x80 (kNumberTerminator), it would normally |
| 5027 | // represent a leading zero. Since that is useless, and therefore won't |
| 5028 | // appear as the first digit of any actual value, it is used to |
| 5029 | // mark the end of the input stream. |
| 5030 | byte* data = *source; |
| 5031 | if (data >= symbol_data_end_) return -1; |
| 5032 | byte input = *data; |
Shimeng (Simon) Wang | 8a31eba | 2010-12-06 19:01:33 -0800 | [diff] [blame] | 5033 | if (input == PreparseDataConstants::kNumberTerminator) { |
Kristian Monsen | 0d5e116 | 2010-09-30 15:31:59 +0100 | [diff] [blame] | 5034 | // End of stream marker. |
| 5035 | return -1; |
| 5036 | } |
| 5037 | int result = input & 0x7f; |
| 5038 | data++; |
| 5039 | while ((input & 0x80u) != 0) { |
| 5040 | if (data >= symbol_data_end_) return -1; |
| 5041 | input = *data; |
| 5042 | result = (result << 7) | (input & 0x7f); |
| 5043 | data++; |
| 5044 | } |
| 5045 | *source = data; |
| 5046 | return result; |
| 5047 | } |
| 5048 | |
| 5049 | |
Shimeng (Simon) Wang | 8a31eba | 2010-12-06 19:01:33 -0800 | [diff] [blame] | 5050 | // Create a Scanner for the preparser to use as input, and preparse the source. |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 5051 | static ScriptDataImpl* DoPreParse(UC16CharacterStream* source, |
Shimeng (Simon) Wang | 8a31eba | 2010-12-06 19:01:33 -0800 | [diff] [blame] | 5052 | bool allow_lazy, |
Steve Block | 9fac840 | 2011-05-12 15:51:54 +0100 | [diff] [blame] | 5053 | ParserRecorder* recorder) { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 5054 | Isolate* isolate = Isolate::Current(); |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame^] | 5055 | JavaScriptScanner scanner(isolate->unicode_cache()); |
Steve Block | 9fac840 | 2011-05-12 15:51:54 +0100 | [diff] [blame] | 5056 | scanner.Initialize(source); |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 5057 | intptr_t stack_limit = isolate->stack_guard()->real_climit(); |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 5058 | if (!preparser::PreParser::PreParseProgram(&scanner, |
| 5059 | recorder, |
| 5060 | allow_lazy, |
| 5061 | stack_limit)) { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 5062 | isolate->StackOverflow(); |
Teng-Hui Zhu | 3e5fa29 | 2010-11-09 16:16:48 -0800 | [diff] [blame] | 5063 | return NULL; |
| 5064 | } |
| 5065 | |
Kristian Monsen | 80d68ea | 2010-09-08 11:05:35 +0100 | [diff] [blame] | 5066 | // Extract the accumulated data from the recorder as a single |
| 5067 | // contiguous vector that we are responsible for disposing. |
Shimeng (Simon) Wang | 8a31eba | 2010-12-06 19:01:33 -0800 | [diff] [blame] | 5068 | Vector<unsigned> store = recorder->ExtractData(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 5069 | return new ScriptDataImpl(store); |
| 5070 | } |
| 5071 | |
| 5072 | |
Shimeng (Simon) Wang | 8a31eba | 2010-12-06 19:01:33 -0800 | [diff] [blame] | 5073 | // Preparse, but only collect data that is immediately useful, |
| 5074 | // even if the preparser data is only used once. |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 5075 | ScriptDataImpl* ParserApi::PartialPreParse(UC16CharacterStream* source, |
Shimeng (Simon) Wang | 8a31eba | 2010-12-06 19:01:33 -0800 | [diff] [blame] | 5076 | v8::Extension* extension) { |
| 5077 | bool allow_lazy = FLAG_lazy && (extension == NULL); |
| 5078 | if (!allow_lazy) { |
| 5079 | // Partial preparsing is only about lazily compiled functions. |
| 5080 | // If we don't allow lazy compilation, the log data will be empty. |
| 5081 | return NULL; |
| 5082 | } |
| 5083 | PartialParserRecorder recorder; |
Steve Block | 9fac840 | 2011-05-12 15:51:54 +0100 | [diff] [blame] | 5084 | return DoPreParse(source, allow_lazy, &recorder); |
Shimeng (Simon) Wang | 8a31eba | 2010-12-06 19:01:33 -0800 | [diff] [blame] | 5085 | } |
| 5086 | |
| 5087 | |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 5088 | ScriptDataImpl* ParserApi::PreParse(UC16CharacterStream* source, |
Teng-Hui Zhu | 3e5fa29 | 2010-11-09 16:16:48 -0800 | [diff] [blame] | 5089 | v8::Extension* extension) { |
| 5090 | Handle<Script> no_script; |
Teng-Hui Zhu | 3e5fa29 | 2010-11-09 16:16:48 -0800 | [diff] [blame] | 5091 | bool allow_lazy = FLAG_lazy && (extension == NULL); |
| 5092 | CompleteParserRecorder recorder; |
Steve Block | 9fac840 | 2011-05-12 15:51:54 +0100 | [diff] [blame] | 5093 | return DoPreParse(source, allow_lazy, &recorder); |
Teng-Hui Zhu | 3e5fa29 | 2010-11-09 16:16:48 -0800 | [diff] [blame] | 5094 | } |
| 5095 | |
| 5096 | |
| 5097 | bool RegExpParser::ParseRegExp(FlatStringReader* input, |
| 5098 | bool multiline, |
| 5099 | RegExpCompileData* result) { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 5100 | ASSERT(result != NULL); |
| 5101 | RegExpParser parser(input, &result->error, multiline); |
| 5102 | RegExpTree* tree = parser.ParsePattern(); |
| 5103 | if (parser.failed()) { |
| 5104 | ASSERT(tree == NULL); |
| 5105 | ASSERT(!result->error.is_null()); |
| 5106 | } else { |
| 5107 | ASSERT(tree != NULL); |
| 5108 | ASSERT(result->error.is_null()); |
| 5109 | result->tree = tree; |
| 5110 | int capture_count = parser.captures_started(); |
| 5111 | result->simple = tree->IsAtom() && parser.simple() && capture_count == 0; |
| 5112 | result->contains_anchor = parser.contains_anchor(); |
| 5113 | result->capture_count = capture_count; |
| 5114 | } |
| 5115 | return !parser.failed(); |
| 5116 | } |
| 5117 | |
| 5118 | |
Teng-Hui Zhu | 3e5fa29 | 2010-11-09 16:16:48 -0800 | [diff] [blame] | 5119 | bool ParserApi::Parse(CompilationInfo* info) { |
Ben Murdoch | f87a203 | 2010-10-22 12:50:53 +0100 | [diff] [blame] | 5120 | ASSERT(info->function() == NULL); |
| 5121 | FunctionLiteral* result = NULL; |
| 5122 | Handle<Script> script = info->script(); |
| 5123 | if (info->is_lazy()) { |
Ben Murdoch | 203a29f | 2011-10-20 14:36:23 +0100 | [diff] [blame] | 5124 | bool allow_natives_syntax = |
| 5125 | FLAG_allow_natives_syntax || |
| 5126 | info->is_native(); |
| 5127 | Parser parser(script, allow_natives_syntax, NULL, NULL); |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 5128 | result = parser.ParseLazy(info); |
Leon Clarke | 4515c47 | 2010-02-03 11:58:03 +0000 | [diff] [blame] | 5129 | } else { |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame^] | 5130 | // Whether we allow %identifier(..) syntax. |
Ben Murdoch | f87a203 | 2010-10-22 12:50:53 +0100 | [diff] [blame] | 5131 | bool allow_natives_syntax = |
Ben Murdoch | 203a29f | 2011-10-20 14:36:23 +0100 | [diff] [blame] | 5132 | info->is_native() || FLAG_allow_natives_syntax; |
Ben Murdoch | f87a203 | 2010-10-22 12:50:53 +0100 | [diff] [blame] | 5133 | ScriptDataImpl* pre_data = info->pre_parse_data(); |
Teng-Hui Zhu | 3e5fa29 | 2010-11-09 16:16:48 -0800 | [diff] [blame] | 5134 | Parser parser(script, allow_natives_syntax, info->extension(), pre_data); |
Ben Murdoch | f87a203 | 2010-10-22 12:50:53 +0100 | [diff] [blame] | 5135 | if (pre_data != NULL && pre_data->has_error()) { |
| 5136 | Scanner::Location loc = pre_data->MessageLocation(); |
| 5137 | const char* message = pre_data->BuildMessage(); |
| 5138 | Vector<const char*> args = pre_data->BuildArgs(); |
| 5139 | parser.ReportMessageAt(loc, message, args); |
| 5140 | DeleteArray(message); |
| 5141 | for (int i = 0; i < args.length(); i++) { |
| 5142 | DeleteArray(args[i]); |
| 5143 | } |
| 5144 | DeleteArray(args.start()); |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 5145 | ASSERT(info->isolate()->has_pending_exception()); |
Ben Murdoch | f87a203 | 2010-10-22 12:50:53 +0100 | [diff] [blame] | 5146 | } else { |
| 5147 | Handle<String> source = Handle<String>(String::cast(script->source())); |
Steve Block | 1e0659c | 2011-05-24 12:43:12 +0100 | [diff] [blame] | 5148 | result = parser.ParseProgram(source, |
| 5149 | info->is_global(), |
| 5150 | info->StrictMode()); |
Ben Murdoch | f87a203 | 2010-10-22 12:50:53 +0100 | [diff] [blame] | 5151 | } |
Leon Clarke | 4515c47 | 2010-02-03 11:58:03 +0000 | [diff] [blame] | 5152 | } |
Ben Murdoch | f87a203 | 2010-10-22 12:50:53 +0100 | [diff] [blame] | 5153 | |
| 5154 | info->SetFunction(result); |
| 5155 | return (result != NULL); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 5156 | } |
| 5157 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 5158 | } } // namespace v8::internal |