[dart2js] Remove vestiges of reflection
Change-Id: I8d6beadc6e1d6b4525d810308957ca5527f19eb5
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/195463
Commit-Queue: Stephen Adams <sra@google.com>
Reviewed-by: Sigmund Cherem <sigmund@google.com>
diff --git a/sdk/lib/_internal/js_runtime/lib/js_helper.dart b/sdk/lib/_internal/js_runtime/lib/js_helper.dart
index a5e1837..cfaaeee 100644
--- a/sdk/lib/_internal/js_runtime/lib/js_helper.dart
+++ b/sdk/lib/_internal/js_runtime/lib/js_helper.dart
@@ -54,11 +54,7 @@
import 'dart:_native_typed_data';
-import 'dart:_js_names'
- show
- extractKeys,
- unmangleGlobalNameIfPreservedAnyways,
- unmangleAllIdentifiersIfPreservedAnyways;
+import 'dart:_js_names' show unmangleGlobalNameIfPreservedAnyways;
import 'dart:_rti' as newRti
show
@@ -1003,29 +999,6 @@
}
}
-/// Helper class for allocating and using JS object literals as caches.
-class JsCache {
- /// Returns a JavaScript object suitable for use as a cache.
- static allocate() {
- var result = JS('=Object', 'Object.create(null)');
- // Deleting a property makes V8 assume that it shouldn't create a hidden
- // class for [result] and map transitions. Although these map transitions
- // pay off if there are many cache hits for the same keys, it becomes
- // really slow when there aren't many repeated hits.
- JS('void', '#.x=0', result);
- JS('void', 'delete #.x', result);
- return result;
- }
-
- static fetch(cache, String key) {
- return JS('', '#[#]', cache, key);
- }
-
- static void update(cache, String key, value) {
- JS('void', '#[#] = #', cache, key, value);
- }
-}
-
/// Called by generated code to throw an illegal-argument exception,
/// for example, if a non-integer index is given to an optimized
/// indexed access.
diff --git a/sdk/lib/_internal/js_runtime/lib/js_names.dart b/sdk/lib/_internal/js_runtime/lib/js_names.dart
index 10efacf..678df00 100644
--- a/sdk/lib/_internal/js_runtime/lib/js_names.dart
+++ b/sdk/lib/_internal/js_runtime/lib/js_names.dart
@@ -4,164 +4,21 @@
library dart._js_names;
-import 'dart:_js_embedded_names'
- show JsGetName, MANGLED_GLOBAL_NAMES, MANGLED_NAMES;
+import 'dart:_js_embedded_names' show MANGLED_GLOBAL_NAMES;
-import 'dart:_foreign_helper' show JS, JS_EMBEDDED_GLOBAL, JS_GET_NAME;
+import 'dart:_foreign_helper' show JS, JS_EMBEDDED_GLOBAL;
-import 'dart:_js_helper' show JsCache, NoInline;
-
-import 'dart:_interceptors' show JSArray;
-
-/// No-op method that is called to inform the compiler that unmangled named
-/// must be preserved.
-preserveNames() {}
-
-/// A map from mangled names to "reflective" names, that is, unmangled names
-/// with some additional information, such as, number of required arguments.
-/// This map is for mangled names used as instance members.
-final _LazyMangledNamesMap mangledNames = new _LazyMangledInstanceNamesMap(
- JS_EMBEDDED_GLOBAL('=Object', MANGLED_NAMES));
-
-/// A map from "reflective" names to mangled names (the reverse of
-/// [mangledNames]).
-final _LazyReflectiveNamesMap reflectiveNames = new _LazyReflectiveNamesMap(
- JS_EMBEDDED_GLOBAL('=Object', MANGLED_NAMES), true);
-
-/// A map from mangled names to "reflective" names (see [mangledNames]). This
-/// map is for globals, that is, static and top-level members.
-final _LazyMangledNamesMap mangledGlobalNames = new _LazyMangledNamesMap(
- JS_EMBEDDED_GLOBAL('=Object', MANGLED_GLOBAL_NAMES));
-
-/// A map from "reflective" names to mangled names (the reverse of
-/// [mangledGlobalNames]).
-final _LazyReflectiveNamesMap reflectiveGlobalNames =
- new _LazyReflectiveNamesMap(
- JS_EMBEDDED_GLOBAL('=Object', MANGLED_GLOBAL_NAMES), false);
-
-/// Implements a mapping from mangled names to their reflective counterparts.
-/// The propertiy names of [_jsMangledNames] are the mangled names, and the
-/// values are the "reflective" names.
-class _LazyMangledNamesMap {
- /// [_jsMangledNames] is a JavaScript object literal.
- var _jsMangledNames;
-
- _LazyMangledNamesMap(this._jsMangledNames);
-
- String? operator [](String key) {
- var result = JS('var', '#[#]', _jsMangledNames, key);
- // Filter out all non-string values to protect against polution from
- // ancillary fields in [_jsMangledNames].
- bool filter = JS('bool', 'typeof # !== "string"', result);
- // To ensure that the inferrer sees that result is a String, we explicitly
- // give it a better type here.
- return filter ? null : JS('String', '#', result);
- }
-}
-
-/// Extends [_LazyMangledNamesMap] with additional support for adding mappings
-/// from mangled setter names to their reflective counterpart by rewriting a
-/// corresponding entry for a getter name, if it exists.
-class _LazyMangledInstanceNamesMap extends _LazyMangledNamesMap {
- _LazyMangledInstanceNamesMap(_jsMangledNames) : super(_jsMangledNames);
-
- String? operator [](String key) {
- String? result = super[key];
- String setterPrefix = JS_GET_NAME(JsGetName.SETTER_PREFIX);
- if (result == null && key.startsWith(setterPrefix)) {
- String getterPrefix = JS_GET_NAME(JsGetName.GETTER_PREFIX);
- int setterPrefixLength = setterPrefix.length;
-
- // Generate the setter name from the getter name.
- key = '$getterPrefix${key.substring(setterPrefixLength)}';
- result = super[key];
- return (result != null) ? "${result}=" : null;
- }
- return result;
- }
-}
-
-/// Implements the inverse of [_LazyMangledNamesMap]. As it would be too
-/// expensive to search the mangled names map for a value that corresponds to
-/// the lookup key on each invocation, we compute the full mapping in demand
-/// and cache it. The cache is invalidated when the underlying [_jsMangledNames]
-/// object changes its length. This condition is sufficient as the name mapping
-/// can only grow over time.
-/// When [_isInstance] is true, we also apply the inverse of the setter/getter
-/// name conversion implemented by [_LazyMangledInstanceNamesMap].
-class _LazyReflectiveNamesMap {
- /// [_jsMangledNames] is a JavaScript object literal.
- final _jsMangledNames;
- final bool _isInstance;
- int _cacheLength = 0;
- Map<String, String>? _cache;
-
- _LazyReflectiveNamesMap(this._jsMangledNames, this._isInstance);
-
- Map<String, String> _updateReflectiveNames() {
- preserveNames();
- Map<String, String> result = <String, String>{};
- List keys = JS('List', 'Object.keys(#)', _jsMangledNames);
- for (String key in keys) {
- var reflectiveName = JS('var', '#[#]', _jsMangledNames, key);
- // Filter out all non-string values to protect against polution from
- // ancillary fields in [_jsMangledNames].
- bool filter = JS('bool', 'typeof # !== "string"', reflectiveName);
- if (filter) continue;
- result[reflectiveName] = JS('String', '#', key);
-
- String getterPrefix = JS_GET_NAME(JsGetName.GETTER_PREFIX);
- if (_isInstance && key.startsWith(getterPrefix)) {
- int getterPrefixLength = getterPrefix.length;
- String setterPrefix = JS_GET_NAME(JsGetName.SETTER_PREFIX);
- result['$reflectiveName='] =
- '$setterPrefix${key.substring(getterPrefixLength)}';
- }
- }
- return result;
- }
-
- int get _jsMangledNamesLength =>
- JS('int', 'Object.keys(#).length', _jsMangledNames);
-
- String? operator [](String key) {
- if (_cache == null || _jsMangledNamesLength != _cacheLength) {
- _cache = _updateReflectiveNames();
- _cacheLength = _jsMangledNamesLength;
- }
- return _cache![key];
- }
-}
-
-@pragma('dart2js:noInline')
-List extractKeys(victim) {
- var result = JS('', '# ? Object.keys(#) : []', victim, victim);
- return new JSArray.markFixed(result);
-}
-
-/// Returns the (global) unmangled version of [name].
+/// Returns the (global) unminified version of [name], or (usual case) `null` if
+/// the name is not known.
///
-/// Normally, you should use [mangledGlobalNames] directly, but this method
-/// doesn't tell the compiler to preserve names. So this method only returns a
-/// non-null value if some other component has made the compiler preserve names.
-///
-/// This is used, for example, to return unmangled names from TypeImpl.toString
-/// *if* names are being preserved for other reasons (use of dart:mirrors, for
-/// example).
+/// The generated app contains a small table that translates a few minified
+/// names to their unminified text. This is used to return unminified names in
+/// some parts of Type.toString. Historically a much more comprehensive and
+/// large table was generated to support 'dart:mirrors', but 'dart:mirrors' is
+/// no longer supported on the web platforms, in part due to the size of tables
+/// like this. The names included are chosen by the emitter, but limited to a
+/// few primitives and `List`.
String? unmangleGlobalNameIfPreservedAnyways(String name) {
var names = JS_EMBEDDED_GLOBAL('', MANGLED_GLOBAL_NAMES);
- return JS('String|Null', '#', JsCache.fetch(names, name));
-}
-
-String unmangleAllIdentifiersIfPreservedAnyways(String str) {
- return JS(
- 'String',
- r'''
- (function(str, names) {
- return str.replace(
- /[^<,> ]+/g,
- function(m) { return names[m] || m; });
- })(#, #)''',
- str,
- JS_EMBEDDED_GLOBAL('', MANGLED_GLOBAL_NAMES));
+ return JS('String|Null', '#[#]', names, name);
}