Skip to content

Commit 6fec16d

Browse files
srujzsCommit Queue
authored andcommitted
[dart:js_interop] Require top-level externals to have @js
To refuse confusion between dart:ffi and dart:js_interop, top-level externals will need to be annotated when using dart:js_interop. Change-Id: I1e4887eb32f135df94426e43fc885346f1b9f1b1 Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/310485 Reviewed-by: Joshua Litt <[email protected]> Commit-Queue: Srujan Gaddam <[email protected]>
1 parent ab884b7 commit 6fec16d

File tree

3 files changed

+56
-3
lines changed

3 files changed

+56
-3
lines changed

pkg/_js_interop_checks/lib/js_interop_checks.dart

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -894,8 +894,8 @@ class JsInteropChecks extends RecursiveVisitor {
894894
/// - inside a JS interop class
895895
/// - inside an extension on a JS interop or @Native annotatable
896896
/// - inside a JS interop inline class
897-
/// - a top level member that is JS interop annotated or in a JS interop
898-
/// library
897+
/// - a top level member that is JS interop annotated or in a package:js JS
898+
/// interop library
899899
bool _isJSInteropMember(Member member) {
900900
if (member.isExternal) {
901901
if (_classHasJSAnnotation) return true;
@@ -906,7 +906,13 @@ class JsInteropChecks extends RecursiveVisitor {
906906
return _inlineExtensionIndex.getInlineClass(member) != null;
907907
}
908908
if (member.enclosingClass == null) {
909-
return hasJSInteropAnnotation(member) || _libraryHasJSAnnotation;
909+
// dart:js_interop requires top-levels to be @JS-annotated. package:js
910+
// historically does not have this restriction. We add this restriction
911+
// to refuse confusion on what an external member does now that we can
912+
// have dart:ffi and dart:js_interop in the same code via dart2wasm.
913+
final libraryHasPkgJSAnnotation =
914+
_libraryHasJSAnnotation && !_libraryHasDartJSInteropAnnotation;
915+
return hasJSInteropAnnotation(member) || libraryHasPkgJSAnnotation;
910916
}
911917
}
912918

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
// Copyright (c) 2023, the Dart project authors. Please see the AUTHORS file
2+
// for details. All rights reserved. Use of this source code is governed by a
3+
// BSD-style license that can be found in the LICENSE file.
4+
5+
// Test that top-level external members need an @JS annotation even if the
6+
// library has one when using dart:js_interop.
7+
8+
@JS()
9+
library top_level_member_annotation_static_test;
10+
11+
import 'dart:js_interop';
12+
13+
external int field;
14+
// ^
15+
// [web] Only JS interop members may be 'external'.
16+
17+
external int finalField;
18+
// ^
19+
// [web] Only JS interop members may be 'external'.
20+
21+
external int get getter;
22+
// ^
23+
// [web] Only JS interop members may be 'external'.
24+
25+
external set setter(_);
26+
// ^
27+
// [web] Only JS interop members may be 'external'.
28+
29+
external int method();
30+
// ^
31+
// [web] Only JS interop members may be 'external'.
32+
33+
@JS()
34+
external int annotatedField;
35+
36+
@JS()
37+
external int annotatedFinalField;
38+
39+
@JS()
40+
external int get annotatedGetter;
41+
42+
@JS()
43+
external set annotatedSetter(_);
44+
45+
@JS()
46+
external int annotatedMethod();

tests/lib/lib.status

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ js/(?!export|static_interop_test)*: SkipByDesign
1717
js/static_interop_test/external_static_member_lowerings_trusttypes_test: SkipByDesign # Tests @trustTypes, which is unsupported on dart2wasm.
1818
js/static_interop_test/inline_class/non_interop_inline_class_static_test: SkipByDesign # Some external checks are not run on dart2wasm.
1919
js/static_interop_test/static_external_extension_members_static_test: SkipByDesign # dart:html not supported on dart2wasm.
20+
js/static_interop_test/top_level_member_annotation_static_test: SkipByDesign # Some external checks are not run on dart2wasm.
2021
mirrors/*: SkipByDesign
2122
web/*: SkipByDesign
2223

0 commit comments

Comments
 (0)