Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2012, 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('dart2js'); | 5 #library('dart2js'); |
| 6 | 6 |
| 7 #import('dart:io'); | 7 #import('dart:io'); |
| 8 #import('dart:uri'); | 8 #import('dart:uri'); |
| 9 #import('dart:utf'); | 9 #import('dart:utf'); |
| 10 | 10 |
| (...skipping 53 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 64 } | 64 } |
| 65 } | 65 } |
| 66 | 66 |
| 67 void compile(List<String> argv) { | 67 void compile(List<String> argv) { |
| 68 Uri cwd = getCurrentDirectory(); | 68 Uri cwd = getCurrentDirectory(); |
| 69 bool throwOnError = false; | 69 bool throwOnError = false; |
| 70 bool showWarnings = true; | 70 bool showWarnings = true; |
| 71 bool verbose = false; | 71 bool verbose = false; |
| 72 Uri libraryRoot = cwd; | 72 Uri libraryRoot = cwd; |
| 73 Uri out = cwd.resolve('out.js'); | 73 Uri out = cwd.resolve('out.js'); |
| 74 Uri sourceMapOut = null; | |
| 74 Uri packageRoot = null; | 75 Uri packageRoot = null; |
| 75 List<String> options = new List<String>(); | 76 List<String> options = new List<String>(); |
| 76 bool explicitOut = false; | 77 bool explicitOut = false; |
| 77 bool wantHelp = false; | 78 bool wantHelp = false; |
| 78 bool enableColors = true; | 79 bool enableColors = true; |
| 79 | 80 |
| 80 passThrough(String argument) => options.add(argument); | 81 passThrough(String argument) => options.add(argument); |
| 81 | 82 |
| 82 setLibraryRoot(String argument) { | 83 setLibraryRoot(String argument) { |
| 83 libraryRoot = cwd.resolve(extractPath(argument)); | 84 libraryRoot = cwd.resolve(extractPath(argument)); |
| 84 } | 85 } |
| 85 | 86 |
| 86 setPackageRoot(String argument) { | 87 setPackageRoot(String argument) { |
| 87 packageRoot = cwd.resolve(extractPath(argument)); | 88 packageRoot = cwd.resolve(extractPath(argument)); |
| 88 } | 89 } |
| 89 | 90 |
| 90 setOutput(String argument) { | 91 setOutput(String argument) { |
| 91 explicitOut = true; | 92 explicitOut = true; |
| 92 out = cwd.resolve(nativeToUriPath(extractParameter(argument))); | 93 out = cwd.resolve(nativeToUriPath(extractParameter(argument))); |
| 93 } | 94 } |
| 94 | 95 |
| 96 setSourceMapOutput(String argument) { | |
| 97 sourceMapOut = cwd.resolve(nativeToUriPath(extractParameter(argument))); | |
| 98 passThrough(argument); | |
| 99 } | |
| 100 | |
| 95 handleShortOptions(String argument) { | 101 handleShortOptions(String argument) { |
| 96 var shortOptions = argument.substring(1).splitChars(); | 102 var shortOptions = argument.substring(1).splitChars(); |
| 97 for (var shortOption in shortOptions) { | 103 for (var shortOption in shortOptions) { |
| 98 switch (shortOption) { | 104 switch (shortOption) { |
| 99 case 'v': | 105 case 'v': |
| 100 verbose = true; | 106 verbose = true; |
| 101 break; | 107 break; |
| 102 case 'h': | 108 case 'h': |
| 103 case '?': | 109 case '?': |
| 104 wantHelp = true; | 110 wantHelp = true; |
| 105 break; | 111 break; |
| 106 case 'c': | 112 case 'c': |
| 107 passThrough('--enable-checked-mode'); | 113 passThrough('--enable-checked-mode'); |
| 108 break; | 114 break; |
| 109 default: | 115 default: |
| 110 throw 'Internal error: "$shortOption" did not match'; | 116 throw 'Internal error: "$shortOption" did not match'; |
| 111 } | 117 } |
| 112 } | 118 } |
| 113 } | 119 } |
| 114 | 120 |
| 115 List<String> arguments = <String>[]; | 121 List<String> arguments = <String>[]; |
| 116 List<OptionHandler> handlers = <OptionHandler>[ | 122 List<OptionHandler> handlers = <OptionHandler>[ |
| 117 new OptionHandler('-[chv?]+', handleShortOptions), | 123 new OptionHandler('-[chv?]+', handleShortOptions), |
| 118 new OptionHandler('--throw-on-error', (_) => throwOnError = true), | 124 new OptionHandler('--throw-on-error', (_) => throwOnError = true), |
| 119 new OptionHandler('--suppress-warnings', (_) => showWarnings = false), | 125 new OptionHandler('--suppress-warnings', (_) => showWarnings = false), |
| 120 new OptionHandler('--output-type=dart|--output-type=js', passThrough), | 126 new OptionHandler('--output-type=dart|--output-type=js', passThrough), |
| 121 new OptionHandler('--verbose', (_) => verbose = true), | 127 new OptionHandler('--verbose', (_) => verbose = true), |
| 122 new OptionHandler('--library-root=.+', setLibraryRoot), | 128 new OptionHandler('--library-root=.+', setLibraryRoot), |
| 123 new OptionHandler('--out=.+|-o.+', setOutput), | 129 new OptionHandler('--out=.+|-o.+', setOutput), |
| 130 new OptionHandler('--source-map-out=.+', setSourceMapOutput), | |
|
ahe
2012/06/20 11:56:59
I just talked to the bigwigs. We want to turn on s
podivilov
2012/06/20 14:15:15
Done.
| |
| 124 new OptionHandler('--allow-mock-compilation', passThrough), | 131 new OptionHandler('--allow-mock-compilation', passThrough), |
| 125 new OptionHandler('--unparse-validation', passThrough), | 132 new OptionHandler('--unparse-validation', passThrough), |
| 126 new OptionHandler('--no-colors', (_) => enableColors = false), | 133 new OptionHandler('--no-colors', (_) => enableColors = false), |
| 127 new OptionHandler('--enable[_-]checked[_-]mode|--checked', | 134 new OptionHandler('--enable[_-]checked[_-]mode|--checked', |
| 128 (_) => passThrough('--enable-checked-mode')), | 135 (_) => passThrough('--enable-checked-mode')), |
| 129 new OptionHandler(@'--help|/\?|/h', (_) => wantHelp = true), | 136 new OptionHandler(@'--help|/\?|/h', (_) => wantHelp = true), |
| 130 new OptionHandler(@'--package-root=.+|-p.+', setPackageRoot), | 137 new OptionHandler(@'--package-root=.+|-p.+', setPackageRoot), |
| 131 // The following two options must come last. | 138 // The following two options must come last. |
| 132 new OptionHandler('-.*', (String argument) { | 139 new OptionHandler('-.*', (String argument) { |
| 133 helpAndFail('Error: Unknown option "$argument".'); | 140 helpAndFail('Error: Unknown option "$argument".'); |
| (...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 174 } | 181 } |
| 175 | 182 |
| 176 bool isAborting = false; | 183 bool isAborting = false; |
| 177 | 184 |
| 178 final int FATAL = api.Diagnostic.CRASH.ordinal | api.Diagnostic.ERROR.ordinal; | 185 final int FATAL = api.Diagnostic.CRASH.ordinal | api.Diagnostic.ERROR.ordinal; |
| 179 final int INFO = | 186 final int INFO = |
| 180 api.Diagnostic.INFO.ordinal | api.Diagnostic.VERBOSE_INFO.ordinal; | 187 api.Diagnostic.INFO.ordinal | api.Diagnostic.VERBOSE_INFO.ordinal; |
| 181 | 188 |
| 182 void handler(Uri uri, int begin, int end, String message, | 189 void handler(Uri uri, int begin, int end, String message, |
| 183 api.Diagnostic kind) { | 190 api.Diagnostic kind) { |
| 191 if (kind.name === 'source map') { | |
| 192 // TODO(podivilov): We should find a better way to return source maps from | |
| 193 // emitter. Using diagnostic handler for that purpose is a temporary hack. | |
| 194 writeString(sourceMapOut, message); | |
| 195 return; | |
| 196 } | |
| 197 | |
| 184 if (isAborting) return; | 198 if (isAborting) return; |
| 185 isAborting = kind === api.Diagnostic.CRASH; | 199 isAborting = kind === api.Diagnostic.CRASH; |
| 186 bool fatal = (kind.ordinal & FATAL) != 0; | 200 bool fatal = (kind.ordinal & FATAL) != 0; |
| 187 bool isInfo = (kind.ordinal & INFO) != 0; | 201 bool isInfo = (kind.ordinal & INFO) != 0; |
| 188 if (isInfo) { | 202 if (isInfo) { |
| 189 assert(uri === null); | 203 assert(uri === null); |
| 190 info(message, kind); | 204 info(message, kind); |
| 191 return; | 205 return; |
| 192 } | 206 } |
| 193 var color; | 207 var color; |
| (...skipping 27 matching lines...) Expand all Loading... | |
| 221 Uri uri = cwd.resolve(arguments[0]); | 235 Uri uri = cwd.resolve(arguments[0]); |
| 222 if (packageRoot === null) { | 236 if (packageRoot === null) { |
| 223 packageRoot = uri.resolve('./packages/'); | 237 packageRoot = uri.resolve('./packages/'); |
| 224 } | 238 } |
| 225 | 239 |
| 226 info('compiling $uri'); | 240 info('compiling $uri'); |
| 227 info('package root is $packageRoot'); | 241 info('package root is $packageRoot'); |
| 228 | 242 |
| 229 // TODO(ahe): We expect the future to be complete and call value | 243 // TODO(ahe): We expect the future to be complete and call value |
| 230 // directly. In effect, we don't support truly asynchronous API. | 244 // directly. In effect, we don't support truly asynchronous API. |
| 231 String code = api.compile(uri, libraryRoot, packageRoot, provider, handler, | 245 String code = api.compile( |
| 232 options).value; | 246 uri, libraryRoot, packageRoot, provider, handler, options).value; |
|
ahe
2012/06/20 11:56:59
This change seem unnecessary.
podivilov
2012/06/20 14:15:15
Done.
| |
| 233 if (code === null) { | 247 if (code === null) { |
| 234 fail('Error: Compilation failed.'); | 248 fail('Error: Compilation failed.'); |
| 235 } | 249 } |
| 250 if (sourceMapOut !== null) { | |
| 251 code = '$code\n//@ sourceMappingURL=$sourceMapOut'; | |
| 252 } | |
| 236 writeString(out, code); | 253 writeString(out, code); |
| 237 int jsBytesWritten = code.length; | 254 int jsBytesWritten = code.length; |
| 238 info('compiled $dartBytesRead bytes Dart -> $jsBytesWritten bytes JS ' | 255 info('compiled $dartBytesRead bytes Dart -> $jsBytesWritten bytes JS ' |
| 239 'in ${relativize(cwd, out)}'); | 256 'in ${relativize(cwd, out)}'); |
| 240 if (!explicitOut) { | 257 if (!explicitOut) { |
| 241 String input = uriPathToNative(arguments[0]); | 258 String input = uriPathToNative(arguments[0]); |
| 242 String output = relativize(cwd, out); | 259 String output = relativize(cwd, out); |
| 243 print('Dart file $input compiled to JavaScript: $output'); | 260 print('Dart file $input compiled to JavaScript: $output'); |
| 244 } | 261 } |
| 245 } | 262 } |
| (...skipping 118 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 364 } catch (var ignored) { | 381 } catch (var ignored) { |
| 365 print('Internal error: error while printing exception'); | 382 print('Internal error: error while printing exception'); |
| 366 } | 383 } |
| 367 try { | 384 try { |
| 368 print(trace); | 385 print(trace); |
| 369 } finally { | 386 } finally { |
| 370 exit(253); // 253 is recognized as a crash by our test scripts. | 387 exit(253); // 253 is recognized as a crash by our test scripts. |
| 371 } | 388 } |
| 372 } | 389 } |
| 373 } | 390 } |
| OLD | NEW |