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 264 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
275 .putIfAbsent(symbol, () => new _DeclarationSet()) | 275 .putIfAbsent(symbol, () => new _DeclarationSet()) |
276 .add(declaration); | 276 .add(declaration); |
277 } | 277 } |
278 } | 278 } |
279 }); | 279 }); |
280 } | 280 } |
281 } | 281 } |
282 | 282 |
283 _finalizeJsInterfaces() native "Js_finalizeJsInterfaces"; | 283 _finalizeJsInterfaces() native "Js_finalizeJsInterfaces"; |
284 | 284 |
285 // Create the files for the generated Dart files from IDLs. These only change | |
286 // when browser's Dart files change (dart:*). | |
287 @Deprecated("Internal Use Only") | |
288 String createCachedPatchesFile() { | |
289 var patches = _generateInteropPatchFiles(['dart:html', | |
290 'dart:indexed_db', | |
291 'dart:web_gl', | |
292 'dart:web_sql', | |
293 'dart:svg', | |
294 'dart:web_audio']); | |
295 var sb = new StringBuffer(); | |
296 | |
297 sb.write(""" | |
298 | |
299 // START_OF_CACHED_PATCHES | |
300 // Copyright (c) 2013, the Dart project authors. Please see the AUTHORS file | |
301 // for details. All rights reserved. Use of this source code is governed by a | |
302 // BSD-style license that can be found in the LICENSE file. | |
303 | |
304 // DO NOT EDIT GENERATED FILE. | |
305 | |
306 library cached_patches; | |
307 | |
308 var cached_patches = {"""); | |
309 | |
310 for (var baseIndex = 0; baseIndex < patches.length; baseIndex += 3) { | |
311 var uri = patches[baseIndex + 0]; | |
312 var uri_js_interop = patches[baseIndex + 1]; | |
313 var source = patches[baseIndex + 2]; | |
314 | |
315 if (uri != 'dart:js') { | |
316 sb.write('"$uri": ["$uri", "$uri_js_interop", """$source"""],'); | |
317 } | |
318 } | |
319 | |
320 sb.write("""}; | |
321 // END_OF_CACHED_PATCHES | |
322 """); | |
323 | |
324 return "$sb"; | |
325 } | |
326 | |
327 String _getJsName(mirrors.DeclarationMirror mirror) { | 285 String _getJsName(mirrors.DeclarationMirror mirror) { |
328 if (_atJsType != null) { | 286 if (_atJsType != null) { |
329 for (var annotation in mirror.metadata) { | 287 for (var annotation in mirror.metadata) { |
330 if (annotation.type.reflectedType == _atJsType) { | 288 if (annotation.type.reflectedType == _atJsType) { |
331 try { | 289 try { |
332 var name = annotation.reflectee.name; | 290 var name = annotation.reflectee.name; |
333 return name != null ? name : ""; | 291 return name != null ? name : ""; |
334 } catch (e) {} | 292 } catch (e) {} |
335 } | 293 } |
336 } | 294 } |
(...skipping 155 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
492 } | 450 } |
493 | 451 |
494 bool _isExternal(mirrors.MethodMirror mirror) { | 452 bool _isExternal(mirrors.MethodMirror mirror) { |
495 // This try-catch block is a workaround for BUG:24834. | 453 // This try-catch block is a workaround for BUG:24834. |
496 try { | 454 try { |
497 return mirror.isExternal; | 455 return mirror.isExternal; |
498 } catch (e) {} | 456 } catch (e) {} |
499 return false; | 457 return false; |
500 } | 458 } |
501 | 459 |
502 List<String> _generateExternalMethods(List<String> libraryPaths) { | 460 List<String> _generateExternalMethods(List<String> libraryPaths, bool useCachedP
atches) { |
503 var staticCodegen = <String>[]; | 461 var staticCodegen = <String>[]; |
504 | 462 |
505 if (libraryPaths.length == 0) { | 463 if (libraryPaths.length == 0) { |
506 mirrors.currentMirrorSystem().libraries.forEach((uri, library) { | 464 mirrors.currentMirrorSystem().libraries.forEach((uri, library) { |
507 var library_name = "${uri.scheme}:${uri.path}"; | 465 var library_name = "${uri.scheme}:${uri.path}"; |
508 if (cached_patches.containsKey(library_name)) { | 466 if (useCachedPatches && cached_patches.containsKey(library_name)) { |
509 // Use the pre-generated patch files for DOM dart:nnnn libraries. | 467 // Use the pre-generated patch files for DOM dart:nnnn libraries. |
510 var patch = cached_patches[library_name]; | 468 var patch = cached_patches[library_name]; |
511 staticCodegen.addAll(patch); | 469 staticCodegen.addAll(patch); |
512 } else if (_hasJsName(library)) { | 470 } else if (_hasJsName(library)) { |
513 // Library marked with @JS | 471 // Library marked with @JS |
514 _generateLibraryCodegen(uri, library, staticCodegen); | 472 _generateLibraryCodegen(uri, library, staticCodegen); |
| 473 } else if (!useCachedPatches) { |
| 474 // Can't use the cached patches file, instead this is a signal to genera
te |
| 475 // the patches for this file. |
| 476 _generateLibraryCodegen(uri, library, staticCodegen); |
515 } | 477 } |
516 }); // End of library foreach | 478 }); // End of library foreach |
517 } else { | 479 } else { |
518 // Used to generate cached_patches.dart file for all IDL generated dart: | 480 // Used to generate cached_patches.dart file for all IDL generated dart: |
519 // files to the WebKit DOM. | 481 // files to the WebKit DOM. |
520 for (var library_name in libraryPaths) { | 482 for (var library_name in libraryPaths) { |
521 var parts = library_name.split(':'); | 483 var parts = library_name.split(':'); |
522 var uri = new Uri(scheme: parts[0], path: parts[1]); | 484 var uri = new Uri(scheme: parts[0], path: parts[1]); |
523 var library = mirrors.currentMirrorSystem().libraries[uri]; | 485 var library = mirrors.currentMirrorSystem().libraries[uri]; |
524 _generateLibraryCodegen(uri, library, staticCodegen); | 486 _generateLibraryCodegen(uri, library, staticCodegen); |
(...skipping 144 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
669 } | 631 } |
670 } | 632 } |
671 | 633 |
672 // Remember the @JS type to compare annotation type. | 634 // Remember the @JS type to compare annotation type. |
673 var _atJsType = -1; | 635 var _atJsType = -1; |
674 | 636 |
675 /** | 637 /** |
676 * Generates part files defining source code for JSObjectImpl, all DOM classes | 638 * Generates part files defining source code for JSObjectImpl, all DOM classes |
677 * classes. This codegen is needed so that type checks for all registered | 639 * classes. This codegen is needed so that type checks for all registered |
678 * JavaScript interop classes pass. | 640 * JavaScript interop classes pass. |
| 641 * If genCachedPatches is true then the patch files don't exist this is a specia
l |
| 642 * signal to generate and emit the patches to stdout to be captured and put into |
| 643 * the file sdk/lib/js/dartium/cached_patches.dart |
679 */ | 644 */ |
680 List<String> _generateInteropPatchFiles(List<String> libraryPaths) { | 645 List<String> _generateInteropPatchFiles(List<String> libraryPaths, genCachedPatc
hes) { |
681 // Cache the @JS Type. | 646 // Cache the @JS Type. |
682 if (_atJsType == -1) { | 647 if (_atJsType == -1) { |
683 var uri = new Uri(scheme: "package", path: "js/js.dart"); | 648 var uri = new Uri(scheme: "package", path: "js/js.dart"); |
684 var jsLibrary = mirrors.currentMirrorSystem().libraries[uri]; | 649 var jsLibrary = mirrors.currentMirrorSystem().libraries[uri]; |
685 if (jsLibrary != null) { | 650 if (jsLibrary != null) { |
686 // @ JS used somewhere. | 651 // @ JS used somewhere. |
687 var jsDeclaration = jsLibrary.declarations[new Symbol("JS")]; | 652 var jsDeclaration = jsLibrary.declarations[new Symbol("JS")]; |
688 _atJsType = jsDeclaration.reflectedType; | 653 _atJsType = jsDeclaration.reflectedType; |
689 } else { | 654 } else { |
690 // @ JS not used in any library. | 655 // @ JS not used in any library. |
691 _atJsType = null; | 656 _atJsType = null; |
692 } | 657 } |
693 } | 658 } |
694 | 659 |
695 var ret = _generateExternalMethods(libraryPaths); | 660 var ret = _generateExternalMethods(libraryPaths, genCachedPatches ? false : tr
ue); |
696 var libraryPrefixes = new Map<mirrors.LibraryMirror, String>(); | 661 var libraryPrefixes = new Map<mirrors.LibraryMirror, String>(); |
697 var prefixNames = new Set<String>(); | 662 var prefixNames = new Set<String>(); |
698 var sb = new StringBuffer(); | 663 var sb = new StringBuffer(); |
699 | 664 |
700 var implements = <String>[]; | 665 var implements = <String>[]; |
701 var implementsArray = <String>[]; | 666 var implementsArray = <String>[]; |
702 var implementsDom = <String>[]; | 667 var implementsDom = <String>[]; |
703 var listMirror = mirrors.reflectType(List); | 668 var listMirror = mirrors.reflectType(List); |
704 var functionMirror = mirrors.reflectType(Function); | 669 var functionMirror = mirrors.reflectType(Function); |
705 var jsObjectMirror = mirrors.reflectType(JSObject); | 670 var jsObjectMirror = mirrors.reflectType(JSObject); |
(...skipping 873 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1579 if (ret == null) { | 1544 if (ret == null) { |
1580 // TODO(jacobr): we could optimize this. | 1545 // TODO(jacobr): we could optimize this. |
1581 ret = JSFunction._createWithThis(f); | 1546 ret = JSFunction._createWithThis(f); |
1582 _interopCaptureThisExpando[f] = ret; | 1547 _interopCaptureThisExpando[f] = ret; |
1583 } | 1548 } |
1584 return ret; | 1549 return ret; |
1585 } | 1550 } |
1586 } | 1551 } |
1587 | 1552 |
1588 debugPrint(_) {} | 1553 debugPrint(_) {} |
OLD | NEW |