Skip to content

Commit

Permalink
From Lua 5.2: Add luaL_testudata().
Browse files Browse the repository at this point in the history
Contributed by François Perrad.
  • Loading branch information
Mike Pall committed Apr 7, 2017
1 parent f2e2a3f commit cde968f
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 3 deletions.
1 change: 1 addition & 0 deletions src/lauxlib.h
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,7 @@ LUALIB_API void luaL_traceback (lua_State *L, lua_State *L1, const char *msg,
LUALIB_API void (luaL_setfuncs) (lua_State *L, const luaL_Reg *l, int nup);
LUALIB_API void (luaL_pushmodule) (lua_State *L, const char *modname,
int sizehint);
LUALIB_API void *(luaL_testudata) (lua_State *L, int ud, const char *tname);


/*
Expand Down
12 changes: 9 additions & 3 deletions src/lj_api.c
Original file line number Diff line number Diff line change
Expand Up @@ -875,7 +875,7 @@ LUA_API void lua_upvaluejoin(lua_State *L, int idx1, int n1, int idx2, int n2)
lj_gc_objbarrier(L, fn1, gcref(fn1->l.uvptr[n1]));
}

LUALIB_API void *luaL_checkudata(lua_State *L, int idx, const char *tname)
LUALIB_API void *luaL_testudata(lua_State *L, int idx, const char *tname)
{
cTValue *o = index2adr(L, idx);
if (tvisudata(o)) {
Expand All @@ -884,8 +884,14 @@ LUALIB_API void *luaL_checkudata(lua_State *L, int idx, const char *tname)
if (tv && tvistab(tv) && tabV(tv) == tabref(ud->metatable))
return uddata(ud);
}
lj_err_argtype(L, idx, tname);
return NULL; /* unreachable */
return NULL; /* value is not a userdata with a metatable */
}

LUALIB_API void *luaL_checkudata(lua_State *L, int idx, const char *tname)
{
void *p = luaL_testudata(L, idx, tname);
if (!p) lj_err_argtype(L, idx, tname);
return p;
}

/* -- Object setters ------------------------------------------------------ */
Expand Down

0 comments on commit cde968f

Please sign in to comment.