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 /// dart2js "primitives", that is, features that cannot be implemented without | |
6 /// access to JavaScript features. | |
7 library dart2js._js_primitives; | |
8 | |
9 import 'dart:_foreign_helper' show | |
10 JS; | |
11 | |
12 /** | |
13 * This is the low-level method that is used to implement [print]. It is | |
14 * possible to override this function from JavaScript by defining a function in | |
15 * JavaScript called "dartPrint". | |
16 * | |
17 * Notice that it is also possible to intercept calls to [print] from within a | |
18 * Dart program using zones. This means that there is no guarantee that a call | |
19 * to print ends in this method. | |
20 */ | |
21 void printString(String string) { | |
22 if (JS('bool', r'typeof dartPrint == "function"')) { | |
23 // Support overriding print from JavaScript. | |
24 JS('void', r'dartPrint(#)', string); | |
25 return; | |
26 } | |
27 | |
28 // Inside browser or nodejs. | |
29 if (JS('bool', r'typeof console == "object"') && | |
30 JS('bool', r'typeof console.log != "undefined"')) { | |
31 JS('void', r'console.log(#)', string); | |
32 return; | |
33 } | |
34 | |
35 // Don't throw inside IE, the console is only defined if dev tools is open. | |
36 if (JS('bool', r'typeof window == "object"')) { | |
37 return; | |
38 } | |
39 | |
40 // Running in d8, the V8 developer shell, or in Firefox' js-shell. | |
41 if (JS('bool', r'typeof print == "function"')) { | |
42 JS('void', r'print(#)', string); | |
43 return; | |
44 } | |
45 | |
46 // This is somewhat nasty, but we don't want to drag in a bunch of | |
47 // dependencies to handle a situation that cannot happen. So we | |
48 // avoid using Dart [:throw:] and Dart [toString]. | |
49 JS('void', 'throw "Unable to print message: " + String(#)', string); | |
50 } | |
OLD | NEW |