diff --git a/sdk/lib/js_util/js_util.dart b/sdk/lib/js_util/js_util.dart index 5dca72d6c7b..9af9ea61be9 100644 --- a/sdk/lib/js_util/js_util.dart +++ b/sdk/lib/js_util/js_util.dart @@ -64,6 +64,9 @@ Object _convertDataTree(Object data) { return _convert(data)!; } +@pragma('dart2js:tryInline') +Object get globalThis => JS('', 'globalThis'); + T newObject() => JS('=Object', '{}'); bool hasProperty(Object o, Object name) => JS('bool', '# in #', name, o); diff --git a/tests/lib/js/js_util/properties_test.dart b/tests/lib/js/js_util/properties_test.dart index 84a09c8947a..5f718a6f1ac 100644 --- a/tests/lib/js/js_util/properties_test.dart +++ b/tests/lib/js/js_util/properties_test.dart @@ -189,8 +189,21 @@ main() { function Five(a, b, c, d, e) { this.count = 5; } + + globalThis.globalKey = 'foo'; """); + group('globalThis', () { + test('create', () { + expect(identical(js_util.globalThis, js_util.globalThis), isTrue); + }); + + test('isGlobalThis', () { + expect(js_util.hasProperty(js_util.globalThis, 'One'), isTrue); + expect(js_util.getProperty(js_util.globalThis, 'globalKey'), 'foo'); + }); + }); + group('newObject', () { test('create', () { expect(identical(js_util.newObject(), js_util.newObject()), isFalse); diff --git a/tests/lib_2/js/js_util/properties_test.dart b/tests/lib_2/js/js_util/properties_test.dart index c19fe145855..2b0af1049cd 100644 --- a/tests/lib_2/js/js_util/properties_test.dart +++ b/tests/lib_2/js/js_util/properties_test.dart @@ -191,8 +191,21 @@ main() { function Five(a, b, c, d, e) { this.count = 5; } + + globalThis.globalKey = 'foo'; """); + group('globalThis', () { + test('create', () { + expect(identical(js_util.globalThis, js_util.globalThis), isTrue); + }); + + test('isGlobalThis', () { + expect(js_util.hasProperty(js_util.globalThis, 'One'), isTrue); + expect(js_util.getProperty(js_util.globalThis, 'globalKey'), 'foo'); + }); + }); + group('newObject', () { test('create', () { expect(identical(js_util.newObject(), js_util.newObject()), isFalse);