Skip to content

Commit

Permalink
Fix an invalid code when exactly 1--2 abbreviations are in use
Browse files Browse the repository at this point in the history
  • Loading branch information
lifthrasiir committed Aug 13, 2021
1 parent ade81bd commit 3f60e44
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 6 deletions.
2 changes: 1 addition & 1 deletion index.js
Expand Up @@ -930,7 +930,7 @@ export class Packer {
outputVar = `c=w=String.fromCharCode(...z)`;
} else if (this.preparedJs.abbrs.length < 3) {
secondLine += `c=w=String.fromCharCode(...z);`;
for (const [, abbr] in this.preparedJs.abbrs) {
for (const [, abbr] of this.preparedJs.abbrs) {
secondLine += `with(c.split(\`${escapeCharInTemplate(abbr)}\`))c=join(shift());`;
}
} else {
Expand Down
27 changes: 22 additions & 5 deletions test.js
Expand Up @@ -229,14 +229,31 @@ test('prepareJs without abbrs', t => {

//------------------------------------------------------------------------------

test('Packer', t => {
const packer = new Packer([{ type: 'js', action: 'eval', data: 'okay = 12345;' }], { maxMemoryMB: 10 });
function packAndEval(data) {
const packer = new Packer([{ type: 'js', action: 'eval', data }], { maxMemoryMB: 10 });
const { firstLine, secondLine } = packer.makeDecoder();

// XXX this is only okay-ish because we know the decoder internals
const possibleVars = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz_$';
let okay = false;
eval(`let ${[...possibleVars].join(',')};${firstLine};${secondLine}`);
t.is(okay, 12345);
// Function doesn't inherit the strictness, which is required for Roadroller outputs
return Function('code', 'return eval(code)')(`let ${[...possibleVars].join(',')};${firstLine};${secondLine}`);
}

test('Packer', t => {
t.is(packAndEval('3 + 4 * 5'), 23);

// abbreviation tests
t.is(packAndEval(`
const alpha = 42;
alpha + alpha + alpha + alpha + alpha
`), 42 * 5);
t.is(packAndEval(`
const alpha = 42, beta = 54;
(alpha + alpha + alpha + alpha + alpha) * (beta + beta + beta + beta)
`), 42 * 5 * 54 * 4);
t.is(packAndEval(`
const alpha = 42, beta = 54, gamma = 13;
(alpha + alpha + alpha + alpha + alpha) * (beta + beta + beta + beta) * (gamma + gamma + gamma)
`), 42 * 5 * 54 * 4 * 13 * 3);
});

0 comments on commit 3f60e44

Please sign in to comment.