Incorrect comparison of two keyCodes in async function after dart2js
Asked Answered
F

2

6

I don't understand this behavior of dart2js code.
I have this only in async function and only after compiling to JS.

e.keyCode is equal 13
KeyCode.ENTER is equal 13

but

(e.keyCode == KeyCode.ENTER) is false

This is simple code to debugging my issue.
Whats going on?

import 'dart:html';

main() async
{
  await for(KeyboardEvent e in window.onKeyDown)
  {
    print('e.keyCode : ${e.keyCode}');
    print('e.keyCode.hashCode : ${e.keyCode.hashCode}');
    print('KeyCode.ENTER : ${KeyCode.ENTER}');
    print('KeyCode.ENTER.hashCode : ${KeyCode.ENTER.hashCode}');
    print('e.keyCode.runtimeType : ${e.keyCode.runtimeType}');
    print('KeyCode.ENTER.runtimeType : ${KeyCode.ENTER.runtimeType}');
    print('e.keyCode == KeyCode.ENTER ${e.keyCode == KeyCode.ENTER}');
    print('e.keyCode != KeyCode.ENTER ${e.keyCode != KeyCode.ENTER}');
    int a = e.keyCode;
    int b = KeyCode.ENTER;
    print('a = $a');
    print('b = $b');
    print('a.hashCode = ${a.hashCode}');
    print('b.hashCode = ${b.hashCode}');
    print('a == b ${(a == b).toString()}');
    print('a == 13 ${(a == 13).toString()}');
    print('b == 13 ${(b == 13).toString()}');
    if(a == b)
      print('DART: a == b');
    else
      print('DART: a != b');
  }
}

Output on Chrome after pressing Enter (dart2js - minified):

e.keyCode : 13
e.keyCode.hashCode : 13
KeyCode.ENTER : 13
KeyCode.ENTER.hashCode : 13
e.keyCode.runtimeType : int
KeyCode.ENTER.runtimeType : int
e.keyCode == KeyCode.ENTER false
e.keyCode != KeyCode.ENTER true
a = 13
b = 13
a.hashCode = 13
b.hashCode = 13
a == b true
a == 13 true
b == 13 true
DART: a != b

On DartVM (Dartium) everything is correct:

e.keyCode : 13
e.keyCode.hashCode : 13
KeyCode.ENTER : 13
KeyCode.ENTER.hashCode : 13
e.keyCode.runtimeType : int
KeyCode.ENTER.runtimeType : int
e.keyCode == KeyCode.ENTER true
e.keyCode != KeyCode.ENTER false
a = 13
b = 13
a.hashCode = 13
b.hashCode = 13
a == b true
a == 13 true
b == 13 true
DART: a == b

// EDIT
I noticed that it does not matter that I'm using keyCode.
This is async issue.
Code below returns 'OK' on Dartium and 'NOPE' on Chrome after compiling to JS.

import 'dart:async';

main() async
{
  var ctrl = new StreamController();
  ctrl.add(true);

  await for(var e in ctrl.stream)
  {
    if(e == e)
      print('OK');
    else
      print('NOPE');
  }
}
Folkmoot answered 18/4, 2015 at 0:41 Comment(3)
You should file a bug at www.dartbug.com. Which version of dart-sdk are you using?Tennietenniel
the final .js code does not even contain the string "DART: a == b".Tennietenniel
Dart Editor version 1.9.3.release (STABLE) Dart SDK version 1.9.3Folkmoot
L
3

This is indeed the same as this bug.

The wrong type was inferred for the iteration variable of async for loops.

It is fixed in 1.10.

Lisabethlisan answered 20/4, 2015 at 8:54 Comment(5)
This does not provide an answer to the question. To critique or request clarification from an author, leave a comment below their post - you can always comment on your own posts, and once you have sufficient reputation you will be able to comment on any post.Reavis
@Reavis I do think this answer the question, as there's no real solution but to update. I edited the answer to add the link to the bugtracker for future readers reference.Lode
I also don't see any additional info to @0pako0 s answer.Inventive
@GünterZöchbauer Well, there's the confirmation this is a known bug, gives the link to it, does a brief in case the link break and ensure the version where is it fixed. It's a better answer IMHO as there's facts and not only observations.Lode
I would have prefered to comment @0pako0's answer - but did not have enough points.Lisabethlisan
F
1

This must be 1.9.3 dart2js bug.

I use now Dart SDK version 1.10.0-dev.1.5 and everything works fine. This is only solution what I found, if I want use 'await for'.

Folkmoot answered 19/4, 2015 at 13:15 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.