Skip to content

Commit

Permalink
#236 Add api to create user data
Browse files Browse the repository at this point in the history
  • Loading branch information
dibyendumajumdar committed Oct 14, 2021
1 parent e9b3ec8 commit d71a897
Show file tree
Hide file tree
Showing 5 changed files with 27 additions and 0 deletions.
2 changes: 2 additions & 0 deletions include/lauxlib.h
Original file line number Diff line number Diff line change
Expand Up @@ -296,6 +296,8 @@ LUALIB_API void *(luaL_checkudata)(lua_State *L, int ud, const char *tname);

LUALIB_API void *ravi_alloc_f(void *msp, void *ptr, size_t osize, size_t nsize);

LUALIB_API int (raviL_newuserdata)(lua_State *L, size_t size, const char *tname);

#endif


3 changes: 3 additions & 0 deletions include/lua.h
Original file line number Diff line number Diff line change
Expand Up @@ -642,6 +642,9 @@ LUA_API int ravi_list_code(lua_State *L);
/* Returns a table with various system limits */
LUA_API int ravi_get_limits(lua_State *L);

LUA_API int ravi_newuserdata (lua_State *L, size_t size);


/* Options */
LUA_API const char *raviV_options(struct lua_State *L);

Expand Down
6 changes: 6 additions & 0 deletions src/lapi.c
Original file line number Diff line number Diff line change
Expand Up @@ -1733,3 +1733,9 @@ LUA_API void ravi_writestringerror(lua_State *L, const char *fmt, const char *p)
raviE_default_writestringerror(fmt, p);
}
}

LUA_API int ravi_newuserdata (lua_State *L, size_t size) {
char *p = cast(char *, lua_newuserdata(L, size));
while (size--) *p++ = '\0';
return 1;
}
8 changes: 8 additions & 0 deletions src/lauxlib.c
Original file line number Diff line number Diff line change
Expand Up @@ -1081,3 +1081,11 @@ LUALIB_API void luaL_checkversion_ (lua_State *L, lua_Number ver, size_t sz) {
luaL_error(L, "version mismatch: app. needs %f, Lua core provides %f",
(LUAI_UACNUMBER)ver, (LUAI_UACNUMBER)*v);
}

LUALIB_API int raviL_newuserdata(lua_State *L, size_t size, const char *tname) {
ravi_newuserdata(L, size);
luaL_newmetatable(L, tname);
lua_setmetatable(L, -2);
return 1;
}

8 changes: 8 additions & 0 deletions src/ravi_jit.c
Original file line number Diff line number Diff line change
Expand Up @@ -250,6 +250,13 @@ static int ravi_get_options(lua_State *L) {
return 1;
}

static int ravi_createnewuserdata(lua_State *L) {
luaL_argcheck(L, lua_isstring(L, 1), 1, "Type name (string) expected as first argument");
luaL_argcheck(L, lua_isinteger(L, 2), 2, "Size (integer) expected as second argument");
raviL_newuserdata(L, (size_t)lua_tointeger(L, 2) , lua_tostring(L, 1));
return 1;
}

static const luaL_Reg ravilib[] = {{"iscompiled", ravi_is_compiled},
{"compile", ravi_compile_n},
{"dumplua", ravi_dump_luacode},
Expand All @@ -268,6 +275,7 @@ static const luaL_Reg ravilib[] = {{"iscompiled", ravi_is_compiled},
{"limits", ravi_get_limits},
{"jitname", ravi_get_jit_id},
{"options", ravi_get_options},
{"userdata", ravi_createnewuserdata},
{NULL, NULL}};

#include <math.h>
Expand Down

0 comments on commit d71a897

Please sign in to comment.