| OLD | NEW |
| (Empty) |
| 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 | |
| 3 // BSD-style license that can be found in the LICENSE file. | |
| 4 | |
| 5 library dart._js_names; | |
| 6 | |
| 7 import 'dart:_js_embedded_names' show | |
| 8 MANGLED_GLOBAL_NAMES, | |
| 9 MANGLED_NAMES; | |
| 10 | |
| 11 import 'dart:_foreign_helper' show | |
| 12 JS, | |
| 13 JS_EMBEDDED_GLOBAL, | |
| 14 JS_GET_NAME; | |
| 15 | |
| 16 import 'dart:_js_helper' show | |
| 17 JsCache, | |
| 18 NoInline; | |
| 19 | |
| 20 import 'dart:_interceptors' show JSArray; | |
| 21 | |
| 22 /// No-op method that is called to inform the compiler that unmangled named | |
| 23 /// must be preserved. | |
| 24 preserveNames() {} | |
| 25 | |
| 26 /// A map from mangled names to "reflective" names, that is, unmangled names | |
| 27 /// with some additional information, such as, number of required arguments. | |
| 28 /// This map is for mangled names used as instance members. | |
| 29 final Map<String, String> mangledNames = | |
| 30 computeMangledNames( | |
| 31 JS_EMBEDDED_GLOBAL('=Object', MANGLED_NAMES), | |
| 32 false); | |
| 33 | |
| 34 /// A map from "reflective" names to mangled names (the reverse of | |
| 35 /// [mangledNames]). | |
| 36 final Map<String, String> reflectiveNames = | |
| 37 computeReflectiveNames(mangledNames); | |
| 38 | |
| 39 /// A map from mangled names to "reflective" names (see [mangledNames]). This | |
| 40 /// map is for globals, that is, static and top-level members. | |
| 41 final Map<String, String> mangledGlobalNames = computeMangledNames( | |
| 42 JS_EMBEDDED_GLOBAL('=Object', MANGLED_GLOBAL_NAMES), | |
| 43 true); | |
| 44 | |
| 45 /// A map from "reflective" names to mangled names (the reverse of | |
| 46 /// [mangledGlobalNames]). | |
| 47 final Map<String, String> reflectiveGlobalNames = | |
| 48 computeReflectiveNames(mangledGlobalNames); | |
| 49 | |
| 50 /// [jsMangledNames] is a JavaScript object literal. The keys are the mangled | |
| 51 /// names, and the values are the "reflective" names. | |
| 52 Map<String, String> computeMangledNames(jsMangledNames, bool isGlobal) { | |
| 53 preserveNames(); | |
| 54 var keys = extractKeys(jsMangledNames); | |
| 55 var result = <String, String>{}; | |
| 56 String getterPrefix = JS_GET_NAME('GETTER_PREFIX'); | |
| 57 int getterPrefixLength = getterPrefix.length; | |
| 58 String setterPrefix = JS_GET_NAME('SETTER_PREFIX'); | |
| 59 for (String key in keys) { | |
| 60 String value = JS('String', '#[#]', jsMangledNames, key); | |
| 61 result[key] = value; | |
| 62 if (!isGlobal) { | |
| 63 if (key.startsWith(getterPrefix)) { | |
| 64 result['$setterPrefix${key.substring(getterPrefixLength)}'] = '$value='; | |
| 65 } | |
| 66 } | |
| 67 } | |
| 68 return result; | |
| 69 } | |
| 70 | |
| 71 Map<String, String> computeReflectiveNames(Map<String, String> map) { | |
| 72 preserveNames(); | |
| 73 var result = <String, String>{}; | |
| 74 map.forEach((String mangledName, String reflectiveName) { | |
| 75 result[reflectiveName] = mangledName; | |
| 76 }); | |
| 77 return result; | |
| 78 } | |
| 79 | |
| 80 @NoInline() | |
| 81 List extractKeys(victim) { | |
| 82 var result = JS('', ''' | |
| 83 (function(victim, hasOwnProperty) { | |
| 84 var result = []; | |
| 85 for (var key in victim) { | |
| 86 if (hasOwnProperty.call(victim, key)) result.push(key); | |
| 87 } | |
| 88 return result; | |
| 89 })(#, Object.prototype.hasOwnProperty)''', victim); | |
| 90 return new JSArray.markFixed(result); | |
| 91 } | |
| 92 | |
| 93 /** | |
| 94 * Returns the (global) unmangled version of [name]. | |
| 95 * | |
| 96 * Normally, you should use [mangledGlobalNames] directly, but this method | |
| 97 * doesn't tell the compiler to preserve names. So this method only returns a | |
| 98 * non-null value if some other component has made the compiler preserve names. | |
| 99 * | |
| 100 * This is used, for example, to return unmangled names from TypeImpl.toString | |
| 101 * *if* names are being preserved for other reasons (use of dart:mirrors, for | |
| 102 * example). | |
| 103 */ | |
| 104 String unmangleGlobalNameIfPreservedAnyways(String name) { | |
| 105 var names = JS_EMBEDDED_GLOBAL('=Object', MANGLED_GLOBAL_NAMES); | |
| 106 return JsCache.fetch(names, name); | |
| 107 } | |
| 108 | |
| 109 String unmangleAllIdentifiersIfPreservedAnyways(String str) { | |
| 110 return JS("String", | |
| 111 r"(#).replace(/[^<,> ]+/g," | |
| 112 r"function(m) { return #[m] || m; })", | |
| 113 str, | |
| 114 JS_EMBEDDED_GLOBAL('', MANGLED_GLOBAL_NAMES)); | |
| 115 } | |
| OLD | NEW |