| OLD | NEW |
| 1 // Copyright (c) 2015, the Dartino project authors. Please see the AUTHORS file | 1 // Copyright (c) 2015, the Dartino 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.md file. | 3 // BSD-style license that can be found in the LICENSE.md file. |
| 4 | 4 |
| 5 library fletchc.fletch_compiler; | 5 library dartino_compiler.dartino_compiler; |
| 6 | 6 |
| 7 import 'dart:async' show | 7 import 'dart:async' show |
| 8 Future; | 8 Future; |
| 9 | 9 |
| 10 import 'dart:convert' show | 10 import 'dart:convert' show |
| 11 UTF8; | 11 UTF8; |
| 12 | 12 |
| 13 import 'dart:io' show | 13 import 'dart:io' show |
| 14 File, | 14 File, |
| 15 Link, | 15 Link, |
| 16 Platform; | 16 Platform; |
| 17 | 17 |
| 18 import 'package:compiler/compiler_new.dart' show | 18 import 'package:compiler/compiler_new.dart' show |
| 19 CompilerInput, | 19 CompilerInput, |
| 20 CompilerOutput, | 20 CompilerOutput, |
| 21 CompilerDiagnostics; | 21 CompilerDiagnostics; |
| 22 | 22 |
| 23 import 'package:compiler/src/source_file_provider.dart' show | 23 import 'package:compiler/src/source_file_provider.dart' show |
| 24 CompilerSourceFileProvider, | 24 CompilerSourceFileProvider, |
| 25 FormattingDiagnosticHandler, | 25 FormattingDiagnosticHandler, |
| 26 SourceFileProvider; | 26 SourceFileProvider; |
| 27 | 27 |
| 28 import 'package:compiler/src/filenames.dart' show | 28 import 'package:compiler/src/filenames.dart' show |
| 29 appendSlash; | 29 appendSlash; |
| 30 | 30 |
| 31 import 'src/fletch_native_descriptor.dart' show | 31 import 'src/dartino_native_descriptor.dart' show |
| 32 FletchNativeDescriptor; | 32 DartinoNativeDescriptor; |
| 33 | 33 |
| 34 import 'src/fletch_backend.dart' show | 34 import 'src/dartino_backend.dart' show |
| 35 FletchBackend; | 35 DartinoBackend; |
| 36 | 36 |
| 37 import 'package:compiler/src/apiimpl.dart' as apiimpl; | 37 import 'package:compiler/src/apiimpl.dart' as apiimpl; |
| 38 | 38 |
| 39 import 'src/fletch_compiler_implementation.dart' show | 39 import 'src/dartino_compiler_implementation.dart' show |
| 40 FletchCompilerImplementation, | 40 DartinoCompilerImplementation, |
| 41 OutputProvider; | 41 OutputProvider; |
| 42 | 42 |
| 43 import 'fletch_system.dart'; | 43 import 'dartino_system.dart'; |
| 44 | 44 |
| 45 import 'incremental/fletchc_incremental.dart' show | 45 import 'incremental/dartino_compiler_incremental.dart' show |
| 46 IncrementalCompiler, | 46 IncrementalCompiler, |
| 47 IncrementalMode; | 47 IncrementalMode; |
| 48 | 48 |
| 49 import 'src/guess_configuration.dart' show | 49 import 'src/guess_configuration.dart' show |
| 50 executable, | 50 executable, |
| 51 guessFletchVm; | 51 guessDartinoVm; |
| 52 | 52 |
| 53 const String _LIBRARY_ROOT = | 53 const String _LIBRARY_ROOT = |
| 54 const String.fromEnvironment("fletchc-library-root"); | 54 const String.fromEnvironment("dartino_compiler-library-root"); |
| 55 | 55 |
| 56 const String fletchDeviceType = | 56 const String dartinoDeviceType = |
| 57 const String.fromEnvironment("fletch.device-type"); | 57 const String.fromEnvironment("dartino.device-type"); |
| 58 const String _NATIVES_JSON = | 58 const String _NATIVES_JSON = |
| 59 const String.fromEnvironment("fletch-natives-json"); | 59 const String.fromEnvironment("dartino-natives-json"); |
| 60 | 60 |
| 61 const String StringOrUri = "String or Uri"; | 61 const String StringOrUri = "String or Uri"; |
| 62 | 62 |
| 63 class FletchCompiler { | 63 class DartinoCompiler { |
| 64 final FletchCompilerImplementation _compiler; | 64 final DartinoCompilerImplementation _compiler; |
| 65 | 65 |
| 66 final Uri script; | 66 final Uri script; |
| 67 | 67 |
| 68 final bool verbose; | 68 final bool verbose; |
| 69 | 69 |
| 70 final String platform; | 70 final String platform; |
| 71 | 71 |
| 72 final Uri nativesJson; | 72 final Uri nativesJson; |
| 73 | 73 |
| 74 FletchCompiler._( | 74 DartinoCompiler._( |
| 75 this._compiler, | 75 this._compiler, |
| 76 this.script, | 76 this.script, |
| 77 this.verbose, | 77 this.verbose, |
| 78 this.platform, | 78 this.platform, |
| 79 this.nativesJson); | 79 this.nativesJson); |
| 80 | 80 |
| 81 Backdoor get backdoor => new Backdoor(this); | 81 Backdoor get backdoor => new Backdoor(this); |
| 82 | 82 |
| 83 factory FletchCompiler( | 83 factory DartinoCompiler( |
| 84 {CompilerInput provider, | 84 {CompilerInput provider, |
| 85 CompilerOutput outputProvider, | 85 CompilerOutput outputProvider, |
| 86 CompilerDiagnostics handler, | 86 CompilerDiagnostics handler, |
| 87 @StringOrUri libraryRoot, | 87 @StringOrUri libraryRoot, |
| 88 @StringOrUri packageConfig, | 88 @StringOrUri packageConfig, |
| 89 @StringOrUri script, | 89 @StringOrUri script, |
| 90 @StringOrUri fletchVm, | 90 @StringOrUri dartinoVm, |
| 91 @StringOrUri currentDirectory, | 91 @StringOrUri currentDirectory, |
| 92 @StringOrUri nativesJson, | 92 @StringOrUri nativesJson, |
| 93 List<String> options, | 93 List<String> options, |
| 94 Map<String, dynamic> environment, | 94 Map<String, dynamic> environment, |
| 95 String platform, | 95 String platform, |
| 96 IncrementalCompiler incrementalCompiler}) { | 96 IncrementalCompiler incrementalCompiler}) { |
| 97 | 97 |
| 98 Uri base = _computeValidatedUri( | 98 Uri base = _computeValidatedUri( |
| 99 currentDirectory, name: 'currentDirectory', ensureTrailingSlash: true); | 99 currentDirectory, name: 'currentDirectory', ensureTrailingSlash: true); |
| 100 if (base == null) { | 100 if (base == null) { |
| (...skipping 52 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 153 packageConfig = _computeValidatedUri( | 153 packageConfig = _computeValidatedUri( |
| 154 packageConfig, name: 'packageConfig', base: base); | 154 packageConfig, name: 'packageConfig', base: base); |
| 155 if (packageConfig == null) { | 155 if (packageConfig == null) { |
| 156 if (script != null) { | 156 if (script != null) { |
| 157 packageConfig = script.resolve('.packages'); | 157 packageConfig = script.resolve('.packages'); |
| 158 } else { | 158 } else { |
| 159 packageConfig = base.resolve('.packages'); | 159 packageConfig = base.resolve('.packages'); |
| 160 } | 160 } |
| 161 } | 161 } |
| 162 | 162 |
| 163 fletchVm = guessFletchVm( | 163 dartinoVm = guessDartinoVm( |
| 164 _computeValidatedUri(fletchVm, name: 'fletchVm', base: base)); | 164 _computeValidatedUri(dartinoVm, name: 'dartinoVm', base: base)); |
| 165 | 165 |
| 166 if (environment == null) { | 166 if (environment == null) { |
| 167 environment = <String, dynamic>{}; | 167 environment = <String, dynamic>{}; |
| 168 } | 168 } |
| 169 | 169 |
| 170 if (nativesJson == null && _NATIVES_JSON != null) { | 170 if (nativesJson == null && _NATIVES_JSON != null) { |
| 171 nativesJson = base.resolve(_NATIVES_JSON); | 171 nativesJson = base.resolve(_NATIVES_JSON); |
| 172 } | 172 } |
| 173 nativesJson = _computeValidatedUri( | 173 nativesJson = _computeValidatedUri( |
| 174 nativesJson, name: 'nativesJson', base: base); | 174 nativesJson, name: 'nativesJson', base: base); |
| 175 | 175 |
| 176 if (nativesJson == null) { | 176 if (nativesJson == null) { |
| 177 nativesJson = _guessNativesJson(); | 177 nativesJson = _guessNativesJson(); |
| 178 if (nativesJson == null) { | 178 if (nativesJson == null) { |
| 179 throw new StateError( | 179 throw new StateError( |
| 180 """ | 180 """ |
| 181 Unable to guess the location of the 'natives.json' file (nativesJson). | 181 Unable to guess the location of the 'natives.json' file (nativesJson). |
| 182 Try adding command-line option '-Dfletch-natives-json=<path to natives.json>.""" | 182 Try adding command-line option '-Ddartino-natives-json=<path to natives.json>.""
" |
| 183 ); | 183 ); |
| 184 } | 184 } |
| 185 } else if (!_looksLikeNativesJson(nativesJson)) { | 185 } else if (!_looksLikeNativesJson(nativesJson)) { |
| 186 throw new ArgumentError( | 186 throw new ArgumentError( |
| 187 "[nativesJson]: natives.json not found in '$nativesJson'."); | 187 "[nativesJson]: natives.json not found in '$nativesJson'."); |
| 188 } | 188 } |
| 189 | 189 |
| 190 FletchCompilerImplementation compiler = new FletchCompilerImplementation( | 190 DartinoCompilerImplementation compiler = new DartinoCompilerImplementation( |
| 191 provider, | 191 provider, |
| 192 outputProvider, | 192 outputProvider, |
| 193 handler, | 193 handler, |
| 194 libraryRoot, | 194 libraryRoot, |
| 195 packageConfig, | 195 packageConfig, |
| 196 nativesJson, | 196 nativesJson, |
| 197 options, | 197 options, |
| 198 environment, | 198 environment, |
| 199 fletchVm, | 199 dartinoVm, |
| 200 incrementalCompiler); | 200 incrementalCompiler); |
| 201 | 201 |
| 202 compiler.log("Using library root: $libraryRoot"); | 202 compiler.log("Using library root: $libraryRoot"); |
| 203 compiler.log("Using package config: $packageConfig"); | 203 compiler.log("Using package config: $packageConfig"); |
| 204 | 204 |
| 205 var helper = new FletchCompiler._( | 205 var helper = new DartinoCompiler._( |
| 206 compiler, script, isVerbose, platform, nativesJson); | 206 compiler, script, isVerbose, platform, nativesJson); |
| 207 compiler.helper = helper; | 207 compiler.helper = helper; |
| 208 return helper; | 208 return helper; |
| 209 } | 209 } |
| 210 | 210 |
| 211 Future<FletchDelta> run([@StringOrUri script]) async { | 211 Future<DartinoDelta> run([@StringOrUri script]) async { |
| 212 // TODO(ahe): Need a base argument. | 212 // TODO(ahe): Need a base argument. |
| 213 script = _computeValidatedUri(script, name: 'script'); | 213 script = _computeValidatedUri(script, name: 'script'); |
| 214 if (script == null) { | 214 if (script == null) { |
| 215 script = this.script; | 215 script = this.script; |
| 216 } | 216 } |
| 217 if (script == null) { | 217 if (script == null) { |
| 218 throw new StateError("No [script] provided."); | 218 throw new StateError("No [script] provided."); |
| 219 } | 219 } |
| 220 await _inititalizeContext(); | 220 await _inititalizeContext(); |
| 221 FletchBackend backend = _compiler.backend; | 221 DartinoBackend backend = _compiler.backend; |
| 222 return _compiler.run(script).then((_) => backend.computeDelta()); | 222 return _compiler.run(script).then((_) => backend.computeDelta()); |
| 223 } | 223 } |
| 224 | 224 |
| 225 Future _inititalizeContext() async { | 225 Future _inititalizeContext() async { |
| 226 var data = await _compiler.callUserProvider(nativesJson); | 226 var data = await _compiler.callUserProvider(nativesJson); |
| 227 if (data is! String) { | 227 if (data is! String) { |
| 228 if (data.last == 0) { | 228 if (data.last == 0) { |
| 229 data = data.sublist(0, data.length - 1); | 229 data = data.sublist(0, data.length - 1); |
| 230 } | 230 } |
| 231 data = UTF8.decode(data); | 231 data = UTF8.decode(data); |
| 232 } | 232 } |
| 233 Map<String, FletchNativeDescriptor> natives = | 233 Map<String, DartinoNativeDescriptor> natives = |
| 234 <String, FletchNativeDescriptor>{}; | 234 <String, DartinoNativeDescriptor>{}; |
| 235 Map<String, String> names = <String, String>{}; | 235 Map<String, String> names = <String, String>{}; |
| 236 FletchNativeDescriptor.decode(data, natives, names); | 236 DartinoNativeDescriptor.decode(data, natives, names); |
| 237 _compiler.context.nativeDescriptors = natives; | 237 _compiler.context.nativeDescriptors = natives; |
| 238 _compiler.context.setNames(names); | 238 _compiler.context.setNames(names); |
| 239 } | 239 } |
| 240 | 240 |
| 241 Uri get fletchVm => _compiler.fletchVm; | 241 Uri get dartinoVm => _compiler.dartinoVm; |
| 242 | 242 |
| 243 /// Create a new instance of [IncrementalCompiler]. | 243 /// Create a new instance of [IncrementalCompiler]. |
| 244 IncrementalCompiler newIncrementalCompiler( | 244 IncrementalCompiler newIncrementalCompiler( |
| 245 IncrementalMode support, | 245 IncrementalMode support, |
| 246 {List<String> options: const <String>[]}) { | 246 {List<String> options: const <String>[]}) { |
| 247 return new IncrementalCompiler( | 247 return new IncrementalCompiler( |
| 248 libraryRoot: _compiler.libraryRoot, | 248 libraryRoot: _compiler.libraryRoot, |
| 249 packageConfig: _compiler.packageConfig, | 249 packageConfig: _compiler.packageConfig, |
| 250 fletchVm: _compiler.fletchVm, | 250 dartinoVm: _compiler.dartinoVm, |
| 251 nativesJson: _compiler.nativesJson, | 251 nativesJson: _compiler.nativesJson, |
| 252 inputProvider: _compiler.provider, | 252 inputProvider: _compiler.provider, |
| 253 diagnosticHandler: _compiler.handler, | 253 diagnosticHandler: _compiler.handler, |
| 254 options: options, | 254 options: options, |
| 255 outputProvider: _compiler.userOutputProvider, | 255 outputProvider: _compiler.userOutputProvider, |
| 256 environment: _compiler.environment, | 256 environment: _compiler.environment, |
| 257 support: support, | 257 support: support, |
| 258 platform: platform); | 258 platform: platform); |
| 259 } | 259 } |
| 260 } | 260 } |
| 261 | 261 |
| 262 // Backdoor around Dart privacy. For now, certain components (in particular | 262 // Backdoor around Dart privacy. For now, certain components (in particular |
| 263 // incremental compilation) need access to implementation details that shouldn't | 263 // incremental compilation) need access to implementation details that shouldn't |
| 264 // be part of the API of this file. | 264 // be part of the API of this file. |
| 265 // TODO(ahe): Delete this class. | 265 // TODO(ahe): Delete this class. |
| 266 class Backdoor { | 266 class Backdoor { |
| 267 final FletchCompiler _compiler; | 267 final DartinoCompiler _compiler; |
| 268 | 268 |
| 269 Backdoor(this._compiler); | 269 Backdoor(this._compiler); |
| 270 | 270 |
| 271 Future<FletchCompilerImplementation> get compilerImplementation async { | 271 Future<DartinoCompilerImplementation> get compilerImplementation async { |
| 272 await _compiler._inititalizeContext(); | 272 await _compiler._inititalizeContext(); |
| 273 return _compiler._compiler; | 273 return _compiler._compiler; |
| 274 } | 274 } |
| 275 } | 275 } |
| 276 | 276 |
| 277 /// Resolves any symbolic links in [uri] if its scheme is "file". Otherwise | 277 /// Resolves any symbolic links in [uri] if its scheme is "file". Otherwise |
| 278 /// return the given [uri]. | 278 /// return the given [uri]. |
| 279 Uri _resolveSymbolicLinks(Uri uri) { | 279 Uri _resolveSymbolicLinks(Uri uri) { |
| 280 if (uri.scheme != 'file') return uri; | 280 if (uri.scheme != 'file') return uri; |
| 281 File apparentLocation = new File.fromUri(uri); | 281 File apparentLocation = new File.fromUri(uri); |
| (...skipping 30 matching lines...) Expand all Loading... |
| 312 } | 312 } |
| 313 return base.resolve(stringOrUri); | 313 return base.resolve(stringOrUri); |
| 314 } else if (stringOrUri is Uri) { | 314 } else if (stringOrUri is Uri) { |
| 315 return base.resolveUri(stringOrUri); | 315 return base.resolveUri(stringOrUri); |
| 316 } else { | 316 } else { |
| 317 throw new ArgumentError("[$name] should be a String or a Uri."); | 317 throw new ArgumentError("[$name] should be a String or a Uri."); |
| 318 } | 318 } |
| 319 } | 319 } |
| 320 | 320 |
| 321 Uri _guessLibraryRoot(String platform) { | 321 Uri _guessLibraryRoot(String platform) { |
| 322 // When running from fletch, [executable] is | 322 // When running from dartino, [executable] is |
| 323 // ".../fletch-repo/fletch/out/$CONFIGURATION/dart", which means that the | 323 // ".../dartino-repo/sdk/out/$CONFIGURATION/dart", which means that the |
| 324 // fletch root is the lib directory in the 2th parent directory (due to | 324 // dartino root is the lib directory in the 2th parent directory (due to |
| 325 // how URI resolution works, the filename ("dart") is removed before | 325 // how URI resolution works, the filename ("dart") is removed before |
| 326 // resolving, for example, | 326 // resolving, for example, |
| 327 // ".../fletch-repo/fletch/out/$CONFIGURATION/../../" becomes | 327 // ".../dartino-repo/sdk/out/$CONFIGURATION/../../" becomes |
| 328 // ".../fletch-repo/fletch/"). | 328 // ".../dartino-repo/sdk/"). |
| 329 Uri guess = executable.resolve('../../lib/'); | 329 Uri guess = executable.resolve('../../lib/'); |
| 330 if (_looksLikeLibraryRoot(guess, platform)) return guess; | 330 if (_looksLikeLibraryRoot(guess, platform)) return guess; |
| 331 return null; | 331 return null; |
| 332 } | 332 } |
| 333 | 333 |
| 334 bool _looksLikeNativesJson(Uri uri) { | 334 bool _looksLikeNativesJson(Uri uri) { |
| 335 return new File.fromUri(uri).existsSync(); | 335 return new File.fromUri(uri).existsSync(); |
| 336 } | 336 } |
| 337 | 337 |
| 338 Uri _guessNativesJson() { | 338 Uri _guessNativesJson() { |
| 339 Uri uri = executable.resolve('natives.json'); | 339 Uri uri = executable.resolve('natives.json'); |
| 340 return _looksLikeNativesJson(uri) ? uri : null; | 340 return _looksLikeNativesJson(uri) ? uri : null; |
| 341 } | 341 } |
| OLD | NEW |