Skip to content

Commit

Permalink
Add lua_cpcall2 for VA is larger than 47bit
Browse files Browse the repository at this point in the history
A new API is added to work around issue when VA (virtual address)
is > 47 bit in lua_cpcall API:

  LUA_API int   (lua_cpcall2) (lua_State *L, lua_CFunction func, void *ud);

In new API, the "ud" is wrapped in userdata instead of as lightuserdata
directly. So user's code needs modification to use new API. To get real
user data in "ud", user has to do double de-reference to un-wrap
"ud". e.g.:

  int callback(lua_State *L) {
    int *ud = (int *)lua_touserdata(L, -1);
    *ud = 2;
    return 0;
  }

  needs update to:

  int callback(lua_State *L) {
    int **ud = (int **)lua_touserdata(L, -1);
    **ud = 2;
    return 0;
  }

Change-Id: I3241237284cb208336d9503f2e33d2591691049a
Jira: ENTLLT-427
  • Loading branch information
zhongweiy committed May 10, 2017
1 parent 31afda3 commit 3fa648b
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 2 deletions.
24 changes: 22 additions & 2 deletions src/lj_api.c
Expand Up @@ -1144,8 +1144,19 @@ static TValue *cpcall(lua_State *L, lua_CFunction func, void *ud)
return top-1; /* Now call the newly allocated C function. */
}

LUA_API int lua_cpcall(lua_State *L, lua_CFunction func, void *ud)
static TValue *cpcall2(lua_State *L, lua_CFunction func, void *ud)
{
GCfunc *fn = lj_func_newC(L, 0, getcurrenv(L));
fn->c.f = func;
setfuncV(L, L->top++, fn);
if (LJ_FR2) setnilV(L->top++);
void **udp = (void **)lua_newuserdata(L, sizeof(void **));
*udp = ud;
cframe_nres(L->cframe) = 1+0; /* Zero results. */
return L->top-1; /* Now call the newly allocated C function. */
}

static int cpcall_help(lua_State *L, lua_CFunction func, void *ud, lua_CPFunction cpcall) {
global_State *g = G(L);
uint8_t oldh = hook_save(g);
int status;
Expand All @@ -1155,6 +1166,16 @@ LUA_API int lua_cpcall(lua_State *L, lua_CFunction func, void *ud)
return status;
}

LUA_API int lua_cpcall(lua_State *L, lua_CFunction func, void *ud)
{
return cpcall_help(L, func, ud, cpcall);
}

LUA_API int lua_cpcall2(lua_State *L, lua_CFunction func, void *ud)
{
return cpcall_help(L, func, ud, cpcall2);
}

LUALIB_API int luaL_callmeta(lua_State *L, int idx, const char *field)
{
if (luaL_getmetafield(L, idx, field)) {
Expand Down Expand Up @@ -1289,4 +1310,3 @@ LUA_API void lua_setallocf(lua_State *L, lua_Alloc f, void *ud)
g->allocd = ud;
g->allocf = f;
}

1 change: 1 addition & 0 deletions src/lua.h
Expand Up @@ -202,6 +202,7 @@ LUA_API int (lua_setfenv) (lua_State *L, int idx);
LUA_API void (lua_call) (lua_State *L, int nargs, int nresults);
LUA_API int (lua_pcall) (lua_State *L, int nargs, int nresults, int errfunc);
LUA_API int (lua_cpcall) (lua_State *L, lua_CFunction func, void *ud);
LUA_API int (lua_cpcall2) (lua_State *L, lua_CFunction func, void *ud);
LUA_API int (lua_load) (lua_State *L, lua_Reader reader, void *dt,
const char *chunkname);

Expand Down

0 comments on commit 3fa648b

Please sign in to comment.