├── .gitignore ├── .travis.yml ├── LICENSE ├── Makefile ├── README.md ├── deps └── lua │ ├── Makefile │ ├── README │ ├── doc │ ├── contents.html │ ├── logo.gif │ ├── lua.1 │ ├── lua.css │ ├── luac.1 │ ├── manual.css │ ├── manual.html │ ├── osi-certified-72x60.png │ └── readme.html │ ├── lua_cjson.c │ └── src │ ├── Makefile │ ├── lapi.c │ ├── lapi.h │ ├── lauxlib.c │ ├── lauxlib.h │ ├── lbaselib.c │ ├── lbitlib.c │ ├── lcode.c │ ├── lcode.h │ ├── lcorolib.c │ ├── lctype.c │ ├── lctype.h │ ├── ldblib.c │ ├── ldebug.c │ ├── ldebug.h │ ├── ldo.c │ ├── ldo.h │ ├── ldump.c │ ├── lfunc.c │ ├── lfunc.h │ ├── lgc.c │ ├── lgc.h │ ├── linit.c │ ├── liolib.c │ ├── llex.c │ ├── llex.h │ ├── llimits.h │ ├── lmathlib.c │ ├── lmem.c │ ├── lmem.h │ ├── loadlib.c │ ├── lobject.c │ ├── lobject.h │ ├── lopcodes.c │ ├── lopcodes.h │ ├── loslib.c │ ├── lparser.c │ ├── lparser.h │ ├── lstate.c │ ├── lstate.h │ ├── lstring.c │ ├── lstring.h │ ├── lstrlib.c │ ├── ltable.c │ ├── ltable.h │ ├── ltablib.c │ ├── ltm.c │ ├── ltm.h │ ├── lua.c │ ├── lua.h │ ├── lua.hpp │ ├── lua_cjson.c │ ├── luac.c │ ├── luaconf.h │ ├── lualib.h │ ├── lundump.c │ ├── lundump.h │ ├── lvm.c │ ├── lvm.h │ ├── lzio.c │ ├── lzio.h │ ├── strbuf.c │ └── strbuf.h ├── scripts ├── example.lua └── json_exapmle.lua ├── snapshot ├── rdb-tools.png └── rdbtools-to-json.png ├── src ├── Makefile ├── crc64.c ├── crc64.h ├── endian.c ├── endian.h ├── intset.c ├── intset.h ├── log.c ├── log.h ├── lzf.h ├── lzfP.h ├── lzf_d.c ├── main.c ├── rdb.c ├── rdb.h ├── script.c ├── script.h ├── util.c ├── util.h ├── ziplist.c ├── ziplist.h ├── zipmap.c └── zipmap.h └── tests ├── dump2.2.rdb ├── dump2.4.rdb ├── dump2.6.rdb └── dump2.8.rdb /.gitignore: -------------------------------------------------------------------------------- 1 | # Object files 2 | *.o 3 | *.ko 4 | *.obj 5 | *.elf 6 | 7 | # Libraries 8 | *.lib 9 | *.a 10 | 11 | # Shared objects (inc. Windows DLLs) 12 | *.dll 13 | *.so 14 | *.so.* 15 | *.dylib 16 | 17 | # Executables 18 | *.exe 19 | *.out 20 | *.app 21 | *.i*86 22 | *.x86_64 23 | *.hex 24 | 25 | # swp file 26 | *.swp 27 | 28 | # ctags index 29 | tags 30 | -------------------------------------------------------------------------------- /.travis.yml: -------------------------------------------------------------------------------- 1 | compiler: 2 | - clang 3 | - gcc 4 | language: c 5 | 6 | script: 7 | - make 8 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | Copyright (c) 2015 git-hulk 2 | 3 | Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: 4 | 5 | The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. 6 | 7 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 8 | -------------------------------------------------------------------------------- /Makefile: -------------------------------------------------------------------------------- 1 | all: 2 | cd src && $(MAKE) $@ 3 | clean: 4 | cd src && $(MAKE) $@ 5 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # rdbtools [![Build Status](https://img.shields.io/badge/license-MIT-blue.svg)](http://opensource.org/licenses/MIT) 2 | 3 | A tool use c to analyze redis rdb file, and use lua to handle. 4 | 5 | > Notice: This tools was tested on 2.2 , 2.4 , 2.6, 2.8 6 | 7 | > bugs in other versions? open issues, I will fix it, thanks~ 8 | 9 | 10 | 11 | #### 1. What about this tool to do? 12 | 13 | > use lua to parse rdb file to user's format data, like aof file, json etc. 14 | 15 | 16 | #### 2. How to use? 17 | ```shell 18 | $ cd rdbtools/src 19 | $ make 20 | $ ./rdbtools -f ../tests/dump2.4.rdb -s ../scripts/example.lua 21 | ``` 22 | 23 | NOTE: Compile error with readline.h is not found, just use yum or apt-get to install readline and readline-devel 24 | #### 3. Options 25 | 26 | ```shell 27 | USAGE: ./rdbtools [-f file] -V -h 28 | -V --version 29 | -h --help show usage 30 | -f --file specify which rdb file would be parsed. 31 | -s --file specify which lua script, default is ../scripts/example.lua 32 | ``` 33 | 34 | If you want to handle key-value in rdb file, you can use `-s your_script.lua`, and lua function `handle` will be callbacked. 35 | 36 | Example can be found in `scripts/example.lua`, and it just print the key-value. 37 | 38 | 39 | ##### Json format example 40 | 41 | `cat scripts/json_exapmle.lua` 42 | 43 | ```lua 44 | local cjson = require "cjson" 45 | 46 | function handle(item) 47 | print(cjson.encode(item)) 48 | end 49 | ``` 50 | 51 | And result is below: 52 | ![image](https://raw.githubusercontent.com/git-hulk/rdbtools/master/snapshot/rdbtools-to-json.png) 53 | 54 | #### 4. Params in handle function 55 | 56 | ```lua 57 | function handle(item) 58 | --item.type, value may be string, set, zset, list, or hash. 59 | --item.expire_time, key expire time . 60 | --item.value, may be string or list or hash, it depends on item.type 61 | end 62 | ``` 63 | 64 | 65 | #### 5. Environment 66 | 67 | If you what to know rdb version and which db is selcted. 68 | 69 | ```lua 70 | print(env.version) 71 | print(env.db_num) 72 | ``` 73 | 74 | #### 6. Contact me? 75 | > ```Sina Weibo```: [@改名hulk](http://www.weibo.com/tianyi4) 76 | 77 | >```Gmail```: [hulk.website@gmail.com](mailto:hulk.website@gmail.com) 78 | 79 | >```Blog```: [www.hulkdev.com](http://www.hulkdev.com) 80 | 81 | any bugs? send mail, and I will appreciate your help. 82 | 83 | -------------------------------------------------------------------------------- /deps/lua/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.0 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 | -------------------------------------------------------------------------------- /deps/lua/README: -------------------------------------------------------------------------------- 1 | 2 | This is Lua 5.2, released on 12 Dec 2011. 3 | 4 | For installation instructions, license details, and 5 | further information about Lua, see doc/readme.html. 6 | 7 | -------------------------------------------------------------------------------- /deps/lua/doc/logo.gif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/git-hulk/rdbtools/08571e5cd22cc9268bdcd9319b21a2d7ea21a0e0/deps/lua/doc/logo.gif -------------------------------------------------------------------------------- /deps/lua/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 | -------------------------------------------------------------------------------- /deps/lua/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: 20px ; 7 | margin-left: 20px ; 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: 20px ; 20 | padding-right: 20px ; 21 | margin-left: -20px ; 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 | -------------------------------------------------------------------------------- /deps/lua/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 | -------------------------------------------------------------------------------- /deps/lua/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: 20px ; 22 | margin-left: -20px ; 23 | background-color: #E0E0FF ; 24 | } 25 | -------------------------------------------------------------------------------- /deps/lua/doc/osi-certified-72x60.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/git-hulk/rdbtools/08571e5cd22cc9268bdcd9319b21a2d7ea21a0e0/deps/lua/doc/osi-certified-72x60.png -------------------------------------------------------------------------------- /deps/lua/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= -g -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 strbuf.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 lua_cjson.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) $@ $? 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 | strbuf.o: strbuf.c strbuf.h 188 | lua_cjson.o: lua_cjson.c lua.h luaconf.h lauxlib.h strbuf.h 189 | -------------------------------------------------------------------------------- /deps/lua/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 | -------------------------------------------------------------------------------- /deps/lua/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 | 12 | #include 13 | #include 14 | 15 | #include "lua.h" 16 | 17 | 18 | 19 | /* extra error code for `luaL_load' */ 20 | #define LUA_ERRFILE (LUA_ERRERR+1) 21 | 22 | 23 | typedef struct luaL_Reg { 24 | const char *name; 25 | lua_CFunction func; 26 | } luaL_Reg; 27 | 28 | 29 | LUALIB_API void (luaL_checkversion_) (lua_State *L, lua_Number ver); 30 | #define luaL_checkversion(L) luaL_checkversion_(L, LUA_VERSION_NUM) 31 | 32 | LUALIB_API int (luaL_getmetafield) (lua_State *L, int obj, const char *e); 33 | LUALIB_API int (luaL_callmeta) (lua_State *L, int obj, const char *e); 34 | LUALIB_API const char *(luaL_tolstring) (lua_State *L, int idx, size_t *len); 35 | LUALIB_API int (luaL_argerror) (lua_State *L, int numarg, const char *extramsg); 36 | LUALIB_API const char *(luaL_checklstring) (lua_State *L, int numArg, 37 | size_t *l); 38 | LUALIB_API const char *(luaL_optlstring) (lua_State *L, int numArg, 39 | const char *def, size_t *l); 40 | LUALIB_API lua_Number (luaL_checknumber) (lua_State *L, int numArg); 41 | LUALIB_API lua_Number (luaL_optnumber) (lua_State *L, int nArg, lua_Number def); 42 | 43 | LUALIB_API lua_Integer (luaL_checkinteger) (lua_State *L, int numArg); 44 | LUALIB_API lua_Integer (luaL_optinteger) (lua_State *L, int nArg, 45 | lua_Integer def); 46 | LUALIB_API lua_Unsigned (luaL_checkunsigned) (lua_State *L, int numArg); 47 | LUALIB_API lua_Unsigned (luaL_optunsigned) (lua_State *L, int numArg, 48 | lua_Unsigned def); 49 | 50 | LUALIB_API void (luaL_checkstack) (lua_State *L, int sz, const char *msg); 51 | LUALIB_API void (luaL_checktype) (lua_State *L, int narg, int t); 52 | LUALIB_API void (luaL_checkany) (lua_State *L, int narg); 53 | 54 | LUALIB_API int (luaL_newmetatable) (lua_State *L, const char *tname); 55 | LUALIB_API void (luaL_setmetatable) (lua_State *L, const char *tname); 56 | LUALIB_API void *(luaL_testudata) (lua_State *L, int ud, const char *tname); 57 | LUALIB_API void *(luaL_checkudata) (lua_State *L, int ud, const char *tname); 58 | 59 | LUALIB_API void (luaL_where) (lua_State *L, int lvl); 60 | LUALIB_API int (luaL_error) (lua_State *L, const char *fmt, ...); 61 | 62 | LUALIB_API int (luaL_checkoption) (lua_State *L, int narg, const char *def, 63 | const char *const lst[]); 64 | 65 | LUALIB_API int (luaL_fileresult) (lua_State *L, int stat, const char *fname); 66 | LUALIB_API int (luaL_execresult) (lua_State *L, int stat); 67 | 68 | /* pre-defined references */ 69 | #define LUA_NOREF (-2) 70 | #define LUA_REFNIL (-1) 71 | 72 | LUALIB_API int (luaL_ref) (lua_State *L, int t); 73 | LUALIB_API void (luaL_unref) (lua_State *L, int t, int ref); 74 | 75 | LUALIB_API int (luaL_loadfilex) (lua_State *L, const char *filename, 76 | const char *mode); 77 | 78 | #define luaL_loadfile(L,f) luaL_loadfilex(L,f,NULL) 79 | 80 | LUALIB_API int (luaL_loadbufferx) (lua_State *L, const char *buff, size_t sz, 81 | const char *name, const char *mode); 82 | LUALIB_API int (luaL_loadstring) (lua_State *L, const char *s); 83 | 84 | LUALIB_API lua_State *(luaL_newstate) (void); 85 | 86 | LUALIB_API int (luaL_len) (lua_State *L, int idx); 87 | 88 | LUALIB_API const char *(luaL_gsub) (lua_State *L, const char *s, const char *p, 89 | const char *r); 90 | 91 | LUALIB_API void (luaL_setfuncs) (lua_State *L, const luaL_Reg *l, int nup); 92 | 93 | LUALIB_API int (luaL_getsubtable) (lua_State *L, int idx, const char *fname); 94 | 95 | LUALIB_API void (luaL_traceback) (lua_State *L, lua_State *L1, 96 | const char *msg, int level); 97 | 98 | LUALIB_API void (luaL_requiref) (lua_State *L, const char *modname, 99 | lua_CFunction openf, int glb); 100 | 101 | /* 102 | ** =============================================================== 103 | ** some useful macros 104 | ** =============================================================== 105 | */ 106 | 107 | 108 | #define luaL_newlibtable(L,l) \ 109 | lua_createtable(L, 0, sizeof(l)/sizeof((l)[0]) - 1) 110 | 111 | #define luaL_newlib(L,l) (luaL_newlibtable(L,l), luaL_setfuncs(L,l,0)) 112 | 113 | #define luaL_argcheck(L, cond,numarg,extramsg) \ 114 | ((void)((cond) || luaL_argerror(L, (numarg), (extramsg)))) 115 | #define luaL_checkstring(L,n) (luaL_checklstring(L, (n), NULL)) 116 | #define luaL_optstring(L,n,d) (luaL_optlstring(L, (n), (d), NULL)) 117 | #define luaL_checkint(L,n) ((int)luaL_checkinteger(L, (n))) 118 | #define luaL_optint(L,n,d) ((int)luaL_optinteger(L, (n), (d))) 119 | #define luaL_checklong(L,n) ((long)luaL_checkinteger(L, (n))) 120 | #define luaL_optlong(L,n,d) ((long)luaL_optinteger(L, (n), (d))) 121 | 122 | #define luaL_typename(L,i) lua_typename(L, lua_type(L,(i))) 123 | 124 | #define luaL_dofile(L, fn) \ 125 | (luaL_loadfile(L, fn) || lua_pcall(L, 0, LUA_MULTRET, 0)) 126 | 127 | #define luaL_dostring(L, s) \ 128 | (luaL_loadstring(L, s) || lua_pcall(L, 0, LUA_MULTRET, 0)) 129 | 130 | #define luaL_getmetatable(L,n) (lua_getfield(L, LUA_REGISTRYINDEX, (n))) 131 | 132 | #define luaL_opt(L,f,n,d) (lua_isnoneornil(L,(n)) ? (d) : f(L,(n))) 133 | 134 | #define luaL_loadbuffer(L,s,sz,n) luaL_loadbufferx(L,s,sz,n,NULL) 135 | 136 | 137 | /* 138 | ** {====================================================== 139 | ** Generic Buffer manipulation 140 | ** ======================================================= 141 | */ 142 | 143 | typedef struct luaL_Buffer { 144 | char *b; /* buffer address */ 145 | size_t size; /* buffer size */ 146 | size_t n; /* number of characters in buffer */ 147 | lua_State *L; 148 | char initb[LUAL_BUFFERSIZE]; /* initial buffer */ 149 | } luaL_Buffer; 150 | 151 | 152 | #define luaL_addchar(B,c) \ 153 | ((void)((B)->n < (B)->size || luaL_prepbuffsize((B), 1)), \ 154 | ((B)->b[(B)->n++] = (c))) 155 | 156 | #define luaL_addsize(B,s) ((B)->n += (s)) 157 | 158 | LUALIB_API void (luaL_buffinit) (lua_State *L, luaL_Buffer *B); 159 | LUALIB_API char *(luaL_prepbuffsize) (luaL_Buffer *B, size_t sz); 160 | LUALIB_API void (luaL_addlstring) (luaL_Buffer *B, const char *s, size_t l); 161 | LUALIB_API void (luaL_addstring) (luaL_Buffer *B, const char *s); 162 | LUALIB_API void (luaL_addvalue) (luaL_Buffer *B); 163 | LUALIB_API void (luaL_pushresult) (luaL_Buffer *B); 164 | LUALIB_API void (luaL_pushresultsize) (luaL_Buffer *B, size_t sz); 165 | LUALIB_API char *(luaL_buffinitsize) (lua_State *L, luaL_Buffer *B, size_t sz); 166 | 167 | #define luaL_prepbuffer(B) luaL_prepbuffsize(B, LUAL_BUFFERSIZE) 168 | 169 | /* }====================================================== */ 170 | 171 | 172 | 173 | /* 174 | ** {====================================================== 175 | ** File handles for IO library 176 | ** ======================================================= 177 | */ 178 | 179 | /* 180 | ** A file handle is a userdata with metatable 'LUA_FILEHANDLE' and 181 | ** initial structure 'luaL_Stream' (it may contain other fields 182 | ** after that initial structure). 183 | */ 184 | 185 | #define LUA_FILEHANDLE "FILE*" 186 | 187 | 188 | typedef struct luaL_Stream { 189 | FILE *f; /* stream (NULL for incompletely created streams) */ 190 | lua_CFunction closef; /* to close stream (NULL for closed streams) */ 191 | } luaL_Stream; 192 | 193 | /* }====================================================== */ 194 | 195 | 196 | 197 | /* compatibility with old module system */ 198 | #if defined(LUA_COMPAT_MODULE) 199 | 200 | LUALIB_API void (luaL_pushmodule) (lua_State *L, const char *modname, 201 | int sizehint); 202 | LUALIB_API void (luaL_openlib) (lua_State *L, const char *libname, 203 | const luaL_Reg *l, int nup); 204 | 205 | #define luaL_register(L,n,l) (luaL_openlib(L,(n),(l),0)) 206 | 207 | #endif 208 | 209 | 210 | #endif 211 | 212 | 213 | -------------------------------------------------------------------------------- /deps/lua/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 | -------------------------------------------------------------------------------- /deps/lua/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 | -------------------------------------------------------------------------------- /deps/lua/src/lcorolib.c: -------------------------------------------------------------------------------- 1 | /* 2 | ** $Id: lcorolib.c,v 1.3 2011/08/23 17:24:34 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 = lua_newthread(L); 84 | luaL_checktype(L, 1, LUA_TFUNCTION); 85 | lua_pushvalue(L, 1); /* move function to top */ 86 | lua_xmove(L, NL, 1); /* move function from L to NL */ 87 | return 1; 88 | } 89 | 90 | 91 | static int luaB_cowrap (lua_State *L) { 92 | luaB_cocreate(L); 93 | lua_pushcclosure(L, luaB_auxwrap, 1); 94 | return 1; 95 | } 96 | 97 | 98 | static int luaB_yield (lua_State *L) { 99 | return lua_yield(L, lua_gettop(L)); 100 | } 101 | 102 | 103 | static int luaB_costatus (lua_State *L) { 104 | lua_State *co = lua_tothread(L, 1); 105 | luaL_argcheck(L, co, 1, "coroutine expected"); 106 | if (L == co) lua_pushliteral(L, "running"); 107 | else { 108 | switch (lua_status(co)) { 109 | case LUA_YIELD: 110 | lua_pushliteral(L, "suspended"); 111 | break; 112 | case LUA_OK: { 113 | lua_Debug ar; 114 | if (lua_getstack(co, 0, &ar) > 0) /* does it have frames? */ 115 | lua_pushliteral(L, "normal"); /* it is running */ 116 | else if (lua_gettop(co) == 0) 117 | lua_pushliteral(L, "dead"); 118 | else 119 | lua_pushliteral(L, "suspended"); /* initial state */ 120 | break; 121 | } 122 | default: /* some error occurred */ 123 | lua_pushliteral(L, "dead"); 124 | break; 125 | } 126 | } 127 | return 1; 128 | } 129 | 130 | 131 | static int luaB_corunning (lua_State *L) { 132 | int ismain = lua_pushthread(L); 133 | lua_pushboolean(L, ismain); 134 | return 2; 135 | } 136 | 137 | 138 | static const luaL_Reg co_funcs[] = { 139 | {"create", luaB_cocreate}, 140 | {"resume", luaB_coresume}, 141 | {"running", luaB_corunning}, 142 | {"status", luaB_costatus}, 143 | {"wrap", luaB_cowrap}, 144 | {"yield", luaB_yield}, 145 | {NULL, NULL} 146 | }; 147 | 148 | 149 | 150 | LUAMOD_API int luaopen_coroutine (lua_State *L) { 151 | luaL_newlib(L, co_funcs); 152 | return 1; 153 | } 154 | 155 | -------------------------------------------------------------------------------- /deps/lua/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 | -------------------------------------------------------------------------------- /deps/lua/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 | -------------------------------------------------------------------------------- /deps/lua/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 | -------------------------------------------------------------------------------- /deps/lua/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 | -------------------------------------------------------------------------------- /deps/lua/src/ldump.c: -------------------------------------------------------------------------------- 1 | /* 2 | ** $Id: ldump.c,v 1.19 2011/11/23 17:48:18 lhf 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(ttype(o),D); 88 | switch (ttype(o)) 89 | { 90 | case LUA_TNIL: 91 | break; 92 | case LUA_TBOOLEAN: 93 | DumpChar(bvalue(o),D); 94 | break; 95 | case LUA_TNUMBER: 96 | DumpNumber(nvalue(o),D); 97 | break; 98 | case LUA_TSTRING: 99 | DumpString(rawtsvalue(o),D); 100 | break; 101 | } 102 | } 103 | n=f->sizep; 104 | DumpInt(n,D); 105 | for (i=0; ip[i],D); 106 | } 107 | 108 | static void DumpUpvalues(const Proto* f, DumpState* D) 109 | { 110 | int i,n=f->sizeupvalues; 111 | DumpInt(n,D); 112 | for (i=0; iupvalues[i].instack,D); 115 | DumpChar(f->upvalues[i].idx,D); 116 | } 117 | } 118 | 119 | static void DumpDebug(const Proto* f, DumpState* D) 120 | { 121 | int i,n; 122 | DumpString((D->strip) ? NULL : f->source,D); 123 | n= (D->strip) ? 0 : f->sizelineinfo; 124 | DumpVector(f->lineinfo,n,sizeof(int),D); 125 | n= (D->strip) ? 0 : f->sizelocvars; 126 | DumpInt(n,D); 127 | for (i=0; ilocvars[i].varname,D); 130 | DumpInt(f->locvars[i].startpc,D); 131 | DumpInt(f->locvars[i].endpc,D); 132 | } 133 | n= (D->strip) ? 0 : f->sizeupvalues; 134 | DumpInt(n,D); 135 | for (i=0; iupvalues[i].name,D); 136 | } 137 | 138 | static void DumpFunction(const Proto* f, DumpState* D) 139 | { 140 | DumpInt(f->linedefined,D); 141 | DumpInt(f->lastlinedefined,D); 142 | DumpChar(f->numparams,D); 143 | DumpChar(f->is_vararg,D); 144 | DumpChar(f->maxstacksize,D); 145 | DumpCode(f,D); 146 | DumpConstants(f,D); 147 | DumpUpvalues(f,D); 148 | DumpDebug(f,D); 149 | } 150 | 151 | static void DumpHeader(DumpState* D) 152 | { 153 | lu_byte h[LUAC_HEADERSIZE]; 154 | luaU_header(h); 155 | DumpBlock(h,LUAC_HEADERSIZE,D); 156 | } 157 | 158 | /* 159 | ** dump Lua function as precompiled chunk 160 | */ 161 | int luaU_dump (lua_State* L, const Proto* f, lua_Writer w, void* data, int strip) 162 | { 163 | DumpState D; 164 | D.L=L; 165 | D.writer=w; 166 | D.data=data; 167 | D.strip=strip; 168 | D.status=0; 169 | DumpHeader(&D); 170 | DumpFunction(f,&D); 171 | return D.status; 172 | } 173 | -------------------------------------------------------------------------------- /deps/lua/src/lfunc.c: -------------------------------------------------------------------------------- 1 | /* 2 | ** $Id: lfunc.c,v 2.27 2010/06/30 14:11:17 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_TFUNCTION, sizeCclosure(n), NULL, 0)->cl; 25 | c->c.isC = 1; 26 | c->c.nupvalues = cast_byte(n); 27 | return c; 28 | } 29 | 30 | 31 | Closure *luaF_newLclosure (lua_State *L, Proto *p) { 32 | int n = p->sizeupvalues; 33 | Closure *c = &luaC_newobj(L, LUA_TFUNCTION, sizeLclosure(n), NULL, 0)->cl; 34 | c->l.isC = 0; 35 | c->l.p = p; 36 | c->l.nupvalues = cast_byte(n); 37 | while (n--) c->l.upvals[n] = NULL; 38 | return c; 39 | } 40 | 41 | 42 | UpVal *luaF_newupval (lua_State *L) { 43 | UpVal *uv = &luaC_newobj(L, LUA_TUPVAL, sizeof(UpVal), NULL, 0)->uv; 44 | uv->v = &uv->u.value; 45 | setnilvalue(uv->v); 46 | return uv; 47 | } 48 | 49 | 50 | UpVal *luaF_findupval (lua_State *L, StkId level) { 51 | global_State *g = G(L); 52 | GCObject **pp = &L->openupval; 53 | UpVal *p; 54 | UpVal *uv; 55 | while (*pp != NULL && (p = gco2uv(*pp))->v >= level) { 56 | GCObject *o = obj2gco(p); 57 | lua_assert(p->v != &p->u.value); 58 | if (p->v == level) { /* found a corresponding upvalue? */ 59 | if (isdead(g, o)) /* is it dead? */ 60 | changewhite(o); /* resurrect it */ 61 | return p; 62 | } 63 | resetoldbit(o); /* may create a newer upval after this one */ 64 | pp = &p->next; 65 | } 66 | /* not found: create a new one */ 67 | uv = &luaC_newobj(L, LUA_TUPVAL, sizeof(UpVal), pp, 0)->uv; 68 | uv->v = level; /* current value lives in the stack */ 69 | uv->u.l.prev = &g->uvhead; /* double link it in `uvhead' list */ 70 | uv->u.l.next = g->uvhead.u.l.next; 71 | uv->u.l.next->u.l.prev = uv; 72 | g->uvhead.u.l.next = uv; 73 | lua_assert(uv->u.l.next->u.l.prev == uv && uv->u.l.prev->u.l.next == uv); 74 | return uv; 75 | } 76 | 77 | 78 | static void unlinkupval (UpVal *uv) { 79 | lua_assert(uv->u.l.next->u.l.prev == uv && uv->u.l.prev->u.l.next == uv); 80 | uv->u.l.next->u.l.prev = uv->u.l.prev; /* remove from `uvhead' list */ 81 | uv->u.l.prev->u.l.next = uv->u.l.next; 82 | } 83 | 84 | 85 | void luaF_freeupval (lua_State *L, UpVal *uv) { 86 | if (uv->v != &uv->u.value) /* is it open? */ 87 | unlinkupval(uv); /* remove from open list */ 88 | luaM_free(L, uv); /* free upvalue */ 89 | } 90 | 91 | 92 | void luaF_close (lua_State *L, StkId level) { 93 | UpVal *uv; 94 | global_State *g = G(L); 95 | while (L->openupval != NULL && (uv = gco2uv(L->openupval))->v >= level) { 96 | GCObject *o = obj2gco(uv); 97 | lua_assert(!isblack(o) && uv->v != &uv->u.value); 98 | L->openupval = uv->next; /* remove from `open' list */ 99 | if (isdead(g, o)) 100 | luaF_freeupval(L, uv); /* free upvalue */ 101 | else { 102 | unlinkupval(uv); /* remove upvalue from 'uvhead' list */ 103 | setobj(L, &uv->u.value, uv->v); /* move value to upvalue slot */ 104 | uv->v = &uv->u.value; /* now current value lives here */ 105 | gch(o)->next = g->allgc; /* link upvalue into 'allgc' list */ 106 | g->allgc = o; 107 | luaC_checkupvalcolor(g, uv); 108 | } 109 | } 110 | } 111 | 112 | 113 | Proto *luaF_newproto (lua_State *L) { 114 | Proto *f = &luaC_newobj(L, LUA_TPROTO, sizeof(Proto), NULL, 0)->p; 115 | f->k = NULL; 116 | f->sizek = 0; 117 | f->p = NULL; 118 | f->sizep = 0; 119 | f->code = NULL; 120 | f->cache = NULL; 121 | f->sizecode = 0; 122 | f->lineinfo = NULL; 123 | f->sizelineinfo = 0; 124 | f->upvalues = NULL; 125 | f->sizeupvalues = 0; 126 | f->numparams = 0; 127 | f->is_vararg = 0; 128 | f->maxstacksize = 0; 129 | f->locvars = NULL; 130 | f->sizelocvars = 0; 131 | f->linedefined = 0; 132 | f->lastlinedefined = 0; 133 | f->source = NULL; 134 | return f; 135 | } 136 | 137 | 138 | void luaF_freeproto (lua_State *L, Proto *f) { 139 | luaM_freearray(L, f->code, f->sizecode); 140 | luaM_freearray(L, f->p, f->sizep); 141 | luaM_freearray(L, f->k, f->sizek); 142 | luaM_freearray(L, f->lineinfo, f->sizelineinfo); 143 | luaM_freearray(L, f->locvars, f->sizelocvars); 144 | luaM_freearray(L, f->upvalues, f->sizeupvalues); 145 | luaM_free(L, f); 146 | } 147 | 148 | 149 | void luaF_freeclosure (lua_State *L, Closure *c) { 150 | int size = (c->c.isC) ? sizeCclosure(c->c.nupvalues) : 151 | sizeLclosure(c->l.nupvalues); 152 | luaM_freemem(L, c, size); 153 | } 154 | 155 | 156 | /* 157 | ** Look for n-th local variable at line `line' in function `func'. 158 | ** Returns NULL if not found. 159 | */ 160 | const char *luaF_getlocalname (const Proto *f, int local_number, int pc) { 161 | int i; 162 | for (i = 0; isizelocvars && f->locvars[i].startpc <= pc; i++) { 163 | if (pc < f->locvars[i].endpc) { /* is variable active? */ 164 | local_number--; 165 | if (local_number == 0) 166 | return getstr(f->locvars[i].varname); 167 | } 168 | } 169 | return NULL; /* not found */ 170 | } 171 | 172 | -------------------------------------------------------------------------------- /deps/lua/src/lfunc.h: -------------------------------------------------------------------------------- 1 | /* 2 | ** $Id: lfunc.h,v 2.6 2010/06/04 13:06:15 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, Proto *p); 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_freeclosure (lua_State *L, Closure *c); 29 | LUAI_FUNC void luaF_freeupval (lua_State *L, UpVal *uv); 30 | LUAI_FUNC const char *luaF_getlocalname (const Proto *func, int local_number, 31 | int pc); 32 | 33 | 34 | #endif 35 | -------------------------------------------------------------------------------- /deps/lua/src/lgc.h: -------------------------------------------------------------------------------- 1 | /* 2 | ** $Id: lgc.h,v 2.52 2011/10/03 17:54:25 roberto Exp $ 3 | ** Garbage Collector 4 | ** See Copyright Notice in lua.h 5 | */ 6 | 7 | #ifndef lgc_h 8 | #define lgc_h 9 | 10 | 11 | #include "lobject.h" 12 | #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 | ** Possible states of the Garbage Collector 30 | */ 31 | #define GCSpropagate 0 32 | #define GCSatomic 1 33 | #define GCSsweepstring 2 34 | #define GCSsweepudata 3 35 | #define GCSsweep 4 36 | #define GCSpause 5 37 | 38 | 39 | #define issweepphase(g) \ 40 | (GCSsweepstring <= (g)->gcstate && (g)->gcstate <= GCSsweep) 41 | 42 | #define isgenerational(g) ((g)->gckind == KGC_GEN) 43 | 44 | /* 45 | ** macro to tell when main invariant (white objects cannot point to black 46 | ** ones) must be kept. During a non-generational collection, the sweep 47 | ** phase may break the invariant, as objects turned white may point to 48 | ** still-black objects. The invariant is restored when sweep ends and 49 | ** all objects are white again. During a generational collection, the 50 | ** invariant must be kept all times. 51 | */ 52 | #define keepinvariant(g) (isgenerational(g) || g->gcstate <= GCSatomic) 53 | 54 | 55 | /* 56 | ** some useful bit tricks 57 | */ 58 | #define resetbits(x,m) ((x) &= cast(lu_byte, ~(m))) 59 | #define setbits(x,m) ((x) |= (m)) 60 | #define testbits(x,m) ((x) & (m)) 61 | #define bitmask(b) (1<<(b)) 62 | #define bit2mask(b1,b2) (bitmask(b1) | bitmask(b2)) 63 | #define l_setbit(x,b) setbits(x, bitmask(b)) 64 | #define resetbit(x,b) resetbits(x, bitmask(b)) 65 | #define testbit(x,b) testbits(x, bitmask(b)) 66 | 67 | 68 | /* Layout for bit use in `marked' field: */ 69 | #define WHITE0BIT 0 /* object is white (type 0) */ 70 | #define WHITE1BIT 1 /* object is white (type 1) */ 71 | #define BLACKBIT 2 /* object is black */ 72 | #define FINALIZEDBIT 3 /* object has been separated for finalization */ 73 | #define SEPARATED 4 /* object is in 'finobj' list or in 'tobefnz' */ 74 | #define FIXEDBIT 5 /* object is fixed (should not be collected) */ 75 | #define OLDBIT 6 /* object is old (only in generational mode) */ 76 | /* bit 7 is currently used by tests (luaL_checkmemory) */ 77 | 78 | #define WHITEBITS bit2mask(WHITE0BIT, WHITE1BIT) 79 | 80 | 81 | #define iswhite(x) testbits((x)->gch.marked, WHITEBITS) 82 | #define isblack(x) testbit((x)->gch.marked, BLACKBIT) 83 | #define isgray(x) /* neither white nor black */ \ 84 | (!testbits((x)->gch.marked, WHITEBITS | bitmask(BLACKBIT))) 85 | 86 | #define isold(x) testbit((x)->gch.marked, OLDBIT) 87 | 88 | /* MOVE OLD rule: whenever an object is moved to the beginning of 89 | a GC list, its old bit must be cleared */ 90 | #define resetoldbit(o) resetbit((o)->gch.marked, OLDBIT) 91 | 92 | #define otherwhite(g) (g->currentwhite ^ WHITEBITS) 93 | #define isdeadm(ow,m) (!(((m) ^ WHITEBITS) & (ow))) 94 | #define isdead(g,v) isdeadm(otherwhite(g), (v)->gch.marked) 95 | 96 | #define changewhite(x) ((x)->gch.marked ^= WHITEBITS) 97 | #define gray2black(x) l_setbit((x)->gch.marked, BLACKBIT) 98 | 99 | #define valiswhite(x) (iscollectable(x) && iswhite(gcvalue(x))) 100 | 101 | #define luaC_white(g) cast(lu_byte, (g)->currentwhite & WHITEBITS) 102 | 103 | 104 | #define luaC_condGC(L,c) \ 105 | {if (G(L)->GCdebt > 0) {c;}; condchangemem(L);} 106 | #define luaC_checkGC(L) luaC_condGC(L, luaC_step(L);) 107 | 108 | 109 | #define luaC_barrier(L,p,v) { if (valiswhite(v) && isblack(obj2gco(p))) \ 110 | luaC_barrier_(L,obj2gco(p),gcvalue(v)); } 111 | 112 | #define luaC_barrierback(L,p,v) { if (valiswhite(v) && isblack(obj2gco(p))) \ 113 | luaC_barrierback_(L,p); } 114 | 115 | #define luaC_objbarrier(L,p,o) \ 116 | { if (iswhite(obj2gco(o)) && isblack(obj2gco(p))) \ 117 | luaC_barrier_(L,obj2gco(p),obj2gco(o)); } 118 | 119 | #define luaC_objbarrierback(L,p,o) \ 120 | { if (iswhite(obj2gco(o)) && isblack(obj2gco(p))) luaC_barrierback_(L,p); } 121 | 122 | #define luaC_barrierproto(L,p,c) \ 123 | { if (isblack(obj2gco(p))) luaC_barrierproto_(L,p,c); } 124 | 125 | LUAI_FUNC void luaC_freeallobjects (lua_State *L); 126 | LUAI_FUNC void luaC_step (lua_State *L); 127 | LUAI_FUNC void luaC_forcestep (lua_State *L); 128 | LUAI_FUNC void luaC_runtilstate (lua_State *L, int statesmask); 129 | LUAI_FUNC void luaC_fullgc (lua_State *L, int isemergency); 130 | LUAI_FUNC GCObject *luaC_newobj (lua_State *L, int tt, size_t sz, 131 | GCObject **list, int offset); 132 | LUAI_FUNC void luaC_barrier_ (lua_State *L, GCObject *o, GCObject *v); 133 | LUAI_FUNC void luaC_barrierback_ (lua_State *L, GCObject *o); 134 | LUAI_FUNC void luaC_barrierproto_ (lua_State *L, Proto *p, Closure *c); 135 | LUAI_FUNC void luaC_checkfinalizer (lua_State *L, GCObject *o, Table *mt); 136 | LUAI_FUNC void luaC_checkupvalcolor (global_State *g, UpVal *uv); 137 | LUAI_FUNC void luaC_changemode (lua_State *L, int mode); 138 | 139 | #endif 140 | -------------------------------------------------------------------------------- /deps/lua/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 | -------------------------------------------------------------------------------- /deps/lua/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 | -------------------------------------------------------------------------------- /deps/lua/src/llimits.h: -------------------------------------------------------------------------------- 1 | /* 2 | ** $Id: llimits.h,v 1.95 2011/12/06 16:58:36 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 | 35 | #define MAX_INT (INT_MAX-2) /* maximum value of an int (-2 for safety) */ 36 | 37 | /* 38 | ** conversion of pointer to integer 39 | ** this is for hashing only; there is no problem if the integer 40 | ** cannot hold the whole pointer value 41 | */ 42 | #define IntPoint(p) ((unsigned int)(lu_mem)(p)) 43 | 44 | 45 | 46 | /* type to ensure maximum alignment */ 47 | #if !defined(LUAI_USER_ALIGNMENT_T) 48 | #define LUAI_USER_ALIGNMENT_T union { double u; void *s; long l; } 49 | #endif 50 | 51 | typedef LUAI_USER_ALIGNMENT_T L_Umaxalign; 52 | 53 | 54 | /* result of a `usual argument conversion' over lua_Number */ 55 | typedef LUAI_UACNUMBER l_uacNumber; 56 | 57 | 58 | /* internal assertions for in-house debugging */ 59 | #if defined(lua_assert) 60 | #define check_exp(c,e) (lua_assert(c), (e)) 61 | /* to avoid problems with conditions too long */ 62 | #define lua_longassert(c) { if (!(c)) lua_assert(0); } 63 | #else 64 | #define lua_assert(c) ((void)0) 65 | #define check_exp(c,e) (e) 66 | #define lua_longassert(c) ((void)0) 67 | #endif 68 | 69 | /* 70 | ** assertion for checking API calls 71 | */ 72 | #if !defined(luai_apicheck) 73 | 74 | #if defined(LUA_USE_APICHECK) 75 | #include 76 | #define luai_apicheck(L,e) assert(e) 77 | #else 78 | #define luai_apicheck(L,e) lua_assert(e) 79 | #endif 80 | 81 | #endif 82 | 83 | #define api_check(l,e,msg) luai_apicheck(l,(e) && msg) 84 | 85 | 86 | #if !defined(UNUSED) 87 | #define UNUSED(x) ((void)(x)) /* to avoid warnings */ 88 | #endif 89 | 90 | 91 | #define cast(t, exp) ((t)(exp)) 92 | 93 | #define cast_byte(i) cast(lu_byte, (i)) 94 | #define cast_num(i) cast(lua_Number, (i)) 95 | #define cast_int(i) cast(int, (i)) 96 | #define cast_uchar(i) cast(unsigned char, (i)) 97 | 98 | 99 | /* 100 | ** non-return type 101 | */ 102 | #if defined(__GNUC__) 103 | #define l_noret void __attribute__((noreturn)) 104 | #elif defined(_MSC_VER) 105 | #define l_noret void __declspec(noreturn) 106 | #else 107 | #define l_noret void 108 | #endif 109 | 110 | 111 | 112 | /* 113 | ** maximum depth for nested C calls and syntactical nested non-terminals 114 | ** in a program. (Value must fit in an unsigned short int.) 115 | */ 116 | #if !defined(LUAI_MAXCCALLS) 117 | #define LUAI_MAXCCALLS 200 118 | #endif 119 | 120 | /* 121 | ** maximum number of upvalues in a closure (both C and Lua). (Value 122 | ** must fit in an unsigned char.) 123 | */ 124 | #define MAXUPVAL UCHAR_MAX 125 | 126 | 127 | /* 128 | ** type for virtual-machine instructions 129 | ** must be an unsigned with (at least) 4 bytes (see details in lopcodes.h) 130 | */ 131 | typedef lu_int32 Instruction; 132 | 133 | 134 | 135 | /* maximum stack for a Lua function */ 136 | #define MAXSTACK 250 137 | 138 | 139 | 140 | /* minimum size for the string table (must be power of 2) */ 141 | #if !defined(MINSTRTABSIZE) 142 | #define MINSTRTABSIZE 32 143 | #endif 144 | 145 | 146 | /* minimum size for string buffer */ 147 | #if !defined(LUA_MINBUFFER) 148 | #define LUA_MINBUFFER 32 149 | #endif 150 | 151 | 152 | #if !defined(lua_lock) 153 | #define lua_lock(L) ((void) 0) 154 | #define lua_unlock(L) ((void) 0) 155 | #endif 156 | 157 | #if !defined(luai_threadyield) 158 | #define luai_threadyield(L) {lua_unlock(L); lua_lock(L);} 159 | #endif 160 | 161 | 162 | /* 163 | ** these macros allow user-specific actions on threads when you defined 164 | ** LUAI_EXTRASPACE and need to do something extra when a thread is 165 | ** created/deleted/resumed/yielded. 166 | */ 167 | #if !defined(luai_userstateopen) 168 | #define luai_userstateopen(L) ((void)L) 169 | #endif 170 | 171 | #if !defined(luai_userstateclose) 172 | #define luai_userstateclose(L) ((void)L) 173 | #endif 174 | 175 | #if !defined(luai_userstatethread) 176 | #define luai_userstatethread(L,L1) ((void)L) 177 | #endif 178 | 179 | #if !defined(luai_userstatefree) 180 | #define luai_userstatefree(L,L1) ((void)L) 181 | #endif 182 | 183 | #if !defined(luai_userstateresume) 184 | #define luai_userstateresume(L,n) ((void)L) 185 | #endif 186 | 187 | #if !defined(luai_userstateyield) 188 | #define luai_userstateyield(L,n) ((void)L) 189 | #endif 190 | 191 | /* 192 | ** lua_number2int is a macro to convert lua_Number to int. 193 | ** lua_number2integer is a macro to convert lua_Number to lua_Integer. 194 | ** lua_number2unsigned is a macro to convert a lua_Number to a lua_Unsigned. 195 | ** lua_unsigned2number is a macro to convert a lua_Unsigned to a lua_Number. 196 | ** luai_hashnum is a macro to hash a lua_Number value into an integer. 197 | ** The hash must be deterministic and give reasonable values for 198 | ** both small and large values (outside the range of integers). 199 | */ 200 | 201 | #if defined(MS_ASMTRICK) /* { */ 202 | /* trick with Microsoft assembler for X86 */ 203 | 204 | #define lua_number2int(i,n) __asm {__asm fld n __asm fistp i} 205 | #define lua_number2integer(i,n) lua_number2int(i, n) 206 | #define lua_number2unsigned(i,n) \ 207 | {__int64 l; __asm {__asm fld n __asm fistp l} i = (unsigned int)l;} 208 | 209 | 210 | #elif defined(LUA_IEEE754TRICK) /* }{ */ 211 | /* the next trick should work on any machine using IEEE754 with 212 | a 32-bit integer type */ 213 | 214 | union luai_Cast { double l_d; LUA_INT32 l_p[2]; }; 215 | 216 | #if !defined(LUA_IEEEENDIAN) /* { */ 217 | #define LUAI_EXTRAIEEE \ 218 | static const union luai_Cast ieeeendian = {-(33.0 + 6755399441055744.0)}; 219 | #define LUA_IEEEENDIAN (ieeeendian.l_p[1] == 33) 220 | #else 221 | #define LUAI_EXTRAIEEE /* empty */ 222 | #endif /* } */ 223 | 224 | #define lua_number2int32(i,n,t) \ 225 | { LUAI_EXTRAIEEE \ 226 | volatile union luai_Cast u; u.l_d = (n) + 6755399441055744.0; \ 227 | (i) = (t)u.l_p[LUA_IEEEENDIAN]; } 228 | 229 | #define luai_hashnum(i,n) \ 230 | { volatile union luai_Cast u; u.l_d = (n) + 1.0; /* avoid -0 */ \ 231 | (i) = u.l_p[0]; (i) += u.l_p[1]; } /* add double bits for his hash */ 232 | 233 | #define lua_number2int(i,n) lua_number2int32(i, n, int) 234 | #define lua_number2integer(i,n) lua_number2int32(i, n, lua_Integer) 235 | #define lua_number2unsigned(i,n) lua_number2int32(i, n, lua_Unsigned) 236 | 237 | #endif /* } */ 238 | 239 | 240 | /* the following definitions always work, but may be slow */ 241 | 242 | #if !defined(lua_number2int) 243 | #define lua_number2int(i,n) ((i)=(int)(n)) 244 | #endif 245 | 246 | #if !defined(lua_number2integer) 247 | #define lua_number2integer(i,n) ((i)=(lua_Integer)(n)) 248 | #endif 249 | 250 | #if !defined(lua_number2unsigned) /* { */ 251 | /* the following definition assures proper modulo behavior */ 252 | #if defined(LUA_NUMBER_DOUBLE) 253 | #include 254 | #define SUPUNSIGNED ((lua_Number)(~(lua_Unsigned)0) + 1) 255 | #define lua_number2unsigned(i,n) \ 256 | ((i)=(lua_Unsigned)((n) - floor((n)/SUPUNSIGNED)*SUPUNSIGNED)) 257 | #else 258 | #define lua_number2unsigned(i,n) ((i)=(lua_Unsigned)(n)) 259 | #endif 260 | #endif /* } */ 261 | 262 | 263 | #if !defined(lua_unsigned2number) 264 | /* on several machines, coercion from unsigned to double is slow, 265 | so it may be worth to avoid */ 266 | #define lua_unsigned2number(u) \ 267 | (((u) <= (lua_Unsigned)INT_MAX) ? (lua_Number)(int)(u) : (lua_Number)(u)) 268 | #endif 269 | 270 | 271 | 272 | #if defined(ltable_c) && !defined(luai_hashnum) 273 | 274 | #include 275 | #include 276 | 277 | #define luai_hashnum(i,n) { int e; \ 278 | n = frexp(n, &e) * (lua_Number)(INT_MAX - DBL_MAX_EXP); \ 279 | lua_number2int(i, n); i += e; } 280 | 281 | #endif 282 | 283 | 284 | 285 | /* 286 | ** macro to control inclusion of some hard tests on stack reallocation 287 | */ 288 | #if !defined(HARDSTACKTESTS) 289 | #define condmovestack(L) ((void)0) 290 | #else 291 | /* realloc stack keeping its size */ 292 | #define condmovestack(L) luaD_reallocstack((L), (L)->stacksize) 293 | #endif 294 | 295 | #if !defined(HARDMEMTESTS) 296 | #define condchangemem(L) condmovestack(L) 297 | #else 298 | #define condchangemem(L) \ 299 | ((void)(!(G(L)->gcrunning) || (luaC_fullgc(L, 0), 1))) 300 | #endif 301 | 302 | #endif 303 | -------------------------------------------------------------------------------- /deps/lua/src/lmathlib.c: -------------------------------------------------------------------------------- 1 | /* 2 | ** $Id: lmathlib.c,v 1.80 2011/07/05 12:49:35 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 | #undef PI 21 | #define PI (3.14159265358979323846) 22 | #define RADIANS_PER_DEGREE (PI/180.0) 23 | 24 | 25 | /* macro 'l_tg' allows the addition of an 'l' or 'f' to all math operations */ 26 | #if !defined(l_tg) 27 | #define l_tg(x) (x) 28 | #endif 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 | -------------------------------------------------------------------------------- /deps/lua/src/lmem.c: -------------------------------------------------------------------------------- 1 | /* 2 | ** $Id: lmem.c,v 1.83 2011/11/30 12:42:49 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 | #if defined(TRACEMEM) 98 | { /* auxiliary patch to monitor garbage collection. 99 | ** To plot, gnuplot with following command: 100 | ** plot TRACEMEM using 1:2 with lines, TRACEMEM using 1:3 with lines 101 | */ 102 | static unsigned long total = 0; /* our "time" */ 103 | static FILE *f = NULL; /* output file */ 104 | total++; /* "time" always grows */ 105 | if ((total % 200) == 0) { 106 | if (f == NULL) f = fopen(TRACEMEM, "w"); 107 | fprintf(f, "%lu %u %d %d\n", total, 108 | gettotalbytes(g), g->GCdebt, g->gcstate * 10000); 109 | } 110 | } 111 | #endif 112 | 113 | return newblock; 114 | } 115 | 116 | -------------------------------------------------------------------------------- /deps/lua/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 | -------------------------------------------------------------------------------- /deps/lua/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 | -------------------------------------------------------------------------------- /deps/lua/src/lopcodes.c: -------------------------------------------------------------------------------- 1 | /* 2 | ** $Id: lopcodes.c,v 1.48 2011/04/19 16:22:13 roberto Exp $ 3 | ** See Copyright Notice in lua.h 4 | */ 5 | 6 | 7 | #define lopcodes_c 8 | #define LUA_CORE 9 | 10 | 11 | #include "lopcodes.h" 12 | 13 | 14 | /* ORDER OP */ 15 | 16 | LUAI_DDEF const char *const luaP_opnames[NUM_OPCODES+1] = { 17 | "MOVE", 18 | "LOADK", 19 | "LOADKX", 20 | "LOADBOOL", 21 | "LOADNIL", 22 | "GETUPVAL", 23 | "GETTABUP", 24 | "GETTABLE", 25 | "SETTABUP", 26 | "SETUPVAL", 27 | "SETTABLE", 28 | "NEWTABLE", 29 | "SELF", 30 | "ADD", 31 | "SUB", 32 | "MUL", 33 | "DIV", 34 | "MOD", 35 | "POW", 36 | "UNM", 37 | "NOT", 38 | "LEN", 39 | "CONCAT", 40 | "JMP", 41 | "EQ", 42 | "LT", 43 | "LE", 44 | "TEST", 45 | "TESTSET", 46 | "CALL", 47 | "TAILCALL", 48 | "RETURN", 49 | "FORLOOP", 50 | "FORPREP", 51 | "TFORCALL", 52 | "TFORLOOP", 53 | "SETLIST", 54 | "CLOSURE", 55 | "VARARG", 56 | "EXTRAARG", 57 | NULL 58 | }; 59 | 60 | 61 | #define opmode(t,a,b,c,m) (((t)<<7) | ((a)<<6) | ((b)<<4) | ((c)<<2) | (m)) 62 | 63 | LUAI_DDEF const lu_byte luaP_opmodes[NUM_OPCODES] = { 64 | /* T A B C mode opcode */ 65 | opmode(0, 1, OpArgR, OpArgN, iABC) /* OP_MOVE */ 66 | ,opmode(0, 1, OpArgK, OpArgN, iABx) /* OP_LOADK */ 67 | ,opmode(0, 1, OpArgN, OpArgN, iABx) /* OP_LOADKX */ 68 | ,opmode(0, 1, OpArgU, OpArgU, iABC) /* OP_LOADBOOL */ 69 | ,opmode(0, 1, OpArgU, OpArgN, iABC) /* OP_LOADNIL */ 70 | ,opmode(0, 1, OpArgU, OpArgN, iABC) /* OP_GETUPVAL */ 71 | ,opmode(0, 1, OpArgU, OpArgK, iABC) /* OP_GETTABUP */ 72 | ,opmode(0, 1, OpArgR, OpArgK, iABC) /* OP_GETTABLE */ 73 | ,opmode(0, 0, OpArgK, OpArgK, iABC) /* OP_SETTABUP */ 74 | ,opmode(0, 0, OpArgU, OpArgN, iABC) /* OP_SETUPVAL */ 75 | ,opmode(0, 0, OpArgK, OpArgK, iABC) /* OP_SETTABLE */ 76 | ,opmode(0, 1, OpArgU, OpArgU, iABC) /* OP_NEWTABLE */ 77 | ,opmode(0, 1, OpArgR, OpArgK, iABC) /* OP_SELF */ 78 | ,opmode(0, 1, OpArgK, OpArgK, iABC) /* OP_ADD */ 79 | ,opmode(0, 1, OpArgK, OpArgK, iABC) /* OP_SUB */ 80 | ,opmode(0, 1, OpArgK, OpArgK, iABC) /* OP_MUL */ 81 | ,opmode(0, 1, OpArgK, OpArgK, iABC) /* OP_DIV */ 82 | ,opmode(0, 1, OpArgK, OpArgK, iABC) /* OP_MOD */ 83 | ,opmode(0, 1, OpArgK, OpArgK, iABC) /* OP_POW */ 84 | ,opmode(0, 1, OpArgR, OpArgN, iABC) /* OP_UNM */ 85 | ,opmode(0, 1, OpArgR, OpArgN, iABC) /* OP_NOT */ 86 | ,opmode(0, 1, OpArgR, OpArgN, iABC) /* OP_LEN */ 87 | ,opmode(0, 1, OpArgR, OpArgR, iABC) /* OP_CONCAT */ 88 | ,opmode(0, 0, OpArgR, OpArgN, iAsBx) /* OP_JMP */ 89 | ,opmode(1, 0, OpArgK, OpArgK, iABC) /* OP_EQ */ 90 | ,opmode(1, 0, OpArgK, OpArgK, iABC) /* OP_LT */ 91 | ,opmode(1, 0, OpArgK, OpArgK, iABC) /* OP_LE */ 92 | ,opmode(1, 0, OpArgN, OpArgU, iABC) /* OP_TEST */ 93 | ,opmode(1, 1, OpArgR, OpArgU, iABC) /* OP_TESTSET */ 94 | ,opmode(0, 1, OpArgU, OpArgU, iABC) /* OP_CALL */ 95 | ,opmode(0, 1, OpArgU, OpArgU, iABC) /* OP_TAILCALL */ 96 | ,opmode(0, 0, OpArgU, OpArgN, iABC) /* OP_RETURN */ 97 | ,opmode(0, 1, OpArgR, OpArgN, iAsBx) /* OP_FORLOOP */ 98 | ,opmode(0, 1, OpArgR, OpArgN, iAsBx) /* OP_FORPREP */ 99 | ,opmode(0, 0, OpArgN, OpArgU, iABC) /* OP_TFORCALL */ 100 | ,opmode(0, 1, OpArgR, OpArgN, iAsBx) /* OP_TFORLOOP */ 101 | ,opmode(0, 0, OpArgU, OpArgU, iABC) /* OP_SETLIST */ 102 | ,opmode(0, 1, OpArgU, OpArgN, iABx) /* OP_CLOSURE */ 103 | ,opmode(0, 1, OpArgU, OpArgN, iABC) /* OP_VARARG */ 104 | ,opmode(0, 0, OpArgU, OpArgU, iAx) /* OP_EXTRAARG */ 105 | }; 106 | 107 | -------------------------------------------------------------------------------- /deps/lua/src/lparser.h: -------------------------------------------------------------------------------- 1 | /* 2 | ** $Id: lparser.h,v 1.69 2011/07/27 18:09:01 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 Proto *luaY_parser (lua_State *L, ZIO *z, Mbuffer *buff, 116 | Dyndata *dyd, const char *name, int firstchar); 117 | 118 | 119 | #endif 120 | -------------------------------------------------------------------------------- /deps/lua/src/lstate.c: -------------------------------------------------------------------------------- 1 | /* 2 | ** $Id: lstate.c,v 2.92 2011/10/03 17:54:25 roberto Exp $ 3 | ** Global State 4 | ** See Copyright Notice in lua.h 5 | */ 6 | 7 | 8 | #include 9 | 10 | #define lstate_c 11 | #define LUA_CORE 12 | 13 | #include "lua.h" 14 | 15 | #include "lapi.h" 16 | #include "ldebug.h" 17 | #include "ldo.h" 18 | #include "lfunc.h" 19 | #include "lgc.h" 20 | #include "llex.h" 21 | #include "lmem.h" 22 | #include "lstate.h" 23 | #include "lstring.h" 24 | #include "ltable.h" 25 | #include "ltm.h" 26 | 27 | 28 | #if !defined(LUAI_GCPAUSE) 29 | #define LUAI_GCPAUSE 200 /* 200% */ 30 | #endif 31 | 32 | #if !defined(LUAI_GCMAJOR) 33 | #define LUAI_GCMAJOR 200 /* 200% */ 34 | #endif 35 | 36 | #if !defined(LUAI_GCMUL) 37 | #define LUAI_GCMUL 200 /* GC runs 'twice the speed' of memory allocation */ 38 | #endif 39 | 40 | 41 | #define MEMERRMSG "not enough memory" 42 | 43 | 44 | /* 45 | ** thread state + extra space 46 | */ 47 | typedef struct LX { 48 | #if defined(LUAI_EXTRASPACE) 49 | char buff[LUAI_EXTRASPACE]; 50 | #endif 51 | lua_State l; 52 | } LX; 53 | 54 | 55 | /* 56 | ** Main thread combines a thread state and the global state 57 | */ 58 | typedef struct LG { 59 | LX l; 60 | global_State g; 61 | } LG; 62 | 63 | 64 | 65 | #define fromstate(L) (cast(LX *, cast(lu_byte *, (L)) - offsetof(LX, l))) 66 | 67 | 68 | /* 69 | ** set GCdebt to a new value keeping the value (totalbytes + GCdebt) 70 | ** invariant 71 | */ 72 | void luaE_setdebt (global_State *g, l_mem debt) { 73 | g->totalbytes -= (debt - g->GCdebt); 74 | g->GCdebt = debt; 75 | } 76 | 77 | 78 | CallInfo *luaE_extendCI (lua_State *L) { 79 | CallInfo *ci = luaM_new(L, CallInfo); 80 | lua_assert(L->ci->next == NULL); 81 | L->ci->next = ci; 82 | ci->previous = L->ci; 83 | ci->next = NULL; 84 | return ci; 85 | } 86 | 87 | 88 | void luaE_freeCI (lua_State *L) { 89 | CallInfo *ci = L->ci; 90 | CallInfo *next = ci->next; 91 | ci->next = NULL; 92 | while ((ci = next) != NULL) { 93 | next = ci->next; 94 | luaM_free(L, ci); 95 | } 96 | } 97 | 98 | 99 | // 初始化调用栈 100 | static void stack_init (lua_State *L1, lua_State *L) { 101 | int i; CallInfo *ci; 102 | /* initialize stack array */ 103 | L1->stack = luaM_newvector(L, BASIC_STACK_SIZE, TValue); 104 | L1->stacksize = BASIC_STACK_SIZE; 105 | for (i = 0; i < BASIC_STACK_SIZE; i++) 106 | setnilvalue(L1->stack + i); /* erase new stack */ 107 | L1->top = L1->stack; 108 | L1->stack_last = L1->stack + L1->stacksize - EXTRA_STACK; 109 | /* initialize first ci */ 110 | ci = &L1->base_ci; 111 | ci->next = ci->previous = NULL; 112 | ci->callstatus = 0; 113 | ci->func = L1->top; 114 | setnilvalue(L1->top++); /* 'function' entry for this 'ci' */ 115 | ci->top = L1->top + LUA_MINSTACK; 116 | L1->ci = ci; 117 | } 118 | 119 | 120 | static void freestack (lua_State *L) { 121 | if (L->stack == NULL) 122 | return; /* stack not completely built yet */ 123 | L->ci = &L->base_ci; /* free the entire 'ci' list */ 124 | luaE_freeCI(L); 125 | luaM_freearray(L, L->stack, L->stacksize); /* free stack array */ 126 | } 127 | 128 | 129 | /* 130 | ** Create registry table and its predefined values 131 | */ 132 | static void init_registry (lua_State *L, global_State *g) { 133 | TValue mt; 134 | /* create registry */ 135 | Table *registry = luaH_new(L); 136 | sethvalue(L, &g->l_registry, registry); 137 | luaH_resize(L, registry, LUA_RIDX_LAST, 0); 138 | /* registry[LUA_RIDX_MAINTHREAD] = L */ 139 | setthvalue(L, &mt, L); 140 | luaH_setint(L, registry, LUA_RIDX_MAINTHREAD, &mt); 141 | /* registry[LUA_RIDX_GLOBALS] = table of globals */ 142 | sethvalue(L, &mt, luaH_new(L)); 143 | luaH_setint(L, registry, LUA_RIDX_GLOBALS, &mt); 144 | } 145 | 146 | 147 | /* 148 | ** open parts of the state that may cause memory-allocation errors 149 | */ 150 | static void f_luaopen (lua_State *L, void *ud) { 151 | global_State *g = G(L); 152 | UNUSED(ud); 153 | stack_init(L, L); /* init stack */ 154 | init_registry(L, g); 155 | luaS_resize(L, MINSTRTABSIZE); /* initial size of string table */ 156 | luaT_init(L); 157 | luaX_init(L); 158 | /* pre-create memory-error message */ 159 | g->memerrmsg = luaS_newliteral(L, MEMERRMSG); 160 | luaS_fix(g->memerrmsg); /* it should never be collected */ 161 | g->gcrunning = 1; /* allow gc */ 162 | } 163 | 164 | 165 | /* 166 | ** preinitialize a state with consistent values without allocating 167 | ** any memory (to avoid errors) 168 | */ 169 | static void preinit_state (lua_State *L, global_State *g) { 170 | G(L) = g; 171 | L->stack = NULL; 172 | L->ci = NULL; 173 | L->stacksize = 0; 174 | L->errorJmp = NULL; 175 | L->nCcalls = 0; 176 | L->hook = NULL; 177 | L->hookmask = 0; 178 | L->basehookcount = 0; 179 | L->allowhook = 1; 180 | resethookcount(L); 181 | L->openupval = NULL; 182 | L->nny = 1; 183 | L->status = LUA_OK; 184 | L->errfunc = 0; 185 | } 186 | 187 | 188 | static void close_state (lua_State *L) { 189 | global_State *g = G(L); 190 | luaF_close(L, L->stack); /* close all upvalues for this thread */ 191 | luaC_freeallobjects(L); /* collect all objects */ 192 | luaM_freearray(L, G(L)->strt.hash, G(L)->strt.size); 193 | luaZ_freebuffer(L, &g->buff); 194 | freestack(L); 195 | lua_assert(gettotalbytes(g) == sizeof(LG)); 196 | (*g->frealloc)(g->ud, fromstate(L), sizeof(LG), 0); /* free main block */ 197 | } 198 | 199 | 200 | LUA_API lua_State *lua_newthread (lua_State *L) { 201 | lua_State *L1; 202 | lua_lock(L); 203 | luaC_checkGC(L); 204 | L1 = &luaC_newobj(L, LUA_TTHREAD, sizeof(LX), NULL, offsetof(LX, l))->th; 205 | setthvalue(L, L->top, L1); 206 | api_incr_top(L); 207 | preinit_state(L1, G(L)); 208 | L1->hookmask = L->hookmask; 209 | L1->basehookcount = L->basehookcount; 210 | L1->hook = L->hook; 211 | resethookcount(L1); 212 | luai_userstatethread(L, L1); 213 | stack_init(L1, L); /* init stack */ 214 | lua_unlock(L); 215 | return L1; 216 | } 217 | 218 | 219 | void luaE_freethread (lua_State *L, lua_State *L1) { 220 | LX *l = fromstate(L1); 221 | luaF_close(L1, L1->stack); /* close all upvalues for this thread */ 222 | lua_assert(L1->openupval == NULL); 223 | luai_userstatefree(L, L1); 224 | freestack(L1); 225 | luaM_free(L, l); 226 | } 227 | 228 | 229 | LUA_API lua_State *lua_newstate (lua_Alloc f, void *ud) { 230 | int i; 231 | lua_State *L; 232 | global_State *g; 233 | // 分配一个 lua_state 和 一个global_state 234 | LG *l = cast(LG *, (*f)(ud, NULL, LUA_TTHREAD, sizeof(LG))); 235 | if (l == NULL) return NULL; 236 | L = &l->l.l; 237 | g = &l->g; 238 | L->next = NULL; 239 | L->tt = LUA_TTHREAD; 240 | g->currentwhite = bit2mask(WHITE0BIT, FIXEDBIT); 241 | L->marked = luaC_white(g); 242 | g->gckind = KGC_NORMAL; 243 | preinit_state(L, g); 244 | g->frealloc = f; 245 | g->ud = ud; 246 | g->mainthread = L; 247 | g->uvhead.u.l.prev = &g->uvhead; 248 | g->uvhead.u.l.next = &g->uvhead; 249 | g->gcrunning = 0; /* no GC while building state */ 250 | g->lastmajormem = 0; 251 | g->strt.size = 0; 252 | g->strt.nuse = 0; 253 | g->strt.hash = NULL; 254 | // l_registry 设置为 NIL 255 | setnilvalue(&g->l_registry); 256 | luaZ_initbuffer(L, &g->buff); 257 | g->panic = NULL; 258 | g->version = lua_version(NULL); 259 | g->gcstate = GCSpause; 260 | g->allgc = NULL; 261 | g->finobj = NULL; 262 | g->tobefnz = NULL; 263 | g->gray = g->grayagain = NULL; 264 | g->weak = g->ephemeron = g->allweak = NULL; 265 | g->totalbytes = sizeof(LG); 266 | g->GCdebt = 0; 267 | g->gcpause = LUAI_GCPAUSE; 268 | g->gcmajorinc = LUAI_GCMAJOR; 269 | g->gcstepmul = LUAI_GCMUL; 270 | for (i=0; i < LUA_NUMTAGS; i++) g->mt[i] = NULL; 271 | if (luaD_rawrunprotected(L, f_luaopen, NULL) != LUA_OK) { 272 | /* memory allocation error: free partial state */ 273 | close_state(L); 274 | L = NULL; 275 | } 276 | else 277 | luai_userstateopen(L); 278 | return L; 279 | } 280 | 281 | 282 | LUA_API void lua_close (lua_State *L) { 283 | L = G(L)->mainthread; /* only the main thread can be closed */ 284 | lua_lock(L); 285 | luai_userstateclose(L); 286 | close_state(L); 287 | } 288 | 289 | 290 | -------------------------------------------------------------------------------- /deps/lua/src/lstate.h: -------------------------------------------------------------------------------- 1 | /* 2 | ** $Id: lstate.h,v 2.74 2011/09/30 12:45:07 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 | union { 76 | struct { /* only for Lua functions */ 77 | StkId base; /* base for this function */ 78 | const Instruction *savedpc; 79 | } l; 80 | struct { /* only for C functions */ 81 | int ctx; /* context info. in case of yields */ 82 | lua_CFunction k; /* continuation in case of yields */ 83 | ptrdiff_t old_errfunc; 84 | ptrdiff_t extra; 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 | 104 | 105 | #define isLua(ci) ((ci)->callstatus & CIST_LUA) 106 | 107 | 108 | /* 109 | ** `global state', shared by all threads of this state 110 | */ 111 | typedef struct global_State { 112 | lua_Alloc frealloc; /* function to reallocate memory */ 113 | void *ud; /* auxiliary data to `frealloc' */ 114 | lu_mem totalbytes; /* number of bytes currently allocated - GCdebt */ 115 | l_mem GCdebt; /* bytes allocated not yet compensated by the collector */ 116 | lu_mem lastmajormem; /* memory in use after last major collection */ 117 | stringtable strt; /* hash table for strings */ 118 | TValue l_registry; 119 | lu_byte currentwhite; 120 | lu_byte gcstate; /* state of garbage collector */ 121 | lu_byte gckind; /* kind of GC running */ 122 | lu_byte gcrunning; /* true if GC is running */ 123 | int sweepstrgc; /* position of sweep in `strt' */ 124 | GCObject *allgc; /* list of all collectable objects */ 125 | GCObject *finobj; /* list of collectable objects with finalizers */ 126 | GCObject **sweepgc; /* current position of sweep */ 127 | GCObject *gray; /* list of gray objects */ 128 | GCObject *grayagain; /* list of objects to be traversed atomically */ 129 | GCObject *weak; /* list of tables with weak values */ 130 | GCObject *ephemeron; /* list of ephemeron tables (weak keys) */ 131 | GCObject *allweak; /* list of all-weak tables */ 132 | GCObject *tobefnz; /* list of userdata to be GC */ 133 | UpVal uvhead; /* head of double-linked list of all open upvalues */ 134 | Mbuffer buff; /* temporary buffer for string concatenation */ 135 | int gcpause; /* size of pause between successive GCs */ 136 | int gcmajorinc; /* how much to wait for a major GC (only in gen. mode) */ 137 | int gcstepmul; /* GC `granularity' */ 138 | lua_CFunction panic; /* to be called in unprotected errors */ 139 | struct lua_State *mainthread; 140 | const lua_Number *version; /* pointer to version number */ 141 | TString *memerrmsg; /* memory-error message */ 142 | TString *tmname[TM_N]; /* array with tag-method names */ 143 | struct Table *mt[LUA_NUMTAGS]; /* metatables for basic types */ 144 | } global_State; 145 | 146 | 147 | /* 148 | ** `per thread' state 149 | */ 150 | struct lua_State { 151 | CommonHeader; 152 | lu_byte status; 153 | StkId top; /* first free slot in the stack */ 154 | global_State *l_G; 155 | CallInfo *ci; /* call info for current function */ 156 | const Instruction *oldpc; /* last pc traced */ 157 | StkId stack_last; /* last free slot in the stack */ 158 | StkId stack; /* stack base */ 159 | int stacksize; 160 | unsigned short nny; /* number of non-yieldable calls in stack */ 161 | unsigned short nCcalls; /* number of nested C calls */ 162 | lu_byte hookmask; 163 | lu_byte allowhook; 164 | int basehookcount; 165 | int hookcount; 166 | lua_Hook hook; 167 | GCObject *openupval; /* list of open upvalues in this stack */ 168 | GCObject *gclist; 169 | struct lua_longjmp *errorJmp; /* current error recover point */ 170 | ptrdiff_t errfunc; /* current error handling function (stack index) */ 171 | CallInfo base_ci; /* CallInfo for first level (C calling Lua) */ 172 | }; 173 | 174 | 175 | #define G(L) (L->l_G) 176 | 177 | 178 | /* 179 | ** Union of all collectable objects 180 | */ 181 | union GCObject { 182 | GCheader gch; /* common header */ 183 | union TString ts; 184 | union Udata u; 185 | union Closure cl; 186 | struct Table h; 187 | struct Proto p; 188 | struct UpVal uv; 189 | struct lua_State th; /* thread */ 190 | }; 191 | 192 | 193 | #define gch(o) (&(o)->gch) 194 | 195 | /* macros to convert a GCObject into a specific value */ 196 | #define rawgco2ts(o) check_exp((o)->gch.tt == LUA_TSTRING, &((o)->ts)) 197 | #define gco2ts(o) (&rawgco2ts(o)->tsv) 198 | #define rawgco2u(o) check_exp((o)->gch.tt == LUA_TUSERDATA, &((o)->u)) 199 | #define gco2u(o) (&rawgco2u(o)->uv) 200 | #define gco2cl(o) check_exp((o)->gch.tt == LUA_TFUNCTION, &((o)->cl)) 201 | #define gco2t(o) check_exp((o)->gch.tt == LUA_TTABLE, &((o)->h)) 202 | #define gco2p(o) check_exp((o)->gch.tt == LUA_TPROTO, &((o)->p)) 203 | #define gco2uv(o) check_exp((o)->gch.tt == LUA_TUPVAL, &((o)->uv)) 204 | #define gco2th(o) check_exp((o)->gch.tt == LUA_TTHREAD, &((o)->th)) 205 | 206 | /* macro to convert any Lua object into a GCObject */ 207 | #define obj2gco(v) (cast(GCObject *, (v))) 208 | 209 | 210 | /* actual number of total bytes allocated */ 211 | #define gettotalbytes(g) ((g)->totalbytes + (g)->GCdebt) 212 | 213 | LUAI_FUNC void luaE_setdebt (global_State *g, l_mem debt); 214 | LUAI_FUNC void luaE_freethread (lua_State *L, lua_State *L1); 215 | LUAI_FUNC CallInfo *luaE_extendCI (lua_State *L); 216 | LUAI_FUNC void luaE_freeCI (lua_State *L); 217 | 218 | 219 | #endif 220 | 221 | -------------------------------------------------------------------------------- /deps/lua/src/lstring.c: -------------------------------------------------------------------------------- 1 | /* 2 | ** $Id: lstring.c,v 2.19 2011/05/03 16:01:57 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 | void luaS_resize (lua_State *L, int newsize) { 23 | int i; 24 | stringtable *tb = &G(L)->strt; 25 | /* cannot resize while GC is traversing strings */ 26 | luaC_runtilstate(L, ~bitmask(GCSsweepstring)); 27 | if (newsize > tb->size) { 28 | luaM_reallocvector(L, tb->hash, tb->size, newsize, GCObject *); 29 | for (i = tb->size; i < newsize; i++) tb->hash[i] = NULL; 30 | } 31 | /* rehash */ 32 | for (i=0; isize; i++) { 33 | GCObject *p = tb->hash[i]; 34 | tb->hash[i] = NULL; 35 | while (p) { /* for each node in the list */ 36 | GCObject *next = gch(p)->next; /* save next */ 37 | unsigned int h = lmod(gco2ts(p)->hash, newsize); /* new position */ 38 | gch(p)->next = tb->hash[h]; /* chain it */ 39 | tb->hash[h] = p; 40 | resetoldbit(p); /* see MOVE OLD rule */ 41 | p = next; 42 | } 43 | } 44 | if (newsize < tb->size) { 45 | /* shrinking slice must be empty */ 46 | lua_assert(tb->hash[newsize] == NULL && tb->hash[tb->size - 1] == NULL); 47 | luaM_reallocvector(L, tb->hash, tb->size, newsize, GCObject *); 48 | } 49 | tb->size = newsize; 50 | } 51 | 52 | 53 | static TString *newlstr (lua_State *L, const char *str, size_t l, 54 | unsigned int h) { 55 | size_t totalsize; /* total size of TString object */ 56 | GCObject **list; /* (pointer to) list where it will be inserted */ 57 | TString *ts; 58 | stringtable *tb = &G(L)->strt; 59 | if (l+1 > (MAX_SIZET - sizeof(TString))/sizeof(char)) 60 | luaM_toobig(L); 61 | if (tb->nuse >= cast(lu_int32, tb->size) && tb->size <= MAX_INT/2) 62 | luaS_resize(L, tb->size*2); /* too crowded */ 63 | totalsize = sizeof(TString) + ((l + 1) * sizeof(char)); 64 | list = &tb->hash[lmod(h, tb->size)]; 65 | ts = &luaC_newobj(L, LUA_TSTRING, totalsize, list, 0)->ts; 66 | ts->tsv.len = l; 67 | ts->tsv.hash = h; 68 | ts->tsv.reserved = 0; 69 | memcpy(ts+1, str, l*sizeof(char)); 70 | ((char *)(ts+1))[l] = '\0'; /* ending 0 */ 71 | tb->nuse++; 72 | return ts; 73 | } 74 | 75 | 76 | TString *luaS_newlstr (lua_State *L, const char *str, size_t l) { 77 | GCObject *o; 78 | unsigned int h = cast(unsigned int, l); /* seed */ 79 | size_t step = (l>>5)+1; /* if string is too long, don't hash all its chars */ 80 | size_t l1; 81 | for (l1=l; l1>=step; l1-=step) /* compute hash */ 82 | h = h ^ ((h<<5)+(h>>2)+cast(unsigned char, str[l1-1])); 83 | for (o = G(L)->strt.hash[lmod(h, G(L)->strt.size)]; 84 | o != NULL; 85 | o = gch(o)->next) { 86 | TString *ts = rawgco2ts(o); 87 | if (h == ts->tsv.hash && 88 | ts->tsv.len == l && 89 | (memcmp(str, getstr(ts), l * sizeof(char)) == 0)) { 90 | if (isdead(G(L), o)) /* string is dead (but was not collected yet)? */ 91 | changewhite(o); /* resurrect it */ 92 | return ts; 93 | } 94 | } 95 | return newlstr(L, str, l, h); /* not found; create a new string */ 96 | } 97 | 98 | 99 | TString *luaS_new (lua_State *L, const char *str) { 100 | return luaS_newlstr(L, str, strlen(str)); 101 | } 102 | 103 | 104 | Udata *luaS_newudata (lua_State *L, size_t s, Table *e) { 105 | Udata *u; 106 | if (s > MAX_SIZET - sizeof(Udata)) 107 | luaM_toobig(L); 108 | u = &luaC_newobj(L, LUA_TUSERDATA, sizeof(Udata) + s, NULL, 0)->u; 109 | u->uv.len = s; 110 | u->uv.metatable = NULL; 111 | u->uv.env = e; 112 | return u; 113 | } 114 | 115 | -------------------------------------------------------------------------------- /deps/lua/src/lstring.h: -------------------------------------------------------------------------------- 1 | /* 2 | ** $Id: lstring.h,v 1.46 2010/04/05 16:26:37 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 | ** as all string are internalized, string equality becomes 27 | ** pointer equality 28 | */ 29 | #define eqstr(a,b) ((a) == (b)) 30 | 31 | LUAI_FUNC void luaS_resize (lua_State *L, int newsize); 32 | LUAI_FUNC Udata *luaS_newudata (lua_State *L, size_t s, Table *e); 33 | LUAI_FUNC TString *luaS_newlstr (lua_State *L, const char *str, size_t l); 34 | LUAI_FUNC TString *luaS_new (lua_State *L, const char *str); 35 | 36 | 37 | #endif 38 | -------------------------------------------------------------------------------- /deps/lua/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 | -------------------------------------------------------------------------------- /deps/lua/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 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 | -------------------------------------------------------------------------------- /deps/lua/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 | -------------------------------------------------------------------------------- /deps/lua/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 | -------------------------------------------------------------------------------- /deps/lua/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 | -------------------------------------------------------------------------------- /deps/lua/src/lundump.c: -------------------------------------------------------------------------------- 1 | /* 2 | ** $Id: lundump.c,v 1.71 2011/12/07 10:39:12 lhf 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 void 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) (f) 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 Proto* LoadFunction(LoadState* S); 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 | } 122 | } 123 | n=LoadInt(S); 124 | f->p=luaM_newvector(S->L,n,Proto*); 125 | f->sizep=n; 126 | for (i=0; ip[i]=NULL; 127 | for (i=0; ip[i]=LoadFunction(S); 128 | } 129 | 130 | static void LoadUpvalues(LoadState* S, Proto* f) 131 | { 132 | int i,n; 133 | n=LoadInt(S); 134 | f->upvalues=luaM_newvector(S->L,n,Upvaldesc); 135 | f->sizeupvalues=n; 136 | for (i=0; iupvalues[i].name=NULL; 137 | for (i=0; iupvalues[i].instack=LoadByte(S); 140 | f->upvalues[i].idx=LoadByte(S); 141 | } 142 | } 143 | 144 | static void LoadDebug(LoadState* S, Proto* f) 145 | { 146 | int i,n; 147 | f->source=LoadString(S); 148 | n=LoadInt(S); 149 | f->lineinfo=luaM_newvector(S->L,n,int); 150 | f->sizelineinfo=n; 151 | LoadVector(S,f->lineinfo,n,sizeof(int)); 152 | n=LoadInt(S); 153 | f->locvars=luaM_newvector(S->L,n,LocVar); 154 | f->sizelocvars=n; 155 | for (i=0; ilocvars[i].varname=NULL; 156 | for (i=0; ilocvars[i].varname=LoadString(S); 159 | f->locvars[i].startpc=LoadInt(S); 160 | f->locvars[i].endpc=LoadInt(S); 161 | } 162 | n=LoadInt(S); 163 | for (i=0; iupvalues[i].name=LoadString(S); 164 | } 165 | 166 | static Proto* LoadFunction(LoadState* S) 167 | { 168 | Proto* f=luaF_newproto(S->L); 169 | setptvalue2s(S->L,S->L->top,f); incr_top(S->L); 170 | f->linedefined=LoadInt(S); 171 | f->lastlinedefined=LoadInt(S); 172 | f->numparams=LoadByte(S); 173 | f->is_vararg=LoadByte(S); 174 | f->maxstacksize=LoadByte(S); 175 | LoadCode(S,f); 176 | LoadConstants(S,f); 177 | LoadUpvalues(S,f); 178 | LoadDebug(S,f); 179 | S->L->top--; 180 | return f; 181 | } 182 | 183 | /* the code below must be consistent with the code in luaU_header */ 184 | #define N0 LUAC_HEADERSIZE 185 | #define N1 (sizeof(LUA_SIGNATURE)-sizeof(char)) 186 | #define N2 N1+2 187 | #define N3 N2+6 188 | 189 | static void LoadHeader(LoadState* S) 190 | { 191 | lu_byte h[LUAC_HEADERSIZE]; 192 | lu_byte s[LUAC_HEADERSIZE]; 193 | luaU_header(h); 194 | memcpy(s,h,sizeof(char)); /* first char already read */ 195 | LoadBlock(S,s+sizeof(char),LUAC_HEADERSIZE-sizeof(char)); 196 | if (memcmp(h,s,N0)==0) return; 197 | if (memcmp(h,s,N1)!=0) error(S,"not a"); 198 | if (memcmp(h,s,N2)!=0) error(S,"version mismatch in"); 199 | if (memcmp(h,s,N3)!=0) error(S,"incompatible"); else error(S,"corrupted"); 200 | } 201 | 202 | /* 203 | ** load precompiled chunk 204 | */ 205 | Proto* luaU_undump (lua_State* L, ZIO* Z, Mbuffer* buff, const char* name) 206 | { 207 | LoadState S; 208 | if (*name=='@' || *name=='=') 209 | S.name=name+1; 210 | else if (*name==LUA_SIGNATURE[0]) 211 | S.name="binary string"; 212 | else 213 | S.name=name; 214 | S.L=L; 215 | S.Z=Z; 216 | S.b=buff; 217 | LoadHeader(&S); 218 | return luai_verifycode(L,buff,LoadFunction(&S)); 219 | } 220 | 221 | #define MYINT(s) (s[0]-'0') 222 | #define VERSION MYINT(LUA_VERSION_MAJOR)*16+MYINT(LUA_VERSION_MINOR) 223 | #define FORMAT 0 /* this is the official format */ 224 | 225 | /* 226 | * make header for precompiled chunks 227 | * if you change the code below be sure to update LoadHeader and FORMAT above 228 | * and LUAC_HEADERSIZE in lundump.h 229 | */ 230 | void luaU_header (lu_byte* h) 231 | { 232 | int x=1; 233 | memcpy(h,LUA_SIGNATURE,sizeof(LUA_SIGNATURE)-sizeof(char)); 234 | h+=sizeof(LUA_SIGNATURE)-sizeof(char); 235 | *h++=cast_byte(VERSION); 236 | *h++=cast_byte(FORMAT); 237 | *h++=cast_byte(*(char*)&x); /* endianness */ 238 | *h++=cast_byte(sizeof(int)); 239 | *h++=cast_byte(sizeof(size_t)); 240 | *h++=cast_byte(sizeof(Instruction)); 241 | *h++=cast_byte(sizeof(lua_Number)); 242 | *h++=cast_byte(((lua_Number)0.5)==0); /* is lua_Number integral? */ 243 | memcpy(h,LUAC_TAIL,sizeof(LUAC_TAIL)-sizeof(char)); 244 | } 245 | -------------------------------------------------------------------------------- /deps/lua/src/lundump.h: -------------------------------------------------------------------------------- 1 | /* 2 | ** $Id: lundump.h,v 1.44 2011/05/06 13:35:17 lhf 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 Proto* 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 | -------------------------------------------------------------------------------- /deps/lua/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 | -------------------------------------------------------------------------------- /deps/lua/src/lzio.c: -------------------------------------------------------------------------------- 1 | /* 2 | ** $Id: lzio.c,v 1.34 2011/07/15 12:35:32 roberto Exp $ 3 | ** a generic input stream interface 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 | -------------------------------------------------------------------------------- /deps/lua/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 | -------------------------------------------------------------------------------- /deps/lua/src/strbuf.c: -------------------------------------------------------------------------------- 1 | /* strbuf - string buffer routines 2 | * 3 | * Copyright (c) 2010-2011 Mark Pulford 4 | * 5 | * Permission is hereby granted, free of charge, to any person obtaining 6 | * a copy of this software and associated documentation files (the 7 | * "Software"), to deal in the Software without restriction, including 8 | * without limitation the rights to use, copy, modify, merge, publish, 9 | * distribute, sublicense, and/or sell copies of the Software, and to 10 | * permit persons to whom the Software is furnished to do so, subject to 11 | * the following conditions: 12 | * 13 | * The above copyright notice and this permission notice shall be 14 | * included in all copies or substantial portions of the Software. 15 | * 16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 17 | * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 18 | * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. 19 | * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY 20 | * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, 21 | * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE 22 | * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 23 | */ 24 | 25 | #include 26 | #include 27 | #include 28 | #include 29 | 30 | #include "strbuf.h" 31 | 32 | void die(const char *fmt, ...) 33 | { 34 | va_list arg; 35 | 36 | va_start(arg, fmt); 37 | vfprintf(stderr, fmt, arg); 38 | va_end(arg); 39 | fprintf(stderr, "\n"); 40 | 41 | exit(-1); 42 | } 43 | 44 | void strbuf_init(strbuf_t *s, int len) 45 | { 46 | int size; 47 | 48 | if (len <= 0) 49 | size = STRBUF_DEFAULT_SIZE; 50 | else 51 | size = len + 1; /* \0 terminator */ 52 | 53 | s->buf = NULL; 54 | s->size = size; 55 | s->length = 0; 56 | s->increment = STRBUF_DEFAULT_INCREMENT; 57 | s->dynamic = 0; 58 | s->reallocs = 0; 59 | s->debug = 0; 60 | 61 | s->buf = malloc(size); 62 | if (!s->buf) 63 | die("Out of memory"); 64 | 65 | strbuf_ensure_null(s); 66 | } 67 | 68 | strbuf_t *strbuf_new(int len) 69 | { 70 | strbuf_t *s; 71 | 72 | s = malloc(sizeof(strbuf_t)); 73 | if (!s) 74 | die("Out of memory"); 75 | 76 | strbuf_init(s, len); 77 | 78 | /* Dynamic strbuf allocation / deallocation */ 79 | s->dynamic = 1; 80 | 81 | return s; 82 | } 83 | 84 | void strbuf_set_increment(strbuf_t *s, int increment) 85 | { 86 | /* Increment > 0: Linear buffer growth rate 87 | * Increment < -1: Exponential buffer growth rate */ 88 | if (increment == 0 || increment == -1) 89 | die("BUG: Invalid string increment"); 90 | 91 | s->increment = increment; 92 | } 93 | 94 | static inline void debug_stats(strbuf_t *s) 95 | { 96 | if (s->debug) { 97 | fprintf(stderr, "strbuf(%lx) reallocs: %d, length: %d, size: %d\n", 98 | (long)s, s->reallocs, s->length, s->size); 99 | } 100 | } 101 | 102 | /* If strbuf_t has not been dynamically allocated, strbuf_free() can 103 | * be called any number of times strbuf_init() */ 104 | void strbuf_free(strbuf_t *s) 105 | { 106 | debug_stats(s); 107 | 108 | if (s->buf) { 109 | free(s->buf); 110 | s->buf = NULL; 111 | } 112 | if (s->dynamic) 113 | free(s); 114 | } 115 | 116 | char *strbuf_free_to_string(strbuf_t *s, int *len) 117 | { 118 | char *buf; 119 | 120 | debug_stats(s); 121 | 122 | strbuf_ensure_null(s); 123 | 124 | buf = s->buf; 125 | if (len) 126 | *len = s->length; 127 | 128 | if (s->dynamic) 129 | free(s); 130 | 131 | return buf; 132 | } 133 | 134 | static int calculate_new_size(strbuf_t *s, int len) 135 | { 136 | int reqsize, newsize; 137 | 138 | if (len <= 0) 139 | die("BUG: Invalid strbuf length requested"); 140 | 141 | /* Ensure there is room for optional NULL termination */ 142 | reqsize = len + 1; 143 | 144 | /* If the user has requested to shrink the buffer, do it exactly */ 145 | if (s->size > reqsize) 146 | return reqsize; 147 | 148 | newsize = s->size; 149 | if (s->increment < 0) { 150 | /* Exponential sizing */ 151 | while (newsize < reqsize) 152 | newsize *= -s->increment; 153 | } else { 154 | /* Linear sizing */ 155 | newsize = ((newsize + s->increment - 1) / s->increment) * s->increment; 156 | } 157 | 158 | return newsize; 159 | } 160 | 161 | 162 | /* Ensure strbuf can handle a string length bytes long (ignoring NULL 163 | * optional termination). */ 164 | void strbuf_resize(strbuf_t *s, int len) 165 | { 166 | int newsize; 167 | 168 | newsize = calculate_new_size(s, len); 169 | 170 | if (s->debug > 1) { 171 | fprintf(stderr, "strbuf(%lx) resize: %d => %d\n", 172 | (long)s, s->size, newsize); 173 | } 174 | 175 | s->size = newsize; 176 | s->buf = realloc(s->buf, s->size); 177 | if (!s->buf) 178 | die("Out of memory"); 179 | s->reallocs++; 180 | } 181 | 182 | void strbuf_append_string(strbuf_t *s, const char *str) 183 | { 184 | int space, i; 185 | 186 | space = strbuf_empty_length(s); 187 | 188 | for (i = 0; str[i]; i++) { 189 | if (space < 1) { 190 | strbuf_resize(s, s->length + 1); 191 | space = strbuf_empty_length(s); 192 | } 193 | 194 | s->buf[s->length] = str[i]; 195 | s->length++; 196 | space--; 197 | } 198 | } 199 | 200 | /* strbuf_append_fmt() should only be used when an upper bound 201 | * is known for the output string. */ 202 | void strbuf_append_fmt(strbuf_t *s, int len, const char *fmt, ...) 203 | { 204 | va_list arg; 205 | int fmt_len; 206 | 207 | strbuf_ensure_empty_length(s, len); 208 | 209 | va_start(arg, fmt); 210 | fmt_len = vsnprintf(s->buf + s->length, len, fmt, arg); 211 | va_end(arg); 212 | 213 | if (fmt_len < 0) 214 | die("BUG: Unable to convert number"); /* This should never happen.. */ 215 | 216 | s->length += fmt_len; 217 | } 218 | 219 | /* strbuf_append_fmt_retry() can be used when the there is no known 220 | * upper bound for the output string. */ 221 | void strbuf_append_fmt_retry(strbuf_t *s, const char *fmt, ...) 222 | { 223 | va_list arg; 224 | int fmt_len, try; 225 | int empty_len; 226 | 227 | /* If the first attempt to append fails, resize the buffer appropriately 228 | * and try again */ 229 | for (try = 0; ; try++) { 230 | va_start(arg, fmt); 231 | /* Append the new formatted string */ 232 | /* fmt_len is the length of the string required, excluding the 233 | * trailing NULL */ 234 | empty_len = strbuf_empty_length(s); 235 | /* Add 1 since there is also space to store the terminating NULL. */ 236 | fmt_len = vsnprintf(s->buf + s->length, empty_len + 1, fmt, arg); 237 | va_end(arg); 238 | 239 | if (fmt_len <= empty_len) 240 | break; /* SUCCESS */ 241 | if (try > 0) 242 | die("BUG: length of formatted string changed"); 243 | 244 | strbuf_resize(s, s->length + fmt_len); 245 | } 246 | 247 | s->length += fmt_len; 248 | } 249 | 250 | /* vi:ai et sw=4 ts=4: 251 | */ 252 | -------------------------------------------------------------------------------- /deps/lua/src/strbuf.h: -------------------------------------------------------------------------------- 1 | /* strbuf - String buffer routines 2 | * 3 | * Copyright (c) 2010-2011 Mark Pulford 4 | * 5 | * Permission is hereby granted, free of charge, to any person obtaining 6 | * a copy of this software and associated documentation files (the 7 | * "Software"), to deal in the Software without restriction, including 8 | * without limitation the rights to use, copy, modify, merge, publish, 9 | * distribute, sublicense, and/or sell copies of the Software, and to 10 | * permit persons to whom the Software is furnished to do so, subject to 11 | * the following conditions: 12 | * 13 | * The above copyright notice and this permission notice shall be 14 | * included in all copies or substantial portions of the Software. 15 | * 16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 17 | * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 18 | * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. 19 | * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY 20 | * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, 21 | * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE 22 | * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 23 | */ 24 | 25 | #include 26 | #include 27 | 28 | /* Size: Total bytes allocated to *buf 29 | * Length: String length, excluding optional NULL terminator. 30 | * Increment: Allocation increments when resizing the string buffer. 31 | * Dynamic: True if created via strbuf_new() 32 | */ 33 | 34 | typedef struct { 35 | char *buf; 36 | int size; 37 | int length; 38 | int increment; 39 | int dynamic; 40 | int reallocs; 41 | int debug; 42 | } strbuf_t; 43 | 44 | #ifndef STRBUF_DEFAULT_SIZE 45 | #define STRBUF_DEFAULT_SIZE 1023 46 | #endif 47 | #ifndef STRBUF_DEFAULT_INCREMENT 48 | #define STRBUF_DEFAULT_INCREMENT -2 49 | #endif 50 | 51 | /* Initialise */ 52 | extern strbuf_t *strbuf_new(int len); 53 | extern void strbuf_init(strbuf_t *s, int len); 54 | extern void strbuf_set_increment(strbuf_t *s, int increment); 55 | 56 | /* Release */ 57 | extern void strbuf_free(strbuf_t *s); 58 | extern char *strbuf_free_to_string(strbuf_t *s, int *len); 59 | 60 | /* Management */ 61 | extern void strbuf_resize(strbuf_t *s, int len); 62 | static int strbuf_empty_length(strbuf_t *s); 63 | static int strbuf_length(strbuf_t *s); 64 | static char *strbuf_string(strbuf_t *s, int *len); 65 | static void strbuf_ensure_empty_length(strbuf_t *s, int len); 66 | 67 | /* Update */ 68 | extern void strbuf_append_fmt(strbuf_t *s, int len, const char *fmt, ...); 69 | extern void strbuf_append_fmt_retry(strbuf_t *s, const char *format, ...); 70 | static void strbuf_append_mem(strbuf_t *s, const char *c, int len); 71 | extern void strbuf_append_string(strbuf_t *s, const char *str); 72 | static void strbuf_append_char(strbuf_t *s, const char c); 73 | static void strbuf_ensure_null(strbuf_t *s); 74 | 75 | /* Reset string for before use */ 76 | static inline void strbuf_reset(strbuf_t *s) 77 | { 78 | s->length = 0; 79 | } 80 | 81 | static inline int strbuf_allocated(strbuf_t *s) 82 | { 83 | return s->buf != NULL; 84 | } 85 | 86 | /* Return bytes remaining in the string buffer 87 | * Ensure there is space for a NULL terminator. */ 88 | static inline int strbuf_empty_length(strbuf_t *s) 89 | { 90 | return s->size - s->length - 1; 91 | } 92 | 93 | static inline void strbuf_ensure_empty_length(strbuf_t *s, int len) 94 | { 95 | if (len > strbuf_empty_length(s)) 96 | strbuf_resize(s, s->length + len); 97 | } 98 | 99 | static inline int strbuf_length(strbuf_t *s) 100 | { 101 | return s->length; 102 | } 103 | 104 | static inline void strbuf_append_char(strbuf_t *s, const char c) 105 | { 106 | strbuf_ensure_empty_length(s, 1); 107 | s->buf[s->length++] = c; 108 | } 109 | 110 | static inline void strbuf_append_char_unsafe(strbuf_t *s, const char c) 111 | { 112 | s->buf[s->length++] = c; 113 | } 114 | 115 | static inline void strbuf_append_mem(strbuf_t *s, const char *c, int len) 116 | { 117 | strbuf_ensure_empty_length(s, len); 118 | memcpy(s->buf + s->length, c, len); 119 | s->length += len; 120 | } 121 | 122 | static inline void strbuf_append_mem_unsafe(strbuf_t *s, const char *c, int len) 123 | { 124 | memcpy(s->buf + s->length, c, len); 125 | s->length += len; 126 | } 127 | 128 | static inline void strbuf_ensure_null(strbuf_t *s) 129 | { 130 | s->buf[s->length] = 0; 131 | } 132 | 133 | static inline char *strbuf_string(strbuf_t *s, int *len) 134 | { 135 | if (len) 136 | *len = s->length; 137 | 138 | return s->buf; 139 | } 140 | 141 | /* vi:ai et sw=4 ts=4: 142 | */ 143 | -------------------------------------------------------------------------------- /scripts/example.lua: -------------------------------------------------------------------------------- 1 | function format_value(item) 2 | -- return hash to string 3 | if item.type == "hash" or item.type == "zset" then 4 | local hash_str = "{" 5 | for k, v in pairs(item.value) do 6 | hash_str = hash_str ..k.."=>"..v.." ," 7 | end 8 | return string.sub(hash_str, 0, -2) .."}" 9 | 10 | -- return list to string 11 | elseif item.type == "set" or item.type == "list" then 12 | return "["..table.concat(item.value, ", ").."]" 13 | else 14 | return item.value 15 | end 16 | end 17 | 18 | function handle(item) 19 | local value = format_value(item) 20 | -- env = {version = $version, db_num = $db_num} 21 | local item_str = string.format("{ select_db:%d, type: %s, expire_time: %s, key: %s, value: %s}", 22 | env.db_num, item.type, item.expire_time, item.key, value) 23 | print(item_str) 24 | end 25 | -------------------------------------------------------------------------------- /scripts/json_exapmle.lua: -------------------------------------------------------------------------------- 1 | local cjson = require "cjson" 2 | 3 | function handle(item) 4 | print(cjson.encode(item)) 5 | end 6 | -------------------------------------------------------------------------------- /snapshot/rdb-tools.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/git-hulk/rdbtools/08571e5cd22cc9268bdcd9319b21a2d7ea21a0e0/snapshot/rdb-tools.png -------------------------------------------------------------------------------- /snapshot/rdbtools-to-json.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/git-hulk/rdbtools/08571e5cd22cc9268bdcd9319b21a2d7ea21a0e0/snapshot/rdbtools-to-json.png -------------------------------------------------------------------------------- /src/Makefile: -------------------------------------------------------------------------------- 1 | CC = gcc 2 | CFLAGS = -g -Wall 3 | CINCLUDES = -I../deps/lua/src 4 | CLIBS = ../deps/lua/src/liblua.a -lm -ldl 5 | 6 | PROG = rdbtools 7 | 8 | INSTALL=/usr/bin/install 9 | INSTALLDIR=/usr/local 10 | BINDIR=$(INSTALLDIR)/bin 11 | 12 | all: deps $(PROG) 13 | .PHONY: all 14 | 15 | OBJS = lzf_d.o rdb.o util.o ziplist.o intset.o zipmap.o endian.o crc64.o log.o script.o main.o 16 | 17 | rdbtools: $(OBJS) 18 | $(CC) $(CFLAGS) $(CINCLUDES) -o $(PROG) $(OBJS) $(CLIBS) 19 | 20 | crc64.o: crc64.c crc64.h 21 | log.o: log.c log.h 22 | endian.o: endian.c endian.h 23 | intset.o: intset.c intset.h endian.h 24 | lzf_d.o: lzf_d.c lzfP.h 25 | main.o: main.c rdb.h script.h ../deps/lua/src/lua.h \ 26 | ../deps/lua/src/luaconf.h ../deps/lua/src/lauxlib.h \ 27 | ../deps/lua/src/lualib.h 28 | rdb.o: rdb.c util.h log.h lzf.h intset.h ziplist.h script.h \ 29 | ../deps/lua/src/lua.h ../deps/lua/src/luaconf.h \ 30 | ../deps/lua/src/lauxlib.h ../deps/lua/src/lualib.h zipmap.h crc64.h \ 31 | endian.h 32 | script.o: script.c script.h log.h ../deps/lua/src/lua.h \ 33 | ../deps/lua/src/luaconf.h ../deps/lua/src/lauxlib.h \ 34 | ../deps/lua/src/lualib.h 35 | util.o: util.c util.h 36 | ziplist.o: ziplist.c ziplist.h script.h ../deps/lua/src/lua.h \ 37 | ../deps/lua/src/luaconf.h ../deps/lua/src/lauxlib.h \ 38 | ../deps/lua/src/lualib.h util.h endian.h log.h 39 | zipmap.o: zipmap.c zipmap.h script.h ../deps/lua/src/lua.h \ 40 | ../deps/lua/src/luaconf.h ../deps/lua/src/lauxlib.h \ 41 | ../deps/lua/src/lualib.h endian.h log.h 42 | 43 | %.o: %.c 44 | $(CC) $(CFLAGS) $(CINCLUDES) -c $< 45 | 46 | uname_S := $(shell sh -c 'uname -s 2>/dev/null || echo not') 47 | ifeq ($(uname_S),SunOS) 48 | os_Platform := solaris 49 | else 50 | ifeq ($(uname_S),Darwin) 51 | os_Platform := macosx 52 | else 53 | ifeq ($(uname_S),AIX) 54 | os_Platform := aix 55 | else 56 | os_Platform := linux 57 | endif 58 | endif 59 | endif 60 | 61 | deps: 62 | @cd ../deps/lua/src && make $(os_Platform) 63 | 64 | clean: 65 | - rm -rf *.o $(PROG) 66 | @cd ../deps/lua/src/ && make clean 67 | 68 | install: 69 | mkdir -p $(BINDIR) 70 | $(INSTALL) $(PROG) $(BINDIR) 71 | -------------------------------------------------------------------------------- /src/crc64.h: -------------------------------------------------------------------------------- 1 | #ifndef CRC64_H 2 | #define CRC64_H 3 | 4 | #include 5 | 6 | uint64_t crc64(uint64_t crc, const unsigned char *s, uint64_t l); 7 | 8 | #endif 9 | -------------------------------------------------------------------------------- /src/endian.c: -------------------------------------------------------------------------------- 1 | /* Toggle the 16 bit unsigned integer pointed by *p from little endian to 2 | * big endian */ 3 | void memrev16(void *p) { 4 | unsigned char *x = p, t; 5 | 6 | t = x[0]; 7 | x[0] = x[1]; 8 | x[1] = t; 9 | } 10 | 11 | /* Toggle the 32 bit unsigned integer pointed by *p from little endian to 12 | * big endian */ 13 | void memrev32(void *p) { 14 | unsigned char *x = p, t; 15 | 16 | t = x[0]; 17 | x[0] = x[3]; 18 | x[3] = t; 19 | t = x[1]; 20 | x[1] = x[2]; 21 | x[2] = t; 22 | } 23 | 24 | /* Toggle the 64 bit unsigned integer pointed by *p from little endian to 25 | * big endian */ 26 | void memrev64(void *p) { 27 | unsigned char *x = p, t; 28 | 29 | t = x[0]; 30 | x[0] = x[7]; 31 | x[7] = t; 32 | t = x[1]; 33 | x[1] = x[6]; 34 | x[6] = t; 35 | t = x[2]; 36 | x[2] = x[5]; 37 | x[5] = t; 38 | t = x[3]; 39 | x[3] = x[4]; 40 | x[4] = t; 41 | } 42 | 43 | #ifdef TESTMAIN 44 | #include 45 | 46 | int main(void) { 47 | char buf[32]; 48 | 49 | sprintf(buf,"ciaoroma"); 50 | memrev16(buf); 51 | printf("%s\n", buf); 52 | 53 | sprintf(buf,"ciaoroma"); 54 | memrev32(buf); 55 | printf("%s\n", buf); 56 | 57 | sprintf(buf,"ciaoroma"); 58 | memrev64(buf); 59 | printf("%s\n", buf); 60 | 61 | return 0; 62 | } 63 | #endif 64 | -------------------------------------------------------------------------------- /src/endian.h: -------------------------------------------------------------------------------- 1 | #ifndef __ENDIAN_H 2 | #define __ENDIAN_H 3 | 4 | void memrev16(void *p); 5 | void memrev32(void *p); 6 | void memrev64(void *p); 7 | 8 | /* variants of the function doing the actual convertion only if the target 9 | * host is big endian */ 10 | #if (BYTE_ORDER == LITTLE_ENDIAN) 11 | #define memrev16ifbe(p) 12 | #define memrev32ifbe(p) 13 | #define memrev64ifbe(p) 14 | #else 15 | #define memrev16ifbe(p) memrev16(p) 16 | #define memrev32ifbe(p) memrev32(p) 17 | #define memrev64ifbe(p) memrev64(p) 18 | #endif 19 | 20 | #endif 21 | -------------------------------------------------------------------------------- /src/intset.c: -------------------------------------------------------------------------------- 1 | #include "intset.h" 2 | #include 3 | #include 4 | 5 | #include "endian.h" 6 | 7 | uint8_t 8 | intset_get(intset *is, int pos, int64_t *v) 9 | { 10 | int64_t v64; 11 | int32_t v32; 12 | int16_t v16; 13 | 14 | if(pos >= is->length) return 0; 15 | 16 | if (is->encoding == sizeof(int64_t)) { 17 | memcpy(&v64, (int64_t*)is->contents + pos, sizeof(int64_t)); 18 | memrev64ifbe(&v64); 19 | *v = v64; 20 | } else if (is->encoding == sizeof(int32_t)) { 21 | memcpy(&v32, (int32_t*)is->contents + pos, sizeof(int32_t)); 22 | memrev32ifbe(&v32); 23 | *v = v32; 24 | } else { 25 | memcpy(&v16, (int16_t*)is->contents + pos, sizeof(int16_t)); 26 | memrev16ifbe(&v16); 27 | *v = v16; 28 | } 29 | return 1; 30 | } 31 | 32 | void 33 | intset_dump(intset *is) 34 | { 35 | printf("encoding: %d\n", is->encoding); 36 | printf("length: %d\n", is->length); 37 | 38 | int i; 39 | int64_t v; 40 | printf("element { "); 41 | for (i = 0; i < is->length; i++) { 42 | intset_get(is, i, &v); 43 | printf("%lld\t", v); 44 | } 45 | printf("}\n"); 46 | } 47 | 48 | -------------------------------------------------------------------------------- /src/intset.h: -------------------------------------------------------------------------------- 1 | #ifndef _INTSET_H_ 2 | #define _INTSET_H_ 3 | #include 4 | typedef struct { 5 | uint32_t encoding; /* int16, int32 or int64 */ 6 | uint32_t length; 7 | uint8_t contents[0]; 8 | } intset; 9 | 10 | void intset_dump(intset *is); 11 | uint8_t intset_get(intset *is, int pos, int64_t *v); 12 | #endif 13 | -------------------------------------------------------------------------------- /src/log.c: -------------------------------------------------------------------------------- 1 | /* 2 | * *************************************************************** 3 | * author by @git-hulk at 2015-07-18 4 | * Copyright (C) 2015 Inc. 5 | * ************************************************************* 6 | * Licensed under the Apache License, Version 2.0 (the "License"); 7 | * you may not use this file except in compliance with the License. 8 | * You may obtain a copy of the License at 9 | * 10 | * http://www.apache.org/licenses/LICENSE-2.0 11 | * 12 | * Unless required by applicable law or agreed to in writing, software 13 | * distributed under the License is distributed on an "AS IS" BASIS, 14 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 15 | * See the License for the specific language governing permissions and 16 | * limitations under the License. 17 | */ 18 | 19 | #include 20 | #include 21 | #include 22 | #include 23 | #include 24 | #include "log.h" 25 | 26 | static char *log_file = NULL; 27 | static enum LEVEL log_level = INFO; 28 | 29 | void 30 | set_log_level(enum LEVEL level) { 31 | log_level = level; 32 | } 33 | 34 | void 35 | set_log_file(char *filename) 36 | { 37 | log_file = filename; 38 | } 39 | 40 | void 41 | logger(enum LEVEL loglevel,char *fmt, ...) 42 | { 43 | FILE *fp; 44 | va_list ap; 45 | time_t now; 46 | char buf[4096]; 47 | char t_buf[64]; 48 | char *msg = NULL; 49 | const char *color = ""; 50 | 51 | if(loglevel < log_level) { 52 | return; 53 | } 54 | 55 | va_start(ap, fmt); 56 | vsnprintf(buf, sizeof(buf), fmt, ap); 57 | va_end(ap); 58 | switch(loglevel) { 59 | case DEBUG: msg = "DEBUG"; break; 60 | case INFO: msg = "INFO"; color = C_YELLOW ; break; 61 | case WARN: msg = "WARN"; color = C_PURPLE; break; 62 | case ERROR: msg = "ERROR"; color = C_RED; break; 63 | } 64 | 65 | now = time(NULL); 66 | strftime(t_buf,64,"%Y-%m-%d %H:%M:%S",localtime(&now)); 67 | fp = (log_file == NULL) ? stdout : fopen(log_file,"a"); 68 | if(log_file) { 69 | fprintf(fp, "[%s] [%s] %s\n", t_buf, msg, buf); 70 | fclose(fp); 71 | } else { 72 | fprintf(fp, "%s[%s] [%s] %s"C_NONE"\n", color, t_buf, msg, buf); 73 | } 74 | 75 | if(loglevel >= ERROR) { 76 | exit(1); 77 | } 78 | } 79 | -------------------------------------------------------------------------------- /src/log.h: -------------------------------------------------------------------------------- 1 | /* 2 | * *************************************************************** 3 | * util.h 4 | * author by @git-hulk at 2015-07-18 5 | * Copyright (C) 2015 Inc. 6 | * ************************************************************* 7 | * Licensed under the Apache License, Version 2.0 (the "License"); 8 | * you may not use this file except in compliance with the License. 9 | * You may obtain a copy of the License at 10 | * 11 | * http://www.apache.org/licenses/LICENSE-2.0 12 | * 13 | * Unless required by applicable law or agreed to in writing, software 14 | * distributed under the License is distributed on an "AS IS" BASIS, 15 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 16 | * See the License for the specific language governing permissions and 17 | * limitations under the License. 18 | */ 19 | 20 | #ifndef _LOG_H_ 21 | #define _LOG_H_ 22 | 23 | #define C_RED "\033[31m" 24 | #define C_GREEN "\033[32m" 25 | #define C_YELLOW "\033[33m" 26 | #define C_PURPLE "\033[35m" 27 | #define C_NONE "\033[0m" 28 | 29 | enum LEVEL { 30 | DEBUG = 1, 31 | INFO, 32 | WARN, 33 | ERROR 34 | }; 35 | 36 | void logger(enum LEVEL loglevel,char *fmt, ...); 37 | void set_log_file(char *filename); 38 | void set_log_level(enum LEVEL level); 39 | #endif 40 | -------------------------------------------------------------------------------- /src/lzf.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2000-2008 Marc Alexander Lehmann 3 | * 4 | * Redistribution and use in source and binary forms, with or without modifica- 5 | * tion, are permitted provided that the following conditions are met: 6 | * 7 | * 1. Redistributions of source code must retain the above copyright notice, 8 | * this list of conditions and the following disclaimer. 9 | * 10 | * 2. Redistributions in binary form must reproduce the above copyright 11 | * notice, this list of conditions and the following disclaimer in the 12 | * documentation and/or other materials provided with the distribution. 13 | * 14 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED 15 | * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MER- 16 | * CHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO 17 | * EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPE- 18 | * CIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, 19 | * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; 20 | * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, 21 | * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTH- 22 | * ERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED 23 | * OF THE POSSIBILITY OF SUCH DAMAGE. 24 | * 25 | * Alternatively, the contents of this file may be used under the terms of 26 | * the GNU General Public License ("GPL") version 2 or any later version, 27 | * in which case the provisions of the GPL are applicable instead of 28 | * the above. If you wish to allow the use of your version of this file 29 | * only under the terms of the GPL and not to allow others to use your 30 | * version of this file under the BSD license, indicate your decision 31 | * by deleting the provisions above and replace them with the notice 32 | * and other provisions required by the GPL. If you do not delete the 33 | * provisions above, a recipient may use your version of this file under 34 | * either the BSD or the GPL. 35 | */ 36 | 37 | #ifndef LZF_H 38 | #define LZF_H 39 | 40 | /*********************************************************************** 41 | ** 42 | ** lzf -- an extremely fast/free compression/decompression-method 43 | ** http://liblzf.plan9.de/ 44 | ** 45 | ** This algorithm is believed to be patent-free. 46 | ** 47 | ***********************************************************************/ 48 | 49 | #define LZF_VERSION 0x0105 /* 1.5, API version */ 50 | 51 | /* 52 | * Decompress data compressed with some version of the lzf_compress 53 | * function and stored at location in_data and length in_len. The result 54 | * will be stored at out_data up to a maximum of out_len characters. 55 | * 56 | * If the output buffer is not large enough to hold the decompressed 57 | * data, a 0 is returned and errno is set to E2BIG. Otherwise the number 58 | * of decompressed bytes (i.e. the original length of the data) is 59 | * returned. 60 | * 61 | * If an error in the compressed data is detected, a zero is returned and 62 | * errno is set to EINVAL. 63 | * 64 | * This function is very fast, about as fast as a copying loop. 65 | */ 66 | unsigned int 67 | lzf_decompress (const void *const in_data, unsigned int in_len, 68 | void *out_data, unsigned int out_len); 69 | 70 | #endif 71 | 72 | -------------------------------------------------------------------------------- /src/lzfP.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2000-2007 Marc Alexander Lehmann 3 | * 4 | * Redistribution and use in source and binary forms, with or without modifica- 5 | * tion, are permitted provided that the following conditions are met: 6 | * 7 | * 1. Redistributions of source code must retain the above copyright notice, 8 | * this list of conditions and the following disclaimer. 9 | * 10 | * 2. Redistributions in binary form must reproduce the above copyright 11 | * notice, this list of conditions and the following disclaimer in the 12 | * documentation and/or other materials provided with the distribution. 13 | * 14 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED 15 | * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MER- 16 | * CHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO 17 | * EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPE- 18 | * CIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, 19 | * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; 20 | * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, 21 | * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTH- 22 | * ERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED 23 | * OF THE POSSIBILITY OF SUCH DAMAGE. 24 | * 25 | * Alternatively, the contents of this file may be used under the terms of 26 | * the GNU General Public License ("GPL") version 2 or any later version, 27 | * in which case the provisions of the GPL are applicable instead of 28 | * the above. If you wish to allow the use of your version of this file 29 | * only under the terms of the GPL and not to allow others to use your 30 | * version of this file under the BSD license, indicate your decision 31 | * by deleting the provisions above and replace them with the notice 32 | * and other provisions required by the GPL. If you do not delete the 33 | * provisions above, a recipient may use your version of this file under 34 | * either the BSD or the GPL. 35 | */ 36 | 37 | #ifndef LZFP_h 38 | #define LZFP_h 39 | 40 | #define STANDALONE 1 /* at the moment, this is ok. */ 41 | 42 | #ifndef STANDALONE 43 | # include "lzf.h" 44 | #endif 45 | 46 | /* 47 | * Size of hashtable is (1 << HLOG) * sizeof (char *) 48 | * decompression is independent of the hash table size 49 | * the difference between 15 and 14 is very small 50 | * for small blocks (and 14 is usually a bit faster). 51 | * For a low-memory/faster configuration, use HLOG == 13; 52 | * For best compression, use 15 or 16 (or more, up to 23). 53 | */ 54 | #ifndef HLOG 55 | # define HLOG 16 56 | #endif 57 | 58 | /* 59 | * Sacrifice very little compression quality in favour of compression speed. 60 | * This gives almost the same compression as the default code, and is 61 | * (very roughly) 15% faster. This is the preferred mode of operation. 62 | */ 63 | #ifndef VERY_FAST 64 | # define VERY_FAST 1 65 | #endif 66 | 67 | /* 68 | * Sacrifice some more compression quality in favour of compression speed. 69 | * (roughly 1-2% worse compression for large blocks and 70 | * 9-10% for small, redundant, blocks and >>20% better speed in both cases) 71 | * In short: when in need for speed, enable this for binary data, 72 | * possibly disable this for text data. 73 | */ 74 | #ifndef ULTRA_FAST 75 | # define ULTRA_FAST 0 76 | #endif 77 | 78 | /* 79 | * Unconditionally aligning does not cost very much, so do it if unsure 80 | */ 81 | #ifndef STRICT_ALIGN 82 | # define STRICT_ALIGN !(defined(__i386) || defined (__amd64)) 83 | #endif 84 | 85 | /* 86 | * You may choose to pre-set the hash table (might be faster on some 87 | * modern cpus and large (>>64k) blocks, and also makes compression 88 | * deterministic/repeatable when the configuration otherwise is the same). 89 | */ 90 | #ifndef INIT_HTAB 91 | # define INIT_HTAB 0 92 | #endif 93 | 94 | /* 95 | * Avoid assigning values to errno variable? for some embedding purposes 96 | * (linux kernel for example), this is necessary. NOTE: this breaks 97 | * the documentation in lzf.h. 98 | */ 99 | #ifndef AVOID_ERRNO 100 | # define AVOID_ERRNO 0 101 | #endif 102 | 103 | /* 104 | * Whether to pass the LZF_STATE variable as argument, or allocate it 105 | * on the stack. For small-stack environments, define this to 1. 106 | * NOTE: this breaks the prototype in lzf.h. 107 | */ 108 | #ifndef LZF_STATE_ARG 109 | # define LZF_STATE_ARG 0 110 | #endif 111 | 112 | /* 113 | * Whether to add extra checks for input validity in lzf_decompress 114 | * and return EINVAL if the input stream has been corrupted. This 115 | * only shields against overflowing the input buffer and will not 116 | * detect most corrupted streams. 117 | * This check is not normally noticeable on modern hardware 118 | * (<1% slowdown), but might slow down older cpus considerably. 119 | */ 120 | #ifndef CHECK_INPUT 121 | # define CHECK_INPUT 1 122 | #endif 123 | 124 | /*****************************************************************************/ 125 | /* nothing should be changed below */ 126 | 127 | typedef unsigned char u8; 128 | 129 | typedef const u8 *LZF_STATE[1 << (HLOG)]; 130 | 131 | #if !STRICT_ALIGN 132 | /* for unaligned accesses we need a 16 bit datatype. */ 133 | # include 134 | # if USHRT_MAX == 65535 135 | typedef unsigned short u16; 136 | # elif UINT_MAX == 65535 137 | typedef unsigned int u16; 138 | # else 139 | # undef STRICT_ALIGN 140 | # define STRICT_ALIGN 1 141 | # endif 142 | #endif 143 | 144 | #if ULTRA_FAST 145 | # if defined(VERY_FAST) 146 | # undef VERY_FAST 147 | # endif 148 | #endif 149 | 150 | #if INIT_HTAB 151 | # ifdef __cplusplus 152 | # include 153 | # else 154 | # include 155 | # endif 156 | #endif 157 | 158 | #endif 159 | 160 | -------------------------------------------------------------------------------- /src/lzf_d.c: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2000-2007 Marc Alexander Lehmann 3 | * 4 | * Redistribution and use in source and binary forms, with or without modifica- 5 | * tion, are permitted provided that the following conditions are met: 6 | * 7 | * 1. Redistributions of source code must retain the above copyright notice, 8 | * this list of conditions and the following disclaimer. 9 | * 10 | * 2. Redistributions in binary form must reproduce the above copyright 11 | * notice, this list of conditions and the following disclaimer in the 12 | * documentation and/or other materials provided with the distribution. 13 | * 14 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED 15 | * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MER- 16 | * CHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO 17 | * EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPE- 18 | * CIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, 19 | * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; 20 | * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, 21 | * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTH- 22 | * ERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED 23 | * OF THE POSSIBILITY OF SUCH DAMAGE. 24 | * 25 | * Alternatively, the contents of this file may be used under the terms of 26 | * the GNU General Public License ("GPL") version 2 or any later version, 27 | * in which case the provisions of the GPL are applicable instead of 28 | * the above. If you wish to allow the use of your version of this file 29 | * only under the terms of the GPL and not to allow others to use your 30 | * version of this file under the BSD license, indicate your decision 31 | * by deleting the provisions above and replace them with the notice 32 | * and other provisions required by the GPL. If you do not delete the 33 | * provisions above, a recipient may use your version of this file under 34 | * either the BSD or the GPL. 35 | */ 36 | 37 | #include "lzfP.h" 38 | 39 | #if AVOID_ERRNO 40 | # define SET_ERRNO(n) 41 | #else 42 | # include 43 | # define SET_ERRNO(n) errno = (n) 44 | #endif 45 | 46 | /* 47 | #if (__i386 || __amd64) && __GNUC__ >= 3 48 | # define lzf_movsb(dst, src, len) \ 49 | asm ("rep movsb" \ 50 | : "=D" (dst), "=S" (src), "=c" (len) \ 51 | : "0" (dst), "1" (src), "2" (len)); 52 | #endif 53 | */ 54 | 55 | unsigned int 56 | lzf_decompress (const void *const in_data, unsigned int in_len, 57 | void *out_data, unsigned int out_len) 58 | { 59 | u8 const *ip = (const u8 *)in_data; 60 | u8 *op = (u8 *)out_data; 61 | u8 const *const in_end = ip + in_len; 62 | u8 *const out_end = op + out_len; 63 | 64 | do 65 | { 66 | unsigned int ctrl = *ip++; 67 | 68 | if (ctrl < (1 << 5)) /* literal run */ 69 | { 70 | ctrl++; 71 | 72 | if (op + ctrl > out_end) 73 | { 74 | SET_ERRNO (E2BIG); 75 | return 0; 76 | } 77 | 78 | #if CHECK_INPUT 79 | if (ip + ctrl > in_end) 80 | { 81 | SET_ERRNO (EINVAL); 82 | return 0; 83 | } 84 | #endif 85 | 86 | #ifdef lzf_movsb 87 | lzf_movsb (op, ip, ctrl); 88 | #else 89 | do 90 | *op++ = *ip++; 91 | while (--ctrl); 92 | #endif 93 | } 94 | else /* back reference */ 95 | { 96 | unsigned int len = ctrl >> 5; 97 | 98 | u8 *ref = op - ((ctrl & 0x1f) << 8) - 1; 99 | 100 | #if CHECK_INPUT 101 | if (ip >= in_end) 102 | { 103 | SET_ERRNO (EINVAL); 104 | return 0; 105 | } 106 | #endif 107 | if (len == 7) 108 | { 109 | len += *ip++; 110 | #if CHECK_INPUT 111 | if (ip >= in_end) 112 | { 113 | SET_ERRNO (EINVAL); 114 | return 0; 115 | } 116 | #endif 117 | } 118 | 119 | ref -= *ip++; 120 | 121 | if (op + len + 2 > out_end) 122 | { 123 | SET_ERRNO (E2BIG); 124 | return 0; 125 | } 126 | 127 | if (ref < (u8 *)out_data) 128 | { 129 | SET_ERRNO (EINVAL); 130 | return 0; 131 | } 132 | 133 | #ifdef lzf_movsb 134 | len += 2; 135 | lzf_movsb (op, ref, len); 136 | #else 137 | *op++ = *ref++; 138 | *op++ = *ref++; 139 | 140 | do 141 | *op++ = *ref++; 142 | while (--len); 143 | #endif 144 | } 145 | } 146 | while (ip < in_end); 147 | 148 | return op - (u8 *)out_data; 149 | } 150 | 151 | -------------------------------------------------------------------------------- /src/main.c: -------------------------------------------------------------------------------- 1 | /* 2 | * rdb parser for redis. 3 | * author: @hulk 4 | * date: 2014-08-13 5 | */ 6 | #include 7 | #include 8 | #include 9 | #include 10 | #include "rdb.h" 11 | #include "log.h" 12 | 13 | static void 14 | usage(void) 15 | { 16 | fprintf(stderr, "USAGE: ./rdbtools [-f file] -V -h\n"); 17 | fprintf(stderr, "\t-V --version \n"); 18 | fprintf(stderr, "\t-h --help show usage \n"); 19 | fprintf(stderr, "\t-f --file specify which rdb file would be parsed.\n"); 20 | fprintf(stderr, "\t-s --file specify which lua script, default is ../scripts/example.lua\n"); 21 | fprintf(stderr, "\t Notice: This tool only test on redis 2.2 and 2.4, 2.6, 2.8.\n\n"); 22 | } 23 | 24 | int 25 | main(int argc, char **argv) 26 | { 27 | int c; 28 | char *rdb_file = NULL; 29 | char *lua_file = NULL; 30 | int is_show_help = 0, is_show_version = 0; 31 | char short_options [] = { "hVf:s:" }; 32 | lua_State *L; 33 | 34 | struct option long_options[] = { 35 | { "help", no_argument, NULL, 'h' }, /* help */ 36 | { "version", no_argument, NULL, 'V' }, /* version */ 37 | { "rdb-iile-path", required_argument, NULL, 'f' }, /* rdb file path*/ 38 | { "lua_file-path", required_argument, NULL, 's' }, /* rdb file path*/ 39 | { NULL, 0, NULL, 0 } 40 | }; 41 | 42 | for (;;) { 43 | c = getopt_long(argc, argv, short_options, long_options, NULL); 44 | if (c == -1) { 45 | break; 46 | } 47 | switch (c) { 48 | case 'h': 49 | is_show_help = 1; 50 | break; 51 | case 'V': 52 | is_show_version = 1; 53 | break; 54 | case 'f': 55 | rdb_file = optarg; 56 | break; 57 | case 's': 58 | lua_file = optarg; 59 | break; 60 | default: 61 | exit(0); 62 | } 63 | } 64 | 65 | if(is_show_version) { 66 | fprintf(stderr, "\nHELLO, THIS RDB PARSER VERSION 1.0\n\n"); 67 | } 68 | if(is_show_help) { 69 | usage(); 70 | } 71 | if(is_show_version || is_show_help) { 72 | exit(0); 73 | } 74 | if(!rdb_file) { 75 | logger(ERROR, "You must specify rdb file by option -f filepath.\n"); 76 | } 77 | if(!lua_file) { 78 | lua_file = "../scripts/example.lua"; 79 | } 80 | if (access(rdb_file, R_OK) != 0) 81 | { 82 | logger(ERROR, "rdb file %s is not exists.\n", rdb_file); 83 | } 84 | if (access(lua_file, R_OK) != 0) 85 | { 86 | logger(ERROR, "lua file %s is not exists.\n", lua_file); 87 | } 88 | 89 | L = script_init(lua_file); 90 | rdb_load(L, rdb_file); 91 | lua_close(L); 92 | return 0; 93 | } 94 | -------------------------------------------------------------------------------- /src/rdb.h: -------------------------------------------------------------------------------- 1 | #ifndef _RDB_H_ 2 | #define _RDB_H_ 3 | 4 | #include "script.h" 5 | int rdb_load(lua_State *L, const char *path); 6 | #endif 7 | -------------------------------------------------------------------------------- /src/script.c: -------------------------------------------------------------------------------- 1 | #include "script.h" 2 | #include 3 | #include 4 | #include "log.h" 5 | 6 | LUALIB_API int (luaopen_cjson) (lua_State *L); 7 | 8 | void 9 | lua_loadlib(lua_State *L, const char *libname, lua_CFunction luafunc) { 10 | lua_pushcfunction(L, luafunc); 11 | lua_pushstring(L, libname); 12 | lua_call(L, 1, 0); 13 | } 14 | 15 | lua_State * 16 | script_init(const char *filename) 17 | { 18 | lua_State *L = luaL_newstate(); 19 | luaL_openlibs(L); 20 | lua_loadlib(L, "cjson", luaopen_cjson); 21 | if (luaL_dofile(L, filename)) { 22 | logger(ERROR,"%s", lua_tostring(L, -1)); 23 | } 24 | 25 | lua_newtable(L); 26 | lua_setglobal(L, RDB_ENV); 27 | 28 | if (!script_check_func_exists(L, RDB_CB)) { 29 | logger(ERROR, "function %s is reqired in %s.\n", RDB_CB, filename); 30 | } 31 | return L; 32 | } 33 | 34 | void 35 | script_release(lua_State *L) 36 | { 37 | lua_close(L); 38 | } 39 | 40 | int 41 | script_check_func_exists(lua_State * L, const char *func_name) { 42 | int ret; 43 | 44 | lua_getglobal(L, func_name); 45 | ret = lua_isfunction(L,lua_gettop(L)); 46 | lua_pop(L,-1); 47 | 48 | return ret; 49 | } 50 | 51 | void 52 | script_pushtableunsigned(lua_State* L , char* key, unsigned value) 53 | { 54 | lua_pushstring(L, key); 55 | lua_pushunsigned(L, value); 56 | lua_settable(L, -3); 57 | } 58 | 59 | void 60 | script_pushtableinteger(lua_State* L , char* key , int value) 61 | { 62 | lua_pushstring(L, key); 63 | lua_pushinteger(L, value); 64 | lua_settable(L, -3); 65 | } 66 | 67 | void 68 | script_pushtablestring(lua_State* L , char* key , char* value) 69 | { 70 | lua_pushstring(L, key); 71 | lua_pushstring(L, value); 72 | lua_settable(L, -3); 73 | } 74 | 75 | void 76 | script_push_list_elem(lua_State* L , char* key, int ind) 77 | { 78 | lua_pushstring(L, key); 79 | lua_rawseti(L,-2,ind + 1); 80 | } 81 | 82 | void 83 | script_need_gc(lua_State* L) 84 | { 85 | #define LUA_GC_CYCLE_PERIOD 500 86 | static long gc_count = 0; 87 | 88 | gc_count++; 89 | if (gc_count == LUA_GC_CYCLE_PERIOD) { 90 | lua_gc(L, LUA_GCSTEP, LUA_GC_CYCLE_PERIOD); 91 | gc_count = 0; 92 | } 93 | } 94 | -------------------------------------------------------------------------------- /src/script.h: -------------------------------------------------------------------------------- 1 | #ifndef _SCRIPT_H_ 2 | #define _SCRIPT_H_ 3 | #include 4 | #include 5 | #include 6 | 7 | #define RDB_ENV "env" 8 | #define RDB_CB "handle" 9 | 10 | lua_State *script_init(const char *filename); 11 | void script_release(lua_State *L); 12 | int script_check_func_exists(lua_State * L, const char *func_name); 13 | void script_pushtablestring(lua_State* L , char* key , char* value); 14 | void script_pushtableinteger(lua_State* L , char* key , int value); 15 | void script_pushtableunsigned(lua_State* L , char* key , unsigned value); 16 | void script_push_list_elem(lua_State* L, char* key, int ind); 17 | void script_need_gc(lua_State* L); 18 | #endif 19 | -------------------------------------------------------------------------------- /src/util.c: -------------------------------------------------------------------------------- 1 | #include "util.h" 2 | #include 3 | #include 4 | 5 | char * 6 | ll2string(long long v) 7 | { 8 | int i, len = 2; 9 | long long tmp; 10 | char *buf; 11 | 12 | tmp = v; 13 | while ((tmp /= 10) > 0) len++; 14 | 15 | buf = malloc(len); 16 | if (!buf) { 17 | fprintf(stderr, "Exited, as malloc failed at ll2string.\n"); 18 | exit(1); 19 | } 20 | 21 | i = len - 2; 22 | while (v > 0) { 23 | buf[i--] = v % 10 + '0'; 24 | v /= 10; 25 | } 26 | buf[len - 1] = '\0'; 27 | 28 | return buf; 29 | } 30 | 31 | #ifdef _UTIL_ 32 | int main() 33 | { 34 | int v1 = 0; 35 | int v2 = 10; 36 | int v3 = 100; 37 | int v4 = 100123; 38 | printf("%d to %s\n", v1, ll2string(v1)); 39 | printf("%d to %s\n", v2, ll2string(v2)); 40 | printf("%d to %s\n", v3, ll2string(v3)); 41 | printf("%d to %s\n", v4, ll2string(v4)); 42 | return 0; 43 | } 44 | #endif 45 | -------------------------------------------------------------------------------- /src/util.h: -------------------------------------------------------------------------------- 1 | #ifndef _UITL_H_ 2 | #define _UITL_H_ 3 | char * ll2string(long long v); 4 | #endif 5 | -------------------------------------------------------------------------------- /src/ziplist.c: -------------------------------------------------------------------------------- 1 | #include "ziplist.h" 2 | 3 | #include 4 | #include 5 | #include 6 | #include 7 | 8 | #include "util.h" 9 | #include "log.h" 10 | #include "endian.h" 11 | 12 | uint32_t 13 | ziplist_prev_len_size(const char *s) 14 | { 15 | return ((uint8_t)s[0] < ZIPLIST_BIGLEN) ? 1 : 5; 16 | } 17 | 18 | uint8_t 19 | ziplist_entry_is_str(const char *entry) 20 | { 21 | uint8_t enc; 22 | enc = entry[ziplist_prev_len_size(entry)]; 23 | enc &= ZIP_ENC_STR_MASK; 24 | if (enc == ZIP_ENC_STR_6B 25 | || enc == ZIP_ENC_STR_14B 26 | || enc == ZIP_ENC_STR_32B 27 | ) { 28 | return 1; 29 | } else { 30 | return 0; 31 | } 32 | } 33 | 34 | uint32_t 35 | ziplist_entry_strlen(const char *entry) 36 | { 37 | uint8_t enc; 38 | uint32_t pos; 39 | 40 | pos = ziplist_prev_len_size(entry); 41 | enc = (uint8_t)entry[pos] & 0xc0; 42 | if (enc == ZIP_ENC_STR_6B) { 43 | return entry[pos] & ~ZIP_ENC_STR_MASK; 44 | } else if (enc == ZIP_ENC_STR_14B) { 45 | uint32_t ret = (((uint8_t)entry[pos] & ~ZIP_ENC_STR_MASK) << 8) | (uint8_t)entry[pos + 1]; 46 | return ret; 47 | } else if (enc == ZIP_ENC_STR_32B) { 48 | return ((uint8_t)entry[pos + 1] << 24) | ((uint8_t)entry[pos + 2] << 16) | ((uint8_t)entry[pos + 3] << 8) | (uint8_t)entry[pos + 4]; 49 | } 50 | 51 | return 0; 52 | } 53 | 54 | uint32_t 55 | ziplist_entry_size(const char *entry) 56 | { 57 | uint8_t enc; 58 | uint32_t size = 1, pos; 59 | 60 | pos = ziplist_prev_len_size(entry); 61 | enc = entry[pos]; 62 | // prev entry length 63 | size += ziplist_prev_len_size(entry); 64 | 65 | if(enc == ZIP_ENC_INT8) { 66 | size += 1; 67 | } else if (enc == ZIP_ENC_INT16) { 68 | size += 2; 69 | } else if (enc == ZIP_ENC_INT24) { 70 | size += 3; 71 | } else if (enc == ZIP_ENC_INT32) { 72 | size += 4; 73 | } else if (enc == ZIP_ENC_INT64) { 74 | size += 8; 75 | } else if ((enc & ZIP_ENC_STR_MASK) == ZIP_ENC_STR_6B) { 76 | size += ziplist_entry_strlen(entry); 77 | } else if ((enc & ZIP_ENC_STR_MASK) == ZIP_ENC_STR_14B) { 78 | size += 1; 79 | size += ziplist_entry_strlen(entry); 80 | } else if ((enc & ZIP_ENC_STR_MASK) == ZIP_ENC_STR_32B) { 81 | size += 4; 82 | size += ziplist_entry_strlen(entry); 83 | } 84 | 85 | return size; 86 | } 87 | 88 | char* 89 | ziplist_entry_str(const char *entry) 90 | { 91 | uint8_t enc; 92 | uint32_t pre_len_size,len_size = 1, slen; 93 | char *content, *str = NULL; 94 | 95 | pre_len_size = ziplist_prev_len_size(entry); 96 | enc = entry[pre_len_size] & ZIP_ENC_STR_MASK; 97 | if (enc == ZIP_ENC_STR_14B) len_size = 2; 98 | if (enc == ZIP_ENC_STR_32B) len_size = 5; 99 | 100 | content = (char *)entry + pre_len_size + len_size; 101 | if (enc == ZIP_ENC_STR_6B || enc == ZIP_ENC_STR_14B 102 | || enc == ZIP_ENC_STR_32B) { 103 | 104 | slen = ziplist_entry_strlen(entry); 105 | str = malloc(slen + 1); 106 | if (!str) { 107 | logger(ERROR, "Exited, as malloc failed at ziplist entry str.\n"); 108 | } 109 | memcpy(str, content, slen); 110 | str[slen] = '\0'; 111 | } 112 | return str; 113 | } 114 | 115 | uint8_t 116 | ziplist_entry_int(const char *entry, int64_t *v) 117 | { 118 | int8_t v8; 119 | int16_t v16; 120 | int32_t v32; 121 | int64_t v64; 122 | uint8_t enc; 123 | uint32_t pre_len_size; 124 | char *content; 125 | 126 | pre_len_size = ziplist_prev_len_size(entry); 127 | content = (char *)entry + pre_len_size; 128 | enc = entry[pre_len_size]; 129 | 130 | // add one byte for encode. 131 | if (enc == ZIP_ENC_INT8) { 132 | memcpy(&v8, content + 1, sizeof(int8_t)); 133 | *v = v8; 134 | } else if (enc == ZIP_ENC_INT16) { 135 | memcpy(&v16, content + 1, sizeof(int16_t)); 136 | memrev16ifbe(&v16); 137 | *v = v16; 138 | } else if (enc == ZIP_ENC_INT24) { 139 | memcpy(&v32, content + 1, 3); 140 | memrev32ifbe(&v32); 141 | *v = v32; 142 | } else if (enc == ZIP_ENC_INT32) { 143 | memcpy(&v32, content + 1, sizeof(int32_t)); 144 | memrev32ifbe(&v32); 145 | *v = v32; 146 | } else if (enc == ZIP_ENC_INT64){ 147 | memcpy(&v64, content + 1, sizeof(int64_t)); 148 | memrev64ifbe(&v64); 149 | *v = v64; 150 | } else if ((enc & 0xf0) == 0xf0){ 151 | v8 = content[0] & 0x0f; 152 | *v = v8; 153 | } else { 154 | return 0; 155 | } 156 | 157 | return 1; 158 | } 159 | 160 | void 161 | push_ziplist_list_or_set (lua_State *L, const char *zl) 162 | { 163 | int64_t i = 0, v; 164 | char *entry, *str; 165 | 166 | entry = (char *)ZL_ENTRY(zl); 167 | while (!ZIP_IS_END(entry)) { 168 | if (ziplist_entry_is_str(entry)) { 169 | str = ziplist_entry_str(entry); 170 | } else { 171 | if(ziplist_entry_int(entry, &v) > 0) { 172 | str = ll2string(v); 173 | } 174 | } 175 | script_push_list_elem(L, str, i++); 176 | entry += ziplist_entry_size(entry); 177 | free(str); 178 | } 179 | } 180 | 181 | void 182 | push_ziplist_hash_or_zset(lua_State *L, const char *zl) 183 | { 184 | int64_t v; 185 | char *entry, *str, *key, *val; 186 | 187 | entry = (char *)ZL_ENTRY(zl); 188 | while (!ZIP_IS_END(entry)) { 189 | int i; 190 | for (i = 0; i < 2; i++) { 191 | if (ziplist_entry_is_str(entry)) { 192 | str = ziplist_entry_str(entry); 193 | } else { 194 | if(ziplist_entry_int(entry, &v) > 0) { 195 | str = ll2string(v); 196 | } 197 | } 198 | if(i == 0) { 199 | key = str; 200 | } else { 201 | val = str; 202 | } 203 | entry += ziplist_entry_size(entry); 204 | } 205 | 206 | script_pushtablestring(L, key, val); 207 | free(key); 208 | free(val); 209 | } 210 | } 211 | 212 | void 213 | ziplist_dump(const char *s) 214 | { 215 | uint32_t i = 0, len; 216 | char *entry, *str; 217 | 218 | printf("ziplist { \n"); 219 | printf("bytes: %u\n", ZL_BYTES(s)); 220 | printf("len: %u\n", ZL_LEN(s)); 221 | len = ZL_LEN(s); 222 | entry = (char *)ZL_ENTRY(s); 223 | while (!ZIP_IS_END(entry)) { 224 | if (ziplist_entry_is_str(entry)) { 225 | str = ziplist_entry_str(entry); 226 | if (str) { 227 | printf("str value: %s\n", str); 228 | free(str); 229 | } 230 | } else { 231 | int64_t v; 232 | if(ziplist_entry_int(entry, &v) > 0) { 233 | printf("int value: %lld\n", v); 234 | } 235 | } 236 | entry += ziplist_entry_size(entry); 237 | ++i; 238 | } 239 | printf("}\n"); 240 | if(i < (0xffff - 1) && i != len) { 241 | printf("====== Ziplist len error. ======\n"); 242 | exit(1); 243 | } 244 | } 245 | -------------------------------------------------------------------------------- /src/ziplist.h: -------------------------------------------------------------------------------- 1 | #ifndef _ZIPLIST_H_ 2 | #define _ZIPLIST_H_ 3 | #include 4 | #include "script.h" 5 | 6 | #define ZIPLIST_BIGLEN 254 7 | #define ZIPLIST_END 0xff 8 | 9 | #define ZIP_ENC_INT8 0xfe 10 | #define ZIP_ENC_INT16 0xc0 11 | #define ZIP_ENC_INT24 0xf0 12 | #define ZIP_ENC_INT32 0xd0 13 | #define ZIP_ENC_INT64 0xe0 14 | 15 | #define ZIP_ENC_STR_6B (0 << 6) 16 | #define ZIP_ENC_STR_14B (1 << 6) 17 | #define ZIP_ENC_STR_32B (2 << 6) 18 | 19 | #define ZIP_ENC_STR_MASK 0xc0 20 | #define ZIP_IS_END(entry) ((uint8_t)entry[0] == ZIPLIST_END) 21 | #define ZL_BYTES(zl) *((uint32_t *)(zl)) 22 | #define ZL_LEN(zl) *((uint16_t*)((zl) + 2 * sizeof(uint32_t))) 23 | #define ZL_HDR_SIZE (2 * sizeof(uint32_t) + sizeof(uint16_t)) 24 | #define ZL_ENTRY(zl) ((uint8_t *)zl + ZL_HDR_SIZE) 25 | 26 | typedef struct { 27 | uint32_t bytes; 28 | uint32_t len; 29 | uint32_t tail; 30 | char entrys[0]; 31 | } ziplist; 32 | 33 | void push_ziplist_hash_or_zset(lua_State *L, const char *zl); 34 | void push_ziplist_list_or_set (lua_State *L, const char *zl); 35 | void ziplist_dump(const char *s); 36 | #endif 37 | -------------------------------------------------------------------------------- /src/zipmap.c: -------------------------------------------------------------------------------- 1 | #include "zipmap.h" 2 | #include 3 | #include 4 | #include 5 | #include 6 | #include "log.h" 7 | #include "endian.h" 8 | 9 | #define ZM_END 0xff 10 | #define ZM_BIGLEN 0xfd 11 | #define ZM_IS_END(entry) ((uint8_t)entry[0] == ZM_END) 12 | 13 | uint32_t 14 | zipmap_entry_len_size(const char *entry) 15 | { 16 | uint32_t size = 0; 17 | if ((uint8_t)entry[0] < ZM_BIGLEN) { 18 | size = 1; 19 | } else if ((uint8_t)entry[0] == ZM_BIGLEN) { 20 | size = 5; 21 | } 22 | 23 | return size; 24 | } 25 | 26 | uint32_t 27 | zipmap_entry_strlen (const char *entry) 28 | { 29 | uint32_t entry_len_size; 30 | uint32_t slen; 31 | 32 | entry_len_size = zipmap_entry_len_size(entry); 33 | if (entry_len_size == 1) { 34 | return (uint8_t)entry[0]; 35 | } else if (entry_len_size == 5) { 36 | memcpy(&slen, entry + 1, 4); 37 | memrev32ifbe(&slen); 38 | return slen; 39 | } else { 40 | return 0; 41 | } 42 | } 43 | 44 | uint32_t 45 | zipmap_entry_len (const char *entry) 46 | { 47 | return zipmap_entry_len_size(entry) + zipmap_entry_strlen(entry); 48 | } 49 | 50 | void push_zipmap(lua_State *L, const char *zm) 51 | { 52 | int klen, vlen; 53 | char *key, *val; 54 | 55 | ++zm; 56 | while (!ZM_IS_END(zm)) { 57 | // key 58 | klen = zipmap_entry_strlen(zm); 59 | key = malloc(klen + 1); 60 | if (!key) { 61 | logger(ERROR, "Exited, as malloc failed at zipmap.\n"); 62 | } 63 | memcpy(key, zm + zipmap_entry_len_size(zm), klen); 64 | key[klen] = '\0'; 65 | zm += zipmap_entry_len(zm); 66 | 67 | // value 68 | vlen = zipmap_entry_strlen(zm); 69 | val = malloc(vlen + 1); 70 | if (!val) { 71 | logger(ERROR, "Exited, as malloc failed at zipmap.\n"); 72 | } 73 | memcpy(val, zm + zipmap_entry_len_size(zm) + 1, vlen); 74 | val[vlen] = '\0'; 75 | zm += zipmap_entry_len(zm) + 1; 76 | 77 | script_pushtablestring(L, key, val); 78 | free(key); 79 | free(val); 80 | } 81 | } 82 | 83 | void 84 | zipmap_dump(const char *zm) 85 | { 86 | int i = 0, len, klen, vlen; 87 | char *key, *val; 88 | 89 | len = (uint8_t) zm[0]; 90 | ++zm; 91 | 92 | while (!ZM_IS_END(zm)) { 93 | // key 94 | klen = zipmap_entry_strlen(zm); 95 | key = malloc(klen + 1); 96 | memcpy(key, zm + zipmap_entry_len_size(zm), klen); 97 | key[klen] = '\0'; 98 | zm += zipmap_entry_len(zm); 99 | 100 | // value 101 | vlen = zipmap_entry_strlen(zm); 102 | val = malloc(vlen + 1); 103 | val[vlen] = '\0'; 104 | memcpy(val, zm + zipmap_entry_len_size(zm) + 1, vlen); 105 | zm += zipmap_entry_len(zm) + 1; 106 | 107 | printf(" key is %s, value is %s\n", key, val); 108 | free(key); 109 | free(val); 110 | i+=2; 111 | } 112 | 113 | if(len >=254 && i == len) { 114 | printf("===== zipmap len error.\n"); 115 | exit(1); 116 | } 117 | } 118 | -------------------------------------------------------------------------------- /src/zipmap.h: -------------------------------------------------------------------------------- 1 | #ifndef _ZIPMAP_H_ 2 | #define _ZIPMAP_H_ 3 | 4 | #include "script.h" 5 | 6 | void push_zipmap(lua_State *L, const char *zm); 7 | void zipmap_dump(const char *zm); 8 | #endif 9 | -------------------------------------------------------------------------------- /tests/dump2.2.rdb: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/git-hulk/rdbtools/08571e5cd22cc9268bdcd9319b21a2d7ea21a0e0/tests/dump2.2.rdb -------------------------------------------------------------------------------- /tests/dump2.4.rdb: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/git-hulk/rdbtools/08571e5cd22cc9268bdcd9319b21a2d7ea21a0e0/tests/dump2.4.rdb -------------------------------------------------------------------------------- /tests/dump2.6.rdb: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/git-hulk/rdbtools/08571e5cd22cc9268bdcd9319b21a2d7ea21a0e0/tests/dump2.6.rdb -------------------------------------------------------------------------------- /tests/dump2.8.rdb: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/git-hulk/rdbtools/08571e5cd22cc9268bdcd9319b21a2d7ea21a0e0/tests/dump2.8.rdb --------------------------------------------------------------------------------