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 71 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 82 * var jsMap = new JsObject.jsify({'a': 1, 'b': 2}); | 82 * var jsMap = new JsObject.jsify({'a': 1, 'b': 2}); |
| 83 * | 83 * |
| 84 * This expression creates a JavaScript array: | 84 * This expression creates a JavaScript array: |
| 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:collection' show ListMixin; | 90 import 'dart:collection' show ListMixin; |
| 91 import 'dart:nativewrappers'; | 91 import 'dart:nativewrappers'; |
| 92 import 'dart:math' as math; | |
| 93 import 'dart:mirrors' as mirrors; | |
| 94 | |
| 95 // Lazily initialized flag specifying whether we should check JS invocations. | |
| 96 final bool _checkJsInvocations = (() { | |
|
vsm
2015/06/26 15:59:36
I'd be tempted to just make this unconditionally t
| |
| 97 // Determine whether we are in Dart checked mode the hard way. | |
| 98 try { | |
| 99 var i = 1; | |
| 100 String s = i; | |
| 101 return false; | |
| 102 } on TypeError catch(e) { | |
| 103 return true; | |
| 104 } | |
| 105 })(); | |
| 106 | |
| 107 | |
| 108 final _allowedMethods = new Map<Symbol, _DeclarationSet>(); | |
| 109 final _allowedGetters = new Map<Symbol, _DeclarationSet>(); | |
| 110 final _allowedSetters = new Map<Symbol, _DeclarationSet>(); | |
| 111 | |
| 112 final _jsInterfaceTypes = new Set<Type>(); | |
| 113 Iterable<Type> get jsInterfaceTypes => _jsInterfaceTypes; | |
| 114 | |
| 115 /// A collection of methods where all methods have the same name. | |
| 116 /// This class is intended to optimize whether a specific invocation is | |
| 117 /// appropritate for at least some of the methods in the collection. | |
| 118 class _DeclarationSet { | |
| 119 _DeclarationSet() : _members = <mirrors.DeclarationMirror>[]; | |
| 120 | |
| 121 static bool _checkType(obj, mirrors.TypeMirror type) { | |
| 122 if (obj == null) return true; | |
| 123 return mirrors.reflectType(obj.runtimeType).isSubtypeOf(type); | |
| 124 } | |
| 125 | |
| 126 /// Returns whether the return [value] has a type is consistent with the | |
| 127 /// return type from at least one of the members matching the DeclarationSet. | |
| 128 bool _checkReturnType(value) { | |
| 129 if (value == null) return true; | |
| 130 var valueMirror = mirrors.reflectType(value.runtimeType); | |
| 131 for (var member in _members) { | |
| 132 if (member is mirrors.VariableMirror || member.isGetter) { | |
| 133 // TODO(jacobr): actually check return types for getters that return | |
| 134 // function types. | |
| 135 return true; | |
| 136 } else { | |
| 137 if (valueMirror.isSubtypeOf(member.returnType)) return true; | |
| 138 } | |
| 139 } | |
| 140 return false; | |
| 141 } | |
| 142 | |
| 143 /** | |
| 144 * Check whether the [invocation] is consistent with the [member] mirror. | |
| 145 */ | |
| 146 bool _checkDeclaration( | |
| 147 Invocation invocation, mirrors.DeclarationMirror member) { | |
| 148 if (member is mirrors.VariableMirror || member.isGetter) { | |
| 149 // TODO(jacobr): actually check method types against the function type | |
| 150 // returned by the getter or field. | |
| 151 return true; | |
| 152 } | |
| 153 var parameters = member.parameters; | |
| 154 var positionalArguments = invocation.positionalArguments; | |
| 155 // Too many arguments | |
| 156 if (parameters.length < positionalArguments.length) return false; | |
| 157 // Too few required arguments. | |
| 158 if (parameters.length > positionalArguments.length && | |
| 159 !parameters[positionalArguments.length].isOptional) return false; | |
| 160 for (var i = 0; i < positionalArguments.length; i++) { | |
| 161 if (parameters[i].isNamed) { | |
| 162 // Not enough positional arguments. | |
| 163 return false; | |
| 164 } | |
| 165 if (!_checkType( | |
| 166 invocation.positionalArguments[i], parameters[i].type)) return false; | |
| 167 } | |
| 168 if (invocation.namedArguments.isNotEmpty) { | |
| 169 var startNamed; | |
| 170 for (startNamed = parameters.length - 1; startNamed >= 0; startNamed--) { | |
| 171 if (!parameters[startNamed].isNamed) break; | |
| 172 } | |
| 173 startNamed++; | |
| 174 | |
| 175 // TODO(jacobr): we are unneccessarily using an O(n^2) algorithm here. | |
| 176 // If we have JS APIs with a lange number of named parameters we should | |
| 177 // optimize this. Either use a HashSet or invert this, walking over | |
| 178 // parameters, querying invocation, and making sure we match | |
| 179 //invocation.namedArguments.size keys. | |
| 180 for (var name in invocation.namedArguments.keys) { | |
| 181 bool match = false; | |
| 182 for (var j = startNamed; j < parameters.length; j++) { | |
| 183 var p = parameters[j]; | |
| 184 if (p.simpleName == name) { | |
| 185 if (!_checkType(invocation.namedArguments[name], | |
| 186 parameters[j].type)) return false; | |
| 187 match = true; | |
| 188 break; | |
| 189 } | |
| 190 } | |
| 191 if (match == false) return false; | |
| 192 } | |
| 193 } | |
| 194 return true; | |
| 195 } | |
| 196 | |
| 197 bool checkInvocation(Invocation invocation) { | |
| 198 for (var member in _members) { | |
| 199 if (_checkDeclaration(invocation, member)) return true; | |
| 200 } | |
| 201 return false; | |
| 202 } | |
| 203 | |
| 204 void add(mirrors.DeclarationMirror mirror) { | |
| 205 _members.add(mirror); | |
| 206 } | |
| 207 | |
| 208 final List<mirrors.DeclarationMirror> _members; | |
| 209 } | |
| 210 | |
| 211 /** | |
| 212 * Temporary method that we hope to remove at some point. This method should | |
| 213 * generally only be called by machine generated code. | |
| 214 */ | |
| 215 void registerJsInterfaces(List<Type> classes) { | |
| 216 if (_finalized == true) { | |
| 217 throw 'JSInterop class registration already finalized'; | |
| 218 } | |
| 219 for (Type type in classes) { | |
| 220 if (!_jsInterfaceTypes.add(type)) continue; // Already registered. | |
| 221 mirrors.ClassMirror typeMirror = mirrors.reflectType(type); | |
| 222 typeMirror.declarations.forEach((symbol, declaration) { | |
| 223 if (declaration is mirrors.MethodMirror || | |
| 224 declaration is mirrors.VariableMirror && !declaration.isStatic) { | |
| 225 bool treatAsGetter = false; | |
| 226 bool treatAsSetter = false; | |
| 227 if (declaration is mirrors.VariableMirror) { | |
| 228 treatAsGetter = true; | |
| 229 if (!declaration.isConst && !declaration.isFinal) { | |
| 230 treatAsSetter = true; | |
| 231 } | |
| 232 } else { | |
| 233 if (declaration.isGetter) { | |
| 234 treatAsGetter = true; | |
| 235 } else if (declaration.isSetter) { | |
| 236 treatAsSetter = true; | |
| 237 } else if (!declaration.isConstructor) { | |
| 238 _allowedMethods | |
| 239 .putIfAbsent(symbol, () => new _DeclarationSet()) | |
| 240 .add(declaration); | |
| 241 } | |
| 242 } | |
| 243 if (treatAsGetter) { | |
| 244 _allowedGetters | |
| 245 .putIfAbsent(symbol, () => new _DeclarationSet()) | |
| 246 .add(declaration); | |
| 247 _allowedMethods | |
| 248 .putIfAbsent(symbol, () => new _DeclarationSet()) | |
| 249 .add(declaration); | |
| 250 } | |
| 251 if (treatAsSetter) { | |
| 252 _allowedSetters | |
| 253 .putIfAbsent(symbol, () => new _DeclarationSet()) | |
| 254 .add(declaration); | |
| 255 } | |
| 256 } | |
| 257 }); | |
| 258 } | |
| 259 } | |
| 260 | |
| 261 _finalizeJsInterfaces() native "Js_finalizeJsInterfaces"; | |
| 262 | |
| 263 /** | |
| 264 * Generates a part file defining source code for JsObjectImpl and related | |
| 265 * classes. This calass is needed so that type checks for all registered JavaScr ipt | |
| 266 * interop classes pass. | |
| 267 */ | |
| 268 String _generateJsObjectImplPart() { | |
| 269 Iterable<Type> types = jsInterfaceTypes; | |
| 270 var libraryPrefixes = new Map<mirrors.LibraryMirror, String>(); | |
| 271 var prefixNames = new Set<String>(); | |
| 272 var sb = new StringBuffer(); | |
| 273 | |
| 274 var implements = <String>[]; | |
| 275 for (var type in types) { | |
| 276 mirrors.ClassMirror typeMirror = mirrors.reflectType(type); | |
| 277 mirrors.LibraryMirror libraryMirror = typeMirror.owner; | |
| 278 var prefixName; | |
| 279 if (libraryPrefixes.containsKey(libraryMirror)) { | |
| 280 prefixName = libraryPrefixes[libraryMirror]; | |
| 281 } else { | |
| 282 var basePrefixName = | |
| 283 mirrors.MirrorSystem.getName(libraryMirror.simpleName); | |
| 284 basePrefixName = basePrefixName.replaceAll('.', '_'); | |
| 285 if (basePrefixName.isEmpty) basePrefixName = "lib"; | |
| 286 prefixName = basePrefixName; | |
| 287 var i = 1; | |
| 288 while (prefixNames.contains(prefixName)) { | |
| 289 prefixName = '$basePrefixName$i'; | |
| 290 i++; | |
| 291 } | |
| 292 prefixNames.add(prefixName); | |
| 293 libraryPrefixes[libraryMirror] = prefixName; | |
| 294 } | |
| 295 implements.add( | |
| 296 '${prefixName}.${mirrors.MirrorSystem.getName(typeMirror.simpleName)}'); | |
| 297 } | |
| 298 libraryPrefixes.forEach((libraryMirror, prefix) { | |
| 299 sb.writeln('import "${libraryMirror.uri}" as $prefix;'); | |
| 300 }); | |
| 301 var implementsClause = | |
| 302 implements.isEmpty ? "" : "implements ${implements.join(', ')}"; | |
| 303 // TODO(jacobr): only certain classes need to be implemented by | |
| 304 // Function and Array. | |
| 305 sb.write(''' | |
| 306 class JsObjectImpl extends JsObject $implementsClause { | |
| 307 JsObjectImpl.internal() : super.internal(); | |
| 308 } | |
| 309 | |
| 310 class JsFunctionImpl extends JsFunction $implementsClause { | |
| 311 JsFunctionImpl.internal() : super.internal(); | |
| 312 } | |
| 313 | |
| 314 class JsArrayImpl<E> extends JsArray<E> $implementsClause { | |
| 315 JsArrayImpl.internal() : super.internal(); | |
| 316 } | |
| 317 '''); | |
| 318 return sb.toString(); | |
| 319 } | |
| 320 | |
| 321 // Start of block of helper methods facilitating emulating JavaScript Array | |
| 322 // methods on Dart List objects passed to JavaScript via JS interop. | |
| 323 // TODO(jacobr): match JS more closely. | |
| 324 String _toStringJs(obj) => '$obj'; | |
| 325 | |
| 326 // TODO(jacobr): this might not exactly match JS semantics but should be | |
| 327 // adequate for now. | |
| 328 int _toIntJs(obj) { | |
| 329 if (obj is int) return obj; | |
| 330 if (obj is num) return obj.toInt(); | |
| 331 return num.parse('$obj'.trim(), (_) => 0).toInt(); | |
| 332 } | |
| 333 | |
| 334 // TODO(jacobr): this might not exactly match JS semantics but should be | |
| 335 // adequate for now. | |
| 336 num _toNumJs(obj) { | |
| 337 return obj is num ? obj : num.parse('$obj'.trim(), (_) => 0); | |
| 338 } | |
| 339 | |
| 340 /// Match the behavior of setting List length in JavaScript with the exception | |
| 341 /// that Dart does not distinguish undefined and null. | |
| 342 _setListLength(List list, rawlen) { | |
| 343 num len = _toNumJs(rawlen); | |
| 344 if (len is! int || len < 0) { | |
| 345 throw new RangeError("Invalid array length"); | |
| 346 } | |
| 347 if (len > list.length) { | |
| 348 _arrayExtend(list, len); | |
| 349 } else if (len < list.length) { | |
| 350 list.removeRange(len, list.length); | |
| 351 } | |
| 352 return rawlen; | |
| 353 } | |
| 354 | |
| 355 // TODO(jacobr): should we really bother with this method instead of just | |
| 356 // shallow copying to a JS array and calling the JavaScript join method? | |
| 357 String _arrayJoin(List list, sep) { | |
| 358 if (sep == null) { | |
| 359 sep = ","; | |
| 360 } | |
| 361 return list.map((e) => e == null ? "" : e.toString()).join(sep.toString()); | |
| 362 } | |
| 363 | |
| 364 // TODO(jacobr): should we really bother with this method instead of just | |
| 365 // shallow copying to a JS array and using the toString method? | |
| 366 String _arrayToString(List list) => _arrayJoin(list, ","); | |
| 367 | |
| 368 int _arrayPush(List list, e) { | |
| 369 list.add(e); | |
| 370 return list.length; | |
| 371 } | |
| 372 | |
| 373 _arrayPop(List list) { | |
| 374 if (list.length > 0) return list.removeLast(); | |
| 375 } | |
| 376 | |
| 377 // TODO(jacobr): would it be better to just copy input to a JS List | |
| 378 // and call Array.concat? | |
| 379 List _arrayConcat(List input, List args) { | |
| 380 var ret = new List.from(input); | |
| 381 for (var e in args) { | |
| 382 // TODO(jacobr): technically in ES6 we should use | |
| 383 // Symbol.isConcatSpreadable to determine whether call addAll. Once v8 | |
| 384 // supports it, we can make all Dart classes implementing Iterable | |
| 385 // specify isConcatSpreadable and tweak this behavior to allow Iterable. | |
| 386 if (e is List) { | |
| 387 ret.addAll(e); | |
| 388 } else { | |
| 389 ret.add(e); | |
| 390 } | |
| 391 } | |
| 392 return ret; | |
| 393 } | |
| 394 | |
| 395 List _arraySplice(List input, List args) { | |
| 396 int start = 0; | |
| 397 if (args.length > 0) { | |
| 398 var rawStart = _toIntJs(args[0]); | |
| 399 if (rawStart < 0) { | |
| 400 start = math.max(0, input.length - rawStart); | |
| 401 } else { | |
| 402 start = math.min(input.length, rawStart); | |
| 403 } | |
| 404 } | |
| 405 var end = start; | |
| 406 if (args.length > 1) { | |
| 407 var rawDeleteCount = _toIntJs(args[1]); | |
| 408 if (rawDeleteCount < 0) rawDeleteCount = 0; | |
| 409 end = math.min(input.length, start + rawDeleteCount); | |
| 410 } | |
| 411 var replacement = []; | |
| 412 var removedElements = input.getRange(start, end).toList(); | |
| 413 if (args.length > 2) { | |
| 414 replacement = args.getRange(2, args.length); | |
| 415 } | |
| 416 input.replaceRange(start, end, replacement); | |
| 417 return removedElements; | |
| 418 } | |
| 419 | |
| 420 List _arrayReverse(List l) { | |
| 421 for (var i = 0, j = l.length - 1; i < j; i++, j--) { | |
| 422 var tmp = l[i]; | |
| 423 l[i] = l[j]; | |
| 424 l[j] = tmp; | |
| 425 } | |
| 426 return l; | |
| 427 } | |
| 428 | |
| 429 _arrayShift(List l) { | |
| 430 if (l.isEmpty) return null; // Technically we should return undefined. | |
| 431 return l.removeAt(0); | |
| 432 } | |
| 433 | |
| 434 int _arrayUnshift(List l, List args) { | |
| 435 l.insertAll(0, args); | |
| 436 return l.length; | |
| 437 } | |
| 438 | |
| 439 _arrayExtend(List l, int newLength) { | |
| 440 for (var i = l.length; i < newLength; i++) { | |
| 441 // TODO(jacobr): we'd really like to add undefined to better match | |
| 442 // JavaScript semantics. | |
| 443 l.add(null); | |
| 444 } | |
| 445 } | |
| 446 | |
| 447 List _arraySort(List l, rawCompare) { | |
| 448 // TODO(jacobr): alternately we could just copy the Array to JavaScript, | |
| 449 // invoke the JS sort method and then copy the result back to Dart. | |
| 450 Comparator compare; | |
| 451 if (rawCompare == null) { | |
| 452 compare = (a, b) => _toStringJs(a).compareTo(_toStringJs(b)); | |
| 453 } else if (rawCompare is JsFunction) { | |
| 454 compare = (a, b) => rawCompare.apply([a, b]); | |
| 455 } else { | |
| 456 compare = rawCompare; | |
| 457 } | |
| 458 l.sort(compare); | |
| 459 return l; | |
| 460 } | |
| 461 // End of block of helper methods to emulate JavaScript Array methods on Dart Li st. | |
| 462 | |
| 463 /** | |
| 464 * Can be called to provide a predictable point where no more JS interfaces can | |
| 465 * be added. Creating an instance of JsObject will also automatically trigger | |
| 466 * all JsObjects to be finalized. | |
| 467 */ | |
| 468 void finalizeJsInterfaces() { | |
| 469 if (_finalized == true) { | |
| 470 throw 'JSInterop class registration already finalized'; | |
| 471 } | |
| 472 _finalizeJsInterfaces(); | |
| 473 } | |
| 92 | 474 |
| 93 JsObject _cachedContext; | 475 JsObject _cachedContext; |
| 94 | 476 |
| 95 JsObject get _context native "Js_context_Callback"; | 477 JsObject get _context native "Js_context_Callback"; |
| 96 | 478 |
| 479 bool get _finalized native "Js_interfacesFinalized_Callback"; | |
| 480 | |
| 97 JsObject get context { | 481 JsObject get context { |
| 98 if (_cachedContext == null) { | 482 if (_cachedContext == null) { |
| 99 _cachedContext = _context; | 483 _cachedContext = _context; |
| 100 } | 484 } |
| 101 return _cachedContext; | 485 return _cachedContext; |
| 102 } | 486 } |
| 103 | 487 |
| 104 /** | 488 /** |
| 105 * Proxies a JavaScript object to Dart. | 489 * Proxies a JavaScript object to Dart. |
| 106 * | 490 * |
| 107 * The properties of the JavaScript object are accessible via the `[]` and | 491 * The properties of the JavaScript object are accessible via the `[]` and |
| 108 * `[]=` operators. Methods are callable via [callMethod]. | 492 * `[]=` operators. Methods are callable via [callMethod]. |
| 109 */ | 493 */ |
| 110 class JsObject extends NativeFieldWrapperClass2 { | 494 class JsObject extends NativeFieldWrapperClass2 { |
| 111 JsObject.internal(); | 495 JsObject.internal(); |
| 112 | 496 |
| 113 /** | 497 /** |
| 114 * Constructs a new JavaScript object from [constructor] and returns a proxy | 498 * Constructs a new JavaScript object from [constructor] and returns a proxy |
| 115 * to it. | 499 * to it. |
| 116 */ | 500 */ |
| 117 factory JsObject(JsFunction constructor, [List arguments]) => _create(construc tor, arguments); | 501 factory JsObject(JsFunction constructor, [List arguments]) => |
| 502 _create(constructor, arguments); | |
| 118 | 503 |
| 119 static JsObject _create(JsFunction constructor, arguments) native "JsObject_co nstructorCallback"; | 504 static JsObject _create( |
| 505 JsFunction constructor, arguments) native "JsObject_constructorCallback"; | |
| 506 | |
| 507 _buildArgs(Invocation invocation) { | |
| 508 if (invocation.namedArguments.isEmpty) { | |
| 509 return invocation.positionalArguments; | |
| 510 } else { | |
| 511 var varArgs = new Map<String, Object>(); | |
| 512 invocation.namedArguments.forEach((symbol, val) { | |
| 513 varArgs[mirrors.MirrorSystem.getName(symbol)] = val; | |
| 514 }); | |
| 515 return invocation.positionalArguments.toList() | |
| 516 ..add(new JsObject.jsify(varArgs)); | |
| 517 } | |
| 518 } | |
| 120 | 519 |
| 121 /** | 520 /** |
| 122 * Constructs a [JsObject] that proxies a native Dart object; _for expert use | 521 * Constructs a [JsObject] that proxies a native Dart object; _for expert use |
| 123 * only_. | 522 * only_. |
| 124 * | 523 * |
| 125 * Use this constructor only if you wish to get access to JavaScript | 524 * Use this constructor only if you wish to get access to JavaScript |
| 126 * properties attached to a browser host object, such as a Node or Blob, that | 525 * properties attached to a browser host object, such as a Node or Blob, that |
| 127 * is normally automatically converted into a native Dart object. | 526 * is normally automatically converted into a native Dart object. |
| 128 * | 527 * |
| 129 * An exception will be thrown if [object] either is `null` or has the type | 528 * An exception will be thrown if [object] either is `null` or has the type |
| 130 * `bool`, `num`, or `String`. | 529 * `bool`, `num`, or `String`. |
| 131 */ | 530 */ |
| 132 factory JsObject.fromBrowserObject(object) { | 531 factory JsObject.fromBrowserObject(object) { |
| 133 if (object is num || object is String || object is bool || object == null) { | 532 if (object is num || object is String || object is bool || object == null) { |
| 134 throw new ArgumentError( | 533 throw new ArgumentError("object cannot be a num, string, bool, or null"); |
| 135 "object cannot be a num, string, bool, or null"); | |
| 136 } | 534 } |
| 137 return _fromBrowserObject(object); | 535 return _fromBrowserObject(object); |
| 138 } | 536 } |
| 139 | 537 |
| 140 /** | 538 /** |
| 141 * Recursively converts a JSON-like collection of Dart objects to a | 539 * Recursively converts a JSON-like collection of Dart objects to a |
| 142 * collection of JavaScript objects and returns a [JsObject] proxy to it. | 540 * collection of JavaScript objects and returns a [JsObject] proxy to it. |
| 143 * | 541 * |
| 144 * [object] must be a [Map] or [Iterable], the contents of which are also | 542 * [object] must be a [Map] or [Iterable], the contents of which are also |
| 145 * converted. Maps and Iterables are copied to a new JavaScript object. | 543 * converted. Maps and Iterables are copied to a new JavaScript object. |
| 146 * Primitives and other transferrable values are directly converted to their | 544 * Primitives and other transferrable values are directly converted to their |
| 147 * JavaScript type, and all other objects are proxied. | 545 * JavaScript type, and all other objects are proxied. |
| 148 */ | 546 */ |
| 149 factory JsObject.jsify(object) { | 547 factory JsObject.jsify(object) { |
| 150 if ((object is! Map) && (object is! Iterable)) { | 548 if ((object is! Map) && (object is! Iterable)) { |
| 151 throw new ArgumentError("object must be a Map or Iterable"); | 549 throw new ArgumentError("object must be a Map or Iterable"); |
| 152 } | 550 } |
| 153 return _jsify(object); | 551 return _jsify(object); |
| 154 } | 552 } |
| 155 | 553 |
| 156 static JsObject _jsify(object) native "JsObject_jsify"; | 554 static JsObject _jsify(object) native "JsObject_jsify"; |
| 157 | 555 |
| 158 static JsObject _fromBrowserObject(object) native "JsObject_fromBrowserObject" ; | 556 static JsObject _fromBrowserObject( |
| 557 object) native "JsObject_fromBrowserObject"; | |
| 159 | 558 |
| 160 /** | 559 /** |
| 161 * Returns the value associated with [property] from the proxied JavaScript | 560 * Returns the value associated with [property] from the proxied JavaScript |
| 162 * object. | 561 * object. |
| 163 * | 562 * |
| 164 * The type of [property] must be either [String] or [num]. | 563 * The type of [property] must be either [String] or [num]. |
| 165 */ | 564 */ |
| 166 operator[](property) native "JsObject_[]"; | 565 operator [](property) native "JsObject_[]"; |
| 167 | 566 |
| 168 /** | 567 /** |
| 169 * Sets the value associated with [property] on the proxied JavaScript | 568 * Sets the value associated with [property] on the proxied JavaScript |
| 170 * object. | 569 * object. |
| 171 * | 570 * |
| 172 * The type of [property] must be either [String] or [num]. | 571 * The type of [property] must be either [String] or [num]. |
| 173 */ | 572 */ |
| 174 operator[]=(property, value) native "JsObject_[]="; | 573 operator []=(property, value) native "JsObject_[]="; |
| 175 | 574 |
| 176 int get hashCode native "JsObject_hashCode"; | 575 int get hashCode native "JsObject_hashCode"; |
| 177 | 576 |
| 178 operator==(other) => other is JsObject && _identityEquality(this, other); | 577 operator ==(other) => other is JsObject && _identityEquality(this, other); |
| 179 | 578 |
| 180 static bool _identityEquality(JsObject a, JsObject b) native "JsObject_identit yEquality"; | 579 static bool _identityEquality( |
| 580 JsObject a, JsObject b) native "JsObject_identityEquality"; | |
| 181 | 581 |
| 182 /** | 582 /** |
| 183 * Returns `true` if the JavaScript object contains the specified property | 583 * Returns `true` if the JavaScript object contains the specified property |
| 184 * either directly or though its prototype chain. | 584 * either directly or though its prototype chain. |
| 185 * | 585 * |
| 186 * This is the equivalent of the `in` operator in JavaScript. | 586 * This is the equivalent of the `in` operator in JavaScript. |
| 187 */ | 587 */ |
| 188 bool hasProperty(String property) native "JsObject_hasProperty"; | 588 bool hasProperty(String property) native "JsObject_hasProperty"; |
| 189 | 589 |
| 190 /** | 590 /** |
| 191 * Removes [property] from the JavaScript object. | 591 * Removes [property] from the JavaScript object. |
| 192 * | 592 * |
| 193 * This is the equivalent of the `delete` operator in JavaScript. | 593 * This is the equivalent of the `delete` operator in JavaScript. |
| 194 */ | 594 */ |
| 195 void deleteProperty(String property) native "JsObject_deleteProperty"; | 595 void deleteProperty(String property) native "JsObject_deleteProperty"; |
| 196 | 596 |
| 197 /** | 597 /** |
| 198 * Returns `true` if the JavaScript object has [type] in its prototype chain. | 598 * Returns `true` if the JavaScript object has [type] in its prototype chain. |
| 199 * | 599 * |
| 200 * This is the equivalent of the `instanceof` operator in JavaScript. | 600 * This is the equivalent of the `instanceof` operator in JavaScript. |
| 201 */ | 601 */ |
| 202 bool instanceof(JsFunction type) native "JsObject_instanceof"; | 602 bool instanceof(JsFunction type) native "JsObject_instanceof"; |
| 203 | 603 |
| 204 /** | 604 /** |
| 205 * Returns the result of the JavaScript objects `toString` method. | 605 * Returns the result of the JavaScript objects `toString` method. |
| 206 */ | 606 */ |
| 207 String toString() { | 607 String toString() { |
| 208 try { | 608 try { |
| 209 return _toString(); | 609 return _toString(); |
| 210 } catch(e) { | 610 } catch (e) { |
| 211 return super.toString(); | 611 return super.toString(); |
| 212 } | 612 } |
| 213 } | 613 } |
| 214 | 614 |
| 215 String _toString() native "JsObject_toString"; | 615 String _toString() native "JsObject_toString"; |
| 216 | 616 |
| 217 /** | 617 /** |
| 218 * Calls [method] on the JavaScript object with the arguments [args] and | 618 * Calls [method] on the JavaScript object with the arguments [args] and |
| 219 * returns the result. | 619 * returns the result. |
| 220 * | 620 * |
| 221 * The type of [method] must be either [String] or [num]. | 621 * The type of [method] must be either [String] or [num]. |
| 222 */ | 622 */ |
| 223 callMethod(String method, [List args]) { | 623 callMethod(String method, [List args]) { |
| 224 try { | 624 try { |
| 225 return _callMethod(method, args); | 625 return _callMethod(method, args); |
| 226 } catch(e) { | 626 } catch (e) { |
| 227 if (hasProperty(method)) { | 627 if (hasProperty(method)) { |
| 228 rethrow; | 628 rethrow; |
| 229 } else { | 629 } else { |
| 230 throw new NoSuchMethodError(this, new Symbol(method), args, null); | 630 throw new NoSuchMethodError(this, new Symbol(method), args, null); |
| 231 } | 631 } |
| 232 } | 632 } |
| 233 } | 633 } |
| 234 | 634 |
| 635 noSuchMethod(Invocation invocation) { | |
| 636 throwError() { | |
| 637 throw new NoSuchMethodError(this, invocation.memberName, | |
| 638 invocation.positionalArguments, invocation.namedArguments); | |
| 639 } | |
| 640 | |
| 641 String name = mirrors.MirrorSystem.getName(invocation.memberName); | |
| 642 if (invocation.isGetter) { | |
| 643 if (_checkJsInvocations) { | |
| 644 var matches = _allowedGetters[invocation.memberName]; | |
| 645 if (matches == null && | |
| 646 !_allowedMethods.containsKey(invocation.memberName)) { | |
| 647 throwError(); | |
| 648 } | |
| 649 var ret = this[name]; | |
| 650 if (matches != null && matches._checkReturnType(ret)) return ret; | |
| 651 if (ret is Function || | |
| 652 (ret is JsFunction /* shouldn't be needed in the future*/) && | |
| 653 _allowedMethods.containsKey( | |
| 654 invocation.memberName)) return ret; // Warning: we have not bound "this"... we could type check on the Function but that is of little value in Dart. | |
| 655 throwError(); | |
| 656 } else { | |
| 657 // TODO(jacobr): should we throw if the JavaScript object doesn't have t he property? | |
| 658 return this[name]; | |
| 659 } | |
| 660 } else if (invocation.isSetter) { | |
| 661 if (_checkJsInvocations) { | |
| 662 var matches = _allowedSetters[invocation.memberName]; | |
| 663 if (matches == null || | |
| 664 !matches.checkInvocation(invocation)) throwError(); | |
| 665 } | |
| 666 assert(name.endsWith("=")); | |
| 667 name = name.substring(0, name.length - 1); | |
| 668 return this[name] = invocation.positionalArguments.first; | |
| 669 } else { | |
| 670 // TODO(jacobr): also allow calling getters that look like functions. | |
| 671 var matches; | |
| 672 if (_checkJsInvocations) { | |
| 673 matches = _allowedMethods[invocation.memberName]; | |
| 674 if (matches == null || | |
| 675 !matches.checkInvocation(invocation)) throwError(); | |
| 676 } | |
| 677 var ret = this.callMethod(name, _buildArgs(invocation)); | |
| 678 if (_checkJsInvocations) { | |
| 679 if (!matches._checkReturnType(ret)) throwError(); | |
| 680 } | |
| 681 return ret; | |
| 682 } | |
| 683 } | |
| 684 | |
| 235 _callMethod(String name, List args) native "JsObject_callMethod"; | 685 _callMethod(String name, List args) native "JsObject_callMethod"; |
| 236 } | 686 } |
| 237 | 687 |
| 238 /** | 688 /** |
| 239 * Proxies a JavaScript Function object. | 689 * Proxies a JavaScript Function object. |
| 240 */ | 690 */ |
| 241 class JsFunction extends JsObject { | 691 class JsFunction extends JsObject implements Function { |
| 242 JsFunction.internal() : super.internal(); | 692 JsFunction.internal() : super.internal(); |
| 243 | 693 |
| 244 /** | 694 /** |
| 245 * Returns a [JsFunction] that captures its 'this' binding and calls [f] | 695 * Returns a [JsFunction] that captures its 'this' binding and calls [f] |
| 246 * with the value of this passed as the first argument. | 696 * with the value of this passed as the first argument. |
| 247 */ | 697 */ |
| 248 factory JsFunction.withThis(Function f) => _withThis(f); | 698 factory JsFunction.withThis(Function f) => _withThis(f); |
| 249 | 699 |
| 250 /** | 700 /** |
| 251 * Invokes the JavaScript function with arguments [args]. If [thisArg] is | 701 * Invokes the JavaScript function with arguments [args]. If [thisArg] is |
| 252 * supplied it is the value of `this` for the invocation. | 702 * supplied it is the value of `this` for the invocation. |
| 253 */ | 703 */ |
| 254 dynamic apply(List args, {thisArg}) native "JsFunction_apply"; | 704 dynamic apply(List args, {thisArg}) native "JsFunction_apply"; |
| 255 | 705 |
| 706 noSuchMethod(Invocation invocation) { | |
| 707 if (invocation.isMethod && invocation.memberName == #call) { | |
| 708 return apply(_buildArgs(invocation)); | |
| 709 } | |
| 710 return super.noSuchMethod(invocation); | |
| 711 } | |
| 712 | |
| 256 /** | 713 /** |
| 257 * Internal only version of apply which uses debugger proxies of Dart objects | 714 * Internal only version of apply which uses debugger proxies of Dart objects |
| 258 * rather than opaque handles. This method is private because it cannot be | 715 * rather than opaque handles. This method is private because it cannot be |
| 259 * efficiently implemented in Dart2Js so should only be used by internal | 716 * efficiently implemented in Dart2Js so should only be used by internal |
| 260 * tools. | 717 * tools. |
| 261 */ | 718 */ |
| 262 _applyDebuggerOnly(List args, {thisArg}) native "JsFunction_applyDebuggerOnly" ; | 719 _applyDebuggerOnly(List args, |
| 720 {thisArg}) native "JsFunction_applyDebuggerOnly"; | |
| 263 | 721 |
| 264 static JsFunction _withThis(Function f) native "JsFunction_withThis"; | 722 static JsFunction _withThis(Function f) native "JsFunction_withThis"; |
| 265 } | 723 } |
| 266 | 724 |
| 267 /** | 725 /** |
| 268 * A [List] proxying a JavaScript Array. | 726 * A [List] proxying a JavaScript Array. |
| 269 */ | 727 */ |
| 270 class JsArray<E> extends JsObject with ListMixin<E> { | 728 class JsArray<E> extends JsObject with ListMixin<E> { |
| 729 JsArray.internal() : super.internal(); | |
| 271 | 730 |
| 272 factory JsArray() => _newJsArray(); | 731 factory JsArray() => _newJsArray(); |
| 273 | 732 |
| 274 static JsArray _newJsArray() native "JsArray_newJsArray"; | 733 static JsArray _newJsArray() native "JsArray_newJsArray"; |
| 275 | 734 |
| 276 factory JsArray.from(Iterable<E> other) => _newJsArrayFromSafeList(new List.fr om(other)); | 735 factory JsArray.from(Iterable<E> other) => |
| 736 _newJsArrayFromSafeList(new List.from(other)); | |
| 277 | 737 |
| 278 static JsArray _newJsArrayFromSafeList(List list) native "JsArray_newJsArrayFr omSafeList"; | 738 static JsArray _newJsArrayFromSafeList( |
| 739 List list) native "JsArray_newJsArrayFromSafeList"; | |
| 279 | 740 |
| 280 _checkIndex(int index, {bool insert: false}) { | 741 _checkIndex(int index, {bool insert: false}) { |
| 281 int length = insert ? this.length + 1 : this.length; | 742 int length = insert ? this.length + 1 : this.length; |
| 282 if (index is int && (index < 0 || index >= length)) { | 743 if (index is int && (index < 0 || index >= length)) { |
| 283 throw new RangeError.range(index, 0, length); | 744 throw new RangeError.range(index, 0, length); |
| 284 } | 745 } |
| 285 } | 746 } |
| 286 | 747 |
| 287 _checkRange(int start, int end) { | 748 _checkRange(int start, int end) { |
| 288 int cachedLength = this.length; | 749 int cachedLength = this.length; |
| 289 if (start < 0 || start > cachedLength) { | 750 if (start < 0 || start > cachedLength) { |
| 290 throw new RangeError.range(start, 0, cachedLength); | 751 throw new RangeError.range(start, 0, cachedLength); |
| 291 } | 752 } |
| 292 if (end < start || end > cachedLength) { | 753 if (end < start || end > cachedLength) { |
| 293 throw new RangeError.range(end, start, cachedLength); | 754 throw new RangeError.range(end, start, cachedLength); |
| 294 } | 755 } |
| 295 } | 756 } |
| 296 | 757 |
| 297 // Methods required by ListMixin | 758 // Methods required by ListMixin |
| 298 | 759 |
| 299 E operator [](index) { | 760 E operator [](index) { |
| 300 if (index is int) { | 761 if (index is int) { |
| 301 _checkIndex(index); | 762 _checkIndex(index); |
| 302 } | 763 } |
| 303 return super[index]; | 764 return super[index]; |
| 304 } | 765 } |
| 305 | 766 |
| 306 void operator []=(index, E value) { | 767 void operator []=(index, E value) { |
| 307 if(index is int) { | 768 if (index is int) { |
| 308 _checkIndex(index); | 769 _checkIndex(index); |
| 309 } | 770 } |
| 310 super[index] = value; | 771 super[index] = value; |
| 311 } | 772 } |
| 312 | 773 |
| 313 int get length native "JsArray_length"; | 774 int get length native "JsArray_length"; |
| 314 | 775 |
| 315 void set length(int length) { super['length'] = length; } | 776 void set length(int length) { |
| 777 super['length'] = length; | |
| 778 } | |
| 316 | 779 |
| 317 // Methods overriden for better performance | 780 // Methods overriden for better performance |
| 318 | 781 |
| 319 void add(E value) { | 782 void add(E value) { |
| 320 callMethod('push', [value]); | 783 callMethod('push', [value]); |
| 321 } | 784 } |
| 322 | 785 |
| 323 void addAll(Iterable<E> iterable) { | 786 void addAll(Iterable<E> iterable) { |
| 324 // TODO(jacobr): this can be optimized slightly. | 787 // TODO(jacobr): this can be optimized slightly. |
| 325 callMethod('push', new List.from(iterable)); | 788 callMethod('push', new List.from(iterable)); |
| 326 } | 789 } |
| 327 | 790 |
| 328 void insert(int index, E element) { | 791 void insert(int index, E element) { |
| 329 _checkIndex(index, insert:true); | 792 _checkIndex(index, insert: true); |
| 330 callMethod('splice', [index, 0, element]); | 793 callMethod('splice', [index, 0, element]); |
| 331 } | 794 } |
| 332 | 795 |
| 333 E removeAt(int index) { | 796 E removeAt(int index) { |
| 334 _checkIndex(index); | 797 _checkIndex(index); |
| 335 return callMethod('splice', [index, 1])[0]; | 798 return callMethod('splice', [index, 1])[0]; |
| 336 } | 799 } |
| 337 | 800 |
| 338 E removeLast() { | 801 E removeLast() { |
| 339 if (length == 0) throw new RangeError(-1); | 802 if (length == 0) throw new RangeError(-1); |
| (...skipping 18 matching lines...) Expand all Loading... | |
| 358 callMethod('sort', [compare]); | 821 callMethod('sort', [compare]); |
| 359 } | 822 } |
| 360 } | 823 } |
| 361 | 824 |
| 362 /** | 825 /** |
| 363 * Placeholder object for cases where we need to determine exactly how many | 826 * Placeholder object for cases where we need to determine exactly how many |
| 364 * args were passed to a function. | 827 * args were passed to a function. |
| 365 */ | 828 */ |
| 366 const _UNDEFINED = const Object(); | 829 const _UNDEFINED = const Object(); |
| 367 | 830 |
| 368 // FIXME(jacobr): this method is a hack to work around the lack of proper dart | 831 // TODO(jacobr): this method is a hack to work around the lack of proper dart |
| 369 // support for varargs methods. | 832 // support for varargs methods. |
| 370 List _stripUndefinedArgs(List args) => | 833 List _stripUndefinedArgs(List args) => |
| 371 args.takeWhile((i) => i != _UNDEFINED).toList(); | 834 args.takeWhile((i) => i != _UNDEFINED).toList(); |
| 372 | 835 |
| 373 /** | 836 /** |
| 374 * Returns a method that can be called with an arbitrary number (for n less | 837 * Returns a method that can be called with an arbitrary number (for n less |
| 375 * than 11) of arguments without violating Dart type checks. | 838 * than 11) of arguments without violating Dart type checks. |
| 376 */ | 839 */ |
| 377 Function _wrapAsDebuggerVarArgsFunction(JsFunction jsFunction) => | 840 Function _wrapAsDebuggerVarArgsFunction(JsFunction jsFunction) => |
| 378 ([a1=_UNDEFINED, a2=_UNDEFINED, a3=_UNDEFINED, a4=_UNDEFINED, | 841 ([a1 = _UNDEFINED, a2 = _UNDEFINED, a3 = _UNDEFINED, a4 = _UNDEFINED, |
| 379 a5=_UNDEFINED, a6=_UNDEFINED, a7=_UNDEFINED, a8=_UNDEFINED, | 842 a5 = _UNDEFINED, a6 = _UNDEFINED, a7 = _UNDEFINED, a8 = _UNDEFINED, |
| 380 a9=_UNDEFINED, a10=_UNDEFINED]) => | 843 a9 = _UNDEFINED, a10 = _UNDEFINED]) => jsFunction._applyDebuggerOnly( |
| 381 jsFunction._applyDebuggerOnly(_stripUndefinedArgs( | 844 _stripUndefinedArgs([a1, a2, a3, a4, a5, a6, a7, a8, a9, a10])); |
| 382 [a1,a2,a3,a4,a5,a6,a7,a8,a9,a10])); | |
| OLD | NEW |