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

Side by Side Diff: src/pattern-rewriter.cc

Issue 1139603005: [destructuring] Implement BindingArrayPattern (Closed) Base URL: https://chromium.googlesource.com/v8/v8.git@master
Patch Set: Rebased + feedback Created 5 years, 7 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « src/parser.cc ('k') | test/mjsunit/harmony/destructuring.js » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright 2015 the V8 project authors. All rights reserved. 1 // Copyright 2015 the V8 project authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "src/ast.h" 5 #include "src/ast.h"
6 #include "src/messages.h" 6 #include "src/messages.h"
7 #include "src/parser.h" 7 #include "src/parser.h"
8 8
9 namespace v8 { 9 namespace v8 {
10 10
(...skipping 193 matching lines...) Expand 10 before | Expand all | Expand 10 after
204 block_->AddStatement( 204 block_->AddStatement(
205 factory()->NewExpressionStatement(assignment, RelocInfo::kNoPosition), 205 factory()->NewExpressionStatement(assignment, RelocInfo::kNoPosition),
206 zone()); 206 zone());
207 } 207 }
208 } 208 }
209 209
210 210
211 Variable* Parser::PatternRewriter::CreateTempVar(Expression* value) { 211 Variable* Parser::PatternRewriter::CreateTempVar(Expression* value) {
212 auto temp_scope = descriptor_->parser->scope_->DeclarationScope(); 212 auto temp_scope = descriptor_->parser->scope_->DeclarationScope();
213 auto temp = temp_scope->NewTemporary(ast_value_factory()->empty_string()); 213 auto temp = temp_scope->NewTemporary(ast_value_factory()->empty_string());
214 auto assignment = 214 if (value != nullptr) {
215 factory()->NewAssignment(Token::ASSIGN, factory()->NewVariableProxy(temp), 215 auto assignment = factory()->NewAssignment(
216 value, RelocInfo::kNoPosition); 216 Token::ASSIGN, factory()->NewVariableProxy(temp), value,
217 RelocInfo::kNoPosition);
217 218
218 block_->AddStatement( 219 block_->AddStatement(
219 factory()->NewExpressionStatement(assignment, RelocInfo::kNoPosition), 220 factory()->NewExpressionStatement(assignment, RelocInfo::kNoPosition),
220 zone()); 221 zone());
222 }
221 return temp; 223 return temp;
222 } 224 }
223 225
224 226
225 void Parser::PatternRewriter::VisitObjectLiteral(ObjectLiteral* pattern) { 227 void Parser::PatternRewriter::VisitObjectLiteral(ObjectLiteral* pattern) {
226 auto temp = CreateTempVar(current_value_); 228 auto temp = CreateTempVar(current_value_);
227 229
228 block_->AddStatement(descriptor_->parser->BuildAssertIsCoercible(temp), 230 block_->AddStatement(descriptor_->parser->BuildAssertIsCoercible(temp),
229 zone()); 231 zone());
230 232
231 for (ObjectLiteralProperty* property : *pattern->properties()) { 233 for (ObjectLiteralProperty* property : *pattern->properties()) {
232 RecurseIntoSubpattern( 234 RecurseIntoSubpattern(
233 property->value(), 235 property->value(),
234 factory()->NewProperty(factory()->NewVariableProxy(temp), 236 factory()->NewProperty(factory()->NewVariableProxy(temp),
235 property->key(), RelocInfo::kNoPosition)); 237 property->key(), RelocInfo::kNoPosition));
236 } 238 }
237 } 239 }
238 240
239 241
240 void Parser::PatternRewriter::VisitArrayLiteral(ArrayLiteral* node) { 242 void Parser::PatternRewriter::VisitArrayLiteral(ArrayLiteral* node) {
241 // TODO(dslomov): implement. 243 auto iterator = CreateTempVar(
244 descriptor_->parser->GetIterator(current_value_, factory()));
245 auto done = CreateTempVar(
246 factory()->NewBooleanLiteral(false, RelocInfo::kNoPosition));
247 auto result = CreateTempVar();
248 auto v = CreateTempVar();
249 for (Expression* value : *node->values()) {
250 // if (!done) {
251 // result = IteratorNext(iterator);
252 // v = (done = result.done) ? undefined : result.value;
253 // }
254 auto next_block =
255 factory()->NewBlock(nullptr, 2, true, RelocInfo::kNoPosition);
256 next_block->AddStatement(factory()->NewExpressionStatement(
257 descriptor_->parser->BuildIteratorNextResult(
258 factory()->NewVariableProxy(iterator),
259 result, RelocInfo::kNoPosition),
260 RelocInfo::kNoPosition),
261 zone());
262
263 auto assign_to_done = factory()->NewAssignment(
264 Token::ASSIGN, factory()->NewVariableProxy(done),
265 factory()->NewProperty(
266 factory()->NewVariableProxy(result),
267 factory()->NewStringLiteral(ast_value_factory()->done_string(),
268 RelocInfo::kNoPosition),
269 RelocInfo::kNoPosition),
270 RelocInfo::kNoPosition);
271 auto next_value = factory()->NewConditional(
272 assign_to_done, factory()->NewUndefinedLiteral(RelocInfo::kNoPosition),
273 factory()->NewProperty(
274 factory()->NewVariableProxy(result),
275 factory()->NewStringLiteral(ast_value_factory()->value_string(),
276 RelocInfo::kNoPosition),
277 RelocInfo::kNoPosition),
278 RelocInfo::kNoPosition);
279 next_block->AddStatement(
280 factory()->NewExpressionStatement(
281 factory()->NewAssignment(Token::ASSIGN,
282 factory()->NewVariableProxy(v), next_value,
283 RelocInfo::kNoPosition),
284 RelocInfo::kNoPosition),
285 zone());
286
287 auto if_statement = factory()->NewIfStatement(
288 factory()->NewUnaryOperation(Token::NOT,
289 factory()->NewVariableProxy(done),
290 RelocInfo::kNoPosition),
291 next_block, factory()->NewEmptyStatement(RelocInfo::kNoPosition),
292 RelocInfo::kNoPosition);
293 block_->AddStatement(if_statement, zone());
294
295 if (!(value->IsLiteral() && value->AsLiteral()->raw_value()->IsTheHole())) {
296 RecurseIntoSubpattern(value, factory()->NewVariableProxy(v));
297 }
298 }
242 } 299 }
243 300
244 301
245 void Parser::PatternRewriter::VisitAssignment(Assignment* node) { 302 void Parser::PatternRewriter::VisitAssignment(Assignment* node) {
246 // let {<pattern> = <init>} = <value> 303 // let {<pattern> = <init>} = <value>
247 // becomes 304 // becomes
248 // temp = <value>; 305 // temp = <value>;
249 // <pattern> = temp === undefined ? <init> : temp; 306 // <pattern> = temp === undefined ? <init> : temp;
250 DCHECK(node->op() == Token::ASSIGN); 307 DCHECK(node->op() == Token::ASSIGN);
251 auto temp = CreateTempVar(current_value_); 308 auto temp = CreateTempVar(current_value_);
(...skipping 59 matching lines...) Expand 10 before | Expand all | Expand 10 after
311 NOT_A_PATTERN(TryFinallyStatement) 368 NOT_A_PATTERN(TryFinallyStatement)
312 NOT_A_PATTERN(UnaryOperation) 369 NOT_A_PATTERN(UnaryOperation)
313 NOT_A_PATTERN(VariableDeclaration) 370 NOT_A_PATTERN(VariableDeclaration)
314 NOT_A_PATTERN(WhileStatement) 371 NOT_A_PATTERN(WhileStatement)
315 NOT_A_PATTERN(WithStatement) 372 NOT_A_PATTERN(WithStatement)
316 NOT_A_PATTERN(Yield) 373 NOT_A_PATTERN(Yield)
317 374
318 #undef NOT_A_PATTERN 375 #undef NOT_A_PATTERN
319 } 376 }
320 } // namespace v8::internal 377 } // namespace v8::internal
OLDNEW
« no previous file with comments | « src/parser.cc ('k') | test/mjsunit/harmony/destructuring.js » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698