Siorki / RegPack

Self-contained packer for size-constrained JS code
Other
301 stars 14 forks source link

Cannot read property 'first' of undefined #94

Closed xem closed 5 years ago

xem commented 5 years ago

Hello,

RegPack gives me this error when I try to pack this code:

onload=function(){var M=(a,M,I,t,A)=>{var V={C:a,I:0,F:t,R:A,V:r(0,0),IM:I?1/I:0,A:r(0,I?100:0),AN:0,AV:0,AA:0,RA:M,SP:r(a.x,a.y+M)};V.I=V.IM?1/V.IM*(V.RA*V.RA)/12:0,v.push(V)},I=(a,M)=>{a.SP=A(a.SP,M),a.C=A(a.C,M)},r=(a,M)=>({x:a,y:M}),t=a=>h(a,a)**.5,A=(a,M)=>r(a.x+M.x,a.y+M.y),V=(a,M)=>A(a,o(M,-1)),o=(a,M)=>r(a.x*M,a.y*M),h=(a,M)=>a.x*M.x+a.y*M.y,n=(a,M)=>a.x*M.y-a.y*M.x,y=(a,M,I,t=a.x-M.x,A=a.y-M.y)=>r(t*Math.cos(I)-A*Math.sin(I)+M.x,t*Math.sin(I)+A*Math.cos(I)+M.y),x=a=>o(a,1/(t(a)||1)),v=[],e=(a,M,I)=>{D=a,N=M,S=I,E=A(I,o(M,a))};for(M(r(400,1100),800,0,.5,.5),i=30;i--;)M(r(800*Math.random(),450*Math.random()/2),20*Math.random()+10,30*Math.random(),Math.random(),Math.random());setInterval(M=>{for(a.width^=0,i=v.length;i--;){c.strokeStyle="#888";var C=v[i];c.beginPath(),c.arc(C.C.x,C.C.y,C.RA,0,7),c.moveTo(C.SP.x,C.SP.y),c.lineTo(C.C.x,C.C.y),c.closePath(),c.stroke()}for(k=15;k--;)for(i=v.length;i--;)for(j=v.length;j-- >i;){var f=v[i],s=v[j],R=V(s.C,f.C),l=f.RA+s.RA,m=t(R);if(m<Math.sqrt(l*l)){var P=x(o(R,-1)),d=o(P,s.RA);if(e(l-m,x(R),A(s.C,d)),f.IM||s.IM){var g=f.IM,F=s.IM,u=D/(g+F)*.8,T=o(N,u);I(f,o(T,-g)),I(s,o(T,F));var b=N,p=o(S,s.IM/(f.IM+s.IM)),q=o(E,f.IM/(f.IM+s.IM)),w=A(p,q),z=V(w,f.C),B=V(w,s.C),G=A(f.V,r(-1*f.AV*z.y,f.AV*z.x)),H=A(s.V,r(-1*s.AV*B.y,s.AV*B.x)),J=V(H,G),K=h(J,b);if(K<0){var L=Math.min(f.R,s.R),O=Math.min(f.F,s.F),Q=n(z,b),U=n(B,b),W=-(1+L)*K;W/=f.IM+s.IM+Q*Q*f.I+U*U*s.I;var X=o(b,W);f.V=V(f.V,o(X,f.IM)),s.V=A(s.V,o(X,s.IM)),f.AV-=Q*W*f.I,s.AV+=U*W*s.I;var Y=V(J,o(b,h(J,b)));Y=o(x(Y),-1);var Z=n(z,Y),$=n(B,Y),_=-(1+L)*h(J,Y)*O;(_/=f.IM+s.IM+Z*Z*f.I+$*$*s.I)>W&&(_=W),X=o(Y,_),f.V=V(f.V,o(X,f.IM)),s.V=A(s.V,o(X,s.IM)),f.AV-=Z*_*f.I,s.AV+=$*_*s.I}}}}for(i=v.length;i--;){var aa=v[i],Ma=.016;aa.V=A(aa.V,o(aa.A,Ma)),I(aa,o(aa.V,Ma)),aa.AV+=aa.AA*Ma,aa.AN+=aa.AV*Ma,aa.SP=y(aa.SP,aa.C,aa.AV*Ma)}},9)}

I think it's not my fault... but I don't know what happens

EDIT: it works when I uncheck the "reassign vars" option.

Siorki commented 5 years ago

Variable reassignment fails, there are more names to replace than available characters. I'll keep investigating to understand why since this condition was not supposed to be possible.

Siorki commented 5 years ago

Nailed it.

z is one of the variables, but is missing from the replacement candidates. Henceforth, there is one variable missing for the replacement.

The bug is in Shapeshifter::reassignVariableNames(), upon building the candidate variable list in the variable unusedBlocks. If a block is initiated inside the loop, but not terminated (no keyword found in between), if never gets added to the list.

Proposed fix : after the loop, if a block is started (blockStartsAt!=-1), append it to the loop. nextToLast can be set at 128, above the ASCII range.

In the meantime, you can disable "reassign vars" with no ill effect, as no replacement is performed anyway - there is simply no unused variable name.

Siorki commented 5 years ago

Implemented proposed fix - solves the issue. https://github.com/Siorki/RegPack/commit/a61e8090fd71618e4470fed4471ff4d76fbbcfd2 in branch master (in case we want to publish a v5.0.3 while working on v6.0)