-
Notifications
You must be signed in to change notification settings - Fork 29
#3180. Add @staticInterop tests. Part 2.
#3257
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,132 @@ | ||
| // Copyright (c) 2025, 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. | ||
|
|
||
| /// @assertion `staticInterop` enables the JS annotated class to be treated as | ||
| /// a "static" interop class. | ||
| /// | ||
| /// These classes implicitly all erase to the internal interceptor | ||
| /// `JavaScriptObject`, so they can be freely casted to and from other | ||
| /// [staticInterop] types, `dart:html` types, and `JSObject` from | ||
| /// `dart:js_interop`. Non-[staticInterop] `package:js` types can be casted to | ||
| /// [staticInterop] types, but the reverse can fail if the underlying value is a | ||
| /// `@Native`-reserved type (like `dart:html` types). | ||
| /// | ||
| /// [staticInterop] classes have the following restrictions: | ||
| /// - They must contain a [JS] annotation, either from this library or from | ||
| /// `dart:js_interop`. | ||
| /// - They should not contain any instance members, inherited or otherwise, and | ||
| /// should instead use static extension members, which can be external or | ||
| /// non-external. | ||
| /// - They can only contain factories and `static` members. They can be | ||
| /// combined with [anonymous] to make external factories create new | ||
| /// JavaScript object literals instead. | ||
| /// - They should not implement, extend, or mixin non-[staticInterop] classes | ||
| /// and vice-versa. | ||
| /// - The annotation should only be applied to non-mixin classes and no other | ||
| /// declarations. | ||
| /// | ||
| /// @description Checks that a class annotated with `@staticInterop` can be | ||
| /// `abstract`, `base`, `final`, `sealed` or `interface`. | ||
| /// @author sgrekhov22@gmail.com | ||
|
|
||
| import 'dart:js_interop'; | ||
| import 'dart:js_interop_unsafe'; | ||
| import '../../Utils/expect.dart'; | ||
| import 'js_utils.dart'; | ||
|
|
||
| @staticInterop | ||
| @JS() | ||
| abstract class C1 { | ||
| } | ||
|
|
||
| @staticInterop | ||
| @JS() | ||
| base class C2 { | ||
| } | ||
|
|
||
| @staticInterop | ||
| @JS() | ||
| final class C3 { | ||
| } | ||
|
|
||
| @staticInterop | ||
| @JS() | ||
| sealed class C4 { | ||
| } | ||
|
|
||
| @staticInterop | ||
| @JS() | ||
| interface class C5 { | ||
| } | ||
|
|
||
| extension Ext1 on C1 { | ||
| external String getString(); | ||
| external int getNumber(); | ||
| external bool getBool(); | ||
| } | ||
|
|
||
| extension Ext2 on C2 { | ||
| external String getString(); | ||
| external int getNumber(); | ||
| external bool getBool(); | ||
| } | ||
|
|
||
| extension Ext3 on C3 { | ||
| external String getString(); | ||
| external int getNumber(); | ||
| external bool getBool(); | ||
| } | ||
|
|
||
| extension Ext4 on C4 { | ||
| external String getString(); | ||
| external int getNumber(); | ||
| external bool getBool(); | ||
| } | ||
|
|
||
| extension Ext5 on C5 { | ||
| external String getString(); | ||
| external int getNumber(); | ||
| external bool getBool(); | ||
| } | ||
|
|
||
| main() { | ||
| eval(r''' | ||
| globalThis.obj = { | ||
| getString: function () { | ||
| return "I'm JS String"; | ||
| }, | ||
| getNumber: function () { | ||
| return 42; | ||
| }, | ||
| getBool: function () { | ||
| return true; | ||
| } | ||
| }; | ||
| '''); | ||
|
|
||
| C1 c1 = globalContext["obj"] as C1; | ||
| Expect.equals("I'm JS String", c1.getString()); | ||
| Expect.equals(42, c1.getNumber()); | ||
| Expect.equals(true, c1.getBool()); | ||
|
|
||
| C2 c2 = globalContext["obj"] as C2; | ||
| Expect.equals("I'm JS String", c2.getString()); | ||
| Expect.equals(42, c2.getNumber()); | ||
| Expect.equals(true, c2.getBool()); | ||
|
|
||
| C3 c3 = globalContext["obj"] as C3; | ||
| Expect.equals("I'm JS String", c3.getString()); | ||
| Expect.equals(42, c3.getNumber()); | ||
| Expect.equals(true, c3.getBool()); | ||
|
|
||
| C4 c4 = globalContext["obj"] as C4; | ||
| Expect.equals("I'm JS String", c4.getString()); | ||
| Expect.equals(42, c4.getNumber()); | ||
| Expect.equals(true, c4.getBool()); | ||
|
|
||
| C5 c5 = globalContext["obj"] as C5; | ||
| Expect.equals("I'm JS String", c5.getString()); | ||
| Expect.equals(42, c5.getNumber()); | ||
| Expect.equals(true, c5.getBool()); | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,58 @@ | ||
| // Copyright (c) 2025, 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. | ||
|
|
||
| /// @assertion `staticInterop` enables the JS annotated class to be treated as | ||
| /// a "static" interop class. | ||
| /// | ||
| /// These classes implicitly all erase to the internal interceptor | ||
| /// `JavaScriptObject`, so they can be freely casted to and from other | ||
| /// [staticInterop] types, `dart:html` types, and `JSObject` from | ||
| /// `dart:js_interop`. Non-[staticInterop] `package:js` types can be casted to | ||
| /// [staticInterop] types, but the reverse can fail if the underlying value is a | ||
| /// `@Native`-reserved type (like `dart:html` types). | ||
| /// | ||
| /// [staticInterop] classes have the following restrictions: | ||
| /// - They must contain a [JS] annotation, either from this library or from | ||
| /// `dart:js_interop`. | ||
| /// - They should not contain any instance members, inherited or otherwise, and | ||
| /// should instead use static extension members, which can be external or | ||
| /// non-external. | ||
| /// - They can only contain factories and `static` members. They can be | ||
| /// combined with [anonymous] to make external factories create new | ||
| /// JavaScript object literals instead. | ||
| /// - They should not implement, extend, or mixin non-[staticInterop] classes | ||
| /// and vice-versa. | ||
| /// - The annotation should only be applied to non-mixin classes and no other | ||
| /// declarations. | ||
| /// | ||
| /// @description Checks that it is a compile-time error if a class without | ||
| /// `@staticInterop` annotation extends or implements one with this annotation. | ||
| /// @author sgrekhov22@gmail.com | ||
|
|
||
| import 'dart:js_interop'; | ||
|
|
||
| @staticInterop | ||
| @JS() | ||
| class A {} | ||
|
|
||
| @staticInterop | ||
| @JS() | ||
| abstract interface class I {} | ||
|
|
||
| @JS() | ||
| class C1 extends A {} | ||
| // ^^ | ||
| // [analyzer] unspecified | ||
| // [web] unspecified | ||
|
|
||
| @JS() | ||
| class C2 implements I {} | ||
| // ^^ | ||
| // [analyzer] unspecified | ||
| // [web] unspecified | ||
|
|
||
| main() { | ||
| print(C1); | ||
| print(C2); | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,67 @@ | ||
| // Copyright (c) 2025, 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. | ||
|
|
||
| /// @assertion `staticInterop` enables the JS annotated class to be treated as | ||
| /// a "static" interop class. | ||
| /// | ||
| /// These classes implicitly all erase to the internal interceptor | ||
| /// `JavaScriptObject`, so they can be freely casted to and from other | ||
| /// [staticInterop] types, `dart:html` types, and `JSObject` from | ||
| /// `dart:js_interop`. Non-[staticInterop] `package:js` types can be casted to | ||
| /// [staticInterop] types, but the reverse can fail if the underlying value is a | ||
| /// `@Native`-reserved type (like `dart:html` types). | ||
| /// | ||
| /// [staticInterop] classes have the following restrictions: | ||
| /// - They must contain a [JS] annotation, either from this library or from | ||
| /// `dart:js_interop`. | ||
| /// - They should not contain any instance members, inherited or otherwise, and | ||
| /// should instead use static extension members, which can be external or | ||
| /// non-external. | ||
| /// - They can only contain factories and `static` members. They can be | ||
| /// combined with [anonymous] to make external factories create new | ||
| /// JavaScript object literals instead. | ||
| /// - They should not implement, extend, or mixin non-[staticInterop] classes | ||
| /// and vice-versa. | ||
| /// - The annotation should only be applied to non-mixin classes and no other | ||
| /// declarations. | ||
| /// | ||
| /// @description Checks that it is a compile-time error if a class annotated | ||
| /// with `@staticInterop` annotation extends, mixin or implements one without | ||
| /// this annotation. | ||
eernstg marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| /// @author sgrekhov22@gmail.com | ||
|
|
||
| import 'dart:js_interop'; | ||
|
|
||
| class A {} | ||
|
|
||
| mixin class M {} | ||
|
|
||
| abstract interface class I {} | ||
|
|
||
| @staticInterop | ||
| @JS() | ||
| class C1 extends A {} | ||
| // ^^ | ||
| // [analyzer] unspecified | ||
| // [web] unspecified | ||
|
|
||
| @staticInterop | ||
| @JS() | ||
| class C2 with M {} | ||
| // ^^ | ||
| // [analyzer] unspecified | ||
| // [web] unspecified | ||
|
|
||
| @staticInterop | ||
| @JS() | ||
| class C3 implements I {} | ||
| // ^^ | ||
| // [analyzer] unspecified | ||
| // [web] unspecified | ||
|
|
||
| main() { | ||
| print(C1); | ||
| print(C2); | ||
| print(C3); | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,82 @@ | ||
| // Copyright (c) 2025, 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. | ||
|
|
||
| /// @assertion `staticInterop` enables the JS annotated class to be treated as | ||
| /// a "static" interop class. | ||
| /// | ||
| /// These classes implicitly all erase to the internal interceptor | ||
| /// `JavaScriptObject`, so they can be freely casted to and from other | ||
| /// [staticInterop] types, `dart:html` types, and `JSObject` from | ||
| /// `dart:js_interop`. Non-[staticInterop] `package:js` types can be casted to | ||
| /// [staticInterop] types, but the reverse can fail if the underlying value is a | ||
| /// `@Native`-reserved type (like `dart:html` types). | ||
| /// | ||
| /// [staticInterop] classes have the following restrictions: | ||
| /// - They must contain a [JS] annotation, either from this library or from | ||
| /// `dart:js_interop`. | ||
| /// - They should not contain any instance members, inherited or otherwise, and | ||
| /// should instead use static extension members, which can be external or | ||
| /// non-external. | ||
| /// - They can only contain factories and `static` members. They can be | ||
| /// combined with [anonymous] to make external factories create new | ||
| /// JavaScript object literals instead. | ||
| /// - They should not implement, extend, or mixin non-[staticInterop] classes | ||
| /// and vice-versa. | ||
| /// - The annotation should only be applied to non-mixin classes and no other | ||
| /// declarations. | ||
| /// | ||
| /// @description Checks that a class annotated with `@staticInterop` can extend, | ||
| /// or implement another class/mixin annotated with `@staticInterop`. | ||
| /// @author sgrekhov22@gmail.com | ||
|
|
||
| import 'dart:js_interop'; | ||
| import 'dart:js_interop_unsafe'; | ||
| import '../../Utils/expect.dart'; | ||
| import 'js_utils.dart'; | ||
|
|
||
| @staticInterop | ||
| @JS() | ||
| abstract class A {} | ||
|
|
||
| extension Ext on A { | ||
| external String getString(); | ||
| external int getNumber(); | ||
| external bool getBool(); | ||
| } | ||
|
|
||
| @staticInterop | ||
| @JS() | ||
| class C1 extends A { | ||
| } | ||
|
|
||
| @staticInterop | ||
| @JS() | ||
| class C2 implements A { | ||
| } | ||
|
|
||
| main() { | ||
| eval(r''' | ||
| globalThis.obj = { | ||
| getString: function () { | ||
| return "I'm JS String"; | ||
| }, | ||
| getNumber: function () { | ||
| return 42; | ||
| }, | ||
| getBool: function () { | ||
| return true; | ||
| } | ||
| }; | ||
| '''); | ||
|
|
||
| C1 c1 = globalContext["obj"] as C1; | ||
| Expect.equals("I'm JS String", c1.getString()); | ||
| Expect.equals(42, c1.getNumber()); | ||
| Expect.equals(true, c1.getBool()); | ||
|
|
||
| C2 c2 = globalContext["obj"] as C2; | ||
| Expect.equals("I'm JS String", c2.getString()); | ||
| Expect.equals(42, c2.getNumber()); | ||
| Expect.equals(true, c2.getBool()); | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.