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

Side by Side Diff: lib/runtime/dart_logging_runtime.dart

Issue 1148283010: Remove dart backend (Closed) Base URL: https://github.com/dart-lang/dev_compiler.git@master
Patch Set: Created 5 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
« no previous file with comments | « lib/devc.dart ('k') | lib/runtime/dart_runtime.dart » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(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 dev_compiler.runtime.dart_logging_runtime;
6
7 import 'dart:mirrors' as mirrors;
8
9 import 'dart_runtime.dart' as rt;
10 export 'dart_runtime.dart' show Arity, getArity, type;
11
12 import 'package:stack_trace/stack_trace.dart';
13
14 // Logging / updating CastRecords for cases that alway pass in Dart and DDC
15 // is expensive. They are also less interesting, so filter out by default.
16 const bool _skipSuccess = true;
17
18 class CastRecord {
19 final Type runtimeType;
20 final Type staticType;
21
22 /// True if the dev_compiler would allow this cast. Otherwise false.
23 final bool soundCast;
24
25 /// True if Dart checked mode would allow this cast. Otherwise false.
26 final bool dartCast;
27
28 CastRecord(this.runtimeType, this.staticType, this.soundCast, this.dartCast);
29 }
30
31 // Register a handler to process CastRecords. The default (see below) just
32 // prints a summary at the end.
33 typedef void CastRecordHandler(String key, CastRecord record);
34 CastRecordHandler castRecordHandler = _record;
35
36 var _cache = <Type, Map<Type, CastRecord>>{};
37
38 Map<Type, CastRecord> _cacheGen() => <Type, CastRecord>{};
39
40 void _addToCache(Type runtimeType, Type staticType, CastRecord record) {
41 _cache.putIfAbsent(runtimeType, _cacheGen)[staticType] = record;
42 }
43
44 CastRecord _lookupInCache(Type runtimeType, Type staticType) {
45 var subcache = _cache[runtimeType];
46 if (subcache == null) return null;
47 return subcache[staticType];
48 }
49
50 var _successCache = <Type, Set<Type>>{};
51
52 dynamic cast(dynamic obj, Type fromType, Type staticType,
53 [String kind, String key, bool dartIs, bool isGround]) {
54 var runtimeType = obj.runtimeType;
55 // Short-circuit uninteresting cases.
56 if (_skipSuccess && _successCache.containsKey(staticType)) {
57 if (_successCache[staticType].contains(runtimeType)) {
58 return obj;
59 }
60 }
61
62 if (key == null) {
63 // If no key is past in, use the caller's frame as a key.
64 final trace = new Trace.current(1);
65 final frame = trace.frames.first;
66 key = frame.toString();
67 }
68
69 CastRecord record = _lookupInCache(runtimeType, staticType);
70 if (record == null) {
71 bool soundCast = true;
72 bool dartCast = true;
73 // TODO(vsm): Use instanceOf once we settle on nullability.
74 try {
75 rt.cast(obj, staticType);
76 } catch (e) {
77 soundCast = false;
78 }
79 if (obj == null) {
80 dartCast = true;
81 } else {
82 // TODO(vsm): We could avoid mirror code by requiring the caller to pass
83 // in obj is TypeLiteral as a parameter. We can't do that once we have a
84 // Type object instead.
85 final staticMirror = mirrors.reflectType(staticType);
86 final instanceMirror = mirrors.reflect(obj);
87 final classMirror = instanceMirror.type;
88 dartCast = classMirror.isSubtypeOf(staticMirror);
89 }
90 if (_skipSuccess && dartCast && soundCast) {
91 _successCache
92 .putIfAbsent(staticType, () => new Set<Type>())
93 .add(runtimeType);
94 return obj;
95 }
96 record = new CastRecord(runtimeType, staticType, soundCast, dartCast);
97 _addToCache(runtimeType, staticType, record);
98 }
99 castRecordHandler(key, record);
100 return obj;
101 }
102
103 dynamic wrap(Function build(Function _), Function f, Type fromType, Type toType,
104 String kind, String key, bool dartIs) {
105 if (f == null) return null;
106 return build(f);
107 }
108
109 // The default handler simply records all CastRecords and prints a summary
110 // at the end.
111 final _recordMap = new Map<String, List<CastRecord>>();
112 void _record(String key, CastRecord record) {
113 _recordMap.putIfAbsent(key, () => <CastRecord>[]).add(record);
114 }
115
116 String summary({bool clear: true}) {
117 final buffer = new StringBuffer();
118 _recordMap.forEach((String key, List<CastRecord> records) {
119 int success = 0;
120 int mismatch = 0;
121 int error = 0;
122 int failure = 0;
123 Type staticType = null;
124 var runtimeTypes = new Set<Type>();
125 for (var record in records) {
126 if (staticType == null) {
127 staticType = record.staticType;
128 } else {
129 // Are these canonicalized?
130 // assert(staticType == record.staticType);
131 }
132 runtimeTypes.add(record.runtimeType);
133 if (record.soundCast) {
134 if (record.dartCast) {
135 success++;
136 } else {
137 error++;
138 }
139 } else {
140 if (record.dartCast) {
141 mismatch++;
142 } else {
143 failure++;
144 }
145 }
146 }
147 final total = success + mismatch + error + failure;
148 assert(total != 0);
149 if (success < total) {
150 buffer.writeln('Key $key:');
151 buffer.writeln(' - static type: $staticType');
152 buffer.writeln(' - runtime types: $runtimeTypes');
153 final category = (String cat, int val) =>
154 buffer.writeln(' - $cat: $val (${val / total})');
155 category('success', success);
156 category('failure', failure);
157 category('mismatch', mismatch);
158 category('error', error);
159 }
160 });
161 if (clear) {
162 _recordMap.clear();
163 }
164 return buffer.toString();
165 }
OLDNEW
« no previous file with comments | « lib/devc.dart ('k') | lib/runtime/dart_runtime.dart » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698