| 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 255 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 266 * returns the result. | 266 * returns the result. |
| 267 * | 267 * |
| 268 * The type of [method] must be either [String] or [num]. | 268 * The type of [method] must be either [String] or [num]. |
| 269 */ | 269 */ |
| 270 dynamic callMethod(method, [List args]) { | 270 dynamic callMethod(method, [List args]) { |
| 271 if (method is! String && method is! num) { | 271 if (method is! String && method is! num) { |
| 272 throw new ArgumentError("method is not a String or num"); | 272 throw new ArgumentError("method is not a String or num"); |
| 273 } | 273 } |
| 274 if (args != null) args = new List.from(args.map(_convertToJS)); | 274 if (args != null) args = new List.from(args.map(_convertToJS)); |
| 275 var fn = JS('', '#[#]', _jsObject, method); | 275 var fn = JS('', '#[#]', _jsObject, method); |
| 276 if (!JS('bool', '# instanceof Function', fn)) { | 276 if (JS('bool', 'typeof(#) !== "function"', fn)) { |
| 277 throw new NoSuchMethodError(_jsObject, new Symbol(method), args, {}); | 277 throw new NoSuchMethodError(_jsObject, new Symbol(method), args, {}); |
| 278 } | 278 } |
| 279 return _convertToDart(JS('', '#.apply(#, #)', fn, _jsObject, args)); | 279 return _convertToDart(JS('', '#.apply(#, #)', fn, _jsObject, args)); |
| 280 } | 280 } |
| 281 } | 281 } |
| 282 | 282 |
| 283 /** | 283 /** |
| 284 * Proxies a JavaScript Function object. | 284 * Proxies a JavaScript Function object. |
| 285 */ | 285 */ |
| 286 class JsFunction extends JsObject { | 286 class JsFunction extends JsObject { |
| (...skipping 141 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 428 void sort([int compare(E a, E b)]) { | 428 void sort([int compare(E a, E b)]) { |
| 429 // Note: arr.sort(null) is a type error in FF | 429 // Note: arr.sort(null) is a type error in FF |
| 430 callMethod('sort', compare == null ? [] : [compare]); | 430 callMethod('sort', compare == null ? [] : [compare]); |
| 431 } | 431 } |
| 432 } | 432 } |
| 433 | 433 |
| 434 bool _isBrowserType(o) => JS('bool', | 434 bool _isBrowserType(o) => JS('bool', |
| 435 '# instanceof Blob || ' | 435 '# instanceof Blob || ' |
| 436 '# instanceof Event || ' | 436 '# instanceof Event || ' |
| 437 '(window.KeyRange && # instanceof KeyRange) || ' | 437 '(window.KeyRange && # instanceof KeyRange) || ' |
| 438 '(window.IDBKeyRange && # instanceof IDBKeyRange) || ' |
| 438 '# instanceof ImageData || ' | 439 '# instanceof ImageData || ' |
| 439 '# instanceof Node || ' | 440 '# instanceof Node || ' |
| 440 '(window.TypedData && # instanceof TypedData) || ' | 441 // Int8Array.__proto__ is TypedArray. |
| 441 '# instanceof Window', o, o, o, o, o, o, o); | 442 '(window.Int8Array && # instanceof Int8Array.__proto__) || ' |
| 443 '# instanceof Window', o, o, o, o, o, o, o, o); |
| 442 | 444 |
| 443 class _DartObject { | 445 class _DartObject { |
| 444 final _dartObj; | 446 final _dartObj; |
| 445 _DartObject(this._dartObj); | 447 _DartObject(this._dartObj); |
| 446 } | 448 } |
| 447 | 449 |
| 448 dynamic _convertToJS(dynamic o) { | 450 dynamic _convertToJS(dynamic o) { |
| 449 if (o == null || | 451 if (o == null || |
| 450 o is String || | 452 o is String || |
| 451 o is num || | 453 o is num || |
| (...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 484 JS('bool', 'typeof # == "boolean"', o) || | 486 JS('bool', 'typeof # == "boolean"', o) || |
| 485 _isBrowserType(o)) { | 487 _isBrowserType(o)) { |
| 486 return o; | 488 return o; |
| 487 } else if (JS('bool', '# instanceof Date', o)) { | 489 } else if (JS('bool', '# instanceof Date', o)) { |
| 488 var ms = JS('num', '#.getTime()', o); | 490 var ms = JS('num', '#.getTime()', o); |
| 489 return new DateTime.fromMillisecondsSinceEpoch(ms); | 491 return new DateTime.fromMillisecondsSinceEpoch(ms); |
| 490 } else if (o is _DartObject && | 492 } else if (o is _DartObject && |
| 491 JS('bool', 'dart.jsobject != dart.getReifiedType(#)', o)) { | 493 JS('bool', 'dart.jsobject != dart.getReifiedType(#)', o)) { |
| 492 return o._dartObj; | 494 return o._dartObj; |
| 493 } else { | 495 } else { |
| 494 return _putIfAbsent(_dartProxies, o, _wrapToDart); | 496 return _wrapToDart(o); |
| 495 } | 497 } |
| 496 } | 498 } |
| 497 | 499 |
| 498 JsObject _wrapToDart(o) { | 500 JsObject _wrapToDart(o) => _putIfAbsent(_dartProxies, o, _wrapToDartHelper); |
| 501 |
| 502 JsObject _wrapToDartHelper(o) { |
| 499 if (JS('bool', 'typeof # == "function"', o)) { | 503 if (JS('bool', 'typeof # == "function"', o)) { |
| 500 return new JsFunction._fromJs(o); | 504 return new JsFunction._fromJs(o); |
| 501 } | 505 } |
| 502 if (JS('bool', '# instanceof Array', o)) { | 506 if (JS('bool', '# instanceof Array', o)) { |
| 503 return new JsArray._fromJs(o); | 507 return new JsArray._fromJs(o); |
| 504 } | 508 } |
| 505 return new JsObject._fromJs(o); | 509 return new JsObject._fromJs(o); |
| 506 } | 510 } |
| 507 | 511 |
| 508 final _dartProxies = JS('', 'new WeakMap()'); | 512 final _dartProxies = JS('', 'new WeakMap()'); |
| (...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 551 ' for (let arg of arguments) {' | 555 ' for (let arg of arguments) {' |
| 552 ' args.push(arg);' | 556 ' args.push(arg);' |
| 553 ' }' | 557 ' }' |
| 554 ' return #(...args);' | 558 ' return #(...args);' |
| 555 '}', | 559 '}', |
| 556 f); | 560 f); |
| 557 _interopCaptureThisExpando[f] = ret; | 561 _interopCaptureThisExpando[f] = ret; |
| 558 } | 562 } |
| 559 return ret; | 563 return ret; |
| 560 } | 564 } |
| OLD | NEW |