Skip to content
Siorki edited this page Oct 9, 2018 · 6 revisions

Preprocessing

Preprocessing encompasses the first series of operations performed on the input code. It has the following goals :

  • reduce the code size while maintaining its executable status
  • prepare it for the packer to optimize its efficiency

It produces Javascript code that can be executed as-is without being unpacked, and is semantically equivalent to the input.

It is composed of several modules run in sequence. The execution flow is however not entirely linear, as the next-to-last module may produce multiple outputs, creating as many branches for the following steps. Variable renaming and packing are performed separately on each branch, then only the best result is kept and shown to the user.

Contents - modules

Modules are run in the following order. Each one can be enabled or disabled through the settings.

[Define environment](Module - define environment)

Removes the occurrences of Math within the code, to replace them with an encaspulation of the final evaluation with(Math). Incurs a large performance penalty.

Reorganizes the code to combine initialization and main loop into a single block, that will be executed with setInterval() instead of eval(). Incurs a slight performance penalty.

Shortens methods' and properties' names inside 2D, GL and Audio contexts. Also replaces GL constants with their numerical values. Produces multiple outputs, with all combinations of contexts found in the code.

Optimizes the choice of string delimiters, both inside the code and for the final packed string, in order to minimize the need for escaping and thus save bytes.

Rename some one-letter variables to free extra tokens for the packer. Does not immediately shorten the code, but may improve the compression by increasing the dictionary size.

Usage

The output of this stage is shown as "Preprocessed" in RegPack GUI. When several branches are created, only the one with the smallest packed size is shown.

Implementation

The class ShapeShifter is in charge of all preprocessing operations. The main entry point is ShapeShifter.preprocess().