Skip to content

Commit

Permalink
[web] use callConstructor for FinalizationRegistry due to bug in dart…
Browse files Browse the repository at this point in the history
…2js (#40798)

[web] use callConstructor for FinalizationRegistry due to bug in dart2js
  • Loading branch information
yjbanov authored Mar 31, 2023
1 parent fcab15f commit d5a0b29
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 6 deletions.
9 changes: 6 additions & 3 deletions lib/web_ui/lib/src/engine/canvaskit/canvaskit_api.dart
Original file line number Diff line number Diff line change
Expand Up @@ -3569,9 +3569,12 @@ extension JsConstructorExtension on JsConstructor {
@JS('window.FinalizationRegistry')
@staticInterop
class SkObjectFinalizationRegistry {
// TODO(hterkelsen): Add a type for the `cleanup` function when
// native constructors support type parameters.
external factory SkObjectFinalizationRegistry(JSFunction cleanup);
factory SkObjectFinalizationRegistry(JSFunction cleanup) {
return js_util.callConstructor(
_finalizationRegistryConstructor!.toObjectShallow,
<Object>[cleanup],
);
}
}

extension SkObjectFinalizationRegistryExtension on SkObjectFinalizationRegistry {
Expand Down
8 changes: 5 additions & 3 deletions lib/web_ui/lib/src/engine/canvaskit/native_memory.dart
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,11 @@ import 'canvaskit_api.dart';

/// Collects native objects that weren't explicitly disposed of using
/// [UniqueRef.dispose] or [CountedRef.unref].
SkObjectFinalizationRegistry _finalizationRegistry = SkObjectFinalizationRegistry((UniqueRef<Object> uniq) {
uniq.collect();
}.toJS);
SkObjectFinalizationRegistry _finalizationRegistry = SkObjectFinalizationRegistry(
(UniqueRef<Object> uniq) {
uniq.collect();
}.toJS
);

NativeMemoryFinalizationRegistry nativeMemoryFinalizationRegistry = NativeMemoryFinalizationRegistry();

Expand Down
10 changes: 10 additions & 0 deletions lib/web_ui/test/canvaskit/canvaskit_api_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

import 'dart:js_interop';
import 'dart:math';
import 'dart:typed_data';

Expand Down Expand Up @@ -1891,6 +1892,15 @@ void _paragraphTests() {
'http://localhost:1234/foo/canvaskit.wasm',
);
});

test('SkObjectFinalizationRegistry', () {
// There's no reliable way to test the actual functionality of
// FinalizationRegistry because it depends on GC, which cannot be controlled,
// So the test simply tests that a FinalizationRegistry can be constructed
// and its `register` method can be called.
final SkObjectFinalizationRegistry registry = SkObjectFinalizationRegistry((String arg) {}.toJS);
registry.register('foo', 'bar');
});
}


Expand Down

0 comments on commit d5a0b29

Please sign in to comment.