diff options
author | Mike Pall <mike> | 2020-09-30 01:31:27 +0200 |
---|---|---|
committer | Mike Pall <mike> | 2020-09-30 01:34:49 +0200 |
commit | e9af1abec542e6f9851ff2368e7f196b6382a44c (patch) | |
tree | 4b6d76732347a402abb43c6efa5ec3e01a4d61f0 /src/lj_api.c | |
parent | e67e2040be693122b54fc83797cdc9eb07221aea (diff) | |
download | luajit-e9af1abec542e6f9851ff2368e7f196b6382a44c.tar.gz luajit-e9af1abec542e6f9851ff2368e7f196b6382a44c.tar.bz2 luajit-e9af1abec542e6f9851ff2368e7f196b6382a44c.zip |
Add support for full-range 64 bit lightuserdata.
Diffstat (limited to 'src/lj_api.c')
-rw-r--r-- | src/lj_api.c | 40 |
1 files changed, 36 insertions, 4 deletions
diff --git a/src/lj_api.c b/src/lj_api.c index f53ecc35..ecaf8a2b 100644 --- a/src/lj_api.c +++ b/src/lj_api.c | |||
@@ -608,7 +608,7 @@ LUA_API void *lua_touserdata(lua_State *L, int idx) | |||
608 | if (tvisudata(o)) | 608 | if (tvisudata(o)) |
609 | return uddata(udataV(o)); | 609 | return uddata(udataV(o)); |
610 | else if (tvislightud(o)) | 610 | else if (tvislightud(o)) |
611 | return lightudV(o); | 611 | return lightudV(G(L), o); |
612 | else | 612 | else |
613 | return NULL; | 613 | return NULL; |
614 | } | 614 | } |
@@ -621,7 +621,7 @@ LUA_API lua_State *lua_tothread(lua_State *L, int idx) | |||
621 | 621 | ||
622 | LUA_API const void *lua_topointer(lua_State *L, int idx) | 622 | LUA_API const void *lua_topointer(lua_State *L, int idx) |
623 | { | 623 | { |
624 | return lj_obj_ptr(index2adr(L, idx)); | 624 | return lj_obj_ptr(G(L), index2adr(L, idx)); |
625 | } | 625 | } |
626 | 626 | ||
627 | /* -- Stack setters (object creation) ------------------------------------- */ | 627 | /* -- Stack setters (object creation) ------------------------------------- */ |
@@ -707,9 +707,38 @@ LUA_API void lua_pushboolean(lua_State *L, int b) | |||
707 | incr_top(L); | 707 | incr_top(L); |
708 | } | 708 | } |
709 | 709 | ||
710 | #if LJ_64 | ||
711 | static void *lightud_intern(lua_State *L, void *p) | ||
712 | { | ||
713 | global_State *g = G(L); | ||
714 | uint64_t u = (uint64_t)p; | ||
715 | uint32_t up = lightudup(u); | ||
716 | uint32_t *segmap = mref(g->gc.lightudseg, uint32_t); | ||
717 | MSize segnum = g->gc.lightudnum; | ||
718 | if (segmap) { | ||
719 | MSize seg; | ||
720 | for (seg = 0; seg <= segnum; seg++) | ||
721 | if (segmap[seg] == up) /* Fast path. */ | ||
722 | return (void *)(((uint64_t)seg << LJ_LIGHTUD_BITS_LO) | lightudlo(u)); | ||
723 | segnum++; | ||
724 | } | ||
725 | if (!((segnum-1) & segnum) && segnum != 1) { | ||
726 | if (segnum >= (1 << LJ_LIGHTUD_BITS_SEG)) lj_err_msg(L, LJ_ERR_BADLU); | ||
727 | lj_mem_reallocvec(L, segmap, segnum, segnum ? 2*segnum : 2u, uint32_t); | ||
728 | setmref(g->gc.lightudseg, segmap); | ||
729 | } | ||
730 | g->gc.lightudnum = segnum; | ||
731 | segmap[segnum] = up; | ||
732 | return (void *)(((uint64_t)segnum << LJ_LIGHTUD_BITS_LO) | lightudlo(u)); | ||
733 | } | ||
734 | #endif | ||
735 | |||
710 | LUA_API void lua_pushlightuserdata(lua_State *L, void *p) | 736 | LUA_API void lua_pushlightuserdata(lua_State *L, void *p) |
711 | { | 737 | { |
712 | setlightudV(L->top, checklightudptr(L, p)); | 738 | #if LJ_64 |
739 | p = lightud_intern(L, p); | ||
740 | #endif | ||
741 | setrawlightudV(L->top, p); | ||
713 | incr_top(L); | 742 | incr_top(L); |
714 | } | 743 | } |
715 | 744 | ||
@@ -1149,7 +1178,10 @@ static TValue *cpcall(lua_State *L, lua_CFunction func, void *ud) | |||
1149 | fn->c.f = func; | 1178 | fn->c.f = func; |
1150 | setfuncV(L, top++, fn); | 1179 | setfuncV(L, top++, fn); |
1151 | if (LJ_FR2) setnilV(top++); | 1180 | if (LJ_FR2) setnilV(top++); |
1152 | setlightudV(top++, checklightudptr(L, ud)); | 1181 | #if LJ_64 |
1182 | ud = lightud_intern(L, ud); | ||
1183 | #endif | ||
1184 | setrawlightudV(top++, ud); | ||
1153 | cframe_nres(L->cframe) = 1+0; /* Zero results. */ | 1185 | cframe_nres(L->cframe) = 1+0; /* Zero results. */ |
1154 | L->top = top; | 1186 | L->top = top; |
1155 | return top-1; /* Now call the newly allocated C function. */ | 1187 | return top-1; /* Now call the newly allocated C function. */ |