Very weird bug with simple conditional replaced by _$UInt_UInt_$Impl_$.gt(var1, var2) (hate to js)

30 views
Skip to first unread message

Thomas John

unread,
Dec 6, 2016, 9:39:42 AM12/6/16
to Haxe
Hello

I'm using the latest Haxe 3.4.0 and compiling into js.
I'm encountering a very weird bug when doing a simple condition like so:

var indexFrom:UInt = 2;
var indexTo:UInt = 0;

js.Browser.console.log(indexFrom < indexTo - 1);

is compiled into:

window.console.log(_$UInt_UInt_$Impl_$.gt(indexTo - 1,indexFrom));

which gives me "true" as a result. And it should be false (unless I'm missing something)...

the code of that function is:

_$UInt_UInt_$Impl_$.gt = function(a,b) {
var aNeg = a < 0;
var bNeg = b < 0;
if(aNeg != bNeg) {
return aNeg;
} else {
return a > b;
}
};

and it shows that when the sign of the two parameters are different, it returns the check on whether the first parameter is negative or not. Which, in my case, is wrong...
And why would a simple condition be compiled into a function call with such a complicated system ?

Maybe I'm doing something wrong.

Thanks.

Thomas.


clemos

unread,
Dec 6, 2016, 10:02:44 AM12/6/16
to haxe...@googlegroups.com
It seems right to me :
-1 converted to UInt (using -1 >>> 0 in chrome dev tools for example)
is 4294967295, which is greater than 2.

Since uint don't exist natively in JS, Haxe uses abstracts to
"simulate" them with signed ints, hence this gt implementation:
- if a and b have different signs and a is negative, then a is always
greater (because a's sign bit (ie the "biggest" bit) is 1 whereas b's
is 0)
- in all other cases, it is safe to just compare a and b

Best,
Clément
> --
> To post to this group haxe...@googlegroups.com
> http://groups.google.com/group/haxelang?hl=en
> ---
> You received this message because you are subscribed to the Google Groups
> "Haxe" group.
> For more options, visit https://groups.google.com/d/optout.

Thomas John

unread,
Dec 6, 2016, 10:10:01 AM12/6/16
to Haxe
Ok, now I understand, I'm working with UInt and one of the two values is -1... and this should not happen.

Thomas John

unread,
Dec 6, 2016, 10:11:33 AM12/6/16
to Haxe
yes, I just noticed it... my bad :)
thanks for the answer
Reply all
Reply to author
Forward
0 new messages