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 171 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
182 | 182 |
183 return _convert(data); | 183 return _convert(data); |
184 } | 184 } |
185 | 185 |
186 /** | 186 /** |
187 * Returns the value associated with [property] from the proxied JavaScript | 187 * Returns the value associated with [property] from the proxied JavaScript |
188 * object. | 188 * object. |
189 * | 189 * |
190 * The type of [property] must be either [String] or [num]. | 190 * The type of [property] must be either [String] or [num]. |
191 */ | 191 */ |
192 dynamic operator [](property) { | 192 dynamic operator [](Object property) { |
193 if (property is! String && property is! num) { | 193 if (property is! String && property is! num) { |
194 throw new ArgumentError("property is not a String or num"); | 194 throw new ArgumentError("property is not a String or num"); |
195 } | 195 } |
196 return _convertToDart(JS('', '#[#]', _jsObject, property)); | 196 return _convertToDart(JS('', '#[#]', _jsObject, property)); |
197 } | 197 } |
198 | 198 |
199 /** | 199 /** |
200 * Sets the value associated with [property] on the proxied JavaScript | 200 * Sets the value associated with [property] on the proxied JavaScript |
201 * object. | 201 * object. |
202 * | 202 * |
203 * The type of [property] must be either [String] or [num]. | 203 * The type of [property] must be either [String] or [num]. |
204 */ | 204 */ |
205 operator []=(property, value) { | 205 operator []=(Object property, value) { |
206 if (property is! String && property is! num) { | 206 if (property is! String && property is! num) { |
207 throw new ArgumentError("property is not a String or num"); | 207 throw new ArgumentError("property is not a String or num"); |
208 } | 208 } |
209 JS('', '#[#] = #', _jsObject, property, _convertToJS(value)); | 209 JS('', '#[#] = #', _jsObject, property, _convertToJS(value)); |
210 } | 210 } |
211 | 211 |
212 int get hashCode => 0; | 212 int get hashCode => 0; |
213 | 213 |
214 bool operator ==(other) => | 214 bool operator ==(other) => |
215 other is JsObject && JS('bool', '# === #', _jsObject, other._jsObject); | 215 other is JsObject && JS('bool', '# === #', _jsObject, other._jsObject); |
(...skipping 126 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
342 if (start < 0 || start > length) { | 342 if (start < 0 || start > length) { |
343 throw new RangeError.range(start, 0, length); | 343 throw new RangeError.range(start, 0, length); |
344 } | 344 } |
345 if (end < start || end > length) { | 345 if (end < start || end > length) { |
346 throw new RangeError.range(end, start, length); | 346 throw new RangeError.range(end, start, length); |
347 } | 347 } |
348 } | 348 } |
349 | 349 |
350 // Methods required by ListMixin | 350 // Methods required by ListMixin |
351 | 351 |
352 E operator [](index) { | 352 E operator [](Object index) { |
353 // TODO(justinfagnani): fix the semantics for non-ints | 353 // TODO(justinfagnani): fix the semantics for non-ints |
354 // dartbug.com/14605 | 354 // dartbug.com/14605 |
355 if (index is num && index == index.toInt()) { | 355 if (index is num && index == index.toInt()) { |
356 _checkIndex(index); | 356 _checkIndex(index); |
357 } | 357 } |
358 return super[index]; | 358 return super[index]; |
359 } | 359 } |
360 | 360 |
361 void operator []=(index, E value) { | 361 void operator []=(Object index, E value) { |
362 // TODO(justinfagnani): fix the semantics for non-ints | 362 // TODO(justinfagnani): fix the semantics for non-ints |
363 // dartbug.com/14605 | 363 // dartbug.com/14605 |
364 if (index is num && index == index.toInt()) { | 364 if (index is num && index == index.toInt()) { |
365 _checkIndex(index); | 365 _checkIndex(index); |
366 } | 366 } |
367 super[index] = value; | 367 super[index] = value; |
368 } | 368 } |
369 | 369 |
370 int get length { | 370 int get length { |
371 // Check the length honours the List contract. | 371 // Check the length honours the List contract. |
(...skipping 113 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
485 var ms = JS('num', '#.getTime()', o); | 485 var ms = JS('num', '#.getTime()', o); |
486 return new DateTime.fromMillisecondsSinceEpoch(ms); | 486 return new DateTime.fromMillisecondsSinceEpoch(ms); |
487 } else if (o is _DartObject && | 487 } else if (o is _DartObject && |
488 JS('bool', 'dart.jsobject != dart.realRuntimeType(#)', o)) { | 488 JS('bool', 'dart.jsobject != dart.realRuntimeType(#)', o)) { |
489 return o._dartObj; | 489 return o._dartObj; |
490 } else { | 490 } else { |
491 return _putIfAbsent(_dartProxies, o, _wrapToDart); | 491 return _putIfAbsent(_dartProxies, o, _wrapToDart); |
492 } | 492 } |
493 } | 493 } |
494 | 494 |
495 _wrapToDart(o) { | 495 JsObject _wrapToDart(o) { |
496 if (JS('bool', 'typeof # == "function"', o)) { | 496 if (JS('bool', 'typeof # == "function"', o)) { |
497 return new JsFunction._fromJs(o); | 497 return new JsFunction._fromJs(o); |
498 } | 498 } |
499 if (JS('bool', '# instanceof Array', o)) { | 499 if (JS('bool', '# instanceof Array', o)) { |
500 return new JsArray._fromJs(o); | 500 return new JsArray._fromJs(o); |
501 } | 501 } |
502 return new JsObject._fromJs(o); | 502 return new JsObject._fromJs(o); |
503 } | 503 } |
504 | 504 |
505 final _dartProxies = JS('', 'new WeakMap()'); | 505 final _dartProxies = JS('', 'new WeakMap()'); |
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 } |
OLD | NEW |