| 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 * 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 74 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 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:html' show Blob, Event, ImageData, Node, Window; | 90 import 'dart:html' show Blob, Event, ImageData, Node, Window; |
| 91 import 'dart:collection' show HashMap, ListMixin; | 91 import 'dart:collection' show HashMap, ListMixin; |
| 92 import 'dart:indexed_db' show KeyRange; | 92 import 'dart:indexed_db' show KeyRange; |
| 93 import 'dart:typed_data' show TypedData; | 93 import 'dart:typed_data' show TypedData; |
| 94 | 94 |
| 95 import 'dart:_foreign_helper' show JS, DART_CLOSURE_TO_JS; | 95 import 'dart:_foreign_helper' show JS, JS_CONST, DART_CLOSURE_TO_JS; |
| 96 import 'dart:_interceptors' show JavaScriptObject, UnknownJavaScriptObject; | 96 import 'dart:_interceptors' |
| 97 import 'dart:_js_helper' show Primitives, convertDartClosureToJS, | 97 show JavaScriptObject, UnknownJavaScriptObject, DART_CLOSURE_PROPERTY_NAME; |
| 98 getIsolateAffinityTag; | 98 import 'dart:_js_helper' |
| 99 show Primitives, convertDartClosureToJS, getIsolateAffinityTag; |
| 100 |
| 101 export 'dart:_interceptors' show JavaScriptObject; |
| 99 | 102 |
| 100 final JsObject context = _wrapToDart(JS('', 'self')); | 103 final JsObject context = _wrapToDart(JS('', 'self')); |
| 101 | 104 |
| 102 _convertDartFunction(Function f, {bool captureThis: false}) { | 105 _convertDartFunction(Function f, {bool captureThis: false}) { |
| 103 return JS('', | 106 return JS( |
| 104 'function(_call, f, captureThis) {' | 107 '', |
| 105 'return function() {' | 108 ''' |
| 106 'return _call(f, captureThis, this, ' | 109 function(_call, f, captureThis) { |
| 107 'Array.prototype.slice.apply(arguments));' | 110 return function() { |
| 108 '}' | 111 return _call(f, captureThis, this, |
| 109 '}(#, #, #)', DART_CLOSURE_TO_JS(_callDartFunction), f, captureThis); | 112 Array.prototype.slice.apply(arguments)); |
| 113 } |
| 114 }(#, #, #) |
| 115 ''', |
| 116 DART_CLOSURE_TO_JS(_callDartFunction), |
| 117 f, |
| 118 captureThis); |
| 110 } | 119 } |
| 111 | 120 |
| 112 _callDartFunction(callback, bool captureThis, self, List arguments) { | 121 _callDartFunction(callback, bool captureThis, self, List arguments) { |
| 113 if (captureThis) { | 122 if (captureThis) { |
| 114 arguments = [self]..addAll(arguments); | 123 arguments = [self]..addAll(arguments); |
| 115 } | 124 } |
| 116 var dartArgs = new List.from(arguments.map(_convertToDart)); | 125 var dartArgs = new List.from(arguments.map(_convertToDart)); |
| 117 return _convertToJS(Function.apply(callback, dartArgs)); | 126 return _convertToJS(Function.apply(callback, dartArgs)); |
| 118 } | 127 } |
| 119 | 128 |
| (...skipping 85 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 205 * | 214 * |
| 206 * Use this constructor only if you wish to get access to JavaScript | 215 * Use this constructor only if you wish to get access to JavaScript |
| 207 * properties attached to a browser host object, such as a Node or Blob, that | 216 * properties attached to a browser host object, such as a Node or Blob, that |
| 208 * is normally automatically converted into a native Dart object. | 217 * is normally automatically converted into a native Dart object. |
| 209 * | 218 * |
| 210 * An exception will be thrown if [object] either is `null` or has the type | 219 * An exception will be thrown if [object] either is `null` or has the type |
| 211 * `bool`, `num`, or `String`. | 220 * `bool`, `num`, or `String`. |
| 212 */ | 221 */ |
| 213 factory JsObject.fromBrowserObject(object) { | 222 factory JsObject.fromBrowserObject(object) { |
| 214 if (object is num || object is String || object is bool || object == null) { | 223 if (object is num || object is String || object is bool || object == null) { |
| 215 throw new ArgumentError( | 224 throw new ArgumentError("object cannot be a num, string, bool, or null"); |
| 216 "object cannot be a num, string, bool, or null"); | |
| 217 } | 225 } |
| 218 return _wrapToDart(_convertToJS(object)); | 226 return _wrapToDart(_convertToJS(object)); |
| 219 } | 227 } |
| 220 | 228 |
| 221 /** | 229 /** |
| 222 * Recursively converts a JSON-like collection of Dart objects to a | 230 * Recursively converts a JSON-like collection of Dart objects to a |
| 223 * collection of JavaScript objects and returns a [JsObject] proxy to it. | 231 * collection of JavaScript objects and returns a [JsObject] proxy to it. |
| 224 * | 232 * |
| 225 * [object] must be a [Map] or [Iterable], the contents of which are also | 233 * [object] must be a [Map] or [Iterable], the contents of which are also |
| 226 * converted. Maps and Iterables are copied to a new JavaScript object. | 234 * converted. Maps and Iterables are copied to a new JavaScript object. |
| (...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 260 | 268 |
| 261 return _convert(data); | 269 return _convert(data); |
| 262 } | 270 } |
| 263 | 271 |
| 264 /** | 272 /** |
| 265 * Returns the value associated with [property] from the proxied JavaScript | 273 * Returns the value associated with [property] from the proxied JavaScript |
| 266 * object. | 274 * object. |
| 267 * | 275 * |
| 268 * The type of [property] must be either [String] or [num]. | 276 * The type of [property] must be either [String] or [num]. |
| 269 */ | 277 */ |
| 270 dynamic operator[](property) { | 278 dynamic operator [](property) { |
| 271 if (property is! String && property is! num) { | 279 if (property is! String && property is! num) { |
| 272 throw new ArgumentError("property is not a String or num"); | 280 throw new ArgumentError("property is not a String or num"); |
| 273 } | 281 } |
| 274 return _convertToDart(JS('', '#[#]', _jsObject, property)); | 282 return _convertToDart(JS('', '#[#]', _jsObject, property)); |
| 275 } | 283 } |
| 276 | 284 |
| 277 /** | 285 /** |
| 278 * Sets the value associated with [property] on the proxied JavaScript | 286 * Sets the value associated with [property] on the proxied JavaScript |
| 279 * object. | 287 * object. |
| 280 * | 288 * |
| 281 * The type of [property] must be either [String] or [num]. | 289 * The type of [property] must be either [String] or [num]. |
| 282 */ | 290 */ |
| 283 operator[]=(property, value) { | 291 operator []=(property, value) { |
| 284 if (property is! String && property is! num) { | 292 if (property is! String && property is! num) { |
| 285 throw new ArgumentError("property is not a String or num"); | 293 throw new ArgumentError("property is not a String or num"); |
| 286 } | 294 } |
| 287 JS('', '#[#]=#', _jsObject, property, _convertToJS(value)); | 295 JS('', '#[#]=#', _jsObject, property, _convertToJS(value)); |
| 288 } | 296 } |
| 289 | 297 |
| 290 int get hashCode => 0; | 298 int get hashCode => 0; |
| 291 | 299 |
| 292 bool operator==(other) => other is JsObject && | 300 bool operator ==(other) => |
| 293 JS('bool', '# === #', _jsObject, other._jsObject); | 301 other is JsObject && JS('bool', '# === #', _jsObject, other._jsObject); |
| 294 | 302 |
| 295 /** | 303 /** |
| 296 * Returns `true` if the JavaScript object contains the specified property | 304 * Returns `true` if the JavaScript object contains the specified property |
| 297 * either directly or though its prototype chain. | 305 * either directly or though its prototype chain. |
| 298 * | 306 * |
| 299 * This is the equivalent of the `in` operator in JavaScript. | 307 * This is the equivalent of the `in` operator in JavaScript. |
| 300 */ | 308 */ |
| 301 bool hasProperty(property) { | 309 bool hasProperty(property) { |
| 302 if (property is! String && property is! num) { | 310 if (property is! String && property is! num) { |
| 303 throw new ArgumentError("property is not a String or num"); | 311 throw new ArgumentError("property is not a String or num"); |
| (...skipping 21 matching lines...) Expand all Loading... |
| 325 bool instanceof(JsFunction type) { | 333 bool instanceof(JsFunction type) { |
| 326 return JS('bool', '# instanceof #', _jsObject, _convertToJS(type)); | 334 return JS('bool', '# instanceof #', _jsObject, _convertToJS(type)); |
| 327 } | 335 } |
| 328 | 336 |
| 329 /** | 337 /** |
| 330 * Returns the result of the JavaScript objects `toString` method. | 338 * Returns the result of the JavaScript objects `toString` method. |
| 331 */ | 339 */ |
| 332 String toString() { | 340 String toString() { |
| 333 try { | 341 try { |
| 334 return JS('String', 'String(#)', _jsObject); | 342 return JS('String', 'String(#)', _jsObject); |
| 335 } catch(e) { | 343 } catch (e) { |
| 336 return super.toString(); | 344 return super.toString(); |
| 337 } | 345 } |
| 338 } | 346 } |
| 339 | 347 |
| 340 /** | 348 /** |
| 341 * Calls [method] on the JavaScript object with the arguments [args] and | 349 * Calls [method] on the JavaScript object with the arguments [args] and |
| 342 * returns the result. | 350 * returns the result. |
| 343 * | 351 * |
| 344 * The type of [method] must be either [String] or [num]. | 352 * The type of [method] must be either [String] or [num]. |
| 345 */ | 353 */ |
| 346 dynamic callMethod(method, [List args]) { | 354 dynamic callMethod(method, [List args]) { |
| 347 if (method is! String && method is! num) { | 355 if (method is! String && method is! num) { |
| 348 throw new ArgumentError("method is not a String or num"); | 356 throw new ArgumentError("method is not a String or num"); |
| 349 } | 357 } |
| 350 return _convertToDart(JS('', '#[#].apply(#, #)', _jsObject, method, | 358 return _convertToDart(JS( |
| 359 '', |
| 360 '#[#].apply(#, #)', |
| 361 _jsObject, |
| 362 method, |
| 351 _jsObject, | 363 _jsObject, |
| 352 args == null ? null : new List.from(args.map(_convertToJS)))); | 364 args == null ? null : new List.from(args.map(_convertToJS)))); |
| 353 } | 365 } |
| 354 } | 366 } |
| 355 | 367 |
| 356 /** | 368 /** |
| 357 * Proxies a JavaScript Function object. | 369 * Proxies a JavaScript Function object. |
| 358 */ | 370 */ |
| 359 class JsFunction extends JsObject { | 371 class JsFunction extends JsObject { |
| 360 | |
| 361 /** | 372 /** |
| 362 * Returns a [JsFunction] that captures its 'this' binding and calls [f] | 373 * Returns a [JsFunction] that captures its 'this' binding and calls [f] |
| 363 * with the value of this passed as the first argument. | 374 * with the value of this passed as the first argument. |
| 364 */ | 375 */ |
| 365 factory JsFunction.withThis(Function f) { | 376 factory JsFunction.withThis(Function f) { |
| 366 var jsFunc = _convertDartFunction(f, captureThis: true); | 377 var jsFunc = _convertDartFunction(f, captureThis: true); |
| 367 return new JsFunction._fromJs(jsFunc); | 378 return new JsFunction._fromJs(jsFunc); |
| 368 } | 379 } |
| 369 | 380 |
| 370 JsFunction._fromJs(jsObject) : super._fromJs(jsObject); | 381 JsFunction._fromJs(jsObject) : super._fromJs(jsObject); |
| 371 | 382 |
| 372 /** | 383 /** |
| 373 * Invokes the JavaScript function with arguments [args]. If [thisArg] is | 384 * Invokes the JavaScript function with arguments [args]. If [thisArg] is |
| 374 * supplied it is the value of `this` for the invocation. | 385 * supplied it is the value of `this` for the invocation. |
| 375 */ | 386 */ |
| 376 dynamic apply(List args, { thisArg }) => | 387 dynamic apply(List args, {thisArg}) => _convertToDart(JS( |
| 377 _convertToDart(JS('', '#.apply(#, #)', _jsObject, | 388 '', |
| 378 _convertToJS(thisArg), | 389 '#.apply(#, #)', |
| 379 args == null ? null : new List.from(args.map(_convertToJS)))); | 390 _jsObject, |
| 391 _convertToJS(thisArg), |
| 392 args == null ? null : new List.from(args.map(_convertToJS)))); |
| 380 } | 393 } |
| 381 | 394 |
| 382 /** | 395 /** |
| 383 * A [List] that proxies a JavaScript array. | 396 * A [List] that proxies a JavaScript array. |
| 384 */ | 397 */ |
| 385 class JsArray<E> extends JsObject with ListMixin<E> { | 398 class JsArray<E> extends JsObject with ListMixin<E> { |
| 386 | |
| 387 /** | 399 /** |
| 388 * Creates a new JavaScript array. | 400 * Creates a new JavaScript array. |
| 389 */ | 401 */ |
| 390 JsArray() : super._fromJs([]); | 402 JsArray() : super._fromJs([]); |
| 391 | 403 |
| 392 /** | 404 /** |
| 393 * Creates a new JavaScript array and initializes it to the contents of | 405 * Creates a new JavaScript array and initializes it to the contents of |
| 394 * [other]. | 406 * [other]. |
| 395 */ | 407 */ |
| 396 JsArray.from(Iterable<E> other) | 408 JsArray.from(Iterable<E> other) |
| (...skipping 45 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 442 int get length { | 454 int get length { |
| 443 // Check the length honours the List contract. | 455 // Check the length honours the List contract. |
| 444 var len = JS('', '#.length', _jsObject); | 456 var len = JS('', '#.length', _jsObject); |
| 445 // JavaScript arrays have lengths which are unsigned 32-bit integers. | 457 // JavaScript arrays have lengths which are unsigned 32-bit integers. |
| 446 if (JS('bool', 'typeof # === "number" && (# >>> 0) === #', len, len, len)) { | 458 if (JS('bool', 'typeof # === "number" && (# >>> 0) === #', len, len, len)) { |
| 447 return JS('int', '#', len); | 459 return JS('int', '#', len); |
| 448 } | 460 } |
| 449 throw new StateError('Bad JsArray length'); | 461 throw new StateError('Bad JsArray length'); |
| 450 } | 462 } |
| 451 | 463 |
| 452 void set length(int length) { super['length'] = length; } | 464 void set length(int length) { |
| 453 | 465 super['length'] = length; |
| 466 } |
| 454 | 467 |
| 455 // Methods overriden for better performance | 468 // Methods overriden for better performance |
| 456 | 469 |
| 457 void add(E value) { | 470 void add(E value) { |
| 458 callMethod('push', [value]); | 471 callMethod('push', [value]); |
| 459 } | 472 } |
| 460 | 473 |
| 461 void addAll(Iterable<E> iterable) { | 474 void addAll(Iterable<E> iterable) { |
| 462 var list = (JS('bool', '# instanceof Array', iterable)) | 475 var list = (JS('bool', '# instanceof Array', iterable)) |
| 463 ? iterable | 476 ? iterable |
| (...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 496 | 509 |
| 497 void sort([int compare(E a, E b)]) { | 510 void sort([int compare(E a, E b)]) { |
| 498 // Note: arr.sort(null) is a type error in FF | 511 // Note: arr.sort(null) is a type error in FF |
| 499 callMethod('sort', compare == null ? [] : [compare]); | 512 callMethod('sort', compare == null ? [] : [compare]); |
| 500 } | 513 } |
| 501 } | 514 } |
| 502 | 515 |
| 503 // property added to a Dart object referencing its JS-side DartObject proxy | 516 // property added to a Dart object referencing its JS-side DartObject proxy |
| 504 final String _DART_OBJECT_PROPERTY_NAME = | 517 final String _DART_OBJECT_PROPERTY_NAME = |
| 505 getIsolateAffinityTag(r'_$dart_dartObject'); | 518 getIsolateAffinityTag(r'_$dart_dartObject'); |
| 506 final String _DART_CLOSURE_PROPERTY_NAME = | |
| 507 getIsolateAffinityTag(r'_$dart_dartClosure'); | |
| 508 | 519 |
| 509 // property added to a JS object referencing its Dart-side JsObject proxy | 520 // property added to a JS object referencing its Dart-side JsObject proxy |
| 510 const _JS_OBJECT_PROPERTY_NAME = r'_$dart_jsObject'; | 521 const _JS_OBJECT_PROPERTY_NAME = r'_$dart_jsObject'; |
| 511 const _JS_FUNCTION_PROPERTY_NAME = r'$dart_jsFunction'; | 522 const _JS_FUNCTION_PROPERTY_NAME = r'$dart_jsFunction'; |
| 523 const _JS_FUNCTION_PROPERTY_NAME_CAPTURE_THIS = r'_$dart_jsFunctionCaptureThis'; |
| 512 | 524 |
| 513 bool _defineProperty(o, String name, value) { | 525 bool _defineProperty(o, String name, value) { |
| 514 if (_isExtensible(o) && | 526 if (_isExtensible(o) && |
| 515 // TODO(ahe): Calling _hasOwnProperty to work around | 527 // TODO(ahe): Calling _hasOwnProperty to work around |
| 516 // https://code.google.com/p/dart/issues/detail?id=21331. | 528 // https://code.google.com/p/dart/issues/detail?id=21331. |
| 517 !_hasOwnProperty(o, name)) { | 529 !_hasOwnProperty(o, name)) { |
| 518 try { | 530 try { |
| 519 JS('void', 'Object.defineProperty(#, #, { value: #})', o, name, value); | 531 JS('void', 'Object.defineProperty(#, #, { value: #})', o, name, value); |
| 520 return true; | 532 return true; |
| 521 } catch (e) { | 533 } catch (e) { |
| (...skipping 25 matching lines...) Expand all Loading... |
| 547 dynamic _convertToJS(dynamic o) { | 559 dynamic _convertToJS(dynamic o) { |
| 548 // Note: we don't write `if (o == null) return null;` to make sure dart2js | 560 // Note: we don't write `if (o == null) return null;` to make sure dart2js |
| 549 // doesn't convert `return null;` into `return;` (which would make `null` be | 561 // doesn't convert `return null;` into `return;` (which would make `null` be |
| 550 // `undefined` in Javascprit). See dartbug.com/20305 for details. | 562 // `undefined` in Javascprit). See dartbug.com/20305 for details. |
| 551 if (o == null || o is String || o is num || o is bool) { | 563 if (o == null || o is String || o is num || o is bool) { |
| 552 return o; | 564 return o; |
| 553 } | 565 } |
| 554 if (o is JsObject) { | 566 if (o is JsObject) { |
| 555 return o._jsObject; | 567 return o._jsObject; |
| 556 } | 568 } |
| 557 if (o is Blob || o is Event || o is KeyRange || o is ImageData || o is Node || | 569 if (o is Blob || |
| 558 o is TypedData || o is Window) { | 570 o is Event || |
| 571 o is KeyRange || |
| 572 o is ImageData || |
| 573 o is Node || |
| 574 o is TypedData || |
| 575 o is Window) { |
| 559 return o; | 576 return o; |
| 560 } | 577 } |
| 561 if (o is DateTime) { | 578 if (o is DateTime) { |
| 562 return Primitives.lazyAsJsDate(o); | 579 return Primitives.lazyAsJsDate(o); |
| 563 } | 580 } |
| 564 if (o is Function) { | 581 if (o is Function) { |
| 565 return _getJsProxy(o, _JS_FUNCTION_PROPERTY_NAME, (o) { | 582 return _getJsProxy(o, _JS_FUNCTION_PROPERTY_NAME, (o) { |
| 566 var jsFunction = _convertDartFunction(o); | 583 var jsFunction = _convertDartFunction(o); |
| 567 // set a property on the JS closure referencing the Dart closure | 584 // set a property on the JS closure referencing the Dart closure |
| 568 _defineProperty(jsFunction, _DART_CLOSURE_PROPERTY_NAME, o); | 585 _defineProperty(jsFunction, DART_CLOSURE_PROPERTY_NAME, o); |
| 569 return jsFunction; | 586 return jsFunction; |
| 570 }); | 587 }); |
| 571 } | 588 } |
| 572 var ctor = _dartProxyCtor; | 589 var ctor = _dartProxyCtor; |
| 573 return _getJsProxy(o, _JS_OBJECT_PROPERTY_NAME, | 590 return _getJsProxy( |
| 574 (o) => JS('', 'new #(#)', ctor, o)); | 591 o, _JS_OBJECT_PROPERTY_NAME, (o) => JS('', 'new #(#)', ctor, o)); |
| 575 } | 592 } |
| 576 | 593 |
| 577 Object _getJsProxy(o, String propertyName, createProxy(o)) { | 594 Object _getJsProxy(o, String propertyName, createProxy(o)) { |
| 578 var jsProxy = _getOwnProperty(o, propertyName); | 595 var jsProxy = _getOwnProperty(o, propertyName); |
| 579 if (jsProxy == null) { | 596 if (jsProxy == null) { |
| 580 jsProxy = createProxy(o); | 597 jsProxy = createProxy(o); |
| 581 _defineProperty(o, propertyName, jsProxy); | 598 _defineProperty(o, propertyName, jsProxy); |
| 582 } | 599 } |
| 583 return jsProxy; | 600 return jsProxy; |
| 584 } | 601 } |
| 585 | 602 |
| 586 // converts a Dart object to a reference to a native JS object | 603 // converts a Dart object to a reference to a native JS object |
| 587 // which might be a DartObject JS->Dart proxy | 604 // which might be a DartObject JS->Dart proxy |
| 588 Object _convertToDart(o) { | 605 Object _convertToDart(o) { |
| 589 if (JS('bool', '# == null', o) || | 606 if (JS('bool', '# == null', o) || |
| 590 JS('bool', 'typeof # == "string"', o) || | 607 JS('bool', 'typeof # == "string"', o) || |
| 591 JS('bool', 'typeof # == "number"', o) || | 608 JS('bool', 'typeof # == "number"', o) || |
| 592 JS('bool', 'typeof # == "boolean"', o)) { | 609 JS('bool', 'typeof # == "boolean"', o)) { |
| 593 return o; | 610 return o; |
| 594 } else if (_isLocalObject(o) | 611 } else if (_isLocalObject(o) && |
| 595 && (o is Blob || o is Event || o is KeyRange || o is ImageData | 612 (o is Blob || |
| 596 || o is Node || o is TypedData || o is Window)) { | 613 o is Event || |
| 614 o is KeyRange || |
| 615 o is ImageData || |
| 616 o is Node || |
| 617 o is TypedData || |
| 618 o is Window)) { |
| 597 // long line: dart2js doesn't allow string concatenation in the JS() form | 619 // long line: dart2js doesn't allow string concatenation in the JS() form |
| 598 return JS('Blob|Event|KeyRange|ImageData|Node|TypedData|Window', '#', o); | 620 return JS('Blob|Event|KeyRange|ImageData|Node|TypedData|Window', '#', o); |
| 599 } else if (JS('bool', '# instanceof Date', o)) { | 621 } else if (JS('bool', '# instanceof Date', o)) { |
| 600 var ms = JS('num', '#.getTime()', o); | 622 var ms = JS('num', '#.getTime()', o); |
| 601 return new DateTime.fromMillisecondsSinceEpoch(ms); | 623 return new DateTime.fromMillisecondsSinceEpoch(ms); |
| 602 } else if (JS('bool', '#.constructor === #', o, _dartProxyCtor)) { | 624 } else if (JS('bool', '#.constructor === #', o, _dartProxyCtor)) { |
| 603 return JS('', '#.o', o); | 625 return JS('', '#.o', o); |
| 604 } else { | 626 } else { |
| 605 return _wrapToDart(o); | 627 return _wrapToDart(o); |
| 606 } | 628 } |
| 607 } | 629 } |
| 608 | 630 |
| 609 JsObject _wrapToDart(o) { | 631 JsObject _wrapToDart(o) { |
| 610 if (JS('bool', 'typeof # == "function"', o)) { | 632 if (JS('bool', 'typeof # == "function"', o)) { |
| 611 return _getDartProxy(o, _DART_CLOSURE_PROPERTY_NAME, | 633 return _getDartProxy( |
| 612 (o) => new JsFunction._fromJs(o)); | 634 o, DART_CLOSURE_PROPERTY_NAME, (o) => new JsFunction._fromJs(o)); |
| 613 } | 635 } |
| 614 if (JS('bool', '# instanceof Array', o)) { | 636 if (JS('bool', '# instanceof Array', o)) { |
| 615 return _getDartProxy(o, _DART_OBJECT_PROPERTY_NAME, | 637 return _getDartProxy( |
| 616 (o) => new JsArray._fromJs(o)); | 638 o, _DART_OBJECT_PROPERTY_NAME, (o) => new JsArray._fromJs(o)); |
| 617 } | 639 } |
| 618 return _getDartProxy(o, _DART_OBJECT_PROPERTY_NAME, | 640 return _getDartProxy( |
| 619 (o) => new JsObject._fromJs(o)); | 641 o, _DART_OBJECT_PROPERTY_NAME, (o) => new JsObject._fromJs(o)); |
| 620 } | 642 } |
| 621 | 643 |
| 622 Object _getDartProxy(o, String propertyName, createProxy(o)) { | 644 Object _getDartProxy(o, String propertyName, createProxy(o)) { |
| 623 var dartProxy = _getOwnProperty(o, propertyName); | 645 var dartProxy = _getOwnProperty(o, propertyName); |
| 624 // Temporary fix for dartbug.com/15193 | 646 // Temporary fix for dartbug.com/15193 |
| 625 // In some cases it's possible to see a JavaScript object that | 647 // In some cases it's possible to see a JavaScript object that |
| 626 // came from a different context and was previously proxied to | 648 // came from a different context and was previously proxied to |
| 627 // Dart in that context. The JS object will have a cached proxy | 649 // Dart in that context. The JS object will have a cached proxy |
| 628 // but it won't be a valid Dart object in this context. | 650 // but it won't be a valid Dart object in this context. |
| 629 // For now we throw away the cached proxy, but we should be able | 651 // For now we throw away the cached proxy, but we should be able |
| 630 // to cache proxies from multiple JS contexts and Dart isolates. | 652 // to cache proxies from multiple JS contexts and Dart isolates. |
| 631 if (dartProxy == null || !_isLocalObject(o)) { | 653 if (dartProxy == null || !_isLocalObject(o)) { |
| 632 dartProxy = createProxy(o); | 654 dartProxy = createProxy(o); |
| 633 _defineProperty(o, propertyName, dartProxy); | 655 _defineProperty(o, propertyName, dartProxy); |
| 634 } | 656 } |
| 635 return dartProxy; | 657 return dartProxy; |
| 636 } | 658 } |
| 659 |
| 660 // Start of methods for new style Dart-JS interop. |
| 661 |
| 662 const _UNDEFINED = const JS_CONST('void 0'); |
| 663 |
| 664 // TODO(jacobr): this method is a hack to work around the lack of proper dart |
| 665 // support for varargs methods. |
| 666 List _stripUndefinedArgs(List args) => |
| 667 args.takeWhile((i) => JS('bool', '# !== #', i, _UNDEFINED)).toList(); |
| 668 |
| 669 class _JavaScriptFunctionHack implements Function { |
| 670 call( |
| 671 [a = _UNDEFINED, |
| 672 b = _UNDEFINED, |
| 673 c = _UNDEFINED, |
| 674 d = _UNDEFINED, |
| 675 e = _UNDEFINED, |
| 676 f = _UNDEFINED, |
| 677 g = _UNDEFINED, |
| 678 h = _UNDEFINED, |
| 679 i = _UNDEFINED, |
| 680 j = _UNDEFINED]) { |
| 681 // Exceedingly slow default implementation. |
| 682 return JS('', '#.apply(null, #)', this, |
| 683 _stripUndefinedArgs([a, b, c, d, e, f, g, h, i, j])); |
| 684 } |
| 685 } |
| 686 |
| 687 void _copyOwnProperties(src, dest) { |
| 688 JS( |
| 689 '', |
| 690 r''' |
| 691 (function(src, dest) { |
| 692 var properties = Object.getOwnPropertyNames(src); |
| 693 for (var i = 0, len = properties.length; i < len; i++) { |
| 694 var name = properties[i]; |
| 695 dest[name] = src[name]; |
| 696 } |
| 697 })(#, #) |
| 698 ''', |
| 699 src, |
| 700 dest); |
| 701 } |
| 702 |
| 703 // TODO(jacobr): remove this method. So far it appears that specifying the list |
| 704 // of registered types in Dart2Js has significant negative code size |
| 705 // implications so it is better to specify usage purely based on which |
| 706 // libraries are imported. Remove after Dartium is modified to function without |
| 707 // requiring this method. |
| 708 void registerJsInterfaces([List<Type> types]) { |
| 709 // No need to actually register in Dart2JS. |
| 710 var fnHackProto = JS('', '#.__proto__', new _JavaScriptFunctionHack()); |
| 711 var fnProto = JS('', 'Function.prototype'); |
| 712 _copyOwnProperties(fnHackProto, fnProto); |
| 713 // Add optimized call methods for small numbers of arguments. |
| 714 if (JS('bool', r'#.hasOwnProperty("call$0") ', fnHackProto)) { |
| 715 JS('', r'#.call$0 = function() { return this(); }', fnProto); |
| 716 JS('', r'#.call$1 = function(a) { return this(a); }', fnProto); |
| 717 JS('', r'#.call$2 = function(a, b) { return this(a, b); }', fnProto); |
| 718 JS('', r'#.call$3 = function(a, b, c) { return this(a, b, c); }', fnProto); |
| 719 JS('', r'#.call$4 = function(a, b, c, d) { return this(a, b, c, d); }', |
| 720 fnProto); |
| 721 } else { |
| 722 if (!JS('bool', r'#.hasOwnProperty("$0") ', fnHackProto)) { |
| 723 throw 'Internal error. Unexpected minified output'; |
| 724 } |
| 725 JS('', r'#.$0 = function() { return this(); }', fnProto); |
| 726 JS('', r'#.$1 = function(a) { return this(a); }', fnProto); |
| 727 JS('', r'#.$2 = function(a, b) { return this(a, b); }', fnProto); |
| 728 JS('', r'#.$3 = function(a, b, c) { return this(a, b, c); }', fnProto); |
| 729 JS('', r'#.$4 = function(a, b, c, d) { return this(a, b, c, d); }', |
| 730 fnProto); |
| 731 } |
| 732 } |
| 733 |
| 734 _convertDartFunctionFast(Function f) { |
| 735 var existing = JS('', '#.#', f, _JS_FUNCTION_PROPERTY_NAME); |
| 736 if (existing != null) return existing; |
| 737 var ret = JS( |
| 738 '', |
| 739 ''' |
| 740 function(_call, f) { |
| 741 return function() { |
| 742 return _call(f, Array.prototype.slice.apply(arguments)); |
| 743 } |
| 744 }(#, #) |
| 745 ''', |
| 746 DART_CLOSURE_TO_JS(_callDartFunctionFast), |
| 747 f); |
| 748 JS('', '#.# = #', ret, DART_CLOSURE_PROPERTY_NAME, f); |
| 749 JS('', '#.# = #', f, _JS_FUNCTION_PROPERTY_NAME, ret); |
| 750 return ret; |
| 751 } |
| 752 |
| 753 _convertDartFunctionFastCaptureThis(Function f) { |
| 754 var existing = JS('', '#.#', f, _JS_FUNCTION_PROPERTY_NAME_CAPTURE_THIS); |
| 755 if (existing != null) return existing; |
| 756 var ret = JS( |
| 757 '', |
| 758 ''' |
| 759 function(_call, f) { |
| 760 return function() { |
| 761 return _call(f, this,Array.prototype.slice.apply(arguments)); |
| 762 } |
| 763 }(#, #) |
| 764 ''', |
| 765 DART_CLOSURE_TO_JS(_callDartFunctionFastCaptureThis), |
| 766 f); |
| 767 JS('', '#.# = #', ret, DART_CLOSURE_PROPERTY_NAME, f); |
| 768 JS('', '#.# = #', f, _JS_FUNCTION_PROPERTY_NAME_CAPTURE_THIS, ret); |
| 769 return ret; |
| 770 } |
| 771 |
| 772 _callDartFunctionFast(callback, List arguments) { |
| 773 return Function.apply(callback, arguments); |
| 774 } |
| 775 |
| 776 _callDartFunctionFastCaptureThis(callback, self, List arguments) { |
| 777 return _convertToJS(Function.apply(callback, [self]..addAll(arguments))); |
| 778 } |
| 779 |
| 780 Function allowInterop(Function f) { |
| 781 if (JS('bool', 'typeof(#) == "function"', f)) { |
| 782 // Already supports interop, just use the existing function. |
| 783 return f; |
| 784 } else { |
| 785 return _convertDartFunctionFast(f); |
| 786 } |
| 787 } |
| 788 |
| 789 Function allowInteropCaptureThis(Function f) { |
| 790 if (JS('bool', 'typeof(#) == "function"', f)) { |
| 791 // Behavior when the function is already a JS function is unspecified. |
| 792 throw new ArgumentError( |
| 793 "Function is already a JS function so cannot capture this."); |
| 794 return f; |
| 795 } else { |
| 796 return _convertDartFunctionFastCaptureThis(f); |
| 797 } |
| 798 } |
| OLD | NEW |