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

Side by Side Diff: frog/leg/lib/mockimpl.dart

Issue 9873021: Move frog/leg to lib/compiler/implementation. (Closed) Base URL: http://dart.googlecode.com/svn/branches/bleeding_edge/dart/
Patch Set: Created 8 years, 8 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
« no previous file with comments | « frog/leg/lib/mock.dart ('k') | frog/leg/lib/native_helper.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) 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
3 // BSD-style license that can be found in the LICENSE file.
4
5 // Mocks of classes and interfaces that Leg cannot read directly.
6
7 // TODO(ahe): Remove this file.
8
9 class JSSyntaxRegExp implements RegExp {
10 final String pattern;
11 final bool multiLine;
12 final bool ignoreCase;
13
14 const JSSyntaxRegExp(String pattern,
15 [bool multiLine = false, bool ignoreCase = false])
16 : this.pattern = pattern,
17 this.multiLine = multiLine,
18 this.ignoreCase = ignoreCase;
19
20 JSSyntaxRegExp._globalVersionOf(JSSyntaxRegExp other)
21 : this.pattern = other.pattern,
22 this.multiLine = other.multiLine,
23 this.ignoreCase = other.ignoreCase {
24 regExpAttachGlobalNative(this);
25 }
26
27 Match firstMatch(String str) {
28 List<String> m = regExpExec(this, checkString(str));
29 if (m === null) return null;
30 var matchStart = regExpMatchStart(m);
31 // m.lastIndex only works with flag 'g'.
32 var matchEnd = matchStart + m[0].length;
33 return new MatchImplementation(pattern, str, matchStart, matchEnd, m);
34 }
35
36 bool hasMatch(String str) => regExpTest(this, checkString(str));
37
38 String stringMatch(String str) {
39 var match = firstMatch(str);
40 return match === null ? null : match.group(0);
41 }
42
43 Iterable<Match> allMatches(String str) {
44 checkString(str);
45 return new _AllMatchesIterable(this, str);
46 }
47
48 _getNative() => regExpGetNative(this);
49 }
50
51 class MatchImplementation implements Match {
52 const MatchImplementation(
53 String this.pattern,
54 String this.str,
55 int this._start,
56 int this._end,
57 List<String> this._groups);
58
59 final String pattern;
60 final String str;
61 final int _start;
62 final int _end;
63 final List<String> _groups;
64
65 int start() => _start;
66 int end() => _end;
67 String group(int index) => _groups[index];
68 String operator [](int index) => group(index);
69 int groupCount() => _groups.length - 1;
70
71 List<String> groups(List<int> groups) {
72 List<String> out = [];
73 for (int i in groups) {
74 out.add(group(i));
75 }
76 return out;
77 }
78 }
79
80 class _AllMatchesIterable implements Iterable<Match> {
81 final JSSyntaxRegExp _re;
82 final String _str;
83
84 const _AllMatchesIterable(this._re, this._str);
85
86 Iterator<Match> iterator() => new _AllMatchesIterator(_re, _str);
87 }
88
89 class _AllMatchesIterator implements Iterator<Match> {
90 final RegExp _re;
91 final String _str;
92 Match _next;
93 bool _done;
94
95 _AllMatchesIterator(JSSyntaxRegExp re, String this._str)
96 : _done = false, _re = new JSSyntaxRegExp._globalVersionOf(re);
97
98 Match next() {
99 if (!hasNext()) {
100 throw const NoMoreElementsException();
101 }
102
103 // _next is set by #hasNext
104 var next = _next;
105 _next = null;
106 return next;
107 }
108
109 bool hasNext() {
110 if (_done) {
111 return false;
112 } else if (_next != null) {
113 return true;
114 }
115
116 _next = _re.firstMatch(_str);
117 if (_next == null) {
118 _done = true;
119 return false;
120 } else {
121 return true;
122 }
123 }
124 }
125
126 class ReceivePortFactory {
127 factory ReceivePort() {
128 throw 'factory ReceivePort is not implemented';
129 }
130 }
131
132 class StringBase {
133 static String createFromCharCodes(List<int> charCodes) {
134 checkNull(charCodes);
135 if (!isJsArray(charCodes)) {
136 if (charCodes is !List) throw new IllegalArgumentException(charCodes);
137 charCodes = new List.from(charCodes);
138 }
139 return Primitives.stringFromCharCodes(charCodes);
140 }
141
142 static String join(List<String> strings, String separator) {
143 checkNull(strings);
144 checkNull(separator);
145 var result = "";
146 var first = true;
147 for (var string in strings) {
148 checkNull(string);
149 if (string is !String) throw new IllegalArgumentException(string);
150 if (!first) result += separator; // TODO(ahe): Use string buffer.
151 result += string; // TODO(ahe): Use string buffer.
152 first = false;
153 }
154 return result;
155 }
156
157 static String concatAll(List<String> strings) {
158 checkNull(strings);
159 var result = "";
160 for (var string in strings) {
161 checkNull(string);
162 if (string is !String) throw new IllegalArgumentException(string);
163 result += string; // TODO(ahe): Use string buffer.
164 }
165 return result;
166 }
167 }
168
169 class TimeZoneImplementation implements TimeZone {
170 const TimeZoneImplementation.utc() : isUtc = true;
171 TimeZoneImplementation.local() : isUtc = false;
172
173 bool operator ==(Object other) {
174 if (!(other is TimeZoneImplementation)) return false;
175 return isUtc == other.isUtc;
176 }
177
178 final bool isUtc;
179 }
180
181 class DateImplementation implements Date {
182 final int value;
183 final TimeZoneImplementation timeZone;
184
185 factory DateImplementation(int years,
186 int month,
187 int day,
188 int hours,
189 int minutes,
190 int seconds,
191 int milliseconds) {
192 return new DateImplementation.withTimeZone(
193 years, month, day,
194 hours, minutes, seconds, milliseconds,
195 new TimeZoneImplementation.local());
196 }
197
198 DateImplementation.withTimeZone(int years,
199 int month,
200 int day,
201 int hours,
202 int minutes,
203 int seconds,
204 int milliseconds,
205 TimeZoneImplementation timeZone)
206 : this.timeZone = checkNull(timeZone),
207 value = Primitives.valueFromDecomposedDate(years, month, day,
208 hours, minutes, seconds,
209 milliseconds,
210 timeZone.isUtc) {
211 _asJs();
212 }
213
214 DateImplementation.now()
215 : timeZone = new TimeZone.local(),
216 value = Primitives.dateNow() {
217 _asJs();
218 }
219
220 factory DateImplementation.fromString(String formattedString) {
221 // Read in (a subset of) ISO 8601.
222 // Examples:
223 // - "2012-02-27 13:27:00"
224 // - "2012-02-27 13:27:00.423z"
225 // - "20120227 13:27:00"
226 // - "20120227T132700"
227 // - "20120227"
228 // - "2012-02-27T14Z"
229 // - "-123450101 00:00:00 Z" // In the year -12345.
230 final RegExp re = const RegExp(
231 @'^([+-]?\d?\d\d\d\d)-?(\d\d)-?(\d\d)' // The day part.
232 @'(?:[ T](\d\d)(?::?(\d\d)(?::?(\d\d)(?:.(\d{1,5}))?)?)? ?([zZ])?)?$');
233 Match match = re.firstMatch(formattedString);
234 if (match !== null) {
235 int parseIntOrZero(String matched) {
236 // TODO(floitsch): we should not need to test against the empty string.
237 if (matched === null || matched == "") return 0;
238 return Math.parseInt(matched);
239 }
240
241 int years = Math.parseInt(match[1]);
242 int month = Math.parseInt(match[2]);
243 int day = Math.parseInt(match[3]);
244 int hours = parseIntOrZero(match[4]);
245 int minutes = parseIntOrZero(match[5]);
246 int seconds = parseIntOrZero(match[6]);
247 bool addOneMillisecond = false;
248 int milliseconds = parseIntOrZero(match[7]);
249 if (milliseconds != 0) {
250 if (match[7].length == 1) {
251 milliseconds *= 100;
252 } else if (match[7].length == 2) {
253 milliseconds *= 10;
254 } else if (match[7].length == 3) {
255 // Do nothing.
256 } else if (match[7].length == 4) {
257 addOneMillisecond = ((milliseconds % 10) >= 5);
258 milliseconds ~/= 10;
259 } else {
260 assert(match[7].length == 5);
261 addOneMillisecond = ((milliseconds %100) >= 50);
262 milliseconds ~/= 100;
263 }
264 if (addOneMillisecond && milliseconds < 999) {
265 addOneMillisecond = false;
266 milliseconds++;
267 }
268 }
269 // TODO(floitsch): we should not need to test against the empty string.
270 bool isUtc = (match[8] !== null) && (match[8] != "");
271 TimeZone timezone = isUtc ? const TimeZone.utc() : new TimeZone.local();
272 int epochValue = Primitives.valueFromDecomposedDate(
273 years, month, day, hours, minutes, seconds, milliseconds, isUtc);
274 if (epochValue === null) {
275 throw new IllegalArgumentException(formattedString);
276 }
277 if (addOneMillisecond) epochValue++;
278 return new DateImplementation.fromEpoch(epochValue, timezone);
279 } else {
280 throw new IllegalArgumentException(formattedString);
281 }
282 }
283
284 const DateImplementation.fromEpoch(this.value, this.timeZone);
285
286 bool operator ==(other) {
287 if (!(other is DateImplementation)) return false;
288 return (value == other.value) && (timeZone == other.timeZone);
289 }
290
291 int compareTo(Date other) {
292 checkNull(other);
293 return value.compareTo(other.value);
294 }
295
296 Date changeTimeZone(TimeZone targetTimeZone) {
297 if (targetTimeZone == null) {
298 targetTimeZone = new TimeZoneImplementation.local();
299 }
300 return new Date.fromEpoch(value, targetTimeZone);
301 }
302
303 int get year() => Primitives.getYear(this);
304
305 int get month() => Primitives.getMonth(this);
306
307 int get day() => Primitives.getDay(this);
308
309 int get hours() => Primitives.getHours(this);
310
311 int get minutes() => Primitives.getMinutes(this);
312
313 int get seconds() => Primitives.getSeconds(this);
314
315 int get milliseconds() => Primitives.getMilliseconds(this);
316
317 int get weekday() {
318 // Adjust by one because JS weeks start on Sunday.
319 var day = Primitives.getWeekday(this);
320 return (day + 6) % 7;
321 }
322
323 bool isLocalTime() {
324 return !timeZone.isUtc;
325 }
326
327 bool isUtc() {
328 return timeZone.isUtc;
329 }
330
331 String toString() {
332 String fourDigits(int n) {
333 int absN = n.abs();
334 String sign = n < 0 ? "-" : "";
335 if (absN >= 1000) return "$n";
336 if (absN >= 100) return "${sign}0$absN";
337 if (absN >= 10) return "${sign}00$absN";
338 if (absN >= 1) return "${sign}000$absN";
339 throw new IllegalArgumentException(n);
340 }
341
342 String threeDigits(int n) {
343 if (n >= 100) return "${n}";
344 if (n > 10) return "0${n}";
345 return "00${n}";
346 }
347
348 String twoDigits(int n) {
349 if (n >= 10) return "${n}";
350 return "0${n}";
351 }
352
353 String y = fourDigits(year);
354 String m = twoDigits(month);
355 String d = twoDigits(day);
356 String h = twoDigits(hours);
357 String min = twoDigits(minutes);
358 String sec = twoDigits(seconds);
359 String ms = threeDigits(milliseconds);
360 if (timeZone.isUtc) {
361 return "$y-$m-$d $h:$min:$sec.${ms}Z";
362 } else {
363 return "$y-$m-$d $h:$min:$sec.$ms";
364 }
365 }
366
367 // Adds the [duration] to this Date instance.
368 Date add(Duration duration) {
369 checkNull(duration);
370 return new DateImplementation.fromEpoch(value + duration.inMilliseconds,
371 timeZone);
372 }
373
374 // Subtracts the [duration] from this Date instance.
375 Date subtract(Duration duration) {
376 checkNull(duration);
377 return new DateImplementation.fromEpoch(value - duration.inMilliseconds,
378 timeZone);
379 }
380
381 // Returns a [Duration] with the difference of [this] and [other].
382 Duration difference(Date other) {
383 checkNull(other);
384 return new Duration(milliseconds: value - other.value);
385 }
386
387 // Lazily keep a JS Date stored in the dart object.
388 var _asJs() => Primitives.lazyAsJsDate(this);
389 }
390
391 class ListFactory<E> implements List<E> {
392 factory List([int length]) => Primitives.newList(length);
393 factory List.from(Iterable<E> other) {
394 List<E> result = new List<E>();
395 // TODO(ahe): Use for-in when it is implemented correctly.
396 Iterator<E> iterator = other.iterator();
397 while (iterator.hasNext()) {
398 result.add(iterator.next());
399 }
400 return result;
401 }
402 }
OLDNEW
« no previous file with comments | « frog/leg/lib/mock.dart ('k') | frog/leg/lib/native_helper.dart » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698