Skip to content

Commit

Permalink
Use new dart:js_interop way to externalize/internalize objects (flutt…
Browse files Browse the repository at this point in the history
…er#53304)

The issue in [0] was fixed. So we no longer have to use conditional
imports and dart2wasm vs dart2js specific implementations.

Instead we use the new extension methods introduced due to [0], namely
`Object.toExternalReference` and `ExternalDartReference.toDartObject`.

[0] dart-lang/sdk#55183
  • Loading branch information
mkustermann authored Jun 10, 2024
1 parent 6ebc89d commit 1f3938d
Show file tree
Hide file tree
Showing 9 changed files with 13 additions and 59 deletions.
4 changes: 0 additions & 4 deletions ci/licenses_golden/licenses_flutter
Original file line number Diff line number Diff line change
Expand Up @@ -42947,7 +42947,6 @@ ORIGIN: ../../../flutter/lib/web_ui/lib/src/engine/shadow.dart + ../../../flutte
ORIGIN: ../../../flutter/lib/web_ui/lib/src/engine/skwasm/skwasm_impl.dart + ../../../flutter/LICENSE
ORIGIN: ../../../flutter/lib/web_ui/lib/src/engine/skwasm/skwasm_impl/canvas.dart + ../../../flutter/LICENSE
ORIGIN: ../../../flutter/lib/web_ui/lib/src/engine/skwasm/skwasm_impl/codecs.dart + ../../../flutter/LICENSE
ORIGIN: ../../../flutter/lib/web_ui/lib/src/engine/skwasm/skwasm_impl/dart_js_conversion.dart + ../../../flutter/LICENSE
ORIGIN: ../../../flutter/lib/web_ui/lib/src/engine/skwasm/skwasm_impl/filters.dart + ../../../flutter/LICENSE
ORIGIN: ../../../flutter/lib/web_ui/lib/src/engine/skwasm/skwasm_impl/font_collection.dart + ../../../flutter/LICENSE
ORIGIN: ../../../flutter/lib/web_ui/lib/src/engine/skwasm/skwasm_impl/image.dart + ../../../flutter/LICENSE
Expand Down Expand Up @@ -42984,7 +42983,6 @@ ORIGIN: ../../../flutter/lib/web_ui/lib/src/engine/skwasm/skwasm_impl/shaders.da
ORIGIN: ../../../flutter/lib/web_ui/lib/src/engine/skwasm/skwasm_impl/surface.dart + ../../../flutter/LICENSE
ORIGIN: ../../../flutter/lib/web_ui/lib/src/engine/skwasm/skwasm_impl/vertices.dart + ../../../flutter/LICENSE
ORIGIN: ../../../flutter/lib/web_ui/lib/src/engine/skwasm/skwasm_stub.dart + ../../../flutter/LICENSE
ORIGIN: ../../../flutter/lib/web_ui/lib/src/engine/skwasm/skwasm_stub/dart_js_conversion.dart + ../../../flutter/LICENSE
ORIGIN: ../../../flutter/lib/web_ui/lib/src/engine/skwasm/skwasm_stub/renderer.dart + ../../../flutter/LICENSE
ORIGIN: ../../../flutter/lib/web_ui/lib/src/engine/svg.dart + ../../../flutter/LICENSE
ORIGIN: ../../../flutter/lib/web_ui/lib/src/engine/test_embedding.dart + ../../../flutter/LICENSE
Expand Down Expand Up @@ -45821,7 +45819,6 @@ FILE: ../../../flutter/lib/web_ui/lib/src/engine/shadow.dart
FILE: ../../../flutter/lib/web_ui/lib/src/engine/skwasm/skwasm_impl.dart
FILE: ../../../flutter/lib/web_ui/lib/src/engine/skwasm/skwasm_impl/canvas.dart
FILE: ../../../flutter/lib/web_ui/lib/src/engine/skwasm/skwasm_impl/codecs.dart
FILE: ../../../flutter/lib/web_ui/lib/src/engine/skwasm/skwasm_impl/dart_js_conversion.dart
FILE: ../../../flutter/lib/web_ui/lib/src/engine/skwasm/skwasm_impl/filters.dart
FILE: ../../../flutter/lib/web_ui/lib/src/engine/skwasm/skwasm_impl/font_collection.dart
FILE: ../../../flutter/lib/web_ui/lib/src/engine/skwasm/skwasm_impl/image.dart
Expand Down Expand Up @@ -45858,7 +45855,6 @@ FILE: ../../../flutter/lib/web_ui/lib/src/engine/skwasm/skwasm_impl/shaders.dart
FILE: ../../../flutter/lib/web_ui/lib/src/engine/skwasm/skwasm_impl/surface.dart
FILE: ../../../flutter/lib/web_ui/lib/src/engine/skwasm/skwasm_impl/vertices.dart
FILE: ../../../flutter/lib/web_ui/lib/src/engine/skwasm/skwasm_stub.dart
FILE: ../../../flutter/lib/web_ui/lib/src/engine/skwasm/skwasm_stub/dart_js_conversion.dart
FILE: ../../../flutter/lib/web_ui/lib/src/engine/skwasm/skwasm_stub/renderer.dart
FILE: ../../../flutter/lib/web_ui/lib/src/engine/svg.dart
FILE: ../../../flutter/lib/web_ui/lib/src/engine/test_embedding.dart
Expand Down
6 changes: 3 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 @@ -21,8 +21,8 @@ import 'package:ui/src/engine.dart';
/// 5. The finalizer function is called with the SkPaint as the sole argument.
/// 6. We call `delete` on SkPaint.
DomFinalizationRegistry _finalizationRegistry = DomFinalizationRegistry(
(JSAny boxedUniq) {
final UniqueRef<Object> uniq = boxedUniq.fromJSWrapper as UniqueRef<Object>;
(ExternalDartReference boxedUniq) {
final UniqueRef<Object> uniq = boxedUniq.toDartObject as UniqueRef<Object>;
uniq.collect();
}.toJS
);
Expand All @@ -34,7 +34,7 @@ NativeMemoryFinalizationRegistry nativeMemoryFinalizationRegistry = NativeMemory
class NativeMemoryFinalizationRegistry {
void register(Object owner, UniqueRef<Object> ref) {
if (browserSupportsFinalizationRegistry) {
_finalizationRegistry.register(owner.toJSWrapper, ref.toJSWrapper);
_finalizationRegistry.register(owner.toExternalReference, ref.toExternalReference);
}
}
}
Expand Down
19 changes: 4 additions & 15 deletions lib/web_ui/lib/src/engine/dom.dart
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import 'dart:typed_data';

import 'package:js/js_util.dart' as js_util;
import 'package:meta/meta.dart';
import 'package:ui/src/engine/skwasm/skwasm_stub.dart' if (dart.library.ffi) 'package:ui/src/engine/skwasm/skwasm_impl.dart';

import 'browser_detection.dart';

Expand Down Expand Up @@ -38,13 +37,6 @@ import 'browser_detection.dart';
/// used carefully and only on types that are known to not contains `JSNull` and
/// `JSUndefined`.
extension ObjectToJSAnyExtension on Object {
// Once `Object.toJSBox` is faster (see
// https://github.com/dart-lang/sdk/issues/55183) we can remove this
// backend-specific workaround.
@pragma('wasm:prefer-inline')
@pragma('dart2js:tryInline')
JSAny get toJSWrapper => dartToJsWrapper(this);

@pragma('wasm:prefer-inline')
@pragma('dart2js:tryInline')
JSAny get toJSAnyShallow {
Expand All @@ -61,10 +53,6 @@ extension ObjectToJSAnyExtension on Object {
}

extension JSAnyToObjectExtension on JSAny {
@pragma('wasm:prefer-inline')
@pragma('dart2js:tryInline')
Object get fromJSWrapper => jsWrapperToDart(this);

@pragma('wasm:prefer-inline')
@pragma('dart2js:tryInline')
Object get toObjectShallow {
Expand Down Expand Up @@ -3682,13 +3670,14 @@ class DomFinalizationRegistry {

extension DomFinalizationRegistryExtension on DomFinalizationRegistry {
@JS('register')
external JSVoid register(JSAny target, JSAny value);
external JSVoid register(
ExternalDartReference target, ExternalDartReference value);

@JS('register')
external JSVoid registerWithToken(JSAny target, JSAny value, JSAny token);
external JSVoid registerWithToken(ExternalDartReference target, ExternalDartReference value, ExternalDartReference token);

@JS('unregister')
external JSVoid unregister(JSAny token);
external JSVoid unregister(ExternalDartReference token);
}

@JS('window.FinalizationRegistry')
Expand Down
1 change: 0 additions & 1 deletion lib/web_ui/lib/src/engine/skwasm/skwasm_impl.dart
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ import 'dart:ffi';

export 'skwasm_impl/canvas.dart';
export 'skwasm_impl/codecs.dart';
export 'skwasm_impl/dart_js_conversion.dart';
export 'skwasm_impl/filters.dart';
export 'skwasm_impl/font_collection.dart';
export 'skwasm_impl/image.dart';
Expand Down

This file was deleted.

10 changes: 5 additions & 5 deletions lib/web_ui/lib/src/engine/skwasm/skwasm_impl/memory.dart
Original file line number Diff line number Diff line change
Expand Up @@ -28,21 +28,21 @@ typedef DisposeFunction<T extends NativeType> = void Function(Pointer<T>);

class SkwasmFinalizationRegistry<T extends NativeType> {
SkwasmFinalizationRegistry(this.dispose)
: registry = DomFinalizationRegistry(((JSNumber address) =>
dispose(Pointer<T>.fromAddress(address.toDartDouble.toInt()))
: registry = DomFinalizationRegistry(((ExternalDartReference address) =>
dispose(Pointer<T>.fromAddress(address.toDartObject as int))
).toJS);

final DomFinalizationRegistry registry;
final DisposeFunction<T> dispose;

void register(SkwasmObjectWrapper<T> wrapper) {
final JSAny jsWrapper = wrapper.toJSWrapper;
final ExternalDartReference jsWrapper = wrapper.toExternalReference;
registry.registerWithToken(
jsWrapper, wrapper.handle.address.toJS, jsWrapper);
jsWrapper, wrapper.handle.address.toExternalReference, jsWrapper);
}

void evict(SkwasmObjectWrapper<T> wrapper) {
final JSAny jsWrapper = wrapper.toJSWrapper;
final ExternalDartReference jsWrapper = wrapper.toExternalReference;
registry.unregister(jsWrapper);
dispose(wrapper.handle);
}
Expand Down
1 change: 0 additions & 1 deletion lib/web_ui/lib/src/engine/skwasm/skwasm_stub.dart
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,4 @@
// ignore: unnecessary_library_directive
library skwasm_stub;

export 'skwasm_stub/dart_js_conversion.dart';
export 'skwasm_stub/renderer.dart';

This file was deleted.

2 changes: 1 addition & 1 deletion lib/web_ui/test/canvaskit/canvaskit_api_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -1943,7 +1943,7 @@ void _paragraphTests() {
// So the test simply tests that a FinalizationRegistry can be constructed
// and its `register` method can be called.
final DomFinalizationRegistry registry = DomFinalizationRegistry((String arg) {}.toJS);
registry.register(Object().toJSWrapper, Object().toJSWrapper);
registry.register(Object().toExternalReference, Object().toExternalReference);
});
}

Expand Down

0 comments on commit 1f3938d

Please sign in to comment.