OLD | NEW |
(Empty) | |
| 1 // Copyright (c) 2013, the Dart project authors. Please see the AUTHORS file |
| 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. |
| 4 |
| 5 library compiler_isolate; |
| 6 |
| 7 import 'dart:async'; |
| 8 import 'dart:html'; |
| 9 import 'dart:isolate'; |
| 10 import 'dart:uri'; |
| 11 import 'dart:json' show parse; |
| 12 |
| 13 import '../sdk/lib/_internal/compiler/compiler.dart' as compiler; |
| 14 |
| 15 const bool THROW_ON_ERROR = false; |
| 16 |
| 17 final cachedSources = new Map<Uri, String>(); |
| 18 |
| 19 Uri sdkLocation; |
| 20 List options = []; |
| 21 |
| 22 compile(source, SendPort replyTo) { |
| 23 if (sdkLocation == null) { |
| 24 // The first message received gives us the URI of this web app. |
| 25 if (source.endsWith('/sdk.dart')) { |
| 26 var request = new HttpRequest(); |
| 27 request.open('GET', source, async: false); |
| 28 request.send(null); |
| 29 sdkLocation = Uri.parse('sdk:/sdk/'); |
| 30 parse(request.responseText).forEach((file, content) { |
| 31 cachedSources[Uri.parse('sdk:/$file')] = content; |
| 32 }); |
| 33 } else { |
| 34 sdkLocation = Uri.parse(source); |
| 35 } |
| 36 replyTo.send(null); |
| 37 return; |
| 38 } |
| 39 if (source is List) { |
| 40 String messageType = (source.length > 0) ? source[0] : null; |
| 41 var data = (source.length > 1) ? source[1] : null; |
| 42 if (messageType == 'options') { |
| 43 options = data as List; |
| 44 } |
| 45 return; |
| 46 } |
| 47 int charactersRead = 0; |
| 48 Future<String> inputProvider(Uri uri) { |
| 49 if (uri.path.endsWith('/lib/html/dart2js/html_dart2js.dart')) { |
| 50 replyTo.send('dart:html'); |
| 51 } |
| 52 if (uri.scheme == 'sdk') { |
| 53 var value = cachedSources[uri]; |
| 54 charactersRead += value.length; |
| 55 return new Future.value(value); |
| 56 } else if (uri.scheme == 'http' || uri.scheme == 'https') { |
| 57 var value = cachedSources.putIfAbsent(uri, () { |
| 58 var request = new HttpRequest(); |
| 59 request.open('GET', '$uri', async: false); |
| 60 request.send(null); |
| 61 return request.responseText; |
| 62 }); |
| 63 charactersRead += value.length; |
| 64 return new Future.value(value); |
| 65 } else if ('$uri' == 'memory:/main.dart') { |
| 66 charactersRead += source.length; |
| 67 return new Future.value(source); |
| 68 } |
| 69 throw new Exception('Error: Cannot read: $uri'); |
| 70 } |
| 71 void handler(Uri uri, int begin, int end, |
| 72 String message, compiler.Diagnostic kind) { |
| 73 replyTo.send(['diagnostic', { 'uri': '$uri', |
| 74 'begin': begin, |
| 75 'end': end, |
| 76 'message': message, |
| 77 'kind': kind.name }]); |
| 78 if (THROW_ON_ERROR && kind == compiler.Diagnostic.ERROR) { |
| 79 throw new Exception('Throw on error'); |
| 80 } |
| 81 } |
| 82 compiler.compile(new Uri('memory:/main.dart'), |
| 83 sdkLocation, |
| 84 null, |
| 85 inputProvider, |
| 86 handler, |
| 87 options).then((js) { |
| 88 try { |
| 89 if (js == null) { |
| 90 if (!options.contains('--analyze-only')) replyTo.send('failed'); |
| 91 } else { |
| 92 var url; |
| 93 if (options.contains('--verbose')) { |
| 94 handler(null, 0, 0, |
| 95 'Compiled ${source.length}/${charactersRead} characters Dart' |
| 96 ' -> ${js.length} characters.', |
| 97 compiler.Diagnostic.VERBOSE_INFO); |
| 98 } |
| 99 try { |
| 100 // At least Safari and Firefox do not support creating an |
| 101 // object URL from a web worker. MDN claims that it will be |
| 102 // supported in Firefox 21. |
| 103 url = Url.createObjectUrl(new Blob([js], 'application/javascript')); |
| 104 } catch (_) { |
| 105 // Ignored. |
| 106 } |
| 107 if (url != null) { |
| 108 replyTo.send(['url', url]); |
| 109 } else { |
| 110 replyTo.send(['code', js]); |
| 111 } |
| 112 } |
| 113 } catch (e, trace) { |
| 114 replyTo.send(['crash', '$e, $trace']); |
| 115 } |
| 116 replyTo.send('done'); |
| 117 }); |
| 118 } |
OLD | NEW |