[dart2js] Cache globalThis

Feature-test for `self` and `globalThis`, and cache the result.

The cached access has smaller minified code size than either `self` or `globalThis`.

All older browsers that we have supported that don't have `globalThis`
do have `self`.

We prioritize `self` over `globalThis`. They are usually the same but it is possible that there are programs which run in an environment that has replaced `self`, for example, a test might do that. We don't want to break existing tests.

Bug: #59982
Change-Id: I11d19183d85a73e9552b66a2f49ac99be10c3c83
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/417912
Reviewed-by: Srujan Gaddam <srujzs@google.com>
Commit-Queue: Stephen Adams <sra@google.com>
This commit is contained in:
Stephen Adams
2025-03-27 15:33:43 -07:00
committed by Commit Queue
parent 82420ba66c
commit 2d21ca796a
7 changed files with 39 additions and 4 deletions
@@ -183,6 +183,10 @@ const STARTUP_METRICS = 'sm';
// TODO(51016): This might be moved to improve deferred loading.
const RECORD_TYPE_TEST_COMBINATORS_PROPERTY = 'rttc';
/// An embedded global that contains the value of `globalThis`. Unlike many
/// embedded globals, this one has many references, so we choose short name.
const CACHED_GLOBAL_THIS = 'G';
/// Names of fields of collected tear-off parameters object.
///
/// Tear-off getters are created before the Dart classes are initialized, so a