Port ubjson.lua fixes to ubjson.js

See the previous commit for details.