Skip to content

Commit 7ff6839

Browse files
authored
#3180. Add more tests for JS interop. Part 3. (#3278)
Add more tests for JS interop. Part 3.
1 parent c8ddbef commit 7ff6839

File tree

7 files changed

+420
-0
lines changed

7 files changed

+420
-0
lines changed
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
// Copyright (c) 2025, 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+
/// @assertion Within an interop type, you can declare several different types
6+
/// of external interop members:
7+
/// - Constructors. When called, constructors with only positional parameters
8+
/// create a new JS object whose constructor is defined by the name of the
9+
/// extension type using `new`.
10+
///
11+
/// @description Check that parameters with private names and wildcard can be
12+
/// used in an external constructor of a JS interop type.
13+
/// @author [email protected]
14+
15+
import 'dart:js_interop';
16+
import '../../Utils/expect.dart';
17+
import 'js_utils.dart';
18+
19+
extension type ET._(JSObject _) implements JSObject {
20+
external ET(int _, String? _name);
21+
22+
external int get id;
23+
external String? get name;
24+
}
25+
26+
@JS("ET")
27+
extension type ET2._(JSObject _) implements JSObject {
28+
external factory ET2(int _id, String? _);
29+
30+
external int get id;
31+
external String? get name;
32+
}
33+
34+
main() {
35+
eval(r'''
36+
class ET {
37+
constructor(id, name) {
38+
this._id = id;
39+
this._name = name;
40+
}
41+
get id() {
42+
return this._id;
43+
}
44+
get name() {
45+
return this._name;
46+
}
47+
}
48+
globalThis.ET = ET;
49+
''');
50+
ET et1 = ET(1, "one");
51+
Expect.equals(1, et1.id);
52+
Expect.equals("one", et1.name);
53+
54+
ET2 et2 = ET2(2, "two");
55+
Expect.equals(2, et2.id);
56+
Expect.equals("two", et2.name);
57+
}
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
// Copyright (c) 2025, 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+
/// @assertion Within an interop type, you can declare several different types
6+
/// of external interop members:
7+
/// - Constructors. When called, constructors with only positional parameters
8+
/// create a new JS object whose constructor is defined by the name of the
9+
/// extension type using `new`.
10+
///
11+
/// @description Check that a constructor of a JS interop type with a private
12+
/// name can be used in JS interop.
13+
/// @author [email protected]
14+
15+
import 'dart:js_interop';
16+
import '../../Utils/expect.dart';
17+
import 'js_utils.dart';
18+
19+
extension type ET._(JSObject _) implements JSObject {
20+
external ET._create(int id, String? name);
21+
22+
external int get id;
23+
external String? get name;
24+
}
25+
26+
@JS("ET")
27+
extension type ET2._(JSObject _) implements JSObject {
28+
external factory ET2._create(int id, String? name);
29+
30+
external int get id;
31+
external String? get name;
32+
}
33+
34+
main() {
35+
eval(r'''
36+
class ET {
37+
constructor(id, name) {
38+
this._id = id;
39+
this._name = name;
40+
}
41+
get id() {
42+
return this._id;
43+
}
44+
get name() {
45+
return this._name;
46+
}
47+
}
48+
globalThis.ET = ET;
49+
''');
50+
ET et1 = ET._create(1, "one");
51+
Expect.equals(1, et1.id);
52+
Expect.equals("one", et1.name);
53+
54+
ET2 et2 = ET2._create(2, "two");
55+
Expect.equals(2, et2.id);
56+
Expect.equals("two", et2.name);
57+
}
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
// Copyright (c) 2025, 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+
/// @assertion Within an interop type, you can declare several different types
6+
/// of external interop members:
7+
/// ...
8+
/// - Object literal constructors. It's sometimes useful to create a JS object
9+
/// literal that simply contains a number of properties and their values. In
10+
/// order to do this, declare a constructor with only named parameters, where
11+
/// the names of the parameters match the property names.
12+
///
13+
/// @description Check that default values of a constructor of a JS interop type
14+
/// with named parameters are ignored.
15+
/// @author [email protected]
16+
/// @issue 61289
17+
18+
import 'dart:js_interop';
19+
import 'dart:js_interop_unsafe';
20+
import '../../Utils/expect.dart';
21+
import 'js_utils.dart';
22+
23+
extension type ET1._(JSObject _) implements JSObject {
24+
external ET1({int id = 0, String name = "default"});
25+
26+
external int? get id;
27+
external String? get name;
28+
}
29+
30+
extension type ET2._(JSObject _) implements JSObject {
31+
external factory ET2({int id = 0, String name = "default"});
32+
33+
external int? get id;
34+
external String? get name;
35+
}
36+
37+
main() {
38+
globalContext["et1"] = ET1();
39+
eval(r'''
40+
globalThis.v1 = globalThis.et1.id;
41+
globalThis.v2 = globalThis.et1.name;
42+
''');
43+
Expect.isNull(globalContext["v1"]);
44+
Expect.isNull(globalContext["v2"]);
45+
46+
globalContext["et2"] = ET1(id: 2);
47+
eval(r'''
48+
globalThis.v3 = globalThis.et2.id;
49+
globalThis.v4 = globalThis.et2.name;
50+
''');
51+
Expect.equals(2, (globalContext["v3"] as JSNumber).toDartInt);
52+
Expect.isNull(globalContext["v4"]);
53+
54+
globalContext["et3"] = ET2();
55+
eval(r'''
56+
globalThis.v5 = globalThis.et3.id;
57+
globalThis.v6 = globalThis.et3.name;
58+
''');
59+
Expect.isNull(globalContext["v5"]);
60+
Expect.isNull(globalContext["v6"]);
61+
62+
globalContext["et4"] = ET2(id: 4);
63+
eval(r'''
64+
globalThis.v7 = globalThis.et4.id;
65+
globalThis.v8 = globalThis.et4.name;
66+
''');
67+
Expect.equals(4, (globalContext["v7"] as JSNumber).toDartInt);
68+
Expect.isNull(globalContext["v8"]);
69+
}
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
// Copyright (c) 2025, 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+
/// @assertion Within an interop type, you can declare several different types
6+
/// of external interop members:
7+
/// ...
8+
/// - Object literal constructors. It's sometimes useful to create a JS object
9+
/// literal that simply contains a number of properties and their values. In
10+
/// order to do this, declare a constructor with only named parameters, where
11+
/// the names of the parameters match the property names.
12+
///
13+
/// @description Check that a constructor of a JS interop type with named
14+
/// parameters creates a new JS object with all properties defined by the
15+
/// constructor. Test constructors with private names.
16+
/// @author [email protected]
17+
18+
import 'dart:js_interop';
19+
import 'dart:js_interop_unsafe';
20+
import '../../Utils/expect.dart';
21+
import 'js_utils.dart';
22+
23+
extension type ET1._(JSObject _) implements JSObject {
24+
external ET1._create({int id, String? name});
25+
26+
external int get id;
27+
external String? get name;
28+
}
29+
30+
extension type ET2._(JSObject _) implements JSObject {
31+
external factory ET2._create({int id, String? name});
32+
33+
external int get id;
34+
external String? get name;
35+
}
36+
37+
main() {
38+
globalContext["et1"] = ET1._create(id: 1, name: "one");
39+
eval(r'''
40+
globalThis.v1 = globalThis.et1.id;
41+
globalThis.v2 = globalThis.et1.name;
42+
''');
43+
Expect.equals(1, (globalContext["v1"] as JSNumber).toDartInt);
44+
Expect.equals("one", (globalContext["v2"] as JSString).toDart);
45+
46+
globalContext["et2"] = ET2._create(id: 2, name: "two");
47+
eval(r'''
48+
globalThis.v3 = globalThis.et2.id;
49+
globalThis.v4 = globalThis.et2.name;
50+
''');
51+
Expect.equals(2, (globalContext["v3"] as JSNumber).toDartInt);
52+
Expect.equals("two", (globalContext["v4"] as JSString).toDart);
53+
}
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
// Copyright (c) 2025, 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+
/// @assertion Within an interop type, you can declare several different types
6+
/// of external interop members:
7+
/// ...
8+
/// - static members. Like constructors, static members use the name of the
9+
/// extension type to generate the JS code.
10+
///
11+
/// @description Check that static members use the name of the extension type to
12+
/// generate the JS code. Test private names of the static members.
13+
/// @author [email protected]
14+
15+
import 'dart:js_interop';
16+
import '../../Utils/expect.dart';
17+
import 'js_utils.dart';
18+
19+
extension type ET._(JSObject _) implements JSObject {
20+
external static int _variable;
21+
external static int get _getter;
22+
external static String _method(String _);
23+
external static void set _setter(int value);
24+
}
25+
26+
@JS("ET")
27+
extension type ET2._(JSObject _) implements JSObject {
28+
external static int _variable;
29+
external static int get _getter;
30+
external static String _method(String _);
31+
external static void set _setter(int value);
32+
}
33+
34+
main() {
35+
eval(r'''
36+
class ET {
37+
static _variable = 42;
38+
static get _getter() {
39+
return ET._variable;
40+
}
41+
static _method(v) {
42+
return v;
43+
}
44+
static set _setter(value) {
45+
ET._variable = value;
46+
}
47+
}
48+
globalThis.ET = ET;
49+
''');
50+
51+
Expect.equals(42, ET._variable);
52+
Expect.equals("x", ET._method("x"));
53+
ET._setter = -1;
54+
Expect.equals(-1, ET._getter);
55+
56+
Expect.equals(-1, ET2._variable);
57+
Expect.equals("y", ET2._method("y"));
58+
ET2._setter = 42;
59+
Expect.equals(42, ET2._getter);
60+
}
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
// Copyright (c) 2025, 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+
/// @assertion Within an interop type, you can declare several different types
6+
/// of external interop members:
7+
/// ...
8+
/// - Instance members. Like with other Dart types, instance members require an
9+
/// instance to be used. These members get, set, or invoke properties on the
10+
/// instance.
11+
///
12+
/// @description Check that instance members with private names are allowed in
13+
/// JS interop.
14+
/// @author [email protected]
15+
16+
import 'dart:js_interop';
17+
import '../../Utils/expect.dart';
18+
import 'js_utils.dart';
19+
20+
extension type ET._(JSObject _) implements JSObject {
21+
external ET();
22+
external int _variable;
23+
external int get _getter;
24+
external String _method(String _);
25+
external void set _setter(int value);
26+
}
27+
28+
@JS("ET")
29+
extension type ET2._(JSObject _) implements JSObject {
30+
external ET2();
31+
external int _variable;
32+
external int get _getter;
33+
external String _method(String _);
34+
external void set _setter(int value);
35+
}
36+
37+
main() {
38+
eval(r'''
39+
class ET {
40+
constructor() {
41+
this._variable = 0;
42+
}
43+
get _getter() {
44+
return this._variable;
45+
}
46+
_method(v) {
47+
return v;
48+
}
49+
set _setter(value) {
50+
this._variable = value;
51+
}
52+
}
53+
globalThis.ET = ET;
54+
''');
55+
ET et = ET();
56+
Expect.equals(0, et._variable);
57+
Expect.equals(0, et._getter);
58+
et._setter = 42;
59+
Expect.equals(42, et._getter);
60+
Expect.equals("x", et._method("x"));
61+
62+
ET2 et2 = ET2();
63+
Expect.equals(0, et2._variable);
64+
Expect.equals(0, et2._getter);
65+
et2._setter = 42;
66+
Expect.equals(42, et2._getter);
67+
Expect.equals("y", et2._method("y"));
68+
}

0 commit comments

Comments
 (0)