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
52 changes: 52 additions & 0 deletions LibTest/js_interop/ObjectToJSBoxedDartObject/toJSBox_A01_t01.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
// 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 A JavaScript object that wraps this [Object].
///
/// There are no usable members in the resulting [JSBoxedDartObject] and you may
/// get a new [JSBoxedDartObject] when calling [toJSBox] on the same Dart
/// [Object].
///
/// Throws an [Exception] if this [Object] is a JavaScript value.
///
/// Unlike [ObjectToExternalDartReference.toExternalReference], this returns a
/// JavaScript value. Therefore, the representation is guaranteed to be
/// consistent across all platforms and interop members can be declared on
/// [JSBoxedDartObjects].
///
/// @description Checks that this property returns a JavaScript object that
/// wraps this [Object] and the original Dart object can be unwrapped via
/// `.toDart`.
/// @author [email protected]

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

class C {
int id;
C(this.id);
}

main() async {
var boxedString = "String".toJSBox;
Expect.equals("String", boxedString.toDart);

var boxedNum = 42.toJSBox;
Expect.equals(42, boxedNum.toDart);

var boxedBool = false.toJSBox;
Expect.isFalse(boxedBool.toDart);

var boxedObject = C(42).toJSBox;
Expect.equals(42, (boxedObject.toDart as C).id);

var boxedMap = {"key": "value"}.toJSBox;
Expect.mapEquals({"key": "value"}, boxedMap.toDart);

var boxedSet = {42}.toJSBox;
Expect.setEquals({42}, boxedSet.toDart as Set);

var boxedFuture = Future.value(42).toJSBox;
Expect.equals(42, await (boxedFuture.toDart as Future));
}
30 changes: 30 additions & 0 deletions LibTest/js_interop/ObjectToJSBoxedDartObject/toJSBox_A01_t02.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
// 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 A JavaScript object that wraps this [Object].
///
/// There are no usable members in the resulting [JSBoxedDartObject] and you may
/// get a new [JSBoxedDartObject] when calling [toJSBox] on the same Dart
/// [Object].
///
/// Throws an [Exception] if this [Object] is a JavaScript value.
///
/// Unlike [ObjectToExternalDartReference.toExternalReference], this returns a
/// JavaScript value. Therefore, the representation is guaranteed to be
/// consistent across all platforms and interop members can be declared on
/// [JSBoxedDartObjects].
///
/// @description Checks that this property returns a JavaScript object that
/// wraps this [Object] and the original Dart object can be unwrapped via
/// `.toDart`. Test Dart [List].
/// @author [email protected]
/// @issue 61405, 56905

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

main() {
var boxedList = [42].toJSBox;
Expect.listEquals([42], boxedList.toDart);
}
31 changes: 31 additions & 0 deletions LibTest/js_interop/ObjectToJSBoxedDartObject/toJSBox_A01_t03.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
// 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 A JavaScript object that wraps this [Object].
///
/// There are no usable members in the resulting [JSBoxedDartObject] and you may
/// get a new [JSBoxedDartObject] when calling [toJSBox] on the same Dart
/// [Object].
///
/// Throws an [Exception] if this [Object] is a JavaScript value.
///
/// Unlike [ObjectToExternalDartReference.toExternalReference], this returns a
/// JavaScript value. Therefore, the representation is guaranteed to be
/// consistent across all platforms and interop members can be declared on
/// [JSBoxedDartObjects].
///
/// @description Checks that this property returns a JavaScript object that
/// wraps this [Object] and the original Dart object can be unwrapped via
/// `.toDart`. Test Dart typed list.
/// @author [email protected]
/// @issue 61405, 56905

import 'dart:typed_data';
import 'dart:js_interop';
import '../../../Utils/expect.dart';

main() {
var boxedInt8List = Int8List.fromList([42]).toJSBox;
Expect.listEquals([42], (boxedInt8List.toDart as Int8List).toList());
}
49 changes: 49 additions & 0 deletions LibTest/js_interop/ObjectToJSBoxedDartObject/toJSBox_A02_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 A JavaScript object that wraps this [Object].
///
/// There are no usable members in the resulting [JSBoxedDartObject] and you may
/// get a new [JSBoxedDartObject] when calling [toJSBox] on the same Dart
/// [Object].
///
/// Throws an [Exception] if this [Object] is a JavaScript value.
///
/// Unlike [ObjectToExternalDartReference.toExternalReference], this returns a
/// JavaScript value. Therefore, the representation is guaranteed to be
/// consistent across all platforms and interop members can be declared on
/// [JSBoxedDartObjects].
///
/// @description Checks that an [Exception] is thrown if this [Object] is a
/// JavaScript value. Test JS primitive types.
/// @author [email protected]
/// @issue 61405, 56905

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

main() {
eval(r'''
globalThis.bi = 42n;
globalThis.s = Symbol("name");
''');
Expect.throws(() {
"String".toJS.toJSBox;
});

Expect.throws(() {
42.toJS.toJSBox;
});
Expect.throws(() {
true.toJS.toJSBox;
});
Expect.throws(() {
(globalContext["bi"] as JSBigInt).toJSBox;
});
Expect.throws(() {
(globalContext["s"] as JSSymbol).toJSBox;
});
}
56 changes: 56 additions & 0 deletions LibTest/js_interop/ObjectToJSBoxedDartObject/toJSBox_A02_t02.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
// 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 A JavaScript object that wraps this [Object].
///
/// There are no usable members in the resulting [JSBoxedDartObject] and you may
/// get a new [JSBoxedDartObject] when calling [toJSBox] on the same Dart
/// [Object].
///
/// Throws an [Exception] if this [Object] is a JavaScript value.
///
/// Unlike [ObjectToExternalDartReference.toExternalReference], this returns a
/// JavaScript value. Therefore, the representation is guaranteed to be
/// consistent across all platforms and interop members can be declared on
/// [JSBoxedDartObjects].
///
/// @description Checks that an [Exception] is thrown if this [Object] is a
/// JavaScript value. Test non-primitive JS types.
/// @author [email protected]

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

main() {
eval(r'''
globalThis.obj = {};
''');
Expect.throws(() {
JSArray().toJSBox;
});

Expect.throws(() {
JSObject().toJSBox;
});

Expect.throws(() {
(globalContext["obj"] as JSObject).toJSBox;
});

eval(r'''
globalThis.foo = function(resolve, reject) {
resolve("Success");
};
''');
Expect.throws(() {
JSPromise(globalContext["foo"] as JSFunction).toJSBox;
});

var boxedObject = Object().toJSBox;
Expect.throws(() {
boxedObject.toJSBox;
});
}