Refactor the code that handles conversion of JSVals to primitive types.

Probably best read commit-by-commit.