Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Unpacked source has "in" strings in wrong places. #73

Closed
kanaka opened this issue Feb 20, 2017 · 3 comments
Closed

Unpacked source has "in" strings in wrong places. #73

kanaka opened this issue Feb 20, 2017 · 3 comments
Assignees
Labels
Milestone

Comments

@kanaka
Copy link
Contributor

kanaka commented Feb 20, 2017

I recently upgraded miniMAL to use a more recent version of RegPack and while most of the source fils pack/unpack correctly, I discovered a case where the unpacked source is wrong. This happened with the options --crushGainFactor 2 --crushLengthFactor 1 --crushCopiesFactor 0. For this same file, using 1, 0, 0 fixes the problem. However, there might be source files for which 1, 0, 0 also displays this problem so it's not obvious to me that the 2, 1, 0 options are where the problem lies or just revealing a deeper issue.

Here is input source that triggers the problem:

module.exports=function(n){function e(n,e){for(;n instanceof Array&&n[0]in e&&e[n[0]].M;)n=e[n[0]](...n.slice(1));return n}function t(n,r){for(;;){if(!(n instanceof Array))return i(n,r);if(n=e(n,r),!(n instanceof Array))return i(n,r);if("def"==n[0])return r[n[1]]=t(n[2],r);if("~"==n[0]){let e=t(n[1],r);return e.M=1,e}if("`"==n[0])return n[1];if(".-"==n[0]){let e=i(n.slice(1),r),t=e[0][e[1]];return 2 in e?e[0][e[1]]=e[2]:t}if("."==n[0]){let e=i(n.slice(1),r),t=e[0][e[1]];return t.apply(e[0],e.slice(2))}if("try"==n[0])try{return t(n[1],r)}catch(e){return t(n[2][2],i([n[2][1]],r,[e]))}else if("fn"==n[0]){let e=function(...e){return t(n[2],i(n[1],r,e))};return e.A=[n[2],r,n[1]],e}if("let"==n[0]){r=Object.create(r);for(let e in n[1])e%2&&(r[n[1][e-1]]=t(n[1][e],r));n=n[2]}else if("do"==n[0]){let e=i(n.slice(1,n.length-1),r);n=n[n.length-1]}else if("if"==n[0])n=t(n[1],r)?n[2]:n[3];else{let e=i(n,r),t=e[0];if(!t.A)return t(...e.slice(1));n=t.A[0],r=i(t.A[2],t.A[1],e.slice(1))}}}let i=function(e,i,r){return r?(i=Object.create(i),e.some((n,t)=>"&"==n?i[e[t+1]]=r.slice(t):(i[n]=r[t],0)),i):e instanceof Array?e.map((...n)=>t(n[0],i)):typeof ""==typeof e?e in i?i[e]:n.throw(e+" not found"):e};return n=Object.assign(Object.create(n),{js:eval,eval:(...e)=>t(e[0],n),"=":(...n)=>n[0]===n[1],"<":(...n)=>n[0]<n[1],"+":(...n)=>n[0]+n[1],"-":(...n)=>n[0]-n[1],"*":(...n)=>n[0]*n[1],"/":(...n)=>n[0]/n[1],isa:(...n)=>n[0]instanceof n[1],type:(...n)=>typeof n[0],new:(...n)=>new(n[0].bind(...n)),del:(...n)=>delete n[0][n[1]],throw:(...n)=>{throw n[0]},read:(...n)=>JSON.parse(n[0]),slurp:(...n)=>require("fs").readFileSync(n[0],"utf8"),load:(...e)=>t(JSON.parse(require("fs").readFileSync(e[0],"utf8")),n),rep:(...e)=>JSON.stringify(t(JSON.parse(e[0]),n))})}

Running the resulting regpack with node results in the following error:

undefined:1
module.exports=function(n){function e(n,e){for(;n stanceof Array&&n[0] e&&e[n[0]].M;)n=e[n[0]](...n.slice(1));return n}function t(n,r){for(;;){if(!(n stanceof Array))return i(n,r);if(n=e(n,r),!(n stanceof Array))return i(n,r);if("def"==n[0])return r[n[1]]=t(n[2],r);if("~"==n[0]){let e=t(n[1],r);return e.M=1,e}if("`"==n[0])return n[1];if(".-"==n[0]){let e=i(n.slice(1),r),t=e[0][e[1]];return 2  e?e[0][e[1]]=e[2]:t}if("."==n[0]){let e=i(n.slice(1),r),t=e[0][e[1]];return t.apply(e[0],e.slice(2))}if("try"==n[0])try{return t(n[1],r)}catch(e){return t(n[2][2]in,i([n[2][1]],r,[e]))}else if("fn"==n[0]){let e=function(...e){return t(n[2]in,i(n[1],r,e))};return e.A=[n[2],r,n[1]],e}if("let"==n[0]){r=Object.create(r);for(let e  n[1])e%2&&(r[n[1][e-1]]=t(n[1][e],r));n=n[2]}else if("do"==n[0]){let e=i(n.slice(1,n.length-1),r);n=n[n.length-1]}else if("if"==n[0])n=t(n[1],r)?n[2]:n[3];else{let e=i(n,r),t=e[0];if(!t.A)return t(...e.slice(1));n=t.A[0],r=i(t.A[2],t.A[1],e.slice(1))}}}let i=function(ein,i,r){return r

SyntaxError: Unexpected identifier
    at Object.<anonymous> (/data/joelm/personal/programming/miniMAL/js/blah-regpack.js:11:6)
    at Module._compile (module.js:571:32)
    at Object.Module._extensions..js (module.js:580:10)
    at Module.load (module.js:488:32)
    at tryModuleLoad (module.js:447:12)
    at Function.Module._load (module.js:439:3)
    at Module.runMain (module.js:605:10)
    at run (bootstrap_node.js:420:7)
    at startup (bootstrap_node.js:139:9)
    at bootstrap_node.js:535:3

I printed out the source code after unpacking (right before the final eval) and got this:

module.exports=function(n){function e(n,e){for(;n stanceof Array&&n[0] e&&e[n[0]].M;)n=e[n[0]](...n.slice(1));return n}function t(n,r){for(;;){if(!(n stanceof Array))return i(n,r);if(n=e(n,r),!(n stanceof Array))return i(n,r);if("def"==n[0])return r[n[1]]=t(n[2],r);if("~"==n[0]){let e=t(n[1],r);return e.M=1,e}if("`"==n[0])return n[1];if(".-"==n[0]){let e=i(n.slice(1),r),t=e[0][e[1]];return 2  e?e[0][e[1]]=e[2]:t}if("."==n[0]){let e=i(n.slice(1),r),t=e[0][e[1]];return t.apply(e[0],e.slice(2))}if("try"==n[0])try{return t(n[1],r)}catch(e){return t(n[2][2]in,i([n[2][1]],r,[e]))}else if("fn"==n[0]){let e=function(...e){return t(n[2]in,i(n[1],r,e))};return e.A=[n[2],r,n[1]],e}if("let"==n[0]){r=Object.create(r);for(let e  n[1])e%2&&(r[n[1][e-1]]=t(n[1][e],r));n=n[2]}else if("do"==n[0]){let e=i(n.slice(1,n.length-1),r);n=n[n.length-1]}else if("if"==n[0])n=t(n[1],r)?n[2]:n[3];else{let e=i(n,r),t=e[0];if(!t.A)return t(...e.slice(1));n=t.A[0],r=i(t.A[2],t.A[1],e.slice(1))}}}let i=function(ein,i,r){return r?(i=Object.create(i),e.some((n,t)=>"&"==n?i[e[t+1]]=r.slice(t):(i[n]=r[t],0)),i):e stanceof Array?e.map((...n)=>t(n[0]in,i)):typeof ""==typeof e?e  i?i[e]:n.throw(e+" not found"):e};return n=Object.assign(Object.create(n),{js:eval,eval:(...e)=>t(e[0],n),"=":(...n)=>n[0]===n[1],"<":(...n)=>n[0]<n[1],"+":(...n)=>n[0]+n[1],"-":(...n)=>n[0]-n[1],"*":(...n)=>n[0]*n[1],"/":(...n)=>n[0]/n[1]in,isa:(...n)=>n[0]stanceof n[1],type:(...n)=>typeof n[0],new:(...n)=>new(n[0].bd(...n)),del:(...n)=>delete n[0][n[1]],throw:(...n)=>{throw n[0]},read:(...n)=>JSON.parse(n[0]),slurp:(...n)=>require("fs").readFileSync(n[0],"utf8"),load:(...e)=>t(JSON.parse(require("fs").readFileSync(e[0],"utf8")),n),rep:(...e)=>JSON.strgify(t(JSON.parse(e[0]),n))})}

This is almost the original source before packing but not quite. The differences are:

  • Missing all of the original 11 occurrences of the string "in". You can easily see the first missing position if you scan across to "n stanceof" which should have been "n instanceof".
  • 5 occurrences of the string "in" in places in the code where it shouldn't be. All of these extra "in" strings occur before commas and all but one appear after a right bracket. I'm not sure if that's relevant for tracking down the bug or not.
@kanaka
Copy link
Contributor Author

kanaka commented Feb 20, 2017

I git bisected it to this commit: f43262a: "#47 : add ] and \ to allowed tokens for packer"

@Siorki Siorki added this to the 5.0.1 milestone Feb 20, 2017
@Siorki Siorki self-assigned this Feb 20, 2017
@Siorki Siorki added the bug label Feb 20, 2017
@Siorki
Copy link
Owner

Siorki commented Feb 20, 2017

Most likely the same issue as #65 . I was able to repeat the issue, and the log mentions the faulty token \ matching the original string in. Let me fix it then confirm by testing with your code.

@Siorki
Copy link
Owner

Siorki commented Feb 22, 2017

Confirmed duplicate of #65, fixing it solves your issue as well.
I added your source code to the nonreg test for #65.

@Siorki Siorki closed this as completed Feb 22, 2017
kanaka added a commit to kanaka/miniMAL that referenced this issue Feb 24, 2017
- New version of RegPack includes fix for
  Siorki/RegPack#73 that was discovered when
  packing stepB_node.js.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants