Skip to content

Commit 685be69

Browse files
committed
luajit: get cdata to not conflict with gmod's vector type
1 parent ee1d7b7 commit 685be69

2 files changed

Lines changed: 65 additions & 25 deletions

File tree

LuaJIT-2.1/src/gmod.cpp

Lines changed: 26 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,9 @@
1-
#include "gmod.h"
1+
extern "C"
2+
{
3+
#include "gmod.h"
4+
#include "lua.h"
5+
}
6+
#include "stdio.h"
27

38
namespace std
49
{
@@ -200,18 +205,37 @@ extern "C" void lua_init_stack_gmod(lua_State* L1, lua_State* L)
200205
}
201206
}
202207

203-
extern "C" void GMOD_LuaPrint(const char* str, lua_State* L) // Idk how gmod does it
208+
extern "C" void GMOD_LuaPrint(const char* str, lua_State* L) // Should be how gmod does it
204209
{
210+
if (!L->luabase) // except for this, gmod doesn't do this, but we do making testing jit less of a pain
211+
{
212+
printf(str);
213+
return;
214+
}
215+
205216
((ILuaInterface*)L->luabase)->Msg("%s", str);
206217
}
207218

219+
struct UserData {
220+
void* data;
221+
unsigned char type;
222+
};
223+
208224
extern "C" void GMOD_LuaCreateEmptyUserdata(lua_State* L)
209225
{
210226
//ILuaBase::UserData* udata = (ILuaBase::UserData*)((ILuaBase*)L->luabase)->NewUserdata(sizeof(ILuaBase::UserData)); // Gmod uses CLuaInterface::PushUserType(NULL, 7) Instead
211227
//udata->data = nullptr;
212228
//udata->type = 7; // 7 = Userdata
213229

214230
//return udata;
231+
232+
if (!L->luabase) // just to make testing easier. Gmod doesn't have this.
233+
{
234+
UserData* pData = (UserData*)lua_newuserdata(L, sizeof(ILuaBase::UserData));
235+
pData->data = nullptr;
236+
pData->type = 7;
237+
return;
238+
}
215239

216240

217241
((ILuaBase*)L->luabase)->PushUserType(NULL, 7);

LuaJIT-2.1/src/lj_api.c

Lines changed: 39 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,9 @@
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(75a06,98042110) >> 4*t) & 15u);
242+
int tt = (int)((U64x(75a06,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

245250
LUALIB_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

281297
LUA_API const char *lua_typename(lua_State *L, int t, int stackpos)

0 commit comments

Comments
 (0)