fix >>> (unsigned right shift) producing wrong results on Lua 5.3+ (#… · TypeScriptToLua/TypeScriptToLua@5176fcd · GitHub
Skip to content

Commit 5176fcd

Browse files
authored
fix >>> (unsigned right shift) producing wrong results on Lua 5.3+ (#1703)
* fix >>> (unsigned right shift) producing wrong results on Lua 5.3+ TS >>> is a logical right shift (zero-fills from the left), but TSTL was mapping it to Lua's >> which is arithmetic (sign-extends). This gave wrong results for negative numbers: e.g. -1 >>> 0 should be 4294967295 but produced -1. Fix by masking to unsigned 32-bit first: (left & 0xFFFFFFFF) >> right. The Lua 5.2 (bit32.rshift) and LuaJIT (bit.rshift) paths were already correct. * lint
1 parent c043cdc commit 5176fcd

3 files changed

Lines changed: 51 additions & 6 deletions

File tree

‎src/transformation/visitors/binary-expression/bit.ts‎

Lines changed: 16 additions & 0 deletions

‎test/unit/__snapshots__/expressions.spec.ts.snap‎

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -374,14 +374,14 @@ return ____exports"
374374
375375
exports[`Bitop [5.3] ("a>>>=b") 1`] = `
376376
"local ____exports = {}
377-
a = a >> b
377+
a = (a & 4294967295) >> b
378378
____exports.__result = a
379379
return ____exports"
380380
`;
381381
382382
exports[`Bitop [5.3] ("a>>>b") 1`] = `
383383
"local ____exports = {}
384-
____exports.__result = a >> b
384+
____exports.__result = (a & 4294967295) >> b
385385
return ____exports"
386386
`;
387387
@@ -445,14 +445,14 @@ return ____exports"
445445
446446
exports[`Bitop [5.4] ("a>>>=b") 1`] = `
447447
"local ____exports = {}
448-
a = a >> b
448+
a = (a & 4294967295) >> b
449449
____exports.__result = a
450450
return ____exports"
451451
`;
452452
453453
exports[`Bitop [5.4] ("a>>>b") 1`] = `
454454
"local ____exports = {}
455-
____exports.__result = a >> b
455+
____exports.__result = (a & 4294967295) >> b
456456
return ____exports"
457457
`;
458458
@@ -516,14 +516,14 @@ return ____exports"
516516
517517
exports[`Bitop [5.5] ("a>>>=b") 1`] = `
518518
"local ____exports = {}
519-
a = a >> b
519+
a = (a & 4294967295) >> b
520520
____exports.__result = a
521521
return ____exports"
522522
`;
523523
524524
exports[`Bitop [5.5] ("a>>>b") 1`] = `
525525
"local ____exports = {}
526-
____exports.__result = a >> b
526+
____exports.__result = (a & 4294967295) >> b
527527
return ____exports"
528528
`;
529529

‎test/unit/expressions.spec.ts‎

Lines changed: 29 additions & 0 deletions

0 commit comments

Comments
 (0)