-
Notifications
You must be signed in to change notification settings - Fork 1.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[dart:js_interop] Allow dart:html types in external signatures
Closes #54482 This is generally useful for users working around some limitations of dart:html. While we want to encourage users to use package:web, dart:html is not available on dart2wasm and users can use dart:html in other ways already e.g. in an interop extension type, so it doesn't make sense to disallow this. We also allow type parameters that extend these types as well. In order to make this a bit more performant (subtyping checks may be expensive), code is refactored to cache more readily and separate the notion of an allowed representation type vs interop extension type. We also define the notion of a "core" interop type, which will be useful when we want to efficiently query what interop type users are using underneath the possible layers of extension types. A few missing tests around typed_data are added and the error around invalid types is reworded to include this change and be more consise. Change-Id: I256b0cce4355d2a21853b0c5bf641166cafc523e Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/347224 Reviewed-by: Sigmund Cherem <[email protected]> Commit-Queue: Srujan Gaddam <[email protected]>
- Loading branch information
Showing
10 changed files
with
257 additions
and
193 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
39 changes: 39 additions & 0 deletions
39
tests/lib/js/static_interop_test/allowed_external_member_native_type_test.dart
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
// Copyright (c) 2024, the Dart project authors. Please see the AUTHORS file | ||
// for details. All rights reserved. Use of this source code is governed by a | ||
// BSD-style license that can be found in the LICENSE file. | ||
|
||
// Like allowed_external_member_type_test, but tests `@Native` types. The only | ||
// valid ones are those that <: `JavaScriptObject` and users can reference, | ||
// which end up only being the types in the SDK web libraries. | ||
|
||
@JS() | ||
library allowed_external_member_native_type_test; | ||
|
||
import 'dart:html'; | ||
import 'dart:js_interop'; | ||
import 'dart:svg'; | ||
import 'dart:typed_data'; | ||
|
||
@JS() | ||
external void documentTest(Document _); | ||
|
||
@JS() | ||
external void elementTypeParamTest<T extends Element>(T _); | ||
|
||
@JS() | ||
external GeometryElement geometryElementTest(); | ||
|
||
// Not an `@Native` type. | ||
@JS() | ||
external void platformTest(Platform _); | ||
// ^ | ||
// [web] Type 'Platform' is not a valid type in the signature of 'dart:js_interop' external APIs or APIs converted via 'toJS'. | ||
|
||
// While the factory returns an `@Native` type that implements | ||
// `JavaScriptObject`, the public interface is not such a type. | ||
@JS() | ||
external void uint8ListTest(Uint8List _); | ||
// ^ | ||
// [web] Type 'Uint8List' is not a valid type in the signature of 'dart:js_interop' external APIs or APIs converted via 'toJS'. | ||
|
||
void main() {} |
108 changes: 108 additions & 0 deletions
108
tests/lib/js/static_interop_test/allowed_external_member_type_test.dart
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,108 @@ | ||
// Copyright (c) 2024, the Dart project authors. Please see the AUTHORS file | ||
// for details. All rights reserved. Use of this source code is governed by a | ||
// BSD-style license that can be found in the LICENSE file. | ||
|
||
@JS() | ||
library allowed_external_member_type_test; | ||
|
||
import 'dart:js_interop'; | ||
|
||
@JS() | ||
@staticInterop | ||
class JSClass { | ||
external factory JSClass(List<int> baz); | ||
// ^ | ||
// [web] Type 'List<int>' is not a valid type in the signature of 'dart:js_interop' external APIs or APIs converted via 'toJS'. | ||
|
||
external factory JSClass.other(Object blu); | ||
// ^ | ||
// [web] Type 'Object' is not a valid type in the signature of 'dart:js_interop' external APIs or APIs converted via 'toJS'. | ||
|
||
external static dynamic foo(); | ||
// ^ | ||
// [web] Type 'dynamic' is not a valid type in the signature of 'dart:js_interop' external APIs or APIs converted via 'toJS'. | ||
|
||
external static Function get fooGet; | ||
// ^ | ||
// [web] Type 'Function' is not a valid type in the signature of 'dart:js_interop' external APIs or APIs converted via 'toJS'. | ||
|
||
external static set fooSet(void Function() bar); | ||
// ^ | ||
// [web] Type 'void Function()' is not a valid type in the signature of 'dart:js_interop' external APIs or APIs converted via 'toJS'. | ||
} | ||
|
||
extension JSClassExtension on JSClass { | ||
external dynamic extFoo(); | ||
// ^ | ||
// [web] Type 'dynamic' is not a valid type in the signature of 'dart:js_interop' external APIs or APIs converted via 'toJS'. | ||
|
||
external JSClass extFoo2(List<Object?> bar); | ||
// ^ | ||
// [web] Type 'List<Object?>' is not a valid type in the signature of 'dart:js_interop' external APIs or APIs converted via 'toJS'. | ||
|
||
external Function get extFooGet; | ||
// ^ | ||
// [web] Type 'Function' is not a valid type in the signature of 'dart:js_interop' external APIs or APIs converted via 'toJS'. | ||
|
||
external set extFooSet(void Function() bar); | ||
// ^ | ||
// [web] Type 'void Function()' is not a valid type in the signature of 'dart:js_interop' external APIs or APIs converted via 'toJS'. | ||
} | ||
|
||
@JS() | ||
extension type ExtensionType(JSObject _) {} | ||
|
||
@JS() | ||
external void jsFunctionTest(JSFunction foo); | ||
|
||
@JS() | ||
external void useStaticInteropClass(JSClass foo); | ||
|
||
@JS() | ||
external void useStaticInteropExtensionType(ExtensionType foo); | ||
|
||
void declareTypeParameter<T extends JSAny?>() {} | ||
|
||
T declareAndUseTypeParameter<T extends JSAny?>(T t) => t; | ||
|
||
T declareAndUseInvalidTypeParameter<T>(T t) => t; | ||
|
||
void main() { | ||
((double foo) => 4.0.toJS).toJS; | ||
|
||
((JSNumber foo) => 4.0).toJS; | ||
|
||
((List foo) => 4.0).toJS; | ||
// ^ | ||
// [web] Type 'List<dynamic>' is not a valid type in the signature of 'dart:js_interop' external APIs or APIs converted via 'toJS'. | ||
|
||
((JSNumber foo) => () {}).toJS; | ||
// ^ | ||
// [web] Type 'Null Function()' is not a valid type in the signature of 'dart:js_interop' external APIs or APIs converted via 'toJS'. | ||
|
||
((((JSNumber foo) => 4.0) as dynamic) as Function).toJS; | ||
// ^ | ||
// [web] `Function.toJS` requires a statically known function type, but Type 'Function' is not a precise function type, e.g., `void Function()`. | ||
|
||
void typeParametersTest<T extends JSAny, U extends ExtensionType, | ||
V extends JSClass, W, Y>() { | ||
((T t) => t).toJS; | ||
((U u) => u).toJS; | ||
((V v) => v).toJS; | ||
((W w) => w as Y).toJS; | ||
// ^ | ||
// [web] Type 'W' is not a valid type in the signature of 'dart:js_interop' external APIs or APIs converted via 'toJS'. | ||
// [web] Type 'Y' is not a valid type in the signature of 'dart:js_interop' external APIs or APIs converted via 'toJS'. | ||
|
||
declareTypeParameter.toJS; | ||
// ^ | ||
// [web] Functions converted via `toJS` cannot declare type parameters. | ||
declareAndUseTypeParameter.toJS; | ||
// ^ | ||
// [web] Functions converted via `toJS` cannot declare type parameters. | ||
declareAndUseInvalidTypeParameter.toJS; | ||
// ^ | ||
// [web] Functions converted via `toJS` cannot declare type parameters. | ||
// [web] Type 'T' is not a valid type in the signature of 'dart:js_interop' external APIs or APIs converted via 'toJS'. | ||
} | ||
} |
Oops, something went wrong.