Skip to content

Commit

Permalink
skynet modified version
Browse files Browse the repository at this point in the history
  • Loading branch information
cloudwu committed Jun 27, 2018
1 parent d3d4e29 commit c990dcb
Show file tree
Hide file tree
Showing 26 changed files with 860 additions and 167 deletions.
4 changes: 2 additions & 2 deletions src/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
PLAT= none

CC= gcc -std=gnu99
CFLAGS= -O2 -Wall -Wextra -DLUA_COMPAT_5_2 $(SYSCFLAGS) $(MYCFLAGS)
CFLAGS= -O2 -Wall -Wextra $(SYSCFLAGS) $(MYCFLAGS)
LDFLAGS= $(SYSLDFLAGS) $(MYLDFLAGS)
LIBS= -lm $(SYSLIBS) $(MYLIBS)

Expand All @@ -19,7 +19,7 @@ SYSCFLAGS=
SYSLDFLAGS=
SYSLIBS=

MYCFLAGS=
MYCFLAGS=-I../threads
MYLDFLAGS=
MYLIBS=
MYOBJS=
Expand Down
6 changes: 6 additions & 0 deletions src/README
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
This is a modify version of lua 5.3.2 (http://www.lua.org/ftp/lua-5.3.2.tar.gz) .

For detail ,
Shared Proto : http://lua-users.org/lists/lua-l/2014-03/msg00489.html
Shared short string table : http://blog.codingnow.com/2015/08/lua_vm_share_string.html
Signal for debug use : http://blog.codingnow.com/2015/03/skynet_signal.html
57 changes: 55 additions & 2 deletions src/lapi.c
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
#include "ltm.h"
#include "lundump.h"
#include "lvm.h"
#include "lfunc.h"



Expand Down Expand Up @@ -1012,6 +1013,58 @@ LUA_API int lua_load (lua_State *L, lua_Reader reader, void *data,
return status;
}

static Proto * cloneproto (lua_State *L, const Proto *src) {
/* copy constants and nested proto */
int i,n;
Proto *f = luaF_newproto(L, src->sp);
n = src->sp->sizek;
f->k=luaM_newvector(L,n,TValue);
for (i=0; i<n; i++) setnilvalue(&f->k[i]);
for (i=0; i<n; i++) {
const TValue *s=&src->k[i];
TValue *o=&f->k[i];
if (ttisstring(s)) {
TString * str = luaS_clonestring(L,tsvalue(s));
setsvalue2n(L,o,str);
} else {
setobj(L,o,s);
}
}
n = src->sp->sizep;
f->p=luaM_newvector(L,n,struct Proto *);
for (i=0; i<n; i++) f->p[i]=NULL;
for (i=0; i<n; i++) {
f->p[i]=cloneproto(L, src->p[i]);
}
return f;
}

LUA_API void lua_clonefunction (lua_State *L, const void * fp) {
LClosure *cl;
LClosure *f = cast(LClosure *, fp);
lua_lock(L);
if (f->p->sp->l_G == G(L)) {
setclLvalue(L,L->top,f);
api_incr_top(L);
lua_unlock(L);
return;
}
cl = luaF_newLclosure(L,f->nupvalues);
cl->p = cloneproto(L, f->p);
setclLvalue(L,L->top,cl);
api_incr_top(L);
luaF_initupvals(L, cl);

if (cl->nupvalues >= 1) { /* does it have an upvalue? */
/* get global table from registry */
Table *reg = hvalue(&G(L)->l_registry);
const TValue *gt = luaH_getint(reg, LUA_RIDX_GLOBALS);
/* set global table as 1st upvalue of 'f' (may be LUA_ENV) */
setobj(L, cl->upvals[0]->v, gt);
luaC_upvalbarrier(L, cl->upvals[0]);
}
lua_unlock(L);
}

LUA_API int lua_dump (lua_State *L, lua_Writer writer, void *data, int strip) {
int status;
Expand Down Expand Up @@ -1207,7 +1260,7 @@ static const char *aux_upvalue (StkId fi, int n, TValue **val,
case LUA_TLCL: { /* Lua closure */
LClosure *f = clLvalue(fi);
TString *name;
Proto *p = f->p;
SharedProto *p = f->p->sp;
if (!(1 <= n && n <= p->sizeupvalues)) return NULL;
*val = f->upvals[n-1]->v;
if (uv) *uv = f->upvals[n - 1];
Expand Down Expand Up @@ -1259,7 +1312,7 @@ static UpVal **getupvalref (lua_State *L, int fidx, int n, LClosure **pf) {
StkId fi = index2addr(L, fidx);
api_check(L, ttisLclosure(fi), "Lua function expected");
f = clLvalue(fi);
api_check(L, (1 <= n && n <= f->p->sizeupvalues), "invalid upvalue index");
api_check(L, (1 <= n && n <= f->p->sp->sizeupvalues), "invalid upvalue index");
if (pf) *pf = f;
return &f->upvals[n - 1]; /* get its upvalue pointer */
}
Expand Down
171 changes: 170 additions & 1 deletion src/lauxlib.c
Original file line number Diff line number Diff line change
Expand Up @@ -700,7 +700,7 @@ static int skipcomment (LoadF *lf, int *cp) {
}


LUALIB_API int luaL_loadfilex (lua_State *L, const char *filename,
static int luaL_loadfilex_ (lua_State *L, const char *filename,
const char *mode) {
LoadF lf;
int status, readstatus;
Expand Down Expand Up @@ -1041,3 +1041,172 @@ LUALIB_API void luaL_checkversion_ (lua_State *L, lua_Number ver, size_t sz) {
(LUAI_UACNUMBER)ver, (LUAI_UACNUMBER)*v);
}

// use clonefunction

#include "spinlock.h"

struct codecache {
struct spinlock lock;
lua_State *L;
};

static struct codecache CC;

static void
clearcache() {
if (CC.L == NULL)
return;
SPIN_LOCK(&CC)
lua_close(CC.L);
CC.L = luaL_newstate();
SPIN_UNLOCK(&CC)
}

static void
init() {
SPIN_INIT(&CC);
CC.L = luaL_newstate();
}

static const void *
load(const char *key) {
if (CC.L == NULL)
return NULL;
SPIN_LOCK(&CC)
lua_State *L = CC.L;
lua_pushstring(L, key);
lua_rawget(L, LUA_REGISTRYINDEX);
const void * result = lua_touserdata(L, -1);
lua_pop(L, 1);
SPIN_UNLOCK(&CC)

return result;
}

static const void *
save(const char *key, const void * proto) {
lua_State *L;
const void * result = NULL;

SPIN_LOCK(&CC)
if (CC.L == NULL) {
init();
L = CC.L;
} else {
L = CC.L;
lua_pushstring(L, key);
lua_pushvalue(L, -1);
lua_rawget(L, LUA_REGISTRYINDEX);
result = lua_touserdata(L, -1); /* stack: key oldvalue */
if (result == NULL) {
lua_pop(L,1);
lua_pushlightuserdata(L, (void *)proto);
lua_rawset(L, LUA_REGISTRYINDEX);
} else {
lua_pop(L,2);
}
}
SPIN_UNLOCK(&CC)
return result;
}

#define CACHE_OFF 0
#define CACHE_EXIST 1
#define CACHE_ON 2

static int cache_key = 0;

static int cache_level(lua_State *L) {
int t = lua_rawgetp(L, LUA_REGISTRYINDEX, &cache_key);
int r = lua_tointeger(L, -1);
lua_pop(L,1);
if (t == LUA_TNUMBER) {
return r;
}
return CACHE_ON;
}

static int cache_mode(lua_State *L) {
static const char * lst[] = {
"OFF",
"EXIST",
"ON",
NULL,
};
if (lua_isnoneornil(L,1)) {
int t = lua_rawgetp(L, LUA_REGISTRYINDEX, &cache_key);
int r = lua_tointeger(L, -1);
if (t == LUA_TNUMBER) {
if (r < 0 || r >= CACHE_ON) {
r = CACHE_ON;
}
} else {
r = CACHE_ON;
}
lua_pushstring(L, lst[r]);
return 1;
}
int t = luaL_checkoption(L, 1, "OFF" , lst);
lua_pushinteger(L, t);
lua_rawsetp(L, LUA_REGISTRYINDEX, &cache_key);
return 0;
}

LUALIB_API int luaL_loadfilex (lua_State *L, const char *filename,
const char *mode) {
int level = cache_level(L);
if (level == CACHE_OFF) {
return luaL_loadfilex_(L, filename, mode);
}
const void * proto = load(filename);
if (proto) {
lua_clonefunction(L, proto);
return LUA_OK;
}
if (level == CACHE_EXIST) {
return luaL_loadfilex_(L, filename, mode);
}
lua_State * eL = luaL_newstate();
if (eL == NULL) {
lua_pushliteral(L, "New state failed");
return LUA_ERRMEM;
}
int err = luaL_loadfilex_(eL, filename, mode);
if (err != LUA_OK) {
size_t sz = 0;
const char * msg = lua_tolstring(eL, -1, &sz);
lua_pushlstring(L, msg, sz);
lua_close(eL);
return err;
}
proto = lua_topointer(eL, -1);
const void * oldv = save(filename, proto);
if (oldv) {
lua_close(eL);
lua_clonefunction(L, oldv);
} else {
lua_clonefunction(L, proto);
/* Never close it. notice: memory leak */
}

return LUA_OK;
}

static int
cache_clear(lua_State *L) {
(void)(L);
clearcache();
return 0;
}

LUAMOD_API int luaopen_cache(lua_State *L) {
luaL_Reg l[] = {
{ "clear", cache_clear },
{ "mode", cache_mode },
{ NULL, NULL },
};
luaL_newlib(L,l);
lua_getglobal(L, "loadfile");
lua_setfield(L, -2, "loadfile");
return 1;
}
Loading

0 comments on commit c990dcb

Please sign in to comment.