add api lua_sharestring

This commit is contained in:
Cloud Wu
2019-06-13 20:01:10 +08:00
parent b45384cc9f
commit 24b333a5e9
3 changed files with 15 additions and 10 deletions

View File

@@ -1049,6 +1049,18 @@ LUA_API void lua_sharefunction (lua_State *L, int index) {
luaF_shareproto(f->p); luaF_shareproto(f->p);
} }
LUA_API void lua_sharestring (lua_State *L, int index) {
const char *str = lua_tostring(L, index);
if (str == NULL)
luaG_runerror(L, "need a string to share");
TString *ts = (TString *)(str - sizeof(UTString));
if(ts->tt == LUA_TLNGSTR)
makeshared(ts);
else
luaS_fix(G(L), ts);
}
LUA_API int lua_dump (lua_State *L, lua_Writer writer, void *data, int strip) { LUA_API int lua_dump (lua_State *L, lua_Writer writer, void *data, int strip) {
int status; int status;
TValue *o; TValue *o;

View File

@@ -235,6 +235,7 @@ LUA_API void (lua_pushlightuserdata) (lua_State *L, void *p);
LUA_API int (lua_pushthread) (lua_State *L); LUA_API int (lua_pushthread) (lua_State *L);
LUA_API void (lua_clonefunction) (lua_State *L, const void * fp); LUA_API void (lua_clonefunction) (lua_State *L, const void * fp);
LUA_API void (lua_sharefunction) (lua_State *L, int index); LUA_API void (lua_sharefunction) (lua_State *L, int index);
LUA_API void (lua_sharestring) (lua_State *L, int index);
/* /*
** get functions (Lua -> stack) ** get functions (Lua -> stack)

View File

@@ -6,8 +6,6 @@
#include "lstring.h" #include "lstring.h"
#include "lobject.h" #include "lobject.h"
#include "ltable.h"
#include "lstate.h"
#include "lapi.h" #include "lapi.h"
#include "lgc.h" #include "lgc.h"
@@ -44,15 +42,9 @@ mark_shared(lua_State *L) {
if (!lua_iscfunction(L, idx) || lua_getupvalue(L, idx, 1) != NULL) if (!lua_iscfunction(L, idx) || lua_getupvalue(L, idx, 1) != NULL)
luaL_error(L, "Invalid function"); luaL_error(L, "Invalid function");
break; break;
case LUA_TSTRING: { case LUA_TSTRING:
const char *str = lua_tostring(L, idx); lua_sharestring(L, idx);
TString *ts = (TString *)(str - sizeof(UTString));
if(ts->tt == LUA_TLNGSTR)
makeshared(ts);
else
luaS_fix(G(L), ts);
break; break;
}
default: default:
luaL_error(L, "Invalid type [%s]", lua_typename(L, t)); luaL_error(L, "Invalid type [%s]", lua_typename(L, t));
break; break;