OLD | NEW |
1 // Copyright (c) 2013, the Dart project authors. Please see the AUTHORS file | 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 | 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 /** | 5 /** |
6 * The js.dart library provides simple JavaScript invocation from Dart that | 6 * The js.dart library provides simple JavaScript invocation from Dart that |
7 * works on both Dartium and on other modern browsers via Dart2JS. | 7 * works on both Dartium and on other modern browsers via Dart2JS. |
8 * | 8 * |
9 * It provides a model based on scoped [JsObject] objects. Proxies give Dart | 9 * It provides a model based on scoped [JsObject] objects. Proxies give Dart |
10 * code access to JavaScript objects, fields, and functions as well as the | 10 * code access to JavaScript objects, fields, and functions as well as the |
(...skipping 58 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
69 import 'dart:html'; | 69 import 'dart:html'; |
70 import 'dart:isolate'; | 70 import 'dart:isolate'; |
71 | 71 |
72 // Global ports to manage communication from Dart to JS. | 72 // Global ports to manage communication from Dart to JS. |
73 SendPortSync _jsPortSync = window.lookupPort('dart-js-context'); | 73 SendPortSync _jsPortSync = window.lookupPort('dart-js-context'); |
74 SendPortSync _jsPortCreate = window.lookupPort('dart-js-create'); | 74 SendPortSync _jsPortCreate = window.lookupPort('dart-js-create'); |
75 SendPortSync _jsPortInstanceof = window.lookupPort('dart-js-instanceof'); | 75 SendPortSync _jsPortInstanceof = window.lookupPort('dart-js-instanceof'); |
76 SendPortSync _jsPortDeleteProperty = window.lookupPort('dart-js-delete-property'
); | 76 SendPortSync _jsPortDeleteProperty = window.lookupPort('dart-js-delete-property'
); |
77 SendPortSync _jsPortConvert = window.lookupPort('dart-js-convert'); | 77 SendPortSync _jsPortConvert = window.lookupPort('dart-js-convert'); |
78 | 78 |
| 79 |
| 80 JsObject _context; |
| 81 |
79 /** | 82 /** |
80 * Returns a proxy to the global JavaScript context for this page. | 83 * Returns a proxy to the global JavaScript context for this page. |
81 */ | 84 */ |
82 JsObject get context { | 85 JsObject get context { |
83 var port = _jsPortSync; | 86 if (_context == null) { |
84 if (port == null) { | 87 var port = _jsPortSync; |
85 return null; | 88 if (port == null) { |
| 89 return null; |
| 90 } |
| 91 _context = _deserialize(_jsPortSync.callSync([])); |
86 } | 92 } |
87 return _deserialize(_jsPortSync.callSync([])); | 93 return _context; |
88 } | 94 } |
89 | 95 |
90 /** | 96 /** |
91 * Converts a json-like [data] to a JavaScript map or array and return a | 97 * Converts a json-like [data] to a JavaScript map or array and return a |
92 * [JsObject] to it. | 98 * [JsObject] to it. |
93 */ | 99 */ |
94 JsObject jsify(dynamic data) => data == null ? null : new JsObject._json(data); | 100 JsObject jsify(dynamic data) => data == null ? null : new JsObject._json(data); |
95 | 101 |
96 /** | 102 /** |
97 * Converts a local Dart function to a callback that can be passed to | 103 * Converts a local Dart function to a callback that can be passed to |
(...skipping 271 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
369 // Serialized type. | 375 // Serialized type. |
370 return message; | 376 return message; |
371 } | 377 } |
372 var tag = message[0]; | 378 var tag = message[0]; |
373 switch (tag) { | 379 switch (tag) { |
374 case 'funcref': return deserializeFunction(message); | 380 case 'funcref': return deserializeFunction(message); |
375 case 'objref': return deserializeObject(message); | 381 case 'objref': return deserializeObject(message); |
376 } | 382 } |
377 throw 'Unsupported serialized data: $message'; | 383 throw 'Unsupported serialized data: $message'; |
378 } | 384 } |
OLD | NEW |