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 library dart._js_mirrors; | 5 library dart._js_mirrors; |
6 | 6 |
7 import 'dart:async'; | 7 import 'dart:async'; |
8 import 'dart:mirrors'; | 8 import 'dart:mirrors'; |
9 | 9 |
10 import 'dart:_foreign_helper' show JS, JS_CURRENT_ISOLATE; | 10 import 'dart:_foreign_helper' show JS, JS_CURRENT_ISOLATE; |
11 import 'dart:_collection-dev' as _symbol_dev; | 11 import 'dart:_collection-dev' as _symbol_dev; |
12 import 'dart:_js_helper' show | 12 import 'dart:_js_helper' show |
13 BoundClosure, | 13 BoundClosure, |
14 Closure, | 14 Closure, |
15 JSInvocationMirror, | 15 JSInvocationMirror, |
16 Null, | 16 Null, |
17 Primitives, | 17 Primitives, |
18 RuntimeError, | 18 RuntimeError, |
19 createInvocationMirror; | 19 createUnmangledInvocationMirror; |
20 import 'dart:_interceptors' show Interceptor; | 20 import 'dart:_interceptors' show Interceptor; |
| 21 import 'dart:_js_names'; |
21 | 22 |
22 /// No-op method that is called to inform the compiler that | 23 /// No-op method that is called to inform the compiler that |
23 /// tree-shaking needs to be disabled. | 24 /// tree-shaking needs to be disabled. |
24 disableTreeShaking() => preserveNames(); | 25 disableTreeShaking() => preserveNames(); |
25 | 26 |
26 /// No-op method that is called to inform the compiler that unmangled | |
27 /// named must be preserved. | |
28 preserveNames() {} | |
29 | |
30 String getName(Symbol symbol) { | 27 String getName(Symbol symbol) { |
31 preserveNames(); | 28 preserveNames(); |
32 return n(symbol); | 29 return n(symbol); |
33 } | 30 } |
34 | 31 |
35 final Map<String, String> mangledNames = JsMirrorSystem.computeMangledNames(); | |
36 | |
37 final Map<String, String> reflectiveNames = | |
38 JsMirrorSystem.computeReflectiveNames(); | |
39 | |
40 final Map<String, String> mangledGlobalNames = | |
41 JsMirrorSystem.computeMangledGlobalNames(); | |
42 | |
43 final Map<String, String> reflectiveGlobalNames = | |
44 JsMirrorSystem.computeReflectiveGlobalNames(); | |
45 | |
46 class JsMirrorSystem implements MirrorSystem { | 32 class JsMirrorSystem implements MirrorSystem { |
47 TypeMirror get dynamicType => _dynamicType; | 33 TypeMirror get dynamicType => _dynamicType; |
48 TypeMirror get voidType => _voidType; | 34 TypeMirror get voidType => _voidType; |
49 | 35 |
50 final static TypeMirror _dynamicType = | 36 final static TypeMirror _dynamicType = |
51 new JsTypeMirror(const Symbol('dynamic')); | 37 new JsTypeMirror(const Symbol('dynamic')); |
52 final static TypeMirror _voidType = new JsTypeMirror(const Symbol('void')); | 38 final static TypeMirror _voidType = new JsTypeMirror(const Symbol('void')); |
53 | 39 |
54 static final Map<String, List<LibraryMirror>> librariesByName = | 40 static final Map<String, List<LibraryMirror>> librariesByName = |
55 computeLibrariesByName(); | 41 computeLibrariesByName(); |
(...skipping 14 matching lines...) Expand all Loading... |
70 List<String> functions = data[3]; | 56 List<String> functions = data[3]; |
71 var metadataFunction = data[4]; | 57 var metadataFunction = data[4]; |
72 List metadata = (metadataFunction == null) | 58 List metadata = (metadataFunction == null) |
73 ? null : JS('List', '#()', metadataFunction); | 59 ? null : JS('List', '#()', metadataFunction); |
74 var libraries = result.putIfAbsent(name, () => <LibraryMirror>[]); | 60 var libraries = result.putIfAbsent(name, () => <LibraryMirror>[]); |
75 libraries.add( | 61 libraries.add( |
76 new JsLibraryMirror(s(name), uri, classes, functions, metadata)); | 62 new JsLibraryMirror(s(name), uri, classes, functions, metadata)); |
77 } | 63 } |
78 return result; | 64 return result; |
79 } | 65 } |
80 | |
81 static Map<String, String> computeMangledNames() { | |
82 disableTreeShaking(); | |
83 var mangledNames = JS('', 'init.mangledNames'); | |
84 var keys = extractKeys(mangledNames); | |
85 var result = <String, String>{}; | |
86 for (String key in keys) { | |
87 result[key] = JS('String', '#[#]', mangledNames, key); | |
88 } | |
89 return result; | |
90 } | |
91 | |
92 static Map<String, String> computeReflectiveNames() { | |
93 disableTreeShaking(); | |
94 var result = <String, String>{}; | |
95 mangledNames.forEach((String mangledName, String reflectiveName) { | |
96 result[reflectiveName] = mangledName; | |
97 }); | |
98 return result; | |
99 } | |
100 | |
101 static Map<String, String> computeMangledGlobalNames() { | |
102 disableTreeShaking(); | |
103 var mangledGlobalNames = JS('', 'init.mangledGlobalNames'); | |
104 var keys = extractKeys(mangledGlobalNames); | |
105 var result = <String, String>{}; | |
106 for (String key in keys) { | |
107 result[key] = JS('String', '#[#]', mangledGlobalNames, key); | |
108 } | |
109 return result; | |
110 } | |
111 | |
112 static Map<String, String> computeReflectiveGlobalNames() { | |
113 disableTreeShaking(); | |
114 var result = <String, String>{}; | |
115 mangledGlobalNames.forEach((String mangledName, String reflectiveName) { | |
116 result[reflectiveName] = mangledName; | |
117 }); | |
118 return result; | |
119 } | |
120 } | 66 } |
121 | 67 |
122 abstract class JsMirror { | 68 abstract class JsMirror { |
123 const JsMirror(); | 69 const JsMirror(); |
124 | 70 |
125 abstract String get _prettyName; | 71 abstract String get _prettyName; |
126 | 72 |
127 String toString() => _prettyName; | 73 String toString() => _prettyName; |
128 } | 74 } |
129 | 75 |
(...skipping 216 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
346 var jsList = new List.from(positionalArguments); | 292 var jsList = new List.from(positionalArguments); |
347 String reflectiveName = '${n(memberName)}:${positionalArguments.length}:0'; | 293 String reflectiveName = '${n(memberName)}:${positionalArguments.length}:0'; |
348 String mangledName = reflectiveNames[reflectiveName]; | 294 String mangledName = reflectiveNames[reflectiveName]; |
349 return _invoke(memberName, JSInvocationMirror.METHOD, mangledName, jsList); | 295 return _invoke(memberName, JSInvocationMirror.METHOD, mangledName, jsList); |
350 } | 296 } |
351 | 297 |
352 InstanceMirror _invoke(Symbol name, | 298 InstanceMirror _invoke(Symbol name, |
353 int type, | 299 int type, |
354 String mangledName, | 300 String mangledName, |
355 List arguments) { | 301 List arguments) { |
| 302 disableTreeShaking(); |
356 // TODO(ahe): Get the argument names. | 303 // TODO(ahe): Get the argument names. |
357 List<String> argumentNames = []; | 304 List<String> argumentNames = []; |
358 Invocation invocation = createInvocationMirror( | 305 Invocation invocation = createUnmangledInvocationMirror( |
359 n(name), mangledName, type, arguments, argumentNames); | 306 name, mangledName, type, arguments, argumentNames); |
360 | 307 |
361 return reflect(delegate(invocation)); | 308 return reflect(delegate(invocation)); |
362 } | 309 } |
363 | 310 |
364 InstanceMirror setField(Symbol fieldName, Object arg) { | 311 InstanceMirror setField(Symbol fieldName, Object arg) { |
365 String reflectiveName = '${n(fieldName)}='; | 312 String reflectiveName = '${n(fieldName)}='; |
366 String mangledName = reflectiveNames[reflectiveName]; | 313 String mangledName = reflectiveNames[reflectiveName]; |
367 _invoke(s(reflectiveName), JSInvocationMirror.SETTER, mangledName, [arg]); | 314 _invoke(s(reflectiveName), JSInvocationMirror.SETTER, mangledName, [arg]); |
368 return reflect(arg); | 315 return reflect(arg); |
369 } | 316 } |
(...skipping 398 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
768 String ownerName = n(owner.qualifiedName); | 715 String ownerName = n(owner.qualifiedName); |
769 if (ownerName == '') return simpleName; | 716 if (ownerName == '') return simpleName; |
770 return s('$ownerName.${n(simpleName)}'); | 717 return s('$ownerName.${n(simpleName)}'); |
771 } | 718 } |
772 | 719 |
773 List extractMetadata(victim) { | 720 List extractMetadata(victim) { |
774 var metadataFunction = JS('', '#["@"]', victim); | 721 var metadataFunction = JS('', '#["@"]', victim); |
775 return (metadataFunction == null) | 722 return (metadataFunction == null) |
776 ? const [] : JS('', '#()', metadataFunction); | 723 ? const [] : JS('', '#()', metadataFunction); |
777 } | 724 } |
778 | |
779 List extractKeys(victim) { | |
780 return JS('List', ''' | |
781 (function(victim, hasOwnProperty) { | |
782 var result = []; | |
783 for (var key in victim) { | |
784 if (hasOwnProperty.call(victim, key)) result.push(key); | |
785 } | |
786 return result; | |
787 })(#, Object.prototype.hasOwnProperty)''', victim); | |
788 } | |
OLD | NEW |