| OLD | NEW |
| 1 // Copyright (c) 2016, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2016, 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 library kernel.transformations.setup_builtin_library; | 5 library kernel.transformations.setup_builtin_library; |
| 6 | 6 |
| 7 import '../ast.dart'; | 7 import '../ast.dart'; |
| 8 | 8 |
| 9 // The DartVM has a special `dart:_builtin` library which exposes a | 9 // The DartVM has a special `dart:_builtin` library which exposes a |
| 10 // `_getMainClosure()` method. We need to change this method to return a | 10 // `_getMainClosure()` method. We need to change this method to return a |
| 11 // closure of `main()`. | 11 // closure of `main()`. |
| 12 Program transformProgram(Program program, | 12 Program transformProgram(Program program, |
| 13 {String libraryUri: 'dart:_builtin'}) { | 13 {String libraryUri: 'dart:_builtin'}) { |
| 14 Procedure mainMethod = program.mainMethod; | 14 Procedure mainMethod = program.mainMethod; |
| 15 | 15 |
| 16 if (mainMethod == null) return program; | |
| 17 | |
| 18 Library builtinLibrary; | 16 Library builtinLibrary; |
| 19 for (Library library in program.libraries) { | 17 for (Library library in program.libraries) { |
| 20 if (library.importUri.toString() == libraryUri) { | 18 if (library.importUri.toString() == libraryUri) { |
| 21 builtinLibrary = library; | 19 builtinLibrary = library; |
| 22 break; | 20 break; |
| 23 } | 21 } |
| 24 } | 22 } |
| 25 | 23 |
| 26 if (builtinLibrary == null) { | 24 if (builtinLibrary == null) { |
| 27 throw new Exception('Could not find "dart:_builtin" library'); | 25 throw new Exception('Could not find "dart:_builtin" library'); |
| 28 } | 26 } |
| 29 | 27 |
| 30 FunctionNode getMainClosure; | 28 FunctionNode getMainClosure; |
| 31 for (Procedure procedure in builtinLibrary.procedures) { | 29 for (Procedure procedure in builtinLibrary.procedures) { |
| 32 if (procedure.name.name == '_getMainClosure') { | 30 if (procedure.name.name == '_getMainClosure') { |
| 33 getMainClosure = procedure.function; | 31 getMainClosure = procedure.function; |
| 34 break; | 32 break; |
| 35 } | 33 } |
| 36 } | 34 } |
| 37 | 35 |
| 38 if (getMainClosure == null) { | 36 if (getMainClosure == null) { |
| 39 throw new Exception('Could not find "_getMainClosure" in "$libraryUri"'); | 37 throw new Exception('Could not find "_getMainClosure" in "$libraryUri"'); |
| 40 } | 38 } |
| 41 | 39 |
| 42 var returnMainStatement = new ReturnStatement(new StaticGet(mainMethod)); | 40 if (mainMethod != null) { |
| 43 getMainClosure.body = returnMainStatement; | 41 var returnMainStatement = new ReturnStatement(new StaticGet(mainMethod)); |
| 44 returnMainStatement.parent = getMainClosure; | 42 getMainClosure.body = returnMainStatement; |
| 43 returnMainStatement.parent = getMainClosure; |
| 44 } else { |
| 45 // TODO(ahe): This should throw no such method error. |
| 46 getMainClosure.body = null; |
| 47 } |
| 45 | 48 |
| 46 return program; | 49 return program; |
| 47 } | 50 } |
| OLD | NEW |