[ddc] Do not require a @JS annotation at the library level.

Dart2js allows to declare JS-interp members, even if the enclosing library
doesn't have a JS annotation. This relaxes DDC to do the same.

Change-Id: I733d1cbb308692d89b8cd443cbde0ed30637e48e
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/177780
Reviewed-by: Srujan Gaddam <srujzs@google.com>
Commit-Queue: Sigmund Cherem <sigmund@google.com>
This commit is contained in:
Sigmund Cherem 2021-01-20 22:11:23 +00:00 committed by commit-bot@chromium.org
parent 9fd4b10fe5
commit 0845ebaad8
3 changed files with 45 additions and 9 deletions

View file

@ -120,14 +120,18 @@ bool hasJSInteropAnnotation(Class c) => c.annotations.any(isPublicJSAnnotation);
/// Returns true iff this element is a JS interop member.
///
/// The element's library must have `@JS(...)` annotation from `package:js`.
/// If the element is a class, it must also be marked with `@JS`. Other
/// elements, such as class members and top-level functions/accessors, should
/// be marked `external`.
/// JS annotations are required explicitly on classes. Other elements, such as
/// class members and top-level functions/accessors, should be marked `external`
/// and should have directly or indirectly a `JS` annotation. It is sufficient
/// if the annotation is in the procedure itself or an enclosing element like
/// the class or library.
bool usesJSInterop(NamedNode n) {
var library = getLibrary(n);
return library != null &&
library.annotations.any(isPublicJSAnnotation) &&
(n is Procedure && n.isExternal ||
n is Class && n.annotations.any(isPublicJSAnnotation));
if (n is Member && n.isExternal) {
return n.enclosingLibrary.annotations.any(isPublicJSAnnotation) ||
n.annotations.any(isPublicJSAnnotation) ||
(n.enclosingClass?.annotations?.any(isPublicJSAnnotation) ?? false);
} else if (n is Class) {
return n.annotations.any(isPublicJSAnnotation);
}
return false;
}

View file

@ -0,0 +1,15 @@
// Copyright (c) 2021, 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.
// Test that JS-interop works without a @JS annotation on the library itself.
import 'package:js/js.dart';
import 'package:expect/expect.dart';
@JS()
external dynamic eval(String code);
void main() {
Expect.equals(2, eval("2"));
}

View file

@ -0,0 +1,17 @@
// Copyright (c) 2021, 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.9
// Test that JS-interop works without a @JS annotation on the library itself.
import 'package:js/js.dart';
import 'package:expect/expect.dart';
@JS()
external dynamic eval(String code);
void main() {
Expect.equals(2, eval("2"));
}