2626#include "lj_strscan.h"
2727#include "lj_strfmt.h"
2828
29+ #include "lj_ctype.h"
30+ #include "lj_cconv.h"
31+
2932/* -- Common helper functions --------------------------------------------- */
3033
3134#define lj_checkapi_slot (idx ) \
@@ -230,16 +233,18 @@ LUA_API int lua_type(lua_State *L, int idx)
230233#endif
231234 } else if (o == niltv (L )) {
232235 return LUA_TNONE ;
233- } else { /* Magic internal/external tag conversion. ORDER LJ_T */
234- uint32_t t = ~itype (o );
236+ } else if (tviscdata (o )) {
237+ return LUA_TUSERDATA ; // we would return 10 which conflicts with gmod!
238+ } /* Magic internal/external tag conversion. ORDER LJ_T */
239+
240+ uint32_t t = ~itype (o );
235241#if LJ_64
236- int tt = (int )((U64x (75 a06 ,98042110 ) >> 4 * t ) & 15u );
242+ int tt = (int )((U64x (75 a06 ,98042110 ) >> 4 * t ) & 15u );
237243#else
238- int tt = (int )(((t < 8 ? 0x98042110u : 0x75a06u ) >> 4 * (t & 7 )) & 15u );
244+ int tt = (int )(((t < 8 ? 0x98042110u : 0x75a06u ) >> 4 * (t & 7 )) & 15u );
239245#endif
240- lj_assertL (tt != LUA_TNIL || tvisnil (o ), "bad tag conversion" );
241- return tt ;
242- }
246+ lj_assertL (tt != LUA_TNIL || tvisnil (o ), "bad tag conversion" );
247+ return tt ;
243248}
244249
245250LUALIB_API void luaL_checktype (lua_State * L , int idx , int tt )
@@ -257,25 +262,36 @@ LUALIB_API void luaL_checkany(lua_State *L, int idx)
257262// Based off https://github.com/meepen/gluajit/blob/master/src/lj_api.c#L225-L247
258263/*extern "C"*/ const char * GMODLUA_GetUserType (lua_State * L , int iStackPos )
259264{
260- static char strName [128 ];
261- const char * strTypeName = "UserData" ;
262- if (lua_getmetatable (L , iStackPos ))
263- {
264- lua_pushstring (L , "MetaName" );
265- lua_gettable (L , -2 );
266-
267- if (lua_isstring (L , -1 ))
268- {
269- strncpy (strName , lua_tostring (L , -1 ), sizeof (strName ));
270- strTypeName = strName ;
271- }
265+ static char strName [128 ];
266+ const char * strTypeName = "UserData" ;
267+ cTValue * o = index2adr (L , iStackPos );
268+ GCtab * mt = NULL ;
269+ if (tvistab (o ))
270+ mt = tabref (tabV (o )-> metatable );
271+ else if (tvisudata (o ))
272+ mt = tabref (udataV (o )-> metatable );
273+ else if (tviscdata (o ))
274+ {
275+ CTState * cts = ctype_cts (L );
276+ CType * ct = ctype_raw (cts , cdataV (o )-> ctypeid );
277+ mt = tabV (lj_tab_getinth (cts -> miscmap , - (int32_t )ctype_typeid (cts , ct )));
278+ } else
279+ mt = tabref (basemt_obj (G (L ), o ));
272280
273- lua_pop (L , 1 );
274- }
281+ if (mt )
282+ {
283+ GCstr * str = lj_str_newz (L , "MetaName" );
284+ cTValue * val = lj_tab_getstr (mt , str );
285+ lj_str_free (G (L ), str );
275286
276- lua_pop (L , 1 );
287+ if (val && tvisstr (val ))
288+ {
289+ strncpy (strName , strdata (strV (val )), sizeof (strName ));
290+ strTypeName = strName ;
291+ }
292+ }
277293
278- return strTypeName ;
294+ return strTypeName ;
279295}
280296
281297LUA_API const char * lua_typename (lua_State * L , int t , int stackpos )
0 commit comments