From 1f3938d80e6e60dd02b70b71a940f84a1d519c57 Mon Sep 17 00:00:00 2001 From: Martin Kustermann Date: Mon, 10 Jun 2024 14:08:35 +0200 Subject: [PATCH] Use new dart:js_interop way to externalize/internalize objects (#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] https://github.com/dart-lang/sdk/issues/55183 --- ci/licenses_golden/licenses_flutter | 4 ---- .../src/engine/canvaskit/native_memory.dart | 6 +++--- lib/web_ui/lib/src/engine/dom.dart | 19 ++++--------------- .../lib/src/engine/skwasm/skwasm_impl.dart | 1 - .../skwasm_impl/dart_js_conversion.dart | 16 ---------------- .../src/engine/skwasm/skwasm_impl/memory.dart | 10 +++++----- .../lib/src/engine/skwasm/skwasm_stub.dart | 1 - .../skwasm_stub/dart_js_conversion.dart | 13 ------------- .../test/canvaskit/canvaskit_api_test.dart | 2 +- 9 files changed, 13 insertions(+), 59 deletions(-) delete mode 100644 lib/web_ui/lib/src/engine/skwasm/skwasm_impl/dart_js_conversion.dart delete mode 100644 lib/web_ui/lib/src/engine/skwasm/skwasm_stub/dart_js_conversion.dart diff --git a/ci/licenses_golden/licenses_flutter b/ci/licenses_golden/licenses_flutter index 24bd093f9a18a..c10a3d2883a51 100644 --- a/ci/licenses_golden/licenses_flutter +++ b/ci/licenses_golden/licenses_flutter @@ -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 @@ -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 @@ -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 @@ -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 diff --git a/lib/web_ui/lib/src/engine/canvaskit/native_memory.dart b/lib/web_ui/lib/src/engine/canvaskit/native_memory.dart index c1e592d242921..8af6b3aaa5532 100644 --- a/lib/web_ui/lib/src/engine/canvaskit/native_memory.dart +++ b/lib/web_ui/lib/src/engine/canvaskit/native_memory.dart @@ -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 uniq = boxedUniq.fromJSWrapper as UniqueRef; + (ExternalDartReference boxedUniq) { + final UniqueRef uniq = boxedUniq.toDartObject as UniqueRef; uniq.collect(); }.toJS ); @@ -34,7 +34,7 @@ NativeMemoryFinalizationRegistry nativeMemoryFinalizationRegistry = NativeMemory class NativeMemoryFinalizationRegistry { void register(Object owner, UniqueRef ref) { if (browserSupportsFinalizationRegistry) { - _finalizationRegistry.register(owner.toJSWrapper, ref.toJSWrapper); + _finalizationRegistry.register(owner.toExternalReference, ref.toExternalReference); } } } diff --git a/lib/web_ui/lib/src/engine/dom.dart b/lib/web_ui/lib/src/engine/dom.dart index d008d5ae1c271..b11e9e97a7458 100644 --- a/lib/web_ui/lib/src/engine/dom.dart +++ b/lib/web_ui/lib/src/engine/dom.dart @@ -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'; @@ -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 { @@ -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 { @@ -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') diff --git a/lib/web_ui/lib/src/engine/skwasm/skwasm_impl.dart b/lib/web_ui/lib/src/engine/skwasm/skwasm_impl.dart index e36e4359c67bc..fd370c7a0fb18 100644 --- a/lib/web_ui/lib/src/engine/skwasm/skwasm_impl.dart +++ b/lib/web_ui/lib/src/engine/skwasm/skwasm_impl.dart @@ -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'; diff --git a/lib/web_ui/lib/src/engine/skwasm/skwasm_impl/dart_js_conversion.dart b/lib/web_ui/lib/src/engine/skwasm/skwasm_impl/dart_js_conversion.dart deleted file mode 100644 index dbde1bee5cd5d..0000000000000 --- a/lib/web_ui/lib/src/engine/skwasm/skwasm_impl/dart_js_conversion.dart +++ /dev/null @@ -1,16 +0,0 @@ -// Copyright 2013 The Flutter Authors. All rights reserved. -// Use of this source code is governed by a BSD-style license that can be -// found in the LICENSE file. - -import 'dart:_wasm'; -import 'dart:js_interop'; - -@pragma('wasm:prefer-inline') -@pragma('dart2js:tryInline') -JSAny dartToJsWrapper(Object object) => - WasmAnyRef.fromObject(object).externalize().toJS; - -@pragma('wasm:prefer-inline') -@pragma('dart2js:tryInline') -Object jsWrapperToDart(JSAny jsWrapper) => - externRefForJSAny(jsWrapper).internalize()!.toObject(); diff --git a/lib/web_ui/lib/src/engine/skwasm/skwasm_impl/memory.dart b/lib/web_ui/lib/src/engine/skwasm/skwasm_impl/memory.dart index c0f9a8470b3ba..ef60ac0ae7c82 100644 --- a/lib/web_ui/lib/src/engine/skwasm/skwasm_impl/memory.dart +++ b/lib/web_ui/lib/src/engine/skwasm/skwasm_impl/memory.dart @@ -28,21 +28,21 @@ typedef DisposeFunction = void Function(Pointer); class SkwasmFinalizationRegistry { SkwasmFinalizationRegistry(this.dispose) - : registry = DomFinalizationRegistry(((JSNumber address) => - dispose(Pointer.fromAddress(address.toDartDouble.toInt())) + : registry = DomFinalizationRegistry(((ExternalDartReference address) => + dispose(Pointer.fromAddress(address.toDartObject as int)) ).toJS); final DomFinalizationRegistry registry; final DisposeFunction dispose; void register(SkwasmObjectWrapper 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 wrapper) { - final JSAny jsWrapper = wrapper.toJSWrapper; + final ExternalDartReference jsWrapper = wrapper.toExternalReference; registry.unregister(jsWrapper); dispose(wrapper.handle); } diff --git a/lib/web_ui/lib/src/engine/skwasm/skwasm_stub.dart b/lib/web_ui/lib/src/engine/skwasm/skwasm_stub.dart index 50c1791aecc2e..382d34840e9f6 100644 --- a/lib/web_ui/lib/src/engine/skwasm/skwasm_stub.dart +++ b/lib/web_ui/lib/src/engine/skwasm/skwasm_stub.dart @@ -6,5 +6,4 @@ // ignore: unnecessary_library_directive library skwasm_stub; -export 'skwasm_stub/dart_js_conversion.dart'; export 'skwasm_stub/renderer.dart'; diff --git a/lib/web_ui/lib/src/engine/skwasm/skwasm_stub/dart_js_conversion.dart b/lib/web_ui/lib/src/engine/skwasm/skwasm_stub/dart_js_conversion.dart deleted file mode 100644 index b05af5dcff599..0000000000000 --- a/lib/web_ui/lib/src/engine/skwasm/skwasm_stub/dart_js_conversion.dart +++ /dev/null @@ -1,13 +0,0 @@ -// Copyright 2013 The Flutter Authors. All rights reserved. -// Use of this source code is governed by a BSD-style license that can be -// found in the LICENSE file. - -import 'dart:js_interop'; - -@pragma('wasm:prefer-inline') -@pragma('dart2js:tryInline') -JSAny dartToJsWrapper(Object object) => object as JSAny; - -@pragma('wasm:prefer-inline') -@pragma('dart2js:tryInline') -Object jsWrapperToDart(JSAny jsWrapper) => jsWrapper; diff --git a/lib/web_ui/test/canvaskit/canvaskit_api_test.dart b/lib/web_ui/test/canvaskit/canvaskit_api_test.dart index fac74150cd019..b799fa966319b 100644 --- a/lib/web_ui/test/canvaskit/canvaskit_api_test.dart +++ b/lib/web_ui/test/canvaskit/canvaskit_api_test.dart @@ -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); }); }