Skip to content

Commit

Permalink
dart:mirrors workaround for generic methods
Browse files Browse the repository at this point in the history
Fixes #29509

R=jmesserly@google.com

Review-Url: https://codereview.chromium.org/2852723002 .
  • Loading branch information
vsmenon committed Apr 29, 2017
1 parent 725261e commit 7bedf63
Show file tree
Hide file tree
Showing 12 changed files with 93 additions and 29 deletions.
19 changes: 14 additions & 5 deletions pkg/dev_compiler/lib/js/amd/dart_sdk.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion pkg/dev_compiler/lib/js/amd/dart_sdk.js.map

Large diffs are not rendered by default.

19 changes: 14 additions & 5 deletions pkg/dev_compiler/lib/js/common/dart_sdk.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion pkg/dev_compiler/lib/js/common/dart_sdk.js.map

Large diffs are not rendered by default.

19 changes: 14 additions & 5 deletions pkg/dev_compiler/lib/js/es6/dart_sdk.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion pkg/dev_compiler/lib/js/es6/dart_sdk.js.map

Large diffs are not rendered by default.

19 changes: 14 additions & 5 deletions pkg/dev_compiler/lib/js/legacy/dart_sdk.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion pkg/dev_compiler/lib/js/legacy/dart_sdk.js.map

Large diffs are not rendered by default.

Binary file modified pkg/dev_compiler/lib/sdk/ddc_sdk.sum
Binary file not shown.
13 changes: 13 additions & 0 deletions pkg/dev_compiler/tool/input_sdk/private/ddc_runtime/types.dart
Original file line number Diff line number Diff line change
Expand Up @@ -512,6 +512,19 @@ functionType(returnType, args, extra) =>
definiteFunctionType(returnType, args, extra) =>
_functionType(true, returnType, args, extra);

///
/// TODO(vsm): Remove when mirrors is deprecated.
/// This is a temporary workaround to support dart:mirrors, which doesn't
/// understand generic methods.
///
getFunctionTypeMirror(AbstractFunctionType type) {
if (type is GenericFunctionType) {
var typeArgs = new List.filled(type.formalCount, dynamic);
return type.instantiate(typeArgs);
}
return type;
}

bool isType(obj) => JS(
'',
'''(() => {
Expand Down
10 changes: 5 additions & 5 deletions pkg/dev_compiler/tool/input_sdk/private/js_mirrors.dart
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,10 @@ dynamic _getMixins(type) {
return JS('', '#.getMixins(#, [])', _dart, type);
}

dynamic _getFunctionType(type) {
return JS('', '#.getFunctionTypeMirror(#)', _dart, type);
}

typedef T _Lazy<T>();

dynamic _getESSymbol(Symbol symbol) =>
Expand Down Expand Up @@ -627,11 +631,7 @@ class JsMethodMirror extends JsMirror implements MethodMirror {

// TODO(vsm): Handle generic function types properly. Or deprecate mirrors
// before we need to!
if (JS('bool', 'typeof(#) == "function"', ftype)) {
// Instantiate the generic version.
// TODO(vsm): Can't use arguments.length on arrow function.
ftype = JS('', '#.apply(null, #)', ftype, [dynamic, dynamic, dynamic]);
}
ftype = _getFunctionType(ftype);

// TODO(vsm): Add named args.
List args = ftype.args;
Expand Down
15 changes: 15 additions & 0 deletions tests/lib_strong/mirrors/generic_method_test.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
// Copyright (c) 2017, 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:mirrors';
import 'package:expect/expect.dart';

class Foo {
T bar<T>() => null;
}

void main() {
var type = reflectClass(Foo);
Expect.isTrue(type.declarations.keys.contains(#bar));
}

0 comments on commit 7bedf63

Please sign in to comment.