| 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 65 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 76 * `a` and `b` defined: | 76 * `a` and `b` defined: |
| 77 * | 77 * |
| 78 * var jsMap = new JsObject.jsify({'a': 1, 'b': 2}); | 78 * var jsMap = new JsObject.jsify({'a': 1, 'b': 2}); |
| 79 * | 79 * |
| 80 * This expression creates a JavaScript array: | 80 * This expression creates a JavaScript array: |
| 81 * | 81 * |
| 82 * var jsArray = new JsObject.jsify([1, 2, 3]); | 82 * var jsArray = new JsObject.jsify([1, 2, 3]); |
| 83 */ | 83 */ |
| 84 library dart.js; | 84 library dart.js; |
| 85 | 85 |
| 86 import 'dart:collection' show ListMixin; |
| 86 import 'dart:nativewrappers'; | 87 import 'dart:nativewrappers'; |
| 87 | 88 |
| 88 JsObject _cachedContext; | 89 JsObject _cachedContext; |
| 89 | 90 |
| 90 JsObject get _context native "Js_context_Callback"; | 91 JsObject get _context native "Js_context_Callback"; |
| 91 | 92 |
| 92 JsObject get context { | 93 JsObject get context { |
| 93 if (_cachedContext == null) { | 94 if (_cachedContext == null) { |
| 94 _cachedContext = _context; | 95 _cachedContext = _context; |
| 95 } | 96 } |
| (...skipping 157 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 253 * rather than opaque handles. This method is private because it cannot be | 254 * rather than opaque handles. This method is private because it cannot be |
| 254 * efficiently implemented in Dart2Js so should only be used by internal | 255 * efficiently implemented in Dart2Js so should only be used by internal |
| 255 * tools. | 256 * tools. |
| 256 */ | 257 */ |
| 257 _applyDebuggerOnly(List args, {thisArg}) native "JsFunction_applyDebuggerOnly"
; | 258 _applyDebuggerOnly(List args, {thisArg}) native "JsFunction_applyDebuggerOnly"
; |
| 258 | 259 |
| 259 static JsFunction _withThis(Function f) native "JsFunction_withThis"; | 260 static JsFunction _withThis(Function f) native "JsFunction_withThis"; |
| 260 } | 261 } |
| 261 | 262 |
| 262 /** | 263 /** |
| 264 * A [List] proxying a JavaScript Array. |
| 265 */ |
| 266 class JsArray<E> extends JsObject with ListMixin<E> { |
| 267 |
| 268 factory JsArray() => _newJsArray(); |
| 269 |
| 270 static JsArray _newJsArray() native "JsArray_newJsArray"; |
| 271 |
| 272 factory JsArray.from(Iterable<E> other) => _newJsArrayFromSafeList(new List.fr
om(other)); |
| 273 |
| 274 static JsArray _newJsArrayFromSafeList(List list) native "JsArray_newJsArrayFr
omSafeList"; |
| 275 |
| 276 _checkIndex(int index, {bool insert: false}) { |
| 277 int length = insert ? this.length + 1 : this.length; |
| 278 if (index is int && (index < 0 || index >= length)) { |
| 279 throw new RangeError.range(index, 0, length); |
| 280 } |
| 281 } |
| 282 |
| 283 _checkRange(int start, int end) { |
| 284 int cachedLength = this.length; |
| 285 if (start < 0 || start > cachedLength) { |
| 286 throw new RangeError.range(start, 0, cachedLength); |
| 287 } |
| 288 if (end < start || end > cachedLength) { |
| 289 throw new RangeError.range(end, start, cachedLength); |
| 290 } |
| 291 } |
| 292 |
| 293 // Methods required by ListMixin |
| 294 |
| 295 E operator [](int index) { |
| 296 _checkIndex(index); |
| 297 return super[index]; |
| 298 } |
| 299 |
| 300 void operator []=(int index, E value) { |
| 301 _checkIndex(index); |
| 302 super[index] = value; |
| 303 } |
| 304 |
| 305 int get length native "JsArray_length"; |
| 306 |
| 307 void set length(int length) { super['length'] = length; } |
| 308 |
| 309 // Methods overriden for better performance |
| 310 |
| 311 void add(E value) { |
| 312 callMethod('push', [value]); |
| 313 } |
| 314 |
| 315 void addAll(Iterable<E> iterable) { |
| 316 // TODO(jacobr): this can be optimized slightly. |
| 317 callMethod('push', new List.from(iterable)); |
| 318 } |
| 319 |
| 320 void insert(int index, E element) { |
| 321 _checkIndex(index, insert:true); |
| 322 callMethod('splice', [index, 0, element]); |
| 323 } |
| 324 |
| 325 E removeAt(int index) { |
| 326 _checkIndex(index); |
| 327 return callMethod('splice', [index, 1])[0]; |
| 328 } |
| 329 |
| 330 E removeLast() { |
| 331 if (length == 0) throw new RangeError(-1); |
| 332 return callMethod('pop'); |
| 333 } |
| 334 |
| 335 void removeRange(int start, int end) { |
| 336 _checkRange(start, end); |
| 337 callMethod('splice', [start, end - start]); |
| 338 } |
| 339 |
| 340 void setRange(int start, int end, Iterable<E> iterable, [int skipCount = 0]) { |
| 341 _checkRange(start, end); |
| 342 int length = end - start; |
| 343 if (length == 0) return; |
| 344 if (skipCount < 0) throw new ArgumentError(skipCount); |
| 345 var args = [start, length]..addAll(iterable.skip(skipCount).take(length)); |
| 346 callMethod('splice', args); |
| 347 } |
| 348 |
| 349 void sort([int compare(E a, E b)]) { |
| 350 callMethod('sort', [compare]); |
| 351 } |
| 352 } |
| 353 |
| 354 /** |
| 263 * Placeholder object for cases where we need to determine exactly how many | 355 * Placeholder object for cases where we need to determine exactly how many |
| 264 * args were passed to a function. | 356 * args were passed to a function. |
| 265 */ | 357 */ |
| 266 const _UNDEFINED = const Object(); | 358 const _UNDEFINED = const Object(); |
| 267 | 359 |
| 268 // FIXME(jacobr): this method is a hack to work around the lack of proper dart | 360 // FIXME(jacobr): this method is a hack to work around the lack of proper dart |
| 269 // support for varargs methods. | 361 // support for varargs methods. |
| 270 List _stripUndefinedArgs(List args) => | 362 List _stripUndefinedArgs(List args) => |
| 271 args.takeWhile((i) => i != _UNDEFINED).toList(); | 363 args.takeWhile((i) => i != _UNDEFINED).toList(); |
| 272 | 364 |
| 273 /** | 365 /** |
| 274 * Returns a method that can be called with an arbitrary number (for n less | 366 * Returns a method that can be called with an arbitrary number (for n less |
| 275 * than 11) of arguments without violating Dart type checks. | 367 * than 11) of arguments without violating Dart type checks. |
| 276 */ | 368 */ |
| 277 Function _wrapAsDebuggerVarArgsFunction(JsFunction jsFunction) => | 369 Function _wrapAsDebuggerVarArgsFunction(JsFunction jsFunction) => |
| 278 ([a1=_UNDEFINED, a2=_UNDEFINED, a3=_UNDEFINED, a4=_UNDEFINED, | 370 ([a1=_UNDEFINED, a2=_UNDEFINED, a3=_UNDEFINED, a4=_UNDEFINED, |
| 279 a5=_UNDEFINED, a6=_UNDEFINED, a7=_UNDEFINED, a8=_UNDEFINED, | 371 a5=_UNDEFINED, a6=_UNDEFINED, a7=_UNDEFINED, a8=_UNDEFINED, |
| 280 a9=_UNDEFINED, a10=_UNDEFINED]) => | 372 a9=_UNDEFINED, a10=_UNDEFINED]) => |
| 281 jsFunction._applyDebuggerOnly(_stripUndefinedArgs( | 373 jsFunction._applyDebuggerOnly(_stripUndefinedArgs( |
| 282 [a1,a2,a3,a4,a5,a6,a7,a8,a9,a10])); | 374 [a1,a2,a3,a4,a5,a6,a7,a8,a9,a10])); |
| OLD | NEW |