Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Feature Request] dart:js_interop needs isJSAny and isJSObject for real-world use cases. #59932

Closed
gmpassos opened this issue Jan 17, 2025 · 3 comments
Labels
area-web Use area-web for Dart web related issues, including the DDC and dart2js compilers and JS interop. triage-automation See https://github.com/dart-lang/ecosystem/tree/main/pkgs/sdk_triage_bot. type-enhancement A request for a change that isn't a bug web-js-interop Issues that impact all js interop

Comments

@gmpassos
Copy link
Contributor

Currently, dart:js_interop only provides isA from the JSAnyUtilityExtension to check for an extension type in the "JS world."

While migrating from dart:html to the web package, we encountered issues in real-world use cases.

The main problem arises when a "JS" instance (JSAny) is assigned to a variable of type Object. There's currently no official or secure way to cast an Object back to a JSAny. Technically, it is prohibitive to handle a JS type as Object because, once treated as an Object, you cannot safely cast it back to JSAny without risking a casting exception, invalid behavior, or even an invalid Wasm cast.

To address this, it would be helpful to have the following extension, with the correct implementation provided based on the platform (JS or Wasm):

extension ObjectToJSAnyNullableExtension on Object? {
  bool get isJSAny;
  bool get isJSObject;
}

This would enable developers to correctly check if an Object is a JSAny or JSObject before attempting a cast.
isJSObject is also included because, in most cases, isA is used to check for a class type rather than a primitive type.

NOTE: After discussing this issue with other developers, we concluded that without a secure way to cast Object to JSAny, the new dart:js_interop is not stable for real-world use cases.

@gmpassos
Copy link
Contributor Author

FYI: @kevmoo @mit-mit

@dart-github-bot
Copy link
Collaborator

Summary: Users need isJSAny and isJSObject extensions for dart:js_interop to safely check if an Object is a JSAny or JSObject before casting, improving real-world usability. Currently, safe casting from Object back to JSAny is impossible.

@dart-github-bot dart-github-bot added area-web Use area-web for Dart web related issues, including the DDC and dart2js compilers and JS interop. triage-automation See https://github.com/dart-lang/ecosystem/tree/main/pkgs/sdk_triage_bot. type-enhancement A request for a change that isn't a bug labels Jan 17, 2025
@sigmundch sigmundch added the web-js-interop Issues that impact all js interop label Jan 17, 2025
@srujzs
Copy link
Contributor

srujzs commented Jan 18, 2025

Duplicate of #56905

I agree that we need this. Unfortunately, JS values flow to where Object is used too often for something like this not to exist.

@srujzs srujzs marked this as a duplicate of #56905 Jan 18, 2025
@srujzs srujzs closed this as completed Jan 18, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area-web Use area-web for Dart web related issues, including the DDC and dart2js compilers and JS interop. triage-automation See https://github.com/dart-lang/ecosystem/tree/main/pkgs/sdk_triage_bot. type-enhancement A request for a change that isn't a bug web-js-interop Issues that impact all js interop
Projects
None yet
Development

No branches or pull requests

4 participants