Skip to content

Add a way to prevent pakettic from reordering certain expressions (was: Lua issue with table access expression reordering) #15

@AMcBain

Description

@AMcBain

I have some code that, at least in my original TIC-80 program works perfectly but after minification (with default settings) it fails with invalid order function for sorting.

tl;dr: The problem isn't the minification per se, it's that certain table accesses if not done in one of a few specific orders results in different behavior/output.


Original code (works):

table.sort(b0,function (a,b)
  ax=(a[1].x+a[2].x+a[3].x)/3
  ay=(a[1].y+a[2].y+a[3].y)/3
  az=(a[1].z+a[2].z+a[3].z)/3
  bx=(b[1].x+b[2].x+b[3].x)/3  
  by=(b[1].y+b[2].y+b[3].y)/3
  bz=(b[1].z+b[2].z+b[3].z)/3
  if az~=bz then
   return az>bz
  end
  if ax~=bx then
   return ax<bx
  end
  return ay>by
 end)

b0 is a table that contains triangles ({{x=1.0,y=1.0,z=1.0}}) that have doubles as property values.

Minified code (fails):

table.sort(b0,function(a,b)
 ax=(a[3].x+a[2].x+a[1].x)/3
 ay=(a[3].y+a[2].y+a[1].y)/3
 az=(a[3].z+a[2].z+a[1].z)/3
 bx=(b[2].x+b[3].x+b[1].x)/3
 by=(b[2].y+b[3].y+b[1].y)/3
 bz=(b[2].z+b[1].z+b[3].z)/3
 if az~=bz then return bz<az end
 if bx~=ax then return ax<bx end
 return by<ay end
)

This has been reformatted for clarity and the variables renamed to match original, but I've tested to ensure the error still occurs. I also verified the comparison "flip" is not the cause either since it maintains the original meaning.


The Fix

 bz=(b[2].z+b[3].z+b[1].z)/3

This reorders the table access for line 6 of the sort function to match the two above it. Then it doesn't fail.

Buuuut I still don't have any d-mn idea why that fixes it because if go grab my original code and pair it with the minified code:

 ax=(a[3].x+a[2].x+a[1].x)/3
 ay=(a[3].y+a[2].y+a[1].y)/3
 az=(a[3].z+a[2].z+a[1].z)/3
 bx=(b[1].x+b[2].x+b[3].x)/3
 by=(b[1].y+b[2].y+b[3].y)/3
 bz=(b[1].z+b[2].z+b[3].z)/3

It now fails!

But this works!

 bx=(b[3].x+b[2].x+b[1].x)/3
 by=(b[3].y+b[2].y+b[1].y)/3
 bz=(b[2].z+b[3].z+b[1].z)/3

I am so confused.

This issue isn't really the minifiers fault, since I can trigger it in unminified code, but there's two-three issues that may be:

  1. The minifier doesn't seem to care if I put --{! and --} around entire table.sort block, and reorders stuff inside anyway.
  2. Even if I reorder the non-minified accesses to match the order of the initial minified output, minifying will still reorder them.
  3. I had a working minified version before moving a couple unrelated statements elsewhere in the file, so it only messes with these arbitrarily.

-- Starchaser

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementNew feature or request

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions