I'm just learning the basics of asmjs, but I've got an error. I have no idea what did I wrong.
TypeError: asm.js type error: arguments to a comparison must both be signed, unsigned or doubles; int and int are given
code:
window.onload = (function(stdlib, foreign) {
"use asm";
var log = foreign.log;
function main() {
var a=0, b=0;
a=10;
b=20;
if(a<b) {
log(0.0);
} else {
log(1.0);
}
return;
}
return {main:main};
}(window, {log:console.log})).main;
There are a couple of different parts of the specification at play here. Let's work our way backwards from the error.
The error states:
arguments to a comparison must both be signed, unsigned or doubles; int and int are given
. Thus, the problem is with the following line of code:If we look up the type semantics of the operator
<
, we get the following table:This clearly reflects the wording of the error message; the
<
operator is only valid for comparisons between signed, unsigned, or double types. "But why?", you ask?The description of the
int
type may give us a hint. Here's an excerpt:Thus, taking the comparison operation in isolation, ASM.js would have no way of knowing which of the two
int
values is less than the other, because it would not know the signedness of the integers.So, why are the variables being resolved as type
int
?Since you're declaring the variables inside of the function itself, we must refer to the following part of the spec:
There we go. Since you're initializing the variables as
var a=0, b=0;
, that corresponds to the latter part of the spec, thus resolving to a type ofint
.So how do we fix this? Well, we need a type cast to something that the
<
operator supports. The|
operator can take twointish
expressions, and return asigned
, so that will suffice.Note: The type diagram, together with the operator type rules are very useful when trying to debug type issues like this.