OLD | NEW |
1 // Copyright (c) 2015, the Dart project authors. Please see the AUTHORS file | 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 | 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 * Support for interoperating with JavaScript. | 6 * Support for interoperating with JavaScript. |
7 * | 7 * |
8 * This library provides access to JavaScript objects from Dart, allowing | 8 * This library provides access to JavaScript objects from Dart, allowing |
9 * Dart code to get and set properties, and call methods of JavaScript objects | 9 * Dart code to get and set properties, and call methods of JavaScript objects |
10 * and invoke JavaScript functions. The library takes care of converting | 10 * and invoke JavaScript functions. The library takes care of converting |
(...skipping 75 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
86 * var jsArray = new JsObject.jsify([1, 2, 3]); | 86 * var jsArray = new JsObject.jsify([1, 2, 3]); |
87 */ | 87 */ |
88 library dart.js; | 88 library dart.js; |
89 | 89 |
90 import 'dart:collection' show HashMap, ListMixin; | 90 import 'dart:collection' show HashMap, ListMixin; |
91 | 91 |
92 import 'dart:_interceptors' as _interceptors show JSArray; | 92 import 'dart:_interceptors' as _interceptors show JSArray; |
93 import 'dart:_js_helper' show Primitives; | 93 import 'dart:_js_helper' show Primitives; |
94 import 'dart:_foreign_helper' show JS; | 94 import 'dart:_foreign_helper' show JS; |
95 | 95 |
96 final JsObject context = _wrapToDart(JS('', 'dart.global')); | 96 final _global = JS('', 'dart.global'); |
| 97 final JsObject context = _wrapToDart(_global); |
97 | 98 |
98 /** | 99 /** |
99 * Proxies a JavaScript object to Dart. | 100 * Proxies a JavaScript object to Dart. |
100 * | 101 * |
101 * The properties of the JavaScript object are accessible via the `[]` and | 102 * The properties of the JavaScript object are accessible via the `[]` and |
102 * `[]=` operators. Methods are callable via [callMethod]. | 103 * `[]=` operators. Methods are callable via [callMethod]. |
103 */ | 104 */ |
104 class JsObject { | 105 class JsObject { |
105 // The wrapped JS object. | 106 // The wrapped JS object. |
106 final dynamic _jsObject; | 107 final dynamic _jsObject; |
(...skipping 360 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
467 var wrapper = JS('', 'function(/*...arguments*/) {' | 468 var wrapper = JS('', 'function(/*...arguments*/) {' |
468 ' let args = Array.prototype.map.call(arguments, #);' | 469 ' let args = Array.prototype.map.call(arguments, #);' |
469 ' return #(#(...args));' | 470 ' return #(#(...args));' |
470 '}', _convertToDart, _convertToJS, f); | 471 '}', _convertToDart, _convertToJS, f); |
471 _dartProxies[wrapper] = f; | 472 _dartProxies[wrapper] = f; |
472 return wrapper; | 473 return wrapper; |
473 } | 474 } |
474 | 475 |
475 // converts a Dart object to a reference to a native JS object | 476 // converts a Dart object to a reference to a native JS object |
476 // which might be a DartObject JS->Dart proxy | 477 // which might be a DartObject JS->Dart proxy |
477 Object _convertToDart(o) { | 478 Object _convertToDart(o, [bool isBrowserType(x)]) { |
| 479 if (isBrowserType == null) isBrowserType = _isBrowserType; |
478 if (JS('bool', '# == null', o) || | 480 if (JS('bool', '# == null', o) || |
479 JS('bool', 'typeof # == "string"', o) || | 481 JS('bool', 'typeof # == "string"', o) || |
480 JS('bool', 'typeof # == "number"', o) || | 482 JS('bool', 'typeof # == "number"', o) || |
481 JS('bool', 'typeof # == "boolean"', o) || | 483 JS('bool', 'typeof # == "boolean"', o) || |
482 _isBrowserType(o)) { | 484 isBrowserType(o)) { |
483 return o; | 485 return o; |
484 } else if (JS('bool', '# instanceof Date', o)) { | 486 } else if (JS('bool', '# instanceof Date', o)) { |
485 var ms = JS('num', '#.getTime()', o); | 487 var ms = JS('num', '#.getTime()', o); |
486 return new DateTime.fromMillisecondsSinceEpoch(ms); | 488 return new DateTime.fromMillisecondsSinceEpoch(ms); |
487 } else if (o is _DartObject && | 489 } else if (o is _DartObject && |
488 JS('bool', 'dart.jsobject != dart.realRuntimeType(#)', o)) { | 490 JS('bool', 'dart.jsobject != dart.realRuntimeType(#)', o)) { |
489 return o._dartObj; | 491 return o._dartObj; |
490 } else { | 492 } else { |
491 return _putIfAbsent(_dartProxies, o, _wrapToDart); | 493 return _putIfAbsent(_dartProxies, o, _wrapToDart); |
492 } | 494 } |
(...skipping 54 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
547 ' let args = [this];' | 549 ' let args = [this];' |
548 ' for (let arg of arguments) {' | 550 ' for (let arg of arguments) {' |
549 ' args.push(arg);' | 551 ' args.push(arg);' |
550 ' }' | 552 ' }' |
551 ' return #(...args);' | 553 ' return #(...args);' |
552 '}', | 554 '}', |
553 f); | 555 f); |
554 _interopCaptureThisExpando[f] = ret; | 556 _interopCaptureThisExpando[f] = ret; |
555 } | 557 } |
556 return ret; | 558 return ret; |
557 } | 559 } |
OLD | NEW |