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 72 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
83 * | 83 * |
84 * This expression creates a JavaScript array: | 84 * This expression creates a JavaScript array: |
85 * | 85 * |
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 JsName, 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 JsObject context = _wrapToDart(JS('', 'dart.global')); |
97 | 97 |
98 /** | 98 /** |
99 * Proxies a JavaScript object to Dart. | 99 * Proxies a JavaScript object to Dart. |
100 * | 100 * |
101 * The properties of the JavaScript object are accessible via the `[]` and | 101 * The properties of the JavaScript object are accessible via the `[]` and |
102 * `[]=` operators. Methods are callable via [callMethod]. | 102 * `[]=` operators. Methods are callable via [callMethod]. |
103 */ | 103 */ |
(...skipping 402 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
506 final _jsProxies = JS('', 'new WeakMap()'); | 506 final _jsProxies = JS('', 'new WeakMap()'); |
507 | 507 |
508 Object _putIfAbsent(weakMap, o, getValue(o)) { | 508 Object _putIfAbsent(weakMap, o, getValue(o)) { |
509 var value = JS('', '#.get(#)', weakMap, o); | 509 var value = JS('', '#.get(#)', weakMap, o); |
510 if (value == null) { | 510 if (value == null) { |
511 value = getValue(o); | 511 value = getValue(o); |
512 JS('', '#.set(#, #)', weakMap, o, value); | 512 JS('', '#.set(#, #)', weakMap, o, value); |
513 } | 513 } |
514 return value; | 514 return value; |
515 } | 515 } |
| 516 |
| 517 // The allowInterop method is a no-op in Dart Dev Compiler. |
| 518 // TODO(jacobr): tag methods so we can throw if a Dart method is passed to |
| 519 // JavaScript using the new interop without calling allowInterop. |
| 520 |
| 521 /// Returns a wrapper around function [f] that can be called from JavaScript |
| 522 /// using the package:js Dart-JavaScript interop. |
| 523 /// |
| 524 /// For performance reasons in Dart2Js, by default Dart functions cannot be |
| 525 /// passed directly to JavaScript unless this method is called to create |
| 526 /// a Function compatible with both Dart and JavaScript. |
| 527 /// Calling this method repeatedly on a function will return the same function. |
| 528 /// The [Function] returned by this method can be used from both Dart and |
| 529 /// JavaScript. We may remove the need to call this method completely in the |
| 530 /// future if Dart2Js is refactored so that its function calling conventions |
| 531 /// are more compatible with JavaScript. |
| 532 Function allowInterop(Function f) => f; |
| 533 |
| 534 Expando<Function> _interopCaptureThisExpando = new Expando<Function>(); |
| 535 |
| 536 /// Returns a [Function] that when called from JavaScript captures its 'this' |
| 537 /// binding and calls [f] with the value of this passed as the first argument. |
| 538 /// When called from Dart, [null] will be passed as the first argument. |
| 539 /// |
| 540 /// See the documention for [allowInterop]. This method should only be used with |
| 541 /// package:js Dart-JavaScript interop. |
| 542 Function allowInteropCaptureThis(Function f) { |
| 543 var ret = _interopCaptureThisExpando[f]; |
| 544 if (ret == null) { |
| 545 ret = JS('', |
| 546 'function(/*...arguments*/) {' |
| 547 ' let args = [this];' |
| 548 ' for (let arg of arguments) {' |
| 549 ' args.push(arg);' |
| 550 ' }' |
| 551 ' return #(...args);' |
| 552 '}', |
| 553 f); |
| 554 _interopCaptureThisExpando[f] = ret; |
| 555 } |
| 556 return ret; |
| 557 } |
OLD | NEW |