[js_util] Add globalThis getter.

Change-Id: I325f3ea7f5aad84dff9e08c6a958b478d6926cb6
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/240962
Reviewed-by: Srujan Gaddam <srujzs@google.com>
Reviewed-by: Riley Porter <rileyporter@google.com>
Commit-Queue: Joshua Litt <joshualitt@google.com>
This commit is contained in:
Joshua Litt 2022-04-12 20:12:02 +00:00 committed by Commit Bot
parent ecf42a3815
commit d06fd8d081
3 changed files with 29 additions and 0 deletions

View file

@ -64,6 +64,9 @@ Object _convertDataTree(Object data) {
return _convert(data)!;
}
@pragma('dart2js:tryInline')
Object get globalThis => JS('', 'globalThis');
T newObject<T>() => JS('=Object', '{}');
bool hasProperty(Object o, Object name) => JS('bool', '# in #', name, o);

View file

@ -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);

View file

@ -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);