Bug 987560 - Greatly refactor generator implementation. Patch mostly written by Andy Wingo. r=wingo