konsoletyper / teavm

Compiles Java bytecode to JavaScript, WebAssembly and C
https://teavm.org
Apache License 2.0
2.55k stars 262 forks source link

Improve JavaScript emitter #804

Closed konsoletyper closed 8 months ago

konsoletyper commented 8 months ago

Currently, emission of JavaScript is performed in one pass. This however has some disadvantages:

  1. To calculate the number of usage of methods in minifier mode, we need to keep AST of each method in memory, which is costly.
  2. The frequency calculation itself is non-precise
  3. It's hard to do some advanced stuff like reserving identifiers which are exported from outer scope (so this clumsy `$rt_global is used right now)
  4. It's hard to minify hand-written runtime.

Instead, generation should be performed in several steps. In first step some marked-up output should be generated, where marks point to usages of internally-generated names. Then, real identifiers should be generated for these names. Then, this whole representation can be written to actual input file. AST living time should be only during emission of one method, not during emission of entire JS.