I found with Lua 5.4 a different behavior to Lua 5.3.
Consider this library code:
////////////////////////////////////////////
#include <lua.h>
#include <lauxlib.h>
#include <stdio.h>
#include <stdlib.h>
int add( lua_State* L )
{
luaL_argcheck( L, lua_type( L, 1 ) == LUA_TUSERDATA, 1, "" );
luaL_argcheck( L, lua_type( L, 2 ) == LUA_TUSERDATA, 2, "" );
return 0;
}
extern int luaopen_xxx( lua_State* L )
{
void* ud = lua_newuserdata( L, 20 );
lua_newtable( L );
lua_pushcfunction( L, add );
lua_setfield( L, -2, "__add" );
lua_setmetatable( L, -2 );
luaL_loadstring( L, "return '' + ..." );
lua_pushvalue( L, -2 );
lua_pcall( L, 1, 0, 0 );
printf( "''+ud: %s\n", lua_tostring( L, -1 ) );
lua_pop( L, 1 );
luaL_loadstring( L, "return ... + ''" );
lua_pushvalue( L, -2 );
lua_pcall( L, 1, 0, 0 );
printf( "ud+'': %s\n", lua_tostring( L, -1 ) );
lua_pop( L, 1 );
luaL_loadstring( L, "return 42+..." );
lua_pushvalue( L, -2 );
lua_pcall( L, 1, 0, 0 );
printf( "42+ud: %s\n", lua_tostring( L, -1 ) );
lua_pop( L, 1 );
exit( 1 );
}
////////////////////////////////////////////
Running it with "lua -lxxx" outputs this:
''+ud: bad argument #1 to '?' ()
ud+'': [string "return ... + ''"]:1: bad argument #2 to 'add' ()
42+ud: [string "return 42+..."]:1: bad argument #1 to 'add' ()
Adding a string with a userdata has no information about the failing function.
But adding a userdata with a string has a good error message.
Same with number + userdata.
Different meta-methods like __sub, __mul, ... have the same problem.
- Jörg