OLD | NEW |
1 // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file | 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 | 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. | 3 // BSD-style license that can be found in the LICENSE file. |
4 | 4 |
5 /// Generic utility functions. Stuff that should possibly be in core. | 5 /// Generic utility functions. Stuff that should possibly be in core. |
6 library utils; | 6 library utils; |
7 | 7 |
8 import 'dart:async'; | 8 import 'dart:async'; |
9 import 'dart:crypto'; | 9 import 'dart:crypto'; |
10 import 'dart:io'; | 10 import 'dart:io'; |
(...skipping 366 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
377 if (uri.path.startsWith("/")) { | 377 if (uri.path.startsWith("/")) { |
378 // Drive-letter paths look like "file:///C:/path/to/file". The replaceFirst | 378 // Drive-letter paths look like "file:///C:/path/to/file". The replaceFirst |
379 // removes the extra initial slash. | 379 // removes the extra initial slash. |
380 return uri.path.replaceFirst("/", "").replaceAll("/", "\\"); | 380 return uri.path.replaceFirst("/", "").replaceAll("/", "\\"); |
381 } else { | 381 } else { |
382 // Network paths look like "file://hostname/path/to/file". | 382 // Network paths look like "file://hostname/path/to/file". |
383 return "\\\\${uri.path.replaceAll("/", "\\")}"; | 383 return "\\\\${uri.path.replaceAll("/", "\\")}"; |
384 } | 384 } |
385 } | 385 } |
386 | 386 |
| 387 /// Wraps [fn] to guard against several different kinds of stack overflow |
| 388 /// exceptions: |
| 389 /// |
| 390 /// * A sufficiently long [Future] chain can cause a stack overflow if there are |
| 391 /// no asynchronous operations in it (issue 9583). |
| 392 /// * A recursive function that recurses too deeply without an asynchronous |
| 393 /// operation can cause a stack overflow. |
| 394 /// * Even if the former is guarded against by adding asynchronous operations, |
| 395 /// returning a value through the [Future] chain can still cause a stack |
| 396 /// overflow. |
| 397 Future resetStack(fn()) { |
| 398 // Using a [Completer] breaks the [Future] chain for the return value and |
| 399 // avoids the third case described above. |
| 400 var completer = new Completer(); |
| 401 |
| 402 // Using [new Future] adds an asynchronous operation that works around the |
| 403 // first and second cases described above. |
| 404 new Future(fn).then((val) { |
| 405 runAsync(() => completer.complete(val)); |
| 406 }).catchError((err) { |
| 407 runAsync(() => completer.completeError(err)); |
| 408 }); |
| 409 return completer.future; |
| 410 } |
| 411 |
387 /// An exception class for exceptions that are intended to be seen by the user. | 412 /// An exception class for exceptions that are intended to be seen by the user. |
388 /// These exceptions won't have any debugging information printed when they're | 413 /// These exceptions won't have any debugging information printed when they're |
389 /// thrown. | 414 /// thrown. |
390 class ApplicationException implements Exception { | 415 class ApplicationException implements Exception { |
391 final String message; | 416 final String message; |
392 | 417 |
393 ApplicationException(this.message); | 418 ApplicationException(this.message); |
394 } | 419 } |
395 | 420 |
396 /// Throw a [ApplicationException] with [message]. | 421 /// Throw a [ApplicationException] with [message]. |
(...skipping 10 matching lines...) Expand all Loading... |
407 error is FileIOException || | 432 error is FileIOException || |
408 error is HttpException || | 433 error is HttpException || |
409 error is HttpParserException || | 434 error is HttpParserException || |
410 error is LinkIOException || | 435 error is LinkIOException || |
411 error is MimeParserException || | 436 error is MimeParserException || |
412 error is OSError || | 437 error is OSError || |
413 error is ProcessException || | 438 error is ProcessException || |
414 error is SocketIOException || | 439 error is SocketIOException || |
415 error is WebSocketException; | 440 error is WebSocketException; |
416 } | 441 } |
OLD | NEW |