Skip to content

Commit e8880b6

Browse files
committed
Merge pull request #1 from mattsta/add-streaming-and-refactor-and-tests
Merge from mattsta's PR collection
2 parents f44f954 + 6078bbd commit e8880b6

File tree

9 files changed

+898
-114
lines changed

9 files changed

+898
-114
lines changed

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,3 +9,5 @@
99

1010
*.o
1111
*.so
12+
13+
build/

.travis.yml

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
language: erlang
2+
3+
env:
4+
global:
5+
- PLATFORM=linux
6+
- LUAROCKS_VER=2.1.0
7+
matrix:
8+
- LUA=lua5.1 LUA_SFX=
9+
- LUA=lua5.2 LUA_SFX=
10+
- LUA=luajit LUA_SFX=jit
11+
- LUA=lua5.3 LUA_SFX=
12+
13+
before_install:
14+
- bash .travis/setup_lua.sh
15+
- sudo pip install cpp-coveralls
16+
17+
install:
18+
- sudo luarocks make rockspec/lua-cmsgpack-scm-1.rockspec CFLAGS="-O2 -fPIC -ftest-coverage -fprofile-arcs" LIBFLAG="-shared --coverage"
19+
20+
script:
21+
- lua$LUA_SFX test.lua
22+
23+
after_success:
24+
- coveralls
25+
26+
notifications:
27+
email:
28+
on_success: change
29+
on_failure: always

.travis/setup_lua.sh

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
# A script for setting up environment for travis-ci testing.
2+
# Sets up Lua and Luarocks.
3+
# LUA must be "lua5.1", "lua5.2" or "luajit".
4+
# PLATFORM must be "linux" or "macosx".
5+
6+
if [ "$LUA" == "luajit" ]; then
7+
curl http://luajit.org/download/LuaJIT-2.0.2.tar.gz | tar xz
8+
cd LuaJIT-2.0.2
9+
make && sudo make install
10+
cd $TRAVIS_BUILD_DIR;
11+
else
12+
if [ "$LUA" == "lua5.1" ]; then
13+
curl http://www.lua.org/ftp/lua-5.1.5.tar.gz | tar xz
14+
cd lua-5.1.5;
15+
elif [ "$LUA" == "lua5.2" ]; then
16+
curl http://www.lua.org/ftp/lua-5.2.3.tar.gz | tar xz
17+
cd lua-5.2.3;
18+
elif [ "$LUA" == "lua5.3" ]; then
19+
curl http://www.lua.org/work/lua-5.3.0-work2.tar.gz | tar xz
20+
cd lua-5.3.0-work2;
21+
fi
22+
sudo make $PLATFORM install
23+
cd $TRAVIS_BUILD_DIR;
24+
fi
25+
26+
LUAROCKS_BASE=luarocks-$LUAROCKS_VER
27+
curl http://luarocks.org/releases/$LUAROCKS_BASE.tar.gz | tar xz
28+
cd $LUAROCKS_BASE;
29+
30+
if [ "$LUA" == "luajit" ]; then
31+
./configure --lua-suffix=jit --with-lua-include=/usr/local/include/luajit-2.0;
32+
else
33+
./configure;
34+
fi
35+
36+
make && sudo make install
37+
38+
cd $TRAVIS_BUILD_DIR
39+
40+
rm -rf $LUAROCKS_BASE
41+
42+
if [ "$LUA" == "luajit" ]; then
43+
rm -rf LuaJIT-2.0.2;
44+
elif [ "$LUA" == "lua5.1" ]; then
45+
rm -rf lua-5.1.5;
46+
elif [ "$LUA" == "lua5.2" ]; then
47+
rm -rf lua-5.2.3;
48+
elif [ "$LUA" == "lua5.3" ]; then
49+
rm -rf lua-5.3.0-work2;
50+
fi

CMakeLists.txt

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
# If Lua is installed in a non-standard location, please set the LUA_DIR
2+
# environment variable to point to prefix for the install. Eg:
3+
# Unix: export LUA_DIR=/home/user/pkg
4+
# Windows: set LUA_DIR=c:\lua51
5+
6+
project(lua-cmsgpack C)
7+
cmake_minimum_required(VERSION 2.6)
8+
9+
if(NOT CMAKE_BUILD_TYPE)
10+
set(CMAKE_BUILD_TYPE Release CACHE STRING
11+
"Choose the type of build, options are: None Debug Release RelWithDebInfo MinSizeRel."
12+
FORCE)
13+
endif()
14+
15+
find_package(Lua51 REQUIRED)
16+
include_directories(${LUA_INCLUDE_DIR})
17+
18+
if(APPLE)
19+
set(CMAKE_SHARED_MODULE_CREATE_C_FLAGS
20+
"${CMAKE_SHARED_MODULE_CREATE_C_FLAGS} -undefined dynamic_lookup")
21+
endif()
22+
23+
if(WIN32)
24+
# Win32 modules need to be linked to the Lua library.
25+
set(_MODULE_LINK ${LUA_LIBRARY} ${_MODULE_LINK})
26+
set(_lua_module_dir "${_lua_lib_dir}")
27+
else()
28+
set(_lua_module_dir "${_lua_lib_dir}/lua/5.1")
29+
endif()
30+
31+
set(CMAKE_C_FLAGS "-O2 -g -ggdb -Wall -pedantic -std=c99")
32+
add_library(cmsgpack MODULE lua_cmsgpack.c)
33+
set_target_properties(cmsgpack PROPERTIES PREFIX "")
34+
target_link_libraries(cmsgpack ${_MODULE_LINK})
35+
install(TARGETS cmsgpack DESTINATION "${_lua_module_dir}")
36+
37+
# vi:ai et sw=4 ts=4:

README.md

Lines changed: 37 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ README for lua-cmsgpack.c
22
===
33

44
Lua-cmsgpack is a [MessagePack](http://msgpack.org) implementation and bindings for
5-
Lua 5.1/5.2 in a self contained C file without external dependencies.
5+
Lua 5.1/5.2/5.3 in a self contained C file without external dependencies.
66

77
This library is open source software licensed under the BSD two-clause license.
88

@@ -33,10 +33,33 @@ interpreter:
3333
USAGE
3434
---
3535

36-
The exported API is very simple, consisting in two functions:
36+
The exported API is very simple, consisting of four functions:
3737

38-
msgpack = cmsgpack.pack(lua_object)
39-
lua_object = cmsgpack.unpack(msgpack)
38+
Basic API:
39+
40+
msgpack = cmsgpack.pack(lua_object1, lua_object2, ..., lua_objectN)
41+
lua_object1, lua_object2, ..., lua_objectN = cmsgpack.unpack(msgpack)
42+
43+
Detailed API giving you more control over unpacking multiple values:
44+
45+
resume_offset, lua_object1 = cmsgpack.unpack_one(msgpack)
46+
resume_offset1, lua_object2 = cmsgpack.unpack_one(msgpack, resume_offset)
47+
...
48+
-1, lua_objectN = cmsgpack.unpack_one(msgpack, resume_offset_previous)
49+
50+
resume_offset, lua_object1, lua_object2 = cmsgpack.unpack_limit(msgpack, 2)
51+
resume_offset2, lua_object3 = cmsgpack.unpack_limit(msgpack, 1, resume_offset1)
52+
53+
Functions:
54+
55+
- `pack(arg1, arg2, ..., argn)` - pack any number of lua objects into one msgpack stream. returns: msgpack
56+
- `unpack(msgpack)` - unpack all objects in msgpack to individual return values. returns: object1, object2, ..., objectN
57+
- `unpack_one(msgpack); unpack_one(msgpack, offset)` - unpacks the first object after offset. returns: offset, object
58+
- `unpack_limit(msgpack, limit); unpack_limit(msgpack, limit, offset)` - unpacks the first `limit` objects and returns: offset, object1, objet2, ..., objectN (up to limit, but may return fewer than limit if not that many objects remain to be unpacked)
59+
60+
When you reach the end of your input stream with `unpack_one` or `unpack_limit`, an offset of `-1` is returned.
61+
62+
You may `require "msgpack"` or you may `require "msgpack.safe"`. The safe version returns errors as (nil, errstring).
4063

4164
However because of the nature of Lua numerical and table type a few behavior
4265
of the library must be well understood to avoid problems:
@@ -50,6 +73,16 @@ maps.
5073
* When a Lua number is converted to float or double, the former is preferred if there is no loss of precision compared to the double representation.
5174
* When a MessagePack big integer (64 bit) is converted to a Lua number it is possible that the resulting number will not represent the original number but just an approximation. This is unavoidable because the Lua numerical type is usually a double precision floating point type.
5275

76+
TESTING
77+
---
78+
79+
Build and test:
80+
81+
mkdir build; cd build
82+
cmake ..
83+
make
84+
lua ../test.lua
85+
5386
NESTED TABLES
5487
---
5588
Nested tables are handled correctly up to `LUACMSGPACK_MAX_NESTING` levels of

0 commit comments

Comments
 (0)