Skip to content

Commit 318ee30

Browse files
committed
add load/save proto for multi states use
1 parent 7602195 commit 318ee30

File tree

2 files changed

+38
-3
lines changed

2 files changed

+38
-3
lines changed

Diff for: Makefile

+1-1
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ LUA_STATICLIB := 3rd/lua/liblua.a
1313
LUA_LIB ?= $(LUA_STATICLIB)
1414
LUA_INC ?= 3rd/lua
1515

16-
$(LUA_STATICLIB) :
16+
$(LUA_STATICLIB) :
1717
cd 3rd/lua && $(MAKE) CC='$(CC) -std=gnu99' $(PLAT)
1818

1919
# jemalloc

Diff for: lualib-src/sproto/lsproto.c

+37-2
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
#include <string.h>
2+
#include <stdlib.h>
23
#include "msvcint.h"
34

45
#include "lua.h"
@@ -41,8 +42,15 @@ LUALIB_API void luaL_setfuncs (lua_State *L, const luaL_Reg *l, int nup) {
4142
static int
4243
lnewproto(lua_State *L) {
4344
size_t sz = 0;
44-
void * buffer = (void *)luaL_checklstring(L,1,&sz);
45-
struct sproto * sp = sproto_create(buffer, sz);
45+
void * buffer;
46+
struct sproto * sp;
47+
if (lua_isuserdata(L,1)) {
48+
buffer = lua_touserdata(L,1);
49+
sz = luaL_checkinteger(L,2);
50+
} else {
51+
buffer = (void *)luaL_checklstring(L,1,&sz);
52+
}
53+
sp = sproto_create(buffer, sz);
4654
if (sp) {
4755
lua_pushlightuserdata(L, sp);
4856
return 1;
@@ -449,6 +457,31 @@ lprotocol(lua_State *L) {
449457
return 3;
450458
}
451459

460+
/* global sproto pointer for multi states */
461+
static void * G_sproto = NULL;
462+
static size_t G_sproto_sz = 0;
463+
464+
static int
465+
lsaveproto(lua_State *L) {
466+
size_t sz;
467+
void * buffer = (void *)luaL_checklstring(L,1,&sz);
468+
void * tmp = malloc(sz);
469+
memcpy(tmp, buffer, sz);
470+
if (G_sproto) {
471+
free(G_sproto);
472+
}
473+
G_sproto = tmp;
474+
G_sproto_sz = sz;
475+
return 0;
476+
}
477+
478+
static int
479+
lloadproto(lua_State *L) {
480+
lua_pushlightuserdata(L, G_sproto);
481+
lua_pushinteger(L, G_sproto_sz);
482+
return 2;
483+
}
484+
452485
int
453486
luaopen_sproto_core(lua_State *L) {
454487
#ifdef luaL_checkversion
@@ -461,6 +494,8 @@ luaopen_sproto_core(lua_State *L) {
461494
{ "querytype", lquerytype },
462495
{ "decode", ldecode },
463496
{ "protocol", lprotocol },
497+
{ "loadproto", lloadproto },
498+
{ "saveproto", lsaveproto },
464499
{ NULL, NULL },
465500
};
466501
luaL_newlib(L,l);

0 commit comments

Comments
 (0)