Finite-state machine for JavaScript