From 444982f364d1926c50a07974e6b779b1e7151aa5 Mon Sep 17 00:00:00 2001 From: Nicholas Shahan Date: Mon, 23 May 2022 18:04:10 +0000 Subject: [PATCH] [web] Share more runtime code between ddc and dart2js Creates a new 'dart:_js_shared_embedded_names' library for the names accessed from the shared 'dart:_rti' library. Migrate all of the shared symbols and uses to the new location. Change-Id: Iaa72c4522888ad630782b921b0b70d7a2626d1b9 Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/241507 Reviewed-by: Sigmund Cherem Reviewed-by: Mayank Patke Commit-Queue: Nicholas Shahan --- pkg/compiler/lib/src/common/elements.dart | 4 +- pkg/compiler/lib/src/common/names.dart | 4 + pkg/compiler/lib/src/js_backend/namer.dart | 2 +- .../startup_emitter/model_emitter.dart | 13 +- .../lib/src/kernel/dart2js_target.dart | 2 + .../lib/src/kernel/element_map_impl.dart | 2 +- pkg/compiler/lib/src/ssa/builder.dart | 2 + pkg/js_runtime/lib/synced/embedded_names.dart | 117 ------------------ pkg/js_shared/lib/synced/embedded_names.dart | 116 +++++++++++++++++ .../js_runtime/lib/foreign_helper.dart | 9 +- .../_internal/js_runtime/lib/js_helper.dart | 6 +- .../js_runtime/lib/synced/embedded_names.dart | 117 ------------------ sdk/lib/_internal/js_shared/lib/rti.dart | 18 +-- .../js_shared/lib/synced/embedded_names.dart | 116 +++++++++++++++++ .../sdk_library_metadata/lib/libraries.dart | 5 + sdk/lib/libraries.json | 3 + sdk/lib/libraries.yaml | 3 + tests/web/internal/rti/subtype_test.dart | 2 +- tests/web_2/internal/rti/subtype_test.dart | 2 +- 19 files changed, 277 insertions(+), 266 deletions(-) create mode 100644 pkg/js_shared/lib/synced/embedded_names.dart create mode 100644 sdk/lib/_internal/js_shared/lib/synced/embedded_names.dart diff --git a/pkg/compiler/lib/src/common/elements.dart b/pkg/compiler/lib/src/common/elements.dart index a66fe73d1d3..47d493065b1 100644 --- a/pkg/compiler/lib/src/common/elements.dart +++ b/pkg/compiler/lib/src/common/elements.dart @@ -915,7 +915,7 @@ abstract class CommonElements { // From dart:_js_embedded_names late final ClassEntity jsGetNameEnum = _findClass( - _env.lookupLibrary(Uris.dart__js_embedded_names, required: true), + _env.lookupLibrary(Uris.dart__js_shared_embedded_names, required: true), 'JsGetName')!; /// Returns `true` if [member] is a "foreign helper", that is, a member whose @@ -1185,7 +1185,7 @@ class JCommonElements extends CommonElements { 'NativeTypedArrayOfDouble')!; late final ClassEntity jsBuiltinEnum = _findClass( - _env.lookupLibrary(Uris.dart__js_embedded_names, required: true), + _env.lookupLibrary(Uris.dart__js_shared_embedded_names, required: true), 'JsBuiltin')!; bool isForeign(MemberEntity element) => element.library == foreignLibrary; diff --git a/pkg/compiler/lib/src/common/names.dart b/pkg/compiler/lib/src/common/names.dart index 024be8a94ef..3058b114d2e 100644 --- a/pkg/compiler/lib/src/common/names.dart +++ b/pkg/compiler/lib/src/common/names.dart @@ -249,6 +249,10 @@ class Uris { static final Uri dart__js_embedded_names = Uri(scheme: 'dart', path: '_js_embedded_names'); + /// The URI for 'dart:_js_shared_embedded_names'. + static final Uri dart__js_shared_embedded_names = + Uri(scheme: 'dart', path: '_js_shared_embedded_names'); + /// The URI for 'dart:js'. static final Uri dart_js = Uri(scheme: 'dart', path: 'js'); diff --git a/pkg/compiler/lib/src/js_backend/namer.dart b/pkg/compiler/lib/src/js_backend/namer.dart index 392e4e5c277..dc91161d9c8 100644 --- a/pkg/compiler/lib/src/js_backend/namer.dart +++ b/pkg/compiler/lib/src/js_backend/namer.dart @@ -9,7 +9,7 @@ library js_backend.namer; import 'package:front_end/src/api_unstable/dart2js.dart' show $0, $9, $A, $Z, $_, $a, $g, $s, $z; -import 'package:js_runtime/synced/embedded_names.dart' show JsGetName; +import 'package:js_shared/synced/embedded_names.dart' show JsGetName; import '../closure.dart'; import '../common.dart'; diff --git a/pkg/compiler/lib/src/js_emitter/startup_emitter/model_emitter.dart b/pkg/compiler/lib/src/js_emitter/startup_emitter/model_emitter.dart index 9ff95c58c2f..254b4c73c5e 100644 --- a/pkg/compiler/lib/src/js_emitter/startup_emitter/model_emitter.dart +++ b/pkg/compiler/lib/src/js_emitter/startup_emitter/model_emitter.dart @@ -10,7 +10,6 @@ import 'dart:convert' show JsonEncoder; import 'package:js_runtime/synced/embedded_names.dart' show - ARRAY_RTI_PROPERTY, DEFERRED_INITIALIZED, DEFERRED_LIBRARY_PARTS, DEFERRED_PART_URIS, @@ -19,18 +18,22 @@ import 'package:js_runtime/synced/embedded_names.dart' INTERCEPTORS_BY_TAG, IS_HUNK_INITIALIZED, IS_HUNK_LOADED, - JsGetName, LEAF_TAGS, MANGLED_GLOBAL_NAMES, MANGLED_NAMES, METADATA, NATIVE_SUPERCLASS_TAG_NAME, - RTI_UNIVERSE, - RtiUniverseFieldNames, RUNTIME_METRICS, STARTUP_METRICS, TearOffParametersPropertyNames, - TYPE_TO_INTERCEPTOR_MAP, + TYPE_TO_INTERCEPTOR_MAP; + +import 'package:js_shared/synced/embedded_names.dart' + show + ARRAY_RTI_PROPERTY, + JsGetName, + RTI_UNIVERSE, + RtiUniverseFieldNames, TYPES; import 'package:js_ast/src/precedence.dart' as js_precedence; diff --git a/pkg/compiler/lib/src/kernel/dart2js_target.dart b/pkg/compiler/lib/src/kernel/dart2js_target.dart index a5172dfad30..5b51960304b 100644 --- a/pkg/compiler/lib/src/kernel/dart2js_target.dart +++ b/pkg/compiler/lib/src/kernel/dart2js_target.dart @@ -261,6 +261,7 @@ const requiredLibraries = >{ 'dart:_js_helper', 'dart:_js_names', 'dart:_js_primitives', + 'dart:_js_shared_embedded_names', 'dart:_late_helper', 'dart:_metadata', 'dart:_native_typed_data', @@ -296,6 +297,7 @@ const requiredLibraries = >{ 'dart:_js_helper', 'dart:_js_names', 'dart:_js_primitives', + 'dart:_js_shared_embedded_names', 'dart:_late_helper', 'dart:_native_typed_data', 'dart:_recipe_syntax', diff --git a/pkg/compiler/lib/src/kernel/element_map_impl.dart b/pkg/compiler/lib/src/kernel/element_map_impl.dart index f28ab293499..e2437be1424 100644 --- a/pkg/compiler/lib/src/kernel/element_map_impl.dart +++ b/pkg/compiler/lib/src/kernel/element_map_impl.dart @@ -6,7 +6,7 @@ import 'package:front_end/src/api_prototype/constant_evaluator.dart' as ir; import 'package:front_end/src/api_unstable/dart2js.dart' as ir; -import 'package:js_runtime/synced/embedded_names.dart'; +import 'package:js_shared/synced/embedded_names.dart' show JsGetName; import 'package:kernel/ast.dart' as ir; import 'package:kernel/class_hierarchy.dart' as ir; import 'package:kernel/core_types.dart' as ir; diff --git a/pkg/compiler/lib/src/ssa/builder.dart b/pkg/compiler/lib/src/ssa/builder.dart index cc261971184..dda60e09c21 100644 --- a/pkg/compiler/lib/src/ssa/builder.dart +++ b/pkg/compiler/lib/src/ssa/builder.dart @@ -5,6 +5,8 @@ // @dart = 2.10 import 'package:js_runtime/synced/embedded_names.dart'; +import 'package:js_shared/synced/embedded_names.dart' + show JsBuiltin, JsGetName, TYPES; import 'package:kernel/ast.dart' as ir; import '../closure.dart'; diff --git a/pkg/js_runtime/lib/synced/embedded_names.dart b/pkg/js_runtime/lib/synced/embedded_names.dart index 9d48e1adb51..0c99f4724d2 100644 --- a/pkg/js_runtime/lib/synced/embedded_names.dart +++ b/pkg/js_runtime/lib/synced/embedded_names.dart @@ -26,22 +26,12 @@ const NATIVE_SUPERCLASS_TAG_NAME = r"$nativeSuperclassTag"; /// the static function's unique (potentially minified) name. const STATIC_FUNCTION_NAME_PROPERTY_NAME = r'$static_name'; -/// The name of a property on the constructor function of Dart Object -/// and interceptor types, used for caching Rti types. -const CONSTRUCTOR_RTI_CACHE_PROPERTY_NAME = r'$ccache'; - /// The name of the embedded global for metadata. /// /// Use [JsBuiltin.getMetadata] instead of directly accessing this embedded /// global. const METADATA = 'metadata'; -/// A list of types used in the program e.g. for reflection or encoding of -/// function types. -/// -/// Use [JsBuiltin.getType] instead of directly accessing this embedded global. -const TYPES = 'types'; - /// A JS map from mangled global names to their unmangled names. /// /// If the program does not use reflection, this embedded global may be empty @@ -94,11 +84,6 @@ const GET_ISOLATE_TAG = 'getIsolateTag'; // [INTERCEPTORS_BY_TAG] and [LEAF_TAGS]. const ISOLATE_TAG = 'isolateTag'; -/// An embedded global that contains the property used to store type information -/// on JavaScript Array instances. This is a Symbol (except for IE11, where is -/// is a String). -const ARRAY_RTI_PROPERTY = 'arrayRti'; - /// This embedded global (a function) returns the isolate-specific dispatch-tag /// that is used to accelerate interceptor calls. const DISPATCH_PROPERTY_NAME = "dispatchPropertyName"; @@ -174,11 +159,6 @@ const IS_HUNK_INITIALIZED = 'isHunkInitialized'; /// globals don't clash with it. const DEFERRED_INITIALIZED = 'deferredInitialized'; -/// A 'Universe' object used by 'dart:_rti'. -/// -/// This embedded global is used for --experiment-new-rti. -const RTI_UNIVERSE = 'typeUniverse'; - /// An embedded global used to collect and access runtime metrics. const RUNTIME_METRICS = 'rm'; @@ -188,103 +168,6 @@ const RUNTIME_METRICS_CONTAINER = 'runtimeMetrics'; /// An embedded global used to collect and access startup metrics. const STARTUP_METRICS = 'sm'; -/// Names that are supported by [JS_GET_NAME]. -// TODO(herhut): Make entries lower case (as in fields) and find a better name. -enum JsGetName { - GETTER_PREFIX, - SETTER_PREFIX, - CALL_PREFIX, - CALL_PREFIX0, - CALL_PREFIX1, - CALL_PREFIX2, - CALL_PREFIX3, - CALL_PREFIX4, - CALL_PREFIX5, - CALL_CATCH_ALL, - REQUIRED_PARAMETER_PROPERTY, - DEFAULT_VALUES_PROPERTY, - CALL_NAME_PROPERTY, - DEFERRED_ACTION_PROPERTY, - - /// Prefix used for generated type test property on classes. - OPERATOR_IS_PREFIX, - - /// Name used for generated function types on classes and methods. - SIGNATURE_NAME, - - /// Name of JavaScript property used to store runtime-type information on - /// instances of parameterized classes. - RTI_NAME, - - /// String representation of the type of the Future class. - FUTURE_CLASS_TYPE_NAME, - - /// Field name used for determining if an object or its interceptor has - /// JavaScript indexing behavior. - IS_INDEXABLE_FIELD_NAME, - - /// String representation of the type of the null class. - NULL_CLASS_TYPE_NAME, - - /// String representation of the type of the object class. - OBJECT_CLASS_TYPE_NAME, - - /// String representation of the type of the List class. - LIST_CLASS_TYPE_NAME, - - /// Property name for Rti._as field. - RTI_FIELD_AS, - - /// Property name for Rti._is field. - RTI_FIELD_IS, -} - -enum JsBuiltin { - /// Returns the JavaScript constructor function for Dart's Object class. - /// This can be used for type tests, as in - /// - /// var constructor = JS_BUILTIN('', JsBuiltin.dartObjectConstructor); - /// if (JS('bool', '# instanceof #', obj, constructor)) - /// ... - dartObjectConstructor, - - /// Returns the JavaScript constructor function for the runtime's Closure - /// class, the base class of all closure objects. This can be used for type - /// tests, as in - /// - /// var constructor = JS_BUILTIN('', JsBuiltin.dartClosureConstructor); - /// if (JS('bool', '# instanceof #', obj, constructor)) - /// ... - dartClosureConstructor, - - /// Returns true if the given type is a type argument of a js-interop class - /// or a supertype of a js-interop class. - /// - /// JS_BUILTIN('bool', JsBuiltin.isJsInteropTypeArgument, o) - isJsInteropTypeArgument, - - /// Returns the metadata of the given [index]. - /// - /// JS_BUILTIN('returns:var;effects:none;depends:none', - /// JsBuiltin.getMetadata, index); - getMetadata, - - /// Returns the type of the given [index]. - /// - /// JS_BUILTIN('returns:var;effects:none;depends:none', - /// JsBuiltin.getType, index); - getType, -} - -/// Names of fields of the Rti Universe object. -class RtiUniverseFieldNames { - static String evalCache = 'eC'; - static String typeRules = 'tR'; - static String erasedTypes = 'eT'; - static String typeParameterVariances = 'tPV'; - static String sharedEmptyArray = 'sEA'; -} - /// Names of fields of collected tear-off parameters object. /// /// Tear-off getters are created before the Dart classes are initialized, so a diff --git a/pkg/js_shared/lib/synced/embedded_names.dart b/pkg/js_shared/lib/synced/embedded_names.dart new file mode 100644 index 00000000000..c2f63075290 --- /dev/null +++ b/pkg/js_shared/lib/synced/embedded_names.dart @@ -0,0 +1,116 @@ +// Copyright (c) 2022, the Dart project authors. Please see the AUTHORS file +// for details. All rights reserved. Use of this source code is governed by a +// BSD-style license that can be found in the LICENSE file. + +// @dart=2.12 + +/// A 'Universe' object used by 'dart:_rti'. +const RTI_UNIVERSE = 'typeUniverse'; + +/// An embedded global that contains the property used to store type information +/// on JavaScript Array instances. This is a Symbol (except for IE11, where is +/// is a String). +const ARRAY_RTI_PROPERTY = 'arrayRti'; + +/// A list of types used in the program e.g. for reflection or encoding of +/// function types. +/// +/// Use [JsBuiltin.getType] instead of directly accessing this embedded global. +const TYPES = 'types'; + +/// Names that are supported by [JS_GET_NAME]. +// TODO(herhut): Make entries lower case (as in fields) and find a better name. +enum JsGetName { + GETTER_PREFIX, + SETTER_PREFIX, + CALL_PREFIX, + CALL_PREFIX0, + CALL_PREFIX1, + CALL_PREFIX2, + CALL_PREFIX3, + CALL_PREFIX4, + CALL_PREFIX5, + CALL_CATCH_ALL, + REQUIRED_PARAMETER_PROPERTY, + DEFAULT_VALUES_PROPERTY, + CALL_NAME_PROPERTY, + DEFERRED_ACTION_PROPERTY, + + /// Prefix used for generated type test property on classes. + OPERATOR_IS_PREFIX, + + /// Name used for generated function types on classes and methods. + SIGNATURE_NAME, + + /// Name of JavaScript property used to store runtime-type information on + /// instances of parameterized classes. + RTI_NAME, + + /// String representation of the type of the Future class. + FUTURE_CLASS_TYPE_NAME, + + /// Field name used for determining if an object or its interceptor has + /// JavaScript indexing behavior. + IS_INDEXABLE_FIELD_NAME, + + /// String representation of the type of the null class. + NULL_CLASS_TYPE_NAME, + + /// String representation of the type of the object class. + OBJECT_CLASS_TYPE_NAME, + + /// String representation of the type of the List class. + LIST_CLASS_TYPE_NAME, + + /// Property name for Rti._as field. + RTI_FIELD_AS, + + /// Property name for Rti._is field. + RTI_FIELD_IS, +} + +enum JsBuiltin { + /// Returns the JavaScript constructor function for Dart's Object class. + /// This can be used for type tests, as in + /// + /// var constructor = JS_BUILTIN('', JsBuiltin.dartObjectConstructor); + /// if (JS('bool', '# instanceof #', obj, constructor)) + /// ... + dartObjectConstructor, + + /// Returns the JavaScript constructor function for the runtime's Closure + /// class, the base class of all closure objects. This can be used for type + /// tests, as in + /// + /// var constructor = JS_BUILTIN('', JsBuiltin.dartClosureConstructor); + /// if (JS('bool', '# instanceof #', obj, constructor)) + /// ... + dartClosureConstructor, + + /// Returns true if the given type is a type argument of a js-interop class + /// or a supertype of a js-interop class. + /// + /// JS_BUILTIN('bool', JsBuiltin.isJsInteropTypeArgument, o) + isJsInteropTypeArgument, + + /// Returns the metadata of the given [index]. + /// + /// JS_BUILTIN('returns:var;effects:none;depends:none', + /// JsBuiltin.getMetadata, index); + getMetadata, + + /// Returns the type of the given [index]. + /// + /// JS_BUILTIN('returns:var;effects:none;depends:none', + /// JsBuiltin.getType, index); + getType, +} + +/// Names of fields of the Rti Universe object. +class RtiUniverseFieldNames { + static String evalCache = 'eC'; + static String typeRules = 'tR'; + static String erasedTypes = 'eT'; + static String typeParameterVariances = 'tPV'; + static String sharedEmptyArray = 'sEA'; +} diff --git a/sdk/lib/_internal/js_runtime/lib/foreign_helper.dart b/sdk/lib/_internal/js_runtime/lib/foreign_helper.dart index 6e8cd728f3e..66b06a7bd7d 100644 --- a/sdk/lib/_internal/js_runtime/lib/foreign_helper.dart +++ b/sdk/lib/_internal/js_runtime/lib/foreign_helper.dart @@ -4,7 +4,7 @@ library _foreign_helper; -import 'dart:_js_embedded_names' show JsGetName, JsBuiltin; +import 'dart:_js_shared_embedded_names' show JsGetName, JsBuiltin; import 'dart:_rti' show Rti; /// Emits a JavaScript code fragment parametrized by arguments. @@ -215,13 +215,14 @@ external String JS_GET_NAME(JsGetName name); /// Reads an embedded global. /// -/// The [name] should be a constant defined in the `_embedded_names` library. +/// The [name] should be a constant defined in the `_embedded_names` or +/// `_js_shared_embedded_names` library. external JS_EMBEDDED_GLOBAL(String typeDescription, String name); /// Instructs the compiler to execute the [builtinName] action at the call-site. /// -/// The [builtin] should be a constant defined in the `_embedded_names` -/// library. +/// The [builtin] should be a constant defined in the +/// `_js_shared_embedded_names` library. // Add additional optional arguments if needed. The method is treated internally // as a variable argument method. external JS_BUILTIN(String typeDescription, JsBuiltin builtin, diff --git a/sdk/lib/_internal/js_runtime/lib/js_helper.dart b/sdk/lib/_internal/js_runtime/lib/js_helper.dart index 50f9c4f34c8..fd3ffd1fbaf 100644 --- a/sdk/lib/_internal/js_runtime/lib/js_helper.dart +++ b/sdk/lib/_internal/js_runtime/lib/js_helper.dart @@ -6,19 +6,15 @@ library _js_helper; import 'dart:_js_embedded_names' show - ARRAY_RTI_PROPERTY, CURRENT_SCRIPT, DEFERRED_LIBRARY_PARTS, DEFERRED_PART_URIS, DEFERRED_PART_HASHES, GET_ISOLATE_TAG, INITIALIZE_LOADED_HUNK, - INTERCEPTED_NAMES, INTERCEPTORS_BY_TAG, IS_HUNK_LOADED, IS_HUNK_INITIALIZED, - JsBuiltin, - JsGetName, LEAF_TAGS, NATIVE_SUPERCLASS_TAG_NAME, RUNTIME_METRICS, @@ -26,6 +22,8 @@ import 'dart:_js_embedded_names' STATIC_FUNCTION_NAME_PROPERTY_NAME, TearOffParametersPropertyNames; +import 'dart:_js_shared_embedded_names' show JsBuiltin, JsGetName; + import 'dart:collection'; import 'dart:async' show Completer, DeferredLoadException, Future, Zone; diff --git a/sdk/lib/_internal/js_runtime/lib/synced/embedded_names.dart b/sdk/lib/_internal/js_runtime/lib/synced/embedded_names.dart index 9d48e1adb51..0c99f4724d2 100644 --- a/sdk/lib/_internal/js_runtime/lib/synced/embedded_names.dart +++ b/sdk/lib/_internal/js_runtime/lib/synced/embedded_names.dart @@ -26,22 +26,12 @@ const NATIVE_SUPERCLASS_TAG_NAME = r"$nativeSuperclassTag"; /// the static function's unique (potentially minified) name. const STATIC_FUNCTION_NAME_PROPERTY_NAME = r'$static_name'; -/// The name of a property on the constructor function of Dart Object -/// and interceptor types, used for caching Rti types. -const CONSTRUCTOR_RTI_CACHE_PROPERTY_NAME = r'$ccache'; - /// The name of the embedded global for metadata. /// /// Use [JsBuiltin.getMetadata] instead of directly accessing this embedded /// global. const METADATA = 'metadata'; -/// A list of types used in the program e.g. for reflection or encoding of -/// function types. -/// -/// Use [JsBuiltin.getType] instead of directly accessing this embedded global. -const TYPES = 'types'; - /// A JS map from mangled global names to their unmangled names. /// /// If the program does not use reflection, this embedded global may be empty @@ -94,11 +84,6 @@ const GET_ISOLATE_TAG = 'getIsolateTag'; // [INTERCEPTORS_BY_TAG] and [LEAF_TAGS]. const ISOLATE_TAG = 'isolateTag'; -/// An embedded global that contains the property used to store type information -/// on JavaScript Array instances. This is a Symbol (except for IE11, where is -/// is a String). -const ARRAY_RTI_PROPERTY = 'arrayRti'; - /// This embedded global (a function) returns the isolate-specific dispatch-tag /// that is used to accelerate interceptor calls. const DISPATCH_PROPERTY_NAME = "dispatchPropertyName"; @@ -174,11 +159,6 @@ const IS_HUNK_INITIALIZED = 'isHunkInitialized'; /// globals don't clash with it. const DEFERRED_INITIALIZED = 'deferredInitialized'; -/// A 'Universe' object used by 'dart:_rti'. -/// -/// This embedded global is used for --experiment-new-rti. -const RTI_UNIVERSE = 'typeUniverse'; - /// An embedded global used to collect and access runtime metrics. const RUNTIME_METRICS = 'rm'; @@ -188,103 +168,6 @@ const RUNTIME_METRICS_CONTAINER = 'runtimeMetrics'; /// An embedded global used to collect and access startup metrics. const STARTUP_METRICS = 'sm'; -/// Names that are supported by [JS_GET_NAME]. -// TODO(herhut): Make entries lower case (as in fields) and find a better name. -enum JsGetName { - GETTER_PREFIX, - SETTER_PREFIX, - CALL_PREFIX, - CALL_PREFIX0, - CALL_PREFIX1, - CALL_PREFIX2, - CALL_PREFIX3, - CALL_PREFIX4, - CALL_PREFIX5, - CALL_CATCH_ALL, - REQUIRED_PARAMETER_PROPERTY, - DEFAULT_VALUES_PROPERTY, - CALL_NAME_PROPERTY, - DEFERRED_ACTION_PROPERTY, - - /// Prefix used for generated type test property on classes. - OPERATOR_IS_PREFIX, - - /// Name used for generated function types on classes and methods. - SIGNATURE_NAME, - - /// Name of JavaScript property used to store runtime-type information on - /// instances of parameterized classes. - RTI_NAME, - - /// String representation of the type of the Future class. - FUTURE_CLASS_TYPE_NAME, - - /// Field name used for determining if an object or its interceptor has - /// JavaScript indexing behavior. - IS_INDEXABLE_FIELD_NAME, - - /// String representation of the type of the null class. - NULL_CLASS_TYPE_NAME, - - /// String representation of the type of the object class. - OBJECT_CLASS_TYPE_NAME, - - /// String representation of the type of the List class. - LIST_CLASS_TYPE_NAME, - - /// Property name for Rti._as field. - RTI_FIELD_AS, - - /// Property name for Rti._is field. - RTI_FIELD_IS, -} - -enum JsBuiltin { - /// Returns the JavaScript constructor function for Dart's Object class. - /// This can be used for type tests, as in - /// - /// var constructor = JS_BUILTIN('', JsBuiltin.dartObjectConstructor); - /// if (JS('bool', '# instanceof #', obj, constructor)) - /// ... - dartObjectConstructor, - - /// Returns the JavaScript constructor function for the runtime's Closure - /// class, the base class of all closure objects. This can be used for type - /// tests, as in - /// - /// var constructor = JS_BUILTIN('', JsBuiltin.dartClosureConstructor); - /// if (JS('bool', '# instanceof #', obj, constructor)) - /// ... - dartClosureConstructor, - - /// Returns true if the given type is a type argument of a js-interop class - /// or a supertype of a js-interop class. - /// - /// JS_BUILTIN('bool', JsBuiltin.isJsInteropTypeArgument, o) - isJsInteropTypeArgument, - - /// Returns the metadata of the given [index]. - /// - /// JS_BUILTIN('returns:var;effects:none;depends:none', - /// JsBuiltin.getMetadata, index); - getMetadata, - - /// Returns the type of the given [index]. - /// - /// JS_BUILTIN('returns:var;effects:none;depends:none', - /// JsBuiltin.getType, index); - getType, -} - -/// Names of fields of the Rti Universe object. -class RtiUniverseFieldNames { - static String evalCache = 'eC'; - static String typeRules = 'tR'; - static String erasedTypes = 'eT'; - static String typeParameterVariances = 'tPV'; - static String sharedEmptyArray = 'sEA'; -} - /// Names of fields of collected tear-off parameters object. /// /// Tear-off getters are created before the Dart classes are initialized, so a diff --git a/sdk/lib/_internal/js_shared/lib/rti.dart b/sdk/lib/_internal/js_shared/lib/rti.dart index 356298bc5a0..b7c9881278c 100644 --- a/sdk/lib/_internal/js_shared/lib/rti.dart +++ b/sdk/lib/_internal/js_shared/lib/rti.dart @@ -18,24 +18,16 @@ import 'dart:_foreign_helper' RAW_DART_FUNCTION_REF, TYPE_REF, LEGACY_TYPE_REF; - import 'dart:_interceptors' show JavaScriptFunction, JSArray, JSNull, JSUnmodifiableArray; - import 'dart:_js_names' show unmangleGlobalNameIfPreservedAnyways; - -import 'dart:_js_embedded_names' - show - JsBuiltin, - JsGetName, - RtiUniverseFieldNames, - ARRAY_RTI_PROPERTY, - CONSTRUCTOR_RTI_CACHE_PROPERTY_NAME, - RTI_UNIVERSE, - TYPES; - +import 'dart:_js_shared_embedded_names'; import 'dart:_recipe_syntax'; +/// The name of a property on the constructor function of Dart Object +/// and interceptor types, used for caching Rti types. +const CONSTRUCTOR_RTI_CACHE_PROPERTY_NAME = r'$ccache'; + // The top type `Object?` is used throughout this library even when values are // not nullable or have narrower types in order to avoid incurring type checks // before the type checking infrastructure has been set up. diff --git a/sdk/lib/_internal/js_shared/lib/synced/embedded_names.dart b/sdk/lib/_internal/js_shared/lib/synced/embedded_names.dart new file mode 100644 index 00000000000..c2f63075290 --- /dev/null +++ b/sdk/lib/_internal/js_shared/lib/synced/embedded_names.dart @@ -0,0 +1,116 @@ +// Copyright (c) 2022, the Dart project authors. Please see the AUTHORS file +// for details. All rights reserved. Use of this source code is governed by a +// BSD-style license that can be found in the LICENSE file. + +// @dart=2.12 + +/// A 'Universe' object used by 'dart:_rti'. +const RTI_UNIVERSE = 'typeUniverse'; + +/// An embedded global that contains the property used to store type information +/// on JavaScript Array instances. This is a Symbol (except for IE11, where is +/// is a String). +const ARRAY_RTI_PROPERTY = 'arrayRti'; + +/// A list of types used in the program e.g. for reflection or encoding of +/// function types. +/// +/// Use [JsBuiltin.getType] instead of directly accessing this embedded global. +const TYPES = 'types'; + +/// Names that are supported by [JS_GET_NAME]. +// TODO(herhut): Make entries lower case (as in fields) and find a better name. +enum JsGetName { + GETTER_PREFIX, + SETTER_PREFIX, + CALL_PREFIX, + CALL_PREFIX0, + CALL_PREFIX1, + CALL_PREFIX2, + CALL_PREFIX3, + CALL_PREFIX4, + CALL_PREFIX5, + CALL_CATCH_ALL, + REQUIRED_PARAMETER_PROPERTY, + DEFAULT_VALUES_PROPERTY, + CALL_NAME_PROPERTY, + DEFERRED_ACTION_PROPERTY, + + /// Prefix used for generated type test property on classes. + OPERATOR_IS_PREFIX, + + /// Name used for generated function types on classes and methods. + SIGNATURE_NAME, + + /// Name of JavaScript property used to store runtime-type information on + /// instances of parameterized classes. + RTI_NAME, + + /// String representation of the type of the Future class. + FUTURE_CLASS_TYPE_NAME, + + /// Field name used for determining if an object or its interceptor has + /// JavaScript indexing behavior. + IS_INDEXABLE_FIELD_NAME, + + /// String representation of the type of the null class. + NULL_CLASS_TYPE_NAME, + + /// String representation of the type of the object class. + OBJECT_CLASS_TYPE_NAME, + + /// String representation of the type of the List class. + LIST_CLASS_TYPE_NAME, + + /// Property name for Rti._as field. + RTI_FIELD_AS, + + /// Property name for Rti._is field. + RTI_FIELD_IS, +} + +enum JsBuiltin { + /// Returns the JavaScript constructor function for Dart's Object class. + /// This can be used for type tests, as in + /// + /// var constructor = JS_BUILTIN('', JsBuiltin.dartObjectConstructor); + /// if (JS('bool', '# instanceof #', obj, constructor)) + /// ... + dartObjectConstructor, + + /// Returns the JavaScript constructor function for the runtime's Closure + /// class, the base class of all closure objects. This can be used for type + /// tests, as in + /// + /// var constructor = JS_BUILTIN('', JsBuiltin.dartClosureConstructor); + /// if (JS('bool', '# instanceof #', obj, constructor)) + /// ... + dartClosureConstructor, + + /// Returns true if the given type is a type argument of a js-interop class + /// or a supertype of a js-interop class. + /// + /// JS_BUILTIN('bool', JsBuiltin.isJsInteropTypeArgument, o) + isJsInteropTypeArgument, + + /// Returns the metadata of the given [index]. + /// + /// JS_BUILTIN('returns:var;effects:none;depends:none', + /// JsBuiltin.getMetadata, index); + getMetadata, + + /// Returns the type of the given [index]. + /// + /// JS_BUILTIN('returns:var;effects:none;depends:none', + /// JsBuiltin.getType, index); + getType, +} + +/// Names of fields of the Rti Universe object. +class RtiUniverseFieldNames { + static String evalCache = 'eC'; + static String typeRules = 'tR'; + static String erasedTypes = 'eT'; + static String typeParameterVariances = 'tPV'; + static String sharedEmptyArray = 'sEA'; +} diff --git a/sdk/lib/_internal/sdk_library_metadata/lib/libraries.dart b/sdk/lib/_internal/sdk_library_metadata/lib/libraries.dart index 5a357e5454b..5cbab01bcd5 100644 --- a/sdk/lib/_internal/sdk_library_metadata/lib/libraries.dart +++ b/sdk/lib/_internal/sdk_library_metadata/lib/libraries.dart @@ -176,6 +176,11 @@ const Map libraries = const { categories: "", documented: false, platforms: DART2JS_PLATFORM), + "_js_shared_embedded_names": const LibraryInfo( + "_internal/js_shared/lib/synced/embedded_names.dart", + categories: "", + documented: false, + platforms: DART2JS_PLATFORM), "_async_await_error_codes": const LibraryInfo( "_internal/js_runtime/lib/synced/async_await_error_codes.dart", categories: "", diff --git a/sdk/lib/libraries.json b/sdk/lib/libraries.json index 6f33d88fad0..4c5eb19962e 100644 --- a/sdk/lib/libraries.json +++ b/sdk/lib/libraries.json @@ -386,6 +386,9 @@ "_js_embedded_names": { "uri": "_internal/js_runtime/lib/synced/embedded_names.dart" }, + "_js_shared_embedded_names": { + "uri": "_internal/js_shared/lib/synced/embedded_names.dart" + }, "_async_await_error_codes": { "uri": "_internal/js_runtime/lib/synced/async_await_error_codes.dart" }, diff --git a/sdk/lib/libraries.yaml b/sdk/lib/libraries.yaml index ed11fa53e7e..c09c34ea18a 100644 --- a/sdk/lib/libraries.yaml +++ b/sdk/lib/libraries.yaml @@ -354,6 +354,9 @@ _dart2js_common: _js_embedded_names: uri: "_internal/js_runtime/lib/synced/embedded_names.dart" + _js_shared_embedded_names: + uri: "_internal/js_shared/lib/synced/embedded_names.dart" + _async_await_error_codes: uri: "_internal/js_runtime/lib/synced/async_await_error_codes.dart" diff --git a/tests/web/internal/rti/subtype_test.dart b/tests/web/internal/rti/subtype_test.dart index d4f0c48d5e4..b6ceded2ed8 100644 --- a/tests/web/internal/rti/subtype_test.dart +++ b/tests/web/internal/rti/subtype_test.dart @@ -3,7 +3,7 @@ // BSD-style license that can be found in the LICENSE file. import 'dart:_foreign_helper' show JS, JS_GET_NAME, TYPE_REF; -import 'dart:_js_embedded_names' show JsGetName; +import 'dart:_js_shared_embedded_names' show JsGetName; import 'dart:_rti' as rti; import 'package:expect/expect.dart'; diff --git a/tests/web_2/internal/rti/subtype_test.dart b/tests/web_2/internal/rti/subtype_test.dart index 3ffea44656c..5cdb5ce14ae 100644 --- a/tests/web_2/internal/rti/subtype_test.dart +++ b/tests/web_2/internal/rti/subtype_test.dart @@ -5,7 +5,7 @@ // @dart = 2.7 import 'dart:_foreign_helper' show JS, JS_GET_NAME, TYPE_REF; -import 'dart:_js_embedded_names' show JsGetName; +import 'dart:_js_shared_embedded_names' show JsGetName; import 'dart:_rti' as rti; import 'subtype_utils.dart';