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 120 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
131 } | 131 } |
132 | 132 |
133 final _allowedMethods = new Map<Symbol, _DeclarationSet>(); | 133 final _allowedMethods = new Map<Symbol, _DeclarationSet>(); |
134 final _allowedGetters = new Map<Symbol, _DeclarationSet>(); | 134 final _allowedGetters = new Map<Symbol, _DeclarationSet>(); |
135 final _allowedSetters = new Map<Symbol, _DeclarationSet>(); | 135 final _allowedSetters = new Map<Symbol, _DeclarationSet>(); |
136 | 136 |
137 final _jsInterfaceTypes = new Set<mirrors.ClassMirror>(); | 137 final _jsInterfaceTypes = new Set<mirrors.ClassMirror>(); |
138 @Deprecated("Internal Use Only") | 138 @Deprecated("Internal Use Only") |
139 Iterable<mirrors.ClassMirror> get jsInterfaceTypes => _jsInterfaceTypes; | 139 Iterable<mirrors.ClassMirror> get jsInterfaceTypes => _jsInterfaceTypes; |
140 | 140 |
| 141 class _StringLiteralEscape { |
| 142 // Character code constants. |
| 143 static const int BACKSPACE = 0x08; |
| 144 static const int TAB = 0x09; |
| 145 static const int NEWLINE = 0x0a; |
| 146 static const int CARRIAGE_RETURN = 0x0d; |
| 147 static const int FORM_FEED = 0x0c; |
| 148 static const int QUOTE = 0x22; |
| 149 static const int CHAR_$ = 0x24; |
| 150 static const int CHAR_0 = 0x30; |
| 151 static const int BACKSLASH = 0x5c; |
| 152 static const int CHAR_b = 0x62; |
| 153 static const int CHAR_f = 0x66; |
| 154 static const int CHAR_n = 0x6e; |
| 155 static const int CHAR_r = 0x72; |
| 156 static const int CHAR_t = 0x74; |
| 157 static const int CHAR_u = 0x75; |
| 158 |
| 159 final StringSink _sink; |
| 160 |
| 161 _StringLiteralEscape(this._sink); |
| 162 |
| 163 void writeString(String string) { |
| 164 _sink.write(string); |
| 165 } |
| 166 |
| 167 void writeStringSlice(String string, int start, int end) { |
| 168 _sink.write(string.substring(start, end)); |
| 169 } |
| 170 |
| 171 void writeCharCode(int charCode) { |
| 172 _sink.writeCharCode(charCode); |
| 173 } |
| 174 |
| 175 /// ('0' + x) or ('a' + x - 10) |
| 176 static int hexDigit(int x) => x < 10 ? 48 + x : 87 + x; |
| 177 |
| 178 /// Write, and suitably escape, a string's content as a JSON string literal. |
| 179 void writeStringContent(String s) { |
| 180 // Identical to JSON string literal escaping except that we also escape $. |
| 181 int offset = 0; |
| 182 final int length = s.length; |
| 183 for (int i = 0; i < length; i++) { |
| 184 int charCode = s.codeUnitAt(i); |
| 185 if (charCode > BACKSLASH) continue; |
| 186 if (charCode < 32) { |
| 187 if (i > offset) writeStringSlice(s, offset, i); |
| 188 offset = i + 1; |
| 189 writeCharCode(BACKSLASH); |
| 190 switch (charCode) { |
| 191 case BACKSPACE: |
| 192 writeCharCode(CHAR_b); |
| 193 break; |
| 194 case TAB: |
| 195 writeCharCode(CHAR_t); |
| 196 break; |
| 197 case NEWLINE: |
| 198 writeCharCode(CHAR_n); |
| 199 break; |
| 200 case FORM_FEED: |
| 201 writeCharCode(CHAR_f); |
| 202 break; |
| 203 case CARRIAGE_RETURN: |
| 204 writeCharCode(CHAR_r); |
| 205 break; |
| 206 default: |
| 207 writeCharCode(CHAR_u); |
| 208 writeCharCode(CHAR_0); |
| 209 writeCharCode(CHAR_0); |
| 210 writeCharCode(hexDigit((charCode >> 4) & 0xf)); |
| 211 writeCharCode(hexDigit(charCode & 0xf)); |
| 212 break; |
| 213 } |
| 214 } else if (charCode == QUOTE || |
| 215 charCode == BACKSLASH || |
| 216 charCode == CHAR_$) { |
| 217 if (i > offset) writeStringSlice(s, offset, i); |
| 218 offset = i + 1; |
| 219 writeCharCode(BACKSLASH); |
| 220 writeCharCode(charCode); |
| 221 } |
| 222 } |
| 223 if (offset == 0) { |
| 224 writeString(s); |
| 225 } else if (offset < length) { |
| 226 writeStringSlice(s, offset, length); |
| 227 } |
| 228 } |
| 229 |
| 230 /** |
| 231 * Serialize a [num], [String], [bool], [Null], [List] or [Map] value. |
| 232 * |
| 233 * Returns true if the value is one of these types, and false if not. |
| 234 * If a value is both a [List] and a [Map], it's serialized as a [List]. |
| 235 */ |
| 236 bool writeStringLiteral(String str) { |
| 237 writeString('"'); |
| 238 writeStringContent(str); |
| 239 writeString('"'); |
| 240 } |
| 241 } |
| 242 |
| 243 String _escapeString(String str) { |
| 244 StringBuffer output = new StringBuffer(); |
| 245 new _StringLiteralEscape(output)..writeStringLiteral(str); |
| 246 return output.toString(); |
| 247 } |
| 248 |
141 /// A collection of methods where all methods have the same name. | 249 /// A collection of methods where all methods have the same name. |
142 /// This class is intended to optimize whether a specific invocation is | 250 /// This class is intended to optimize whether a specific invocation is |
143 /// appropritate for at least some of the methods in the collection. | 251 /// appropritate for at least some of the methods in the collection. |
144 class _DeclarationSet { | 252 class _DeclarationSet { |
145 _DeclarationSet() : _members = <mirrors.DeclarationMirror>[]; | 253 _DeclarationSet() : _members = <mirrors.DeclarationMirror>[]; |
146 | 254 |
147 static bool _checkType(obj, mirrors.TypeMirror type) { | 255 static bool _checkType(obj, mirrors.TypeMirror type) { |
148 if (obj == null) return true; | 256 if (obj == null) return true; |
149 return mirrors.reflectType(obj.runtimeType).isSubtypeOf(type); | 257 return mirrors.reflectType(obj.runtimeType).isSubtypeOf(type); |
150 } | 258 } |
(...skipping 30 matching lines...) Expand all Loading... |
181 // Too many arguments | 289 // Too many arguments |
182 if (parameters.length < positionalArguments.length) return false; | 290 if (parameters.length < positionalArguments.length) return false; |
183 // Too few required arguments. | 291 // Too few required arguments. |
184 if (parameters.length > positionalArguments.length && | 292 if (parameters.length > positionalArguments.length && |
185 !parameters[positionalArguments.length].isOptional) return false; | 293 !parameters[positionalArguments.length].isOptional) return false; |
186 for (var i = 0; i < positionalArguments.length; i++) { | 294 for (var i = 0; i < positionalArguments.length; i++) { |
187 if (parameters[i].isNamed) { | 295 if (parameters[i].isNamed) { |
188 // Not enough positional arguments. | 296 // Not enough positional arguments. |
189 return false; | 297 return false; |
190 } | 298 } |
191 if (!_checkType( | 299 if (!_checkType(invocation.positionalArguments[i], parameters[i].type)) |
192 invocation.positionalArguments[i], parameters[i].type)) return false; | 300 return false; |
193 } | 301 } |
194 if (invocation.namedArguments.isNotEmpty) { | 302 if (invocation.namedArguments.isNotEmpty) { |
195 var startNamed; | 303 var startNamed; |
196 for (startNamed = parameters.length - 1; startNamed >= 0; startNamed--) { | 304 for (startNamed = parameters.length - 1; startNamed >= 0; startNamed--) { |
197 if (!parameters[startNamed].isNamed) break; | 305 if (!parameters[startNamed].isNamed) break; |
198 } | 306 } |
199 startNamed++; | 307 startNamed++; |
200 | 308 |
201 // TODO(jacobr): we are unneccessarily using an O(n^2) algorithm here. | 309 // TODO(jacobr): we are unneccessarily using an O(n^2) algorithm here. |
202 // If we have JS APIs with a lange number of named parameters we should | 310 // If we have JS APIs with a lange number of named parameters we should |
203 // optimize this. Either use a HashSet or invert this, walking over | 311 // optimize this. Either use a HashSet or invert this, walking over |
204 // parameters, querying invocation, and making sure we match | 312 // parameters, querying invocation, and making sure we match |
205 //invocation.namedArguments.size keys. | 313 //invocation.namedArguments.size keys. |
206 for (var name in invocation.namedArguments.keys) { | 314 for (var name in invocation.namedArguments.keys) { |
207 bool match = false; | 315 bool match = false; |
208 for (var j = startNamed; j < parameters.length; j++) { | 316 for (var j = startNamed; j < parameters.length; j++) { |
209 var p = parameters[j]; | 317 var p = parameters[j]; |
210 if (p.simpleName == name) { | 318 if (p.simpleName == name) { |
211 if (!_checkType(invocation.namedArguments[name], | 319 if (!_checkType( |
212 parameters[j].type)) return false; | 320 invocation.namedArguments[name], parameters[j].type)) |
| 321 return false; |
213 match = true; | 322 match = true; |
214 break; | 323 break; |
215 } | 324 } |
216 } | 325 } |
217 if (match == false) return false; | 326 if (match == false) return false; |
218 } | 327 } |
219 } | 328 } |
220 return true; | 329 return true; |
221 } | 330 } |
222 | 331 |
(...skipping 115 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
338 // is file://dart/sdk/lib/html/html_common/metadata.dart | 447 // is file://dart/sdk/lib/html/html_common/metadata.dart |
339 // instead of a proper dart: location. | 448 // instead of a proper dart: location. |
340 return true; | 449 return true; |
341 } | 450 } |
342 } | 451 } |
343 return false; | 452 return false; |
344 } | 453 } |
345 | 454 |
346 _getJsMemberName(mirrors.DeclarationMirror mirror) { | 455 _getJsMemberName(mirrors.DeclarationMirror mirror) { |
347 var name = _getJsName(mirror); | 456 var name = _getJsName(mirror); |
348 return name == null || name.isEmpty ? _stripReservedNamePrefix(_getDeclaration
Name(mirror)) : name; | 457 return name == null || name.isEmpty |
| 458 ? _stripReservedNamePrefix(_getDeclarationName(mirror)) |
| 459 : name; |
349 } | 460 } |
350 | 461 |
351 // TODO(jacobr): handle setters correctyl. | 462 // TODO(jacobr): handle setters correctyl. |
352 String _getDeclarationName(mirrors.DeclarationMirror declaration) { | 463 String _getDeclarationName(mirrors.DeclarationMirror declaration) { |
353 var name = mirrors.MirrorSystem.getName(declaration.simpleName); | 464 var name = mirrors.MirrorSystem.getName(declaration.simpleName); |
354 if (declaration is mirrors.MethodMirror && declaration.isSetter) { | 465 if (declaration is mirrors.MethodMirror && declaration.isSetter) { |
355 assert(name.endsWith("=")); | 466 assert(name.endsWith("=")); |
356 name = name.substring(0, name.length - 1); | 467 name = name.substring(0, name.length - 1); |
357 } | 468 } |
358 return name; | 469 return name; |
359 } | 470 } |
360 | 471 |
361 final _JS_LIBRARY_PREFIX = "js_library"; | 472 final _JS_LIBRARY_PREFIX = "js_library"; |
362 final _UNDEFINED_VAR = "_UNDEFINED_JS_CONST"; | 473 final _UNDEFINED_VAR = "_UNDEFINED_JS_CONST"; |
363 | 474 |
364 String _accessJsPath(String path) => _accessJsPathHelper(path.split(".")); | 475 String _accessJsPath(String path) => _accessJsPathHelper(path.split(".")); |
365 | 476 |
366 String _accessJsPathHelper(Iterable<String> parts) { | 477 String _accessJsPathHelper(Iterable<String> parts) { |
367 var sb = new StringBuffer(); | 478 var sb = new StringBuffer(); |
368 sb | 479 sb |
369 ..write('${_JS_LIBRARY_PREFIX}.JsNative.getProperty(' * parts.length) | 480 ..write('${_JS_LIBRARY_PREFIX}.JsNative.getProperty(' * parts.length) |
370 ..write("${_JS_LIBRARY_PREFIX}.context"); | 481 ..write("${_JS_LIBRARY_PREFIX}.context"); |
371 for (var p in parts) { | 482 for (var p in parts) { |
372 sb.write(", '$p')"); | 483 sb.write(", ${_escapeString(p)})"); |
373 } | 484 } |
374 return sb.toString(); | 485 return sb.toString(); |
375 } | 486 } |
376 | 487 |
377 // TODO(jacobr): remove these helpers and add JsNative.setPropertyDotted, | 488 // TODO(jacobr): remove these helpers and add JsNative.setPropertyDotted, |
378 // getPropertyDotted, and callMethodDotted helpers that would be simpler | 489 // getPropertyDotted, and callMethodDotted helpers that would be simpler |
379 // and more efficient. | 490 // and more efficient. |
380 String _accessJsPathSetter(String path) { | 491 String _accessJsPathSetter(String path) { |
381 var parts = path.split("."); | 492 var parts = path.split("."); |
382 return "${_JS_LIBRARY_PREFIX}.JsNative.setProperty(${_accessJsPathHelper(parts
.getRange(0, parts.length - 1)) | 493 return "${_JS_LIBRARY_PREFIX}.JsNative.setProperty(${_accessJsPathHelper(parts
.getRange(0, parts.length - 1)) |
383 }, '${parts.last}', v)"; | 494 }, ${_escapeString(parts.last)}, v)"; |
384 } | 495 } |
385 | 496 |
386 String _accessJsPathCallMethodHelper(String path) { | 497 String _accessJsPathCallMethodHelper(String path) { |
387 var parts = path.split("."); | 498 var parts = path.split("."); |
388 return "${_JS_LIBRARY_PREFIX}.JsNative.callMethod(${_accessJsPathHelper(parts.
getRange(0, parts.length - 1)) | 499 return "${_JS_LIBRARY_PREFIX}.JsNative.callMethod(${_accessJsPathHelper(parts.
getRange(0, parts.length - 1)) |
389 }, '${parts.last}',"; | 500 }, ${_escapeString(parts.last)},"; |
390 } | 501 } |
391 | 502 |
392 @Deprecated("Internal Use Only") | 503 @Deprecated("Internal Use Only") |
393 void addMemberHelper( | 504 void addMemberHelper( |
394 mirrors.MethodMirror declaration, String path, StringBuffer sb, | 505 mirrors.MethodMirror declaration, String path, StringBuffer sb, |
395 {bool isStatic: false, String memberName}) { | 506 {bool isStatic: false, String memberName}) { |
396 if (!declaration.isConstructor) { | 507 if (!declaration.isConstructor) { |
397 var jsName = _getJsMemberName(declaration); | 508 var jsName = _getJsMemberName(declaration); |
398 path = (path != null && path.isNotEmpty) ? "${path}.${jsName}" : jsName; | 509 path = (path != null && path.isNotEmpty) ? "${path}.${jsName}" : jsName; |
399 } | 510 } |
400 var name = memberName != null ? memberName : _getDeclarationName(declaration); | 511 var name = memberName != null ? memberName : _getDeclarationName(declaration); |
401 if (declaration.isConstructor) { | 512 if (declaration.isConstructor) { |
402 sb.write("factory"); | 513 sb.write("factory"); |
403 } else if (isStatic) { | 514 } else if (isStatic) { |
404 sb.write("static"); | 515 sb.write("static"); |
405 } else { | 516 } else { |
406 sb.write("patch"); | 517 sb.write("patch"); |
407 } | 518 } |
408 sb.write(" "); | 519 sb.write(" "); |
409 if (declaration.isGetter) { | 520 if (declaration.isGetter) { |
410 sb.write( | 521 sb.write("get $name => ${_accessJsPath(path)};"); |
411 "get $name => ${_accessJsPath(path)};"); | |
412 } else if (declaration.isSetter) { | 522 } else if (declaration.isSetter) { |
413 sb.write("set $name(v) {\n" | 523 sb.write("set $name(v) {\n" |
414 " ${_JS_LIBRARY_PREFIX}.safeForTypedInterop(v);\n" | 524 " ${_JS_LIBRARY_PREFIX}.safeForTypedInterop(v);\n" |
415 " return ${_accessJsPathSetter(path)};\n" | 525 " return ${_accessJsPathSetter(path)};\n" |
416 "}\n"); | 526 "}\n"); |
417 } else { | 527 } else { |
418 sb.write("$name("); | 528 sb.write("$name("); |
419 bool hasOptional = false; | 529 bool hasOptional = false; |
420 int i = 0; | 530 int i = 0; |
421 var args = <String>[]; | 531 var args = <String>[]; |
(...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
464 } | 574 } |
465 | 575 |
466 bool _isExternal(mirrors.MethodMirror mirror) { | 576 bool _isExternal(mirrors.MethodMirror mirror) { |
467 // This try-catch block is a workaround for BUG:24834. | 577 // This try-catch block is a workaround for BUG:24834. |
468 try { | 578 try { |
469 return mirror.isExternal; | 579 return mirror.isExternal; |
470 } catch (e) {} | 580 } catch (e) {} |
471 return false; | 581 return false; |
472 } | 582 } |
473 | 583 |
474 List<String> _generateExternalMethods(List<String> libraryPaths, bool useCachedP
atches) { | 584 List<String> _generateExternalMethods( |
| 585 List<String> libraryPaths, bool useCachedPatches) { |
475 var staticCodegen = <String>[]; | 586 var staticCodegen = <String>[]; |
476 | 587 |
477 if (libraryPaths.length == 0) { | 588 if (libraryPaths.length == 0) { |
478 mirrors.currentMirrorSystem().libraries.forEach((uri, library) { | 589 mirrors.currentMirrorSystem().libraries.forEach((uri, library) { |
479 var library_name = "${uri.scheme}:${uri.path}"; | 590 var library_name = "${uri.scheme}:${uri.path}"; |
480 if (useCachedPatches && cached_patches.containsKey(library_name)) { | 591 if (useCachedPatches && cached_patches.containsKey(library_name)) { |
481 // Use the pre-generated patch files for DOM dart:nnnn libraries. | 592 // Use the pre-generated patch files for DOM dart:nnnn libraries. |
482 var patch = cached_patches[library_name]; | 593 var patch = cached_patches[library_name]; |
483 staticCodegen.addAll(patch); | 594 staticCodegen.addAll(patch); |
484 } else if (_hasJsName(library)) { | 595 } else if (_hasJsName(library)) { |
485 // Library marked with @JS | 596 // Library marked with @JS |
486 _generateLibraryCodegen(uri, library, staticCodegen); | 597 _generateLibraryCodegen(uri, library, staticCodegen); |
487 } else if (!useCachedPatches) { | 598 } else if (!useCachedPatches) { |
488 // Can't use the cached patches file, instead this is a signal to genera
te | 599 // Can't use the cached patches file, instead this is a signal to genera
te |
489 // the patches for this file. | 600 // the patches for this file. |
490 _generateLibraryCodegen(uri, library, staticCodegen); | 601 _generateLibraryCodegen(uri, library, staticCodegen); |
491 } | 602 } |
492 }); // End of library foreach | 603 }); // End of library foreach |
493 } else { | 604 } else { |
494 // Used to generate cached_patches.dart file for all IDL generated dart: | 605 // Used to generate cached_patches.dart file for all IDL generated dart: |
495 // files to the WebKit DOM. | 606 // files to the WebKit DOM. |
496 for (var library_name in libraryPaths) { | 607 for (var library_name in libraryPaths) { |
497 var parts = library_name.split(':'); | 608 var parts = library_name.split(':'); |
498 var uri = new Uri(scheme: parts[0], path: parts[1]); | 609 var uri = new Uri(scheme: parts[0], path: parts[1]); |
499 var library = mirrors.currentMirrorSystem().libraries[uri]; | 610 var library = mirrors.currentMirrorSystem().libraries[uri]; |
500 _generateLibraryCodegen(uri, library, staticCodegen); | 611 _generateLibraryCodegen(uri, library, staticCodegen); |
501 } | 612 } |
502 } | 613 } |
503 | 614 |
504 return staticCodegen; | 615 return staticCodegen; |
505 } | 616 } |
506 | 617 |
507 _generateLibraryCodegen(uri, library, staticCodegen) { | 618 _generateLibraryCodegen(uri, library, staticCodegen) { |
508 // Is it a dart generated library? | 619 // Is it a dart generated library? |
509 var dartLibrary = uri.scheme == 'dart'; | 620 var dartLibrary = uri.scheme == 'dart'; |
510 | 621 |
511 var sb = new StringBuffer(); | 622 var sb = new StringBuffer(); |
512 String jsLibraryName = _getJsName(library); | 623 String jsLibraryName = _getJsName(library); |
513 | 624 |
514 // Sort by patch file by its declaration name. | 625 // Sort by patch file by its declaration name. |
515 var sortedDeclKeys = library.declarations.keys.toList(); | 626 var sortedDeclKeys = library.declarations.keys.toList(); |
516 sortedDeclKeys.sort((a, b) => mirrors.MirrorSystem.getName(a).compareTo(mirr
ors.MirrorSystem.getName(b))); | 627 sortedDeclKeys.sort((a, b) => mirrors.MirrorSystem |
| 628 .getName(a) |
| 629 .compareTo(mirrors.MirrorSystem.getName(b))); |
517 | 630 |
518 sortedDeclKeys.forEach((name) { | 631 sortedDeclKeys.forEach((name) { |
519 var declaration = library.declarations[name]; | 632 var declaration = library.declarations[name]; |
520 if (declaration is mirrors.MethodMirror) { | 633 if (declaration is mirrors.MethodMirror) { |
521 if ((_hasJsName(declaration) || jsLibraryName != null) && | 634 if ((_hasJsName(declaration) || jsLibraryName != null) && |
522 _isExternal(declaration)) { | 635 _isExternal(declaration)) { |
523 addMemberHelper(declaration, jsLibraryName, sb); | 636 addMemberHelper(declaration, jsLibraryName, sb); |
| 637 } |
| 638 } else if (declaration is mirrors.ClassMirror) { |
| 639 mirrors.ClassMirror clazz = declaration; |
| 640 var isDom = dartLibrary ? hasDomName(clazz) : false; |
| 641 var isJsInterop = _hasJsName(clazz); |
| 642 if (isDom || isJsInterop) { |
| 643 // TODO(jacobr): verify class implements JavaScriptObject. |
| 644 var className = mirrors.MirrorSystem.getName(clazz.simpleName); |
| 645 bool isPrivate = className.startsWith('_'); |
| 646 var classNameImpl = '${className}Impl'; |
| 647 var sbPatch = new StringBuffer(); |
| 648 if (isJsInterop) { |
| 649 String jsClassName = _getJsMemberName(clazz); |
| 650 |
| 651 jsInterfaceTypes.add(clazz); |
| 652 clazz.declarations.forEach((name, declaration) { |
| 653 if (declaration is! mirrors.MethodMirror || |
| 654 !_isExternal(declaration)) return; |
| 655 if (declaration.isFactoryConstructor && _isAnonymousClass(clazz)) { |
| 656 sbPatch.write(" factory ${className}("); |
| 657 int i = 0; |
| 658 var args = <String>[]; |
| 659 for (var p in declaration.parameters) { |
| 660 args.add(mirrors.MirrorSystem.getName(p.simpleName)); |
| 661 i++; |
| 662 } |
| 663 if (args.isNotEmpty) { |
| 664 sbPatch |
| 665 ..write('{') |
| 666 ..write( |
| 667 args.map((name) => '$name:${_UNDEFINED_VAR}').join(", ")) |
| 668 ..write('}'); |
| 669 } |
| 670 sbPatch.write(") {\n" |
| 671 " var ret = ${_JS_LIBRARY_PREFIX}.JsNative.newObject();\n")
; |
| 672 i = 0; |
| 673 for (var p in declaration.parameters) { |
| 674 assert(p.isNamed); // TODO(jacobr): throw. |
| 675 var name = args[i]; |
| 676 var jsName = _stripReservedNamePrefix( |
| 677 mirrors.MirrorSystem.getName(p.simpleName)); |
| 678 sbPatch.write(" if($name != ${_UNDEFINED_VAR}) {\n" |
| 679 " ${_JS_LIBRARY_PREFIX}.safeForTypedInterop($name);\n" |
| 680 " ${_JS_LIBRARY_PREFIX}.JsNative.setProperty(ret, ${_es
capeString(jsName)}, $name);\n" |
| 681 " }\n"); |
| 682 i++; |
| 683 } |
| 684 |
| 685 sbPatch.write(" return ret;" |
| 686 "}\n"); |
| 687 } else if (declaration.isConstructor || |
| 688 declaration.isFactoryConstructor) { |
| 689 sbPatch.write(" "); |
| 690 addMemberHelper( |
| 691 declaration, |
| 692 (jsLibraryName != null && jsLibraryName.isNotEmpty) |
| 693 ? "${jsLibraryName}.${jsClassName}" |
| 694 : jsClassName, |
| 695 sbPatch, |
| 696 isStatic: true, |
| 697 memberName: className); |
| 698 } |
| 699 }); // End of clazz.declarations.forEach |
| 700 |
| 701 clazz.staticMembers.forEach((memberName, member) { |
| 702 if (_isExternal(member)) { |
| 703 sbPatch.write(" "); |
| 704 addMemberHelper( |
| 705 member, |
| 706 (jsLibraryName != null && jsLibraryName.isNotEmpty) |
| 707 ? "${jsLibraryName}.${jsClassName}" |
| 708 : jsClassName, |
| 709 sbPatch, |
| 710 isStatic: true); |
| 711 } |
| 712 }); |
524 } | 713 } |
525 } else if (declaration is mirrors.ClassMirror) { | 714 if (isDom) { |
526 mirrors.ClassMirror clazz = declaration; | 715 sbPatch.write( |
527 var isDom = dartLibrary ? hasDomName(clazz) : false; | 716 " static Type get instanceRuntimeType => ${classNameImpl};\n"); |
528 var isJsInterop = _hasJsName(clazz); | 717 } |
529 if (isDom || isJsInterop) { | 718 if (isPrivate) { |
530 // TODO(jacobr): verify class implements JavaScriptObject. | 719 sb.write(""" |
531 var className = mirrors.MirrorSystem.getName(clazz.simpleName); | |
532 bool isPrivate = className.startsWith('_'); | |
533 var classNameImpl = '${className}Impl'; | |
534 var sbPatch = new StringBuffer(); | |
535 if (isJsInterop) { | |
536 String jsClassName = _getJsMemberName(clazz); | |
537 | |
538 jsInterfaceTypes.add(clazz); | |
539 clazz.declarations.forEach((name, declaration) { | |
540 if (declaration is! mirrors.MethodMirror || | |
541 !_isExternal(declaration)) return; | |
542 if (declaration.isFactoryConstructor && | |
543 _isAnonymousClass(clazz)) { | |
544 sbPatch.write(" factory ${className}("); | |
545 int i = 0; | |
546 var args = <String>[]; | |
547 for (var p in declaration.parameters) { | |
548 args.add(mirrors.MirrorSystem.getName(p.simpleName)); | |
549 i++; | |
550 } | |
551 if (args.isNotEmpty) { | |
552 sbPatch | |
553 ..write('{') | |
554 ..write(args | |
555 .map((name) => '$name:${_UNDEFINED_VAR}') | |
556 .join(", ")) | |
557 ..write('}'); | |
558 } | |
559 sbPatch.write(") {\n" | |
560 " var ret = ${_JS_LIBRARY_PREFIX}.JsNative.newObject();\n
"); | |
561 i = 0; | |
562 for (var p in declaration.parameters) { | |
563 assert(p.isNamed); // TODO(jacobr): throw. | |
564 var name = args[i]; | |
565 var jsName = _stripReservedNamePrefix( | |
566 mirrors.MirrorSystem.getName(p.simpleName)); | |
567 sbPatch.write(" if($name != ${_UNDEFINED_VAR}) {\n" | |
568 " ${_JS_LIBRARY_PREFIX}.safeForTypedInterop($name);\n
" | |
569 " ${_JS_LIBRARY_PREFIX}.JsNative.setProperty(ret, '$j
sName', $name);\n" | |
570 " }\n"); | |
571 i++; | |
572 } | |
573 | |
574 sbPatch.write( | |
575 " return ret;" | |
576 "}\n"); | |
577 } else if (declaration.isConstructor || | |
578 declaration.isFactoryConstructor) { | |
579 sbPatch.write(" "); | |
580 addMemberHelper( | |
581 declaration, | |
582 (jsLibraryName != null && jsLibraryName.isNotEmpty) | |
583 ? "${jsLibraryName}.${jsClassName}" | |
584 : jsClassName, | |
585 sbPatch, | |
586 isStatic: true, | |
587 memberName: className); | |
588 } | |
589 }); // End of clazz.declarations.forEach | |
590 | |
591 clazz.staticMembers.forEach((memberName, member) { | |
592 if (_isExternal(member)) { | |
593 sbPatch.write(" "); | |
594 addMemberHelper( | |
595 member, | |
596 (jsLibraryName != null && jsLibraryName.isNotEmpty) | |
597 ? "${jsLibraryName}.${jsClassName}" | |
598 : jsClassName, | |
599 sbPatch, | |
600 isStatic: true); | |
601 } | |
602 }); | |
603 } | |
604 if (isDom) { | |
605 sbPatch.write(" static Type get instanceRuntimeType => ${classNameI
mpl};\n"); | |
606 } | |
607 if (isPrivate) { | |
608 sb.write(""" | |
609 class ${escapePrivateClassPrefix}${className} implements $className {} | 720 class ${escapePrivateClassPrefix}${className} implements $className {} |
610 """); | 721 """); |
| 722 } |
| 723 |
| 724 if (sbPatch.isNotEmpty) { |
| 725 var typeVariablesClause = ''; |
| 726 if (!clazz.typeVariables.isEmpty) { |
| 727 typeVariablesClause = |
| 728 '<${clazz.typeVariables.map((m) => mirrors.MirrorSystem.getName(
m.simpleName)).join(',')}>'; |
611 } | 729 } |
612 | 730 sb.write(""" |
613 if (sbPatch.isNotEmpty) { | |
614 var typeVariablesClause = ''; | |
615 if (!clazz.typeVariables.isEmpty) { | |
616 typeVariablesClause = | |
617 '<${clazz.typeVariables.map((m) => mirrors.MirrorSystem.getNam
e(m.simpleName)).join(',')}>'; | |
618 } | |
619 sb.write(""" | |
620 patch class $className$typeVariablesClause { | 731 patch class $className$typeVariablesClause { |
621 $sbPatch | 732 $sbPatch |
622 } | 733 } |
623 """); | 734 """); |
624 if (isDom) { | 735 if (isDom) { |
625 sb.write(""" | 736 sb.write(""" |
626 class $classNameImpl$typeVariablesClause extends $className implements ${_JS_LIB
RARY_PREFIX}.JSObjectInterfacesDom { | 737 class $classNameImpl$typeVariablesClause extends $className implements ${_JS_LIB
RARY_PREFIX}.JSObjectInterfacesDom { |
627 ${classNameImpl}.internal_() : super.internal_(); | 738 ${classNameImpl}.internal_() : super.internal_(); |
628 get runtimeType => $className; | 739 get runtimeType => $className; |
629 toString() => super.toString(); | 740 toString() => super.toString(); |
630 } | 741 } |
631 """); | 742 """); |
632 } | |
633 } | 743 } |
634 } | 744 } |
635 } | 745 } |
636 }); | 746 } |
637 if (sb.isNotEmpty) { | 747 }); |
638 staticCodegen | 748 if (sb.isNotEmpty) { |
639 ..add(uri.toString()) | 749 staticCodegen |
640 ..add("${uri}_js_interop_patch.dart") | 750 ..add(uri.toString()) |
641 ..add(""" | 751 ..add("${uri}_js_interop_patch.dart") |
| 752 ..add(""" |
642 import 'dart:js' as ${_JS_LIBRARY_PREFIX}; | 753 import 'dart:js' as ${_JS_LIBRARY_PREFIX}; |
643 | 754 |
644 /** | 755 /** |
645 * Placeholder object for cases where we need to determine exactly how many | 756 * Placeholder object for cases where we need to determine exactly how many |
646 * args were passed to a function. | 757 * args were passed to a function. |
647 */ | 758 */ |
648 const ${_UNDEFINED_VAR} = const Object(); | 759 const ${_UNDEFINED_VAR} = const Object(); |
649 | 760 |
650 ${sb} | 761 ${sb} |
651 """); | 762 """); |
652 } | 763 } |
653 } | 764 } |
654 | 765 |
655 // Remember the @JS type to compare annotation type. | 766 // Remember the @JS type to compare annotation type. |
656 var _atJsType = -1; | 767 var _atJsType = -1; |
657 | 768 |
658 void setupJsTypeCache() { | 769 void setupJsTypeCache() { |
659 // Cache the @JS Type. | 770 // Cache the @JS Type. |
660 if (_atJsType == -1) { | 771 if (_atJsType == -1) { |
661 var uri = new Uri(scheme: "package", path: "js/js.dart"); | 772 var uri = new Uri(scheme: "package", path: "js/js.dart"); |
662 var jsLibrary = mirrors.currentMirrorSystem().libraries[uri]; | 773 var jsLibrary = mirrors.currentMirrorSystem().libraries[uri]; |
663 if (jsLibrary != null) { | 774 if (jsLibrary != null) { |
664 // @ JS used somewhere. | 775 // @ JS used somewhere. |
665 var jsDeclaration = jsLibrary.declarations[new Symbol("JS")]; | 776 var jsDeclaration = jsLibrary.declarations[new Symbol("JS")]; |
666 _atJsType = jsDeclaration.reflectedType; | 777 _atJsType = jsDeclaration.reflectedType; |
667 } else { | 778 } else { |
668 // @ JS not used in any library. | 779 // @ JS not used in any library. |
669 _atJsType = null; | 780 _atJsType = null; |
670 } | 781 } |
671 } | 782 } |
672 } | 783 } |
673 | 784 |
674 /** | 785 /** |
675 * Generates part files defining source code for JSObjectImpl, all DOM classes | 786 * Generates part files defining source code for JSObjectImpl, all DOM classes |
676 * classes. This codegen is needed so that type checks for all registered | 787 * classes. This codegen is needed so that type checks for all registered |
677 * JavaScript interop classes pass. | 788 * JavaScript interop classes pass. |
678 * If genCachedPatches is true then the patch files don't exist this is a specia
l | 789 * If genCachedPatches is true then the patch files don't exist this is a specia
l |
679 * signal to generate and emit the patches to stdout to be captured and put into | 790 * signal to generate and emit the patches to stdout to be captured and put into |
680 * the file sdk/lib/js/dartium/cached_patches.dart | 791 * the file sdk/lib/js/dartium/cached_patches.dart |
681 */ | 792 */ |
682 List<String> _generateInteropPatchFiles(List<String> libraryPaths, genCachedPatc
hes) { | 793 List<String> _generateInteropPatchFiles( |
| 794 List<String> libraryPaths, genCachedPatches) { |
683 // Cache the @JS Type. | 795 // Cache the @JS Type. |
684 if (_atJsType == -1) setupJsTypeCache(); | 796 if (_atJsType == -1) setupJsTypeCache(); |
685 | 797 |
686 var ret = _generateExternalMethods(libraryPaths, genCachedPatches ? false : tr
ue); | 798 var ret = |
| 799 _generateExternalMethods(libraryPaths, genCachedPatches ? false : true); |
687 var libraryPrefixes = new Map<mirrors.LibraryMirror, String>(); | 800 var libraryPrefixes = new Map<mirrors.LibraryMirror, String>(); |
688 var prefixNames = new Set<String>(); | 801 var prefixNames = new Set<String>(); |
689 var sb = new StringBuffer(); | 802 var sb = new StringBuffer(); |
690 | 803 |
691 var implements = <String>[]; | 804 var implements = <String>[]; |
692 var implementsArray = <String>[]; | 805 var implementsArray = <String>[]; |
693 var implementsDom = <String>[]; | 806 var implementsDom = <String>[]; |
694 var listMirror = mirrors.reflectType(List); | 807 var listMirror = mirrors.reflectType(List); |
695 var functionMirror = mirrors.reflectType(Function); | 808 var functionMirror = mirrors.reflectType(Function); |
696 var jsObjectMirror = mirrors.reflectType(JSObject); | 809 var jsObjectMirror = mirrors.reflectType(JSObject); |
(...skipping 16 matching lines...) Expand all Loading... |
713 } | 826 } |
714 prefixNames.add(prefixName); | 827 prefixNames.add(prefixName); |
715 libraryPrefixes[libraryMirror] = prefixName; | 828 libraryPrefixes[libraryMirror] = prefixName; |
716 } | 829 } |
717 var isArray = typeMirror.isSubtypeOf(listMirror); | 830 var isArray = typeMirror.isSubtypeOf(listMirror); |
718 var isFunction = typeMirror.isSubtypeOf(functionMirror); | 831 var isFunction = typeMirror.isSubtypeOf(functionMirror); |
719 var isJSObject = typeMirror.isSubtypeOf(jsObjectMirror); | 832 var isJSObject = typeMirror.isSubtypeOf(jsObjectMirror); |
720 var className = mirrors.MirrorSystem.getName(typeMirror.simpleName); | 833 var className = mirrors.MirrorSystem.getName(typeMirror.simpleName); |
721 var isPrivate = className.startsWith('_'); | 834 var isPrivate = className.startsWith('_'); |
722 if (isPrivate) className = '${escapePrivateClassPrefix}${className}'; | 835 if (isPrivate) className = '${escapePrivateClassPrefix}${className}'; |
723 var fullName = | 836 var fullName = '${prefixName}.${className}'; |
724 '${prefixName}.${className}'; | |
725 (isArray ? implementsArray : implements).add(fullName); | 837 (isArray ? implementsArray : implements).add(fullName); |
726 if (!isArray && !isFunction && !isJSObject) { | 838 if (!isArray && !isFunction && !isJSObject) { |
727 // For DOM classes we need to be a bit more conservative at tagging them | 839 // For DOM classes we need to be a bit more conservative at tagging them |
728 // as implementing JS inteorp classes risks strange unintended | 840 // as implementing JS inteorp classes risks strange unintended |
729 // consequences as unrleated code may have instanceof checks. Checking | 841 // consequences as unrleated code may have instanceof checks. Checking |
730 // for isJSObject ensures we do not accidentally pull in existing | 842 // for isJSObject ensures we do not accidentally pull in existing |
731 // dart:html classes as they all have JSObject as a base class. | 843 // dart:html classes as they all have JSObject as a base class. |
732 // Note that methods from these classes can still be called on a | 844 // Note that methods from these classes can still be called on a |
733 // dart:html instance but checked mode type checks will fail. This is | 845 // dart:html instance but checked mode type checks will fail. This is |
734 // not ideal but is better than causing strange breaks in existing | 846 // not ideal but is better than causing strange breaks in existing |
(...skipping 218 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
953 | 1065 |
954 JsObject get context { | 1066 JsObject get context { |
955 if (_cachedContext == null) { | 1067 if (_cachedContext == null) { |
956 _cachedContext = _context; | 1068 _cachedContext = _context; |
957 } | 1069 } |
958 return _cachedContext; | 1070 return _cachedContext; |
959 } | 1071 } |
960 | 1072 |
961 _lookupType(o, bool isCrossFrame, bool isElement) { | 1073 _lookupType(o, bool isCrossFrame, bool isElement) { |
962 try { | 1074 try { |
963 var type = html_common.lookupType(o, isElement); | 1075 var type = html_common.lookupType(o, isElement); |
964 var typeMirror = mirrors.reflectType(type); | 1076 var typeMirror = mirrors.reflectType(type); |
965 var legacyInteropConvertToNative = typeMirror.isSubtypeOf(mirrors.reflectType
(html.Blob)) || | 1077 var legacyInteropConvertToNative = |
966 typeMirror.isSubtypeOf(mirrors.reflectType(html.Event)) || | 1078 typeMirror.isSubtypeOf(mirrors.reflectType(html.Blob)) || |
967 typeMirror.isSubtypeOf(mirrors.reflectType(indexed_db.KeyRange)) || | 1079 typeMirror.isSubtypeOf(mirrors.reflectType(html.Event)) || |
968 typeMirror.isSubtypeOf(mirrors.reflectType(html.ImageData)) || | 1080 typeMirror.isSubtypeOf(mirrors.reflectType(indexed_db.KeyRange)) || |
969 typeMirror.isSubtypeOf(mirrors.reflectType(html.Node)) || | 1081 typeMirror.isSubtypeOf(mirrors.reflectType(html.ImageData)) || |
| 1082 typeMirror.isSubtypeOf(mirrors.reflectType(html.Node)) || |
970 // TypedData is removed from this list as it is converted directly | 1083 // TypedData is removed from this list as it is converted directly |
971 // rather than flowing through the interceptor code path. | 1084 // rather than flowing through the interceptor code path. |
972 // typeMirror.isSubtypeOf(mirrors.reflectType(typed_data.TypedData)) || | 1085 // typeMirror.isSubtypeOf(mirrors.reflectType(typed_data.TypedData)) || |
973 typeMirror.isSubtypeOf(mirrors.reflectType(html.Window)); | 1086 typeMirror.isSubtypeOf(mirrors.reflectType(html.Window)); |
974 if (isCrossFrame && !typeMirror.isSubtypeOf(mirrors.reflectType(html.Window)
)) { | 1087 if (isCrossFrame && |
| 1088 !typeMirror.isSubtypeOf(mirrors.reflectType(html.Window))) { |
975 // TODO(jacobr): evaluate using the true cross frame Window class, etc. | 1089 // TODO(jacobr): evaluate using the true cross frame Window class, etc. |
976 // as well as triggering that legacy JS Interop returns raw JsObject | 1090 // as well as triggering that legacy JS Interop returns raw JsObject |
977 // instances. | 1091 // instances. |
978 legacyInteropConvertToNative = false; | 1092 legacyInteropConvertToNative = false; |
979 } | 1093 } |
980 return [type, legacyInteropConvertToNative]; | 1094 return [type, legacyInteropConvertToNative]; |
981 } catch (e) { } | 1095 } catch (e) {} |
982 return [JSObject.instanceRuntimeType, false]; | 1096 return [JSObject.instanceRuntimeType, false]; |
983 } | 1097 } |
984 | 1098 |
985 /** | 1099 /** |
986 * Base class for both the legacy JsObject class and the modern JSObject class. | 1100 * Base class for both the legacy JsObject class and the modern JSObject class. |
987 * This allows the JsNative utility class tobehave identically whether it is | 1101 * This allows the JsNative utility class tobehave identically whether it is |
988 * called on a JsObject or a JSObject. | 1102 * called on a JsObject or a JSObject. |
989 */ | 1103 */ |
990 class _JSObjectBase extends NativeFieldWrapperClass2 { | 1104 class _JSObjectBase extends NativeFieldWrapperClass2 { |
991 String _toString() native "JSObject_toString"; | 1105 String _toString() native "JSObject_toString"; |
(...skipping 61 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1053 */ | 1167 */ |
1054 factory JsObject.jsify(object) { | 1168 factory JsObject.jsify(object) { |
1055 if ((object is! Map) && (object is! Iterable)) { | 1169 if ((object is! Map) && (object is! Iterable)) { |
1056 throw new ArgumentError("object must be a Map or Iterable"); | 1170 throw new ArgumentError("object must be a Map or Iterable"); |
1057 } | 1171 } |
1058 return _jsify(object); | 1172 return _jsify(object); |
1059 } | 1173 } |
1060 | 1174 |
1061 static JsObject _jsify(object) native "JsObject_jsify"; | 1175 static JsObject _jsify(object) native "JsObject_jsify"; |
1062 | 1176 |
1063 static JsObject _fromBrowserObject(object) native "JsObject_fromBrowserObject"
; | 1177 static JsObject _fromBrowserObject(object) |
| 1178 native "JsObject_fromBrowserObject"; |
1064 | 1179 |
1065 /** | 1180 /** |
1066 * Returns the value associated with [property] from the proxied JavaScript | 1181 * Returns the value associated with [property] from the proxied JavaScript |
1067 * object. | 1182 * object. |
1068 * | 1183 * |
1069 * The type of [property] must be either [String] or [num]. | 1184 * The type of [property] must be either [String] or [num]. |
1070 */ | 1185 */ |
1071 operator [](property) { | 1186 operator [](property) { |
1072 try { | 1187 try { |
1073 return _operator_getterLegacy(property); | 1188 return _operator_getterLegacy(property); |
(...skipping 22 matching lines...) Expand all Loading... |
1096 | 1211 |
1097 _operator_setterLegacy(property, value) native "JsObject_[]=Legacy"; | 1212 _operator_setterLegacy(property, value) native "JsObject_[]=Legacy"; |
1098 | 1213 |
1099 int get hashCode native "JsObject_hashCode"; | 1214 int get hashCode native "JsObject_hashCode"; |
1100 | 1215 |
1101 operator ==(other) { | 1216 operator ==(other) { |
1102 if (other is! JsObject && other is! JSObject) return false; | 1217 if (other is! JsObject && other is! JSObject) return false; |
1103 return _identityEquality(this, other); | 1218 return _identityEquality(this, other); |
1104 } | 1219 } |
1105 | 1220 |
1106 static bool _identityEquality(a, b) | 1221 static bool _identityEquality(a, b) native "JsObject_identityEquality"; |
1107 native "JsObject_identityEquality"; | |
1108 | 1222 |
1109 /** | 1223 /** |
1110 * Returns `true` if the JavaScript object contains the specified property | 1224 * Returns `true` if the JavaScript object contains the specified property |
1111 * either directly or though its prototype chain. | 1225 * either directly or though its prototype chain. |
1112 * | 1226 * |
1113 * This is the equivalent of the `in` operator in JavaScript. | 1227 * This is the equivalent of the `in` operator in JavaScript. |
1114 */ | 1228 */ |
1115 bool hasProperty(String property) => _hasProperty(property); | 1229 bool hasProperty(String property) => _hasProperty(property); |
1116 | 1230 |
1117 /** | 1231 /** |
(...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1156 throw new html.DomException.jsInterop(e); | 1270 throw new html.DomException.jsInterop(e); |
1157 } else { | 1271 } else { |
1158 throw new NoSuchMethodError(this, new Symbol(method), args, null); | 1272 throw new NoSuchMethodError(this, new Symbol(method), args, null); |
1159 } | 1273 } |
1160 } | 1274 } |
1161 } | 1275 } |
1162 | 1276 |
1163 _callMethodLegacy(String name, List args) native "JsObject_callMethodLegacy"; | 1277 _callMethodLegacy(String name, List args) native "JsObject_callMethodLegacy"; |
1164 } | 1278 } |
1165 | 1279 |
1166 | |
1167 /// Base class for all JS objects used through dart:html and typed JS interop. | 1280 /// Base class for all JS objects used through dart:html and typed JS interop. |
1168 @Deprecated("Internal Use Only") | 1281 @Deprecated("Internal Use Only") |
1169 class JSObject extends _JSObjectBase { | 1282 class JSObject extends _JSObjectBase { |
1170 JSObject.internal() {} | 1283 JSObject.internal() {} |
1171 external static Type get instanceRuntimeType; | 1284 external static Type get instanceRuntimeType; |
1172 | 1285 |
1173 /** | 1286 /** |
1174 * Returns the result of the JavaScript objects `toString` method. | 1287 * Returns the result of the JavaScript objects `toString` method. |
1175 */ | 1288 */ |
1176 String toString() { | 1289 String toString() { |
(...skipping 16 matching lines...) Expand all Loading... |
1193 if (CHECK_JS_INVOCATIONS) { | 1306 if (CHECK_JS_INVOCATIONS) { |
1194 var matches = _allowedGetters[invocation.memberName]; | 1307 var matches = _allowedGetters[invocation.memberName]; |
1195 if (matches == null && | 1308 if (matches == null && |
1196 !_allowedMethods.containsKey(invocation.memberName)) { | 1309 !_allowedMethods.containsKey(invocation.memberName)) { |
1197 throwError(); | 1310 throwError(); |
1198 } | 1311 } |
1199 var ret = _operator_getter(name); | 1312 var ret = _operator_getter(name); |
1200 if (matches != null) return ret; | 1313 if (matches != null) return ret; |
1201 if (ret is Function || | 1314 if (ret is Function || |
1202 (ret is JsFunction /* shouldn't be needed in the future*/) && | 1315 (ret is JsFunction /* shouldn't be needed in the future*/) && |
1203 _allowedMethods.containsKey( | 1316 _allowedMethods.containsKey(invocation.memberName)) |
1204 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. | 1317 return ret; // Warning: we have not bound "this"... we could type chec
k on the Function but that is of little value in Dart. |
1205 throwError(); | 1318 throwError(); |
1206 } else { | 1319 } else { |
1207 // TODO(jacobr): should we throw if the JavaScript object doesn't have t
he property? | 1320 // TODO(jacobr): should we throw if the JavaScript object doesn't have t
he property? |
1208 return _operator_getter(name); | 1321 return _operator_getter(name); |
1209 } | 1322 } |
1210 } else if (invocation.isSetter) { | 1323 } else if (invocation.isSetter) { |
1211 if (CHECK_JS_INVOCATIONS) { | 1324 if (CHECK_JS_INVOCATIONS) { |
1212 var matches = _allowedSetters[invocation.memberName]; | 1325 var matches = _allowedSetters[invocation.memberName]; |
1213 if (matches == null || | 1326 if (matches == null || !matches.checkInvocation(invocation)) |
1214 !matches.checkInvocation(invocation)) throwError(); | 1327 throwError(); |
1215 } | 1328 } |
1216 assert(name.endsWith("=")); | 1329 assert(name.endsWith("=")); |
1217 name = name.substring(0, name.length - 1); | 1330 name = name.substring(0, name.length - 1); |
1218 return _operator_setter(name, invocation.positionalArguments.first); | 1331 return _operator_setter(name, invocation.positionalArguments.first); |
1219 } else { | 1332 } else { |
1220 // TODO(jacobr): also allow calling getters that look like functions. | 1333 // TODO(jacobr): also allow calling getters that look like functions. |
1221 var matches; | 1334 var matches; |
1222 if (CHECK_JS_INVOCATIONS) { | 1335 if (CHECK_JS_INVOCATIONS) { |
1223 matches = _allowedMethods[invocation.memberName]; | 1336 matches = _allowedMethods[invocation.memberName]; |
1224 if (matches == null || | 1337 if (matches == null || !matches.checkInvocation(invocation)) |
1225 !matches.checkInvocation(invocation)) throwError(); | 1338 throwError(); |
1226 } | 1339 } |
1227 var ret = _callMethod(name, _buildArgs(invocation)); | 1340 var ret = _callMethod(name, _buildArgs(invocation)); |
1228 if (CHECK_JS_INVOCATIONS) { | 1341 if (CHECK_JS_INVOCATIONS) { |
1229 if (!matches._checkReturnType(ret)) { | 1342 if (!matches._checkReturnType(ret)) { |
1230 html.window.console.error("Return value for method: ${name} is " | 1343 html.window.console.error("Return value for method: ${name} is " |
1231 "${ret.runtimeType} which is inconsistent with all typed " | 1344 "${ret.runtimeType} which is inconsistent with all typed " |
1232 "JS interop definitions for method ${name}."); | 1345 "JS interop definitions for method ${name}."); |
1233 } | 1346 } |
1234 } | 1347 } |
1235 return ret; | 1348 return ret; |
(...skipping 59 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1295 [a1 = _UNDEFINED, | 1408 [a1 = _UNDEFINED, |
1296 a2 = _UNDEFINED, | 1409 a2 = _UNDEFINED, |
1297 a3 = _UNDEFINED, | 1410 a3 = _UNDEFINED, |
1298 a4 = _UNDEFINED, | 1411 a4 = _UNDEFINED, |
1299 a5 = _UNDEFINED, | 1412 a5 = _UNDEFINED, |
1300 a6 = _UNDEFINED, | 1413 a6 = _UNDEFINED, |
1301 a7 = _UNDEFINED, | 1414 a7 = _UNDEFINED, |
1302 a8 = _UNDEFINED, | 1415 a8 = _UNDEFINED, |
1303 a9 = _UNDEFINED, | 1416 a9 = _UNDEFINED, |
1304 a10 = _UNDEFINED]) { | 1417 a10 = _UNDEFINED]) { |
1305 return _apply(_stripUndefinedArgs([a1, a2, a3, a4, a5, a6, a7, a8, a9, a10])
); | 1418 return _apply( |
| 1419 _stripUndefinedArgs([a1, a2, a3, a4, a5, a6, a7, a8, a9, a10])); |
1306 } | 1420 } |
1307 | 1421 |
1308 noSuchMethod(Invocation invocation) { | 1422 noSuchMethod(Invocation invocation) { |
1309 if (invocation.isMethod && invocation.memberName == #call) { | 1423 if (invocation.isMethod && invocation.memberName == #call) { |
1310 return _apply(_buildArgs(invocation)); | 1424 return _apply(_buildArgs(invocation)); |
1311 } | 1425 } |
1312 return super.noSuchMethod(invocation); | 1426 return super.noSuchMethod(invocation); |
1313 } | 1427 } |
1314 | 1428 |
1315 dynamic _apply(List args, {thisArg}) native "JSFunction_apply"; | 1429 dynamic _apply(List args, {thisArg}) native "JSFunction_apply"; |
1316 | 1430 |
1317 static JSFunction _createWithThis(Function f) native "JSFunction_createWithThi
s"; | 1431 static JSFunction _createWithThis(Function f) |
| 1432 native "JSFunction_createWithThis"; |
1318 static JSFunction _create(Function f) native "JSFunction_create"; | 1433 static JSFunction _create(Function f) native "JSFunction_create"; |
1319 } | 1434 } |
1320 | 1435 |
1321 // JavaScript interop methods that do not automatically wrap to dart:html types. | 1436 // JavaScript interop methods that do not automatically wrap to dart:html types. |
1322 // Warning: this API is not exposed to dart:js. | 1437 // Warning: this API is not exposed to dart:js. |
1323 // TODO(jacobr): rename to JSNative and make at least part of this API public. | 1438 // TODO(jacobr): rename to JSNative and make at least part of this API public. |
1324 @Deprecated("Internal Use Only") | 1439 @Deprecated("Internal Use Only") |
1325 class JsNative { | 1440 class JsNative { |
1326 static JSObject jsify(object) native "JSObject_jsify"; | 1441 static JSObject jsify(object) native "JSObject_jsify"; |
1327 static JSObject newObject() native "JSObject_newObject"; | 1442 static JSObject newObject() native "JSObject_newObject"; |
1328 static JSArray newArray() native "JSObject_newArray"; | 1443 static JSArray newArray() native "JSObject_newArray"; |
1329 | 1444 |
1330 static hasProperty(_JSObjectBase o, name) => o._hasProperty(name); | 1445 static hasProperty(_JSObjectBase o, name) => o._hasProperty(name); |
1331 static getProperty(_JSObjectBase o, name) => o._operator_getter(name); | 1446 static getProperty(_JSObjectBase o, name) => o._operator_getter(name); |
1332 static setProperty(_JSObjectBase o, name, value) => o._operator_setter(name, v
alue); | 1447 static setProperty(_JSObjectBase o, name, value) => |
1333 static callMethod(_JSObjectBase o, String method, List args) => o._callMethod(
method, args); | 1448 o._operator_setter(name, value); |
1334 static instanceof(_JSObjectBase o, /*JsFunction|JSFunction*/ type) => o._insta
nceof(type); | 1449 static callMethod(_JSObjectBase o, String method, List args) => |
1335 static callConstructor0(_JSObjectBase constructor) native "JSNative_callConstr
uctor0"; | 1450 o._callMethod(method, args); |
1336 static callConstructor(_JSObjectBase constructor, List args) native "JSNative_
callConstructor"; | 1451 static instanceof(_JSObjectBase o, /*JsFunction|JSFunction*/ type) => |
| 1452 o._instanceof(type); |
| 1453 static callConstructor0(_JSObjectBase constructor) |
| 1454 native "JSNative_callConstructor0"; |
| 1455 static callConstructor(_JSObjectBase constructor, List args) |
| 1456 native "JSNative_callConstructor"; |
1337 | 1457 |
1338 static toTypedObject(JsObject o) native "JSNative_toTypedObject"; | 1458 static toTypedObject(JsObject o) native "JSNative_toTypedObject"; |
1339 | 1459 |
1340 /** | 1460 /** |
1341 * Same behavior as new JsFunction.withThis except that JavaScript "this" is n
ot | 1461 * Same behavior as new JsFunction.withThis except that JavaScript "this" is n
ot |
1342 * wrapped. | 1462 * wrapped. |
1343 */ | 1463 */ |
1344 static JSFunction withThis(Function f) native "JsFunction_withThisNoWrap"; | 1464 static JSFunction withThis(Function f) native "JsFunction_withThisNoWrap"; |
1345 } | 1465 } |
1346 | 1466 |
1347 /// Utility methods to efficiently manipulate typed JSInterop objects in cases | |
1348 /// where the name to call is not known at runtime. You should only use these | |
1349 /// methods when the same effect cannot be achieved with @JS annotations. | |
1350 /// These methods would be extension methods on JSObject if Dart supported | |
1351 /// extension methods. | |
1352 class JSNative { | |
1353 /** | |
1354 * WARNING: performance of this method is much worse than other methods | |
1355 * in JSNative. Only use this method as a last resort. | |
1356 * | |
1357 * Recursively converts a JSON-like collection of Dart objects to a | |
1358 * collection of JavaScript objects and returns a [JsObject] proxy to it. | |
1359 * | |
1360 * [object] must be a [Map] or [Iterable], the contents of which are also | |
1361 * converted. Maps and Iterables are copied to a new JavaScript object. | |
1362 * Primitives and other transferrable values are directly converted to their | |
1363 * JavaScript type, and all other objects are proxied. | |
1364 */ | |
1365 static jsify(object) { | |
1366 if ((object is! Map) && (object is! Iterable)) { | |
1367 throw new ArgumentError("object must be a Map or Iterable"); | |
1368 } | |
1369 return _jsify(object); | |
1370 } | |
1371 | |
1372 static _jsify(object) native "JSObject_jsify"; | |
1373 static JSObject newObject() native "JSObject_newObject"; | |
1374 | |
1375 static hasProperty(JSObject o, name) => o._hasProperty(name); | |
1376 static getProperty(JSObject o, name) => o._operator_getter(name); | |
1377 static setProperty(JSObject o, name, value) => o._operator_setter(name, value)
; | |
1378 static callMethod(JSObject o, String method, List args) => o._callMethod(metho
d, args); | |
1379 static instanceof(JSObject o, Function type) => o._instanceof(type); | |
1380 static callConstructor(JSObject constructor, List args) native "JSNative_callC
onstructor"; | |
1381 } | |
1382 | |
1383 /** | 1467 /** |
1384 * Proxies a JavaScript Function object. | 1468 * Proxies a JavaScript Function object. |
1385 */ | 1469 */ |
1386 class JsFunction extends JsObject { | 1470 class JsFunction extends JsObject { |
1387 JsFunction.internal() : super.internal(); | 1471 JsFunction.internal() : super.internal(); |
1388 | 1472 |
1389 /** | 1473 /** |
1390 * Returns a [JsFunction] that captures its 'this' binding and calls [f] | 1474 * Returns a [JsFunction] that captures its 'this' binding and calls [f] |
1391 * with the value of this passed as the first argument. | 1475 * with the value of this passed as the first argument. |
1392 */ | 1476 */ |
1393 factory JsFunction.withThis(Function f) => _withThis(f); | 1477 factory JsFunction.withThis(Function f) => _withThis(f); |
1394 | 1478 |
1395 /** | 1479 /** |
1396 * Invokes the JavaScript function with arguments [args]. If [thisArg] is | 1480 * Invokes the JavaScript function with arguments [args]. If [thisArg] is |
1397 * supplied it is the value of `this` for the invocation. | 1481 * supplied it is the value of `this` for the invocation. |
1398 */ | 1482 */ |
1399 dynamic apply(List args, {thisArg}) => | 1483 dynamic apply(List args, {thisArg}) => _apply(args, thisArg: thisArg); |
1400 _apply(args, thisArg: thisArg); | |
1401 | 1484 |
1402 dynamic _apply(List args, {thisArg}) native "JsFunction_apply"; | 1485 dynamic _apply(List args, {thisArg}) native "JsFunction_apply"; |
1403 | 1486 |
1404 /** | 1487 /** |
1405 * Internal only version of apply which uses debugger proxies of Dart objects | 1488 * Internal only version of apply which uses debugger proxies of Dart objects |
1406 * rather than opaque handles. This method is private because it cannot be | 1489 * rather than opaque handles. This method is private because it cannot be |
1407 * efficiently implemented in Dart2Js so should only be used by internal | 1490 * efficiently implemented in Dart2Js so should only be used by internal |
1408 * tools. | 1491 * tools. |
1409 */ | 1492 */ |
1410 _applyDebuggerOnly(List args, {thisArg}) | 1493 _applyDebuggerOnly(List args, {thisArg}) |
(...skipping 160 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1571 /// using the package:js Dart-JavaScript interop. | 1654 /// using the package:js Dart-JavaScript interop. |
1572 /// | 1655 /// |
1573 /// For performance reasons in Dart2Js, by default Dart functions cannot be | 1656 /// For performance reasons in Dart2Js, by default Dart functions cannot be |
1574 /// passed directly to JavaScript unless this method is called to create | 1657 /// passed directly to JavaScript unless this method is called to create |
1575 /// a Function compatible with both Dart and JavaScript. | 1658 /// a Function compatible with both Dart and JavaScript. |
1576 /// Calling this method repeatedly on a function will return the same function. | 1659 /// Calling this method repeatedly on a function will return the same function. |
1577 /// The [Function] returned by this method can be used from both Dart and | 1660 /// The [Function] returned by this method can be used from both Dart and |
1578 /// JavaScript. We may remove the need to call this method completely in the | 1661 /// JavaScript. We may remove the need to call this method completely in the |
1579 /// future if Dart2Js is refactored so that its function calling conventions | 1662 /// future if Dart2Js is refactored so that its function calling conventions |
1580 /// are more compatible with JavaScript. | 1663 /// are more compatible with JavaScript. |
1581 Function /*=F*/ allowInterop/*<F extends Function>*/(Function /*=F*/ f) { | 1664 Function/*=F*/ allowInterop/*<F extends Function>*/(Function/*=F*/ f) { |
1582 if (f is JSFunction) { | 1665 if (f is JSFunction) { |
1583 // The function is already a JSFunction... no need to do anything. | 1666 // The function is already a JSFunction... no need to do anything. |
1584 return f; | 1667 return f; |
1585 } else { | 1668 } else { |
1586 return JSFunction._create(f); | 1669 return JSFunction._create(f); |
1587 } | 1670 } |
1588 } | 1671 } |
1589 | 1672 |
1590 /// Cached JSFunction associated with the Dart function when "this" is | 1673 /// Cached JSFunction associated with the Dart function when "this" is |
1591 /// captured. | 1674 /// captured. |
(...skipping 14 matching lines...) Expand all Loading... |
1606 } else { | 1689 } else { |
1607 var ret = _interopCaptureThisExpando[f]; | 1690 var ret = _interopCaptureThisExpando[f]; |
1608 if (ret == null) { | 1691 if (ret == null) { |
1609 // TODO(jacobr): we could optimize this. | 1692 // TODO(jacobr): we could optimize this. |
1610 ret = JSFunction._createWithThis(f); | 1693 ret = JSFunction._createWithThis(f); |
1611 _interopCaptureThisExpando[f] = ret; | 1694 _interopCaptureThisExpando[f] = ret; |
1612 } | 1695 } |
1613 return ret; | 1696 return ret; |
1614 } | 1697 } |
1615 } | 1698 } |
OLD | NEW |