This maintains the type information which can be used for enforce
const-correctness and also for implementing more advanced ways of
handling the type information.
The bounds check was not correct for 1-based indices. I moved the
Lua->C++ index conversion before the bounds check so that it makes
more sense.
Also, atoi isn't a reliable way for converting a string to an integer.