Skip to content

Commit

Permalink
[dart2js] new-rti: Implement basic is-test
Browse files Browse the repository at this point in the history
Also add a few more type literal tests.

TBR=fishythefish@google.co,

Change-Id: Iab5ca4a5f8d224f67deaf1fe391e17ece46bde59
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/107600
Reviewed-by: Stephen Adams <sra@google.com>
Commit-Queue: Stephen Adams <sra@google.com>
  • Loading branch information
rakudrama authored and commit-bot@chromium.org committed Jul 1, 2019
1 parent 39b7125 commit ca4b6e5
Show file tree
Hide file tree
Showing 6 changed files with 66 additions and 3 deletions.
7 changes: 7 additions & 0 deletions pkg/compiler/lib/src/ssa/builder_kernel.dart
Expand Up @@ -5302,6 +5302,13 @@ class KernelSsaGraphBuilder extends ir.Visitor {
return;
}

if (options.experimentNewRti) {
HInstruction rti =
_typeBuilder.analyzeTypeArgumentNewRti(typeValue, sourceElement);
push(HIsTest(typeValue, expression, rti, _abstractValueDomain.boolType));
return;
}

if (typeValue is FunctionType) {
HInstruction representation =
_typeBuilder.analyzeTypeArgument(typeValue, sourceElement);
Expand Down
2 changes: 2 additions & 0 deletions pkg/compiler/lib/src/ssa/codegen.dart
Expand Up @@ -3340,6 +3340,8 @@ class SsaCodeGenerator implements HVisitor, HBlockInformationVisitor {

@override
visitIsTest(HIsTest node) {
_registry.registerTypeUse(new TypeUse.isCheck(node.dartType));

use(node.typeInput);
js.Expression first = pop();
use(node.checkedInput);
Expand Down
5 changes: 4 additions & 1 deletion pkg/compiler/lib/src/ssa/nodes.dart
Expand Up @@ -4348,7 +4348,10 @@ class HTypeInfoExpression extends HInstruction {
/// lowered to other instructions, so this instruction remains for types that
/// depend on type variables and complex types.
class HIsTest extends HInstruction {
HIsTest(HInstruction checked, HInstruction rti, AbstractValue type)
final DartType dartType;

HIsTest(
this.dartType, HInstruction checked, HInstruction rti, AbstractValue type)
: super([rti, checked], type) {
setUseGvn();
}
Expand Down
4 changes: 2 additions & 2 deletions sdk/lib/_internal/js_runtime/lib/rti.dart
Expand Up @@ -329,8 +329,8 @@ bool _generalIsTestImplementation(object) {
// This static method is installed on an Rti object as a JavaScript instance
// method. The Rti object is 'this'.
Rti testRti = _castToRti(JS('', 'this'));
throw UnimplementedError(
'${Error.safeToString(object)} is ${_rtiToString(testRti, null)}');
Rti objectRti = instanceType(object);
return isSubtype(_theUniverse(), objectRti, testRti);
}

/// Called from generated code.
Expand Down
11 changes: 11 additions & 0 deletions tests/compiler/dart2js_extra/rti/runtime_type_2_test.dart
Expand Up @@ -14,9 +14,20 @@ Type grabList<T>() => grab<List<T>>();

main() {
Expect.equals('int', grab<int>().toString());

Expect.identical(int, grab<int>());
Expect.identical(dynamic, grab<dynamic>());
Expect.identical(Object, grab<Object>());
Expect.identical(Null, grab<Null>());

Expect.equals('List<int>', grabList<int>().toString());
Expect.equals('List<Null>', grabList<Null>().toString());

Expect.equals('List<dynamic>', (List).toString());

Expect.equals('dynamic', (dynamic).toString());
Expect.equals('Object', (Object).toString());
Expect.equals('Null', (Null).toString());

Expect.equals(List, grabList<dynamic>());
}
40 changes: 40 additions & 0 deletions tests/compiler/dart2js_extra/rti/simple_is_test.dart
@@ -0,0 +1,40 @@
// 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.
//
// dart2jsOptions=--experiment-new-rti --no-minify

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

class Thingy {}

class Generic<AA> {
checkMethod(o) => o is AA;
}

@pragma('dart2js:noInline')
void check<T>(o) {
Expect.isTrue(o is T);
Expect.isFalse(o is! T);
}

main() {
check<Thingy>(Thingy());

check<Generic<int>>(Generic<int>());

check<Generic<dynamic>>(Generic<dynamic>());
check<Generic<Object>>(Generic<Object>());
check<Generic<Object>>(Generic<dynamic>());
check<Generic<dynamic>>(Generic<Object>());

Expect.isTrue(Generic<Thingy>().checkMethod(Thingy()));
Expect.isTrue(Generic<Object>().checkMethod(Object()));
Expect.isTrue(Generic<Object>().checkMethod(Thingy()));
Expect.isFalse(Generic<Thingy>().checkMethod(Object()));
Expect.isTrue(Generic<dynamic>().checkMethod(Object()));

Expect.isFalse(Generic<Thingy>().checkMethod(123));
Expect.isFalse(Generic<Thingy>().checkMethod(Object()));
}

0 comments on commit ca4b6e5

Please sign in to comment.