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

Side by Side Diff: lib/src/compiler/code_generator.dart

Issue 2201973002: fix optional params to mock methods, allow all signatures (Closed) Base URL: git@github.com:dart-lang/dev_compiler.git@master
Patch Set: fix getters and setters Created 4 years, 4 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
OLDNEW
1 // Copyright (c) 2015, the Dart project authors. Please see the AUTHORS file 1 // Copyright (c) 2015, the Dart project authors. Please see the AUTHORS file
2 // for details. All rights reserved. Use of this source code is governed by a 2 // for details. All rights reserved. Use of this source code is governed by a
3 // BSD-style license that can be found in the LICENSE file. 3 // BSD-style license that can be found in the LICENSE file.
4 4
5 import 'dart:collection' show HashMap, HashSet; 5 import 'dart:collection' show HashMap, HashSet;
6 import 'dart:math' show min, max; 6 import 'dart:math' show min, max;
7 7
8 import 'package:analyzer/analyzer.dart' hide ConstantEvaluator; 8 import 'package:analyzer/analyzer.dart' hide ConstantEvaluator;
9 import 'package:analyzer/dart/ast/ast.dart'; 9 import 'package:analyzer/dart/ast/ast.dart';
10 import 'package:analyzer/dart/ast/token.dart' show Token, TokenType; 10 import 'package:analyzer/dart/ast/token.dart' show Token, TokenType;
(...skipping 1386 matching lines...) Expand 10 before | Expand all | Expand 10 after
1397 /// 1397 ///
1398 /// class Cat { 1398 /// class Cat {
1399 /// bool eatFood(String food) => true; 1399 /// bool eatFood(String food) => true;
1400 /// } 1400 /// }
1401 /// class MockCat implements Cat { 1401 /// class MockCat implements Cat {
1402 /// noSuchMethod(Invocation invocation) => 3; 1402 /// noSuchMethod(Invocation invocation) => 3;
1403 /// } 1403 /// }
1404 /// 1404 ///
1405 /// It will generate an `eatFood` that looks like: 1405 /// It will generate an `eatFood` that looks like:
1406 /// 1406 ///
1407 /// eatFood(food) { 1407 /// eatFood(...args) {
1408 /// return core.bool.as(this.noSuchMethod( 1408 /// return core.bool.as(this.noSuchMethod(
1409 /// new dart.InvocationImpl('eatFood', [food]))); 1409 /// new dart.InvocationImpl('eatFood', args)));
1410 /// } 1410 /// }
1411 JS.Method _implementMockMethod(ExecutableElement method) { 1411 JS.Method _implementMockMethod(ExecutableElement method) {
1412 var positionalArgs = <JS.Identifier>[]
1413 ..addAll(
1414 method.type.normalParameterNames.map((a) => new JS.Identifier(a)))
1415 ..addAll(
1416 method.type.optionalParameterNames.map((a) => new JS.Identifier(a)));
1417
1418 var fnArgs = positionalArgs.toList();
1419
1420 var invocationProps = <JS.Property>[]; 1412 var invocationProps = <JS.Property>[];
1421 addProperty(String name, JS.Expression value) { 1413 addProperty(String name, JS.Expression value) {
1422 invocationProps.add(new JS.Property(js.string(name), value)); 1414 invocationProps.add(new JS.Property(js.string(name), value));
1423 } 1415 }
1424 1416
1417 var args = new JS.TemporaryId('args');
1418 var fnArgs = <JS.Parameter>[];
1419 JS.Expression positionalArgs;;
1420
1425 if (method.type.namedParameterTypes.isNotEmpty) { 1421 if (method.type.namedParameterTypes.isNotEmpty) {
1426 fnArgs.add(namedArgumentTemp); 1422 addProperty(
1427 addProperty('namedArguments', namedArgumentTemp); 1423 'namedArguments', js.call('dart.extractNamedArgs(#)', [args]));
1428 } 1424 }
1429 1425
1430 if (method is MethodElement) { 1426 if (method is MethodElement) {
1431 addProperty('isMethod', js.boolean(true)); 1427 addProperty('isMethod', js.boolean(true));
1428
1429 fnArgs.add(new JS.RestParameter(args));
1430 positionalArgs = args;
1432 } else { 1431 } else {
1433 var property = method as PropertyAccessorElement; 1432 var property = method as PropertyAccessorElement;
1434 if (property.isGetter) { 1433 if (property.isGetter) {
1435 addProperty('isGetter', js.boolean(true)); 1434 addProperty('isGetter', js.boolean(true));
1435
1436 positionalArgs = new JS.ArrayInitializer([]);
1436 } else if (property.isSetter) { 1437 } else if (property.isSetter) {
1437 addProperty('isSetter', js.boolean(true)); 1438 addProperty('isSetter', js.boolean(true));
1439
1440 fnArgs.add(args);
1441 positionalArgs = new JS.ArrayInitializer([args]);
1438 } 1442 }
1439 } 1443 }
1440 1444
1441 var fnBody = 1445 var fnBody = js.call(
1442 js.call('this.noSuchMethod(new dart.InvocationImpl(#, #, #))', [ 1446 'this.noSuchMethod(new dart.InvocationImpl(#, #, #))', [
1443 _elementMemberName(method), 1447 _elementMemberName(method),
1444 new JS.ArrayInitializer(positionalArgs), 1448 positionalArgs,
1445 new JS.ObjectInitializer(invocationProps) 1449 new JS.ObjectInitializer(invocationProps)
1446 ]); 1450 ]);
1447 1451
1448 if (!method.returnType.isDynamic) { 1452 if (!method.returnType.isDynamic) {
1449 fnBody = js.call('#._check(#)', [_emitType(method.returnType), fnBody]); 1453 fnBody = js.call('#._check(#)', [_emitType(method.returnType), fnBody]);
1450 } 1454 }
1451 1455
1452 var fn = new JS.Fun(fnArgs, js.statement('{ return #; }', [fnBody]), 1456 var fn = new JS.Fun(fnArgs, js.statement('{ return #; }', [fnBody]),
1453 typeParams: _emitTypeFormals(method.type.typeFormals)); 1457 typeParams: _emitTypeFormals(method.type.typeFormals));
1454 1458
(...skipping 3907 matching lines...) Expand 10 before | Expand all | Expand 10 after
5362 } 5366 }
5363 5367
5364 bool isLibraryPrefix(Expression node) => 5368 bool isLibraryPrefix(Expression node) =>
5365 node is SimpleIdentifier && node.staticElement is PrefixElement; 5369 node is SimpleIdentifier && node.staticElement is PrefixElement;
5366 5370
5367 LibraryElement _getLibrary(AnalysisContext c, String uri) => 5371 LibraryElement _getLibrary(AnalysisContext c, String uri) =>
5368 c.computeLibraryElement(c.sourceFactory.forUri(uri)); 5372 c.computeLibraryElement(c.sourceFactory.forUri(uri));
5369 5373
5370 bool _isDartRuntime(LibraryElement l) => 5374 bool _isDartRuntime(LibraryElement l) =>
5371 l.isInSdk && l.source.uri.toString() == 'dart:_runtime'; 5375 l.isInSdk && l.source.uri.toString() == 'dart:_runtime';
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698