Skip to content

Commit

Permalink
make luvi compatible with plain Lua 5.2 or 5.3
Browse files Browse the repository at this point in the history
  • Loading branch information
starwing committed Feb 25, 2016
1 parent fa81852 commit 9947423
Show file tree
Hide file tree
Showing 10 changed files with 289 additions and 10 deletions.
15 changes: 12 additions & 3 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -92,9 +92,14 @@ else (WithSharedLibluv)
set(BUILD_MODULE OFF CACHE STRING "Build luv as static library")
include_directories(deps/luv/src)
include_directories(deps/luv/deps/libuv/include)
include_directories(deps/luv/deps/luajit/src)
if (WITH_LUA_ENGINE STREQUAL Lua)
include_directories(deps/luv/deps/lua/src)
set(LUVI_LIBRARIES luv lualib uv)
else()
include_directories(deps/luv/deps/luajit/src)
set(LUVI_LIBRARIES luv luajit-5.1 uv)
endif()
add_subdirectory(deps/luv)
set(LUVI_LIBRARIES luv luajit-5.1 uv)
endif (WithSharedLibluv)


Expand Down Expand Up @@ -127,7 +132,11 @@ if(UNIX)
add_definitions(-Wall)
endif()

luajit_add_executable(luvi
if (WITH_LUA_ENGINE STREQUAL Lua)
add_definitions(-DWITH_PLAIN_LUA)
endif ()

lua_add_executable(luvi
${winsvc}
src/main.c
src/lua/init.lua
Expand Down
192 changes: 192 additions & 0 deletions deps/bit.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,192 @@
/*
** Lua BitOp -- a bit operations library for Lua 5.1/5.2.
** http://bitop.luajit.org/
**
** Copyright (C) 2008-2012 Mike Pall. All rights reserved.
**
** Permission is hereby granted, free of charge, to any person obtaining
** a copy of this software and associated documentation files (the
** "Software"), to deal in the Software without restriction, including
** without limitation the rights to use, copy, modify, merge, publish,
** distribute, sublicense, and/or sell copies of the Software, and to
** permit persons to whom the Software is furnished to do so, subject to
** the following conditions:
**
** The above copyright notice and this permission notice shall be
** included in all copies or substantial portions of the Software.
**
** THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
** EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
** MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
** IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
** CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
** TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
** SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
**
** [ MIT license: http://www.opensource.org/licenses/mit-license.php ]
*/

#define LUA_BITOP_VERSION "1.0.2"

#define LUA_LIB
#include "lua.h"
#include "lauxlib.h"

#ifdef _MSC_VER
/* MSVC is stuck in the last century and doesn't have C99's stdint.h. */
typedef __int32 int32_t;
typedef unsigned __int32 uint32_t;
typedef unsigned __int64 uint64_t;
#else
#include <stdint.h>
#endif

typedef int32_t SBits;
typedef uint32_t UBits;

typedef union {
lua_Number n;
#ifdef LUA_NUMBER_DOUBLE
uint64_t b;
#else
UBits b;
#endif
} BitNum;

/* Convert argument to bit type. */
static UBits barg(lua_State *L, int idx)
{
BitNum bn;
UBits b;
#if LUA_VERSION_NUM < 502
bn.n = lua_tonumber(L, idx);
#elif LUA_VERSION_NUM >= 503
(void)bn;
b = (UBits)luaL_checkinteger(L, idx);
#else
bn.n = luaL_checknumber(L, idx);
#if defined(LUA_NUMBER_DOUBLE)
bn.n += 6755399441055744.0; /* 2^52+2^51 */
#ifdef SWAPPED_DOUBLE
b = (UBits)(bn.b >> 32);
#else
b = (UBits)bn.b;
#endif
#elif defined(LUA_NUMBER_INT) || defined(LUA_NUMBER_LONG) || \
defined(LUA_NUMBER_LONGLONG) || defined(LUA_NUMBER_LONG_LONG) || \
defined(LUA_NUMBER_LLONG)
if (sizeof(UBits) == sizeof(lua_Number))
b = bn.b;
else
b = (UBits)(SBits)bn.n;
#elif defined(LUA_NUMBER_FLOAT)
#error "A 'float' lua_Number type is incompatible with this library"
#else
#error "Unknown number type, check LUA_NUMBER_* in luaconf.h"
#endif
#if LUA_VERSION_NUM < 502
if (b == 0 && !lua_isnumber(L, idx)) {
luaL_typerror(L, idx, "number");
}
#endif
#endif
return b;
}

/* Return bit type. */
#define BRET(b) lua_pushnumber(L, (lua_Number)(SBits)(b)); return 1;

static int bit_tobit(lua_State *L) { BRET(barg(L, 1)) }
static int bit_bnot(lua_State *L) { BRET(~barg(L, 1)) }

#define BIT_OP(func, opr) \
static int func(lua_State *L) { int i; UBits b = barg(L, 1); \
for (i = lua_gettop(L); i > 1; i--) b opr barg(L, i); BRET(b) }
BIT_OP(bit_band, &=)
BIT_OP(bit_bor, |=)
BIT_OP(bit_bxor, ^=)

#define bshl(b, n) (b << n)
#define bshr(b, n) (b >> n)
#define bsar(b, n) ((SBits)b >> n)
#define brol(b, n) ((b << n) | (b >> (32-n)))
#define bror(b, n) ((b << (32-n)) | (b >> n))
#define BIT_SH(func, fn) \
static int func(lua_State *L) { \
UBits b = barg(L, 1); UBits n = barg(L, 2) & 31; BRET(fn(b, n)) }
BIT_SH(bit_lshift, bshl)
BIT_SH(bit_rshift, bshr)
BIT_SH(bit_arshift, bsar)
BIT_SH(bit_rol, brol)
BIT_SH(bit_ror, bror)

static int bit_bswap(lua_State *L)
{
UBits b = barg(L, 1);
b = (b >> 24) | ((b >> 8) & 0xff00) | ((b & 0xff00) << 8) | (b << 24);
BRET(b)
}

static int bit_tohex(lua_State *L)
{
UBits b = barg(L, 1);
SBits n = lua_isnone(L, 2) ? 8 : (SBits)barg(L, 2);
const char *hexdigits = "0123456789abcdef";
char buf[8];
int i;
if (n < 0) { n = -n; hexdigits = "0123456789ABCDEF"; }
if (n > 8) n = 8;
for (i = (int)n; --i >= 0; ) { buf[i] = hexdigits[b & 15]; b >>= 4; }
lua_pushlstring(L, buf, (size_t)n);
return 1;
}

static const struct luaL_Reg bit_funcs[] = {
{ "tobit", bit_tobit },
{ "bnot", bit_bnot },
{ "band", bit_band },
{ "bor", bit_bor },
{ "bxor", bit_bxor },
{ "lshift", bit_lshift },
{ "rshift", bit_rshift },
{ "arshift", bit_arshift },
{ "rol", bit_rol },
{ "ror", bit_ror },
{ "bswap", bit_bswap },
{ "tohex", bit_tohex },
{ NULL, NULL }
};

/* Signed right-shifts are implementation-defined per C89/C99.
** But the de facto standard are arithmetic right-shifts on two's
** complement CPUs. This behaviour is required here, so test for it.
*/
#define BAD_SAR (bsar(-8, 2) != (SBits)-2)

LUALIB_API int luaopen_bit(lua_State *L)
{
UBits b;
lua_pushnumber(L, (lua_Number)1437217655L);
b = barg(L, -1);
if (b != (UBits)1437217655L || BAD_SAR) { /* Perform a simple self-test. */
const char *msg = "compiled with incompatible luaconf.h";
#ifdef LUA_NUMBER_DOUBLE
#ifdef _WIN32
if (b == (UBits)1610612736L)
msg = "use D3DCREATE_FPU_PRESERVE with DirectX";
#endif
if (b == (UBits)1127743488L)
msg = "not compiled with SWAPPED_DOUBLE";
#endif
if (BAD_SAR)
msg = "arithmetic right-shift broken";
luaL_error(L, "bit library self-test failed (%s)", msg);
}
#if LUA_VERSION_NUM < 502
luaL_register(L, "bit", bit_funcs);
#else
luaL_newlib(L, bit_funcs);
#endif
return 1;
}

6 changes: 2 additions & 4 deletions deps/lrexlib.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,14 @@ include_directories(
${LREXLIB_DIR}/src
)

add_definitions(
-DVERSION="2.8.0"
)

add_library(lrexlib
${LREXLIB_DIR}/src/common.c
${LREXLIB_DIR}/src/pcre/lpcre.c
${LREXLIB_DIR}/src/pcre/lpcre_f.c
)

set_target_properties(lrexlib PROPERTIES
COMPILE_FLAGS "-DLUA_LIB -DLUA_COMPAT_APIINTCASTS -DVERSION=\\\"2.8.0\\\"")
target_link_libraries(lrexlib pcre)

set(EXTRA_LIBS ${EXTRA_LIBS} lrexlib)
2 changes: 1 addition & 1 deletion deps/lua-openssl
Submodule lua-openssl updated 64 files
+9 −8 .travis.yml
+9 −2 .travis/platform.sh
+3 −0 .travis/setenv_lua.sh
+48 −31 .travis/setup_lua.sh
+27 −27 appveyor.yml
+2 −2 ldoc/pkcs7.lua
+1 −1 ldoc/pkey.lua
+302 −305 lib/crypto.lua
+88 −82 lib/ssl.lua
+183 −123 src/asn1.c
+0 −1 src/bio.c
+2 −1 src/callback.c
+9 −9 src/cipher.c
+17 −17 src/cms.c
+24 −29 src/crl.c
+61 −37 src/csr.c
+12 −11 src/dh.c
+19 −13 src/digest.c
+12 −11 src/dsa.c
+36 −21 src/ec.c
+3 −9 src/engine.c
+8 −0 src/hmac.c
+0 −3 src/lbn.c
+4 −3 src/lhash.c
+84 −15 src/misc.c
+5 −5 src/ocsp.c
+42 −33 src/openssl.c
+3 −3 src/openssl.h
+29 −37 src/ots.c
+6 −7 src/pkcs12.c
+331 −203 src/pkcs7.c
+65 −74 src/pkey.c
+18 −13 src/private.h
+79 −11 src/rsa.c
+5 −154 src/sk.h
+18 −29 src/ssl.c
+0 −3 src/th-lock.c
+81 −73 src/x509.c
+74 −56 src/xalgor.c
+2 −38 src/xattrs.c
+4 −46 src/xexts.c
+7 −3 src/xname.c
+0 −2 src/xstore.c
+0 −1 test/0.engine.lua
+2 −1 test/0.misc.lua
+20 −7 test/1.asn1.lua
+1 −5 test/1.x509_attr.lua
+2 −6 test/1.x509_extension.lua
+8 −1 test/1.x509_name.lua
+8 −4 test/2.asn1.lua
+1 −1 test/2.digest.lua
+1 −1 test/2.hmac.lua
+3 −3 test/4.pkey.lua
+12 −9 test/5.ts.lua
+7 −4 test/5.x509.lua
+30 −16 test/5.x509_req.lua
+9 −15 test/6.pkcs7.lua
+5 −6 test/7.pkcs12.lua
+2 −1 test/ec.lua
+1 −1 test/luacrypto/pkeytest.lua
+21 −21 test/luacrypto/run-test.lua
+873 −399 test/luaunit.lua
+27 −8 test/memleaks.lua
+2 −0 test/test.lua
2 changes: 1 addition & 1 deletion deps/lua-zlib
Submodule lua-zlib updated 4 files
+5 −3 CMakeLists.txt
+6 −1 lua_zlib.c
+5 −2 rockspec
+7 −4 tap.lua
2 changes: 1 addition & 1 deletion deps/luv
34 changes: 34 additions & 0 deletions src/lenv.c
Original file line number Diff line number Diff line change
Expand Up @@ -229,5 +229,39 @@ static const luaL_Reg lenv_f[] = {

LUALIB_API int luaopen_env(lua_State *L) {
luaL_newlib(L, lenv_f);
#if defined(_WIN32) && !defined(_XBOX_VER)
lua_pushstring(L, "Windows");
#elif defined(__linux__)
lua_pushstring(L, "Linux");
#elif defined(__MACH__) && defined(__APPLE__)
lua_pushstring(L, "OSX");
#elif (defined(__FreeBSD__) || defined(__FreeBSD_kernel__) || \
defined(__NetBSD__) || defined(__OpenBSD__) || \
defined(__DragonFly__)) && !defined(__ORBIS__)
lua_pushstring(L, "BSD");
#elif (defined(__sun__) && defined(__svr4__)) || defined(__CYGWIN__)
lua_pushstring(L, "POSIX");
#else
lua_pushstring(L, "Other");
#endif
lua_setfield(L, -2, "os");

#if defined(__i386) || defined(__i386__) || defined(_M_IX86)
lua_pushstring(L, "x86");
#elif defined(__x86_64__) || defined(__x86_64) || defined(_M_X64) || defined(_M_AMD64)
lua_pushstring(L, "x64");
#elif defined(__arm__) || defined(__arm) || defined(__ARM__) || defined(__ARM)
lua_pushstring(L, "arm");
#elif defined(__aarch64__)
lua_pushstring(L, "arm64");
#elif defined(__ppc__) || defined(__ppc) || defined(__PPC__) || defined(__PPC) || defined(__powerpc__) || defined(__powerpc) || defined(__POWERPC__) || defined(__POWERPC) || defined(_M_PPC)
lua_pushstring(L, "ppc");
#elif defined(__mips__) || defined(__mips) || defined(__MIPS__) || defined(__MIPS)
lua_pushstring(L, "mips");
#else
lua_pushstring(L, "other");
#endif
lua_setfield(L, -2, "arch");

return 1;
}
3 changes: 3 additions & 0 deletions src/lua/luvibundle.lua
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@ local luviPath = require('luvipath')
local pathJoin = luviPath.pathJoin
local getenv = require('os').getenv

loadstring = loadstring or load
unpack = unpack or table.unpack

local tmpBase = luviPath.isWindows and (getenv("TMP") or uv.cwd()) or
(getenv("TMPDIR") or '/tmp')

Expand Down
23 changes: 23 additions & 0 deletions src/luvi.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,10 @@
#ifndef LUVI_H
#define LUVI_H

#ifdef _MSC_VER
#define _CRT_SECURE_NO_WARNINGS
#endif

#include "lua.h"
#include "lualib.h"
#include "lauxlib.h"
Expand Down Expand Up @@ -51,3 +55,22 @@ LUALIB_API int luaopen_zlib(lua_State * const L);
int luaopen_lpeg(lua_State* L);
#endif
#endif

#if (LUA_VERSION_NUM >= 502)
# undef luaL_register
# define luaL_register(L,n,f) \
{ if ((n) == NULL) luaL_setfuncs(L,f,0); else luaL_newlib(L,f); }

# undef luaL_checkint
# define luaL_checkint(L,i) ((int)luaL_checkinteger(L,(i)))
# undef luaL_optint
# define luaL_optint(L,i,d) ((int)luaL_optinteger(L,(i),(d)))

#define lua_setfenv lua_setuservalue

#define lua_objlen lua_rawlen
#define lua_getfenv lua_getuservalue
#define lua_setfenv lua_setuservalue

#endif

20 changes: 20 additions & 0 deletions src/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,9 @@
int luaopen_rex_pcre(lua_State* L);
#endif
#include "../deps/strlib.c"
#ifdef WITH_PLAIN_LUA
#include "../deps/bit.c"
#endif

#if WITH_CUSTOM
int luvi_custom(lua_State* L);
Expand All @@ -39,7 +42,9 @@ static lua_State* vm_acquire(){
luaL_openlibs(L);

// Add string lua 5.3 compat apis, pack,unpack,packsize
#if LUA_VERSION_NUM < 503
make_compat53_string(L);
#endif

// Get package.preload so we can store builtins in it.
lua_getglobal(L, "package");
Expand Down Expand Up @@ -85,6 +90,21 @@ static lua_State* vm_acquire(){
lua_setfield(L, -2, "zlib");
#endif

#ifdef WITH_PLAIN_LUA
{
LUALIB_API int luaopen_init(lua_State *L);
LUALIB_API int luaopen_luvibundle(lua_State *L);
LUALIB_API int luaopen_luvipath(lua_State *L);
lua_pushcfunction(L, luaopen_init);
lua_setfield(L, -2, "init");
lua_pushcfunction(L, luaopen_luvibundle);
lua_setfield(L, -2, "luvibundle");
lua_pushcfunction(L, luaopen_luvipath);
lua_setfield(L, -2, "luvipath");
luaL_requiref(L, "bit", luaopen_bit, 1);
}
#endif

#ifdef WITH_CUSTOM
luvi_custom(L);
#endif
Expand Down

0 comments on commit 9947423

Please sign in to comment.