Preparation to use the generator based interface.
Also removes many globals. (but doesn't reduce their scope by much)
Refs: #4964
Use `globalThis` instead of `global`. They refere to the same object but `globalThtis` is defined by JSguaranteed to work (in all embeddings).