Skip to content

Make static optional instead of absent #39

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 12 additions & 5 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,19 +5,26 @@ env:
- PLATFORM=linux
- LUAROCKS_VER=2.2.0
matrix:
- LUA=lua5.1 LUA_SFX=
- LUA=lua5.2 LUA_SFX=
- LUA=luajit LUA_SFX=jit
- LUA=lua5.3 LUA_SFX=
- LUA=lua5.1 LUA_SFX= BUILD=-m32
- LUA=lua5.1 LUA_SFX= BUILD=-m64
- LUA=lua5.2 LUA_SFX= BUILD=-m32
- LUA=lua5.2 LUA_SFX= BUILD=-m64
- LUA=luajit LUA_SFX=jit BUILD=-m32
- LUA=luajit LUA_SFX=jit BUILD=-m64
- LUA=lua5.3 LUA_SFX= BUILD=-m32
- LUA=lua5.3 LUA_SFX= BUILD=-m64

before_install:
- sudo apt-get update
- sudo apt-get install gcc-multilib libreadline-dev:i386 # for 32 bit builds
- bash -x .travis/setup_lua.sh
- sudo pip install cpp-coveralls

install:
- sudo luarocks make rockspec/lua-cmsgpack-scm-1.rockspec CFLAGS="-O2 -fPIC -ftest-coverage -fprofile-arcs" LIBFLAG="-shared --coverage"
- sudo luarocks make rockspec/lua-cmsgpack-scm-1.rockspec CFLAGS="$BUILD -O2 -fPIC -ftest-coverage -fprofile-arcs" LIBFLAG="$BUILD -shared --coverage"

script:
- echo $(file $(which lua$LUA_SFX))
- lua$LUA_SFX test.lua

after_success:
Expand Down
9 changes: 7 additions & 2 deletions .travis/setup_lua.sh
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ if [ "$LUAJIT" == "yes" ]; then
git checkout v2.1;
fi

make && sudo make install
make HOST_CC="gcc $BUILD" CC="gcc $BUILD" && sudo make install

if [ "$LUA" == "luajit2.1" ]; then
sudo ln -s /usr/local/bin/luajit-2.1.0-alpha /usr/local/bin/luajit
Expand All @@ -60,7 +60,12 @@ else
curl http://www.lua.org/work/lua-5.3.0-beta.tar.gz | tar xz
cd lua-5.3.0-beta;
fi
sudo make $PLATFORM install;
if [[ "$PLATFORM" == "linux" ]]; then
(cd src && make all MYCFLAGS="$BUILD -DLUA_USE_LINUX" MYLDFLAGS="$BUILD" MYLIBS="-Wl,-E -ldl -lreadline")
elif [[ "$PLATFORM" == "macosx" ]]; then
(cd src && make all MYCFLAGS="$BUILD -DLUA_USE_MACOSX" MYLDFLAGS="$BUILD" MYLIBS="-lreadline" CC=cc)
fi
sudo make install
fi

cd $TRAVIS_BUILD_DIR;
Expand Down
2 changes: 1 addition & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ endif()

option(Build32Bit "Build 32-bit Library" OFF)

set(CMAKE_C_FLAGS "-O2 -g -ggdb -Wall -pedantic -std=c99")
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -O2 -g -ggdb -Wall -pedantic -std=c99")
add_library(cmsgpack MODULE lua_cmsgpack.c)
set_target_properties(cmsgpack PROPERTIES PREFIX "")

Expand Down
76 changes: 42 additions & 34 deletions lua_cmsgpack.c
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,10 @@
#define lua_pushunsigned(L, n) lua_pushinteger(L, n)
#endif

#ifndef REDIS_STATIC
#define REDIS_STATIC static
#endif

/* =============================================================================
* MessagePack implementation and bindings for Lua 5.1/5.2.
* Copyright(C) 2012 Salvatore Sanfilippo <antirez@gmail.com>
Expand Down Expand Up @@ -66,7 +70,7 @@
/* Reverse memory bytes if arch is little endian. Given the conceptual
* simplicity of the Lua build system we prefer check for endianess at runtime.
* The performance difference should be acceptable. */
void memrevifle(void *ptr, size_t len) {
REDIS_STATIC void memrevifle(void *ptr, size_t len) {
unsigned char *p = (unsigned char *)ptr,
*e = (unsigned char *)p+len-1,
aux;
Expand Down Expand Up @@ -96,7 +100,8 @@ typedef struct mp_buf {
size_t len, free;
} mp_buf;

void *mp_realloc(lua_State *L, void *target, size_t osize,size_t nsize) {
REDIS_STATIC void *mp_realloc(lua_State *L, void *target,
size_t osize, size_t nsize) {
void *(*local_realloc) (void *, void *, size_t osize, size_t nsize) = NULL;
void *ud;

Expand All @@ -105,7 +110,7 @@ void *mp_realloc(lua_State *L, void *target, size_t osize,size_t nsize) {
return local_realloc(ud, target, osize, nsize);
}

mp_buf *mp_buf_new(lua_State *L) {
REDIS_STATIC mp_buf *mp_buf_new(lua_State *L) {
mp_buf *buf = NULL;

/* Old size = 0; new size = sizeof(*buf) */
Expand All @@ -117,7 +122,8 @@ mp_buf *mp_buf_new(lua_State *L) {
return buf;
}

void mp_buf_append(mp_buf *buf, const unsigned char *s, size_t len) {
REDIS_STATIC void mp_buf_append(mp_buf *buf, const unsigned char *s,
size_t len) {
if (buf->free < len) {
size_t newlen = buf->len+len;

Expand All @@ -129,7 +135,7 @@ void mp_buf_append(mp_buf *buf, const unsigned char *s, size_t len) {
buf->free -= len;
}

void mp_buf_free(mp_buf *buf) {
REDIS_STATIC void mp_buf_free(mp_buf *buf) {
mp_realloc(buf->L, buf->b, buf->len, 0); /* realloc to 0 = free */
mp_realloc(buf->L, buf, sizeof(*buf), 0);
}
Expand All @@ -153,7 +159,8 @@ typedef struct mp_cur {
int err;
} mp_cur;

void mp_cur_init(mp_cur *cursor, const unsigned char *s, size_t len) {
REDIS_STATIC void mp_cur_init(mp_cur *cursor, const unsigned char *s,
size_t len) {
cursor->p = s;
cursor->left = len;
cursor->err = MP_CUR_ERROR_NONE;
Expand All @@ -173,7 +180,8 @@ void mp_cur_init(mp_cur *cursor, const unsigned char *s, size_t len) {

/* ------------------------- Low level MP encoding -------------------------- */

void mp_encode_bytes(mp_buf *buf, const unsigned char *s, size_t len) {
REDIS_STATIC void mp_encode_bytes(mp_buf *buf, const unsigned char *s,
size_t len) {
unsigned char hdr[5];
int hdrlen;

Expand All @@ -198,7 +206,7 @@ void mp_encode_bytes(mp_buf *buf, const unsigned char *s, size_t len) {
}

/* we assume IEEE 754 internal format for single and double precision floats. */
void mp_encode_double(mp_buf *buf, double d) {
REDIS_STATIC void mp_encode_double(mp_buf *buf, double d) {
unsigned char b[9];
float f = d;

Expand All @@ -216,7 +224,7 @@ void mp_encode_double(mp_buf *buf, double d) {
}
}

void mp_encode_int(mp_buf *buf, int64_t n) {
REDIS_STATIC void mp_encode_int(mp_buf *buf, int64_t n) {
unsigned char b[9];
int enclen;

Expand Down Expand Up @@ -288,7 +296,7 @@ void mp_encode_int(mp_buf *buf, int64_t n) {
mp_buf_append(buf,b,enclen);
}

void mp_encode_array(mp_buf *buf, int64_t n) {
REDIS_STATIC void mp_encode_array(mp_buf *buf, int64_t n) {
unsigned char b[5];
int enclen;

Expand All @@ -311,7 +319,7 @@ void mp_encode_array(mp_buf *buf, int64_t n) {
mp_buf_append(buf,b,enclen);
}

void mp_encode_map(mp_buf *buf, int64_t n) {
REDIS_STATIC void mp_encode_map(mp_buf *buf, int64_t n) {
unsigned char b[5];
int enclen;

Expand All @@ -336,21 +344,21 @@ void mp_encode_map(mp_buf *buf, int64_t n) {

/* --------------------------- Lua types encoding --------------------------- */

void mp_encode_lua_string(lua_State *L, mp_buf *buf) {
REDIS_STATIC void mp_encode_lua_string(lua_State *L, mp_buf *buf) {
size_t len;
const char *s;

s = lua_tolstring(L,-1,&len);
mp_encode_bytes(buf,(const unsigned char*)s,len);
}

void mp_encode_lua_bool(lua_State *L, mp_buf *buf) {
REDIS_STATIC void mp_encode_lua_bool(lua_State *L, mp_buf *buf) {
unsigned char b = lua_toboolean(L,-1) ? 0xc3 : 0xc2;
mp_buf_append(buf,&b,1);
}

/* Lua 5.3 has a built in 64-bit integer type */
void mp_encode_lua_integer(lua_State *L, mp_buf *buf) {
REDIS_STATIC void mp_encode_lua_integer(lua_State *L, mp_buf *buf) {
#if (LUA_VERSION_NUM < 503) && BITS_32
lua_Number i = lua_tonumber(L,-1);
#else
Expand All @@ -362,7 +370,7 @@ void mp_encode_lua_integer(lua_State *L, mp_buf *buf) {
/* Lua 5.2 and lower only has 64-bit doubles, so we need to
* detect if the double may be representable as an int
* for Lua < 5.3 */
void mp_encode_lua_number(lua_State *L, mp_buf *buf) {
REDIS_STATIC void mp_encode_lua_number(lua_State *L, mp_buf *buf) {
lua_Number n = lua_tonumber(L,-1);

if (IS_INT64_EQUIVALENT(n)) {
Expand All @@ -372,10 +380,10 @@ void mp_encode_lua_number(lua_State *L, mp_buf *buf) {
}
}

void mp_encode_lua_type(lua_State *L, mp_buf *buf, int level);
REDIS_STATIC void mp_encode_lua_type(lua_State *L, mp_buf *buf, int level);

/* Convert a lua table into a message pack list. */
void mp_encode_lua_table_as_array(lua_State *L, mp_buf *buf, int level) {
REDIS_STATIC void mp_encode_lua_table_as_array(lua_State *L, mp_buf *buf, int level) {
#if LUA_VERSION_NUM < 502
size_t len = lua_objlen(L,-1), j;
#else
Expand All @@ -391,7 +399,7 @@ void mp_encode_lua_table_as_array(lua_State *L, mp_buf *buf, int level) {
}

/* Convert a lua table into a message pack key-value map. */
void mp_encode_lua_table_as_map(lua_State *L, mp_buf *buf, int level) {
REDIS_STATIC void mp_encode_lua_table_as_map(lua_State *L, mp_buf *buf, int level) {
size_t len = 0;

/* First step: count keys into table. No other way to do it with the
Expand All @@ -418,7 +426,7 @@ void mp_encode_lua_table_as_map(lua_State *L, mp_buf *buf, int level) {
/* Returns true if the Lua table on top of the stack is exclusively composed
* of keys from numerical keys from 1 up to N, with N being the total number
* of elements, without any hole in the middle. */
int table_is_an_array(lua_State *L) {
REDIS_STATIC int table_is_an_array(lua_State *L) {
int count = 0, max = 0;
#if LUA_VERSION_NUM < 503
lua_Number n;
Expand Down Expand Up @@ -461,22 +469,22 @@ int table_is_an_array(lua_State *L) {
/* If the length operator returns non-zero, that is, there is at least
* an object at key '1', we serialize to message pack list. Otherwise
* we use a map. */
void mp_encode_lua_table(lua_State *L, mp_buf *buf, int level) {
REDIS_STATIC void mp_encode_lua_table(lua_State *L, mp_buf *buf, int level) {
if (table_is_an_array(L))
mp_encode_lua_table_as_array(L,buf,level);
else
mp_encode_lua_table_as_map(L,buf,level);
}

void mp_encode_lua_null(lua_State *L, mp_buf *buf) {
REDIS_STATIC void mp_encode_lua_null(lua_State *L, mp_buf *buf) {
unsigned char b[1];
(void)L;

b[0] = 0xc0;
mp_buf_append(buf,b,1);
}

void mp_encode_lua_type(lua_State *L, mp_buf *buf, int level) {
REDIS_STATIC void mp_encode_lua_type(lua_State *L, mp_buf *buf, int level) {
int t = lua_type(L,-1);

/* Limit the encoding of nested tables to a specified maximum depth, so that
Expand Down Expand Up @@ -506,7 +514,7 @@ void mp_encode_lua_type(lua_State *L, mp_buf *buf, int level) {
* Packs all arguments as a stream for multiple upacking later.
* Returns error if no arguments provided.
*/
int mp_pack(lua_State *L) {
REDIS_STATIC int mp_pack(lua_State *L) {
int nargs = lua_gettop(L);
int i;
mp_buf *buf;
Expand Down Expand Up @@ -539,9 +547,9 @@ int mp_pack(lua_State *L) {

/* ------------------------------- Decoding --------------------------------- */

void mp_decode_to_lua_type(lua_State *L, mp_cur *c);
REDIS_STATIC void mp_decode_to_lua_type(lua_State *L, mp_cur *c);

void mp_decode_to_lua_array(lua_State *L, mp_cur *c, size_t len) {
REDIS_STATIC void mp_decode_to_lua_array(lua_State *L, mp_cur *c, size_t len) {
assert(len <= UINT_MAX);
int index = 1;

Expand All @@ -554,7 +562,7 @@ void mp_decode_to_lua_array(lua_State *L, mp_cur *c, size_t len) {
}
}

void mp_decode_to_lua_hash(lua_State *L, mp_cur *c, size_t len) {
REDIS_STATIC void mp_decode_to_lua_hash(lua_State *L, mp_cur *c, size_t len) {
assert(len <= UINT_MAX);
lua_newtable(L);
while(len--) {
Expand All @@ -568,7 +576,7 @@ void mp_decode_to_lua_hash(lua_State *L, mp_cur *c, size_t len) {

/* Decode a Message Pack raw object pointed by the string cursor 'c' to
* a Lua type, that is left as the only result on the stack. */
void mp_decode_to_lua_type(lua_State *L, mp_cur *c) {
REDIS_STATIC void mp_decode_to_lua_type(lua_State *L, mp_cur *c) {
mp_cur_need(c,1);

/* If we return more than 18 elements, we must resize the stack to
Expand Down Expand Up @@ -773,7 +781,7 @@ void mp_decode_to_lua_type(lua_State *L, mp_cur *c) {
}
}

int mp_unpack_full(lua_State *L, int limit, int offset) {
REDIS_STATIC int mp_unpack_full(lua_State *L, int limit, int offset) {
size_t len;
const char *s;
mp_cur c;
Expand Down Expand Up @@ -826,18 +834,18 @@ int mp_unpack_full(lua_State *L, int limit, int offset) {
return cnt;
}

int mp_unpack(lua_State *L) {
REDIS_STATIC int mp_unpack(lua_State *L) {
return mp_unpack_full(L, 0, 0);
}

int mp_unpack_one(lua_State *L) {
REDIS_STATIC int mp_unpack_one(lua_State *L) {
int offset = luaL_optinteger(L, 2, 0);
/* Variable pop because offset may not exist */
lua_pop(L, lua_gettop(L)-1);
return mp_unpack_full(L, 1, offset);
}

int mp_unpack_limit(lua_State *L) {
REDIS_STATIC int mp_unpack_limit(lua_State *L) {
int limit = luaL_checkinteger(L, 2);
int offset = luaL_optinteger(L, 3, 0);
/* Variable pop because offset may not exist */
Expand All @@ -846,7 +854,7 @@ int mp_unpack_limit(lua_State *L) {
return mp_unpack_full(L, limit, offset);
}

int mp_safe(lua_State *L) {
REDIS_STATIC int mp_safe(lua_State *L) {
int argc, err, total_results;

argc = lua_gettop(L);
Expand All @@ -869,15 +877,15 @@ int mp_safe(lua_State *L) {
}

/* -------------------------------------------------------------------------- */
const struct luaL_Reg cmds[] = {
static const struct luaL_Reg cmds[] = {
{"pack", mp_pack},
{"unpack", mp_unpack},
{"unpack_one", mp_unpack_one},
{"unpack_limit", mp_unpack_limit},
{0}
};

int luaopen_create(lua_State *L) {
REDIS_STATIC int luaopen_create(lua_State *L) {
int i;
/* Manually construct our module table instead of
* relying on _register or _newlib */
Expand Down