!UPSTREAM: WIP: Rework builtin_variables.cpp

TODO:
- Test
- TODO comments
- Split out GS specific stuff to its own commit