Skip to content

Commit 0985b33

Browse files
committed
modify lua
1 parent 58aa755 commit 0985b33

21 files changed

+395
-166
lines changed

3rd/lua/README

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
This is a modify version of lua 5.2.3 (http://www.lua.org/ftp/lua-5.2.3.tar.gz) .
2+
3+
For detail : http://lua-users.org/lists/lua-l/2014-03/msg00489.html

3rd/lua/lapi.c

+58-1
Original file line numberDiff line numberDiff line change
@@ -993,6 +993,63 @@ LUA_API int lua_load (lua_State *L, lua_Reader reader, void *data,
993993
return status;
994994
}
995995

996+
static Proto *
997+
cloneproto (lua_State *L, const Proto *src) {
998+
/* copy constants and nested proto */
999+
int i,n;
1000+
Proto *f = luaF_newproto (L, src->sp);
1001+
n = src->sp->sizek;
1002+
f->k=luaM_newvector(L,n,TValue);
1003+
for (i=0; i<n; i++) setnilvalue(&f->k[i]);
1004+
for (i=0; i<n; i++) {
1005+
const TValue *s=&src->k[i];
1006+
TValue *o=&f->k[i];
1007+
if (ttisstring(s)) {
1008+
TString * str = luaS_newlstr(L,svalue(s),tsvalue(s)->len);
1009+
setsvalue2n(L,o,str);
1010+
} else {
1011+
setobj(L,o,s);
1012+
}
1013+
}
1014+
n = src->sp->sizep;
1015+
f->p=luaM_newvector(L,n,struct Proto *);
1016+
for (i=0; i<n; i++) f->p[i]=NULL;
1017+
for (i=0; i<n; i++) {
1018+
f->p[i]=cloneproto(L, src->p[i]);
1019+
}
1020+
return f;
1021+
}
1022+
1023+
LUA_API void lua_clonefunction (lua_State *L, const void * fp) {
1024+
int i;
1025+
Closure *cl;
1026+
const LClosure *f = cast(const LClosure *, fp);
1027+
lua_lock(L);
1028+
if (f->p->sp->l_G == G(L)) {
1029+
setclLvalue(L,L->top,f);
1030+
api_incr_top(L);
1031+
lua_unlock(L);
1032+
return;
1033+
}
1034+
cl = luaF_newLclosure(L,f->nupvalues);
1035+
cl->l.p = cloneproto(L, f->p);
1036+
setclLvalue(L,L->top,cl);
1037+
api_incr_top(L);
1038+
for (i = 0; i < cl->l.nupvalues; i++) { /* initialize upvalues */
1039+
UpVal *up = luaF_newupval(L);
1040+
cl->l.upvals[i] = up;
1041+
luaC_objbarrier(L, cl, up);
1042+
}
1043+
if (f->nupvalues == 1) { /* does it have one upvalue? */
1044+
/* get global table from registry */
1045+
Table *reg = hvalue(&G(L)->l_registry);
1046+
const TValue *gt = luaH_getint(reg, LUA_RIDX_GLOBALS);
1047+
/* set global table as 1st upvalue of 'cl' (may be LUA_ENV) */
1048+
setobj(L, cl->l.upvals[0]->v, gt);
1049+
luaC_barrier(L, cl->l.upvals[0], gt);
1050+
}
1051+
lua_unlock(L);
1052+
}
9961053

9971054
LUA_API int lua_dump (lua_State *L, lua_Writer writer, void *data) {
9981055
int status;
@@ -1198,7 +1255,7 @@ static const char *aux_upvalue (StkId fi, int n, TValue **val,
11981255
case LUA_TLCL: { /* Lua closure */
11991256
LClosure *f = clLvalue(fi);
12001257
TString *name;
1201-
Proto *p = f->p;
1258+
SharedProto *p = f->p->sp;
12021259
if (!(1 <= n && n <= p->sizeupvalues)) return NULL;
12031260
*val = f->upvals[n-1]->v;
12041261
if (owner) *owner = obj2gco(f->upvals[n - 1]);

3rd/lua/lauxlib.c

+119-1
Original file line numberDiff line numberDiff line change
@@ -627,7 +627,7 @@ static int skipcomment (LoadF *lf, int *cp) {
627627
}
628628

629629

630-
LUALIB_API int luaL_loadfilex (lua_State *L, const char *filename,
630+
static int luaL_loadfilex_ (lua_State *L, const char *filename,
631631
const char *mode) {
632632
LoadF lf;
633633
int status, readstatus;
@@ -957,3 +957,121 @@ LUALIB_API void luaL_checkversion_ (lua_State *L, lua_Number ver) {
957957
lua_pop(L, 1);
958958
}
959959

960+
// use clonefunction
961+
962+
#define LOCK(q) while (__sync_lock_test_and_set(&(q)->lock,1)) {}
963+
#define UNLOCK(q) __sync_lock_release(&(q)->lock);
964+
965+
struct codecache {
966+
int lock;
967+
lua_State *L;
968+
};
969+
970+
static struct codecache CC = { 0 , NULL };
971+
972+
static void
973+
clearcache() {
974+
if (CC.L == NULL)
975+
return;
976+
LOCK(&CC)
977+
lua_close(CC.L);
978+
CC.L = luaL_newstate();
979+
UNLOCK(&CC)
980+
}
981+
982+
static void
983+
init() {
984+
CC.lock = 0;
985+
CC.L = luaL_newstate();
986+
}
987+
988+
static const void *
989+
load(const char *key) {
990+
if (CC.L == NULL)
991+
return NULL;
992+
LOCK(&CC)
993+
lua_State *L = CC.L;
994+
lua_pushstring(L, key);
995+
lua_rawget(L, LUA_REGISTRYINDEX);
996+
const void * result = lua_touserdata(L, -1);
997+
lua_pop(L, 1);
998+
UNLOCK(&CC)
999+
1000+
return result;
1001+
}
1002+
1003+
static const void *
1004+
save(const char *key, const void * proto) {
1005+
lua_State *L;
1006+
const void * result = NULL;
1007+
1008+
LOCK(&CC)
1009+
if (CC.L == NULL) {
1010+
init();
1011+
L = CC.L;
1012+
} else {
1013+
L = CC.L;
1014+
lua_pushstring(L, key);
1015+
lua_pushvalue(L, -1);
1016+
lua_rawget(L, LUA_REGISTRYINDEX);
1017+
result = lua_touserdata(L, -1); /* stack: key oldvalue */
1018+
if (result == NULL) {
1019+
lua_pop(L,1);
1020+
lua_pushlightuserdata(L, (void *)proto);
1021+
lua_rawset(L, LUA_REGISTRYINDEX);
1022+
} else {
1023+
lua_pop(L,2);
1024+
}
1025+
}
1026+
UNLOCK(&CC)
1027+
return result;
1028+
}
1029+
1030+
LUALIB_API int luaL_loadfilex (lua_State *L, const char *filename,
1031+
const char *mode) {
1032+
const void * proto = load(filename);
1033+
if (proto) {
1034+
lua_clonefunction(L, proto);
1035+
return LUA_OK;
1036+
}
1037+
lua_State * eL = luaL_newstate();
1038+
if (eL == NULL) {
1039+
lua_pushliteral(L, "New state failed");
1040+
return LUA_ERRMEM;
1041+
}
1042+
int err = luaL_loadfilex_(eL, filename, mode);
1043+
if (err != LUA_OK) {
1044+
size_t sz = 0;
1045+
const char * msg = lua_tolstring(eL, -1, &sz);
1046+
lua_pushlstring(L, msg, sz);
1047+
lua_close(eL);
1048+
return err;
1049+
}
1050+
proto = lua_topointer(eL, -1);
1051+
const void * oldv = save(filename, proto);
1052+
if (oldv) {
1053+
lua_close(eL);
1054+
lua_clonefunction(L, oldv);
1055+
} else {
1056+
lua_clonefunction(L, proto);
1057+
/* Never close it. notice: memory leak */
1058+
}
1059+
return LUA_OK;
1060+
}
1061+
1062+
static int
1063+
cache_clear(lua_State *L) {
1064+
clearcache();
1065+
return 0;
1066+
}
1067+
1068+
LUAMOD_API int luaopen_cache(lua_State *L) {
1069+
luaL_Reg l[] = {
1070+
{ "clear", cache_clear },
1071+
{ NULL, NULL },
1072+
};
1073+
luaL_newlib(L,l);
1074+
lua_getglobal(L, "loadfile");
1075+
lua_setfield(L, -2, "loadfile");
1076+
return 1;
1077+
}

3rd/lua/lcode.c

+15-15
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ void luaK_nil (FuncState *fs, int from, int n) {
3838
Instruction *previous;
3939
int l = from + n - 1; /* last register to set nil */
4040
if (fs->pc > fs->lasttarget) { /* no jumps to current position? */
41-
previous = &fs->f->code[fs->pc-1];
41+
previous = &fs->f->sp->code[fs->pc-1];
4242
if (GET_OPCODE(*previous) == OP_LOADNIL) {
4343
int pfrom = GETARG_A(*previous);
4444
int pl = pfrom + GETARG_B(*previous);
@@ -78,7 +78,7 @@ static int condjump (FuncState *fs, OpCode op, int A, int B, int C) {
7878

7979

8080
static void fixjump (FuncState *fs, int pc, int dest) {
81-
Instruction *jmp = &fs->f->code[pc];
81+
Instruction *jmp = &fs->f->sp->code[pc];
8282
int offset = dest-(pc+1);
8383
lua_assert(dest != NO_JUMP);
8484
if (abs(offset) > MAXARG_sBx)
@@ -98,7 +98,7 @@ int luaK_getlabel (FuncState *fs) {
9898

9999

100100
static int getjump (FuncState *fs, int pc) {
101-
int offset = GETARG_sBx(fs->f->code[pc]);
101+
int offset = GETARG_sBx(fs->f->sp->code[pc]);
102102
if (offset == NO_JUMP) /* point to itself represents end of list */
103103
return NO_JUMP; /* end of list */
104104
else
@@ -107,7 +107,7 @@ static int getjump (FuncState *fs, int pc) {
107107

108108

109109
static Instruction *getjumpcontrol (FuncState *fs, int pc) {
110-
Instruction *pi = &fs->f->code[pc];
110+
Instruction *pi = &fs->f->sp->code[pc];
111111
if (pc >= 1 && testTMode(GET_OPCODE(*(pi-1))))
112112
return pi-1;
113113
else
@@ -180,10 +180,10 @@ LUAI_FUNC void luaK_patchclose (FuncState *fs, int list, int level) {
180180
level++; /* argument is +1 to reserve 0 as non-op */
181181
while (list != NO_JUMP) {
182182
int next = getjump(fs, list);
183-
lua_assert(GET_OPCODE(fs->f->code[list]) == OP_JMP &&
184-
(GETARG_A(fs->f->code[list]) == 0 ||
185-
GETARG_A(fs->f->code[list]) >= level));
186-
SETARG_A(fs->f->code[list], level);
183+
lua_assert(GET_OPCODE(fs->f->sp->code[list]) == OP_JMP &&
184+
(GETARG_A(fs->f->sp->code[list]) == 0 ||
185+
GETARG_A(fs->f->sp->code[list]) >= level));
186+
SETARG_A(fs->f->sp->code[list], level);
187187
list = next;
188188
}
189189
}
@@ -210,7 +210,7 @@ void luaK_concat (FuncState *fs, int *l1, int l2) {
210210

211211

212212
static int luaK_code (FuncState *fs, Instruction i) {
213-
Proto *f = fs->f;
213+
SharedProto *f = fs->f->sp;
214214
dischargejpc(fs); /* `pc' will change */
215215
/* put new instruction in code array */
216216
luaM_growvector(fs->ls->L, f->code, fs->pc, f->sizecode, Instruction,
@@ -260,10 +260,10 @@ int luaK_codek (FuncState *fs, int reg, int k) {
260260

261261
void luaK_checkstack (FuncState *fs, int n) {
262262
int newstack = fs->freereg + n;
263-
if (newstack > fs->f->maxstacksize) {
263+
if (newstack > fs->f->sp->maxstacksize) {
264264
if (newstack >= MAXSTACK)
265265
luaX_syntaxerror(fs->ls, "function or expression too complex");
266-
fs->f->maxstacksize = cast_byte(newstack);
266+
fs->f->sp->maxstacksize = cast_byte(newstack);
267267
}
268268
}
269269

@@ -302,13 +302,13 @@ static int addk (FuncState *fs, TValue *key, TValue *v) {
302302
go through and create a new entry for this value */
303303
}
304304
/* constant not found; create a new entry */
305-
oldsize = f->sizek;
305+
oldsize = f->sp->sizek;
306306
k = fs->nk;
307307
/* numerical value does not need GC barrier;
308308
table has no metatable, so it does not need to invalidate cache */
309309
setnvalue(idx, cast_num(k));
310-
luaM_growvector(L, f->k, k, f->sizek, TValue, MAXARG_Ax, "constants");
311-
while (oldsize < f->sizek) setnilvalue(&f->k[oldsize++]);
310+
luaM_growvector(L, f->k, k, f->sp->sizek, TValue, MAXARG_Ax, "constants");
311+
while (oldsize < f->sp->sizek) setnilvalue(&f->k[oldsize++]);
312312
setobj(L, &f->k[k], v);
313313
fs->nk++;
314314
luaC_barrier(L, f, v);
@@ -860,7 +860,7 @@ void luaK_posfix (FuncState *fs, BinOpr op,
860860

861861

862862
void luaK_fixline (FuncState *fs, int line) {
863-
fs->f->lineinfo[fs->pc - 1] = line;
863+
fs->f->sp->lineinfo[fs->pc - 1] = line;
864864
}
865865

866866

3rd/lua/lcode.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ typedef enum BinOpr {
3636
typedef enum UnOpr { OPR_MINUS, OPR_NOT, OPR_LEN, OPR_NOUNOPR } UnOpr;
3737

3838

39-
#define getcode(fs,e) ((fs)->f->code[(e)->u.info])
39+
#define getcode(fs,e) ((fs)->f->sp->code[(e)->u.info])
4040

4141
#define luaK_codeAsBx(fs,o,A,sBx) luaK_codeABx(fs,o,A,(sBx)+MAXARG_sBx)
4242

0 commit comments

Comments
 (0)