Skip to content

Commit

Permalink
dart2js: Add skeleton dart:_rti libary
Browse files Browse the repository at this point in the history
- Skeleton Rti type
- Skeleton test
- Add dependency on command-line flag to get dart:_rti into platform dill

Change-Id: Idf383269c66c9951e23fd70a45ce65c54a973586
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/104921
Commit-Queue: Stephen Adams <sra@google.com>
Reviewed-by: Mayank Patke <fishythefish@google.com>
  • Loading branch information
rakudrama authored and commit-bot@chromium.org committed Jun 6, 2019
1 parent 63994f0 commit 237d3ca
Show file tree
Hide file tree
Showing 8 changed files with 170 additions and 0 deletions.
2 changes: 2 additions & 0 deletions pkg/compiler/lib/src/ssa/builder_kernel.dart
Original file line number Diff line number Diff line change
Expand Up @@ -377,6 +377,8 @@ class KernelSsaGraphBuilder extends ir.Visitor {
return false;
case 'USE_CONTENT_SECURITY_POLICY':
return options.useContentSecurityPolicy;
case 'USE_NEW_RTI':
return options.experimentNewRti;
default:
return null;
}
Expand Down
2 changes: 2 additions & 0 deletions sdk/lib/_internal/js_runtime/lib/js_helper.dart
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,8 @@ import 'dart:_js_names'
unmangleGlobalNameIfPreservedAnyways,
unmangleAllIdentifiersIfPreservedAnyways;

import 'dart:_rti' as newRti show getRuntimeType;

part 'annotations.dart';
part 'constant_map.dart';
part 'instantiation.dart';
Expand Down
1 change: 1 addition & 0 deletions sdk/lib/_internal/js_runtime/lib/js_rti.dart
Original file line number Diff line number Diff line change
Expand Up @@ -391,6 +391,7 @@ getRti(o) {
}

Type getRuntimeType(var object) {
if (JS_GET_FLAG('USE_NEW_RTI')) return newRti.getRuntimeType(object);
return new TypeImpl(getRti(object));
}

Expand Down
140 changes: 140 additions & 0 deletions sdk/lib/_internal/js_runtime/lib/rti.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,140 @@
// Copyright (c) 2019, 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.

/// This library contains support for runtime type information.
library rti;

import 'dart:_foreign_helper' show JS;
import 'dart:_interceptors' show JSArray, JSUnmodifiableArray;

/// An Rti object represents both a type (e.g `Map<int, String>`) and a type
/// environment (`Map<int, String>` binds `Map.K=int` and `Map.V=String`).
///
/// There is a single [Rti] class to help reduce polymorphism in the JavaScript
/// runtime. The class has a default constructor and no final fields so it can
/// be created before much of the runtime exists.
///
/// The fields are declared in an order that gets shorter minified names for the
/// more commonly used fields. (TODO: we should exploit the fact that an Rti
/// instance never appears in a dynamic context, so does not need field names to
/// be distinct from dynamic selectors).
///
class Rti {
/// JavaScript method for 'as' check. The method is called from generated code,
/// e.g. `o as T` generates something like `rtiForT._as(o)`.
dynamic _as;

/// JavaScript method for type check. The method is called from generated
/// code, e.g. parameter check for `T param` generates something like
/// `rtiForT._check(param)`.
dynamic _check;

/// JavaScript method for 'is' test. The method is called from generated
/// code, e.g. `o is T` generates something like `rtiForT._is(o)`.
dynamic _is;

/// Method called from generated code to evaluate a type environment recipe in
/// `this` type environment.
Rti _eval(String recipe) => _rtiEval(this, recipe);

/// Method called from generated code to extend `this` type environment with a
/// function type parameter.
Rti _bind1(Rti type) => _rtiBind1(this, type);

/// Method called from generated code to extend `this` type environment with a
/// tuple of function type parameters.
Rti _bind(Rti typeTuple) => _rtiBind(this, typeTuple);

// Precomputed derived types. These fields are used to hold derived types that
// are computed eagerly.
// TODO(sra): Implement precomputed type optimizations.
dynamic _precomputed1;
dynamic _precomputed2;
dynamic _precomputed3;
dynamic _precomputed4;

// The Type object corresponding to this Rti.
Type _typeCache;

/// The kind of Rti `this` is, one of the kindXXX constants below.
///
/// We don't use an enum since we need to create Rti objects very early.
///
/// The zero initializer ensures dart2js type analysis considers [_kind] is
/// non-nullable.
int _kind = 0;

// Terminal terms.
static const kindNever = 1;
static const kindDynamic = 2;
static const kindVoid = 3; // TODO(sra): Use `dynamic` instead?
static const kindAny = 4; // Dart1-style 'dynamic' for JS-interop.
// Unary terms.
static const kindStar = 5;
static const kindQuestion = 6;
static const kindFutureOr = 7;
// More complex terms.
static const kindInterface = 8;
// A vector of type parameters from enclosing functions and closures.
static const kindBinding = 9;
static const kindFunction = 10;
static const kindGenericFunction = 11;

/// Primary data associated with type.
///
/// - Minified name of interface for interface types.
/// - Underlying type for unary terms.
/// - Class part of a type environment inside a generic class, or `null` for
/// type tuple.
/// - Return type of function types.
dynamic _primary;

String get interfaceName {
assert(_kind == kindInterface);
return JS('String', '#', _primary);
}

/// Additional data associated with type.
///
/// - The type arguments of an interface type.
/// - The type arguments from enclosing functions and closures for a
/// kindBinding.
/// - TBD for kindFunction and kindGenericFunction.
dynamic _rest;

JSArray get interfaceTypeArguments {
// The array is a plain JavaScript Array, otherwise we would need the type
// `JSArray<Rti>` to exist before we could create the type `JSArray<Rti>`.
assert(_kind == kindInterface);
return JS('JSUnmodifiableArray', '#', _primary);
}

/// On [Rti]s that are type environments, derived types are cached on the
/// environment to ensure fast canonicalization. Ground-term types (i.e. not
/// dependent on class or function type parameters) are cached in the
/// universe. This field starts as `null` and the cache is created on demand.
dynamic _evalCache;
}

Rti _rtiEval(Rti environment, String recipe) {
throw UnimplementedError('_rtiEval');
}

Rti _rtiBind1(Rti environment, Rti type) {
throw UnimplementedError('_rtiBind1');
}

Rti _rtiBind(Rti environment, Rti typeTuple) {
throw UnimplementedError('_rtiBind');
}

Type getRuntimeType(object) {
throw UnimplementedError('getRuntimeType');
}

// Entry points for testing

String testingRtiToString(dynamic rti) {
return 'Rti';
}
2 changes: 2 additions & 0 deletions sdk/lib/_internal/sdk_library_metadata/lib/libraries.dart
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,8 @@ const Map<String, LibraryInfo> libraries = const {
dart2jsPatchPath: "_internal/js_runtime/lib/internal_patch.dart"),
"_js_helper": const LibraryInfo("_internal/js_runtime/lib/js_helper.dart",
categories: "", documented: false, platforms: DART2JS_PLATFORM),
"_rti": const LibraryInfo("_internal/js_runtime/lib/rti.dart",
categories: "", documented: false, platforms: DART2JS_PLATFORM),
"_interceptors": const LibraryInfo(
"_internal/js_runtime/lib/interceptors.dart",
categories: "",
Expand Down
6 changes: 6 additions & 0 deletions sdk/lib/libraries.json
Original file line number Diff line number Diff line change
Expand Up @@ -265,6 +265,9 @@
"web_sql": {
"uri": "web_sql/dart2js/web_sql_dart2js.dart"
},
"_rti": {
"uri": "_internal/js_runtime/lib/rti.dart"
},
"svg": {
"uri": "svg/dart2js/svg_dart2js.dart"
}
Expand Down Expand Up @@ -464,6 +467,9 @@
"_js_helper": {
"uri": "_internal/js_runtime/lib/js_helper.dart"
},
"_rti": {
"uri": "_internal/js_runtime/lib/rti.dart"
},
"js": {
"uri": "js/dart2js/js_dart2js.dart"
}
Expand Down
7 changes: 7 additions & 0 deletions sdk/lib/libraries.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -245,6 +245,9 @@ dart2js:
_js_helper:
uri: "_internal/js_runtime/lib/js_helper.dart"

_rti:
uri: "_internal/js_runtime/lib/rti.dart"

_interceptors:
uri: "_internal/js_runtime/lib/interceptors.dart"

Expand Down Expand Up @@ -334,6 +337,9 @@ dart2js_server:
_js_helper:
uri: "_internal/js_runtime/lib/js_helper.dart"

_rti:
uri: "_internal/js_runtime/lib/rti.dart"

_interceptors:
uri: "_internal/js_runtime/lib/interceptors.dart"

Expand All @@ -351,6 +357,7 @@ dart2js_server:

_async_await_error_codes:
uri: "_internal/js_runtime/lib/shared/async_await_error_codes.dart"

dartdevc:
libraries:
_runtime:
Expand Down
10 changes: 10 additions & 0 deletions tests/compiler/dart2js_extra/rti/simple_test.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
// Copyright (c) 2019, 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.

import 'dart:_rti' as rti;
import "package:expect/expect.dart";

main() {
Expect.equals('Rti', rti.testingRtiToString(null));
}

0 comments on commit 237d3ca

Please sign in to comment.