-
Notifications
You must be signed in to change notification settings - Fork 29
#3180. Add @JS() annotation tests. Part 3.
#3269
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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,71 @@ | ||
| // 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 An annotation on a JavaScript interop declaration. | ||
| /// ... | ||
| /// Specifying name customizes the JavaScript name to use, which can be used in | ||
| /// the following scenarios: | ||
| /// - Adding a JavaScript prefix to all the external top-level declarations, | ||
| /// static members, and constructors of a library by parameterizing the | ||
| /// annotation on the library with `name`. | ||
| /// - Specifying the JavaScript class to use for external static members and | ||
| /// constructors of an interop extension type by parameterizing the annotation | ||
| /// on the interop extension type with `name`. | ||
| /// - Renaming external declarations by parameterizing the annotation on the | ||
| /// member with `name`. | ||
| /// | ||
| /// @description Check that specifying `name` customizes the JavaScript `name` | ||
| /// to use. Test top-level declarations. | ||
| /// @author sgrekhov22@gmail.com | ||
|
|
||
| import 'dart:js_interop'; | ||
| import 'dart:js_interop_unsafe'; | ||
| import '../../../Utils/expect.dart'; | ||
| import '../js_utils.dart'; | ||
|
|
||
| @JS("jsVar1") | ||
| external int var1; | ||
|
|
||
| @JS("jsVar2") | ||
| external final String var2; | ||
|
|
||
| @JS("jsGetter") | ||
| external int get getter; | ||
|
|
||
| @JS("jsF1") | ||
| external int f1(); | ||
|
|
||
| @JS("jsF2") | ||
| external int f2(int v); | ||
|
|
||
| @JS("jsSetter") | ||
| external void set setter(int v); | ||
|
|
||
| main() { | ||
| eval(r''' | ||
| globalThis.jsVar1 = 42; | ||
| globalThis.jsVar2 = "var2 value"; | ||
| globalThis.jsGetter = -1; | ||
| globalThis.jsF1 = function() {return 1;}; | ||
| globalThis.jsF2 = function(v) {return v;}; | ||
| globalThis.jsSetter = 0; | ||
|
|
||
| globalThis.var1 = -42; | ||
| globalThis.var2 = "Wrong var2 value"; | ||
| globalThis.getter = -100; | ||
| globalThis.f1 = function() {return 100;}; | ||
| globalThis.f2 = function(v) {return 100 + v;}; | ||
| globalThis.setter = 100; | ||
| '''); | ||
| Expect.equals(42, var1); | ||
| Expect.equals("var2 value", var2); | ||
| Expect.equals(-1, getter); | ||
| Expect.equals(1, f1()); | ||
| Expect.equals(2, f2(2)); | ||
| setter = 3; | ||
| Expect.equals(3, (globalContext["jsSetter"] as JSNumber).toDartInt); | ||
| var1 = -1; | ||
| eval("globalThis.jsVar1++"); | ||
| Expect.equals(0, var1); | ||
| } |
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,51 @@ | ||
| // 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 An annotation on a JavaScript interop declaration. | ||
| /// ... | ||
| /// Specifying name customizes the JavaScript name to use, which can be used in | ||
| /// the following scenarios: | ||
| /// - Adding a JavaScript prefix to all the external top-level declarations, | ||
| /// static members, and constructors of a library by parameterizing the | ||
| /// annotation on the library with `name`. | ||
| /// - Specifying the JavaScript class to use for external static members and | ||
| /// constructors of an interop extension type by parameterizing the annotation | ||
| /// on the interop extension type with `name`. | ||
| /// - Renaming external declarations by parameterizing the annotation on the | ||
| /// member with `name`. | ||
| /// | ||
| /// @description Check that specifying `name` customizes the JavaScript `name` | ||
| /// to use. Test extension type declaration. | ||
| /// @author sgrekhov22@gmail.com | ||
|
|
||
| import 'dart:js_interop'; | ||
| import 'dart:js_interop_unsafe'; | ||
| import '../../../Utils/expect.dart'; | ||
| import '../js_utils.dart'; | ||
|
|
||
| @JS("JSEt") | ||
| extension type ET(JSObject _) implements JSObject { | ||
| external String foo(); | ||
| external static String bar(); | ||
| } | ||
|
|
||
| main() { | ||
| eval(r''' | ||
| class JSEt { | ||
| foo() { | ||
| return "foo() from JSEt"; | ||
| } | ||
|
|
||
| static bar() { | ||
| return "bar() from JSEt"; | ||
| } | ||
| } | ||
| globalThis.JSEt = JSEt; | ||
| globalThis.et = new JSEt(); | ||
| '''); | ||
|
|
||
| ET et = ET(globalContext["et"] as JSObject); | ||
| Expect.equals("foo() from JSEt", et.foo()); | ||
| Expect.equals("bar() from JSEt", ET.bar()); | ||
| } | ||
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,83 @@ | ||
| // 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 An annotation on a JavaScript interop declaration. | ||
| /// ... | ||
| /// Specifying name customizes the JavaScript name to use, which can be used in | ||
| /// the following scenarios: | ||
| /// - Adding a JavaScript prefix to all the external top-level declarations, | ||
| /// static members, and constructors of a library by parameterizing the | ||
| /// annotation on the library with `name`. | ||
| /// - Specifying the JavaScript class to use for external static members and | ||
| /// constructors of an interop extension type by parameterizing the annotation | ||
| /// on the interop extension type with `name`. | ||
| /// - Renaming external declarations by parameterizing the annotation on the | ||
| /// member with `name`. | ||
| /// | ||
| /// @description Check that specifying `name` customizes the JavaScript `name` | ||
| /// to use. Test members of an extension type. | ||
| /// @author sgrekhov22@gmail.com | ||
|
|
||
| import 'dart:js_interop'; | ||
| import 'dart:js_interop_unsafe'; | ||
| import '../../../Utils/expect.dart'; | ||
| import '../js_utils.dart'; | ||
|
|
||
| extension type ET(JSObject _) implements JSObject { | ||
| @JS("jsVar1") | ||
| external int var1; | ||
|
|
||
| @JS("jsVar2") | ||
| external final String var2; | ||
|
|
||
| @JS("jsX") | ||
| external int get x; | ||
|
|
||
| @JS("jsX") | ||
| external void set x(int v); | ||
|
|
||
| @JS("jsF1") | ||
| external int f1(); | ||
|
|
||
| @JS("jsF2") | ||
| external int f2(int v); | ||
| } | ||
|
|
||
| main() { | ||
| eval(r''' | ||
| class ET { | ||
| constructor(v1, v2) { | ||
| this.jsVar1 = v1; | ||
| this.jsVar2 = v2; | ||
| this._x = 0; | ||
| } | ||
|
|
||
| get jsX() { | ||
| return this._x; | ||
| } | ||
|
|
||
| set jsX(v) { | ||
| this._x = v; | ||
| } | ||
|
|
||
| jsF1() { | ||
| return this.jsVar1; | ||
| } | ||
|
|
||
| jsF2(v) { | ||
| return v; | ||
| } | ||
| } | ||
| globalThis.et = new ET(1, "two"); | ||
| '''); | ||
|
|
||
| ET et = ET(globalContext["et"] as JSObject); | ||
| Expect.equals(1, et.var1); | ||
| Expect.equals("two", et.var2); | ||
| Expect.equals(0, et.x); | ||
| Expect.equals(1, et.f1()); | ||
| Expect.equals(2, et.f2(2)); | ||
| et.x = 3; | ||
| Expect.equals(3, et.x); | ||
| } |
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,79 @@ | ||
| // 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 An annotation on a JavaScript interop declaration. | ||
| /// ... | ||
| /// Specifying name customizes the JavaScript name to use, which can be used in | ||
| /// the following scenarios: | ||
| /// - Adding a JavaScript prefix to all the external top-level declarations, | ||
| /// static members, and constructors of a library by parameterizing the | ||
| /// annotation on the library with `name`. | ||
| /// - Specifying the JavaScript class to use for external static members and | ||
| /// constructors of an interop extension type by parameterizing the annotation | ||
| /// on the interop extension type with `name`. | ||
| /// - Renaming external declarations by parameterizing the annotation on the | ||
| /// member with `name`. | ||
| /// | ||
| /// @description Check that specifying `name` customizes the JavaScript `name` | ||
| /// to use. Test static members of an extension type. | ||
| /// @author sgrekhov22@gmail.com | ||
|
|
||
| import 'dart:js_interop'; | ||
| import 'dart:js_interop_unsafe'; | ||
| import '../../../Utils/expect.dart'; | ||
| import '../js_utils.dart'; | ||
|
|
||
| extension type ET(JSObject _) implements JSObject { | ||
| @JS("jsVar1") | ||
| external static int var1; | ||
|
|
||
| @JS("jsVar2") | ||
| external static final String var2; | ||
|
|
||
| @JS("jsX") | ||
| external static int get x; | ||
|
|
||
| @JS("jsX") | ||
| external static void set x(int v); | ||
|
|
||
| @JS("jsF1") | ||
| external static int f1(); | ||
|
|
||
| @JS("jsF2") | ||
| external static int f2(int v); | ||
| } | ||
|
|
||
| main() { | ||
| eval(r''' | ||
| class ET { | ||
| static jsVar1 = 1; | ||
| static jsVar2 = "two"; | ||
| static _x = 0; | ||
|
|
||
| static get jsX() { | ||
| return ET._x; | ||
| } | ||
|
|
||
| static set jsX(v) { | ||
| ET._x = v; | ||
| } | ||
|
|
||
| static jsF1() { | ||
| return ET.jsVar1; | ||
| } | ||
|
|
||
| static jsF2(v) { | ||
| return v; | ||
| } | ||
| } | ||
| globalThis.ET = ET; | ||
| '''); | ||
| Expect.equals(1, ET.var1); | ||
| Expect.equals("two", ET.var2); | ||
| Expect.equals(0, ET.x); | ||
| Expect.equals(1, ET.f1()); | ||
| Expect.equals(2, ET.f2(2)); | ||
| ET.x = 3; | ||
| Expect.equals(3, ET.x); | ||
| } |
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,47 @@ | ||
| // 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 An annotation on a JavaScript interop declaration. | ||
| /// | ||
| /// This annotation defines a given library, top-level external declaration, or | ||
| /// extension type as a JavaScript interop declaration. | ||
| /// | ||
| /// @description Check that a library directive can be annotated with a `@JS()` | ||
| /// annotation. | ||
| /// @author sgrekhov22@gmail.com | ||
|
|
||
| @JS("lib1") | ||
| library; | ||
|
|
||
| import 'dart:async'; | ||
| import 'dart:js_interop'; | ||
| import 'dart:js_interop_unsafe'; | ||
| import '../../../Utils/expect.dart'; | ||
| import '../js_utils.dart'; | ||
|
|
||
| @JS() | ||
eernstg marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| external int answer(); | ||
|
|
||
| final completer = Completer<String>(); | ||
|
|
||
| void complete(String value) { | ||
| completer.complete(value); | ||
| } | ||
|
|
||
eernstg marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| main() { | ||
| globalContext["complete"] = complete.toJS; | ||
| eval(r''' | ||
| var lib1; | ||
| (async () => { | ||
| lib1 = await import('/root_dart/tests/co19/src/LibTest/js_interop/module.js'); | ||
| })().then(function(v) { | ||
| globalThis.complete(""); | ||
| }); | ||
| '''); | ||
| asyncStart(); | ||
| completer.future.then((_) { | ||
| Expect.equals(42, answer()); // calls lib1.answer() | ||
| asyncEnd(); | ||
| }); | ||
| } | ||
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
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 |
|---|---|---|
|
|
@@ -4,3 +4,7 @@ export function A(id, name) { | |
| } | ||
|
|
||
| export function B() {} | ||
|
|
||
| export function answer() { | ||
| return 42; | ||
| } | ||
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.