-
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
@JS()
doesn't support or fail on external List<NotDynamic> get
#34195
Comments
The following code is invalid in Dart 2: @JS()
class Farm {
external factory Farm();
external List<Animal> animals();
// ^^^^^^^^^^^^^^
} In JavaScript-compiled Dart, the backing implementation of List<Animal> animals = <dynamic>[]; This of course, is not valid in Dart 2. We'll have to either:
You can see some documentation here: |
Thanks for the documentation repo - I can confirm using Can I ask what the argument is for the first option over the second? As I understand it, there's no way of getting a typed list back from javascript, so I'd rather not have to write
for every class in something like this, which is itself already a wrapper around another source (even if that wrapper is generated automatically). |
Having to change 2 compilers in a potentially non-trivial way to add code that isn't always necessary, I guess. It's not really clear to me what or how
Both carry an overhead too. Technically I believe this is an issue with DDC as well, not just Dart2JS, so re-categorizing. |
@JS()
doesn't support or fail on external List<NotDynamic> get
I think at this point I would prefer option 2, automatic List.from. Without this change, if we had to wrap ourselves with @JS('foo.bar.getStuff')
external List<dynamic /* num */> _getStuff();
List<num> getStuff() => _getStuff();
@JS('foo.bar.getThings')
external List<dynamic /* num */ > _getThings(Something a, [String b, String c]);
List<num> getThings(Something a, [String b, String c]) {
if (b == null) {
return _getThings(a);
} else {
return c == null ? _getThings(a, b) : _getThings(a, b, c);
}
} What a joy that is! (The proper arity is required for And I'm not sure how you would write a similar wrapper for a class: @JS('foo.bar.Bar')
class Bar {
external factory Bar();
// Can you wrap this with `@JS()` as well???
// So that you could write this as _getList()?
external List<num> getList();
} |
Ah, I see @jackd actually already gave a little solution to the class issue. Here's a slightly different take, with some visibility benefits. Any code outside this library only sees the one class, and is unaware that it's a wrapper. Let's say Farm's constructor also takes a @JS('Farm')
class _Farm {
external factory _Farm(String name);
external List<Animal> animals();
}
class Farm {
final _Farm _jsFarm;
factory Farm(String name) {
var jsFarm = _Farm(name);
return Farm._(jsFarm);
}
Farm._(this._jsFarm);
List<Animal> animals() => List<Animal>.from(_jsFarm.animals());
} |
My biggest issue here isn't so much the need for additional code, but the inconsistency between DDC and dart2js. Currently, |
To be clear, both DDC and Dart2JS always throw. Dart2JS with |
Confirmed, sorry for the confusion. That comment was based on my experience with 2.0 dev release. |
I'll add a note to #35084 so we handle this correctly, and merge this bug into that one. Thanks so much for reporting! I'm working on (what I hope will be) big improvements to JS interop. TL;DR -- I want to support the initial example. Arrays created in JS will not have a Dart reified type, so we'll allow them to be cast to any list type. |
Original issue file by @jackd dart-lang/build#1771
Dart2js produces code throwing
TypeError
s when using wrapped javascript which is known to return typed arrays. ddc code runs fine.cast
on the output of dart2js fixes the issue, but is somewhat unsatisfying.Repo demonstrating the issue here (including all code below).
Result of running ddc js:
woof, moo
Result of running dart2js js:
Uncaught Error: TypeError: Closure 'minified:by': type '(minified:F) => String' is not a subtype of type '(dynamic) => String'
farm.js
farm.dart
index.dart
index.html
The text was updated successfully, but these errors were encountered: