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 frame; | |
6 | |
7 import 'dart:uri'; | |
8 | |
9 import 'package:pathos/path.dart' as path; | |
10 | |
11 import 'trace.dart'; | |
12 | |
13 final _regExp = new RegExp(r'^#\d+\s+([^\s].*) \((.+):(\d+):(\d+)\)$'); | |
Bob Nystrom
2013/03/27 00:46:44
Better name.
nweiz
2013/03/27 21:15:46
Done.
| |
14 | |
15 /// A single stack frame. Each frame points to a precise location in Dart code. | |
16 class Frame { | |
17 /// The URI of the file in which the code is located. | |
18 /// | |
19 /// This URI will usually have the scheme `dart`, `file`, `http`, or `https`. | |
20 final Uri uri; | |
21 | |
22 /// The line number on which the code location is located. | |
23 final int line; | |
24 | |
25 /// The column number of the code location. | |
26 final int column; | |
27 | |
28 /// The name of the member in which the code location occurs. | |
29 /// | |
30 /// Anonymous closures are represented as `<fn>` in this member string. | |
31 final String member; | |
32 | |
33 /// Whether this stack frame comes from the Dart core libraries. | |
34 bool get isCore => uri.scheme == 'dart'; | |
35 | |
36 /// Returns a human-friendly description of the library that this stack frame | |
37 /// comes from. | |
38 /// | |
39 /// This will usually be the string form of [uri], but a relative path will be | |
40 /// used if possible. | |
41 String get library { | |
42 // TODO(nweiz): handle relative URIs here as well once pathos supports that. | |
43 if (uri.scheme != 'file') return uri.toString(); | |
44 return path.relative(uri.path); | |
45 } | |
46 | |
47 /// A human-friendly description of the code location. | |
48 /// | |
49 /// For Dart core libraries, this will omit the line and column information, | |
50 /// since those are useless for baked-in libraries. | |
51 String get location { | |
52 if (isCore) return library; | |
53 return '$library $line:$column'; | |
54 } | |
55 | |
56 /// Returns a single frame of the current stack. | |
57 /// | |
58 /// By default, this will return the frame above the current method. If | |
59 /// [level] is `0`, it will return the current method's frame; if [level] is | |
60 /// higher than `1`, it will return higher frames. | |
61 factory Frame.caller([int level=1]) { | |
62 if (level < 0) { | |
63 throw new ArgumentError("Argument [level] must be greater than or equal to " | |
Bob Nystrom
2013/03/27 00:46:44
Long line.
nweiz
2013/03/27 21:15:46
Done.
| |
64 "0."); | |
65 } | |
66 | |
67 return new Trace.current(level + 1).frames.first; | |
68 } | |
69 | |
70 /// Parses a string representation of a stack frame. | |
71 /// | |
72 /// [frame] should be formatted in the same was a native stack trace frame. | |
Bob Nystrom
2013/03/27 00:46:44
was -> way as
nweiz
2013/03/27 21:15:46
Done.
| |
73 factory Frame.parse(String frame) { | |
74 var match = _regExp.firstMatch(frame); | |
75 if (match == null) { | |
76 throw new FormatException("Couldn't parse stack trace line '$frame'."); | |
77 } | |
78 | |
79 var uri = new Uri.fromString(match[2]); | |
80 var member = match[1].replaceAll("<anonymous closure>", "<fn>"); | |
81 return new Frame(uri, int.parse(match[3]), int.parse(match[4]), member); | |
82 } | |
83 | |
84 Frame(this.uri, this.line, this.column, this.member); | |
85 | |
86 String toString() => '$location in $member'; | |
87 } | |
OLD | NEW |