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:_foreign_helper' show JS; |
| 8 |
| 9 /// No-op method that is called to inform the compiler that unmangled named |
| 10 /// must be preserved. |
| 11 preserveNames() {} |
| 12 |
| 13 /// A map from mangled names to "reflective" names, that is, unmangled names |
| 14 /// with some additional information, such as, number of required arguments. |
| 15 /// This map is for mangled names used as instance members. |
| 16 final Map<String, String> mangledNames = |
| 17 computeMangledNames(JS('', 'init.mangledNames')); |
| 18 |
| 19 /// A map from "reflective" names to mangled names (the reverse of |
| 20 /// [mangledNames]). |
| 21 final Map<String, String> reflectiveNames = |
| 22 computeReflectiveNames(mangledNames); |
| 23 |
| 24 /// A map from mangled names to "reflective" names (see [mangledNames]). This |
| 25 /// map is for globals, that is, static and top-level members. |
| 26 final Map<String, String> mangledGlobalNames = |
| 27 computeMangledNames(JS('', 'init.mangledGlobalNames')); |
| 28 |
| 29 /// A map from "reflective" names to mangled names (the reverse of |
| 30 /// [mangledGlobalNames]). |
| 31 final Map<String, String> reflectiveGlobalNames = |
| 32 computeReflectiveNames(mangledGlobalNames); |
| 33 |
| 34 /// [jsMangledNames] is a JavaScript object literal. The keys are the mangled |
| 35 /// names, and the values are the "reflective" names. |
| 36 Map<String, String> computeMangledNames(jsMangledNames) { |
| 37 preserveNames(); |
| 38 var keys = extractKeys(jsMangledNames); |
| 39 var result = <String, String>{}; |
| 40 for (String key in keys) { |
| 41 result[key] = JS('String', '#[#]', jsMangledNames, key); |
| 42 } |
| 43 return result; |
| 44 } |
| 45 |
| 46 Map<String, String> computeReflectiveNames(Map<String, String> map) { |
| 47 preserveNames(); |
| 48 var result = <String, String>{}; |
| 49 map.forEach((String mangledName, String reflectiveName) { |
| 50 result[reflectiveName] = mangledName; |
| 51 }); |
| 52 return result; |
| 53 } |
| 54 |
| 55 List extractKeys(victim) { |
| 56 return JS('List', ''' |
| 57 (function(victim, hasOwnProperty) { |
| 58 var result = []; |
| 59 for (var key in victim) { |
| 60 if (hasOwnProperty.call(victim, key)) result.push(key); |
| 61 } |
| 62 return result; |
| 63 })(#, Object.prototype.hasOwnProperty)''', victim); |
| 64 } |
OLD | NEW |