-
Notifications
You must be signed in to change notification settings - Fork 1.6k
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
Add a dart:js_interop
API that can determine if an Object
is a JS value
#56905
Comments
dart:js_interop
API that can determine if a Object
is a JS value dart:js_interop
API that can determine if an Object
is a JS value
Any update on this? |
We urgently need something like the extension below, with official support for each platform: extension ObjectToJSAnyNullableExtension on Object? {
bool get isJSAny;
bool get isJSObject;
} NOTE: After discussing this issue with other developers, we concluded that, without a secure way to cast |
@srujzs is the expert here. I will make sure we follow-up this week. |
Thanks for the quick response |
I want to get this done before the next stable, for both the JS compilers and dart2wasm. I don't believe we need |
I agree that It also provides an opportunity for an optimized implementation of Therefore, it is much easier to implement I have internally implemented a |
Historically, it was common for users to treat JS values through interop as Dart types and not JS types, mainly because the concept of "JS types" did not exist yet. So, values may flow through as
Object
ordynamic
to other parts of code. This code may accept both a Dart and a JS value, making it difficult to determine how to treat such a value without some way of distinguishing the two runtimes.The canonical way to type-check Dart objects is to use
is
, but we don't want users using that if it's a JS value, as it may give inconsistent values. This is partially why we have theinvalid_runtime_check_with_js_interop_types
lint.The canonical way to type-check JS values is to use
isA
, but that requires a cast toJSAny
in the first place. This cast will always succeed when compiling to JS (because it'sObject
under-the-hood there), but may fail when compiling to Wasm (because it'sJSValue
). This is our best option today.So, to determine how to type-check a given
Object
, it is useful to know whether it's a JS value in the first place. One such mechanism is a compiler-specificexternal
patched API indart:js_interop
.Some quirks that we might come across:
String
,int
/double
/num
,bool
) are both Dart and JS values when compiling to JS. This is not true when compiling to Wasm. We may want to always treat them as JS values for the sake of this API, but users should be aware that they should still type-check for the Dart primitive types when this API returns false so that they handle the Wasm case correctly.invalid_runtime_check_with_js_interop_types
check to account for this new API e.g. pointing users to this API when they do ais JSAny
check.The text was updated successfully, but these errors were encountered: