summaryrefslogtreecommitdiff
path: root/examples/redis-unstable/deps/lua/src
diff options
context:
space:
mode:
authorMitja Felicijan <mitja.felicijan@gmail.com>2026-01-21 22:52:54 +0100
committerMitja Felicijan <mitja.felicijan@gmail.com>2026-01-21 22:52:54 +0100
commitdcacc00e3750300617ba6e16eb346713f91a783a (patch)
tree38e2d4fb5ed9d119711d4295c6eda4b014af73fd /examples/redis-unstable/deps/lua/src
parent58dac10aeb8f5a041c46bddbeaf4c7966a99b998 (diff)
downloadcrep-dcacc00e3750300617ba6e16eb346713f91a783a.tar.gz
Remove testing data
Diffstat (limited to 'examples/redis-unstable/deps/lua/src')
-rw-r--r--examples/redis-unstable/deps/lua/src/Makefile183
-rw-r--r--examples/redis-unstable/deps/lua/src/fpconv.c205
-rw-r--r--examples/redis-unstable/deps/lua/src/fpconv.h22
-rw-r--r--examples/redis-unstable/deps/lua/src/lapi.c1109
-rw-r--r--examples/redis-unstable/deps/lua/src/lapi.h16
-rw-r--r--examples/redis-unstable/deps/lua/src/lauxlib.c652
-rw-r--r--examples/redis-unstable/deps/lua/src/lauxlib.h174
-rw-r--r--examples/redis-unstable/deps/lua/src/lbaselib.c654
-rw-r--r--examples/redis-unstable/deps/lua/src/lcode.c831
-rw-r--r--examples/redis-unstable/deps/lua/src/lcode.h76
-rw-r--r--examples/redis-unstable/deps/lua/src/ldblib.c398
-rw-r--r--examples/redis-unstable/deps/lua/src/ldebug.c637
-rw-r--r--examples/redis-unstable/deps/lua/src/ldebug.h33
-rw-r--r--examples/redis-unstable/deps/lua/src/ldo.c519
-rw-r--r--examples/redis-unstable/deps/lua/src/ldo.h57
-rw-r--r--examples/redis-unstable/deps/lua/src/ldump.c164
-rw-r--r--examples/redis-unstable/deps/lua/src/lfunc.c174
-rw-r--r--examples/redis-unstable/deps/lua/src/lfunc.h34
-rw-r--r--examples/redis-unstable/deps/lua/src/lgc.c710
-rw-r--r--examples/redis-unstable/deps/lua/src/lgc.h110
-rw-r--r--examples/redis-unstable/deps/lua/src/linit.c38
-rw-r--r--examples/redis-unstable/deps/lua/src/liolib.c556
-rw-r--r--examples/redis-unstable/deps/lua/src/llex.c471
-rw-r--r--examples/redis-unstable/deps/lua/src/llex.h81
-rw-r--r--examples/redis-unstable/deps/lua/src/llimits.h128
-rw-r--r--examples/redis-unstable/deps/lua/src/lmathlib.c263
-rw-r--r--examples/redis-unstable/deps/lua/src/lmem.c86
-rw-r--r--examples/redis-unstable/deps/lua/src/lmem.h49
-rw-r--r--examples/redis-unstable/deps/lua/src/loadlib.c666
-rw-r--r--examples/redis-unstable/deps/lua/src/lobject.c214
-rw-r--r--examples/redis-unstable/deps/lua/src/lobject.h382
-rw-r--r--examples/redis-unstable/deps/lua/src/lopcodes.c102
-rw-r--r--examples/redis-unstable/deps/lua/src/lopcodes.h268
-rw-r--r--examples/redis-unstable/deps/lua/src/loslib.c250
-rw-r--r--examples/redis-unstable/deps/lua/src/lparser.c1343
-rw-r--r--examples/redis-unstable/deps/lua/src/lparser.h82
-rw-r--r--examples/redis-unstable/deps/lua/src/lstate.c214
-rw-r--r--examples/redis-unstable/deps/lua/src/lstate.h169
-rw-r--r--examples/redis-unstable/deps/lua/src/lstring.c111
-rw-r--r--examples/redis-unstable/deps/lua/src/lstring.h31
-rw-r--r--examples/redis-unstable/deps/lua/src/lstrlib.c871
-rw-r--r--examples/redis-unstable/deps/lua/src/ltable.c588
-rw-r--r--examples/redis-unstable/deps/lua/src/ltable.h40
-rw-r--r--examples/redis-unstable/deps/lua/src/ltablib.c287
-rw-r--r--examples/redis-unstable/deps/lua/src/ltm.c75
-rw-r--r--examples/redis-unstable/deps/lua/src/ltm.h54
-rw-r--r--examples/redis-unstable/deps/lua/src/lua.c392
-rw-r--r--examples/redis-unstable/deps/lua/src/lua.h391
-rw-r--r--examples/redis-unstable/deps/lua/src/lua_bit.c191
-rw-r--r--examples/redis-unstable/deps/lua/src/lua_cjson.c1471
-rw-r--r--examples/redis-unstable/deps/lua/src/lua_cmsgpack.c981
-rw-r--r--examples/redis-unstable/deps/lua/src/lua_struct.c429
-rw-r--r--examples/redis-unstable/deps/lua/src/luac.c200
-rw-r--r--examples/redis-unstable/deps/lua/src/luaconf.h763
-rw-r--r--examples/redis-unstable/deps/lua/src/lualib.h53
-rw-r--r--examples/redis-unstable/deps/lua/src/lundump.c227
-rw-r--r--examples/redis-unstable/deps/lua/src/lundump.h36
-rw-r--r--examples/redis-unstable/deps/lua/src/lvm.c769
-rw-r--r--examples/redis-unstable/deps/lua/src/lvm.h36
-rw-r--r--examples/redis-unstable/deps/lua/src/lzio.c82
-rw-r--r--examples/redis-unstable/deps/lua/src/lzio.h67
-rw-r--r--examples/redis-unstable/deps/lua/src/print.c227
-rw-r--r--examples/redis-unstable/deps/lua/src/strbuf.c197
-rw-r--r--examples/redis-unstable/deps/lua/src/strbuf.h146
64 files changed, 0 insertions, 20835 deletions
diff --git a/examples/redis-unstable/deps/lua/src/Makefile b/examples/redis-unstable/deps/lua/src/Makefile
deleted file mode 100644
index f3bba2f..0000000
--- a/examples/redis-unstable/deps/lua/src/Makefile
+++ /dev/null
@@ -1,183 +0,0 @@
1# makefile for building Lua
2# see ../INSTALL for installation instructions
3# see ../Makefile and luaconf.h for further customization
4
5# == CHANGE THE SETTINGS BELOW TO SUIT YOUR ENVIRONMENT =======================
6
7# Your platform. See PLATS for possible values.
8PLAT= none
9
10CC?= gcc
11CFLAGS= -O2 -Wall $(MYCFLAGS)
12AR= ar rcu
13RANLIB= ranlib
14RM= rm -f
15LIBS= -lm $(MYLIBS)
16
17MYCFLAGS=
18MYLDFLAGS=
19MYLIBS=
20
21# == END OF USER SETTINGS. NO NEED TO CHANGE ANYTHING BELOW THIS LINE =========
22
23PLATS= aix ansi bsd freebsd generic linux macosx mingw posix solaris
24
25LUA_A= liblua.a
26CORE_O= lapi.o lcode.o ldebug.o ldo.o ldump.o lfunc.o lgc.o llex.o lmem.o \
27 lobject.o lopcodes.o lparser.o lstate.o lstring.o ltable.o ltm.o \
28 lundump.o lvm.o lzio.o strbuf.o fpconv.o
29LIB_O= lauxlib.o lbaselib.o ldblib.o liolib.o lmathlib.o loslib.o ltablib.o \
30 lstrlib.o loadlib.o linit.o lua_cjson.o lua_struct.o lua_cmsgpack.o \
31 lua_bit.o
32
33LUA_T= lua
34LUA_O= lua.o
35
36LUAC_T= luac
37LUAC_O= luac.o print.o
38
39ALL_O= $(CORE_O) $(LIB_O) $(LUA_O) $(LUAC_O)
40ALL_T= $(LUA_A) $(LUA_T) $(LUAC_T)
41ALL_A= $(LUA_A)
42
43default: $(PLAT)
44
45all: $(ALL_T)
46
47o: $(ALL_O)
48
49a: $(ALL_A)
50
51$(LUA_A): $(CORE_O) $(LIB_O)
52 $(AR) $@ $(CORE_O) $(LIB_O) # DLL needs all object files
53 $(RANLIB) $@
54
55$(LUA_T): $(LUA_O) $(LUA_A)
56 $(CC) -o $@ $(MYLDFLAGS) $(LUA_O) $(LUA_A) $(LIBS)
57
58$(LUAC_T): $(LUAC_O) $(LUA_A)
59 $(CC) -o $@ $(MYLDFLAGS) $(LUAC_O) $(LUA_A) $(LIBS)
60
61clean:
62 $(RM) $(ALL_T) $(ALL_O)
63
64depend:
65 @$(CC) $(CFLAGS) -MM l*.c print.c
66
67echo:
68 @echo "PLAT = $(PLAT)"
69 @echo "CC = $(CC)"
70 @echo "CFLAGS = $(CFLAGS)"
71 @echo "AR = $(AR)"
72 @echo "RANLIB = $(RANLIB)"
73 @echo "RM = $(RM)"
74 @echo "MYCFLAGS = $(MYCFLAGS)"
75 @echo "MYLDFLAGS = $(MYLDFLAGS)"
76 @echo "MYLIBS = $(MYLIBS)"
77
78# convenience targets for popular platforms
79
80none:
81 @echo "Please choose a platform:"
82 @echo " $(PLATS)"
83
84aix:
85 $(MAKE) all CC="xlc" CFLAGS="-O2 -DLUA_USE_POSIX -DLUA_USE_DLOPEN" MYLIBS="-ldl" MYLDFLAGS="-brtl -bexpall"
86
87ansi:
88 $(MAKE) all MYCFLAGS=-DLUA_ANSI
89
90bsd:
91 $(MAKE) all MYCFLAGS="-DLUA_USE_POSIX -DLUA_USE_DLOPEN" MYLIBS="-Wl,-E"
92
93freebsd:
94 $(MAKE) all MYCFLAGS="-DLUA_USE_LINUX" MYLIBS="-Wl,-E -lreadline"
95
96generic:
97 $(MAKE) all MYCFLAGS=
98
99linux:
100 $(MAKE) all MYCFLAGS=-DLUA_USE_LINUX MYLIBS="-Wl,-E -ldl -lreadline -lhistory -lncurses"
101
102macosx:
103 $(MAKE) all MYCFLAGS=-DLUA_USE_LINUX MYLIBS="-lreadline"
104# use this on Mac OS X 10.3-
105# $(MAKE) all MYCFLAGS=-DLUA_USE_MACOSX
106
107mingw:
108 $(MAKE) "LUA_A=lua51.dll" "LUA_T=lua.exe" \
109 "AR=$(CC) -shared -o" "RANLIB=strip --strip-unneeded" \
110 "MYCFLAGS=-DLUA_BUILD_AS_DLL" "MYLIBS=" "MYLDFLAGS=-s" lua.exe
111 $(MAKE) "LUAC_T=luac.exe" luac.exe
112
113posix:
114 $(MAKE) all MYCFLAGS=-DLUA_USE_POSIX
115
116solaris:
117 $(MAKE) all MYCFLAGS="-DLUA_USE_POSIX -DLUA_USE_DLOPEN" MYLIBS="-ldl"
118
119# list targets that do not create files (but not all makes understand .PHONY)
120.PHONY: all $(PLATS) default o a clean depend echo none
121
122# DO NOT DELETE
123
124lapi.o: lapi.c lua.h luaconf.h lapi.h lobject.h llimits.h ldebug.h \
125 lstate.h ltm.h lzio.h lmem.h ldo.h lfunc.h lgc.h lstring.h ltable.h \
126 lundump.h lvm.h
127lauxlib.o: lauxlib.c lua.h luaconf.h lauxlib.h
128lbaselib.o: lbaselib.c lua.h luaconf.h lauxlib.h lualib.h
129lcode.o: lcode.c lua.h luaconf.h lcode.h llex.h lobject.h llimits.h \
130 lzio.h lmem.h lopcodes.h lparser.h ldebug.h lstate.h ltm.h ldo.h lgc.h \
131 ltable.h
132ldblib.o: ldblib.c lua.h luaconf.h lauxlib.h lualib.h
133ldebug.o: ldebug.c lua.h luaconf.h lapi.h lobject.h llimits.h lcode.h \
134 llex.h lzio.h lmem.h lopcodes.h lparser.h ldebug.h lstate.h ltm.h ldo.h \
135 lfunc.h lstring.h lgc.h ltable.h lvm.h
136ldo.o: ldo.c lua.h luaconf.h ldebug.h lstate.h lobject.h llimits.h ltm.h \
137 lzio.h lmem.h ldo.h lfunc.h lgc.h lopcodes.h lparser.h lstring.h \
138 ltable.h lundump.h lvm.h
139ldump.o: ldump.c lua.h luaconf.h lobject.h llimits.h lstate.h ltm.h \
140 lzio.h lmem.h lundump.h
141lfunc.o: lfunc.c lua.h luaconf.h lfunc.h lobject.h llimits.h lgc.h lmem.h \
142 lstate.h ltm.h lzio.h
143lgc.o: lgc.c lua.h luaconf.h ldebug.h lstate.h lobject.h llimits.h ltm.h \
144 lzio.h lmem.h ldo.h lfunc.h lgc.h lstring.h ltable.h
145linit.o: linit.c lua.h luaconf.h lualib.h lauxlib.h
146liolib.o: liolib.c lua.h luaconf.h lauxlib.h lualib.h
147llex.o: llex.c lua.h luaconf.h ldo.h lobject.h llimits.h lstate.h ltm.h \
148 lzio.h lmem.h llex.h lparser.h lstring.h lgc.h ltable.h
149lmathlib.o: lmathlib.c lua.h luaconf.h lauxlib.h lualib.h
150lmem.o: lmem.c lua.h luaconf.h ldebug.h lstate.h lobject.h llimits.h \
151 ltm.h lzio.h lmem.h ldo.h
152loadlib.o: loadlib.c lua.h luaconf.h lauxlib.h lualib.h
153lobject.o: lobject.c lua.h luaconf.h ldo.h lobject.h llimits.h lstate.h \
154 ltm.h lzio.h lmem.h lstring.h lgc.h lvm.h
155lopcodes.o: lopcodes.c lopcodes.h llimits.h lua.h luaconf.h
156loslib.o: loslib.c lua.h luaconf.h lauxlib.h lualib.h
157lparser.o: lparser.c lua.h luaconf.h lcode.h llex.h lobject.h llimits.h \
158 lzio.h lmem.h lopcodes.h lparser.h ldebug.h lstate.h ltm.h ldo.h \
159 lfunc.h lstring.h lgc.h ltable.h
160lstate.o: lstate.c lua.h luaconf.h ldebug.h lstate.h lobject.h llimits.h \
161 ltm.h lzio.h lmem.h ldo.h lfunc.h lgc.h llex.h lstring.h ltable.h
162lstring.o: lstring.c lua.h luaconf.h lmem.h llimits.h lobject.h lstate.h \
163 ltm.h lzio.h lstring.h lgc.h
164lstrlib.o: lstrlib.c lua.h luaconf.h lauxlib.h lualib.h
165ltable.o: ltable.c lua.h luaconf.h ldebug.h lstate.h lobject.h llimits.h \
166 ltm.h lzio.h lmem.h ldo.h lgc.h ltable.h
167ltablib.o: ltablib.c lua.h luaconf.h lauxlib.h lualib.h
168ltm.o: ltm.c lua.h luaconf.h lobject.h llimits.h lstate.h ltm.h lzio.h \
169 lmem.h lstring.h lgc.h ltable.h
170lua.o: lua.c lua.h luaconf.h lauxlib.h lualib.h
171luac.o: luac.c lua.h luaconf.h lauxlib.h ldo.h lobject.h llimits.h \
172 lstate.h ltm.h lzio.h lmem.h lfunc.h lopcodes.h lstring.h lgc.h \
173 lundump.h
174lundump.o: lundump.c lua.h luaconf.h ldebug.h lstate.h lobject.h \
175 llimits.h ltm.h lzio.h lmem.h ldo.h lfunc.h lstring.h lgc.h lundump.h
176lvm.o: lvm.c lua.h luaconf.h ldebug.h lstate.h lobject.h llimits.h ltm.h \
177 lzio.h lmem.h ldo.h lfunc.h lgc.h lopcodes.h lstring.h ltable.h lvm.h
178lzio.o: lzio.c lua.h luaconf.h llimits.h lmem.h lstate.h lobject.h ltm.h \
179 lzio.h
180print.o: print.c ldebug.h lstate.h lua.h luaconf.h lobject.h llimits.h \
181 ltm.h lzio.h lmem.h lopcodes.h lundump.h
182
183# (end of Makefile)
diff --git a/examples/redis-unstable/deps/lua/src/fpconv.c b/examples/redis-unstable/deps/lua/src/fpconv.c
deleted file mode 100644
index 7990831..0000000
--- a/examples/redis-unstable/deps/lua/src/fpconv.c
+++ /dev/null
@@ -1,205 +0,0 @@
1/* fpconv - Floating point conversion routines
2 *
3 * Copyright (c) 2011-2012 Mark Pulford <mark@kyne.com.au>
4 *
5 * Permission is hereby granted, free of charge, to any person obtaining
6 * a copy of this software and associated documentation files (the
7 * "Software"), to deal in the Software without restriction, including
8 * without limitation the rights to use, copy, modify, merge, publish,
9 * distribute, sublicense, and/or sell copies of the Software, and to
10 * permit persons to whom the Software is furnished to do so, subject to
11 * the following conditions:
12 *
13 * The above copyright notice and this permission notice shall be
14 * included in all copies or substantial portions of the Software.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
19 * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
20 * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
21 * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
22 * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
23 */
24
25/* JSON uses a '.' decimal separator. strtod() / sprintf() under C libraries
26 * with locale support will break when the decimal separator is a comma.
27 *
28 * fpconv_* will around these issues with a translation buffer if required.
29 */
30
31#include <stdio.h>
32#include <stdlib.h>
33#include <assert.h>
34#include <string.h>
35
36#include "fpconv.h"
37
38/* Lua CJSON assumes the locale is the same for all threads within a
39 * process and doesn't change after initialisation.
40 *
41 * This avoids the need for per thread storage or expensive checks
42 * for call. */
43static char locale_decimal_point = '.';
44
45/* In theory multibyte decimal_points are possible, but
46 * Lua CJSON only supports UTF-8 and known locales only have
47 * single byte decimal points ([.,]).
48 *
49 * localconv() may not be thread safe (=>crash), and nl_langinfo() is
50 * not supported on some platforms. Use sprintf() instead - if the
51 * locale does change, at least Lua CJSON won't crash. */
52static void fpconv_update_locale()
53{
54 char buf[8];
55
56 snprintf(buf, sizeof(buf), "%g", 0.5);
57
58 /* Failing this test might imply the platform has a buggy dtoa
59 * implementation or wide characters */
60 if (buf[0] != '0' || buf[2] != '5' || buf[3] != 0) {
61 fprintf(stderr, "Error: wide characters found or printf() bug.");
62 abort();
63 }
64
65 locale_decimal_point = buf[1];
66}
67
68/* Check for a valid number character: [-+0-9a-yA-Y.]
69 * Eg: -0.6e+5, infinity, 0xF0.F0pF0
70 *
71 * Used to find the probable end of a number. It doesn't matter if
72 * invalid characters are counted - strtod() will find the valid
73 * number if it exists. The risk is that slightly more memory might
74 * be allocated before a parse error occurs. */
75static inline int valid_number_character(char ch)
76{
77 char lower_ch;
78
79 if ('0' <= ch && ch <= '9')
80 return 1;
81 if (ch == '-' || ch == '+' || ch == '.')
82 return 1;
83
84 /* Hex digits, exponent (e), base (p), "infinity",.. */
85 lower_ch = ch | 0x20;
86 if ('a' <= lower_ch && lower_ch <= 'y')
87 return 1;
88
89 return 0;
90}
91
92/* Calculate the size of the buffer required for a strtod locale
93 * conversion. */
94static int strtod_buffer_size(const char *s)
95{
96 const char *p = s;
97
98 while (valid_number_character(*p))
99 p++;
100
101 return p - s;
102}
103
104/* Similar to strtod(), but must be passed the current locale's decimal point
105 * character. Guaranteed to be called at the start of any valid number in a string */
106double fpconv_strtod(const char *nptr, char **endptr)
107{
108 char localbuf[FPCONV_G_FMT_BUFSIZE];
109 char *buf, *endbuf, *dp;
110 int buflen;
111 double value;
112
113 /* System strtod() is fine when decimal point is '.' */
114 if (locale_decimal_point == '.')
115 return strtod(nptr, endptr);
116
117 buflen = strtod_buffer_size(nptr);
118 if (!buflen) {
119 /* No valid characters found, standard strtod() return */
120 *endptr = (char *)nptr;
121 return 0;
122 }
123
124 /* Duplicate number into buffer */
125 if (buflen >= FPCONV_G_FMT_BUFSIZE) {
126 /* Handle unusually large numbers */
127 buf = malloc(buflen + 1);
128 if (!buf) {
129 fprintf(stderr, "Out of memory");
130 abort();
131 }
132 } else {
133 /* This is the common case.. */
134 buf = localbuf;
135 }
136 memcpy(buf, nptr, buflen);
137 buf[buflen] = 0;
138
139 /* Update decimal point character if found */
140 dp = strchr(buf, '.');
141 if (dp)
142 *dp = locale_decimal_point;
143
144 value = strtod(buf, &endbuf);
145 *endptr = (char *)&nptr[endbuf - buf];
146 if (buflen >= FPCONV_G_FMT_BUFSIZE)
147 free(buf);
148
149 return value;
150}
151
152/* "fmt" must point to a buffer of at least 6 characters */
153static void set_number_format(char *fmt, int precision)
154{
155 int d1, d2, i;
156
157 assert(1 <= precision && precision <= 14);
158
159 /* Create printf format (%.14g) from precision */
160 d1 = precision / 10;
161 d2 = precision % 10;
162 fmt[0] = '%';
163 fmt[1] = '.';
164 i = 2;
165 if (d1) {
166 fmt[i++] = '0' + d1;
167 }
168 fmt[i++] = '0' + d2;
169 fmt[i++] = 'g';
170 fmt[i] = 0;
171}
172
173/* Assumes there is always at least 32 characters available in the target buffer */
174int fpconv_g_fmt(char *str, double num, int precision)
175{
176 char buf[FPCONV_G_FMT_BUFSIZE];
177 char fmt[6];
178 int len;
179 char *b;
180
181 set_number_format(fmt, precision);
182
183 /* Pass through when decimal point character is dot. */
184 if (locale_decimal_point == '.')
185 return snprintf(str, FPCONV_G_FMT_BUFSIZE, fmt, num);
186
187 /* snprintf() to a buffer then translate for other decimal point characters */
188 len = snprintf(buf, FPCONV_G_FMT_BUFSIZE, fmt, num);
189
190 /* Copy into target location. Translate decimal point if required */
191 b = buf;
192 do {
193 *str++ = (*b == locale_decimal_point ? '.' : *b);
194 } while(*b++);
195
196 return len;
197}
198
199void fpconv_init()
200{
201 fpconv_update_locale();
202}
203
204/* vi:ai et sw=4 ts=4:
205 */
diff --git a/examples/redis-unstable/deps/lua/src/fpconv.h b/examples/redis-unstable/deps/lua/src/fpconv.h
deleted file mode 100644
index 7b0d0ee..0000000
--- a/examples/redis-unstable/deps/lua/src/fpconv.h
+++ /dev/null
@@ -1,22 +0,0 @@
1/* Lua CJSON floating point conversion routines */
2
3/* Buffer required to store the largest string representation of a double.
4 *
5 * Longest double printed with %.14g is 21 characters long:
6 * -1.7976931348623e+308 */
7# define FPCONV_G_FMT_BUFSIZE 32
8
9#ifdef USE_INTERNAL_FPCONV
10static inline void fpconv_init()
11{
12 /* Do nothing - not required */
13}
14#else
15extern void fpconv_init();
16#endif
17
18extern int fpconv_g_fmt(char*, double, int);
19extern double fpconv_strtod(const char*, char**);
20
21/* vi:ai et sw=4 ts=4:
22 */
diff --git a/examples/redis-unstable/deps/lua/src/lapi.c b/examples/redis-unstable/deps/lua/src/lapi.c
deleted file mode 100644
index e8ef41e..0000000
--- a/examples/redis-unstable/deps/lua/src/lapi.c
+++ /dev/null
@@ -1,1109 +0,0 @@
1/*
2** $Id: lapi.c,v 2.55.1.5 2008/07/04 18:41:18 roberto Exp $
3** Lua API
4** See Copyright Notice in lua.h
5*/
6
7
8#include <assert.h>
9#include <math.h>
10#include <stdarg.h>
11#include <string.h>
12
13#define lapi_c
14#define LUA_CORE
15
16#include "lua.h"
17
18#include "lapi.h"
19#include "ldebug.h"
20#include "ldo.h"
21#include "lfunc.h"
22#include "lgc.h"
23#include "lmem.h"
24#include "lobject.h"
25#include "lstate.h"
26#include "lstring.h"
27#include "ltable.h"
28#include "ltm.h"
29#include "lundump.h"
30#include "lvm.h"
31
32
33
34const char lua_ident[] =
35 "$Lua: " LUA_RELEASE " " LUA_COPYRIGHT " $\n"
36 "$Authors: " LUA_AUTHORS " $\n"
37 "$URL: www.lua.org $\n";
38
39
40
41#define api_checknelems(L, n) api_check(L, (n) <= (L->top - L->base))
42
43#define api_checkvalidindex(L, i) api_check(L, (i) != luaO_nilobject)
44
45#define api_incr_top(L) {api_check(L, L->top < L->ci->top); L->top++;}
46
47
48
49static TValue *index2adr (lua_State *L, int idx) {
50 if (idx > 0) {
51 TValue *o = L->base + (idx - 1);
52 api_check(L, idx <= L->ci->top - L->base);
53 if (o >= L->top) return cast(TValue *, luaO_nilobject);
54 else return o;
55 }
56 else if (idx > LUA_REGISTRYINDEX) {
57 api_check(L, idx != 0 && -idx <= L->top - L->base);
58 return L->top + idx;
59 }
60 else switch (idx) { /* pseudo-indices */
61 case LUA_REGISTRYINDEX: return registry(L);
62 case LUA_ENVIRONINDEX: {
63 Closure *func = curr_func(L);
64 sethvalue(L, &L->env, func->c.env);
65 return &L->env;
66 }
67 case LUA_GLOBALSINDEX: return gt(L);
68 default: {
69 Closure *func = curr_func(L);
70 idx = LUA_GLOBALSINDEX - idx;
71 return (idx <= func->c.nupvalues)
72 ? &func->c.upvalue[idx-1]
73 : cast(TValue *, luaO_nilobject);
74 }
75 }
76}
77
78
79static Table *getcurrenv (lua_State *L) {
80 if (L->ci == L->base_ci) /* no enclosing function? */
81 return hvalue(gt(L)); /* use global table as environment */
82 else {
83 Closure *func = curr_func(L);
84 return func->c.env;
85 }
86}
87
88
89void luaA_pushobject (lua_State *L, const TValue *o) {
90 setobj2s(L, L->top, o);
91 api_incr_top(L);
92}
93
94
95LUA_API int lua_checkstack (lua_State *L, int size) {
96 int res = 1;
97 lua_lock(L);
98 if (size > LUAI_MAXCSTACK || (L->top - L->base + size) > LUAI_MAXCSTACK)
99 res = 0; /* stack overflow */
100 else if (size > 0) {
101 luaD_checkstack(L, size);
102 if (L->ci->top < L->top + size)
103 L->ci->top = L->top + size;
104 }
105 lua_unlock(L);
106 return res;
107}
108
109
110LUA_API void lua_xmove (lua_State *from, lua_State *to, int n) {
111 int i;
112 if (from == to) return;
113 lua_lock(to);
114 api_checknelems(from, n);
115 api_check(from, G(from) == G(to));
116 api_check(from, to->ci->top - to->top >= n);
117 from->top -= n;
118 for (i = 0; i < n; i++) {
119 setobj2s(to, to->top++, from->top + i);
120 }
121 lua_unlock(to);
122}
123
124
125LUA_API void lua_setlevel (lua_State *from, lua_State *to) {
126 to->nCcalls = from->nCcalls;
127}
128
129
130LUA_API lua_CFunction lua_atpanic (lua_State *L, lua_CFunction panicf) {
131 lua_CFunction old;
132 lua_lock(L);
133 old = G(L)->panic;
134 G(L)->panic = panicf;
135 lua_unlock(L);
136 return old;
137}
138
139
140LUA_API lua_State *lua_newthread (lua_State *L) {
141 lua_State *L1;
142 lua_lock(L);
143 luaC_checkGC(L);
144 L1 = luaE_newthread(L);
145 setthvalue(L, L->top, L1);
146 api_incr_top(L);
147 lua_unlock(L);
148 luai_userstatethread(L, L1);
149 return L1;
150}
151
152
153
154/*
155** basic stack manipulation
156*/
157
158
159LUA_API int lua_gettop (lua_State *L) {
160 return cast_int(L->top - L->base);
161}
162
163
164LUA_API void lua_settop (lua_State *L, int idx) {
165 lua_lock(L);
166 if (idx >= 0) {
167 api_check(L, idx <= L->stack_last - L->base);
168 while (L->top < L->base + idx)
169 setnilvalue(L->top++);
170 L->top = L->base + idx;
171 }
172 else {
173 api_check(L, -(idx+1) <= (L->top - L->base));
174 L->top += idx+1; /* `subtract' index (index is negative) */
175 }
176 lua_unlock(L);
177}
178
179
180LUA_API void lua_remove (lua_State *L, int idx) {
181 StkId p;
182 lua_lock(L);
183 p = index2adr(L, idx);
184 api_checkvalidindex(L, p);
185 while (++p < L->top) setobjs2s(L, p-1, p);
186 L->top--;
187 lua_unlock(L);
188}
189
190
191LUA_API void lua_insert (lua_State *L, int idx) {
192 StkId p;
193 StkId q;
194 lua_lock(L);
195 p = index2adr(L, idx);
196 api_checkvalidindex(L, p);
197 for (q = L->top; q>p; q--) setobjs2s(L, q, q-1);
198 setobjs2s(L, p, L->top);
199 lua_unlock(L);
200}
201
202
203LUA_API void lua_replace (lua_State *L, int idx) {
204 StkId o;
205 lua_lock(L);
206 /* explicit test for incompatible code */
207 if (idx == LUA_ENVIRONINDEX && L->ci == L->base_ci)
208 luaG_runerror(L, "no calling environment");
209 api_checknelems(L, 1);
210 o = index2adr(L, idx);
211 api_checkvalidindex(L, o);
212 if (idx == LUA_ENVIRONINDEX) {
213 Closure *func = curr_func(L);
214 api_check(L, ttistable(L->top - 1));
215 func->c.env = hvalue(L->top - 1);
216 luaC_barrier(L, func, L->top - 1);
217 }
218 else {
219 setobj(L, o, L->top - 1);
220 if (idx < LUA_GLOBALSINDEX) /* function upvalue? */
221 luaC_barrier(L, curr_func(L), L->top - 1);
222 }
223 L->top--;
224 lua_unlock(L);
225}
226
227
228LUA_API void lua_pushvalue (lua_State *L, int idx) {
229 lua_lock(L);
230 setobj2s(L, L->top, index2adr(L, idx));
231 api_incr_top(L);
232 lua_unlock(L);
233}
234
235
236
237/*
238** access functions (stack -> C)
239*/
240
241
242LUA_API int lua_type (lua_State *L, int idx) {
243 StkId o = index2adr(L, idx);
244 return (o == luaO_nilobject) ? LUA_TNONE : ttype(o);
245}
246
247
248LUA_API const char *lua_typename (lua_State *L, int t) {
249 UNUSED(L);
250 return (t == LUA_TNONE) ? "no value" : luaT_typenames[t];
251}
252
253
254LUA_API int lua_iscfunction (lua_State *L, int idx) {
255 StkId o = index2adr(L, idx);
256 return iscfunction(o);
257}
258
259
260LUA_API int lua_isnumber (lua_State *L, int idx) {
261 TValue n;
262 const TValue *o = index2adr(L, idx);
263 return tonumber(o, &n);
264}
265
266
267LUA_API int lua_isstring (lua_State *L, int idx) {
268 int t = lua_type(L, idx);
269 return (t == LUA_TSTRING || t == LUA_TNUMBER);
270}
271
272
273LUA_API int lua_isuserdata (lua_State *L, int idx) {
274 const TValue *o = index2adr(L, idx);
275 return (ttisuserdata(o) || ttislightuserdata(o));
276}
277
278
279LUA_API int lua_rawequal (lua_State *L, int index1, int index2) {
280 StkId o1 = index2adr(L, index1);
281 StkId o2 = index2adr(L, index2);
282 return (o1 == luaO_nilobject || o2 == luaO_nilobject) ? 0
283 : luaO_rawequalObj(o1, o2);
284}
285
286
287LUA_API int lua_equal (lua_State *L, int index1, int index2) {
288 StkId o1, o2;
289 int i;
290 lua_lock(L); /* may call tag method */
291 o1 = index2adr(L, index1);
292 o2 = index2adr(L, index2);
293 i = (o1 == luaO_nilobject || o2 == luaO_nilobject) ? 0 : equalobj(L, o1, o2);
294 lua_unlock(L);
295 return i;
296}
297
298
299LUA_API int lua_lessthan (lua_State *L, int index1, int index2) {
300 StkId o1, o2;
301 int i;
302 lua_lock(L); /* may call tag method */
303 o1 = index2adr(L, index1);
304 o2 = index2adr(L, index2);
305 i = (o1 == luaO_nilobject || o2 == luaO_nilobject) ? 0
306 : luaV_lessthan(L, o1, o2);
307 lua_unlock(L);
308 return i;
309}
310
311
312
313LUA_API lua_Number lua_tonumber (lua_State *L, int idx) {
314 TValue n;
315 const TValue *o = index2adr(L, idx);
316 if (tonumber(o, &n))
317 return nvalue(o);
318 else
319 return 0;
320}
321
322
323LUA_API lua_Integer lua_tointeger (lua_State *L, int idx) {
324 TValue n;
325 const TValue *o = index2adr(L, idx);
326 if (tonumber(o, &n)) {
327 lua_Integer res;
328 lua_Number num = nvalue(o);
329 lua_number2integer(res, num);
330 return res;
331 }
332 else
333 return 0;
334}
335
336
337LUA_API int lua_toboolean (lua_State *L, int idx) {
338 const TValue *o = index2adr(L, idx);
339 return !l_isfalse(o);
340}
341
342
343LUA_API const char *lua_tolstring (lua_State *L, int idx, size_t *len) {
344 StkId o = index2adr(L, idx);
345 if (!ttisstring(o)) {
346 lua_lock(L); /* `luaV_tostring' may create a new string */
347 if (!luaV_tostring(L, o)) { /* conversion failed? */
348 if (len != NULL) *len = 0;
349 lua_unlock(L);
350 return NULL;
351 }
352 luaC_checkGC(L);
353 o = index2adr(L, idx); /* previous call may reallocate the stack */
354 lua_unlock(L);
355 }
356 if (len != NULL) *len = tsvalue(o)->len;
357 return svalue(o);
358}
359
360
361LUA_API size_t lua_objlen (lua_State *L, int idx) {
362 StkId o = index2adr(L, idx);
363 switch (ttype(o)) {
364 case LUA_TSTRING: return tsvalue(o)->len;
365 case LUA_TUSERDATA: return uvalue(o)->len;
366 case LUA_TTABLE: return luaH_getn(hvalue(o));
367 case LUA_TNUMBER: {
368 size_t l;
369 lua_lock(L); /* `luaV_tostring' may create a new string */
370 l = (luaV_tostring(L, o) ? tsvalue(o)->len : 0);
371 lua_unlock(L);
372 return l;
373 }
374 default: return 0;
375 }
376}
377
378
379LUA_API lua_CFunction lua_tocfunction (lua_State *L, int idx) {
380 StkId o = index2adr(L, idx);
381 return (!iscfunction(o)) ? NULL : clvalue(o)->c.f;
382}
383
384
385LUA_API void *lua_touserdata (lua_State *L, int idx) {
386 StkId o = index2adr(L, idx);
387 switch (ttype(o)) {
388 case LUA_TUSERDATA: return (rawuvalue(o) + 1);
389 case LUA_TLIGHTUSERDATA: return pvalue(o);
390 default: return NULL;
391 }
392}
393
394
395LUA_API lua_State *lua_tothread (lua_State *L, int idx) {
396 StkId o = index2adr(L, idx);
397 return (!ttisthread(o)) ? NULL : thvalue(o);
398}
399
400
401LUA_API const void *lua_topointer (lua_State *L, int idx) {
402 StkId o = index2adr(L, idx);
403 switch (ttype(o)) {
404 case LUA_TTABLE: return hvalue(o);
405 case LUA_TFUNCTION: return clvalue(o);
406 case LUA_TTHREAD: return thvalue(o);
407 case LUA_TUSERDATA:
408 case LUA_TLIGHTUSERDATA:
409 return lua_touserdata(L, idx);
410 default: return NULL;
411 }
412}
413
414
415
416/*
417** push functions (C -> stack)
418*/
419
420
421LUA_API void lua_pushnil (lua_State *L) {
422 lua_lock(L);
423 setnilvalue(L->top);
424 api_incr_top(L);
425 lua_unlock(L);
426}
427
428
429LUA_API void lua_pushnumber (lua_State *L, lua_Number n) {
430 lua_lock(L);
431 setnvalue(L->top, n);
432 api_incr_top(L);
433 lua_unlock(L);
434}
435
436
437LUA_API void lua_pushinteger (lua_State *L, lua_Integer n) {
438 lua_lock(L);
439 setnvalue(L->top, cast_num(n));
440 api_incr_top(L);
441 lua_unlock(L);
442}
443
444
445LUA_API void lua_pushlstring (lua_State *L, const char *s, size_t len) {
446 lua_lock(L);
447 luaC_checkGC(L);
448 setsvalue2s(L, L->top, luaS_newlstr(L, s, len));
449 api_incr_top(L);
450 lua_unlock(L);
451}
452
453
454LUA_API void lua_pushstring (lua_State *L, const char *s) {
455 if (s == NULL)
456 lua_pushnil(L);
457 else
458 lua_pushlstring(L, s, strlen(s));
459}
460
461
462LUA_API const char *lua_pushvfstring (lua_State *L, const char *fmt,
463 va_list argp) {
464 const char *ret;
465 lua_lock(L);
466 luaC_checkGC(L);
467 ret = luaO_pushvfstring(L, fmt, argp);
468 lua_unlock(L);
469 return ret;
470}
471
472
473LUA_API const char *lua_pushfstring (lua_State *L, const char *fmt, ...) {
474 const char *ret;
475 va_list argp;
476 lua_lock(L);
477 luaC_checkGC(L);
478 va_start(argp, fmt);
479 ret = luaO_pushvfstring(L, fmt, argp);
480 va_end(argp);
481 lua_unlock(L);
482 return ret;
483}
484
485
486LUA_API void lua_pushcclosure (lua_State *L, lua_CFunction fn, int n) {
487 Closure *cl;
488 lua_lock(L);
489 luaC_checkGC(L);
490 api_checknelems(L, n);
491 cl = luaF_newCclosure(L, n, getcurrenv(L));
492 cl->c.f = fn;
493 L->top -= n;
494 while (n--)
495 setobj2n(L, &cl->c.upvalue[n], L->top+n);
496 setclvalue(L, L->top, cl);
497 lua_assert(iswhite(obj2gco(cl)));
498 api_incr_top(L);
499 lua_unlock(L);
500}
501
502
503LUA_API void lua_pushboolean (lua_State *L, int b) {
504 lua_lock(L);
505 setbvalue(L->top, (b != 0)); /* ensure that true is 1 */
506 api_incr_top(L);
507 lua_unlock(L);
508}
509
510
511LUA_API void lua_pushlightuserdata (lua_State *L, void *p) {
512 lua_lock(L);
513 setpvalue(L->top, p);
514 api_incr_top(L);
515 lua_unlock(L);
516}
517
518
519LUA_API int lua_pushthread (lua_State *L) {
520 lua_lock(L);
521 setthvalue(L, L->top, L);
522 api_incr_top(L);
523 lua_unlock(L);
524 return (G(L)->mainthread == L);
525}
526
527
528
529/*
530** get functions (Lua -> stack)
531*/
532
533
534LUA_API void lua_gettable (lua_State *L, int idx) {
535 StkId t;
536 lua_lock(L);
537 t = index2adr(L, idx);
538 api_checkvalidindex(L, t);
539 luaV_gettable(L, t, L->top - 1, L->top - 1);
540 lua_unlock(L);
541}
542
543
544LUA_API void lua_getfield (lua_State *L, int idx, const char *k) {
545 StkId t;
546 TValue key;
547 lua_lock(L);
548 t = index2adr(L, idx);
549 api_checkvalidindex(L, t);
550 setsvalue(L, &key, luaS_new(L, k));
551 luaV_gettable(L, t, &key, L->top);
552 api_incr_top(L);
553 lua_unlock(L);
554}
555
556
557LUA_API void lua_rawget (lua_State *L, int idx) {
558 StkId t;
559 lua_lock(L);
560 t = index2adr(L, idx);
561 api_check(L, ttistable(t));
562 setobj2s(L, L->top - 1, luaH_get(hvalue(t), L->top - 1));
563 lua_unlock(L);
564}
565
566
567LUA_API void lua_rawgeti (lua_State *L, int idx, int n) {
568 StkId o;
569 lua_lock(L);
570 o = index2adr(L, idx);
571 api_check(L, ttistable(o));
572 setobj2s(L, L->top, luaH_getnum(hvalue(o), n));
573 api_incr_top(L);
574 lua_unlock(L);
575}
576
577
578LUA_API void lua_createtable (lua_State *L, int narray, int nrec) {
579 lua_lock(L);
580 luaC_checkGC(L);
581 sethvalue(L, L->top, luaH_new(L, narray, nrec));
582 api_incr_top(L);
583 lua_unlock(L);
584}
585
586
587LUA_API int lua_getmetatable (lua_State *L, int objindex) {
588 const TValue *obj;
589 Table *mt = NULL;
590 int res;
591 lua_lock(L);
592 obj = index2adr(L, objindex);
593 switch (ttype(obj)) {
594 case LUA_TTABLE:
595 mt = hvalue(obj)->metatable;
596 break;
597 case LUA_TUSERDATA:
598 mt = uvalue(obj)->metatable;
599 break;
600 default:
601 mt = G(L)->mt[ttype(obj)];
602 break;
603 }
604 if (mt == NULL)
605 res = 0;
606 else {
607 sethvalue(L, L->top, mt);
608 api_incr_top(L);
609 res = 1;
610 }
611 lua_unlock(L);
612 return res;
613}
614
615
616LUA_API void lua_getfenv (lua_State *L, int idx) {
617 StkId o;
618 lua_lock(L);
619 o = index2adr(L, idx);
620 api_checkvalidindex(L, o);
621 switch (ttype(o)) {
622 case LUA_TFUNCTION:
623 sethvalue(L, L->top, clvalue(o)->c.env);
624 break;
625 case LUA_TUSERDATA:
626 sethvalue(L, L->top, uvalue(o)->env);
627 break;
628 case LUA_TTHREAD:
629 setobj2s(L, L->top, gt(thvalue(o)));
630 break;
631 default:
632 setnilvalue(L->top);
633 break;
634 }
635 api_incr_top(L);
636 lua_unlock(L);
637}
638
639
640/*
641** set functions (stack -> Lua)
642*/
643
644
645LUA_API void lua_settable (lua_State *L, int idx) {
646 StkId t;
647 lua_lock(L);
648 api_checknelems(L, 2);
649 t = index2adr(L, idx);
650 api_checkvalidindex(L, t);
651 luaV_settable(L, t, L->top - 2, L->top - 1);
652 L->top -= 2; /* pop index and value */
653 lua_unlock(L);
654}
655
656
657LUA_API void lua_setfield (lua_State *L, int idx, const char *k) {
658 StkId t;
659 TValue key;
660 lua_lock(L);
661 api_checknelems(L, 1);
662 t = index2adr(L, idx);
663 api_checkvalidindex(L, t);
664 setsvalue(L, &key, luaS_new(L, k));
665 luaV_settable(L, t, &key, L->top - 1);
666 L->top--; /* pop value */
667 lua_unlock(L);
668}
669
670
671LUA_API void lua_rawset (lua_State *L, int idx) {
672 StkId t;
673 lua_lock(L);
674 api_checknelems(L, 2);
675 t = index2adr(L, idx);
676 api_check(L, ttistable(t));
677 if (hvalue(t)->readonly)
678 luaG_runerror(L, "Attempt to modify a readonly table");
679 setobj2t(L, luaH_set(L, hvalue(t), L->top-2), L->top-1);
680 luaC_barriert(L, hvalue(t), L->top-1);
681 L->top -= 2;
682 lua_unlock(L);
683}
684
685
686LUA_API void lua_rawseti (lua_State *L, int idx, int n) {
687 StkId o;
688 lua_lock(L);
689 api_checknelems(L, 1);
690 o = index2adr(L, idx);
691 api_check(L, ttistable(o));
692 if (hvalue(o)->readonly)
693 luaG_runerror(L, "Attempt to modify a readonly table");
694 setobj2t(L, luaH_setnum(L, hvalue(o), n), L->top-1);
695 luaC_barriert(L, hvalue(o), L->top-1);
696 L->top--;
697 lua_unlock(L);
698}
699
700
701LUA_API int lua_setmetatable (lua_State *L, int objindex) {
702 TValue *obj;
703 Table *mt;
704 lua_lock(L);
705 api_checknelems(L, 1);
706 obj = index2adr(L, objindex);
707 api_checkvalidindex(L, obj);
708 if (ttisnil(L->top - 1))
709 mt = NULL;
710 else {
711 api_check(L, ttistable(L->top - 1));
712 mt = hvalue(L->top - 1);
713 }
714 switch (ttype(obj)) {
715 case LUA_TTABLE: {
716 if (hvalue(obj)->readonly)
717 luaG_runerror(L, "Attempt to modify a readonly table");
718 hvalue(obj)->metatable = mt;
719 if (mt)
720 luaC_objbarriert(L, hvalue(obj), mt);
721 break;
722 }
723 case LUA_TUSERDATA: {
724 uvalue(obj)->metatable = mt;
725 if (mt)
726 luaC_objbarrier(L, rawuvalue(obj), mt);
727 break;
728 }
729 default: {
730 G(L)->mt[ttype(obj)] = mt;
731 break;
732 }
733 }
734 L->top--;
735 lua_unlock(L);
736 return 1;
737}
738
739
740LUA_API int lua_setfenv (lua_State *L, int idx) {
741 StkId o;
742 int res = 1;
743 lua_lock(L);
744 api_checknelems(L, 1);
745 o = index2adr(L, idx);
746 api_checkvalidindex(L, o);
747 api_check(L, ttistable(L->top - 1));
748 switch (ttype(o)) {
749 case LUA_TFUNCTION:
750 clvalue(o)->c.env = hvalue(L->top - 1);
751 break;
752 case LUA_TUSERDATA:
753 uvalue(o)->env = hvalue(L->top - 1);
754 break;
755 case LUA_TTHREAD:
756 sethvalue(L, gt(thvalue(o)), hvalue(L->top - 1));
757 break;
758 default:
759 res = 0;
760 break;
761 }
762 if (res) luaC_objbarrier(L, gcvalue(o), hvalue(L->top - 1));
763 L->top--;
764 lua_unlock(L);
765 return res;
766}
767
768
769/*
770** `load' and `call' functions (run Lua code)
771*/
772
773
774#define adjustresults(L,nres) \
775 { if (nres == LUA_MULTRET && L->top >= L->ci->top) L->ci->top = L->top; }
776
777
778#define checkresults(L,na,nr) \
779 api_check(L, (nr) == LUA_MULTRET || (L->ci->top - L->top >= (nr) - (na)))
780
781
782LUA_API void lua_call (lua_State *L, int nargs, int nresults) {
783 StkId func;
784 lua_lock(L);
785 api_checknelems(L, nargs+1);
786 checkresults(L, nargs, nresults);
787 func = L->top - (nargs+1);
788 luaD_call(L, func, nresults);
789 adjustresults(L, nresults);
790 lua_unlock(L);
791}
792
793
794
795/*
796** Execute a protected call.
797*/
798struct CallS { /* data to `f_call' */
799 StkId func;
800 int nresults;
801};
802
803
804static void f_call (lua_State *L, void *ud) {
805 struct CallS *c = cast(struct CallS *, ud);
806 luaD_call(L, c->func, c->nresults);
807}
808
809
810
811LUA_API int lua_pcall (lua_State *L, int nargs, int nresults, int errfunc) {
812 struct CallS c;
813 int status;
814 ptrdiff_t func;
815 lua_lock(L);
816 api_checknelems(L, nargs+1);
817 checkresults(L, nargs, nresults);
818 if (errfunc == 0)
819 func = 0;
820 else {
821 StkId o = index2adr(L, errfunc);
822 api_checkvalidindex(L, o);
823 func = savestack(L, o);
824 }
825 c.func = L->top - (nargs+1); /* function to be called */
826 c.nresults = nresults;
827 status = luaD_pcall(L, f_call, &c, savestack(L, c.func), func);
828 adjustresults(L, nresults);
829 lua_unlock(L);
830 return status;
831}
832
833
834/*
835** Execute a protected C call.
836*/
837struct CCallS { /* data to `f_Ccall' */
838 lua_CFunction func;
839 void *ud;
840};
841
842
843static void f_Ccall (lua_State *L, void *ud) {
844 struct CCallS *c = cast(struct CCallS *, ud);
845 Closure *cl;
846 cl = luaF_newCclosure(L, 0, getcurrenv(L));
847 cl->c.f = c->func;
848 setclvalue(L, L->top, cl); /* push function */
849 api_incr_top(L);
850 setpvalue(L->top, c->ud); /* push only argument */
851 api_incr_top(L);
852 luaD_call(L, L->top - 2, 0);
853}
854
855
856LUA_API int lua_cpcall (lua_State *L, lua_CFunction func, void *ud) {
857 struct CCallS c;
858 int status;
859 lua_lock(L);
860 c.func = func;
861 c.ud = ud;
862 status = luaD_pcall(L, f_Ccall, &c, savestack(L, L->top), 0);
863 lua_unlock(L);
864 return status;
865}
866
867
868LUA_API int lua_load (lua_State *L, lua_Reader reader, void *data,
869 const char *chunkname) {
870 ZIO z;
871 int status;
872 lua_lock(L);
873 if (!chunkname) chunkname = "?";
874 luaZ_init(L, &z, reader, data);
875 status = luaD_protectedparser(L, &z, chunkname);
876 lua_unlock(L);
877 return status;
878}
879
880
881LUA_API int lua_dump (lua_State *L, lua_Writer writer, void *data) {
882 int status;
883 TValue *o;
884 lua_lock(L);
885 api_checknelems(L, 1);
886 o = L->top - 1;
887 if (isLfunction(o))
888 status = luaU_dump(L, clvalue(o)->l.p, writer, data, 0);
889 else
890 status = 1;
891 lua_unlock(L);
892 return status;
893}
894
895
896LUA_API int lua_status (lua_State *L) {
897 return L->status;
898}
899
900
901/*
902** Garbage-collection function
903*/
904
905LUA_API int lua_gc (lua_State *L, int what, int data) {
906 int res = 0;
907 global_State *g;
908 lua_lock(L);
909 g = G(L);
910 switch (what) {
911 case LUA_GCSTOP: {
912 g->GCthreshold = MAX_LUMEM;
913 break;
914 }
915 case LUA_GCRESTART: {
916 g->GCthreshold = g->totalbytes;
917 break;
918 }
919 case LUA_GCCOLLECT: {
920 luaC_fullgc(L);
921 break;
922 }
923 case LUA_GCCOUNT: {
924 /* GC values are expressed in Kbytes: #bytes/2^10 */
925 res = cast_int(g->totalbytes >> 10);
926 break;
927 }
928 case LUA_GCCOUNTB: {
929 res = cast_int(g->totalbytes & 0x3ff);
930 break;
931 }
932 case LUA_GCSTEP: {
933 lu_mem a = (cast(lu_mem, data) << 10);
934 if (a <= g->totalbytes)
935 g->GCthreshold = g->totalbytes - a;
936 else
937 g->GCthreshold = 0;
938 while (g->GCthreshold <= g->totalbytes) {
939 luaC_step(L);
940 if (g->gcstate == GCSpause) { /* end of cycle? */
941 res = 1; /* signal it */
942 break;
943 }
944 }
945 break;
946 }
947 case LUA_GCSETPAUSE: {
948 res = g->gcpause;
949 g->gcpause = data;
950 break;
951 }
952 case LUA_GCSETSTEPMUL: {
953 res = g->gcstepmul;
954 g->gcstepmul = data;
955 break;
956 }
957 default: res = -1; /* invalid option */
958 }
959 lua_unlock(L);
960 return res;
961}
962
963
964
965/*
966** miscellaneous functions
967*/
968
969
970LUA_API int lua_error (lua_State *L) {
971 lua_lock(L);
972 api_checknelems(L, 1);
973 luaG_errormsg(L);
974 lua_unlock(L);
975 return 0; /* to avoid warnings */
976}
977
978
979LUA_API int lua_next (lua_State *L, int idx) {
980 StkId t;
981 int more;
982 lua_lock(L);
983 t = index2adr(L, idx);
984 api_check(L, ttistable(t));
985 more = luaH_next(L, hvalue(t), L->top - 1);
986 if (more) {
987 api_incr_top(L);
988 }
989 else /* no more elements */
990 L->top -= 1; /* remove key */
991 lua_unlock(L);
992 return more;
993}
994
995
996LUA_API void lua_concat (lua_State *L, int n) {
997 lua_lock(L);
998 api_checknelems(L, n);
999 if (n >= 2) {
1000 luaC_checkGC(L);
1001 luaV_concat(L, n, cast_int(L->top - L->base) - 1);
1002 L->top -= (n-1);
1003 }
1004 else if (n == 0) { /* push empty string */
1005 setsvalue2s(L, L->top, luaS_newlstr(L, "", 0));
1006 api_incr_top(L);
1007 }
1008 /* else n == 1; nothing to do */
1009 lua_unlock(L);
1010}
1011
1012
1013LUA_API lua_Alloc lua_getallocf (lua_State *L, void **ud) {
1014 lua_Alloc f;
1015 lua_lock(L);
1016 if (ud) *ud = G(L)->ud;
1017 f = G(L)->frealloc;
1018 lua_unlock(L);
1019 return f;
1020}
1021
1022
1023LUA_API void lua_setallocf (lua_State *L, lua_Alloc f, void *ud) {
1024 lua_lock(L);
1025 G(L)->ud = ud;
1026 G(L)->frealloc = f;
1027 lua_unlock(L);
1028}
1029
1030
1031LUA_API void *lua_newuserdata (lua_State *L, size_t size) {
1032 Udata *u;
1033 lua_lock(L);
1034 luaC_checkGC(L);
1035 u = luaS_newudata(L, size, getcurrenv(L));
1036 setuvalue(L, L->top, u);
1037 api_incr_top(L);
1038 lua_unlock(L);
1039 return u + 1;
1040}
1041
1042
1043
1044
1045static const char *aux_upvalue (StkId fi, int n, TValue **val) {
1046 Closure *f;
1047 if (!ttisfunction(fi)) return NULL;
1048 f = clvalue(fi);
1049 if (f->c.isC) {
1050 if (!(1 <= n && n <= f->c.nupvalues)) return NULL;
1051 *val = &f->c.upvalue[n-1];
1052 return "";
1053 }
1054 else {
1055 Proto *p = f->l.p;
1056 if (!(1 <= n && n <= p->sizeupvalues)) return NULL;
1057 *val = f->l.upvals[n-1]->v;
1058 return getstr(p->upvalues[n-1]);
1059 }
1060}
1061
1062
1063LUA_API const char *lua_getupvalue (lua_State *L, int funcindex, int n) {
1064 const char *name;
1065 TValue *val;
1066 lua_lock(L);
1067 name = aux_upvalue(index2adr(L, funcindex), n, &val);
1068 if (name) {
1069 setobj2s(L, L->top, val);
1070 api_incr_top(L);
1071 }
1072 lua_unlock(L);
1073 return name;
1074}
1075
1076
1077LUA_API const char *lua_setupvalue (lua_State *L, int funcindex, int n) {
1078 const char *name;
1079 TValue *val;
1080 StkId fi;
1081 lua_lock(L);
1082 fi = index2adr(L, funcindex);
1083 api_checknelems(L, 1);
1084 name = aux_upvalue(fi, n, &val);
1085 if (name) {
1086 L->top--;
1087 setobj(L, val, L->top);
1088 luaC_barrier(L, clvalue(fi), L->top);
1089 }
1090 lua_unlock(L);
1091 return name;
1092}
1093
1094LUA_API void lua_enablereadonlytable (lua_State *L, int objindex, int enabled) {
1095 const TValue* o = index2adr(L, objindex);
1096 api_check(L, ttistable(o));
1097 Table* t = hvalue(o);
1098 api_check(L, t != hvalue(registry(L)));
1099 t->readonly = enabled;
1100}
1101
1102LUA_API int lua_isreadonlytable (lua_State *L, int objindex) {
1103 const TValue* o = index2adr(L, objindex);
1104 api_check(L, ttistable(o));
1105 Table* t = hvalue(o);
1106 api_check(L, t != hvalue(registry(L)));
1107 return t->readonly;
1108}
1109
diff --git a/examples/redis-unstable/deps/lua/src/lapi.h b/examples/redis-unstable/deps/lua/src/lapi.h
deleted file mode 100644
index 2c3fab2..0000000
--- a/examples/redis-unstable/deps/lua/src/lapi.h
+++ /dev/null
@@ -1,16 +0,0 @@
1/*
2** $Id: lapi.h,v 2.2.1.1 2007/12/27 13:02:25 roberto Exp $
3** Auxiliary functions from Lua API
4** See Copyright Notice in lua.h
5*/
6
7#ifndef lapi_h
8#define lapi_h
9
10
11#include "lobject.h"
12
13
14LUAI_FUNC void luaA_pushobject (lua_State *L, const TValue *o);
15
16#endif
diff --git a/examples/redis-unstable/deps/lua/src/lauxlib.c b/examples/redis-unstable/deps/lua/src/lauxlib.c
deleted file mode 100644
index 751f1e8..0000000
--- a/examples/redis-unstable/deps/lua/src/lauxlib.c
+++ /dev/null
@@ -1,652 +0,0 @@
1/*
2** $Id: lauxlib.c,v 1.159.1.3 2008/01/21 13:20:51 roberto Exp $
3** Auxiliary functions for building Lua libraries
4** See Copyright Notice in lua.h
5*/
6
7
8#include <ctype.h>
9#include <errno.h>
10#include <stdarg.h>
11#include <stdio.h>
12#include <stdlib.h>
13#include <string.h>
14
15
16/* This file uses only the official API of Lua.
17** Any function declared here could be written as an application function.
18*/
19
20#define lauxlib_c
21#define LUA_LIB
22
23#include "lua.h"
24
25#include "lauxlib.h"
26
27
28#define FREELIST_REF 0 /* free list of references */
29
30
31/* convert a stack index to positive */
32#define abs_index(L, i) ((i) > 0 || (i) <= LUA_REGISTRYINDEX ? (i) : \
33 lua_gettop(L) + (i) + 1)
34
35
36/*
37** {======================================================
38** Error-report functions
39** =======================================================
40*/
41
42
43LUALIB_API int luaL_argerror (lua_State *L, int narg, const char *extramsg) {
44 lua_Debug ar;
45 if (!lua_getstack(L, 0, &ar)) /* no stack frame? */
46 return luaL_error(L, "bad argument #%d (%s)", narg, extramsg);
47 lua_getinfo(L, "n", &ar);
48 if (strcmp(ar.namewhat, "method") == 0) {
49 narg--; /* do not count `self' */
50 if (narg == 0) /* error is in the self argument itself? */
51 return luaL_error(L, "calling " LUA_QS " on bad self (%s)",
52 ar.name, extramsg);
53 }
54 if (ar.name == NULL)
55 ar.name = "?";
56 return luaL_error(L, "bad argument #%d to " LUA_QS " (%s)",
57 narg, ar.name, extramsg);
58}
59
60
61LUALIB_API int luaL_typerror (lua_State *L, int narg, const char *tname) {
62 const char *msg = lua_pushfstring(L, "%s expected, got %s",
63 tname, luaL_typename(L, narg));
64 return luaL_argerror(L, narg, msg);
65}
66
67
68static void tag_error (lua_State *L, int narg, int tag) {
69 luaL_typerror(L, narg, lua_typename(L, tag));
70}
71
72
73LUALIB_API void luaL_where (lua_State *L, int level) {
74 lua_Debug ar;
75 if (lua_getstack(L, level, &ar)) { /* check function at level */
76 lua_getinfo(L, "Sl", &ar); /* get info about it */
77 if (ar.currentline > 0) { /* is there info? */
78 lua_pushfstring(L, "%s:%d: ", ar.short_src, ar.currentline);
79 return;
80 }
81 }
82 lua_pushliteral(L, ""); /* else, no information available... */
83}
84
85
86LUALIB_API int luaL_error (lua_State *L, const char *fmt, ...) {
87 va_list argp;
88 va_start(argp, fmt);
89 luaL_where(L, 1);
90 lua_pushvfstring(L, fmt, argp);
91 va_end(argp);
92 lua_concat(L, 2);
93 return lua_error(L);
94}
95
96/* }====================================================== */
97
98
99LUALIB_API int luaL_checkoption (lua_State *L, int narg, const char *def,
100 const char *const lst[]) {
101 const char *name = (def) ? luaL_optstring(L, narg, def) :
102 luaL_checkstring(L, narg);
103 int i;
104 for (i=0; lst[i]; i++)
105 if (strcmp(lst[i], name) == 0)
106 return i;
107 return luaL_argerror(L, narg,
108 lua_pushfstring(L, "invalid option " LUA_QS, name));
109}
110
111
112LUALIB_API int luaL_newmetatable (lua_State *L, const char *tname) {
113 lua_getfield(L, LUA_REGISTRYINDEX, tname); /* get registry.name */
114 if (!lua_isnil(L, -1)) /* name already in use? */
115 return 0; /* leave previous value on top, but return 0 */
116 lua_pop(L, 1);
117 lua_newtable(L); /* create metatable */
118 lua_pushvalue(L, -1);
119 lua_setfield(L, LUA_REGISTRYINDEX, tname); /* registry.name = metatable */
120 return 1;
121}
122
123
124LUALIB_API void *luaL_checkudata (lua_State *L, int ud, const char *tname) {
125 void *p = lua_touserdata(L, ud);
126 if (p != NULL) { /* value is a userdata? */
127 if (lua_getmetatable(L, ud)) { /* does it have a metatable? */
128 lua_getfield(L, LUA_REGISTRYINDEX, tname); /* get correct metatable */
129 if (lua_rawequal(L, -1, -2)) { /* does it have the correct mt? */
130 lua_pop(L, 2); /* remove both metatables */
131 return p;
132 }
133 }
134 }
135 luaL_typerror(L, ud, tname); /* else error */
136 return NULL; /* to avoid warnings */
137}
138
139
140LUALIB_API void luaL_checkstack (lua_State *L, int space, const char *mes) {
141 if (!lua_checkstack(L, space))
142 luaL_error(L, "stack overflow (%s)", mes);
143}
144
145
146LUALIB_API void luaL_checktype (lua_State *L, int narg, int t) {
147 if (lua_type(L, narg) != t)
148 tag_error(L, narg, t);
149}
150
151
152LUALIB_API void luaL_checkany (lua_State *L, int narg) {
153 if (lua_type(L, narg) == LUA_TNONE)
154 luaL_argerror(L, narg, "value expected");
155}
156
157
158LUALIB_API const char *luaL_checklstring (lua_State *L, int narg, size_t *len) {
159 const char *s = lua_tolstring(L, narg, len);
160 if (!s) tag_error(L, narg, LUA_TSTRING);
161 return s;
162}
163
164
165LUALIB_API const char *luaL_optlstring (lua_State *L, int narg,
166 const char *def, size_t *len) {
167 if (lua_isnoneornil(L, narg)) {
168 if (len)
169 *len = (def ? strlen(def) : 0);
170 return def;
171 }
172 else return luaL_checklstring(L, narg, len);
173}
174
175
176LUALIB_API lua_Number luaL_checknumber (lua_State *L, int narg) {
177 lua_Number d = lua_tonumber(L, narg);
178 if (d == 0 && !lua_isnumber(L, narg)) /* avoid extra test when d is not 0 */
179 tag_error(L, narg, LUA_TNUMBER);
180 return d;
181}
182
183
184LUALIB_API lua_Number luaL_optnumber (lua_State *L, int narg, lua_Number def) {
185 return luaL_opt(L, luaL_checknumber, narg, def);
186}
187
188
189LUALIB_API lua_Integer luaL_checkinteger (lua_State *L, int narg) {
190 lua_Integer d = lua_tointeger(L, narg);
191 if (d == 0 && !lua_isnumber(L, narg)) /* avoid extra test when d is not 0 */
192 tag_error(L, narg, LUA_TNUMBER);
193 return d;
194}
195
196
197LUALIB_API lua_Integer luaL_optinteger (lua_State *L, int narg,
198 lua_Integer def) {
199 return luaL_opt(L, luaL_checkinteger, narg, def);
200}
201
202
203LUALIB_API int luaL_getmetafield (lua_State *L, int obj, const char *event) {
204 if (!lua_getmetatable(L, obj)) /* no metatable? */
205 return 0;
206 lua_pushstring(L, event);
207 lua_rawget(L, -2);
208 if (lua_isnil(L, -1)) {
209 lua_pop(L, 2); /* remove metatable and metafield */
210 return 0;
211 }
212 else {
213 lua_remove(L, -2); /* remove only metatable */
214 return 1;
215 }
216}
217
218
219LUALIB_API int luaL_callmeta (lua_State *L, int obj, const char *event) {
220 obj = abs_index(L, obj);
221 if (!luaL_getmetafield(L, obj, event)) /* no metafield? */
222 return 0;
223 lua_pushvalue(L, obj);
224 lua_call(L, 1, 1);
225 return 1;
226}
227
228
229LUALIB_API void (luaL_register) (lua_State *L, const char *libname,
230 const luaL_Reg *l) {
231 luaI_openlib(L, libname, l, 0);
232}
233
234
235static int libsize (const luaL_Reg *l) {
236 int size = 0;
237 for (; l->name; l++) size++;
238 return size;
239}
240
241
242LUALIB_API void luaI_openlib (lua_State *L, const char *libname,
243 const luaL_Reg *l, int nup) {
244 if (libname) {
245 int size = libsize(l);
246 /* check whether lib already exists */
247 luaL_findtable(L, LUA_REGISTRYINDEX, "_LOADED", 1);
248 lua_getfield(L, -1, libname); /* get _LOADED[libname] */
249 if (!lua_istable(L, -1)) { /* not found? */
250 lua_pop(L, 1); /* remove previous result */
251 /* try global variable (and create one if it does not exist) */
252 if (luaL_findtable(L, LUA_GLOBALSINDEX, libname, size) != NULL)
253 luaL_error(L, "name conflict for module " LUA_QS, libname);
254 lua_pushvalue(L, -1);
255 lua_setfield(L, -3, libname); /* _LOADED[libname] = new table */
256 }
257 lua_remove(L, -2); /* remove _LOADED table */
258 lua_insert(L, -(nup+1)); /* move library table to below upvalues */
259 }
260 for (; l->name; l++) {
261 int i;
262 for (i=0; i<nup; i++) /* copy upvalues to the top */
263 lua_pushvalue(L, -nup);
264 lua_pushcclosure(L, l->func, nup);
265 lua_setfield(L, -(nup+2), l->name);
266 }
267 lua_pop(L, nup); /* remove upvalues */
268}
269
270
271
272/*
273** {======================================================
274** getn-setn: size for arrays
275** =======================================================
276*/
277
278#if defined(LUA_COMPAT_GETN)
279
280static int checkint (lua_State *L, int topop) {
281 int n = (lua_type(L, -1) == LUA_TNUMBER) ? lua_tointeger(L, -1) : -1;
282 lua_pop(L, topop);
283 return n;
284}
285
286
287static void getsizes (lua_State *L) {
288 lua_getfield(L, LUA_REGISTRYINDEX, "LUA_SIZES");
289 if (lua_isnil(L, -1)) { /* no `size' table? */
290 lua_pop(L, 1); /* remove nil */
291 lua_newtable(L); /* create it */
292 lua_pushvalue(L, -1); /* `size' will be its own metatable */
293 lua_setmetatable(L, -2);
294 lua_pushliteral(L, "kv");
295 lua_setfield(L, -2, "__mode"); /* metatable(N).__mode = "kv" */
296 lua_pushvalue(L, -1);
297 lua_setfield(L, LUA_REGISTRYINDEX, "LUA_SIZES"); /* store in register */
298 }
299}
300
301
302LUALIB_API void luaL_setn (lua_State *L, int t, int n) {
303 t = abs_index(L, t);
304 lua_pushliteral(L, "n");
305 lua_rawget(L, t);
306 if (checkint(L, 1) >= 0) { /* is there a numeric field `n'? */
307 lua_pushliteral(L, "n"); /* use it */
308 lua_pushinteger(L, n);
309 lua_rawset(L, t);
310 }
311 else { /* use `sizes' */
312 getsizes(L);
313 lua_pushvalue(L, t);
314 lua_pushinteger(L, n);
315 lua_rawset(L, -3); /* sizes[t] = n */
316 lua_pop(L, 1); /* remove `sizes' */
317 }
318}
319
320
321LUALIB_API int luaL_getn (lua_State *L, int t) {
322 int n;
323 t = abs_index(L, t);
324 lua_pushliteral(L, "n"); /* try t.n */
325 lua_rawget(L, t);
326 if ((n = checkint(L, 1)) >= 0) return n;
327 getsizes(L); /* else try sizes[t] */
328 lua_pushvalue(L, t);
329 lua_rawget(L, -2);
330 if ((n = checkint(L, 2)) >= 0) return n;
331 return (int)lua_objlen(L, t);
332}
333
334#endif
335
336/* }====================================================== */
337
338
339
340LUALIB_API const char *luaL_gsub (lua_State *L, const char *s, const char *p,
341 const char *r) {
342 const char *wild;
343 size_t l = strlen(p);
344 luaL_Buffer b;
345 luaL_buffinit(L, &b);
346 while ((wild = strstr(s, p)) != NULL) {
347 luaL_addlstring(&b, s, wild - s); /* push prefix */
348 luaL_addstring(&b, r); /* push replacement in place of pattern */
349 s = wild + l; /* continue after `p' */
350 }
351 luaL_addstring(&b, s); /* push last suffix */
352 luaL_pushresult(&b);
353 return lua_tostring(L, -1);
354}
355
356
357LUALIB_API const char *luaL_findtable (lua_State *L, int idx,
358 const char *fname, int szhint) {
359 const char *e;
360 lua_pushvalue(L, idx);
361 do {
362 e = strchr(fname, '.');
363 if (e == NULL) e = fname + strlen(fname);
364 lua_pushlstring(L, fname, e - fname);
365 lua_rawget(L, -2);
366 if (lua_isnil(L, -1)) { /* no such field? */
367 lua_pop(L, 1); /* remove this nil */
368 lua_createtable(L, 0, (*e == '.' ? 1 : szhint)); /* new table for field */
369 lua_pushlstring(L, fname, e - fname);
370 lua_pushvalue(L, -2);
371 lua_settable(L, -4); /* set new table into field */
372 }
373 else if (!lua_istable(L, -1)) { /* field has a non-table value? */
374 lua_pop(L, 2); /* remove table and value */
375 return fname; /* return problematic part of the name */
376 }
377 lua_remove(L, -2); /* remove previous table */
378 fname = e + 1;
379 } while (*e == '.');
380 return NULL;
381}
382
383
384
385/*
386** {======================================================
387** Generic Buffer manipulation
388** =======================================================
389*/
390
391
392#define bufflen(B) ((B)->p - (B)->buffer)
393#define bufffree(B) ((size_t)(LUAL_BUFFERSIZE - bufflen(B)))
394
395#define LIMIT (LUA_MINSTACK/2)
396
397
398static int emptybuffer (luaL_Buffer *B) {
399 size_t l = bufflen(B);
400 if (l == 0) return 0; /* put nothing on stack */
401 else {
402 lua_pushlstring(B->L, B->buffer, l);
403 B->p = B->buffer;
404 B->lvl++;
405 return 1;
406 }
407}
408
409
410static void adjuststack (luaL_Buffer *B) {
411 if (B->lvl > 1) {
412 lua_State *L = B->L;
413 int toget = 1; /* number of levels to concat */
414 size_t toplen = lua_strlen(L, -1);
415 do {
416 size_t l = lua_strlen(L, -(toget+1));
417 if (B->lvl - toget + 1 >= LIMIT || toplen > l) {
418 toplen += l;
419 toget++;
420 }
421 else break;
422 } while (toget < B->lvl);
423 lua_concat(L, toget);
424 B->lvl = B->lvl - toget + 1;
425 }
426}
427
428
429LUALIB_API char *luaL_prepbuffer (luaL_Buffer *B) {
430 if (emptybuffer(B))
431 adjuststack(B);
432 return B->buffer;
433}
434
435
436LUALIB_API void luaL_addlstring (luaL_Buffer *B, const char *s, size_t l) {
437 while (l--)
438 luaL_addchar(B, *s++);
439}
440
441
442LUALIB_API void luaL_addstring (luaL_Buffer *B, const char *s) {
443 luaL_addlstring(B, s, strlen(s));
444}
445
446
447LUALIB_API void luaL_pushresult (luaL_Buffer *B) {
448 emptybuffer(B);
449 lua_concat(B->L, B->lvl);
450 B->lvl = 1;
451}
452
453
454LUALIB_API void luaL_addvalue (luaL_Buffer *B) {
455 lua_State *L = B->L;
456 size_t vl;
457 const char *s = lua_tolstring(L, -1, &vl);
458 if (vl <= bufffree(B)) { /* fit into buffer? */
459 memcpy(B->p, s, vl); /* put it there */
460 B->p += vl;
461 lua_pop(L, 1); /* remove from stack */
462 }
463 else {
464 if (emptybuffer(B))
465 lua_insert(L, -2); /* put buffer before new value */
466 B->lvl++; /* add new value into B stack */
467 adjuststack(B);
468 }
469}
470
471
472LUALIB_API void luaL_buffinit (lua_State *L, luaL_Buffer *B) {
473 B->L = L;
474 B->p = B->buffer;
475 B->lvl = 0;
476}
477
478/* }====================================================== */
479
480
481LUALIB_API int luaL_ref (lua_State *L, int t) {
482 int ref;
483 t = abs_index(L, t);
484 if (lua_isnil(L, -1)) {
485 lua_pop(L, 1); /* remove from stack */
486 return LUA_REFNIL; /* `nil' has a unique fixed reference */
487 }
488 lua_rawgeti(L, t, FREELIST_REF); /* get first free element */
489 ref = (int)lua_tointeger(L, -1); /* ref = t[FREELIST_REF] */
490 lua_pop(L, 1); /* remove it from stack */
491 if (ref != 0) { /* any free element? */
492 lua_rawgeti(L, t, ref); /* remove it from list */
493 lua_rawseti(L, t, FREELIST_REF); /* (t[FREELIST_REF] = t[ref]) */
494 }
495 else { /* no free elements */
496 ref = (int)lua_objlen(L, t);
497 ref++; /* create new reference */
498 }
499 lua_rawseti(L, t, ref);
500 return ref;
501}
502
503
504LUALIB_API void luaL_unref (lua_State *L, int t, int ref) {
505 if (ref >= 0) {
506 t = abs_index(L, t);
507 lua_rawgeti(L, t, FREELIST_REF);
508 lua_rawseti(L, t, ref); /* t[ref] = t[FREELIST_REF] */
509 lua_pushinteger(L, ref);
510 lua_rawseti(L, t, FREELIST_REF); /* t[FREELIST_REF] = ref */
511 }
512}
513
514
515
516/*
517** {======================================================
518** Load functions
519** =======================================================
520*/
521
522typedef struct LoadF {
523 int extraline;
524 FILE *f;
525 char buff[LUAL_BUFFERSIZE];
526} LoadF;
527
528
529static const char *getF (lua_State *L, void *ud, size_t *size) {
530 LoadF *lf = (LoadF *)ud;
531 (void)L;
532 if (lf->extraline) {
533 lf->extraline = 0;
534 *size = 1;
535 return "\n";
536 }
537 if (feof(lf->f)) return NULL;
538 *size = fread(lf->buff, 1, sizeof(lf->buff), lf->f);
539 return (*size > 0) ? lf->buff : NULL;
540}
541
542
543static int errfile (lua_State *L, const char *what, int fnameindex) {
544 const char *serr = strerror(errno);
545 const char *filename = lua_tostring(L, fnameindex) + 1;
546 lua_pushfstring(L, "cannot %s %s: %s", what, filename, serr);
547 lua_remove(L, fnameindex);
548 return LUA_ERRFILE;
549}
550
551
552LUALIB_API int luaL_loadfile (lua_State *L, const char *filename) {
553 LoadF lf;
554 int status, readstatus;
555 int c;
556 int fnameindex = lua_gettop(L) + 1; /* index of filename on the stack */
557 lf.extraline = 0;
558 if (filename == NULL) {
559 lua_pushliteral(L, "=stdin");
560 lf.f = stdin;
561 }
562 else {
563 lua_pushfstring(L, "@%s", filename);
564 lf.f = fopen(filename, "r");
565 if (lf.f == NULL) return errfile(L, "open", fnameindex);
566 }
567 c = getc(lf.f);
568 if (c == '#') { /* Unix exec. file? */
569 lf.extraline = 1;
570 while ((c = getc(lf.f)) != EOF && c != '\n') ; /* skip first line */
571 if (c == '\n') c = getc(lf.f);
572 }
573 if (c == LUA_SIGNATURE[0] && filename) { /* binary file? */
574 lf.f = freopen(filename, "rb", lf.f); /* reopen in binary mode */
575 if (lf.f == NULL) return errfile(L, "reopen", fnameindex);
576 /* skip eventual `#!...' */
577 while ((c = getc(lf.f)) != EOF && c != LUA_SIGNATURE[0]) ;
578 lf.extraline = 0;
579 }
580 ungetc(c, lf.f);
581 status = lua_load(L, getF, &lf, lua_tostring(L, -1));
582 readstatus = ferror(lf.f);
583 if (filename) fclose(lf.f); /* close file (even in case of errors) */
584 if (readstatus) {
585 lua_settop(L, fnameindex); /* ignore results from `lua_load' */
586 return errfile(L, "read", fnameindex);
587 }
588 lua_remove(L, fnameindex);
589 return status;
590}
591
592
593typedef struct LoadS {
594 const char *s;
595 size_t size;
596} LoadS;
597
598
599static const char *getS (lua_State *L, void *ud, size_t *size) {
600 LoadS *ls = (LoadS *)ud;
601 (void)L;
602 if (ls->size == 0) return NULL;
603 *size = ls->size;
604 ls->size = 0;
605 return ls->s;
606}
607
608
609LUALIB_API int luaL_loadbuffer (lua_State *L, const char *buff, size_t size,
610 const char *name) {
611 LoadS ls;
612 ls.s = buff;
613 ls.size = size;
614 return lua_load(L, getS, &ls, name);
615}
616
617
618LUALIB_API int (luaL_loadstring) (lua_State *L, const char *s) {
619 return luaL_loadbuffer(L, s, strlen(s), s);
620}
621
622
623
624/* }====================================================== */
625
626
627static void *l_alloc (void *ud, void *ptr, size_t osize, size_t nsize) {
628 (void)ud;
629 (void)osize;
630 if (nsize == 0) {
631 free(ptr);
632 return NULL;
633 }
634 else
635 return realloc(ptr, nsize);
636}
637
638
639static int panic (lua_State *L) {
640 (void)L; /* to avoid warnings */
641 fprintf(stderr, "PANIC: unprotected error in call to Lua API (%s)\n",
642 lua_tostring(L, -1));
643 return 0;
644}
645
646
647LUALIB_API lua_State *luaL_newstate (void) {
648 lua_State *L = lua_newstate(l_alloc, NULL);
649 if (L) lua_atpanic(L, &panic);
650 return L;
651}
652
diff --git a/examples/redis-unstable/deps/lua/src/lauxlib.h b/examples/redis-unstable/deps/lua/src/lauxlib.h
deleted file mode 100644
index 3425823..0000000
--- a/examples/redis-unstable/deps/lua/src/lauxlib.h
+++ /dev/null
@@ -1,174 +0,0 @@
1/*
2** $Id: lauxlib.h,v 1.88.1.1 2007/12/27 13:02:25 roberto Exp $
3** Auxiliary functions for building Lua libraries
4** See Copyright Notice in lua.h
5*/
6
7
8#ifndef lauxlib_h
9#define lauxlib_h
10
11
12#include <stddef.h>
13#include <stdio.h>
14
15#include "lua.h"
16
17
18#if defined(LUA_COMPAT_GETN)
19LUALIB_API int (luaL_getn) (lua_State *L, int t);
20LUALIB_API void (luaL_setn) (lua_State *L, int t, int n);
21#else
22#define luaL_getn(L,i) ((int)lua_objlen(L, i))
23#define luaL_setn(L,i,j) ((void)0) /* no op! */
24#endif
25
26#if defined(LUA_COMPAT_OPENLIB)
27#define luaI_openlib luaL_openlib
28#endif
29
30
31/* extra error code for `luaL_load' */
32#define LUA_ERRFILE (LUA_ERRERR+1)
33
34
35typedef struct luaL_Reg {
36 const char *name;
37 lua_CFunction func;
38} luaL_Reg;
39
40
41
42LUALIB_API void (luaI_openlib) (lua_State *L, const char *libname,
43 const luaL_Reg *l, int nup);
44LUALIB_API void (luaL_register) (lua_State *L, const char *libname,
45 const luaL_Reg *l);
46LUALIB_API int (luaL_getmetafield) (lua_State *L, int obj, const char *e);
47LUALIB_API int (luaL_callmeta) (lua_State *L, int obj, const char *e);
48LUALIB_API int (luaL_typerror) (lua_State *L, int narg, const char *tname);
49LUALIB_API int (luaL_argerror) (lua_State *L, int numarg, const char *extramsg);
50LUALIB_API const char *(luaL_checklstring) (lua_State *L, int numArg,
51 size_t *l);
52LUALIB_API const char *(luaL_optlstring) (lua_State *L, int numArg,
53 const char *def, size_t *l);
54LUALIB_API lua_Number (luaL_checknumber) (lua_State *L, int numArg);
55LUALIB_API lua_Number (luaL_optnumber) (lua_State *L, int nArg, lua_Number def);
56
57LUALIB_API lua_Integer (luaL_checkinteger) (lua_State *L, int numArg);
58LUALIB_API lua_Integer (luaL_optinteger) (lua_State *L, int nArg,
59 lua_Integer def);
60
61LUALIB_API void (luaL_checkstack) (lua_State *L, int sz, const char *msg);
62LUALIB_API void (luaL_checktype) (lua_State *L, int narg, int t);
63LUALIB_API void (luaL_checkany) (lua_State *L, int narg);
64
65LUALIB_API int (luaL_newmetatable) (lua_State *L, const char *tname);
66LUALIB_API void *(luaL_checkudata) (lua_State *L, int ud, const char *tname);
67
68LUALIB_API void (luaL_where) (lua_State *L, int lvl);
69LUALIB_API int (luaL_error) (lua_State *L, const char *fmt, ...);
70
71LUALIB_API int (luaL_checkoption) (lua_State *L, int narg, const char *def,
72 const char *const lst[]);
73
74LUALIB_API int (luaL_ref) (lua_State *L, int t);
75LUALIB_API void (luaL_unref) (lua_State *L, int t, int ref);
76
77LUALIB_API int (luaL_loadfile) (lua_State *L, const char *filename);
78LUALIB_API int (luaL_loadbuffer) (lua_State *L, const char *buff, size_t sz,
79 const char *name);
80LUALIB_API int (luaL_loadstring) (lua_State *L, const char *s);
81
82LUALIB_API lua_State *(luaL_newstate) (void);
83
84
85LUALIB_API const char *(luaL_gsub) (lua_State *L, const char *s, const char *p,
86 const char *r);
87
88LUALIB_API const char *(luaL_findtable) (lua_State *L, int idx,
89 const char *fname, int szhint);
90
91
92
93
94/*
95** ===============================================================
96** some useful macros
97** ===============================================================
98*/
99
100#define luaL_argcheck(L, cond,numarg,extramsg) \
101 ((void)((cond) || luaL_argerror(L, (numarg), (extramsg))))
102#define luaL_checkstring(L,n) (luaL_checklstring(L, (n), NULL))
103#define luaL_optstring(L,n,d) (luaL_optlstring(L, (n), (d), NULL))
104#define luaL_checkint(L,n) ((int)luaL_checkinteger(L, (n)))
105#define luaL_optint(L,n,d) ((int)luaL_optinteger(L, (n), (d)))
106#define luaL_checklong(L,n) ((long)luaL_checkinteger(L, (n)))
107#define luaL_optlong(L,n,d) ((long)luaL_optinteger(L, (n), (d)))
108
109#define luaL_typename(L,i) lua_typename(L, lua_type(L,(i)))
110
111#define luaL_dofile(L, fn) \
112 (luaL_loadfile(L, fn) || lua_pcall(L, 0, LUA_MULTRET, 0))
113
114#define luaL_dostring(L, s) \
115 (luaL_loadstring(L, s) || lua_pcall(L, 0, LUA_MULTRET, 0))
116
117#define luaL_getmetatable(L,n) (lua_getfield(L, LUA_REGISTRYINDEX, (n)))
118
119#define luaL_opt(L,f,n,d) (lua_isnoneornil(L,(n)) ? (d) : f(L,(n)))
120
121/*
122** {======================================================
123** Generic Buffer manipulation
124** =======================================================
125*/
126
127
128
129typedef struct luaL_Buffer {
130 char *p; /* current position in buffer */
131 int lvl; /* number of strings in the stack (level) */
132 lua_State *L;
133 char buffer[LUAL_BUFFERSIZE];
134} luaL_Buffer;
135
136#define luaL_addchar(B,c) \
137 ((void)((B)->p < ((B)->buffer+LUAL_BUFFERSIZE) || luaL_prepbuffer(B)), \
138 (*(B)->p++ = (char)(c)))
139
140/* compatibility only */
141#define luaL_putchar(B,c) luaL_addchar(B,c)
142
143#define luaL_addsize(B,n) ((B)->p += (n))
144
145LUALIB_API void (luaL_buffinit) (lua_State *L, luaL_Buffer *B);
146LUALIB_API char *(luaL_prepbuffer) (luaL_Buffer *B);
147LUALIB_API void (luaL_addlstring) (luaL_Buffer *B, const char *s, size_t l);
148LUALIB_API void (luaL_addstring) (luaL_Buffer *B, const char *s);
149LUALIB_API void (luaL_addvalue) (luaL_Buffer *B);
150LUALIB_API void (luaL_pushresult) (luaL_Buffer *B);
151
152
153/* }====================================================== */
154
155
156/* compatibility with ref system */
157
158/* pre-defined references */
159#define LUA_NOREF (-2)
160#define LUA_REFNIL (-1)
161
162#define lua_ref(L,lock) ((lock) ? luaL_ref(L, LUA_REGISTRYINDEX) : \
163 (lua_pushstring(L, "unlocked references are obsolete"), lua_error(L), 0))
164
165#define lua_unref(L,ref) luaL_unref(L, LUA_REGISTRYINDEX, (ref))
166
167#define lua_getref(L,ref) lua_rawgeti(L, LUA_REGISTRYINDEX, (ref))
168
169
170#define luaL_reg luaL_Reg
171
172#endif
173
174
diff --git a/examples/redis-unstable/deps/lua/src/lbaselib.c b/examples/redis-unstable/deps/lua/src/lbaselib.c
deleted file mode 100644
index 26172d1..0000000
--- a/examples/redis-unstable/deps/lua/src/lbaselib.c
+++ /dev/null
@@ -1,654 +0,0 @@
1/*
2** $Id: lbaselib.c,v 1.191.1.6 2008/02/14 16:46:22 roberto Exp $
3** Basic library
4** See Copyright Notice in lua.h
5*/
6
7
8
9#include <ctype.h>
10#include <stdio.h>
11#include <stdlib.h>
12#include <string.h>
13
14#define lbaselib_c
15#define LUA_LIB
16
17#include "lua.h"
18
19#include "lauxlib.h"
20#include "lualib.h"
21
22
23
24
25/*
26** If your system does not support `stdout', you can just remove this function.
27** If you need, you can define your own `print' function, following this
28** model but changing `fputs' to put the strings at a proper place
29** (a console window or a log file, for instance).
30*/
31static int luaB_print (lua_State *L) {
32 int n = lua_gettop(L); /* number of arguments */
33 int i;
34 lua_getglobal(L, "tostring");
35 for (i=1; i<=n; i++) {
36 const char *s;
37 lua_pushvalue(L, -1); /* function to be called */
38 lua_pushvalue(L, i); /* value to print */
39 lua_call(L, 1, 1);
40 s = lua_tostring(L, -1); /* get result */
41 if (s == NULL)
42 return luaL_error(L, LUA_QL("tostring") " must return a string to "
43 LUA_QL("print"));
44 if (i>1) fputs("\t", stdout);
45 fputs(s, stdout);
46 lua_pop(L, 1); /* pop result */
47 }
48 fputs("\n", stdout);
49 return 0;
50}
51
52
53static int luaB_tonumber (lua_State *L) {
54 int base = luaL_optint(L, 2, 10);
55 if (base == 10) { /* standard conversion */
56 luaL_checkany(L, 1);
57 if (lua_isnumber(L, 1)) {
58 lua_pushnumber(L, lua_tonumber(L, 1));
59 return 1;
60 }
61 }
62 else {
63 const char *s1 = luaL_checkstring(L, 1);
64 char *s2;
65 unsigned long n;
66 luaL_argcheck(L, 2 <= base && base <= 36, 2, "base out of range");
67 n = strtoul(s1, &s2, base);
68 if (s1 != s2) { /* at least one valid digit? */
69 while (isspace((unsigned char)(*s2))) s2++; /* skip trailing spaces */
70 if (*s2 == '\0') { /* no invalid trailing characters? */
71 lua_pushnumber(L, (lua_Number)n);
72 return 1;
73 }
74 }
75 }
76 lua_pushnil(L); /* else not a number */
77 return 1;
78}
79
80
81static int luaB_error (lua_State *L) {
82 int level = luaL_optint(L, 2, 1);
83 lua_settop(L, 1);
84 if (lua_isstring(L, 1) && level > 0) { /* add extra information? */
85 luaL_where(L, level);
86 lua_pushvalue(L, 1);
87 lua_concat(L, 2);
88 }
89 return lua_error(L);
90}
91
92
93static int luaB_getmetatable (lua_State *L) {
94 luaL_checkany(L, 1);
95 if (!lua_getmetatable(L, 1)) {
96 lua_pushnil(L);
97 return 1; /* no metatable */
98 }
99 luaL_getmetafield(L, 1, "__metatable");
100 return 1; /* returns either __metatable field (if present) or metatable */
101}
102
103
104static int luaB_setmetatable (lua_State *L) {
105 int t = lua_type(L, 2);
106 luaL_checktype(L, 1, LUA_TTABLE);
107 luaL_argcheck(L, t == LUA_TNIL || t == LUA_TTABLE, 2,
108 "nil or table expected");
109 if (luaL_getmetafield(L, 1, "__metatable"))
110 luaL_error(L, "cannot change a protected metatable");
111 lua_settop(L, 2);
112 lua_setmetatable(L, 1);
113 return 1;
114}
115
116
117static void getfunc (lua_State *L, int opt) {
118 if (lua_isfunction(L, 1)) lua_pushvalue(L, 1);
119 else {
120 lua_Debug ar;
121 int level = opt ? luaL_optint(L, 1, 1) : luaL_checkint(L, 1);
122 luaL_argcheck(L, level >= 0, 1, "level must be non-negative");
123 if (lua_getstack(L, level, &ar) == 0)
124 luaL_argerror(L, 1, "invalid level");
125 lua_getinfo(L, "f", &ar);
126 if (lua_isnil(L, -1))
127 luaL_error(L, "no function environment for tail call at level %d",
128 level);
129 }
130}
131
132
133static int luaB_getfenv (lua_State *L) {
134 getfunc(L, 1);
135 if (lua_iscfunction(L, -1)) /* is a C function? */
136 lua_pushvalue(L, LUA_GLOBALSINDEX); /* return the thread's global env. */
137 else
138 lua_getfenv(L, -1);
139 return 1;
140}
141
142
143static int luaB_setfenv (lua_State *L) {
144 luaL_checktype(L, 2, LUA_TTABLE);
145 getfunc(L, 0);
146 lua_pushvalue(L, 2);
147 if (lua_isnumber(L, 1) && lua_tonumber(L, 1) == 0) {
148 /* change environment of current thread */
149 lua_pushthread(L);
150 lua_insert(L, -2);
151 lua_setfenv(L, -2);
152 return 0;
153 }
154 else if (lua_iscfunction(L, -2) || lua_setfenv(L, -2) == 0)
155 luaL_error(L,
156 LUA_QL("setfenv") " cannot change environment of given object");
157 return 1;
158}
159
160
161static int luaB_rawequal (lua_State *L) {
162 luaL_checkany(L, 1);
163 luaL_checkany(L, 2);
164 lua_pushboolean(L, lua_rawequal(L, 1, 2));
165 return 1;
166}
167
168
169static int luaB_rawget (lua_State *L) {
170 luaL_checktype(L, 1, LUA_TTABLE);
171 luaL_checkany(L, 2);
172 lua_settop(L, 2);
173 lua_rawget(L, 1);
174 return 1;
175}
176
177static int luaB_rawset (lua_State *L) {
178 luaL_checktype(L, 1, LUA_TTABLE);
179 luaL_checkany(L, 2);
180 luaL_checkany(L, 3);
181 lua_settop(L, 3);
182 lua_rawset(L, 1);
183 return 1;
184}
185
186
187static int luaB_gcinfo (lua_State *L) {
188 lua_pushinteger(L, lua_getgccount(L));
189 return 1;
190}
191
192
193static int luaB_collectgarbage (lua_State *L) {
194 static const char *const opts[] = {"stop", "restart", "collect",
195 "count", "step", "setpause", "setstepmul", NULL};
196 static const int optsnum[] = {LUA_GCSTOP, LUA_GCRESTART, LUA_GCCOLLECT,
197 LUA_GCCOUNT, LUA_GCSTEP, LUA_GCSETPAUSE, LUA_GCSETSTEPMUL};
198 int o = luaL_checkoption(L, 1, "collect", opts);
199 int ex = luaL_optint(L, 2, 0);
200 int res = lua_gc(L, optsnum[o], ex);
201 switch (optsnum[o]) {
202 case LUA_GCCOUNT: {
203 int b = lua_gc(L, LUA_GCCOUNTB, 0);
204 lua_pushnumber(L, res + ((lua_Number)b/1024));
205 return 1;
206 }
207 case LUA_GCSTEP: {
208 lua_pushboolean(L, res);
209 return 1;
210 }
211 default: {
212 lua_pushnumber(L, res);
213 return 1;
214 }
215 }
216}
217
218
219static int luaB_type (lua_State *L) {
220 luaL_checkany(L, 1);
221 lua_pushstring(L, luaL_typename(L, 1));
222 return 1;
223}
224
225
226static int luaB_next (lua_State *L) {
227 luaL_checktype(L, 1, LUA_TTABLE);
228 lua_settop(L, 2); /* create a 2nd argument if there isn't one */
229 if (lua_next(L, 1))
230 return 2;
231 else {
232 lua_pushnil(L);
233 return 1;
234 }
235}
236
237
238static int luaB_pairs (lua_State *L) {
239 luaL_checktype(L, 1, LUA_TTABLE);
240 lua_pushvalue(L, lua_upvalueindex(1)); /* return generator, */
241 lua_pushvalue(L, 1); /* state, */
242 lua_pushnil(L); /* and initial value */
243 return 3;
244}
245
246
247static int ipairsaux (lua_State *L) {
248 int i = luaL_checkint(L, 2);
249 luaL_checktype(L, 1, LUA_TTABLE);
250 i++; /* next value */
251 lua_pushinteger(L, i);
252 lua_rawgeti(L, 1, i);
253 return (lua_isnil(L, -1)) ? 0 : 2;
254}
255
256
257static int luaB_ipairs (lua_State *L) {
258 luaL_checktype(L, 1, LUA_TTABLE);
259 lua_pushvalue(L, lua_upvalueindex(1)); /* return generator, */
260 lua_pushvalue(L, 1); /* state, */
261 lua_pushinteger(L, 0); /* and initial value */
262 return 3;
263}
264
265
266static int load_aux (lua_State *L, int status) {
267 if (status == 0) /* OK? */
268 return 1;
269 else {
270 lua_pushnil(L);
271 lua_insert(L, -2); /* put before error message */
272 return 2; /* return nil plus error message */
273 }
274}
275
276
277static int luaB_loadstring (lua_State *L) {
278 size_t l;
279 const char *s = luaL_checklstring(L, 1, &l);
280 const char *chunkname = luaL_optstring(L, 2, s);
281 return load_aux(L, luaL_loadbuffer(L, s, l, chunkname));
282}
283
284
285static int luaB_loadfile (lua_State *L) {
286 const char *fname = luaL_optstring(L, 1, NULL);
287 return load_aux(L, luaL_loadfile(L, fname));
288}
289
290
291/*
292** Reader for generic `load' function: `lua_load' uses the
293** stack for internal stuff, so the reader cannot change the
294** stack top. Instead, it keeps its resulting string in a
295** reserved slot inside the stack.
296*/
297static const char *generic_reader (lua_State *L, void *ud, size_t *size) {
298 (void)ud; /* to avoid warnings */
299 luaL_checkstack(L, 2, "too many nested functions");
300 lua_pushvalue(L, 1); /* get function */
301 lua_call(L, 0, 1); /* call it */
302 if (lua_isnil(L, -1)) {
303 *size = 0;
304 return NULL;
305 }
306 else if (lua_isstring(L, -1)) {
307 lua_replace(L, 3); /* save string in a reserved stack slot */
308 return lua_tolstring(L, 3, size);
309 }
310 else luaL_error(L, "reader function must return a string");
311 return NULL; /* to avoid warnings */
312}
313
314
315static int luaB_load (lua_State *L) {
316 int status;
317 const char *cname = luaL_optstring(L, 2, "=(load)");
318 luaL_checktype(L, 1, LUA_TFUNCTION);
319 lua_settop(L, 3); /* function, eventual name, plus one reserved slot */
320 status = lua_load(L, generic_reader, NULL, cname);
321 return load_aux(L, status);
322}
323
324
325static int luaB_dofile (lua_State *L) {
326 const char *fname = luaL_optstring(L, 1, NULL);
327 int n = lua_gettop(L);
328 if (luaL_loadfile(L, fname) != 0) lua_error(L);
329 lua_call(L, 0, LUA_MULTRET);
330 return lua_gettop(L) - n;
331}
332
333
334static int luaB_assert (lua_State *L) {
335 luaL_checkany(L, 1);
336 if (!lua_toboolean(L, 1))
337 return luaL_error(L, "%s", luaL_optstring(L, 2, "assertion failed!"));
338 return lua_gettop(L);
339}
340
341
342static int luaB_unpack (lua_State *L) {
343 int i, e;
344 unsigned int n;
345 luaL_checktype(L, 1, LUA_TTABLE);
346 i = luaL_optint(L, 2, 1);
347 e = luaL_opt(L, luaL_checkint, 3, luaL_getn(L, 1));
348 if (i > e) return 0; /* empty range */
349 n = (unsigned int)e - (unsigned int)i; /* number of elements minus 1 */
350 if (n >= INT_MAX || !lua_checkstack(L, ++n))
351 return luaL_error(L, "too many results to unpack");
352 lua_rawgeti(L, 1, i); /* push arg[i] (avoiding overflow problems) */
353 while (i++ < e) /* push arg[i + 1...e] */
354 lua_rawgeti(L, 1, i);
355 return n;
356}
357
358
359static int luaB_select (lua_State *L) {
360 int n = lua_gettop(L);
361 if (lua_type(L, 1) == LUA_TSTRING && *lua_tostring(L, 1) == '#') {
362 lua_pushinteger(L, n-1);
363 return 1;
364 }
365 else {
366 int i = luaL_checkint(L, 1);
367 if (i < 0) i = n + i;
368 else if (i > n) i = n;
369 luaL_argcheck(L, 1 <= i, 1, "index out of range");
370 return n - i;
371 }
372}
373
374
375static int luaB_pcall (lua_State *L) {
376 int status;
377 luaL_checkany(L, 1);
378 status = lua_pcall(L, lua_gettop(L) - 1, LUA_MULTRET, 0);
379 lua_pushboolean(L, (status == 0));
380 lua_insert(L, 1);
381 return lua_gettop(L); /* return status + all results */
382}
383
384
385static int luaB_xpcall (lua_State *L) {
386 int status;
387 luaL_checkany(L, 2);
388 lua_settop(L, 2);
389 lua_insert(L, 1); /* put error function under function to be called */
390 status = lua_pcall(L, 0, LUA_MULTRET, 1);
391 lua_pushboolean(L, (status == 0));
392 lua_replace(L, 1);
393 return lua_gettop(L); /* return status + all results */
394}
395
396
397static int luaB_tostring (lua_State *L) {
398 luaL_checkany(L, 1);
399 if (luaL_callmeta(L, 1, "__tostring")) /* is there a metafield? */
400 return 1; /* use its value */
401 switch (lua_type(L, 1)) {
402 case LUA_TNUMBER:
403 lua_pushstring(L, lua_tostring(L, 1));
404 break;
405 case LUA_TSTRING:
406 lua_pushvalue(L, 1);
407 break;
408 case LUA_TBOOLEAN:
409 lua_pushstring(L, (lua_toboolean(L, 1) ? "true" : "false"));
410 break;
411 case LUA_TNIL:
412 lua_pushliteral(L, "nil");
413 break;
414 default:
415 lua_pushfstring(L, "%s: %p", luaL_typename(L, 1), lua_topointer(L, 1));
416 break;
417 }
418 return 1;
419}
420
421
422static int luaB_newproxy (lua_State *L) {
423 lua_settop(L, 1);
424 lua_newuserdata(L, 0); /* create proxy */
425 if (lua_toboolean(L, 1) == 0)
426 return 1; /* no metatable */
427 else if (lua_isboolean(L, 1)) {
428 lua_newtable(L); /* create a new metatable `m' ... */
429 lua_pushvalue(L, -1); /* ... and mark `m' as a valid metatable */
430 lua_pushboolean(L, 1);
431 lua_rawset(L, lua_upvalueindex(1)); /* weaktable[m] = true */
432 }
433 else {
434 int validproxy = 0; /* to check if weaktable[metatable(u)] == true */
435 if (lua_getmetatable(L, 1)) {
436 lua_rawget(L, lua_upvalueindex(1));
437 validproxy = lua_toboolean(L, -1);
438 lua_pop(L, 1); /* remove value */
439 }
440 luaL_argcheck(L, validproxy, 1, "boolean or proxy expected");
441 lua_getmetatable(L, 1); /* metatable is valid; get it */
442 }
443 lua_setmetatable(L, 2);
444 return 1;
445}
446
447
448static const luaL_Reg base_funcs[] = {
449 {"assert", luaB_assert},
450 {"collectgarbage", luaB_collectgarbage},
451 {"dofile", luaB_dofile},
452 {"error", luaB_error},
453 {"gcinfo", luaB_gcinfo},
454 {"getfenv", luaB_getfenv},
455 {"getmetatable", luaB_getmetatable},
456 {"loadfile", luaB_loadfile},
457 {"load", luaB_load},
458 {"loadstring", luaB_loadstring},
459 {"next", luaB_next},
460 {"pcall", luaB_pcall},
461 {"print", luaB_print},
462 {"rawequal", luaB_rawequal},
463 {"rawget", luaB_rawget},
464 {"rawset", luaB_rawset},
465 {"select", luaB_select},
466 {"setfenv", luaB_setfenv},
467 {"setmetatable", luaB_setmetatable},
468 {"tonumber", luaB_tonumber},
469 {"tostring", luaB_tostring},
470 {"type", luaB_type},
471 {"unpack", luaB_unpack},
472 {"xpcall", luaB_xpcall},
473 {NULL, NULL}
474};
475
476
477/*
478** {======================================================
479** Coroutine library
480** =======================================================
481*/
482
483#define CO_RUN 0 /* running */
484#define CO_SUS 1 /* suspended */
485#define CO_NOR 2 /* 'normal' (it resumed another coroutine) */
486#define CO_DEAD 3
487
488static const char *const statnames[] =
489 {"running", "suspended", "normal", "dead"};
490
491static int costatus (lua_State *L, lua_State *co) {
492 if (L == co) return CO_RUN;
493 switch (lua_status(co)) {
494 case LUA_YIELD:
495 return CO_SUS;
496 case 0: {
497 lua_Debug ar;
498 if (lua_getstack(co, 0, &ar) > 0) /* does it have frames? */
499 return CO_NOR; /* it is running */
500 else if (lua_gettop(co) == 0)
501 return CO_DEAD;
502 else
503 return CO_SUS; /* initial state */
504 }
505 default: /* some error occured */
506 return CO_DEAD;
507 }
508}
509
510
511static int luaB_costatus (lua_State *L) {
512 lua_State *co = lua_tothread(L, 1);
513 luaL_argcheck(L, co, 1, "coroutine expected");
514 lua_pushstring(L, statnames[costatus(L, co)]);
515 return 1;
516}
517
518
519static int auxresume (lua_State *L, lua_State *co, int narg) {
520 int status = costatus(L, co);
521 if (!lua_checkstack(co, narg))
522 luaL_error(L, "too many arguments to resume");
523 if (status != CO_SUS) {
524 lua_pushfstring(L, "cannot resume %s coroutine", statnames[status]);
525 return -1; /* error flag */
526 }
527 lua_xmove(L, co, narg);
528 lua_setlevel(L, co);
529 status = lua_resume(co, narg);
530 if (status == 0 || status == LUA_YIELD) {
531 int nres = lua_gettop(co);
532 if (!lua_checkstack(L, nres + 1))
533 luaL_error(L, "too many results to resume");
534 lua_xmove(co, L, nres); /* move yielded values */
535 return nres;
536 }
537 else {
538 lua_xmove(co, L, 1); /* move error message */
539 return -1; /* error flag */
540 }
541}
542
543
544static int luaB_coresume (lua_State *L) {
545 lua_State *co = lua_tothread(L, 1);
546 int r;
547 luaL_argcheck(L, co, 1, "coroutine expected");
548 r = auxresume(L, co, lua_gettop(L) - 1);
549 if (r < 0) {
550 lua_pushboolean(L, 0);
551 lua_insert(L, -2);
552 return 2; /* return false + error message */
553 }
554 else {
555 lua_pushboolean(L, 1);
556 lua_insert(L, -(r + 1));
557 return r + 1; /* return true + `resume' returns */
558 }
559}
560
561
562static int luaB_auxwrap (lua_State *L) {
563 lua_State *co = lua_tothread(L, lua_upvalueindex(1));
564 int r = auxresume(L, co, lua_gettop(L));
565 if (r < 0) {
566 if (lua_isstring(L, -1)) { /* error object is a string? */
567 luaL_where(L, 1); /* add extra info */
568 lua_insert(L, -2);
569 lua_concat(L, 2);
570 }
571 lua_error(L); /* propagate error */
572 }
573 return r;
574}
575
576
577static int luaB_cocreate (lua_State *L) {
578 lua_State *NL = lua_newthread(L);
579 luaL_argcheck(L, lua_isfunction(L, 1) && !lua_iscfunction(L, 1), 1,
580 "Lua function expected");
581 lua_pushvalue(L, 1); /* move function to top */
582 lua_xmove(L, NL, 1); /* move function from L to NL */
583 return 1;
584}
585
586
587static int luaB_cowrap (lua_State *L) {
588 luaB_cocreate(L);
589 lua_pushcclosure(L, luaB_auxwrap, 1);
590 return 1;
591}
592
593
594static int luaB_yield (lua_State *L) {
595 return lua_yield(L, lua_gettop(L));
596}
597
598
599static int luaB_corunning (lua_State *L) {
600 if (lua_pushthread(L))
601 lua_pushnil(L); /* main thread is not a coroutine */
602 return 1;
603}
604
605
606static const luaL_Reg co_funcs[] = {
607 {"create", luaB_cocreate},
608 {"resume", luaB_coresume},
609 {"running", luaB_corunning},
610 {"status", luaB_costatus},
611 {"wrap", luaB_cowrap},
612 {"yield", luaB_yield},
613 {NULL, NULL}
614};
615
616/* }====================================================== */
617
618
619static void auxopen (lua_State *L, const char *name,
620 lua_CFunction f, lua_CFunction u) {
621 lua_pushcfunction(L, u);
622 lua_pushcclosure(L, f, 1);
623 lua_setfield(L, -2, name);
624}
625
626
627static void base_open (lua_State *L) {
628 /* set global _G */
629 lua_pushvalue(L, LUA_GLOBALSINDEX);
630 lua_setglobal(L, "_G");
631 /* open lib into global table */
632 luaL_register(L, "_G", base_funcs);
633 lua_pushliteral(L, LUA_VERSION);
634 lua_setglobal(L, "_VERSION"); /* set global _VERSION */
635 /* `ipairs' and `pairs' need auxiliary functions as upvalues */
636 auxopen(L, "ipairs", luaB_ipairs, ipairsaux);
637 auxopen(L, "pairs", luaB_pairs, luaB_next);
638 /* `newproxy' needs a weaktable as upvalue */
639 lua_createtable(L, 0, 1); /* new table `w' */
640 lua_pushvalue(L, -1); /* `w' will be its own metatable */
641 lua_setmetatable(L, -2);
642 lua_pushliteral(L, "kv");
643 lua_setfield(L, -2, "__mode"); /* metatable(w).__mode = "kv" */
644 lua_pushcclosure(L, luaB_newproxy, 1);
645 lua_setglobal(L, "newproxy"); /* set global `newproxy' */
646}
647
648
649LUALIB_API int luaopen_base (lua_State *L) {
650 base_open(L);
651 luaL_register(L, LUA_COLIBNAME, co_funcs);
652 return 2;
653}
654
diff --git a/examples/redis-unstable/deps/lua/src/lcode.c b/examples/redis-unstable/deps/lua/src/lcode.c
deleted file mode 100644
index 679cb9c..0000000
--- a/examples/redis-unstable/deps/lua/src/lcode.c
+++ /dev/null
@@ -1,831 +0,0 @@
1/*
2** $Id: lcode.c,v 2.25.1.5 2011/01/31 14:53:16 roberto Exp $
3** Code generator for Lua
4** See Copyright Notice in lua.h
5*/
6
7
8#include <stdlib.h>
9
10#define lcode_c
11#define LUA_CORE
12
13#include "lua.h"
14
15#include "lcode.h"
16#include "ldebug.h"
17#include "ldo.h"
18#include "lgc.h"
19#include "llex.h"
20#include "lmem.h"
21#include "lobject.h"
22#include "lopcodes.h"
23#include "lparser.h"
24#include "ltable.h"
25
26
27#define hasjumps(e) ((e)->t != (e)->f)
28
29
30static int isnumeral(expdesc *e) {
31 return (e->k == VKNUM && e->t == NO_JUMP && e->f == NO_JUMP);
32}
33
34
35void luaK_nil (FuncState *fs, int from, int n) {
36 Instruction *previous;
37 if (fs->pc > fs->lasttarget) { /* no jumps to current position? */
38 if (fs->pc == 0) { /* function start? */
39 if (from >= fs->nactvar)
40 return; /* positions are already clean */
41 }
42 else {
43 previous = &fs->f->code[fs->pc-1];
44 if (GET_OPCODE(*previous) == OP_LOADNIL) {
45 int pfrom = GETARG_A(*previous);
46 int pto = GETARG_B(*previous);
47 if (pfrom <= from && from <= pto+1) { /* can connect both? */
48 if (from+n-1 > pto)
49 SETARG_B(*previous, from+n-1);
50 return;
51 }
52 }
53 }
54 }
55 luaK_codeABC(fs, OP_LOADNIL, from, from+n-1, 0); /* else no optimization */
56}
57
58
59int luaK_jump (FuncState *fs) {
60 int jpc = fs->jpc; /* save list of jumps to here */
61 int j;
62 fs->jpc = NO_JUMP;
63 j = luaK_codeAsBx(fs, OP_JMP, 0, NO_JUMP);
64 luaK_concat(fs, &j, jpc); /* keep them on hold */
65 return j;
66}
67
68
69void luaK_ret (FuncState *fs, int first, int nret) {
70 luaK_codeABC(fs, OP_RETURN, first, nret+1, 0);
71}
72
73
74static int condjump (FuncState *fs, OpCode op, int A, int B, int C) {
75 luaK_codeABC(fs, op, A, B, C);
76 return luaK_jump(fs);
77}
78
79
80static void fixjump (FuncState *fs, int pc, int dest) {
81 Instruction *jmp = &fs->f->code[pc];
82 int offset = dest-(pc+1);
83 lua_assert(dest != NO_JUMP);
84 if (abs(offset) > MAXARG_sBx)
85 luaX_syntaxerror(fs->ls, "control structure too long");
86 SETARG_sBx(*jmp, offset);
87}
88
89
90/*
91** returns current `pc' and marks it as a jump target (to avoid wrong
92** optimizations with consecutive instructions not in the same basic block).
93*/
94int luaK_getlabel (FuncState *fs) {
95 fs->lasttarget = fs->pc;
96 return fs->pc;
97}
98
99
100static int getjump (FuncState *fs, int pc) {
101 int offset = GETARG_sBx(fs->f->code[pc]);
102 if (offset == NO_JUMP) /* point to itself represents end of list */
103 return NO_JUMP; /* end of list */
104 else
105 return (pc+1)+offset; /* turn offset into absolute position */
106}
107
108
109static Instruction *getjumpcontrol (FuncState *fs, int pc) {
110 Instruction *pi = &fs->f->code[pc];
111 if (pc >= 1 && testTMode(GET_OPCODE(*(pi-1))))
112 return pi-1;
113 else
114 return pi;
115}
116
117
118/*
119** check whether list has any jump that do not produce a value
120** (or produce an inverted value)
121*/
122static int need_value (FuncState *fs, int list) {
123 for (; list != NO_JUMP; list = getjump(fs, list)) {
124 Instruction i = *getjumpcontrol(fs, list);
125 if (GET_OPCODE(i) != OP_TESTSET) return 1;
126 }
127 return 0; /* not found */
128}
129
130
131static int patchtestreg (FuncState *fs, int node, int reg) {
132 Instruction *i = getjumpcontrol(fs, node);
133 if (GET_OPCODE(*i) != OP_TESTSET)
134 return 0; /* cannot patch other instructions */
135 if (reg != NO_REG && reg != GETARG_B(*i))
136 SETARG_A(*i, reg);
137 else /* no register to put value or register already has the value */
138 *i = CREATE_ABC(OP_TEST, GETARG_B(*i), 0, GETARG_C(*i));
139
140 return 1;
141}
142
143
144static void removevalues (FuncState *fs, int list) {
145 for (; list != NO_JUMP; list = getjump(fs, list))
146 patchtestreg(fs, list, NO_REG);
147}
148
149
150static void patchlistaux (FuncState *fs, int list, int vtarget, int reg,
151 int dtarget) {
152 while (list != NO_JUMP) {
153 int next = getjump(fs, list);
154 if (patchtestreg(fs, list, reg))
155 fixjump(fs, list, vtarget);
156 else
157 fixjump(fs, list, dtarget); /* jump to default target */
158 list = next;
159 }
160}
161
162
163static void dischargejpc (FuncState *fs) {
164 patchlistaux(fs, fs->jpc, fs->pc, NO_REG, fs->pc);
165 fs->jpc = NO_JUMP;
166}
167
168
169void luaK_patchlist (FuncState *fs, int list, int target) {
170 if (target == fs->pc)
171 luaK_patchtohere(fs, list);
172 else {
173 lua_assert(target < fs->pc);
174 patchlistaux(fs, list, target, NO_REG, target);
175 }
176}
177
178
179void luaK_patchtohere (FuncState *fs, int list) {
180 luaK_getlabel(fs);
181 luaK_concat(fs, &fs->jpc, list);
182}
183
184
185void luaK_concat (FuncState *fs, int *l1, int l2) {
186 if (l2 == NO_JUMP) return;
187 else if (*l1 == NO_JUMP)
188 *l1 = l2;
189 else {
190 int list = *l1;
191 int next;
192 while ((next = getjump(fs, list)) != NO_JUMP) /* find last element */
193 list = next;
194 fixjump(fs, list, l2);
195 }
196}
197
198
199void luaK_checkstack (FuncState *fs, int n) {
200 int newstack = fs->freereg + n;
201 if (newstack > fs->f->maxstacksize) {
202 if (newstack >= MAXSTACK)
203 luaX_syntaxerror(fs->ls, "function or expression too complex");
204 fs->f->maxstacksize = cast_byte(newstack);
205 }
206}
207
208
209void luaK_reserveregs (FuncState *fs, int n) {
210 luaK_checkstack(fs, n);
211 fs->freereg += n;
212}
213
214
215static void freereg (FuncState *fs, int reg) {
216 if (!ISK(reg) && reg >= fs->nactvar) {
217 fs->freereg--;
218 lua_assert(reg == fs->freereg);
219 }
220}
221
222
223static void freeexp (FuncState *fs, expdesc *e) {
224 if (e->k == VNONRELOC)
225 freereg(fs, e->u.s.info);
226}
227
228
229static int addk (FuncState *fs, TValue *k, TValue *v) {
230 lua_State *L = fs->L;
231 TValue *idx = luaH_set(L, fs->h, k);
232 Proto *f = fs->f;
233 int oldsize = f->sizek;
234 if (ttisnumber(idx)) {
235 lua_assert(luaO_rawequalObj(&fs->f->k[cast_int(nvalue(idx))], v));
236 return cast_int(nvalue(idx));
237 }
238 else { /* constant not found; create a new entry */
239 setnvalue(idx, cast_num(fs->nk));
240 luaM_growvector(L, f->k, fs->nk, f->sizek, TValue,
241 MAXARG_Bx, "constant table overflow");
242 while (oldsize < f->sizek) setnilvalue(&f->k[oldsize++]);
243 setobj(L, &f->k[fs->nk], v);
244 luaC_barrier(L, f, v);
245 return fs->nk++;
246 }
247}
248
249
250int luaK_stringK (FuncState *fs, TString *s) {
251 TValue o;
252 setsvalue(fs->L, &o, s);
253 return addk(fs, &o, &o);
254}
255
256
257int luaK_numberK (FuncState *fs, lua_Number r) {
258 TValue o;
259 setnvalue(&o, r);
260 return addk(fs, &o, &o);
261}
262
263
264static int boolK (FuncState *fs, int b) {
265 TValue o;
266 setbvalue(&o, b);
267 return addk(fs, &o, &o);
268}
269
270
271static int nilK (FuncState *fs) {
272 TValue k, v;
273 setnilvalue(&v);
274 /* cannot use nil as key; instead use table itself to represent nil */
275 sethvalue(fs->L, &k, fs->h);
276 return addk(fs, &k, &v);
277}
278
279
280void luaK_setreturns (FuncState *fs, expdesc *e, int nresults) {
281 if (e->k == VCALL) { /* expression is an open function call? */
282 SETARG_C(getcode(fs, e), nresults+1);
283 }
284 else if (e->k == VVARARG) {
285 SETARG_B(getcode(fs, e), nresults+1);
286 SETARG_A(getcode(fs, e), fs->freereg);
287 luaK_reserveregs(fs, 1);
288 }
289}
290
291
292void luaK_setoneret (FuncState *fs, expdesc *e) {
293 if (e->k == VCALL) { /* expression is an open function call? */
294 e->k = VNONRELOC;
295 e->u.s.info = GETARG_A(getcode(fs, e));
296 }
297 else if (e->k == VVARARG) {
298 SETARG_B(getcode(fs, e), 2);
299 e->k = VRELOCABLE; /* can relocate its simple result */
300 }
301}
302
303
304void luaK_dischargevars (FuncState *fs, expdesc *e) {
305 switch (e->k) {
306 case VLOCAL: {
307 e->k = VNONRELOC;
308 break;
309 }
310 case VUPVAL: {
311 e->u.s.info = luaK_codeABC(fs, OP_GETUPVAL, 0, e->u.s.info, 0);
312 e->k = VRELOCABLE;
313 break;
314 }
315 case VGLOBAL: {
316 e->u.s.info = luaK_codeABx(fs, OP_GETGLOBAL, 0, e->u.s.info);
317 e->k = VRELOCABLE;
318 break;
319 }
320 case VINDEXED: {
321 freereg(fs, e->u.s.aux);
322 freereg(fs, e->u.s.info);
323 e->u.s.info = luaK_codeABC(fs, OP_GETTABLE, 0, e->u.s.info, e->u.s.aux);
324 e->k = VRELOCABLE;
325 break;
326 }
327 case VVARARG:
328 case VCALL: {
329 luaK_setoneret(fs, e);
330 break;
331 }
332 default: break; /* there is one value available (somewhere) */
333 }
334}
335
336
337static int code_label (FuncState *fs, int A, int b, int jump) {
338 luaK_getlabel(fs); /* those instructions may be jump targets */
339 return luaK_codeABC(fs, OP_LOADBOOL, A, b, jump);
340}
341
342
343static void discharge2reg (FuncState *fs, expdesc *e, int reg) {
344 luaK_dischargevars(fs, e);
345 switch (e->k) {
346 case VNIL: {
347 luaK_nil(fs, reg, 1);
348 break;
349 }
350 case VFALSE: case VTRUE: {
351 luaK_codeABC(fs, OP_LOADBOOL, reg, e->k == VTRUE, 0);
352 break;
353 }
354 case VK: {
355 luaK_codeABx(fs, OP_LOADK, reg, e->u.s.info);
356 break;
357 }
358 case VKNUM: {
359 luaK_codeABx(fs, OP_LOADK, reg, luaK_numberK(fs, e->u.nval));
360 break;
361 }
362 case VRELOCABLE: {
363 Instruction *pc = &getcode(fs, e);
364 SETARG_A(*pc, reg);
365 break;
366 }
367 case VNONRELOC: {
368 if (reg != e->u.s.info)
369 luaK_codeABC(fs, OP_MOVE, reg, e->u.s.info, 0);
370 break;
371 }
372 default: {
373 lua_assert(e->k == VVOID || e->k == VJMP);
374 return; /* nothing to do... */
375 }
376 }
377 e->u.s.info = reg;
378 e->k = VNONRELOC;
379}
380
381
382static void discharge2anyreg (FuncState *fs, expdesc *e) {
383 if (e->k != VNONRELOC) {
384 luaK_reserveregs(fs, 1);
385 discharge2reg(fs, e, fs->freereg-1);
386 }
387}
388
389
390static void exp2reg (FuncState *fs, expdesc *e, int reg) {
391 discharge2reg(fs, e, reg);
392 if (e->k == VJMP)
393 luaK_concat(fs, &e->t, e->u.s.info); /* put this jump in `t' list */
394 if (hasjumps(e)) {
395 int final; /* position after whole expression */
396 int p_f = NO_JUMP; /* position of an eventual LOAD false */
397 int p_t = NO_JUMP; /* position of an eventual LOAD true */
398 if (need_value(fs, e->t) || need_value(fs, e->f)) {
399 int fj = (e->k == VJMP) ? NO_JUMP : luaK_jump(fs);
400 p_f = code_label(fs, reg, 0, 1);
401 p_t = code_label(fs, reg, 1, 0);
402 luaK_patchtohere(fs, fj);
403 }
404 final = luaK_getlabel(fs);
405 patchlistaux(fs, e->f, final, reg, p_f);
406 patchlistaux(fs, e->t, final, reg, p_t);
407 }
408 e->f = e->t = NO_JUMP;
409 e->u.s.info = reg;
410 e->k = VNONRELOC;
411}
412
413
414void luaK_exp2nextreg (FuncState *fs, expdesc *e) {
415 luaK_dischargevars(fs, e);
416 freeexp(fs, e);
417 luaK_reserveregs(fs, 1);
418 exp2reg(fs, e, fs->freereg - 1);
419}
420
421
422int luaK_exp2anyreg (FuncState *fs, expdesc *e) {
423 luaK_dischargevars(fs, e);
424 if (e->k == VNONRELOC) {
425 if (!hasjumps(e)) return e->u.s.info; /* exp is already in a register */
426 if (e->u.s.info >= fs->nactvar) { /* reg. is not a local? */
427 exp2reg(fs, e, e->u.s.info); /* put value on it */
428 return e->u.s.info;
429 }
430 }
431 luaK_exp2nextreg(fs, e); /* default */
432 return e->u.s.info;
433}
434
435
436void luaK_exp2val (FuncState *fs, expdesc *e) {
437 if (hasjumps(e))
438 luaK_exp2anyreg(fs, e);
439 else
440 luaK_dischargevars(fs, e);
441}
442
443
444int luaK_exp2RK (FuncState *fs, expdesc *e) {
445 luaK_exp2val(fs, e);
446 switch (e->k) {
447 case VKNUM:
448 case VTRUE:
449 case VFALSE:
450 case VNIL: {
451 if (fs->nk <= MAXINDEXRK) { /* constant fit in RK operand? */
452 e->u.s.info = (e->k == VNIL) ? nilK(fs) :
453 (e->k == VKNUM) ? luaK_numberK(fs, e->u.nval) :
454 boolK(fs, (e->k == VTRUE));
455 e->k = VK;
456 return RKASK(e->u.s.info);
457 }
458 else break;
459 }
460 case VK: {
461 if (e->u.s.info <= MAXINDEXRK) /* constant fit in argC? */
462 return RKASK(e->u.s.info);
463 else break;
464 }
465 default: break;
466 }
467 /* not a constant in the right range: put it in a register */
468 return luaK_exp2anyreg(fs, e);
469}
470
471
472void luaK_storevar (FuncState *fs, expdesc *var, expdesc *ex) {
473 switch (var->k) {
474 case VLOCAL: {
475 freeexp(fs, ex);
476 exp2reg(fs, ex, var->u.s.info);
477 return;
478 }
479 case VUPVAL: {
480 int e = luaK_exp2anyreg(fs, ex);
481 luaK_codeABC(fs, OP_SETUPVAL, e, var->u.s.info, 0);
482 break;
483 }
484 case VGLOBAL: {
485 int e = luaK_exp2anyreg(fs, ex);
486 luaK_codeABx(fs, OP_SETGLOBAL, e, var->u.s.info);
487 break;
488 }
489 case VINDEXED: {
490 int e = luaK_exp2RK(fs, ex);
491 luaK_codeABC(fs, OP_SETTABLE, var->u.s.info, var->u.s.aux, e);
492 break;
493 }
494 default: {
495 lua_assert(0); /* invalid var kind to store */
496 break;
497 }
498 }
499 freeexp(fs, ex);
500}
501
502
503void luaK_self (FuncState *fs, expdesc *e, expdesc *key) {
504 int func;
505 luaK_exp2anyreg(fs, e);
506 freeexp(fs, e);
507 func = fs->freereg;
508 luaK_reserveregs(fs, 2);
509 luaK_codeABC(fs, OP_SELF, func, e->u.s.info, luaK_exp2RK(fs, key));
510 freeexp(fs, key);
511 e->u.s.info = func;
512 e->k = VNONRELOC;
513}
514
515
516static void invertjump (FuncState *fs, expdesc *e) {
517 Instruction *pc = getjumpcontrol(fs, e->u.s.info);
518 lua_assert(testTMode(GET_OPCODE(*pc)) && GET_OPCODE(*pc) != OP_TESTSET &&
519 GET_OPCODE(*pc) != OP_TEST);
520 SETARG_A(*pc, !(GETARG_A(*pc)));
521}
522
523
524static int jumponcond (FuncState *fs, expdesc *e, int cond) {
525 if (e->k == VRELOCABLE) {
526 Instruction ie = getcode(fs, e);
527 if (GET_OPCODE(ie) == OP_NOT) {
528 fs->pc--; /* remove previous OP_NOT */
529 return condjump(fs, OP_TEST, GETARG_B(ie), 0, !cond);
530 }
531 /* else go through */
532 }
533 discharge2anyreg(fs, e);
534 freeexp(fs, e);
535 return condjump(fs, OP_TESTSET, NO_REG, e->u.s.info, cond);
536}
537
538
539void luaK_goiftrue (FuncState *fs, expdesc *e) {
540 int pc; /* pc of last jump */
541 luaK_dischargevars(fs, e);
542 switch (e->k) {
543 case VK: case VKNUM: case VTRUE: {
544 pc = NO_JUMP; /* always true; do nothing */
545 break;
546 }
547 case VJMP: {
548 invertjump(fs, e);
549 pc = e->u.s.info;
550 break;
551 }
552 default: {
553 pc = jumponcond(fs, e, 0);
554 break;
555 }
556 }
557 luaK_concat(fs, &e->f, pc); /* insert last jump in `f' list */
558 luaK_patchtohere(fs, e->t);
559 e->t = NO_JUMP;
560}
561
562
563static void luaK_goiffalse (FuncState *fs, expdesc *e) {
564 int pc; /* pc of last jump */
565 luaK_dischargevars(fs, e);
566 switch (e->k) {
567 case VNIL: case VFALSE: {
568 pc = NO_JUMP; /* always false; do nothing */
569 break;
570 }
571 case VJMP: {
572 pc = e->u.s.info;
573 break;
574 }
575 default: {
576 pc = jumponcond(fs, e, 1);
577 break;
578 }
579 }
580 luaK_concat(fs, &e->t, pc); /* insert last jump in `t' list */
581 luaK_patchtohere(fs, e->f);
582 e->f = NO_JUMP;
583}
584
585
586static void codenot (FuncState *fs, expdesc *e) {
587 luaK_dischargevars(fs, e);
588 switch (e->k) {
589 case VNIL: case VFALSE: {
590 e->k = VTRUE;
591 break;
592 }
593 case VK: case VKNUM: case VTRUE: {
594 e->k = VFALSE;
595 break;
596 }
597 case VJMP: {
598 invertjump(fs, e);
599 break;
600 }
601 case VRELOCABLE:
602 case VNONRELOC: {
603 discharge2anyreg(fs, e);
604 freeexp(fs, e);
605 e->u.s.info = luaK_codeABC(fs, OP_NOT, 0, e->u.s.info, 0);
606 e->k = VRELOCABLE;
607 break;
608 }
609 default: {
610 lua_assert(0); /* cannot happen */
611 break;
612 }
613 }
614 /* interchange true and false lists */
615 { int temp = e->f; e->f = e->t; e->t = temp; }
616 removevalues(fs, e->f);
617 removevalues(fs, e->t);
618}
619
620
621void luaK_indexed (FuncState *fs, expdesc *t, expdesc *k) {
622 t->u.s.aux = luaK_exp2RK(fs, k);
623 t->k = VINDEXED;
624}
625
626
627static int constfolding (OpCode op, expdesc *e1, expdesc *e2) {
628 lua_Number v1, v2, r;
629 if (!isnumeral(e1) || !isnumeral(e2)) return 0;
630 v1 = e1->u.nval;
631 v2 = e2->u.nval;
632 switch (op) {
633 case OP_ADD: r = luai_numadd(v1, v2); break;
634 case OP_SUB: r = luai_numsub(v1, v2); break;
635 case OP_MUL: r = luai_nummul(v1, v2); break;
636 case OP_DIV:
637 if (v2 == 0) return 0; /* do not attempt to divide by 0 */
638 r = luai_numdiv(v1, v2); break;
639 case OP_MOD:
640 if (v2 == 0) return 0; /* do not attempt to divide by 0 */
641 r = luai_nummod(v1, v2); break;
642 case OP_POW: r = luai_numpow(v1, v2); break;
643 case OP_UNM: r = luai_numunm(v1); break;
644 case OP_LEN: return 0; /* no constant folding for 'len' */
645 default: lua_assert(0); r = 0; break;
646 }
647 if (luai_numisnan(r)) return 0; /* do not attempt to produce NaN */
648 e1->u.nval = r;
649 return 1;
650}
651
652
653static void codearith (FuncState *fs, OpCode op, expdesc *e1, expdesc *e2) {
654 if (constfolding(op, e1, e2))
655 return;
656 else {
657 int o2 = (op != OP_UNM && op != OP_LEN) ? luaK_exp2RK(fs, e2) : 0;
658 int o1 = luaK_exp2RK(fs, e1);
659 if (o1 > o2) {
660 freeexp(fs, e1);
661 freeexp(fs, e2);
662 }
663 else {
664 freeexp(fs, e2);
665 freeexp(fs, e1);
666 }
667 e1->u.s.info = luaK_codeABC(fs, op, 0, o1, o2);
668 e1->k = VRELOCABLE;
669 }
670}
671
672
673static void codecomp (FuncState *fs, OpCode op, int cond, expdesc *e1,
674 expdesc *e2) {
675 int o1 = luaK_exp2RK(fs, e1);
676 int o2 = luaK_exp2RK(fs, e2);
677 freeexp(fs, e2);
678 freeexp(fs, e1);
679 if (cond == 0 && op != OP_EQ) {
680 int temp; /* exchange args to replace by `<' or `<=' */
681 temp = o1; o1 = o2; o2 = temp; /* o1 <==> o2 */
682 cond = 1;
683 }
684 e1->u.s.info = condjump(fs, op, cond, o1, o2);
685 e1->k = VJMP;
686}
687
688
689void luaK_prefix (FuncState *fs, UnOpr op, expdesc *e) {
690 expdesc e2;
691 e2.t = e2.f = NO_JUMP; e2.k = VKNUM; e2.u.nval = 0;
692 switch (op) {
693 case OPR_MINUS: {
694 if (!isnumeral(e))
695 luaK_exp2anyreg(fs, e); /* cannot operate on non-numeric constants */
696 codearith(fs, OP_UNM, e, &e2);
697 break;
698 }
699 case OPR_NOT: codenot(fs, e); break;
700 case OPR_LEN: {
701 luaK_exp2anyreg(fs, e); /* cannot operate on constants */
702 codearith(fs, OP_LEN, e, &e2);
703 break;
704 }
705 default: lua_assert(0);
706 }
707}
708
709
710void luaK_infix (FuncState *fs, BinOpr op, expdesc *v) {
711 switch (op) {
712 case OPR_AND: {
713 luaK_goiftrue(fs, v);
714 break;
715 }
716 case OPR_OR: {
717 luaK_goiffalse(fs, v);
718 break;
719 }
720 case OPR_CONCAT: {
721 luaK_exp2nextreg(fs, v); /* operand must be on the `stack' */
722 break;
723 }
724 case OPR_ADD: case OPR_SUB: case OPR_MUL: case OPR_DIV:
725 case OPR_MOD: case OPR_POW: {
726 if (!isnumeral(v)) luaK_exp2RK(fs, v);
727 break;
728 }
729 default: {
730 luaK_exp2RK(fs, v);
731 break;
732 }
733 }
734}
735
736
737void luaK_posfix (FuncState *fs, BinOpr op, expdesc *e1, expdesc *e2) {
738 switch (op) {
739 case OPR_AND: {
740 lua_assert(e1->t == NO_JUMP); /* list must be closed */
741 luaK_dischargevars(fs, e2);
742 luaK_concat(fs, &e2->f, e1->f);
743 *e1 = *e2;
744 break;
745 }
746 case OPR_OR: {
747 lua_assert(e1->f == NO_JUMP); /* list must be closed */
748 luaK_dischargevars(fs, e2);
749 luaK_concat(fs, &e2->t, e1->t);
750 *e1 = *e2;
751 break;
752 }
753 case OPR_CONCAT: {
754 luaK_exp2val(fs, e2);
755 if (e2->k == VRELOCABLE && GET_OPCODE(getcode(fs, e2)) == OP_CONCAT) {
756 lua_assert(e1->u.s.info == GETARG_B(getcode(fs, e2))-1);
757 freeexp(fs, e1);
758 SETARG_B(getcode(fs, e2), e1->u.s.info);
759 e1->k = VRELOCABLE; e1->u.s.info = e2->u.s.info;
760 }
761 else {
762 luaK_exp2nextreg(fs, e2); /* operand must be on the 'stack' */
763 codearith(fs, OP_CONCAT, e1, e2);
764 }
765 break;
766 }
767 case OPR_ADD: codearith(fs, OP_ADD, e1, e2); break;
768 case OPR_SUB: codearith(fs, OP_SUB, e1, e2); break;
769 case OPR_MUL: codearith(fs, OP_MUL, e1, e2); break;
770 case OPR_DIV: codearith(fs, OP_DIV, e1, e2); break;
771 case OPR_MOD: codearith(fs, OP_MOD, e1, e2); break;
772 case OPR_POW: codearith(fs, OP_POW, e1, e2); break;
773 case OPR_EQ: codecomp(fs, OP_EQ, 1, e1, e2); break;
774 case OPR_NE: codecomp(fs, OP_EQ, 0, e1, e2); break;
775 case OPR_LT: codecomp(fs, OP_LT, 1, e1, e2); break;
776 case OPR_LE: codecomp(fs, OP_LE, 1, e1, e2); break;
777 case OPR_GT: codecomp(fs, OP_LT, 0, e1, e2); break;
778 case OPR_GE: codecomp(fs, OP_LE, 0, e1, e2); break;
779 default: lua_assert(0);
780 }
781}
782
783
784void luaK_fixline (FuncState *fs, int line) {
785 fs->f->lineinfo[fs->pc - 1] = line;
786}
787
788
789static int luaK_code (FuncState *fs, Instruction i, int line) {
790 Proto *f = fs->f;
791 dischargejpc(fs); /* `pc' will change */
792 /* put new instruction in code array */
793 luaM_growvector(fs->L, f->code, fs->pc, f->sizecode, Instruction,
794 MAX_INT, "code size overflow");
795 f->code[fs->pc] = i;
796 /* save corresponding line information */
797 luaM_growvector(fs->L, f->lineinfo, fs->pc, f->sizelineinfo, int,
798 MAX_INT, "code size overflow");
799 f->lineinfo[fs->pc] = line;
800 return fs->pc++;
801}
802
803
804int luaK_codeABC (FuncState *fs, OpCode o, int a, int b, int c) {
805 lua_assert(getOpMode(o) == iABC);
806 lua_assert(getBMode(o) != OpArgN || b == 0);
807 lua_assert(getCMode(o) != OpArgN || c == 0);
808 return luaK_code(fs, CREATE_ABC(o, a, b, c), fs->ls->lastline);
809}
810
811
812int luaK_codeABx (FuncState *fs, OpCode o, int a, unsigned int bc) {
813 lua_assert(getOpMode(o) == iABx || getOpMode(o) == iAsBx);
814 lua_assert(getCMode(o) == OpArgN);
815 return luaK_code(fs, CREATE_ABx(o, a, bc), fs->ls->lastline);
816}
817
818
819void luaK_setlist (FuncState *fs, int base, int nelems, int tostore) {
820 int c = (nelems - 1)/LFIELDS_PER_FLUSH + 1;
821 int b = (tostore == LUA_MULTRET) ? 0 : tostore;
822 lua_assert(tostore != 0);
823 if (c <= MAXARG_C)
824 luaK_codeABC(fs, OP_SETLIST, base, b, c);
825 else {
826 luaK_codeABC(fs, OP_SETLIST, base, b, 0);
827 luaK_code(fs, cast(Instruction, c), fs->ls->lastline);
828 }
829 fs->freereg = base + 1; /* free registers with list values */
830}
831
diff --git a/examples/redis-unstable/deps/lua/src/lcode.h b/examples/redis-unstable/deps/lua/src/lcode.h
deleted file mode 100644
index b941c60..0000000
--- a/examples/redis-unstable/deps/lua/src/lcode.h
+++ /dev/null
@@ -1,76 +0,0 @@
1/*
2** $Id: lcode.h,v 1.48.1.1 2007/12/27 13:02:25 roberto Exp $
3** Code generator for Lua
4** See Copyright Notice in lua.h
5*/
6
7#ifndef lcode_h
8#define lcode_h
9
10#include "llex.h"
11#include "lobject.h"
12#include "lopcodes.h"
13#include "lparser.h"
14
15
16/*
17** Marks the end of a patch list. It is an invalid value both as an absolute
18** address, and as a list link (would link an element to itself).
19*/
20#define NO_JUMP (-1)
21
22
23/*
24** grep "ORDER OPR" if you change these enums
25*/
26typedef enum BinOpr {
27 OPR_ADD, OPR_SUB, OPR_MUL, OPR_DIV, OPR_MOD, OPR_POW,
28 OPR_CONCAT,
29 OPR_NE, OPR_EQ,
30 OPR_LT, OPR_LE, OPR_GT, OPR_GE,
31 OPR_AND, OPR_OR,
32 OPR_NOBINOPR
33} BinOpr;
34
35
36typedef enum UnOpr { OPR_MINUS, OPR_NOT, OPR_LEN, OPR_NOUNOPR } UnOpr;
37
38
39#define getcode(fs,e) ((fs)->f->code[(e)->u.s.info])
40
41#define luaK_codeAsBx(fs,o,A,sBx) luaK_codeABx(fs,o,A,(sBx)+MAXARG_sBx)
42
43#define luaK_setmultret(fs,e) luaK_setreturns(fs, e, LUA_MULTRET)
44
45LUAI_FUNC int luaK_codeABx (FuncState *fs, OpCode o, int A, unsigned int Bx);
46LUAI_FUNC int luaK_codeABC (FuncState *fs, OpCode o, int A, int B, int C);
47LUAI_FUNC void luaK_fixline (FuncState *fs, int line);
48LUAI_FUNC void luaK_nil (FuncState *fs, int from, int n);
49LUAI_FUNC void luaK_reserveregs (FuncState *fs, int n);
50LUAI_FUNC void luaK_checkstack (FuncState *fs, int n);
51LUAI_FUNC int luaK_stringK (FuncState *fs, TString *s);
52LUAI_FUNC int luaK_numberK (FuncState *fs, lua_Number r);
53LUAI_FUNC void luaK_dischargevars (FuncState *fs, expdesc *e);
54LUAI_FUNC int luaK_exp2anyreg (FuncState *fs, expdesc *e);
55LUAI_FUNC void luaK_exp2nextreg (FuncState *fs, expdesc *e);
56LUAI_FUNC void luaK_exp2val (FuncState *fs, expdesc *e);
57LUAI_FUNC int luaK_exp2RK (FuncState *fs, expdesc *e);
58LUAI_FUNC void luaK_self (FuncState *fs, expdesc *e, expdesc *key);
59LUAI_FUNC void luaK_indexed (FuncState *fs, expdesc *t, expdesc *k);
60LUAI_FUNC void luaK_goiftrue (FuncState *fs, expdesc *e);
61LUAI_FUNC void luaK_storevar (FuncState *fs, expdesc *var, expdesc *e);
62LUAI_FUNC void luaK_setreturns (FuncState *fs, expdesc *e, int nresults);
63LUAI_FUNC void luaK_setoneret (FuncState *fs, expdesc *e);
64LUAI_FUNC int luaK_jump (FuncState *fs);
65LUAI_FUNC void luaK_ret (FuncState *fs, int first, int nret);
66LUAI_FUNC void luaK_patchlist (FuncState *fs, int list, int target);
67LUAI_FUNC void luaK_patchtohere (FuncState *fs, int list);
68LUAI_FUNC void luaK_concat (FuncState *fs, int *l1, int l2);
69LUAI_FUNC int luaK_getlabel (FuncState *fs);
70LUAI_FUNC void luaK_prefix (FuncState *fs, UnOpr op, expdesc *v);
71LUAI_FUNC void luaK_infix (FuncState *fs, BinOpr op, expdesc *v);
72LUAI_FUNC void luaK_posfix (FuncState *fs, BinOpr op, expdesc *v1, expdesc *v2);
73LUAI_FUNC void luaK_setlist (FuncState *fs, int base, int nelems, int tostore);
74
75
76#endif
diff --git a/examples/redis-unstable/deps/lua/src/ldblib.c b/examples/redis-unstable/deps/lua/src/ldblib.c
deleted file mode 100644
index 2027eda..0000000
--- a/examples/redis-unstable/deps/lua/src/ldblib.c
+++ /dev/null
@@ -1,398 +0,0 @@
1/*
2** $Id: ldblib.c,v 1.104.1.4 2009/08/04 18:50:18 roberto Exp $
3** Interface from Lua to its debug API
4** See Copyright Notice in lua.h
5*/
6
7
8#include <stdio.h>
9#include <stdlib.h>
10#include <string.h>
11
12#define ldblib_c
13#define LUA_LIB
14
15#include "lua.h"
16
17#include "lauxlib.h"
18#include "lualib.h"
19
20
21
22static int db_getregistry (lua_State *L) {
23 lua_pushvalue(L, LUA_REGISTRYINDEX);
24 return 1;
25}
26
27
28static int db_getmetatable (lua_State *L) {
29 luaL_checkany(L, 1);
30 if (!lua_getmetatable(L, 1)) {
31 lua_pushnil(L); /* no metatable */
32 }
33 return 1;
34}
35
36
37static int db_setmetatable (lua_State *L) {
38 int t = lua_type(L, 2);
39 luaL_argcheck(L, t == LUA_TNIL || t == LUA_TTABLE, 2,
40 "nil or table expected");
41 lua_settop(L, 2);
42 lua_pushboolean(L, lua_setmetatable(L, 1));
43 return 1;
44}
45
46
47static int db_getfenv (lua_State *L) {
48 luaL_checkany(L, 1);
49 lua_getfenv(L, 1);
50 return 1;
51}
52
53
54static int db_setfenv (lua_State *L) {
55 luaL_checktype(L, 2, LUA_TTABLE);
56 lua_settop(L, 2);
57 if (lua_setfenv(L, 1) == 0)
58 luaL_error(L, LUA_QL("setfenv")
59 " cannot change environment of given object");
60 return 1;
61}
62
63
64static void settabss (lua_State *L, const char *i, const char *v) {
65 lua_pushstring(L, v);
66 lua_setfield(L, -2, i);
67}
68
69
70static void settabsi (lua_State *L, const char *i, int v) {
71 lua_pushinteger(L, v);
72 lua_setfield(L, -2, i);
73}
74
75
76static lua_State *getthread (lua_State *L, int *arg) {
77 if (lua_isthread(L, 1)) {
78 *arg = 1;
79 return lua_tothread(L, 1);
80 }
81 else {
82 *arg = 0;
83 return L;
84 }
85}
86
87
88static void treatstackoption (lua_State *L, lua_State *L1, const char *fname) {
89 if (L == L1) {
90 lua_pushvalue(L, -2);
91 lua_remove(L, -3);
92 }
93 else
94 lua_xmove(L1, L, 1);
95 lua_setfield(L, -2, fname);
96}
97
98
99static int db_getinfo (lua_State *L) {
100 lua_Debug ar;
101 int arg;
102 lua_State *L1 = getthread(L, &arg);
103 const char *options = luaL_optstring(L, arg+2, "flnSu");
104 if (lua_isnumber(L, arg+1)) {
105 if (!lua_getstack(L1, (int)lua_tointeger(L, arg+1), &ar)) {
106 lua_pushnil(L); /* level out of range */
107 return 1;
108 }
109 }
110 else if (lua_isfunction(L, arg+1)) {
111 lua_pushfstring(L, ">%s", options);
112 options = lua_tostring(L, -1);
113 lua_pushvalue(L, arg+1);
114 lua_xmove(L, L1, 1);
115 }
116 else
117 return luaL_argerror(L, arg+1, "function or level expected");
118 if (!lua_getinfo(L1, options, &ar))
119 return luaL_argerror(L, arg+2, "invalid option");
120 lua_createtable(L, 0, 2);
121 if (strchr(options, 'S')) {
122 settabss(L, "source", ar.source);
123 settabss(L, "short_src", ar.short_src);
124 settabsi(L, "linedefined", ar.linedefined);
125 settabsi(L, "lastlinedefined", ar.lastlinedefined);
126 settabss(L, "what", ar.what);
127 }
128 if (strchr(options, 'l'))
129 settabsi(L, "currentline", ar.currentline);
130 if (strchr(options, 'u'))
131 settabsi(L, "nups", ar.nups);
132 if (strchr(options, 'n')) {
133 settabss(L, "name", ar.name);
134 settabss(L, "namewhat", ar.namewhat);
135 }
136 if (strchr(options, 'L'))
137 treatstackoption(L, L1, "activelines");
138 if (strchr(options, 'f'))
139 treatstackoption(L, L1, "func");
140 return 1; /* return table */
141}
142
143
144static int db_getlocal (lua_State *L) {
145 int arg;
146 lua_State *L1 = getthread(L, &arg);
147 lua_Debug ar;
148 const char *name;
149 if (!lua_getstack(L1, luaL_checkint(L, arg+1), &ar)) /* out of range? */
150 return luaL_argerror(L, arg+1, "level out of range");
151 name = lua_getlocal(L1, &ar, luaL_checkint(L, arg+2));
152 if (name) {
153 lua_xmove(L1, L, 1);
154 lua_pushstring(L, name);
155 lua_pushvalue(L, -2);
156 return 2;
157 }
158 else {
159 lua_pushnil(L);
160 return 1;
161 }
162}
163
164
165static int db_setlocal (lua_State *L) {
166 int arg;
167 lua_State *L1 = getthread(L, &arg);
168 lua_Debug ar;
169 if (!lua_getstack(L1, luaL_checkint(L, arg+1), &ar)) /* out of range? */
170 return luaL_argerror(L, arg+1, "level out of range");
171 luaL_checkany(L, arg+3);
172 lua_settop(L, arg+3);
173 lua_xmove(L, L1, 1);
174 lua_pushstring(L, lua_setlocal(L1, &ar, luaL_checkint(L, arg+2)));
175 return 1;
176}
177
178
179static int auxupvalue (lua_State *L, int get) {
180 const char *name;
181 int n = luaL_checkint(L, 2);
182 luaL_checktype(L, 1, LUA_TFUNCTION);
183 if (lua_iscfunction(L, 1)) return 0; /* cannot touch C upvalues from Lua */
184 name = get ? lua_getupvalue(L, 1, n) : lua_setupvalue(L, 1, n);
185 if (name == NULL) return 0;
186 lua_pushstring(L, name);
187 lua_insert(L, -(get+1));
188 return get + 1;
189}
190
191
192static int db_getupvalue (lua_State *L) {
193 return auxupvalue(L, 1);
194}
195
196
197static int db_setupvalue (lua_State *L) {
198 luaL_checkany(L, 3);
199 return auxupvalue(L, 0);
200}
201
202
203
204static const char KEY_HOOK = 'h';
205
206
207static void hookf (lua_State *L, lua_Debug *ar) {
208 static const char *const hooknames[] =
209 {"call", "return", "line", "count", "tail return"};
210 lua_pushlightuserdata(L, (void *)&KEY_HOOK);
211 lua_rawget(L, LUA_REGISTRYINDEX);
212 lua_pushlightuserdata(L, L);
213 lua_rawget(L, -2);
214 if (lua_isfunction(L, -1)) {
215 lua_pushstring(L, hooknames[(int)ar->event]);
216 if (ar->currentline >= 0)
217 lua_pushinteger(L, ar->currentline);
218 else lua_pushnil(L);
219 lua_assert(lua_getinfo(L, "lS", ar));
220 lua_call(L, 2, 0);
221 }
222}
223
224
225static int makemask (const char *smask, int count) {
226 int mask = 0;
227 if (strchr(smask, 'c')) mask |= LUA_MASKCALL;
228 if (strchr(smask, 'r')) mask |= LUA_MASKRET;
229 if (strchr(smask, 'l')) mask |= LUA_MASKLINE;
230 if (count > 0) mask |= LUA_MASKCOUNT;
231 return mask;
232}
233
234
235static char *unmakemask (int mask, char *smask) {
236 int i = 0;
237 if (mask & LUA_MASKCALL) smask[i++] = 'c';
238 if (mask & LUA_MASKRET) smask[i++] = 'r';
239 if (mask & LUA_MASKLINE) smask[i++] = 'l';
240 smask[i] = '\0';
241 return smask;
242}
243
244
245static void gethooktable (lua_State *L) {
246 lua_pushlightuserdata(L, (void *)&KEY_HOOK);
247 lua_rawget(L, LUA_REGISTRYINDEX);
248 if (!lua_istable(L, -1)) {
249 lua_pop(L, 1);
250 lua_createtable(L, 0, 1);
251 lua_pushlightuserdata(L, (void *)&KEY_HOOK);
252 lua_pushvalue(L, -2);
253 lua_rawset(L, LUA_REGISTRYINDEX);
254 }
255}
256
257
258static int db_sethook (lua_State *L) {
259 int arg, mask, count;
260 lua_Hook func;
261 lua_State *L1 = getthread(L, &arg);
262 if (lua_isnoneornil(L, arg+1)) {
263 lua_settop(L, arg+1);
264 func = NULL; mask = 0; count = 0; /* turn off hooks */
265 }
266 else {
267 const char *smask = luaL_checkstring(L, arg+2);
268 luaL_checktype(L, arg+1, LUA_TFUNCTION);
269 count = luaL_optint(L, arg+3, 0);
270 func = hookf; mask = makemask(smask, count);
271 }
272 gethooktable(L);
273 lua_pushlightuserdata(L, L1);
274 lua_pushvalue(L, arg+1);
275 lua_rawset(L, -3); /* set new hook */
276 lua_pop(L, 1); /* remove hook table */
277 lua_sethook(L1, func, mask, count); /* set hooks */
278 return 0;
279}
280
281
282static int db_gethook (lua_State *L) {
283 int arg;
284 lua_State *L1 = getthread(L, &arg);
285 char buff[5];
286 int mask = lua_gethookmask(L1);
287 lua_Hook hook = lua_gethook(L1);
288 if (hook != NULL && hook != hookf) /* external hook? */
289 lua_pushliteral(L, "external hook");
290 else {
291 gethooktable(L);
292 lua_pushlightuserdata(L, L1);
293 lua_rawget(L, -2); /* get hook */
294 lua_remove(L, -2); /* remove hook table */
295 }
296 lua_pushstring(L, unmakemask(mask, buff));
297 lua_pushinteger(L, lua_gethookcount(L1));
298 return 3;
299}
300
301
302static int db_debug (lua_State *L) {
303 for (;;) {
304 char buffer[250];
305 fputs("lua_debug> ", stderr);
306 if (fgets(buffer, sizeof(buffer), stdin) == 0 ||
307 strcmp(buffer, "cont\n") == 0)
308 return 0;
309 if (luaL_loadbuffer(L, buffer, strlen(buffer), "=(debug command)") ||
310 lua_pcall(L, 0, 0, 0)) {
311 fputs(lua_tostring(L, -1), stderr);
312 fputs("\n", stderr);
313 }
314 lua_settop(L, 0); /* remove eventual returns */
315 }
316}
317
318
319#define LEVELS1 12 /* size of the first part of the stack */
320#define LEVELS2 10 /* size of the second part of the stack */
321
322static int db_errorfb (lua_State *L) {
323 int level;
324 int firstpart = 1; /* still before eventual `...' */
325 int arg;
326 lua_State *L1 = getthread(L, &arg);
327 lua_Debug ar;
328 if (lua_isnumber(L, arg+2)) {
329 level = (int)lua_tointeger(L, arg+2);
330 lua_pop(L, 1);
331 }
332 else
333 level = (L == L1) ? 1 : 0; /* level 0 may be this own function */
334 if (lua_gettop(L) == arg)
335 lua_pushliteral(L, "");
336 else if (!lua_isstring(L, arg+1)) return 1; /* message is not a string */
337 else lua_pushliteral(L, "\n");
338 lua_pushliteral(L, "stack traceback:");
339 while (lua_getstack(L1, level++, &ar)) {
340 if (level > LEVELS1 && firstpart) {
341 /* no more than `LEVELS2' more levels? */
342 if (!lua_getstack(L1, level+LEVELS2, &ar))
343 level--; /* keep going */
344 else {
345 lua_pushliteral(L, "\n\t..."); /* too many levels */
346 while (lua_getstack(L1, level+LEVELS2, &ar)) /* find last levels */
347 level++;
348 }
349 firstpart = 0;
350 continue;
351 }
352 lua_pushliteral(L, "\n\t");
353 lua_getinfo(L1, "Snl", &ar);
354 lua_pushfstring(L, "%s:", ar.short_src);
355 if (ar.currentline > 0)
356 lua_pushfstring(L, "%d:", ar.currentline);
357 if (*ar.namewhat != '\0') /* is there a name? */
358 lua_pushfstring(L, " in function " LUA_QS, ar.name);
359 else {
360 if (*ar.what == 'm') /* main? */
361 lua_pushfstring(L, " in main chunk");
362 else if (*ar.what == 'C' || *ar.what == 't')
363 lua_pushliteral(L, " ?"); /* C function or tail call */
364 else
365 lua_pushfstring(L, " in function <%s:%d>",
366 ar.short_src, ar.linedefined);
367 }
368 lua_concat(L, lua_gettop(L) - arg);
369 }
370 lua_concat(L, lua_gettop(L) - arg);
371 return 1;
372}
373
374
375static const luaL_Reg dblib[] = {
376 {"debug", db_debug},
377 {"getfenv", db_getfenv},
378 {"gethook", db_gethook},
379 {"getinfo", db_getinfo},
380 {"getlocal", db_getlocal},
381 {"getregistry", db_getregistry},
382 {"getmetatable", db_getmetatable},
383 {"getupvalue", db_getupvalue},
384 {"setfenv", db_setfenv},
385 {"sethook", db_sethook},
386 {"setlocal", db_setlocal},
387 {"setmetatable", db_setmetatable},
388 {"setupvalue", db_setupvalue},
389 {"traceback", db_errorfb},
390 {NULL, NULL}
391};
392
393
394LUALIB_API int luaopen_debug (lua_State *L) {
395 luaL_register(L, LUA_DBLIBNAME, dblib);
396 return 1;
397}
398
diff --git a/examples/redis-unstable/deps/lua/src/ldebug.c b/examples/redis-unstable/deps/lua/src/ldebug.c
deleted file mode 100644
index 4940e65..0000000
--- a/examples/redis-unstable/deps/lua/src/ldebug.c
+++ /dev/null
@@ -1,637 +0,0 @@
1/*
2** $Id: ldebug.c,v 2.29.1.6 2008/05/08 16:56:26 roberto Exp $
3** Debug Interface
4** See Copyright Notice in lua.h
5*/
6
7
8#include <stdarg.h>
9#include <stddef.h>
10#include <string.h>
11
12
13#define ldebug_c
14#define LUA_CORE
15
16#include "lua.h"
17
18#include "lapi.h"
19#include "lcode.h"
20#include "ldebug.h"
21#include "ldo.h"
22#include "lfunc.h"
23#include "lobject.h"
24#include "lopcodes.h"
25#include "lstate.h"
26#include "lstring.h"
27#include "ltable.h"
28#include "ltm.h"
29#include "lvm.h"
30
31
32
33static const char *getfuncname (lua_State *L, CallInfo *ci, const char **name);
34
35
36static int currentpc (lua_State *L, CallInfo *ci) {
37 if (!isLua(ci)) return -1; /* function is not a Lua function? */
38 if (ci == L->ci)
39 ci->savedpc = L->savedpc;
40 return pcRel(ci->savedpc, ci_func(ci)->l.p);
41}
42
43
44static int currentline (lua_State *L, CallInfo *ci) {
45 int pc = currentpc(L, ci);
46 if (pc < 0)
47 return -1; /* only active lua functions have current-line information */
48 else
49 return getline(ci_func(ci)->l.p, pc);
50}
51
52
53/*
54** this function can be called asynchronous (e.g. during a signal)
55*/
56LUA_API int lua_sethook (lua_State *L, lua_Hook func, int mask, int count) {
57 if (func == NULL || mask == 0) { /* turn off hooks? */
58 mask = 0;
59 func = NULL;
60 }
61 L->hook = func;
62 L->basehookcount = count;
63 resethookcount(L);
64 L->hookmask = cast_byte(mask);
65 return 1;
66}
67
68
69LUA_API lua_Hook lua_gethook (lua_State *L) {
70 return L->hook;
71}
72
73
74LUA_API int lua_gethookmask (lua_State *L) {
75 return L->hookmask;
76}
77
78
79LUA_API int lua_gethookcount (lua_State *L) {
80 return L->basehookcount;
81}
82
83LUA_API int lua_getstack (lua_State *L, int level, lua_Debug *ar) {
84 int status;
85 CallInfo *ci;
86 lua_lock(L);
87 for (ci = L->ci; level > 0 && ci > L->base_ci; ci--) {
88 level--;
89 if (f_isLua(ci)) /* Lua function? */
90 level -= ci->tailcalls; /* skip lost tail calls */
91 }
92 if (level == 0 && ci > L->base_ci) { /* level found? */
93 status = 1;
94 ar->i_ci = cast_int(ci - L->base_ci);
95 }
96 else if (level < 0) { /* level is of a lost tail call? */
97 status = 1;
98 ar->i_ci = 0;
99 }
100 else status = 0; /* no such level */
101 lua_unlock(L);
102 return status;
103}
104
105
106static Proto *getluaproto (CallInfo *ci) {
107 return (isLua(ci) ? ci_func(ci)->l.p : NULL);
108}
109
110
111static const char *findlocal (lua_State *L, CallInfo *ci, int n) {
112 const char *name;
113 Proto *fp = getluaproto(ci);
114 if (fp && (name = luaF_getlocalname(fp, n, currentpc(L, ci))) != NULL)
115 return name; /* is a local variable in a Lua function */
116 else {
117 StkId limit = (ci == L->ci) ? L->top : (ci+1)->func;
118 if (limit - ci->base >= n && n > 0) /* is 'n' inside 'ci' stack? */
119 return "(*temporary)";
120 else
121 return NULL;
122 }
123}
124
125
126LUA_API const char *lua_getlocal (lua_State *L, const lua_Debug *ar, int n) {
127 CallInfo *ci = L->base_ci + ar->i_ci;
128 const char *name = findlocal(L, ci, n);
129 lua_lock(L);
130 if (name)
131 luaA_pushobject(L, ci->base + (n - 1));
132 lua_unlock(L);
133 return name;
134}
135
136
137LUA_API const char *lua_setlocal (lua_State *L, const lua_Debug *ar, int n) {
138 CallInfo *ci = L->base_ci + ar->i_ci;
139 const char *name = findlocal(L, ci, n);
140 lua_lock(L);
141 if (name)
142 setobjs2s(L, ci->base + (n - 1), L->top - 1);
143 L->top--; /* pop value */
144 lua_unlock(L);
145 return name;
146}
147
148
149static void funcinfo (lua_Debug *ar, Closure *cl) {
150 if (cl->c.isC) {
151 ar->source = "=[C]";
152 ar->linedefined = -1;
153 ar->lastlinedefined = -1;
154 ar->what = "C";
155 }
156 else {
157 ar->source = getstr(cl->l.p->source);
158 ar->linedefined = cl->l.p->linedefined;
159 ar->lastlinedefined = cl->l.p->lastlinedefined;
160 ar->what = (ar->linedefined == 0) ? "main" : "Lua";
161 }
162 luaO_chunkid(ar->short_src, ar->source, LUA_IDSIZE);
163}
164
165
166static void info_tailcall (lua_Debug *ar) {
167 ar->name = ar->namewhat = "";
168 ar->what = "tail";
169 ar->lastlinedefined = ar->linedefined = ar->currentline = -1;
170 ar->source = "=(tail call)";
171 luaO_chunkid(ar->short_src, ar->source, LUA_IDSIZE);
172 ar->nups = 0;
173}
174
175
176static void collectvalidlines (lua_State *L, Closure *f) {
177 if (f == NULL || f->c.isC) {
178 setnilvalue(L->top);
179 }
180 else {
181 Table *t = luaH_new(L, 0, 0);
182 int *lineinfo = f->l.p->lineinfo;
183 int i;
184 for (i=0; i<f->l.p->sizelineinfo; i++)
185 setbvalue(luaH_setnum(L, t, lineinfo[i]), 1);
186 sethvalue(L, L->top, t);
187 }
188 incr_top(L);
189}
190
191
192static int auxgetinfo (lua_State *L, const char *what, lua_Debug *ar,
193 Closure *f, CallInfo *ci) {
194 int status = 1;
195 if (f == NULL) {
196 info_tailcall(ar);
197 return status;
198 }
199 for (; *what; what++) {
200 switch (*what) {
201 case 'S': {
202 funcinfo(ar, f);
203 break;
204 }
205 case 'l': {
206 ar->currentline = (ci) ? currentline(L, ci) : -1;
207 break;
208 }
209 case 'u': {
210 ar->nups = f->c.nupvalues;
211 break;
212 }
213 case 'n': {
214 ar->namewhat = (ci) ? getfuncname(L, ci, &ar->name) : NULL;
215 if (ar->namewhat == NULL) {
216 ar->namewhat = ""; /* not found */
217 ar->name = NULL;
218 }
219 break;
220 }
221 case 'L':
222 case 'f': /* handled by lua_getinfo */
223 break;
224 default: status = 0; /* invalid option */
225 }
226 }
227 return status;
228}
229
230
231LUA_API int lua_getinfo (lua_State *L, const char *what, lua_Debug *ar) {
232 int status;
233 Closure *f = NULL;
234 CallInfo *ci = NULL;
235 lua_lock(L);
236 if (*what == '>') {
237 StkId func = L->top - 1;
238 luai_apicheck(L, ttisfunction(func));
239 what++; /* skip the '>' */
240 f = clvalue(func);
241 L->top--; /* pop function */
242 }
243 else if (ar->i_ci != 0) { /* no tail call? */
244 ci = L->base_ci + ar->i_ci;
245 lua_assert(ttisfunction(ci->func));
246 f = clvalue(ci->func);
247 }
248 status = auxgetinfo(L, what, ar, f, ci);
249 if (strchr(what, 'f')) {
250 if (f == NULL) setnilvalue(L->top);
251 else setclvalue(L, L->top, f);
252 incr_top(L);
253 }
254 if (strchr(what, 'L'))
255 collectvalidlines(L, f);
256 lua_unlock(L);
257 return status;
258}
259
260
261/*
262** {======================================================
263** Symbolic Execution and code checker
264** =======================================================
265*/
266
267#define check(x) if (!(x)) return 0;
268
269#define checkjump(pt,pc) check(0 <= pc && pc < pt->sizecode)
270
271#define checkreg(pt,reg) check((reg) < (pt)->maxstacksize)
272
273
274
275static int precheck (const Proto *pt) {
276 check(pt->maxstacksize <= MAXSTACK);
277 check(pt->numparams+(pt->is_vararg & VARARG_HASARG) <= pt->maxstacksize);
278 check(!(pt->is_vararg & VARARG_NEEDSARG) ||
279 (pt->is_vararg & VARARG_HASARG));
280 check(pt->sizeupvalues <= pt->nups);
281 check(pt->sizelineinfo == pt->sizecode || pt->sizelineinfo == 0);
282 check(pt->sizecode > 0 && GET_OPCODE(pt->code[pt->sizecode-1]) == OP_RETURN);
283 return 1;
284}
285
286
287#define checkopenop(pt,pc) luaG_checkopenop((pt)->code[(pc)+1])
288
289int luaG_checkopenop (Instruction i) {
290 switch (GET_OPCODE(i)) {
291 case OP_CALL:
292 case OP_TAILCALL:
293 case OP_RETURN:
294 case OP_SETLIST: {
295 check(GETARG_B(i) == 0);
296 return 1;
297 }
298 default: return 0; /* invalid instruction after an open call */
299 }
300}
301
302
303static int checkArgMode (const Proto *pt, int r, enum OpArgMask mode) {
304 switch (mode) {
305 case OpArgN: check(r == 0); break;
306 case OpArgU: break;
307 case OpArgR: checkreg(pt, r); break;
308 case OpArgK:
309 check(ISK(r) ? INDEXK(r) < pt->sizek : r < pt->maxstacksize);
310 break;
311 }
312 return 1;
313}
314
315
316static Instruction symbexec (const Proto *pt, int lastpc, int reg) {
317 int pc;
318 int last; /* stores position of last instruction that changed `reg' */
319 last = pt->sizecode-1; /* points to final return (a `neutral' instruction) */
320 check(precheck(pt));
321 for (pc = 0; pc < lastpc; pc++) {
322 Instruction i = pt->code[pc];
323 OpCode op = GET_OPCODE(i);
324 int a = GETARG_A(i);
325 int b = 0;
326 int c = 0;
327 check(op < NUM_OPCODES);
328 checkreg(pt, a);
329 switch (getOpMode(op)) {
330 case iABC: {
331 b = GETARG_B(i);
332 c = GETARG_C(i);
333 check(checkArgMode(pt, b, getBMode(op)));
334 check(checkArgMode(pt, c, getCMode(op)));
335 break;
336 }
337 case iABx: {
338 b = GETARG_Bx(i);
339 if (getBMode(op) == OpArgK) check(b < pt->sizek);
340 break;
341 }
342 case iAsBx: {
343 b = GETARG_sBx(i);
344 if (getBMode(op) == OpArgR) {
345 int dest = pc+1+b;
346 check(0 <= dest && dest < pt->sizecode);
347 if (dest > 0) {
348 int j;
349 /* check that it does not jump to a setlist count; this
350 is tricky, because the count from a previous setlist may
351 have the same value of an invalid setlist; so, we must
352 go all the way back to the first of them (if any) */
353 for (j = 0; j < dest; j++) {
354 Instruction d = pt->code[dest-1-j];
355 if (!(GET_OPCODE(d) == OP_SETLIST && GETARG_C(d) == 0)) break;
356 }
357 /* if 'j' is even, previous value is not a setlist (even if
358 it looks like one) */
359 check((j&1) == 0);
360 }
361 }
362 break;
363 }
364 }
365 if (testAMode(op)) {
366 if (a == reg) last = pc; /* change register `a' */
367 }
368 if (testTMode(op)) {
369 check(pc+2 < pt->sizecode); /* check skip */
370 check(GET_OPCODE(pt->code[pc+1]) == OP_JMP);
371 }
372 switch (op) {
373 case OP_LOADBOOL: {
374 if (c == 1) { /* does it jump? */
375 check(pc+2 < pt->sizecode); /* check its jump */
376 check(GET_OPCODE(pt->code[pc+1]) != OP_SETLIST ||
377 GETARG_C(pt->code[pc+1]) != 0);
378 }
379 break;
380 }
381 case OP_LOADNIL: {
382 if (a <= reg && reg <= b)
383 last = pc; /* set registers from `a' to `b' */
384 break;
385 }
386 case OP_GETUPVAL:
387 case OP_SETUPVAL: {
388 check(b < pt->nups);
389 break;
390 }
391 case OP_GETGLOBAL:
392 case OP_SETGLOBAL: {
393 check(ttisstring(&pt->k[b]));
394 break;
395 }
396 case OP_SELF: {
397 checkreg(pt, a+1);
398 if (reg == a+1) last = pc;
399 break;
400 }
401 case OP_CONCAT: {
402 check(b < c); /* at least two operands */
403 break;
404 }
405 case OP_TFORLOOP: {
406 check(c >= 1); /* at least one result (control variable) */
407 checkreg(pt, a+2+c); /* space for results */
408 if (reg >= a+2) last = pc; /* affect all regs above its base */
409 break;
410 }
411 case OP_FORLOOP:
412 case OP_FORPREP:
413 checkreg(pt, a+3);
414 /* go through */
415 case OP_JMP: {
416 int dest = pc+1+b;
417 /* not full check and jump is forward and do not skip `lastpc'? */
418 if (reg != NO_REG && pc < dest && dest <= lastpc)
419 pc += b; /* do the jump */
420 break;
421 }
422 case OP_CALL:
423 case OP_TAILCALL: {
424 if (b != 0) {
425 checkreg(pt, a+b-1);
426 }
427 c--; /* c = num. returns */
428 if (c == LUA_MULTRET) {
429 check(checkopenop(pt, pc));
430 }
431 else if (c != 0)
432 checkreg(pt, a+c-1);
433 if (reg >= a) last = pc; /* affect all registers above base */
434 break;
435 }
436 case OP_RETURN: {
437 b--; /* b = num. returns */
438 if (b > 0) checkreg(pt, a+b-1);
439 break;
440 }
441 case OP_SETLIST: {
442 if (b > 0) checkreg(pt, a + b);
443 if (c == 0) {
444 pc++;
445 check(pc < pt->sizecode - 1);
446 }
447 break;
448 }
449 case OP_CLOSURE: {
450 int nup, j;
451 check(b < pt->sizep);
452 nup = pt->p[b]->nups;
453 check(pc + nup < pt->sizecode);
454 for (j = 1; j <= nup; j++) {
455 OpCode op1 = GET_OPCODE(pt->code[pc + j]);
456 check(op1 == OP_GETUPVAL || op1 == OP_MOVE);
457 }
458 if (reg != NO_REG) /* tracing? */
459 pc += nup; /* do not 'execute' these pseudo-instructions */
460 break;
461 }
462 case OP_VARARG: {
463 check((pt->is_vararg & VARARG_ISVARARG) &&
464 !(pt->is_vararg & VARARG_NEEDSARG));
465 b--;
466 if (b == LUA_MULTRET) check(checkopenop(pt, pc));
467 checkreg(pt, a+b-1);
468 break;
469 }
470 default: break;
471 }
472 }
473 return pt->code[last];
474}
475
476#undef check
477#undef checkjump
478#undef checkreg
479
480/* }====================================================== */
481
482
483int luaG_checkcode (const Proto *pt) {
484 return (symbexec(pt, pt->sizecode, NO_REG) != 0);
485}
486
487
488static const char *kname (Proto *p, int c) {
489 if (ISK(c) && ttisstring(&p->k[INDEXK(c)]))
490 return svalue(&p->k[INDEXK(c)]);
491 else
492 return "?";
493}
494
495
496static const char *getobjname (lua_State *L, CallInfo *ci, int stackpos,
497 const char **name) {
498 if (isLua(ci)) { /* a Lua function? */
499 Proto *p = ci_func(ci)->l.p;
500 int pc = currentpc(L, ci);
501 Instruction i;
502 *name = luaF_getlocalname(p, stackpos+1, pc);
503 if (*name) /* is a local? */
504 return "local";
505 i = symbexec(p, pc, stackpos); /* try symbolic execution */
506 lua_assert(pc != -1);
507 switch (GET_OPCODE(i)) {
508 case OP_GETGLOBAL: {
509 int g = GETARG_Bx(i); /* global index */
510 lua_assert(ttisstring(&p->k[g]));
511 *name = svalue(&p->k[g]);
512 return "global";
513 }
514 case OP_MOVE: {
515 int a = GETARG_A(i);
516 int b = GETARG_B(i); /* move from `b' to `a' */
517 if (b < a)
518 return getobjname(L, ci, b, name); /* get name for `b' */
519 break;
520 }
521 case OP_GETTABLE: {
522 int k = GETARG_C(i); /* key index */
523 *name = kname(p, k);
524 return "field";
525 }
526 case OP_GETUPVAL: {
527 int u = GETARG_B(i); /* upvalue index */
528 *name = p->upvalues ? getstr(p->upvalues[u]) : "?";
529 return "upvalue";
530 }
531 case OP_SELF: {
532 int k = GETARG_C(i); /* key index */
533 *name = kname(p, k);
534 return "method";
535 }
536 default: break;
537 }
538 }
539 return NULL; /* no useful name found */
540}
541
542
543static const char *getfuncname (lua_State *L, CallInfo *ci, const char **name) {
544 Instruction i;
545 if ((isLua(ci) && ci->tailcalls > 0) || !isLua(ci - 1))
546 return NULL; /* calling function is not Lua (or is unknown) */
547 ci--; /* calling function */
548 i = ci_func(ci)->l.p->code[currentpc(L, ci)];
549 if (GET_OPCODE(i) == OP_CALL || GET_OPCODE(i) == OP_TAILCALL ||
550 GET_OPCODE(i) == OP_TFORLOOP)
551 return getobjname(L, ci, GETARG_A(i), name);
552 else
553 return NULL; /* no useful name can be found */
554}
555
556
557/* only ANSI way to check whether a pointer points to an array */
558static int isinstack (CallInfo *ci, const TValue *o) {
559 StkId p;
560 for (p = ci->base; p < ci->top; p++)
561 if (o == p) return 1;
562 return 0;
563}
564
565
566void luaG_typeerror (lua_State *L, const TValue *o, const char *op) {
567 const char *name = NULL;
568 const char *t = luaT_typenames[ttype(o)];
569 const char *kind = (isinstack(L->ci, o)) ?
570 getobjname(L, L->ci, cast_int(o - L->base), &name) :
571 NULL;
572 if (kind)
573 luaG_runerror(L, "attempt to %s %s " LUA_QS " (a %s value)",
574 op, kind, name, t);
575 else
576 luaG_runerror(L, "attempt to %s a %s value", op, t);
577}
578
579
580void luaG_concaterror (lua_State *L, StkId p1, StkId p2) {
581 if (ttisstring(p1) || ttisnumber(p1)) p1 = p2;
582 lua_assert(!ttisstring(p1) && !ttisnumber(p1));
583 luaG_typeerror(L, p1, "concatenate");
584}
585
586
587void luaG_aritherror (lua_State *L, const TValue *p1, const TValue *p2) {
588 TValue temp;
589 if (luaV_tonumber(p1, &temp) == NULL)
590 p2 = p1; /* first operand is wrong */
591 luaG_typeerror(L, p2, "perform arithmetic on");
592}
593
594
595int luaG_ordererror (lua_State *L, const TValue *p1, const TValue *p2) {
596 const char *t1 = luaT_typenames[ttype(p1)];
597 const char *t2 = luaT_typenames[ttype(p2)];
598 if (t1[2] == t2[2])
599 luaG_runerror(L, "attempt to compare two %s values", t1);
600 else
601 luaG_runerror(L, "attempt to compare %s with %s", t1, t2);
602 return 0;
603}
604
605
606static void addinfo (lua_State *L, const char *msg) {
607 CallInfo *ci = L->ci;
608 if (isLua(ci)) { /* is Lua code? */
609 char buff[LUA_IDSIZE]; /* add file:line information */
610 int line = currentline(L, ci);
611 luaO_chunkid(buff, getstr(getluaproto(ci)->source), LUA_IDSIZE);
612 luaO_pushfstring(L, "%s:%d: %s", buff, line, msg);
613 }
614}
615
616
617void luaG_errormsg (lua_State *L) {
618 if (L->errfunc != 0) { /* is there an error handling function? */
619 StkId errfunc = restorestack(L, L->errfunc);
620 if (!ttisfunction(errfunc)) luaD_throw(L, LUA_ERRERR);
621 setobjs2s(L, L->top, L->top - 1); /* move argument */
622 setobjs2s(L, L->top - 1, errfunc); /* push function */
623 incr_top(L);
624 luaD_call(L, L->top - 2, 1); /* call it */
625 }
626 luaD_throw(L, LUA_ERRRUN);
627}
628
629
630void luaG_runerror (lua_State *L, const char *fmt, ...) {
631 va_list argp;
632 va_start(argp, fmt);
633 addinfo(L, luaO_pushvfstring(L, fmt, argp));
634 va_end(argp);
635 luaG_errormsg(L);
636}
637
diff --git a/examples/redis-unstable/deps/lua/src/ldebug.h b/examples/redis-unstable/deps/lua/src/ldebug.h
deleted file mode 100644
index ba28a97..0000000
--- a/examples/redis-unstable/deps/lua/src/ldebug.h
+++ /dev/null
@@ -1,33 +0,0 @@
1/*
2** $Id: ldebug.h,v 2.3.1.1 2007/12/27 13:02:25 roberto Exp $
3** Auxiliary functions from Debug Interface module
4** See Copyright Notice in lua.h
5*/
6
7#ifndef ldebug_h
8#define ldebug_h
9
10
11#include "lstate.h"
12
13
14#define pcRel(pc, p) (cast(int, (pc) - (p)->code) - 1)
15
16#define getline(f,pc) (((f)->lineinfo) ? (f)->lineinfo[pc] : 0)
17
18#define resethookcount(L) (L->hookcount = L->basehookcount)
19
20
21LUAI_FUNC void luaG_typeerror (lua_State *L, const TValue *o,
22 const char *opname);
23LUAI_FUNC void luaG_concaterror (lua_State *L, StkId p1, StkId p2);
24LUAI_FUNC void luaG_aritherror (lua_State *L, const TValue *p1,
25 const TValue *p2);
26LUAI_FUNC int luaG_ordererror (lua_State *L, const TValue *p1,
27 const TValue *p2);
28LUAI_FUNC void luaG_runerror (lua_State *L, const char *fmt, ...);
29LUAI_FUNC void luaG_errormsg (lua_State *L);
30LUAI_FUNC int luaG_checkcode (const Proto *pt);
31LUAI_FUNC int luaG_checkopenop (Instruction i);
32
33#endif
diff --git a/examples/redis-unstable/deps/lua/src/ldo.c b/examples/redis-unstable/deps/lua/src/ldo.c
deleted file mode 100644
index e641dfc..0000000
--- a/examples/redis-unstable/deps/lua/src/ldo.c
+++ /dev/null
@@ -1,519 +0,0 @@
1/*
2** $Id: ldo.c,v 2.38.1.4 2012/01/18 02:27:10 roberto Exp $
3** Stack and Call structure of Lua
4** See Copyright Notice in lua.h
5*/
6
7
8#include <setjmp.h>
9#include <stdlib.h>
10#include <string.h>
11
12#define ldo_c
13#define LUA_CORE
14
15#include "lua.h"
16
17#include "ldebug.h"
18#include "ldo.h"
19#include "lfunc.h"
20#include "lgc.h"
21#include "lmem.h"
22#include "lobject.h"
23#include "lopcodes.h"
24#include "lparser.h"
25#include "lstate.h"
26#include "lstring.h"
27#include "ltable.h"
28#include "ltm.h"
29#include "lundump.h"
30#include "lvm.h"
31#include "lzio.h"
32
33
34
35
36/*
37** {======================================================
38** Error-recovery functions
39** =======================================================
40*/
41
42
43/* chain list of long jump buffers */
44struct lua_longjmp {
45 struct lua_longjmp *previous;
46 luai_jmpbuf b;
47 volatile int status; /* error code */
48};
49
50
51void luaD_seterrorobj (lua_State *L, int errcode, StkId oldtop) {
52 switch (errcode) {
53 case LUA_ERRMEM: {
54 setsvalue2s(L, oldtop, luaS_newliteral(L, MEMERRMSG));
55 break;
56 }
57 case LUA_ERRERR: {
58 setsvalue2s(L, oldtop, luaS_newliteral(L, "error in error handling"));
59 break;
60 }
61 case LUA_ERRSYNTAX:
62 case LUA_ERRRUN: {
63 setobjs2s(L, oldtop, L->top - 1); /* error message on current top */
64 break;
65 }
66 }
67 L->top = oldtop + 1;
68}
69
70
71static void restore_stack_limit (lua_State *L) {
72 lua_assert(L->stack_last - L->stack == L->stacksize - EXTRA_STACK - 1);
73 if (L->size_ci > LUAI_MAXCALLS) { /* there was an overflow? */
74 int inuse = cast_int(L->ci - L->base_ci);
75 if (inuse + 1 < LUAI_MAXCALLS) /* can `undo' overflow? */
76 luaD_reallocCI(L, LUAI_MAXCALLS);
77 }
78}
79
80
81static void resetstack (lua_State *L, int status) {
82 L->ci = L->base_ci;
83 L->base = L->ci->base;
84 luaF_close(L, L->base); /* close eventual pending closures */
85 luaD_seterrorobj(L, status, L->base);
86 L->nCcalls = L->baseCcalls;
87 L->allowhook = 1;
88 restore_stack_limit(L);
89 L->errfunc = 0;
90 L->errorJmp = NULL;
91}
92
93
94void luaD_throw (lua_State *L, int errcode) {
95 if (L->errorJmp) {
96 L->errorJmp->status = errcode;
97 LUAI_THROW(L, L->errorJmp);
98 }
99 else {
100 L->status = cast_byte(errcode);
101 if (G(L)->panic) {
102 resetstack(L, errcode);
103 lua_unlock(L);
104 G(L)->panic(L);
105 }
106 exit(EXIT_FAILURE);
107 }
108}
109
110
111int luaD_rawrunprotected (lua_State *L, Pfunc f, void *ud) {
112 struct lua_longjmp lj;
113 lj.status = 0;
114 lj.previous = L->errorJmp; /* chain new error handler */
115 L->errorJmp = &lj;
116 LUAI_TRY(L, &lj,
117 (*f)(L, ud);
118 );
119 L->errorJmp = lj.previous; /* restore old error handler */
120 return lj.status;
121}
122
123/* }====================================================== */
124
125
126static void correctstack (lua_State *L, TValue *oldstack) {
127 CallInfo *ci;
128 GCObject *up;
129 L->top = (L->top - oldstack) + L->stack;
130 for (up = L->openupval; up != NULL; up = up->gch.next)
131 gco2uv(up)->v = (gco2uv(up)->v - oldstack) + L->stack;
132 for (ci = L->base_ci; ci <= L->ci; ci++) {
133 ci->top = (ci->top - oldstack) + L->stack;
134 ci->base = (ci->base - oldstack) + L->stack;
135 ci->func = (ci->func - oldstack) + L->stack;
136 }
137 L->base = (L->base - oldstack) + L->stack;
138}
139
140
141void luaD_reallocstack (lua_State *L, int newsize) {
142 TValue *oldstack = L->stack;
143 int realsize = newsize + 1 + EXTRA_STACK;
144 lua_assert(L->stack_last - L->stack == L->stacksize - EXTRA_STACK - 1);
145 luaM_reallocvector(L, L->stack, L->stacksize, realsize, TValue);
146 L->stacksize = realsize;
147 L->stack_last = L->stack+newsize;
148 correctstack(L, oldstack);
149}
150
151
152void luaD_reallocCI (lua_State *L, int newsize) {
153 CallInfo *oldci = L->base_ci;
154 luaM_reallocvector(L, L->base_ci, L->size_ci, newsize, CallInfo);
155 L->size_ci = newsize;
156 L->ci = (L->ci - oldci) + L->base_ci;
157 L->end_ci = L->base_ci + L->size_ci - 1;
158}
159
160
161void luaD_growstack (lua_State *L, int n) {
162 if (n <= L->stacksize) /* double size is enough? */
163 luaD_reallocstack(L, 2*L->stacksize);
164 else
165 luaD_reallocstack(L, L->stacksize + n);
166}
167
168
169static CallInfo *growCI (lua_State *L) {
170 if (L->size_ci > LUAI_MAXCALLS) /* overflow while handling overflow? */
171 luaD_throw(L, LUA_ERRERR);
172 else {
173 luaD_reallocCI(L, 2*L->size_ci);
174 if (L->size_ci > LUAI_MAXCALLS)
175 luaG_runerror(L, "stack overflow");
176 }
177 return ++L->ci;
178}
179
180
181void luaD_callhook (lua_State *L, int event, int line) {
182 lua_Hook hook = L->hook;
183 if (hook && L->allowhook) {
184 ptrdiff_t top = savestack(L, L->top);
185 ptrdiff_t ci_top = savestack(L, L->ci->top);
186 lua_Debug ar;
187 ar.event = event;
188 ar.currentline = line;
189 if (event == LUA_HOOKTAILRET)
190 ar.i_ci = 0; /* tail call; no debug information about it */
191 else
192 ar.i_ci = cast_int(L->ci - L->base_ci);
193 luaD_checkstack(L, LUA_MINSTACK); /* ensure minimum stack size */
194 L->ci->top = L->top + LUA_MINSTACK;
195 lua_assert(L->ci->top <= L->stack_last);
196 L->allowhook = 0; /* cannot call hooks inside a hook */
197 lua_unlock(L);
198 (*hook)(L, &ar);
199 lua_lock(L);
200 lua_assert(!L->allowhook);
201 L->allowhook = 1;
202 L->ci->top = restorestack(L, ci_top);
203 L->top = restorestack(L, top);
204 }
205}
206
207
208static StkId adjust_varargs (lua_State *L, Proto *p, int actual) {
209 int i;
210 int nfixargs = p->numparams;
211 Table *htab = NULL;
212 StkId base, fixed;
213 for (; actual < nfixargs; ++actual)
214 setnilvalue(L->top++);
215#if defined(LUA_COMPAT_VARARG)
216 if (p->is_vararg & VARARG_NEEDSARG) { /* compat. with old-style vararg? */
217 int nvar = actual - nfixargs; /* number of extra arguments */
218 lua_assert(p->is_vararg & VARARG_HASARG);
219 luaC_checkGC(L);
220 luaD_checkstack(L, p->maxstacksize);
221 htab = luaH_new(L, nvar, 1); /* create `arg' table */
222 for (i=0; i<nvar; i++) /* put extra arguments into `arg' table */
223 setobj2n(L, luaH_setnum(L, htab, i+1), L->top - nvar + i);
224 /* store counter in field `n' */
225 setnvalue(luaH_setstr(L, htab, luaS_newliteral(L, "n")), cast_num(nvar));
226 }
227#endif
228 /* move fixed parameters to final position */
229 fixed = L->top - actual; /* first fixed argument */
230 base = L->top; /* final position of first argument */
231 for (i=0; i<nfixargs; i++) {
232 setobjs2s(L, L->top++, fixed+i);
233 setnilvalue(fixed+i);
234 }
235 /* add `arg' parameter */
236 if (htab) {
237 sethvalue(L, L->top++, htab);
238 lua_assert(iswhite(obj2gco(htab)));
239 }
240 return base;
241}
242
243
244static StkId tryfuncTM (lua_State *L, StkId func) {
245 const TValue *tm = luaT_gettmbyobj(L, func, TM_CALL);
246 StkId p;
247 ptrdiff_t funcr = savestack(L, func);
248 if (!ttisfunction(tm))
249 luaG_typeerror(L, func, "call");
250 /* Open a hole inside the stack at `func' */
251 for (p = L->top; p > func; p--) setobjs2s(L, p, p-1);
252 incr_top(L);
253 func = restorestack(L, funcr); /* previous call may change stack */
254 setobj2s(L, func, tm); /* tag method is the new function to be called */
255 return func;
256}
257
258
259
260#define inc_ci(L) \
261 ((L->ci == L->end_ci) ? growCI(L) : \
262 (condhardstacktests(luaD_reallocCI(L, L->size_ci)), ++L->ci))
263
264
265int luaD_precall (lua_State *L, StkId func, int nresults) {
266 LClosure *cl;
267 ptrdiff_t funcr;
268 if (!ttisfunction(func)) /* `func' is not a function? */
269 func = tryfuncTM(L, func); /* check the `function' tag method */
270 funcr = savestack(L, func);
271 cl = &clvalue(func)->l;
272 L->ci->savedpc = L->savedpc;
273 if (!cl->isC) { /* Lua function? prepare its call */
274 CallInfo *ci;
275 StkId st, base;
276 Proto *p = cl->p;
277 luaD_checkstack(L, p->maxstacksize + p->numparams);
278 func = restorestack(L, funcr);
279 if (!p->is_vararg) { /* no varargs? */
280 base = func + 1;
281 if (L->top > base + p->numparams)
282 L->top = base + p->numparams;
283 }
284 else { /* vararg function */
285 int nargs = cast_int(L->top - func) - 1;
286 base = adjust_varargs(L, p, nargs);
287 func = restorestack(L, funcr); /* previous call may change the stack */
288 }
289 ci = inc_ci(L); /* now `enter' new function */
290 ci->func = func;
291 L->base = ci->base = base;
292 ci->top = L->base + p->maxstacksize;
293 lua_assert(ci->top <= L->stack_last);
294 L->savedpc = p->code; /* starting point */
295 ci->tailcalls = 0;
296 ci->nresults = nresults;
297 for (st = L->top; st < ci->top; st++)
298 setnilvalue(st);
299 L->top = ci->top;
300 if (L->hookmask & LUA_MASKCALL) {
301 L->savedpc++; /* hooks assume 'pc' is already incremented */
302 luaD_callhook(L, LUA_HOOKCALL, -1);
303 L->savedpc--; /* correct 'pc' */
304 }
305 return PCRLUA;
306 }
307 else { /* if is a C function, call it */
308 CallInfo *ci;
309 int n;
310 luaD_checkstack(L, LUA_MINSTACK); /* ensure minimum stack size */
311 ci = inc_ci(L); /* now `enter' new function */
312 ci->func = restorestack(L, funcr);
313 L->base = ci->base = ci->func + 1;
314 ci->top = L->top + LUA_MINSTACK;
315 lua_assert(ci->top <= L->stack_last);
316 ci->nresults = nresults;
317 if (L->hookmask & LUA_MASKCALL)
318 luaD_callhook(L, LUA_HOOKCALL, -1);
319 lua_unlock(L);
320 n = (*curr_func(L)->c.f)(L); /* do the actual call */
321 lua_lock(L);
322 if (n < 0) /* yielding? */
323 return PCRYIELD;
324 else {
325 luaD_poscall(L, L->top - n);
326 return PCRC;
327 }
328 }
329}
330
331
332static StkId callrethooks (lua_State *L, StkId firstResult) {
333 ptrdiff_t fr = savestack(L, firstResult); /* next call may change stack */
334 luaD_callhook(L, LUA_HOOKRET, -1);
335 if (f_isLua(L->ci)) { /* Lua function? */
336 while ((L->hookmask & LUA_MASKRET) && L->ci->tailcalls--) /* tail calls */
337 luaD_callhook(L, LUA_HOOKTAILRET, -1);
338 }
339 return restorestack(L, fr);
340}
341
342
343int luaD_poscall (lua_State *L, StkId firstResult) {
344 StkId res;
345 int wanted, i;
346 CallInfo *ci;
347 if (L->hookmask & LUA_MASKRET)
348 firstResult = callrethooks(L, firstResult);
349 ci = L->ci--;
350 res = ci->func; /* res == final position of 1st result */
351 wanted = ci->nresults;
352 L->base = (ci - 1)->base; /* restore base */
353 L->savedpc = (ci - 1)->savedpc; /* restore savedpc */
354 /* move results to correct place */
355 for (i = wanted; i != 0 && firstResult < L->top; i--)
356 setobjs2s(L, res++, firstResult++);
357 while (i-- > 0)
358 setnilvalue(res++);
359 L->top = res;
360 return (wanted - LUA_MULTRET); /* 0 iff wanted == LUA_MULTRET */
361}
362
363
364/*
365** Call a function (C or Lua). The function to be called is at *func.
366** The arguments are on the stack, right after the function.
367** When returns, all the results are on the stack, starting at the original
368** function position.
369*/
370void luaD_call (lua_State *L, StkId func, int nResults) {
371 if (++L->nCcalls >= LUAI_MAXCCALLS) {
372 if (L->nCcalls == LUAI_MAXCCALLS)
373 luaG_runerror(L, "C stack overflow");
374 else if (L->nCcalls >= (LUAI_MAXCCALLS + (LUAI_MAXCCALLS>>3)))
375 luaD_throw(L, LUA_ERRERR); /* error while handing stack error */
376 }
377 if (luaD_precall(L, func, nResults) == PCRLUA) /* is a Lua function? */
378 luaV_execute(L, 1); /* call it */
379 L->nCcalls--;
380 luaC_checkGC(L);
381}
382
383
384static void resume (lua_State *L, void *ud) {
385 StkId firstArg = cast(StkId, ud);
386 CallInfo *ci = L->ci;
387 if (L->status == 0) { /* start coroutine? */
388 lua_assert(ci == L->base_ci && firstArg > L->base);
389 if (luaD_precall(L, firstArg - 1, LUA_MULTRET) != PCRLUA)
390 return;
391 }
392 else { /* resuming from previous yield */
393 lua_assert(L->status == LUA_YIELD);
394 L->status = 0;
395 if (!f_isLua(ci)) { /* `common' yield? */
396 /* finish interrupted execution of `OP_CALL' */
397 lua_assert(GET_OPCODE(*((ci-1)->savedpc - 1)) == OP_CALL ||
398 GET_OPCODE(*((ci-1)->savedpc - 1)) == OP_TAILCALL);
399 if (luaD_poscall(L, firstArg)) /* complete it... */
400 L->top = L->ci->top; /* and correct top if not multiple results */
401 }
402 else /* yielded inside a hook: just continue its execution */
403 L->base = L->ci->base;
404 }
405 luaV_execute(L, cast_int(L->ci - L->base_ci));
406}
407
408
409static int resume_error (lua_State *L, const char *msg) {
410 L->top = L->ci->base;
411 setsvalue2s(L, L->top, luaS_new(L, msg));
412 incr_top(L);
413 lua_unlock(L);
414 return LUA_ERRRUN;
415}
416
417
418LUA_API int lua_resume (lua_State *L, int nargs) {
419 int status;
420 lua_lock(L);
421 if (L->status != LUA_YIELD && (L->status != 0 || L->ci != L->base_ci))
422 return resume_error(L, "cannot resume non-suspended coroutine");
423 if (L->nCcalls >= LUAI_MAXCCALLS)
424 return resume_error(L, "C stack overflow");
425 luai_userstateresume(L, nargs);
426 lua_assert(L->errfunc == 0);
427 L->baseCcalls = ++L->nCcalls;
428 status = luaD_rawrunprotected(L, resume, L->top - nargs);
429 if (status != 0) { /* error? */
430 L->status = cast_byte(status); /* mark thread as `dead' */
431 luaD_seterrorobj(L, status, L->top);
432 L->ci->top = L->top;
433 }
434 else {
435 lua_assert(L->nCcalls == L->baseCcalls);
436 status = L->status;
437 }
438 --L->nCcalls;
439 lua_unlock(L);
440 return status;
441}
442
443
444LUA_API int lua_yield (lua_State *L, int nresults) {
445 luai_userstateyield(L, nresults);
446 lua_lock(L);
447 if (L->nCcalls > L->baseCcalls)
448 luaG_runerror(L, "attempt to yield across metamethod/C-call boundary");
449 L->base = L->top - nresults; /* protect stack slots below */
450 L->status = LUA_YIELD;
451 lua_unlock(L);
452 return -1;
453}
454
455
456int luaD_pcall (lua_State *L, Pfunc func, void *u,
457 ptrdiff_t old_top, ptrdiff_t ef) {
458 int status;
459 unsigned short oldnCcalls = L->nCcalls;
460 ptrdiff_t old_ci = saveci(L, L->ci);
461 lu_byte old_allowhooks = L->allowhook;
462 ptrdiff_t old_errfunc = L->errfunc;
463 L->errfunc = ef;
464 status = luaD_rawrunprotected(L, func, u);
465 if (status != 0) { /* an error occurred? */
466 StkId oldtop = restorestack(L, old_top);
467 luaF_close(L, oldtop); /* close eventual pending closures */
468 luaD_seterrorobj(L, status, oldtop);
469 L->nCcalls = oldnCcalls;
470 L->ci = restoreci(L, old_ci);
471 L->base = L->ci->base;
472 L->savedpc = L->ci->savedpc;
473 L->allowhook = old_allowhooks;
474 restore_stack_limit(L);
475 }
476 L->errfunc = old_errfunc;
477 return status;
478}
479
480
481
482/*
483** Execute a protected parser.
484*/
485struct SParser { /* data to `f_parser' */
486 ZIO *z;
487 Mbuffer buff; /* buffer to be used by the scanner */
488 const char *name;
489};
490
491static void f_parser (lua_State *L, void *ud) {
492 int i;
493 Proto *tf;
494 Closure *cl;
495 struct SParser *p = cast(struct SParser *, ud);
496 luaZ_lookahead(p->z);
497 luaC_checkGC(L);
498 tf = (luaY_parser)(L, p->z,
499 &p->buff, p->name);
500 cl = luaF_newLclosure(L, tf->nups, hvalue(gt(L)));
501 cl->l.p = tf;
502 for (i = 0; i < tf->nups; i++) /* initialize eventual upvalues */
503 cl->l.upvals[i] = luaF_newupval(L);
504 setclvalue(L, L->top, cl);
505 incr_top(L);
506}
507
508
509int luaD_protectedparser (lua_State *L, ZIO *z, const char *name) {
510 struct SParser p;
511 int status;
512 p.z = z; p.name = name;
513 luaZ_initbuffer(L, &p.buff);
514 status = luaD_pcall(L, f_parser, &p, savestack(L, L->top), L->errfunc);
515 luaZ_freebuffer(L, &p.buff);
516 return status;
517}
518
519
diff --git a/examples/redis-unstable/deps/lua/src/ldo.h b/examples/redis-unstable/deps/lua/src/ldo.h
deleted file mode 100644
index 98fddac..0000000
--- a/examples/redis-unstable/deps/lua/src/ldo.h
+++ /dev/null
@@ -1,57 +0,0 @@
1/*
2** $Id: ldo.h,v 2.7.1.1 2007/12/27 13:02:25 roberto Exp $
3** Stack and Call structure of Lua
4** See Copyright Notice in lua.h
5*/
6
7#ifndef ldo_h
8#define ldo_h
9
10
11#include "lobject.h"
12#include "lstate.h"
13#include "lzio.h"
14
15
16#define luaD_checkstack(L,n) \
17 if ((char *)L->stack_last - (char *)L->top <= (n)*(int)sizeof(TValue)) \
18 luaD_growstack(L, n); \
19 else condhardstacktests(luaD_reallocstack(L, L->stacksize - EXTRA_STACK - 1));
20
21
22#define incr_top(L) {luaD_checkstack(L,1); L->top++;}
23
24#define savestack(L,p) ((char *)(p) - (char *)L->stack)
25#define restorestack(L,n) ((TValue *)((char *)L->stack + (n)))
26
27#define saveci(L,p) ((char *)(p) - (char *)L->base_ci)
28#define restoreci(L,n) ((CallInfo *)((char *)L->base_ci + (n)))
29
30
31/* results from luaD_precall */
32#define PCRLUA 0 /* initiated a call to a Lua function */
33#define PCRC 1 /* did a call to a C function */
34#define PCRYIELD 2 /* C funtion yielded */
35
36
37/* type of protected functions, to be ran by `runprotected' */
38typedef void (*Pfunc) (lua_State *L, void *ud);
39
40LUAI_FUNC int luaD_protectedparser (lua_State *L, ZIO *z, const char *name);
41LUAI_FUNC void luaD_callhook (lua_State *L, int event, int line);
42LUAI_FUNC int luaD_precall (lua_State *L, StkId func, int nresults);
43LUAI_FUNC void luaD_call (lua_State *L, StkId func, int nResults);
44LUAI_FUNC int luaD_pcall (lua_State *L, Pfunc func, void *u,
45 ptrdiff_t oldtop, ptrdiff_t ef);
46LUAI_FUNC int luaD_poscall (lua_State *L, StkId firstResult);
47LUAI_FUNC void luaD_reallocCI (lua_State *L, int newsize);
48LUAI_FUNC void luaD_reallocstack (lua_State *L, int newsize);
49LUAI_FUNC void luaD_growstack (lua_State *L, int n);
50
51LUAI_FUNC void luaD_throw (lua_State *L, int errcode);
52LUAI_FUNC int luaD_rawrunprotected (lua_State *L, Pfunc f, void *ud);
53
54LUAI_FUNC void luaD_seterrorobj (lua_State *L, int errcode, StkId oldtop);
55
56#endif
57
diff --git a/examples/redis-unstable/deps/lua/src/ldump.c b/examples/redis-unstable/deps/lua/src/ldump.c
deleted file mode 100644
index b69a127..0000000
--- a/examples/redis-unstable/deps/lua/src/ldump.c
+++ /dev/null
@@ -1,164 +0,0 @@
1/*
2** $Id: ldump.c,v 2.8.1.1 2007/12/27 13:02:25 roberto Exp $
3** save precompiled Lua chunks
4** See Copyright Notice in lua.h
5*/
6
7#include <stddef.h>
8
9#define ldump_c
10#define LUA_CORE
11
12#include "lua.h"
13
14#include "lobject.h"
15#include "lstate.h"
16#include "lundump.h"
17
18typedef struct {
19 lua_State* L;
20 lua_Writer writer;
21 void* data;
22 int strip;
23 int status;
24} DumpState;
25
26#define DumpMem(b,n,size,D) DumpBlock(b,(n)*(size),D)
27#define DumpVar(x,D) DumpMem(&x,1,sizeof(x),D)
28
29static void DumpBlock(const void* b, size_t size, DumpState* D)
30{
31 if (D->status==0)
32 {
33 lua_unlock(D->L);
34 D->status=(*D->writer)(D->L,b,size,D->data);
35 lua_lock(D->L);
36 }
37}
38
39static void DumpChar(int y, DumpState* D)
40{
41 char x=(char)y;
42 DumpVar(x,D);
43}
44
45static void DumpInt(int x, DumpState* D)
46{
47 DumpVar(x,D);
48}
49
50static void DumpNumber(lua_Number x, DumpState* D)
51{
52 DumpVar(x,D);
53}
54
55static void DumpVector(const void* b, int n, size_t size, DumpState* D)
56{
57 DumpInt(n,D);
58 DumpMem(b,n,size,D);
59}
60
61static void DumpString(const TString* s, DumpState* D)
62{
63 if (s==NULL)
64 {
65 size_t size=0;
66 DumpVar(size,D);
67 }
68 else
69 {
70 size_t size=s->tsv.len+1; /* include trailing '\0' */
71 DumpVar(size,D);
72 DumpBlock(getstr(s),size,D);
73 }
74}
75
76#define DumpCode(f,D) DumpVector(f->code,f->sizecode,sizeof(Instruction),D)
77
78static void DumpFunction(const Proto* f, const TString* p, DumpState* D);
79
80static void DumpConstants(const Proto* f, DumpState* D)
81{
82 int i,n=f->sizek;
83 DumpInt(n,D);
84 for (i=0; i<n; i++)
85 {
86 const TValue* o=&f->k[i];
87 DumpChar(ttype(o),D);
88 switch (ttype(o))
89 {
90 case LUA_TNIL:
91 break;
92 case LUA_TBOOLEAN:
93 DumpChar(bvalue(o),D);
94 break;
95 case LUA_TNUMBER:
96 DumpNumber(nvalue(o),D);
97 break;
98 case LUA_TSTRING:
99 DumpString(rawtsvalue(o),D);
100 break;
101 default:
102 lua_assert(0); /* cannot happen */
103 break;
104 }
105 }
106 n=f->sizep;
107 DumpInt(n,D);
108 for (i=0; i<n; i++) DumpFunction(f->p[i],f->source,D);
109}
110
111static void DumpDebug(const Proto* f, DumpState* D)
112{
113 int i,n;
114 n= (D->strip) ? 0 : f->sizelineinfo;
115 DumpVector(f->lineinfo,n,sizeof(int),D);
116 n= (D->strip) ? 0 : f->sizelocvars;
117 DumpInt(n,D);
118 for (i=0; i<n; i++)
119 {
120 DumpString(f->locvars[i].varname,D);
121 DumpInt(f->locvars[i].startpc,D);
122 DumpInt(f->locvars[i].endpc,D);
123 }
124 n= (D->strip) ? 0 : f->sizeupvalues;
125 DumpInt(n,D);
126 for (i=0; i<n; i++) DumpString(f->upvalues[i],D);
127}
128
129static void DumpFunction(const Proto* f, const TString* p, DumpState* D)
130{
131 DumpString((f->source==p || D->strip) ? NULL : f->source,D);
132 DumpInt(f->linedefined,D);
133 DumpInt(f->lastlinedefined,D);
134 DumpChar(f->nups,D);
135 DumpChar(f->numparams,D);
136 DumpChar(f->is_vararg,D);
137 DumpChar(f->maxstacksize,D);
138 DumpCode(f,D);
139 DumpConstants(f,D);
140 DumpDebug(f,D);
141}
142
143static void DumpHeader(DumpState* D)
144{
145 char h[LUAC_HEADERSIZE];
146 luaU_header(h);
147 DumpBlock(h,LUAC_HEADERSIZE,D);
148}
149
150/*
151** dump Lua function as precompiled chunk
152*/
153int luaU_dump (lua_State* L, const Proto* f, lua_Writer w, void* data, int strip)
154{
155 DumpState D;
156 D.L=L;
157 D.writer=w;
158 D.data=data;
159 D.strip=strip;
160 D.status=0;
161 DumpHeader(&D);
162 DumpFunction(f,NULL,&D);
163 return D.status;
164}
diff --git a/examples/redis-unstable/deps/lua/src/lfunc.c b/examples/redis-unstable/deps/lua/src/lfunc.c
deleted file mode 100644
index 813e88f..0000000
--- a/examples/redis-unstable/deps/lua/src/lfunc.c
+++ /dev/null
@@ -1,174 +0,0 @@
1/*
2** $Id: lfunc.c,v 2.12.1.2 2007/12/28 14:58:43 roberto Exp $
3** Auxiliary functions to manipulate prototypes and closures
4** See Copyright Notice in lua.h
5*/
6
7
8#include <stddef.h>
9
10#define lfunc_c
11#define LUA_CORE
12
13#include "lua.h"
14
15#include "lfunc.h"
16#include "lgc.h"
17#include "lmem.h"
18#include "lobject.h"
19#include "lstate.h"
20
21
22
23Closure *luaF_newCclosure (lua_State *L, int nelems, Table *e) {
24 Closure *c = cast(Closure *, luaM_malloc(L, sizeCclosure(nelems)));
25 luaC_link(L, obj2gco(c), LUA_TFUNCTION);
26 c->c.isC = 1;
27 c->c.env = e;
28 c->c.nupvalues = cast_byte(nelems);
29 return c;
30}
31
32
33Closure *luaF_newLclosure (lua_State *L, int nelems, Table *e) {
34 Closure *c = cast(Closure *, luaM_malloc(L, sizeLclosure(nelems)));
35 luaC_link(L, obj2gco(c), LUA_TFUNCTION);
36 c->l.isC = 0;
37 c->l.env = e;
38 c->l.nupvalues = cast_byte(nelems);
39 while (nelems--) c->l.upvals[nelems] = NULL;
40 return c;
41}
42
43
44UpVal *luaF_newupval (lua_State *L) {
45 UpVal *uv = luaM_new(L, UpVal);
46 luaC_link(L, obj2gco(uv), LUA_TUPVAL);
47 uv->v = &uv->u.value;
48 setnilvalue(uv->v);
49 return uv;
50}
51
52
53UpVal *luaF_findupval (lua_State *L, StkId level) {
54 global_State *g = G(L);
55 GCObject **pp = &L->openupval;
56 UpVal *p;
57 UpVal *uv;
58 while (*pp != NULL && (p = ngcotouv(*pp))->v >= level) {
59 lua_assert(p->v != &p->u.value);
60 if (p->v == level) { /* found a corresponding upvalue? */
61 if (isdead(g, obj2gco(p))) /* is it dead? */
62 changewhite(obj2gco(p)); /* ressurect it */
63 return p;
64 }
65 pp = &p->next;
66 }
67 uv = luaM_new(L, UpVal); /* not found: create a new one */
68 uv->tt = LUA_TUPVAL;
69 uv->marked = luaC_white(g);
70 uv->v = level; /* current value lives in the stack */
71 uv->next = *pp; /* chain it in the proper position */
72 *pp = obj2gco(uv);
73 uv->u.l.prev = &g->uvhead; /* double link it in `uvhead' list */
74 uv->u.l.next = g->uvhead.u.l.next;
75 uv->u.l.next->u.l.prev = uv;
76 g->uvhead.u.l.next = uv;
77 lua_assert(uv->u.l.next->u.l.prev == uv && uv->u.l.prev->u.l.next == uv);
78 return uv;
79}
80
81
82static void unlinkupval (UpVal *uv) {
83 lua_assert(uv->u.l.next->u.l.prev == uv && uv->u.l.prev->u.l.next == uv);
84 uv->u.l.next->u.l.prev = uv->u.l.prev; /* remove from `uvhead' list */
85 uv->u.l.prev->u.l.next = uv->u.l.next;
86}
87
88
89void luaF_freeupval (lua_State *L, UpVal *uv) {
90 if (uv->v != &uv->u.value) /* is it open? */
91 unlinkupval(uv); /* remove from open list */
92 luaM_free(L, uv); /* free upvalue */
93}
94
95
96void luaF_close (lua_State *L, StkId level) {
97 UpVal *uv;
98 global_State *g = G(L);
99 while (L->openupval != NULL && (uv = ngcotouv(L->openupval))->v >= level) {
100 GCObject *o = obj2gco(uv);
101 lua_assert(!isblack(o) && uv->v != &uv->u.value);
102 L->openupval = uv->next; /* remove from `open' list */
103 if (isdead(g, o))
104 luaF_freeupval(L, uv); /* free upvalue */
105 else {
106 unlinkupval(uv);
107 setobj(L, &uv->u.value, uv->v);
108 uv->v = &uv->u.value; /* now current value lives here */
109 luaC_linkupval(L, uv); /* link upvalue into `gcroot' list */
110 }
111 }
112}
113
114
115Proto *luaF_newproto (lua_State *L) {
116 Proto *f = luaM_new(L, Proto);
117 luaC_link(L, obj2gco(f), LUA_TPROTO);
118 f->k = NULL;
119 f->sizek = 0;
120 f->p = NULL;
121 f->sizep = 0;
122 f->code = NULL;
123 f->sizecode = 0;
124 f->sizelineinfo = 0;
125 f->sizeupvalues = 0;
126 f->nups = 0;
127 f->upvalues = NULL;
128 f->numparams = 0;
129 f->is_vararg = 0;
130 f->maxstacksize = 0;
131 f->lineinfo = NULL;
132 f->sizelocvars = 0;
133 f->locvars = NULL;
134 f->linedefined = 0;
135 f->lastlinedefined = 0;
136 f->source = NULL;
137 return f;
138}
139
140
141void luaF_freeproto (lua_State *L, Proto *f) {
142 luaM_freearray(L, f->code, f->sizecode, Instruction);
143 luaM_freearray(L, f->p, f->sizep, Proto *);
144 luaM_freearray(L, f->k, f->sizek, TValue);
145 luaM_freearray(L, f->lineinfo, f->sizelineinfo, int);
146 luaM_freearray(L, f->locvars, f->sizelocvars, struct LocVar);
147 luaM_freearray(L, f->upvalues, f->sizeupvalues, TString *);
148 luaM_free(L, f);
149}
150
151
152void luaF_freeclosure (lua_State *L, Closure *c) {
153 int size = (c->c.isC) ? sizeCclosure(c->c.nupvalues) :
154 sizeLclosure(c->l.nupvalues);
155 luaM_freemem(L, c, size);
156}
157
158
159/*
160** Look for n-th local variable at line `line' in function `func'.
161** Returns NULL if not found.
162*/
163const char *luaF_getlocalname (const Proto *f, int local_number, int pc) {
164 int i;
165 for (i = 0; i<f->sizelocvars && f->locvars[i].startpc <= pc; i++) {
166 if (pc < f->locvars[i].endpc) { /* is variable active? */
167 local_number--;
168 if (local_number == 0)
169 return getstr(f->locvars[i].varname);
170 }
171 }
172 return NULL; /* not found */
173}
174
diff --git a/examples/redis-unstable/deps/lua/src/lfunc.h b/examples/redis-unstable/deps/lua/src/lfunc.h
deleted file mode 100644
index a68cf51..0000000
--- a/examples/redis-unstable/deps/lua/src/lfunc.h
+++ /dev/null
@@ -1,34 +0,0 @@
1/*
2** $Id: lfunc.h,v 2.4.1.1 2007/12/27 13:02:25 roberto Exp $
3** Auxiliary functions to manipulate prototypes and closures
4** See Copyright Notice in lua.h
5*/
6
7#ifndef lfunc_h
8#define lfunc_h
9
10
11#include "lobject.h"
12
13
14#define sizeCclosure(n) (cast(int, sizeof(CClosure)) + \
15 cast(int, sizeof(TValue)*((n)-1)))
16
17#define sizeLclosure(n) (cast(int, sizeof(LClosure)) + \
18 cast(int, sizeof(TValue *)*((n)-1)))
19
20
21LUAI_FUNC Proto *luaF_newproto (lua_State *L);
22LUAI_FUNC Closure *luaF_newCclosure (lua_State *L, int nelems, Table *e);
23LUAI_FUNC Closure *luaF_newLclosure (lua_State *L, int nelems, Table *e);
24LUAI_FUNC UpVal *luaF_newupval (lua_State *L);
25LUAI_FUNC UpVal *luaF_findupval (lua_State *L, StkId level);
26LUAI_FUNC void luaF_close (lua_State *L, StkId level);
27LUAI_FUNC void luaF_freeproto (lua_State *L, Proto *f);
28LUAI_FUNC void luaF_freeclosure (lua_State *L, Closure *c);
29LUAI_FUNC void luaF_freeupval (lua_State *L, UpVal *uv);
30LUAI_FUNC const char *luaF_getlocalname (const Proto *func, int local_number,
31 int pc);
32
33
34#endif
diff --git a/examples/redis-unstable/deps/lua/src/lgc.c b/examples/redis-unstable/deps/lua/src/lgc.c
deleted file mode 100644
index e909c79..0000000
--- a/examples/redis-unstable/deps/lua/src/lgc.c
+++ /dev/null
@@ -1,710 +0,0 @@
1/*
2** $Id: lgc.c,v 2.38.1.2 2011/03/18 18:05:38 roberto Exp $
3** Garbage Collector
4** See Copyright Notice in lua.h
5*/
6
7#include <string.h>
8
9#define lgc_c
10#define LUA_CORE
11
12#include "lua.h"
13
14#include "ldebug.h"
15#include "ldo.h"
16#include "lfunc.h"
17#include "lgc.h"
18#include "lmem.h"
19#include "lobject.h"
20#include "lstate.h"
21#include "lstring.h"
22#include "ltable.h"
23#include "ltm.h"
24
25
26#define GCSTEPSIZE 1024u
27#define GCSWEEPMAX 40
28#define GCSWEEPCOST 10
29#define GCFINALIZECOST 100
30
31
32#define maskmarks cast_byte(~(bitmask(BLACKBIT)|WHITEBITS))
33
34#define makewhite(g,x) \
35 ((x)->gch.marked = cast_byte(((x)->gch.marked & maskmarks) | luaC_white(g)))
36
37#define white2gray(x) reset2bits((x)->gch.marked, WHITE0BIT, WHITE1BIT)
38#define black2gray(x) resetbit((x)->gch.marked, BLACKBIT)
39
40#define stringmark(s) reset2bits((s)->tsv.marked, WHITE0BIT, WHITE1BIT)
41
42
43#define isfinalized(u) testbit((u)->marked, FINALIZEDBIT)
44#define markfinalized(u) l_setbit((u)->marked, FINALIZEDBIT)
45
46
47#define KEYWEAK bitmask(KEYWEAKBIT)
48#define VALUEWEAK bitmask(VALUEWEAKBIT)
49
50
51
52#define markvalue(g,o) { checkconsistency(o); \
53 if (iscollectable(o) && iswhite(gcvalue(o))) reallymarkobject(g,gcvalue(o)); }
54
55#define markobject(g,t) { if (iswhite(obj2gco(t))) \
56 reallymarkobject(g, obj2gco(t)); }
57
58
59#define setthreshold(g) (g->GCthreshold = (g->estimate/100) * g->gcpause)
60
61
62static void removeentry (Node *n) {
63 lua_assert(ttisnil(gval(n)));
64 if (iscollectable(gkey(n)))
65 setttype(gkey(n), LUA_TDEADKEY); /* dead key; remove it */
66}
67
68
69static void reallymarkobject (global_State *g, GCObject *o) {
70 lua_assert(iswhite(o) && !isdead(g, o));
71 white2gray(o);
72 switch (o->gch.tt) {
73 case LUA_TSTRING: {
74 return;
75 }
76 case LUA_TUSERDATA: {
77 Table *mt = gco2u(o)->metatable;
78 gray2black(o); /* udata are never gray */
79 if (mt) markobject(g, mt);
80 markobject(g, gco2u(o)->env);
81 return;
82 }
83 case LUA_TUPVAL: {
84 UpVal *uv = gco2uv(o);
85 markvalue(g, uv->v);
86 if (uv->v == &uv->u.value) /* closed? */
87 gray2black(o); /* open upvalues are never black */
88 return;
89 }
90 case LUA_TFUNCTION: {
91 gco2cl(o)->c.gclist = g->gray;
92 g->gray = o;
93 break;
94 }
95 case LUA_TTABLE: {
96 gco2h(o)->gclist = g->gray;
97 g->gray = o;
98 break;
99 }
100 case LUA_TTHREAD: {
101 gco2th(o)->gclist = g->gray;
102 g->gray = o;
103 break;
104 }
105 case LUA_TPROTO: {
106 gco2p(o)->gclist = g->gray;
107 g->gray = o;
108 break;
109 }
110 default: lua_assert(0);
111 }
112}
113
114
115static void marktmu (global_State *g) {
116 GCObject *u = g->tmudata;
117 if (u) {
118 do {
119 u = u->gch.next;
120 makewhite(g, u); /* may be marked, if left from previous GC */
121 reallymarkobject(g, u);
122 } while (u != g->tmudata);
123 }
124}
125
126
127/* move `dead' udata that need finalization to list `tmudata' */
128size_t luaC_separateudata (lua_State *L, int all) {
129 global_State *g = G(L);
130 size_t deadmem = 0;
131 GCObject **p = &g->mainthread->next;
132 GCObject *curr;
133 while ((curr = *p) != NULL) {
134 if (!(iswhite(curr) || all) || isfinalized(gco2u(curr)))
135 p = &curr->gch.next; /* don't bother with them */
136 else if (fasttm(L, gco2u(curr)->metatable, TM_GC) == NULL) {
137 markfinalized(gco2u(curr)); /* don't need finalization */
138 p = &curr->gch.next;
139 }
140 else { /* must call its gc method */
141 deadmem += sizeudata(gco2u(curr));
142 markfinalized(gco2u(curr));
143 *p = curr->gch.next;
144 /* link `curr' at the end of `tmudata' list */
145 if (g->tmudata == NULL) /* list is empty? */
146 g->tmudata = curr->gch.next = curr; /* creates a circular list */
147 else {
148 curr->gch.next = g->tmudata->gch.next;
149 g->tmudata->gch.next = curr;
150 g->tmudata = curr;
151 }
152 }
153 }
154 return deadmem;
155}
156
157
158static int traversetable (global_State *g, Table *h) {
159 int i;
160 int weakkey = 0;
161 int weakvalue = 0;
162 const TValue *mode;
163 if (h->metatable)
164 markobject(g, h->metatable);
165 mode = gfasttm(g, h->metatable, TM_MODE);
166 if (mode && ttisstring(mode)) { /* is there a weak mode? */
167 weakkey = (strchr(svalue(mode), 'k') != NULL);
168 weakvalue = (strchr(svalue(mode), 'v') != NULL);
169 if (weakkey || weakvalue) { /* is really weak? */
170 h->marked &= ~(KEYWEAK | VALUEWEAK); /* clear bits */
171 h->marked |= cast_byte((weakkey << KEYWEAKBIT) |
172 (weakvalue << VALUEWEAKBIT));
173 h->gclist = g->weak; /* must be cleared after GC, ... */
174 g->weak = obj2gco(h); /* ... so put in the appropriate list */
175 }
176 }
177 if (weakkey && weakvalue) return 1;
178 if (!weakvalue) {
179 i = h->sizearray;
180 while (i--)
181 markvalue(g, &h->array[i]);
182 }
183 i = sizenode(h);
184 while (i--) {
185 Node *n = gnode(h, i);
186 lua_assert(ttype(gkey(n)) != LUA_TDEADKEY || ttisnil(gval(n)));
187 if (ttisnil(gval(n)))
188 removeentry(n); /* remove empty entries */
189 else {
190 lua_assert(!ttisnil(gkey(n)));
191 if (!weakkey) markvalue(g, gkey(n));
192 if (!weakvalue) markvalue(g, gval(n));
193 }
194 }
195 return weakkey || weakvalue;
196}
197
198
199/*
200** All marks are conditional because a GC may happen while the
201** prototype is still being created
202*/
203static void traverseproto (global_State *g, Proto *f) {
204 int i;
205 if (f->source) stringmark(f->source);
206 for (i=0; i<f->sizek; i++) /* mark literals */
207 markvalue(g, &f->k[i]);
208 for (i=0; i<f->sizeupvalues; i++) { /* mark upvalue names */
209 if (f->upvalues[i])
210 stringmark(f->upvalues[i]);
211 }
212 for (i=0; i<f->sizep; i++) { /* mark nested protos */
213 if (f->p[i])
214 markobject(g, f->p[i]);
215 }
216 for (i=0; i<f->sizelocvars; i++) { /* mark local-variable names */
217 if (f->locvars[i].varname)
218 stringmark(f->locvars[i].varname);
219 }
220}
221
222
223
224static void traverseclosure (global_State *g, Closure *cl) {
225 markobject(g, cl->c.env);
226 if (cl->c.isC) {
227 int i;
228 for (i=0; i<cl->c.nupvalues; i++) /* mark its upvalues */
229 markvalue(g, &cl->c.upvalue[i]);
230 }
231 else {
232 int i;
233 lua_assert(cl->l.nupvalues == cl->l.p->nups);
234 markobject(g, cl->l.p);
235 for (i=0; i<cl->l.nupvalues; i++) /* mark its upvalues */
236 markobject(g, cl->l.upvals[i]);
237 }
238}
239
240
241static void checkstacksizes (lua_State *L, StkId max) {
242 int ci_used = cast_int(L->ci - L->base_ci); /* number of `ci' in use */
243 int s_used = cast_int(max - L->stack); /* part of stack in use */
244 if (L->size_ci > LUAI_MAXCALLS) /* handling overflow? */
245 return; /* do not touch the stacks */
246 if (4*ci_used < L->size_ci && 2*BASIC_CI_SIZE < L->size_ci)
247 luaD_reallocCI(L, L->size_ci/2); /* still big enough... */
248 condhardstacktests(luaD_reallocCI(L, ci_used + 1));
249 if (4*s_used < L->stacksize &&
250 2*(BASIC_STACK_SIZE+EXTRA_STACK) < L->stacksize)
251 luaD_reallocstack(L, L->stacksize/2); /* still big enough... */
252 condhardstacktests(luaD_reallocstack(L, s_used));
253}
254
255
256static void traversestack (global_State *g, lua_State *l) {
257 StkId o, lim;
258 CallInfo *ci;
259 markvalue(g, gt(l));
260 lim = l->top;
261 for (ci = l->base_ci; ci <= l->ci; ci++) {
262 lua_assert(ci->top <= l->stack_last);
263 if (lim < ci->top) lim = ci->top;
264 }
265 for (o = l->stack; o < l->top; o++)
266 markvalue(g, o);
267 for (; o <= lim; o++)
268 setnilvalue(o);
269 checkstacksizes(l, lim);
270}
271
272
273/*
274** traverse one gray object, turning it to black.
275** Returns `quantity' traversed.
276*/
277static l_mem propagatemark (global_State *g) {
278 GCObject *o = g->gray;
279 lua_assert(isgray(o));
280 gray2black(o);
281 switch (o->gch.tt) {
282 case LUA_TTABLE: {
283 Table *h = gco2h(o);
284 g->gray = h->gclist;
285 if (traversetable(g, h)) /* table is weak? */
286 black2gray(o); /* keep it gray */
287 return sizeof(Table) + sizeof(TValue) * h->sizearray +
288 sizeof(Node) * sizenode(h);
289 }
290 case LUA_TFUNCTION: {
291 Closure *cl = gco2cl(o);
292 g->gray = cl->c.gclist;
293 traverseclosure(g, cl);
294 return (cl->c.isC) ? sizeCclosure(cl->c.nupvalues) :
295 sizeLclosure(cl->l.nupvalues);
296 }
297 case LUA_TTHREAD: {
298 lua_State *th = gco2th(o);
299 g->gray = th->gclist;
300 th->gclist = g->grayagain;
301 g->grayagain = o;
302 black2gray(o);
303 traversestack(g, th);
304 return sizeof(lua_State) + sizeof(TValue) * th->stacksize +
305 sizeof(CallInfo) * th->size_ci;
306 }
307 case LUA_TPROTO: {
308 Proto *p = gco2p(o);
309 g->gray = p->gclist;
310 traverseproto(g, p);
311 return sizeof(Proto) + sizeof(Instruction) * p->sizecode +
312 sizeof(Proto *) * p->sizep +
313 sizeof(TValue) * p->sizek +
314 sizeof(int) * p->sizelineinfo +
315 sizeof(LocVar) * p->sizelocvars +
316 sizeof(TString *) * p->sizeupvalues;
317 }
318 default: lua_assert(0); return 0;
319 }
320}
321
322
323static size_t propagateall (global_State *g) {
324 size_t m = 0;
325 while (g->gray) m += propagatemark(g);
326 return m;
327}
328
329
330/*
331** The next function tells whether a key or value can be cleared from
332** a weak table. Non-collectable objects are never removed from weak
333** tables. Strings behave as `values', so are never removed too. for
334** other objects: if really collected, cannot keep them; for userdata
335** being finalized, keep them in keys, but not in values
336*/
337static int iscleared (const TValue *o, int iskey) {
338 if (!iscollectable(o)) return 0;
339 if (ttisstring(o)) {
340 stringmark(rawtsvalue(o)); /* strings are `values', so are never weak */
341 return 0;
342 }
343 return iswhite(gcvalue(o)) ||
344 (ttisuserdata(o) && (!iskey && isfinalized(uvalue(o))));
345}
346
347
348/*
349** clear collected entries from weaktables
350*/
351static void cleartable (GCObject *l) {
352 while (l) {
353 Table *h = gco2h(l);
354 int i = h->sizearray;
355 lua_assert(testbit(h->marked, VALUEWEAKBIT) ||
356 testbit(h->marked, KEYWEAKBIT));
357 if (testbit(h->marked, VALUEWEAKBIT)) {
358 while (i--) {
359 TValue *o = &h->array[i];
360 if (iscleared(o, 0)) /* value was collected? */
361 setnilvalue(o); /* remove value */
362 }
363 }
364 i = sizenode(h);
365 while (i--) {
366 Node *n = gnode(h, i);
367 if (!ttisnil(gval(n)) && /* non-empty entry? */
368 (iscleared(key2tval(n), 1) || iscleared(gval(n), 0))) {
369 setnilvalue(gval(n)); /* remove value ... */
370 removeentry(n); /* remove entry from table */
371 }
372 }
373 l = h->gclist;
374 }
375}
376
377
378static void freeobj (lua_State *L, GCObject *o) {
379 switch (o->gch.tt) {
380 case LUA_TPROTO: luaF_freeproto(L, gco2p(o)); break;
381 case LUA_TFUNCTION: luaF_freeclosure(L, gco2cl(o)); break;
382 case LUA_TUPVAL: luaF_freeupval(L, gco2uv(o)); break;
383 case LUA_TTABLE: luaH_free(L, gco2h(o)); break;
384 case LUA_TTHREAD: {
385 lua_assert(gco2th(o) != L && gco2th(o) != G(L)->mainthread);
386 luaE_freethread(L, gco2th(o));
387 break;
388 }
389 case LUA_TSTRING: {
390 G(L)->strt.nuse--;
391 luaM_freemem(L, o, sizestring(gco2ts(o)));
392 break;
393 }
394 case LUA_TUSERDATA: {
395 luaM_freemem(L, o, sizeudata(gco2u(o)));
396 break;
397 }
398 default: lua_assert(0);
399 }
400}
401
402
403
404#define sweepwholelist(L,p) sweeplist(L,p,MAX_LUMEM)
405
406
407static GCObject **sweeplist (lua_State *L, GCObject **p, lu_mem count) {
408 GCObject *curr;
409 global_State *g = G(L);
410 int deadmask = otherwhite(g);
411 while ((curr = *p) != NULL && count-- > 0) {
412 if (curr->gch.tt == LUA_TTHREAD) /* sweep open upvalues of each thread */
413 sweepwholelist(L, &gco2th(curr)->openupval);
414 if ((curr->gch.marked ^ WHITEBITS) & deadmask) { /* not dead? */
415 lua_assert(!isdead(g, curr) || testbit(curr->gch.marked, FIXEDBIT));
416 makewhite(g, curr); /* make it white (for next cycle) */
417 p = &curr->gch.next;
418 }
419 else { /* must erase `curr' */
420 lua_assert(isdead(g, curr) || deadmask == bitmask(SFIXEDBIT));
421 *p = curr->gch.next;
422 if (curr == g->rootgc) /* is the first element of the list? */
423 g->rootgc = curr->gch.next; /* adjust first */
424 freeobj(L, curr);
425 }
426 }
427 return p;
428}
429
430
431static void checkSizes (lua_State *L) {
432 global_State *g = G(L);
433 /* check size of string hash */
434 if (g->strt.nuse < cast(lu_int32, g->strt.size/4) &&
435 g->strt.size > MINSTRTABSIZE*2)
436 luaS_resize(L, g->strt.size/2); /* table is too big */
437 /* check size of buffer */
438 if (luaZ_sizebuffer(&g->buff) > LUA_MINBUFFER*2) { /* buffer too big? */
439 size_t newsize = luaZ_sizebuffer(&g->buff) / 2;
440 luaZ_resizebuffer(L, &g->buff, newsize);
441 }
442}
443
444
445static void GCTM (lua_State *L) {
446 global_State *g = G(L);
447 GCObject *o = g->tmudata->gch.next; /* get first element */
448 Udata *udata = rawgco2u(o);
449 const TValue *tm;
450 /* remove udata from `tmudata' */
451 if (o == g->tmudata) /* last element? */
452 g->tmudata = NULL;
453 else
454 g->tmudata->gch.next = udata->uv.next;
455 udata->uv.next = g->mainthread->next; /* return it to `root' list */
456 g->mainthread->next = o;
457 makewhite(g, o);
458 tm = fasttm(L, udata->uv.metatable, TM_GC);
459 if (tm != NULL) {
460 lu_byte oldah = L->allowhook;
461 lu_mem oldt = g->GCthreshold;
462 L->allowhook = 0; /* stop debug hooks during GC tag method */
463 g->GCthreshold = 2*g->totalbytes; /* avoid GC steps */
464 setobj2s(L, L->top, tm);
465 setuvalue(L, L->top+1, udata);
466 L->top += 2;
467 luaD_call(L, L->top - 2, 0);
468 L->allowhook = oldah; /* restore hooks */
469 g->GCthreshold = oldt; /* restore threshold */
470 }
471}
472
473
474/*
475** Call all GC tag methods
476*/
477void luaC_callGCTM (lua_State *L) {
478 while (G(L)->tmudata)
479 GCTM(L);
480}
481
482
483void luaC_freeall (lua_State *L) {
484 global_State *g = G(L);
485 int i;
486 g->currentwhite = WHITEBITS | bitmask(SFIXEDBIT); /* mask to collect all elements */
487 sweepwholelist(L, &g->rootgc);
488 for (i = 0; i < g->strt.size; i++) /* free all string lists */
489 sweepwholelist(L, &g->strt.hash[i]);
490}
491
492
493static void markmt (global_State *g) {
494 int i;
495 for (i=0; i<NUM_TAGS; i++)
496 if (g->mt[i]) markobject(g, g->mt[i]);
497}
498
499
500/* mark root set */
501static void markroot (lua_State *L) {
502 global_State *g = G(L);
503 g->gray = NULL;
504 g->grayagain = NULL;
505 g->weak = NULL;
506 markobject(g, g->mainthread);
507 /* make global table be traversed before main stack */
508 markvalue(g, gt(g->mainthread));
509 markvalue(g, registry(L));
510 markmt(g);
511 g->gcstate = GCSpropagate;
512}
513
514
515static void remarkupvals (global_State *g) {
516 UpVal *uv;
517 for (uv = g->uvhead.u.l.next; uv != &g->uvhead; uv = uv->u.l.next) {
518 lua_assert(uv->u.l.next->u.l.prev == uv && uv->u.l.prev->u.l.next == uv);
519 if (isgray(obj2gco(uv)))
520 markvalue(g, uv->v);
521 }
522}
523
524
525static void atomic (lua_State *L) {
526 global_State *g = G(L);
527 size_t udsize; /* total size of userdata to be finalized */
528 /* remark occasional upvalues of (maybe) dead threads */
529 remarkupvals(g);
530 /* traverse objects cautch by write barrier and by 'remarkupvals' */
531 propagateall(g);
532 /* remark weak tables */
533 g->gray = g->weak;
534 g->weak = NULL;
535 lua_assert(!iswhite(obj2gco(g->mainthread)));
536 markobject(g, L); /* mark running thread */
537 markmt(g); /* mark basic metatables (again) */
538 propagateall(g);
539 /* remark gray again */
540 g->gray = g->grayagain;
541 g->grayagain = NULL;
542 propagateall(g);
543 udsize = luaC_separateudata(L, 0); /* separate userdata to be finalized */
544 marktmu(g); /* mark `preserved' userdata */
545 udsize += propagateall(g); /* remark, to propagate `preserveness' */
546 cleartable(g->weak); /* remove collected objects from weak tables */
547 /* flip current white */
548 g->currentwhite = cast_byte(otherwhite(g));
549 g->sweepstrgc = 0;
550 g->sweepgc = &g->rootgc;
551 g->gcstate = GCSsweepstring;
552 g->estimate = g->totalbytes - udsize; /* first estimate */
553}
554
555
556static l_mem singlestep (lua_State *L) {
557 global_State *g = G(L);
558 /*lua_checkmemory(L);*/
559 switch (g->gcstate) {
560 case GCSpause: {
561 markroot(L); /* start a new collection */
562 return 0;
563 }
564 case GCSpropagate: {
565 if (g->gray)
566 return propagatemark(g);
567 else { /* no more `gray' objects */
568 atomic(L); /* finish mark phase */
569 return 0;
570 }
571 }
572 case GCSsweepstring: {
573 lu_mem old = g->totalbytes;
574 sweepwholelist(L, &g->strt.hash[g->sweepstrgc++]);
575 if (g->sweepstrgc >= g->strt.size) /* nothing more to sweep? */
576 g->gcstate = GCSsweep; /* end sweep-string phase */
577 lua_assert(old >= g->totalbytes);
578 g->estimate -= old - g->totalbytes;
579 return GCSWEEPCOST;
580 }
581 case GCSsweep: {
582 lu_mem old = g->totalbytes;
583 g->sweepgc = sweeplist(L, g->sweepgc, GCSWEEPMAX);
584 if (*g->sweepgc == NULL) { /* nothing more to sweep? */
585 checkSizes(L);
586 g->gcstate = GCSfinalize; /* end sweep phase */
587 }
588 lua_assert(old >= g->totalbytes);
589 g->estimate -= old - g->totalbytes;
590 return GCSWEEPMAX*GCSWEEPCOST;
591 }
592 case GCSfinalize: {
593 if (g->tmudata) {
594 GCTM(L);
595 if (g->estimate > GCFINALIZECOST)
596 g->estimate -= GCFINALIZECOST;
597 return GCFINALIZECOST;
598 }
599 else {
600 g->gcstate = GCSpause; /* end collection */
601 g->gcdept = 0;
602 return 0;
603 }
604 }
605 default: lua_assert(0); return 0;
606 }
607}
608
609
610void luaC_step (lua_State *L) {
611 global_State *g = G(L);
612 l_mem lim = (GCSTEPSIZE/100) * g->gcstepmul;
613 if (lim == 0)
614 lim = (MAX_LUMEM-1)/2; /* no limit */
615 g->gcdept += g->totalbytes - g->GCthreshold;
616 do {
617 lim -= singlestep(L);
618 if (g->gcstate == GCSpause)
619 break;
620 } while (lim > 0);
621 if (g->gcstate != GCSpause) {
622 if (g->gcdept < GCSTEPSIZE)
623 g->GCthreshold = g->totalbytes + GCSTEPSIZE; /* - lim/g->gcstepmul;*/
624 else {
625 g->gcdept -= GCSTEPSIZE;
626 g->GCthreshold = g->totalbytes;
627 }
628 }
629 else {
630 setthreshold(g);
631 }
632}
633
634
635void luaC_fullgc (lua_State *L) {
636 global_State *g = G(L);
637 if (g->gcstate <= GCSpropagate) {
638 /* reset sweep marks to sweep all elements (returning them to white) */
639 g->sweepstrgc = 0;
640 g->sweepgc = &g->rootgc;
641 /* reset other collector lists */
642 g->gray = NULL;
643 g->grayagain = NULL;
644 g->weak = NULL;
645 g->gcstate = GCSsweepstring;
646 }
647 lua_assert(g->gcstate != GCSpause && g->gcstate != GCSpropagate);
648 /* finish any pending sweep phase */
649 while (g->gcstate != GCSfinalize) {
650 lua_assert(g->gcstate == GCSsweepstring || g->gcstate == GCSsweep);
651 singlestep(L);
652 }
653 markroot(L);
654 while (g->gcstate != GCSpause) {
655 singlestep(L);
656 }
657 setthreshold(g);
658}
659
660
661void luaC_barrierf (lua_State *L, GCObject *o, GCObject *v) {
662 global_State *g = G(L);
663 lua_assert(isblack(o) && iswhite(v) && !isdead(g, v) && !isdead(g, o));
664 lua_assert(g->gcstate != GCSfinalize && g->gcstate != GCSpause);
665 lua_assert(ttype(&o->gch) != LUA_TTABLE);
666 /* must keep invariant? */
667 if (g->gcstate == GCSpropagate)
668 reallymarkobject(g, v); /* restore invariant */
669 else /* don't mind */
670 makewhite(g, o); /* mark as white just to avoid other barriers */
671}
672
673
674void luaC_barrierback (lua_State *L, Table *t) {
675 global_State *g = G(L);
676 GCObject *o = obj2gco(t);
677 lua_assert(isblack(o) && !isdead(g, o));
678 lua_assert(g->gcstate != GCSfinalize && g->gcstate != GCSpause);
679 black2gray(o); /* make table gray (again) */
680 t->gclist = g->grayagain;
681 g->grayagain = o;
682}
683
684
685void luaC_link (lua_State *L, GCObject *o, lu_byte tt) {
686 global_State *g = G(L);
687 o->gch.next = g->rootgc;
688 g->rootgc = o;
689 o->gch.marked = luaC_white(g);
690 o->gch.tt = tt;
691}
692
693
694void luaC_linkupval (lua_State *L, UpVal *uv) {
695 global_State *g = G(L);
696 GCObject *o = obj2gco(uv);
697 o->gch.next = g->rootgc; /* link upvalue into `rootgc' list */
698 g->rootgc = o;
699 if (isgray(o)) {
700 if (g->gcstate == GCSpropagate) {
701 gray2black(o); /* closed upvalues need barrier */
702 luaC_barrier(L, uv, uv->v);
703 }
704 else { /* sweep phase: sweep it (turning it into white) */
705 makewhite(g, o);
706 lua_assert(g->gcstate != GCSfinalize && g->gcstate != GCSpause);
707 }
708 }
709}
710
diff --git a/examples/redis-unstable/deps/lua/src/lgc.h b/examples/redis-unstable/deps/lua/src/lgc.h
deleted file mode 100644
index 5a8dc60..0000000
--- a/examples/redis-unstable/deps/lua/src/lgc.h
+++ /dev/null
@@ -1,110 +0,0 @@
1/*
2** $Id: lgc.h,v 2.15.1.1 2007/12/27 13:02:25 roberto Exp $
3** Garbage Collector
4** See Copyright Notice in lua.h
5*/
6
7#ifndef lgc_h
8#define lgc_h
9
10
11#include "lobject.h"
12
13
14/*
15** Possible states of the Garbage Collector
16*/
17#define GCSpause 0
18#define GCSpropagate 1
19#define GCSsweepstring 2
20#define GCSsweep 3
21#define GCSfinalize 4
22
23
24/*
25** some userful bit tricks
26*/
27#define resetbits(x,m) ((x) &= cast(lu_byte, ~(m)))
28#define setbits(x,m) ((x) |= (m))
29#define testbits(x,m) ((x) & (m))
30#define bitmask(b) (1<<(b))
31#define bit2mask(b1,b2) (bitmask(b1) | bitmask(b2))
32#define l_setbit(x,b) setbits(x, bitmask(b))
33#define resetbit(x,b) resetbits(x, bitmask(b))
34#define testbit(x,b) testbits(x, bitmask(b))
35#define set2bits(x,b1,b2) setbits(x, (bit2mask(b1, b2)))
36#define reset2bits(x,b1,b2) resetbits(x, (bit2mask(b1, b2)))
37#define test2bits(x,b1,b2) testbits(x, (bit2mask(b1, b2)))
38
39
40
41/*
42** Layout for bit use in `marked' field:
43** bit 0 - object is white (type 0)
44** bit 1 - object is white (type 1)
45** bit 2 - object is black
46** bit 3 - for userdata: has been finalized
47** bit 3 - for tables: has weak keys
48** bit 4 - for tables: has weak values
49** bit 5 - object is fixed (should not be collected)
50** bit 6 - object is "super" fixed (only the main thread)
51*/
52
53
54#define WHITE0BIT 0
55#define WHITE1BIT 1
56#define BLACKBIT 2
57#define FINALIZEDBIT 3
58#define KEYWEAKBIT 3
59#define VALUEWEAKBIT 4
60#define FIXEDBIT 5
61#define SFIXEDBIT 6
62#define WHITEBITS bit2mask(WHITE0BIT, WHITE1BIT)
63
64
65#define iswhite(x) test2bits((x)->gch.marked, WHITE0BIT, WHITE1BIT)
66#define isblack(x) testbit((x)->gch.marked, BLACKBIT)
67#define isgray(x) (!isblack(x) && !iswhite(x))
68
69#define otherwhite(g) (g->currentwhite ^ WHITEBITS)
70#define isdead(g,v) ((v)->gch.marked & otherwhite(g) & WHITEBITS)
71
72#define changewhite(x) ((x)->gch.marked ^= WHITEBITS)
73#define gray2black(x) l_setbit((x)->gch.marked, BLACKBIT)
74
75#define valiswhite(x) (iscollectable(x) && iswhite(gcvalue(x)))
76
77#define luaC_white(g) cast(lu_byte, (g)->currentwhite & WHITEBITS)
78
79
80#define luaC_checkGC(L) { \
81 condhardstacktests(luaD_reallocstack(L, L->stacksize - EXTRA_STACK - 1)); \
82 if (G(L)->totalbytes >= G(L)->GCthreshold) \
83 luaC_step(L); }
84
85
86#define luaC_barrier(L,p,v) { if (valiswhite(v) && isblack(obj2gco(p))) \
87 luaC_barrierf(L,obj2gco(p),gcvalue(v)); }
88
89#define luaC_barriert(L,t,v) { if (valiswhite(v) && isblack(obj2gco(t))) \
90 luaC_barrierback(L,t); }
91
92#define luaC_objbarrier(L,p,o) \
93 { if (iswhite(obj2gco(o)) && isblack(obj2gco(p))) \
94 luaC_barrierf(L,obj2gco(p),obj2gco(o)); }
95
96#define luaC_objbarriert(L,t,o) \
97 { if (iswhite(obj2gco(o)) && isblack(obj2gco(t))) luaC_barrierback(L,t); }
98
99LUAI_FUNC size_t luaC_separateudata (lua_State *L, int all);
100LUAI_FUNC void luaC_callGCTM (lua_State *L);
101LUAI_FUNC void luaC_freeall (lua_State *L);
102LUAI_FUNC void luaC_step (lua_State *L);
103LUAI_FUNC void luaC_fullgc (lua_State *L);
104LUAI_FUNC void luaC_link (lua_State *L, GCObject *o, lu_byte tt);
105LUAI_FUNC void luaC_linkupval (lua_State *L, UpVal *uv);
106LUAI_FUNC void luaC_barrierf (lua_State *L, GCObject *o, GCObject *v);
107LUAI_FUNC void luaC_barrierback (lua_State *L, Table *t);
108
109
110#endif
diff --git a/examples/redis-unstable/deps/lua/src/linit.c b/examples/redis-unstable/deps/lua/src/linit.c
deleted file mode 100644
index c1f90df..0000000
--- a/examples/redis-unstable/deps/lua/src/linit.c
+++ /dev/null
@@ -1,38 +0,0 @@
1/*
2** $Id: linit.c,v 1.14.1.1 2007/12/27 13:02:25 roberto Exp $
3** Initialization of libraries for lua.c
4** See Copyright Notice in lua.h
5*/
6
7
8#define linit_c
9#define LUA_LIB
10
11#include "lua.h"
12
13#include "lualib.h"
14#include "lauxlib.h"
15
16
17static const luaL_Reg lualibs[] = {
18 {"", luaopen_base},
19 {LUA_LOADLIBNAME, luaopen_package},
20 {LUA_TABLIBNAME, luaopen_table},
21 {LUA_IOLIBNAME, luaopen_io},
22 {LUA_OSLIBNAME, luaopen_os},
23 {LUA_STRLIBNAME, luaopen_string},
24 {LUA_MATHLIBNAME, luaopen_math},
25 {LUA_DBLIBNAME, luaopen_debug},
26 {NULL, NULL}
27};
28
29
30LUALIB_API void luaL_openlibs (lua_State *L) {
31 const luaL_Reg *lib = lualibs;
32 for (; lib->func; lib++) {
33 lua_pushcfunction(L, lib->func);
34 lua_pushstring(L, lib->name);
35 lua_call(L, 1, 0);
36 }
37}
38
diff --git a/examples/redis-unstable/deps/lua/src/liolib.c b/examples/redis-unstable/deps/lua/src/liolib.c
deleted file mode 100644
index 649f9a5..0000000
--- a/examples/redis-unstable/deps/lua/src/liolib.c
+++ /dev/null
@@ -1,556 +0,0 @@
1/*
2** $Id: liolib.c,v 2.73.1.4 2010/05/14 15:33:51 roberto Exp $
3** Standard I/O (and system) library
4** See Copyright Notice in lua.h
5*/
6
7
8#include <errno.h>
9#include <stdio.h>
10#include <stdlib.h>
11#include <string.h>
12
13#define liolib_c
14#define LUA_LIB
15
16#include "lua.h"
17
18#include "lauxlib.h"
19#include "lualib.h"
20
21
22
23#define IO_INPUT 1
24#define IO_OUTPUT 2
25
26
27static const char *const fnames[] = {"input", "output"};
28
29
30static int pushresult (lua_State *L, int i, const char *filename) {
31 int en = errno; /* calls to Lua API may change this value */
32 if (i) {
33 lua_pushboolean(L, 1);
34 return 1;
35 }
36 else {
37 lua_pushnil(L);
38 if (filename)
39 lua_pushfstring(L, "%s: %s", filename, strerror(en));
40 else
41 lua_pushfstring(L, "%s", strerror(en));
42 lua_pushinteger(L, en);
43 return 3;
44 }
45}
46
47
48static void fileerror (lua_State *L, int arg, const char *filename) {
49 lua_pushfstring(L, "%s: %s", filename, strerror(errno));
50 luaL_argerror(L, arg, lua_tostring(L, -1));
51}
52
53
54#define tofilep(L) ((FILE **)luaL_checkudata(L, 1, LUA_FILEHANDLE))
55
56
57static int io_type (lua_State *L) {
58 void *ud;
59 luaL_checkany(L, 1);
60 ud = lua_touserdata(L, 1);
61 lua_getfield(L, LUA_REGISTRYINDEX, LUA_FILEHANDLE);
62 if (ud == NULL || !lua_getmetatable(L, 1) || !lua_rawequal(L, -2, -1))
63 lua_pushnil(L); /* not a file */
64 else if (*((FILE **)ud) == NULL)
65 lua_pushliteral(L, "closed file");
66 else
67 lua_pushliteral(L, "file");
68 return 1;
69}
70
71
72static FILE *tofile (lua_State *L) {
73 FILE **f = tofilep(L);
74 if (*f == NULL)
75 luaL_error(L, "attempt to use a closed file");
76 return *f;
77}
78
79
80
81/*
82** When creating file handles, always creates a `closed' file handle
83** before opening the actual file; so, if there is a memory error, the
84** file is not left opened.
85*/
86static FILE **newfile (lua_State *L) {
87 FILE **pf = (FILE **)lua_newuserdata(L, sizeof(FILE *));
88 *pf = NULL; /* file handle is currently `closed' */
89 luaL_getmetatable(L, LUA_FILEHANDLE);
90 lua_setmetatable(L, -2);
91 return pf;
92}
93
94
95/*
96** function to (not) close the standard files stdin, stdout, and stderr
97*/
98static int io_noclose (lua_State *L) {
99 lua_pushnil(L);
100 lua_pushliteral(L, "cannot close standard file");
101 return 2;
102}
103
104
105/*
106** function to close 'popen' files
107*/
108static int io_pclose (lua_State *L) {
109 FILE **p = tofilep(L);
110 int ok = lua_pclose(L, *p);
111 *p = NULL;
112 return pushresult(L, ok, NULL);
113}
114
115
116/*
117** function to close regular files
118*/
119static int io_fclose (lua_State *L) {
120 FILE **p = tofilep(L);
121 int ok = (fclose(*p) == 0);
122 *p = NULL;
123 return pushresult(L, ok, NULL);
124}
125
126
127static int aux_close (lua_State *L) {
128 lua_getfenv(L, 1);
129 lua_getfield(L, -1, "__close");
130 return (lua_tocfunction(L, -1))(L);
131}
132
133
134static int io_close (lua_State *L) {
135 if (lua_isnone(L, 1))
136 lua_rawgeti(L, LUA_ENVIRONINDEX, IO_OUTPUT);
137 tofile(L); /* make sure argument is a file */
138 return aux_close(L);
139}
140
141
142static int io_gc (lua_State *L) {
143 FILE *f = *tofilep(L);
144 /* ignore closed files */
145 if (f != NULL)
146 aux_close(L);
147 return 0;
148}
149
150
151static int io_tostring (lua_State *L) {
152 FILE *f = *tofilep(L);
153 if (f == NULL)
154 lua_pushliteral(L, "file (closed)");
155 else
156 lua_pushfstring(L, "file (%p)", f);
157 return 1;
158}
159
160
161static int io_open (lua_State *L) {
162 const char *filename = luaL_checkstring(L, 1);
163 const char *mode = luaL_optstring(L, 2, "r");
164 FILE **pf = newfile(L);
165 *pf = fopen(filename, mode);
166 return (*pf == NULL) ? pushresult(L, 0, filename) : 1;
167}
168
169
170/*
171** this function has a separated environment, which defines the
172** correct __close for 'popen' files
173*/
174static int io_popen (lua_State *L) {
175 const char *filename = luaL_checkstring(L, 1);
176 const char *mode = luaL_optstring(L, 2, "r");
177 FILE **pf = newfile(L);
178 *pf = lua_popen(L, filename, mode);
179 return (*pf == NULL) ? pushresult(L, 0, filename) : 1;
180}
181
182
183static int io_tmpfile (lua_State *L) {
184 FILE **pf = newfile(L);
185 *pf = tmpfile();
186 return (*pf == NULL) ? pushresult(L, 0, NULL) : 1;
187}
188
189
190static FILE *getiofile (lua_State *L, int findex) {
191 FILE *f;
192 lua_rawgeti(L, LUA_ENVIRONINDEX, findex);
193 f = *(FILE **)lua_touserdata(L, -1);
194 if (f == NULL)
195 luaL_error(L, "standard %s file is closed", fnames[findex - 1]);
196 return f;
197}
198
199
200static int g_iofile (lua_State *L, int f, const char *mode) {
201 if (!lua_isnoneornil(L, 1)) {
202 const char *filename = lua_tostring(L, 1);
203 if (filename) {
204 FILE **pf = newfile(L);
205 *pf = fopen(filename, mode);
206 if (*pf == NULL)
207 fileerror(L, 1, filename);
208 }
209 else {
210 tofile(L); /* check that it's a valid file handle */
211 lua_pushvalue(L, 1);
212 }
213 lua_rawseti(L, LUA_ENVIRONINDEX, f);
214 }
215 /* return current value */
216 lua_rawgeti(L, LUA_ENVIRONINDEX, f);
217 return 1;
218}
219
220
221static int io_input (lua_State *L) {
222 return g_iofile(L, IO_INPUT, "r");
223}
224
225
226static int io_output (lua_State *L) {
227 return g_iofile(L, IO_OUTPUT, "w");
228}
229
230
231static int io_readline (lua_State *L);
232
233
234static void aux_lines (lua_State *L, int idx, int toclose) {
235 lua_pushvalue(L, idx);
236 lua_pushboolean(L, toclose); /* close/not close file when finished */
237 lua_pushcclosure(L, io_readline, 2);
238}
239
240
241static int f_lines (lua_State *L) {
242 tofile(L); /* check that it's a valid file handle */
243 aux_lines(L, 1, 0);
244 return 1;
245}
246
247
248static int io_lines (lua_State *L) {
249 if (lua_isnoneornil(L, 1)) { /* no arguments? */
250 /* will iterate over default input */
251 lua_rawgeti(L, LUA_ENVIRONINDEX, IO_INPUT);
252 return f_lines(L);
253 }
254 else {
255 const char *filename = luaL_checkstring(L, 1);
256 FILE **pf = newfile(L);
257 *pf = fopen(filename, "r");
258 if (*pf == NULL)
259 fileerror(L, 1, filename);
260 aux_lines(L, lua_gettop(L), 1);
261 return 1;
262 }
263}
264
265
266/*
267** {======================================================
268** READ
269** =======================================================
270*/
271
272
273static int read_number (lua_State *L, FILE *f) {
274 lua_Number d;
275 if (fscanf(f, LUA_NUMBER_SCAN, &d) == 1) {
276 lua_pushnumber(L, d);
277 return 1;
278 }
279 else {
280 lua_pushnil(L); /* "result" to be removed */
281 return 0; /* read fails */
282 }
283}
284
285
286static int test_eof (lua_State *L, FILE *f) {
287 int c = getc(f);
288 ungetc(c, f);
289 lua_pushlstring(L, NULL, 0);
290 return (c != EOF);
291}
292
293
294static int read_line (lua_State *L, FILE *f) {
295 luaL_Buffer b;
296 luaL_buffinit(L, &b);
297 for (;;) {
298 size_t l;
299 char *p = luaL_prepbuffer(&b);
300 if (fgets(p, LUAL_BUFFERSIZE, f) == NULL) { /* eof? */
301 luaL_pushresult(&b); /* close buffer */
302 return (lua_objlen(L, -1) > 0); /* check whether read something */
303 }
304 l = strlen(p);
305 if (l == 0 || p[l-1] != '\n')
306 luaL_addsize(&b, l);
307 else {
308 luaL_addsize(&b, l - 1); /* do not include `eol' */
309 luaL_pushresult(&b); /* close buffer */
310 return 1; /* read at least an `eol' */
311 }
312 }
313}
314
315
316static int read_chars (lua_State *L, FILE *f, size_t n) {
317 size_t rlen; /* how much to read */
318 size_t nr; /* number of chars actually read */
319 luaL_Buffer b;
320 luaL_buffinit(L, &b);
321 rlen = LUAL_BUFFERSIZE; /* try to read that much each time */
322 do {
323 char *p = luaL_prepbuffer(&b);
324 if (rlen > n) rlen = n; /* cannot read more than asked */
325 nr = fread(p, sizeof(char), rlen, f);
326 luaL_addsize(&b, nr);
327 n -= nr; /* still have to read `n' chars */
328 } while (n > 0 && nr == rlen); /* until end of count or eof */
329 luaL_pushresult(&b); /* close buffer */
330 return (n == 0 || lua_objlen(L, -1) > 0);
331}
332
333
334static int g_read (lua_State *L, FILE *f, int first) {
335 int nargs = lua_gettop(L) - 1;
336 int success;
337 int n;
338 clearerr(f);
339 if (nargs == 0) { /* no arguments? */
340 success = read_line(L, f);
341 n = first+1; /* to return 1 result */
342 }
343 else { /* ensure stack space for all results and for auxlib's buffer */
344 luaL_checkstack(L, nargs+LUA_MINSTACK, "too many arguments");
345 success = 1;
346 for (n = first; nargs-- && success; n++) {
347 if (lua_type(L, n) == LUA_TNUMBER) {
348 size_t l = (size_t)lua_tointeger(L, n);
349 success = (l == 0) ? test_eof(L, f) : read_chars(L, f, l);
350 }
351 else {
352 const char *p = lua_tostring(L, n);
353 luaL_argcheck(L, p && p[0] == '*', n, "invalid option");
354 switch (p[1]) {
355 case 'n': /* number */
356 success = read_number(L, f);
357 break;
358 case 'l': /* line */
359 success = read_line(L, f);
360 break;
361 case 'a': /* file */
362 read_chars(L, f, ~((size_t)0)); /* read MAX_SIZE_T chars */
363 success = 1; /* always success */
364 break;
365 default:
366 return luaL_argerror(L, n, "invalid format");
367 }
368 }
369 }
370 }
371 if (ferror(f))
372 return pushresult(L, 0, NULL);
373 if (!success) {
374 lua_pop(L, 1); /* remove last result */
375 lua_pushnil(L); /* push nil instead */
376 }
377 return n - first;
378}
379
380
381static int io_read (lua_State *L) {
382 return g_read(L, getiofile(L, IO_INPUT), 1);
383}
384
385
386static int f_read (lua_State *L) {
387 return g_read(L, tofile(L), 2);
388}
389
390
391static int io_readline (lua_State *L) {
392 FILE *f = *(FILE **)lua_touserdata(L, lua_upvalueindex(1));
393 int sucess;
394 if (f == NULL) /* file is already closed? */
395 luaL_error(L, "file is already closed");
396 sucess = read_line(L, f);
397 if (ferror(f))
398 return luaL_error(L, "%s", strerror(errno));
399 if (sucess) return 1;
400 else { /* EOF */
401 if (lua_toboolean(L, lua_upvalueindex(2))) { /* generator created file? */
402 lua_settop(L, 0);
403 lua_pushvalue(L, lua_upvalueindex(1));
404 aux_close(L); /* close it */
405 }
406 return 0;
407 }
408}
409
410/* }====================================================== */
411
412
413static int g_write (lua_State *L, FILE *f, int arg) {
414 int nargs = lua_gettop(L) - 1;
415 int status = 1;
416 for (; nargs--; arg++) {
417 if (lua_type(L, arg) == LUA_TNUMBER) {
418 /* optimization: could be done exactly as for strings */
419 status = status &&
420 fprintf(f, LUA_NUMBER_FMT, lua_tonumber(L, arg)) > 0;
421 }
422 else {
423 size_t l;
424 const char *s = luaL_checklstring(L, arg, &l);
425 status = status && (fwrite(s, sizeof(char), l, f) == l);
426 }
427 }
428 return pushresult(L, status, NULL);
429}
430
431
432static int io_write (lua_State *L) {
433 return g_write(L, getiofile(L, IO_OUTPUT), 1);
434}
435
436
437static int f_write (lua_State *L) {
438 return g_write(L, tofile(L), 2);
439}
440
441
442static int f_seek (lua_State *L) {
443 static const int mode[] = {SEEK_SET, SEEK_CUR, SEEK_END};
444 static const char *const modenames[] = {"set", "cur", "end", NULL};
445 FILE *f = tofile(L);
446 int op = luaL_checkoption(L, 2, "cur", modenames);
447 long offset = luaL_optlong(L, 3, 0);
448 op = fseek(f, offset, mode[op]);
449 if (op)
450 return pushresult(L, 0, NULL); /* error */
451 else {
452 lua_pushinteger(L, ftell(f));
453 return 1;
454 }
455}
456
457
458static int f_setvbuf (lua_State *L) {
459 static const int mode[] = {_IONBF, _IOFBF, _IOLBF};
460 static const char *const modenames[] = {"no", "full", "line", NULL};
461 FILE *f = tofile(L);
462 int op = luaL_checkoption(L, 2, NULL, modenames);
463 lua_Integer sz = luaL_optinteger(L, 3, LUAL_BUFFERSIZE);
464 int res = setvbuf(f, NULL, mode[op], sz);
465 return pushresult(L, res == 0, NULL);
466}
467
468
469
470static int io_flush (lua_State *L) {
471 return pushresult(L, fflush(getiofile(L, IO_OUTPUT)) == 0, NULL);
472}
473
474
475static int f_flush (lua_State *L) {
476 return pushresult(L, fflush(tofile(L)) == 0, NULL);
477}
478
479
480static const luaL_Reg iolib[] = {
481 {"close", io_close},
482 {"flush", io_flush},
483 {"input", io_input},
484 {"lines", io_lines},
485 {"open", io_open},
486 {"output", io_output},
487 {"popen", io_popen},
488 {"read", io_read},
489 {"tmpfile", io_tmpfile},
490 {"type", io_type},
491 {"write", io_write},
492 {NULL, NULL}
493};
494
495
496static const luaL_Reg flib[] = {
497 {"close", io_close},
498 {"flush", f_flush},
499 {"lines", f_lines},
500 {"read", f_read},
501 {"seek", f_seek},
502 {"setvbuf", f_setvbuf},
503 {"write", f_write},
504 {"__gc", io_gc},
505 {"__tostring", io_tostring},
506 {NULL, NULL}
507};
508
509
510static void createmeta (lua_State *L) {
511 luaL_newmetatable(L, LUA_FILEHANDLE); /* create metatable for file handles */
512 lua_pushvalue(L, -1); /* push metatable */
513 lua_setfield(L, -2, "__index"); /* metatable.__index = metatable */
514 luaL_register(L, NULL, flib); /* file methods */
515}
516
517
518static void createstdfile (lua_State *L, FILE *f, int k, const char *fname) {
519 *newfile(L) = f;
520 if (k > 0) {
521 lua_pushvalue(L, -1);
522 lua_rawseti(L, LUA_ENVIRONINDEX, k);
523 }
524 lua_pushvalue(L, -2); /* copy environment */
525 lua_setfenv(L, -2); /* set it */
526 lua_setfield(L, -3, fname);
527}
528
529
530static void newfenv (lua_State *L, lua_CFunction cls) {
531 lua_createtable(L, 0, 1);
532 lua_pushcfunction(L, cls);
533 lua_setfield(L, -2, "__close");
534}
535
536
537LUALIB_API int luaopen_io (lua_State *L) {
538 createmeta(L);
539 /* create (private) environment (with fields IO_INPUT, IO_OUTPUT, __close) */
540 newfenv(L, io_fclose);
541 lua_replace(L, LUA_ENVIRONINDEX);
542 /* open library */
543 luaL_register(L, LUA_IOLIBNAME, iolib);
544 /* create (and set) default files */
545 newfenv(L, io_noclose); /* close function for default files */
546 createstdfile(L, stdin, IO_INPUT, "stdin");
547 createstdfile(L, stdout, IO_OUTPUT, "stdout");
548 createstdfile(L, stderr, 0, "stderr");
549 lua_pop(L, 1); /* pop environment for default files */
550 lua_getfield(L, -1, "popen");
551 newfenv(L, io_pclose); /* create environment for 'popen' */
552 lua_setfenv(L, -2); /* set fenv for 'popen' */
553 lua_pop(L, 1); /* pop 'popen' */
554 return 1;
555}
556
diff --git a/examples/redis-unstable/deps/lua/src/llex.c b/examples/redis-unstable/deps/lua/src/llex.c
deleted file mode 100644
index efad709..0000000
--- a/examples/redis-unstable/deps/lua/src/llex.c
+++ /dev/null
@@ -1,471 +0,0 @@
1/*
2** $Id: llex.c,v 2.20.1.2 2009/11/23 14:58:22 roberto Exp $
3** Lexical Analyzer
4** See Copyright Notice in lua.h
5*/
6
7
8#include <ctype.h>
9#include <locale.h>
10#include <string.h>
11
12#define llex_c
13#define LUA_CORE
14
15#include "lua.h"
16
17#include "ldo.h"
18#include "llex.h"
19#include "lobject.h"
20#include "lparser.h"
21#include "lstate.h"
22#include "lstring.h"
23#include "ltable.h"
24#include "lzio.h"
25
26
27
28#define next(ls) (ls->current = zgetc(ls->z))
29
30
31
32
33#define currIsNewline(ls) (ls->current == '\n' || ls->current == '\r')
34
35
36/* ORDER RESERVED */
37const char *const luaX_tokens [] = {
38 "and", "break", "do", "else", "elseif",
39 "end", "false", "for", "function", "if",
40 "in", "local", "nil", "not", "or", "repeat",
41 "return", "then", "true", "until", "while",
42 "..", "...", "==", ">=", "<=", "~=",
43 "<number>", "<name>", "<string>", "<eof>",
44 NULL
45};
46
47
48#define save_and_next(ls) (save(ls, ls->current), next(ls))
49
50
51static void save (LexState *ls, int c) {
52 Mbuffer *b = ls->buff;
53 if (b->n + 1 > b->buffsize) {
54 size_t newsize;
55 if (b->buffsize >= MAX_SIZET/2)
56 luaX_lexerror(ls, "lexical element too long", 0);
57 newsize = b->buffsize * 2;
58 luaZ_resizebuffer(ls->L, b, newsize);
59 }
60 b->buffer[b->n++] = cast(char, c);
61}
62
63
64void luaX_init (lua_State *L) {
65 int i;
66 for (i=0; i<NUM_RESERVED; i++) {
67 TString *ts = luaS_new(L, luaX_tokens[i]);
68 luaS_fix(ts); /* reserved words are never collected */
69 lua_assert(strlen(luaX_tokens[i])+1 <= TOKEN_LEN);
70 ts->tsv.reserved = cast_byte(i+1); /* reserved word */
71 }
72}
73
74
75#define MAXSRC 80
76
77
78const char *luaX_token2str (LexState *ls, int token) {
79 if (token < FIRST_RESERVED) {
80 lua_assert(token == cast(unsigned char, token));
81 return (iscntrl(token)) ? luaO_pushfstring(ls->L, "char(%d)", token) :
82 luaO_pushfstring(ls->L, "%c", token);
83 }
84 else
85 return luaX_tokens[token-FIRST_RESERVED];
86}
87
88
89static const char *txtToken (LexState *ls, int token) {
90 switch (token) {
91 case TK_NAME:
92 case TK_STRING:
93 case TK_NUMBER:
94 save(ls, '\0');
95 return luaZ_buffer(ls->buff);
96 default:
97 return luaX_token2str(ls, token);
98 }
99}
100
101
102void luaX_lexerror (LexState *ls, const char *msg, int token) {
103 char buff[MAXSRC];
104 luaO_chunkid(buff, getstr(ls->source), MAXSRC);
105 msg = luaO_pushfstring(ls->L, "%s:%d: %s", buff, ls->linenumber, msg);
106 if (token)
107 luaO_pushfstring(ls->L, "%s near " LUA_QS, msg, txtToken(ls, token));
108 luaD_throw(ls->L, LUA_ERRSYNTAX);
109}
110
111
112void luaX_syntaxerror (LexState *ls, const char *msg) {
113 luaX_lexerror(ls, msg, ls->t.token);
114}
115
116
117TString *luaX_newstring (LexState *ls, const char *str, size_t l) {
118 lua_State *L = ls->L;
119 TString *ts = luaS_newlstr(L, str, l);
120 TValue *o = luaH_setstr(L, ls->fs->h, ts); /* entry for `str' */
121 if (ttisnil(o)) {
122 setbvalue(o, 1); /* make sure `str' will not be collected */
123 luaC_checkGC(L);
124 }
125 return ts;
126}
127
128
129static void inclinenumber (LexState *ls) {
130 int old = ls->current;
131 lua_assert(currIsNewline(ls));
132 next(ls); /* skip `\n' or `\r' */
133 if (currIsNewline(ls) && ls->current != old)
134 next(ls); /* skip `\n\r' or `\r\n' */
135 if (++ls->linenumber >= MAX_INT)
136 luaX_syntaxerror(ls, "chunk has too many lines");
137}
138
139
140void luaX_setinput (lua_State *L, LexState *ls, ZIO *z, TString *source) {
141 ls->t.token = 0;
142 ls->decpoint = '.';
143 ls->L = L;
144 ls->lookahead.token = TK_EOS; /* no look-ahead token */
145 ls->z = z;
146 ls->fs = NULL;
147 ls->linenumber = 1;
148 ls->lastline = 1;
149 ls->source = source;
150 luaZ_resizebuffer(ls->L, ls->buff, LUA_MINBUFFER); /* initialize buffer */
151 next(ls); /* read first char */
152}
153
154
155
156/*
157** =======================================================
158** LEXICAL ANALYZER
159** =======================================================
160*/
161
162
163
164static int check_next (LexState *ls, const char *set) {
165 if (!strchr(set, ls->current))
166 return 0;
167 save_and_next(ls);
168 return 1;
169}
170
171
172static void buffreplace (LexState *ls, char from, char to) {
173 size_t n = luaZ_bufflen(ls->buff);
174 char *p = luaZ_buffer(ls->buff);
175 while (n--)
176 if (p[n] == from) p[n] = to;
177}
178
179
180static void trydecpoint (LexState *ls, SemInfo *seminfo) {
181 /* format error: try to update decimal point separator */
182 struct lconv *cv = localeconv();
183 char old = ls->decpoint;
184 ls->decpoint = (cv ? cv->decimal_point[0] : '.');
185 buffreplace(ls, old, ls->decpoint); /* try updated decimal separator */
186 if (!luaO_str2d(luaZ_buffer(ls->buff), &seminfo->r)) {
187 /* format error with correct decimal point: no more options */
188 buffreplace(ls, ls->decpoint, '.'); /* undo change (for error message) */
189 luaX_lexerror(ls, "malformed number", TK_NUMBER);
190 }
191}
192
193
194/* LUA_NUMBER */
195static void read_numeral (LexState *ls, SemInfo *seminfo) {
196 lua_assert(isdigit(ls->current));
197 do {
198 save_and_next(ls);
199 } while (isdigit(ls->current) || ls->current == '.');
200 if (check_next(ls, "Ee")) /* `E'? */
201 check_next(ls, "+-"); /* optional exponent sign */
202 while (isalnum(ls->current) || ls->current == '_')
203 save_and_next(ls);
204 save(ls, '\0');
205 buffreplace(ls, '.', ls->decpoint); /* follow locale for decimal point */
206 if (!luaO_str2d(luaZ_buffer(ls->buff), &seminfo->r)) /* format error? */
207 trydecpoint(ls, seminfo); /* try to update decimal point separator */
208}
209
210/*
211** reads a sequence '[=*[' or ']=*]', leaving the last bracket.
212** If a sequence is well-formed, return its number of '='s + 2; otherwise,
213** return 1 if there is no '='s or 0 otherwise (an unfinished '[==...').
214*/
215static size_t skip_sep (LexState *ls) {
216 size_t count = 0;
217 int s = ls->current;
218 lua_assert(s == '[' || s == ']');
219 save_and_next(ls);
220 while (ls->current == '=') {
221 save_and_next(ls);
222 count++;
223 }
224 return (ls->current == s) ? count + 2
225 : (count == 0) ? 1
226 : 0;
227}
228
229
230static void read_long_string (LexState *ls, SemInfo *seminfo, size_t sep) {
231 int cont = 0;
232 (void)(cont); /* avoid warnings when `cont' is not used */
233 save_and_next(ls); /* skip 2nd `[' */
234 if (currIsNewline(ls)) /* string starts with a newline? */
235 inclinenumber(ls); /* skip it */
236 for (;;) {
237 switch (ls->current) {
238 case EOZ:
239 luaX_lexerror(ls, (seminfo) ? "unfinished long string" :
240 "unfinished long comment", TK_EOS);
241 break; /* to avoid warnings */
242#if defined(LUA_COMPAT_LSTR)
243 case '[': {
244 if (skip_sep(ls) == sep) {
245 save_and_next(ls); /* skip 2nd `[' */
246 cont++;
247#if LUA_COMPAT_LSTR == 1
248 if (sep == 0)
249 luaX_lexerror(ls, "nesting of [[...]] is deprecated", '[');
250#endif
251 }
252 break;
253 }
254#endif
255 case ']': {
256 if (skip_sep(ls) == sep) {
257 save_and_next(ls); /* skip 2nd `]' */
258#if defined(LUA_COMPAT_LSTR) && LUA_COMPAT_LSTR == 2
259 cont--;
260 if (sep == 0 && cont >= 0) break;
261#endif
262 goto endloop;
263 }
264 break;
265 }
266 case '\n':
267 case '\r': {
268 save(ls, '\n');
269 inclinenumber(ls);
270 if (!seminfo) luaZ_resetbuffer(ls->buff); /* avoid wasting space */
271 break;
272 }
273 default: {
274 if (seminfo) save_and_next(ls);
275 else next(ls);
276 }
277 }
278 } endloop:
279 if (seminfo)
280 seminfo->ts = luaX_newstring(ls, luaZ_buffer(ls->buff) + sep,
281 luaZ_bufflen(ls->buff) - 2 * sep);
282}
283
284
285static void read_string (LexState *ls, int del, SemInfo *seminfo) {
286 save_and_next(ls);
287 while (ls->current != del) {
288 switch (ls->current) {
289 case EOZ:
290 luaX_lexerror(ls, "unfinished string", TK_EOS);
291 continue; /* to avoid warnings */
292 case '\n':
293 case '\r':
294 luaX_lexerror(ls, "unfinished string", TK_STRING);
295 continue; /* to avoid warnings */
296 case '\\': {
297 int c;
298 next(ls); /* do not save the `\' */
299 switch (ls->current) {
300 case 'a': c = '\a'; break;
301 case 'b': c = '\b'; break;
302 case 'f': c = '\f'; break;
303 case 'n': c = '\n'; break;
304 case 'r': c = '\r'; break;
305 case 't': c = '\t'; break;
306 case 'v': c = '\v'; break;
307 case '\n': /* go through */
308 case '\r': save(ls, '\n'); inclinenumber(ls); continue;
309 case EOZ: continue; /* will raise an error next loop */
310 default: {
311 if (!isdigit(ls->current))
312 save_and_next(ls); /* handles \\, \", \', and \? */
313 else { /* \xxx */
314 int i = 0;
315 c = 0;
316 do {
317 c = 10*c + (ls->current-'0');
318 next(ls);
319 } while (++i<3 && isdigit(ls->current));
320 if (c > UCHAR_MAX)
321 luaX_lexerror(ls, "escape sequence too large", TK_STRING);
322 save(ls, c);
323 }
324 continue;
325 }
326 }
327 save(ls, c);
328 next(ls);
329 continue;
330 }
331 default:
332 save_and_next(ls);
333 }
334 }
335 save_and_next(ls); /* skip delimiter */
336 seminfo->ts = luaX_newstring(ls, luaZ_buffer(ls->buff) + 1,
337 luaZ_bufflen(ls->buff) - 2);
338}
339
340
341static int llex (LexState *ls, SemInfo *seminfo) {
342 luaZ_resetbuffer(ls->buff);
343 for (;;) {
344 switch (ls->current) {
345 case '\n':
346 case '\r': {
347 inclinenumber(ls);
348 continue;
349 }
350 case '-': {
351 next(ls);
352 if (ls->current != '-') return '-';
353 /* else is a comment */
354 next(ls);
355 if (ls->current == '[') {
356 size_t sep = skip_sep(ls);
357 luaZ_resetbuffer(ls->buff); /* `skip_sep' may dirty the buffer */
358 if (sep >= 2) {
359 read_long_string(ls, NULL, sep); /* long comment */
360 luaZ_resetbuffer(ls->buff);
361 continue;
362 }
363 }
364 /* else short comment */
365 while (!currIsNewline(ls) && ls->current != EOZ)
366 next(ls);
367 continue;
368 }
369 case '[': {
370 size_t sep = skip_sep(ls);
371 if (sep >= 2) {
372 read_long_string(ls, seminfo, sep);
373 return TK_STRING;
374 }
375 else if (sep == 0) /* '[=...' missing second bracket */
376 luaX_lexerror(ls, "invalid long string delimiter", TK_STRING);
377 return '[';
378 }
379 case '=': {
380 next(ls);
381 if (ls->current != '=') return '=';
382 else { next(ls); return TK_EQ; }
383 }
384 case '<': {
385 next(ls);
386 if (ls->current != '=') return '<';
387 else { next(ls); return TK_LE; }
388 }
389 case '>': {
390 next(ls);
391 if (ls->current != '=') return '>';
392 else { next(ls); return TK_GE; }
393 }
394 case '~': {
395 next(ls);
396 if (ls->current != '=') return '~';
397 else { next(ls); return TK_NE; }
398 }
399 case '"':
400 case '\'': {
401 read_string(ls, ls->current, seminfo);
402 return TK_STRING;
403 }
404 case '.': {
405 save_and_next(ls);
406 if (check_next(ls, ".")) {
407 if (check_next(ls, "."))
408 return TK_DOTS; /* ... */
409 else return TK_CONCAT; /* .. */
410 }
411 else if (!isdigit(ls->current)) return '.';
412 else {
413 read_numeral(ls, seminfo);
414 return TK_NUMBER;
415 }
416 }
417 case EOZ: {
418 return TK_EOS;
419 }
420 default: {
421 if (isspace(ls->current)) {
422 lua_assert(!currIsNewline(ls));
423 next(ls);
424 continue;
425 }
426 else if (isdigit(ls->current)) {
427 read_numeral(ls, seminfo);
428 return TK_NUMBER;
429 }
430 else if (isalpha(ls->current) || ls->current == '_') {
431 /* identifier or reserved word */
432 TString *ts;
433 do {
434 save_and_next(ls);
435 } while (isalnum(ls->current) || ls->current == '_');
436 ts = luaX_newstring(ls, luaZ_buffer(ls->buff),
437 luaZ_bufflen(ls->buff));
438 if (ts->tsv.reserved > 0) /* reserved word? */
439 return ts->tsv.reserved - 1 + FIRST_RESERVED;
440 else {
441 seminfo->ts = ts;
442 return TK_NAME;
443 }
444 }
445 else {
446 int c = ls->current;
447 next(ls);
448 return c; /* single-char tokens (+ - / ...) */
449 }
450 }
451 }
452 }
453}
454
455
456void luaX_next (LexState *ls) {
457 ls->lastline = ls->linenumber;
458 if (ls->lookahead.token != TK_EOS) { /* is there a look-ahead token? */
459 ls->t = ls->lookahead; /* use this one */
460 ls->lookahead.token = TK_EOS; /* and discharge it */
461 }
462 else
463 ls->t.token = llex(ls, &ls->t.seminfo); /* read next token */
464}
465
466
467void luaX_lookahead (LexState *ls) {
468 lua_assert(ls->lookahead.token == TK_EOS);
469 ls->lookahead.token = llex(ls, &ls->lookahead.seminfo);
470}
471
diff --git a/examples/redis-unstable/deps/lua/src/llex.h b/examples/redis-unstable/deps/lua/src/llex.h
deleted file mode 100644
index a9201ce..0000000
--- a/examples/redis-unstable/deps/lua/src/llex.h
+++ /dev/null
@@ -1,81 +0,0 @@
1/*
2** $Id: llex.h,v 1.58.1.1 2007/12/27 13:02:25 roberto Exp $
3** Lexical Analyzer
4** See Copyright Notice in lua.h
5*/
6
7#ifndef llex_h
8#define llex_h
9
10#include "lobject.h"
11#include "lzio.h"
12
13
14#define FIRST_RESERVED 257
15
16/* maximum length of a reserved word */
17#define TOKEN_LEN (sizeof("function")/sizeof(char))
18
19
20/*
21* WARNING: if you change the order of this enumeration,
22* grep "ORDER RESERVED"
23*/
24enum RESERVED {
25 /* terminal symbols denoted by reserved words */
26 TK_AND = FIRST_RESERVED, TK_BREAK,
27 TK_DO, TK_ELSE, TK_ELSEIF, TK_END, TK_FALSE, TK_FOR, TK_FUNCTION,
28 TK_IF, TK_IN, TK_LOCAL, TK_NIL, TK_NOT, TK_OR, TK_REPEAT,
29 TK_RETURN, TK_THEN, TK_TRUE, TK_UNTIL, TK_WHILE,
30 /* other terminal symbols */
31 TK_CONCAT, TK_DOTS, TK_EQ, TK_GE, TK_LE, TK_NE, TK_NUMBER,
32 TK_NAME, TK_STRING, TK_EOS
33};
34
35/* number of reserved words */
36#define NUM_RESERVED (cast(int, TK_WHILE-FIRST_RESERVED+1))
37
38
39/* array with token `names' */
40LUAI_DATA const char *const luaX_tokens [];
41
42
43typedef union {
44 lua_Number r;
45 TString *ts;
46} SemInfo; /* semantics information */
47
48
49typedef struct Token {
50 int token;
51 SemInfo seminfo;
52} Token;
53
54
55typedef struct LexState {
56 int current; /* current character (charint) */
57 int linenumber; /* input line counter */
58 int lastline; /* line of last token `consumed' */
59 Token t; /* current token */
60 Token lookahead; /* look ahead token */
61 struct FuncState *fs; /* `FuncState' is private to the parser */
62 struct lua_State *L;
63 ZIO *z; /* input stream */
64 Mbuffer *buff; /* buffer for tokens */
65 TString *source; /* current source name */
66 char decpoint; /* locale decimal point */
67} LexState;
68
69
70LUAI_FUNC void luaX_init (lua_State *L);
71LUAI_FUNC void luaX_setinput (lua_State *L, LexState *ls, ZIO *z,
72 TString *source);
73LUAI_FUNC TString *luaX_newstring (LexState *ls, const char *str, size_t l);
74LUAI_FUNC void luaX_next (LexState *ls);
75LUAI_FUNC void luaX_lookahead (LexState *ls);
76LUAI_FUNC void luaX_lexerror (LexState *ls, const char *msg, int token);
77LUAI_FUNC void luaX_syntaxerror (LexState *ls, const char *s);
78LUAI_FUNC const char *luaX_token2str (LexState *ls, int token);
79
80
81#endif
diff --git a/examples/redis-unstable/deps/lua/src/llimits.h b/examples/redis-unstable/deps/lua/src/llimits.h
deleted file mode 100644
index ca8dcb7..0000000
--- a/examples/redis-unstable/deps/lua/src/llimits.h
+++ /dev/null
@@ -1,128 +0,0 @@
1/*
2** $Id: llimits.h,v 1.69.1.1 2007/12/27 13:02:25 roberto Exp $
3** Limits, basic types, and some other `installation-dependent' definitions
4** See Copyright Notice in lua.h
5*/
6
7#ifndef llimits_h
8#define llimits_h
9
10
11#include <limits.h>
12#include <stddef.h>
13
14
15#include "lua.h"
16
17
18typedef LUAI_UINT32 lu_int32;
19
20typedef LUAI_UMEM lu_mem;
21
22typedef LUAI_MEM l_mem;
23
24
25
26/* chars used as small naturals (so that `char' is reserved for characters) */
27typedef unsigned char lu_byte;
28
29
30#define MAX_SIZET ((size_t)(~(size_t)0)-2)
31
32#define MAX_LUMEM ((lu_mem)(~(lu_mem)0)-2)
33
34
35#define MAX_INT (INT_MAX-2) /* maximum value of an int (-2 for safety) */
36
37/*
38** conversion of pointer to integer
39** this is for hashing only; there is no problem if the integer
40** cannot hold the whole pointer value
41*/
42#define IntPoint(p) ((unsigned int)(lu_mem)(p))
43
44
45
46/* type to ensure maximum alignment */
47typedef LUAI_USER_ALIGNMENT_T L_Umaxalign;
48
49
50/* result of a `usual argument conversion' over lua_Number */
51typedef LUAI_UACNUMBER l_uacNumber;
52
53
54/* internal assertions for in-house debugging */
55#ifdef lua_assert
56
57#define check_exp(c,e) (lua_assert(c), (e))
58#define api_check(l,e) lua_assert(e)
59
60#else
61
62#define lua_assert(c) ((void)0)
63#define check_exp(c,e) (e)
64#define api_check luai_apicheck
65
66#endif
67
68
69#ifndef UNUSED
70#define UNUSED(x) ((void)(x)) /* to avoid warnings */
71#endif
72
73
74#ifndef cast
75#define cast(t, exp) ((t)(exp))
76#endif
77
78#define cast_byte(i) cast(lu_byte, (i))
79#define cast_num(i) cast(lua_Number, (i))
80#define cast_int(i) cast(int, (i))
81
82
83
84/*
85** type for virtual-machine instructions
86** must be an unsigned with (at least) 4 bytes (see details in lopcodes.h)
87*/
88typedef lu_int32 Instruction;
89
90
91
92/* maximum stack for a Lua function */
93#define MAXSTACK 250
94
95
96
97/* minimum size for the string table (must be power of 2) */
98#ifndef MINSTRTABSIZE
99#define MINSTRTABSIZE 32
100#endif
101
102
103/* minimum size for string buffer */
104#ifndef LUA_MINBUFFER
105#define LUA_MINBUFFER 32
106#endif
107
108
109#ifndef lua_lock
110#define lua_lock(L) ((void) 0)
111#define lua_unlock(L) ((void) 0)
112#endif
113
114#ifndef luai_threadyield
115#define luai_threadyield(L) {lua_unlock(L); lua_lock(L);}
116#endif
117
118
119/*
120** macro to control inclusion of some hard tests on stack reallocation
121*/
122#ifndef HARDSTACKTESTS
123#define condhardstacktests(x) ((void)0)
124#else
125#define condhardstacktests(x) x
126#endif
127
128#endif
diff --git a/examples/redis-unstable/deps/lua/src/lmathlib.c b/examples/redis-unstable/deps/lua/src/lmathlib.c
deleted file mode 100644
index 441fbf7..0000000
--- a/examples/redis-unstable/deps/lua/src/lmathlib.c
+++ /dev/null
@@ -1,263 +0,0 @@
1/*
2** $Id: lmathlib.c,v 1.67.1.1 2007/12/27 13:02:25 roberto Exp $
3** Standard mathematical library
4** See Copyright Notice in lua.h
5*/
6
7
8#include <stdlib.h>
9#include <math.h>
10
11#define lmathlib_c
12#define LUA_LIB
13
14#include "lua.h"
15
16#include "lauxlib.h"
17#include "lualib.h"
18
19
20#undef PI
21#define PI (3.14159265358979323846)
22#define RADIANS_PER_DEGREE (PI/180.0)
23
24
25
26static int math_abs (lua_State *L) {
27 lua_pushnumber(L, fabs(luaL_checknumber(L, 1)));
28 return 1;
29}
30
31static int math_sin (lua_State *L) {
32 lua_pushnumber(L, sin(luaL_checknumber(L, 1)));
33 return 1;
34}
35
36static int math_sinh (lua_State *L) {
37 lua_pushnumber(L, sinh(luaL_checknumber(L, 1)));
38 return 1;
39}
40
41static int math_cos (lua_State *L) {
42 lua_pushnumber(L, cos(luaL_checknumber(L, 1)));
43 return 1;
44}
45
46static int math_cosh (lua_State *L) {
47 lua_pushnumber(L, cosh(luaL_checknumber(L, 1)));
48 return 1;
49}
50
51static int math_tan (lua_State *L) {
52 lua_pushnumber(L, tan(luaL_checknumber(L, 1)));
53 return 1;
54}
55
56static int math_tanh (lua_State *L) {
57 lua_pushnumber(L, tanh(luaL_checknumber(L, 1)));
58 return 1;
59}
60
61static int math_asin (lua_State *L) {
62 lua_pushnumber(L, asin(luaL_checknumber(L, 1)));
63 return 1;
64}
65
66static int math_acos (lua_State *L) {
67 lua_pushnumber(L, acos(luaL_checknumber(L, 1)));
68 return 1;
69}
70
71static int math_atan (lua_State *L) {
72 lua_pushnumber(L, atan(luaL_checknumber(L, 1)));
73 return 1;
74}
75
76static int math_atan2 (lua_State *L) {
77 lua_pushnumber(L, atan2(luaL_checknumber(L, 1), luaL_checknumber(L, 2)));
78 return 1;
79}
80
81static int math_ceil (lua_State *L) {
82 lua_pushnumber(L, ceil(luaL_checknumber(L, 1)));
83 return 1;
84}
85
86static int math_floor (lua_State *L) {
87 lua_pushnumber(L, floor(luaL_checknumber(L, 1)));
88 return 1;
89}
90
91static int math_fmod (lua_State *L) {
92 lua_pushnumber(L, fmod(luaL_checknumber(L, 1), luaL_checknumber(L, 2)));
93 return 1;
94}
95
96static int math_modf (lua_State *L) {
97 double ip;
98 double fp = modf(luaL_checknumber(L, 1), &ip);
99 lua_pushnumber(L, ip);
100 lua_pushnumber(L, fp);
101 return 2;
102}
103
104static int math_sqrt (lua_State *L) {
105 lua_pushnumber(L, sqrt(luaL_checknumber(L, 1)));
106 return 1;
107}
108
109static int math_pow (lua_State *L) {
110 lua_pushnumber(L, pow(luaL_checknumber(L, 1), luaL_checknumber(L, 2)));
111 return 1;
112}
113
114static int math_log (lua_State *L) {
115 lua_pushnumber(L, log(luaL_checknumber(L, 1)));
116 return 1;
117}
118
119static int math_log10 (lua_State *L) {
120 lua_pushnumber(L, log10(luaL_checknumber(L, 1)));
121 return 1;
122}
123
124static int math_exp (lua_State *L) {
125 lua_pushnumber(L, exp(luaL_checknumber(L, 1)));
126 return 1;
127}
128
129static int math_deg (lua_State *L) {
130 lua_pushnumber(L, luaL_checknumber(L, 1)/RADIANS_PER_DEGREE);
131 return 1;
132}
133
134static int math_rad (lua_State *L) {
135 lua_pushnumber(L, luaL_checknumber(L, 1)*RADIANS_PER_DEGREE);
136 return 1;
137}
138
139static int math_frexp (lua_State *L) {
140 int e;
141 lua_pushnumber(L, frexp(luaL_checknumber(L, 1), &e));
142 lua_pushinteger(L, e);
143 return 2;
144}
145
146static int math_ldexp (lua_State *L) {
147 lua_pushnumber(L, ldexp(luaL_checknumber(L, 1), luaL_checkint(L, 2)));
148 return 1;
149}
150
151
152
153static int math_min (lua_State *L) {
154 int n = lua_gettop(L); /* number of arguments */
155 lua_Number dmin = luaL_checknumber(L, 1);
156 int i;
157 for (i=2; i<=n; i++) {
158 lua_Number d = luaL_checknumber(L, i);
159 if (d < dmin)
160 dmin = d;
161 }
162 lua_pushnumber(L, dmin);
163 return 1;
164}
165
166
167static int math_max (lua_State *L) {
168 int n = lua_gettop(L); /* number of arguments */
169 lua_Number dmax = luaL_checknumber(L, 1);
170 int i;
171 for (i=2; i<=n; i++) {
172 lua_Number d = luaL_checknumber(L, i);
173 if (d > dmax)
174 dmax = d;
175 }
176 lua_pushnumber(L, dmax);
177 return 1;
178}
179
180
181static int math_random (lua_State *L) {
182 /* the `%' avoids the (rare) case of r==1, and is needed also because on
183 some systems (SunOS!) `rand()' may return a value larger than RAND_MAX */
184 lua_Number r = (lua_Number)(rand()%RAND_MAX) / (lua_Number)RAND_MAX;
185 switch (lua_gettop(L)) { /* check number of arguments */
186 case 0: { /* no arguments */
187 lua_pushnumber(L, r); /* Number between 0 and 1 */
188 break;
189 }
190 case 1: { /* only upper limit */
191 int u = luaL_checkint(L, 1);
192 luaL_argcheck(L, 1<=u, 1, "interval is empty");
193 lua_pushnumber(L, floor(r*u)+1); /* int between 1 and `u' */
194 break;
195 }
196 case 2: { /* lower and upper limits */
197 int l = luaL_checkint(L, 1);
198 int u = luaL_checkint(L, 2);
199 luaL_argcheck(L, l<=u, 2, "interval is empty");
200 lua_pushnumber(L, floor(r*(u-l+1))+l); /* int between `l' and `u' */
201 break;
202 }
203 default: return luaL_error(L, "wrong number of arguments");
204 }
205 return 1;
206}
207
208
209static int math_randomseed (lua_State *L) {
210 srand(luaL_checkint(L, 1));
211 return 0;
212}
213
214
215static const luaL_Reg mathlib[] = {
216 {"abs", math_abs},
217 {"acos", math_acos},
218 {"asin", math_asin},
219 {"atan2", math_atan2},
220 {"atan", math_atan},
221 {"ceil", math_ceil},
222 {"cosh", math_cosh},
223 {"cos", math_cos},
224 {"deg", math_deg},
225 {"exp", math_exp},
226 {"floor", math_floor},
227 {"fmod", math_fmod},
228 {"frexp", math_frexp},
229 {"ldexp", math_ldexp},
230 {"log10", math_log10},
231 {"log", math_log},
232 {"max", math_max},
233 {"min", math_min},
234 {"modf", math_modf},
235 {"pow", math_pow},
236 {"rad", math_rad},
237 {"random", math_random},
238 {"randomseed", math_randomseed},
239 {"sinh", math_sinh},
240 {"sin", math_sin},
241 {"sqrt", math_sqrt},
242 {"tanh", math_tanh},
243 {"tan", math_tan},
244 {NULL, NULL}
245};
246
247
248/*
249** Open math library
250*/
251LUALIB_API int luaopen_math (lua_State *L) {
252 luaL_register(L, LUA_MATHLIBNAME, mathlib);
253 lua_pushnumber(L, PI);
254 lua_setfield(L, -2, "pi");
255 lua_pushnumber(L, HUGE_VAL);
256 lua_setfield(L, -2, "huge");
257#if defined(LUA_COMPAT_MOD)
258 lua_getfield(L, -1, "fmod");
259 lua_setfield(L, -2, "mod");
260#endif
261 return 1;
262}
263
diff --git a/examples/redis-unstable/deps/lua/src/lmem.c b/examples/redis-unstable/deps/lua/src/lmem.c
deleted file mode 100644
index ae7d8c9..0000000
--- a/examples/redis-unstable/deps/lua/src/lmem.c
+++ /dev/null
@@ -1,86 +0,0 @@
1/*
2** $Id: lmem.c,v 1.70.1.1 2007/12/27 13:02:25 roberto Exp $
3** Interface to Memory Manager
4** See Copyright Notice in lua.h
5*/
6
7
8#include <stddef.h>
9
10#define lmem_c
11#define LUA_CORE
12
13#include "lua.h"
14
15#include "ldebug.h"
16#include "ldo.h"
17#include "lmem.h"
18#include "lobject.h"
19#include "lstate.h"
20
21
22
23/*
24** About the realloc function:
25** void * frealloc (void *ud, void *ptr, size_t osize, size_t nsize);
26** (`osize' is the old size, `nsize' is the new size)
27**
28** Lua ensures that (ptr == NULL) iff (osize == 0).
29**
30** * frealloc(ud, NULL, 0, x) creates a new block of size `x'
31**
32** * frealloc(ud, p, x, 0) frees the block `p'
33** (in this specific case, frealloc must return NULL).
34** particularly, frealloc(ud, NULL, 0, 0) does nothing
35** (which is equivalent to free(NULL) in ANSI C)
36**
37** frealloc returns NULL if it cannot create or reallocate the area
38** (any reallocation to an equal or smaller size cannot fail!)
39*/
40
41
42
43#define MINSIZEARRAY 4
44
45
46void *luaM_growaux_ (lua_State *L, void *block, int *size, size_t size_elems,
47 int limit, const char *errormsg) {
48 void *newblock;
49 int newsize;
50 if (*size >= limit/2) { /* cannot double it? */
51 if (*size >= limit) /* cannot grow even a little? */
52 luaG_runerror(L, errormsg);
53 newsize = limit; /* still have at least one free place */
54 }
55 else {
56 newsize = (*size)*2;
57 if (newsize < MINSIZEARRAY)
58 newsize = MINSIZEARRAY; /* minimum size */
59 }
60 newblock = luaM_reallocv(L, block, *size, newsize, size_elems);
61 *size = newsize; /* update only when everything else is OK */
62 return newblock;
63}
64
65
66void *luaM_toobig (lua_State *L) {
67 luaG_runerror(L, "memory allocation error: block too big");
68 return NULL; /* to avoid warnings */
69}
70
71
72
73/*
74** generic allocation routine.
75*/
76void *luaM_realloc_ (lua_State *L, void *block, size_t osize, size_t nsize) {
77 global_State *g = G(L);
78 lua_assert((osize == 0) == (block == NULL));
79 block = (*g->frealloc)(g->ud, block, osize, nsize);
80 if (block == NULL && nsize > 0)
81 luaD_throw(L, LUA_ERRMEM);
82 lua_assert((nsize == 0) == (block == NULL));
83 g->totalbytes = (g->totalbytes - osize) + nsize;
84 return block;
85}
86
diff --git a/examples/redis-unstable/deps/lua/src/lmem.h b/examples/redis-unstable/deps/lua/src/lmem.h
deleted file mode 100644
index 7c2dcb3..0000000
--- a/examples/redis-unstable/deps/lua/src/lmem.h
+++ /dev/null
@@ -1,49 +0,0 @@
1/*
2** $Id: lmem.h,v 1.31.1.1 2007/12/27 13:02:25 roberto Exp $
3** Interface to Memory Manager
4** See Copyright Notice in lua.h
5*/
6
7#ifndef lmem_h
8#define lmem_h
9
10
11#include <stddef.h>
12
13#include "llimits.h"
14#include "lua.h"
15
16#define MEMERRMSG "not enough memory"
17
18
19#define luaM_reallocv(L,b,on,n,e) \
20 ((cast(size_t, (n)+1) <= MAX_SIZET/(e)) ? /* +1 to avoid warnings */ \
21 luaM_realloc_(L, (b), (on)*(e), (n)*(e)) : \
22 luaM_toobig(L))
23
24#define luaM_freemem(L, b, s) luaM_realloc_(L, (b), (s), 0)
25#define luaM_free(L, b) luaM_realloc_(L, (b), sizeof(*(b)), 0)
26#define luaM_freearray(L, b, n, t) luaM_reallocv(L, (b), n, 0, sizeof(t))
27
28#define luaM_malloc(L,t) luaM_realloc_(L, NULL, 0, (t))
29#define luaM_new(L,t) cast(t *, luaM_malloc(L, sizeof(t)))
30#define luaM_newvector(L,n,t) \
31 cast(t *, luaM_reallocv(L, NULL, 0, n, sizeof(t)))
32
33#define luaM_growvector(L,v,nelems,size,t,limit,e) \
34 if ((nelems)+1 > (size)) \
35 ((v)=cast(t *, luaM_growaux_(L,v,&(size),sizeof(t),limit,e)))
36
37#define luaM_reallocvector(L, v,oldn,n,t) \
38 ((v)=cast(t *, luaM_reallocv(L, v, oldn, n, sizeof(t))))
39
40
41LUAI_FUNC void *luaM_realloc_ (lua_State *L, void *block, size_t oldsize,
42 size_t size);
43LUAI_FUNC void *luaM_toobig (lua_State *L);
44LUAI_FUNC void *luaM_growaux_ (lua_State *L, void *block, int *size,
45 size_t size_elem, int limit,
46 const char *errormsg);
47
48#endif
49
diff --git a/examples/redis-unstable/deps/lua/src/loadlib.c b/examples/redis-unstable/deps/lua/src/loadlib.c
deleted file mode 100644
index 6158c53..0000000
--- a/examples/redis-unstable/deps/lua/src/loadlib.c
+++ /dev/null
@@ -1,666 +0,0 @@
1/*
2** $Id: loadlib.c,v 1.52.1.4 2009/09/09 13:17:16 roberto Exp $
3** Dynamic library loader for Lua
4** See Copyright Notice in lua.h
5**
6** This module contains an implementation of loadlib for Unix systems
7** that have dlfcn, an implementation for Darwin (Mac OS X), an
8** implementation for Windows, and a stub for other systems.
9*/
10
11
12#include <stdlib.h>
13#include <string.h>
14
15
16#define loadlib_c
17#define LUA_LIB
18
19#include "lua.h"
20
21#include "lauxlib.h"
22#include "lualib.h"
23
24
25/* prefix for open functions in C libraries */
26#define LUA_POF "luaopen_"
27
28/* separator for open functions in C libraries */
29#define LUA_OFSEP "_"
30
31
32#define LIBPREFIX "LOADLIB: "
33
34#define POF LUA_POF
35#define LIB_FAIL "open"
36
37
38/* error codes for ll_loadfunc */
39#define ERRLIB 1
40#define ERRFUNC 2
41
42#define setprogdir(L) ((void)0)
43
44
45static void ll_unloadlib (void *lib);
46static void *ll_load (lua_State *L, const char *path);
47static lua_CFunction ll_sym (lua_State *L, void *lib, const char *sym);
48
49
50
51#if defined(LUA_DL_DLOPEN)
52/*
53** {========================================================================
54** This is an implementation of loadlib based on the dlfcn interface.
55** The dlfcn interface is available in Linux, SunOS, Solaris, IRIX, FreeBSD,
56** NetBSD, AIX 4.2, HPUX 11, and probably most other Unix flavors, at least
57** as an emulation layer on top of native functions.
58** =========================================================================
59*/
60
61#include <dlfcn.h>
62
63static void ll_unloadlib (void *lib) {
64 dlclose(lib);
65}
66
67
68static void *ll_load (lua_State *L, const char *path) {
69 void *lib = dlopen(path, RTLD_NOW);
70 if (lib == NULL) lua_pushstring(L, dlerror());
71 return lib;
72}
73
74
75static lua_CFunction ll_sym (lua_State *L, void *lib, const char *sym) {
76 lua_CFunction f = (lua_CFunction)dlsym(lib, sym);
77 if (f == NULL) lua_pushstring(L, dlerror());
78 return f;
79}
80
81/* }====================================================== */
82
83
84
85#elif defined(LUA_DL_DLL)
86/*
87** {======================================================================
88** This is an implementation of loadlib for Windows using native functions.
89** =======================================================================
90*/
91
92#include <windows.h>
93
94
95#undef setprogdir
96
97static void setprogdir (lua_State *L) {
98 char buff[MAX_PATH + 1];
99 char *lb;
100 DWORD nsize = sizeof(buff)/sizeof(char);
101 DWORD n = GetModuleFileNameA(NULL, buff, nsize);
102 if (n == 0 || n == nsize || (lb = strrchr(buff, '\\')) == NULL)
103 luaL_error(L, "unable to get ModuleFileName");
104 else {
105 *lb = '\0';
106 luaL_gsub(L, lua_tostring(L, -1), LUA_EXECDIR, buff);
107 lua_remove(L, -2); /* remove original string */
108 }
109}
110
111
112static void pusherror (lua_State *L) {
113 int error = GetLastError();
114 char buffer[128];
115 if (FormatMessageA(FORMAT_MESSAGE_IGNORE_INSERTS | FORMAT_MESSAGE_FROM_SYSTEM,
116 NULL, error, 0, buffer, sizeof(buffer), NULL))
117 lua_pushstring(L, buffer);
118 else
119 lua_pushfstring(L, "system error %d\n", error);
120}
121
122static void ll_unloadlib (void *lib) {
123 FreeLibrary((HINSTANCE)lib);
124}
125
126
127static void *ll_load (lua_State *L, const char *path) {
128 HINSTANCE lib = LoadLibraryA(path);
129 if (lib == NULL) pusherror(L);
130 return lib;
131}
132
133
134static lua_CFunction ll_sym (lua_State *L, void *lib, const char *sym) {
135 lua_CFunction f = (lua_CFunction)GetProcAddress((HINSTANCE)lib, sym);
136 if (f == NULL) pusherror(L);
137 return f;
138}
139
140/* }====================================================== */
141
142
143
144#elif defined(LUA_DL_DYLD)
145/*
146** {======================================================================
147** Native Mac OS X / Darwin Implementation
148** =======================================================================
149*/
150
151#include <mach-o/dyld.h>
152
153
154/* Mac appends a `_' before C function names */
155#undef POF
156#define POF "_" LUA_POF
157
158
159static void pusherror (lua_State *L) {
160 const char *err_str;
161 const char *err_file;
162 NSLinkEditErrors err;
163 int err_num;
164 NSLinkEditError(&err, &err_num, &err_file, &err_str);
165 lua_pushstring(L, err_str);
166}
167
168
169static const char *errorfromcode (NSObjectFileImageReturnCode ret) {
170 switch (ret) {
171 case NSObjectFileImageInappropriateFile:
172 return "file is not a bundle";
173 case NSObjectFileImageArch:
174 return "library is for wrong CPU type";
175 case NSObjectFileImageFormat:
176 return "bad format";
177 case NSObjectFileImageAccess:
178 return "cannot access file";
179 case NSObjectFileImageFailure:
180 default:
181 return "unable to load library";
182 }
183}
184
185
186static void ll_unloadlib (void *lib) {
187 NSUnLinkModule((NSModule)lib, NSUNLINKMODULE_OPTION_RESET_LAZY_REFERENCES);
188}
189
190
191static void *ll_load (lua_State *L, const char *path) {
192 NSObjectFileImage img;
193 NSObjectFileImageReturnCode ret;
194 /* this would be a rare case, but prevents crashing if it happens */
195 if(!_dyld_present()) {
196 lua_pushliteral(L, "dyld not present");
197 return NULL;
198 }
199 ret = NSCreateObjectFileImageFromFile(path, &img);
200 if (ret == NSObjectFileImageSuccess) {
201 NSModule mod = NSLinkModule(img, path, NSLINKMODULE_OPTION_PRIVATE |
202 NSLINKMODULE_OPTION_RETURN_ON_ERROR);
203 NSDestroyObjectFileImage(img);
204 if (mod == NULL) pusherror(L);
205 return mod;
206 }
207 lua_pushstring(L, errorfromcode(ret));
208 return NULL;
209}
210
211
212static lua_CFunction ll_sym (lua_State *L, void *lib, const char *sym) {
213 NSSymbol nss = NSLookupSymbolInModule((NSModule)lib, sym);
214 if (nss == NULL) {
215 lua_pushfstring(L, "symbol " LUA_QS " not found", sym);
216 return NULL;
217 }
218 return (lua_CFunction)NSAddressOfSymbol(nss);
219}
220
221/* }====================================================== */
222
223
224
225#else
226/*
227** {======================================================
228** Fallback for other systems
229** =======================================================
230*/
231
232#undef LIB_FAIL
233#define LIB_FAIL "absent"
234
235
236#define DLMSG "dynamic libraries not enabled; check your Lua installation"
237
238
239static void ll_unloadlib (void *lib) {
240 (void)lib; /* to avoid warnings */
241}
242
243
244static void *ll_load (lua_State *L, const char *path) {
245 (void)path; /* to avoid warnings */
246 lua_pushliteral(L, DLMSG);
247 return NULL;
248}
249
250
251static lua_CFunction ll_sym (lua_State *L, void *lib, const char *sym) {
252 (void)lib; (void)sym; /* to avoid warnings */
253 lua_pushliteral(L, DLMSG);
254 return NULL;
255}
256
257/* }====================================================== */
258#endif
259
260
261
262static void **ll_register (lua_State *L, const char *path) {
263 void **plib;
264 lua_pushfstring(L, "%s%s", LIBPREFIX, path);
265 lua_gettable(L, LUA_REGISTRYINDEX); /* check library in registry? */
266 if (!lua_isnil(L, -1)) /* is there an entry? */
267 plib = (void **)lua_touserdata(L, -1);
268 else { /* no entry yet; create one */
269 lua_pop(L, 1);
270 plib = (void **)lua_newuserdata(L, sizeof(const void *));
271 *plib = NULL;
272 luaL_getmetatable(L, "_LOADLIB");
273 lua_setmetatable(L, -2);
274 lua_pushfstring(L, "%s%s", LIBPREFIX, path);
275 lua_pushvalue(L, -2);
276 lua_settable(L, LUA_REGISTRYINDEX);
277 }
278 return plib;
279}
280
281
282/*
283** __gc tag method: calls library's `ll_unloadlib' function with the lib
284** handle
285*/
286static int gctm (lua_State *L) {
287 void **lib = (void **)luaL_checkudata(L, 1, "_LOADLIB");
288 if (*lib) ll_unloadlib(*lib);
289 *lib = NULL; /* mark library as closed */
290 return 0;
291}
292
293
294static int ll_loadfunc (lua_State *L, const char *path, const char *sym) {
295 void **reg = ll_register(L, path);
296 if (*reg == NULL) *reg = ll_load(L, path);
297 if (*reg == NULL)
298 return ERRLIB; /* unable to load library */
299 else {
300 lua_CFunction f = ll_sym(L, *reg, sym);
301 if (f == NULL)
302 return ERRFUNC; /* unable to find function */
303 lua_pushcfunction(L, f);
304 return 0; /* return function */
305 }
306}
307
308
309static int ll_loadlib (lua_State *L) {
310 const char *path = luaL_checkstring(L, 1);
311 const char *init = luaL_checkstring(L, 2);
312 int stat = ll_loadfunc(L, path, init);
313 if (stat == 0) /* no errors? */
314 return 1; /* return the loaded function */
315 else { /* error; error message is on stack top */
316 lua_pushnil(L);
317 lua_insert(L, -2);
318 lua_pushstring(L, (stat == ERRLIB) ? LIB_FAIL : "init");
319 return 3; /* return nil, error message, and where */
320 }
321}
322
323
324
325/*
326** {======================================================
327** 'require' function
328** =======================================================
329*/
330
331
332static int readable (const char *filename) {
333 FILE *f = fopen(filename, "r"); /* try to open file */
334 if (f == NULL) return 0; /* open failed */
335 fclose(f);
336 return 1;
337}
338
339
340static const char *pushnexttemplate (lua_State *L, const char *path) {
341 const char *l;
342 while (*path == *LUA_PATHSEP) path++; /* skip separators */
343 if (*path == '\0') return NULL; /* no more templates */
344 l = strchr(path, *LUA_PATHSEP); /* find next separator */
345 if (l == NULL) l = path + strlen(path);
346 lua_pushlstring(L, path, l - path); /* template */
347 return l;
348}
349
350
351static const char *findfile (lua_State *L, const char *name,
352 const char *pname) {
353 const char *path;
354 name = luaL_gsub(L, name, ".", LUA_DIRSEP);
355 lua_getfield(L, LUA_ENVIRONINDEX, pname);
356 path = lua_tostring(L, -1);
357 if (path == NULL)
358 luaL_error(L, LUA_QL("package.%s") " must be a string", pname);
359 lua_pushliteral(L, ""); /* error accumulator */
360 while ((path = pushnexttemplate(L, path)) != NULL) {
361 const char *filename;
362 filename = luaL_gsub(L, lua_tostring(L, -1), LUA_PATH_MARK, name);
363 lua_remove(L, -2); /* remove path template */
364 if (readable(filename)) /* does file exist and is readable? */
365 return filename; /* return that file name */
366 lua_pushfstring(L, "\n\tno file " LUA_QS, filename);
367 lua_remove(L, -2); /* remove file name */
368 lua_concat(L, 2); /* add entry to possible error message */
369 }
370 return NULL; /* not found */
371}
372
373
374static void loaderror (lua_State *L, const char *filename) {
375 luaL_error(L, "error loading module " LUA_QS " from file " LUA_QS ":\n\t%s",
376 lua_tostring(L, 1), filename, lua_tostring(L, -1));
377}
378
379
380static int loader_Lua (lua_State *L) {
381 const char *filename;
382 const char *name = luaL_checkstring(L, 1);
383 filename = findfile(L, name, "path");
384 if (filename == NULL) return 1; /* library not found in this path */
385 if (luaL_loadfile(L, filename) != 0)
386 loaderror(L, filename);
387 return 1; /* library loaded successfully */
388}
389
390
391static const char *mkfuncname (lua_State *L, const char *modname) {
392 const char *funcname;
393 const char *mark = strchr(modname, *LUA_IGMARK);
394 if (mark) modname = mark + 1;
395 funcname = luaL_gsub(L, modname, ".", LUA_OFSEP);
396 funcname = lua_pushfstring(L, POF"%s", funcname);
397 lua_remove(L, -2); /* remove 'gsub' result */
398 return funcname;
399}
400
401
402static int loader_C (lua_State *L) {
403 const char *funcname;
404 const char *name = luaL_checkstring(L, 1);
405 const char *filename = findfile(L, name, "cpath");
406 if (filename == NULL) return 1; /* library not found in this path */
407 funcname = mkfuncname(L, name);
408 if (ll_loadfunc(L, filename, funcname) != 0)
409 loaderror(L, filename);
410 return 1; /* library loaded successfully */
411}
412
413
414static int loader_Croot (lua_State *L) {
415 const char *funcname;
416 const char *filename;
417 const char *name = luaL_checkstring(L, 1);
418 const char *p = strchr(name, '.');
419 int stat;
420 if (p == NULL) return 0; /* is root */
421 lua_pushlstring(L, name, p - name);
422 filename = findfile(L, lua_tostring(L, -1), "cpath");
423 if (filename == NULL) return 1; /* root not found */
424 funcname = mkfuncname(L, name);
425 if ((stat = ll_loadfunc(L, filename, funcname)) != 0) {
426 if (stat != ERRFUNC) loaderror(L, filename); /* real error */
427 lua_pushfstring(L, "\n\tno module " LUA_QS " in file " LUA_QS,
428 name, filename);
429 return 1; /* function not found */
430 }
431 return 1;
432}
433
434
435static int loader_preload (lua_State *L) {
436 const char *name = luaL_checkstring(L, 1);
437 lua_getfield(L, LUA_ENVIRONINDEX, "preload");
438 if (!lua_istable(L, -1))
439 luaL_error(L, LUA_QL("package.preload") " must be a table");
440 lua_getfield(L, -1, name);
441 if (lua_isnil(L, -1)) /* not found? */
442 lua_pushfstring(L, "\n\tno field package.preload['%s']", name);
443 return 1;
444}
445
446
447static const int sentinel_ = 0;
448#define sentinel ((void *)&sentinel_)
449
450
451static int ll_require (lua_State *L) {
452 const char *name = luaL_checkstring(L, 1);
453 int i;
454 lua_settop(L, 1); /* _LOADED table will be at index 2 */
455 lua_getfield(L, LUA_REGISTRYINDEX, "_LOADED");
456 lua_getfield(L, 2, name);
457 if (lua_toboolean(L, -1)) { /* is it there? */
458 if (lua_touserdata(L, -1) == sentinel) /* check loops */
459 luaL_error(L, "loop or previous error loading module " LUA_QS, name);
460 return 1; /* package is already loaded */
461 }
462 /* else must load it; iterate over available loaders */
463 lua_getfield(L, LUA_ENVIRONINDEX, "loaders");
464 if (!lua_istable(L, -1))
465 luaL_error(L, LUA_QL("package.loaders") " must be a table");
466 lua_pushliteral(L, ""); /* error message accumulator */
467 for (i=1; ; i++) {
468 lua_rawgeti(L, -2, i); /* get a loader */
469 if (lua_isnil(L, -1))
470 luaL_error(L, "module " LUA_QS " not found:%s",
471 name, lua_tostring(L, -2));
472 lua_pushstring(L, name);
473 lua_call(L, 1, 1); /* call it */
474 if (lua_isfunction(L, -1)) /* did it find module? */
475 break; /* module loaded successfully */
476 else if (lua_isstring(L, -1)) /* loader returned error message? */
477 lua_concat(L, 2); /* accumulate it */
478 else
479 lua_pop(L, 1);
480 }
481 lua_pushlightuserdata(L, sentinel);
482 lua_setfield(L, 2, name); /* _LOADED[name] = sentinel */
483 lua_pushstring(L, name); /* pass name as argument to module */
484 lua_call(L, 1, 1); /* run loaded module */
485 if (!lua_isnil(L, -1)) /* non-nil return? */
486 lua_setfield(L, 2, name); /* _LOADED[name] = returned value */
487 lua_getfield(L, 2, name);
488 if (lua_touserdata(L, -1) == sentinel) { /* module did not set a value? */
489 lua_pushboolean(L, 1); /* use true as result */
490 lua_pushvalue(L, -1); /* extra copy to be returned */
491 lua_setfield(L, 2, name); /* _LOADED[name] = true */
492 }
493 return 1;
494}
495
496/* }====================================================== */
497
498
499
500/*
501** {======================================================
502** 'module' function
503** =======================================================
504*/
505
506
507static void setfenv (lua_State *L) {
508 lua_Debug ar;
509 if (lua_getstack(L, 1, &ar) == 0 ||
510 lua_getinfo(L, "f", &ar) == 0 || /* get calling function */
511 lua_iscfunction(L, -1))
512 luaL_error(L, LUA_QL("module") " not called from a Lua function");
513 lua_pushvalue(L, -2);
514 lua_setfenv(L, -2);
515 lua_pop(L, 1);
516}
517
518
519static void dooptions (lua_State *L, int n) {
520 int i;
521 for (i = 2; i <= n; i++) {
522 lua_pushvalue(L, i); /* get option (a function) */
523 lua_pushvalue(L, -2); /* module */
524 lua_call(L, 1, 0);
525 }
526}
527
528
529static void modinit (lua_State *L, const char *modname) {
530 const char *dot;
531 lua_pushvalue(L, -1);
532 lua_setfield(L, -2, "_M"); /* module._M = module */
533 lua_pushstring(L, modname);
534 lua_setfield(L, -2, "_NAME");
535 dot = strrchr(modname, '.'); /* look for last dot in module name */
536 if (dot == NULL) dot = modname;
537 else dot++;
538 /* set _PACKAGE as package name (full module name minus last part) */
539 lua_pushlstring(L, modname, dot - modname);
540 lua_setfield(L, -2, "_PACKAGE");
541}
542
543
544static int ll_module (lua_State *L) {
545 const char *modname = luaL_checkstring(L, 1);
546 int loaded = lua_gettop(L) + 1; /* index of _LOADED table */
547 lua_getfield(L, LUA_REGISTRYINDEX, "_LOADED");
548 lua_getfield(L, loaded, modname); /* get _LOADED[modname] */
549 if (!lua_istable(L, -1)) { /* not found? */
550 lua_pop(L, 1); /* remove previous result */
551 /* try global variable (and create one if it does not exist) */
552 if (luaL_findtable(L, LUA_GLOBALSINDEX, modname, 1) != NULL)
553 return luaL_error(L, "name conflict for module " LUA_QS, modname);
554 lua_pushvalue(L, -1);
555 lua_setfield(L, loaded, modname); /* _LOADED[modname] = new table */
556 }
557 /* check whether table already has a _NAME field */
558 lua_getfield(L, -1, "_NAME");
559 if (!lua_isnil(L, -1)) /* is table an initialized module? */
560 lua_pop(L, 1);
561 else { /* no; initialize it */
562 lua_pop(L, 1);
563 modinit(L, modname);
564 }
565 lua_pushvalue(L, -1);
566 setfenv(L);
567 dooptions(L, loaded - 1);
568 return 0;
569}
570
571
572static int ll_seeall (lua_State *L) {
573 luaL_checktype(L, 1, LUA_TTABLE);
574 if (!lua_getmetatable(L, 1)) {
575 lua_createtable(L, 0, 1); /* create new metatable */
576 lua_pushvalue(L, -1);
577 lua_setmetatable(L, 1);
578 }
579 lua_pushvalue(L, LUA_GLOBALSINDEX);
580 lua_setfield(L, -2, "__index"); /* mt.__index = _G */
581 return 0;
582}
583
584
585/* }====================================================== */
586
587
588
589/* auxiliary mark (for internal use) */
590#define AUXMARK "\1"
591
592static void setpath (lua_State *L, const char *fieldname, const char *envname,
593 const char *def) {
594 const char *path = getenv(envname);
595 if (path == NULL) /* no environment variable? */
596 lua_pushstring(L, def); /* use default */
597 else {
598 /* replace ";;" by ";AUXMARK;" and then AUXMARK by default path */
599 path = luaL_gsub(L, path, LUA_PATHSEP LUA_PATHSEP,
600 LUA_PATHSEP AUXMARK LUA_PATHSEP);
601 luaL_gsub(L, path, AUXMARK, def);
602 lua_remove(L, -2);
603 }
604 setprogdir(L);
605 lua_setfield(L, -2, fieldname);
606}
607
608
609static const luaL_Reg pk_funcs[] = {
610 {"loadlib", ll_loadlib},
611 {"seeall", ll_seeall},
612 {NULL, NULL}
613};
614
615
616static const luaL_Reg ll_funcs[] = {
617 {"module", ll_module},
618 {"require", ll_require},
619 {NULL, NULL}
620};
621
622
623static const lua_CFunction loaders[] =
624 {loader_preload, loader_Lua, loader_C, loader_Croot, NULL};
625
626
627LUALIB_API int luaopen_package (lua_State *L) {
628 int i;
629 /* create new type _LOADLIB */
630 luaL_newmetatable(L, "_LOADLIB");
631 lua_pushcfunction(L, gctm);
632 lua_setfield(L, -2, "__gc");
633 /* create `package' table */
634 luaL_register(L, LUA_LOADLIBNAME, pk_funcs);
635#if defined(LUA_COMPAT_LOADLIB)
636 lua_getfield(L, -1, "loadlib");
637 lua_setfield(L, LUA_GLOBALSINDEX, "loadlib");
638#endif
639 lua_pushvalue(L, -1);
640 lua_replace(L, LUA_ENVIRONINDEX);
641 /* create `loaders' table */
642 lua_createtable(L, sizeof(loaders)/sizeof(loaders[0]) - 1, 0);
643 /* fill it with pre-defined loaders */
644 for (i=0; loaders[i] != NULL; i++) {
645 lua_pushcfunction(L, loaders[i]);
646 lua_rawseti(L, -2, i+1);
647 }
648 lua_setfield(L, -2, "loaders"); /* put it in field `loaders' */
649 setpath(L, "path", LUA_PATH, LUA_PATH_DEFAULT); /* set field `path' */
650 setpath(L, "cpath", LUA_CPATH, LUA_CPATH_DEFAULT); /* set field `cpath' */
651 /* store config information */
652 lua_pushliteral(L, LUA_DIRSEP "\n" LUA_PATHSEP "\n" LUA_PATH_MARK "\n"
653 LUA_EXECDIR "\n" LUA_IGMARK);
654 lua_setfield(L, -2, "config");
655 /* set field `loaded' */
656 luaL_findtable(L, LUA_REGISTRYINDEX, "_LOADED", 2);
657 lua_setfield(L, -2, "loaded");
658 /* set field `preload' */
659 lua_newtable(L);
660 lua_setfield(L, -2, "preload");
661 lua_pushvalue(L, LUA_GLOBALSINDEX);
662 luaL_register(L, NULL, ll_funcs); /* open lib into global table */
663 lua_pop(L, 1);
664 return 1; /* return 'package' table */
665}
666
diff --git a/examples/redis-unstable/deps/lua/src/lobject.c b/examples/redis-unstable/deps/lua/src/lobject.c
deleted file mode 100644
index 4ff5073..0000000
--- a/examples/redis-unstable/deps/lua/src/lobject.c
+++ /dev/null
@@ -1,214 +0,0 @@
1/*
2** $Id: lobject.c,v 2.22.1.1 2007/12/27 13:02:25 roberto Exp $
3** Some generic functions over Lua objects
4** See Copyright Notice in lua.h
5*/
6
7#include <ctype.h>
8#include <stdarg.h>
9#include <stdio.h>
10#include <stdlib.h>
11#include <string.h>
12
13#define lobject_c
14#define LUA_CORE
15
16#include "lua.h"
17
18#include "ldo.h"
19#include "lmem.h"
20#include "lobject.h"
21#include "lstate.h"
22#include "lstring.h"
23#include "lvm.h"
24
25
26
27const TValue luaO_nilobject_ = {{NULL}, LUA_TNIL};
28
29
30/*
31** converts an integer to a "floating point byte", represented as
32** (eeeeexxx), where the real value is (1xxx) * 2^(eeeee - 1) if
33** eeeee != 0 and (xxx) otherwise.
34*/
35int luaO_int2fb (unsigned int x) {
36 int e = 0; /* expoent */
37 while (x >= 16) {
38 x = (x+1) >> 1;
39 e++;
40 }
41 if (x < 8) return x;
42 else return ((e+1) << 3) | (cast_int(x) - 8);
43}
44
45
46/* converts back */
47int luaO_fb2int (int x) {
48 int e = (x >> 3) & 31;
49 if (e == 0) return x;
50 else return ((x & 7)+8) << (e - 1);
51}
52
53
54int luaO_log2 (unsigned int x) {
55 static const lu_byte log_2[256] = {
56 0,1,2,2,3,3,3,3,4,4,4,4,4,4,4,4,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,
57 6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,
58 7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,
59 7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,
60 8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,
61 8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,
62 8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,
63 8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8
64 };
65 int l = -1;
66 while (x >= 256) { l += 8; x >>= 8; }
67 return l + log_2[x];
68
69}
70
71
72int luaO_rawequalObj (const TValue *t1, const TValue *t2) {
73 if (ttype(t1) != ttype(t2)) return 0;
74 else switch (ttype(t1)) {
75 case LUA_TNIL:
76 return 1;
77 case LUA_TNUMBER:
78 return luai_numeq(nvalue(t1), nvalue(t2));
79 case LUA_TBOOLEAN:
80 return bvalue(t1) == bvalue(t2); /* boolean true must be 1 !! */
81 case LUA_TLIGHTUSERDATA:
82 return pvalue(t1) == pvalue(t2);
83 default:
84 lua_assert(iscollectable(t1));
85 return gcvalue(t1) == gcvalue(t2);
86 }
87}
88
89
90int luaO_str2d (const char *s, lua_Number *result) {
91 char *endptr;
92 *result = lua_str2number(s, &endptr);
93 if (endptr == s) return 0; /* conversion failed */
94 if (*endptr == 'x' || *endptr == 'X') /* maybe an hexadecimal constant? */
95 *result = cast_num(strtoul(s, &endptr, 16));
96 if (*endptr == '\0') return 1; /* most common case */
97 while (isspace(cast(unsigned char, *endptr))) endptr++;
98 if (*endptr != '\0') return 0; /* invalid trailing characters? */
99 return 1;
100}
101
102
103
104static void pushstr (lua_State *L, const char *str) {
105 setsvalue2s(L, L->top, luaS_new(L, str));
106 incr_top(L);
107}
108
109
110/* this function handles only `%d', `%c', %f, %p, and `%s' formats */
111const char *luaO_pushvfstring (lua_State *L, const char *fmt, va_list argp) {
112 int n = 1;
113 pushstr(L, "");
114 for (;;) {
115 const char *e = strchr(fmt, '%');
116 if (e == NULL) break;
117 setsvalue2s(L, L->top, luaS_newlstr(L, fmt, e-fmt));
118 incr_top(L);
119 switch (*(e+1)) {
120 case 's': {
121 const char *s = va_arg(argp, char *);
122 if (s == NULL) s = "(null)";
123 pushstr(L, s);
124 break;
125 }
126 case 'c': {
127 char buff[2];
128 buff[0] = cast(char, va_arg(argp, int));
129 buff[1] = '\0';
130 pushstr(L, buff);
131 break;
132 }
133 case 'd': {
134 setnvalue(L->top, cast_num(va_arg(argp, int)));
135 incr_top(L);
136 break;
137 }
138 case 'f': {
139 setnvalue(L->top, cast_num(va_arg(argp, l_uacNumber)));
140 incr_top(L);
141 break;
142 }
143 case 'p': {
144 char buff[4*sizeof(void *) + 8]; /* should be enough space for a `%p' */
145 sprintf(buff, "%p", va_arg(argp, void *));
146 pushstr(L, buff);
147 break;
148 }
149 case '%': {
150 pushstr(L, "%");
151 break;
152 }
153 default: {
154 char buff[3];
155 buff[0] = '%';
156 buff[1] = *(e+1);
157 buff[2] = '\0';
158 pushstr(L, buff);
159 break;
160 }
161 }
162 n += 2;
163 fmt = e+2;
164 }
165 pushstr(L, fmt);
166 luaV_concat(L, n+1, cast_int(L->top - L->base) - 1);
167 L->top -= n;
168 return svalue(L->top - 1);
169}
170
171
172const char *luaO_pushfstring (lua_State *L, const char *fmt, ...) {
173 const char *msg;
174 va_list argp;
175 va_start(argp, fmt);
176 msg = luaO_pushvfstring(L, fmt, argp);
177 va_end(argp);
178 return msg;
179}
180
181
182void luaO_chunkid (char *out, const char *source, size_t bufflen) {
183 if (*source == '=') {
184 strncpy(out, source+1, bufflen); /* remove first char */
185 out[bufflen-1] = '\0'; /* ensures null termination */
186 }
187 else { /* out = "source", or "...source" */
188 if (*source == '@') {
189 size_t l;
190 source++; /* skip the `@' */
191 bufflen -= sizeof(" '...' ");
192 l = strlen(source);
193 strcpy(out, "");
194 if (l > bufflen) {
195 source += (l-bufflen); /* get last part of file name */
196 strcat(out, "...");
197 }
198 strcat(out, source);
199 }
200 else { /* out = [string "string"] */
201 size_t len = strcspn(source, "\n\r"); /* stop at first newline */
202 bufflen -= sizeof(" [string \"...\"] ");
203 if (len > bufflen) len = bufflen;
204 strcpy(out, "[string \"");
205 if (source[len] != '\0') { /* must truncate? */
206 strncat(out, source, len);
207 strcat(out, "...");
208 }
209 else
210 strcat(out, source);
211 strcat(out, "\"]");
212 }
213 }
214}
diff --git a/examples/redis-unstable/deps/lua/src/lobject.h b/examples/redis-unstable/deps/lua/src/lobject.h
deleted file mode 100644
index 10e46b1..0000000
--- a/examples/redis-unstable/deps/lua/src/lobject.h
+++ /dev/null
@@ -1,382 +0,0 @@
1/*
2** $Id: lobject.h,v 2.20.1.2 2008/08/06 13:29:48 roberto Exp $
3** Type definitions for Lua objects
4** See Copyright Notice in lua.h
5*/
6
7
8#ifndef lobject_h
9#define lobject_h
10
11
12#include <stdarg.h>
13
14
15#include "llimits.h"
16#include "lua.h"
17
18
19/* tags for values visible from Lua */
20#define LAST_TAG LUA_TTHREAD
21
22#define NUM_TAGS (LAST_TAG+1)
23
24
25/*
26** Extra tags for non-values
27*/
28#define LUA_TPROTO (LAST_TAG+1)
29#define LUA_TUPVAL (LAST_TAG+2)
30#define LUA_TDEADKEY (LAST_TAG+3)
31
32
33/*
34** Union of all collectable objects
35*/
36typedef union GCObject GCObject;
37
38
39/*
40** Common Header for all collectable objects (in macro form, to be
41** included in other objects)
42*/
43#define CommonHeader GCObject *next; lu_byte tt; lu_byte marked
44
45
46/*
47** Common header in struct form
48*/
49typedef struct GCheader {
50 CommonHeader;
51} GCheader;
52
53
54
55
56/*
57** Union of all Lua values
58*/
59typedef union {
60 GCObject *gc;
61 void *p;
62 lua_Number n;
63 int b;
64} Value;
65
66
67/*
68** Tagged Values
69*/
70
71#define TValuefields Value value; int tt
72
73typedef struct lua_TValue {
74 TValuefields;
75} TValue;
76
77
78/* Macros to test type */
79#define ttisnil(o) (ttype(o) == LUA_TNIL)
80#define ttisnumber(o) (ttype(o) == LUA_TNUMBER)
81#define ttisstring(o) (ttype(o) == LUA_TSTRING)
82#define ttistable(o) (ttype(o) == LUA_TTABLE)
83#define ttisfunction(o) (ttype(o) == LUA_TFUNCTION)
84#define ttisboolean(o) (ttype(o) == LUA_TBOOLEAN)
85#define ttisuserdata(o) (ttype(o) == LUA_TUSERDATA)
86#define ttisthread(o) (ttype(o) == LUA_TTHREAD)
87#define ttislightuserdata(o) (ttype(o) == LUA_TLIGHTUSERDATA)
88
89/* Macros to access values */
90#define ttype(o) ((o)->tt)
91#define gcvalue(o) check_exp(iscollectable(o), (o)->value.gc)
92#define pvalue(o) check_exp(ttislightuserdata(o), (o)->value.p)
93#define nvalue(o) check_exp(ttisnumber(o), (o)->value.n)
94#define rawtsvalue(o) check_exp(ttisstring(o), &(o)->value.gc->ts)
95#define tsvalue(o) (&rawtsvalue(o)->tsv)
96#define rawuvalue(o) check_exp(ttisuserdata(o), &(o)->value.gc->u)
97#define uvalue(o) (&rawuvalue(o)->uv)
98#define clvalue(o) check_exp(ttisfunction(o), &(o)->value.gc->cl)
99#define hvalue(o) check_exp(ttistable(o), &(o)->value.gc->h)
100#define bvalue(o) check_exp(ttisboolean(o), (o)->value.b)
101#define thvalue(o) check_exp(ttisthread(o), &(o)->value.gc->th)
102
103#define l_isfalse(o) (ttisnil(o) || (ttisboolean(o) && bvalue(o) == 0))
104
105/*
106** for internal debug only
107*/
108#define checkconsistency(obj) \
109 lua_assert(!iscollectable(obj) || (ttype(obj) == (obj)->value.gc->gch.tt))
110
111#define checkliveness(g,obj) \
112 lua_assert(!iscollectable(obj) || \
113 ((ttype(obj) == (obj)->value.gc->gch.tt) && !isdead(g, (obj)->value.gc)))
114
115
116/* Macros to set values */
117#define setnilvalue(obj) ((obj)->tt=LUA_TNIL)
118
119#define setnvalue(obj,x) \
120 { TValue *i_o=(obj); i_o->value.n=(x); i_o->tt=LUA_TNUMBER; }
121
122#define setpvalue(obj,x) \
123 { TValue *i_o=(obj); i_o->value.p=(x); i_o->tt=LUA_TLIGHTUSERDATA; }
124
125#define setbvalue(obj,x) \
126 { TValue *i_o=(obj); i_o->value.b=(x); i_o->tt=LUA_TBOOLEAN; }
127
128#define setsvalue(L,obj,x) \
129 { TValue *i_o=(obj); \
130 i_o->value.gc=cast(GCObject *, (x)); i_o->tt=LUA_TSTRING; \
131 checkliveness(G(L),i_o); }
132
133#define setuvalue(L,obj,x) \
134 { TValue *i_o=(obj); \
135 i_o->value.gc=cast(GCObject *, (x)); i_o->tt=LUA_TUSERDATA; \
136 checkliveness(G(L),i_o); }
137
138#define setthvalue(L,obj,x) \
139 { TValue *i_o=(obj); \
140 i_o->value.gc=cast(GCObject *, (x)); i_o->tt=LUA_TTHREAD; \
141 checkliveness(G(L),i_o); }
142
143#define setclvalue(L,obj,x) \
144 { TValue *i_o=(obj); \
145 i_o->value.gc=cast(GCObject *, (x)); i_o->tt=LUA_TFUNCTION; \
146 checkliveness(G(L),i_o); }
147
148#define sethvalue(L,obj,x) \
149 { TValue *i_o=(obj); \
150 i_o->value.gc=cast(GCObject *, (x)); i_o->tt=LUA_TTABLE; \
151 checkliveness(G(L),i_o); }
152
153#define setptvalue(L,obj,x) \
154 { TValue *i_o=(obj); \
155 i_o->value.gc=cast(GCObject *, (x)); i_o->tt=LUA_TPROTO; \
156 checkliveness(G(L),i_o); }
157
158
159
160
161#define setobj(L,obj1,obj2) \
162 { const TValue *o2=(obj2); TValue *o1=(obj1); \
163 o1->value = o2->value; o1->tt=o2->tt; \
164 checkliveness(G(L),o1); }
165
166
167/*
168** different types of sets, according to destination
169*/
170
171/* from stack to (same) stack */
172#define setobjs2s setobj
173/* to stack (not from same stack) */
174#define setobj2s setobj
175#define setsvalue2s setsvalue
176#define sethvalue2s sethvalue
177#define setptvalue2s setptvalue
178/* from table to same table */
179#define setobjt2t setobj
180/* to table */
181#define setobj2t setobj
182/* to new object */
183#define setobj2n setobj
184#define setsvalue2n setsvalue
185
186#define setttype(obj, tt) (ttype(obj) = (tt))
187
188
189#define iscollectable(o) (ttype(o) >= LUA_TSTRING)
190
191
192
193typedef TValue *StkId; /* index to stack elements */
194
195
196/*
197** String headers for string table
198*/
199typedef union TString {
200 L_Umaxalign dummy; /* ensures maximum alignment for strings */
201 struct {
202 CommonHeader;
203 lu_byte reserved;
204 unsigned int hash;
205 size_t len;
206 } tsv;
207} TString;
208
209
210#define getstr(ts) cast(const char *, (ts) + 1)
211#define svalue(o) getstr(rawtsvalue(o))
212
213
214
215typedef union Udata {
216 L_Umaxalign dummy; /* ensures maximum alignment for `local' udata */
217 struct {
218 CommonHeader;
219 struct Table *metatable;
220 struct Table *env;
221 size_t len;
222 } uv;
223} Udata;
224
225
226
227
228/*
229** Function Prototypes
230*/
231typedef struct Proto {
232 CommonHeader;
233 TValue *k; /* constants used by the function */
234 Instruction *code;
235 struct Proto **p; /* functions defined inside the function */
236 int *lineinfo; /* map from opcodes to source lines */
237 struct LocVar *locvars; /* information about local variables */
238 TString **upvalues; /* upvalue names */
239 TString *source;
240 int sizeupvalues;
241 int sizek; /* size of `k' */
242 int sizecode;
243 int sizelineinfo;
244 int sizep; /* size of `p' */
245 int sizelocvars;
246 int linedefined;
247 int lastlinedefined;
248 GCObject *gclist;
249 lu_byte nups; /* number of upvalues */
250 lu_byte numparams;
251 lu_byte is_vararg;
252 lu_byte maxstacksize;
253} Proto;
254
255
256/* masks for new-style vararg */
257#define VARARG_HASARG 1
258#define VARARG_ISVARARG 2
259#define VARARG_NEEDSARG 4
260
261
262typedef struct LocVar {
263 TString *varname;
264 int startpc; /* first point where variable is active */
265 int endpc; /* first point where variable is dead */
266} LocVar;
267
268
269
270/*
271** Upvalues
272*/
273
274typedef struct UpVal {
275 CommonHeader;
276 TValue *v; /* points to stack or to its own value */
277 union {
278 TValue value; /* the value (when closed) */
279 struct { /* double linked list (when open) */
280 struct UpVal *prev;
281 struct UpVal *next;
282 } l;
283 } u;
284} UpVal;
285
286
287/*
288** Closures
289*/
290
291#define ClosureHeader \
292 CommonHeader; lu_byte isC; lu_byte nupvalues; GCObject *gclist; \
293 struct Table *env
294
295typedef struct CClosure {
296 ClosureHeader;
297 lua_CFunction f;
298 TValue upvalue[1];
299} CClosure;
300
301
302typedef struct LClosure {
303 ClosureHeader;
304 struct Proto *p;
305 UpVal *upvals[1];
306} LClosure;
307
308
309typedef union Closure {
310 CClosure c;
311 LClosure l;
312} Closure;
313
314
315#define iscfunction(o) (ttype(o) == LUA_TFUNCTION && clvalue(o)->c.isC)
316#define isLfunction(o) (ttype(o) == LUA_TFUNCTION && !clvalue(o)->c.isC)
317
318
319/*
320** Tables
321*/
322
323typedef union TKey {
324 struct {
325 TValuefields;
326 struct Node *next; /* for chaining */
327 } nk;
328 TValue tvk;
329} TKey;
330
331
332typedef struct Node {
333 TValue i_val;
334 TKey i_key;
335} Node;
336
337
338typedef struct Table {
339 CommonHeader;
340 lu_byte flags; /* 1<<p means tagmethod(p) is not present */
341 int readonly;
342 lu_byte lsizenode; /* log2 of size of `node' array */
343 struct Table *metatable;
344 TValue *array; /* array part */
345 Node *node;
346 Node *lastfree; /* any free position is before this position */
347 GCObject *gclist;
348 int sizearray; /* size of `array' array */
349} Table;
350
351
352
353/*
354** `module' operation for hashing (size is always a power of 2)
355*/
356#define lmod(s,size) \
357 (check_exp((size&(size-1))==0, (cast(int, (s) & ((size)-1)))))
358
359
360#define twoto(x) (1<<(x))
361#define sizenode(t) (twoto((t)->lsizenode))
362
363
364#define luaO_nilobject (&luaO_nilobject_)
365
366LUAI_DATA const TValue luaO_nilobject_;
367
368#define ceillog2(x) (luaO_log2((x)-1) + 1)
369
370LUAI_FUNC int luaO_log2 (unsigned int x);
371LUAI_FUNC int luaO_int2fb (unsigned int x);
372LUAI_FUNC int luaO_fb2int (int x);
373LUAI_FUNC int luaO_rawequalObj (const TValue *t1, const TValue *t2);
374LUAI_FUNC int luaO_str2d (const char *s, lua_Number *result);
375LUAI_FUNC const char *luaO_pushvfstring (lua_State *L, const char *fmt,
376 va_list argp);
377LUAI_FUNC const char *luaO_pushfstring (lua_State *L, const char *fmt, ...);
378LUAI_FUNC void luaO_chunkid (char *out, const char *source, size_t len);
379
380
381#endif
382
diff --git a/examples/redis-unstable/deps/lua/src/lopcodes.c b/examples/redis-unstable/deps/lua/src/lopcodes.c
deleted file mode 100644
index 4cc7452..0000000
--- a/examples/redis-unstable/deps/lua/src/lopcodes.c
+++ /dev/null
@@ -1,102 +0,0 @@
1/*
2** $Id: lopcodes.c,v 1.37.1.1 2007/12/27 13:02:25 roberto Exp $
3** See Copyright Notice in lua.h
4*/
5
6
7#define lopcodes_c
8#define LUA_CORE
9
10
11#include "lopcodes.h"
12
13
14/* ORDER OP */
15
16const char *const luaP_opnames[NUM_OPCODES+1] = {
17 "MOVE",
18 "LOADK",
19 "LOADBOOL",
20 "LOADNIL",
21 "GETUPVAL",
22 "GETGLOBAL",
23 "GETTABLE",
24 "SETGLOBAL",
25 "SETUPVAL",
26 "SETTABLE",
27 "NEWTABLE",
28 "SELF",
29 "ADD",
30 "SUB",
31 "MUL",
32 "DIV",
33 "MOD",
34 "POW",
35 "UNM",
36 "NOT",
37 "LEN",
38 "CONCAT",
39 "JMP",
40 "EQ",
41 "LT",
42 "LE",
43 "TEST",
44 "TESTSET",
45 "CALL",
46 "TAILCALL",
47 "RETURN",
48 "FORLOOP",
49 "FORPREP",
50 "TFORLOOP",
51 "SETLIST",
52 "CLOSE",
53 "CLOSURE",
54 "VARARG",
55 NULL
56};
57
58
59#define opmode(t,a,b,c,m) (((t)<<7) | ((a)<<6) | ((b)<<4) | ((c)<<2) | (m))
60
61const lu_byte luaP_opmodes[NUM_OPCODES] = {
62/* T A B C mode opcode */
63 opmode(0, 1, OpArgR, OpArgN, iABC) /* OP_MOVE */
64 ,opmode(0, 1, OpArgK, OpArgN, iABx) /* OP_LOADK */
65 ,opmode(0, 1, OpArgU, OpArgU, iABC) /* OP_LOADBOOL */
66 ,opmode(0, 1, OpArgR, OpArgN, iABC) /* OP_LOADNIL */
67 ,opmode(0, 1, OpArgU, OpArgN, iABC) /* OP_GETUPVAL */
68 ,opmode(0, 1, OpArgK, OpArgN, iABx) /* OP_GETGLOBAL */
69 ,opmode(0, 1, OpArgR, OpArgK, iABC) /* OP_GETTABLE */
70 ,opmode(0, 0, OpArgK, OpArgN, iABx) /* OP_SETGLOBAL */
71 ,opmode(0, 0, OpArgU, OpArgN, iABC) /* OP_SETUPVAL */
72 ,opmode(0, 0, OpArgK, OpArgK, iABC) /* OP_SETTABLE */
73 ,opmode(0, 1, OpArgU, OpArgU, iABC) /* OP_NEWTABLE */
74 ,opmode(0, 1, OpArgR, OpArgK, iABC) /* OP_SELF */
75 ,opmode(0, 1, OpArgK, OpArgK, iABC) /* OP_ADD */
76 ,opmode(0, 1, OpArgK, OpArgK, iABC) /* OP_SUB */
77 ,opmode(0, 1, OpArgK, OpArgK, iABC) /* OP_MUL */
78 ,opmode(0, 1, OpArgK, OpArgK, iABC) /* OP_DIV */
79 ,opmode(0, 1, OpArgK, OpArgK, iABC) /* OP_MOD */
80 ,opmode(0, 1, OpArgK, OpArgK, iABC) /* OP_POW */
81 ,opmode(0, 1, OpArgR, OpArgN, iABC) /* OP_UNM */
82 ,opmode(0, 1, OpArgR, OpArgN, iABC) /* OP_NOT */
83 ,opmode(0, 1, OpArgR, OpArgN, iABC) /* OP_LEN */
84 ,opmode(0, 1, OpArgR, OpArgR, iABC) /* OP_CONCAT */
85 ,opmode(0, 0, OpArgR, OpArgN, iAsBx) /* OP_JMP */
86 ,opmode(1, 0, OpArgK, OpArgK, iABC) /* OP_EQ */
87 ,opmode(1, 0, OpArgK, OpArgK, iABC) /* OP_LT */
88 ,opmode(1, 0, OpArgK, OpArgK, iABC) /* OP_LE */
89 ,opmode(1, 1, OpArgR, OpArgU, iABC) /* OP_TEST */
90 ,opmode(1, 1, OpArgR, OpArgU, iABC) /* OP_TESTSET */
91 ,opmode(0, 1, OpArgU, OpArgU, iABC) /* OP_CALL */
92 ,opmode(0, 1, OpArgU, OpArgU, iABC) /* OP_TAILCALL */
93 ,opmode(0, 0, OpArgU, OpArgN, iABC) /* OP_RETURN */
94 ,opmode(0, 1, OpArgR, OpArgN, iAsBx) /* OP_FORLOOP */
95 ,opmode(0, 1, OpArgR, OpArgN, iAsBx) /* OP_FORPREP */
96 ,opmode(1, 0, OpArgN, OpArgU, iABC) /* OP_TFORLOOP */
97 ,opmode(0, 0, OpArgU, OpArgU, iABC) /* OP_SETLIST */
98 ,opmode(0, 0, OpArgN, OpArgN, iABC) /* OP_CLOSE */
99 ,opmode(0, 1, OpArgU, OpArgN, iABx) /* OP_CLOSURE */
100 ,opmode(0, 1, OpArgU, OpArgN, iABC) /* OP_VARARG */
101};
102
diff --git a/examples/redis-unstable/deps/lua/src/lopcodes.h b/examples/redis-unstable/deps/lua/src/lopcodes.h
deleted file mode 100644
index 41224d6..0000000
--- a/examples/redis-unstable/deps/lua/src/lopcodes.h
+++ /dev/null
@@ -1,268 +0,0 @@
1/*
2** $Id: lopcodes.h,v 1.125.1.1 2007/12/27 13:02:25 roberto Exp $
3** Opcodes for Lua virtual machine
4** See Copyright Notice in lua.h
5*/
6
7#ifndef lopcodes_h
8#define lopcodes_h
9
10#include "llimits.h"
11
12
13/*===========================================================================
14 We assume that instructions are unsigned numbers.
15 All instructions have an opcode in the first 6 bits.
16 Instructions can have the following fields:
17 `A' : 8 bits
18 `B' : 9 bits
19 `C' : 9 bits
20 `Bx' : 18 bits (`B' and `C' together)
21 `sBx' : signed Bx
22
23 A signed argument is represented in excess K; that is, the number
24 value is the unsigned value minus K. K is exactly the maximum value
25 for that argument (so that -max is represented by 0, and +max is
26 represented by 2*max), which is half the maximum for the corresponding
27 unsigned argument.
28===========================================================================*/
29
30
31enum OpMode {iABC, iABx, iAsBx}; /* basic instruction format */
32
33
34/*
35** size and position of opcode arguments.
36*/
37#define SIZE_C 9
38#define SIZE_B 9
39#define SIZE_Bx (SIZE_C + SIZE_B)
40#define SIZE_A 8
41
42#define SIZE_OP 6
43
44#define POS_OP 0
45#define POS_A (POS_OP + SIZE_OP)
46#define POS_C (POS_A + SIZE_A)
47#define POS_B (POS_C + SIZE_C)
48#define POS_Bx POS_C
49
50
51/*
52** limits for opcode arguments.
53** we use (signed) int to manipulate most arguments,
54** so they must fit in LUAI_BITSINT-1 bits (-1 for sign)
55*/
56#if SIZE_Bx < LUAI_BITSINT-1
57#define MAXARG_Bx ((1<<SIZE_Bx)-1)
58#define MAXARG_sBx (MAXARG_Bx>>1) /* `sBx' is signed */
59#else
60#define MAXARG_Bx MAX_INT
61#define MAXARG_sBx MAX_INT
62#endif
63
64
65#define MAXARG_A ((1<<SIZE_A)-1)
66#define MAXARG_B ((1<<SIZE_B)-1)
67#define MAXARG_C ((1<<SIZE_C)-1)
68
69
70/* creates a mask with `n' 1 bits at position `p' */
71#define MASK1(n,p) ((~((~(Instruction)0)<<n))<<p)
72
73/* creates a mask with `n' 0 bits at position `p' */
74#define MASK0(n,p) (~MASK1(n,p))
75
76/*
77** the following macros help to manipulate instructions
78*/
79
80#define GET_OPCODE(i) (cast(OpCode, ((i)>>POS_OP) & MASK1(SIZE_OP,0)))
81#define SET_OPCODE(i,o) ((i) = (((i)&MASK0(SIZE_OP,POS_OP)) | \
82 ((cast(Instruction, o)<<POS_OP)&MASK1(SIZE_OP,POS_OP))))
83
84#define GETARG_A(i) (cast(int, ((i)>>POS_A) & MASK1(SIZE_A,0)))
85#define SETARG_A(i,u) ((i) = (((i)&MASK0(SIZE_A,POS_A)) | \
86 ((cast(Instruction, u)<<POS_A)&MASK1(SIZE_A,POS_A))))
87
88#define GETARG_B(i) (cast(int, ((i)>>POS_B) & MASK1(SIZE_B,0)))
89#define SETARG_B(i,b) ((i) = (((i)&MASK0(SIZE_B,POS_B)) | \
90 ((cast(Instruction, b)<<POS_B)&MASK1(SIZE_B,POS_B))))
91
92#define GETARG_C(i) (cast(int, ((i)>>POS_C) & MASK1(SIZE_C,0)))
93#define SETARG_C(i,b) ((i) = (((i)&MASK0(SIZE_C,POS_C)) | \
94 ((cast(Instruction, b)<<POS_C)&MASK1(SIZE_C,POS_C))))
95
96#define GETARG_Bx(i) (cast(int, ((i)>>POS_Bx) & MASK1(SIZE_Bx,0)))
97#define SETARG_Bx(i,b) ((i) = (((i)&MASK0(SIZE_Bx,POS_Bx)) | \
98 ((cast(Instruction, b)<<POS_Bx)&MASK1(SIZE_Bx,POS_Bx))))
99
100#define GETARG_sBx(i) (GETARG_Bx(i)-MAXARG_sBx)
101#define SETARG_sBx(i,b) SETARG_Bx((i),cast(unsigned int, (b)+MAXARG_sBx))
102
103
104#define CREATE_ABC(o,a,b,c) ((cast(Instruction, o)<<POS_OP) \
105 | (cast(Instruction, a)<<POS_A) \
106 | (cast(Instruction, b)<<POS_B) \
107 | (cast(Instruction, c)<<POS_C))
108
109#define CREATE_ABx(o,a,bc) ((cast(Instruction, o)<<POS_OP) \
110 | (cast(Instruction, a)<<POS_A) \
111 | (cast(Instruction, bc)<<POS_Bx))
112
113
114/*
115** Macros to operate RK indices
116*/
117
118/* this bit 1 means constant (0 means register) */
119#define BITRK (1 << (SIZE_B - 1))
120
121/* test whether value is a constant */
122#define ISK(x) ((x) & BITRK)
123
124/* gets the index of the constant */
125#define INDEXK(r) ((int)(r) & ~BITRK)
126
127#define MAXINDEXRK (BITRK - 1)
128
129/* code a constant index as a RK value */
130#define RKASK(x) ((x) | BITRK)
131
132
133/*
134** invalid register that fits in 8 bits
135*/
136#define NO_REG MAXARG_A
137
138
139/*
140** R(x) - register
141** Kst(x) - constant (in constant table)
142** RK(x) == if ISK(x) then Kst(INDEXK(x)) else R(x)
143*/
144
145
146/*
147** grep "ORDER OP" if you change these enums
148*/
149
150typedef enum {
151/*----------------------------------------------------------------------
152name args description
153------------------------------------------------------------------------*/
154OP_MOVE,/* A B R(A) := R(B) */
155OP_LOADK,/* A Bx R(A) := Kst(Bx) */
156OP_LOADBOOL,/* A B C R(A) := (Bool)B; if (C) pc++ */
157OP_LOADNIL,/* A B R(A) := ... := R(B) := nil */
158OP_GETUPVAL,/* A B R(A) := UpValue[B] */
159
160OP_GETGLOBAL,/* A Bx R(A) := Gbl[Kst(Bx)] */
161OP_GETTABLE,/* A B C R(A) := R(B)[RK(C)] */
162
163OP_SETGLOBAL,/* A Bx Gbl[Kst(Bx)] := R(A) */
164OP_SETUPVAL,/* A B UpValue[B] := R(A) */
165OP_SETTABLE,/* A B C R(A)[RK(B)] := RK(C) */
166
167OP_NEWTABLE,/* A B C R(A) := {} (size = B,C) */
168
169OP_SELF,/* A B C R(A+1) := R(B); R(A) := R(B)[RK(C)] */
170
171OP_ADD,/* A B C R(A) := RK(B) + RK(C) */
172OP_SUB,/* A B C R(A) := RK(B) - RK(C) */
173OP_MUL,/* A B C R(A) := RK(B) * RK(C) */
174OP_DIV,/* A B C R(A) := RK(B) / RK(C) */
175OP_MOD,/* A B C R(A) := RK(B) % RK(C) */
176OP_POW,/* A B C R(A) := RK(B) ^ RK(C) */
177OP_UNM,/* A B R(A) := -R(B) */
178OP_NOT,/* A B R(A) := not R(B) */
179OP_LEN,/* A B R(A) := length of R(B) */
180
181OP_CONCAT,/* A B C R(A) := R(B).. ... ..R(C) */
182
183OP_JMP,/* sBx pc+=sBx */
184
185OP_EQ,/* A B C if ((RK(B) == RK(C)) ~= A) then pc++ */
186OP_LT,/* A B C if ((RK(B) < RK(C)) ~= A) then pc++ */
187OP_LE,/* A B C if ((RK(B) <= RK(C)) ~= A) then pc++ */
188
189OP_TEST,/* A C if not (R(A) <=> C) then pc++ */
190OP_TESTSET,/* A B C if (R(B) <=> C) then R(A) := R(B) else pc++ */
191
192OP_CALL,/* A B C R(A), ... ,R(A+C-2) := R(A)(R(A+1), ... ,R(A+B-1)) */
193OP_TAILCALL,/* A B C return R(A)(R(A+1), ... ,R(A+B-1)) */
194OP_RETURN,/* A B return R(A), ... ,R(A+B-2) (see note) */
195
196OP_FORLOOP,/* A sBx R(A)+=R(A+2);
197 if R(A) <?= R(A+1) then { pc+=sBx; R(A+3)=R(A) }*/
198OP_FORPREP,/* A sBx R(A)-=R(A+2); pc+=sBx */
199
200OP_TFORLOOP,/* A C R(A+3), ... ,R(A+2+C) := R(A)(R(A+1), R(A+2));
201 if R(A+3) ~= nil then R(A+2)=R(A+3) else pc++ */
202OP_SETLIST,/* A B C R(A)[(C-1)*FPF+i] := R(A+i), 1 <= i <= B */
203
204OP_CLOSE,/* A close all variables in the stack up to (>=) R(A)*/
205OP_CLOSURE,/* A Bx R(A) := closure(KPROTO[Bx], R(A), ... ,R(A+n)) */
206
207OP_VARARG/* A B R(A), R(A+1), ..., R(A+B-1) = vararg */
208} OpCode;
209
210
211#define NUM_OPCODES (cast(int, OP_VARARG) + 1)
212
213
214
215/*===========================================================================
216 Notes:
217 (*) In OP_CALL, if (B == 0) then B = top. C is the number of returns - 1,
218 and can be 0: OP_CALL then sets `top' to last_result+1, so
219 next open instruction (OP_CALL, OP_RETURN, OP_SETLIST) may use `top'.
220
221 (*) In OP_VARARG, if (B == 0) then use actual number of varargs and
222 set top (like in OP_CALL with C == 0).
223
224 (*) In OP_RETURN, if (B == 0) then return up to `top'
225
226 (*) In OP_SETLIST, if (B == 0) then B = `top';
227 if (C == 0) then next `instruction' is real C
228
229 (*) For comparisons, A specifies what condition the test should accept
230 (true or false).
231
232 (*) All `skips' (pc++) assume that next instruction is a jump
233===========================================================================*/
234
235
236/*
237** masks for instruction properties. The format is:
238** bits 0-1: op mode
239** bits 2-3: C arg mode
240** bits 4-5: B arg mode
241** bit 6: instruction set register A
242** bit 7: operator is a test
243*/
244
245enum OpArgMask {
246 OpArgN, /* argument is not used */
247 OpArgU, /* argument is used */
248 OpArgR, /* argument is a register or a jump offset */
249 OpArgK /* argument is a constant or register/constant */
250};
251
252LUAI_DATA const lu_byte luaP_opmodes[NUM_OPCODES];
253
254#define getOpMode(m) (cast(enum OpMode, luaP_opmodes[m] & 3))
255#define getBMode(m) (cast(enum OpArgMask, (luaP_opmodes[m] >> 4) & 3))
256#define getCMode(m) (cast(enum OpArgMask, (luaP_opmodes[m] >> 2) & 3))
257#define testAMode(m) (luaP_opmodes[m] & (1 << 6))
258#define testTMode(m) (luaP_opmodes[m] & (1 << 7))
259
260
261LUAI_DATA const char *const luaP_opnames[NUM_OPCODES+1]; /* opcode names */
262
263
264/* number of list items to accumulate before a SETLIST instruction */
265#define LFIELDS_PER_FLUSH 50
266
267
268#endif
diff --git a/examples/redis-unstable/deps/lua/src/loslib.c b/examples/redis-unstable/deps/lua/src/loslib.c
deleted file mode 100644
index 403f416..0000000
--- a/examples/redis-unstable/deps/lua/src/loslib.c
+++ /dev/null
@@ -1,250 +0,0 @@
1/*
2** $Id: loslib.c,v 1.19.1.3 2008/01/18 16:38:18 roberto Exp $
3** Standard Operating System library
4** See Copyright Notice in lua.h
5*/
6
7
8#include <errno.h>
9#include <locale.h>
10#include <stdlib.h>
11#include <string.h>
12#include <time.h>
13
14#define loslib_c
15#define LUA_LIB
16
17#include "lua.h"
18
19#include "lauxlib.h"
20#include "lualib.h"
21
22
23static int os_pushresult (lua_State *L, int i, const char *filename) {
24 int en = errno; /* calls to Lua API may change this value */
25 if (i) {
26 lua_pushboolean(L, 1);
27 return 1;
28 }
29 else {
30 lua_pushnil(L);
31 lua_pushfstring(L, "%s: %s", filename, strerror(en));
32 lua_pushinteger(L, en);
33 return 3;
34 }
35}
36
37
38static int os_execute (lua_State *L) {
39 lua_pushinteger(L, system(luaL_optstring(L, 1, NULL)));
40 return 1;
41}
42
43
44static int os_remove (lua_State *L) {
45 const char *filename = luaL_checkstring(L, 1);
46 return os_pushresult(L, remove(filename) == 0, filename);
47}
48
49
50static int os_rename (lua_State *L) {
51 const char *fromname = luaL_checkstring(L, 1);
52 const char *toname = luaL_checkstring(L, 2);
53 return os_pushresult(L, rename(fromname, toname) == 0, fromname);
54}
55
56
57static int os_tmpname (lua_State *L) {
58 char buff[LUA_TMPNAMBUFSIZE];
59 int err;
60 lua_tmpnam(buff, err);
61 if (err)
62 return luaL_error(L, "unable to generate a unique filename");
63 lua_pushstring(L, buff);
64 return 1;
65}
66
67
68static int os_getenv (lua_State *L) {
69 lua_pushstring(L, getenv(luaL_checkstring(L, 1))); /* if NULL push nil */
70 return 1;
71}
72
73
74static int os_clock (lua_State *L) {
75 lua_pushnumber(L, ((lua_Number)clock())/(lua_Number)CLOCKS_PER_SEC);
76 return 1;
77}
78
79
80/*
81** {======================================================
82** Time/Date operations
83** { year=%Y, month=%m, day=%d, hour=%H, min=%M, sec=%S,
84** wday=%w+1, yday=%j, isdst=? }
85** =======================================================
86*/
87
88static void setfield (lua_State *L, const char *key, int value) {
89 lua_pushinteger(L, value);
90 lua_setfield(L, -2, key);
91}
92
93static void setboolfield (lua_State *L, const char *key, int value) {
94 if (value < 0) /* undefined? */
95 return; /* does not set field */
96 lua_pushboolean(L, value);
97 lua_setfield(L, -2, key);
98}
99
100static int getboolfield (lua_State *L, const char *key) {
101 int res;
102 lua_getfield(L, -1, key);
103 res = lua_isnil(L, -1) ? -1 : lua_toboolean(L, -1);
104 lua_pop(L, 1);
105 return res;
106}
107
108
109static int getfield (lua_State *L, const char *key, int d) {
110 int res;
111 lua_getfield(L, -1, key);
112 if (lua_isnumber(L, -1))
113 res = (int)lua_tointeger(L, -1);
114 else {
115 if (d < 0)
116 return luaL_error(L, "field " LUA_QS " missing in date table", key);
117 res = d;
118 }
119 lua_pop(L, 1);
120 return res;
121}
122
123
124static int os_date (lua_State *L) {
125 const char *s = luaL_optstring(L, 1, "%c");
126 time_t t = luaL_opt(L, (time_t)luaL_checknumber, 2, time(NULL));
127 struct tm *stm;
128 if (*s == '!') { /* UTC? */
129 stm = gmtime(&t);
130 s++; /* skip `!' */
131 }
132 else
133 stm = localtime(&t);
134 if (stm == NULL) /* invalid date? */
135 lua_pushnil(L);
136 else if (strcmp(s, "*t") == 0) {
137 lua_createtable(L, 0, 9); /* 9 = number of fields */
138 setfield(L, "sec", stm->tm_sec);
139 setfield(L, "min", stm->tm_min);
140 setfield(L, "hour", stm->tm_hour);
141 setfield(L, "day", stm->tm_mday);
142 setfield(L, "month", stm->tm_mon+1);
143 setfield(L, "year", stm->tm_year+1900);
144 setfield(L, "wday", stm->tm_wday+1);
145 setfield(L, "yday", stm->tm_yday+1);
146 setboolfield(L, "isdst", stm->tm_isdst);
147 }
148 else {
149 char cc[3];
150 luaL_Buffer b;
151 cc[0] = '%'; cc[2] = '\0';
152 luaL_buffinit(L, &b);
153 for (; *s; s++) {
154 if (*s != '%' || *(s + 1) == '\0') /* no conversion specifier? */
155 luaL_addchar(&b, *s);
156 else {
157 size_t reslen;
158 char buff[200]; /* should be big enough for any conversion result */
159 cc[1] = *(++s);
160 reslen = strftime(buff, sizeof(buff), cc, stm);
161 luaL_addlstring(&b, buff, reslen);
162 }
163 }
164 luaL_pushresult(&b);
165 }
166 return 1;
167}
168
169
170static int os_time (lua_State *L) {
171 time_t t;
172 if (lua_isnoneornil(L, 1)) /* called without args? */
173 t = time(NULL); /* get current time */
174 else {
175 struct tm ts;
176 luaL_checktype(L, 1, LUA_TTABLE);
177 lua_settop(L, 1); /* make sure table is at the top */
178 ts.tm_sec = getfield(L, "sec", 0);
179 ts.tm_min = getfield(L, "min", 0);
180 ts.tm_hour = getfield(L, "hour", 12);
181 ts.tm_mday = getfield(L, "day", -1);
182 ts.tm_mon = getfield(L, "month", -1) - 1;
183 ts.tm_year = getfield(L, "year", -1) - 1900;
184 ts.tm_isdst = getboolfield(L, "isdst");
185 t = mktime(&ts);
186 }
187 if (t == (time_t)(-1))
188 lua_pushnil(L);
189 else
190 lua_pushnumber(L, (lua_Number)t);
191 return 1;
192}
193
194
195static int os_difftime (lua_State *L) {
196 lua_pushnumber(L, difftime((time_t)(luaL_checknumber(L, 1)),
197 (time_t)(luaL_optnumber(L, 2, 0))));
198 return 1;
199}
200
201/* }====================================================== */
202
203
204static int os_setlocale (lua_State *L) {
205 static const int cat[] = {LC_ALL, LC_COLLATE, LC_CTYPE, LC_MONETARY,
206 LC_NUMERIC, LC_TIME};
207 static const char *const catnames[] = {"all", "collate", "ctype", "monetary",
208 "numeric", "time", NULL};
209 const char *l = luaL_optstring(L, 1, NULL);
210 int op = luaL_checkoption(L, 2, "all", catnames);
211 lua_pushstring(L, setlocale(cat[op], l));
212 return 1;
213}
214
215
216static int os_exit (lua_State *L) {
217 exit(luaL_optint(L, 1, EXIT_SUCCESS));
218}
219
220static const luaL_Reg syslib[] = {
221 {"clock", os_clock},
222 {"date", os_date},
223 {"difftime", os_difftime},
224 {"execute", os_execute},
225 {"exit", os_exit},
226 {"getenv", os_getenv},
227 {"remove", os_remove},
228 {"rename", os_rename},
229 {"setlocale", os_setlocale},
230 {"time", os_time},
231 {"tmpname", os_tmpname},
232 {NULL, NULL}
233};
234
235/* }====================================================== */
236
237#define UNUSED(V) ((void) V)
238
239/* Only a subset is loaded currently, for sandboxing concerns. */
240static const luaL_Reg sandbox_syslib[] = {
241 {"clock", os_clock},
242 {NULL, NULL}
243};
244
245LUALIB_API int luaopen_os (lua_State *L) {
246 UNUSED(syslib);
247 luaL_register(L, LUA_OSLIBNAME, sandbox_syslib);
248 return 1;
249}
250
diff --git a/examples/redis-unstable/deps/lua/src/lparser.c b/examples/redis-unstable/deps/lua/src/lparser.c
deleted file mode 100644
index ee7d90c..0000000
--- a/examples/redis-unstable/deps/lua/src/lparser.c
+++ /dev/null
@@ -1,1343 +0,0 @@
1/*
2** $Id: lparser.c,v 2.42.1.4 2011/10/21 19:31:42 roberto Exp $
3** Lua Parser
4** See Copyright Notice in lua.h
5*/
6
7
8#include <string.h>
9
10#define lparser_c
11#define LUA_CORE
12
13#include "lua.h"
14
15#include "lcode.h"
16#include "ldebug.h"
17#include "ldo.h"
18#include "lfunc.h"
19#include "llex.h"
20#include "lmem.h"
21#include "lobject.h"
22#include "lopcodes.h"
23#include "lparser.h"
24#include "lstate.h"
25#include "lstring.h"
26#include "ltable.h"
27
28
29
30#define hasmultret(k) ((k) == VCALL || (k) == VVARARG)
31
32#define getlocvar(fs, i) ((fs)->f->locvars[(fs)->actvar[i]])
33
34#define luaY_checklimit(fs,v,l,m) if ((v)>(l)) errorlimit(fs,l,m)
35
36
37/*
38** nodes for block list (list of active blocks)
39*/
40typedef struct BlockCnt {
41 struct BlockCnt *previous; /* chain */
42 int breaklist; /* list of jumps out of this loop */
43 lu_byte nactvar; /* # active locals outside the breakable structure */
44 lu_byte upval; /* true if some variable in the block is an upvalue */
45 lu_byte isbreakable; /* true if `block' is a loop */
46} BlockCnt;
47
48
49
50/*
51** prototypes for recursive non-terminal functions
52*/
53static void chunk (LexState *ls);
54static void expr (LexState *ls, expdesc *v);
55
56
57static void anchor_token (LexState *ls) {
58 if (ls->t.token == TK_NAME || ls->t.token == TK_STRING) {
59 TString *ts = ls->t.seminfo.ts;
60 luaX_newstring(ls, getstr(ts), ts->tsv.len);
61 }
62}
63
64
65static void error_expected (LexState *ls, int token) {
66 luaX_syntaxerror(ls,
67 luaO_pushfstring(ls->L, LUA_QS " expected", luaX_token2str(ls, token)));
68}
69
70
71static void errorlimit (FuncState *fs, int limit, const char *what) {
72 const char *msg = (fs->f->linedefined == 0) ?
73 luaO_pushfstring(fs->L, "main function has more than %d %s", limit, what) :
74 luaO_pushfstring(fs->L, "function at line %d has more than %d %s",
75 fs->f->linedefined, limit, what);
76 luaX_lexerror(fs->ls, msg, 0);
77}
78
79
80static int testnext (LexState *ls, int c) {
81 if (ls->t.token == c) {
82 luaX_next(ls);
83 return 1;
84 }
85 else return 0;
86}
87
88
89static void check (LexState *ls, int c) {
90 if (ls->t.token != c)
91 error_expected(ls, c);
92}
93
94static void checknext (LexState *ls, int c) {
95 check(ls, c);
96 luaX_next(ls);
97}
98
99
100#define check_condition(ls,c,msg) { if (!(c)) luaX_syntaxerror(ls, msg); }
101
102
103
104static void check_match (LexState *ls, int what, int who, int where) {
105 if (!testnext(ls, what)) {
106 if (where == ls->linenumber)
107 error_expected(ls, what);
108 else {
109 luaX_syntaxerror(ls, luaO_pushfstring(ls->L,
110 LUA_QS " expected (to close " LUA_QS " at line %d)",
111 luaX_token2str(ls, what), luaX_token2str(ls, who), where));
112 }
113 }
114}
115
116
117static TString *str_checkname (LexState *ls) {
118 TString *ts;
119 check(ls, TK_NAME);
120 ts = ls->t.seminfo.ts;
121 luaX_next(ls);
122 return ts;
123}
124
125
126static void init_exp (expdesc *e, expkind k, int i) {
127 e->f = e->t = NO_JUMP;
128 e->k = k;
129 e->u.s.info = i;
130}
131
132
133static void codestring (LexState *ls, expdesc *e, TString *s) {
134 init_exp(e, VK, luaK_stringK(ls->fs, s));
135}
136
137
138static void checkname(LexState *ls, expdesc *e) {
139 codestring(ls, e, str_checkname(ls));
140}
141
142
143static int registerlocalvar (LexState *ls, TString *varname) {
144 FuncState *fs = ls->fs;
145 Proto *f = fs->f;
146 int oldsize = f->sizelocvars;
147 luaM_growvector(ls->L, f->locvars, fs->nlocvars, f->sizelocvars,
148 LocVar, SHRT_MAX, "too many local variables");
149 while (oldsize < f->sizelocvars) f->locvars[oldsize++].varname = NULL;
150 f->locvars[fs->nlocvars].varname = varname;
151 luaC_objbarrier(ls->L, f, varname);
152 return fs->nlocvars++;
153}
154
155
156#define new_localvarliteral(ls,v,n) \
157 new_localvar(ls, luaX_newstring(ls, "" v, (sizeof(v)/sizeof(char))-1), n)
158
159
160static void new_localvar (LexState *ls, TString *name, int n) {
161 FuncState *fs = ls->fs;
162 luaY_checklimit(fs, fs->nactvar+n+1, LUAI_MAXVARS, "local variables");
163 fs->actvar[fs->nactvar+n] = cast(unsigned short, registerlocalvar(ls, name));
164}
165
166
167static void adjustlocalvars (LexState *ls, int nvars) {
168 FuncState *fs = ls->fs;
169 fs->nactvar = cast_byte(fs->nactvar + nvars);
170 for (; nvars; nvars--) {
171 getlocvar(fs, fs->nactvar - nvars).startpc = fs->pc;
172 }
173}
174
175
176static void removevars (LexState *ls, int tolevel) {
177 FuncState *fs = ls->fs;
178 while (fs->nactvar > tolevel)
179 getlocvar(fs, --fs->nactvar).endpc = fs->pc;
180}
181
182
183static int indexupvalue (FuncState *fs, TString *name, expdesc *v) {
184 int i;
185 Proto *f = fs->f;
186 int oldsize = f->sizeupvalues;
187 for (i=0; i<f->nups; i++) {
188 if (fs->upvalues[i].k == v->k && fs->upvalues[i].info == v->u.s.info) {
189 lua_assert(f->upvalues[i] == name);
190 return i;
191 }
192 }
193 /* new one */
194 luaY_checklimit(fs, f->nups + 1, LUAI_MAXUPVALUES, "upvalues");
195 luaM_growvector(fs->L, f->upvalues, f->nups, f->sizeupvalues,
196 TString *, MAX_INT, "");
197 while (oldsize < f->sizeupvalues) f->upvalues[oldsize++] = NULL;
198 f->upvalues[f->nups] = name;
199 luaC_objbarrier(fs->L, f, name);
200 lua_assert(v->k == VLOCAL || v->k == VUPVAL);
201 fs->upvalues[f->nups].k = cast_byte(v->k);
202 fs->upvalues[f->nups].info = cast_byte(v->u.s.info);
203 return f->nups++;
204}
205
206
207static int searchvar (FuncState *fs, TString *n) {
208 int i;
209 for (i=fs->nactvar-1; i >= 0; i--) {
210 if (n == getlocvar(fs, i).varname)
211 return i;
212 }
213 return -1; /* not found */
214}
215
216
217static void markupval (FuncState *fs, int level) {
218 BlockCnt *bl = fs->bl;
219 while (bl && bl->nactvar > level) bl = bl->previous;
220 if (bl) bl->upval = 1;
221}
222
223
224static int singlevaraux (FuncState *fs, TString *n, expdesc *var, int base) {
225 if (fs == NULL) { /* no more levels? */
226 init_exp(var, VGLOBAL, NO_REG); /* default is global variable */
227 return VGLOBAL;
228 }
229 else {
230 int v = searchvar(fs, n); /* look up at current level */
231 if (v >= 0) {
232 init_exp(var, VLOCAL, v);
233 if (!base)
234 markupval(fs, v); /* local will be used as an upval */
235 return VLOCAL;
236 }
237 else { /* not found at current level; try upper one */
238 if (singlevaraux(fs->prev, n, var, 0) == VGLOBAL)
239 return VGLOBAL;
240 var->u.s.info = indexupvalue(fs, n, var); /* else was LOCAL or UPVAL */
241 var->k = VUPVAL; /* upvalue in this level */
242 return VUPVAL;
243 }
244 }
245}
246
247
248static void singlevar (LexState *ls, expdesc *var) {
249 TString *varname = str_checkname(ls);
250 FuncState *fs = ls->fs;
251 if (singlevaraux(fs, varname, var, 1) == VGLOBAL)
252 var->u.s.info = luaK_stringK(fs, varname); /* info points to global name */
253}
254
255
256static void adjust_assign (LexState *ls, int nvars, int nexps, expdesc *e) {
257 FuncState *fs = ls->fs;
258 int extra = nvars - nexps;
259 if (hasmultret(e->k)) {
260 extra++; /* includes call itself */
261 if (extra < 0) extra = 0;
262 luaK_setreturns(fs, e, extra); /* last exp. provides the difference */
263 if (extra > 1) luaK_reserveregs(fs, extra-1);
264 }
265 else {
266 if (e->k != VVOID) luaK_exp2nextreg(fs, e); /* close last expression */
267 if (extra > 0) {
268 int reg = fs->freereg;
269 luaK_reserveregs(fs, extra);
270 luaK_nil(fs, reg, extra);
271 }
272 }
273}
274
275
276static void enterlevel (LexState *ls) {
277 if (++ls->L->nCcalls > LUAI_MAXCCALLS)
278 luaX_lexerror(ls, "chunk has too many syntax levels", 0);
279}
280
281
282#define leavelevel(ls) ((ls)->L->nCcalls--)
283
284
285static void enterblock (FuncState *fs, BlockCnt *bl, lu_byte isbreakable) {
286 bl->breaklist = NO_JUMP;
287 bl->isbreakable = isbreakable;
288 bl->nactvar = fs->nactvar;
289 bl->upval = 0;
290 bl->previous = fs->bl;
291 fs->bl = bl;
292 lua_assert(fs->freereg == fs->nactvar);
293}
294
295
296static void leaveblock (FuncState *fs) {
297 BlockCnt *bl = fs->bl;
298 fs->bl = bl->previous;
299 removevars(fs->ls, bl->nactvar);
300 if (bl->upval)
301 luaK_codeABC(fs, OP_CLOSE, bl->nactvar, 0, 0);
302 /* a block either controls scope or breaks (never both) */
303 lua_assert(!bl->isbreakable || !bl->upval);
304 lua_assert(bl->nactvar == fs->nactvar);
305 fs->freereg = fs->nactvar; /* free registers */
306 luaK_patchtohere(fs, bl->breaklist);
307}
308
309
310static void pushclosure (LexState *ls, FuncState *func, expdesc *v) {
311 FuncState *fs = ls->fs;
312 Proto *f = fs->f;
313 int oldsize = f->sizep;
314 int i;
315 luaM_growvector(ls->L, f->p, fs->np, f->sizep, Proto *,
316 MAXARG_Bx, "constant table overflow");
317 while (oldsize < f->sizep) f->p[oldsize++] = NULL;
318 f->p[fs->np++] = func->f;
319 luaC_objbarrier(ls->L, f, func->f);
320 init_exp(v, VRELOCABLE, luaK_codeABx(fs, OP_CLOSURE, 0, fs->np-1));
321 for (i=0; i<func->f->nups; i++) {
322 OpCode o = (func->upvalues[i].k == VLOCAL) ? OP_MOVE : OP_GETUPVAL;
323 luaK_codeABC(fs, o, 0, func->upvalues[i].info, 0);
324 }
325}
326
327
328static void open_func (LexState *ls, FuncState *fs) {
329 lua_State *L = ls->L;
330 Proto *f = luaF_newproto(L);
331 fs->f = f;
332 fs->prev = ls->fs; /* linked list of funcstates */
333 fs->ls = ls;
334 fs->L = L;
335 ls->fs = fs;
336 fs->pc = 0;
337 fs->lasttarget = -1;
338 fs->jpc = NO_JUMP;
339 fs->freereg = 0;
340 fs->nk = 0;
341 fs->np = 0;
342 fs->nlocvars = 0;
343 fs->nactvar = 0;
344 fs->bl = NULL;
345 f->source = ls->source;
346 f->maxstacksize = 2; /* registers 0/1 are always valid */
347 fs->h = luaH_new(L, 0, 0);
348 /* anchor table of constants and prototype (to avoid being collected) */
349 sethvalue2s(L, L->top, fs->h);
350 incr_top(L);
351 setptvalue2s(L, L->top, f);
352 incr_top(L);
353}
354
355
356static void close_func (LexState *ls) {
357 lua_State *L = ls->L;
358 FuncState *fs = ls->fs;
359 Proto *f = fs->f;
360 removevars(ls, 0);
361 luaK_ret(fs, 0, 0); /* final return */
362 luaM_reallocvector(L, f->code, f->sizecode, fs->pc, Instruction);
363 f->sizecode = fs->pc;
364 luaM_reallocvector(L, f->lineinfo, f->sizelineinfo, fs->pc, int);
365 f->sizelineinfo = fs->pc;
366 luaM_reallocvector(L, f->k, f->sizek, fs->nk, TValue);
367 f->sizek = fs->nk;
368 luaM_reallocvector(L, f->p, f->sizep, fs->np, Proto *);
369 f->sizep = fs->np;
370 luaM_reallocvector(L, f->locvars, f->sizelocvars, fs->nlocvars, LocVar);
371 f->sizelocvars = fs->nlocvars;
372 luaM_reallocvector(L, f->upvalues, f->sizeupvalues, f->nups, TString *);
373 f->sizeupvalues = f->nups;
374 lua_assert(luaG_checkcode(f));
375 lua_assert(fs->bl == NULL);
376 ls->fs = fs->prev;
377 /* last token read was anchored in defunct function; must reanchor it */
378 if (fs) anchor_token(ls);
379 L->top -= 2; /* remove table and prototype from the stack */
380}
381
382
383Proto *luaY_parser (lua_State *L, ZIO *z, Mbuffer *buff, const char *name) {
384 struct LexState lexstate;
385 struct FuncState funcstate;
386 lexstate.buff = buff;
387 TString *tname = luaS_new(L, name);
388 setsvalue2s(L, L->top, tname);
389 incr_top(L);
390 luaX_setinput(L, &lexstate, z, tname);
391 open_func(&lexstate, &funcstate);
392 funcstate.f->is_vararg = VARARG_ISVARARG; /* main func. is always vararg */
393 luaX_next(&lexstate); /* read first token */
394 chunk(&lexstate);
395 check(&lexstate, TK_EOS);
396 close_func(&lexstate);
397 --L->top;
398 lua_assert(funcstate.prev == NULL);
399 lua_assert(funcstate.f->nups == 0);
400 lua_assert(lexstate.fs == NULL);
401 return funcstate.f;
402}
403
404
405
406/*============================================================*/
407/* GRAMMAR RULES */
408/*============================================================*/
409
410
411static void field (LexState *ls, expdesc *v) {
412 /* field -> ['.' | ':'] NAME */
413 FuncState *fs = ls->fs;
414 expdesc key;
415 luaK_exp2anyreg(fs, v);
416 luaX_next(ls); /* skip the dot or colon */
417 checkname(ls, &key);
418 luaK_indexed(fs, v, &key);
419}
420
421
422static void yindex (LexState *ls, expdesc *v) {
423 /* index -> '[' expr ']' */
424 luaX_next(ls); /* skip the '[' */
425 expr(ls, v);
426 luaK_exp2val(ls->fs, v);
427 checknext(ls, ']');
428}
429
430
431/*
432** {======================================================================
433** Rules for Constructors
434** =======================================================================
435*/
436
437
438struct ConsControl {
439 expdesc v; /* last list item read */
440 expdesc *t; /* table descriptor */
441 int nh; /* total number of `record' elements */
442 int na; /* total number of array elements */
443 int tostore; /* number of array elements pending to be stored */
444};
445
446
447static void recfield (LexState *ls, struct ConsControl *cc) {
448 /* recfield -> (NAME | `['exp1`]') = exp1 */
449 FuncState *fs = ls->fs;
450 int reg = ls->fs->freereg;
451 expdesc key, val;
452 int rkkey;
453 if (ls->t.token == TK_NAME) {
454 luaY_checklimit(fs, cc->nh, MAX_INT, "items in a constructor");
455 checkname(ls, &key);
456 }
457 else /* ls->t.token == '[' */
458 yindex(ls, &key);
459 cc->nh++;
460 checknext(ls, '=');
461 rkkey = luaK_exp2RK(fs, &key);
462 expr(ls, &val);
463 luaK_codeABC(fs, OP_SETTABLE, cc->t->u.s.info, rkkey, luaK_exp2RK(fs, &val));
464 fs->freereg = reg; /* free registers */
465}
466
467
468static void closelistfield (FuncState *fs, struct ConsControl *cc) {
469 if (cc->v.k == VVOID) return; /* there is no list item */
470 luaK_exp2nextreg(fs, &cc->v);
471 cc->v.k = VVOID;
472 if (cc->tostore == LFIELDS_PER_FLUSH) {
473 luaK_setlist(fs, cc->t->u.s.info, cc->na, cc->tostore); /* flush */
474 cc->tostore = 0; /* no more items pending */
475 }
476}
477
478
479static void lastlistfield (FuncState *fs, struct ConsControl *cc) {
480 if (cc->tostore == 0) return;
481 if (hasmultret(cc->v.k)) {
482 luaK_setmultret(fs, &cc->v);
483 luaK_setlist(fs, cc->t->u.s.info, cc->na, LUA_MULTRET);
484 cc->na--; /* do not count last expression (unknown number of elements) */
485 }
486 else {
487 if (cc->v.k != VVOID)
488 luaK_exp2nextreg(fs, &cc->v);
489 luaK_setlist(fs, cc->t->u.s.info, cc->na, cc->tostore);
490 }
491}
492
493
494static void listfield (LexState *ls, struct ConsControl *cc) {
495 expr(ls, &cc->v);
496 luaY_checklimit(ls->fs, cc->na, MAX_INT, "items in a constructor");
497 cc->na++;
498 cc->tostore++;
499}
500
501
502static void constructor (LexState *ls, expdesc *t) {
503 /* constructor -> ?? */
504 FuncState *fs = ls->fs;
505 int line = ls->linenumber;
506 int pc = luaK_codeABC(fs, OP_NEWTABLE, 0, 0, 0);
507 struct ConsControl cc;
508 cc.na = cc.nh = cc.tostore = 0;
509 cc.t = t;
510 init_exp(t, VRELOCABLE, pc);
511 init_exp(&cc.v, VVOID, 0); /* no value (yet) */
512 luaK_exp2nextreg(ls->fs, t); /* fix it at stack top (for gc) */
513 checknext(ls, '{');
514 do {
515 lua_assert(cc.v.k == VVOID || cc.tostore > 0);
516 if (ls->t.token == '}') break;
517 closelistfield(fs, &cc);
518 switch(ls->t.token) {
519 case TK_NAME: { /* may be listfields or recfields */
520 luaX_lookahead(ls);
521 if (ls->lookahead.token != '=') /* expression? */
522 listfield(ls, &cc);
523 else
524 recfield(ls, &cc);
525 break;
526 }
527 case '[': { /* constructor_item -> recfield */
528 recfield(ls, &cc);
529 break;
530 }
531 default: { /* constructor_part -> listfield */
532 listfield(ls, &cc);
533 break;
534 }
535 }
536 } while (testnext(ls, ',') || testnext(ls, ';'));
537 check_match(ls, '}', '{', line);
538 lastlistfield(fs, &cc);
539 SETARG_B(fs->f->code[pc], luaO_int2fb(cc.na)); /* set initial array size */
540 SETARG_C(fs->f->code[pc], luaO_int2fb(cc.nh)); /* set initial table size */
541}
542
543/* }====================================================================== */
544
545
546
547static void parlist (LexState *ls) {
548 /* parlist -> [ param { `,' param } ] */
549 FuncState *fs = ls->fs;
550 Proto *f = fs->f;
551 int nparams = 0;
552 f->is_vararg = 0;
553 if (ls->t.token != ')') { /* is `parlist' not empty? */
554 do {
555 switch (ls->t.token) {
556 case TK_NAME: { /* param -> NAME */
557 new_localvar(ls, str_checkname(ls), nparams++);
558 break;
559 }
560 case TK_DOTS: { /* param -> `...' */
561 luaX_next(ls);
562#if defined(LUA_COMPAT_VARARG)
563 /* use `arg' as default name */
564 new_localvarliteral(ls, "arg", nparams++);
565 f->is_vararg = VARARG_HASARG | VARARG_NEEDSARG;
566#endif
567 f->is_vararg |= VARARG_ISVARARG;
568 break;
569 }
570 default: luaX_syntaxerror(ls, "<name> or " LUA_QL("...") " expected");
571 }
572 } while (!f->is_vararg && testnext(ls, ','));
573 }
574 adjustlocalvars(ls, nparams);
575 f->numparams = cast_byte(fs->nactvar - (f->is_vararg & VARARG_HASARG));
576 luaK_reserveregs(fs, fs->nactvar); /* reserve register for parameters */
577}
578
579
580static void body (LexState *ls, expdesc *e, int needself, int line) {
581 /* body -> `(' parlist `)' chunk END */
582 FuncState new_fs;
583 open_func(ls, &new_fs);
584 new_fs.f->linedefined = line;
585 checknext(ls, '(');
586 if (needself) {
587 new_localvarliteral(ls, "self", 0);
588 adjustlocalvars(ls, 1);
589 }
590 parlist(ls);
591 checknext(ls, ')');
592 chunk(ls);
593 new_fs.f->lastlinedefined = ls->linenumber;
594 check_match(ls, TK_END, TK_FUNCTION, line);
595 close_func(ls);
596 pushclosure(ls, &new_fs, e);
597}
598
599
600static int explist1 (LexState *ls, expdesc *v) {
601 /* explist1 -> expr { `,' expr } */
602 int n = 1; /* at least one expression */
603 expr(ls, v);
604 while (testnext(ls, ',')) {
605 luaK_exp2nextreg(ls->fs, v);
606 expr(ls, v);
607 n++;
608 }
609 return n;
610}
611
612
613static void funcargs (LexState *ls, expdesc *f) {
614 FuncState *fs = ls->fs;
615 expdesc args;
616 int base, nparams;
617 int line = ls->linenumber;
618 switch (ls->t.token) {
619 case '(': { /* funcargs -> `(' [ explist1 ] `)' */
620 if (line != ls->lastline)
621 luaX_syntaxerror(ls,"ambiguous syntax (function call x new statement)");
622 luaX_next(ls);
623 if (ls->t.token == ')') /* arg list is empty? */
624 args.k = VVOID;
625 else {
626 explist1(ls, &args);
627 luaK_setmultret(fs, &args);
628 }
629 check_match(ls, ')', '(', line);
630 break;
631 }
632 case '{': { /* funcargs -> constructor */
633 constructor(ls, &args);
634 break;
635 }
636 case TK_STRING: { /* funcargs -> STRING */
637 codestring(ls, &args, ls->t.seminfo.ts);
638 luaX_next(ls); /* must use `seminfo' before `next' */
639 break;
640 }
641 default: {
642 luaX_syntaxerror(ls, "function arguments expected");
643 return;
644 }
645 }
646 lua_assert(f->k == VNONRELOC);
647 base = f->u.s.info; /* base register for call */
648 if (hasmultret(args.k))
649 nparams = LUA_MULTRET; /* open call */
650 else {
651 if (args.k != VVOID)
652 luaK_exp2nextreg(fs, &args); /* close last argument */
653 nparams = fs->freereg - (base+1);
654 }
655 init_exp(f, VCALL, luaK_codeABC(fs, OP_CALL, base, nparams+1, 2));
656 luaK_fixline(fs, line);
657 fs->freereg = base+1; /* call remove function and arguments and leaves
658 (unless changed) one result */
659}
660
661
662
663
664/*
665** {======================================================================
666** Expression parsing
667** =======================================================================
668*/
669
670
671static void prefixexp (LexState *ls, expdesc *v) {
672 /* prefixexp -> NAME | '(' expr ')' */
673 switch (ls->t.token) {
674 case '(': {
675 int line = ls->linenumber;
676 luaX_next(ls);
677 expr(ls, v);
678 check_match(ls, ')', '(', line);
679 luaK_dischargevars(ls->fs, v);
680 return;
681 }
682 case TK_NAME: {
683 singlevar(ls, v);
684 return;
685 }
686 default: {
687 luaX_syntaxerror(ls, "unexpected symbol");
688 return;
689 }
690 }
691}
692
693
694static void primaryexp (LexState *ls, expdesc *v) {
695 /* primaryexp ->
696 prefixexp { `.' NAME | `[' exp `]' | `:' NAME funcargs | funcargs } */
697 FuncState *fs = ls->fs;
698 prefixexp(ls, v);
699 for (;;) {
700 switch (ls->t.token) {
701 case '.': { /* field */
702 field(ls, v);
703 break;
704 }
705 case '[': { /* `[' exp1 `]' */
706 expdesc key;
707 luaK_exp2anyreg(fs, v);
708 yindex(ls, &key);
709 luaK_indexed(fs, v, &key);
710 break;
711 }
712 case ':': { /* `:' NAME funcargs */
713 expdesc key;
714 luaX_next(ls);
715 checkname(ls, &key);
716 luaK_self(fs, v, &key);
717 funcargs(ls, v);
718 break;
719 }
720 case '(': case TK_STRING: case '{': { /* funcargs */
721 luaK_exp2nextreg(fs, v);
722 funcargs(ls, v);
723 break;
724 }
725 default: return;
726 }
727 }
728}
729
730
731static void simpleexp (LexState *ls, expdesc *v) {
732 /* simpleexp -> NUMBER | STRING | NIL | true | false | ... |
733 constructor | FUNCTION body | primaryexp */
734 switch (ls->t.token) {
735 case TK_NUMBER: {
736 init_exp(v, VKNUM, 0);
737 v->u.nval = ls->t.seminfo.r;
738 break;
739 }
740 case TK_STRING: {
741 codestring(ls, v, ls->t.seminfo.ts);
742 break;
743 }
744 case TK_NIL: {
745 init_exp(v, VNIL, 0);
746 break;
747 }
748 case TK_TRUE: {
749 init_exp(v, VTRUE, 0);
750 break;
751 }
752 case TK_FALSE: {
753 init_exp(v, VFALSE, 0);
754 break;
755 }
756 case TK_DOTS: { /* vararg */
757 FuncState *fs = ls->fs;
758 check_condition(ls, fs->f->is_vararg,
759 "cannot use " LUA_QL("...") " outside a vararg function");
760 fs->f->is_vararg &= ~VARARG_NEEDSARG; /* don't need 'arg' */
761 init_exp(v, VVARARG, luaK_codeABC(fs, OP_VARARG, 0, 1, 0));
762 break;
763 }
764 case '{': { /* constructor */
765 constructor(ls, v);
766 return;
767 }
768 case TK_FUNCTION: {
769 luaX_next(ls);
770 body(ls, v, 0, ls->linenumber);
771 return;
772 }
773 default: {
774 primaryexp(ls, v);
775 return;
776 }
777 }
778 luaX_next(ls);
779}
780
781
782static UnOpr getunopr (int op) {
783 switch (op) {
784 case TK_NOT: return OPR_NOT;
785 case '-': return OPR_MINUS;
786 case '#': return OPR_LEN;
787 default: return OPR_NOUNOPR;
788 }
789}
790
791
792static BinOpr getbinopr (int op) {
793 switch (op) {
794 case '+': return OPR_ADD;
795 case '-': return OPR_SUB;
796 case '*': return OPR_MUL;
797 case '/': return OPR_DIV;
798 case '%': return OPR_MOD;
799 case '^': return OPR_POW;
800 case TK_CONCAT: return OPR_CONCAT;
801 case TK_NE: return OPR_NE;
802 case TK_EQ: return OPR_EQ;
803 case '<': return OPR_LT;
804 case TK_LE: return OPR_LE;
805 case '>': return OPR_GT;
806 case TK_GE: return OPR_GE;
807 case TK_AND: return OPR_AND;
808 case TK_OR: return OPR_OR;
809 default: return OPR_NOBINOPR;
810 }
811}
812
813
814static const struct {
815 lu_byte left; /* left priority for each binary operator */
816 lu_byte right; /* right priority */
817} priority[] = { /* ORDER OPR */
818 {6, 6}, {6, 6}, {7, 7}, {7, 7}, {7, 7}, /* `+' `-' `/' `%' */
819 {10, 9}, {5, 4}, /* power and concat (right associative) */
820 {3, 3}, {3, 3}, /* equality and inequality */
821 {3, 3}, {3, 3}, {3, 3}, {3, 3}, /* order */
822 {2, 2}, {1, 1} /* logical (and/or) */
823};
824
825#define UNARY_PRIORITY 8 /* priority for unary operators */
826
827
828/*
829** subexpr -> (simpleexp | unop subexpr) { binop subexpr }
830** where `binop' is any binary operator with a priority higher than `limit'
831*/
832static BinOpr subexpr (LexState *ls, expdesc *v, unsigned int limit) {
833 BinOpr op;
834 UnOpr uop;
835 enterlevel(ls);
836 uop = getunopr(ls->t.token);
837 if (uop != OPR_NOUNOPR) {
838 luaX_next(ls);
839 subexpr(ls, v, UNARY_PRIORITY);
840 luaK_prefix(ls->fs, uop, v);
841 }
842 else simpleexp(ls, v);
843 /* expand while operators have priorities higher than `limit' */
844 op = getbinopr(ls->t.token);
845 while (op != OPR_NOBINOPR && priority[op].left > limit) {
846 expdesc v2;
847 BinOpr nextop;
848 luaX_next(ls);
849 luaK_infix(ls->fs, op, v);
850 /* read sub-expression with higher priority */
851 nextop = subexpr(ls, &v2, priority[op].right);
852 luaK_posfix(ls->fs, op, v, &v2);
853 op = nextop;
854 }
855 leavelevel(ls);
856 return op; /* return first untreated operator */
857}
858
859
860static void expr (LexState *ls, expdesc *v) {
861 subexpr(ls, v, 0);
862}
863
864/* }==================================================================== */
865
866
867
868/*
869** {======================================================================
870** Rules for Statements
871** =======================================================================
872*/
873
874
875static int block_follow (int token) {
876 switch (token) {
877 case TK_ELSE: case TK_ELSEIF: case TK_END:
878 case TK_UNTIL: case TK_EOS:
879 return 1;
880 default: return 0;
881 }
882}
883
884
885static void block (LexState *ls) {
886 /* block -> chunk */
887 FuncState *fs = ls->fs;
888 BlockCnt bl;
889 enterblock(fs, &bl, 0);
890 chunk(ls);
891 lua_assert(bl.breaklist == NO_JUMP);
892 leaveblock(fs);
893}
894
895
896/*
897** structure to chain all variables in the left-hand side of an
898** assignment
899*/
900struct LHS_assign {
901 struct LHS_assign *prev;
902 expdesc v; /* variable (global, local, upvalue, or indexed) */
903};
904
905
906/*
907** check whether, in an assignment to a local variable, the local variable
908** is needed in a previous assignment (to a table). If so, save original
909** local value in a safe place and use this safe copy in the previous
910** assignment.
911*/
912static void check_conflict (LexState *ls, struct LHS_assign *lh, expdesc *v) {
913 FuncState *fs = ls->fs;
914 int extra = fs->freereg; /* eventual position to save local variable */
915 int conflict = 0;
916 for (; lh; lh = lh->prev) {
917 if (lh->v.k == VINDEXED) {
918 if (lh->v.u.s.info == v->u.s.info) { /* conflict? */
919 conflict = 1;
920 lh->v.u.s.info = extra; /* previous assignment will use safe copy */
921 }
922 if (lh->v.u.s.aux == v->u.s.info) { /* conflict? */
923 conflict = 1;
924 lh->v.u.s.aux = extra; /* previous assignment will use safe copy */
925 }
926 }
927 }
928 if (conflict) {
929 luaK_codeABC(fs, OP_MOVE, fs->freereg, v->u.s.info, 0); /* make copy */
930 luaK_reserveregs(fs, 1);
931 }
932}
933
934
935static void assignment (LexState *ls, struct LHS_assign *lh, int nvars) {
936 expdesc e;
937 check_condition(ls, VLOCAL <= lh->v.k && lh->v.k <= VINDEXED,
938 "syntax error");
939 if (testnext(ls, ',')) { /* assignment -> `,' primaryexp assignment */
940 struct LHS_assign nv;
941 nv.prev = lh;
942 primaryexp(ls, &nv.v);
943 if (nv.v.k == VLOCAL)
944 check_conflict(ls, lh, &nv.v);
945 luaY_checklimit(ls->fs, nvars, LUAI_MAXCCALLS - ls->L->nCcalls,
946 "variables in assignment");
947 assignment(ls, &nv, nvars+1);
948 }
949 else { /* assignment -> `=' explist1 */
950 int nexps;
951 checknext(ls, '=');
952 nexps = explist1(ls, &e);
953 if (nexps != nvars) {
954 adjust_assign(ls, nvars, nexps, &e);
955 if (nexps > nvars)
956 ls->fs->freereg -= nexps - nvars; /* remove extra values */
957 }
958 else {
959 luaK_setoneret(ls->fs, &e); /* close last expression */
960 luaK_storevar(ls->fs, &lh->v, &e);
961 return; /* avoid default */
962 }
963 }
964 init_exp(&e, VNONRELOC, ls->fs->freereg-1); /* default assignment */
965 luaK_storevar(ls->fs, &lh->v, &e);
966}
967
968
969static int cond (LexState *ls) {
970 /* cond -> exp */
971 expdesc v;
972 expr(ls, &v); /* read condition */
973 if (v.k == VNIL) v.k = VFALSE; /* `falses' are all equal here */
974 luaK_goiftrue(ls->fs, &v);
975 return v.f;
976}
977
978
979static void breakstat (LexState *ls) {
980 FuncState *fs = ls->fs;
981 BlockCnt *bl = fs->bl;
982 int upval = 0;
983 while (bl && !bl->isbreakable) {
984 upval |= bl->upval;
985 bl = bl->previous;
986 }
987 if (!bl)
988 luaX_syntaxerror(ls, "no loop to break");
989 if (upval)
990 luaK_codeABC(fs, OP_CLOSE, bl->nactvar, 0, 0);
991 luaK_concat(fs, &bl->breaklist, luaK_jump(fs));
992}
993
994
995static void whilestat (LexState *ls, int line) {
996 /* whilestat -> WHILE cond DO block END */
997 FuncState *fs = ls->fs;
998 int whileinit;
999 int condexit;
1000 BlockCnt bl;
1001 luaX_next(ls); /* skip WHILE */
1002 whileinit = luaK_getlabel(fs);
1003 condexit = cond(ls);
1004 enterblock(fs, &bl, 1);
1005 checknext(ls, TK_DO);
1006 block(ls);
1007 luaK_patchlist(fs, luaK_jump(fs), whileinit);
1008 check_match(ls, TK_END, TK_WHILE, line);
1009 leaveblock(fs);
1010 luaK_patchtohere(fs, condexit); /* false conditions finish the loop */
1011}
1012
1013
1014static void repeatstat (LexState *ls, int line) {
1015 /* repeatstat -> REPEAT block UNTIL cond */
1016 int condexit;
1017 FuncState *fs = ls->fs;
1018 int repeat_init = luaK_getlabel(fs);
1019 BlockCnt bl1, bl2;
1020 enterblock(fs, &bl1, 1); /* loop block */
1021 enterblock(fs, &bl2, 0); /* scope block */
1022 luaX_next(ls); /* skip REPEAT */
1023 chunk(ls);
1024 check_match(ls, TK_UNTIL, TK_REPEAT, line);
1025 condexit = cond(ls); /* read condition (inside scope block) */
1026 if (!bl2.upval) { /* no upvalues? */
1027 leaveblock(fs); /* finish scope */
1028 luaK_patchlist(ls->fs, condexit, repeat_init); /* close the loop */
1029 }
1030 else { /* complete semantics when there are upvalues */
1031 breakstat(ls); /* if condition then break */
1032 luaK_patchtohere(ls->fs, condexit); /* else... */
1033 leaveblock(fs); /* finish scope... */
1034 luaK_patchlist(ls->fs, luaK_jump(fs), repeat_init); /* and repeat */
1035 }
1036 leaveblock(fs); /* finish loop */
1037}
1038
1039
1040static int exp1 (LexState *ls) {
1041 expdesc e;
1042 int k;
1043 expr(ls, &e);
1044 k = e.k;
1045 luaK_exp2nextreg(ls->fs, &e);
1046 return k;
1047}
1048
1049
1050static void forbody (LexState *ls, int base, int line, int nvars, int isnum) {
1051 /* forbody -> DO block */
1052 BlockCnt bl;
1053 FuncState *fs = ls->fs;
1054 int prep, endfor;
1055 adjustlocalvars(ls, 3); /* control variables */
1056 checknext(ls, TK_DO);
1057 prep = isnum ? luaK_codeAsBx(fs, OP_FORPREP, base, NO_JUMP) : luaK_jump(fs);
1058 enterblock(fs, &bl, 0); /* scope for declared variables */
1059 adjustlocalvars(ls, nvars);
1060 luaK_reserveregs(fs, nvars);
1061 block(ls);
1062 leaveblock(fs); /* end of scope for declared variables */
1063 luaK_patchtohere(fs, prep);
1064 endfor = (isnum) ? luaK_codeAsBx(fs, OP_FORLOOP, base, NO_JUMP) :
1065 luaK_codeABC(fs, OP_TFORLOOP, base, 0, nvars);
1066 luaK_fixline(fs, line); /* pretend that `OP_FOR' starts the loop */
1067 luaK_patchlist(fs, (isnum ? endfor : luaK_jump(fs)), prep + 1);
1068}
1069
1070
1071static void fornum (LexState *ls, TString *varname, int line) {
1072 /* fornum -> NAME = exp1,exp1[,exp1] forbody */
1073 FuncState *fs = ls->fs;
1074 int base = fs->freereg;
1075 new_localvarliteral(ls, "(for index)", 0);
1076 new_localvarliteral(ls, "(for limit)", 1);
1077 new_localvarliteral(ls, "(for step)", 2);
1078 new_localvar(ls, varname, 3);
1079 checknext(ls, '=');
1080 exp1(ls); /* initial value */
1081 checknext(ls, ',');
1082 exp1(ls); /* limit */
1083 if (testnext(ls, ','))
1084 exp1(ls); /* optional step */
1085 else { /* default step = 1 */
1086 luaK_codeABx(fs, OP_LOADK, fs->freereg, luaK_numberK(fs, 1));
1087 luaK_reserveregs(fs, 1);
1088 }
1089 forbody(ls, base, line, 1, 1);
1090}
1091
1092
1093static void forlist (LexState *ls, TString *indexname) {
1094 /* forlist -> NAME {,NAME} IN explist1 forbody */
1095 FuncState *fs = ls->fs;
1096 expdesc e;
1097 int nvars = 0;
1098 int line;
1099 int base = fs->freereg;
1100 /* create control variables */
1101 new_localvarliteral(ls, "(for generator)", nvars++);
1102 new_localvarliteral(ls, "(for state)", nvars++);
1103 new_localvarliteral(ls, "(for control)", nvars++);
1104 /* create declared variables */
1105 new_localvar(ls, indexname, nvars++);
1106 while (testnext(ls, ','))
1107 new_localvar(ls, str_checkname(ls), nvars++);
1108 checknext(ls, TK_IN);
1109 line = ls->linenumber;
1110 adjust_assign(ls, 3, explist1(ls, &e), &e);
1111 luaK_checkstack(fs, 3); /* extra space to call generator */
1112 forbody(ls, base, line, nvars - 3, 0);
1113}
1114
1115
1116static void forstat (LexState *ls, int line) {
1117 /* forstat -> FOR (fornum | forlist) END */
1118 FuncState *fs = ls->fs;
1119 TString *varname;
1120 BlockCnt bl;
1121 enterblock(fs, &bl, 1); /* scope for loop and control variables */
1122 luaX_next(ls); /* skip `for' */
1123 varname = str_checkname(ls); /* first variable name */
1124 switch (ls->t.token) {
1125 case '=': fornum(ls, varname, line); break;
1126 case ',': case TK_IN: forlist(ls, varname); break;
1127 default: luaX_syntaxerror(ls, LUA_QL("=") " or " LUA_QL("in") " expected");
1128 }
1129 check_match(ls, TK_END, TK_FOR, line);
1130 leaveblock(fs); /* loop scope (`break' jumps to this point) */
1131}
1132
1133
1134static int test_then_block (LexState *ls) {
1135 /* test_then_block -> [IF | ELSEIF] cond THEN block */
1136 int condexit;
1137 luaX_next(ls); /* skip IF or ELSEIF */
1138 condexit = cond(ls);
1139 checknext(ls, TK_THEN);
1140 block(ls); /* `then' part */
1141 return condexit;
1142}
1143
1144
1145static void ifstat (LexState *ls, int line) {
1146 /* ifstat -> IF cond THEN block {ELSEIF cond THEN block} [ELSE block] END */
1147 FuncState *fs = ls->fs;
1148 int flist;
1149 int escapelist = NO_JUMP;
1150 flist = test_then_block(ls); /* IF cond THEN block */
1151 while (ls->t.token == TK_ELSEIF) {
1152 luaK_concat(fs, &escapelist, luaK_jump(fs));
1153 luaK_patchtohere(fs, flist);
1154 flist = test_then_block(ls); /* ELSEIF cond THEN block */
1155 }
1156 if (ls->t.token == TK_ELSE) {
1157 luaK_concat(fs, &escapelist, luaK_jump(fs));
1158 luaK_patchtohere(fs, flist);
1159 luaX_next(ls); /* skip ELSE (after patch, for correct line info) */
1160 block(ls); /* `else' part */
1161 }
1162 else
1163 luaK_concat(fs, &escapelist, flist);
1164 luaK_patchtohere(fs, escapelist);
1165 check_match(ls, TK_END, TK_IF, line);
1166}
1167
1168
1169static void localfunc (LexState *ls) {
1170 expdesc v, b;
1171 FuncState *fs = ls->fs;
1172 new_localvar(ls, str_checkname(ls), 0);
1173 init_exp(&v, VLOCAL, fs->freereg);
1174 luaK_reserveregs(fs, 1);
1175 adjustlocalvars(ls, 1);
1176 body(ls, &b, 0, ls->linenumber);
1177 luaK_storevar(fs, &v, &b);
1178 /* debug information will only see the variable after this point! */
1179 getlocvar(fs, fs->nactvar - 1).startpc = fs->pc;
1180}
1181
1182
1183static void localstat (LexState *ls) {
1184 /* stat -> LOCAL NAME {`,' NAME} [`=' explist1] */
1185 int nvars = 0;
1186 int nexps;
1187 expdesc e;
1188 do {
1189 new_localvar(ls, str_checkname(ls), nvars++);
1190 } while (testnext(ls, ','));
1191 if (testnext(ls, '='))
1192 nexps = explist1(ls, &e);
1193 else {
1194 e.k = VVOID;
1195 nexps = 0;
1196 }
1197 adjust_assign(ls, nvars, nexps, &e);
1198 adjustlocalvars(ls, nvars);
1199}
1200
1201
1202static int funcname (LexState *ls, expdesc *v) {
1203 /* funcname -> NAME {field} [`:' NAME] */
1204 int needself = 0;
1205 singlevar(ls, v);
1206 while (ls->t.token == '.')
1207 field(ls, v);
1208 if (ls->t.token == ':') {
1209 needself = 1;
1210 field(ls, v);
1211 }
1212 return needself;
1213}
1214
1215
1216static void funcstat (LexState *ls, int line) {
1217 /* funcstat -> FUNCTION funcname body */
1218 int needself;
1219 expdesc v, b;
1220 luaX_next(ls); /* skip FUNCTION */
1221 needself = funcname(ls, &v);
1222 body(ls, &b, needself, line);
1223 luaK_storevar(ls->fs, &v, &b);
1224 luaK_fixline(ls->fs, line); /* definition `happens' in the first line */
1225}
1226
1227
1228static void exprstat (LexState *ls) {
1229 /* stat -> func | assignment */
1230 FuncState *fs = ls->fs;
1231 struct LHS_assign v;
1232 primaryexp(ls, &v.v);
1233 if (v.v.k == VCALL) /* stat -> func */
1234 SETARG_C(getcode(fs, &v.v), 1); /* call statement uses no results */
1235 else { /* stat -> assignment */
1236 v.prev = NULL;
1237 assignment(ls, &v, 1);
1238 }
1239}
1240
1241
1242static void retstat (LexState *ls) {
1243 /* stat -> RETURN explist */
1244 FuncState *fs = ls->fs;
1245 expdesc e;
1246 int first, nret; /* registers with returned values */
1247 luaX_next(ls); /* skip RETURN */
1248 if (block_follow(ls->t.token) || ls->t.token == ';')
1249 first = nret = 0; /* return no values */
1250 else {
1251 nret = explist1(ls, &e); /* optional return values */
1252 if (hasmultret(e.k)) {
1253 luaK_setmultret(fs, &e);
1254 if (e.k == VCALL && nret == 1) { /* tail call? */
1255 SET_OPCODE(getcode(fs,&e), OP_TAILCALL);
1256 lua_assert(GETARG_A(getcode(fs,&e)) == fs->nactvar);
1257 }
1258 first = fs->nactvar;
1259 nret = LUA_MULTRET; /* return all values */
1260 }
1261 else {
1262 if (nret == 1) /* only one single value? */
1263 first = luaK_exp2anyreg(fs, &e);
1264 else {
1265 luaK_exp2nextreg(fs, &e); /* values must go to the `stack' */
1266 first = fs->nactvar; /* return all `active' values */
1267 lua_assert(nret == fs->freereg - first);
1268 }
1269 }
1270 }
1271 luaK_ret(fs, first, nret);
1272}
1273
1274
1275static int statement (LexState *ls) {
1276 int line = ls->linenumber; /* may be needed for error messages */
1277 switch (ls->t.token) {
1278 case TK_IF: { /* stat -> ifstat */
1279 ifstat(ls, line);
1280 return 0;
1281 }
1282 case TK_WHILE: { /* stat -> whilestat */
1283 whilestat(ls, line);
1284 return 0;
1285 }
1286 case TK_DO: { /* stat -> DO block END */
1287 luaX_next(ls); /* skip DO */
1288 block(ls);
1289 check_match(ls, TK_END, TK_DO, line);
1290 return 0;
1291 }
1292 case TK_FOR: { /* stat -> forstat */
1293 forstat(ls, line);
1294 return 0;
1295 }
1296 case TK_REPEAT: { /* stat -> repeatstat */
1297 repeatstat(ls, line);
1298 return 0;
1299 }
1300 case TK_FUNCTION: {
1301 funcstat(ls, line); /* stat -> funcstat */
1302 return 0;
1303 }
1304 case TK_LOCAL: { /* stat -> localstat */
1305 luaX_next(ls); /* skip LOCAL */
1306 if (testnext(ls, TK_FUNCTION)) /* local function? */
1307 localfunc(ls);
1308 else
1309 localstat(ls);
1310 return 0;
1311 }
1312 case TK_RETURN: { /* stat -> retstat */
1313 retstat(ls);
1314 return 1; /* must be last statement */
1315 }
1316 case TK_BREAK: { /* stat -> breakstat */
1317 luaX_next(ls); /* skip BREAK */
1318 breakstat(ls);
1319 return 1; /* must be last statement */
1320 }
1321 default: {
1322 exprstat(ls);
1323 return 0; /* to avoid warnings */
1324 }
1325 }
1326}
1327
1328
1329static void chunk (LexState *ls) {
1330 /* chunk -> { stat [`;'] } */
1331 int islast = 0;
1332 enterlevel(ls);
1333 while (!islast && !block_follow(ls->t.token)) {
1334 islast = statement(ls);
1335 testnext(ls, ';');
1336 lua_assert(ls->fs->f->maxstacksize >= ls->fs->freereg &&
1337 ls->fs->freereg >= ls->fs->nactvar);
1338 ls->fs->freereg = ls->fs->nactvar; /* free registers */
1339 }
1340 leavelevel(ls);
1341}
1342
1343/* }====================================================================== */
diff --git a/examples/redis-unstable/deps/lua/src/lparser.h b/examples/redis-unstable/deps/lua/src/lparser.h
deleted file mode 100644
index 18836af..0000000
--- a/examples/redis-unstable/deps/lua/src/lparser.h
+++ /dev/null
@@ -1,82 +0,0 @@
1/*
2** $Id: lparser.h,v 1.57.1.1 2007/12/27 13:02:25 roberto Exp $
3** Lua Parser
4** See Copyright Notice in lua.h
5*/
6
7#ifndef lparser_h
8#define lparser_h
9
10#include "llimits.h"
11#include "lobject.h"
12#include "lzio.h"
13
14
15/*
16** Expression descriptor
17*/
18
19typedef enum {
20 VVOID, /* no value */
21 VNIL,
22 VTRUE,
23 VFALSE,
24 VK, /* info = index of constant in `k' */
25 VKNUM, /* nval = numerical value */
26 VLOCAL, /* info = local register */
27 VUPVAL, /* info = index of upvalue in `upvalues' */
28 VGLOBAL, /* info = index of table; aux = index of global name in `k' */
29 VINDEXED, /* info = table register; aux = index register (or `k') */
30 VJMP, /* info = instruction pc */
31 VRELOCABLE, /* info = instruction pc */
32 VNONRELOC, /* info = result register */
33 VCALL, /* info = instruction pc */
34 VVARARG /* info = instruction pc */
35} expkind;
36
37typedef struct expdesc {
38 expkind k;
39 union {
40 struct { int info, aux; } s;
41 lua_Number nval;
42 } u;
43 int t; /* patch list of `exit when true' */
44 int f; /* patch list of `exit when false' */
45} expdesc;
46
47
48typedef struct upvaldesc {
49 lu_byte k;
50 lu_byte info;
51} upvaldesc;
52
53
54struct BlockCnt; /* defined in lparser.c */
55
56
57/* state needed to generate code for a given function */
58typedef struct FuncState {
59 Proto *f; /* current function header */
60 Table *h; /* table to find (and reuse) elements in `k' */
61 struct FuncState *prev; /* enclosing function */
62 struct LexState *ls; /* lexical state */
63 struct lua_State *L; /* copy of the Lua state */
64 struct BlockCnt *bl; /* chain of current blocks */
65 int pc; /* next position to code (equivalent to `ncode') */
66 int lasttarget; /* `pc' of last `jump target' */
67 int jpc; /* list of pending jumps to `pc' */
68 int freereg; /* first free register */
69 int nk; /* number of elements in `k' */
70 int np; /* number of elements in `p' */
71 short nlocvars; /* number of elements in `locvars' */
72 lu_byte nactvar; /* number of active local variables */
73 upvaldesc upvalues[LUAI_MAXUPVALUES]; /* upvalues */
74 unsigned short actvar[LUAI_MAXVARS]; /* declared-variable stack */
75} FuncState;
76
77
78LUAI_FUNC Proto *luaY_parser (lua_State *L, ZIO *z, Mbuffer *buff,
79 const char *name);
80
81
82#endif
diff --git a/examples/redis-unstable/deps/lua/src/lstate.c b/examples/redis-unstable/deps/lua/src/lstate.c
deleted file mode 100644
index 4313b83..0000000
--- a/examples/redis-unstable/deps/lua/src/lstate.c
+++ /dev/null
@@ -1,214 +0,0 @@
1/*
2** $Id: lstate.c,v 2.36.1.2 2008/01/03 15:20:39 roberto Exp $
3** Global State
4** See Copyright Notice in lua.h
5*/
6
7
8#include <stddef.h>
9
10#define lstate_c
11#define LUA_CORE
12
13#include "lua.h"
14
15#include "ldebug.h"
16#include "ldo.h"
17#include "lfunc.h"
18#include "lgc.h"
19#include "llex.h"
20#include "lmem.h"
21#include "lstate.h"
22#include "lstring.h"
23#include "ltable.h"
24#include "ltm.h"
25
26
27#define state_size(x) (sizeof(x) + LUAI_EXTRASPACE)
28#define fromstate(l) (cast(lu_byte *, (l)) - LUAI_EXTRASPACE)
29#define tostate(l) (cast(lua_State *, cast(lu_byte *, l) + LUAI_EXTRASPACE))
30
31
32/*
33** Main thread combines a thread state and the global state
34*/
35typedef struct LG {
36 lua_State l;
37 global_State g;
38} LG;
39
40
41
42static void stack_init (lua_State *L1, lua_State *L) {
43 /* initialize CallInfo array */
44 L1->base_ci = luaM_newvector(L, BASIC_CI_SIZE, CallInfo);
45 L1->ci = L1->base_ci;
46 L1->size_ci = BASIC_CI_SIZE;
47 L1->end_ci = L1->base_ci + L1->size_ci - 1;
48 /* initialize stack array */
49 L1->stack = luaM_newvector(L, BASIC_STACK_SIZE + EXTRA_STACK, TValue);
50 L1->stacksize = BASIC_STACK_SIZE + EXTRA_STACK;
51 L1->top = L1->stack;
52 L1->stack_last = L1->stack+(L1->stacksize - EXTRA_STACK)-1;
53 /* initialize first ci */
54 L1->ci->func = L1->top;
55 setnilvalue(L1->top++); /* `function' entry for this `ci' */
56 L1->base = L1->ci->base = L1->top;
57 L1->ci->top = L1->top + LUA_MINSTACK;
58}
59
60
61static void freestack (lua_State *L, lua_State *L1) {
62 luaM_freearray(L, L1->base_ci, L1->size_ci, CallInfo);
63 luaM_freearray(L, L1->stack, L1->stacksize, TValue);
64}
65
66
67/*
68** open parts that may cause memory-allocation errors
69*/
70static void f_luaopen (lua_State *L, void *ud) {
71 global_State *g = G(L);
72 UNUSED(ud);
73 stack_init(L, L); /* init stack */
74 sethvalue(L, gt(L), luaH_new(L, 0, 2)); /* table of globals */
75 sethvalue(L, registry(L), luaH_new(L, 0, 2)); /* registry */
76 luaS_resize(L, MINSTRTABSIZE); /* initial size of string table */
77 luaT_init(L);
78 luaX_init(L);
79 luaS_fix(luaS_newliteral(L, MEMERRMSG));
80 g->GCthreshold = 4*g->totalbytes;
81}
82
83
84static void preinit_state (lua_State *L, global_State *g) {
85 G(L) = g;
86 L->stack = NULL;
87 L->stacksize = 0;
88 L->errorJmp = NULL;
89 L->hook = NULL;
90 L->hookmask = 0;
91 L->basehookcount = 0;
92 L->allowhook = 1;
93 resethookcount(L);
94 L->openupval = NULL;
95 L->size_ci = 0;
96 L->nCcalls = L->baseCcalls = 0;
97 L->status = 0;
98 L->base_ci = L->ci = NULL;
99 L->savedpc = NULL;
100 L->errfunc = 0;
101 setnilvalue(gt(L));
102}
103
104
105static void close_state (lua_State *L) {
106 global_State *g = G(L);
107 luaF_close(L, L->stack); /* close all upvalues for this thread */
108 luaC_freeall(L); /* collect all objects */
109 lua_assert(g->rootgc == obj2gco(L));
110 lua_assert(g->strt.nuse == 0);
111 luaM_freearray(L, G(L)->strt.hash, G(L)->strt.size, TString *);
112 luaZ_freebuffer(L, &g->buff);
113 freestack(L, L);
114 lua_assert(g->totalbytes == sizeof(LG));
115 (*g->frealloc)(g->ud, fromstate(L), state_size(LG), 0);
116}
117
118
119lua_State *luaE_newthread (lua_State *L) {
120 lua_State *L1 = tostate(luaM_malloc(L, state_size(lua_State)));
121 luaC_link(L, obj2gco(L1), LUA_TTHREAD);
122 preinit_state(L1, G(L));
123 stack_init(L1, L); /* init stack */
124 setobj2n(L, gt(L1), gt(L)); /* share table of globals */
125 L1->hookmask = L->hookmask;
126 L1->basehookcount = L->basehookcount;
127 L1->hook = L->hook;
128 resethookcount(L1);
129 lua_assert(iswhite(obj2gco(L1)));
130 return L1;
131}
132
133
134void luaE_freethread (lua_State *L, lua_State *L1) {
135 luaF_close(L1, L1->stack); /* close all upvalues for this thread */
136 lua_assert(L1->openupval == NULL);
137 luai_userstatefree(L1);
138 freestack(L, L1);
139 luaM_freemem(L, fromstate(L1), state_size(lua_State));
140}
141
142
143LUA_API lua_State *lua_newstate (lua_Alloc f, void *ud) {
144 int i;
145 lua_State *L;
146 global_State *g;
147 void *l = (*f)(ud, NULL, 0, state_size(LG));
148 if (l == NULL) return NULL;
149 L = tostate(l);
150 g = &((LG *)L)->g;
151 L->next = NULL;
152 L->tt = LUA_TTHREAD;
153 g->currentwhite = bit2mask(WHITE0BIT, FIXEDBIT);
154 L->marked = luaC_white(g);
155 set2bits(L->marked, FIXEDBIT, SFIXEDBIT);
156 preinit_state(L, g);
157 g->frealloc = f;
158 g->ud = ud;
159 g->mainthread = L;
160 g->uvhead.u.l.prev = &g->uvhead;
161 g->uvhead.u.l.next = &g->uvhead;
162 g->GCthreshold = 0; /* mark it as unfinished state */
163 g->strt.size = 0;
164 g->strt.nuse = 0;
165 g->strt.hash = NULL;
166 setnilvalue(registry(L));
167 luaZ_initbuffer(L, &g->buff);
168 g->panic = NULL;
169 g->gcstate = GCSpause;
170 g->rootgc = obj2gco(L);
171 g->sweepstrgc = 0;
172 g->sweepgc = &g->rootgc;
173 g->gray = NULL;
174 g->grayagain = NULL;
175 g->weak = NULL;
176 g->tmudata = NULL;
177 g->totalbytes = sizeof(LG);
178 g->gcpause = LUAI_GCPAUSE;
179 g->gcstepmul = LUAI_GCMUL;
180 g->gcdept = 0;
181 for (i=0; i<NUM_TAGS; i++) g->mt[i] = NULL;
182 if (luaD_rawrunprotected(L, f_luaopen, NULL) != 0) {
183 /* memory allocation error: free partial state */
184 close_state(L);
185 L = NULL;
186 }
187 else
188 luai_userstateopen(L);
189 return L;
190}
191
192
193static void callallgcTM (lua_State *L, void *ud) {
194 UNUSED(ud);
195 luaC_callGCTM(L); /* call GC metamethods for all udata */
196}
197
198
199LUA_API void lua_close (lua_State *L) {
200 L = G(L)->mainthread; /* only the main thread can be closed */
201 lua_lock(L);
202 luaF_close(L, L->stack); /* close all upvalues for this thread */
203 luaC_separateudata(L, 1); /* separate udata that have GC metamethods */
204 L->errfunc = 0; /* no error function during GC metamethods */
205 do { /* repeat until no more errors */
206 L->ci = L->base_ci;
207 L->base = L->top = L->ci->base;
208 L->nCcalls = L->baseCcalls = 0;
209 } while (luaD_rawrunprotected(L, callallgcTM, NULL) != 0);
210 lua_assert(G(L)->tmudata == NULL);
211 luai_userstateclose(L);
212 close_state(L);
213}
214
diff --git a/examples/redis-unstable/deps/lua/src/lstate.h b/examples/redis-unstable/deps/lua/src/lstate.h
deleted file mode 100644
index 3bc575b..0000000
--- a/examples/redis-unstable/deps/lua/src/lstate.h
+++ /dev/null
@@ -1,169 +0,0 @@
1/*
2** $Id: lstate.h,v 2.24.1.2 2008/01/03 15:20:39 roberto Exp $
3** Global State
4** See Copyright Notice in lua.h
5*/
6
7#ifndef lstate_h
8#define lstate_h
9
10#include "lua.h"
11
12#include "lobject.h"
13#include "ltm.h"
14#include "lzio.h"
15
16
17
18struct lua_longjmp; /* defined in ldo.c */
19
20
21/* table of globals */
22#define gt(L) (&L->l_gt)
23
24/* registry */
25#define registry(L) (&G(L)->l_registry)
26
27
28/* extra stack space to handle TM calls and some other extras */
29#define EXTRA_STACK 5
30
31
32#define BASIC_CI_SIZE 8
33
34#define BASIC_STACK_SIZE (2*LUA_MINSTACK)
35
36
37
38typedef struct stringtable {
39 GCObject **hash;
40 lu_int32 nuse; /* number of elements */
41 int size;
42} stringtable;
43
44
45/*
46** informations about a call
47*/
48typedef struct CallInfo {
49 StkId base; /* base for this function */
50 StkId func; /* function index in the stack */
51 StkId top; /* top for this function */
52 const Instruction *savedpc;
53 int nresults; /* expected number of results from this function */
54 int tailcalls; /* number of tail calls lost under this entry */
55} CallInfo;
56
57
58
59#define curr_func(L) (clvalue(L->ci->func))
60#define ci_func(ci) (clvalue((ci)->func))
61#define f_isLua(ci) (!ci_func(ci)->c.isC)
62#define isLua(ci) (ttisfunction((ci)->func) && f_isLua(ci))
63
64
65/*
66** `global state', shared by all threads of this state
67*/
68typedef struct global_State {
69 stringtable strt; /* hash table for strings */
70 lua_Alloc frealloc; /* function to reallocate memory */
71 void *ud; /* auxiliary data to `frealloc' */
72 lu_byte currentwhite;
73 lu_byte gcstate; /* state of garbage collector */
74 int sweepstrgc; /* position of sweep in `strt' */
75 GCObject *rootgc; /* list of all collectable objects */
76 GCObject **sweepgc; /* position of sweep in `rootgc' */
77 GCObject *gray; /* list of gray objects */
78 GCObject *grayagain; /* list of objects to be traversed atomically */
79 GCObject *weak; /* list of weak tables (to be cleared) */
80 GCObject *tmudata; /* last element of list of userdata to be GC */
81 Mbuffer buff; /* temporary buffer for string concatentation */
82 lu_mem GCthreshold;
83 lu_mem totalbytes; /* number of bytes currently allocated */
84 lu_mem estimate; /* an estimate of number of bytes actually in use */
85 lu_mem gcdept; /* how much GC is `behind schedule' */
86 int gcpause; /* size of pause between successive GCs */
87 int gcstepmul; /* GC `granularity' */
88 lua_CFunction panic; /* to be called in unprotected errors */
89 TValue l_registry;
90 struct lua_State *mainthread;
91 UpVal uvhead; /* head of double-linked list of all open upvalues */
92 struct Table *mt[NUM_TAGS]; /* metatables for basic types */
93 TString *tmname[TM_N]; /* array with tag-method names */
94} global_State;
95
96
97/*
98** `per thread' state
99*/
100struct lua_State {
101 CommonHeader;
102 lu_byte status;
103 StkId top; /* first free slot in the stack */
104 StkId base; /* base of current function */
105 global_State *l_G;
106 CallInfo *ci; /* call info for current function */
107 const Instruction *savedpc; /* `savedpc' of current function */
108 StkId stack_last; /* last free slot in the stack */
109 StkId stack; /* stack base */
110 CallInfo *end_ci; /* points after end of ci array*/
111 CallInfo *base_ci; /* array of CallInfo's */
112 int stacksize;
113 int size_ci; /* size of array `base_ci' */
114 unsigned short nCcalls; /* number of nested C calls */
115 unsigned short baseCcalls; /* nested C calls when resuming coroutine */
116 lu_byte hookmask;
117 lu_byte allowhook;
118 int basehookcount;
119 int hookcount;
120 lua_Hook hook;
121 TValue l_gt; /* table of globals */
122 TValue env; /* temporary place for environments */
123 GCObject *openupval; /* list of open upvalues in this stack */
124 GCObject *gclist;
125 struct lua_longjmp *errorJmp; /* current error recover point */
126 ptrdiff_t errfunc; /* current error handling function (stack index) */
127};
128
129
130#define G(L) (L->l_G)
131
132
133/*
134** Union of all collectable objects
135*/
136union GCObject {
137 GCheader gch;
138 union TString ts;
139 union Udata u;
140 union Closure cl;
141 struct Table h;
142 struct Proto p;
143 struct UpVal uv;
144 struct lua_State th; /* thread */
145};
146
147
148/* macros to convert a GCObject into a specific value */
149#define rawgco2ts(o) check_exp((o)->gch.tt == LUA_TSTRING, &((o)->ts))
150#define gco2ts(o) (&rawgco2ts(o)->tsv)
151#define rawgco2u(o) check_exp((o)->gch.tt == LUA_TUSERDATA, &((o)->u))
152#define gco2u(o) (&rawgco2u(o)->uv)
153#define gco2cl(o) check_exp((o)->gch.tt == LUA_TFUNCTION, &((o)->cl))
154#define gco2h(o) check_exp((o)->gch.tt == LUA_TTABLE, &((o)->h))
155#define gco2p(o) check_exp((o)->gch.tt == LUA_TPROTO, &((o)->p))
156#define gco2uv(o) check_exp((o)->gch.tt == LUA_TUPVAL, &((o)->uv))
157#define ngcotouv(o) \
158 check_exp((o) == NULL || (o)->gch.tt == LUA_TUPVAL, &((o)->uv))
159#define gco2th(o) check_exp((o)->gch.tt == LUA_TTHREAD, &((o)->th))
160
161/* macro to convert any Lua object into a GCObject */
162#define obj2gco(v) (cast(GCObject *, (v)))
163
164
165LUAI_FUNC lua_State *luaE_newthread (lua_State *L);
166LUAI_FUNC void luaE_freethread (lua_State *L, lua_State *L1);
167
168#endif
169
diff --git a/examples/redis-unstable/deps/lua/src/lstring.c b/examples/redis-unstable/deps/lua/src/lstring.c
deleted file mode 100644
index 6a825f7..0000000
--- a/examples/redis-unstable/deps/lua/src/lstring.c
+++ /dev/null
@@ -1,111 +0,0 @@
1/*
2** $Id: lstring.c,v 2.8.1.1 2007/12/27 13:02:25 roberto Exp $
3** String table (keeps all strings handled by Lua)
4** See Copyright Notice in lua.h
5*/
6
7
8#include <string.h>
9
10#define lstring_c
11#define LUA_CORE
12
13#include "lua.h"
14
15#include "lmem.h"
16#include "lobject.h"
17#include "lstate.h"
18#include "lstring.h"
19
20
21
22void luaS_resize (lua_State *L, int newsize) {
23 GCObject **newhash;
24 stringtable *tb;
25 int i;
26 if (G(L)->gcstate == GCSsweepstring)
27 return; /* cannot resize during GC traverse */
28 newhash = luaM_newvector(L, newsize, GCObject *);
29 tb = &G(L)->strt;
30 for (i=0; i<newsize; i++) newhash[i] = NULL;
31 /* rehash */
32 for (i=0; i<tb->size; i++) {
33 GCObject *p = tb->hash[i];
34 while (p) { /* for each node in the list */
35 GCObject *next = p->gch.next; /* save next */
36 unsigned int h = gco2ts(p)->hash;
37 int h1 = lmod(h, newsize); /* new position */
38 lua_assert(cast_int(h%newsize) == lmod(h, newsize));
39 p->gch.next = newhash[h1]; /* chain it */
40 newhash[h1] = p;
41 p = next;
42 }
43 }
44 luaM_freearray(L, tb->hash, tb->size, TString *);
45 tb->size = newsize;
46 tb->hash = newhash;
47}
48
49
50static TString *newlstr (lua_State *L, const char *str, size_t l,
51 unsigned int h) {
52 TString *ts;
53 stringtable *tb;
54 if (l+1 > (MAX_SIZET - sizeof(TString))/sizeof(char))
55 luaM_toobig(L);
56 ts = cast(TString *, luaM_malloc(L, (l+1)*sizeof(char)+sizeof(TString)));
57 ts->tsv.len = l;
58 ts->tsv.hash = h;
59 ts->tsv.marked = luaC_white(G(L));
60 ts->tsv.tt = LUA_TSTRING;
61 ts->tsv.reserved = 0;
62 memcpy(ts+1, str, l*sizeof(char));
63 ((char *)(ts+1))[l] = '\0'; /* ending 0 */
64 tb = &G(L)->strt;
65 h = lmod(h, tb->size);
66 ts->tsv.next = tb->hash[h]; /* chain new entry */
67 tb->hash[h] = obj2gco(ts);
68 tb->nuse++;
69 if (tb->nuse > cast(lu_int32, tb->size) && tb->size <= MAX_INT/2)
70 luaS_resize(L, tb->size*2); /* too crowded */
71 return ts;
72}
73
74
75TString *luaS_newlstr (lua_State *L, const char *str, size_t l) {
76 GCObject *o;
77 unsigned int h = cast(unsigned int, l); /* seed */
78 size_t step = 1;
79 size_t l1;
80 for (l1=l; l1>=step; l1-=step) /* compute hash */
81 h = h ^ ((h<<5)+(h>>2)+cast(unsigned char, str[l1-1]));
82 for (o = G(L)->strt.hash[lmod(h, G(L)->strt.size)];
83 o != NULL;
84 o = o->gch.next) {
85 TString *ts = rawgco2ts(o);
86 if (ts->tsv.len == l && (memcmp(str, getstr(ts), l) == 0)) {
87 /* string may be dead */
88 if (isdead(G(L), o)) changewhite(o);
89 return ts;
90 }
91 }
92 return newlstr(L, str, l, h); /* not found */
93}
94
95
96Udata *luaS_newudata (lua_State *L, size_t s, Table *e) {
97 Udata *u;
98 if (s > MAX_SIZET - sizeof(Udata))
99 luaM_toobig(L);
100 u = cast(Udata *, luaM_malloc(L, s + sizeof(Udata)));
101 u->uv.marked = luaC_white(G(L)); /* is not finalized */
102 u->uv.tt = LUA_TUSERDATA;
103 u->uv.len = s;
104 u->uv.metatable = NULL;
105 u->uv.env = e;
106 /* chain it on udata list (after main thread) */
107 u->uv.next = G(L)->mainthread->next;
108 G(L)->mainthread->next = obj2gco(u);
109 return u;
110}
111
diff --git a/examples/redis-unstable/deps/lua/src/lstring.h b/examples/redis-unstable/deps/lua/src/lstring.h
deleted file mode 100644
index 73a2ff8..0000000
--- a/examples/redis-unstable/deps/lua/src/lstring.h
+++ /dev/null
@@ -1,31 +0,0 @@
1/*
2** $Id: lstring.h,v 1.43.1.1 2007/12/27 13:02:25 roberto Exp $
3** String table (keep all strings handled by Lua)
4** See Copyright Notice in lua.h
5*/
6
7#ifndef lstring_h
8#define lstring_h
9
10
11#include "lgc.h"
12#include "lobject.h"
13#include "lstate.h"
14
15
16#define sizestring(s) (sizeof(union TString)+((s)->len+1)*sizeof(char))
17
18#define sizeudata(u) (sizeof(union Udata)+(u)->len)
19
20#define luaS_new(L, s) (luaS_newlstr(L, s, strlen(s)))
21#define luaS_newliteral(L, s) (luaS_newlstr(L, "" s, \
22 (sizeof(s)/sizeof(char))-1))
23
24#define luaS_fix(s) l_setbit((s)->tsv.marked, FIXEDBIT)
25
26LUAI_FUNC void luaS_resize (lua_State *L, int newsize);
27LUAI_FUNC Udata *luaS_newudata (lua_State *L, size_t s, Table *e);
28LUAI_FUNC TString *luaS_newlstr (lua_State *L, const char *str, size_t l);
29
30
31#endif
diff --git a/examples/redis-unstable/deps/lua/src/lstrlib.c b/examples/redis-unstable/deps/lua/src/lstrlib.c
deleted file mode 100644
index 7a03489..0000000
--- a/examples/redis-unstable/deps/lua/src/lstrlib.c
+++ /dev/null
@@ -1,871 +0,0 @@
1/*
2** $Id: lstrlib.c,v 1.132.1.5 2010/05/14 15:34:19 roberto Exp $
3** Standard library for string operations and pattern-matching
4** See Copyright Notice in lua.h
5*/
6
7
8#include <ctype.h>
9#include <stddef.h>
10#include <stdio.h>
11#include <stdlib.h>
12#include <string.h>
13
14#define lstrlib_c
15#define LUA_LIB
16
17#include "lua.h"
18
19#include "lauxlib.h"
20#include "lualib.h"
21
22
23/* macro to `unsign' a character */
24#define uchar(c) ((unsigned char)(c))
25
26
27
28static int str_len (lua_State *L) {
29 size_t l;
30 luaL_checklstring(L, 1, &l);
31 lua_pushinteger(L, l);
32 return 1;
33}
34
35
36static ptrdiff_t posrelat (ptrdiff_t pos, size_t len) {
37 /* relative string position: negative means back from end */
38 if (pos < 0) pos += (ptrdiff_t)len + 1;
39 return (pos >= 0) ? pos : 0;
40}
41
42
43static int str_sub (lua_State *L) {
44 size_t l;
45 const char *s = luaL_checklstring(L, 1, &l);
46 ptrdiff_t start = posrelat(luaL_checkinteger(L, 2), l);
47 ptrdiff_t end = posrelat(luaL_optinteger(L, 3, -1), l);
48 if (start < 1) start = 1;
49 if (end > (ptrdiff_t)l) end = (ptrdiff_t)l;
50 if (start <= end)
51 lua_pushlstring(L, s+start-1, end-start+1);
52 else lua_pushliteral(L, "");
53 return 1;
54}
55
56
57static int str_reverse (lua_State *L) {
58 size_t l;
59 luaL_Buffer b;
60 const char *s = luaL_checklstring(L, 1, &l);
61 luaL_buffinit(L, &b);
62 while (l--) luaL_addchar(&b, s[l]);
63 luaL_pushresult(&b);
64 return 1;
65}
66
67
68static int str_lower (lua_State *L) {
69 size_t l;
70 size_t i;
71 luaL_Buffer b;
72 const char *s = luaL_checklstring(L, 1, &l);
73 luaL_buffinit(L, &b);
74 for (i=0; i<l; i++)
75 luaL_addchar(&b, tolower(uchar(s[i])));
76 luaL_pushresult(&b);
77 return 1;
78}
79
80
81static int str_upper (lua_State *L) {
82 size_t l;
83 size_t i;
84 luaL_Buffer b;
85 const char *s = luaL_checklstring(L, 1, &l);
86 luaL_buffinit(L, &b);
87 for (i=0; i<l; i++)
88 luaL_addchar(&b, toupper(uchar(s[i])));
89 luaL_pushresult(&b);
90 return 1;
91}
92
93static int str_rep (lua_State *L) {
94 size_t l;
95 luaL_Buffer b;
96 const char *s = luaL_checklstring(L, 1, &l);
97 int n = luaL_checkint(L, 2);
98 luaL_buffinit(L, &b);
99 while (n-- > 0)
100 luaL_addlstring(&b, s, l);
101 luaL_pushresult(&b);
102 return 1;
103}
104
105
106static int str_byte (lua_State *L) {
107 size_t l;
108 const char *s = luaL_checklstring(L, 1, &l);
109 ptrdiff_t posi = posrelat(luaL_optinteger(L, 2, 1), l);
110 ptrdiff_t pose = posrelat(luaL_optinteger(L, 3, posi), l);
111 int n, i;
112 if (posi <= 0) posi = 1;
113 if ((size_t)pose > l) pose = l;
114 if (posi > pose) return 0; /* empty interval; return no values */
115 n = (int)(pose - posi + 1);
116 if (posi + n <= pose) /* overflow? */
117 luaL_error(L, "string slice too long");
118 luaL_checkstack(L, n, "string slice too long");
119 for (i=0; i<n; i++)
120 lua_pushinteger(L, uchar(s[posi+i-1]));
121 return n;
122}
123
124
125static int str_char (lua_State *L) {
126 int n = lua_gettop(L); /* number of arguments */
127 int i;
128 luaL_Buffer b;
129 luaL_buffinit(L, &b);
130 for (i=1; i<=n; i++) {
131 int c = luaL_checkint(L, i);
132 luaL_argcheck(L, uchar(c) == c, i, "invalid value");
133 luaL_addchar(&b, uchar(c));
134 }
135 luaL_pushresult(&b);
136 return 1;
137}
138
139
140static int writer (lua_State *L, const void* b, size_t size, void* B) {
141 (void)L;
142 luaL_addlstring((luaL_Buffer*) B, (const char *)b, size);
143 return 0;
144}
145
146
147static int str_dump (lua_State *L) {
148 luaL_Buffer b;
149 luaL_checktype(L, 1, LUA_TFUNCTION);
150 lua_settop(L, 1);
151 luaL_buffinit(L,&b);
152 if (lua_dump(L, writer, &b) != 0)
153 luaL_error(L, "unable to dump given function");
154 luaL_pushresult(&b);
155 return 1;
156}
157
158
159
160/*
161** {======================================================
162** PATTERN MATCHING
163** =======================================================
164*/
165
166
167#define CAP_UNFINISHED (-1)
168#define CAP_POSITION (-2)
169
170typedef struct MatchState {
171 const char *src_init; /* init of source string */
172 const char *src_end; /* end (`\0') of source string */
173 lua_State *L;
174 int level; /* total number of captures (finished or unfinished) */
175 struct {
176 const char *init;
177 ptrdiff_t len;
178 } capture[LUA_MAXCAPTURES];
179} MatchState;
180
181
182#define L_ESC '%'
183#define SPECIALS "^$*+?.([%-"
184
185
186static int check_capture (MatchState *ms, int l) {
187 l -= '1';
188 if (l < 0 || l >= ms->level || ms->capture[l].len == CAP_UNFINISHED)
189 return luaL_error(ms->L, "invalid capture index");
190 return l;
191}
192
193
194static int capture_to_close (MatchState *ms) {
195 int level = ms->level;
196 for (level--; level>=0; level--)
197 if (ms->capture[level].len == CAP_UNFINISHED) return level;
198 return luaL_error(ms->L, "invalid pattern capture");
199}
200
201
202static const char *classend (MatchState *ms, const char *p) {
203 switch (*p++) {
204 case L_ESC: {
205 if (*p == '\0')
206 luaL_error(ms->L, "malformed pattern (ends with " LUA_QL("%%") ")");
207 return p+1;
208 }
209 case '[': {
210 if (*p == '^') p++;
211 do { /* look for a `]' */
212 if (*p == '\0')
213 luaL_error(ms->L, "malformed pattern (missing " LUA_QL("]") ")");
214 if (*(p++) == L_ESC && *p != '\0')
215 p++; /* skip escapes (e.g. `%]') */
216 } while (*p != ']');
217 return p+1;
218 }
219 default: {
220 return p;
221 }
222 }
223}
224
225
226static int match_class (int c, int cl) {
227 int res;
228 switch (tolower(cl)) {
229 case 'a' : res = isalpha(c); break;
230 case 'c' : res = iscntrl(c); break;
231 case 'd' : res = isdigit(c); break;
232 case 'l' : res = islower(c); break;
233 case 'p' : res = ispunct(c); break;
234 case 's' : res = isspace(c); break;
235 case 'u' : res = isupper(c); break;
236 case 'w' : res = isalnum(c); break;
237 case 'x' : res = isxdigit(c); break;
238 case 'z' : res = (c == 0); break;
239 default: return (cl == c);
240 }
241 return (islower(cl) ? res : !res);
242}
243
244
245static int matchbracketclass (int c, const char *p, const char *ec) {
246 int sig = 1;
247 if (*(p+1) == '^') {
248 sig = 0;
249 p++; /* skip the `^' */
250 }
251 while (++p < ec) {
252 if (*p == L_ESC) {
253 p++;
254 if (match_class(c, uchar(*p)))
255 return sig;
256 }
257 else if ((*(p+1) == '-') && (p+2 < ec)) {
258 p+=2;
259 if (uchar(*(p-2)) <= c && c <= uchar(*p))
260 return sig;
261 }
262 else if (uchar(*p) == c) return sig;
263 }
264 return !sig;
265}
266
267
268static int singlematch (int c, const char *p, const char *ep) {
269 switch (*p) {
270 case '.': return 1; /* matches any char */
271 case L_ESC: return match_class(c, uchar(*(p+1)));
272 case '[': return matchbracketclass(c, p, ep-1);
273 default: return (uchar(*p) == c);
274 }
275}
276
277
278static const char *match (MatchState *ms, const char *s, const char *p);
279
280
281static const char *matchbalance (MatchState *ms, const char *s,
282 const char *p) {
283 if (*p == 0 || *(p+1) == 0)
284 luaL_error(ms->L, "unbalanced pattern");
285 if (*s != *p) return NULL;
286 else {
287 int b = *p;
288 int e = *(p+1);
289 int cont = 1;
290 while (++s < ms->src_end) {
291 if (*s == e) {
292 if (--cont == 0) return s+1;
293 }
294 else if (*s == b) cont++;
295 }
296 }
297 return NULL; /* string ends out of balance */
298}
299
300
301static const char *max_expand (MatchState *ms, const char *s,
302 const char *p, const char *ep) {
303 ptrdiff_t i = 0; /* counts maximum expand for item */
304 while ((s+i)<ms->src_end && singlematch(uchar(*(s+i)), p, ep))
305 i++;
306 /* keeps trying to match with the maximum repetitions */
307 while (i>=0) {
308 const char *res = match(ms, (s+i), ep+1);
309 if (res) return res;
310 i--; /* else didn't match; reduce 1 repetition to try again */
311 }
312 return NULL;
313}
314
315
316static const char *min_expand (MatchState *ms, const char *s,
317 const char *p, const char *ep) {
318 for (;;) {
319 const char *res = match(ms, s, ep+1);
320 if (res != NULL)
321 return res;
322 else if (s<ms->src_end && singlematch(uchar(*s), p, ep))
323 s++; /* try with one more repetition */
324 else return NULL;
325 }
326}
327
328
329static const char *start_capture (MatchState *ms, const char *s,
330 const char *p, int what) {
331 const char *res;
332 int level = ms->level;
333 if (level >= LUA_MAXCAPTURES) luaL_error(ms->L, "too many captures");
334 ms->capture[level].init = s;
335 ms->capture[level].len = what;
336 ms->level = level+1;
337 if ((res=match(ms, s, p)) == NULL) /* match failed? */
338 ms->level--; /* undo capture */
339 return res;
340}
341
342
343static const char *end_capture (MatchState *ms, const char *s,
344 const char *p) {
345 int l = capture_to_close(ms);
346 const char *res;
347 ms->capture[l].len = s - ms->capture[l].init; /* close capture */
348 if ((res = match(ms, s, p)) == NULL) /* match failed? */
349 ms->capture[l].len = CAP_UNFINISHED; /* undo capture */
350 return res;
351}
352
353
354static const char *match_capture (MatchState *ms, const char *s, int l) {
355 size_t len;
356 l = check_capture(ms, l);
357 len = ms->capture[l].len;
358 if ((size_t)(ms->src_end-s) >= len &&
359 memcmp(ms->capture[l].init, s, len) == 0)
360 return s+len;
361 else return NULL;
362}
363
364
365static const char *match (MatchState *ms, const char *s, const char *p) {
366 init: /* using goto's to optimize tail recursion */
367 switch (*p) {
368 case '(': { /* start capture */
369 if (*(p+1) == ')') /* position capture? */
370 return start_capture(ms, s, p+2, CAP_POSITION);
371 else
372 return start_capture(ms, s, p+1, CAP_UNFINISHED);
373 }
374 case ')': { /* end capture */
375 return end_capture(ms, s, p+1);
376 }
377 case L_ESC: {
378 switch (*(p+1)) {
379 case 'b': { /* balanced string? */
380 s = matchbalance(ms, s, p+2);
381 if (s == NULL) return NULL;
382 p+=4; goto init; /* else return match(ms, s, p+4); */
383 }
384 case 'f': { /* frontier? */
385 const char *ep; char previous;
386 p += 2;
387 if (*p != '[')
388 luaL_error(ms->L, "missing " LUA_QL("[") " after "
389 LUA_QL("%%f") " in pattern");
390 ep = classend(ms, p); /* points to what is next */
391 previous = (s == ms->src_init) ? '\0' : *(s-1);
392 if (matchbracketclass(uchar(previous), p, ep-1) ||
393 !matchbracketclass(uchar(*s), p, ep-1)) return NULL;
394 p=ep; goto init; /* else return match(ms, s, ep); */
395 }
396 default: {
397 if (isdigit(uchar(*(p+1)))) { /* capture results (%0-%9)? */
398 s = match_capture(ms, s, uchar(*(p+1)));
399 if (s == NULL) return NULL;
400 p+=2; goto init; /* else return match(ms, s, p+2) */
401 }
402 goto dflt; /* case default */
403 }
404 }
405 }
406 case '\0': { /* end of pattern */
407 return s; /* match succeeded */
408 }
409 case '$': {
410 if (*(p+1) == '\0') /* is the `$' the last char in pattern? */
411 return (s == ms->src_end) ? s : NULL; /* check end of string */
412 else goto dflt;
413 }
414 default: dflt: { /* it is a pattern item */
415 const char *ep = classend(ms, p); /* points to what is next */
416 int m = s<ms->src_end && singlematch(uchar(*s), p, ep);
417 switch (*ep) {
418 case '?': { /* optional */
419 const char *res;
420 if (m && ((res=match(ms, s+1, ep+1)) != NULL))
421 return res;
422 p=ep+1; goto init; /* else return match(ms, s, ep+1); */
423 }
424 case '*': { /* 0 or more repetitions */
425 return max_expand(ms, s, p, ep);
426 }
427 case '+': { /* 1 or more repetitions */
428 return (m ? max_expand(ms, s+1, p, ep) : NULL);
429 }
430 case '-': { /* 0 or more repetitions (minimum) */
431 return min_expand(ms, s, p, ep);
432 }
433 default: {
434 if (!m) return NULL;
435 s++; p=ep; goto init; /* else return match(ms, s+1, ep); */
436 }
437 }
438 }
439 }
440}
441
442
443
444static const char *lmemfind (const char *s1, size_t l1,
445 const char *s2, size_t l2) {
446 if (l2 == 0) return s1; /* empty strings are everywhere */
447 else if (l2 > l1) return NULL; /* avoids a negative `l1' */
448 else {
449 const char *init; /* to search for a `*s2' inside `s1' */
450 l2--; /* 1st char will be checked by `memchr' */
451 l1 = l1-l2; /* `s2' cannot be found after that */
452 while (l1 > 0 && (init = (const char *)memchr(s1, *s2, l1)) != NULL) {
453 init++; /* 1st char is already checked */
454 if (memcmp(init, s2+1, l2) == 0)
455 return init-1;
456 else { /* correct `l1' and `s1' to try again */
457 l1 -= init-s1;
458 s1 = init;
459 }
460 }
461 return NULL; /* not found */
462 }
463}
464
465
466static void push_onecapture (MatchState *ms, int i, const char *s,
467 const char *e) {
468 if (i >= ms->level) {
469 if (i == 0) /* ms->level == 0, too */
470 lua_pushlstring(ms->L, s, e - s); /* add whole match */
471 else
472 luaL_error(ms->L, "invalid capture index");
473 }
474 else {
475 ptrdiff_t l = ms->capture[i].len;
476 if (l == CAP_UNFINISHED) luaL_error(ms->L, "unfinished capture");
477 if (l == CAP_POSITION)
478 lua_pushinteger(ms->L, ms->capture[i].init - ms->src_init + 1);
479 else
480 lua_pushlstring(ms->L, ms->capture[i].init, l);
481 }
482}
483
484
485static int push_captures (MatchState *ms, const char *s, const char *e) {
486 int i;
487 int nlevels = (ms->level == 0 && s) ? 1 : ms->level;
488 luaL_checkstack(ms->L, nlevels, "too many captures");
489 for (i = 0; i < nlevels; i++)
490 push_onecapture(ms, i, s, e);
491 return nlevels; /* number of strings pushed */
492}
493
494
495static int str_find_aux (lua_State *L, int find) {
496 size_t l1, l2;
497 const char *s = luaL_checklstring(L, 1, &l1);
498 const char *p = luaL_checklstring(L, 2, &l2);
499 ptrdiff_t init = posrelat(luaL_optinteger(L, 3, 1), l1) - 1;
500 if (init < 0) init = 0;
501 else if ((size_t)(init) > l1) init = (ptrdiff_t)l1;
502 if (find && (lua_toboolean(L, 4) || /* explicit request? */
503 strpbrk(p, SPECIALS) == NULL)) { /* or no special characters? */
504 /* do a plain search */
505 const char *s2 = lmemfind(s+init, l1-init, p, l2);
506 if (s2) {
507 lua_pushinteger(L, s2-s+1);
508 lua_pushinteger(L, s2-s+l2);
509 return 2;
510 }
511 }
512 else {
513 MatchState ms;
514 int anchor = (*p == '^') ? (p++, 1) : 0;
515 const char *s1=s+init;
516 ms.L = L;
517 ms.src_init = s;
518 ms.src_end = s+l1;
519 do {
520 const char *res;
521 ms.level = 0;
522 if ((res=match(&ms, s1, p)) != NULL) {
523 if (find) {
524 lua_pushinteger(L, s1-s+1); /* start */
525 lua_pushinteger(L, res-s); /* end */
526 return push_captures(&ms, NULL, 0) + 2;
527 }
528 else
529 return push_captures(&ms, s1, res);
530 }
531 } while (s1++ < ms.src_end && !anchor);
532 }
533 lua_pushnil(L); /* not found */
534 return 1;
535}
536
537
538static int str_find (lua_State *L) {
539 return str_find_aux(L, 1);
540}
541
542
543static int str_match (lua_State *L) {
544 return str_find_aux(L, 0);
545}
546
547
548static int gmatch_aux (lua_State *L) {
549 MatchState ms;
550 size_t ls;
551 const char *s = lua_tolstring(L, lua_upvalueindex(1), &ls);
552 const char *p = lua_tostring(L, lua_upvalueindex(2));
553 const char *src;
554 ms.L = L;
555 ms.src_init = s;
556 ms.src_end = s+ls;
557 for (src = s + (size_t)lua_tointeger(L, lua_upvalueindex(3));
558 src <= ms.src_end;
559 src++) {
560 const char *e;
561 ms.level = 0;
562 if ((e = match(&ms, src, p)) != NULL) {
563 lua_Integer newstart = e-s;
564 if (e == src) newstart++; /* empty match? go at least one position */
565 lua_pushinteger(L, newstart);
566 lua_replace(L, lua_upvalueindex(3));
567 return push_captures(&ms, src, e);
568 }
569 }
570 return 0; /* not found */
571}
572
573
574static int gmatch (lua_State *L) {
575 luaL_checkstring(L, 1);
576 luaL_checkstring(L, 2);
577 lua_settop(L, 2);
578 lua_pushinteger(L, 0);
579 lua_pushcclosure(L, gmatch_aux, 3);
580 return 1;
581}
582
583
584static int gfind_nodef (lua_State *L) {
585 return luaL_error(L, LUA_QL("string.gfind") " was renamed to "
586 LUA_QL("string.gmatch"));
587}
588
589
590static void add_s (MatchState *ms, luaL_Buffer *b, const char *s,
591 const char *e) {
592 size_t l, i;
593 const char *news = lua_tolstring(ms->L, 3, &l);
594 for (i = 0; i < l; i++) {
595 if (news[i] != L_ESC)
596 luaL_addchar(b, news[i]);
597 else {
598 i++; /* skip ESC */
599 if (!isdigit(uchar(news[i])))
600 luaL_addchar(b, news[i]);
601 else if (news[i] == '0')
602 luaL_addlstring(b, s, e - s);
603 else {
604 push_onecapture(ms, news[i] - '1', s, e);
605 luaL_addvalue(b); /* add capture to accumulated result */
606 }
607 }
608 }
609}
610
611
612static void add_value (MatchState *ms, luaL_Buffer *b, const char *s,
613 const char *e) {
614 lua_State *L = ms->L;
615 switch (lua_type(L, 3)) {
616 case LUA_TNUMBER:
617 case LUA_TSTRING: {
618 add_s(ms, b, s, e);
619 return;
620 }
621 case LUA_TFUNCTION: {
622 int n;
623 lua_pushvalue(L, 3);
624 n = push_captures(ms, s, e);
625 lua_call(L, n, 1);
626 break;
627 }
628 case LUA_TTABLE: {
629 push_onecapture(ms, 0, s, e);
630 lua_gettable(L, 3);
631 break;
632 }
633 }
634 if (!lua_toboolean(L, -1)) { /* nil or false? */
635 lua_pop(L, 1);
636 lua_pushlstring(L, s, e - s); /* keep original text */
637 }
638 else if (!lua_isstring(L, -1))
639 luaL_error(L, "invalid replacement value (a %s)", luaL_typename(L, -1));
640 luaL_addvalue(b); /* add result to accumulator */
641}
642
643
644static int str_gsub (lua_State *L) {
645 size_t srcl;
646 const char *src = luaL_checklstring(L, 1, &srcl);
647 const char *p = luaL_checkstring(L, 2);
648 int tr = lua_type(L, 3);
649 int max_s = luaL_optint(L, 4, srcl+1);
650 int anchor = (*p == '^') ? (p++, 1) : 0;
651 int n = 0;
652 MatchState ms;
653 luaL_Buffer b;
654 luaL_argcheck(L, tr == LUA_TNUMBER || tr == LUA_TSTRING ||
655 tr == LUA_TFUNCTION || tr == LUA_TTABLE, 3,
656 "string/function/table expected");
657 luaL_buffinit(L, &b);
658 ms.L = L;
659 ms.src_init = src;
660 ms.src_end = src+srcl;
661 while (n < max_s) {
662 const char *e;
663 ms.level = 0;
664 e = match(&ms, src, p);
665 if (e) {
666 n++;
667 add_value(&ms, &b, src, e);
668 }
669 if (e && e>src) /* non empty match? */
670 src = e; /* skip it */
671 else if (src < ms.src_end)
672 luaL_addchar(&b, *src++);
673 else break;
674 if (anchor) break;
675 }
676 luaL_addlstring(&b, src, ms.src_end-src);
677 luaL_pushresult(&b);
678 lua_pushinteger(L, n); /* number of substitutions */
679 return 2;
680}
681
682/* }====================================================== */
683
684
685/* maximum size of each formatted item (> len(format('%99.99f', -1e308))) */
686#define MAX_ITEM 512
687/* valid flags in a format specification */
688#define FLAGS "-+ #0"
689/*
690** maximum size of each format specification (such as '%-099.99d')
691** (+10 accounts for %99.99x plus margin of error)
692*/
693#define MAX_FORMAT (sizeof(FLAGS) + sizeof(LUA_INTFRMLEN) + 10)
694
695
696static void addquoted (lua_State *L, luaL_Buffer *b, int arg) {
697 size_t l;
698 const char *s = luaL_checklstring(L, arg, &l);
699 luaL_addchar(b, '"');
700 while (l--) {
701 switch (*s) {
702 case '"': case '\\': case '\n': {
703 luaL_addchar(b, '\\');
704 luaL_addchar(b, *s);
705 break;
706 }
707 case '\r': {
708 luaL_addlstring(b, "\\r", 2);
709 break;
710 }
711 case '\0': {
712 luaL_addlstring(b, "\\000", 4);
713 break;
714 }
715 default: {
716 luaL_addchar(b, *s);
717 break;
718 }
719 }
720 s++;
721 }
722 luaL_addchar(b, '"');
723}
724
725static const char *scanformat (lua_State *L, const char *strfrmt, char *form) {
726 const char *p = strfrmt;
727 while (*p != '\0' && strchr(FLAGS, *p) != NULL) p++; /* skip flags */
728 if ((size_t)(p - strfrmt) >= sizeof(FLAGS))
729 luaL_error(L, "invalid format (repeated flags)");
730 if (isdigit(uchar(*p))) p++; /* skip width */
731 if (isdigit(uchar(*p))) p++; /* (2 digits at most) */
732 if (*p == '.') {
733 p++;
734 if (isdigit(uchar(*p))) p++; /* skip precision */
735 if (isdigit(uchar(*p))) p++; /* (2 digits at most) */
736 }
737 if (isdigit(uchar(*p)))
738 luaL_error(L, "invalid format (width or precision too long)");
739 *(form++) = '%';
740 strncpy(form, strfrmt, p - strfrmt + 1);
741 form += p - strfrmt + 1;
742 *form = '\0';
743 return p;
744}
745
746
747static void addintlen (char *form) {
748 size_t l = strlen(form);
749 char spec = form[l - 1];
750 strcpy(form + l - 1, LUA_INTFRMLEN);
751 form[l + sizeof(LUA_INTFRMLEN) - 2] = spec;
752 form[l + sizeof(LUA_INTFRMLEN) - 1] = '\0';
753}
754
755
756static int str_format (lua_State *L) {
757 int top = lua_gettop(L);
758 int arg = 1;
759 size_t sfl;
760 const char *strfrmt = luaL_checklstring(L, arg, &sfl);
761 const char *strfrmt_end = strfrmt+sfl;
762 luaL_Buffer b;
763 luaL_buffinit(L, &b);
764 while (strfrmt < strfrmt_end) {
765 if (*strfrmt != L_ESC)
766 luaL_addchar(&b, *strfrmt++);
767 else if (*++strfrmt == L_ESC)
768 luaL_addchar(&b, *strfrmt++); /* %% */
769 else { /* format item */
770 char form[MAX_FORMAT]; /* to store the format (`%...') */
771 char buff[MAX_ITEM]; /* to store the formatted item */
772 if (++arg > top)
773 luaL_argerror(L, arg, "no value");
774 strfrmt = scanformat(L, strfrmt, form);
775 switch (*strfrmt++) {
776 case 'c': {
777 sprintf(buff, form, (int)luaL_checknumber(L, arg));
778 break;
779 }
780 case 'd': case 'i': {
781 addintlen(form);
782 sprintf(buff, form, (LUA_INTFRM_T)luaL_checknumber(L, arg));
783 break;
784 }
785 case 'o': case 'u': case 'x': case 'X': {
786 addintlen(form);
787 sprintf(buff, form, (unsigned LUA_INTFRM_T)luaL_checknumber(L, arg));
788 break;
789 }
790 case 'e': case 'E': case 'f':
791 case 'g': case 'G': {
792 sprintf(buff, form, (double)luaL_checknumber(L, arg));
793 break;
794 }
795 case 'q': {
796 addquoted(L, &b, arg);
797 continue; /* skip the 'addsize' at the end */
798 }
799 case 's': {
800 size_t l;
801 const char *s = luaL_checklstring(L, arg, &l);
802 if (!strchr(form, '.') && l >= 100) {
803 /* no precision and string is too long to be formatted;
804 keep original string */
805 lua_pushvalue(L, arg);
806 luaL_addvalue(&b);
807 continue; /* skip the `addsize' at the end */
808 }
809 else {
810 sprintf(buff, form, s);
811 break;
812 }
813 }
814 default: { /* also treat cases `pnLlh' */
815 return luaL_error(L, "invalid option " LUA_QL("%%%c") " to "
816 LUA_QL("format"), *(strfrmt - 1));
817 }
818 }
819 luaL_addlstring(&b, buff, strlen(buff));
820 }
821 }
822 luaL_pushresult(&b);
823 return 1;
824}
825
826
827static const luaL_Reg strlib[] = {
828 {"byte", str_byte},
829 {"char", str_char},
830 {"dump", str_dump},
831 {"find", str_find},
832 {"format", str_format},
833 {"gfind", gfind_nodef},
834 {"gmatch", gmatch},
835 {"gsub", str_gsub},
836 {"len", str_len},
837 {"lower", str_lower},
838 {"match", str_match},
839 {"rep", str_rep},
840 {"reverse", str_reverse},
841 {"sub", str_sub},
842 {"upper", str_upper},
843 {NULL, NULL}
844};
845
846
847static void createmetatable (lua_State *L) {
848 lua_createtable(L, 0, 1); /* create metatable for strings */
849 lua_pushliteral(L, ""); /* dummy string */
850 lua_pushvalue(L, -2);
851 lua_setmetatable(L, -2); /* set string metatable */
852 lua_pop(L, 1); /* pop dummy string */
853 lua_pushvalue(L, -2); /* string library... */
854 lua_setfield(L, -2, "__index"); /* ...is the __index metamethod */
855 lua_pop(L, 1); /* pop metatable */
856}
857
858
859/*
860** Open string library
861*/
862LUALIB_API int luaopen_string (lua_State *L) {
863 luaL_register(L, LUA_STRLIBNAME, strlib);
864#if defined(LUA_COMPAT_GFIND)
865 lua_getfield(L, -1, "gmatch");
866 lua_setfield(L, -2, "gfind");
867#endif
868 createmetatable(L);
869 return 1;
870}
871
diff --git a/examples/redis-unstable/deps/lua/src/ltable.c b/examples/redis-unstable/deps/lua/src/ltable.c
deleted file mode 100644
index 55575a8..0000000
--- a/examples/redis-unstable/deps/lua/src/ltable.c
+++ /dev/null
@@ -1,588 +0,0 @@
1/*
2** $Id: ltable.c,v 2.32.1.2 2007/12/28 15:32:23 roberto Exp $
3** Lua tables (hash)
4** See Copyright Notice in lua.h
5*/
6
7
8/*
9** Implementation of tables (aka arrays, objects, or hash tables).
10** Tables keep its elements in two parts: an array part and a hash part.
11** Non-negative integer keys are all candidates to be kept in the array
12** part. The actual size of the array is the largest `n' such that at
13** least half the slots between 0 and n are in use.
14** Hash uses a mix of chained scatter table with Brent's variation.
15** A main invariant of these tables is that, if an element is not
16** in its main position (i.e. the `original' position that its hash gives
17** to it), then the colliding element is in its own main position.
18** Hence even when the load factor reaches 100%, performance remains good.
19*/
20
21#include <math.h>
22#include <string.h>
23
24#define ltable_c
25#define LUA_CORE
26
27#include "lua.h"
28
29#include "ldebug.h"
30#include "ldo.h"
31#include "lgc.h"
32#include "lmem.h"
33#include "lobject.h"
34#include "lstate.h"
35#include "ltable.h"
36
37
38/*
39** max size of array part is 2^MAXBITS
40*/
41#if LUAI_BITSINT > 26
42#define MAXBITS 26
43#else
44#define MAXBITS (LUAI_BITSINT-2)
45#endif
46
47#define MAXASIZE (1 << MAXBITS)
48
49
50#define hashpow2(t,n) (gnode(t, lmod((n), sizenode(t))))
51
52#define hashstr(t,str) hashpow2(t, (str)->tsv.hash)
53#define hashboolean(t,p) hashpow2(t, p)
54
55
56/*
57** for some types, it is better to avoid modulus by power of 2, as
58** they tend to have many 2 factors.
59*/
60#define hashmod(t,n) (gnode(t, ((n) % ((sizenode(t)-1)|1))))
61
62
63#define hashpointer(t,p) hashmod(t, IntPoint(p))
64
65
66/*
67** number of ints inside a lua_Number
68*/
69#define numints cast_int(sizeof(lua_Number)/sizeof(int))
70
71
72
73#define dummynode (&dummynode_)
74
75static const Node dummynode_ = {
76 {{NULL}, LUA_TNIL}, /* value */
77 {{{NULL}, LUA_TNIL, NULL}} /* key */
78};
79
80
81/*
82** hash for lua_Numbers
83*/
84static Node *hashnum (const Table *t, lua_Number n) {
85 unsigned int a[numints];
86 int i;
87 if (luai_numeq(n, 0)) /* avoid problems with -0 */
88 return gnode(t, 0);
89 memcpy(a, &n, sizeof(a));
90 for (i = 1; i < numints; i++) a[0] += a[i];
91 return hashmod(t, a[0]);
92}
93
94
95
96/*
97** returns the `main' position of an element in a table (that is, the index
98** of its hash value)
99*/
100static Node *mainposition (const Table *t, const TValue *key) {
101 switch (ttype(key)) {
102 case LUA_TNUMBER:
103 return hashnum(t, nvalue(key));
104 case LUA_TSTRING:
105 return hashstr(t, rawtsvalue(key));
106 case LUA_TBOOLEAN:
107 return hashboolean(t, bvalue(key));
108 case LUA_TLIGHTUSERDATA:
109 return hashpointer(t, pvalue(key));
110 default:
111 return hashpointer(t, gcvalue(key));
112 }
113}
114
115
116/*
117** returns the index for `key' if `key' is an appropriate key to live in
118** the array part of the table, -1 otherwise.
119*/
120static int arrayindex (const TValue *key) {
121 if (ttisnumber(key)) {
122 lua_Number n = nvalue(key);
123 int k;
124 lua_number2int(k, n);
125 if (luai_numeq(cast_num(k), n))
126 return k;
127 }
128 return -1; /* `key' did not match some condition */
129}
130
131
132/*
133** returns the index of a `key' for table traversals. First goes all
134** elements in the array part, then elements in the hash part. The
135** beginning of a traversal is signalled by -1.
136*/
137static int findindex (lua_State *L, Table *t, StkId key) {
138 int i;
139 if (ttisnil(key)) return -1; /* first iteration */
140 i = arrayindex(key);
141 if (0 < i && i <= t->sizearray) /* is `key' inside array part? */
142 return i-1; /* yes; that's the index (corrected to C) */
143 else {
144 Node *n = mainposition(t, key);
145 do { /* check whether `key' is somewhere in the chain */
146 /* key may be dead already, but it is ok to use it in `next' */
147 if (luaO_rawequalObj(key2tval(n), key) ||
148 (ttype(gkey(n)) == LUA_TDEADKEY && iscollectable(key) &&
149 gcvalue(gkey(n)) == gcvalue(key))) {
150 i = cast_int(n - gnode(t, 0)); /* key index in hash table */
151 /* hash elements are numbered after array ones */
152 return i + t->sizearray;
153 }
154 else n = gnext(n);
155 } while (n);
156 luaG_runerror(L, "invalid key to " LUA_QL("next")); /* key not found */
157 return 0; /* to avoid warnings */
158 }
159}
160
161
162int luaH_next (lua_State *L, Table *t, StkId key) {
163 int i = findindex(L, t, key); /* find original element */
164 for (i++; i < t->sizearray; i++) { /* try first array part */
165 if (!ttisnil(&t->array[i])) { /* a non-nil value? */
166 setnvalue(key, cast_num(i+1));
167 setobj2s(L, key+1, &t->array[i]);
168 return 1;
169 }
170 }
171 for (i -= t->sizearray; i < sizenode(t); i++) { /* then hash part */
172 if (!ttisnil(gval(gnode(t, i)))) { /* a non-nil value? */
173 setobj2s(L, key, key2tval(gnode(t, i)));
174 setobj2s(L, key+1, gval(gnode(t, i)));
175 return 1;
176 }
177 }
178 return 0; /* no more elements */
179}
180
181
182/*
183** {=============================================================
184** Rehash
185** ==============================================================
186*/
187
188
189static int computesizes (int nums[], int *narray) {
190 int i;
191 int twotoi; /* 2^i */
192 int a = 0; /* number of elements smaller than 2^i */
193 int na = 0; /* number of elements to go to array part */
194 int n = 0; /* optimal size for array part */
195 for (i = 0, twotoi = 1; twotoi/2 < *narray; i++, twotoi *= 2) {
196 if (nums[i] > 0) {
197 a += nums[i];
198 if (a > twotoi/2) { /* more than half elements present? */
199 n = twotoi; /* optimal size (till now) */
200 na = a; /* all elements smaller than n will go to array part */
201 }
202 }
203 if (a == *narray) break; /* all elements already counted */
204 }
205 *narray = n;
206 lua_assert(*narray/2 <= na && na <= *narray);
207 return na;
208}
209
210
211static int countint (const TValue *key, int *nums) {
212 int k = arrayindex(key);
213 if (0 < k && k <= MAXASIZE) { /* is `key' an appropriate array index? */
214 nums[ceillog2(k)]++; /* count as such */
215 return 1;
216 }
217 else
218 return 0;
219}
220
221
222static int numusearray (const Table *t, int *nums) {
223 int lg;
224 int ttlg; /* 2^lg */
225 int ause = 0; /* summation of `nums' */
226 int i = 1; /* count to traverse all array keys */
227 for (lg=0, ttlg=1; lg<=MAXBITS; lg++, ttlg*=2) { /* for each slice */
228 int lc = 0; /* counter */
229 int lim = ttlg;
230 if (lim > t->sizearray) {
231 lim = t->sizearray; /* adjust upper limit */
232 if (i > lim)
233 break; /* no more elements to count */
234 }
235 /* count elements in range (2^(lg-1), 2^lg] */
236 for (; i <= lim; i++) {
237 if (!ttisnil(&t->array[i-1]))
238 lc++;
239 }
240 nums[lg] += lc;
241 ause += lc;
242 }
243 return ause;
244}
245
246
247static int numusehash (const Table *t, int *nums, int *pnasize) {
248 int totaluse = 0; /* total number of elements */
249 int ause = 0; /* summation of `nums' */
250 int i = sizenode(t);
251 while (i--) {
252 Node *n = &t->node[i];
253 if (!ttisnil(gval(n))) {
254 ause += countint(key2tval(n), nums);
255 totaluse++;
256 }
257 }
258 *pnasize += ause;
259 return totaluse;
260}
261
262
263static void setarrayvector (lua_State *L, Table *t, int size) {
264 int i;
265 luaM_reallocvector(L, t->array, t->sizearray, size, TValue);
266 for (i=t->sizearray; i<size; i++)
267 setnilvalue(&t->array[i]);
268 t->sizearray = size;
269}
270
271
272static void setnodevector (lua_State *L, Table *t, int size) {
273 int lsize;
274 if (size == 0) { /* no elements to hash part? */
275 t->node = cast(Node *, dummynode); /* use common `dummynode' */
276 lsize = 0;
277 }
278 else {
279 int i;
280 lsize = ceillog2(size);
281 if (lsize > MAXBITS)
282 luaG_runerror(L, "table overflow");
283 size = twoto(lsize);
284 t->node = luaM_newvector(L, size, Node);
285 for (i=0; i<size; i++) {
286 Node *n = gnode(t, i);
287 gnext(n) = NULL;
288 setnilvalue(gkey(n));
289 setnilvalue(gval(n));
290 }
291 }
292 t->lsizenode = cast_byte(lsize);
293 t->lastfree = gnode(t, size); /* all positions are free */
294}
295
296
297static void resize (lua_State *L, Table *t, int nasize, int nhsize) {
298 int i;
299 int oldasize = t->sizearray;
300 int oldhsize = t->lsizenode;
301 Node *nold = t->node; /* save old hash ... */
302 if (nasize > oldasize) /* array part must grow? */
303 setarrayvector(L, t, nasize);
304 /* create new hash part with appropriate size */
305 setnodevector(L, t, nhsize);
306 if (nasize < oldasize) { /* array part must shrink? */
307 t->sizearray = nasize;
308 /* re-insert elements from vanishing slice */
309 for (i=nasize; i<oldasize; i++) {
310 if (!ttisnil(&t->array[i]))
311 setobjt2t(L, luaH_setnum(L, t, i+1), &t->array[i]);
312 }
313 /* shrink array */
314 luaM_reallocvector(L, t->array, oldasize, nasize, TValue);
315 }
316 /* re-insert elements from hash part */
317 for (i = twoto(oldhsize) - 1; i >= 0; i--) {
318 Node *old = nold+i;
319 if (!ttisnil(gval(old)))
320 setobjt2t(L, luaH_set(L, t, key2tval(old)), gval(old));
321 }
322 if (nold != dummynode)
323 luaM_freearray(L, nold, twoto(oldhsize), Node); /* free old array */
324}
325
326
327void luaH_resizearray (lua_State *L, Table *t, int nasize) {
328 int nsize = (t->node == dummynode) ? 0 : sizenode(t);
329 resize(L, t, nasize, nsize);
330}
331
332
333static void rehash (lua_State *L, Table *t, const TValue *ek) {
334 int nasize, na;
335 int nums[MAXBITS+1]; /* nums[i] = number of keys between 2^(i-1) and 2^i */
336 int i;
337 int totaluse;
338 for (i=0; i<=MAXBITS; i++) nums[i] = 0; /* reset counts */
339 nasize = numusearray(t, nums); /* count keys in array part */
340 totaluse = nasize; /* all those keys are integer keys */
341 totaluse += numusehash(t, nums, &nasize); /* count keys in hash part */
342 /* count extra key */
343 nasize += countint(ek, nums);
344 totaluse++;
345 /* compute new size for array part */
346 na = computesizes(nums, &nasize);
347 /* resize the table to new computed sizes */
348 resize(L, t, nasize, totaluse - na);
349}
350
351
352
353/*
354** }=============================================================
355*/
356
357
358Table *luaH_new (lua_State *L, int narray, int nhash) {
359 Table *t = luaM_new(L, Table);
360 luaC_link(L, obj2gco(t), LUA_TTABLE);
361 t->metatable = NULL;
362 t->flags = cast_byte(~0);
363 /* temporary values (kept only if some malloc fails) */
364 t->array = NULL;
365 t->sizearray = 0;
366 t->lsizenode = 0;
367 t->readonly = 0;
368 t->node = cast(Node *, dummynode);
369 setarrayvector(L, t, narray);
370 setnodevector(L, t, nhash);
371 return t;
372}
373
374
375void luaH_free (lua_State *L, Table *t) {
376 if (t->node != dummynode)
377 luaM_freearray(L, t->node, sizenode(t), Node);
378 luaM_freearray(L, t->array, t->sizearray, TValue);
379 luaM_free(L, t);
380}
381
382
383static Node *getfreepos (Table *t) {
384 while (t->lastfree-- > t->node) {
385 if (ttisnil(gkey(t->lastfree)))
386 return t->lastfree;
387 }
388 return NULL; /* could not find a free place */
389}
390
391
392
393/*
394** inserts a new key into a hash table; first, check whether key's main
395** position is free. If not, check whether colliding node is in its main
396** position or not: if it is not, move colliding node to an empty place and
397** put new key in its main position; otherwise (colliding node is in its main
398** position), new key goes to an empty position.
399*/
400static TValue *newkey (lua_State *L, Table *t, const TValue *key) {
401 Node *mp = mainposition(t, key);
402 if (!ttisnil(gval(mp)) || mp == dummynode) {
403 Node *othern;
404 Node *n = getfreepos(t); /* get a free place */
405 if (n == NULL) { /* cannot find a free place? */
406 rehash(L, t, key); /* grow table */
407 return luaH_set(L, t, key); /* re-insert key into grown table */
408 }
409 lua_assert(n != dummynode);
410 othern = mainposition(t, key2tval(mp));
411 if (othern != mp) { /* is colliding node out of its main position? */
412 /* yes; move colliding node into free position */
413 while (gnext(othern) != mp) othern = gnext(othern); /* find previous */
414 gnext(othern) = n; /* redo the chain with `n' in place of `mp' */
415 *n = *mp; /* copy colliding node into free pos. (mp->next also goes) */
416 gnext(mp) = NULL; /* now `mp' is free */
417 setnilvalue(gval(mp));
418 }
419 else { /* colliding node is in its own main position */
420 /* new node will go into free position */
421 gnext(n) = gnext(mp); /* chain new position */
422 gnext(mp) = n;
423 mp = n;
424 }
425 }
426 gkey(mp)->value = key->value; gkey(mp)->tt = key->tt;
427 luaC_barriert(L, t, key);
428 lua_assert(ttisnil(gval(mp)));
429 return gval(mp);
430}
431
432
433/*
434** search function for integers
435*/
436const TValue *luaH_getnum (Table *t, int key) {
437 if (1 <= key && key <= t->sizearray)
438 return &t->array[key-1];
439 else {
440 lua_Number nk = cast_num(key);
441 Node *n = hashnum(t, nk);
442 do { /* check whether `key' is somewhere in the chain */
443 if (ttisnumber(gkey(n)) && luai_numeq(nvalue(gkey(n)), nk))
444 return gval(n); /* that's it */
445 else n = gnext(n);
446 } while (n);
447 return luaO_nilobject;
448 }
449}
450
451
452/*
453** search function for strings
454*/
455const TValue *luaH_getstr (Table *t, TString *key) {
456 Node *n = hashstr(t, key);
457 do { /* check whether `key' is somewhere in the chain */
458 if (ttisstring(gkey(n)) && rawtsvalue(gkey(n)) == key)
459 return gval(n); /* that's it */
460 else n = gnext(n);
461 } while (n);
462 return luaO_nilobject;
463}
464
465
466/*
467** main search function
468*/
469const TValue *luaH_get (Table *t, const TValue *key) {
470 switch (ttype(key)) {
471 case LUA_TNIL: return luaO_nilobject;
472 case LUA_TSTRING: return luaH_getstr(t, rawtsvalue(key));
473 case LUA_TNUMBER: {
474 int k;
475 lua_Number n = nvalue(key);
476 lua_number2int(k, n);
477 if (luai_numeq(cast_num(k), nvalue(key))) /* index is int? */
478 return luaH_getnum(t, k); /* use specialized version */
479 /* else go through */
480 }
481 default: {
482 Node *n = mainposition(t, key);
483 do { /* check whether `key' is somewhere in the chain */
484 if (luaO_rawequalObj(key2tval(n), key))
485 return gval(n); /* that's it */
486 else n = gnext(n);
487 } while (n);
488 return luaO_nilobject;
489 }
490 }
491}
492
493
494TValue *luaH_set (lua_State *L, Table *t, const TValue *key) {
495 const TValue *p = luaH_get(t, key);
496 t->flags = 0;
497 if (p != luaO_nilobject)
498 return cast(TValue *, p);
499 else {
500 if (ttisnil(key)) luaG_runerror(L, "table index is nil");
501 else if (ttisnumber(key) && luai_numisnan(nvalue(key)))
502 luaG_runerror(L, "table index is NaN");
503 return newkey(L, t, key);
504 }
505}
506
507
508TValue *luaH_setnum (lua_State *L, Table *t, int key) {
509 const TValue *p = luaH_getnum(t, key);
510 if (p != luaO_nilobject)
511 return cast(TValue *, p);
512 else {
513 TValue k;
514 setnvalue(&k, cast_num(key));
515 return newkey(L, t, &k);
516 }
517}
518
519
520TValue *luaH_setstr (lua_State *L, Table *t, TString *key) {
521 const TValue *p = luaH_getstr(t, key);
522 if (p != luaO_nilobject)
523 return cast(TValue *, p);
524 else {
525 TValue k;
526 setsvalue(L, &k, key);
527 return newkey(L, t, &k);
528 }
529}
530
531
532static int unbound_search (Table *t, unsigned int j) {
533 unsigned int i = j; /* i is zero or a present index */
534 j++;
535 /* find `i' and `j' such that i is present and j is not */
536 while (!ttisnil(luaH_getnum(t, j))) {
537 i = j;
538 j *= 2;
539 if (j > cast(unsigned int, MAX_INT)) { /* overflow? */
540 /* table was built with bad purposes: resort to linear search */
541 i = 1;
542 while (!ttisnil(luaH_getnum(t, i))) i++;
543 return i - 1;
544 }
545 }
546 /* now do a binary search between them */
547 while (j - i > 1) {
548 unsigned int m = (i+j)/2;
549 if (ttisnil(luaH_getnum(t, m))) j = m;
550 else i = m;
551 }
552 return i;
553}
554
555
556/*
557** Try to find a boundary in table `t'. A `boundary' is an integer index
558** such that t[i] is non-nil and t[i+1] is nil (and 0 if t[1] is nil).
559*/
560int luaH_getn (Table *t) {
561 unsigned int j = t->sizearray;
562 if (j > 0 && ttisnil(&t->array[j - 1])) {
563 /* there is a boundary in the array part: (binary) search for it */
564 unsigned int i = 0;
565 while (j - i > 1) {
566 unsigned int m = (i+j)/2;
567 if (ttisnil(&t->array[m - 1])) j = m;
568 else i = m;
569 }
570 return i;
571 }
572 /* else must find a boundary in hash part */
573 else if (t->node == dummynode) /* hash part is empty? */
574 return j; /* that is easy... */
575 else return unbound_search(t, j);
576}
577
578
579
580#if defined(LUA_DEBUG)
581
582Node *luaH_mainposition (const Table *t, const TValue *key) {
583 return mainposition(t, key);
584}
585
586int luaH_isdummy (Node *n) { return n == dummynode; }
587
588#endif
diff --git a/examples/redis-unstable/deps/lua/src/ltable.h b/examples/redis-unstable/deps/lua/src/ltable.h
deleted file mode 100644
index f5b9d5e..0000000
--- a/examples/redis-unstable/deps/lua/src/ltable.h
+++ /dev/null
@@ -1,40 +0,0 @@
1/*
2** $Id: ltable.h,v 2.10.1.1 2007/12/27 13:02:25 roberto Exp $
3** Lua tables (hash)
4** See Copyright Notice in lua.h
5*/
6
7#ifndef ltable_h
8#define ltable_h
9
10#include "lobject.h"
11
12
13#define gnode(t,i) (&(t)->node[i])
14#define gkey(n) (&(n)->i_key.nk)
15#define gval(n) (&(n)->i_val)
16#define gnext(n) ((n)->i_key.nk.next)
17
18#define key2tval(n) (&(n)->i_key.tvk)
19
20
21LUAI_FUNC const TValue *luaH_getnum (Table *t, int key);
22LUAI_FUNC TValue *luaH_setnum (lua_State *L, Table *t, int key);
23LUAI_FUNC const TValue *luaH_getstr (Table *t, TString *key);
24LUAI_FUNC TValue *luaH_setstr (lua_State *L, Table *t, TString *key);
25LUAI_FUNC const TValue *luaH_get (Table *t, const TValue *key);
26LUAI_FUNC TValue *luaH_set (lua_State *L, Table *t, const TValue *key);
27LUAI_FUNC Table *luaH_new (lua_State *L, int narray, int lnhash);
28LUAI_FUNC void luaH_resizearray (lua_State *L, Table *t, int nasize);
29LUAI_FUNC void luaH_free (lua_State *L, Table *t);
30LUAI_FUNC int luaH_next (lua_State *L, Table *t, StkId key);
31LUAI_FUNC int luaH_getn (Table *t);
32
33
34#if defined(LUA_DEBUG)
35LUAI_FUNC Node *luaH_mainposition (const Table *t, const TValue *key);
36LUAI_FUNC int luaH_isdummy (Node *n);
37#endif
38
39
40#endif
diff --git a/examples/redis-unstable/deps/lua/src/ltablib.c b/examples/redis-unstable/deps/lua/src/ltablib.c
deleted file mode 100644
index 0bdac7f..0000000
--- a/examples/redis-unstable/deps/lua/src/ltablib.c
+++ /dev/null
@@ -1,287 +0,0 @@
1/*
2** $Id: ltablib.c,v 1.38.1.3 2008/02/14 16:46:58 roberto Exp $
3** Library for Table Manipulation
4** See Copyright Notice in lua.h
5*/
6
7
8#include <stddef.h>
9
10#define ltablib_c
11#define LUA_LIB
12
13#include "lua.h"
14
15#include "lauxlib.h"
16#include "lualib.h"
17
18
19#define aux_getn(L,n) (luaL_checktype(L, n, LUA_TTABLE), luaL_getn(L, n))
20
21
22static int foreachi (lua_State *L) {
23 int i;
24 int n = aux_getn(L, 1);
25 luaL_checktype(L, 2, LUA_TFUNCTION);
26 for (i=1; i <= n; i++) {
27 lua_pushvalue(L, 2); /* function */
28 lua_pushinteger(L, i); /* 1st argument */
29 lua_rawgeti(L, 1, i); /* 2nd argument */
30 lua_call(L, 2, 1);
31 if (!lua_isnil(L, -1))
32 return 1;
33 lua_pop(L, 1); /* remove nil result */
34 }
35 return 0;
36}
37
38
39static int foreach (lua_State *L) {
40 luaL_checktype(L, 1, LUA_TTABLE);
41 luaL_checktype(L, 2, LUA_TFUNCTION);
42 lua_pushnil(L); /* first key */
43 while (lua_next(L, 1)) {
44 lua_pushvalue(L, 2); /* function */
45 lua_pushvalue(L, -3); /* key */
46 lua_pushvalue(L, -3); /* value */
47 lua_call(L, 2, 1);
48 if (!lua_isnil(L, -1))
49 return 1;
50 lua_pop(L, 2); /* remove value and result */
51 }
52 return 0;
53}
54
55
56static int maxn (lua_State *L) {
57 lua_Number max = 0;
58 luaL_checktype(L, 1, LUA_TTABLE);
59 lua_pushnil(L); /* first key */
60 while (lua_next(L, 1)) {
61 lua_pop(L, 1); /* remove value */
62 if (lua_type(L, -1) == LUA_TNUMBER) {
63 lua_Number v = lua_tonumber(L, -1);
64 if (v > max) max = v;
65 }
66 }
67 lua_pushnumber(L, max);
68 return 1;
69}
70
71
72static int getn (lua_State *L) {
73 lua_pushinteger(L, aux_getn(L, 1));
74 return 1;
75}
76
77
78static int setn (lua_State *L) {
79 luaL_checktype(L, 1, LUA_TTABLE);
80#ifndef luaL_setn
81 luaL_setn(L, 1, luaL_checkint(L, 2));
82#else
83 luaL_error(L, LUA_QL("setn") " is obsolete");
84#endif
85 lua_pushvalue(L, 1);
86 return 1;
87}
88
89
90static int tinsert (lua_State *L) {
91 int e = aux_getn(L, 1) + 1; /* first empty element */
92 int pos; /* where to insert new element */
93 switch (lua_gettop(L)) {
94 case 2: { /* called with only 2 arguments */
95 pos = e; /* insert new element at the end */
96 break;
97 }
98 case 3: {
99 int i;
100 pos = luaL_checkint(L, 2); /* 2nd argument is the position */
101 if (pos > e) e = pos; /* `grow' array if necessary */
102 for (i = e; i > pos; i--) { /* move up elements */
103 lua_rawgeti(L, 1, i-1);
104 lua_rawseti(L, 1, i); /* t[i] = t[i-1] */
105 }
106 break;
107 }
108 default: {
109 return luaL_error(L, "wrong number of arguments to " LUA_QL("insert"));
110 }
111 }
112 luaL_setn(L, 1, e); /* new size */
113 lua_rawseti(L, 1, pos); /* t[pos] = v */
114 return 0;
115}
116
117
118static int tremove (lua_State *L) {
119 int e = aux_getn(L, 1);
120 int pos = luaL_optint(L, 2, e);
121 if (!(1 <= pos && pos <= e)) /* position is outside bounds? */
122 return 0; /* nothing to remove */
123 luaL_setn(L, 1, e - 1); /* t.n = n-1 */
124 lua_rawgeti(L, 1, pos); /* result = t[pos] */
125 for ( ;pos<e; pos++) {
126 lua_rawgeti(L, 1, pos+1);
127 lua_rawseti(L, 1, pos); /* t[pos] = t[pos+1] */
128 }
129 lua_pushnil(L);
130 lua_rawseti(L, 1, e); /* t[e] = nil */
131 return 1;
132}
133
134
135static void addfield (lua_State *L, luaL_Buffer *b, int i) {
136 lua_rawgeti(L, 1, i);
137 if (!lua_isstring(L, -1))
138 luaL_error(L, "invalid value (%s) at index %d in table for "
139 LUA_QL("concat"), luaL_typename(L, -1), i);
140 luaL_addvalue(b);
141}
142
143
144static int tconcat (lua_State *L) {
145 luaL_Buffer b;
146 size_t lsep;
147 int i, last;
148 const char *sep = luaL_optlstring(L, 2, "", &lsep);
149 luaL_checktype(L, 1, LUA_TTABLE);
150 i = luaL_optint(L, 3, 1);
151 last = luaL_opt(L, luaL_checkint, 4, luaL_getn(L, 1));
152 luaL_buffinit(L, &b);
153 for (; i < last; i++) {
154 addfield(L, &b, i);
155 luaL_addlstring(&b, sep, lsep);
156 }
157 if (i == last) /* add last value (if interval was not empty) */
158 addfield(L, &b, i);
159 luaL_pushresult(&b);
160 return 1;
161}
162
163
164
165/*
166** {======================================================
167** Quicksort
168** (based on `Algorithms in MODULA-3', Robert Sedgewick;
169** Addison-Wesley, 1993.)
170*/
171
172
173static void set2 (lua_State *L, int i, int j) {
174 lua_rawseti(L, 1, i);
175 lua_rawseti(L, 1, j);
176}
177
178static int sort_comp (lua_State *L, int a, int b) {
179 if (!lua_isnil(L, 2)) { /* function? */
180 int res;
181 lua_pushvalue(L, 2);
182 lua_pushvalue(L, a-1); /* -1 to compensate function */
183 lua_pushvalue(L, b-2); /* -2 to compensate function and `a' */
184 lua_call(L, 2, 1);
185 res = lua_toboolean(L, -1);
186 lua_pop(L, 1);
187 return res;
188 }
189 else /* a < b? */
190 return lua_lessthan(L, a, b);
191}
192
193static void auxsort (lua_State *L, int l, int u) {
194 while (l < u) { /* for tail recursion */
195 int i, j;
196 /* sort elements a[l], a[(l+u)/2] and a[u] */
197 lua_rawgeti(L, 1, l);
198 lua_rawgeti(L, 1, u);
199 if (sort_comp(L, -1, -2)) /* a[u] < a[l]? */
200 set2(L, l, u); /* swap a[l] - a[u] */
201 else
202 lua_pop(L, 2);
203 if (u-l == 1) break; /* only 2 elements */
204 i = (l+u)/2;
205 lua_rawgeti(L, 1, i);
206 lua_rawgeti(L, 1, l);
207 if (sort_comp(L, -2, -1)) /* a[i]<a[l]? */
208 set2(L, i, l);
209 else {
210 lua_pop(L, 1); /* remove a[l] */
211 lua_rawgeti(L, 1, u);
212 if (sort_comp(L, -1, -2)) /* a[u]<a[i]? */
213 set2(L, i, u);
214 else
215 lua_pop(L, 2);
216 }
217 if (u-l == 2) break; /* only 3 elements */
218 lua_rawgeti(L, 1, i); /* Pivot */
219 lua_pushvalue(L, -1);
220 lua_rawgeti(L, 1, u-1);
221 set2(L, i, u-1);
222 /* a[l] <= P == a[u-1] <= a[u], only need to sort from l+1 to u-2 */
223 i = l; j = u-1;
224 for (;;) { /* invariant: a[l..i] <= P <= a[j..u] */
225 /* repeat ++i until a[i] >= P */
226 while (lua_rawgeti(L, 1, ++i), sort_comp(L, -1, -2)) {
227 if (i>u) luaL_error(L, "invalid order function for sorting");
228 lua_pop(L, 1); /* remove a[i] */
229 }
230 /* repeat --j until a[j] <= P */
231 while (lua_rawgeti(L, 1, --j), sort_comp(L, -3, -1)) {
232 if (j<l) luaL_error(L, "invalid order function for sorting");
233 lua_pop(L, 1); /* remove a[j] */
234 }
235 if (j<i) {
236 lua_pop(L, 3); /* pop pivot, a[i], a[j] */
237 break;
238 }
239 set2(L, i, j);
240 }
241 lua_rawgeti(L, 1, u-1);
242 lua_rawgeti(L, 1, i);
243 set2(L, u-1, i); /* swap pivot (a[u-1]) with a[i] */
244 /* a[l..i-1] <= a[i] == P <= a[i+1..u] */
245 /* adjust so that smaller half is in [j..i] and larger one in [l..u] */
246 if (i-l < u-i) {
247 j=l; i=i-1; l=i+2;
248 }
249 else {
250 j=i+1; i=u; u=j-2;
251 }
252 auxsort(L, j, i); /* call recursively the smaller one */
253 } /* repeat the routine for the larger one */
254}
255
256static int sort (lua_State *L) {
257 int n = aux_getn(L, 1);
258 luaL_checkstack(L, 40, ""); /* assume array is smaller than 2^40 */
259 if (!lua_isnoneornil(L, 2)) /* is there a 2nd argument? */
260 luaL_checktype(L, 2, LUA_TFUNCTION);
261 lua_settop(L, 2); /* make sure there is two arguments */
262 auxsort(L, 1, n);
263 return 0;
264}
265
266/* }====================================================== */
267
268
269static const luaL_Reg tab_funcs[] = {
270 {"concat", tconcat},
271 {"foreach", foreach},
272 {"foreachi", foreachi},
273 {"getn", getn},
274 {"maxn", maxn},
275 {"insert", tinsert},
276 {"remove", tremove},
277 {"setn", setn},
278 {"sort", sort},
279 {NULL, NULL}
280};
281
282
283LUALIB_API int luaopen_table (lua_State *L) {
284 luaL_register(L, LUA_TABLIBNAME, tab_funcs);
285 return 1;
286}
287
diff --git a/examples/redis-unstable/deps/lua/src/ltm.c b/examples/redis-unstable/deps/lua/src/ltm.c
deleted file mode 100644
index c27f0f6..0000000
--- a/examples/redis-unstable/deps/lua/src/ltm.c
+++ /dev/null
@@ -1,75 +0,0 @@
1/*
2** $Id: ltm.c,v 2.8.1.1 2007/12/27 13:02:25 roberto Exp $
3** Tag methods
4** See Copyright Notice in lua.h
5*/
6
7
8#include <string.h>
9
10#define ltm_c
11#define LUA_CORE
12
13#include "lua.h"
14
15#include "lobject.h"
16#include "lstate.h"
17#include "lstring.h"
18#include "ltable.h"
19#include "ltm.h"
20
21
22
23const char *const luaT_typenames[] = {
24 "nil", "boolean", "userdata", "number",
25 "string", "table", "function", "userdata", "thread",
26 "proto", "upval"
27};
28
29
30void luaT_init (lua_State *L) {
31 static const char *const luaT_eventname[] = { /* ORDER TM */
32 "__index", "__newindex",
33 "__gc", "__mode", "__eq",
34 "__add", "__sub", "__mul", "__div", "__mod",
35 "__pow", "__unm", "__len", "__lt", "__le",
36 "__concat", "__call"
37 };
38 int i;
39 for (i=0; i<TM_N; i++) {
40 G(L)->tmname[i] = luaS_new(L, luaT_eventname[i]);
41 luaS_fix(G(L)->tmname[i]); /* never collect these names */
42 }
43}
44
45
46/*
47** function to be used with macro "fasttm": optimized for absence of
48** tag methods
49*/
50const TValue *luaT_gettm (Table *events, TMS event, TString *ename) {
51 const TValue *tm = luaH_getstr(events, ename);
52 lua_assert(event <= TM_EQ);
53 if (ttisnil(tm)) { /* no tag method? */
54 events->flags |= cast_byte(1u<<event); /* cache this fact */
55 return NULL;
56 }
57 else return tm;
58}
59
60
61const TValue *luaT_gettmbyobj (lua_State *L, const TValue *o, TMS event) {
62 Table *mt;
63 switch (ttype(o)) {
64 case LUA_TTABLE:
65 mt = hvalue(o)->metatable;
66 break;
67 case LUA_TUSERDATA:
68 mt = uvalue(o)->metatable;
69 break;
70 default:
71 mt = G(L)->mt[ttype(o)];
72 }
73 return (mt ? luaH_getstr(mt, G(L)->tmname[event]) : luaO_nilobject);
74}
75
diff --git a/examples/redis-unstable/deps/lua/src/ltm.h b/examples/redis-unstable/deps/lua/src/ltm.h
deleted file mode 100644
index 64343b7..0000000
--- a/examples/redis-unstable/deps/lua/src/ltm.h
+++ /dev/null
@@ -1,54 +0,0 @@
1/*
2** $Id: ltm.h,v 2.6.1.1 2007/12/27 13:02:25 roberto Exp $
3** Tag methods
4** See Copyright Notice in lua.h
5*/
6
7#ifndef ltm_h
8#define ltm_h
9
10
11#include "lobject.h"
12
13
14/*
15* WARNING: if you change the order of this enumeration,
16* grep "ORDER TM"
17*/
18typedef enum {
19 TM_INDEX,
20 TM_NEWINDEX,
21 TM_GC,
22 TM_MODE,
23 TM_EQ, /* last tag method with `fast' access */
24 TM_ADD,
25 TM_SUB,
26 TM_MUL,
27 TM_DIV,
28 TM_MOD,
29 TM_POW,
30 TM_UNM,
31 TM_LEN,
32 TM_LT,
33 TM_LE,
34 TM_CONCAT,
35 TM_CALL,
36 TM_N /* number of elements in the enum */
37} TMS;
38
39
40
41#define gfasttm(g,et,e) ((et) == NULL ? NULL : \
42 ((et)->flags & (1u<<(e))) ? NULL : luaT_gettm(et, e, (g)->tmname[e]))
43
44#define fasttm(l,et,e) gfasttm(G(l), et, e)
45
46LUAI_DATA const char *const luaT_typenames[];
47
48
49LUAI_FUNC const TValue *luaT_gettm (Table *events, TMS event, TString *ename);
50LUAI_FUNC const TValue *luaT_gettmbyobj (lua_State *L, const TValue *o,
51 TMS event);
52LUAI_FUNC void luaT_init (lua_State *L);
53
54#endif
diff --git a/examples/redis-unstable/deps/lua/src/lua.c b/examples/redis-unstable/deps/lua/src/lua.c
deleted file mode 100644
index 3a46609..0000000
--- a/examples/redis-unstable/deps/lua/src/lua.c
+++ /dev/null
@@ -1,392 +0,0 @@
1/*
2** $Id: lua.c,v 1.160.1.2 2007/12/28 15:32:23 roberto Exp $
3** Lua stand-alone interpreter
4** See Copyright Notice in lua.h
5*/
6
7
8#include <signal.h>
9#include <stdio.h>
10#include <stdlib.h>
11#include <string.h>
12
13#define lua_c
14
15#include "lua.h"
16
17#include "lauxlib.h"
18#include "lualib.h"
19
20
21
22static lua_State *globalL = NULL;
23
24static const char *progname = LUA_PROGNAME;
25
26
27
28static void lstop (lua_State *L, lua_Debug *ar) {
29 (void)ar; /* unused arg. */
30 lua_sethook(L, NULL, 0, 0);
31 luaL_error(L, "interrupted!");
32}
33
34
35static void laction (int i) {
36 signal(i, SIG_DFL); /* if another SIGINT happens before lstop,
37 terminate process (default action) */
38 lua_sethook(globalL, lstop, LUA_MASKCALL | LUA_MASKRET | LUA_MASKCOUNT, 1);
39}
40
41
42static void print_usage (void) {
43 fprintf(stderr,
44 "usage: %s [options] [script [args]].\n"
45 "Available options are:\n"
46 " -e stat execute string " LUA_QL("stat") "\n"
47 " -l name require library " LUA_QL("name") "\n"
48 " -i enter interactive mode after executing " LUA_QL("script") "\n"
49 " -v show version information\n"
50 " -- stop handling options\n"
51 " - execute stdin and stop handling options\n"
52 ,
53 progname);
54 fflush(stderr);
55}
56
57
58static void l_message (const char *pname, const char *msg) {
59 if (pname) fprintf(stderr, "%s: ", pname);
60 fprintf(stderr, "%s\n", msg);
61 fflush(stderr);
62}
63
64
65static int report (lua_State *L, int status) {
66 if (status && !lua_isnil(L, -1)) {
67 const char *msg = lua_tostring(L, -1);
68 if (msg == NULL) msg = "(error object is not a string)";
69 l_message(progname, msg);
70 lua_pop(L, 1);
71 }
72 return status;
73}
74
75
76static int traceback (lua_State *L) {
77 if (!lua_isstring(L, 1)) /* 'message' not a string? */
78 return 1; /* keep it intact */
79 lua_getfield(L, LUA_GLOBALSINDEX, "debug");
80 if (!lua_istable(L, -1)) {
81 lua_pop(L, 1);
82 return 1;
83 }
84 lua_getfield(L, -1, "traceback");
85 if (!lua_isfunction(L, -1)) {
86 lua_pop(L, 2);
87 return 1;
88 }
89 lua_pushvalue(L, 1); /* pass error message */
90 lua_pushinteger(L, 2); /* skip this function and traceback */
91 lua_call(L, 2, 1); /* call debug.traceback */
92 return 1;
93}
94
95
96static int docall (lua_State *L, int narg, int clear) {
97 int status;
98 int base = lua_gettop(L) - narg; /* function index */
99 lua_pushcfunction(L, traceback); /* push traceback function */
100 lua_insert(L, base); /* put it under chunk and args */
101 signal(SIGINT, laction);
102 status = lua_pcall(L, narg, (clear ? 0 : LUA_MULTRET), base);
103 signal(SIGINT, SIG_DFL);
104 lua_remove(L, base); /* remove traceback function */
105 /* force a complete garbage collection in case of errors */
106 if (status != 0) lua_gc(L, LUA_GCCOLLECT, 0);
107 return status;
108}
109
110
111static void print_version (void) {
112 l_message(NULL, LUA_RELEASE " " LUA_COPYRIGHT);
113}
114
115
116static int getargs (lua_State *L, char **argv, int n) {
117 int narg;
118 int i;
119 int argc = 0;
120 while (argv[argc]) argc++; /* count total number of arguments */
121 narg = argc - (n + 1); /* number of arguments to the script */
122 luaL_checkstack(L, narg + 3, "too many arguments to script");
123 for (i=n+1; i < argc; i++)
124 lua_pushstring(L, argv[i]);
125 lua_createtable(L, narg, n + 1);
126 for (i=0; i < argc; i++) {
127 lua_pushstring(L, argv[i]);
128 lua_rawseti(L, -2, i - n);
129 }
130 return narg;
131}
132
133
134static int dofile (lua_State *L, const char *name) {
135 int status = luaL_loadfile(L, name) || docall(L, 0, 1);
136 return report(L, status);
137}
138
139
140static int dostring (lua_State *L, const char *s, const char *name) {
141 int status = luaL_loadbuffer(L, s, strlen(s), name) || docall(L, 0, 1);
142 return report(L, status);
143}
144
145
146static int dolibrary (lua_State *L, const char *name) {
147 lua_getglobal(L, "require");
148 lua_pushstring(L, name);
149 return report(L, docall(L, 1, 1));
150}
151
152
153static const char *get_prompt (lua_State *L, int firstline) {
154 const char *p;
155 lua_getfield(L, LUA_GLOBALSINDEX, firstline ? "_PROMPT" : "_PROMPT2");
156 p = lua_tostring(L, -1);
157 if (p == NULL) p = (firstline ? LUA_PROMPT : LUA_PROMPT2);
158 lua_pop(L, 1); /* remove global */
159 return p;
160}
161
162
163static int incomplete (lua_State *L, int status) {
164 if (status == LUA_ERRSYNTAX) {
165 size_t lmsg;
166 const char *msg = lua_tolstring(L, -1, &lmsg);
167 const char *tp = msg + lmsg - (sizeof(LUA_QL("<eof>")) - 1);
168 if (strstr(msg, LUA_QL("<eof>")) == tp) {
169 lua_pop(L, 1);
170 return 1;
171 }
172 }
173 return 0; /* else... */
174}
175
176
177static int pushline (lua_State *L, int firstline) {
178 char buffer[LUA_MAXINPUT];
179 char *b = buffer;
180 size_t l;
181 const char *prmt = get_prompt(L, firstline);
182 if (lua_readline(L, b, prmt) == 0)
183 return 0; /* no input */
184 l = strlen(b);
185 if (l > 0 && b[l-1] == '\n') /* line ends with newline? */
186 b[l-1] = '\0'; /* remove it */
187 if (firstline && b[0] == '=') /* first line starts with `=' ? */
188 lua_pushfstring(L, "return %s", b+1); /* change it to `return' */
189 else
190 lua_pushstring(L, b);
191 lua_freeline(L, b);
192 return 1;
193}
194
195
196static int loadline (lua_State *L) {
197 int status;
198 lua_settop(L, 0);
199 if (!pushline(L, 1))
200 return -1; /* no input */
201 for (;;) { /* repeat until gets a complete line */
202 status = luaL_loadbuffer(L, lua_tostring(L, 1), lua_strlen(L, 1), "=stdin");
203 if (!incomplete(L, status)) break; /* cannot try to add lines? */
204 if (!pushline(L, 0)) /* no more input? */
205 return -1;
206 lua_pushliteral(L, "\n"); /* add a new line... */
207 lua_insert(L, -2); /* ...between the two lines */
208 lua_concat(L, 3); /* join them */
209 }
210 lua_saveline(L, 1);
211 lua_remove(L, 1); /* remove line */
212 return status;
213}
214
215
216static void dotty (lua_State *L) {
217 int status;
218 const char *oldprogname = progname;
219 progname = NULL;
220 while ((status = loadline(L)) != -1) {
221 if (status == 0) status = docall(L, 0, 0);
222 report(L, status);
223 if (status == 0 && lua_gettop(L) > 0) { /* any result to print? */
224 lua_getglobal(L, "print");
225 lua_insert(L, 1);
226 if (lua_pcall(L, lua_gettop(L)-1, 0, 0) != 0)
227 l_message(progname, lua_pushfstring(L,
228 "error calling " LUA_QL("print") " (%s)",
229 lua_tostring(L, -1)));
230 }
231 }
232 lua_settop(L, 0); /* clear stack */
233 fputs("\n", stdout);
234 fflush(stdout);
235 progname = oldprogname;
236}
237
238
239static int handle_script (lua_State *L, char **argv, int n) {
240 int status;
241 const char *fname;
242 int narg = getargs(L, argv, n); /* collect arguments */
243 lua_setglobal(L, "arg");
244 fname = argv[n];
245 if (strcmp(fname, "-") == 0 && strcmp(argv[n-1], "--") != 0)
246 fname = NULL; /* stdin */
247 status = luaL_loadfile(L, fname);
248 lua_insert(L, -(narg+1));
249 if (status == 0)
250 status = docall(L, narg, 0);
251 else
252 lua_pop(L, narg);
253 return report(L, status);
254}
255
256
257/* check that argument has no extra characters at the end */
258#define notail(x) {if ((x)[2] != '\0') return -1;}
259
260
261static int collectargs (char **argv, int *pi, int *pv, int *pe) {
262 int i;
263 for (i = 1; argv[i] != NULL; i++) {
264 if (argv[i][0] != '-') /* not an option? */
265 return i;
266 switch (argv[i][1]) { /* option */
267 case '-':
268 notail(argv[i]);
269 return (argv[i+1] != NULL ? i+1 : 0);
270 case '\0':
271 return i;
272 case 'i':
273 notail(argv[i]);
274 *pi = 1; /* go through */
275 case 'v':
276 notail(argv[i]);
277 *pv = 1;
278 break;
279 case 'e':
280 *pe = 1; /* go through */
281 case 'l':
282 if (argv[i][2] == '\0') {
283 i++;
284 if (argv[i] == NULL) return -1;
285 }
286 break;
287 default: return -1; /* invalid option */
288 }
289 }
290 return 0;
291}
292
293
294static int runargs (lua_State *L, char **argv, int n) {
295 int i;
296 for (i = 1; i < n; i++) {
297 if (argv[i] == NULL) continue;
298 lua_assert(argv[i][0] == '-');
299 switch (argv[i][1]) { /* option */
300 case 'e': {
301 const char *chunk = argv[i] + 2;
302 if (*chunk == '\0') chunk = argv[++i];
303 lua_assert(chunk != NULL);
304 if (dostring(L, chunk, "=(command line)") != 0)
305 return 1;
306 break;
307 }
308 case 'l': {
309 const char *filename = argv[i] + 2;
310 if (*filename == '\0') filename = argv[++i];
311 lua_assert(filename != NULL);
312 if (dolibrary(L, filename))
313 return 1; /* stop if file fails */
314 break;
315 }
316 default: break;
317 }
318 }
319 return 0;
320}
321
322
323static int handle_luainit (lua_State *L) {
324 const char *init = getenv(LUA_INIT);
325 if (init == NULL) return 0; /* status OK */
326 else if (init[0] == '@')
327 return dofile(L, init+1);
328 else
329 return dostring(L, init, "=" LUA_INIT);
330}
331
332
333struct Smain {
334 int argc;
335 char **argv;
336 int status;
337};
338
339
340static int pmain (lua_State *L) {
341 struct Smain *s = (struct Smain *)lua_touserdata(L, 1);
342 char **argv = s->argv;
343 int script;
344 int has_i = 0, has_v = 0, has_e = 0;
345 globalL = L;
346 if (argv[0] && argv[0][0]) progname = argv[0];
347 lua_gc(L, LUA_GCSTOP, 0); /* stop collector during initialization */
348 luaL_openlibs(L); /* open libraries */
349 lua_gc(L, LUA_GCRESTART, 0);
350 s->status = handle_luainit(L);
351 if (s->status != 0) return 0;
352 script = collectargs(argv, &has_i, &has_v, &has_e);
353 if (script < 0) { /* invalid args? */
354 print_usage();
355 s->status = 1;
356 return 0;
357 }
358 if (has_v) print_version();
359 s->status = runargs(L, argv, (script > 0) ? script : s->argc);
360 if (s->status != 0) return 0;
361 if (script)
362 s->status = handle_script(L, argv, script);
363 if (s->status != 0) return 0;
364 if (has_i)
365 dotty(L);
366 else if (script == 0 && !has_e && !has_v) {
367 if (lua_stdin_is_tty()) {
368 print_version();
369 dotty(L);
370 }
371 else dofile(L, NULL); /* executes stdin as a file */
372 }
373 return 0;
374}
375
376
377int main (int argc, char **argv) {
378 int status;
379 struct Smain s;
380 lua_State *L = lua_open(); /* create state */
381 if (L == NULL) {
382 l_message(argv[0], "cannot create state: not enough memory");
383 return EXIT_FAILURE;
384 }
385 s.argc = argc;
386 s.argv = argv;
387 status = lua_cpcall(L, &pmain, &s);
388 report(L, status);
389 lua_close(L);
390 return (status || s.status) ? EXIT_FAILURE : EXIT_SUCCESS;
391}
392
diff --git a/examples/redis-unstable/deps/lua/src/lua.h b/examples/redis-unstable/deps/lua/src/lua.h
deleted file mode 100644
index 280ef23..0000000
--- a/examples/redis-unstable/deps/lua/src/lua.h
+++ /dev/null
@@ -1,391 +0,0 @@
1/*
2** $Id: lua.h,v 1.218.1.7 2012/01/13 20:36:20 roberto Exp $
3** Lua - An Extensible Extension Language
4** Lua.org, PUC-Rio, Brazil (http://www.lua.org)
5** See Copyright Notice at the end of this file
6*/
7
8
9#ifndef lua_h
10#define lua_h
11
12#include <stdarg.h>
13#include <stddef.h>
14
15
16#include "luaconf.h"
17
18
19#define LUA_VERSION "Lua 5.1"
20#define LUA_RELEASE "Lua 5.1.5"
21#define LUA_VERSION_NUM 501
22#define LUA_COPYRIGHT "Copyright (C) 1994-2012 Lua.org, PUC-Rio"
23#define LUA_AUTHORS "R. Ierusalimschy, L. H. de Figueiredo & W. Celes"
24
25
26/* mark for precompiled code (`<esc>Lua') */
27#define LUA_SIGNATURE "\033Lua"
28
29/* option for multiple returns in `lua_pcall' and `lua_call' */
30#define LUA_MULTRET (-1)
31
32
33/*
34** pseudo-indices
35*/
36#define LUA_REGISTRYINDEX (-10000)
37#define LUA_ENVIRONINDEX (-10001)
38#define LUA_GLOBALSINDEX (-10002)
39#define lua_upvalueindex(i) (LUA_GLOBALSINDEX-(i))
40
41
42/* thread status; 0 is OK */
43#define LUA_YIELD 1
44#define LUA_ERRRUN 2
45#define LUA_ERRSYNTAX 3
46#define LUA_ERRMEM 4
47#define LUA_ERRERR 5
48
49
50typedef struct lua_State lua_State;
51
52typedef int (*lua_CFunction) (lua_State *L);
53
54
55/*
56** functions that read/write blocks when loading/dumping Lua chunks
57*/
58typedef const char * (*lua_Reader) (lua_State *L, void *ud, size_t *sz);
59
60typedef int (*lua_Writer) (lua_State *L, const void* p, size_t sz, void* ud);
61
62
63/*
64** prototype for memory-allocation functions
65*/
66typedef void * (*lua_Alloc) (void *ud, void *ptr, size_t osize, size_t nsize);
67
68
69/*
70** basic types
71*/
72#define LUA_TNONE (-1)
73
74#define LUA_TNIL 0
75#define LUA_TBOOLEAN 1
76#define LUA_TLIGHTUSERDATA 2
77#define LUA_TNUMBER 3
78#define LUA_TSTRING 4
79#define LUA_TTABLE 5
80#define LUA_TFUNCTION 6
81#define LUA_TUSERDATA 7
82#define LUA_TTHREAD 8
83
84
85
86/* minimum Lua stack available to a C function */
87#define LUA_MINSTACK 20
88
89
90/*
91** generic extra include file
92*/
93#if defined(LUA_USER_H)
94#include LUA_USER_H
95#endif
96
97
98/* type of numbers in Lua */
99typedef LUA_NUMBER lua_Number;
100
101
102/* type for integer functions */
103typedef LUA_INTEGER lua_Integer;
104
105
106
107/*
108** state manipulation
109*/
110LUA_API lua_State *(lua_newstate) (lua_Alloc f, void *ud);
111LUA_API void (lua_close) (lua_State *L);
112LUA_API lua_State *(lua_newthread) (lua_State *L);
113
114LUA_API lua_CFunction (lua_atpanic) (lua_State *L, lua_CFunction panicf);
115
116
117/*
118** basic stack manipulation
119*/
120LUA_API int (lua_gettop) (lua_State *L);
121LUA_API void (lua_settop) (lua_State *L, int idx);
122LUA_API void (lua_pushvalue) (lua_State *L, int idx);
123LUA_API void (lua_remove) (lua_State *L, int idx);
124LUA_API void (lua_insert) (lua_State *L, int idx);
125LUA_API void (lua_replace) (lua_State *L, int idx);
126LUA_API int (lua_checkstack) (lua_State *L, int sz);
127
128LUA_API void (lua_xmove) (lua_State *from, lua_State *to, int n);
129
130
131/*
132** access functions (stack -> C)
133*/
134
135LUA_API int (lua_isnumber) (lua_State *L, int idx);
136LUA_API int (lua_isstring) (lua_State *L, int idx);
137LUA_API int (lua_iscfunction) (lua_State *L, int idx);
138LUA_API int (lua_isuserdata) (lua_State *L, int idx);
139LUA_API int (lua_type) (lua_State *L, int idx);
140LUA_API const char *(lua_typename) (lua_State *L, int tp);
141
142LUA_API int (lua_equal) (lua_State *L, int idx1, int idx2);
143LUA_API int (lua_rawequal) (lua_State *L, int idx1, int idx2);
144LUA_API int (lua_lessthan) (lua_State *L, int idx1, int idx2);
145
146LUA_API lua_Number (lua_tonumber) (lua_State *L, int idx);
147LUA_API lua_Integer (lua_tointeger) (lua_State *L, int idx);
148LUA_API int (lua_toboolean) (lua_State *L, int idx);
149LUA_API const char *(lua_tolstring) (lua_State *L, int idx, size_t *len);
150LUA_API size_t (lua_objlen) (lua_State *L, int idx);
151LUA_API lua_CFunction (lua_tocfunction) (lua_State *L, int idx);
152LUA_API void *(lua_touserdata) (lua_State *L, int idx);
153LUA_API lua_State *(lua_tothread) (lua_State *L, int idx);
154LUA_API const void *(lua_topointer) (lua_State *L, int idx);
155
156
157/*
158** push functions (C -> stack)
159*/
160LUA_API void (lua_pushnil) (lua_State *L);
161LUA_API void (lua_pushnumber) (lua_State *L, lua_Number n);
162LUA_API void (lua_pushinteger) (lua_State *L, lua_Integer n);
163LUA_API void (lua_pushlstring) (lua_State *L, const char *s, size_t l);
164LUA_API void (lua_pushstring) (lua_State *L, const char *s);
165LUA_API const char *(lua_pushvfstring) (lua_State *L, const char *fmt,
166 va_list argp);
167LUA_API const char *(lua_pushfstring) (lua_State *L, const char *fmt, ...);
168LUA_API void (lua_pushcclosure) (lua_State *L, lua_CFunction fn, int n);
169LUA_API void (lua_pushboolean) (lua_State *L, int b);
170LUA_API void (lua_pushlightuserdata) (lua_State *L, void *p);
171LUA_API int (lua_pushthread) (lua_State *L);
172
173
174/*
175** get functions (Lua -> stack)
176*/
177LUA_API void (lua_gettable) (lua_State *L, int idx);
178LUA_API void (lua_getfield) (lua_State *L, int idx, const char *k);
179LUA_API void (lua_rawget) (lua_State *L, int idx);
180LUA_API void (lua_rawgeti) (lua_State *L, int idx, int n);
181LUA_API void (lua_createtable) (lua_State *L, int narr, int nrec);
182LUA_API void *(lua_newuserdata) (lua_State *L, size_t sz);
183LUA_API int (lua_getmetatable) (lua_State *L, int objindex);
184LUA_API void (lua_getfenv) (lua_State *L, int idx);
185
186
187/*
188** set functions (stack -> Lua)
189*/
190LUA_API void (lua_settable) (lua_State *L, int idx);
191LUA_API void (lua_setfield) (lua_State *L, int idx, const char *k);
192LUA_API void (lua_rawset) (lua_State *L, int idx);
193LUA_API void (lua_rawseti) (lua_State *L, int idx, int n);
194LUA_API int (lua_setmetatable) (lua_State *L, int objindex);
195LUA_API int (lua_setfenv) (lua_State *L, int idx);
196
197
198/*
199** `load' and `call' functions (load and run Lua code)
200*/
201LUA_API void (lua_call) (lua_State *L, int nargs, int nresults);
202LUA_API int (lua_pcall) (lua_State *L, int nargs, int nresults, int errfunc);
203LUA_API int (lua_cpcall) (lua_State *L, lua_CFunction func, void *ud);
204LUA_API int (lua_load) (lua_State *L, lua_Reader reader, void *dt,
205 const char *chunkname);
206
207LUA_API int (lua_dump) (lua_State *L, lua_Writer writer, void *data);
208
209
210/*
211** coroutine functions
212*/
213LUA_API int (lua_yield) (lua_State *L, int nresults);
214LUA_API int (lua_resume) (lua_State *L, int narg);
215LUA_API int (lua_status) (lua_State *L);
216
217/*
218** garbage-collection function and options
219*/
220
221#define LUA_GCSTOP 0
222#define LUA_GCRESTART 1
223#define LUA_GCCOLLECT 2
224#define LUA_GCCOUNT 3
225#define LUA_GCCOUNTB 4
226#define LUA_GCSTEP 5
227#define LUA_GCSETPAUSE 6
228#define LUA_GCSETSTEPMUL 7
229
230LUA_API int (lua_gc) (lua_State *L, int what, int data);
231
232
233/*
234** miscellaneous functions
235*/
236
237LUA_API int (lua_error) (lua_State *L);
238
239LUA_API int (lua_next) (lua_State *L, int idx);
240
241LUA_API void (lua_concat) (lua_State *L, int n);
242
243LUA_API lua_Alloc (lua_getallocf) (lua_State *L, void **ud);
244LUA_API void lua_setallocf (lua_State *L, lua_Alloc f, void *ud);
245
246
247
248/*
249** ===============================================================
250** some useful macros
251** ===============================================================
252*/
253
254#define lua_pop(L,n) lua_settop(L, -(n)-1)
255
256#define lua_newtable(L) lua_createtable(L, 0, 0)
257
258#define lua_register(L,n,f) (lua_pushcfunction(L, (f)), lua_setglobal(L, (n)))
259
260#define lua_pushcfunction(L,f) lua_pushcclosure(L, (f), 0)
261
262#define lua_strlen(L,i) lua_objlen(L, (i))
263
264#define lua_isfunction(L,n) (lua_type(L, (n)) == LUA_TFUNCTION)
265#define lua_istable(L,n) (lua_type(L, (n)) == LUA_TTABLE)
266#define lua_islightuserdata(L,n) (lua_type(L, (n)) == LUA_TLIGHTUSERDATA)
267#define lua_isnil(L,n) (lua_type(L, (n)) == LUA_TNIL)
268#define lua_isboolean(L,n) (lua_type(L, (n)) == LUA_TBOOLEAN)
269#define lua_isthread(L,n) (lua_type(L, (n)) == LUA_TTHREAD)
270#define lua_isnone(L,n) (lua_type(L, (n)) == LUA_TNONE)
271#define lua_isnoneornil(L, n) (lua_type(L, (n)) <= 0)
272
273#define lua_pushliteral(L, s) \
274 lua_pushlstring(L, "" s, (sizeof(s)/sizeof(char))-1)
275
276#define lua_setglobal(L,s) lua_setfield(L, LUA_GLOBALSINDEX, (s))
277#define lua_getglobal(L,s) lua_getfield(L, LUA_GLOBALSINDEX, (s))
278
279#define lua_tostring(L,i) lua_tolstring(L, (i), NULL)
280
281
282
283/*
284** compatibility macros and functions
285*/
286
287#define lua_open() luaL_newstate()
288
289#define lua_getregistry(L) lua_pushvalue(L, LUA_REGISTRYINDEX)
290
291#define lua_getgccount(L) lua_gc(L, LUA_GCCOUNT, 0)
292
293#define lua_Chunkreader lua_Reader
294#define lua_Chunkwriter lua_Writer
295
296
297/* hack */
298LUA_API void lua_setlevel (lua_State *from, lua_State *to);
299
300
301/*
302** {======================================================================
303** Debug API
304** =======================================================================
305*/
306
307
308/*
309** Event codes
310*/
311#define LUA_HOOKCALL 0
312#define LUA_HOOKRET 1
313#define LUA_HOOKLINE 2
314#define LUA_HOOKCOUNT 3
315#define LUA_HOOKTAILRET 4
316
317
318/*
319** Event masks
320*/
321#define LUA_MASKCALL (1 << LUA_HOOKCALL)
322#define LUA_MASKRET (1 << LUA_HOOKRET)
323#define LUA_MASKLINE (1 << LUA_HOOKLINE)
324#define LUA_MASKCOUNT (1 << LUA_HOOKCOUNT)
325
326typedef struct lua_Debug lua_Debug; /* activation record */
327
328
329/* Functions to be called by the debuger in specific events */
330typedef void (*lua_Hook) (lua_State *L, lua_Debug *ar);
331
332
333LUA_API int lua_getstack (lua_State *L, int level, lua_Debug *ar);
334LUA_API int lua_getinfo (lua_State *L, const char *what, lua_Debug *ar);
335LUA_API const char *lua_getlocal (lua_State *L, const lua_Debug *ar, int n);
336LUA_API const char *lua_setlocal (lua_State *L, const lua_Debug *ar, int n);
337LUA_API const char *lua_getupvalue (lua_State *L, int funcindex, int n);
338LUA_API const char *lua_setupvalue (lua_State *L, int funcindex, int n);
339
340LUA_API int lua_sethook (lua_State *L, lua_Hook func, int mask, int count);
341LUA_API lua_Hook lua_gethook (lua_State *L);
342LUA_API int lua_gethookmask (lua_State *L);
343LUA_API int lua_gethookcount (lua_State *L);
344
345
346struct lua_Debug {
347 int event;
348 const char *name; /* (n) */
349 const char *namewhat; /* (n) `global', `local', `field', `method' */
350 const char *what; /* (S) `Lua', `C', `main', `tail' */
351 const char *source; /* (S) */
352 int currentline; /* (l) */
353 int nups; /* (u) number of upvalues */
354 int linedefined; /* (S) */
355 int lastlinedefined; /* (S) */
356 char short_src[LUA_IDSIZE]; /* (S) */
357 /* private part */
358 int i_ci; /* active function */
359};
360
361LUA_API void lua_enablereadonlytable (lua_State *L, int index, int enabled);
362LUA_API int lua_isreadonlytable (lua_State *L, int index);
363
364/* }====================================================================== */
365
366
367/******************************************************************************
368* Copyright (C) 1994-2012 Lua.org, PUC-Rio. All rights reserved.
369*
370* Permission is hereby granted, free of charge, to any person obtaining
371* a copy of this software and associated documentation files (the
372* "Software"), to deal in the Software without restriction, including
373* without limitation the rights to use, copy, modify, merge, publish,
374* distribute, sublicense, and/or sell copies of the Software, and to
375* permit persons to whom the Software is furnished to do so, subject to
376* the following conditions:
377*
378* The above copyright notice and this permission notice shall be
379* included in all copies or substantial portions of the Software.
380*
381* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
382* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
383* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
384* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
385* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
386* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
387* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
388******************************************************************************/
389
390
391#endif
diff --git a/examples/redis-unstable/deps/lua/src/lua_bit.c b/examples/redis-unstable/deps/lua/src/lua_bit.c
deleted file mode 100644
index 7e43fae..0000000
--- a/examples/redis-unstable/deps/lua/src/lua_bit.c
+++ /dev/null
@@ -1,191 +0,0 @@
1/*
2** Lua BitOp -- a bit operations library for Lua 5.1/5.2.
3** http://bitop.luajit.org/
4**
5** Copyright (C) 2008-2012 Mike Pall. All rights reserved.
6**
7** Permission is hereby granted, free of charge, to any person obtaining
8** a copy of this software and associated documentation files (the
9** "Software"), to deal in the Software without restriction, including
10** without limitation the rights to use, copy, modify, merge, publish,
11** distribute, sublicense, and/or sell copies of the Software, and to
12** permit persons to whom the Software is furnished to do so, subject to
13** the following conditions:
14**
15** The above copyright notice and this permission notice shall be
16** included in all copies or substantial portions of the Software.
17**
18** THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
19** EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
20** MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
21** IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
22** CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
23** TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
24** SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
25**
26** [ MIT license: http://www.opensource.org/licenses/mit-license.php ]
27*/
28
29#define LUA_BITOP_VERSION "1.0.2"
30
31#define LUA_LIB
32#include "lua.h"
33#include "lauxlib.h"
34
35#ifdef _MSC_VER
36/* MSVC is stuck in the last century and doesn't have C99's stdint.h. */
37typedef __int32 int32_t;
38typedef unsigned __int32 uint32_t;
39typedef unsigned __int64 uint64_t;
40#else
41#include <stdint.h>
42#endif
43
44typedef int32_t SBits;
45typedef uint32_t UBits;
46
47typedef union {
48 lua_Number n;
49#ifdef LUA_NUMBER_DOUBLE
50 uint64_t b;
51#else
52 UBits b;
53#endif
54} BitNum;
55
56/* Convert argument to bit type. */
57static UBits barg(lua_State *L, int idx)
58{
59 BitNum bn;
60 UBits b;
61#if LUA_VERSION_NUM < 502
62 bn.n = lua_tonumber(L, idx);
63#else
64 bn.n = luaL_checknumber(L, idx);
65#endif
66#if defined(LUA_NUMBER_DOUBLE)
67 bn.n += 6755399441055744.0; /* 2^52+2^51 */
68#ifdef SWAPPED_DOUBLE
69 b = (UBits)(bn.b >> 32);
70#else
71 b = (UBits)bn.b;
72#endif
73#elif defined(LUA_NUMBER_INT) || defined(LUA_NUMBER_LONG) || \
74 defined(LUA_NUMBER_LONGLONG) || defined(LUA_NUMBER_LONG_LONG) || \
75 defined(LUA_NUMBER_LLONG)
76 if (sizeof(UBits) == sizeof(lua_Number))
77 b = bn.b;
78 else
79 b = (UBits)(SBits)bn.n;
80#elif defined(LUA_NUMBER_FLOAT)
81#error "A 'float' lua_Number type is incompatible with this library"
82#else
83#error "Unknown number type, check LUA_NUMBER_* in luaconf.h"
84#endif
85#if LUA_VERSION_NUM < 502
86 if (b == 0 && !lua_isnumber(L, idx)) {
87 luaL_typerror(L, idx, "number");
88 }
89#endif
90 return b;
91}
92
93/* Return bit type. */
94#define BRET(b) lua_pushnumber(L, (lua_Number)(SBits)(b)); return 1;
95
96static int bit_tobit(lua_State *L) { BRET(barg(L, 1)) }
97static int bit_bnot(lua_State *L) { BRET(~barg(L, 1)) }
98
99#define BIT_OP(func, opr) \
100 static int func(lua_State *L) { int i; UBits b = barg(L, 1); \
101 for (i = lua_gettop(L); i > 1; i--) b opr barg(L, i); \
102 BRET(b) }
103BIT_OP(bit_band, &=)
104BIT_OP(bit_bor, |=)
105BIT_OP(bit_bxor, ^=)
106
107#define bshl(b, n) (b << n)
108#define bshr(b, n) (b >> n)
109#define bsar(b, n) ((SBits)b >> n)
110#define brol(b, n) ((b << n) | (b >> (32-n)))
111#define bror(b, n) ((b << (32-n)) | (b >> n))
112#define BIT_SH(func, fn) \
113 static int func(lua_State *L) { \
114 UBits b = barg(L, 1); UBits n = barg(L, 2) & 31; BRET(fn(b, n)) }
115BIT_SH(bit_lshift, bshl)
116BIT_SH(bit_rshift, bshr)
117BIT_SH(bit_arshift, bsar)
118BIT_SH(bit_rol, brol)
119BIT_SH(bit_ror, bror)
120
121static int bit_bswap(lua_State *L)
122{
123 UBits b = barg(L, 1);
124 b = (b >> 24) | ((b >> 8) & 0xff00) | ((b & 0xff00) << 8) | (b << 24);
125 BRET(b)
126}
127
128static int bit_tohex(lua_State *L)
129{
130 UBits b = barg(L, 1);
131 SBits n = lua_isnone(L, 2) ? 8 : (SBits)barg(L, 2);
132 const char *hexdigits = "0123456789abcdef";
133 char buf[8];
134 int i;
135 if (n == INT32_MIN) n = INT32_MIN+1;
136 if (n < 0) { n = -n; hexdigits = "0123456789ABCDEF"; }
137 if (n > 8) n = 8;
138 for (i = (int)n; --i >= 0; ) { buf[i] = hexdigits[b & 15]; b >>= 4; }
139 lua_pushlstring(L, buf, (size_t)n);
140 return 1;
141}
142
143static const struct luaL_Reg bit_funcs[] = {
144 { "tobit", bit_tobit },
145 { "bnot", bit_bnot },
146 { "band", bit_band },
147 { "bor", bit_bor },
148 { "bxor", bit_bxor },
149 { "lshift", bit_lshift },
150 { "rshift", bit_rshift },
151 { "arshift", bit_arshift },
152 { "rol", bit_rol },
153 { "ror", bit_ror },
154 { "bswap", bit_bswap },
155 { "tohex", bit_tohex },
156 { NULL, NULL }
157};
158
159/* Signed right-shifts are implementation-defined per C89/C99.
160** But the de facto standard are arithmetic right-shifts on two's
161** complement CPUs. This behaviour is required here, so test for it.
162*/
163#define BAD_SAR (bsar(-8, 2) != (SBits)-2)
164
165LUALIB_API int luaopen_bit(lua_State *L)
166{
167 UBits b;
168 lua_pushnumber(L, (lua_Number)1437217655L);
169 b = barg(L, -1);
170 if (b != (UBits)1437217655L || BAD_SAR) { /* Perform a simple self-test. */
171 const char *msg = "compiled with incompatible luaconf.h";
172#ifdef LUA_NUMBER_DOUBLE
173#ifdef _WIN32
174 if (b == (UBits)1610612736L)
175 msg = "use D3DCREATE_FPU_PRESERVE with DirectX";
176#endif
177 if (b == (UBits)1127743488L)
178 msg = "not compiled with SWAPPED_DOUBLE";
179#endif
180 if (BAD_SAR)
181 msg = "arithmetic right-shift broken";
182 luaL_error(L, "bit library self-test failed (%s)", msg);
183 }
184#if LUA_VERSION_NUM < 502
185 luaL_register(L, "bit", bit_funcs);
186#else
187 luaL_newlib(L, bit_funcs);
188#endif
189 return 1;
190}
191
diff --git a/examples/redis-unstable/deps/lua/src/lua_cjson.c b/examples/redis-unstable/deps/lua/src/lua_cjson.c
deleted file mode 100644
index 703e3e7..0000000
--- a/examples/redis-unstable/deps/lua/src/lua_cjson.c
+++ /dev/null
@@ -1,1471 +0,0 @@
1/* Lua CJSON - JSON support for Lua
2 *
3 * Copyright (c) 2010-2012 Mark Pulford <mark@kyne.com.au>
4 *
5 * Permission is hereby granted, free of charge, to any person obtaining
6 * a copy of this software and associated documentation files (the
7 * "Software"), to deal in the Software without restriction, including
8 * without limitation the rights to use, copy, modify, merge, publish,
9 * distribute, sublicense, and/or sell copies of the Software, and to
10 * permit persons to whom the Software is furnished to do so, subject to
11 * the following conditions:
12 *
13 * The above copyright notice and this permission notice shall be
14 * included in all copies or substantial portions of the Software.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
19 * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
20 * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
21 * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
22 * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
23 */
24
25/* Caveats:
26 * - JSON "null" values are represented as lightuserdata since Lua
27 * tables cannot contain "nil". Compare with cjson.null.
28 * - Invalid UTF-8 characters are not detected and will be passed
29 * untouched. If required, UTF-8 error checking should be done
30 * outside this library.
31 * - Javascript comments are not part of the JSON spec, and are not
32 * currently supported.
33 *
34 * Note: Decoding is slower than encoding. Lua spends significant
35 * time (30%) managing tables when parsing JSON since it is
36 * difficult to know object/array sizes ahead of time.
37 */
38
39#include <assert.h>
40#include <string.h>
41#include <math.h>
42#include <stdint.h>
43#include <limits.h>
44#include "lua.h"
45#include "lauxlib.h"
46
47#include "strbuf.h"
48#include "fpconv.h"
49
50#include "../../../src/solarisfixes.h"
51
52#ifndef CJSON_MODNAME
53#define CJSON_MODNAME "cjson"
54#endif
55
56#ifndef CJSON_VERSION
57#define CJSON_VERSION "2.1.0"
58#endif
59
60/* Workaround for Solaris platforms missing isinf() */
61#if !defined(isinf) && (defined(USE_INTERNAL_ISINF) || defined(MISSING_ISINF))
62#define isinf(x) (!isnan(x) && isnan((x) - (x)))
63#endif
64
65#define DEFAULT_SPARSE_CONVERT 0
66#define DEFAULT_SPARSE_RATIO 2
67#define DEFAULT_SPARSE_SAFE 10
68#define DEFAULT_ENCODE_MAX_DEPTH 1000
69#define DEFAULT_DECODE_MAX_DEPTH 1000
70#define DEFAULT_ENCODE_INVALID_NUMBERS 0
71#define DEFAULT_DECODE_INVALID_NUMBERS 1
72#define DEFAULT_ENCODE_KEEP_BUFFER 1
73#define DEFAULT_ENCODE_NUMBER_PRECISION 14
74#define DEFAULT_DECODE_ARRAY_WITH_ARRAY_MT 0
75
76#ifdef DISABLE_INVALID_NUMBERS
77#undef DEFAULT_DECODE_INVALID_NUMBERS
78#define DEFAULT_DECODE_INVALID_NUMBERS 0
79#endif
80
81typedef enum {
82 T_OBJ_BEGIN,
83 T_OBJ_END,
84 T_ARR_BEGIN,
85 T_ARR_END,
86 T_STRING,
87 T_NUMBER,
88 T_BOOLEAN,
89 T_NULL,
90 T_COLON,
91 T_COMMA,
92 T_END,
93 T_WHITESPACE,
94 T_ERROR,
95 T_UNKNOWN
96} json_token_type_t;
97
98static const char *json_token_type_name[] = {
99 "T_OBJ_BEGIN",
100 "T_OBJ_END",
101 "T_ARR_BEGIN",
102 "T_ARR_END",
103 "T_STRING",
104 "T_NUMBER",
105 "T_BOOLEAN",
106 "T_NULL",
107 "T_COLON",
108 "T_COMMA",
109 "T_END",
110 "T_WHITESPACE",
111 "T_ERROR",
112 "T_UNKNOWN",
113 NULL
114};
115
116typedef struct {
117 json_token_type_t ch2token[256];
118 char escape2char[256]; /* Decoding */
119
120 /* encode_buf is only allocated and used when
121 * encode_keep_buffer is set */
122 strbuf_t encode_buf;
123
124 int encode_sparse_convert;
125 int encode_sparse_ratio;
126 int encode_sparse_safe;
127 int encode_max_depth;
128 int encode_invalid_numbers; /* 2 => Encode as "null" */
129 int encode_number_precision;
130 int encode_keep_buffer;
131
132 int decode_invalid_numbers;
133 int decode_max_depth;
134 int decode_array_with_array_mt;
135} json_config_t;
136
137typedef struct {
138 const char *data;
139 const char *ptr;
140 strbuf_t *tmp; /* Temporary storage for strings */
141 json_config_t *cfg;
142 int current_depth;
143} json_parse_t;
144
145typedef struct {
146 json_token_type_t type;
147 size_t index;
148 union {
149 const char *string;
150 double number;
151 int boolean;
152 } value;
153 size_t string_len;
154} json_token_t;
155
156static const char *char2escape[256] = {
157 "\\u0000", "\\u0001", "\\u0002", "\\u0003",
158 "\\u0004", "\\u0005", "\\u0006", "\\u0007",
159 "\\b", "\\t", "\\n", "\\u000b",
160 "\\f", "\\r", "\\u000e", "\\u000f",
161 "\\u0010", "\\u0011", "\\u0012", "\\u0013",
162 "\\u0014", "\\u0015", "\\u0016", "\\u0017",
163 "\\u0018", "\\u0019", "\\u001a", "\\u001b",
164 "\\u001c", "\\u001d", "\\u001e", "\\u001f",
165 NULL, NULL, "\\\"", NULL, NULL, NULL, NULL, NULL,
166 NULL, NULL, NULL, NULL, NULL, NULL, NULL, "\\/",
167 NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL,
168 NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL,
169 NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL,
170 NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL,
171 NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL,
172 NULL, NULL, NULL, NULL, "\\\\", NULL, NULL, NULL,
173 NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL,
174 NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL,
175 NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL,
176 NULL, NULL, NULL, NULL, NULL, NULL, NULL, "\\u007f",
177 NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL,
178 NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL,
179 NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL,
180 NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL,
181 NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL,
182 NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL,
183 NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL,
184 NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL,
185 NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL,
186 NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL,
187 NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL,
188 NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL,
189 NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL,
190 NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL,
191 NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL,
192 NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL,
193};
194
195/* ===== CONFIGURATION ===== */
196
197static json_config_t *json_fetch_config(lua_State *l)
198{
199 json_config_t *cfg;
200
201 cfg = lua_touserdata(l, lua_upvalueindex(1));
202 if (!cfg)
203 luaL_error(l, "BUG: Unable to fetch CJSON configuration");
204
205 return cfg;
206}
207
208/* Ensure the correct number of arguments have been provided.
209 * Pad with nil to allow other functions to simply check arg[i]
210 * to find whether an argument was provided */
211static json_config_t *json_arg_init(lua_State *l, int args)
212{
213 luaL_argcheck(l, lua_gettop(l) <= args, args + 1,
214 "found too many arguments");
215
216 while (lua_gettop(l) < args)
217 lua_pushnil(l);
218
219 return json_fetch_config(l);
220}
221
222/* Process integer options for configuration functions */
223static int json_integer_option(lua_State *l, int optindex, int *setting,
224 int min, int max)
225{
226 char errmsg[64];
227 int value;
228
229 if (!lua_isnil(l, optindex)) {
230 value = luaL_checkinteger(l, optindex);
231 snprintf(errmsg, sizeof(errmsg), "expected integer between %d and %d", min, max);
232 luaL_argcheck(l, min <= value && value <= max, 1, errmsg);
233 *setting = value;
234 }
235
236 lua_pushinteger(l, *setting);
237
238 return 1;
239}
240
241/* Process enumerated arguments for a configuration function */
242static int json_enum_option(lua_State *l, int optindex, int *setting,
243 const char **options, int bool_true)
244{
245 static const char *bool_options[] = { "off", "on", NULL };
246
247 if (!options) {
248 options = bool_options;
249 bool_true = 1;
250 }
251
252 if (!lua_isnil(l, optindex)) {
253 if (bool_true && lua_isboolean(l, optindex))
254 *setting = lua_toboolean(l, optindex) * bool_true;
255 else
256 *setting = luaL_checkoption(l, optindex, NULL, options);
257 }
258
259 if (bool_true && (*setting == 0 || *setting == bool_true))
260 lua_pushboolean(l, *setting);
261 else
262 lua_pushstring(l, options[*setting]);
263
264 return 1;
265}
266
267/* Configures handling of extremely sparse arrays:
268 * convert: Convert extremely sparse arrays into objects? Otherwise error.
269 * ratio: 0: always allow sparse; 1: never allow sparse; >1: use ratio
270 * safe: Always use an array when the max index <= safe */
271static int json_cfg_encode_sparse_array(lua_State *l)
272{
273 json_config_t *cfg = json_arg_init(l, 3);
274
275 json_enum_option(l, 1, &cfg->encode_sparse_convert, NULL, 1);
276 json_integer_option(l, 2, &cfg->encode_sparse_ratio, 0, INT_MAX);
277 json_integer_option(l, 3, &cfg->encode_sparse_safe, 0, INT_MAX);
278
279 return 3;
280}
281
282/* Configures the maximum number of nested arrays/objects allowed when
283 * encoding */
284static int json_cfg_encode_max_depth(lua_State *l)
285{
286 json_config_t *cfg = json_arg_init(l, 1);
287
288 return json_integer_option(l, 1, &cfg->encode_max_depth, 1, INT_MAX);
289}
290
291/* Configures the maximum number of nested arrays/objects allowed when
292 * encoding */
293static int json_cfg_decode_max_depth(lua_State *l)
294{
295 json_config_t *cfg = json_arg_init(l, 1);
296
297 return json_integer_option(l, 1, &cfg->decode_max_depth, 1, INT_MAX);
298}
299
300/* Configures number precision when converting doubles to text */
301static int json_cfg_encode_number_precision(lua_State *l)
302{
303 json_config_t *cfg = json_arg_init(l, 1);
304
305 return json_integer_option(l, 1, &cfg->encode_number_precision, 1, 14);
306}
307
308/* Configures how to decode arrays */
309static int json_cfg_decode_array_with_array_mt(lua_State *l)
310{
311 json_config_t *cfg = json_arg_init(l, 1);
312
313 return json_enum_option(l, 1, &cfg->decode_array_with_array_mt, NULL, 1);
314}
315
316/* Configures JSON encoding buffer persistence */
317static int json_cfg_encode_keep_buffer(lua_State *l)
318{
319 json_config_t *cfg = json_arg_init(l, 1);
320 int old_value;
321
322 old_value = cfg->encode_keep_buffer;
323
324 json_enum_option(l, 1, &cfg->encode_keep_buffer, NULL, 1);
325
326 /* Init / free the buffer if the setting has changed */
327 if (old_value ^ cfg->encode_keep_buffer) {
328 if (cfg->encode_keep_buffer)
329 strbuf_init(&cfg->encode_buf, 0);
330 else
331 strbuf_free(&cfg->encode_buf);
332 }
333
334 return 1;
335}
336
337#if defined(DISABLE_INVALID_NUMBERS) && !defined(USE_INTERNAL_FPCONV)
338void json_verify_invalid_number_setting(lua_State *l, int *setting)
339{
340 if (*setting == 1) {
341 *setting = 0;
342 luaL_error(l, "Infinity, NaN, and/or hexadecimal numbers are not supported.");
343 }
344}
345#else
346#define json_verify_invalid_number_setting(l, s) do { } while(0)
347#endif
348
349static int json_cfg_encode_invalid_numbers(lua_State *l)
350{
351 static const char *options[] = { "off", "on", "null", NULL };
352 json_config_t *cfg = json_arg_init(l, 1);
353
354 json_enum_option(l, 1, &cfg->encode_invalid_numbers, options, 1);
355
356 json_verify_invalid_number_setting(l, &cfg->encode_invalid_numbers);
357
358 return 1;
359}
360
361static int json_cfg_decode_invalid_numbers(lua_State *l)
362{
363 json_config_t *cfg = json_arg_init(l, 1);
364
365 json_enum_option(l, 1, &cfg->decode_invalid_numbers, NULL, 1);
366
367 json_verify_invalid_number_setting(l, &cfg->encode_invalid_numbers);
368
369 return 1;
370}
371
372static int json_destroy_config(lua_State *l)
373{
374 json_config_t *cfg;
375
376 cfg = lua_touserdata(l, 1);
377 if (cfg)
378 strbuf_free(&cfg->encode_buf);
379 cfg = NULL;
380
381 return 0;
382}
383
384static void json_create_config(lua_State *l)
385{
386 json_config_t *cfg;
387 int i;
388
389 cfg = lua_newuserdata(l, sizeof(*cfg));
390
391 /* Create GC method to clean up strbuf */
392 lua_newtable(l);
393 lua_pushcfunction(l, json_destroy_config);
394 lua_setfield(l, -2, "__gc");
395 lua_setmetatable(l, -2);
396
397 cfg->encode_sparse_convert = DEFAULT_SPARSE_CONVERT;
398 cfg->encode_sparse_ratio = DEFAULT_SPARSE_RATIO;
399 cfg->encode_sparse_safe = DEFAULT_SPARSE_SAFE;
400 cfg->encode_max_depth = DEFAULT_ENCODE_MAX_DEPTH;
401 cfg->decode_max_depth = DEFAULT_DECODE_MAX_DEPTH;
402 cfg->encode_invalid_numbers = DEFAULT_ENCODE_INVALID_NUMBERS;
403 cfg->decode_invalid_numbers = DEFAULT_DECODE_INVALID_NUMBERS;
404 cfg->encode_keep_buffer = DEFAULT_ENCODE_KEEP_BUFFER;
405 cfg->encode_number_precision = DEFAULT_ENCODE_NUMBER_PRECISION;
406 cfg->decode_array_with_array_mt = DEFAULT_DECODE_ARRAY_WITH_ARRAY_MT;
407
408#if DEFAULT_ENCODE_KEEP_BUFFER > 0
409 strbuf_init(&cfg->encode_buf, 0);
410#endif
411
412 /* Decoding init */
413
414 /* Tag all characters as an error */
415 for (i = 0; i < 256; i++)
416 cfg->ch2token[i] = T_ERROR;
417
418 /* Set tokens that require no further processing */
419 cfg->ch2token['{'] = T_OBJ_BEGIN;
420 cfg->ch2token['}'] = T_OBJ_END;
421 cfg->ch2token['['] = T_ARR_BEGIN;
422 cfg->ch2token[']'] = T_ARR_END;
423 cfg->ch2token[','] = T_COMMA;
424 cfg->ch2token[':'] = T_COLON;
425 cfg->ch2token['\0'] = T_END;
426 cfg->ch2token[' '] = T_WHITESPACE;
427 cfg->ch2token['\t'] = T_WHITESPACE;
428 cfg->ch2token['\n'] = T_WHITESPACE;
429 cfg->ch2token['\r'] = T_WHITESPACE;
430
431 /* Update characters that require further processing */
432 cfg->ch2token['f'] = T_UNKNOWN; /* false? */
433 cfg->ch2token['i'] = T_UNKNOWN; /* inf, ininity? */
434 cfg->ch2token['I'] = T_UNKNOWN;
435 cfg->ch2token['n'] = T_UNKNOWN; /* null, nan? */
436 cfg->ch2token['N'] = T_UNKNOWN;
437 cfg->ch2token['t'] = T_UNKNOWN; /* true? */
438 cfg->ch2token['"'] = T_UNKNOWN; /* string? */
439 cfg->ch2token['+'] = T_UNKNOWN; /* number? */
440 cfg->ch2token['-'] = T_UNKNOWN;
441 for (i = 0; i < 10; i++)
442 cfg->ch2token['0' + i] = T_UNKNOWN;
443
444 /* Lookup table for parsing escape characters */
445 for (i = 0; i < 256; i++)
446 cfg->escape2char[i] = 0; /* String error */
447 cfg->escape2char['"'] = '"';
448 cfg->escape2char['\\'] = '\\';
449 cfg->escape2char['/'] = '/';
450 cfg->escape2char['b'] = '\b';
451 cfg->escape2char['t'] = '\t';
452 cfg->escape2char['n'] = '\n';
453 cfg->escape2char['f'] = '\f';
454 cfg->escape2char['r'] = '\r';
455 cfg->escape2char['u'] = 'u'; /* Unicode parsing required */
456}
457
458/* ===== ENCODING ===== */
459
460static void json_encode_exception(lua_State *l, json_config_t *cfg, strbuf_t *json, int lindex,
461 const char *reason)
462{
463 if (!cfg->encode_keep_buffer)
464 strbuf_free(json);
465 luaL_error(l, "Cannot serialise %s: %s",
466 lua_typename(l, lua_type(l, lindex)), reason);
467}
468
469/* json_append_string args:
470 * - lua_State
471 * - JSON strbuf
472 * - String (Lua stack index)
473 *
474 * Returns nothing. Doesn't remove string from Lua stack */
475static void json_append_string(lua_State *l, strbuf_t *json, int lindex)
476{
477 const char *escstr;
478 const char *str;
479 size_t i, len;
480
481 str = lua_tolstring(l, lindex, &len);
482
483 /* Worst case is len * 6 (all unicode escapes).
484 * This buffer is reused constantly for small strings
485 * If there are any excess pages, they won't be hit anyway.
486 * This gains ~5% speedup. */
487 if (len > SIZE_MAX / 6 - 3)
488 abort(); /* Overflow check */
489 strbuf_ensure_empty_length(json, len * 6 + 2);
490
491 strbuf_append_char_unsafe(json, '\"');
492 for (i = 0; i < len; i++) {
493 escstr = char2escape[(unsigned char)str[i]];
494 if (escstr)
495 strbuf_append_string(json, escstr);
496 else
497 strbuf_append_char_unsafe(json, str[i]);
498 }
499 strbuf_append_char_unsafe(json, '\"');
500}
501
502/* Find the size of the array on the top of the Lua stack
503 * -1 object (not a pure array)
504 * >=0 elements in array
505 */
506static int lua_array_length(lua_State *l, json_config_t *cfg, strbuf_t *json)
507{
508 double k;
509 int max;
510 int items;
511
512 max = 0;
513 items = 0;
514
515 lua_pushnil(l);
516 /* table, startkey */
517 while (lua_next(l, -2) != 0) {
518 /* table, key, value */
519 if (lua_type(l, -2) == LUA_TNUMBER &&
520 (k = lua_tonumber(l, -2))) {
521 /* Integer >= 1 ? */
522 if (floor(k) == k && k >= 1) {
523 if (k > max)
524 max = k;
525 items++;
526 lua_pop(l, 1);
527 continue;
528 }
529 }
530
531 /* Must not be an array (non integer key) */
532 lua_pop(l, 2);
533 return -1;
534 }
535
536 /* Encode excessively sparse arrays as objects (if enabled) */
537 if (cfg->encode_sparse_ratio > 0 &&
538 max > items * cfg->encode_sparse_ratio &&
539 max > cfg->encode_sparse_safe) {
540 if (!cfg->encode_sparse_convert)
541 json_encode_exception(l, cfg, json, -1, "excessively sparse array");
542
543 return -1;
544 }
545
546 return max;
547}
548
549static void json_check_encode_depth(lua_State *l, json_config_t *cfg,
550 int current_depth, strbuf_t *json)
551{
552 /* Ensure there are enough slots free to traverse a table (key,
553 * value) and push a string for a potential error message.
554 *
555 * Unlike "decode", the key and value are still on the stack when
556 * lua_checkstack() is called. Hence an extra slot for luaL_error()
557 * below is required just in case the next check to lua_checkstack()
558 * fails.
559 *
560 * While this won't cause a crash due to the EXTRA_STACK reserve
561 * slots, it would still be an improper use of the API. */
562 if (current_depth <= cfg->encode_max_depth && lua_checkstack(l, 3))
563 return;
564
565 if (!cfg->encode_keep_buffer)
566 strbuf_free(json);
567
568 luaL_error(l, "Cannot serialise, excessive nesting (%d)",
569 current_depth);
570}
571
572static void json_append_data(lua_State *l, json_config_t *cfg,
573 int current_depth, strbuf_t *json);
574
575/* json_append_array args:
576 * - lua_State
577 * - JSON strbuf
578 * - Size of passwd Lua array (top of stack) */
579static void json_append_array(lua_State *l, json_config_t *cfg, int current_depth,
580 strbuf_t *json, int array_length)
581{
582 int comma, i;
583
584 strbuf_append_char(json, '[');
585
586 comma = 0;
587 for (i = 1; i <= array_length; i++) {
588 if (comma)
589 strbuf_append_char(json, ',');
590 else
591 comma = 1;
592
593 lua_rawgeti(l, -1, i);
594 json_append_data(l, cfg, current_depth, json);
595 lua_pop(l, 1);
596 }
597
598 strbuf_append_char(json, ']');
599}
600
601static void json_append_number(lua_State *l, json_config_t *cfg,
602 strbuf_t *json, int lindex)
603{
604 double num = lua_tonumber(l, lindex);
605 int len;
606
607 if (cfg->encode_invalid_numbers == 0) {
608 /* Prevent encoding invalid numbers */
609 if (isinf(num) || isnan(num))
610 json_encode_exception(l, cfg, json, lindex, "must not be NaN or Inf");
611 } else if (cfg->encode_invalid_numbers == 1) {
612 /* Encode invalid numbers, but handle "nan" separately
613 * since some platforms may encode as "-nan". */
614 if (isnan(num)) {
615 strbuf_append_mem(json, "nan", 3);
616 return;
617 }
618 } else {
619 /* Encode invalid numbers as "null" */
620 if (isinf(num) || isnan(num)) {
621 strbuf_append_mem(json, "null", 4);
622 return;
623 }
624 }
625
626 strbuf_ensure_empty_length(json, FPCONV_G_FMT_BUFSIZE);
627 len = fpconv_g_fmt(strbuf_empty_ptr(json), num, cfg->encode_number_precision);
628 strbuf_extend_length(json, len);
629}
630
631static void json_append_object(lua_State *l, json_config_t *cfg,
632 int current_depth, strbuf_t *json)
633{
634 int comma, keytype;
635
636 /* Object */
637 strbuf_append_char(json, '{');
638
639 lua_pushnil(l);
640 /* table, startkey */
641 comma = 0;
642 while (lua_next(l, -2) != 0) {
643 if (comma)
644 strbuf_append_char(json, ',');
645 else
646 comma = 1;
647
648 /* table, key, value */
649 keytype = lua_type(l, -2);
650 if (keytype == LUA_TNUMBER) {
651 strbuf_append_char(json, '"');
652 json_append_number(l, cfg, json, -2);
653 strbuf_append_mem(json, "\":", 2);
654 } else if (keytype == LUA_TSTRING) {
655 json_append_string(l, json, -2);
656 strbuf_append_char(json, ':');
657 } else {
658 json_encode_exception(l, cfg, json, -2,
659 "table key must be a number or string");
660 /* never returns */
661 }
662
663 /* table, key, value */
664 json_append_data(l, cfg, current_depth, json);
665 lua_pop(l, 1);
666 /* table, key */
667 }
668
669 strbuf_append_char(json, '}');
670}
671
672/* Serialise Lua data into JSON string. */
673static void json_append_data(lua_State *l, json_config_t *cfg,
674 int current_depth, strbuf_t *json)
675{
676 int len;
677
678 switch (lua_type(l, -1)) {
679 case LUA_TSTRING:
680 json_append_string(l, json, -1);
681 break;
682 case LUA_TNUMBER:
683 json_append_number(l, cfg, json, -1);
684 break;
685 case LUA_TBOOLEAN:
686 if (lua_toboolean(l, -1))
687 strbuf_append_mem(json, "true", 4);
688 else
689 strbuf_append_mem(json, "false", 5);
690 break;
691 case LUA_TTABLE:
692 current_depth++;
693 json_check_encode_depth(l, cfg, current_depth, json);
694
695 /* Check if this is an array */
696 int as_array = 0;
697 if (!lua_checkstack(l, 2))
698 luaL_error(l, "max lua stack reached");
699 if (lua_getmetatable(l, -1)) {
700 lua_getfield(l, -1, "__is_cjson_array");
701 as_array = lua_toboolean(l, -1);
702 lua_pop(l, 2); /* pop value and metatable */
703 }
704
705 if (as_array) {
706 len = lua_objlen(l, -1);
707 json_append_array(l, cfg, current_depth, json, len);
708 break;
709 }
710
711 len = lua_array_length(l, cfg, json);
712 if (len > 0)
713 json_append_array(l, cfg, current_depth, json, len);
714 else
715 json_append_object(l, cfg, current_depth, json);
716 break;
717 case LUA_TNIL:
718 strbuf_append_mem(json, "null", 4);
719 break;
720 case LUA_TLIGHTUSERDATA:
721 if (lua_touserdata(l, -1) == NULL) {
722 strbuf_append_mem(json, "null", 4);
723 break;
724 }
725 default:
726 /* Remaining types (LUA_TFUNCTION, LUA_TUSERDATA, LUA_TTHREAD,
727 * and LUA_TLIGHTUSERDATA) cannot be serialised */
728 json_encode_exception(l, cfg, json, -1, "type not supported");
729 /* never returns */
730 }
731}
732
733static int json_encode(lua_State *l)
734{
735 json_config_t *cfg = json_fetch_config(l);
736 strbuf_t local_encode_buf;
737 strbuf_t *encode_buf;
738 char *json;
739 size_t len;
740
741 luaL_argcheck(l, lua_gettop(l) == 1, 1, "expected 1 argument");
742
743 if (!cfg->encode_keep_buffer) {
744 /* Use private buffer */
745 encode_buf = &local_encode_buf;
746 strbuf_init(encode_buf, 0);
747 } else {
748 /* Reuse existing buffer */
749 encode_buf = &cfg->encode_buf;
750 strbuf_reset(encode_buf);
751 }
752
753 json_append_data(l, cfg, 0, encode_buf);
754 json = strbuf_string(encode_buf, &len);
755
756 lua_pushlstring(l, json, len);
757
758 if (!cfg->encode_keep_buffer)
759 strbuf_free(encode_buf);
760
761 return 1;
762}
763
764/* ===== DECODING ===== */
765
766static void json_process_value(lua_State *l, json_parse_t *json,
767 json_token_t *token);
768
769static int hexdigit2int(char hex)
770{
771 if ('0' <= hex && hex <= '9')
772 return hex - '0';
773
774 /* Force lowercase */
775 hex |= 0x20;
776 if ('a' <= hex && hex <= 'f')
777 return 10 + hex - 'a';
778
779 return -1;
780}
781
782static int decode_hex4(const char *hex)
783{
784 int digit[4];
785 int i;
786
787 /* Convert ASCII hex digit to numeric digit
788 * Note: this returns an error for invalid hex digits, including
789 * NULL */
790 for (i = 0; i < 4; i++) {
791 digit[i] = hexdigit2int(hex[i]);
792 if (digit[i] < 0) {
793 return -1;
794 }
795 }
796
797 return (digit[0] << 12) +
798 (digit[1] << 8) +
799 (digit[2] << 4) +
800 digit[3];
801}
802
803/* Converts a Unicode codepoint to UTF-8.
804 * Returns UTF-8 string length, and up to 4 bytes in *utf8 */
805static int codepoint_to_utf8(char *utf8, int codepoint)
806{
807 /* 0xxxxxxx */
808 if (codepoint <= 0x7F) {
809 utf8[0] = codepoint;
810 return 1;
811 }
812
813 /* 110xxxxx 10xxxxxx */
814 if (codepoint <= 0x7FF) {
815 utf8[0] = (codepoint >> 6) | 0xC0;
816 utf8[1] = (codepoint & 0x3F) | 0x80;
817 return 2;
818 }
819
820 /* 1110xxxx 10xxxxxx 10xxxxxx */
821 if (codepoint <= 0xFFFF) {
822 utf8[0] = (codepoint >> 12) | 0xE0;
823 utf8[1] = ((codepoint >> 6) & 0x3F) | 0x80;
824 utf8[2] = (codepoint & 0x3F) | 0x80;
825 return 3;
826 }
827
828 /* 11110xxx 10xxxxxx 10xxxxxx 10xxxxxx */
829 if (codepoint <= 0x1FFFFF) {
830 utf8[0] = (codepoint >> 18) | 0xF0;
831 utf8[1] = ((codepoint >> 12) & 0x3F) | 0x80;
832 utf8[2] = ((codepoint >> 6) & 0x3F) | 0x80;
833 utf8[3] = (codepoint & 0x3F) | 0x80;
834 return 4;
835 }
836
837 return 0;
838}
839
840
841/* Called when index pointing to beginning of UTF-16 code escape: \uXXXX
842 * \u is guaranteed to exist, but the remaining hex characters may be
843 * missing.
844 * Translate to UTF-8 and append to temporary token string.
845 * Must advance index to the next character to be processed.
846 * Returns: 0 success
847 * -1 error
848 */
849static int json_append_unicode_escape(json_parse_t *json)
850{
851 char utf8[4]; /* Surrogate pairs require 4 UTF-8 bytes */
852 int codepoint;
853 int surrogate_low;
854 int len;
855 int escape_len = 6;
856
857 /* Fetch UTF-16 code unit */
858 codepoint = decode_hex4(json->ptr + 2);
859 if (codepoint < 0)
860 return -1;
861
862 /* UTF-16 surrogate pairs take the following 2 byte form:
863 * 11011 x yyyyyyyyyy
864 * When x = 0: y is the high 10 bits of the codepoint
865 * x = 1: y is the low 10 bits of the codepoint
866 *
867 * Check for a surrogate pair (high or low) */
868 if ((codepoint & 0xF800) == 0xD800) {
869 /* Error if the 1st surrogate is not high */
870 if (codepoint & 0x400)
871 return -1;
872
873 /* Ensure the next code is a unicode escape */
874 if (*(json->ptr + escape_len) != '\\' ||
875 *(json->ptr + escape_len + 1) != 'u') {
876 return -1;
877 }
878
879 /* Fetch the next codepoint */
880 surrogate_low = decode_hex4(json->ptr + 2 + escape_len);
881 if (surrogate_low < 0)
882 return -1;
883
884 /* Error if the 2nd code is not a low surrogate */
885 if ((surrogate_low & 0xFC00) != 0xDC00)
886 return -1;
887
888 /* Calculate Unicode codepoint */
889 codepoint = (codepoint & 0x3FF) << 10;
890 surrogate_low &= 0x3FF;
891 codepoint = (codepoint | surrogate_low) + 0x10000;
892 escape_len = 12;
893 }
894
895 /* Convert codepoint to UTF-8 */
896 len = codepoint_to_utf8(utf8, codepoint);
897 if (!len)
898 return -1;
899
900 /* Append bytes and advance parse index */
901 strbuf_append_mem_unsafe(json->tmp, utf8, len);
902 json->ptr += escape_len;
903
904 return 0;
905}
906
907static void json_set_token_error(json_token_t *token, json_parse_t *json,
908 const char *errtype)
909{
910 token->type = T_ERROR;
911 token->index = json->ptr - json->data;
912 token->value.string = errtype;
913}
914
915static void json_next_string_token(json_parse_t *json, json_token_t *token)
916{
917 char *escape2char = json->cfg->escape2char;
918 char ch;
919
920 /* Caller must ensure a string is next */
921 assert(*json->ptr == '"');
922
923 /* Skip " */
924 json->ptr++;
925
926 /* json->tmp is the temporary strbuf used to accumulate the
927 * decoded string value.
928 * json->tmp is sized to handle JSON containing only a string value.
929 */
930 strbuf_reset(json->tmp);
931
932 while ((ch = *json->ptr) != '"') {
933 if (!ch) {
934 /* Premature end of the string */
935 json_set_token_error(token, json, "unexpected end of string");
936 return;
937 }
938
939 /* Handle escapes */
940 if (ch == '\\') {
941 /* Fetch escape character */
942 ch = *(json->ptr + 1);
943
944 /* Translate escape code and append to tmp string */
945 ch = escape2char[(unsigned char)ch];
946 if (ch == 'u') {
947 if (json_append_unicode_escape(json) == 0)
948 continue;
949
950 json_set_token_error(token, json,
951 "invalid unicode escape code");
952 return;
953 }
954 if (!ch) {
955 json_set_token_error(token, json, "invalid escape code");
956 return;
957 }
958
959 /* Skip '\' */
960 json->ptr++;
961 }
962 /* Append normal character or translated single character
963 * Unicode escapes are handled above */
964 strbuf_append_char_unsafe(json->tmp, ch);
965 json->ptr++;
966 }
967 json->ptr++; /* Eat final quote (") */
968
969 strbuf_ensure_null(json->tmp);
970
971 token->type = T_STRING;
972 token->value.string = strbuf_string(json->tmp, &token->string_len);
973}
974
975/* JSON numbers should take the following form:
976 * -?(0|[1-9]|[1-9][0-9]+)(.[0-9]+)?([eE][-+]?[0-9]+)?
977 *
978 * json_next_number_token() uses strtod() which allows other forms:
979 * - numbers starting with '+'
980 * - NaN, -NaN, infinity, -infinity
981 * - hexadecimal numbers
982 * - numbers with leading zeros
983 *
984 * json_is_invalid_number() detects "numbers" which may pass strtod()'s
985 * error checking, but should not be allowed with strict JSON.
986 *
987 * json_is_invalid_number() may pass numbers which cause strtod()
988 * to generate an error.
989 */
990static int json_is_invalid_number(json_parse_t *json)
991{
992 const char *p = json->ptr;
993
994 /* Reject numbers starting with + */
995 if (*p == '+')
996 return 1;
997
998 /* Skip minus sign if it exists */
999 if (*p == '-')
1000 p++;
1001
1002 /* Reject numbers starting with 0x, or leading zeros */
1003 if (*p == '0') {
1004 int ch2 = *(p + 1);
1005
1006 if ((ch2 | 0x20) == 'x' || /* Hex */
1007 ('0' <= ch2 && ch2 <= '9')) /* Leading zero */
1008 return 1;
1009
1010 return 0;
1011 } else if (*p <= '9') {
1012 return 0; /* Ordinary number */
1013 }
1014
1015 /* Reject inf/nan */
1016 if (!strncasecmp(p, "inf", 3))
1017 return 1;
1018 if (!strncasecmp(p, "nan", 3))
1019 return 1;
1020
1021 /* Pass all other numbers which may still be invalid, but
1022 * strtod() will catch them. */
1023 return 0;
1024}
1025
1026static void json_next_number_token(json_parse_t *json, json_token_t *token)
1027{
1028 char *endptr;
1029
1030 token->type = T_NUMBER;
1031 token->value.number = fpconv_strtod(json->ptr, &endptr);
1032 if (json->ptr == endptr)
1033 json_set_token_error(token, json, "invalid number");
1034 else
1035 json->ptr = endptr; /* Skip the processed number */
1036
1037 return;
1038}
1039
1040/* Fills in the token struct.
1041 * T_STRING will return a pointer to the json_parse_t temporary string
1042 * T_ERROR will leave the json->ptr pointer at the error.
1043 */
1044static void json_next_token(json_parse_t *json, json_token_t *token)
1045{
1046 const json_token_type_t *ch2token = json->cfg->ch2token;
1047 int ch;
1048
1049 /* Eat whitespace. */
1050 while (1) {
1051 ch = (unsigned char)*(json->ptr);
1052 token->type = ch2token[ch];
1053 if (token->type != T_WHITESPACE)
1054 break;
1055 json->ptr++;
1056 }
1057
1058 /* Store location of new token. Required when throwing errors
1059 * for unexpected tokens (syntax errors). */
1060 token->index = json->ptr - json->data;
1061
1062 /* Don't advance the pointer for an error or the end */
1063 if (token->type == T_ERROR) {
1064 json_set_token_error(token, json, "invalid token");
1065 return;
1066 }
1067
1068 if (token->type == T_END) {
1069 return;
1070 }
1071
1072 /* Found a known single character token, advance index and return */
1073 if (token->type != T_UNKNOWN) {
1074 json->ptr++;
1075 return;
1076 }
1077
1078 /* Process characters which triggered T_UNKNOWN
1079 *
1080 * Must use strncmp() to match the front of the JSON string.
1081 * JSON identifier must be lowercase.
1082 * When strict_numbers if disabled, either case is allowed for
1083 * Infinity/NaN (since we are no longer following the spec..) */
1084 if (ch == '"') {
1085 json_next_string_token(json, token);
1086 return;
1087 } else if (ch == '-' || ('0' <= ch && ch <= '9')) {
1088 if (!json->cfg->decode_invalid_numbers && json_is_invalid_number(json)) {
1089 json_set_token_error(token, json, "invalid number");
1090 return;
1091 }
1092 json_next_number_token(json, token);
1093 return;
1094 } else if (!strncmp(json->ptr, "true", 4)) {
1095 token->type = T_BOOLEAN;
1096 token->value.boolean = 1;
1097 json->ptr += 4;
1098 return;
1099 } else if (!strncmp(json->ptr, "false", 5)) {
1100 token->type = T_BOOLEAN;
1101 token->value.boolean = 0;
1102 json->ptr += 5;
1103 return;
1104 } else if (!strncmp(json->ptr, "null", 4)) {
1105 token->type = T_NULL;
1106 json->ptr += 4;
1107 return;
1108 } else if (json->cfg->decode_invalid_numbers &&
1109 json_is_invalid_number(json)) {
1110 /* When decode_invalid_numbers is enabled, only attempt to process
1111 * numbers we know are invalid JSON (Inf, NaN, hex)
1112 * This is required to generate an appropriate token error,
1113 * otherwise all bad tokens will register as "invalid number"
1114 */
1115 json_next_number_token(json, token);
1116 return;
1117 }
1118
1119 /* Token starts with t/f/n but isn't recognised above. */
1120 json_set_token_error(token, json, "invalid token");
1121}
1122
1123/* This function does not return.
1124 * DO NOT CALL WITH DYNAMIC MEMORY ALLOCATED.
1125 * The only supported exception is the temporary parser string
1126 * json->tmp struct.
1127 * json and token should exist on the stack somewhere.
1128 * luaL_error() will long_jmp and release the stack */
1129static void json_throw_parse_error(lua_State *l, json_parse_t *json,
1130 const char *exp, json_token_t *token)
1131{
1132 const char *found;
1133
1134 strbuf_free(json->tmp);
1135
1136 if (token->type == T_ERROR)
1137 found = token->value.string;
1138 else
1139 found = json_token_type_name[token->type];
1140
1141 /* Note: token->index is 0 based, display starting from 1 */
1142 luaL_error(l, "Expected %s but found %s at character %d",
1143 exp, found, token->index + 1);
1144}
1145
1146static inline void json_decode_ascend(json_parse_t *json)
1147{
1148 json->current_depth--;
1149}
1150
1151static void json_decode_descend(lua_State *l, json_parse_t *json, int slots)
1152{
1153 json->current_depth++;
1154
1155 if (json->current_depth <= json->cfg->decode_max_depth &&
1156 lua_checkstack(l, slots)) {
1157 return;
1158 }
1159
1160 strbuf_free(json->tmp);
1161 luaL_error(l, "Found too many nested data structures (%d) at character %d",
1162 json->current_depth, json->ptr - json->data);
1163}
1164
1165static void json_parse_object_context(lua_State *l, json_parse_t *json)
1166{
1167 json_token_t token;
1168
1169 /* 3 slots required:
1170 * .., table, key, value */
1171 json_decode_descend(l, json, 3);
1172
1173 lua_newtable(l);
1174
1175 json_next_token(json, &token);
1176
1177 /* Handle empty objects */
1178 if (token.type == T_OBJ_END) {
1179 json_decode_ascend(json);
1180 return;
1181 }
1182
1183 while (1) {
1184 if (token.type != T_STRING)
1185 json_throw_parse_error(l, json, "object key string", &token);
1186
1187 /* Push key */
1188 lua_pushlstring(l, token.value.string, token.string_len);
1189
1190 json_next_token(json, &token);
1191 if (token.type != T_COLON)
1192 json_throw_parse_error(l, json, "colon", &token);
1193
1194 /* Fetch value */
1195 json_next_token(json, &token);
1196 json_process_value(l, json, &token);
1197
1198 /* Set key = value */
1199 lua_rawset(l, -3);
1200
1201 json_next_token(json, &token);
1202
1203 if (token.type == T_OBJ_END) {
1204 json_decode_ascend(json);
1205 return;
1206 }
1207
1208 if (token.type != T_COMMA)
1209 json_throw_parse_error(l, json, "comma or object end", &token);
1210
1211 json_next_token(json, &token);
1212 }
1213}
1214
1215/* Handle the array context */
1216static void json_parse_array_context(lua_State *l, json_parse_t *json)
1217{
1218 json_token_t token;
1219 int i;
1220
1221 /* 2 slots required:
1222 * .., table, value */
1223 json_decode_descend(l, json, 2);
1224
1225 lua_newtable(l);
1226
1227 /* set array_mt on the table at the top of the stack */
1228 if (json->cfg->decode_array_with_array_mt) {
1229 /* Ensure sufficient stack space for metatable creation (metatable + boolean) */
1230 if (!lua_checkstack(l, 2))
1231 luaL_error(l, "max lua stack reached");
1232 /* Mark this table so encoder can emit [] for empty arrays */
1233 lua_newtable(l);
1234 lua_pushboolean(l, 1);
1235 lua_setfield(l, -2, "__is_cjson_array");
1236 lua_enablereadonlytable(l, -1, 1); /* protect the metatable. */
1237 lua_setmetatable(l, -2); /* set metatable for the array table */
1238 }
1239
1240 json_next_token(json, &token);
1241
1242 /* Handle empty arrays */
1243 if (token.type == T_ARR_END) {
1244 json_decode_ascend(json);
1245 return;
1246 }
1247
1248 for (i = 1; ; i++) {
1249 json_process_value(l, json, &token);
1250 lua_rawseti(l, -2, i); /* arr[i] = value */
1251
1252 json_next_token(json, &token);
1253
1254 if (token.type == T_ARR_END) {
1255 json_decode_ascend(json);
1256 return;
1257 }
1258
1259 if (token.type != T_COMMA)
1260 json_throw_parse_error(l, json, "comma or array end", &token);
1261
1262 json_next_token(json, &token);
1263 }
1264}
1265
1266/* Handle the "value" context */
1267static void json_process_value(lua_State *l, json_parse_t *json,
1268 json_token_t *token)
1269{
1270 switch (token->type) {
1271 case T_STRING:
1272 lua_pushlstring(l, token->value.string, token->string_len);
1273 break;;
1274 case T_NUMBER:
1275 lua_pushnumber(l, token->value.number);
1276 break;;
1277 case T_BOOLEAN:
1278 lua_pushboolean(l, token->value.boolean);
1279 break;;
1280 case T_OBJ_BEGIN:
1281 json_parse_object_context(l, json);
1282 break;;
1283 case T_ARR_BEGIN:
1284 json_parse_array_context(l, json);
1285 break;;
1286 case T_NULL:
1287 /* In Lua, setting "t[k] = nil" will delete k from the table.
1288 * Hence a NULL pointer lightuserdata object is used instead */
1289 lua_pushlightuserdata(l, NULL);
1290 break;;
1291 default:
1292 json_throw_parse_error(l, json, "value", token);
1293 }
1294}
1295
1296static int json_decode(lua_State *l)
1297{
1298 json_parse_t json;
1299 json_token_t token;
1300 size_t json_len;
1301
1302 luaL_argcheck(l, lua_gettop(l) == 1, 1, "expected 1 argument");
1303
1304 json.cfg = json_fetch_config(l);
1305 json.data = luaL_checklstring(l, 1, &json_len);
1306 json.current_depth = 0;
1307 json.ptr = json.data;
1308
1309 /* Detect Unicode other than UTF-8 (see RFC 4627, Sec 3)
1310 *
1311 * CJSON can support any simple data type, hence only the first
1312 * character is guaranteed to be ASCII (at worst: '"'). This is
1313 * still enough to detect whether the wrong encoding is in use. */
1314 if (json_len >= 2 && (!json.data[0] || !json.data[1]))
1315 luaL_error(l, "JSON parser does not support UTF-16 or UTF-32");
1316
1317 /* Ensure the temporary buffer can hold the entire string.
1318 * This means we no longer need to do length checks since the decoded
1319 * string must be smaller than the entire json string */
1320 json.tmp = strbuf_new(json_len);
1321
1322 json_next_token(&json, &token);
1323 json_process_value(l, &json, &token);
1324
1325 /* Ensure there is no more input left */
1326 json_next_token(&json, &token);
1327
1328 if (token.type != T_END)
1329 json_throw_parse_error(l, &json, "the end", &token);
1330
1331 strbuf_free(json.tmp);
1332
1333 return 1;
1334}
1335
1336/* ===== INITIALISATION ===== */
1337
1338#if !defined(LUA_VERSION_NUM) || LUA_VERSION_NUM < 502
1339/* Compatibility for Lua 5.1.
1340 *
1341 * luaL_setfuncs() is used to create a module table where the functions have
1342 * json_config_t as their first upvalue. Code borrowed from Lua 5.2 source. */
1343static void luaL_setfuncs (lua_State *l, const luaL_Reg *reg, int nup)
1344{
1345 int i;
1346
1347 luaL_checkstack(l, nup, "too many upvalues");
1348 for (; reg->name != NULL; reg++) { /* fill the table with given functions */
1349 for (i = 0; i < nup; i++) /* copy upvalues to the top */
1350 lua_pushvalue(l, -nup);
1351 lua_pushcclosure(l, reg->func, nup); /* closure with those upvalues */
1352 lua_setfield(l, -(nup + 2), reg->name);
1353 }
1354 lua_pop(l, nup); /* remove upvalues */
1355}
1356#endif
1357
1358/* Call target function in protected mode with all supplied args.
1359 * Assumes target function only returns a single non-nil value.
1360 * Convert and return thrown errors as: nil, "error message" */
1361static int json_protect_conversion(lua_State *l)
1362{
1363 int err;
1364
1365 /* Deliberately throw an error for invalid arguments */
1366 luaL_argcheck(l, lua_gettop(l) == 1, 1, "expected 1 argument");
1367
1368 /* pcall() the function stored as upvalue(1) */
1369 lua_pushvalue(l, lua_upvalueindex(1));
1370 lua_insert(l, 1);
1371 err = lua_pcall(l, 1, 1, 0);
1372 if (!err)
1373 return 1;
1374
1375 if (err == LUA_ERRRUN) {
1376 lua_pushnil(l);
1377 lua_insert(l, -2);
1378 return 2;
1379 }
1380
1381 /* Since we are not using a custom error handler, the only remaining
1382 * errors are memory related */
1383 return luaL_error(l, "Memory allocation error in CJSON protected call");
1384}
1385
1386/* Return cjson module table */
1387static int lua_cjson_new(lua_State *l)
1388{
1389 luaL_Reg reg[] = {
1390 { "encode", json_encode },
1391 { "decode", json_decode },
1392 { "decode_array_with_array_mt", json_cfg_decode_array_with_array_mt },
1393 { "encode_sparse_array", json_cfg_encode_sparse_array },
1394 { "encode_max_depth", json_cfg_encode_max_depth },
1395 { "decode_max_depth", json_cfg_decode_max_depth },
1396 { "encode_number_precision", json_cfg_encode_number_precision },
1397 { "encode_keep_buffer", json_cfg_encode_keep_buffer },
1398 { "encode_invalid_numbers", json_cfg_encode_invalid_numbers },
1399 { "decode_invalid_numbers", json_cfg_decode_invalid_numbers },
1400 { "new", lua_cjson_new },
1401 { NULL, NULL }
1402 };
1403
1404 /* Initialise number conversions */
1405 fpconv_init();
1406
1407 /* cjson module table */
1408 lua_newtable(l);
1409
1410 /* Register functions with config data as upvalue */
1411 json_create_config(l);
1412 luaL_setfuncs(l, reg, 1);
1413
1414 /* Set cjson.null */
1415 lua_pushlightuserdata(l, NULL);
1416 lua_setfield(l, -2, "null");
1417
1418 /* Set module name / version fields */
1419 lua_pushliteral(l, CJSON_MODNAME);
1420 lua_setfield(l, -2, "_NAME");
1421 lua_pushliteral(l, CJSON_VERSION);
1422 lua_setfield(l, -2, "_VERSION");
1423
1424 return 1;
1425}
1426
1427/* Return cjson.safe module table */
1428static int lua_cjson_safe_new(lua_State *l)
1429{
1430 const char *func[] = { "decode", "encode", NULL };
1431 int i;
1432
1433 lua_cjson_new(l);
1434
1435 /* Fix new() method */
1436 lua_pushcfunction(l, lua_cjson_safe_new);
1437 lua_setfield(l, -2, "new");
1438
1439 for (i = 0; func[i]; i++) {
1440 lua_getfield(l, -1, func[i]);
1441 lua_pushcclosure(l, json_protect_conversion, 1);
1442 lua_setfield(l, -2, func[i]);
1443 }
1444
1445 return 1;
1446}
1447
1448int luaopen_cjson(lua_State *l)
1449{
1450 lua_cjson_new(l);
1451
1452#ifdef ENABLE_CJSON_GLOBAL
1453 /* Register a global "cjson" table. */
1454 lua_pushvalue(l, -1);
1455 lua_setglobal(l, CJSON_MODNAME);
1456#endif
1457
1458 /* Return cjson table */
1459 return 1;
1460}
1461
1462int luaopen_cjson_safe(lua_State *l)
1463{
1464 lua_cjson_safe_new(l);
1465
1466 /* Return cjson.safe table */
1467 return 1;
1468}
1469
1470/* vi:ai et sw=4 ts=4:
1471 */
diff --git a/examples/redis-unstable/deps/lua/src/lua_cmsgpack.c b/examples/redis-unstable/deps/lua/src/lua_cmsgpack.c
deleted file mode 100644
index 5f8929d..0000000
--- a/examples/redis-unstable/deps/lua/src/lua_cmsgpack.c
+++ /dev/null
@@ -1,981 +0,0 @@
1#include <math.h>
2#include <stdlib.h>
3#include <stdint.h>
4#include <string.h>
5#include <assert.h>
6
7#include "lua.h"
8#include "lauxlib.h"
9
10#define LUACMSGPACK_NAME "cmsgpack"
11#define LUACMSGPACK_SAFE_NAME "cmsgpack_safe"
12#define LUACMSGPACK_VERSION "lua-cmsgpack 0.4.0"
13#define LUACMSGPACK_COPYRIGHT "Copyright (C) 2012, Salvatore Sanfilippo"
14#define LUACMSGPACK_DESCRIPTION "MessagePack C implementation for Lua"
15
16/* Allows a preprocessor directive to override MAX_NESTING */
17#ifndef LUACMSGPACK_MAX_NESTING
18 #define LUACMSGPACK_MAX_NESTING 16 /* Max tables nesting. */
19#endif
20
21/* Check if float or double can be an integer without loss of precision */
22#define IS_INT_TYPE_EQUIVALENT(x, T) (!isinf(x) && (T)(x) == (x))
23
24#define IS_INT64_EQUIVALENT(x) IS_INT_TYPE_EQUIVALENT(x, int64_t)
25#define IS_INT_EQUIVALENT(x) IS_INT_TYPE_EQUIVALENT(x, int)
26
27/* If size of pointer is equal to a 4 byte integer, we're on 32 bits. */
28#if UINTPTR_MAX == UINT_MAX
29 #define BITS_32 1
30#else
31 #define BITS_32 0
32#endif
33
34#if BITS_32
35 #define lua_pushunsigned(L, n) lua_pushnumber(L, n)
36#else
37 #define lua_pushunsigned(L, n) lua_pushinteger(L, n)
38#endif
39
40/* =============================================================================
41 * MessagePack implementation and bindings for Lua 5.1/5.2.
42 * Copyright(C) 2012 Salvatore Sanfilippo <antirez@gmail.com>
43 *
44 * http://github.com/antirez/lua-cmsgpack
45 *
46 * For MessagePack specification check the following web site:
47 * http://wiki.msgpack.org/display/MSGPACK/Format+specification
48 *
49 * See Copyright Notice at the end of this file.
50 *
51 * CHANGELOG:
52 * 19-Feb-2012 (ver 0.1.0): Initial release.
53 * 20-Feb-2012 (ver 0.2.0): Tables encoding improved.
54 * 20-Feb-2012 (ver 0.2.1): Minor bug fixing.
55 * 20-Feb-2012 (ver 0.3.0): Module renamed lua-cmsgpack (was lua-msgpack).
56 * 04-Apr-2014 (ver 0.3.1): Lua 5.2 support and minor bug fix.
57 * 07-Apr-2014 (ver 0.4.0): Multiple pack/unpack, lua allocator, efficiency.
58 * ========================================================================== */
59
60/* -------------------------- Endian conversion --------------------------------
61 * We use it only for floats and doubles, all the other conversions performed
62 * in an endian independent fashion. So the only thing we need is a function
63 * that swaps a binary string if arch is little endian (and left it untouched
64 * otherwise). */
65
66/* Reverse memory bytes if arch is little endian. Given the conceptual
67 * simplicity of the Lua build system we prefer check for endianess at runtime.
68 * The performance difference should be acceptable. */
69void memrevifle(void *ptr, size_t len) {
70 unsigned char *p = (unsigned char *)ptr,
71 *e = (unsigned char *)p+len-1,
72 aux;
73 int test = 1;
74 unsigned char *testp = (unsigned char*) &test;
75
76 if (testp[0] == 0) return; /* Big endian, nothing to do. */
77 len /= 2;
78 while(len--) {
79 aux = *p;
80 *p = *e;
81 *e = aux;
82 p++;
83 e--;
84 }
85}
86
87/* ---------------------------- String buffer ----------------------------------
88 * This is a simple implementation of string buffers. The only operation
89 * supported is creating empty buffers and appending bytes to it.
90 * The string buffer uses 2x preallocation on every realloc for O(N) append
91 * behavior. */
92
93typedef struct mp_buf {
94 unsigned char *b;
95 size_t len, free;
96} mp_buf;
97
98void *mp_realloc(lua_State *L, void *target, size_t osize,size_t nsize) {
99 void *(*local_realloc) (void *, void *, size_t osize, size_t nsize) = NULL;
100 void *ud;
101
102 local_realloc = lua_getallocf(L, &ud);
103
104 return local_realloc(ud, target, osize, nsize);
105}
106
107mp_buf *mp_buf_new(lua_State *L) {
108 mp_buf *buf = NULL;
109
110 /* Old size = 0; new size = sizeof(*buf) */
111 buf = (mp_buf*)mp_realloc(L, NULL, 0, sizeof(*buf));
112
113 buf->b = NULL;
114 buf->len = buf->free = 0;
115 return buf;
116}
117
118void mp_buf_append(lua_State *L, mp_buf *buf, const unsigned char *s, size_t len) {
119 if (buf->free < len) {
120 size_t newsize = buf->len+len;
121 if (newsize < buf->len || newsize >= SIZE_MAX/2) abort();
122 newsize *= 2;
123
124 buf->b = (unsigned char*)mp_realloc(L, buf->b, buf->len + buf->free, newsize);
125 buf->free = newsize - buf->len;
126 }
127 memcpy(buf->b+buf->len,s,len);
128 buf->len += len;
129 buf->free -= len;
130}
131
132void mp_buf_free(lua_State *L, mp_buf *buf) {
133 mp_realloc(L, buf->b, buf->len + buf->free, 0); /* realloc to 0 = free */
134 mp_realloc(L, buf, sizeof(*buf), 0);
135}
136
137/* ---------------------------- String cursor ----------------------------------
138 * This simple data structure is used for parsing. Basically you create a cursor
139 * using a string pointer and a length, then it is possible to access the
140 * current string position with cursor->p, check the remaining length
141 * in cursor->left, and finally consume more string using
142 * mp_cur_consume(cursor,len), to advance 'p' and subtract 'left'.
143 * An additional field cursor->error is set to zero on initialization and can
144 * be used to report errors. */
145
146#define MP_CUR_ERROR_NONE 0
147#define MP_CUR_ERROR_EOF 1 /* Not enough data to complete operation. */
148#define MP_CUR_ERROR_BADFMT 2 /* Bad data format */
149
150typedef struct mp_cur {
151 const unsigned char *p;
152 size_t left;
153 int err;
154} mp_cur;
155
156void mp_cur_init(mp_cur *cursor, const unsigned char *s, size_t len) {
157 cursor->p = s;
158 cursor->left = len;
159 cursor->err = MP_CUR_ERROR_NONE;
160}
161
162#define mp_cur_consume(_c,_len) do { _c->p += _len; _c->left -= _len; } while(0)
163
164/* When there is not enough room we set an error in the cursor and return. This
165 * is very common across the code so we have a macro to make the code look
166 * a bit simpler. */
167#define mp_cur_need(_c,_len) do { \
168 if (_c->left < _len) { \
169 _c->err = MP_CUR_ERROR_EOF; \
170 return; \
171 } \
172} while(0)
173
174/* ------------------------- Low level MP encoding -------------------------- */
175
176void mp_encode_bytes(lua_State *L, mp_buf *buf, const unsigned char *s, size_t len) {
177 unsigned char hdr[5];
178 size_t hdrlen;
179
180 if (len < 32) {
181 hdr[0] = 0xa0 | (len&0xff); /* fix raw */
182 hdrlen = 1;
183 } else if (len <= 0xff) {
184 hdr[0] = 0xd9;
185 hdr[1] = len;
186 hdrlen = 2;
187 } else if (len <= 0xffff) {
188 hdr[0] = 0xda;
189 hdr[1] = (len&0xff00)>>8;
190 hdr[2] = len&0xff;
191 hdrlen = 3;
192 } else {
193 hdr[0] = 0xdb;
194 hdr[1] = (len&0xff000000)>>24;
195 hdr[2] = (len&0xff0000)>>16;
196 hdr[3] = (len&0xff00)>>8;
197 hdr[4] = len&0xff;
198 hdrlen = 5;
199 }
200 mp_buf_append(L,buf,hdr,hdrlen);
201 mp_buf_append(L,buf,s,len);
202}
203
204/* we assume IEEE 754 internal format for single and double precision floats. */
205void mp_encode_double(lua_State *L, mp_buf *buf, double d) {
206 unsigned char b[9];
207 float f = d;
208
209 assert(sizeof(f) == 4 && sizeof(d) == 8);
210 if (d == (double)f) {
211 b[0] = 0xca; /* float IEEE 754 */
212 memcpy(b+1,&f,4);
213 memrevifle(b+1,4);
214 mp_buf_append(L,buf,b,5);
215 } else if (sizeof(d) == 8) {
216 b[0] = 0xcb; /* double IEEE 754 */
217 memcpy(b+1,&d,8);
218 memrevifle(b+1,8);
219 mp_buf_append(L,buf,b,9);
220 }
221}
222
223void mp_encode_int(lua_State *L, mp_buf *buf, int64_t n) {
224 unsigned char b[9];
225 size_t enclen;
226
227 if (n >= 0) {
228 if (n <= 127) {
229 b[0] = n & 0x7f; /* positive fixnum */
230 enclen = 1;
231 } else if (n <= 0xff) {
232 b[0] = 0xcc; /* uint 8 */
233 b[1] = n & 0xff;
234 enclen = 2;
235 } else if (n <= 0xffff) {
236 b[0] = 0xcd; /* uint 16 */
237 b[1] = (n & 0xff00) >> 8;
238 b[2] = n & 0xff;
239 enclen = 3;
240 } else if (n <= 0xffffffffLL) {
241 b[0] = 0xce; /* uint 32 */
242 b[1] = (n & 0xff000000) >> 24;
243 b[2] = (n & 0xff0000) >> 16;
244 b[3] = (n & 0xff00) >> 8;
245 b[4] = n & 0xff;
246 enclen = 5;
247 } else {
248 b[0] = 0xcf; /* uint 64 */
249 b[1] = (n & 0xff00000000000000LL) >> 56;
250 b[2] = (n & 0xff000000000000LL) >> 48;
251 b[3] = (n & 0xff0000000000LL) >> 40;
252 b[4] = (n & 0xff00000000LL) >> 32;
253 b[5] = (n & 0xff000000) >> 24;
254 b[6] = (n & 0xff0000) >> 16;
255 b[7] = (n & 0xff00) >> 8;
256 b[8] = n & 0xff;
257 enclen = 9;
258 }
259 } else {
260 if (n >= -32) {
261 b[0] = ((signed char)n); /* negative fixnum */
262 enclen = 1;
263 } else if (n >= -128) {
264 b[0] = 0xd0; /* int 8 */
265 b[1] = n & 0xff;
266 enclen = 2;
267 } else if (n >= -32768) {
268 b[0] = 0xd1; /* int 16 */
269 b[1] = (n & 0xff00) >> 8;
270 b[2] = n & 0xff;
271 enclen = 3;
272 } else if (n >= -2147483648LL) {
273 b[0] = 0xd2; /* int 32 */
274 b[1] = (n & 0xff000000) >> 24;
275 b[2] = (n & 0xff0000) >> 16;
276 b[3] = (n & 0xff00) >> 8;
277 b[4] = n & 0xff;
278 enclen = 5;
279 } else {
280 b[0] = 0xd3; /* int 64 */
281 b[1] = (n & 0xff00000000000000LL) >> 56;
282 b[2] = (n & 0xff000000000000LL) >> 48;
283 b[3] = (n & 0xff0000000000LL) >> 40;
284 b[4] = (n & 0xff00000000LL) >> 32;
285 b[5] = (n & 0xff000000) >> 24;
286 b[6] = (n & 0xff0000) >> 16;
287 b[7] = (n & 0xff00) >> 8;
288 b[8] = n & 0xff;
289 enclen = 9;
290 }
291 }
292 mp_buf_append(L,buf,b,enclen);
293}
294
295void mp_encode_array(lua_State *L, mp_buf *buf, uint64_t n) {
296 unsigned char b[5];
297 size_t enclen;
298
299 if (n <= 15) {
300 b[0] = 0x90 | (n & 0xf); /* fix array */
301 enclen = 1;
302 } else if (n <= 65535) {
303 b[0] = 0xdc; /* array 16 */
304 b[1] = (n & 0xff00) >> 8;
305 b[2] = n & 0xff;
306 enclen = 3;
307 } else {
308 b[0] = 0xdd; /* array 32 */
309 b[1] = (n & 0xff000000) >> 24;
310 b[2] = (n & 0xff0000) >> 16;
311 b[3] = (n & 0xff00) >> 8;
312 b[4] = n & 0xff;
313 enclen = 5;
314 }
315 mp_buf_append(L,buf,b,enclen);
316}
317
318void mp_encode_map(lua_State *L, mp_buf *buf, uint64_t n) {
319 unsigned char b[5];
320 int enclen;
321
322 if (n <= 15) {
323 b[0] = 0x80 | (n & 0xf); /* fix map */
324 enclen = 1;
325 } else if (n <= 65535) {
326 b[0] = 0xde; /* map 16 */
327 b[1] = (n & 0xff00) >> 8;
328 b[2] = n & 0xff;
329 enclen = 3;
330 } else {
331 b[0] = 0xdf; /* map 32 */
332 b[1] = (n & 0xff000000) >> 24;
333 b[2] = (n & 0xff0000) >> 16;
334 b[3] = (n & 0xff00) >> 8;
335 b[4] = n & 0xff;
336 enclen = 5;
337 }
338 mp_buf_append(L,buf,b,enclen);
339}
340
341/* --------------------------- Lua types encoding --------------------------- */
342
343void mp_encode_lua_string(lua_State *L, mp_buf *buf) {
344 size_t len;
345 const char *s;
346
347 s = lua_tolstring(L,-1,&len);
348 mp_encode_bytes(L,buf,(const unsigned char*)s,len);
349}
350
351void mp_encode_lua_bool(lua_State *L, mp_buf *buf) {
352 unsigned char b = lua_toboolean(L,-1) ? 0xc3 : 0xc2;
353 mp_buf_append(L,buf,&b,1);
354}
355
356/* Lua 5.3 has a built in 64-bit integer type */
357void mp_encode_lua_integer(lua_State *L, mp_buf *buf) {
358#if (LUA_VERSION_NUM < 503) && BITS_32
359 lua_Number i = lua_tonumber(L,-1);
360#else
361 lua_Integer i = lua_tointeger(L,-1);
362#endif
363 mp_encode_int(L, buf, (int64_t)i);
364}
365
366/* Lua 5.2 and lower only has 64-bit doubles, so we need to
367 * detect if the double may be representable as an int
368 * for Lua < 5.3 */
369void mp_encode_lua_number(lua_State *L, mp_buf *buf) {
370 lua_Number n = lua_tonumber(L,-1);
371
372 if (IS_INT64_EQUIVALENT(n)) {
373 mp_encode_lua_integer(L, buf);
374 } else {
375 mp_encode_double(L,buf,(double)n);
376 }
377}
378
379void mp_encode_lua_type(lua_State *L, mp_buf *buf, int level);
380
381/* Convert a lua table into a message pack list. */
382void mp_encode_lua_table_as_array(lua_State *L, mp_buf *buf, int level) {
383#if LUA_VERSION_NUM < 502
384 size_t len = lua_objlen(L,-1), j;
385#else
386 size_t len = lua_rawlen(L,-1), j;
387#endif
388
389 mp_encode_array(L,buf,len);
390 luaL_checkstack(L, 1, "in function mp_encode_lua_table_as_array");
391 for (j = 1; j <= len; j++) {
392 lua_pushnumber(L,j);
393 lua_gettable(L,-2);
394 mp_encode_lua_type(L,buf,level+1);
395 }
396}
397
398/* Convert a lua table into a message pack key-value map. */
399void mp_encode_lua_table_as_map(lua_State *L, mp_buf *buf, int level) {
400 size_t len = 0;
401
402 /* First step: count keys into table. No other way to do it with the
403 * Lua API, we need to iterate a first time. Note that an alternative
404 * would be to do a single run, and then hack the buffer to insert the
405 * map opcodes for message pack. Too hackish for this lib. */
406 luaL_checkstack(L, 3, "in function mp_encode_lua_table_as_map");
407 lua_pushnil(L);
408 while(lua_next(L,-2)) {
409 lua_pop(L,1); /* remove value, keep key for next iteration. */
410 len++;
411 }
412
413 /* Step two: actually encoding of the map. */
414 mp_encode_map(L,buf,len);
415 lua_pushnil(L);
416 while(lua_next(L,-2)) {
417 /* Stack: ... key value */
418 lua_pushvalue(L,-2); /* Stack: ... key value key */
419 mp_encode_lua_type(L,buf,level+1); /* encode key */
420 mp_encode_lua_type(L,buf,level+1); /* encode val */
421 }
422}
423
424/* Returns true if the Lua table on top of the stack is exclusively composed
425 * of keys from numerical keys from 1 up to N, with N being the total number
426 * of elements, without any hole in the middle. */
427int table_is_an_array(lua_State *L) {
428 int count = 0, max = 0;
429#if LUA_VERSION_NUM < 503
430 lua_Number n;
431#else
432 lua_Integer n;
433#endif
434
435 /* Stack top on function entry */
436 int stacktop;
437
438 stacktop = lua_gettop(L);
439
440 luaL_checkstack(L, 2, "in function table_is_an_array");
441 lua_pushnil(L);
442 while(lua_next(L,-2)) {
443 /* Stack: ... key value */
444 lua_pop(L,1); /* Stack: ... key */
445 /* The <= 0 check is valid here because we're comparing indexes. */
446#if LUA_VERSION_NUM < 503
447 if ((LUA_TNUMBER != lua_type(L,-1)) || (n = lua_tonumber(L, -1)) <= 0 ||
448 !IS_INT_EQUIVALENT(n))
449#else
450 if (!lua_isinteger(L,-1) || (n = lua_tointeger(L, -1)) <= 0)
451#endif
452 {
453 lua_settop(L, stacktop);
454 return 0;
455 }
456 max = (n > max ? n : max);
457 count++;
458 }
459 /* We have the total number of elements in "count". Also we have
460 * the max index encountered in "max". We can't reach this code
461 * if there are indexes <= 0. If you also note that there can not be
462 * repeated keys into a table, you have that if max==count you are sure
463 * that there are all the keys form 1 to count (both included). */
464 lua_settop(L, stacktop);
465 return max == count;
466}
467
468/* If the length operator returns non-zero, that is, there is at least
469 * an object at key '1', we serialize to message pack list. Otherwise
470 * we use a map. */
471void mp_encode_lua_table(lua_State *L, mp_buf *buf, int level) {
472 if (table_is_an_array(L))
473 mp_encode_lua_table_as_array(L,buf,level);
474 else
475 mp_encode_lua_table_as_map(L,buf,level);
476}
477
478void mp_encode_lua_null(lua_State *L, mp_buf *buf) {
479 unsigned char b[1];
480
481 b[0] = 0xc0;
482 mp_buf_append(L,buf,b,1);
483}
484
485void mp_encode_lua_type(lua_State *L, mp_buf *buf, int level) {
486 int t = lua_type(L,-1);
487
488 /* Limit the encoding of nested tables to a specified maximum depth, so that
489 * we survive when called against circular references in tables. */
490 if (t == LUA_TTABLE && level == LUACMSGPACK_MAX_NESTING) t = LUA_TNIL;
491 switch(t) {
492 case LUA_TSTRING: mp_encode_lua_string(L,buf); break;
493 case LUA_TBOOLEAN: mp_encode_lua_bool(L,buf); break;
494 case LUA_TNUMBER:
495 #if LUA_VERSION_NUM < 503
496 mp_encode_lua_number(L,buf); break;
497 #else
498 if (lua_isinteger(L, -1)) {
499 mp_encode_lua_integer(L, buf);
500 } else {
501 mp_encode_lua_number(L, buf);
502 }
503 break;
504 #endif
505 case LUA_TTABLE: mp_encode_lua_table(L,buf,level); break;
506 default: mp_encode_lua_null(L,buf); break;
507 }
508 lua_pop(L,1);
509}
510
511/*
512 * Packs all arguments as a stream for multiple upacking later.
513 * Returns error if no arguments provided.
514 */
515int mp_pack(lua_State *L) {
516 int nargs = lua_gettop(L);
517 int i;
518 mp_buf *buf;
519
520 if (nargs == 0)
521 return luaL_argerror(L, 0, "MessagePack pack needs input.");
522
523 if (!lua_checkstack(L, nargs))
524 return luaL_argerror(L, 0, "Too many arguments for MessagePack pack.");
525
526 buf = mp_buf_new(L);
527 for(i = 1; i <= nargs; i++) {
528 /* Copy argument i to top of stack for _encode processing;
529 * the encode function pops it from the stack when complete. */
530 luaL_checkstack(L, 1, "in function mp_check");
531 lua_pushvalue(L, i);
532
533 mp_encode_lua_type(L,buf,0);
534
535 lua_pushlstring(L,(char*)buf->b,buf->len);
536
537 /* Reuse the buffer for the next operation by
538 * setting its free count to the total buffer size
539 * and the current position to zero. */
540 buf->free += buf->len;
541 buf->len = 0;
542 }
543 mp_buf_free(L, buf);
544
545 /* Concatenate all nargs buffers together */
546 lua_concat(L, nargs);
547 return 1;
548}
549
550/* ------------------------------- Decoding --------------------------------- */
551
552void mp_decode_to_lua_type(lua_State *L, mp_cur *c);
553
554void mp_decode_to_lua_array(lua_State *L, mp_cur *c, size_t len) {
555 assert(len <= UINT_MAX);
556 int index = 1;
557
558 lua_newtable(L);
559 luaL_checkstack(L, 1, "in function mp_decode_to_lua_array");
560 while(len--) {
561 lua_pushnumber(L,index++);
562 mp_decode_to_lua_type(L,c);
563 if (c->err) return;
564 lua_settable(L,-3);
565 }
566}
567
568void mp_decode_to_lua_hash(lua_State *L, mp_cur *c, size_t len) {
569 assert(len <= UINT_MAX);
570 lua_newtable(L);
571 while(len--) {
572 mp_decode_to_lua_type(L,c); /* key */
573 if (c->err) return;
574 mp_decode_to_lua_type(L,c); /* value */
575 if (c->err) return;
576 lua_settable(L,-3);
577 }
578}
579
580/* Decode a Message Pack raw object pointed by the string cursor 'c' to
581 * a Lua type, that is left as the only result on the stack. */
582void mp_decode_to_lua_type(lua_State *L, mp_cur *c) {
583 mp_cur_need(c,1);
584
585 /* If we return more than 18 elements, we must resize the stack to
586 * fit all our return values. But, there is no way to
587 * determine how many objects a msgpack will unpack to up front, so
588 * we request a +1 larger stack on each iteration (noop if stack is
589 * big enough, and when stack does require resize it doubles in size) */
590 luaL_checkstack(L, 1,
591 "too many return values at once; "
592 "use unpack_one or unpack_limit instead.");
593
594 switch(c->p[0]) {
595 case 0xcc: /* uint 8 */
596 mp_cur_need(c,2);
597 lua_pushunsigned(L,c->p[1]);
598 mp_cur_consume(c,2);
599 break;
600 case 0xd0: /* int 8 */
601 mp_cur_need(c,2);
602 lua_pushinteger(L,(signed char)c->p[1]);
603 mp_cur_consume(c,2);
604 break;
605 case 0xcd: /* uint 16 */
606 mp_cur_need(c,3);
607 lua_pushunsigned(L,
608 (c->p[1] << 8) |
609 c->p[2]);
610 mp_cur_consume(c,3);
611 break;
612 case 0xd1: /* int 16 */
613 mp_cur_need(c,3);
614 lua_pushinteger(L,(int16_t)
615 (c->p[1] << 8) |
616 c->p[2]);
617 mp_cur_consume(c,3);
618 break;
619 case 0xce: /* uint 32 */
620 mp_cur_need(c,5);
621 lua_pushunsigned(L,
622 ((uint32_t)c->p[1] << 24) |
623 ((uint32_t)c->p[2] << 16) |
624 ((uint32_t)c->p[3] << 8) |
625 (uint32_t)c->p[4]);
626 mp_cur_consume(c,5);
627 break;
628 case 0xd2: /* int 32 */
629 mp_cur_need(c,5);
630 lua_pushinteger(L,
631 ((int32_t)c->p[1] << 24) |
632 ((int32_t)c->p[2] << 16) |
633 ((int32_t)c->p[3] << 8) |
634 (int32_t)c->p[4]);
635 mp_cur_consume(c,5);
636 break;
637 case 0xcf: /* uint 64 */
638 mp_cur_need(c,9);
639 lua_pushunsigned(L,
640 ((uint64_t)c->p[1] << 56) |
641 ((uint64_t)c->p[2] << 48) |
642 ((uint64_t)c->p[3] << 40) |
643 ((uint64_t)c->p[4] << 32) |
644 ((uint64_t)c->p[5] << 24) |
645 ((uint64_t)c->p[6] << 16) |
646 ((uint64_t)c->p[7] << 8) |
647 (uint64_t)c->p[8]);
648 mp_cur_consume(c,9);
649 break;
650 case 0xd3: /* int 64 */
651 mp_cur_need(c,9);
652#if LUA_VERSION_NUM < 503
653 lua_pushnumber(L,
654#else
655 lua_pushinteger(L,
656#endif
657 ((int64_t)c->p[1] << 56) |
658 ((int64_t)c->p[2] << 48) |
659 ((int64_t)c->p[3] << 40) |
660 ((int64_t)c->p[4] << 32) |
661 ((int64_t)c->p[5] << 24) |
662 ((int64_t)c->p[6] << 16) |
663 ((int64_t)c->p[7] << 8) |
664 (int64_t)c->p[8]);
665 mp_cur_consume(c,9);
666 break;
667 case 0xc0: /* nil */
668 lua_pushnil(L);
669 mp_cur_consume(c,1);
670 break;
671 case 0xc3: /* true */
672 lua_pushboolean(L,1);
673 mp_cur_consume(c,1);
674 break;
675 case 0xc2: /* false */
676 lua_pushboolean(L,0);
677 mp_cur_consume(c,1);
678 break;
679 case 0xca: /* float */
680 mp_cur_need(c,5);
681 assert(sizeof(float) == 4);
682 {
683 float f;
684 memcpy(&f,c->p+1,4);
685 memrevifle(&f,4);
686 lua_pushnumber(L,f);
687 mp_cur_consume(c,5);
688 }
689 break;
690 case 0xcb: /* double */
691 mp_cur_need(c,9);
692 assert(sizeof(double) == 8);
693 {
694 double d;
695 memcpy(&d,c->p+1,8);
696 memrevifle(&d,8);
697 lua_pushnumber(L,d);
698 mp_cur_consume(c,9);
699 }
700 break;
701 case 0xd9: /* raw 8 */
702 mp_cur_need(c,2);
703 {
704 size_t l = c->p[1];
705 mp_cur_need(c,2+l);
706 lua_pushlstring(L,(char*)c->p+2,l);
707 mp_cur_consume(c,2+l);
708 }
709 break;
710 case 0xda: /* raw 16 */
711 mp_cur_need(c,3);
712 {
713 size_t l = (c->p[1] << 8) | c->p[2];
714 mp_cur_need(c,3+l);
715 lua_pushlstring(L,(char*)c->p+3,l);
716 mp_cur_consume(c,3+l);
717 }
718 break;
719 case 0xdb: /* raw 32 */
720 mp_cur_need(c,5);
721 {
722 size_t l = ((size_t)c->p[1] << 24) |
723 ((size_t)c->p[2] << 16) |
724 ((size_t)c->p[3] << 8) |
725 (size_t)c->p[4];
726 mp_cur_consume(c,5);
727 mp_cur_need(c,l);
728 lua_pushlstring(L,(char*)c->p,l);
729 mp_cur_consume(c,l);
730 }
731 break;
732 case 0xdc: /* array 16 */
733 mp_cur_need(c,3);
734 {
735 size_t l = (c->p[1] << 8) | c->p[2];
736 mp_cur_consume(c,3);
737 mp_decode_to_lua_array(L,c,l);
738 }
739 break;
740 case 0xdd: /* array 32 */
741 mp_cur_need(c,5);
742 {
743 size_t l = ((size_t)c->p[1] << 24) |
744 ((size_t)c->p[2] << 16) |
745 ((size_t)c->p[3] << 8) |
746 (size_t)c->p[4];
747 mp_cur_consume(c,5);
748 mp_decode_to_lua_array(L,c,l);
749 }
750 break;
751 case 0xde: /* map 16 */
752 mp_cur_need(c,3);
753 {
754 size_t l = (c->p[1] << 8) | c->p[2];
755 mp_cur_consume(c,3);
756 mp_decode_to_lua_hash(L,c,l);
757 }
758 break;
759 case 0xdf: /* map 32 */
760 mp_cur_need(c,5);
761 {
762 size_t l = ((size_t)c->p[1] << 24) |
763 ((size_t)c->p[2] << 16) |
764 ((size_t)c->p[3] << 8) |
765 (size_t)c->p[4];
766 mp_cur_consume(c,5);
767 mp_decode_to_lua_hash(L,c,l);
768 }
769 break;
770 default: /* types that can't be idenitified by first byte value. */
771 if ((c->p[0] & 0x80) == 0) { /* positive fixnum */
772 lua_pushunsigned(L,c->p[0]);
773 mp_cur_consume(c,1);
774 } else if ((c->p[0] & 0xe0) == 0xe0) { /* negative fixnum */
775 lua_pushinteger(L,(signed char)c->p[0]);
776 mp_cur_consume(c,1);
777 } else if ((c->p[0] & 0xe0) == 0xa0) { /* fix raw */
778 size_t l = c->p[0] & 0x1f;
779 mp_cur_need(c,1+l);
780 lua_pushlstring(L,(char*)c->p+1,l);
781 mp_cur_consume(c,1+l);
782 } else if ((c->p[0] & 0xf0) == 0x90) { /* fix map */
783 size_t l = c->p[0] & 0xf;
784 mp_cur_consume(c,1);
785 mp_decode_to_lua_array(L,c,l);
786 } else if ((c->p[0] & 0xf0) == 0x80) { /* fix map */
787 size_t l = c->p[0] & 0xf;
788 mp_cur_consume(c,1);
789 mp_decode_to_lua_hash(L,c,l);
790 } else {
791 c->err = MP_CUR_ERROR_BADFMT;
792 }
793 }
794}
795
796int mp_unpack_full(lua_State *L, lua_Integer limit, lua_Integer offset) {
797 size_t len;
798 const char *s;
799 mp_cur c;
800 int cnt; /* Number of objects unpacked */
801 int decode_all = (!limit && !offset);
802
803 s = luaL_checklstring(L,1,&len); /* if no match, exits */
804
805 if (offset < 0 || limit < 0) /* requesting negative off or lim is invalid */
806 return luaL_error(L,
807 "Invalid request to unpack with offset of %d and limit of %d.",
808 (int) offset, (int) len);
809 else if (offset > len)
810 return luaL_error(L,
811 "Start offset %d greater than input length %d.", (int) offset, (int) len);
812
813 if (decode_all) limit = INT_MAX;
814
815 mp_cur_init(&c,(const unsigned char *)s+offset,len-offset);
816
817 /* We loop over the decode because this could be a stream
818 * of multiple top-level values serialized together */
819 for(cnt = 0; c.left > 0 && cnt < limit; cnt++) {
820 mp_decode_to_lua_type(L,&c);
821
822 if (c.err == MP_CUR_ERROR_EOF) {
823 return luaL_error(L,"Missing bytes in input.");
824 } else if (c.err == MP_CUR_ERROR_BADFMT) {
825 return luaL_error(L,"Bad data format in input.");
826 }
827 }
828
829 if (!decode_all) {
830 /* c->left is the remaining size of the input buffer.
831 * subtract the entire buffer size from the unprocessed size
832 * to get our next start offset */
833 size_t new_offset = len - c.left;
834 if (new_offset > LONG_MAX) abort();
835
836 luaL_checkstack(L, 1, "in function mp_unpack_full");
837
838 /* Return offset -1 when we have have processed the entire buffer. */
839 lua_pushinteger(L, c.left == 0 ? -1 : (lua_Integer) new_offset);
840 /* Results are returned with the arg elements still
841 * in place. Lua takes care of only returning
842 * elements above the args for us.
843 * In this case, we have one arg on the stack
844 * for this function, so we insert our first return
845 * value at position 2. */
846 lua_insert(L, 2);
847 cnt += 1; /* increase return count by one to make room for offset */
848 }
849
850 return cnt;
851}
852
853int mp_unpack(lua_State *L) {
854 return mp_unpack_full(L, 0, 0);
855}
856
857int mp_unpack_one(lua_State *L) {
858 lua_Integer offset = luaL_optinteger(L, 2, 0);
859 /* Variable pop because offset may not exist */
860 lua_pop(L, lua_gettop(L)-1);
861 return mp_unpack_full(L, 1, offset);
862}
863
864int mp_unpack_limit(lua_State *L) {
865 lua_Integer limit = luaL_checkinteger(L, 2);
866 lua_Integer offset = luaL_optinteger(L, 3, 0);
867 /* Variable pop because offset may not exist */
868 lua_pop(L, lua_gettop(L)-1);
869
870 return mp_unpack_full(L, limit, offset);
871}
872
873int mp_safe(lua_State *L) {
874 int argc, err, total_results;
875
876 argc = lua_gettop(L);
877
878 /* This adds our function to the bottom of the stack
879 * (the "call this function" position) */
880 lua_pushvalue(L, lua_upvalueindex(1));
881 lua_insert(L, 1);
882
883 err = lua_pcall(L, argc, LUA_MULTRET, 0);
884 total_results = lua_gettop(L);
885
886 if (!err) {
887 return total_results;
888 } else {
889 lua_pushnil(L);
890 lua_insert(L,-2);
891 return 2;
892 }
893}
894
895/* -------------------------------------------------------------------------- */
896const struct luaL_Reg cmds[] = {
897 {"pack", mp_pack},
898 {"unpack", mp_unpack},
899 {"unpack_one", mp_unpack_one},
900 {"unpack_limit", mp_unpack_limit},
901 {0}
902};
903
904int luaopen_create(lua_State *L) {
905 int i;
906 /* Manually construct our module table instead of
907 * relying on _register or _newlib */
908 lua_newtable(L);
909
910 for (i = 0; i < (sizeof(cmds)/sizeof(*cmds) - 1); i++) {
911 lua_pushcfunction(L, cmds[i].func);
912 lua_setfield(L, -2, cmds[i].name);
913 }
914
915 /* Add metadata */
916 lua_pushliteral(L, LUACMSGPACK_NAME);
917 lua_setfield(L, -2, "_NAME");
918 lua_pushliteral(L, LUACMSGPACK_VERSION);
919 lua_setfield(L, -2, "_VERSION");
920 lua_pushliteral(L, LUACMSGPACK_COPYRIGHT);
921 lua_setfield(L, -2, "_COPYRIGHT");
922 lua_pushliteral(L, LUACMSGPACK_DESCRIPTION);
923 lua_setfield(L, -2, "_DESCRIPTION");
924 return 1;
925}
926
927LUALIB_API int luaopen_cmsgpack(lua_State *L) {
928 luaopen_create(L);
929
930#if LUA_VERSION_NUM < 502
931 /* Register name globally for 5.1 */
932 lua_pushvalue(L, -1);
933 lua_setglobal(L, LUACMSGPACK_NAME);
934#endif
935
936 return 1;
937}
938
939LUALIB_API int luaopen_cmsgpack_safe(lua_State *L) {
940 int i;
941
942 luaopen_cmsgpack(L);
943
944 /* Wrap all functions in the safe handler */
945 for (i = 0; i < (sizeof(cmds)/sizeof(*cmds) - 1); i++) {
946 lua_getfield(L, -1, cmds[i].name);
947 lua_pushcclosure(L, mp_safe, 1);
948 lua_setfield(L, -2, cmds[i].name);
949 }
950
951#if LUA_VERSION_NUM < 502
952 /* Register name globally for 5.1 */
953 lua_pushvalue(L, -1);
954 lua_setglobal(L, LUACMSGPACK_SAFE_NAME);
955#endif
956
957 return 1;
958}
959
960/******************************************************************************
961* Copyright (C) 2012 Salvatore Sanfilippo. All rights reserved.
962*
963* Permission is hereby granted, free of charge, to any person obtaining
964* a copy of this software and associated documentation files (the
965* "Software"), to deal in the Software without restriction, including
966* without limitation the rights to use, copy, modify, merge, publish,
967* distribute, sublicense, and/or sell copies of the Software, and to
968* permit persons to whom the Software is furnished to do so, subject to
969* the following conditions:
970*
971* The above copyright notice and this permission notice shall be
972* included in all copies or substantial portions of the Software.
973*
974* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
975* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
976* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
977* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
978* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
979* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
980* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
981******************************************************************************/
diff --git a/examples/redis-unstable/deps/lua/src/lua_struct.c b/examples/redis-unstable/deps/lua/src/lua_struct.c
deleted file mode 100644
index c58c8e7..0000000
--- a/examples/redis-unstable/deps/lua/src/lua_struct.c
+++ /dev/null
@@ -1,429 +0,0 @@
1/*
2** {======================================================
3** Library for packing/unpacking structures.
4** $Id: struct.c,v 1.7 2018/05/11 22:04:31 roberto Exp $
5** See Copyright Notice at the end of this file
6** =======================================================
7*/
8/*
9** Valid formats:
10** > - big endian
11** < - little endian
12** ![num] - alignment
13** x - pading
14** b/B - signed/unsigned byte
15** h/H - signed/unsigned short
16** l/L - signed/unsigned long
17** T - size_t
18** i/In - signed/unsigned integer with size 'n' (default is size of int)
19** cn - sequence of 'n' chars (from/to a string); when packing, n==0 means
20 the whole string; when unpacking, n==0 means use the previous
21 read number as the string length
22** s - zero-terminated string
23** f - float
24** d - double
25** ' ' - ignored
26*/
27
28
29#include <assert.h>
30#include <ctype.h>
31#include <limits.h>
32#include <stddef.h>
33#include <string.h>
34
35
36#include "lua.h"
37#include "lauxlib.h"
38
39
40#if (LUA_VERSION_NUM >= 502)
41
42#define luaL_register(L,n,f) luaL_newlib(L,f)
43
44#endif
45
46
47/* basic integer type */
48#if !defined(STRUCT_INT)
49#define STRUCT_INT long
50#endif
51
52typedef STRUCT_INT Inttype;
53
54/* corresponding unsigned version */
55typedef unsigned STRUCT_INT Uinttype;
56
57
58/* maximum size (in bytes) for integral types */
59#define MAXINTSIZE 32
60
61/* is 'x' a power of 2? */
62#define isp2(x) ((x) > 0 && ((x) & ((x) - 1)) == 0)
63
64/* dummy structure to get alignment requirements */
65struct cD {
66 char c;
67 double d;
68};
69
70
71#define PADDING (sizeof(struct cD) - sizeof(double))
72#define MAXALIGN (PADDING > sizeof(int) ? PADDING : sizeof(int))
73
74
75/* endian options */
76#define BIG 0
77#define LITTLE 1
78
79
80static union {
81 int dummy;
82 char endian;
83} const native = {1};
84
85
86typedef struct Header {
87 int endian;
88 int align;
89} Header;
90
91
92static int getnum (lua_State *L, const char **fmt, int df) {
93 if (!isdigit(**fmt)) /* no number? */
94 return df; /* return default value */
95 else {
96 int a = 0;
97 do {
98 if (a > (INT_MAX / 10) || a * 10 > (INT_MAX - (**fmt - '0')))
99 luaL_error(L, "integral size overflow");
100 a = a*10 + *((*fmt)++) - '0';
101 } while (isdigit(**fmt));
102 return a;
103 }
104}
105
106
107#define defaultoptions(h) ((h)->endian = native.endian, (h)->align = 1)
108
109
110
111static size_t optsize (lua_State *L, char opt, const char **fmt) {
112 switch (opt) {
113 case 'B': case 'b': return sizeof(char);
114 case 'H': case 'h': return sizeof(short);
115 case 'L': case 'l': return sizeof(long);
116 case 'T': return sizeof(size_t);
117 case 'f': return sizeof(float);
118 case 'd': return sizeof(double);
119 case 'x': return 1;
120 case 'c': return getnum(L, fmt, 1);
121 case 'i': case 'I': {
122 int sz = getnum(L, fmt, sizeof(int));
123 if (sz > MAXINTSIZE)
124 luaL_error(L, "integral size %d is larger than limit of %d",
125 sz, MAXINTSIZE);
126 return sz;
127 }
128 default: return 0; /* other cases do not need alignment */
129 }
130}
131
132
133/*
134** return number of bytes needed to align an element of size 'size'
135** at current position 'len'
136*/
137static int gettoalign (size_t len, Header *h, int opt, size_t size) {
138 if (size == 0 || opt == 'c') return 0;
139 if (size > (size_t)h->align)
140 size = h->align; /* respect max. alignment */
141 return (size - (len & (size - 1))) & (size - 1);
142}
143
144
145/*
146** options to control endianess and alignment
147*/
148static void controloptions (lua_State *L, int opt, const char **fmt,
149 Header *h) {
150 switch (opt) {
151 case ' ': return; /* ignore white spaces */
152 case '>': h->endian = BIG; return;
153 case '<': h->endian = LITTLE; return;
154 case '!': {
155 int a = getnum(L, fmt, MAXALIGN);
156 if (!isp2(a))
157 luaL_error(L, "alignment %d is not a power of 2", a);
158 h->align = a;
159 return;
160 }
161 default: {
162 const char *msg = lua_pushfstring(L, "invalid format option '%c'", opt);
163 luaL_argerror(L, 1, msg);
164 }
165 }
166}
167
168
169static void putinteger (lua_State *L, luaL_Buffer *b, int arg, int endian,
170 int size) {
171 lua_Number n = luaL_checknumber(L, arg);
172 Uinttype value;
173 char buff[MAXINTSIZE];
174 if (n < 0)
175 value = (Uinttype)(Inttype)n;
176 else
177 value = (Uinttype)n;
178 if (endian == LITTLE) {
179 int i;
180 for (i = 0; i < size; i++) {
181 buff[i] = (value & 0xff);
182 value >>= 8;
183 }
184 }
185 else {
186 int i;
187 for (i = size - 1; i >= 0; i--) {
188 buff[i] = (value & 0xff);
189 value >>= 8;
190 }
191 }
192 luaL_addlstring(b, buff, size);
193}
194
195
196static void correctbytes (char *b, int size, int endian) {
197 if (endian != native.endian) {
198 int i = 0;
199 while (i < --size) {
200 char temp = b[i];
201 b[i++] = b[size];
202 b[size] = temp;
203 }
204 }
205}
206
207
208static int b_pack (lua_State *L) {
209 luaL_Buffer b;
210 const char *fmt = luaL_checkstring(L, 1);
211 Header h;
212 int arg = 2;
213 size_t totalsize = 0;
214 defaultoptions(&h);
215 lua_pushnil(L); /* mark to separate arguments from string buffer */
216 luaL_buffinit(L, &b);
217 while (*fmt != '\0') {
218 int opt = *fmt++;
219 size_t size = optsize(L, opt, &fmt);
220 int toalign = gettoalign(totalsize, &h, opt, size);
221 totalsize += toalign;
222 while (toalign-- > 0) luaL_addchar(&b, '\0');
223 switch (opt) {
224 case 'b': case 'B': case 'h': case 'H':
225 case 'l': case 'L': case 'T': case 'i': case 'I': { /* integer types */
226 putinteger(L, &b, arg++, h.endian, size);
227 break;
228 }
229 case 'x': {
230 luaL_addchar(&b, '\0');
231 break;
232 }
233 case 'f': {
234 float f = (float)luaL_checknumber(L, arg++);
235 correctbytes((char *)&f, size, h.endian);
236 luaL_addlstring(&b, (char *)&f, size);
237 break;
238 }
239 case 'd': {
240 double d = luaL_checknumber(L, arg++);
241 correctbytes((char *)&d, size, h.endian);
242 luaL_addlstring(&b, (char *)&d, size);
243 break;
244 }
245 case 'c': case 's': {
246 size_t l;
247 const char *s = luaL_checklstring(L, arg++, &l);
248 if (size == 0) size = l;
249 luaL_argcheck(L, l >= (size_t)size, arg, "string too short");
250 luaL_addlstring(&b, s, size);
251 if (opt == 's') {
252 luaL_addchar(&b, '\0'); /* add zero at the end */
253 size++;
254 }
255 break;
256 }
257 default: controloptions(L, opt, &fmt, &h);
258 }
259 totalsize += size;
260 }
261 luaL_pushresult(&b);
262 return 1;
263}
264
265
266static lua_Number getinteger (const char *buff, int endian,
267 int issigned, int size) {
268 Uinttype l = 0;
269 int i;
270 if (endian == BIG) {
271 for (i = 0; i < size; i++) {
272 l <<= 8;
273 l |= (Uinttype)(unsigned char)buff[i];
274 }
275 }
276 else {
277 for (i = size - 1; i >= 0; i--) {
278 l <<= 8;
279 l |= (Uinttype)(unsigned char)buff[i];
280 }
281 }
282 if (!issigned)
283 return (lua_Number)l;
284 else { /* signed format */
285 Uinttype mask = (Uinttype)(~((Uinttype)0)) << (size*8 - 1);
286 if (l & mask) /* negative value? */
287 l |= mask; /* signal extension */
288 return (lua_Number)(Inttype)l;
289 }
290}
291
292
293static int b_unpack (lua_State *L) {
294 Header h;
295 const char *fmt = luaL_checkstring(L, 1);
296 size_t ld;
297 const char *data = luaL_checklstring(L, 2, &ld);
298 size_t pos = luaL_optinteger(L, 3, 1);
299 luaL_argcheck(L, pos > 0, 3, "offset must be 1 or greater");
300 pos--; /* Lua indexes are 1-based, but here we want 0-based for C
301 * pointer math. */
302 int n = 0; /* number of results */
303 defaultoptions(&h);
304 while (*fmt) {
305 int opt = *fmt++;
306 size_t size = optsize(L, opt, &fmt);
307 pos += gettoalign(pos, &h, opt, size);
308 luaL_argcheck(L, size <= ld && pos <= ld - size,
309 2, "data string too short");
310 /* stack space for item + next position */
311 luaL_checkstack(L, 2, "too many results");
312 switch (opt) {
313 case 'b': case 'B': case 'h': case 'H':
314 case 'l': case 'L': case 'T': case 'i': case 'I': { /* integer types */
315 int issigned = islower(opt);
316 lua_Number res = getinteger(data+pos, h.endian, issigned, size);
317 lua_pushnumber(L, res); n++;
318 break;
319 }
320 case 'x': {
321 break;
322 }
323 case 'f': {
324 float f;
325 memcpy(&f, data+pos, size);
326 correctbytes((char *)&f, sizeof(f), h.endian);
327 lua_pushnumber(L, f); n++;
328 break;
329 }
330 case 'd': {
331 double d;
332 memcpy(&d, data+pos, size);
333 correctbytes((char *)&d, sizeof(d), h.endian);
334 lua_pushnumber(L, d); n++;
335 break;
336 }
337 case 'c': {
338 if (size == 0) {
339 if (n == 0 || !lua_isnumber(L, -1))
340 luaL_error(L, "format 'c0' needs a previous size");
341 size = lua_tonumber(L, -1);
342 lua_pop(L, 1); n--;
343 luaL_argcheck(L, size <= ld && pos <= ld - size,
344 2, "data string too short");
345 }
346 lua_pushlstring(L, data+pos, size); n++;
347 break;
348 }
349 case 's': {
350 const char *e = (const char *)memchr(data+pos, '\0', ld - pos);
351 if (e == NULL)
352 luaL_error(L, "unfinished string in data");
353 size = (e - (data+pos)) + 1;
354 lua_pushlstring(L, data+pos, size - 1); n++;
355 break;
356 }
357 default: controloptions(L, opt, &fmt, &h);
358 }
359 pos += size;
360 }
361 lua_pushinteger(L, pos + 1); /* next position */
362 return n + 1;
363}
364
365
366static int b_size (lua_State *L) {
367 Header h;
368 const char *fmt = luaL_checkstring(L, 1);
369 size_t pos = 0;
370 defaultoptions(&h);
371 while (*fmt) {
372 int opt = *fmt++;
373 size_t size = optsize(L, opt, &fmt);
374 pos += gettoalign(pos, &h, opt, size);
375 if (opt == 's')
376 luaL_argerror(L, 1, "option 's' has no fixed size");
377 else if (opt == 'c' && size == 0)
378 luaL_argerror(L, 1, "option 'c0' has no fixed size");
379 if (!isalnum(opt))
380 controloptions(L, opt, &fmt, &h);
381 pos += size;
382 }
383 lua_pushinteger(L, pos);
384 return 1;
385}
386
387/* }====================================================== */
388
389
390
391static const struct luaL_Reg thislib[] = {
392 {"pack", b_pack},
393 {"unpack", b_unpack},
394 {"size", b_size},
395 {NULL, NULL}
396};
397
398
399LUALIB_API int luaopen_struct (lua_State *L);
400
401LUALIB_API int luaopen_struct (lua_State *L) {
402 luaL_register(L, "struct", thislib);
403 return 1;
404}
405
406
407/******************************************************************************
408* Copyright (C) 2010-2018 Lua.org, PUC-Rio. All rights reserved.
409*
410* Permission is hereby granted, free of charge, to any person obtaining
411* a copy of this software and associated documentation files (the
412* "Software"), to deal in the Software without restriction, including
413* without limitation the rights to use, copy, modify, merge, publish,
414* distribute, sublicense, and/or sell copies of the Software, and to
415* permit persons to whom the Software is furnished to do so, subject to
416* the following conditions:
417*
418* The above copyright notice and this permission notice shall be
419* included in all copies or substantial portions of the Software.
420*
421* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
422* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
423* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
424* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
425* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
426* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
427* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
428******************************************************************************/
429
diff --git a/examples/redis-unstable/deps/lua/src/luac.c b/examples/redis-unstable/deps/lua/src/luac.c
deleted file mode 100644
index d070173..0000000
--- a/examples/redis-unstable/deps/lua/src/luac.c
+++ /dev/null
@@ -1,200 +0,0 @@
1/*
2** $Id: luac.c,v 1.54 2006/06/02 17:37:11 lhf Exp $
3** Lua compiler (saves bytecodes to files; also list bytecodes)
4** See Copyright Notice in lua.h
5*/
6
7#include <errno.h>
8#include <stdio.h>
9#include <stdlib.h>
10#include <string.h>
11
12#define luac_c
13#define LUA_CORE
14
15#include "lua.h"
16#include "lauxlib.h"
17
18#include "ldo.h"
19#include "lfunc.h"
20#include "lmem.h"
21#include "lobject.h"
22#include "lopcodes.h"
23#include "lstring.h"
24#include "lundump.h"
25
26#define PROGNAME "luac" /* default program name */
27#define OUTPUT PROGNAME ".out" /* default output file */
28
29static int listing=0; /* list bytecodes? */
30static int dumping=1; /* dump bytecodes? */
31static int stripping=0; /* strip debug information? */
32static char Output[]={ OUTPUT }; /* default output file name */
33static const char* output=Output; /* actual output file name */
34static const char* progname=PROGNAME; /* actual program name */
35
36static void fatal(const char* message)
37{
38 fprintf(stderr,"%s: %s\n",progname,message);
39 exit(EXIT_FAILURE);
40}
41
42static void cannot(const char* what)
43{
44 fprintf(stderr,"%s: cannot %s %s: %s\n",progname,what,output,strerror(errno));
45 exit(EXIT_FAILURE);
46}
47
48static void usage(const char* message)
49{
50 if (*message=='-')
51 fprintf(stderr,"%s: unrecognized option " LUA_QS "\n",progname,message);
52 else
53 fprintf(stderr,"%s: %s\n",progname,message);
54 fprintf(stderr,
55 "usage: %s [options] [filenames].\n"
56 "Available options are:\n"
57 " - process stdin\n"
58 " -l list\n"
59 " -o name output to file " LUA_QL("name") " (default is \"%s\")\n"
60 " -p parse only\n"
61 " -s strip debug information\n"
62 " -v show version information\n"
63 " -- stop handling options\n",
64 progname,Output);
65 exit(EXIT_FAILURE);
66}
67
68#define IS(s) (strcmp(argv[i],s)==0)
69
70static int doargs(int argc, char* argv[])
71{
72 int i;
73 int version=0;
74 if (argv[0]!=NULL && *argv[0]!=0) progname=argv[0];
75 for (i=1; i<argc; i++)
76 {
77 if (*argv[i]!='-') /* end of options; keep it */
78 break;
79 else if (IS("--")) /* end of options; skip it */
80 {
81 ++i;
82 if (version) ++version;
83 break;
84 }
85 else if (IS("-")) /* end of options; use stdin */
86 break;
87 else if (IS("-l")) /* list */
88 ++listing;
89 else if (IS("-o")) /* output file */
90 {
91 output=argv[++i];
92 if (output==NULL || *output==0) usage(LUA_QL("-o") " needs argument");
93 if (IS("-")) output=NULL;
94 }
95 else if (IS("-p")) /* parse only */
96 dumping=0;
97 else if (IS("-s")) /* strip debug information */
98 stripping=1;
99 else if (IS("-v")) /* show version */
100 ++version;
101 else /* unknown option */
102 usage(argv[i]);
103 }
104 if (i==argc && (listing || !dumping))
105 {
106 dumping=0;
107 argv[--i]=Output;
108 }
109 if (version)
110 {
111 printf("%s %s\n",LUA_RELEASE,LUA_COPYRIGHT);
112 if (version==argc-1) exit(EXIT_SUCCESS);
113 }
114 return i;
115}
116
117#define toproto(L,i) (clvalue(L->top+(i))->l.p)
118
119static const Proto* combine(lua_State* L, int n)
120{
121 if (n==1)
122 return toproto(L,-1);
123 else
124 {
125 int i,pc;
126 Proto* f=luaF_newproto(L);
127 setptvalue2s(L,L->top,f); incr_top(L);
128 f->source=luaS_newliteral(L,"=(" PROGNAME ")");
129 f->maxstacksize=1;
130 pc=2*n+1;
131 f->code=luaM_newvector(L,pc,Instruction);
132 f->sizecode=pc;
133 f->p=luaM_newvector(L,n,Proto*);
134 f->sizep=n;
135 pc=0;
136 for (i=0; i<n; i++)
137 {
138 f->p[i]=toproto(L,i-n-1);
139 f->code[pc++]=CREATE_ABx(OP_CLOSURE,0,i);
140 f->code[pc++]=CREATE_ABC(OP_CALL,0,1,1);
141 }
142 f->code[pc++]=CREATE_ABC(OP_RETURN,0,1,0);
143 return f;
144 }
145}
146
147static int writer(lua_State* L, const void* p, size_t size, void* u)
148{
149 UNUSED(L);
150 return (fwrite(p,size,1,(FILE*)u)!=1) && (size!=0);
151}
152
153struct Smain {
154 int argc;
155 char** argv;
156};
157
158static int pmain(lua_State* L)
159{
160 struct Smain* s = (struct Smain*)lua_touserdata(L, 1);
161 int argc=s->argc;
162 char** argv=s->argv;
163 const Proto* f;
164 int i;
165 if (!lua_checkstack(L,argc)) fatal("too many input files");
166 for (i=0; i<argc; i++)
167 {
168 const char* filename=IS("-") ? NULL : argv[i];
169 if (luaL_loadfile(L,filename)!=0) fatal(lua_tostring(L,-1));
170 }
171 f=combine(L,argc);
172 if (listing) luaU_print(f,listing>1);
173 if (dumping)
174 {
175 FILE* D= (output==NULL) ? stdout : fopen(output,"wb");
176 if (D==NULL) cannot("open");
177 lua_lock(L);
178 luaU_dump(L,f,writer,D,stripping);
179 lua_unlock(L);
180 if (ferror(D)) cannot("write");
181 if (fclose(D)) cannot("close");
182 }
183 return 0;
184}
185
186int main(int argc, char* argv[])
187{
188 lua_State* L;
189 struct Smain s;
190 int i=doargs(argc,argv);
191 argc-=i; argv+=i;
192 if (argc<=0) usage("no input files given");
193 L=lua_open();
194 if (L==NULL) fatal("not enough memory for state");
195 s.argc=argc;
196 s.argv=argv;
197 if (lua_cpcall(L,pmain,&s)!=0) fatal(lua_tostring(L,-1));
198 lua_close(L);
199 return EXIT_SUCCESS;
200}
diff --git a/examples/redis-unstable/deps/lua/src/luaconf.h b/examples/redis-unstable/deps/lua/src/luaconf.h
deleted file mode 100644
index e2cb261..0000000
--- a/examples/redis-unstable/deps/lua/src/luaconf.h
+++ /dev/null
@@ -1,763 +0,0 @@
1/*
2** $Id: luaconf.h,v 1.82.1.7 2008/02/11 16:25:08 roberto Exp $
3** Configuration file for Lua
4** See Copyright Notice in lua.h
5*/
6
7
8#ifndef lconfig_h
9#define lconfig_h
10
11#include <limits.h>
12#include <stddef.h>
13
14
15/*
16** ==================================================================
17** Search for "@@" to find all configurable definitions.
18** ===================================================================
19*/
20
21
22/*
23@@ LUA_ANSI controls the use of non-ansi features.
24** CHANGE it (define it) if you want Lua to avoid the use of any
25** non-ansi feature or library.
26*/
27#if defined(__STRICT_ANSI__)
28#define LUA_ANSI
29#endif
30
31
32#if !defined(LUA_ANSI) && defined(_WIN32)
33#define LUA_WIN
34#endif
35
36#if defined(LUA_USE_LINUX)
37#define LUA_USE_POSIX
38#define LUA_USE_DLOPEN /* needs an extra library: -ldl */
39#define LUA_USE_READLINE /* needs some extra libraries */
40#endif
41
42#if defined(LUA_USE_MACOSX)
43#define LUA_USE_POSIX
44#define LUA_DL_DYLD /* does not need extra library */
45#endif
46
47
48
49/*
50@@ LUA_USE_POSIX includes all functionallity listed as X/Open System
51@* Interfaces Extension (XSI).
52** CHANGE it (define it) if your system is XSI compatible.
53*/
54#if defined(LUA_USE_POSIX)
55#define LUA_USE_MKSTEMP
56#define LUA_USE_ISATTY
57#define LUA_USE_POPEN
58#define LUA_USE_ULONGJMP
59#endif
60
61
62/*
63@@ LUA_PATH and LUA_CPATH are the names of the environment variables that
64@* Lua check to set its paths.
65@@ LUA_INIT is the name of the environment variable that Lua
66@* checks for initialization code.
67** CHANGE them if you want different names.
68*/
69#define LUA_PATH "LUA_PATH"
70#define LUA_CPATH "LUA_CPATH"
71#define LUA_INIT "LUA_INIT"
72
73
74/*
75@@ LUA_PATH_DEFAULT is the default path that Lua uses to look for
76@* Lua libraries.
77@@ LUA_CPATH_DEFAULT is the default path that Lua uses to look for
78@* C libraries.
79** CHANGE them if your machine has a non-conventional directory
80** hierarchy or if you want to install your libraries in
81** non-conventional directories.
82*/
83#if defined(_WIN32)
84/*
85** In Windows, any exclamation mark ('!') in the path is replaced by the
86** path of the directory of the executable file of the current process.
87*/
88#define LUA_LDIR "!\\lua\\"
89#define LUA_CDIR "!\\"
90#define LUA_PATH_DEFAULT \
91 ".\\?.lua;" LUA_LDIR"?.lua;" LUA_LDIR"?\\init.lua;" \
92 LUA_CDIR"?.lua;" LUA_CDIR"?\\init.lua"
93#define LUA_CPATH_DEFAULT \
94 ".\\?.dll;" LUA_CDIR"?.dll;" LUA_CDIR"loadall.dll"
95
96#else
97#define LUA_ROOT "/usr/local/"
98#define LUA_LDIR LUA_ROOT "share/lua/5.1/"
99#define LUA_CDIR LUA_ROOT "lib/lua/5.1/"
100#define LUA_PATH_DEFAULT \
101 "./?.lua;" LUA_LDIR"?.lua;" LUA_LDIR"?/init.lua;" \
102 LUA_CDIR"?.lua;" LUA_CDIR"?/init.lua"
103#define LUA_CPATH_DEFAULT \
104 "./?.so;" LUA_CDIR"?.so;" LUA_CDIR"loadall.so"
105#endif
106
107
108/*
109@@ LUA_DIRSEP is the directory separator (for submodules).
110** CHANGE it if your machine does not use "/" as the directory separator
111** and is not Windows. (On Windows Lua automatically uses "\".)
112*/
113#if defined(_WIN32)
114#define LUA_DIRSEP "\\"
115#else
116#define LUA_DIRSEP "/"
117#endif
118
119
120/*
121@@ LUA_PATHSEP is the character that separates templates in a path.
122@@ LUA_PATH_MARK is the string that marks the substitution points in a
123@* template.
124@@ LUA_EXECDIR in a Windows path is replaced by the executable's
125@* directory.
126@@ LUA_IGMARK is a mark to ignore all before it when bulding the
127@* luaopen_ function name.
128** CHANGE them if for some reason your system cannot use those
129** characters. (E.g., if one of those characters is a common character
130** in file/directory names.) Probably you do not need to change them.
131*/
132#define LUA_PATHSEP ";"
133#define LUA_PATH_MARK "?"
134#define LUA_EXECDIR "!"
135#define LUA_IGMARK "-"
136
137
138/*
139@@ LUA_INTEGER is the integral type used by lua_pushinteger/lua_tointeger.
140** CHANGE that if ptrdiff_t is not adequate on your machine. (On most
141** machines, ptrdiff_t gives a good choice between int or long.)
142*/
143#define LUA_INTEGER ptrdiff_t
144
145
146/*
147@@ LUA_API is a mark for all core API functions.
148@@ LUALIB_API is a mark for all standard library functions.
149** CHANGE them if you need to define those functions in some special way.
150** For instance, if you want to create one Windows DLL with the core and
151** the libraries, you may want to use the following definition (define
152** LUA_BUILD_AS_DLL to get it).
153*/
154#if defined(LUA_BUILD_AS_DLL)
155
156#if defined(LUA_CORE) || defined(LUA_LIB)
157#define LUA_API __declspec(dllexport)
158#else
159#define LUA_API __declspec(dllimport)
160#endif
161
162#else
163
164#define LUA_API extern
165
166#endif
167
168/* more often than not the libs go together with the core */
169#define LUALIB_API LUA_API
170
171
172/*
173@@ LUAI_FUNC is a mark for all extern functions that are not to be
174@* exported to outside modules.
175@@ LUAI_DATA is a mark for all extern (const) variables that are not to
176@* be exported to outside modules.
177** CHANGE them if you need to mark them in some special way. Elf/gcc
178** (versions 3.2 and later) mark them as "hidden" to optimize access
179** when Lua is compiled as a shared library.
180*/
181#if defined(luaall_c)
182#define LUAI_FUNC static
183#define LUAI_DATA /* empty */
184
185#elif defined(__GNUC__) && ((__GNUC__*100 + __GNUC_MINOR__) >= 302) && \
186 defined(__ELF__)
187#define LUAI_FUNC __attribute__((visibility("hidden"))) extern
188#define LUAI_DATA LUAI_FUNC
189
190#else
191#define LUAI_FUNC extern
192#define LUAI_DATA extern
193#endif
194
195
196
197/*
198@@ LUA_QL describes how error messages quote program elements.
199** CHANGE it if you want a different appearance.
200*/
201#define LUA_QL(x) "'" x "'"
202#define LUA_QS LUA_QL("%s")
203
204
205/*
206@@ LUA_IDSIZE gives the maximum size for the description of the source
207@* of a function in debug information.
208** CHANGE it if you want a different size.
209*/
210#define LUA_IDSIZE 60
211
212
213/*
214** {==================================================================
215** Stand-alone configuration
216** ===================================================================
217*/
218
219#if defined(lua_c) || defined(luaall_c)
220
221/*
222@@ lua_stdin_is_tty detects whether the standard input is a 'tty' (that
223@* is, whether we're running lua interactively).
224** CHANGE it if you have a better definition for non-POSIX/non-Windows
225** systems.
226*/
227#if defined(LUA_USE_ISATTY)
228#include <unistd.h>
229#define lua_stdin_is_tty() isatty(0)
230#elif defined(LUA_WIN)
231#include <io.h>
232#include <stdio.h>
233#define lua_stdin_is_tty() _isatty(_fileno(stdin))
234#else
235#define lua_stdin_is_tty() 1 /* assume stdin is a tty */
236#endif
237
238
239/*
240@@ LUA_PROMPT is the default prompt used by stand-alone Lua.
241@@ LUA_PROMPT2 is the default continuation prompt used by stand-alone Lua.
242** CHANGE them if you want different prompts. (You can also change the
243** prompts dynamically, assigning to globals _PROMPT/_PROMPT2.)
244*/
245#define LUA_PROMPT "> "
246#define LUA_PROMPT2 ">> "
247
248
249/*
250@@ LUA_PROGNAME is the default name for the stand-alone Lua program.
251** CHANGE it if your stand-alone interpreter has a different name and
252** your system is not able to detect that name automatically.
253*/
254#define LUA_PROGNAME "lua"
255
256
257/*
258@@ LUA_MAXINPUT is the maximum length for an input line in the
259@* stand-alone interpreter.
260** CHANGE it if you need longer lines.
261*/
262#define LUA_MAXINPUT 512
263
264
265/*
266@@ lua_readline defines how to show a prompt and then read a line from
267@* the standard input.
268@@ lua_saveline defines how to "save" a read line in a "history".
269@@ lua_freeline defines how to free a line read by lua_readline.
270** CHANGE them if you want to improve this functionality (e.g., by using
271** GNU readline and history facilities).
272*/
273#if defined(LUA_USE_READLINE)
274#include <stdio.h>
275#include <readline/readline.h>
276#include <readline/history.h>
277#define lua_readline(L,b,p) ((void)L, ((b)=readline(p)) != NULL)
278#define lua_saveline(L,idx) \
279 if (lua_strlen(L,idx) > 0) /* non-empty line? */ \
280 add_history(lua_tostring(L, idx)); /* add it to history */
281#define lua_freeline(L,b) ((void)L, free(b))
282#else
283#define lua_readline(L,b,p) \
284 ((void)L, fputs(p, stdout), fflush(stdout), /* show prompt */ \
285 fgets(b, LUA_MAXINPUT, stdin) != NULL) /* get line */
286#define lua_saveline(L,idx) { (void)L; (void)idx; }
287#define lua_freeline(L,b) { (void)L; (void)b; }
288#endif
289
290#endif
291
292/* }================================================================== */
293
294
295/*
296@@ LUAI_GCPAUSE defines the default pause between garbage-collector cycles
297@* as a percentage.
298** CHANGE it if you want the GC to run faster or slower (higher values
299** mean larger pauses which mean slower collection.) You can also change
300** this value dynamically.
301*/
302#define LUAI_GCPAUSE 200 /* 200% (wait memory to double before next GC) */
303
304
305/*
306@@ LUAI_GCMUL defines the default speed of garbage collection relative to
307@* memory allocation as a percentage.
308** CHANGE it if you want to change the granularity of the garbage
309** collection. (Higher values mean coarser collections. 0 represents
310** infinity, where each step performs a full collection.) You can also
311** change this value dynamically.
312*/
313#define LUAI_GCMUL 200 /* GC runs 'twice the speed' of memory allocation */
314
315
316
317/*
318@@ LUA_COMPAT_GETN controls compatibility with old getn behavior.
319** CHANGE it (define it) if you want exact compatibility with the
320** behavior of setn/getn in Lua 5.0.
321*/
322#undef LUA_COMPAT_GETN
323
324/*
325@@ LUA_COMPAT_LOADLIB controls compatibility about global loadlib.
326** CHANGE it to undefined as soon as you do not need a global 'loadlib'
327** function (the function is still available as 'package.loadlib').
328*/
329#undef LUA_COMPAT_LOADLIB
330
331/*
332@@ LUA_COMPAT_VARARG controls compatibility with old vararg feature.
333** CHANGE it to undefined as soon as your programs use only '...' to
334** access vararg parameters (instead of the old 'arg' table).
335*/
336#define LUA_COMPAT_VARARG
337
338/*
339@@ LUA_COMPAT_MOD controls compatibility with old math.mod function.
340** CHANGE it to undefined as soon as your programs use 'math.fmod' or
341** the new '%' operator instead of 'math.mod'.
342*/
343#define LUA_COMPAT_MOD
344
345/*
346@@ LUA_COMPAT_LSTR controls compatibility with old long string nesting
347@* facility.
348** CHANGE it to 2 if you want the old behaviour, or undefine it to turn
349** off the advisory error when nesting [[...]].
350*/
351#define LUA_COMPAT_LSTR 1
352
353/*
354@@ LUA_COMPAT_GFIND controls compatibility with old 'string.gfind' name.
355** CHANGE it to undefined as soon as you rename 'string.gfind' to
356** 'string.gmatch'.
357*/
358#define LUA_COMPAT_GFIND
359
360/*
361@@ LUA_COMPAT_OPENLIB controls compatibility with old 'luaL_openlib'
362@* behavior.
363** CHANGE it to undefined as soon as you replace to 'luaL_register'
364** your uses of 'luaL_openlib'
365*/
366#define LUA_COMPAT_OPENLIB
367
368
369
370/*
371@@ luai_apicheck is the assert macro used by the Lua-C API.
372** CHANGE luai_apicheck if you want Lua to perform some checks in the
373** parameters it gets from API calls. This may slow down the interpreter
374** a bit, but may be quite useful when debugging C code that interfaces
375** with Lua. A useful redefinition is to use assert.h.
376*/
377#if defined(LUA_USE_APICHECK)
378#include <assert.h>
379#define luai_apicheck(L,o) { (void)L; assert(o); }
380#else
381#define luai_apicheck(L,o) { (void)L; }
382#endif
383
384
385/*
386@@ LUAI_BITSINT defines the number of bits in an int.
387** CHANGE here if Lua cannot automatically detect the number of bits of
388** your machine. Probably you do not need to change this.
389*/
390/* avoid overflows in comparison */
391#if INT_MAX-20 < 32760
392#define LUAI_BITSINT 16
393#elif INT_MAX > 2147483640L
394/* int has at least 32 bits */
395#define LUAI_BITSINT 32
396#else
397#error "you must define LUA_BITSINT with number of bits in an integer"
398#endif
399
400
401/*
402@@ LUAI_UINT32 is an unsigned integer with at least 32 bits.
403@@ LUAI_INT32 is an signed integer with at least 32 bits.
404@@ LUAI_UMEM is an unsigned integer big enough to count the total
405@* memory used by Lua.
406@@ LUAI_MEM is a signed integer big enough to count the total memory
407@* used by Lua.
408** CHANGE here if for some weird reason the default definitions are not
409** good enough for your machine. (The definitions in the 'else'
410** part always works, but may waste space on machines with 64-bit
411** longs.) Probably you do not need to change this.
412*/
413#if LUAI_BITSINT >= 32
414#define LUAI_UINT32 unsigned int
415#define LUAI_INT32 int
416#define LUAI_MAXINT32 INT_MAX
417#define LUAI_UMEM size_t
418#define LUAI_MEM ptrdiff_t
419#else
420/* 16-bit ints */
421#define LUAI_UINT32 unsigned long
422#define LUAI_INT32 long
423#define LUAI_MAXINT32 LONG_MAX
424#define LUAI_UMEM unsigned long
425#define LUAI_MEM long
426#endif
427
428
429/*
430@@ LUAI_MAXCALLS limits the number of nested calls.
431** CHANGE it if you need really deep recursive calls. This limit is
432** arbitrary; its only purpose is to stop infinite recursion before
433** exhausting memory.
434*/
435#define LUAI_MAXCALLS 20000
436
437
438/*
439@@ LUAI_MAXCSTACK limits the number of Lua stack slots that a C function
440@* can use.
441** CHANGE it if you need lots of (Lua) stack space for your C
442** functions. This limit is arbitrary; its only purpose is to stop C
443** functions to consume unlimited stack space. (must be smaller than
444** -LUA_REGISTRYINDEX)
445*/
446#define LUAI_MAXCSTACK 8000
447
448
449
450/*
451** {==================================================================
452** CHANGE (to smaller values) the following definitions if your system
453** has a small C stack. (Or you may want to change them to larger
454** values if your system has a large C stack and these limits are
455** too rigid for you.) Some of these constants control the size of
456** stack-allocated arrays used by the compiler or the interpreter, while
457** others limit the maximum number of recursive calls that the compiler
458** or the interpreter can perform. Values too large may cause a C stack
459** overflow for some forms of deep constructs.
460** ===================================================================
461*/
462
463
464/*
465@@ LUAI_MAXCCALLS is the maximum depth for nested C calls (short) and
466@* syntactical nested non-terminals in a program.
467*/
468#define LUAI_MAXCCALLS 200
469
470
471/*
472@@ LUAI_MAXVARS is the maximum number of local variables per function
473@* (must be smaller than 250).
474*/
475#define LUAI_MAXVARS 200
476
477
478/*
479@@ LUAI_MAXUPVALUES is the maximum number of upvalues per function
480@* (must be smaller than 250).
481*/
482#define LUAI_MAXUPVALUES 60
483
484
485/*
486@@ LUAL_BUFFERSIZE is the buffer size used by the lauxlib buffer system.
487*/
488#define LUAL_BUFFERSIZE BUFSIZ
489
490/* }================================================================== */
491
492
493
494
495/*
496** {==================================================================
497@@ LUA_NUMBER is the type of numbers in Lua.
498** CHANGE the following definitions only if you want to build Lua
499** with a number type different from double. You may also need to
500** change lua_number2int & lua_number2integer.
501** ===================================================================
502*/
503
504#define LUA_NUMBER_DOUBLE
505#define LUA_NUMBER double
506
507/*
508@@ LUAI_UACNUMBER is the result of an 'usual argument conversion'
509@* over a number.
510*/
511#define LUAI_UACNUMBER double
512
513
514/*
515@@ LUA_NUMBER_SCAN is the format for reading numbers.
516@@ LUA_NUMBER_FMT is the format for writing numbers.
517@@ lua_number2str converts a number to a string.
518@@ LUAI_MAXNUMBER2STR is maximum size of previous conversion.
519@@ lua_str2number converts a string to a number.
520*/
521#define LUA_NUMBER_SCAN "%lf"
522#define LUA_NUMBER_FMT "%.14g"
523#define lua_number2str(s,n) sprintf((s), LUA_NUMBER_FMT, (n))
524#define LUAI_MAXNUMBER2STR 32 /* 16 digits, sign, point, and \0 */
525#define lua_str2number(s,p) strtod((s), (p))
526
527
528/*
529@@ The luai_num* macros define the primitive operations over numbers.
530*/
531#if defined(LUA_CORE)
532#include <math.h>
533#define luai_numadd(a,b) ((a)+(b))
534#define luai_numsub(a,b) ((a)-(b))
535#define luai_nummul(a,b) ((a)*(b))
536#define luai_numdiv(a,b) ((a)/(b))
537#define luai_nummod(a,b) ((a) - floor((a)/(b))*(b))
538#define luai_numpow(a,b) (pow(a,b))
539#define luai_numunm(a) (-(a))
540#define luai_numeq(a,b) ((a)==(b))
541#define luai_numlt(a,b) ((a)<(b))
542#define luai_numle(a,b) ((a)<=(b))
543#define luai_numisnan(a) (!luai_numeq((a), (a)))
544#endif
545
546
547/*
548@@ lua_number2int is a macro to convert lua_Number to int.
549@@ lua_number2integer is a macro to convert lua_Number to lua_Integer.
550** CHANGE them if you know a faster way to convert a lua_Number to
551** int (with any rounding method and without throwing errors) in your
552** system. In Pentium machines, a naive typecast from double to int
553** in C is extremely slow, so any alternative is worth trying.
554*/
555
556/* On a Pentium, resort to a trick */
557#if defined(LUA_NUMBER_DOUBLE) && !defined(LUA_ANSI) && !defined(__SSE2__) && \
558 (defined(__i386) || defined (_M_IX86) || defined(__i386__))
559
560/* On a Microsoft compiler, use assembler */
561#if defined(_MSC_VER)
562
563#define lua_number2int(i,d) __asm fld d __asm fistp i
564#define lua_number2integer(i,n) lua_number2int(i, n)
565
566/* the next trick should work on any Pentium, but sometimes clashes
567 with a DirectX idiosyncrasy */
568#else
569
570union luai_Cast { double l_d; long l_l; };
571#define lua_number2int(i,d) \
572 { volatile union luai_Cast u; u.l_d = (d) + 6755399441055744.0; (i) = u.l_l; }
573#define lua_number2integer(i,n) lua_number2int(i, n)
574
575#endif
576
577
578/* this option always works, but may be slow */
579#else
580#define lua_number2int(i,d) ((i)=(int)(d))
581#define lua_number2integer(i,d) ((i)=(lua_Integer)(d))
582
583#endif
584
585/* }================================================================== */
586
587
588/*
589@@ LUAI_USER_ALIGNMENT_T is a type that requires maximum alignment.
590** CHANGE it if your system requires alignments larger than double. (For
591** instance, if your system supports long doubles and they must be
592** aligned in 16-byte boundaries, then you should add long double in the
593** union.) Probably you do not need to change this.
594*/
595#define LUAI_USER_ALIGNMENT_T union { double u; void *s; long l; }
596
597
598/*
599@@ LUAI_THROW/LUAI_TRY define how Lua does exception handling.
600** CHANGE them if you prefer to use longjmp/setjmp even with C++
601** or if want/don't to use _longjmp/_setjmp instead of regular
602** longjmp/setjmp. By default, Lua handles errors with exceptions when
603** compiling as C++ code, with _longjmp/_setjmp when asked to use them,
604** and with longjmp/setjmp otherwise.
605*/
606#if defined(__cplusplus)
607/* C++ exceptions */
608#define LUAI_THROW(L,c) throw(c)
609#define LUAI_TRY(L,c,a) try { a } catch(...) \
610 { if ((c)->status == 0) (c)->status = -1; }
611#define luai_jmpbuf int /* dummy variable */
612
613#elif defined(LUA_USE_ULONGJMP)
614/* in Unix, try _longjmp/_setjmp (more efficient) */
615#define LUAI_THROW(L,c) _longjmp((c)->b, 1)
616#define LUAI_TRY(L,c,a) if (_setjmp((c)->b) == 0) { a }
617#define luai_jmpbuf jmp_buf
618
619#else
620/* default handling with long jumps */
621#define LUAI_THROW(L,c) longjmp((c)->b, 1)
622#define LUAI_TRY(L,c,a) if (setjmp((c)->b) == 0) { a }
623#define luai_jmpbuf jmp_buf
624
625#endif
626
627
628/*
629@@ LUA_MAXCAPTURES is the maximum number of captures that a pattern
630@* can do during pattern-matching.
631** CHANGE it if you need more captures. This limit is arbitrary.
632*/
633#define LUA_MAXCAPTURES 32
634
635
636/*
637@@ lua_tmpnam is the function that the OS library uses to create a
638@* temporary name.
639@@ LUA_TMPNAMBUFSIZE is the maximum size of a name created by lua_tmpnam.
640** CHANGE them if you have an alternative to tmpnam (which is considered
641** insecure) or if you want the original tmpnam anyway. By default, Lua
642** uses tmpnam except when POSIX is available, where it uses mkstemp.
643*/
644#if defined(loslib_c) || defined(luaall_c)
645
646#if defined(LUA_USE_MKSTEMP)
647#include <unistd.h>
648#define LUA_TMPNAMBUFSIZE 32
649#define lua_tmpnam(b,e) { \
650 strcpy(b, "/tmp/lua_XXXXXX"); \
651 e = mkstemp(b); \
652 if (e != -1) close(e); \
653 e = (e == -1); }
654
655#else
656#define LUA_TMPNAMBUFSIZE L_tmpnam
657#define lua_tmpnam(b,e) { e = (tmpnam(b) == NULL); }
658#endif
659
660#endif
661
662
663/*
664@@ lua_popen spawns a new process connected to the current one through
665@* the file streams.
666** CHANGE it if you have a way to implement it in your system.
667*/
668#if defined(LUA_USE_POPEN)
669
670#define lua_popen(L,c,m) ((void)L, fflush(NULL), popen(c,m))
671#define lua_pclose(L,file) ((void)L, (pclose(file) != -1))
672
673#elif defined(LUA_WIN)
674
675#define lua_popen(L,c,m) ((void)L, _popen(c,m))
676#define lua_pclose(L,file) ((void)L, (_pclose(file) != -1))
677
678#else
679
680#define lua_popen(L,c,m) ((void)((void)c, m), \
681 luaL_error(L, LUA_QL("popen") " not supported"), (FILE*)0)
682#define lua_pclose(L,file) ((void)((void)L, file), 0)
683
684#endif
685
686/*
687@@ LUA_DL_* define which dynamic-library system Lua should use.
688** CHANGE here if Lua has problems choosing the appropriate
689** dynamic-library system for your platform (either Windows' DLL, Mac's
690** dyld, or Unix's dlopen). If your system is some kind of Unix, there
691** is a good chance that it has dlopen, so LUA_DL_DLOPEN will work for
692** it. To use dlopen you also need to adapt the src/Makefile (probably
693** adding -ldl to the linker options), so Lua does not select it
694** automatically. (When you change the makefile to add -ldl, you must
695** also add -DLUA_USE_DLOPEN.)
696** If you do not want any kind of dynamic library, undefine all these
697** options.
698** By default, _WIN32 gets LUA_DL_DLL and MAC OS X gets LUA_DL_DYLD.
699*/
700#if defined(LUA_USE_DLOPEN)
701#define LUA_DL_DLOPEN
702#endif
703
704#if defined(LUA_WIN)
705#define LUA_DL_DLL
706#endif
707
708
709/*
710@@ LUAI_EXTRASPACE allows you to add user-specific data in a lua_State
711@* (the data goes just *before* the lua_State pointer).
712** CHANGE (define) this if you really need that. This value must be
713** a multiple of the maximum alignment required for your machine.
714*/
715#define LUAI_EXTRASPACE 0
716
717
718/*
719@@ luai_userstate* allow user-specific actions on threads.
720** CHANGE them if you defined LUAI_EXTRASPACE and need to do something
721** extra when a thread is created/deleted/resumed/yielded.
722*/
723#define luai_userstateopen(L) ((void)L)
724#define luai_userstateclose(L) ((void)L)
725#define luai_userstatethread(L,L1) ((void)L)
726#define luai_userstatefree(L) ((void)L)
727#define luai_userstateresume(L,n) ((void)L)
728#define luai_userstateyield(L,n) ((void)L)
729
730
731/*
732@@ LUA_INTFRMLEN is the length modifier for integer conversions
733@* in 'string.format'.
734@@ LUA_INTFRM_T is the integer type correspoding to the previous length
735@* modifier.
736** CHANGE them if your system supports long long or does not support long.
737*/
738
739#if defined(LUA_USELONGLONG)
740
741#define LUA_INTFRMLEN "ll"
742#define LUA_INTFRM_T long long
743
744#else
745
746#define LUA_INTFRMLEN "l"
747#define LUA_INTFRM_T long
748
749#endif
750
751
752
753/* =================================================================== */
754
755/*
756** Local configuration. You can use this space to add your redefinitions
757** without modifying the main part of the file.
758*/
759
760
761
762#endif
763
diff --git a/examples/redis-unstable/deps/lua/src/lualib.h b/examples/redis-unstable/deps/lua/src/lualib.h
deleted file mode 100644
index 469417f..0000000
--- a/examples/redis-unstable/deps/lua/src/lualib.h
+++ /dev/null
@@ -1,53 +0,0 @@
1/*
2** $Id: lualib.h,v 1.36.1.1 2007/12/27 13:02:25 roberto Exp $
3** Lua standard libraries
4** See Copyright Notice in lua.h
5*/
6
7
8#ifndef lualib_h
9#define lualib_h
10
11#include "lua.h"
12
13
14/* Key to file-handle type */
15#define LUA_FILEHANDLE "FILE*"
16
17
18#define LUA_COLIBNAME "coroutine"
19LUALIB_API int (luaopen_base) (lua_State *L);
20
21#define LUA_TABLIBNAME "table"
22LUALIB_API int (luaopen_table) (lua_State *L);
23
24#define LUA_IOLIBNAME "io"
25LUALIB_API int (luaopen_io) (lua_State *L);
26
27#define LUA_OSLIBNAME "os"
28LUALIB_API int (luaopen_os) (lua_State *L);
29
30#define LUA_STRLIBNAME "string"
31LUALIB_API int (luaopen_string) (lua_State *L);
32
33#define LUA_MATHLIBNAME "math"
34LUALIB_API int (luaopen_math) (lua_State *L);
35
36#define LUA_DBLIBNAME "debug"
37LUALIB_API int (luaopen_debug) (lua_State *L);
38
39#define LUA_LOADLIBNAME "package"
40LUALIB_API int (luaopen_package) (lua_State *L);
41
42
43/* open all previous libraries */
44LUALIB_API void (luaL_openlibs) (lua_State *L);
45
46
47
48#ifndef lua_assert
49#define lua_assert(x) ((void)0)
50#endif
51
52
53#endif
diff --git a/examples/redis-unstable/deps/lua/src/lundump.c b/examples/redis-unstable/deps/lua/src/lundump.c
deleted file mode 100644
index 8010a45..0000000
--- a/examples/redis-unstable/deps/lua/src/lundump.c
+++ /dev/null
@@ -1,227 +0,0 @@
1/*
2** $Id: lundump.c,v 2.7.1.4 2008/04/04 19:51:41 roberto Exp $
3** load precompiled Lua chunks
4** See Copyright Notice in lua.h
5*/
6
7#include <string.h>
8
9#define lundump_c
10#define LUA_CORE
11
12#include "lua.h"
13
14#include "ldebug.h"
15#include "ldo.h"
16#include "lfunc.h"
17#include "lmem.h"
18#include "lobject.h"
19#include "lstring.h"
20#include "lundump.h"
21#include "lzio.h"
22
23typedef struct {
24 lua_State* L;
25 ZIO* Z;
26 Mbuffer* b;
27 const char* name;
28} LoadState;
29
30#ifdef LUAC_TRUST_BINARIES
31#define IF(c,s)
32#define error(S,s)
33#else
34#define IF(c,s) if (c) error(S,s)
35
36static void error(LoadState* S, const char* why)
37{
38 luaO_pushfstring(S->L,"%s: %s in precompiled chunk",S->name,why);
39 luaD_throw(S->L,LUA_ERRSYNTAX);
40}
41#endif
42
43#define LoadMem(S,b,n,size) LoadBlock(S,b,(n)*(size))
44#define LoadByte(S) (lu_byte)LoadChar(S)
45#define LoadVar(S,x) LoadMem(S,&x,1,sizeof(x))
46#define LoadVector(S,b,n,size) LoadMem(S,b,n,size)
47
48static void LoadBlock(LoadState* S, void* b, size_t size)
49{
50 size_t r=luaZ_read(S->Z,b,size);
51 IF (r!=0, "unexpected end");
52}
53
54static int LoadChar(LoadState* S)
55{
56 char x;
57 LoadVar(S,x);
58 return x;
59}
60
61static int LoadInt(LoadState* S)
62{
63 int x;
64 LoadVar(S,x);
65 IF (x<0, "bad integer");
66 return x;
67}
68
69static lua_Number LoadNumber(LoadState* S)
70{
71 lua_Number x;
72 LoadVar(S,x);
73 return x;
74}
75
76static TString* LoadString(LoadState* S)
77{
78 size_t size;
79 LoadVar(S,size);
80 if (size==0)
81 return NULL;
82 else
83 {
84 char* s=luaZ_openspace(S->L,S->b,size);
85 LoadBlock(S,s,size);
86 return luaS_newlstr(S->L,s,size-1); /* remove trailing '\0' */
87 }
88}
89
90static void LoadCode(LoadState* S, Proto* f)
91{
92 int n=LoadInt(S);
93 f->code=luaM_newvector(S->L,n,Instruction);
94 f->sizecode=n;
95 LoadVector(S,f->code,n,sizeof(Instruction));
96}
97
98static Proto* LoadFunction(LoadState* S, TString* p);
99
100static void LoadConstants(LoadState* S, Proto* f)
101{
102 int i,n;
103 n=LoadInt(S);
104 f->k=luaM_newvector(S->L,n,TValue);
105 f->sizek=n;
106 for (i=0; i<n; i++) setnilvalue(&f->k[i]);
107 for (i=0; i<n; i++)
108 {
109 TValue* o=&f->k[i];
110 int t=LoadChar(S);
111 switch (t)
112 {
113 case LUA_TNIL:
114 setnilvalue(o);
115 break;
116 case LUA_TBOOLEAN:
117 setbvalue(o,LoadChar(S)!=0);
118 break;
119 case LUA_TNUMBER:
120 setnvalue(o,LoadNumber(S));
121 break;
122 case LUA_TSTRING:
123 setsvalue2n(S->L,o,LoadString(S));
124 break;
125 default:
126 error(S,"bad constant");
127 break;
128 }
129 }
130 n=LoadInt(S);
131 f->p=luaM_newvector(S->L,n,Proto*);
132 f->sizep=n;
133 for (i=0; i<n; i++) f->p[i]=NULL;
134 for (i=0; i<n; i++) f->p[i]=LoadFunction(S,f->source);
135}
136
137static void LoadDebug(LoadState* S, Proto* f)
138{
139 int i,n;
140 n=LoadInt(S);
141 f->lineinfo=luaM_newvector(S->L,n,int);
142 f->sizelineinfo=n;
143 LoadVector(S,f->lineinfo,n,sizeof(int));
144 n=LoadInt(S);
145 f->locvars=luaM_newvector(S->L,n,LocVar);
146 f->sizelocvars=n;
147 for (i=0; i<n; i++) f->locvars[i].varname=NULL;
148 for (i=0; i<n; i++)
149 {
150 f->locvars[i].varname=LoadString(S);
151 f->locvars[i].startpc=LoadInt(S);
152 f->locvars[i].endpc=LoadInt(S);
153 }
154 n=LoadInt(S);
155 f->upvalues=luaM_newvector(S->L,n,TString*);
156 f->sizeupvalues=n;
157 for (i=0; i<n; i++) f->upvalues[i]=NULL;
158 for (i=0; i<n; i++) f->upvalues[i]=LoadString(S);
159}
160
161static Proto* LoadFunction(LoadState* S, TString* p)
162{
163 Proto* f;
164 if (++S->L->nCcalls > LUAI_MAXCCALLS) error(S,"code too deep");
165 f=luaF_newproto(S->L);
166 setptvalue2s(S->L,S->L->top,f); incr_top(S->L);
167 f->source=LoadString(S); if (f->source==NULL) f->source=p;
168 f->linedefined=LoadInt(S);
169 f->lastlinedefined=LoadInt(S);
170 f->nups=LoadByte(S);
171 f->numparams=LoadByte(S);
172 f->is_vararg=LoadByte(S);
173 f->maxstacksize=LoadByte(S);
174 LoadCode(S,f);
175 LoadConstants(S,f);
176 LoadDebug(S,f);
177 IF (!luaG_checkcode(f), "bad code");
178 S->L->top--;
179 S->L->nCcalls--;
180 return f;
181}
182
183static void LoadHeader(LoadState* S)
184{
185 char h[LUAC_HEADERSIZE];
186 char s[LUAC_HEADERSIZE];
187 luaU_header(h);
188 LoadBlock(S,s,LUAC_HEADERSIZE);
189 IF (memcmp(h,s,LUAC_HEADERSIZE)!=0, "bad header");
190}
191
192/*
193** load precompiled chunk
194*/
195Proto* luaU_undump (lua_State* L, ZIO* Z, Mbuffer* buff, const char* name)
196{
197 LoadState S;
198 if (*name=='@' || *name=='=')
199 S.name=name+1;
200 else if (*name==LUA_SIGNATURE[0])
201 S.name="binary string";
202 else
203 S.name=name;
204 S.L=L;
205 S.Z=Z;
206 S.b=buff;
207 LoadHeader(&S);
208 return LoadFunction(&S,luaS_newliteral(L,"=?"));
209}
210
211/*
212* make header
213*/
214void luaU_header (char* h)
215{
216 int x=1;
217 memcpy(h,LUA_SIGNATURE,sizeof(LUA_SIGNATURE)-1);
218 h+=sizeof(LUA_SIGNATURE)-1;
219 *h++=(char)LUAC_VERSION;
220 *h++=(char)LUAC_FORMAT;
221 *h++=(char)*(char*)&x; /* endianness */
222 *h++=(char)sizeof(int);
223 *h++=(char)sizeof(size_t);
224 *h++=(char)sizeof(Instruction);
225 *h++=(char)sizeof(lua_Number);
226 *h++=(char)(((lua_Number)0.5)==0); /* is lua_Number integral? */
227}
diff --git a/examples/redis-unstable/deps/lua/src/lundump.h b/examples/redis-unstable/deps/lua/src/lundump.h
deleted file mode 100644
index c80189d..0000000
--- a/examples/redis-unstable/deps/lua/src/lundump.h
+++ /dev/null
@@ -1,36 +0,0 @@
1/*
2** $Id: lundump.h,v 1.37.1.1 2007/12/27 13:02:25 roberto Exp $
3** load precompiled Lua chunks
4** See Copyright Notice in lua.h
5*/
6
7#ifndef lundump_h
8#define lundump_h
9
10#include "lobject.h"
11#include "lzio.h"
12
13/* load one chunk; from lundump.c */
14LUAI_FUNC Proto* luaU_undump (lua_State* L, ZIO* Z, Mbuffer* buff, const char* name);
15
16/* make header; from lundump.c */
17LUAI_FUNC void luaU_header (char* h);
18
19/* dump one chunk; from ldump.c */
20LUAI_FUNC int luaU_dump (lua_State* L, const Proto* f, lua_Writer w, void* data, int strip);
21
22#ifdef luac_c
23/* print one chunk; from print.c */
24LUAI_FUNC void luaU_print (const Proto* f, int full);
25#endif
26
27/* for header of binary files -- this is Lua 5.1 */
28#define LUAC_VERSION 0x51
29
30/* for header of binary files -- this is the official format */
31#define LUAC_FORMAT 0
32
33/* size of header of binary files */
34#define LUAC_HEADERSIZE 12
35
36#endif
diff --git a/examples/redis-unstable/deps/lua/src/lvm.c b/examples/redis-unstable/deps/lua/src/lvm.c
deleted file mode 100644
index 5cd313b..0000000
--- a/examples/redis-unstable/deps/lua/src/lvm.c
+++ /dev/null
@@ -1,769 +0,0 @@
1/*
2** $Id: lvm.c,v 2.63.1.5 2011/08/17 20:43:11 roberto Exp $
3** Lua virtual machine
4** See Copyright Notice in lua.h
5*/
6
7
8#include <stdio.h>
9#include <stdlib.h>
10#include <string.h>
11
12#define lvm_c
13#define LUA_CORE
14
15#include "lua.h"
16
17#include "ldebug.h"
18#include "ldo.h"
19#include "lfunc.h"
20#include "lgc.h"
21#include "lobject.h"
22#include "lopcodes.h"
23#include "lstate.h"
24#include "lstring.h"
25#include "ltable.h"
26#include "ltm.h"
27#include "lvm.h"
28
29
30
31/* limit for table tag-method chains (to avoid loops) */
32#define MAXTAGLOOP 100
33
34
35const TValue *luaV_tonumber (const TValue *obj, TValue *n) {
36 lua_Number num;
37 if (ttisnumber(obj)) return obj;
38 if (ttisstring(obj) && luaO_str2d(svalue(obj), &num)) {
39 setnvalue(n, num);
40 return n;
41 }
42 else
43 return NULL;
44}
45
46
47int luaV_tostring (lua_State *L, StkId obj) {
48 if (!ttisnumber(obj))
49 return 0;
50 else {
51 char s[LUAI_MAXNUMBER2STR];
52 lua_Number n = nvalue(obj);
53 lua_number2str(s, n);
54 setsvalue2s(L, obj, luaS_new(L, s));
55 return 1;
56 }
57}
58
59
60static void traceexec (lua_State *L, const Instruction *pc) {
61 lu_byte mask = L->hookmask;
62 const Instruction *oldpc = L->savedpc;
63 L->savedpc = pc;
64 if ((mask & LUA_MASKCOUNT) && L->hookcount == 0) {
65 resethookcount(L);
66 luaD_callhook(L, LUA_HOOKCOUNT, -1);
67 }
68 if (mask & LUA_MASKLINE) {
69 Proto *p = ci_func(L->ci)->l.p;
70 int npc = pcRel(pc, p);
71 int newline = getline(p, npc);
72 /* call linehook when enter a new function, when jump back (loop),
73 or when enter a new line */
74 if (npc == 0 || pc <= oldpc || newline != getline(p, pcRel(oldpc, p)))
75 luaD_callhook(L, LUA_HOOKLINE, newline);
76 }
77}
78
79
80static void callTMres (lua_State *L, StkId res, const TValue *f,
81 const TValue *p1, const TValue *p2) {
82 ptrdiff_t result = savestack(L, res);
83 setobj2s(L, L->top, f); /* push function */
84 setobj2s(L, L->top+1, p1); /* 1st argument */
85 setobj2s(L, L->top+2, p2); /* 2nd argument */
86 luaD_checkstack(L, 3);
87 L->top += 3;
88 luaD_call(L, L->top - 3, 1);
89 res = restorestack(L, result);
90 L->top--;
91 setobjs2s(L, res, L->top);
92}
93
94
95
96static void callTM (lua_State *L, const TValue *f, const TValue *p1,
97 const TValue *p2, const TValue *p3) {
98 setobj2s(L, L->top, f); /* push function */
99 setobj2s(L, L->top+1, p1); /* 1st argument */
100 setobj2s(L, L->top+2, p2); /* 2nd argument */
101 setobj2s(L, L->top+3, p3); /* 3th argument */
102 luaD_checkstack(L, 4);
103 L->top += 4;
104 luaD_call(L, L->top - 4, 0);
105}
106
107
108void luaV_gettable (lua_State *L, const TValue *t, TValue *key, StkId val) {
109 int loop;
110 for (loop = 0; loop < MAXTAGLOOP; loop++) {
111 const TValue *tm;
112 if (ttistable(t)) { /* `t' is a table? */
113 Table *h = hvalue(t);
114 const TValue *res = luaH_get(h, key); /* do a primitive get */
115 if (!ttisnil(res) || /* result is no nil? */
116 (tm = fasttm(L, h->metatable, TM_INDEX)) == NULL) { /* or no TM? */
117 setobj2s(L, val, res);
118 return;
119 }
120 /* else will try the tag method */
121 }
122 else if (ttisnil(tm = luaT_gettmbyobj(L, t, TM_INDEX)))
123 luaG_typeerror(L, t, "index");
124 if (ttisfunction(tm)) {
125 callTMres(L, val, tm, t, key);
126 return;
127 }
128 t = tm; /* else repeat with `tm' */
129 }
130 luaG_runerror(L, "loop in gettable");
131}
132
133
134void luaV_settable (lua_State *L, const TValue *t, TValue *key, StkId val) {
135 int loop;
136 TValue temp;
137 for (loop = 0; loop < MAXTAGLOOP; loop++) {
138 const TValue *tm;
139 if (ttistable(t)) { /* `t' is a table? */
140 Table *h = hvalue(t);
141 if (h->readonly)
142 luaG_runerror(L, "Attempt to modify a readonly table");
143 TValue *oldval = luaH_set(L, h, key); /* do a primitive set */
144 if (!ttisnil(oldval) || /* result is no nil? */
145 (tm = fasttm(L, h->metatable, TM_NEWINDEX)) == NULL) { /* or no TM? */
146 setobj2t(L, oldval, val);
147 h->flags = 0;
148 luaC_barriert(L, h, val);
149 return;
150 }
151 /* else will try the tag method */
152 }
153 else if (ttisnil(tm = luaT_gettmbyobj(L, t, TM_NEWINDEX)))
154 luaG_typeerror(L, t, "index");
155 if (ttisfunction(tm)) {
156 callTM(L, tm, t, key, val);
157 return;
158 }
159 /* else repeat with `tm' */
160 setobj(L, &temp, tm); /* avoid pointing inside table (may rehash) */
161 t = &temp;
162 }
163 luaG_runerror(L, "loop in settable");
164}
165
166
167static int call_binTM (lua_State *L, const TValue *p1, const TValue *p2,
168 StkId res, TMS event) {
169 const TValue *tm = luaT_gettmbyobj(L, p1, event); /* try first operand */
170 if (ttisnil(tm))
171 tm = luaT_gettmbyobj(L, p2, event); /* try second operand */
172 if (ttisnil(tm)) return 0;
173 callTMres(L, res, tm, p1, p2);
174 return 1;
175}
176
177
178static const TValue *get_compTM (lua_State *L, Table *mt1, Table *mt2,
179 TMS event) {
180 const TValue *tm1 = fasttm(L, mt1, event);
181 const TValue *tm2;
182 if (tm1 == NULL) return NULL; /* no metamethod */
183 if (mt1 == mt2) return tm1; /* same metatables => same metamethods */
184 tm2 = fasttm(L, mt2, event);
185 if (tm2 == NULL) return NULL; /* no metamethod */
186 if (luaO_rawequalObj(tm1, tm2)) /* same metamethods? */
187 return tm1;
188 return NULL;
189}
190
191
192static int call_orderTM (lua_State *L, const TValue *p1, const TValue *p2,
193 TMS event) {
194 const TValue *tm1 = luaT_gettmbyobj(L, p1, event);
195 const TValue *tm2;
196 if (ttisnil(tm1)) return -1; /* no metamethod? */
197 tm2 = luaT_gettmbyobj(L, p2, event);
198 if (!luaO_rawequalObj(tm1, tm2)) /* different metamethods? */
199 return -1;
200 callTMres(L, L->top, tm1, p1, p2);
201 return !l_isfalse(L->top);
202}
203
204
205static int l_strcmp (const TString *ls, const TString *rs) {
206 const char *l = getstr(ls);
207 size_t ll = ls->tsv.len;
208 const char *r = getstr(rs);
209 size_t lr = rs->tsv.len;
210 for (;;) {
211 int temp = strcoll(l, r);
212 if (temp != 0) return temp;
213 else { /* strings are equal up to a `\0' */
214 size_t len = strlen(l); /* index of first `\0' in both strings */
215 if (len == lr) /* r is finished? */
216 return (len == ll) ? 0 : 1;
217 else if (len == ll) /* l is finished? */
218 return -1; /* l is smaller than r (because r is not finished) */
219 /* both strings longer than `len'; go on comparing (after the `\0') */
220 len++;
221 l += len; ll -= len; r += len; lr -= len;
222 }
223 }
224}
225
226
227int luaV_lessthan (lua_State *L, const TValue *l, const TValue *r) {
228 int res;
229 if (ttype(l) != ttype(r))
230 return luaG_ordererror(L, l, r);
231 else if (ttisnumber(l))
232 return luai_numlt(nvalue(l), nvalue(r));
233 else if (ttisstring(l))
234 return l_strcmp(rawtsvalue(l), rawtsvalue(r)) < 0;
235 else if ((res = call_orderTM(L, l, r, TM_LT)) != -1)
236 return res;
237 return luaG_ordererror(L, l, r);
238}
239
240
241static int lessequal (lua_State *L, const TValue *l, const TValue *r) {
242 int res;
243 if (ttype(l) != ttype(r))
244 return luaG_ordererror(L, l, r);
245 else if (ttisnumber(l))
246 return luai_numle(nvalue(l), nvalue(r));
247 else if (ttisstring(l))
248 return l_strcmp(rawtsvalue(l), rawtsvalue(r)) <= 0;
249 else if ((res = call_orderTM(L, l, r, TM_LE)) != -1) /* first try `le' */
250 return res;
251 else if ((res = call_orderTM(L, r, l, TM_LT)) != -1) /* else try `lt' */
252 return !res;
253 return luaG_ordererror(L, l, r);
254}
255
256
257int luaV_equalval (lua_State *L, const TValue *t1, const TValue *t2) {
258 const TValue *tm;
259 lua_assert(ttype(t1) == ttype(t2));
260 switch (ttype(t1)) {
261 case LUA_TNIL: return 1;
262 case LUA_TNUMBER: return luai_numeq(nvalue(t1), nvalue(t2));
263 case LUA_TBOOLEAN: return bvalue(t1) == bvalue(t2); /* true must be 1 !! */
264 case LUA_TLIGHTUSERDATA: return pvalue(t1) == pvalue(t2);
265 case LUA_TUSERDATA: {
266 if (uvalue(t1) == uvalue(t2)) return 1;
267 tm = get_compTM(L, uvalue(t1)->metatable, uvalue(t2)->metatable,
268 TM_EQ);
269 break; /* will try TM */
270 }
271 case LUA_TTABLE: {
272 if (hvalue(t1) == hvalue(t2)) return 1;
273 tm = get_compTM(L, hvalue(t1)->metatable, hvalue(t2)->metatable, TM_EQ);
274 break; /* will try TM */
275 }
276 default: return gcvalue(t1) == gcvalue(t2);
277 }
278 if (tm == NULL) return 0; /* no TM? */
279 callTMres(L, L->top, tm, t1, t2); /* call TM */
280 return !l_isfalse(L->top);
281}
282
283
284void luaV_concat (lua_State *L, int total, int last) {
285 do {
286 StkId top = L->base + last + 1;
287 int n = 2; /* number of elements handled in this pass (at least 2) */
288 if (!(ttisstring(top-2) || ttisnumber(top-2)) || !tostring(L, top-1)) {
289 if (!call_binTM(L, top-2, top-1, top-2, TM_CONCAT))
290 luaG_concaterror(L, top-2, top-1);
291 } else if (tsvalue(top-1)->len == 0) /* second op is empty? */
292 (void)tostring(L, top - 2); /* result is first op (as string) */
293 else {
294 /* at least two string values; get as many as possible */
295 size_t tl = tsvalue(top-1)->len;
296 char *buffer;
297 int i;
298 /* collect total length */
299 for (n = 1; n < total && tostring(L, top-n-1); n++) {
300 size_t l = tsvalue(top-n-1)->len;
301 if (l >= MAX_SIZET - tl) luaG_runerror(L, "string length overflow");
302 tl += l;
303 }
304 buffer = luaZ_openspace(L, &G(L)->buff, tl);
305 tl = 0;
306 for (i=n; i>0; i--) { /* concat all strings */
307 size_t l = tsvalue(top-i)->len;
308 memcpy(buffer+tl, svalue(top-i), l);
309 tl += l;
310 }
311 setsvalue2s(L, top-n, luaS_newlstr(L, buffer, tl));
312 }
313 total -= n-1; /* got `n' strings to create 1 new */
314 last -= n-1;
315 } while (total > 1); /* repeat until only 1 result left */
316}
317
318
319static void Arith (lua_State *L, StkId ra, const TValue *rb,
320 const TValue *rc, TMS op) {
321 TValue tempb, tempc;
322 const TValue *b, *c;
323 if ((b = luaV_tonumber(rb, &tempb)) != NULL &&
324 (c = luaV_tonumber(rc, &tempc)) != NULL) {
325 lua_Number nb = nvalue(b), nc = nvalue(c);
326 switch (op) {
327 case TM_ADD: setnvalue(ra, luai_numadd(nb, nc)); break;
328 case TM_SUB: setnvalue(ra, luai_numsub(nb, nc)); break;
329 case TM_MUL: setnvalue(ra, luai_nummul(nb, nc)); break;
330 case TM_DIV: setnvalue(ra, luai_numdiv(nb, nc)); break;
331 case TM_MOD: setnvalue(ra, luai_nummod(nb, nc)); break;
332 case TM_POW: setnvalue(ra, luai_numpow(nb, nc)); break;
333 case TM_UNM: setnvalue(ra, luai_numunm(nb)); break;
334 default: lua_assert(0); break;
335 }
336 }
337 else if (!call_binTM(L, rb, rc, ra, op))
338 luaG_aritherror(L, rb, rc);
339}
340
341
342
343/*
344** some macros for common tasks in `luaV_execute'
345*/
346
347#define runtime_check(L, c) { if (!(c)) break; }
348
349#define RA(i) (base+GETARG_A(i))
350/* to be used after possible stack reallocation */
351#define RB(i) check_exp(getBMode(GET_OPCODE(i)) == OpArgR, base+GETARG_B(i))
352#define RC(i) check_exp(getCMode(GET_OPCODE(i)) == OpArgR, base+GETARG_C(i))
353#define RKB(i) check_exp(getBMode(GET_OPCODE(i)) == OpArgK, \
354 ISK(GETARG_B(i)) ? k+INDEXK(GETARG_B(i)) : base+GETARG_B(i))
355#define RKC(i) check_exp(getCMode(GET_OPCODE(i)) == OpArgK, \
356 ISK(GETARG_C(i)) ? k+INDEXK(GETARG_C(i)) : base+GETARG_C(i))
357#define KBx(i) check_exp(getBMode(GET_OPCODE(i)) == OpArgK, k+GETARG_Bx(i))
358
359
360#define dojump(L,pc,i) {(pc) += (i); luai_threadyield(L);}
361
362
363#define Protect(x) { L->savedpc = pc; {x;}; base = L->base; }
364
365
366#define arith_op(op,tm) { \
367 TValue *rb = RKB(i); \
368 TValue *rc = RKC(i); \
369 if (ttisnumber(rb) && ttisnumber(rc)) { \
370 lua_Number nb = nvalue(rb), nc = nvalue(rc); \
371 setnvalue(ra, op(nb, nc)); \
372 } \
373 else \
374 Protect(Arith(L, ra, rb, rc, tm)); \
375 }
376
377
378
379void luaV_execute (lua_State *L, int nexeccalls) {
380 LClosure *cl;
381 StkId base;
382 TValue *k;
383 const Instruction *pc;
384 reentry: /* entry point */
385 lua_assert(isLua(L->ci));
386 pc = L->savedpc;
387 cl = &clvalue(L->ci->func)->l;
388 base = L->base;
389 k = cl->p->k;
390 /* main loop of interpreter */
391 for (;;) {
392 const Instruction i = *pc++;
393 StkId ra;
394 if ((L->hookmask & (LUA_MASKLINE | LUA_MASKCOUNT)) &&
395 (--L->hookcount == 0 || L->hookmask & LUA_MASKLINE)) {
396 traceexec(L, pc);
397 if (L->status == LUA_YIELD) { /* did hook yield? */
398 L->savedpc = pc - 1;
399 return;
400 }
401 base = L->base;
402 }
403 /* warning!! several calls may realloc the stack and invalidate `ra' */
404 ra = RA(i);
405 lua_assert(base == L->base && L->base == L->ci->base);
406 lua_assert(base <= L->top && L->top <= L->stack + L->stacksize);
407 lua_assert(L->top == L->ci->top || luaG_checkopenop(i));
408 switch (GET_OPCODE(i)) {
409 case OP_MOVE: {
410 setobjs2s(L, ra, RB(i));
411 continue;
412 }
413 case OP_LOADK: {
414 setobj2s(L, ra, KBx(i));
415 continue;
416 }
417 case OP_LOADBOOL: {
418 setbvalue(ra, GETARG_B(i));
419 if (GETARG_C(i)) pc++; /* skip next instruction (if C) */
420 continue;
421 }
422 case OP_LOADNIL: {
423 TValue *rb = RB(i);
424 do {
425 setnilvalue(rb--);
426 } while (rb >= ra);
427 continue;
428 }
429 case OP_GETUPVAL: {
430 int b = GETARG_B(i);
431 setobj2s(L, ra, cl->upvals[b]->v);
432 continue;
433 }
434 case OP_GETGLOBAL: {
435 TValue g;
436 TValue *rb = KBx(i);
437 sethvalue(L, &g, cl->env);
438 lua_assert(ttisstring(rb));
439 Protect(luaV_gettable(L, &g, rb, ra));
440 continue;
441 }
442 case OP_GETTABLE: {
443 Protect(luaV_gettable(L, RB(i), RKC(i), ra));
444 continue;
445 }
446 case OP_SETGLOBAL: {
447 TValue g;
448 sethvalue(L, &g, cl->env);
449 lua_assert(ttisstring(KBx(i)));
450 Protect(luaV_settable(L, &g, KBx(i), ra));
451 continue;
452 }
453 case OP_SETUPVAL: {
454 UpVal *uv = cl->upvals[GETARG_B(i)];
455 setobj(L, uv->v, ra);
456 luaC_barrier(L, uv, ra);
457 continue;
458 }
459 case OP_SETTABLE: {
460 Protect(luaV_settable(L, ra, RKB(i), RKC(i)));
461 continue;
462 }
463 case OP_NEWTABLE: {
464 int b = GETARG_B(i);
465 int c = GETARG_C(i);
466 sethvalue(L, ra, luaH_new(L, luaO_fb2int(b), luaO_fb2int(c)));
467 Protect(luaC_checkGC(L));
468 continue;
469 }
470 case OP_SELF: {
471 StkId rb = RB(i);
472 setobjs2s(L, ra+1, rb);
473 Protect(luaV_gettable(L, rb, RKC(i), ra));
474 continue;
475 }
476 case OP_ADD: {
477 arith_op(luai_numadd, TM_ADD);
478 continue;
479 }
480 case OP_SUB: {
481 arith_op(luai_numsub, TM_SUB);
482 continue;
483 }
484 case OP_MUL: {
485 arith_op(luai_nummul, TM_MUL);
486 continue;
487 }
488 case OP_DIV: {
489 arith_op(luai_numdiv, TM_DIV);
490 continue;
491 }
492 case OP_MOD: {
493 arith_op(luai_nummod, TM_MOD);
494 continue;
495 }
496 case OP_POW: {
497 arith_op(luai_numpow, TM_POW);
498 continue;
499 }
500 case OP_UNM: {
501 TValue *rb = RB(i);
502 if (ttisnumber(rb)) {
503 lua_Number nb = nvalue(rb);
504 setnvalue(ra, luai_numunm(nb));
505 }
506 else {
507 Protect(Arith(L, ra, rb, rb, TM_UNM));
508 }
509 continue;
510 }
511 case OP_NOT: {
512 int res = l_isfalse(RB(i)); /* next assignment may change this value */
513 setbvalue(ra, res);
514 continue;
515 }
516 case OP_LEN: {
517 const TValue *rb = RB(i);
518 switch (ttype(rb)) {
519 case LUA_TTABLE: {
520 setnvalue(ra, cast_num(luaH_getn(hvalue(rb))));
521 break;
522 }
523 case LUA_TSTRING: {
524 setnvalue(ra, cast_num(tsvalue(rb)->len));
525 break;
526 }
527 default: { /* try metamethod */
528 Protect(
529 if (!call_binTM(L, rb, luaO_nilobject, ra, TM_LEN))
530 luaG_typeerror(L, rb, "get length of");
531 )
532 }
533 }
534 continue;
535 }
536 case OP_CONCAT: {
537 int b = GETARG_B(i);
538 int c = GETARG_C(i);
539 Protect(luaV_concat(L, c-b+1, c); luaC_checkGC(L));
540 setobjs2s(L, RA(i), base+b);
541 continue;
542 }
543 case OP_JMP: {
544 dojump(L, pc, GETARG_sBx(i));
545 continue;
546 }
547 case OP_EQ: {
548 TValue *rb = RKB(i);
549 TValue *rc = RKC(i);
550 Protect(
551 if (equalobj(L, rb, rc) == GETARG_A(i))
552 dojump(L, pc, GETARG_sBx(*pc));
553 )
554 pc++;
555 continue;
556 }
557 case OP_LT: {
558 Protect(
559 if (luaV_lessthan(L, RKB(i), RKC(i)) == GETARG_A(i))
560 dojump(L, pc, GETARG_sBx(*pc));
561 )
562 pc++;
563 continue;
564 }
565 case OP_LE: {
566 Protect(
567 if (lessequal(L, RKB(i), RKC(i)) == GETARG_A(i))
568 dojump(L, pc, GETARG_sBx(*pc));
569 )
570 pc++;
571 continue;
572 }
573 case OP_TEST: {
574 if (l_isfalse(ra) != GETARG_C(i))
575 dojump(L, pc, GETARG_sBx(*pc));
576 pc++;
577 continue;
578 }
579 case OP_TESTSET: {
580 TValue *rb = RB(i);
581 if (l_isfalse(rb) != GETARG_C(i)) {
582 setobjs2s(L, ra, rb);
583 dojump(L, pc, GETARG_sBx(*pc));
584 }
585 pc++;
586 continue;
587 }
588 case OP_CALL: {
589 int b = GETARG_B(i);
590 int nresults = GETARG_C(i) - 1;
591 if (b != 0) L->top = ra+b; /* else previous instruction set top */
592 L->savedpc = pc;
593 switch (luaD_precall(L, ra, nresults)) {
594 case PCRLUA: {
595 nexeccalls++;
596 goto reentry; /* restart luaV_execute over new Lua function */
597 }
598 case PCRC: {
599 /* it was a C function (`precall' called it); adjust results */
600 if (nresults >= 0) L->top = L->ci->top;
601 base = L->base;
602 continue;
603 }
604 default: {
605 return; /* yield */
606 }
607 }
608 }
609 case OP_TAILCALL: {
610 int b = GETARG_B(i);
611 if (b != 0) L->top = ra+b; /* else previous instruction set top */
612 L->savedpc = pc;
613 lua_assert(GETARG_C(i) - 1 == LUA_MULTRET);
614 switch (luaD_precall(L, ra, LUA_MULTRET)) {
615 case PCRLUA: {
616 /* tail call: put new frame in place of previous one */
617 CallInfo *ci = L->ci - 1; /* previous frame */
618 int aux;
619 StkId func = ci->func;
620 StkId pfunc = (ci+1)->func; /* previous function index */
621 if (L->openupval) luaF_close(L, ci->base);
622 L->base = ci->base = ci->func + ((ci+1)->base - pfunc);
623 for (aux = 0; pfunc+aux < L->top; aux++) /* move frame down */
624 setobjs2s(L, func+aux, pfunc+aux);
625 ci->top = L->top = func+aux; /* correct top */
626 lua_assert(L->top == L->base + clvalue(func)->l.p->maxstacksize);
627 ci->savedpc = L->savedpc;
628 ci->tailcalls++; /* one more call lost */
629 L->ci--; /* remove new frame */
630 goto reentry;
631 }
632 case PCRC: { /* it was a C function (`precall' called it) */
633 base = L->base;
634 continue;
635 }
636 default: {
637 return; /* yield */
638 }
639 }
640 }
641 case OP_RETURN: {
642 int b = GETARG_B(i);
643 if (b != 0) L->top = ra+b-1;
644 if (L->openupval) luaF_close(L, base);
645 L->savedpc = pc;
646 b = luaD_poscall(L, ra);
647 if (--nexeccalls == 0) /* was previous function running `here'? */
648 return; /* no: return */
649 else { /* yes: continue its execution */
650 if (b) L->top = L->ci->top;
651 lua_assert(isLua(L->ci));
652 lua_assert(GET_OPCODE(*((L->ci)->savedpc - 1)) == OP_CALL);
653 goto reentry;
654 }
655 }
656 case OP_FORLOOP: {
657 lua_Number step = nvalue(ra+2);
658 lua_Number idx = luai_numadd(nvalue(ra), step); /* increment index */
659 lua_Number limit = nvalue(ra+1);
660 if (luai_numlt(0, step) ? luai_numle(idx, limit)
661 : luai_numle(limit, idx)) {
662 dojump(L, pc, GETARG_sBx(i)); /* jump back */
663 setnvalue(ra, idx); /* update internal index... */
664 setnvalue(ra+3, idx); /* ...and external index */
665 }
666 continue;
667 }
668 case OP_FORPREP: {
669 const TValue *init = ra;
670 const TValue *plimit = ra+1;
671 const TValue *pstep = ra+2;
672 L->savedpc = pc; /* next steps may throw errors */
673 if (!tonumber(init, ra))
674 luaG_runerror(L, LUA_QL("for") " initial value must be a number");
675 else if (!tonumber(plimit, ra+1))
676 luaG_runerror(L, LUA_QL("for") " limit must be a number");
677 else if (!tonumber(pstep, ra+2))
678 luaG_runerror(L, LUA_QL("for") " step must be a number");
679 setnvalue(ra, luai_numsub(nvalue(ra), nvalue(pstep)));
680 dojump(L, pc, GETARG_sBx(i));
681 continue;
682 }
683 case OP_TFORLOOP: {
684 StkId cb = ra + 3; /* call base */
685 setobjs2s(L, cb+2, ra+2);
686 setobjs2s(L, cb+1, ra+1);
687 setobjs2s(L, cb, ra);
688 L->top = cb+3; /* func. + 2 args (state and index) */
689 Protect(luaD_call(L, cb, GETARG_C(i)));
690 L->top = L->ci->top;
691 cb = RA(i) + 3; /* previous call may change the stack */
692 if (!ttisnil(cb)) { /* continue loop? */
693 setobjs2s(L, cb-1, cb); /* save control variable */
694 dojump(L, pc, GETARG_sBx(*pc)); /* jump back */
695 }
696 pc++;
697 continue;
698 }
699 case OP_SETLIST: {
700 int n = GETARG_B(i);
701 int c = GETARG_C(i);
702 int last;
703 Table *h;
704 if (n == 0) {
705 n = cast_int(L->top - ra) - 1;
706 L->top = L->ci->top;
707 }
708 if (c == 0) c = cast_int(*pc++);
709 runtime_check(L, ttistable(ra));
710 h = hvalue(ra);
711 last = ((c-1)*LFIELDS_PER_FLUSH) + n;
712 if (last > h->sizearray) /* needs more space? */
713 luaH_resizearray(L, h, last); /* pre-alloc it at once */
714 for (; n > 0; n--) {
715 TValue *val = ra+n;
716 setobj2t(L, luaH_setnum(L, h, last--), val);
717 luaC_barriert(L, h, val);
718 }
719 continue;
720 }
721 case OP_CLOSE: {
722 luaF_close(L, ra);
723 continue;
724 }
725 case OP_CLOSURE: {
726 Proto *p;
727 Closure *ncl;
728 int nup, j;
729 p = cl->p->p[GETARG_Bx(i)];
730 nup = p->nups;
731 ncl = luaF_newLclosure(L, nup, cl->env);
732 ncl->l.p = p;
733 for (j=0; j<nup; j++, pc++) {
734 if (GET_OPCODE(*pc) == OP_GETUPVAL)
735 ncl->l.upvals[j] = cl->upvals[GETARG_B(*pc)];
736 else {
737 lua_assert(GET_OPCODE(*pc) == OP_MOVE);
738 ncl->l.upvals[j] = luaF_findupval(L, base + GETARG_B(*pc));
739 }
740 }
741 setclvalue(L, ra, ncl);
742 Protect(luaC_checkGC(L));
743 continue;
744 }
745 case OP_VARARG: {
746 int b = GETARG_B(i) - 1;
747 int j;
748 CallInfo *ci = L->ci;
749 int n = cast_int(ci->base - ci->func) - cl->p->numparams - 1;
750 if (b == LUA_MULTRET) {
751 Protect(luaD_checkstack(L, n));
752 ra = RA(i); /* previous call may change the stack */
753 b = n;
754 L->top = ra + n;
755 }
756 for (j = 0; j < b; j++) {
757 if (j < n) {
758 setobjs2s(L, ra + j, ci->base - n + j);
759 }
760 else {
761 setnilvalue(ra + j);
762 }
763 }
764 continue;
765 }
766 }
767 }
768}
769
diff --git a/examples/redis-unstable/deps/lua/src/lvm.h b/examples/redis-unstable/deps/lua/src/lvm.h
deleted file mode 100644
index bfe4f56..0000000
--- a/examples/redis-unstable/deps/lua/src/lvm.h
+++ /dev/null
@@ -1,36 +0,0 @@
1/*
2** $Id: lvm.h,v 2.5.1.1 2007/12/27 13:02:25 roberto Exp $
3** Lua virtual machine
4** See Copyright Notice in lua.h
5*/
6
7#ifndef lvm_h
8#define lvm_h
9
10
11#include "ldo.h"
12#include "lobject.h"
13#include "ltm.h"
14
15
16#define tostring(L,o) ((ttype(o) == LUA_TSTRING) || (luaV_tostring(L, o)))
17
18#define tonumber(o,n) (ttype(o) == LUA_TNUMBER || \
19 (((o) = luaV_tonumber(o,n)) != NULL))
20
21#define equalobj(L,o1,o2) \
22 (ttype(o1) == ttype(o2) && luaV_equalval(L, o1, o2))
23
24
25LUAI_FUNC int luaV_lessthan (lua_State *L, const TValue *l, const TValue *r);
26LUAI_FUNC int luaV_equalval (lua_State *L, const TValue *t1, const TValue *t2);
27LUAI_FUNC const TValue *luaV_tonumber (const TValue *obj, TValue *n);
28LUAI_FUNC int luaV_tostring (lua_State *L, StkId obj);
29LUAI_FUNC void luaV_gettable (lua_State *L, const TValue *t, TValue *key,
30 StkId val);
31LUAI_FUNC void luaV_settable (lua_State *L, const TValue *t, TValue *key,
32 StkId val);
33LUAI_FUNC void luaV_execute (lua_State *L, int nexeccalls);
34LUAI_FUNC void luaV_concat (lua_State *L, int total, int last);
35
36#endif
diff --git a/examples/redis-unstable/deps/lua/src/lzio.c b/examples/redis-unstable/deps/lua/src/lzio.c
deleted file mode 100644
index 293edd5..0000000
--- a/examples/redis-unstable/deps/lua/src/lzio.c
+++ /dev/null
@@ -1,82 +0,0 @@
1/*
2** $Id: lzio.c,v 1.31.1.1 2007/12/27 13:02:25 roberto Exp $
3** a generic input stream interface
4** See Copyright Notice in lua.h
5*/
6
7
8#include <string.h>
9
10#define lzio_c
11#define LUA_CORE
12
13#include "lua.h"
14
15#include "llimits.h"
16#include "lmem.h"
17#include "lstate.h"
18#include "lzio.h"
19
20
21int luaZ_fill (ZIO *z) {
22 size_t size;
23 lua_State *L = z->L;
24 const char *buff;
25 lua_unlock(L);
26 buff = z->reader(L, z->data, &size);
27 lua_lock(L);
28 if (buff == NULL || size == 0) return EOZ;
29 z->n = size - 1;
30 z->p = buff;
31 return char2int(*(z->p++));
32}
33
34
35int luaZ_lookahead (ZIO *z) {
36 if (z->n == 0) {
37 if (luaZ_fill(z) == EOZ)
38 return EOZ;
39 else {
40 z->n++; /* luaZ_fill removed first byte; put back it */
41 z->p--;
42 }
43 }
44 return char2int(*z->p);
45}
46
47
48void luaZ_init (lua_State *L, ZIO *z, lua_Reader reader, void *data) {
49 z->L = L;
50 z->reader = reader;
51 z->data = data;
52 z->n = 0;
53 z->p = NULL;
54}
55
56
57/* --------------------------------------------------------------- read --- */
58size_t luaZ_read (ZIO *z, void *b, size_t n) {
59 while (n) {
60 size_t m;
61 if (luaZ_lookahead(z) == EOZ)
62 return n; /* return number of missing bytes */
63 m = (n <= z->n) ? n : z->n; /* min. between n and z->n */
64 memcpy(b, z->p, m);
65 z->n -= m;
66 z->p += m;
67 b = (char *)b + m;
68 n -= m;
69 }
70 return 0;
71}
72
73/* ------------------------------------------------------------------------ */
74char *luaZ_openspace (lua_State *L, Mbuffer *buff, size_t n) {
75 if (n > buff->buffsize) {
76 if (n < LUA_MINBUFFER) n = LUA_MINBUFFER;
77 luaZ_resizebuffer(L, buff, n);
78 }
79 return buff->buffer;
80}
81
82
diff --git a/examples/redis-unstable/deps/lua/src/lzio.h b/examples/redis-unstable/deps/lua/src/lzio.h
deleted file mode 100644
index 51d695d..0000000
--- a/examples/redis-unstable/deps/lua/src/lzio.h
+++ /dev/null
@@ -1,67 +0,0 @@
1/*
2** $Id: lzio.h,v 1.21.1.1 2007/12/27 13:02:25 roberto Exp $
3** Buffered streams
4** See Copyright Notice in lua.h
5*/
6
7
8#ifndef lzio_h
9#define lzio_h
10
11#include "lua.h"
12
13#include "lmem.h"
14
15
16#define EOZ (-1) /* end of stream */
17
18typedef struct Zio ZIO;
19
20#define char2int(c) cast(int, cast(unsigned char, (c)))
21
22#define zgetc(z) (((z)->n--)>0 ? char2int(*(z)->p++) : luaZ_fill(z))
23
24typedef struct Mbuffer {
25 char *buffer;
26 size_t n;
27 size_t buffsize;
28} Mbuffer;
29
30#define luaZ_initbuffer(L, buff) ((buff)->buffer = NULL, (buff)->buffsize = 0)
31
32#define luaZ_buffer(buff) ((buff)->buffer)
33#define luaZ_sizebuffer(buff) ((buff)->buffsize)
34#define luaZ_bufflen(buff) ((buff)->n)
35
36#define luaZ_resetbuffer(buff) ((buff)->n = 0)
37
38
39#define luaZ_resizebuffer(L, buff, size) \
40 (luaM_reallocvector(L, (buff)->buffer, (buff)->buffsize, size, char), \
41 (buff)->buffsize = size)
42
43#define luaZ_freebuffer(L, buff) luaZ_resizebuffer(L, buff, 0)
44
45
46LUAI_FUNC char *luaZ_openspace (lua_State *L, Mbuffer *buff, size_t n);
47LUAI_FUNC void luaZ_init (lua_State *L, ZIO *z, lua_Reader reader,
48 void *data);
49LUAI_FUNC size_t luaZ_read (ZIO* z, void* b, size_t n); /* read next n bytes */
50LUAI_FUNC int luaZ_lookahead (ZIO *z);
51
52
53
54/* --------- Private Part ------------------ */
55
56struct Zio {
57 size_t n; /* bytes still unread */
58 const char *p; /* current position in buffer */
59 lua_Reader reader;
60 void* data; /* additional data */
61 lua_State *L; /* Lua state (for reader) */
62};
63
64
65LUAI_FUNC int luaZ_fill (ZIO *z);
66
67#endif
diff --git a/examples/redis-unstable/deps/lua/src/print.c b/examples/redis-unstable/deps/lua/src/print.c
deleted file mode 100644
index e240cfc..0000000
--- a/examples/redis-unstable/deps/lua/src/print.c
+++ /dev/null
@@ -1,227 +0,0 @@
1/*
2** $Id: print.c,v 1.55a 2006/05/31 13:30:05 lhf Exp $
3** print bytecodes
4** See Copyright Notice in lua.h
5*/
6
7#include <ctype.h>
8#include <stdio.h>
9
10#define luac_c
11#define LUA_CORE
12
13#include "ldebug.h"
14#include "lobject.h"
15#include "lopcodes.h"
16#include "lundump.h"
17
18#define PrintFunction luaU_print
19
20#define Sizeof(x) ((int)sizeof(x))
21#define VOID(p) ((const void*)(p))
22
23static void PrintString(const TString* ts)
24{
25 const char* s=getstr(ts);
26 size_t i,n=ts->tsv.len;
27 putchar('"');
28 for (i=0; i<n; i++)
29 {
30 int c=s[i];
31 switch (c)
32 {
33 case '"': printf("\\\""); break;
34 case '\\': printf("\\\\"); break;
35 case '\a': printf("\\a"); break;
36 case '\b': printf("\\b"); break;
37 case '\f': printf("\\f"); break;
38 case '\n': printf("\\n"); break;
39 case '\r': printf("\\r"); break;
40 case '\t': printf("\\t"); break;
41 case '\v': printf("\\v"); break;
42 default: if (isprint((unsigned char)c))
43 putchar(c);
44 else
45 printf("\\%03u",(unsigned char)c);
46 }
47 }
48 putchar('"');
49}
50
51static void PrintConstant(const Proto* f, int i)
52{
53 const TValue* o=&f->k[i];
54 switch (ttype(o))
55 {
56 case LUA_TNIL:
57 printf("nil");
58 break;
59 case LUA_TBOOLEAN:
60 printf(bvalue(o) ? "true" : "false");
61 break;
62 case LUA_TNUMBER:
63 printf(LUA_NUMBER_FMT,nvalue(o));
64 break;
65 case LUA_TSTRING:
66 PrintString(rawtsvalue(o));
67 break;
68 default: /* cannot happen */
69 printf("? type=%d",ttype(o));
70 break;
71 }
72}
73
74static void PrintCode(const Proto* f)
75{
76 const Instruction* code=f->code;
77 int pc,n=f->sizecode;
78 for (pc=0; pc<n; pc++)
79 {
80 Instruction i=code[pc];
81 OpCode o=GET_OPCODE(i);
82 int a=GETARG_A(i);
83 int b=GETARG_B(i);
84 int c=GETARG_C(i);
85 int bx=GETARG_Bx(i);
86 int sbx=GETARG_sBx(i);
87 int line=getline(f,pc);
88 printf("\t%d\t",pc+1);
89 if (line>0) printf("[%d]\t",line); else printf("[-]\t");
90 printf("%-9s\t",luaP_opnames[o]);
91 switch (getOpMode(o))
92 {
93 case iABC:
94 printf("%d",a);
95 if (getBMode(o)!=OpArgN) printf(" %d",ISK(b) ? (-1-INDEXK(b)) : b);
96 if (getCMode(o)!=OpArgN) printf(" %d",ISK(c) ? (-1-INDEXK(c)) : c);
97 break;
98 case iABx:
99 if (getBMode(o)==OpArgK) printf("%d %d",a,-1-bx); else printf("%d %d",a,bx);
100 break;
101 case iAsBx:
102 if (o==OP_JMP) printf("%d",sbx); else printf("%d %d",a,sbx);
103 break;
104 }
105 switch (o)
106 {
107 case OP_LOADK:
108 printf("\t; "); PrintConstant(f,bx);
109 break;
110 case OP_GETUPVAL:
111 case OP_SETUPVAL:
112 printf("\t; %s", (f->sizeupvalues>0) ? getstr(f->upvalues[b]) : "-");
113 break;
114 case OP_GETGLOBAL:
115 case OP_SETGLOBAL:
116 printf("\t; %s",svalue(&f->k[bx]));
117 break;
118 case OP_GETTABLE:
119 case OP_SELF:
120 if (ISK(c)) { printf("\t; "); PrintConstant(f,INDEXK(c)); }
121 break;
122 case OP_SETTABLE:
123 case OP_ADD:
124 case OP_SUB:
125 case OP_MUL:
126 case OP_DIV:
127 case OP_POW:
128 case OP_EQ:
129 case OP_LT:
130 case OP_LE:
131 if (ISK(b) || ISK(c))
132 {
133 printf("\t; ");
134 if (ISK(b)) PrintConstant(f,INDEXK(b)); else printf("-");
135 printf(" ");
136 if (ISK(c)) PrintConstant(f,INDEXK(c)); else printf("-");
137 }
138 break;
139 case OP_JMP:
140 case OP_FORLOOP:
141 case OP_FORPREP:
142 printf("\t; to %d",sbx+pc+2);
143 break;
144 case OP_CLOSURE:
145 printf("\t; %p",VOID(f->p[bx]));
146 break;
147 case OP_SETLIST:
148 if (c==0) printf("\t; %d",(int)code[++pc]);
149 else printf("\t; %d",c);
150 break;
151 default:
152 break;
153 }
154 printf("\n");
155 }
156}
157
158#define SS(x) (x==1)?"":"s"
159#define S(x) x,SS(x)
160
161static void PrintHeader(const Proto* f)
162{
163 const char* s=getstr(f->source);
164 if (*s=='@' || *s=='=')
165 s++;
166 else if (*s==LUA_SIGNATURE[0])
167 s="(bstring)";
168 else
169 s="(string)";
170 printf("\n%s <%s:%d,%d> (%d instruction%s, %d bytes at %p)\n",
171 (f->linedefined==0)?"main":"function",s,
172 f->linedefined,f->lastlinedefined,
173 S(f->sizecode),f->sizecode*Sizeof(Instruction),VOID(f));
174 printf("%d%s param%s, %d slot%s, %d upvalue%s, ",
175 f->numparams,f->is_vararg?"+":"",SS(f->numparams),
176 S(f->maxstacksize),S(f->nups));
177 printf("%d local%s, %d constant%s, %d function%s\n",
178 S(f->sizelocvars),S(f->sizek),S(f->sizep));
179}
180
181static void PrintConstants(const Proto* f)
182{
183 int i,n=f->sizek;
184 printf("constants (%d) for %p:\n",n,VOID(f));
185 for (i=0; i<n; i++)
186 {
187 printf("\t%d\t",i+1);
188 PrintConstant(f,i);
189 printf("\n");
190 }
191}
192
193static void PrintLocals(const Proto* f)
194{
195 int i,n=f->sizelocvars;
196 printf("locals (%d) for %p:\n",n,VOID(f));
197 for (i=0; i<n; i++)
198 {
199 printf("\t%d\t%s\t%d\t%d\n",
200 i,getstr(f->locvars[i].varname),f->locvars[i].startpc+1,f->locvars[i].endpc+1);
201 }
202}
203
204static void PrintUpvalues(const Proto* f)
205{
206 int i,n=f->sizeupvalues;
207 printf("upvalues (%d) for %p:\n",n,VOID(f));
208 if (f->upvalues==NULL) return;
209 for (i=0; i<n; i++)
210 {
211 printf("\t%d\t%s\n",i,getstr(f->upvalues[i]));
212 }
213}
214
215void PrintFunction(const Proto* f, int full)
216{
217 int i,n=f->sizep;
218 PrintHeader(f);
219 PrintCode(f);
220 if (full)
221 {
222 PrintConstants(f);
223 PrintLocals(f);
224 PrintUpvalues(f);
225 }
226 for (i=0; i<n; i++) PrintFunction(f->p[i],full);
227}
diff --git a/examples/redis-unstable/deps/lua/src/strbuf.c b/examples/redis-unstable/deps/lua/src/strbuf.c
deleted file mode 100644
index 97ee940..0000000
--- a/examples/redis-unstable/deps/lua/src/strbuf.c
+++ /dev/null
@@ -1,197 +0,0 @@
1/* strbuf - String buffer routines
2 *
3 * Copyright (c) 2010-2012 Mark Pulford <mark@kyne.com.au>
4 *
5 * Permission is hereby granted, free of charge, to any person obtaining
6 * a copy of this software and associated documentation files (the
7 * "Software"), to deal in the Software without restriction, including
8 * without limitation the rights to use, copy, modify, merge, publish,
9 * distribute, sublicense, and/or sell copies of the Software, and to
10 * permit persons to whom the Software is furnished to do so, subject to
11 * the following conditions:
12 *
13 * The above copyright notice and this permission notice shall be
14 * included in all copies or substantial portions of the Software.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
19 * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
20 * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
21 * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
22 * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
23 */
24
25#include <stdio.h>
26#include <stdlib.h>
27#include <stdarg.h>
28#include <string.h>
29#include <stdint.h>
30
31#include "strbuf.h"
32
33static void die(const char *fmt, ...)
34{
35 va_list arg;
36
37 va_start(arg, fmt);
38 vfprintf(stderr, fmt, arg);
39 va_end(arg);
40 fprintf(stderr, "\n");
41
42 abort();
43}
44
45void strbuf_init(strbuf_t *s, size_t len)
46{
47 size_t size;
48
49 if (!len)
50 size = STRBUF_DEFAULT_SIZE;
51 else
52 size = len + 1;
53 if (size < len)
54 die("Overflow, len: %zu", len);
55 s->buf = NULL;
56 s->size = size;
57 s->length = 0;
58 s->dynamic = 0;
59 s->reallocs = 0;
60 s->debug = 0;
61
62 s->buf = malloc(size);
63 if (!s->buf)
64 die("Out of memory");
65
66 strbuf_ensure_null(s);
67}
68
69strbuf_t *strbuf_new(size_t len)
70{
71 strbuf_t *s;
72
73 s = malloc(sizeof(strbuf_t));
74 if (!s)
75 die("Out of memory");
76
77 strbuf_init(s, len);
78
79 /* Dynamic strbuf allocation / deallocation */
80 s->dynamic = 1;
81
82 return s;
83}
84
85static inline void debug_stats(strbuf_t *s)
86{
87 if (s->debug) {
88 fprintf(stderr, "strbuf(%lx) reallocs: %d, length: %zd, size: %zd\n",
89 (long)s, s->reallocs, s->length, s->size);
90 }
91}
92
93/* If strbuf_t has not been dynamically allocated, strbuf_free() can
94 * be called any number of times strbuf_init() */
95void strbuf_free(strbuf_t *s)
96{
97 debug_stats(s);
98
99 if (s->buf) {
100 free(s->buf);
101 s->buf = NULL;
102 }
103 if (s->dynamic)
104 free(s);
105}
106
107char *strbuf_free_to_string(strbuf_t *s, size_t *len)
108{
109 char *buf;
110
111 debug_stats(s);
112
113 strbuf_ensure_null(s);
114
115 buf = s->buf;
116 if (len)
117 *len = s->length;
118
119 if (s->dynamic)
120 free(s);
121
122 return buf;
123}
124
125static size_t calculate_new_size(strbuf_t *s, size_t len)
126{
127 size_t reqsize, newsize;
128
129 if (len <= 0)
130 die("BUG: Invalid strbuf length requested");
131
132 /* Ensure there is room for optional NULL termination */
133 reqsize = len + 1;
134 if (reqsize < len)
135 die("Overflow, len: %zu", len);
136
137 /* If the user has requested to shrink the buffer, do it exactly */
138 if (s->size > reqsize)
139 return reqsize;
140
141 newsize = s->size;
142 if (reqsize >= SIZE_MAX / 2) {
143 newsize = reqsize;
144 } else {
145 /* Exponential sizing */
146 while (newsize < reqsize)
147 newsize *= 2;
148 }
149
150 if (newsize < reqsize)
151 die("BUG: strbuf length would overflow, len: %zu", len);
152
153 return newsize;
154}
155
156
157/* Ensure strbuf can handle a string length bytes long (ignoring NULL
158 * optional termination). */
159void strbuf_resize(strbuf_t *s, size_t len)
160{
161 size_t newsize;
162
163 newsize = calculate_new_size(s, len);
164
165 if (s->debug > 1) {
166 fprintf(stderr, "strbuf(%lx) resize: %zd => %zd\n",
167 (long)s, s->size, newsize);
168 }
169
170 s->size = newsize;
171 s->buf = realloc(s->buf, s->size);
172 if (!s->buf)
173 die("Out of memory, len: %zu", len);
174 s->reallocs++;
175}
176
177void strbuf_append_string(strbuf_t *s, const char *str)
178{
179 size_t i, space;
180
181 space = strbuf_empty_length(s);
182
183 for (i = 0; str[i]; i++) {
184 if (space < 1) {
185 strbuf_resize(s, s->length + 1);
186 space = strbuf_empty_length(s);
187 }
188
189 s->buf[s->length] = str[i];
190 s->length++;
191 space--;
192 }
193}
194
195
196/* vi:ai et sw=4 ts=4:
197 */
diff --git a/examples/redis-unstable/deps/lua/src/strbuf.h b/examples/redis-unstable/deps/lua/src/strbuf.h
deleted file mode 100644
index c10f83f..0000000
--- a/examples/redis-unstable/deps/lua/src/strbuf.h
+++ /dev/null
@@ -1,146 +0,0 @@
1/* strbuf - String buffer routines
2 *
3 * Copyright (c) 2010-2012 Mark Pulford <mark@kyne.com.au>
4 *
5 * Permission is hereby granted, free of charge, to any person obtaining
6 * a copy of this software and associated documentation files (the
7 * "Software"), to deal in the Software without restriction, including
8 * without limitation the rights to use, copy, modify, merge, publish,
9 * distribute, sublicense, and/or sell copies of the Software, and to
10 * permit persons to whom the Software is furnished to do so, subject to
11 * the following conditions:
12 *
13 * The above copyright notice and this permission notice shall be
14 * included in all copies or substantial portions of the Software.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
19 * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
20 * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
21 * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
22 * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
23 */
24
25#include <stdlib.h>
26#include <stdarg.h>
27
28/* Size: Total bytes allocated to *buf
29 * Length: String length, excluding optional NULL terminator.
30 * Dynamic: True if created via strbuf_new()
31 */
32
33typedef struct {
34 char *buf;
35 size_t size;
36 size_t length;
37 int dynamic;
38 int reallocs;
39 int debug;
40} strbuf_t;
41
42#ifndef STRBUF_DEFAULT_SIZE
43#define STRBUF_DEFAULT_SIZE 1023
44#endif
45
46/* Initialise */
47extern strbuf_t *strbuf_new(size_t len);
48extern void strbuf_init(strbuf_t *s, size_t len);
49
50/* Release */
51extern void strbuf_free(strbuf_t *s);
52extern char *strbuf_free_to_string(strbuf_t *s, size_t *len);
53
54/* Management */
55extern void strbuf_resize(strbuf_t *s, size_t len);
56static size_t strbuf_empty_length(strbuf_t *s);
57static size_t strbuf_length(strbuf_t *s);
58static char *strbuf_string(strbuf_t *s, size_t *len);
59static void strbuf_ensure_empty_length(strbuf_t *s, size_t len);
60static char *strbuf_empty_ptr(strbuf_t *s);
61static void strbuf_extend_length(strbuf_t *s, size_t len);
62
63/* Update */
64static void strbuf_append_mem(strbuf_t *s, const char *c, size_t len);
65extern void strbuf_append_string(strbuf_t *s, const char *str);
66static void strbuf_append_char(strbuf_t *s, const char c);
67static void strbuf_ensure_null(strbuf_t *s);
68
69/* Reset string for before use */
70static inline void strbuf_reset(strbuf_t *s)
71{
72 s->length = 0;
73}
74
75static inline int strbuf_allocated(strbuf_t *s)
76{
77 return s->buf != NULL;
78}
79
80/* Return bytes remaining in the string buffer
81 * Ensure there is space for a NULL terminator. */
82static inline size_t strbuf_empty_length(strbuf_t *s)
83{
84 return s->size - s->length - 1;
85}
86
87static inline void strbuf_ensure_empty_length(strbuf_t *s, size_t len)
88{
89 if (len > strbuf_empty_length(s))
90 strbuf_resize(s, s->length + len);
91}
92
93static inline char *strbuf_empty_ptr(strbuf_t *s)
94{
95 return s->buf + s->length;
96}
97
98static inline void strbuf_extend_length(strbuf_t *s, size_t len)
99{
100 s->length += len;
101}
102
103static inline size_t strbuf_length(strbuf_t *s)
104{
105 return s->length;
106}
107
108static inline void strbuf_append_char(strbuf_t *s, const char c)
109{
110 strbuf_ensure_empty_length(s, 1);
111 s->buf[s->length++] = c;
112}
113
114static inline void strbuf_append_char_unsafe(strbuf_t *s, const char c)
115{
116 s->buf[s->length++] = c;
117}
118
119static inline void strbuf_append_mem(strbuf_t *s, const char *c, size_t len)
120{
121 strbuf_ensure_empty_length(s, len);
122 memcpy(s->buf + s->length, c, len);
123 s->length += len;
124}
125
126static inline void strbuf_append_mem_unsafe(strbuf_t *s, const char *c, size_t len)
127{
128 memcpy(s->buf + s->length, c, len);
129 s->length += len;
130}
131
132static inline void strbuf_ensure_null(strbuf_t *s)
133{
134 s->buf[s->length] = 0;
135}
136
137static inline char *strbuf_string(strbuf_t *s, size_t *len)
138{
139 if (len)
140 *len = s->length;
141
142 return s->buf;
143}
144
145/* vi:ai et sw=4 ts=4:
146 */