[dart2js] new-rti: Make all closures have class type 'Closure'

Change-Id: I6713449b61a717907a5f8eac8fa3e2bea7e11ddd
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/111202
Commit-Queue: Mayank Patke <fishythefish@google.com>
Reviewed-by: Mayank Patke <fishythefish@google.com>
This commit is contained in:
Stephen Adams 2019-07-30 01:29:47 +00:00 committed by commit-bot@chromium.org
parent f9f005cdb9
commit 4b35a044c9
2 changed files with 43 additions and 0 deletions

View file

@ -332,6 +332,18 @@ Rti instanceType(object) {
'depends:none;effects:none;', JsBuiltin.dartObjectConstructor))) {
var rti = JS('', r'#[#]', object, JS_GET_NAME(JsGetName.RTI_NAME));
if (rti != null) return _castToRti(rti);
// Subclasses of Closure are synthetic classes, so make them appear to be
// the 'Closure' class.
// TODO(sra): Can this be done less expensively, e.g. by putting $ti on the
// prototype of Closure class?
var closureClassConstructor = JS_BUILTIN(
'depends:none;effects:none;', JsBuiltin.dartClosureConstructor);
if (closureClassConstructor != null &&
_Utils.instanceOf(object, closureClassConstructor)) {
return _instanceTypeFromConstructor(closureClassConstructor);
}
return _instanceTypeFromConstructor(JS('', '#.constructor', object));
}

View file

@ -0,0 +1,31 @@
// 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
// Test that some closures are 'is Function'.
import "package:expect/expect.dart";
@pragma('dart2js:noInline')
confuse(x) => x;
main() {
// static tear-off.
Expect.isTrue(confuse(main) is Function);
// instance tear-off.
Expect.isTrue(confuse([].add) is Function);
// function expression.
Expect.isTrue(confuse(() => 1) is Function);
// local function.
int add1(int x) => x;
Expect.isTrue(confuse(add1) is Function);
Expect.isFalse(confuse(null) is Function);
Expect.isFalse(confuse(1) is Function);
}