Chromium Code Reviews| 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 453 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 464 final String _DART_OBJECT_PROPERTY_NAME = | 464 final String _DART_OBJECT_PROPERTY_NAME = |
| 465 getIsolateAffinityTag(r'_$dart_dartObject'); | 465 getIsolateAffinityTag(r'_$dart_dartObject'); |
| 466 final String _DART_CLOSURE_PROPERTY_NAME = | 466 final String _DART_CLOSURE_PROPERTY_NAME = |
| 467 getIsolateAffinityTag(r'_$dart_dartClosure'); | 467 getIsolateAffinityTag(r'_$dart_dartClosure'); |
| 468 | 468 |
| 469 // property added to a JS object referencing its Dart-side JsObject proxy | 469 // property added to a JS object referencing its Dart-side JsObject proxy |
| 470 const _JS_OBJECT_PROPERTY_NAME = r'_$dart_jsObject'; | 470 const _JS_OBJECT_PROPERTY_NAME = r'_$dart_jsObject'; |
| 471 const _JS_FUNCTION_PROPERTY_NAME = r'$dart_jsFunction'; | 471 const _JS_FUNCTION_PROPERTY_NAME = r'$dart_jsFunction'; |
| 472 | 472 |
| 473 bool _defineProperty(o, String name, value) { | 473 bool _defineProperty(o, String name, value) { |
| 474 if (JS('bool', 'Object.isExtensible(#)', o)) { | 474 if (_isExtensible(o) && !_hasOwnProperty(o, name)) { |
| 475 try { | 475 try { |
| 476 JS('void', 'Object.defineProperty(#, #, { value: #})', o, name, value); | 476 JS('void', 'Object.defineProperty(#, #, { value: #})', o, name, value); |
| 477 return true; | 477 return true; |
| 478 } catch(e) { | 478 } catch (e) { |
| 479 // object is native and lies about being extensible | 479 // object is native and lies about being extensible |
| 480 // see https://bugzilla.mozilla.org/show_bug.cgi?id=775185 | 480 // see https://bugzilla.mozilla.org/show_bug.cgi?id=775185 |
| 481 } | 481 } |
| 482 } | 482 } |
| 483 return false; | 483 return false; |
| 484 } | 484 } |
| 485 | 485 |
| 486 bool _hasOwnProperty(o, String name) { | |
| 487 return JS('bool', 'Object.prototype.hasOwnProperty.call(#, #)', o, name); | |
| 488 } | |
| 489 | |
| 490 bool _isExtensible(o) => JS('bool', 'Object.isExtensible(#)', o); | |
| 491 | |
| 486 Object _getOwnProperty(o, String name) { | 492 Object _getOwnProperty(o, String name) { |
| 487 if (JS('bool', 'Object.prototype.hasOwnProperty.call(#, #)', o, name)) { | 493 if (_hasOwnProperty(o, name) { |
| 488 return JS('', '#[#]', o, name); | 494 return JS('', '#[#]', o, name); |
| 489 } | 495 } |
| 490 return null; | 496 return null; |
| 491 } | 497 } |
| 492 | 498 |
| 493 bool _isLocalObject(o) => JS('bool', '# instanceof Object', o); | 499 bool _isLocalObject(o) => JS('bool', '# instanceof Object', o); |
| 494 | 500 |
| 495 // The shared constructor function for proxies to Dart objects in JavaScript. | 501 // The shared constructor function for proxies to Dart objects in JavaScript. |
| 496 final _dartProxyCtor = JS('', 'function DartObject(o) { this.o = o; }'); | 502 final _dartProxyCtor = JS('', 'function DartObject(o) { this.o = o; }'); |
| 497 | 503 |
| (...skipping 73 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 571 var dartProxy = _getOwnProperty(o, propertyName); | 577 var dartProxy = _getOwnProperty(o, propertyName); |
| 572 // Temporary fix for dartbug.com/15193 | 578 // Temporary fix for dartbug.com/15193 |
| 573 // In some cases it's possible to see a JavaScript object that | 579 // In some cases it's possible to see a JavaScript object that |
| 574 // came from a different context and was previously proxied to | 580 // came from a different context and was previously proxied to |
| 575 // Dart in that context. The JS object will have a cached proxy | 581 // Dart in that context. The JS object will have a cached proxy |
| 576 // but it won't be a valid Dart object in this context. | 582 // but it won't be a valid Dart object in this context. |
| 577 // For now we throw away the cached proxy, but we should be able | 583 // For now we throw away the cached proxy, but we should be able |
| 578 // to cache proxies from multiple JS contexts and Dart isolates. | 584 // to cache proxies from multiple JS contexts and Dart isolates. |
| 579 if (dartProxy == null || !_isLocalObject(o)) { | 585 if (dartProxy == null || !_isLocalObject(o)) { |
| 580 dartProxy = createProxy(o); | 586 dartProxy = createProxy(o); |
| 581 _defineProperty(o, propertyName, dartProxy); | 587 _defineProperty(o, propertyName, dartProxy); |
|
ahe
2014/10/13 11:36:23
Perhaps the problem is here.
I'm using dart:js to
| |
| 582 } | 588 } |
| 583 return dartProxy; | 589 return dartProxy; |
| 584 } | 590 } |
| OLD | NEW |