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

Harder Switch Case Obfuscation #1255

Open
andylovescode opened this issue May 17, 2024 · 2 comments
Open

Harder Switch Case Obfuscation #1255

andylovescode opened this issue May 17, 2024 · 2 comments

Comments

@andylovescode
Copy link

andylovescode commented May 17, 2024

Is your feature request related to a problem? Please describe.
At the moment, control flow flattening creates a lot of switch statements, which use incremental identifiers and are easy to read.

Describe the solution you'd like
Because of the abundance of switch cases, I suggest turning switch cases with no default case, and simple breaks (where it always breaks at the end of a case and never breaks in the middle) into objects which are then indexed and invoked.

Example

switch (x) {
	case 1:
		console.log("case one!");
		break;
	case 2:
		console.log("case two!");
		break;
}

gets turned into

(({
	[1]: () => { console.log("case one!") },
	[2]: () => { console.log("case two!") },
})[x] || (() => {}))() // Or'ed with a do nothing function in case of null

This would become even harder to read when combined the pass that changes object keys.

Other optimizations can be made if all case values are constant integers, example:

([
	null,
	() => { console.log("case one!") }, // Case 1
	() => { console.log("case two!") }, // Case 2
][x] || (() => {}))()

The setting should probably be called something like switchIIFEObjects

Describe alternatives you've considered
None

Additional context
None

@doctor8296
Copy link

doctor8296 commented May 23, 2024

I don't get it. Will not it break the logic of switch?
Switch uses strict equality operator, but in case with object keys, it will works with strings and with numbers.
Also you suggesting to fill whole array with nulls if cases were first 1, and second 10000?

@andylovescode
Copy link
Author

Also you suggesting to fill whole array with nulls if cases were first 1, and second 10000?

There will probably be an exception for arrays with a large number of items.

Switch uses strict equality operator, but in case with object keys, it will works with strings and with numbers.

Oh my goodness I did not think of that, perhaps we could use a Map object? But despite Map being builtin that seems unappealing.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants