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

Side by Side Diff: lib/compiler/implementation/dart2js.dart

Issue 10579019: First shot at source maps generation in dart2js. (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: Created 8 years, 6 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 | Annotate | Revision Log
OLDNEW
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
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 79
79 passThrough(String argument) => options.add(argument); 80 passThrough(String argument) => options.add(argument);
80 81
81 setLibraryRoot(String argument) { 82 setLibraryRoot(String argument) {
82 libraryRoot = cwd.resolve(extractPath(argument)); 83 libraryRoot = cwd.resolve(extractPath(argument));
83 } 84 }
84 85
85 setPackageRoot(String argument) { 86 setPackageRoot(String argument) {
86 packageRoot = cwd.resolve(extractPath(argument)); 87 packageRoot = cwd.resolve(extractPath(argument));
87 } 88 }
88 89
89 setOutput(String argument) { 90 setOutput(String argument) {
90 explicitOut = true; 91 explicitOut = true;
91 out = cwd.resolve(nativeToUriPath(extractParameter(argument))); 92 out = cwd.resolve(nativeToUriPath(extractParameter(argument)));
92 } 93 }
93 94
95 setSourceMapOutput(String argument) {
96 sourceMapOut = cwd.resolve(nativeToUriPath(extractParameter(argument)));
97 }
98
94 handleShortOptions(String argument) { 99 handleShortOptions(String argument) {
95 var shortOptions = argument.substring(1).splitChars(); 100 var shortOptions = argument.substring(1).splitChars();
96 for (var shortOption in shortOptions) { 101 for (var shortOption in shortOptions) {
97 switch (shortOption) { 102 switch (shortOption) {
98 case 'v': 103 case 'v':
99 verbose = true; 104 verbose = true;
100 break; 105 break;
101 case 'h': 106 case 'h':
102 case '?': 107 case '?':
103 wantHelp = true; 108 wantHelp = true;
104 break; 109 break;
105 case 'c': 110 case 'c':
106 passThrough('--enable-checked-mode'); 111 passThrough('--enable-checked-mode');
107 break; 112 break;
108 default: 113 default:
109 throw 'Internal error: "$shortOption" did not match'; 114 throw 'Internal error: "$shortOption" did not match';
110 } 115 }
111 } 116 }
112 } 117 }
113 118
114 List<String> arguments = <String>[]; 119 List<String> arguments = <String>[];
115 List<OptionHandler> handlers = <OptionHandler>[ 120 List<OptionHandler> handlers = <OptionHandler>[
116 new OptionHandler('-[chv?]+', handleShortOptions), 121 new OptionHandler('-[chv?]+', handleShortOptions),
117 new OptionHandler('--throw-on-error', (_) => throwOnError = true), 122 new OptionHandler('--throw-on-error', (_) => throwOnError = true),
118 new OptionHandler('--suppress-warnings', (_) => showWarnings = false), 123 new OptionHandler('--suppress-warnings', (_) => showWarnings = false),
119 new OptionHandler('--output-type=dart|--output-type=js', passThrough), 124 new OptionHandler('--output-type=dart|--output-type=js', passThrough),
120 new OptionHandler('--verbose', (_) => verbose = true), 125 new OptionHandler('--verbose', (_) => verbose = true),
121 new OptionHandler('--library-root=.+', setLibraryRoot), 126 new OptionHandler('--library-root=.+', setLibraryRoot),
122 new OptionHandler('--out=.+|-o.+', setOutput), 127 new OptionHandler('--out=.+|-o.+', setOutput),
128 new OptionHandler('--source-map-out=.+', setSourceMapOutput),
123 new OptionHandler('--allow-mock-compilation', passThrough), 129 new OptionHandler('--allow-mock-compilation', passThrough),
124 new OptionHandler('--unparse-validation', passThrough), 130 new OptionHandler('--unparse-validation', passThrough),
125 new OptionHandler('--no-colors', (_) => colors.enabled = false), 131 new OptionHandler('--no-colors', (_) => colors.enabled = false),
ahe 2012/06/19 17:00:41 I think you accidentally copied a line here.
podivilov 2012/06/20 09:37:14 Done.
132 new OptionHandler('--no-colors', (_) => colors.enabled = false),
126 new OptionHandler('--enable[_-]checked[_-]mode|--checked', 133 new OptionHandler('--enable[_-]checked[_-]mode|--checked',
127 (_) => passThrough('--enable-checked-mode')), 134 (_) => passThrough('--enable-checked-mode')),
128 new OptionHandler(@'--help|/\?|/h', (_) => wantHelp = true), 135 new OptionHandler(@'--help|/\?|/h', (_) => wantHelp = true),
129 new OptionHandler(@'--package-root=.+|-p.+', setPackageRoot), 136 new OptionHandler(@'--package-root=.+|-p.+', setPackageRoot),
130 // The following two options must come last. 137 // The following two options must come last.
131 new OptionHandler('-.*', (String argument) { 138 new OptionHandler('-.*', (String argument) {
132 helpAndFail('Error: Unknown option "$argument".'); 139 helpAndFail('Error: Unknown option "$argument".');
133 }), 140 }),
134 new OptionHandler('.*', (String argument) { 141 new OptionHandler('.*', (String argument) {
135 arguments.add(nativeToUriPath(argument)); 142 arguments.add(nativeToUriPath(argument));
(...skipping 30 matching lines...) Expand all
166 new SourceFile(relativize(cwd, uri), source); 173 new SourceFile(relativize(cwd, uri), source);
167 return new Future.immediate(source); 174 return new Future.immediate(source);
168 } 175 }
169 176
170 void info(var message) { 177 void info(var message) {
171 if (verbose) print('${colors.green("info:")} $message'); 178 if (verbose) print('${colors.green("info:")} $message');
172 } 179 }
173 180
174 bool isAborting = false; 181 bool isAborting = false;
175 182
176 void handler(Uri uri, int begin, int end, String message, bool fatal) { 183 void handler(Uri uri, int begin, int end, String message, bool fatal) {
ahe 2012/06/19 17:00:41 Earlier today, I created changed "fatal" to kind.
podivilov 2012/06/20 09:37:14 Done.
177 if (isAborting) return; 184 if (isAborting) return;
178 if (uri === null && !fatal) { 185 if (uri === null && !fatal) {
179 info(message); 186 info(message);
180 return; 187 return;
181 } 188 }
182 if (uri === null) { 189 if (uri === null) {
183 assert(fatal); 190 assert(fatal);
184 print(message); 191 print(message);
185 } else if (fatal || showWarnings) { 192 } else if (fatal || showWarnings) {
186 SourceFile file = sourceFiles[uri.toString()]; 193 SourceFile file = sourceFiles[uri.toString()];
187 print(file.getLocationMessage(message, begin, end, true)); 194 print(file.getLocationMessage(message, begin, end, true));
188 } 195 }
189 if (fatal && throwOnError) { 196 if (fatal && throwOnError) {
190 isAborting = true; 197 isAborting = true;
191 throw new AbortLeg(message); 198 throw new AbortLeg(message);
192 } 199 }
193 } 200 }
194 201
195 Uri uri = cwd.resolve(arguments[0]); 202 Uri uri = cwd.resolve(arguments[0]);
196 if (packageRoot === null) { 203 if (packageRoot === null) {
197 packageRoot = uri.resolve('./packages/'); 204 packageRoot = uri.resolve('./packages/');
198 } 205 }
199 206
200 info('compiling $uri'); 207 info('compiling $uri');
201 info('package root is $packageRoot'); 208 info('package root is $packageRoot');
202 209
203 // TODO(ahe): We expect the future to be complete and call value 210 // TODO(ahe): We expect the future to be complete and call value
204 // directly. In effect, we don't support truly asynchronous API. 211 // directly. In effect, we don't support truly asynchronous API.
205 String code = api.compile(uri, libraryRoot, packageRoot, provider, handler, 212 api.CompiledScript script = api.compile(
206 options).value; 213 uri, libraryRoot, packageRoot, provider, handler, options).value;
214 String code = script.code;
207 if (code === null) { 215 if (code === null) {
208 fail('Error: Compilation failed.'); 216 fail('Error: Compilation failed.');
209 } 217 }
218 if (sourceMapOut !== null) {
219 writeString(sourceMapOut, script.sourceMap);
220 code = '$code\n//@ sourceMappingURL=$sourceMapOut';
221 }
210 writeString(out, code); 222 writeString(out, code);
211 int jsBytesWritten = code.length; 223 int jsBytesWritten = code.length;
212 info('compiled $dartBytesRead bytes Dart -> $jsBytesWritten bytes JS ' 224 info('compiled $dartBytesRead bytes Dart -> $jsBytesWritten bytes JS '
213 'in ${relativize(cwd, out)}'); 225 'in ${relativize(cwd, out)}');
214 if (!explicitOut) { 226 if (!explicitOut) {
215 String input = uriPathToNative(arguments[0]); 227 String input = uriPathToNative(arguments[0]);
216 String output = relativize(cwd, out); 228 String output = relativize(cwd, out);
217 print('Dart file $input compiled to JavaScript: $output'); 229 print('Dart file $input compiled to JavaScript: $output');
218 } 230 }
219 } 231 }
(...skipping 118 matching lines...) Expand 10 before | Expand all | Expand 10 after
338 } catch (var ignored) { 350 } catch (var ignored) {
339 print('Internal error: error while printing exception'); 351 print('Internal error: error while printing exception');
340 } 352 }
341 try { 353 try {
342 print(trace); 354 print(trace);
343 } finally { 355 } finally {
344 exit(253); // 253 is recognized as a crash by our test scripts. 356 exit(253); // 253 is recognized as a crash by our test scripts.
345 } 357 }
346 } 358 }
347 } 359 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698