TranscryptOrg / Transcrypt

Python 3.9 to JavaScript compiler - Lean, fast, open!
https://www.transcrypt.org
Apache License 2.0
2.82k stars 215 forks source link

A boolean operator is translated to a bitwise operator, not a boolean operator #856

Open chopin opened 1 year ago

chopin commented 1 year ago

I found that Python boolean operator '|=' was translated to '|=' in Javascript. They look the same, but they are not the same.

In Javascript, '|=' is a bitwise operator returning 1 or 0, not true or false. Javascript has another operator for boolean operations, '||=' returning true or false. Following is examples showing the difference:

// Javascript example:
var b= false;
b||= true;
console.log("after b||= true; b= ", b);
b|= true;
console.log("after b|= true; b= ", b);

output result:

after b||= true; b=  true
after b|= true; b=  1

Thanks.

chopin commented 1 year ago

It seems that Python |= operates either in boolean or in bitwise depending on the types of its operands, but Javascript doesn't. Does Transcyprt have an option to translate it correctly? Should I modified all Python source code using boolean operators?