Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
49 changes: 49 additions & 0 deletions LibTest/js_interop/JSExport/jsExport_A01_t01.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
// 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 Annotation to allow Dart classes to be wrapped with a JS object
/// using `dart:js_interop`'s `createJSInteropWrapper`.
///
/// When an instance of a class annotated with this annotation is passed to
/// `createJSInteropWrapper`, the method returns a JS object that contains a
/// property for each of the class' instance members. When called, these
/// properties forward to the instance's corresponding members.
///
/// @description Checks that when an instance of a class annotated with
/// `@JSExport()` is passed to `createJSInteropWrapper`, the method returns a JS
/// object that contains a property for each of the class' instance members.
/// @author sgrekhov22@gmail.com

import 'dart:js_interop';
import 'dart:js_interop_unsafe';
import '../../../Utils/expect.dart';
import '../js_utils.dart';

String log = "";

@JSExport()
class C {
int variable = 42;
String method(String v) => "method($v);";
String get getter => "Some getter";
void set setter(bool value) {
log = "setter($value);";
}
}

void main() {
var c = C();
var jsC = createJSInteropWrapper<C>(c);
globalContext["jsC"] = jsC;
eval(r'''
globalThis.v1 = globalThis.jsC.variable;
globalThis.v2 = globalThis.jsC.method('x');
globalThis.v3 = globalThis.jsC.getter;
globalThis.jsC.setter = false;
''');
Expect.equals(42, (globalContext["v1"] as JSNumber).toDartInt);
Expect.equals("method(x);", (globalContext["v2"] as JSString).toDart);
Expect.equals("Some getter", (globalContext["v3"] as JSString).toDart);
Expect.equals("setter(false);", log);
}
36 changes: 36 additions & 0 deletions LibTest/js_interop/JSExport/jsExport_A01_t02.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
// 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 Annotation to allow Dart classes to be wrapped with a JS object
/// using `dart:js_interop`'s `createJSInteropWrapper`.
///
/// When an instance of a class annotated with this annotation is passed to
/// `createJSInteropWrapper`, the method returns a JS object that contains a
/// property for each of the class' instance members. When called, these
/// properties forward to the instance's corresponding members.
///
/// @description Checks that value of a final variable of a clas annotated with
/// `@JSExport()` cannot be changed.
/// @author sgrekhov22@gmail.com

import 'dart:js_interop';
import 'dart:js_interop_unsafe';
import '../../../Utils/expect.dart';
import '../js_utils.dart';

@JSExport()
class C {
final int variable = 42;
}

void main() {
var c = C();
var jsC = createJSInteropWrapper<C>(c);
globalContext["jsC"] = jsC;
eval(r'''
globalThis.jsC.variable = 0;
globalThis.v = globalThis.jsC.variable;
''');
Expect.equals(42, (globalContext["v"] as JSNumber).toDartInt);
}
43 changes: 43 additions & 0 deletions LibTest/js_interop/JSExport/jsExport_A01_t03.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
// 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 Annotation to allow Dart classes to be wrapped with a JS object
/// using `dart:js_interop`'s `createJSInteropWrapper`.
///
/// When an instance of a class annotated with this annotation is passed to
/// `createJSInteropWrapper`, the method returns a JS object that contains a
/// property for each of the class' instance members. When called, these
/// properties forward to the instance's corresponding members.
///
/// @description Checks that uninitialized late instance variables of a class
/// annotated with `@JSExport()` are created by `createJSInteropWrapper` and
/// produce a runtime error when read before initialization.
/// @author sgrekhov22@gmail.com

import 'dart:js_interop';
import 'dart:js_interop_unsafe';
import '../../../Utils/expect.dart';
import '../js_utils.dart';

@JSExport()
class C {
late int variable;

void init() {
variable = 42;
}
}

void main() {
var c = C();
var jsC = createJSInteropWrapper<C>(c);
globalContext["jsC"] = jsC;
Expect.throws(() {
eval("globalThis.jsC.variable;"); // LateInitializationError
});

c.init();
eval("globalThis.v1 = globalThis.jsC.variable;");
Expect.equals(42, (globalContext["v1"] as JSNumber).toDartInt);
}
55 changes: 55 additions & 0 deletions LibTest/js_interop/JSExport/jsExport_A01_t04.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
// 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 Annotation to allow Dart classes to be wrapped with a JS object
/// using `dart:js_interop`'s `createJSInteropWrapper`.
///
/// When an instance of a class annotated with this annotation is passed to
/// `createJSInteropWrapper`, the method returns a JS object that contains a
/// property for each of the class' instance members. When called, these
/// properties forward to the instance's corresponding members.
///
/// @description Checks that it is a compile-time error if a class or a mixin
/// annotated with `@JSExport()` doesn't contain any instance members.
/// @author sgrekhov22@gmail.com

import 'dart:js_interop';

@JSExport()
class C1 {
// ^^
// [analyzer] unspecified
// [web] unspecified
static int variable = 42;
static String method(String v) => "method($v);";
static String get getter => "Some getter";
static void set setter(bool value) {}
}

@JSExport()
class C2 {
// ^^
// [analyzer] unspecified
// [web] unspecified
C2();
C2.x();
factory C2.f() = C2.x;
}

@JSExport()
mixin M {
// ^
// [analyzer] unspecified
// [web] unspecified
static int variable = 42;
static String method(String v) => "method($v);";
static String get getter => "Some getter";
static void set setter(bool value) {}
}

void main() {
print(C1);
print(C2);
print(M);
}
55 changes: 55 additions & 0 deletions LibTest/js_interop/JSExport/jsExport_A01_t05.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
// 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 Annotation to allow Dart classes to be wrapped with a JS object
/// using `dart:js_interop`'s `createJSInteropWrapper`.
///
/// When an instance of a class annotated with this annotation is passed to
/// `createJSInteropWrapper`, the method returns a JS object that contains a
/// property for each of the class' instance members. When called, these
/// properties forward to the instance's corresponding members.
///
/// @description Checks that when an instance of a class annotated with
/// `@JSExport()` is passed to `createJSInteropWrapper`, the method returns a JS
/// object that contains a property for each of the class' instance members.
/// Test type variables.
/// @author sgrekhov22@gmail.com

import 'dart:js_interop';
import 'dart:js_interop_unsafe';
import '../../../Utils/expect.dart';
import '../js_utils.dart';

String log = "";

@JSExport()
class C<X, Y> {
X variable;
X method(Y y) {
log = "method($y) => $variable;";
return variable;
}
X get getter => variable;
void set setter(Y y) {
log = "setter($y);";
}
C(this.variable);
}

void main() {
var c = C<int, String>(42);
var jsC = createJSInteropWrapper<C<int, String>>(c);
globalContext["jsC"] = jsC;
eval(r'''
globalThis.v1 = globalThis.jsC.variable;
globalThis.v2 = globalThis.jsC.method('x');
globalThis.v3 = globalThis.jsC.getter;
''');
Expect.equals(42, (globalContext["v1"] as JSNumber).toDartInt);
Expect.equals("method(x) => 42;", log);
Expect.equals(42, (globalContext["v3"] as JSNumber).toDartInt);

eval("globalThis.jsC.setter = 'z';");
Expect.equals("setter(z);", log);
}