Skip to content

Commit

Permalink
fix name conflict with Lua source
Browse files Browse the repository at this point in the history
  • Loading branch information
starwing committed Jun 2, 2023
1 parent 75672cb commit 987c5e5
Showing 1 changed file with 19 additions and 19 deletions.
38 changes: 19 additions & 19 deletions pb.c
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -237,20 +237,20 @@ static size_t lpb_addlength(lua_State *L, pb_Buffer *b, size_t len, size_t preal
return wlen ? wlen : (size_t)luaL_error(L, "encode bytes fail");
}

static int typeerror(lua_State *L, int idx, const char *type) {
static int lpb_typeerror(lua_State *L, int idx, const char *type) {
lua_pushfstring(L, "%s expected, got %s", type, luaL_typename(L, idx));
return luaL_argerror(L, idx, lua_tostring(L, -1));
}

static lua_Integer posrelat(lua_Integer pos, size_t len) {
static lua_Integer lpb_posrelat(lua_Integer pos, size_t len) {
if (pos >= 0) return pos;
else if (0u - (size_t)pos > len) return 0;
else return (lua_Integer)len + pos + 1;
}

static lua_Integer rangerelat(lua_State *L, int idx, lua_Integer r[2], size_t len) {
r[0] = posrelat(luaL_optinteger(L, idx, 1), len);
r[1] = posrelat(luaL_optinteger(L, idx+1, len), len);
r[0] = lpb_posrelat(luaL_optinteger(L, idx, 1), len);
r[1] = lpb_posrelat(luaL_optinteger(L, idx+1, len), len);
if (r[0] < 1) r[0] = 1;
if (r[1] > (lua_Integer)len) r[1] = len;
return r[0] <= r[1] ? r[1] - r[0] + 1 : 0;
Expand Down Expand Up @@ -286,7 +286,7 @@ static pb_Slice lpb_toslice(lua_State *L, int idx) {

LUALIB_API pb_Slice lpb_checkslice(lua_State *L, int idx) {
pb_Slice ret = lpb_toslice(L, idx);
if (ret.p == NULL) typeerror(L, idx, "string/buffer/slice");
if (ret.p == NULL) lpb_typeerror(L, idx, "string/buffer/slice");
return ret;
}

Expand Down Expand Up @@ -354,7 +354,7 @@ static uint64_t lpb_checkinteger(lua_State *L, int idx) {
if (!isint) {
if (lua_type(L, idx) == LUA_TSTRING)
luaL_error(L, "integer format error: '%s'", lua_tostring(L, idx));
typeerror(L, idx, "number/string");
lpb_typeerror(L, idx, "number/string");
}
return v;
}
Expand Down Expand Up @@ -544,7 +544,7 @@ static void lpb_readtype(lua_State *L, lpb_State *LS, int type, pb_Slice *s) {
# define setmode(a,b) ((void)0)
#endif

static int io_read(lua_State *L) {
static int lpbF_read(lua_State *L) {
FILE *fp = (FILE*)lua_touserdata(L, 1);
size_t nr;
luaL_Buffer b;
Expand All @@ -558,7 +558,7 @@ static int io_read(lua_State *L) {
return 1;
}

static int io_write(lua_State *L, FILE *f, int idx) {
static int lpbF_write(lua_State *L, FILE *f, int idx) {
int nargs = lua_gettop(L) - idx + 1;
int status = 1;
for (; nargs--; idx++) {
Expand All @@ -577,7 +577,7 @@ static int Lio_read(lua_State *L) {
(void)setmode(fileno(stdin), O_BINARY);
else if ((fp = fopen(fname, "rb")) == NULL)
return luaL_fileresult(L, 0, fname);
lua_pushcfunction(L, io_read);
lua_pushcfunction(L, lpbF_read);
lua_pushlightuserdata(L, fp);
ret = lua_pcall(L, 1, 1, 0);
if (fp != stdin) fclose(fp);
Expand All @@ -589,7 +589,7 @@ static int Lio_read(lua_State *L) {
static int Lio_write(lua_State *L) {
int res;
(void)setmode(fileno(stdout), O_BINARY);
res = io_write(L, stdout, 1);
res = lpbF_write(L, stdout, 1);
fflush(stdout);
(void)setmode(fileno(stdout), O_TEXT);
return res;
Expand All @@ -600,7 +600,7 @@ static int Lio_dump(lua_State *L) {
const char *fname = luaL_checkstring(L, 1);
FILE *fp = fopen(fname, "wb");
if (fp == NULL) return luaL_fileresult(L, 0, fname);
res = io_write(L, fp, 2);
res = lpbF_write(L, fp, 2);
fclose(fp);
return res;
}
Expand Down Expand Up @@ -1011,7 +1011,7 @@ static int lpb_unpackloc(lua_State *L, int *pidx, int top, int fmt, pb_Slice *s,
case '*': case '+':
luaL_argcheck(L, *pidx <= top, 1, "format argument exceed");
if (fmt == '*')
li = posrelat(luaL_checkinteger(L, (*pidx)++), len);
li = lpb_posrelat(luaL_checkinteger(L, (*pidx)++), len);
else
li = pb_pos(*s) + luaL_checkinteger(L, (*pidx)++) + 1;
if (li == 0) li = 1;
Expand Down Expand Up @@ -1102,7 +1102,7 @@ static int Lslice_level(lua_State *L) {
lpb_Slice *s = check_lslice(L, 1);
if (!lua_isnoneornil(L, 2)) {
pb_Slice *se;
lua_Integer level = posrelat(luaL_checkinteger(L, 2), s->used);
lua_Integer level = lpb_posrelat(luaL_checkinteger(L, 2), s->used);
if (level > (lua_Integer)s->used)
return 0;
else if (level == (lua_Integer)s->used)
Expand Down Expand Up @@ -1139,7 +1139,7 @@ static int Lslice_enter(lua_State *L) {

static int Lslice_leave(lua_State *L) {
lpb_Slice *s = check_lslice(L, 1);
lua_Integer count = posrelat(luaL_optinteger(L, 2, 1), s->used);
lua_Integer count = lpb_posrelat(luaL_optinteger(L, 2, 1), s->used);
if (count > (lua_Integer)s->used)
argcheck(L, 0, 2, "level (%d) exceed max level %d",
(int)count, (int)s->used);
Expand Down Expand Up @@ -1245,7 +1245,7 @@ static int Lpb_load_unsafe(lua_State *L) {
size_t size = (size_t)luaL_checkinteger(L, 2);
pb_Slice s = pb_lslice(data, size);
int r;
if (data == NULL) typeerror(L, 1, "userdata");
if (data == NULL) lpb_typeerror(L, 1, "userdata");
r = pb_load(&LS->local, &s);
if (r == PB_OK) global_state = &LS->local;
lua_pushboolean(L, r == PB_OK);
Expand Down Expand Up @@ -1501,7 +1501,7 @@ static int Lpb_hook(lua_State *L) {
int type = lua_type(L, 2);
if (t == NULL) luaL_argerror(L, 1, "type not found");
if (type != LUA_TNONE && type != LUA_TNIL && type != LUA_TFUNCTION)
typeerror(L, 2, "function");
lpb_typeerror(L, 2, "function");
lua_settop(L, 2);
lpb_pushdechooktable(L, LS);
lua_rawgetp(L, 3, t);
Expand All @@ -1518,7 +1518,7 @@ static int Lpb_encode_hook(lua_State *L) {
int type = lua_type(L, 2);
if (t == NULL) luaL_argerror(L, 1, "type not found");
if (type != LUA_TNONE && type != LUA_TNIL && type != LUA_TFUNCTION)
typeerror(L, 2, "function");
lpb_typeerror(L, 2, "function");
lua_settop(L, 2);
lpb_pushenchooktable(L, LS);
lua_rawgetp(L, 3, t);
Expand Down Expand Up @@ -2147,14 +2147,14 @@ LUALIB_API int luaopen_pb(lua_State *L) {
static int Lpb_decode_unsafe(lua_State *L) {
const char *data = (const char *)lua_touserdata(L, 2);
size_t size = (size_t)luaL_checkinteger(L, 3);
if (data == NULL) typeerror(L, 2, "userdata");
if (data == NULL) lpb_typeerror(L, 2, "userdata");
return lpbD_decode(L, pb_lslice(data, size), 4);
}

static int Lpb_slice_unsafe(lua_State *L) {
const char *data = (const char *)lua_touserdata(L, 1);
size_t size = (size_t)luaL_checkinteger(L, 2);
if (data == NULL) typeerror(L, 1, "userdata");
if (data == NULL) lpb_typeerror(L, 1, "userdata");
return lpb_newslice(L, data, size);
}

Expand Down

0 comments on commit 987c5e5

Please sign in to comment.