[...]
On the other hand I agree the current behaviour of lua_replace(L,
lua_gettop(L)) is counter-intuitive. IMHO it should either be a no-op
or throw an error.
It should not be a no-op. Functions that result in different stack depths, especially without producing an error, are very annoying.