diff --git a/LibTest/js_interop/JSAnyUtilityExtension/instanceOfString_A01_t01.dart b/LibTest/js_interop/JSAnyUtilityExtension/instanceOfString_A01_t01.dart new file mode 100644 index 0000000000..19c55bfd31 --- /dev/null +++ b/LibTest/js_interop/JSAnyUtilityExtension/instanceOfString_A01_t01.dart @@ -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 bool instanceOfString( String constructorName ) +/// +/// Whether this [JSAny?] is an `instanceof` the constructor that is defined by +/// `constructorName`, which is looked up in the `globalContext`. +/// +/// If `constructorName` contains '.'s, the name is split into several parts in +/// order to get the constructor. For example, `library1.JSClass` would involve +/// fetching `library1` off of the `globalContext`, and then fetching `JSClass` +/// off of `library1` to get the constructor. +/// +/// If `constructorName` is empty or any of the parts or the constructor don't +/// exist, returns `false`. +/// +/// @description Checks that `instanceOfString()` returns `true` if this +/// `JSAny?` is is an `instanceof` the constructor that is defined by +/// `constructorName`. +/// @author sgrekhov22@gmail.com + +import 'dart:js_interop'; +import 'dart:js_interop_unsafe'; +import '../../../Utils/expect.dart'; +import '../js_utils.dart'; + +main() { + eval(r''' + function A(id, name) { + this.id = id; + this.name = name; + } + function B() {} + + globalThis.objA = new A(42, "A form JS"); + globalThis.objB = new B(); + '''); + Expect.isTrue(globalContext["objA"].instanceOfString("A")); + Expect.isTrue(globalContext["objB"].instanceOfString("B")); + Expect.isFalse(globalContext["objA"].instanceOfString("B")); + Expect.isFalse(globalContext["objB"].instanceOfString("A")); +} diff --git a/LibTest/js_interop/JSAnyUtilityExtension/instanceOfString_A01_t02.dart b/LibTest/js_interop/JSAnyUtilityExtension/instanceOfString_A01_t02.dart new file mode 100644 index 0000000000..51d28b176f --- /dev/null +++ b/LibTest/js_interop/JSAnyUtilityExtension/instanceOfString_A01_t02.dart @@ -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 bool instanceOfString( String constructorName ) +/// +/// Whether this [JSAny?] is an `instanceof` the constructor that is defined by +/// `constructorName`, which is looked up in the `globalContext`. +/// +/// If `constructorName` contains '.'s, the name is split into several parts in +/// order to get the constructor. For example, `library1.JSClass` would involve +/// fetching `library1` off of the `globalContext`, and then fetching `JSClass` +/// off of `library1` to get the constructor. +/// +/// If `constructorName` is empty or any of the parts or the constructor don't +/// exist, returns `false`. +/// +/// @description Checks that `instanceOfString()` returns `true` if this +/// `JSAny?` is is an `instanceof` the constructor that is defined by +/// `constructorName`. +/// @author sgrekhov22@gmail.com + +import 'dart:js_interop'; +import 'dart:js_interop_unsafe'; +import '../../../Utils/expect.dart'; +import '../js_utils.dart'; + +main() { + eval(r''' + class A { + constructor(id, name) { + this.id = id; + this.name = name; + } + } + class B { + constructor() {} + } + + globalThis.fnB = B; + globalThis.fnA = A; + globalThis.objA = new A(42, "A form JS"); + globalThis.objB = new B(); + '''); + Expect.isTrue(globalContext["objA"].instanceOfString("fnA")); + Expect.isTrue(globalContext["objB"].instanceOfString("fnB")); + Expect.isFalse(globalContext["objA"].instanceOfString("B")); + Expect.isFalse(globalContext["objB"].instanceOfString("A")); +} diff --git a/LibTest/js_interop/JSAnyUtilityExtension/instanceOfString_A02_t01.dart b/LibTest/js_interop/JSAnyUtilityExtension/instanceOfString_A02_t01.dart new file mode 100644 index 0000000000..0854f8ee68 --- /dev/null +++ b/LibTest/js_interop/JSAnyUtilityExtension/instanceOfString_A02_t01.dart @@ -0,0 +1,44 @@ +// 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 bool instanceOfString( String constructorName ) +/// +/// Whether this [JSAny?] is an `instanceof` the constructor that is defined by +/// `constructorName`, which is looked up in the `globalContext`. +/// +/// If `constructorName` contains '.'s, the name is split into several parts in +/// order to get the constructor. For example, `library1.JSClass` would involve +/// fetching `library1` off of the `globalContext`, and then fetching `JSClass` +/// off of `library1` to get the constructor. +/// +/// If `constructorName` is empty or any of the parts or the constructor don't +/// exist, returns `false`. +/// +/// @description Checks that `instanceOfString()` returns `false` if +/// `constructorName` cannot be found in the `globalContext`. +/// @author sgrekhov22@gmail.com + +import 'dart:js_interop'; +import 'dart:js_interop_unsafe'; +import '../../../Utils/expect.dart'; +import '../js_utils.dart'; + +main() { + eval(r''' + class A { + constructor(id, name) { + this.id = id; + this.name = name; + } + } + class B { + constructor() {} + } + + globalThis.objA = new A(42, "A form JS"); + globalThis.objB = new B(); + '''); + Expect.isFalse(globalContext["objA"].instanceOfString("A")); + Expect.isFalse(globalContext["objB"].instanceOfString("B")); +} diff --git a/LibTest/js_interop/JSAnyUtilityExtension/instanceOfString_A03_t01.dart b/LibTest/js_interop/JSAnyUtilityExtension/instanceOfString_A03_t01.dart new file mode 100644 index 0000000000..088f7824f0 --- /dev/null +++ b/LibTest/js_interop/JSAnyUtilityExtension/instanceOfString_A03_t01.dart @@ -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 bool instanceOfString( String constructorName ) +/// +/// Whether this [JSAny?] is an `instanceof` the constructor that is defined by +/// `constructorName`, which is looked up in the `globalContext`. +/// +/// If `constructorName` contains '.'s, the name is split into several parts in +/// order to get the constructor. For example, `library1.JSClass` would involve +/// fetching `library1` off of the `globalContext`, and then fetching `JSClass` +/// off of `library1` to get the constructor. +/// +/// If `constructorName` is empty or any of the parts or the constructor don't +/// exist, returns `false`. +/// +/// @description Checks that if `constructorName` then `instanceOfString` +/// returns `false`. +/// @author sgrekhov22@gmail.com + +import 'dart:js_interop'; +import 'dart:js_interop_unsafe'; +import '../../../Utils/expect.dart'; +import '../js_utils.dart'; + +main() { + eval(r''' + class A { + constructor(id, name) { + this.id = id; + this.name = name; + } + } + class B { + constructor() {} + } + globalThis.objA = new A(42, "A form JS"); + globalThis.objB = new B(); + '''); + Expect.isFalse(globalContext["objA"].instanceOfString("")); + Expect.isFalse(globalContext["objB"].instanceOfString("")); +} diff --git a/LibTest/js_interop/JSAnyUtilityExtension/instanceOfString_A03_t02.dart b/LibTest/js_interop/JSAnyUtilityExtension/instanceOfString_A03_t02.dart new file mode 100644 index 0000000000..e1fb7c9036 --- /dev/null +++ b/LibTest/js_interop/JSAnyUtilityExtension/instanceOfString_A03_t02.dart @@ -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 bool instanceOfString( String constructorName ) +/// +/// Whether this [JSAny?] is an `instanceof` the constructor that is defined by +/// `constructorName`, which is looked up in the `globalContext`. +/// +/// If `constructorName` contains '.'s, the name is split into several parts in +/// order to get the constructor. For example, `library1.JSClass` would involve +/// fetching `library1` off of the `globalContext`, and then fetching `JSClass` +/// off of `library1` to get the constructor. +/// +/// If `constructorName` is empty or any of the parts or the constructor don't +/// exist, returns `false`. +/// +/// @description Checks that if any of the parts or the constructor don't exist, +/// `instanceOfString` returns `false` +/// @author sgrekhov22@gmail.com + +import 'dart:js_interop'; +import 'dart:js_interop_unsafe'; +import '../../../Utils/expect.dart'; +import '../js_utils.dart'; + +main() { + eval(r''' + class A { + constructor(id, name) { + this.id = id; + this.name = name; + } + } + class B { + constructor() {} + } + globalThis.objA = new A(42, "A form JS"); + globalThis.objB = new B(); + '''); + Expect.isFalse(globalContext["objA"].instanceOfString("lib1.A")); + Expect.isFalse(globalContext["objB"].instanceOfString("lib2.B")); +} diff --git a/LibTest/js_interop/JSAnyUtilityExtension/instanceOfString_A04_t01.dart b/LibTest/js_interop/JSAnyUtilityExtension/instanceOfString_A04_t01.dart new file mode 100644 index 0000000000..80071ac571 --- /dev/null +++ b/LibTest/js_interop/JSAnyUtilityExtension/instanceOfString_A04_t01.dart @@ -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 bool instanceOfString( String constructorName ) +/// +/// Whether this [JSAny?] is an `instanceof` the constructor that is defined by +/// `constructorName`, which is looked up in the `globalContext`. +/// +/// If `constructorName` contains '.'s, the name is split into several parts in +/// order to get the constructor. For example, `library1.JSClass` would involve +/// fetching `library1` off of the `globalContext`, and then fetching `JSClass` +/// off of `library1` to get the constructor. +/// +/// If `constructorName` is empty or any of the parts or the constructor don't +/// exist, returns `false`. +/// +/// @description Checks that `instanceOfString()` works as expected if this +/// `JSAny?` is is an `instanceof` the constructor that is defined by +/// `constructorName`. Test the case when `constructorName` contains '.'. +/// @author sgrekhov22@gmail.com + +// OtherResources=module.js +import 'dart:async'; +import 'dart:js_interop'; +import 'dart:js_interop_unsafe'; +import '../../../Utils/expect.dart'; +import '../js_utils.dart'; + +final completer = Completer(); + +void complete(String value) { + completer.complete(value); +} + +main() { + globalContext["complete"] = complete.toJS; + eval(r''' + var lib1; + (async () => { + // This is path to the module on tryjobs. May not work locally. + lib1 = await import('/root_dart/tests/co19/src/LibTest/js_interop/JSAnyUtilityExtension/module.js'); + globalThis.objA = new lib1.A(42, "A form JS"); + globalThis.objB = new lib1.B(); + console.log('initialized'); + })().then(function(v) { + globalThis.complete(""); + }); + '''); + asyncStart(); + completer.future.then((_) { + Expect.isTrue(globalContext["objA"].instanceOfString("lib1.A")); + Expect.isTrue(globalContext["objB"].instanceOfString("lib1.B")); + Expect.isFalse(globalContext["objA"].instanceOfString("A")); + Expect.isFalse(globalContext["objB"].instanceOfString("B")); + asyncEnd(); + }); +} diff --git a/LibTest/js_interop/JSAnyUtilityExtension/instanceof_A01_t01.dart b/LibTest/js_interop/JSAnyUtilityExtension/instanceof_A01_t01.dart new file mode 100644 index 0000000000..f7c41db6ba --- /dev/null +++ b/LibTest/js_interop/JSAnyUtilityExtension/instanceof_A01_t01.dart @@ -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 bool instanceof( JSFunction constructor ) +/// +/// Whether this `JSAny?` is an instance of `constructor`. +/// +/// @description Checks that `instanceof()` returns `true` if this `JSAny?` is +/// an instance of `constructor` and `false` otherwise. +/// @author sgrekhov22@gmail.com + +import 'dart:js_interop'; +import 'dart:js_interop_unsafe'; +import '../../../Utils/expect.dart'; +import '../js_utils.dart'; + +main() { + eval(r''' + function A(id, name) { + this.id = id; + this.name = name; + } + function B() {} + + globalThis.fnB = B; + globalThis.fnA = A; + globalThis.objA = new A(42, "A form JS"); + globalThis.objB = new B(); + '''); + Expect.isTrue( + globalContext["objA"].instanceof(globalContext["fnA"] as JSFunction), + ); + Expect.isTrue( + globalContext["objB"].instanceof(globalContext["fnB"] as JSFunction), + ); + Expect.isFalse( + globalContext["objA"].instanceof(globalContext["fnB"] as JSFunction), + ); + Expect.isFalse( + globalContext["objF"].instanceof(globalContext["fnA"] as JSFunction), + ); +} diff --git a/LibTest/js_interop/JSAnyUtilityExtension/instanceof_A01_t02.dart b/LibTest/js_interop/JSAnyUtilityExtension/instanceof_A01_t02.dart new file mode 100644 index 0000000000..e5e357e2c7 --- /dev/null +++ b/LibTest/js_interop/JSAnyUtilityExtension/instanceof_A01_t02.dart @@ -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 bool instanceof( JSFunction constructor ) +/// +/// Whether this `JSAny?` is an instance of `constructor`. +/// +/// @description Checks that `instanceof()` returns `true` if this `JSAny?` is +/// an instance of `constructor` and `false` otherwise. +/// @author sgrekhov22@gmail.com + +import 'dart:js_interop'; +import 'dart:js_interop_unsafe'; +import '../../../Utils/expect.dart'; +import '../js_utils.dart'; + +main() { + eval(r''' + class A { + constructor(id, name) { + this.id = id; + this.name = name; + } + } + class B { + constructor() {} + } + + globalThis.fnB = B; + globalThis.fnA = A; + globalThis.objA = new A(42, "A form JS"); + globalThis.objB = new B(); + '''); + Expect.isTrue( + globalContext["objA"].instanceof(globalContext["fnA"] as JSFunction), + ); + Expect.isTrue( + globalContext["objB"].instanceof(globalContext["fnB"] as JSFunction), + ); + Expect.isFalse( + globalContext["objA"].instanceof(globalContext["fnB"] as JSFunction), + ); + Expect.isFalse( + globalContext["objF"].instanceof(globalContext["fnA"] as JSFunction), + ); +} diff --git a/LibTest/js_interop/JSAnyUtilityExtension/module.js b/LibTest/js_interop/JSAnyUtilityExtension/module.js new file mode 100644 index 0000000000..8488df28e2 --- /dev/null +++ b/LibTest/js_interop/JSAnyUtilityExtension/module.js @@ -0,0 +1,6 @@ +export function A(id, name) { + this.id = id; + this.name = name; +} + +export function B() {}