OLD | NEW |
(Empty) | |
| 1 // Copyright (c) 2015, 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 test.util.dart; |
| 6 |
| 7 import 'dart:async'; |
| 8 import 'dart:isolate'; |
| 9 |
| 10 import 'package:analyzer/analyzer.dart'; |
| 11 import 'package:source_span/source_span.dart'; |
| 12 |
| 13 import 'string_literal_iterator.dart'; |
| 14 |
| 15 /// Runs [code] in an isolate. |
| 16 /// |
| 17 /// [code] should be the contents of a Dart entrypoint. It may contain imports; |
| 18 /// they will be resolved in the same context as the host isolate. [message] is |
| 19 /// passed to the [main] method of the code being run; the caller is responsible |
| 20 /// for using this to establish communication with the isolate. |
| 21 /// |
| 22 /// [packageRoot] controls the package root of the isolate. It may be either a |
| 23 /// [String] or a [Uri]. |
| 24 Future<Isolate> runInIsolate(String code, message, {packageRoot, |
| 25 bool checked}) async { |
| 26 if (packageRoot is String) packageRoot = Uri.parse(packageRoot); |
| 27 |
| 28 return await Isolate.spawnUri( |
| 29 Uri.parse('data:application/dart;charset=utf-8,' + Uri.encodeFull(code)), |
| 30 [], |
| 31 message, |
| 32 packageRoot: packageRoot, |
| 33 checked: checked); |
| 34 } |
| 35 |
| 36 // TODO(nweiz): Move this into the analyzer once it starts using SourceSpan |
| 37 // (issue 22977). |
| 38 /// Takes a span whose source is the value of a string that has been parsed from |
| 39 /// a Dart file and returns the corresponding span from within that Dart file. |
| 40 /// |
| 41 /// For example, suppose a Dart file contains `@Eval("1 + a")`. The |
| 42 /// [StringLiteral] `"1 + a"` is extracted; this is [context]. Its contents are |
| 43 /// then parsed, producing an error pointing to [span]: |
| 44 /// |
| 45 /// line 1, column 5: |
| 46 /// 1 + a |
| 47 /// ^ |
| 48 /// |
| 49 /// This span isn't very useful, since it only shows the location within the |
| 50 /// [StringLiteral]'s value. So it's passed to [contextualizeSpan] along with |
| 51 /// [context] and [file] (which contains the source of the entire Dart file), |
| 52 /// which then returns: |
| 53 /// |
| 54 /// line 4, column 12 of file.dart: |
| 55 /// @Eval("1 + a") |
| 56 /// ^ |
| 57 /// |
| 58 /// This properly handles multiline literals, adjacent literals, and literals |
| 59 /// containing escape sequences. It does not support interpolated literals. |
| 60 /// |
| 61 /// This will return `null` if [context] contains an invalid string or does not |
| 62 /// contain [span]. |
| 63 SourceSpan contextualizeSpan(SourceSpan span, StringLiteral context, |
| 64 SourceFile file) { |
| 65 var contextRunes = new StringLiteralIterator(context)..moveNext(); |
| 66 |
| 67 for (var i = 0; i < span.start.offset; i++) { |
| 68 if (!contextRunes.moveNext()) return null; |
| 69 } |
| 70 |
| 71 var start = contextRunes.offset; |
| 72 for (var spanRune in span.text.runes) { |
| 73 if (spanRune != contextRunes.current) return null; |
| 74 contextRunes.moveNext(); |
| 75 } |
| 76 |
| 77 return file.span(start, contextRunes.offset); |
| 78 } |
OLD | NEW |