├── .gitignore ├── COPYRIGHT ├── DIFFS ├── HISTORY ├── INSTALL ├── LICENSE.txt ├── MANIFEST ├── Makefile ├── Makefile.cross ├── Makefile.macos ├── README.lua ├── README.md ├── UPDATE ├── VERSION ├── build ├── config ├── config.cross ├── config.macos ├── configure ├── distrib ├── ._License.txt ├── License.txt └── mkmacos ├── doc ├── ._logo.gif ├── contents.html ├── logo.gif ├── lua.1 ├── lua.html ├── luac.1 ├── luac.html ├── manual.html └── readme.html ├── etc ├── Makefile ├── README ├── bin2c.c ├── compat.lua ├── doall.lua ├── lua.ico ├── lua.magic ├── lua.xpm ├── luser_number.h ├── luser_tests.h ├── min.c ├── noparser.c ├── saconfig.c └── trace.c ├── include ├── Makefile ├── compat.h ├── lauxlib.h ├── lua.h └── lualib.h ├── ls-lR.txt ├── src ├── Makefile ├── Makefile.cross ├── Makefile.macos ├── README ├── compat.c ├── lapi.c ├── lapi.h ├── lcode.c ├── lcode.h ├── ldebug.c ├── ldebug.h ├── ldo.c ├── ldo.h ├── ldump.c ├── lfunc.c ├── lfunc.h ├── lgc.c ├── lgc.h ├── lib │ ├── Makefile │ ├── Makefile.cross │ ├── Makefile.macos │ ├── README │ ├── lauxlib.c │ ├── lbaselib.c │ ├── lbwlib.c │ ├── ldblib.c │ ├── liolib.c │ ├── lmathlib.c │ ├── loadlib.c │ ├── lpacklib.c │ ├── lstrlib.c │ └── ltablib.c ├── llex.c ├── llex.h ├── llimits.h ├── lmem.c ├── lmem.h ├── lobject.c ├── lobject.h ├── lopcodes.c ├── lopcodes.h ├── lparser.c ├── lparser.h ├── lstate.c ├── lstate.h ├── lstring.c ├── lstring.h ├── ltable.c ├── ltable.h ├── ltests.c ├── ltm.c ├── ltm.h ├── lua │ ├── Makefile │ ├── README │ └── lua.c ├── luac │ ├── Makefile │ ├── Makefile.cross │ ├── Makefile.macos │ ├── README │ ├── luac.c │ ├── pdb.c │ ├── pdb.h │ ├── pluac.c │ ├── print.c │ └── resources.h ├── lundump.c ├── lundump.h ├── lvm.c ├── lvm.h ├── lzio.c └── lzio.h └── test ├── README ├── bisect.lua ├── cf.lua ├── echo.lua ├── env.lua ├── factorial.lua ├── fib.lua ├── fibfor.lua ├── globals.lua ├── hello.lua ├── life.lua ├── lua ├── luac ├── luac.lua ├── printf.lua ├── readonly.lua ├── sieve.lua ├── sort.lua ├── table.lua ├── trace-calls.lua ├── trace-globals.lua ├── undefined.lua └── xd.lua /.gitignore: -------------------------------------------------------------------------------- 1 | bin/cross/* 2 | bin/macos/* 3 | bin/lua 4 | bin/luac 5 | bin/plua2c 6 | *.o 7 | *.a 8 | -------------------------------------------------------------------------------- /COPYRIGHT: -------------------------------------------------------------------------------- 1 | Lua License 2 | ----------- 3 | 4 | Lua is licensed under the terms of the MIT license reproduced below. 5 | This means that Lua is free software and can be used for both academic 6 | and commercial purposes at absolutely no cost. 7 | 8 | For details and rationale, see http://www.lua.org/license.html . 9 | 10 | =============================================================================== 11 | 12 | Copyright (C) 2003-2006 Tecgraf, PUC-Rio. 13 | 14 | Permission is hereby granted, free of charge, to any person obtaining a copy 15 | of this software and associated documentation files (the "Software"), to deal 16 | in the Software without restriction, including without limitation the rights 17 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 18 | copies of the Software, and to permit persons to whom the Software is 19 | furnished to do so, subject to the following conditions: 20 | 21 | The above copyright notice and this permission notice shall be included in 22 | all copies or substantial portions of the Software. 23 | 24 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 25 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 26 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 27 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 28 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 29 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 30 | THE SOFTWARE. 31 | 32 | =============================================================================== 33 | 34 | (end of COPYRIGHT) 35 | -------------------------------------------------------------------------------- /INSTALL: -------------------------------------------------------------------------------- 1 | This is Lua 5.0. 2 | 3 | * Installation 4 | ------------ 5 | Building Lua on a Unix system should be very easy: 6 | 7 | 1. Read "config" and edit it to suit your platform and needs. 8 | We strongly recommend that you enable support for dynamic loading, 9 | if your platform allows it. 10 | 2. Do "make". 11 | 3. If you want to install Lua in an "official" place in your system, 12 | then do "make install". The official place and the way to install 13 | files are defined in "config". You may have to be root to do this. 14 | 15 | See below for instructions for Windows and other systems. 16 | 17 | * What you get 18 | ------------ 19 | If "make" succeeds, you get: 20 | * an interpreter in ./bin/lua and a compiler in ./bin/luac; 21 | * libraries in ./lib; 22 | * include files in ./include. 23 | These are the only directories you need for development. 24 | 25 | There are man pages for lua and luac, in both nroff and html; a reference 26 | manual in html in ./doc, some sample code in ./test, and some useful stuff 27 | in ./etc. You don't need these directories for development. 28 | 29 | See also the README files in the various subdirectories. 30 | A convenient starting point is ./doc/readme.html. 31 | 32 | * If you have problems (and solutions!) 33 | ------------------------------------- 34 | If "make" fails, please let us know (lua@tecgraf.puc-rio.br). 35 | If you make changes to "config" or to the Makefiles, please send them to us. 36 | 37 | * Shared libraries 38 | ---------------- 39 | If you are running Linux, do "make so" after "make" succeeds. 40 | This will create shared libraries in ./lib. 41 | To install those shared libraries in an official place, do "make soinstall". 42 | 43 | If you want the interpreter and the compiler to use shared libraries, 44 | then do "make sobin" too. You may want to do this before "make install". 45 | 46 | If you only want the shared libraries, you may want to add -fPIC to MYCFLAGS 47 | in "config". Also, you may need to run "ldconfig" as root. 48 | 49 | You may need to include ./lib in the LD_LIBRARY_PATH environment variable 50 | to link programs that use the shared libraries if you don't put them in the 51 | official places with "make install". (You may need to use the full path.) 52 | 53 | Building shared libraries in other systems is similar but details differ; 54 | you may need to fix a few details in the top-level Makefile. 55 | 56 | * Installation on Windows and other systems 57 | ----------------------------------------------------- 58 | The instructions for building Lua on other systems machine depend on the 59 | particular compiler you are using. The simplest way is to create a folder 60 | with all .c and .h files, and then create projects for the core library, 61 | the standard library, the interpreter, and the compiler, as follows: 62 | 63 | core lib: lapi.c lcode.c ldebug.c ldo.c ldump.c lfunc.c lgc.c llex.c 64 | lmem.c lobject.c lopcodes.c lparser.c lstate.c lstring.c 65 | ltable.c ltests.c ltm.c lundump.c lvm.c lzio.c 66 | 67 | standard lib: lauxlib.c lbaselib.c ldblib.c liolib.c lmathlib.c ltablib.c 68 | lstrlib.c loadlib.c 69 | 70 | interpreter: core lib, standard lib, lua.c 71 | 72 | compiler: core lib, standard lib, luac.c print.c 73 | and also lopcodes.c (with LUA_OPNAMES defined) from core. 74 | 75 | Of course, to use Lua as a library, you'll have to know how to create 76 | and use libraries with your compiler. 77 | 78 | Also, read "config" to see what can be customized at compilation time. 79 | 80 | (end of INSTALL) 81 | -------------------------------------------------------------------------------- /LICENSE.txt: -------------------------------------------------------------------------------- 1 | Plua2c License 2 | -------------- 3 | 4 | Plua2c is licensed under the terms of the MIT license reproduced below, using 5 | the same terms as Lua itself. 6 | 7 | =============================================================================== 8 | 9 | Lua Copyright (C) 1994-2021 Lua.org, PUC-Rio. 10 | Plua Copyright (C) 2001-2021 Marcio Migueletto de Andrade and Contributors. 11 | All rights reserved. 12 | 13 | Permission is hereby granted, free of charge, to any person obtaining a copy 14 | of this software and associated documentation files (the "Software"), to deal 15 | in the Software without restriction, including without limitation the rights 16 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 17 | copies of the Software, and to permit persons to whom the Software is 18 | furnished to do so, subject to the following conditions: 19 | 20 | The above copyright notice and this permission notice shall be included in 21 | all copies or substantial portions of the Software. 22 | 23 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 24 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 25 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 26 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 27 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 28 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 29 | THE SOFTWARE. 30 | 31 | =============================================================================== 32 | 33 | -------------------------------------------------------------------------------- /MANIFEST: -------------------------------------------------------------------------------- 1 | MANIFEST contents of Lua 5.0.3 distribution on Mon Jun 19 11:04:48 BRT 2006 2 | lua-5.0.3 3 | lua-5.0.3/COPYRIGHT 4 | lua-5.0.3/DIFFS 5 | lua-5.0.3/HISTORY 6 | lua-5.0.3/INSTALL 7 | lua-5.0.3/MANIFEST 8 | lua-5.0.3/Makefile 9 | lua-5.0.3/README 10 | lua-5.0.3/UPDATE 11 | lua-5.0.3/bin 12 | lua-5.0.3/build 13 | lua-5.0.3/config 14 | lua-5.0.3/configure 15 | lua-5.0.3/doc 16 | lua-5.0.3/doc/contents.html 17 | lua-5.0.3/doc/logo.gif 18 | lua-5.0.3/doc/lua.1 19 | lua-5.0.3/doc/lua.html 20 | lua-5.0.3/doc/luac.1 21 | lua-5.0.3/doc/luac.html 22 | lua-5.0.3/doc/manual.html 23 | lua-5.0.3/doc/readme.html 24 | lua-5.0.3/etc 25 | lua-5.0.3/etc/Makefile 26 | lua-5.0.3/etc/README 27 | lua-5.0.3/etc/bin2c.c 28 | lua-5.0.3/etc/compat.lua 29 | lua-5.0.3/etc/doall.lua 30 | lua-5.0.3/etc/lua.ico 31 | lua-5.0.3/etc/lua.magic 32 | lua-5.0.3/etc/lua.xpm 33 | lua-5.0.3/etc/luser_number.h 34 | lua-5.0.3/etc/luser_tests.h 35 | lua-5.0.3/etc/min.c 36 | lua-5.0.3/etc/noparser.c 37 | lua-5.0.3/etc/saconfig.c 38 | lua-5.0.3/etc/trace.c 39 | lua-5.0.3/include 40 | lua-5.0.3/include/Makefile 41 | lua-5.0.3/include/lauxlib.h 42 | lua-5.0.3/include/lua.h 43 | lua-5.0.3/include/lualib.h 44 | lua-5.0.3/lib 45 | lua-5.0.3/src 46 | lua-5.0.3/src/Makefile 47 | lua-5.0.3/src/README 48 | lua-5.0.3/src/lapi.c 49 | lua-5.0.3/src/lapi.h 50 | lua-5.0.3/src/lcode.c 51 | lua-5.0.3/src/lcode.h 52 | lua-5.0.3/src/ldebug.c 53 | lua-5.0.3/src/ldebug.h 54 | lua-5.0.3/src/ldo.c 55 | lua-5.0.3/src/ldo.h 56 | lua-5.0.3/src/ldump.c 57 | lua-5.0.3/src/lfunc.c 58 | lua-5.0.3/src/lfunc.h 59 | lua-5.0.3/src/lgc.c 60 | lua-5.0.3/src/lgc.h 61 | lua-5.0.3/src/lib 62 | lua-5.0.3/src/lib/Makefile 63 | lua-5.0.3/src/lib/README 64 | lua-5.0.3/src/lib/lauxlib.c 65 | lua-5.0.3/src/lib/lbaselib.c 66 | lua-5.0.3/src/lib/ldblib.c 67 | lua-5.0.3/src/lib/liolib.c 68 | lua-5.0.3/src/lib/lmathlib.c 69 | lua-5.0.3/src/lib/loadlib.c 70 | lua-5.0.3/src/lib/lstrlib.c 71 | lua-5.0.3/src/lib/ltablib.c 72 | lua-5.0.3/src/llex.c 73 | lua-5.0.3/src/llex.h 74 | lua-5.0.3/src/llimits.h 75 | lua-5.0.3/src/lmem.c 76 | lua-5.0.3/src/lmem.h 77 | lua-5.0.3/src/lobject.c 78 | lua-5.0.3/src/lobject.h 79 | lua-5.0.3/src/lopcodes.c 80 | lua-5.0.3/src/lopcodes.h 81 | lua-5.0.3/src/lparser.c 82 | lua-5.0.3/src/lparser.h 83 | lua-5.0.3/src/lstate.c 84 | lua-5.0.3/src/lstate.h 85 | lua-5.0.3/src/lstring.c 86 | lua-5.0.3/src/lstring.h 87 | lua-5.0.3/src/ltable.c 88 | lua-5.0.3/src/ltable.h 89 | lua-5.0.3/src/ltests.c 90 | lua-5.0.3/src/ltm.c 91 | lua-5.0.3/src/ltm.h 92 | lua-5.0.3/src/lua 93 | lua-5.0.3/src/lua/Makefile 94 | lua-5.0.3/src/lua/README 95 | lua-5.0.3/src/lua/lua.c 96 | lua-5.0.3/src/luac 97 | lua-5.0.3/src/luac/Makefile 98 | lua-5.0.3/src/luac/README 99 | lua-5.0.3/src/luac/luac.c 100 | lua-5.0.3/src/luac/print.c 101 | lua-5.0.3/src/lundump.c 102 | lua-5.0.3/src/lundump.h 103 | lua-5.0.3/src/lvm.c 104 | lua-5.0.3/src/lvm.h 105 | lua-5.0.3/src/lzio.c 106 | lua-5.0.3/src/lzio.h 107 | lua-5.0.3/test 108 | lua-5.0.3/test/README 109 | lua-5.0.3/test/bisect.lua 110 | lua-5.0.3/test/cf.lua 111 | lua-5.0.3/test/echo.lua 112 | lua-5.0.3/test/env.lua 113 | lua-5.0.3/test/factorial.lua 114 | lua-5.0.3/test/fib.lua 115 | lua-5.0.3/test/fibfor.lua 116 | lua-5.0.3/test/globals.lua 117 | lua-5.0.3/test/hello.lua 118 | lua-5.0.3/test/life.lua 119 | lua-5.0.3/test/lua 120 | lua-5.0.3/test/luac 121 | lua-5.0.3/test/luac.lua 122 | lua-5.0.3/test/printf.lua 123 | lua-5.0.3/test/readonly.lua 124 | lua-5.0.3/test/sieve.lua 125 | lua-5.0.3/test/sort.lua 126 | lua-5.0.3/test/table.lua 127 | lua-5.0.3/test/trace-calls.lua 128 | lua-5.0.3/test/trace-globals.lua 129 | lua-5.0.3/test/undefined.lua 130 | lua-5.0.3/test/xd.lua 131 | END OF MANIFEST 132 | -------------------------------------------------------------------------------- /Makefile: -------------------------------------------------------------------------------- 1 | # makefile for Lua hierarchy 2 | # see INSTALL for installation instructions 3 | # see config for customization instructions 4 | 5 | LUA= . 6 | 7 | include $(LUA)/config 8 | 9 | # primary targets ("co" and "klean" are used for making the distribution) 10 | all clean co klean: dirs 11 | cd include; $(MAKE) $@ 12 | cd src; $(MAKE) $@ 13 | cd src/lib; $(MAKE) $@ 14 | cd src/luac; $(MAKE) $@ 15 | cd src/lua; $(MAKE) $@ 16 | find . -path './*/macos/*' -print | xargs rm || echo 'never mind' 17 | find . -path './*/cross/*' -print | xargs rm || echo 'never mind' 18 | 19 | # in case they were not created during unpacking 20 | dirs: bin lib 21 | 22 | bin lib: 23 | mkdir -p $@ 24 | 25 | # simple test to see Lua working 26 | test: all 27 | bin/lua test/hello.lua 28 | 29 | # remove debug information from binaries 30 | strip: 31 | $(STRIP) bin/* 32 | 33 | # official installation 34 | install: all strip 35 | mkdir -p $(INSTALL_BIN) $(INSTALL_INC) $(INSTALL_LIB) $(INSTALL_MAN) 36 | $(INSTALL_EXEC) bin/* $(INSTALL_BIN) 37 | $(INSTALL_DATA) include/*.h $(INSTALL_INC) 38 | $(INSTALL_DATA) lib/*.a $(INSTALL_LIB) 39 | $(INSTALL_DATA) doc/*.1 $(INSTALL_MAN) 40 | 41 | # shared libraries (for Linux) 42 | so: 43 | ld -o lib/liblua.so.$V -shared src/*.o 44 | ld -o lib/liblualib.so.$V -shared src/lib/*.o 45 | cd lib; ln -fs liblua.so.$V liblua.so; ln -fs liblualib.so.$V liblualib.so 46 | 47 | # binaries using shared libraries 48 | sobin: 49 | rm -f bin/* 50 | cd src/lua; $(MAKE) 51 | cd src/luac; $(MAKE) 52 | 53 | # install shared libraries 54 | soinstall: 55 | $(INSTALL_EXEC) lib/*.so.* $(INSTALL_LIB) 56 | cd $(INSTALL_LIB); ln -fs liblua.so.$V liblua.so; ln -fs liblualib.so.$V liblualib.so 57 | 58 | # clean shared libraries 59 | soclean: 60 | rm -f lib/*.so* bin/* 61 | 62 | # echo config parameters 63 | echo: 64 | @echo "" 65 | @echo "These are the parameters currently set in $(LUA)/config to build Lua $V:" 66 | @echo "" 67 | @echo "LOADLIB = $(LOADLIB)" 68 | @echo "DLLIB = $(DLLIB)" 69 | @echo "NUMBER = $(NUMBER)" 70 | @echo "POPEN = $(POPEN)" 71 | @echo "TMPNAM = $(TMPNAM)" 72 | @echo "DEGREES = $(DEGREES)" 73 | @echo "USERCONF = $(USERCONF)" 74 | @echo "CC = $(CC)" 75 | @echo "WARN = $(WARN)" 76 | @echo "MYCFLAGS = $(MYCFLAGS)" 77 | @echo "MYLDFLAGS = $(MYLDFLAGS)" 78 | @echo "EXTRA_LIBS = $(EXTRA_LIBS)" 79 | @echo "AR = $(AR)" 80 | @echo "RANLIB = $(RANLIB)" 81 | @echo "STRIP = $(STRIP)" 82 | @echo "INSTALL_ROOT = $(INSTALL_ROOT)" 83 | @echo "INSTALL_BIN = $(INSTALL_BIN)" 84 | @echo "INSTALL_INC = $(INSTALL_INC)" 85 | @echo "INSTALL_LIB = $(INSTALL_LIB)" 86 | @echo "INSTALL_MAN = $(INSTALL_MAN)" 87 | @echo "INSTALL_EXEC = $(INSTALL_EXEC)" 88 | @echo "INSTALL_DATA = $(INSTALL_DATA)" 89 | @echo "" 90 | @echo "Edit $(LUA)/config if needed to suit your platform and then run make." 91 | @echo "" 92 | 93 | # turn config into Lua code 94 | # uncomment the last sed expression if you want nil instead of empty strings 95 | lecho: 96 | @echo "-- $(LUA)/config for Lua $V" 97 | @echo "VERSION = '$(V)'" 98 | @make echo | grep = | sed -e 's/= /= "/' -e 's/$$/"/' #-e 's/""/nil/' 99 | @echo "-- EOF" 100 | 101 | newer: 102 | @find . -newer MANIFEST -type f 103 | 104 | # (end of Makefile) 105 | -------------------------------------------------------------------------------- /Makefile.cross: -------------------------------------------------------------------------------- 1 | LUA=. 2 | 3 | include $(LUA)/config.cross 4 | 5 | all clean: 6 | cd src; $(MAKE) -f Makefile.cross $@ 7 | cd src/lib; $(MAKE) -f Makefile.cross $@ 8 | cd src/luac; $(MAKE) -f Makefile.cross $@ 9 | -------------------------------------------------------------------------------- /Makefile.macos: -------------------------------------------------------------------------------- 1 | LUA=. 2 | 3 | include $(LUA)/config.macos 4 | 5 | all clean: 6 | cd src; $(MAKE) -f Makefile.macos $@ 7 | cd src/lib; $(MAKE) -f Makefile.macos $@ 8 | cd src/luac; $(MAKE) -f Makefile.macos $@ 9 | -------------------------------------------------------------------------------- /README.lua: -------------------------------------------------------------------------------- 1 | This is Lua 5.0. 2 | See HISTORY for a summary of changes since the last released version. 3 | 4 | * What is Lua? 5 | ------------ 6 | Lua is a powerful, light-weight programming language designed for extending 7 | applications. Lua is also frequently used as a general-purpose, stand-alone 8 | language. Lua is free software. 9 | 10 | For complete information, visit Lua's web site at http://www.lua.org/ . 11 | For an executive summary, see http://www.lua.org/about.html . 12 | 13 | Lua has been used in many different projects around the world. 14 | For a short list, see http://www.lua.org/uses.html . 15 | 16 | * Availability 17 | ------------ 18 | Lua is freely available for both academic and commercial purposes. 19 | See COPYRIGHT and http://www.lua.org/license.html for details. 20 | Lua can be downloaded from its official site http://www.lua.org/ and 21 | several other sites aroung the world. For a complete list of mirror sites, 22 | see http://www.lua.org/mirrors.html . 23 | 24 | * Installation 25 | ------------ 26 | Lua is implemented in pure ANSI C, and compiles unmodified in all known 27 | platforms that have an ANSI C compiler. Under Unix, simply typing "make" 28 | should work. See INSTALL for detailed instructions. 29 | 30 | * Contacting the authors 31 | ---------------------- 32 | Send your comments, questions, and bug reports to lua@tecgraf.puc-rio.br. 33 | For more information about the authors, see http://www.lua.org/authors.html . 34 | For reporting bugs, try also the mailing list: lua-l@tecgraf.puc-rio.br. 35 | For more information about this list, including instructions on how to 36 | subscribe and access the archives, see http://www.lua.org/lua-l.html . 37 | 38 | * Origin 39 | ------ 40 | Lua is developed at Tecgraf, the Computer Graphics Technology Group 41 | of PUC-Rio (the Pontifical Catholic University of Rio de Janeiro in Brazil). 42 | Tecgraf is a laboratory of the Department of Computer Science. 43 | 44 | (end of README) 45 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Plua2c 2 | 3 | Plua is a superset of Lua for classic PalmOS devices with added support for graphics, UI, networking, events and sound. This is the source code for the Plua2c "cross-compiler" that generates .prc 4 | files from Plua 2.0 source. You still need Plua2RT and MathLib to run the .prc 5 | installed on the same device, which can be downloaded from 6 | [Plua Revisited](http://www.floodgap.com/retrotech/plua/). 7 | 8 | Plua2c has generously been relicensed under the same MIT terms as Lua by 9 | its original author, Marcio Migueletto de Andrade. For details, see 10 | `LICENSE.txt`. 11 | 12 | The current version generates binaries compatible with Plua2RT 2.0, based on 13 | Lua 5.0.3. It should build and run on 32- and 64-bit hosts, either big or 14 | little endian. It has been tested on Linux and Mac OS X (PowerPC and x86_64). 15 | 16 | Most systems will build with a simple `make`. This includes current versions 17 | of macOS. PowerPC-based versions of Mac OS X should use 18 | `make -f Makefile.macos`. 19 | 20 | If you are building for a foreign platform, edit `Makefile.cross` if necessary, 21 | which is in three places in this tree, and use that instead 22 | (`make -f Makefile.cross`). 23 | -------------------------------------------------------------------------------- /UPDATE: -------------------------------------------------------------------------------- 1 | This is Lua 5.0.2, an update of Lua 5.0 that includes the following changes, 2 | which fix all known bugs in Lua 5.0. For the exact differences, see DIFFS. 3 | 4 | src/ldo.c 5 | Attempt to resume running coroutine crashed Lua 6 | src/lgc.c 7 | C functions also may have stacks larger than current top 8 | Userdata to be collected still counted into new GC threshold 9 | src/lgc.h 10 | Userdata to be collected still counted into new GC threshold 11 | src/lparser.c 12 | Syntax `local function' did not increment stack size 13 | src/lvm.c 14 | `pc' address was invalidated when a coroutine was suspended 15 | Count hook might be called without being set 16 | src/lib/lbaselib.c 17 | Buffer overflow for unusual %p representation 18 | Wrong number of returns from chunks loaded from stdin 19 | src/lib/liolib.c 20 | `file.close()' could not be called without arguments 21 | Buffer overflow for unusual %p representation 22 | src/luac/luac.c 23 | Missing lock/unlock 24 | -------------------------------------------------------------------------------- /VERSION: -------------------------------------------------------------------------------- 1 | VERSION=2.0 2 | -------------------------------------------------------------------------------- /build: -------------------------------------------------------------------------------- 1 | # If you don't want to use make, run this script. 2 | # But make sure you read config to see what can be customized. 3 | 4 | 5 | # Easiest way to build bin/lua: 6 | # cc -O2 -o bin/lua -Iinclude -Isrc src/*.c src/lib/*.c src/lua/*.c -lm -ldl 7 | 8 | 9 | # Easiest way to build Lua libraries and executables: 10 | echo -n 'building core library... ' 11 | cd src 12 | cc -O2 -c -I../include *.c 13 | ar rc ../lib/liblua.a *.o 14 | rm -f *.o 15 | 16 | echo -n 'standard library... ' 17 | cd lib 18 | cc -O2 -c -I../../include *.c 19 | ar rc ../../lib/liblualib.a *.o 20 | rm -f *.o 21 | 22 | echo -n 'lua... ' 23 | cd ../lua 24 | cc -O2 -o ../../bin/lua -I../../include *.c ../../lib/*.a -lm -ldl 25 | 26 | echo -n 'luac... ' 27 | cd ../luac 28 | cc -O2 -o ../../bin/luac -I../../include -I.. *.c -DLUA_OPNAMES ../lopcodes.c ../../lib/*.a 29 | 30 | echo 'done' 31 | 32 | cd ../.. 33 | bin/lua test/hello.lua 34 | -------------------------------------------------------------------------------- /config: -------------------------------------------------------------------------------- 1 | # configuration file for making Lua 5.0 2 | # see INSTALL for installation instructions 3 | 4 | # These are default values. Skip this section and see the explanations below. 5 | 6 | LOADLIB= 7 | DLLIB= 8 | NUMBER= 9 | POPEN= 10 | TMPNAM= 11 | DEGREES= 12 | USERCONF= 13 | 14 | # == CHANGE THE SETTINGS BELOW TO SUIT YOUR ENVIRONMENT ======================= 15 | 16 | # --------------------------------------------------------------- Lua libraries 17 | 18 | # Support for dynamically loading C libraries for Lua is a very important 19 | # feature, which we strongly recommend be enabled. By default, this support is 20 | # enabled on Windows systems (see below) but disabled on other systems because 21 | # it relies on system-dependent code that is not part of ANSI C. For more 22 | # information on dynamic loading, read the comments in src/lib/liolib.c . 23 | # 24 | # To enable support for dynamic loading on Unix systems that support the dlfcn 25 | # interface (e.g., Linux, Solaris, IRIX, BSD, AIX, HPUX, and probably others), 26 | # uncomment the next two lines. 27 | # 28 | #LOADLIB= -DUSE_DLOPEN=1 29 | #DLLIB= -ldl 30 | # 31 | # In Linux with gcc, you should also uncomment the next definition for 32 | # MYLDFLAGS, which passes -E (= -export-dynamic) to the linker. This option 33 | # allows dynamic libraries to link back to the `lua' program, so that they do 34 | # not need the Lua libraries. (Other systems may have an equivalent facility.) 35 | # 36 | #MYLDFLAGS= -Wl,-E 37 | # 38 | # On Windows systems. support for dynamic loading is enabled by default. 39 | # To disable this support, uncomment the next line. 40 | # 41 | #LOADLIB= -DUSE_DLL=0 42 | 43 | # The Lua IO library (src/lib/liolib.c) has support for pipes using popen and 44 | # pclose. This support is enabled by default on POSIX systems. 45 | # If your system is not POSIX but has popen and pclose, define USE_POPEN=1. 46 | # If you don't want to support pipes, define USE_POPEN=0. 47 | # 48 | #POPEN= -DUSE_POPEN=1 49 | #POPEN= -DUSE_POPEN=0 50 | # 51 | # The form below will probably work in (some) Windows systems. 52 | # 53 | #POPEN= -DUSE_POPEN=1 -Dpopen=_popen -Dpclose=_pclose 54 | 55 | # The Lua OS library (src/lib/liolib.c) exports an interface to the C function 56 | # tmpnam, which gcc now thinks is `dangerous'. So, support for tmpnam is 57 | # disabled by default when compiling with gcc. 58 | # If you still want to use tmpnam, define USE_TMPNAME=1. If you don't want to 59 | # use tmpnam even if you're not compiling with gcc, define USE_TMPNAME=0. 60 | # 61 | #TMPNAM= -DUSE_TMPNAME=1 62 | #TMPNAM= -DUSE_TMPNAME=0 63 | 64 | # The Lua math library (src/lib/lmathlib.c) now operates in radians, unlike 65 | # previous versions of Lua, which used degrees. To use degrees instead of 66 | # radians, define USE_DEGREES. 67 | # 68 | #DEGREES= -DUSE_DEGREES 69 | 70 | # ------------------------------------------------------------------ Lua core 71 | 72 | # Lua uses double for numbers. To change this, uncomment and edit the following 73 | # line, changing USE_XXX to one of USE_DOUBLE, USE_FLOAT, USE_LONG, USE_INT. 74 | # 75 | #NUMBER= -DLUA_USER_H='"../etc/luser_number.h"' -DUSE_XXX 76 | 77 | # When compiling Lua with gcc on a Pentium machine, using a fast rounding 78 | # method for the conversion of doubles to ints can give around 20% speed 79 | # improvement. To use this rounding method, uncomment the following line. 80 | #NUMBER= -DLUA_USER_H='"../etc/luser_number.h"' -DUSE_FASTROUND 81 | 82 | # For partial compatibility with old upvalue syntax, define LUA_COMPATUPSYNTAX. 83 | # For partial compatibility with old upvalue behavior in C functions, define 84 | # LUA_COMPATUPVALUES. Add these definitions to MYCFLAGS. 85 | # 86 | # -DLUA_COMPATUPSYNTAX -DLUA_COMPATUPVALUES 87 | 88 | # ------------------------------------------------------------- Lua interpreter 89 | 90 | # The stand-alone Lua interpreter needs the math functions, which are usually 91 | # in libm.a (-lm). If your C library already includes the math functions, 92 | # or if you are using a modified interpreter that does not need them, 93 | # then comment the following line or add the appropriates libraries. 94 | # 95 | EXTRA_LIBS= -lm 96 | 97 | # If you want to customize the stand-alone Lua interpreter, uncomment and 98 | # edit the following two lines; also edit etc/saconfig.c to suit your needs. 99 | # -DUSE_READLINE adds line editing and history to the interpreter. You need 100 | # to add -lreadline (and perhaps also -lhistory and -lcurses or -lncurses) 101 | # to EXTRA_LIBS. 102 | # 103 | #USERCONF=-DLUA_USERCONFIG='"$(LUA)/etc/saconfig.c"' -DUSE_READLINE 104 | #EXTRA_LIBS= -lm -ldl -lreadline # -lhistory -lcurses -lncurses 105 | 106 | # ------------------------------------------------------------------ C compiler 107 | 108 | # You need an ANSI C compiler. gcc is a popular one. We do not use -ansi in 109 | # WARN because it disables POSIX features used in the libraries. 110 | # 111 | CC= gcc 112 | WARN= -Wall 113 | 114 | # ------------------------------------------------------------------ C options 115 | 116 | # Write here any options you may need for your C compiler. 117 | # If you are using gcc, -O3 will get you a faster but larger code. You can 118 | # also add -fomit-frame-pointer to get even faster code at the cost of losing 119 | # debug information. If you only want the shared libraries, you may want to 120 | # add -fPIC to MYCFLAGS. 121 | # 122 | MYCFLAGS= -O2 123 | #MYCFLAGS= -O3 -fomit-frame-pointer # -fPIC 124 | 125 | # Write here any options you may need for your C linker. 126 | #MYLDFLAGS= 127 | 128 | # ------------------------------------------------------------------ librarian 129 | 130 | # This should work in all Unix systems. 131 | # 132 | AR= ar rcu 133 | 134 | # If your system doesn't have (or need) ranlib, use RANLIB=true. 135 | # On some systems, "ar s" does what ranlib would do. 136 | # 137 | RANLIB= ranlib 138 | #RANLIB= ar s 139 | #RANLIB= true 140 | 141 | # ------------------------------------------------------------------ stripper 142 | 143 | # This should work in all Unix systems, but you may want to add options. 144 | # 145 | STRIP= strip 146 | 147 | # ------------------------------------------------------------------ install 148 | 149 | # Locations for "make install". You may need to be root do "make install". 150 | # 151 | INSTALL_ROOT= /usr/local 152 | INSTALL_BIN= $(INSTALL_ROOT)/bin 153 | INSTALL_INC= $(INSTALL_ROOT)/include 154 | INSTALL_LIB= $(INSTALL_ROOT)/lib 155 | INSTALL_MAN= $(INSTALL_ROOT)/man/man1 156 | 157 | # You may prefer to use "install" instead of "cp" if you have it. 158 | # If you use "install", you may also want to change the permissions after -m. 159 | # 160 | INSTALL_EXEC= cp 161 | INSTALL_DATA= cp 162 | #INSTALL_EXEC= install -m 0755 163 | #INSTALL_DATA= install -m 0644 164 | 165 | # == END OF USER SETTINGS. NO NEED TO CHANGE ANYTHING BELOW THIS LINE ========= 166 | 167 | V=5.0 168 | 169 | BIN= $(LUA)/bin 170 | INC= $(LUA)/include 171 | LIB= $(LUA)/lib 172 | 173 | MYDEFS=-DEXTRA_CREDITS="\"64-bit port maintained by Cameron Kaiser: www.floodgap.com/retrotech/plua/\"" 174 | 175 | INCS= -I$(INC) $(EXTRA_INCS) 176 | DEFS= $(NUMBER) $(EXTRA_DEFS) $(MYDEFS) 177 | 178 | CFLAGS= $(MYCFLAGS) $(WARN) $(INCS) $(DEFS) 179 | 180 | # (end of config) 181 | -------------------------------------------------------------------------------- /config.macos: -------------------------------------------------------------------------------- 1 | include $(LUA)/VERSION 2 | 3 | # configuration file for making Lua 5.0 4 | # see INSTALL for installation instructions 5 | 6 | # These are default values. Skip this section and see the explanations below. 7 | 8 | LOADLIB= 9 | DLLIB= 10 | NUMBER= 11 | POPEN= 12 | TMPNAM= 13 | DEGREES= 14 | USERCONF= 15 | 16 | # == CHANGE THE SETTINGS BELOW TO SUIT YOUR ENVIRONMENT ======================= 17 | 18 | # --------------------------------------------------------------- Lua libraries 19 | 20 | # Support for dynamically loading C libraries for Lua is a very important 21 | # feature, which we strongly recommend be enabled. By default, this support is 22 | # enabled on Windows systems (see below) but disabled on other systems because 23 | # it relies on system-dependent code that is not part of ANSI C. For more 24 | # information on dynamic loading, read the comments in src/lib/liolib.c . 25 | # 26 | # To enable support for dynamic loading on Unix systems that support the dlfcn 27 | # interface (e.g., Linux, Solaris, IRIX, BSD, AIX, HPUX, and probably others), 28 | # uncomment the next two lines. 29 | # 30 | #LOADLIB= -DUSE_DLOPEN=1 31 | #DLLIB= -ldl 32 | # 33 | # In Linux with gcc, you should also uncomment the next definition for 34 | # MYLDFLAGS, which passes -E (= -export-dynamic) to the linker. This option 35 | # allows dynamic libraries to link back to the `lua' program, so that they do 36 | # not need the Lua libraries. (Other systems may have an equivalent facility.) 37 | # 38 | #MYLDFLAGS= -Wl,-E 39 | # 40 | # On Windows systems. support for dynamic loading is enabled by default. 41 | # To disable this support, uncomment the next line. 42 | # 43 | #LOADLIB= -DUSE_DLL=0 44 | 45 | # The Lua IO library (src/lib/liolib.c) has support for pipes using popen and 46 | # pclose. This support is enabled by default on POSIX systems. 47 | # If your system is not POSIX but has popen and pclose, define USE_POPEN=1. 48 | # If you don't want to support pipes, define USE_POPEN=0. 49 | # 50 | #POPEN= -DUSE_POPEN=1 51 | #POPEN= -DUSE_POPEN=0 52 | # 53 | # The form below will probably work in (some) Windows systems. 54 | # 55 | #POPEN= -DUSE_POPEN=1 -Dpopen=_popen -Dpclose=_pclose 56 | 57 | # The Lua OS library (src/lib/liolib.c) exports an interface to the C function 58 | # tmpnam, which gcc now thinks is `dangerous'. So, support for tmpnam is 59 | # disabled by default when compiling with gcc. 60 | # If you still want to use tmpnam, define USE_TMPNAME=1. If you don't want to 61 | # use tmpnam even if you're not compiling with gcc, define USE_TMPNAME=0. 62 | # 63 | #TMPNAM= -DUSE_TMPNAME=1 64 | #TMPNAM= -DUSE_TMPNAME=0 65 | 66 | # The Lua math library (src/lib/lmathlib.c) now operates in radians, unlike 67 | # previous versions of Lua, which used degrees. To use degrees instead of 68 | # radians, define USE_DEGREES. 69 | # 70 | #DEGREES= -DUSE_DEGREES 71 | 72 | # ------------------------------------------------------------------ Lua core 73 | 74 | # Lua uses double for numbers. To change this, uncomment and edit the following 75 | # line, changing USE_XXX to one of USE_DOUBLE, USE_FLOAT, USE_LONG, USE_INT. 76 | # 77 | #NUMBER= -DLUA_USER_H='"../etc/luser_number.h"' -DUSE_XXX 78 | 79 | # When compiling Lua with gcc on a Pentium machine, using a fast rounding 80 | # method for the conversion of doubles to ints can give around 20% speed 81 | # improvement. To use this rounding method, uncomment the following line. 82 | #NUMBER= -DLUA_USER_H='"../etc/luser_number.h"' -DUSE_FASTROUND 83 | 84 | # For partial compatibility with old upvalue syntax, define LUA_COMPATUPSYNTAX. 85 | # For partial compatibility with old upvalue behavior in C functions, define 86 | # LUA_COMPATUPVALUES. Add these definitions to MYCFLAGS. 87 | # 88 | # -DLUA_COMPATUPSYNTAX -DLUA_COMPATUPVALUES 89 | 90 | # ------------------------------------------------------------- Lua interpreter 91 | 92 | # The stand-alone Lua interpreter needs the math functions, which are usually 93 | # in libm.a (-lm). If your C library already includes the math functions, 94 | # or if you are using a modified interpreter that does not need them, 95 | # then comment the following line or add the appropriates libraries. 96 | # 97 | EXTRA_LIBS= -lm 98 | 99 | # If you want to customize the stand-alone Lua interpreter, uncomment and 100 | # edit the following two lines; also edit etc/saconfig.c to suit your needs. 101 | # -DUSE_READLINE adds line editing and history to the interpreter. You need 102 | # to add -lreadline (and perhaps also -lhistory and -lcurses or -lncurses) 103 | # to EXTRA_LIBS. 104 | # 105 | #USERCONF=-DLUA_USERCONFIG='"$(LUA)/etc/saconfig.c"' -DUSE_READLINE 106 | #EXTRA_LIBS= -lm -ldl -lreadline # -lhistory -lcurses -lncurses 107 | 108 | # ------------------------------------------------------------------ C compiler 109 | 110 | # You need an ANSI C compiler. gcc is a popular one. We do not use -ansi in 111 | # WARN because it disables POSIX features used in the libraries. 112 | # 113 | CC= gcc 114 | WARN= -Wall 115 | 116 | # ------------------------------------------------------------------ C options 117 | 118 | # Write here any options you may need for your C compiler. 119 | # If you are using gcc, -O3 will get you a faster but larger code. You can 120 | # also add -fomit-frame-pointer to get even faster code at the cost of losing 121 | # debug information. If you only want the shared libraries, you may want to 122 | # add -fPIC to MYCFLAGS. 123 | # 124 | MYCFLAGS= -O2 125 | #MYCFLAGS= -O3 -fomit-frame-pointer # -fPIC 126 | 127 | # Write here any options you may need for your C linker. 128 | #MYLDFLAGS= 129 | 130 | # ------------------------------------------------------------------ librarian 131 | 132 | # This should work in all Unix systems. 133 | # 134 | AR= ar rcu 135 | 136 | # If your system doesn't have (or need) ranlib, use RANLIB=true. 137 | # On some systems, "ar s" does what ranlib would do. 138 | # 139 | RANLIB= ranlib 140 | #RANLIB= ar s 141 | #RANLIB= true 142 | 143 | # ------------------------------------------------------------------ stripper 144 | 145 | # This should work in all Unix systems, but you may want to add options. 146 | # 147 | STRIP= strip 148 | 149 | # ------------------------------------------------------------------ install 150 | 151 | # Locations for "make install". You may need to be root do "make install". 152 | # 153 | INSTALL_ROOT= /usr/local 154 | INSTALL_BIN= $(INSTALL_ROOT)/bin 155 | INSTALL_INC= $(INSTALL_ROOT)/include 156 | INSTALL_LIB= $(INSTALL_ROOT)/lib 157 | INSTALL_MAN= $(INSTALL_ROOT)/man/man1 158 | 159 | # You may prefer to use "install" instead of "cp" if you have it. 160 | # If you use "install", you may also want to change the permissions after -m. 161 | # 162 | INSTALL_EXEC= cp 163 | INSTALL_DATA= cp 164 | #INSTALL_EXEC= install -m 0755 165 | #INSTALL_DATA= install -m 0644 166 | 167 | # == END OF USER SETTINGS. NO NEED TO CHANGE ANYTHING BELOW THIS LINE ========= 168 | 169 | V=5.0 170 | 171 | BIN= $(LUA)/bin/macos 172 | INC= -I$(LUA)/include -I$(LUA)/src 173 | LIB= $(LUA)/lib/macos 174 | 175 | MYDEFS=-DEXTRA_CREDITS="\"Mac OS X port maintained by Cameron Kaiser: www.floodgap.com/software/macplua/\"" 176 | 177 | INCS= $(INC) $(EXTRA_INCS) 178 | DEFS= $(NUMBER) $(EXTRA_DEFS) $(MYDEFS) 179 | 180 | CFLAGS= $(MYCFLAGS) $(WARN) $(INCS) $(DEFS) 181 | 182 | # (end of config) 183 | -------------------------------------------------------------------------------- /configure: -------------------------------------------------------------------------------- 1 | #!/bin/sh 2 | 3 | # Lua does not use GNU autoconf; just edit ./config if needed to suit your 4 | # platform and then run make. 5 | 6 | # This shows the parameters currently set in ./config: 7 | make echo 8 | 9 | # If you want config as a Lua program, run "make lecho". 10 | -------------------------------------------------------------------------------- /distrib/._License.txt: -------------------------------------------------------------------------------- 1 | Mac OS X  2 RTEXTttxt -------------------------------------------------------------------------------- /distrib/License.txt: -------------------------------------------------------------------------------- 1 | 1) You can distribute applications built with Plua under any license you want; 2 | 2) You are allowed to include one copy of the Plua runtime (plua2rt.prc) with your application. You do not have to pay any royalties or any kind of fees for including the runtime with your application. You are not allowed to distribute ANY other component of Plua in addition to the Plua runtime; 3 | 3) You can distribute libraries built with libkit under any license you want; 4 | 4) You are not allowed to reverse engineer or modify any component of Plua. 5 | 6 | 7 | THIS SOFTWARE COMES WITH ABSOLUTELY NO WARRANTY, SO USE IT AT YOUR OWN RISK. 8 | 9 | Plua is Copyright (C) Marcio Migueletto de Andrade. 10 | -------------------------------------------------------------------------------- /distrib/mkmacos: -------------------------------------------------------------------------------- 1 | #!/bin/sh 2 | 3 | . ../VERSION 4 | 5 | rm -f pluac-${VERSION}.macos.zip 6 | 7 | cp ../bin/macos/plua2c macos 8 | 9 | zip pluac-${VERSION}.macos.zip License.txt macos/plua2c macos/Readme.txt 10 | 11 | exit 0 12 | -------------------------------------------------------------------------------- /doc/._logo.gif: -------------------------------------------------------------------------------- 1 | Mac OS X  2 RGIFfogle -------------------------------------------------------------------------------- /doc/contents.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | Lua: 5.0 reference manual - contents 4 | 5 | 6 | 7 | 8 |
9 |

10 | Lua 11 | Reference manual for Lua 5.0 12 |

13 | 14 | Lua 5.0 Reference Manual 15 | [ 16 | top 17 | | 18 | ps 19 | | 20 | pdf 21 | ] 22 |

23 | 24 | 25 | Copyright 26 | © 2003 Tecgraf, PUC-Rio. All rights reserved. 27 |


28 | 29 | 115 | 116 |
117 | 118 | Last update: 119 | Wed May 7 18:34:34 EST 2003 120 | 121 | 122 | 123 | 124 | -------------------------------------------------------------------------------- /doc/logo.gif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/classilla/plua2c/dac9524911237e23bdadd1c32e154cf265a24f56/doc/logo.gif -------------------------------------------------------------------------------- /doc/lua.1: -------------------------------------------------------------------------------- 1 | .\" lua.man,v 1.8 2003/04/02 00:05:20 lhf Exp 2 | .TH LUA 1 "2003/04/02 00:05:20" 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 stand-alone 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 | (see below) 31 | are executed and then 32 | the Lua program in file 33 | .I script 34 | is loaded and executed. 35 | The given 36 | .I args 37 | are available to 38 | .I script 39 | as strings in a global table named 40 | .BR arg . 41 | If these arguments contain spaces or other characters special to the shell, 42 | then they should be quoted 43 | (but note that the quotes will be removed by the shell). 44 | The arguments in 45 | .B arg 46 | start at 0, 47 | which contains the string 48 | .RI ` script '. 49 | The index of the last argument is stored in 50 | .BR "arg.n" . 51 | The arguments given in the command line before 52 | .IR script , 53 | including the name of the interpreter, 54 | are available in negative indices in 55 | .BR arg . 56 | .LP 57 | At the very start, 58 | before even handling the command line, 59 | .B lua 60 | executes the contents of the environment variable 61 | .BR LUA_INIT , 62 | if it is defined. 63 | If the value of 64 | .B LUA_INIT 65 | is of the form 66 | .RI `@ filename ', 67 | then 68 | .I filename 69 | is executed. 70 | Otherwise, the string is assumed to be a Lua statement and is executed. 71 | .LP 72 | Options start with 73 | .B \- 74 | and are described below. 75 | You can use 76 | .B "\--" 77 | to signal the end of options. 78 | .LP 79 | If no arguments are given, 80 | then 81 | .B "\-v \-i" 82 | is assumed when the standard input is a terminal; 83 | otherwise, 84 | .B "\-" 85 | is assumed. 86 | .LP 87 | In interactive mode, 88 | .B lua 89 | prompts the user, 90 | reads lines from the standard input, 91 | and executes them as they are read. 92 | If a line does not contain a complete statement, 93 | then a secondary prompt is displayed and 94 | lines are read until a complete statement is formed or 95 | a syntax error is found. 96 | So, one way to interrupt the reading of an incomplete statement is 97 | to force a syntax error: 98 | adding a 99 | .B `;' 100 | in the middle of a statement is a sure way of forcing a syntax error 101 | (except inside multiline strings and comments; these must be closed explicitly). 102 | If a line starts with 103 | .BR `=' , 104 | then 105 | .B lua 106 | displays the values of all the expressions in the remainder of the 107 | line. The expressions must be separated by commas. 108 | The primary prompt is the value of the global variable 109 | .BR _PROMPT , 110 | if this value is a string; 111 | otherwise, the default prompt is used. 112 | Similarly, the secondary prompt is the value of the global variable 113 | .BR _PROMPT2 . 114 | So, 115 | to change the prompts, 116 | set the corresponding variable to a string of your choice. 117 | You can do that after calling the interpreter 118 | or on the command line with 119 | .BR "_PROMPT" "=\'lua: \'" , 120 | for example. 121 | (Note the need for quotes, because the string contains a space.) 122 | The default prompts are ``> '' and ``>> ''. 123 | .SH OPTIONS 124 | .TP 125 | .B \- 126 | load and execute the standard input as a file, 127 | that is, 128 | not interactively, 129 | even when the standard input is a terminal. 130 | .TP 131 | .BI \-e " stat" 132 | execute statement 133 | .IR stat . 134 | You need to quote 135 | .I stat 136 | if it contains spaces, quotes, 137 | or other characters special to the shell. 138 | .TP 139 | .B \-i 140 | enter interactive mode after 141 | .I script 142 | is executed. 143 | .TP 144 | .BI \-l " file" 145 | call 146 | .BI require( file ) 147 | before executing 148 | .IR script. 149 | Typically used to load libraries 150 | (hence the letter 151 | .IR l ). 152 | .TP 153 | .B \-v 154 | show version information. 155 | .SH "SEE ALSO" 156 | .BR luac (1) 157 | .br 158 | http://www.lua.org/ 159 | .SH DIAGNOSTICS 160 | Error messages should be self explanatory. 161 | .SH AUTHORS 162 | R. Ierusalimschy, 163 | L. H. de Figueiredo, 164 | and 165 | W. Celes 166 | (lua@tecgraf.puc-rio.br) 167 | .\" EOF 168 | -------------------------------------------------------------------------------- /doc/lua.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | LUA man page 5 | 6 | 7 | 8 | 9 |

NAME

10 | lua - Lua interpreter 11 |

SYNOPSIS

12 | lua 13 | [ 14 | options 15 | ] 16 | [ 17 | script 18 | [ 19 | args 20 | ] 21 | ] 22 |

DESCRIPTION

23 | lua 24 | is the stand-alone Lua interpreter. 25 | It loads and executes Lua programs, 26 | either in textual source form or 27 | in precompiled binary form. 28 | (Precompiled binaries are output by 29 | luac, 30 | the Lua compiler.) 31 | lua 32 | can be used as a batch interpreter and also interactively. 33 |

34 | The given 35 | options 36 | (see below) 37 | are executed and then 38 | the Lua program in file 39 | script 40 | is loaded and executed. 41 | The given 42 | args 43 | are available to 44 | script 45 | as strings in a global table named 46 | arg. 47 | If these arguments contain spaces or other characters special to the shell, 48 | then they should be quoted 49 | (but note that the quotes will be removed by the shell). 50 | The arguments in 51 | arg 52 | start at 0, 53 | which contains the string 54 | `script'. 55 | The index of the last argument is stored in 56 | "arg.n". 57 | The arguments given in the command line before 58 | script, 59 | including the name of the interpreter, 60 | are available in negative indices in 61 | arg. 62 |

63 | At the very start, 64 | before even handling the command line, 65 | lua 66 | executes the contents of the environment variable 67 | LUA_INIT, 68 | if it is defined. 69 | If the value of 70 | LUA_INIT 71 | is of the form 72 | `@filename', 73 | then 74 | filename 75 | is executed. 76 | Otherwise, the string is assumed to be a Lua statement and is executed. 77 |

78 | Options start with 79 | - 80 | and are described below. 81 | You can use 82 | "--" 83 | to signal the end of options. 84 |

85 | If no arguments are given, 86 | then 87 | "-v -i" 88 | is assumed when the standard input is a terminal; 89 | otherwise, 90 | "-" 91 | is assumed. 92 |

93 | In interactive mode, 94 | lua 95 | prompts the user, 96 | reads lines from the standard input, 97 | and executes them as they are read. 98 | If a line does not contain a complete statement, 99 | then a secondary prompt is displayed and 100 | lines are read until a complete statement is formed or 101 | a syntax error is found. 102 | So, one way to interrupt the reading of an incomplete statement is 103 | to force a syntax error: 104 | adding a 105 | `;' 106 | in the middle of a statement is a sure way of forcing a syntax error 107 | (except inside multiline strings and comments; these must be closed explicitly). 108 | If a line starts with 109 | `=', 110 | then 111 | lua 112 | displays the values of all the expressions in the remainder of the 113 | line. The expressions must be separated by commas. 114 | The primary prompt is the value of the global variable 115 | _PROMPT, 116 | if this value is a string; 117 | otherwise, the default prompt is used. 118 | Similarly, the secondary prompt is the value of the global variable 119 | _PROMPT2. 120 | So, 121 | to change the prompts, 122 | set the corresponding variable to a string of your choice. 123 | You can do that after calling the interpreter 124 | or on the command line with 125 | "_PROMPT" "=\'lua: \'", 126 | for example. 127 | (Note the need for quotes, because the string contains a space.) 128 | The default prompts are ``> '' and ``>> ''. 129 |

OPTIONS

130 |

131 | - 132 | load and execute the standard input as a file, 133 | that is, 134 | not interactively, 135 | even when the standard input is a terminal. 136 |

137 | -e "stat" 138 | execute statement 139 | stat. 140 | You need to quote 141 | stat 142 | if it contains spaces, quotes, 143 | or other characters special to the shell. 144 |

145 | -i 146 | enter interactive mode after 147 | script 148 | is executed. 149 |

150 | -l "file" 151 | call 152 | require( file) 153 | before executing 154 | script. 155 | Typically used to load libraries 156 | (hence the letter 157 | l). 158 |

159 | -v 160 | show version information. 161 |

SEE ALSO

162 | luac(1) 163 |
164 | http://www.lua.org/ 165 |

DIAGNOSTICS

166 | Error messages should be self explanatory. 167 |

AUTHORS

168 | R. Ierusalimschy, 169 | L. H. de Figueiredo, 170 | and 171 | W. Celes 172 | (lua AT tecgraf.puc-rio.br) 173 | 174 | 175 | 176 | -------------------------------------------------------------------------------- /doc/luac.1: -------------------------------------------------------------------------------- 1 | .\" luac.man,v 1.25 2002/12/13 11:45:12 lhf Exp 2 | .TH LUAC 1 "2002/12/13 11:45:12" 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 that can be latter loaded and executed. 17 | .LP 18 | The main advantages of precompiling chunks are: 19 | faster loading, 20 | protecting source code from user changes, 21 | and 22 | off-line syntax checking. 23 | .LP 24 | Pre-compiling 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 | .LP 29 | .B luac 30 | produces a single output file containing the bytecodes 31 | for all source files given. 32 | By default, 33 | the output file is named 34 | .BR luac.out , 35 | but you can change this with the 36 | .B \-o 37 | option. 38 | .LP 39 | The binary files created by 40 | .B luac 41 | are portable to all architectures with the same word size. 42 | This means that 43 | binary files created on a 32-bit platform (such as Intel) 44 | can be read without change in another 32-bit platform (such as Sparc), 45 | even if the byte order (``endianness'') is different. 46 | On the other hand, 47 | binary files created on a 16-bit platform cannot be read in a 32-bit platform, 48 | nor vice-versa. 49 | .LP 50 | In the command line, 51 | you can mix 52 | text files containing Lua source and 53 | binary files containing precompiled chunks. 54 | This is useful to combine several precompiled chunks, 55 | even from different (but compatible) platforms, 56 | into a single precompiled chunk. 57 | .LP 58 | You can use 59 | .B "\-" 60 | to indicate the standard input as a source file 61 | and 62 | .B "\--" 63 | to signal the end of options 64 | (that is, 65 | all remaining arguments will be treated as files even if they start with 66 | .BR "\-" ). 67 | .LP 68 | The internal format of the binary files produced by 69 | .B luac 70 | is likely to change when a new version of Lua is released. 71 | So, 72 | save the source files of all Lua programs that you precompile. 73 | .LP 74 | .SH OPTIONS 75 | Options must be separate. 76 | .TP 77 | .B \-l 78 | produce a listing of the compiled bytecode for Lua's virtual machine. 79 | Listing bytecodes is useful to learn about Lua's virtual machine. 80 | If no files are given, then 81 | .B luac 82 | loads 83 | .B luac.out 84 | and lists its contents. 85 | .TP 86 | .BI \-o " file" 87 | output to 88 | .IR file , 89 | instead of the default 90 | .BR luac.out . 91 | The output file may be a source file because 92 | all files are loaded before the output file is written. 93 | Be careful not to overwrite precious files. 94 | .TP 95 | .B \-p 96 | load files but do not generate any output file. 97 | Used mainly for syntax checking and for testing precompiled chunks: 98 | corrupted files will probably generate errors when loaded. 99 | Lua always performs a thorough integrity test on precompiled chunks. 100 | Bytecode that passes this test is completely safe, 101 | in the sense that it will not break the interpreter. 102 | However, 103 | there is no guarantee that such code does anything sensible. 104 | (None can be given, because the halting problem is unsolvable.) 105 | If no files are given, then 106 | .B luac 107 | loads 108 | .B luac.out 109 | and tests its contents. 110 | No messages are displayed if the file passes the integrity test. 111 | .TP 112 | .B \-s 113 | strip debug information before writing the output file. 114 | This saves some space in very large chunks, 115 | but if errors occur when running these chunks, 116 | then the error messages may not contain the full information they usually do 117 | (line numbers and names of locals are lost). 118 | .TP 119 | .B \-v 120 | show version information. 121 | .SH FILES 122 | .TP 15 123 | .B luac.out 124 | default output file 125 | .SH "SEE ALSO" 126 | .BR lua (1) 127 | .br 128 | http://www.lua.org/ 129 | .SH DIAGNOSTICS 130 | Error messages should be self explanatory. 131 | .SH AUTHORS 132 | L. H. de Figueiredo, 133 | R. Ierusalimschy and 134 | W. Celes 135 | (lua@tecgraf.puc-rio.br) 136 | .\" EOF 137 | -------------------------------------------------------------------------------- /doc/luac.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | LUAC man page 5 | 6 | 7 | 8 | 9 |

NAME

10 | luac - Lua compiler 11 |

SYNOPSIS

12 | luac 13 | [ 14 | options 15 | ] [ 16 | filenames 17 | ] 18 |

DESCRIPTION

19 | luac 20 | is the Lua compiler. 21 | It translates programs written in the Lua programming language 22 | into binary files that can be latter loaded and executed. 23 |

24 | The main advantages of precompiling chunks are: 25 | faster loading, 26 | protecting source code from user changes, 27 | and 28 | off-line syntax checking. 29 |

30 | Pre-compiling does not imply faster execution 31 | because in Lua chunks are always compiled into bytecodes before being executed. 32 | luac 33 | simply allows those bytecodes to be saved in a file for later execution. 34 |

35 | luac 36 | produces a single output file containing the bytecodes 37 | for all source files given. 38 | By default, 39 | the output file is named 40 | luac.out, 41 | but you can change this with the 42 | -o 43 | option. 44 |

45 | The binary files created by 46 | luac 47 | are portable to all architectures with the same word size. 48 | This means that 49 | binary files created on a 32-bit platform (such as Intel) 50 | can be read without change in another 32-bit platform (such as Sparc), 51 | even if the byte order (``endianness'') is different. 52 | On the other hand, 53 | binary files created on a 16-bit platform cannot be read in a 32-bit platform, 54 | nor vice-versa. 55 |

56 | In the command line, 57 | you can mix 58 | text files containing Lua source and 59 | binary files containing precompiled chunks. 60 | This is useful to combine several precompiled chunks, 61 | even from different (but compatible) platforms, 62 | into a single precompiled chunk. 63 |

64 | You can use 65 | "-" 66 | to indicate the standard input as a source file 67 | and 68 | "--" 69 | to signal the end of options 70 | (that is, 71 | all remaining arguments will be treated as files even if they start with 72 | "-"). 73 |

74 | The internal format of the binary files produced by 75 | luac 76 | is likely to change when a new version of Lua is released. 77 | So, 78 | save the source files of all Lua programs that you precompile. 79 |

80 |

OPTIONS

81 | Options must be separate. 82 |

83 | -l 84 | produce a listing of the compiled bytecode for Lua's virtual machine. 85 | Listing bytecodes is useful to learn about Lua's virtual machine. 86 | If no files are given, then 87 | luac 88 | loads 89 | luac.out 90 | and lists its contents. 91 |

92 | -o "file" 93 | output to 94 | file, 95 | instead of the default 96 | luac.out. 97 | The output file may be a source file because 98 | all files are loaded before the output file is written. 99 | Be careful not to overwrite precious files. 100 |

101 | -p 102 | load files but do not generate any output file. 103 | Used mainly for syntax checking and for testing precompiled chunks: 104 | corrupted files will probably generate errors when loaded. 105 | Lua always performs a thorough integrity test on precompiled chunks. 106 | Bytecode that passes this test is completely safe, 107 | in the sense that it will not break the interpreter. 108 | However, 109 | there is no guarantee that such code does anything sensible. 110 | (None can be given, because the halting problem is unsolvable.) 111 | If no files are given, then 112 | luac 113 | loads 114 | luac.out 115 | and tests its contents. 116 | No messages are displayed if the file passes the integrity test. 117 |

118 | -s 119 | strip debug information before writing the output file. 120 | This saves some space in very large chunks, 121 | but if errors occur when running these chunks, 122 | then the error messages may not contain the full information they usually do 123 | (line numbers and names of locals are lost). 124 |

125 | -v 126 | show version information. 127 |

FILES

128 |

129 | luac.out 130 | default output file 131 |

SEE ALSO

132 | lua(1) 133 |
134 | http://www.lua.org/ 135 |

DIAGNOSTICS

136 | Error messages should be self explanatory. 137 |

AUTHORS

138 | L. H. de Figueiredo, 139 | R. Ierusalimschy and 140 | W. Celes 141 | (lua AT tecgraf.puc-rio.br) 142 | 143 | 144 | 145 | -------------------------------------------------------------------------------- /doc/readme.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | Lua documentation 4 | 5 | 6 | 7 | 8 |
9 |

10 | Lua 11 | Documentation 12 |

13 | 14 | 27 | 28 |
29 | 30 | Last update: 31 | Thu Mar 11 23:08:56 BRT 2004 32 | 33 | 34 | 35 | 36 | -------------------------------------------------------------------------------- /etc/Makefile: -------------------------------------------------------------------------------- 1 | # makefile for Lua etc 2 | 3 | LUA= .. 4 | 5 | include $(LUA)/config 6 | 7 | LIBLUA=$(LIB)/liblua.a 8 | ALL= bin2c min trace noparser luab 9 | 10 | all: 11 | @echo 'choose a target:' $(ALL) 12 | 13 | bin2c: bin2c.c 14 | $(CC) $(CFLAGS) -o $@ $@.c 15 | 16 | min: min.c $(LIBLUA) 17 | $(CC) $(CFLAGS) -o $@ $@.c -L$(LIB) -llua 18 | 19 | trace: trace.c $(LIBLUA) 20 | $(CC) -g $(CFLAGS) -o $@ $@.c -L$(LIB) -llua -llualib $(EXTRA_LIBS) 21 | 22 | noparser: noparser.c 23 | $(CC) $(CFLAGS) -I$(LUA)/src -o $@.o -c $@.c 24 | 25 | luab: noparser $(LIBLUA) 26 | cc -o $@ noparser.o $(LUA)/src/lua/lua.o -L$(LIB) -llua -llualib $(EXTRA_LIBS) 27 | $(BIN)/luac $(LUA)/test/hello.lua 28 | $@ luac.out 29 | -$@ -e'a=1' 30 | 31 | flat: 32 | cd ..; mkdir flat; mv include/*.h src/*.[ch] src/*/*.[ch] flat 33 | 34 | $(LIBLUA): 35 | cd ../src; $(MAKE) 36 | 37 | clean: 38 | rm -f $(ALL) a.out core *.o luac.out 39 | 40 | luser_tests.h: RCS/ltests.h,v 41 | co -q -M ltests.h 42 | mv -f ltests.h $@ 43 | -------------------------------------------------------------------------------- /etc/README: -------------------------------------------------------------------------------- 1 | This directory contains some useful files and code. 2 | Unlike the code in ../src, everything here is in the public domain. 3 | 4 | bin2c.c 5 | This program converts files to byte arrays that are automatically run 6 | with lua_dobuffer. This allows C programs to include all necessary Lua 7 | code, even in precompiled form. Even if the code is included in source 8 | form, bin2c is useful because it avoids the hassle of having to quote 9 | special characters in C strings. 10 | Example of usage: Run bin2c file1 file2 ... > init.h. Then, in your 11 | C program, just do #include "init.h" anywhere in the *body* of a 12 | function. This will be equivalent to calling 13 | lua_dofile(L,"file1"); lua_dofile(L,"file2"); ... 14 | Note that the Lua state is called "L". If you use a different name, 15 | say "mystate", just #define L mystate before you #include "init.h". 16 | 17 | compat.lua 18 | A compatibility module for Lua 4.0 functions. 19 | 20 | doall.lua 21 | Emulate the command line behaviour of Lua 4.0 22 | 23 | lua.ico 24 | A Lua icon for Windows. 25 | Drawn by hand by Markus Gritsch . 26 | 27 | lua.magic 28 | Data for teaching file(1) about Lua precompiled chunks. 29 | 30 | lua.xpm 31 | The same icon as lua.ico, but in XPM format. 32 | It was converted with ImageMagick by Andy Tai . 33 | 34 | luser_number.h 35 | Number type configuration for Lua core. 36 | 37 | luser_tests.h 38 | Self-test configuration for Lua core. 39 | 40 | min.c 41 | A minimal Lua interpreter. 42 | Good for learning and for starting your own. 43 | 44 | noparser.c 45 | Linking with noparser.o avoids loading the parsing modules in lualib.a. 46 | Do "make luab" to build a sample Lua intepreter that does not parse 47 | Lua programs, only loads precompiled programs. 48 | 49 | saconfig.c 50 | Configuration for Lua interpreter. 51 | 52 | trace.c 53 | A simple execution tracer. 54 | An example of how to use the debug hooks in C. 55 | -------------------------------------------------------------------------------- /etc/bin2c.c: -------------------------------------------------------------------------------- 1 | /* 2 | * bin2c.c 3 | * convert files to byte arrays for automatic loading with lua_dobuffer 4 | * Luiz Henrique de Figueiredo (lhf@tecgraf.puc-rio.br) 5 | * 02 Apr 2003 20:44:31 6 | */ 7 | 8 | #include 9 | #include 10 | #include 11 | 12 | static void dump(FILE* f, int n) 13 | { 14 | printf("static const unsigned char B%d[]={\n",n); 15 | for (n=1;;n++) 16 | { 17 | int c=getc(f); 18 | if (c==EOF) break; 19 | printf("%3u,",c); 20 | if (n==20) { putchar('\n'); n=0; } 21 | } 22 | printf("\n};\n\n"); 23 | } 24 | 25 | static void fdump(const char* fn, int n) 26 | { 27 | FILE* f= fopen(fn,"rb"); /* must open in binary mode */ 28 | if (f==NULL) 29 | { 30 | fprintf(stderr,"bin2c: cannot open "); 31 | perror(fn); 32 | exit(1); 33 | } 34 | else 35 | { 36 | printf("/* %s */\n",fn); 37 | dump(f,n); 38 | fclose(f); 39 | } 40 | } 41 | 42 | static void emit(const char* fn, int n) 43 | { 44 | printf(" lua_dobuffer(L,(const char*)B%d,sizeof(B%d),\"%s\");\n",n,n,fn); 45 | } 46 | 47 | int main(int argc, char* argv[]) 48 | { 49 | printf("/* code automatically generated by bin2c -- DO NOT EDIT */\n"); 50 | printf("{\n"); 51 | if (argc<2) 52 | { 53 | dump(stdin,0); 54 | emit("=stdin",0); 55 | } 56 | else 57 | { 58 | int i; 59 | printf("/* #include'ing this file in a C program is equivalent to calling\n"); 60 | for (i=1; i protect(f, err) 25 | -- loadfile 26 | -- loadstring 27 | 28 | -- rawget 29 | -- rawset 30 | 31 | -- getargs = Main.getargs ?? 32 | 33 | 34 | function do_ (f, err) 35 | if not f then print(err); return end 36 | local a,b = pcall(f) 37 | if not a then print(b); return nil 38 | else return b or true 39 | end 40 | end 41 | 42 | function dostring(s) return do_(loadstring(s)) end 43 | -- function dofile(s) return do_(loadfile(s)) end 44 | 45 | ------------------------------------------------------------------- 46 | -- Table library 47 | local tab = table 48 | foreach = tab.foreach 49 | foreachi = tab.foreachi 50 | getn = tab.getn 51 | tinsert = tab.insert 52 | tremove = tab.remove 53 | sort = tab.sort 54 | 55 | ------------------------------------------------------------------- 56 | -- Debug library 57 | local dbg = debug 58 | getinfo = dbg.getinfo 59 | getlocal = dbg.getlocal 60 | setcallhook = function () error"`setcallhook' is deprecated" end 61 | setlinehook = function () error"`setlinehook' is deprecated" end 62 | setlocal = dbg.setlocal 63 | 64 | ------------------------------------------------------------------- 65 | -- math library 66 | local math = math 67 | abs = math.abs 68 | acos = function (x) return math.deg(math.acos(x)) end 69 | asin = function (x) return math.deg(math.asin(x)) end 70 | atan = function (x) return math.deg(math.atan(x)) end 71 | atan2 = function (x,y) return math.deg(math.atan2(x,y)) end 72 | ceil = math.ceil 73 | cos = function (x) return math.cos(math.rad(x)) end 74 | deg = math.deg 75 | exp = math.exp 76 | floor = math.floor 77 | frexp = math.frexp 78 | ldexp = math.ldexp 79 | log = math.log 80 | log10 = math.log10 81 | max = math.max 82 | min = math.min 83 | mod = math.mod 84 | PI = math.pi 85 | --??? pow = math.pow 86 | rad = math.rad 87 | random = math.random 88 | randomseed = math.randomseed 89 | sin = function (x) return math.sin(math.rad(x)) end 90 | sqrt = math.sqrt 91 | tan = function (x) return math.tan(math.rad(x)) end 92 | 93 | ------------------------------------------------------------------- 94 | -- string library 95 | local str = string 96 | strbyte = str.byte 97 | strchar = str.char 98 | strfind = str.find 99 | format = str.format 100 | gsub = str.gsub 101 | strlen = str.len 102 | strlower = str.lower 103 | strrep = str.rep 104 | strsub = str.sub 105 | strupper = str.upper 106 | 107 | ------------------------------------------------------------------- 108 | -- os library 109 | clock = os.clock 110 | date = os.date 111 | difftime = os.difftime 112 | execute = os.execute --? 113 | exit = os.exit 114 | getenv = os.getenv 115 | remove = os.remove 116 | rename = os.rename 117 | setlocale = os.setlocale 118 | time = os.time 119 | tmpname = os.tmpname 120 | 121 | ------------------------------------------------------------------- 122 | -- compatibility only 123 | getglobal = function (n) return _G[n] end 124 | setglobal = function (n,v) _G[n] = v end 125 | 126 | ------------------------------------------------------------------- 127 | 128 | local io, tab = io, table 129 | 130 | -- IO library (files) 131 | _STDIN = io.stdin 132 | _STDERR = io.stderr 133 | _STDOUT = io.stdout 134 | _INPUT = io.stdin 135 | _OUTPUT = io.stdout 136 | seek = io.stdin.seek -- sick ;-) 137 | tmpfile = io.tmpfile 138 | closefile = io.close 139 | openfile = io.open 140 | 141 | function flush (f) 142 | if f then f:flush() 143 | else _OUTPUT:flush() 144 | end 145 | end 146 | 147 | function readfrom (name) 148 | if name == nil then 149 | local f, err, cod = io.close(_INPUT) 150 | _INPUT = io.stdin 151 | return f, err, cod 152 | else 153 | local f, err, cod = io.open(name, "r") 154 | _INPUT = f or _INPUT 155 | return f, err, cod 156 | end 157 | end 158 | 159 | function writeto (name) 160 | if name == nil then 161 | local f, err, cod = io.close(_OUTPUT) 162 | _OUTPUT = io.stdout 163 | return f, err, cod 164 | else 165 | local f, err, cod = io.open(name, "w") 166 | _OUTPUT = f or _OUTPUT 167 | return f, err, cod 168 | end 169 | end 170 | 171 | function appendto (name) 172 | local f, err, cod = io.open(name, "a") 173 | _OUTPUT = f or _OUTPUT 174 | return f, err, cod 175 | end 176 | 177 | function read (...) 178 | local f = _INPUT 179 | if type(arg[1]) == 'userdata' then 180 | f = tab.remove(arg, 1) 181 | end 182 | return f:read(unpack(arg)) 183 | end 184 | 185 | function write (...) 186 | local f = _OUTPUT 187 | if type(arg[1]) == 'userdata' then 188 | f = tab.remove(arg, 1) 189 | end 190 | return f:write(unpack(arg)) 191 | end 192 | 193 | -------------------------------------------------------------------------------- /etc/doall.lua: -------------------------------------------------------------------------------- 1 | -- emulate the command line behaviour of Lua 4.0 2 | -- usage: lua doall.lua f1.lua f2.lua f3.lua ... 3 | 4 | for i=1,table.getn(arg) do 5 | dofile(arg[i]) 6 | end 7 | -------------------------------------------------------------------------------- /etc/lua.ico: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/classilla/plua2c/dac9524911237e23bdadd1c32e154cf265a24f56/etc/lua.ico -------------------------------------------------------------------------------- /etc/lua.magic: -------------------------------------------------------------------------------- 1 | 2 | # Lua precompiled files. Versions 2.3 and 4.1 were never officially released. 3 | 0 string \33Lua precompiled chunk for Lua 4 | >4 byte 0x23 2.3* 5 | >4 byte 0x24 2.4 6 | >4 byte 0x25 2.5 7 | >4 byte 0x30 3.0 8 | >4 byte 0x31 3.1 9 | >4 byte 0x32 3.2 10 | >4 byte 0x40 4.0 11 | >4 byte 0x41 4.1* 12 | >4 byte 0x50 5.0 13 | -------------------------------------------------------------------------------- /etc/lua.xpm: -------------------------------------------------------------------------------- 1 | /* XPM */ 2 | static char *magick[] = { 3 | /* columns rows colors chars-per-pixel */ 4 | "32 32 6 1", 5 | " c Gray0", 6 | ". c #000000008080", 7 | "X c #808080808080", 8 | "o c #c0c0c0c0c0c0", 9 | "O c Gray100", 10 | "+ c None", 11 | /* pixels */ 12 | "++++++++++++++++++++++++++ooo+++", 13 | "++++++++++++++++++++++++oX...Xo+", 14 | "++++++++++++++++++++++++X.....X+", 15 | "+++++++++++++++++++++++o.......o", 16 | "+++++++++XX......XX++++o.......o", 17 | "+++++++X............X++o.......o", 18 | "+++++o................o+X.....X+", 19 | "++++X..................XoX...Xo+", 20 | "+++X..............XXX...X+ooo+++", 21 | "++o.............XoOOOoX..o++++++", 22 | "++..............oOOOOOo...++++++", 23 | "+X.............XOOOOOOOX..X+++++", 24 | "+..............XOOOOOOOX...+++++", 25 | "X..............XOOOOOOOX...X++++", 26 | "X...............oOOOOOo....X++++", 27 | "................XoOOOoX.....++++", 28 | "....XO............XXX.......++++", 29 | "....XO......................++++", 30 | "....XO.....OX..OX.XOOOo.....++++", 31 | "....XO.....OX..OX.OoXXOX....++++", 32 | "....XO.....OX..OX....XOX....++++", 33 | "X...XO.....OX..OX..OOoOX...X++++", 34 | "X...XO.....OX..OX.OX..OX...X++++", 35 | "+...XOXXXX.OoXoOX.OXXXOX...+++++", 36 | "+X..XOOOOO.XOOXOX.XOOOXo..X+++++", 37 | "++........................++++++", 38 | "++o......................o++++++", 39 | "+++X....................X+++++++", 40 | "++++X..................X++++++++", 41 | "+++++o................o+++++++++", 42 | "+++++++X............X+++++++++++", 43 | "+++++++++XX......XX+++++++++++++" 44 | }; 45 | -------------------------------------------------------------------------------- /etc/luser_number.h: -------------------------------------------------------------------------------- 1 | /* luser_number.h -- number type configuration for Lua core 2 | * 3 | * #define LUA_USER_H to this file and #define one of USE_* below 4 | */ 5 | 6 | #ifdef USE_DOUBLE 7 | #define LUA_NUMBER double 8 | #define LUA_NUMBER_SCAN "%lf" 9 | #define LUA_NUMBER_FMT "%.14g" 10 | #endif 11 | 12 | #ifdef USE_FLOAT 13 | #define LUA_NUMBER float 14 | #define LUA_NUMBER_SCAN "%f" 15 | #define LUA_NUMBER_FMT "%.5g" 16 | #endif 17 | 18 | #ifdef USE_LONG 19 | #define LUA_NUMBER long 20 | #define LUA_NUMBER_SCAN "%ld" 21 | #define LUA_NUMBER_FMT "%ld" 22 | #define lua_str2number(s,p) strtol((s), (p), 10) 23 | #endif 24 | 25 | #ifdef USE_INT 26 | #define LUA_NUMBER int 27 | #define LUA_NUMBER_SCAN "%d" 28 | #define LUA_NUMBER_FMT "%d" 29 | #define lua_str2number(s,p) ((int) strtol((s), (p), 10)) 30 | #endif 31 | 32 | #ifdef USE_FASTROUND 33 | #define lua_number2int(i,d) __asm__("fldl %1\nfistpl %0":"=m"(i):"m"(d)) 34 | #endif 35 | -------------------------------------------------------------------------------- /etc/luser_tests.h: -------------------------------------------------------------------------------- 1 | /* 2 | ** $Id: ltests.h,v 1.20 2002/12/04 17:29:05 roberto Exp $ 3 | ** Internal Header for Debugging of the Lua Implementation 4 | ** See Copyright Notice in lua.h 5 | */ 6 | 7 | #ifndef ltests_h 8 | #define ltests_h 9 | 10 | 11 | #include 12 | 13 | 14 | #define LUA_DEBUG 15 | 16 | #define LUA_OPNAMES 17 | 18 | #undef NDEBUG 19 | #include 20 | #define lua_assert(c) assert(c) 21 | #define check_exp(c,e) (lua_assert(c), (e)) 22 | #define api_check(L, o) lua_assert(o) 23 | 24 | 25 | /* to avoid warnings, and to make sure value is really unused */ 26 | #define UNUSED(x) (x=0, (void)(x)) 27 | 28 | 29 | /* memory allocator control variables */ 30 | extern unsigned long memdebug_numblocks; 31 | extern unsigned long memdebug_total; 32 | extern unsigned long memdebug_maxmem; 33 | extern unsigned long memdebug_memlimit; 34 | 35 | 36 | #define l_realloc(b, os, s) debug_realloc(b, os, s) 37 | #define l_free(b, os) debug_realloc(b, os, 0) 38 | 39 | void *debug_realloc (void *block, size_t oldsize, size_t size); 40 | 41 | 42 | 43 | /* test for lock/unlock */ 44 | extern int islocked; 45 | #define LUA_USERSTATE int * 46 | #define getlock(l) (*(cast(LUA_USERSTATE *, l) - 1)) 47 | #define lua_userstateopen(l) if (l != NULL) getlock(l) = &islocked; 48 | #define lua_lock(l) lua_assert((*getlock(l))++ == 0) 49 | #define lua_unlock(l) lua_assert(--(*getlock(l)) == 0) 50 | 51 | 52 | int luaB_opentests (lua_State *L); 53 | 54 | #define LUA_EXTRALIBS { "tests", luaB_opentests }, 55 | 56 | 57 | /* real main will be defined at `ltests.c' */ 58 | int l_main (int argc, char *argv[]); 59 | #define main l_main 60 | 61 | 62 | 63 | /* change some sizes to give some bugs a chance */ 64 | 65 | #define LUAL_BUFFERSIZE 27 66 | #define MINSTRTABSIZE 2 67 | 68 | #endif 69 | -------------------------------------------------------------------------------- /etc/min.c: -------------------------------------------------------------------------------- 1 | /* 2 | * min.c -- a minimal Lua interpreter 3 | * loads stdin only with minimal error handling. 4 | * no interaction, and no standard library, only a "print" function. 5 | */ 6 | 7 | #include 8 | #include "lua.h" 9 | 10 | static int print(lua_State *L) 11 | { 12 | int n=lua_gettop(L); 13 | int i; 14 | for (i=1; i<=n; i++) 15 | { 16 | if (i>1) printf("\t"); 17 | if (lua_isstring(L,i)) 18 | printf("%s",lua_tostring(L,i)); 19 | else if (lua_isnil(L,i)) 20 | printf("%s","nil"); 21 | else if (lua_isboolean(L,i)) 22 | printf("%s",lua_toboolean(L,i) ? "true" : "false"); 23 | else 24 | printf("%s:%p",lua_typename(L,lua_type(L,i)),lua_topointer(L,i)); 25 | } 26 | printf("\n"); 27 | return 0; 28 | } 29 | 30 | static const char *getF(lua_State *L, void *ud, size_t *size) 31 | { 32 | FILE *f=(FILE *)ud; 33 | static char buff[512]; 34 | if (feof(f)) return NULL; 35 | *size=fread(buff,1,sizeof(buff),f); 36 | return (*size>0) ? buff : NULL; 37 | } 38 | 39 | int main(void) 40 | { 41 | lua_State *L=lua_open(); 42 | lua_register(L,"print",print); 43 | if (lua_load(L,getF,stdin,"=stdin") || lua_pcall(L,0,0,0)) 44 | fprintf(stderr,"%s\n",lua_tostring(L,-1)); 45 | return 0; 46 | } 47 | -------------------------------------------------------------------------------- /etc/noparser.c: -------------------------------------------------------------------------------- 1 | /* 2 | * The code below can be used to make a Lua core that does not contain the 3 | * parsing modules (lcode, llex, lparser), which represent 35% of the total core. 4 | * You'll only be able to load binary files and strings, precompiled with luac. 5 | * (Of course, you'll have to build luac with the original parsing modules!) 6 | * 7 | * To use this module, simply compile it ("make noparser" does that) and 8 | * list its object file before the Lua libraries. The linker should then not 9 | * load the parsing modules. To try it, do "make luab". 10 | */ 11 | 12 | #include "llex.h" 13 | #include "lparser.h" 14 | #include "lzio.h" 15 | 16 | void luaX_init (lua_State *L) { 17 | UNUSED(L); 18 | } 19 | 20 | Proto *luaY_parser (lua_State *L, ZIO *z, Mbuffer *buff) { 21 | UNUSED(z); 22 | UNUSED(buff); 23 | lua_pushstring(L,"parser not loaded"); 24 | lua_error(L); 25 | return NULL; 26 | } 27 | -------------------------------------------------------------------------------- /etc/saconfig.c: -------------------------------------------------------------------------------- 1 | /* sa-config.c -- configuration for stand-alone Lua interpreter 2 | * 3 | * #define LUA_USERCONFIG to this file 4 | * 5 | * Here are the features that can be customized using #define: 6 | * 7 | *** Line edit and history: 8 | * #define USE_READLINE to use the GNU readline library. 9 | * 10 | * To use another library for this, use the code below as a start. 11 | * Make sure you #define lua_readline and lua_saveline accordingly. 12 | * If you do not #define lua_readline, you'll get a version based on fgets 13 | * that uses a static buffer of size MAXINPUT. 14 | * 15 | * 16 | *** Static Lua libraries to be loaded at startup: 17 | * #define lua_userinit(L) to a Lua function that loads libraries; typically 18 | * #define lua_userinit(L) openstdlibs(L);myinit(L) 19 | * or 20 | * #define lua_userinit(L) myinit(L) 21 | * 22 | * Another way is to add the prototypes of the init functions here and 23 | * #define LUA_EXTRALIBS accordingly. For example, 24 | * #define LUA_EXTRALIBS {"mylib","luaopen_mylib"}, 25 | * Note the ending comma! 26 | * 27 | * 28 | *** Prompts: 29 | * The stand-alone Lua interpreter uses two prompts: PROMPT and PROMPT2. 30 | * PROMPT is the primary prompt, shown when the intepreter is ready to receive 31 | * a new statement. PROMPT2 is the secondary prompt, shown while a statement 32 | * is being entered but is still incomplete. 33 | * 34 | * 35 | *** Program name: 36 | * Error messages usually show argv[0] as a program name. In systems that do 37 | * not give a valid string as argv[0], error messages show PROGNAME instead. 38 | * 39 | * 40 | */ 41 | 42 | #ifdef USE_READLINE 43 | /* 44 | * This section implements of lua_readline and lua_saveline for lua.c using 45 | * the GNU readline and history libraries. It should also work with drop-in 46 | * replacements such as editline and libedit (you may have to include 47 | * different headers, though). 48 | * 49 | */ 50 | 51 | #define lua_readline myreadline 52 | #define lua_saveline mysaveline 53 | 54 | #include 55 | #include 56 | #include 57 | 58 | static int myreadline (lua_State *L, const char *prompt) { 59 | char *s=readline(prompt); 60 | if (s==NULL) 61 | return 0; 62 | else { 63 | lua_pushstring(L,s); 64 | lua_pushliteral(L,"\n"); 65 | lua_concat(L,2); 66 | free(s); 67 | return 1; 68 | } 69 | } 70 | 71 | static void mysaveline (lua_State *L, const char *s) { 72 | const char *p; 73 | for (p=s; isspace(*p); p++) 74 | ; 75 | if (*p!=0) { 76 | size_t n=strlen(s)-1; 77 | if (s[n]!='\n') 78 | add_history(s); 79 | else { 80 | lua_pushlstring(L,s,n); 81 | s=lua_tostring(L,-1); 82 | add_history(s); 83 | lua_remove(L,-1); 84 | } 85 | } 86 | } 87 | #endif 88 | -------------------------------------------------------------------------------- /etc/trace.c: -------------------------------------------------------------------------------- 1 | /* 2 | * trace.c -- a simple execution tracer for Lua 3 | */ 4 | 5 | #include 6 | #include 7 | #include "lua.h" 8 | #include "lualib.h" 9 | #include "lauxlib.h" 10 | 11 | static FILE* LOG; /* log file */ 12 | static int I=0; /* indentation level */ 13 | 14 | static void hook(lua_State *L, lua_Debug *ar) 15 | { 16 | const char* s=""; 17 | switch (ar->event) 18 | { 19 | case LUA_HOOKTAILRET: ar->event=LUA_HOOKRET; 20 | case LUA_HOOKRET: s="return"; break; 21 | case LUA_HOOKCALL: s="call"; break; 22 | case LUA_HOOKLINE: s="line"; break; 23 | default: break; 24 | } 25 | fprintf(LOG,"[%d]\t%*s%s\t-- %d\n",I,I,"",s,ar->currentline); 26 | if (ar->event==LUA_HOOKCALL) ++I; else if (ar->event==LUA_HOOKRET) --I; 27 | } 28 | 29 | static void start_trace(lua_State *L, FILE* logfile) 30 | { 31 | lua_sethook(L,hook,LUA_MASKCALL | LUA_MASKRET | LUA_MASKLINE, 0); 32 | LOG=logfile; 33 | } 34 | 35 | static void stop_trace(lua_State *L) 36 | { 37 | lua_sethook(L,NULL,0,0); 38 | fclose(LOG); 39 | } 40 | 41 | int main(void) 42 | { 43 | int rc; 44 | lua_State *L=lua_open(); 45 | lua_baselibopen(L); 46 | lua_tablibopen(L); 47 | lua_iolibopen(L); 48 | lua_strlibopen(L); 49 | lua_mathlibopen(L); 50 | lua_dblibopen(L); 51 | start_trace(L,stderr); 52 | rc=lua_dofile(L,NULL); 53 | stop_trace(L); 54 | return rc; 55 | } 56 | -------------------------------------------------------------------------------- /include/Makefile: -------------------------------------------------------------------------------- 1 | # makefile for Lua distribution (includes) 2 | 3 | LUA= .. 4 | 5 | include $(LUA)/config 6 | 7 | SRCS= lua.h lualib.h lauxlib.h 8 | 9 | all: 10 | 11 | clean: 12 | 13 | co: 14 | co -q -f -M $(SRCS) 15 | 16 | klean: clean 17 | rm -f $(SRCS) 18 | -------------------------------------------------------------------------------- /include/compat.h: -------------------------------------------------------------------------------- 1 | #ifndef compat_h 2 | #define compat_h 3 | 4 | #include "lua.h" 5 | #include "lstate.h" 6 | 7 | void lua_pushclosure(lua_State *L, void *p); 8 | void lua_pushtable(lua_State *L, void *p); 9 | 10 | #endif 11 | -------------------------------------------------------------------------------- /include/lauxlib.h: -------------------------------------------------------------------------------- 1 | /* 2 | ** $Id: lauxlib.h,v 1.60 2003/04/03 13:35:34 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 | #ifndef LUALIB_API 19 | #define LUALIB_API LUA_API 20 | #endif 21 | 22 | 23 | 24 | typedef struct luaL_reg { 25 | const char *name; 26 | lua_CFunction func; 27 | } luaL_reg; 28 | 29 | 30 | LUALIB_API void luaL_openlib (lua_State *L, const char *libname, 31 | const luaL_reg *l, int nup); 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 int luaL_typerror (lua_State *L, int narg, const char *tname); 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, size_t *l); 37 | LUALIB_API const char *luaL_optlstring (lua_State *L, int numArg, 38 | const char *def, size_t *l); 39 | LUALIB_API lua_Number luaL_checknumber (lua_State *L, int numArg); 40 | LUALIB_API lua_Number luaL_optnumber (lua_State *L, int nArg, lua_Number def); 41 | 42 | LUALIB_API void luaL_checkstack (lua_State *L, int sz, const char *msg); 43 | LUALIB_API void luaL_checktype (lua_State *L, int narg, int t); 44 | LUALIB_API void luaL_checkany (lua_State *L, int narg); 45 | 46 | LUALIB_API int luaL_newmetatable (lua_State *L, const char *tname); 47 | LUALIB_API void luaL_getmetatable (lua_State *L, const char *tname); 48 | LUALIB_API void *luaL_checkudata (lua_State *L, int ud, const char *tname); 49 | 50 | LUALIB_API void luaL_where (lua_State *L, int lvl); 51 | LUALIB_API int luaL_error (lua_State *L, const char *fmt, ...); 52 | 53 | LUALIB_API int luaL_findstring (const char *st, const char *const lst[]); 54 | 55 | LUALIB_API int luaL_ref (lua_State *L, int t); 56 | LUALIB_API void luaL_unref (lua_State *L, int t, int ref); 57 | 58 | LUALIB_API int luaL_getn (lua_State *L, int t); 59 | LUALIB_API void luaL_setn (lua_State *L, int t, int n); 60 | 61 | LUALIB_API int luaL_loadfile (lua_State *L, const char *filename); 62 | LUALIB_API int luaL_loadbuffer (lua_State *L, const char *buff, size_t sz, 63 | const char *name); 64 | 65 | 66 | 67 | /* 68 | ** =============================================================== 69 | ** some useful macros 70 | ** =============================================================== 71 | */ 72 | 73 | #define luaL_argcheck(L, cond,numarg,extramsg) if (!(cond)) \ 74 | luaL_argerror(L, numarg,extramsg) 75 | #define luaL_checkstring(L,n) (luaL_checklstring(L, (n), NULL)) 76 | #define luaL_optstring(L,n,d) (luaL_optlstring(L, (n), (d), NULL)) 77 | #define luaL_checkint(L,n) ((int)luaL_checknumber(L, n)) 78 | #define luaL_checklong(L,n) ((long)luaL_checknumber(L, n)) 79 | #define luaL_optint(L,n,d) ((int)luaL_optnumber(L, n,(lua_Number)(d))) 80 | #define luaL_optlong(L,n,d) ((long)luaL_optnumber(L, n,(lua_Number)(d))) 81 | 82 | 83 | /* 84 | ** {====================================================== 85 | ** Generic Buffer manipulation 86 | ** ======================================================= 87 | */ 88 | 89 | 90 | #ifndef LUAL_BUFFERSIZE 91 | #define LUAL_BUFFERSIZE BUFSIZ 92 | #endif 93 | 94 | 95 | typedef struct luaL_Buffer { 96 | char *p; /* current position in buffer */ 97 | int lvl; /* number of strings in the stack (level) */ 98 | lua_State *L; 99 | char buffer[LUAL_BUFFERSIZE]; 100 | } luaL_Buffer; 101 | 102 | #define luaL_putchar(B,c) \ 103 | ((void)((B)->p < ((B)->buffer+LUAL_BUFFERSIZE) || luaL_prepbuffer(B)), \ 104 | (*(B)->p++ = (char)(c))) 105 | 106 | #define luaL_addsize(B,n) ((B)->p += (n)) 107 | 108 | LUALIB_API void luaL_buffinit (lua_State *L, luaL_Buffer *B); 109 | LUALIB_API char *luaL_prepbuffer (luaL_Buffer *B); 110 | LUALIB_API void luaL_addlstring (luaL_Buffer *B, const char *s, size_t l); 111 | LUALIB_API void luaL_addstring (luaL_Buffer *B, const char *s); 112 | LUALIB_API void luaL_addvalue (luaL_Buffer *B); 113 | LUALIB_API void luaL_pushresult (luaL_Buffer *B); 114 | 115 | 116 | /* }====================================================== */ 117 | 118 | 119 | 120 | /* 121 | ** Compatibility macros and functions 122 | */ 123 | 124 | LUALIB_API int lua_dofile (lua_State *L, const char *filename); 125 | LUALIB_API int lua_dostring (lua_State *L, const char *str); 126 | LUALIB_API int lua_dobuffer (lua_State *L, const char *buff, size_t sz, 127 | const char *n); 128 | 129 | 130 | #define luaL_check_lstr luaL_checklstring 131 | #define luaL_opt_lstr luaL_optlstring 132 | #define luaL_check_number luaL_checknumber 133 | #define luaL_opt_number luaL_optnumber 134 | #define luaL_arg_check luaL_argcheck 135 | #define luaL_check_string luaL_checkstring 136 | #define luaL_opt_string luaL_optstring 137 | #define luaL_check_int luaL_checkint 138 | #define luaL_check_long luaL_checklong 139 | #define luaL_opt_int luaL_optint 140 | #define luaL_opt_long luaL_optlong 141 | 142 | 143 | #endif 144 | 145 | 146 | -------------------------------------------------------------------------------- /include/lualib.h: -------------------------------------------------------------------------------- 1 | /* 2 | ** $Id: lualib.h,v 1.28 2003/03/18 12:24:26 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 | #ifndef LUALIB_API 15 | #define LUALIB_API LUA_API 16 | #endif 17 | 18 | 19 | #define LUA_COLIBNAME "coroutine" 20 | LUALIB_API int luaopen_base (lua_State *L); 21 | 22 | #define LUA_TABLIBNAME "table" 23 | LUALIB_API int luaopen_table (lua_State *L); 24 | 25 | #define LUA_IOLIBNAME "io" 26 | #define LUA_OSLIBNAME "os" 27 | LUALIB_API int luaopen_io (lua_State *L); 28 | 29 | #define LUA_STRLIBNAME "string" 30 | LUALIB_API int luaopen_string (lua_State *L); 31 | 32 | #define LUA_MATHLIBNAME "math" 33 | LUALIB_API int luaopen_math (lua_State *L); 34 | 35 | #define LUA_DBLIBNAME "debug" 36 | LUALIB_API int luaopen_debug (lua_State *L); 37 | 38 | 39 | LUALIB_API int luaopen_loadlib (lua_State *L); 40 | 41 | 42 | /* to help testing the libraries */ 43 | #ifndef lua_assert 44 | #define lua_assert(c) /* empty */ 45 | #endif 46 | 47 | 48 | /* compatibility code */ 49 | #define lua_baselibopen luaopen_base 50 | #define lua_tablibopen luaopen_table 51 | #define lua_iolibopen luaopen_io 52 | #define lua_strlibopen luaopen_string 53 | #define lua_mathlibopen luaopen_math 54 | #define lua_dblibopen luaopen_debug 55 | 56 | #endif 57 | -------------------------------------------------------------------------------- /src/Makefile: -------------------------------------------------------------------------------- 1 | # makefile for Lua core library 2 | 3 | LUA= .. 4 | 5 | include $(LUA)/config 6 | 7 | OBJS= \ 8 | lapi.o \ 9 | lcode.o \ 10 | ldebug.o \ 11 | ldo.o \ 12 | ldump.o \ 13 | lfunc.o \ 14 | lgc.o \ 15 | llex.o \ 16 | lmem.o \ 17 | lobject.o \ 18 | lopcodes.o \ 19 | lparser.o \ 20 | lstate.o \ 21 | lstring.o \ 22 | ltable.o \ 23 | ltests.o \ 24 | ltm.o \ 25 | lundump.o \ 26 | lvm.o \ 27 | lzio.o 28 | 29 | SRCS= \ 30 | lapi.c \ 31 | lcode.c \ 32 | ldebug.c \ 33 | ldo.c \ 34 | ldump.c \ 35 | lfunc.c \ 36 | lgc.c \ 37 | llex.c \ 38 | lmem.c \ 39 | lobject.c \ 40 | lopcodes.c \ 41 | lparser.c \ 42 | lstate.c \ 43 | lstring.c \ 44 | ltable.c \ 45 | ltests.c \ 46 | ltm.c \ 47 | lundump.c \ 48 | lvm.c \ 49 | lzio.c \ 50 | lapi.h \ 51 | lcode.h \ 52 | ldebug.h \ 53 | ldo.h \ 54 | lfunc.h \ 55 | lgc.h \ 56 | llex.h \ 57 | llimits.h \ 58 | lmem.h \ 59 | lobject.h \ 60 | lopcodes.h \ 61 | lparser.h \ 62 | lstate.h \ 63 | lstring.h \ 64 | ltable.h \ 65 | ltm.h \ 66 | lundump.h \ 67 | lvm.h \ 68 | lzio.h 69 | 70 | T= $(LIB)/liblua.a 71 | 72 | all: $T 73 | 74 | $T: $(OBJS) 75 | $(AR) $@ $(OBJS) 76 | $(RANLIB) $@ 77 | 78 | clean: 79 | rm -f $(OBJS) $T 80 | 81 | co: 82 | co -q -f -M $(SRCS) 83 | 84 | klean: clean 85 | rm -f $(SRCS) 86 | -------------------------------------------------------------------------------- /src/Makefile.cross: -------------------------------------------------------------------------------- 1 | # makefile for Lua core library 2 | 3 | LUA= .. 4 | 5 | include $(LUA)/config.cross 6 | 7 | cross/%.o: %.c 8 | $(CC) $(CFLAGS) -c $< -o $@ 9 | 10 | OBJS= \ 11 | cross/lapi.o \ 12 | cross/lcode.o \ 13 | cross/ldebug.o \ 14 | cross/ldo.o \ 15 | cross/ldump.o \ 16 | cross/lfunc.o \ 17 | cross/lgc.o \ 18 | cross/llex.o \ 19 | cross/lmem.o \ 20 | cross/lobject.o \ 21 | cross/lopcodes.o \ 22 | cross/lparser.o \ 23 | cross/lstate.o \ 24 | cross/lstring.o \ 25 | cross/ltable.o \ 26 | cross/ltests.o \ 27 | cross/ltm.o \ 28 | cross/lundump.o \ 29 | cross/lvm.o \ 30 | cross/lzio.o \ 31 | cross/compat.o 32 | 33 | SRCS= \ 34 | lapi.c \ 35 | lcode.c \ 36 | ldebug.c \ 37 | ldo.c \ 38 | ldump.c \ 39 | lfunc.c \ 40 | lgc.c \ 41 | llex.c \ 42 | lmem.c \ 43 | lobject.c \ 44 | lopcodes.c \ 45 | lparser.c \ 46 | lstate.c \ 47 | lstring.c \ 48 | ltable.c \ 49 | ltests.c \ 50 | ltm.c \ 51 | lundump.c \ 52 | lvm.c \ 53 | lzio.c \ 54 | lapi.h \ 55 | lcode.h \ 56 | ldebug.h \ 57 | ldo.h \ 58 | lfunc.h \ 59 | lgc.h \ 60 | llex.h \ 61 | llimits.h \ 62 | lmem.h \ 63 | lobject.h \ 64 | lopcodes.h \ 65 | lparser.h \ 66 | lstate.h \ 67 | lstring.h \ 68 | ltable.h \ 69 | ltm.h \ 70 | lundump.h \ 71 | lvm.h \ 72 | lzio.h \ 73 | compat.c 74 | 75 | T= $(LIB)/liblua.a 76 | 77 | all: $T 78 | 79 | $T: $(OBJS) 80 | $(AR) $@ $(OBJS) 81 | $(RANLIB) $@ 82 | 83 | clean: 84 | rm -f $(OBJS) $T 85 | 86 | co: 87 | co -q -f -M $(SRCS) 88 | 89 | klean: clean 90 | rm -f $(SRCS) 91 | -------------------------------------------------------------------------------- /src/Makefile.macos: -------------------------------------------------------------------------------- 1 | # makefile for Lua core library 2 | 3 | LUA= .. 4 | 5 | include $(LUA)/config.macos 6 | 7 | macos/%.o: %.c 8 | $(CC) $(CFLAGS) -c $< -o $@ 9 | 10 | OBJS= \ 11 | macos/lapi.o \ 12 | macos/lcode.o \ 13 | macos/ldebug.o \ 14 | macos/ldo.o \ 15 | macos/ldump.o \ 16 | macos/lfunc.o \ 17 | macos/lgc.o \ 18 | macos/llex.o \ 19 | macos/lmem.o \ 20 | macos/lobject.o \ 21 | macos/lopcodes.o \ 22 | macos/lparser.o \ 23 | macos/lstate.o \ 24 | macos/lstring.o \ 25 | macos/ltable.o \ 26 | macos/ltests.o \ 27 | macos/ltm.o \ 28 | macos/lundump.o \ 29 | macos/lvm.o \ 30 | macos/lzio.o \ 31 | macos/compat.o 32 | 33 | SRCS= \ 34 | lapi.c \ 35 | lcode.c \ 36 | ldebug.c \ 37 | ldo.c \ 38 | ldump.c \ 39 | lfunc.c \ 40 | lgc.c \ 41 | llex.c \ 42 | lmem.c \ 43 | lobject.c \ 44 | lopcodes.c \ 45 | lparser.c \ 46 | lstate.c \ 47 | lstring.c \ 48 | ltable.c \ 49 | ltests.c \ 50 | ltm.c \ 51 | lundump.c \ 52 | lvm.c \ 53 | lzio.c \ 54 | lapi.h \ 55 | lcode.h \ 56 | ldebug.h \ 57 | ldo.h \ 58 | lfunc.h \ 59 | lgc.h \ 60 | llex.h \ 61 | llimits.h \ 62 | lmem.h \ 63 | lobject.h \ 64 | lopcodes.h \ 65 | lparser.h \ 66 | lstate.h \ 67 | lstring.h \ 68 | ltable.h \ 69 | ltm.h \ 70 | lundump.h \ 71 | lvm.h \ 72 | lzio.h \ 73 | compat.c 74 | 75 | T= $(LIB)/liblua.a 76 | 77 | all: $T 78 | 79 | $T: $(OBJS) 80 | $(AR) $@ $(OBJS) 81 | $(RANLIB) $@ 82 | 83 | clean: 84 | rm -f $(OBJS) $T 85 | 86 | co: 87 | co -q -f -M $(SRCS) 88 | 89 | klean: clean 90 | rm -f $(SRCS) 91 | -------------------------------------------------------------------------------- /src/README: -------------------------------------------------------------------------------- 1 | This is the Lua core. 2 | 3 | The standard Lua library are in lib/. 4 | A sample interpreter is in lua/. 5 | A standalone compiler is in luac/. 6 | -------------------------------------------------------------------------------- /src/compat.c: -------------------------------------------------------------------------------- 1 | #include "compat.h" 2 | 3 | #ifndef api_check 4 | #define api_check(L, o) /*{ assert(o); }*/ 5 | #endif 6 | 7 | #define api_incr_top(L) {api_check(L, L->top < L->ci->top); L->top++;} 8 | 9 | void lua_pushclosure (lua_State *L, void *p) 10 | { 11 | lua_lock(L); 12 | setclvalue(L->top, p); 13 | api_incr_top(L); 14 | lua_unlock(L); 15 | } 16 | 17 | void lua_pushtable (lua_State *L, void *p) 18 | { 19 | lua_lock(L); 20 | sethvalue(L->top, p); 21 | api_incr_top(L); 22 | lua_unlock(L); 23 | } 24 | -------------------------------------------------------------------------------- /src/lapi.h: -------------------------------------------------------------------------------- 1 | /* 2 | ** $Id: lapi.h,v 1.21 2002/03/04 21:29:41 roberto Exp $ 3 | ** Auxiliary functions from Lua API 4 | ** See Copyright Notice in lua.h 5 | */ 6 | 7 | #ifndef lapi_h 8 | #define lapi_h 9 | 10 | 11 | #include "lobject.h" 12 | 13 | 14 | void luaA_pushobject (lua_State *L, const TObject *o); 15 | 16 | #endif 17 | -------------------------------------------------------------------------------- /src/lcode.h: -------------------------------------------------------------------------------- 1 | /* 2 | ** $Id: lcode.h,v 1.38 2002/12/11 12:34:22 roberto Exp $ 3 | ** Code generator for Lua 4 | ** See Copyright Notice in lua.h 5 | */ 6 | 7 | #ifndef lcode_h 8 | #define lcode_h 9 | 10 | #include "llex.h" 11 | #include "lobject.h" 12 | #include "lopcodes.h" 13 | #include "lparser.h" 14 | 15 | 16 | /* 17 | ** Marks the end of a patch list. It is an invalid value both as an absolute 18 | ** address, and as a list link (would link an element to itself). 19 | */ 20 | #define NO_JUMP (-1) 21 | 22 | 23 | /* 24 | ** grep "ORDER OPR" if you change these enums 25 | */ 26 | typedef enum BinOpr { 27 | OPR_ADD, OPR_SUB, OPR_MULT, OPR_DIV, OPR_POW, 28 | OPR_CONCAT, 29 | OPR_NE, OPR_EQ, 30 | OPR_LT, OPR_LE, OPR_GT, OPR_GE, 31 | OPR_AND, OPR_OR, 32 | OPR_NOBINOPR 33 | } BinOpr; 34 | 35 | #define binopistest(op) ((op) >= OPR_NE) 36 | 37 | typedef enum UnOpr { OPR_MINUS, OPR_NOT, OPR_NOUNOPR } UnOpr; 38 | 39 | 40 | #define getcode(fs,e) ((fs)->f->code[(e)->info]) 41 | 42 | #define luaK_codeAsBx(fs,o,A,sBx) luaK_codeABx(fs,o,A,(sBx)+MAXARG_sBx) 43 | 44 | int luaK_code (FuncState *fs, Instruction i, int line); 45 | int luaK_codeABx (FuncState *fs, OpCode o, int A, unsigned int Bx); 46 | int luaK_codeABC (FuncState *fs, OpCode o, int A, int B, int C); 47 | void luaK_fixline (FuncState *fs, int line); 48 | void luaK_nil (FuncState *fs, int from, int n); 49 | void luaK_reserveregs (FuncState *fs, int n); 50 | void luaK_checkstack (FuncState *fs, int n); 51 | int luaK_stringK (FuncState *fs, TString *s); 52 | int luaK_numberK (FuncState *fs, lua_Number r); 53 | void luaK_dischargevars (FuncState *fs, expdesc *e); 54 | int luaK_exp2anyreg (FuncState *fs, expdesc *e); 55 | void luaK_exp2nextreg (FuncState *fs, expdesc *e); 56 | void luaK_exp2val (FuncState *fs, expdesc *e); 57 | int luaK_exp2RK (FuncState *fs, expdesc *e); 58 | void luaK_self (FuncState *fs, expdesc *e, expdesc *key); 59 | void luaK_indexed (FuncState *fs, expdesc *t, expdesc *k); 60 | void luaK_goiftrue (FuncState *fs, expdesc *e); 61 | void luaK_goiffalse (FuncState *fs, expdesc *e); 62 | void luaK_storevar (FuncState *fs, expdesc *var, expdesc *e); 63 | void luaK_setcallreturns (FuncState *fs, expdesc *var, int nresults); 64 | int luaK_jump (FuncState *fs); 65 | void luaK_patchlist (FuncState *fs, int list, int target); 66 | void luaK_patchtohere (FuncState *fs, int list); 67 | void luaK_concat (FuncState *fs, int *l1, int l2); 68 | int luaK_getlabel (FuncState *fs); 69 | void luaK_prefix (FuncState *fs, UnOpr op, expdesc *v); 70 | void luaK_infix (FuncState *fs, BinOpr op, expdesc *v); 71 | void luaK_posfix (FuncState *fs, BinOpr op, expdesc *v1, expdesc *v2); 72 | 73 | 74 | #endif 75 | -------------------------------------------------------------------------------- /src/ldebug.h: -------------------------------------------------------------------------------- 1 | /* 2 | ** $Id: ldebug.h,v 1.32 2002/11/18 11:01:55 roberto Exp $ 3 | ** Auxiliary functions from Debug Interface module 4 | ** See Copyright Notice in lua.h 5 | */ 6 | 7 | #ifndef ldebug_h 8 | #define ldebug_h 9 | 10 | 11 | #include "lstate.h" 12 | 13 | 14 | #define pcRel(pc, p) (cast(int, (pc) - (p)->code) - 1) 15 | 16 | #define getline(f,pc) (((f)->lineinfo) ? (f)->lineinfo[pc] : 0) 17 | 18 | #define resethookcount(L) (L->hookcount = L->basehookcount) 19 | 20 | 21 | void luaG_inithooks (lua_State *L); 22 | void luaG_typeerror (lua_State *L, const TObject *o, const char *opname); 23 | void luaG_concaterror (lua_State *L, StkId p1, StkId p2); 24 | void luaG_aritherror (lua_State *L, const TObject *p1, const TObject *p2); 25 | int luaG_ordererror (lua_State *L, const TObject *p1, const TObject *p2); 26 | void luaG_runerror (lua_State *L, const char *fmt, ...); 27 | void luaG_errormsg (lua_State *L); 28 | int luaG_checkcode (const Proto *pt); 29 | 30 | 31 | #endif 32 | -------------------------------------------------------------------------------- /src/ldo.h: -------------------------------------------------------------------------------- 1 | /* 2 | ** $Id: ldo.h,v 1.56 2002/12/04 17:29:32 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 | /* 17 | ** macro to control inclusion of some hard tests on stack reallocation 18 | */ 19 | #ifndef HARDSTACKTESTS 20 | #define condhardstacktests(x) { /* empty */ } 21 | #else 22 | #define condhardstacktests(x) x 23 | #endif 24 | 25 | 26 | #define luaD_checkstack(L,n) \ 27 | if ((char *)L->stack_last - (char *)L->top <= (n)*(int)sizeof(TObject)) \ 28 | luaD_growstack(L, n); \ 29 | else condhardstacktests(luaD_reallocstack(L, L->stacksize)); 30 | 31 | 32 | #define incr_top(L) {luaD_checkstack(L,1); L->top++;} 33 | 34 | #define savestack(L,p) ((char *)(p) - (char *)L->stack) 35 | #define restorestack(L,n) ((TObject *)((char *)L->stack + (n))) 36 | 37 | #define saveci(L,p) ((char *)(p) - (char *)L->base_ci) 38 | #define restoreci(L,n) ((CallInfo *)((char *)L->base_ci + (n))) 39 | 40 | 41 | /* type of protected functions, to be ran by `runprotected' */ 42 | typedef void (*Pfunc) (lua_State *L, void *ud); 43 | 44 | void luaD_resetprotection (lua_State *L); 45 | int luaD_protectedparser (lua_State *L, ZIO *z, int bin); 46 | void luaD_callhook (lua_State *L, int event, int line); 47 | StkId luaD_precall (lua_State *L, StkId func); 48 | void luaD_call (lua_State *L, StkId func, int nResults); 49 | int luaD_pcall (lua_State *L, Pfunc func, void *u, 50 | ptrdiff_t oldtop, ptrdiff_t ef); 51 | void luaD_poscall (lua_State *L, int wanted, StkId firstResult); 52 | void luaD_reallocCI (lua_State *L, int newsize); 53 | void luaD_reallocstack (lua_State *L, int newsize); 54 | void luaD_growstack (lua_State *L, int n); 55 | 56 | void luaD_throw (lua_State *L, int errcode); 57 | int luaD_rawrunprotected (lua_State *L, Pfunc f, void *ud); 58 | 59 | 60 | #endif 61 | -------------------------------------------------------------------------------- /src/ldump.c: -------------------------------------------------------------------------------- 1 | /* 2 | ** $Id: ldump.c,v 1.4 2003/02/11 23:52:12 lhf Exp $ 3 | ** save bytecodes 4 | ** See Copyright Notice in lua.h 5 | */ 6 | 7 | #include 8 | 9 | #define ldump_c 10 | 11 | #include "lua.h" 12 | 13 | #include "lobject.h" 14 | #include "lopcodes.h" 15 | #include "lstate.h" 16 | #include "lundump.h" 17 | 18 | #ifdef __LP64__ 19 | #include 20 | #endif 21 | 22 | #define DumpVector(b,n,size,D) DumpBlock(b,(n)*(size),D) 23 | #define DumpLiteral(s,D) DumpBlock("" s,(sizeof(s))-1,D) 24 | 25 | typedef struct { 26 | lua_State* L; 27 | lua_Chunkwriter write; 28 | void* data; 29 | } DumpState; 30 | 31 | static void DumpBlock(const void* b, size_t size, DumpState* D) 32 | { 33 | lua_unlock(D->L); 34 | (*D->write)(D->L,b,size,D->data); 35 | lua_lock(D->L); 36 | } 37 | 38 | static void DumpByte(int y, DumpState* D) 39 | { 40 | char x=(char)y; 41 | DumpBlock(&x,sizeof(x),D); 42 | } 43 | 44 | static void DumpInt(int x, DumpState* D) 45 | { 46 | #ifdef __LP64__ 47 | int32_t sx = (int32_t)x; 48 | DumpBlock(&sx,sizeof(sx),D); 49 | #else 50 | DumpBlock(&x,sizeof(x),D); 51 | #endif 52 | } 53 | 54 | static void DumpSize(size_t x, DumpState* D) 55 | { 56 | #ifdef __LP64__ 57 | uint32_t sx = (uint32_t)x; 58 | DumpBlock(&sx,sizeof(sx),D); 59 | #else 60 | DumpBlock(&x,sizeof(x),D); 61 | #endif 62 | } 63 | 64 | static void DumpNumber(lua_Number x, DumpState* D) 65 | { 66 | DumpBlock(&x,sizeof(x),D); 67 | } 68 | 69 | static void DumpString(TString* s, DumpState* D) 70 | { 71 | if (s==NULL || getstr(s)==NULL) 72 | DumpSize(0,D); 73 | else 74 | { 75 | size_t size=s->tsv.len+1; /* include trailing '\0' */ 76 | DumpSize(size,D); 77 | DumpBlock(getstr(s),size,D); 78 | } 79 | } 80 | 81 | static void DumpCode(const Proto* f, DumpState* D) 82 | { 83 | DumpInt(f->sizecode,D); 84 | DumpVector(f->code,f->sizecode,sizeof(*f->code),D); 85 | } 86 | 87 | static void DumpLocals(const Proto* f, DumpState* D) 88 | { 89 | int i,n=f->sizelocvars; 90 | DumpInt(n,D); 91 | for (i=0; ilocvars[i].varname,D); 94 | DumpInt(f->locvars[i].startpc,D); 95 | DumpInt(f->locvars[i].endpc,D); 96 | } 97 | } 98 | 99 | static void DumpLines(const Proto* f, DumpState* D) 100 | { 101 | DumpInt(f->sizelineinfo,D); 102 | DumpVector(f->lineinfo,f->sizelineinfo,sizeof(*f->lineinfo),D); 103 | } 104 | 105 | static void DumpUpvalues(const Proto* f, DumpState* D) 106 | { 107 | int i,n=f->sizeupvalues; 108 | DumpInt(n,D); 109 | for (i=0; iupvalues[i],D); 110 | } 111 | 112 | static void DumpFunction(const Proto* f, const TString* p, DumpState* D); 113 | 114 | static void DumpConstants(const Proto* f, DumpState* D) 115 | { 116 | int i,n; 117 | DumpInt(n=f->sizek,D); 118 | for (i=0; ik[i]; 121 | DumpByte(ttype(o),D); 122 | switch (ttype(o)) 123 | { 124 | case LUA_TNUMBER: 125 | DumpNumber(nvalue(o),D); 126 | break; 127 | case LUA_TSTRING: 128 | DumpString(tsvalue(o),D); 129 | break; 130 | case LUA_TNIL: 131 | break; 132 | default: 133 | lua_assert(0); /* cannot happen */ 134 | break; 135 | } 136 | } 137 | DumpInt(n=f->sizep,D); 138 | for (i=0; ip[i],f->source,D); 139 | } 140 | 141 | static void DumpFunction(const Proto* f, const TString* p, DumpState* D) 142 | { 143 | DumpString((f->source==p) ? NULL : f->source,D); 144 | DumpInt(f->lineDefined,D); 145 | DumpByte(f->nups,D); 146 | DumpByte(f->numparams,D); 147 | DumpByte(f->is_vararg,D); 148 | DumpByte(f->maxstacksize,D); 149 | DumpLines(f,D); 150 | DumpLocals(f,D); 151 | DumpUpvalues(f,D); 152 | DumpConstants(f,D); 153 | DumpCode(f,D); 154 | } 155 | 156 | static void DumpHeader(DumpState* D) 157 | { 158 | DumpLiteral(LUA_SIGNATURE,D); 159 | DumpByte(VERSION,D); 160 | DumpByte(luaU_endianness(),D); 161 | #ifdef __LP64__ 162 | // hardcoded 163 | DumpByte(4,D); 164 | DumpByte(4,D); 165 | #else 166 | DumpByte(sizeof(int),D); 167 | DumpByte(sizeof(size_t),D); 168 | #endif 169 | DumpByte(sizeof(Instruction),D); 170 | DumpByte(SIZE_OP,D); 171 | DumpByte(SIZE_A,D); 172 | DumpByte(SIZE_B,D); 173 | DumpByte(SIZE_C,D); 174 | DumpByte(sizeof(lua_Number),D); 175 | DumpNumber(TEST_NUMBER,D); 176 | } 177 | 178 | /* 179 | ** dump function as precompiled chunk 180 | */ 181 | void luaU_dump (lua_State* L, const Proto* Main, lua_Chunkwriter w, void* data) 182 | { 183 | DumpState D; 184 | D.L=L; 185 | D.write=w; 186 | D.data=data; 187 | DumpHeader(&D); 188 | DumpFunction(Main,NULL,&D); 189 | } 190 | 191 | -------------------------------------------------------------------------------- /src/lfunc.c: -------------------------------------------------------------------------------- 1 | /* 2 | ** $Id: lfunc.c,v 1.67a 2003/03/18 12:50:04 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 | 12 | #include "lua.h" 13 | 14 | #include "lfunc.h" 15 | #include "lgc.h" 16 | #include "lmem.h" 17 | #include "lobject.h" 18 | #include "lstate.h" 19 | 20 | 21 | Closure *luaF_newCclosure (lua_State *L, int nelems) { 22 | Closure *c = cast(Closure *, luaM_malloc(L, sizeCclosure(nelems))); 23 | luaC_link(L, valtogco(c), LUA_TFUNCTION); 24 | c->c.isC = 1; 25 | c->c.nupvalues = cast(lu_byte, nelems); 26 | return c; 27 | } 28 | 29 | 30 | Closure *luaF_newLclosure (lua_State *L, int nelems, TObject *e) { 31 | Closure *c = cast(Closure *, luaM_malloc(L, sizeLclosure(nelems))); 32 | luaC_link(L, valtogco(c), LUA_TFUNCTION); 33 | c->l.isC = 0; 34 | c->l.g = *e; 35 | c->l.nupvalues = cast(lu_byte, nelems); 36 | return c; 37 | } 38 | 39 | 40 | UpVal *luaF_findupval (lua_State *L, StkId level) { 41 | GCObject **pp = &L->openupval; 42 | UpVal *p; 43 | UpVal *v; 44 | while ((p = ngcotouv(*pp)) != NULL && p->v >= level) { 45 | if (p->v == level) return p; 46 | pp = &p->next; 47 | } 48 | v = luaM_new(L, UpVal); /* not found: create a new one */ 49 | v->tt = LUA_TUPVAL; 50 | v->marked = 1; /* open upvalues should not be collected */ 51 | v->v = level; /* current value lives in the stack */ 52 | v->next = *pp; /* chain it in the proper position */ 53 | *pp = valtogco(v); 54 | return v; 55 | } 56 | 57 | 58 | void luaF_close (lua_State *L, StkId level) { 59 | UpVal *p; 60 | while ((p = ngcotouv(L->openupval)) != NULL && p->v >= level) { 61 | setobj(&p->value, p->v); /* save current value (write barrier) */ 62 | p->v = &p->value; /* now current value lives here */ 63 | L->openupval = p->next; /* remove from `open' list */ 64 | luaC_link(L, valtogco(p), LUA_TUPVAL); 65 | } 66 | } 67 | 68 | 69 | Proto *luaF_newproto (lua_State *L) { 70 | Proto *f = luaM_new(L, Proto); 71 | luaC_link(L, valtogco(f), LUA_TPROTO); 72 | f->k = NULL; 73 | f->sizek = 0; 74 | f->p = NULL; 75 | f->sizep = 0; 76 | f->code = NULL; 77 | f->sizecode = 0; 78 | f->sizelineinfo = 0; 79 | f->sizeupvalues = 0; 80 | f->nups = 0; 81 | f->upvalues = NULL; 82 | f->numparams = 0; 83 | f->is_vararg = 0; 84 | f->maxstacksize = 0; 85 | f->lineinfo = NULL; 86 | f->sizelocvars = 0; 87 | f->locvars = NULL; 88 | f->lineDefined = 0; 89 | f->source = NULL; 90 | return f; 91 | } 92 | 93 | 94 | void luaF_freeproto (lua_State *L, Proto *f) { 95 | luaM_freearray(L, f->code, f->sizecode, Instruction); 96 | luaM_freearray(L, f->p, f->sizep, Proto *); 97 | luaM_freearray(L, f->k, f->sizek, TObject); 98 | luaM_freearray(L, f->lineinfo, f->sizelineinfo, int); 99 | luaM_freearray(L, f->locvars, f->sizelocvars, struct LocVar); 100 | luaM_freearray(L, f->upvalues, f->sizeupvalues, TString *); 101 | luaM_freelem(L, f); 102 | } 103 | 104 | 105 | void luaF_freeclosure (lua_State *L, Closure *c) { 106 | int size = (c->c.isC) ? sizeCclosure(c->c.nupvalues) : 107 | sizeLclosure(c->l.nupvalues); 108 | luaM_free(L, c, size); 109 | } 110 | 111 | 112 | /* 113 | ** Look for n-th local variable at line `line' in function `func'. 114 | ** Returns NULL if not found. 115 | */ 116 | const char *luaF_getlocalname (const Proto *f, int local_number, int pc) { 117 | int i; 118 | for (i = 0; isizelocvars && f->locvars[i].startpc <= pc; i++) { 119 | if (pc < f->locvars[i].endpc) { /* is variable active? */ 120 | local_number--; 121 | if (local_number == 0) 122 | return getstr(f->locvars[i].varname); 123 | } 124 | } 125 | return NULL; /* not found */ 126 | } 127 | 128 | -------------------------------------------------------------------------------- /src/lfunc.h: -------------------------------------------------------------------------------- 1 | /* 2 | ** $Id: lfunc.h,v 1.21a 2003/03/18 12:50:04 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(TObject)*((n)-1))) 16 | 17 | #define sizeLclosure(n) (cast(int, sizeof(LClosure)) + \ 18 | cast(int, sizeof(TObject *)*((n)-1))) 19 | 20 | 21 | Proto *luaF_newproto (lua_State *L); 22 | Closure *luaF_newCclosure (lua_State *L, int nelems); 23 | Closure *luaF_newLclosure (lua_State *L, int nelems, TObject *e); 24 | UpVal *luaF_findupval (lua_State *L, StkId level); 25 | void luaF_close (lua_State *L, StkId level); 26 | void luaF_freeproto (lua_State *L, Proto *f); 27 | void luaF_freeclosure (lua_State *L, Closure *c); 28 | 29 | const char *luaF_getlocalname (const Proto *func, int local_number, int pc); 30 | 31 | 32 | #endif 33 | -------------------------------------------------------------------------------- /src/lgc.h: -------------------------------------------------------------------------------- 1 | /* 2 | ** $Id: lgc.h,v 1.19a 2003/02/28 19:45:15 roberto Exp $ 3 | ** Garbage Collector 4 | ** See Copyright Notice in lua.h 5 | */ 6 | 7 | #ifndef lgc_h 8 | #define lgc_h 9 | 10 | 11 | #include "lobject.h" 12 | 13 | 14 | #define luaC_checkGC(L) { lua_assert(!(L->ci->state & CI_CALLING)); \ 15 | if (G(L)->nblocks >= G(L)->GCthreshold) luaC_collectgarbage(L); } 16 | 17 | 18 | size_t luaC_separateudata (lua_State *L); 19 | void luaC_callGCTM (lua_State *L); 20 | void luaC_sweep (lua_State *L, int all); 21 | void luaC_collectgarbage (lua_State *L); 22 | void luaC_link (lua_State *L, GCObject *o, lu_byte tt); 23 | 24 | 25 | #endif 26 | -------------------------------------------------------------------------------- /src/lib/Makefile: -------------------------------------------------------------------------------- 1 | # makefile for Lua standard library 2 | 3 | LUA= ../.. 4 | 5 | include $(LUA)/config 6 | 7 | EXTRA_DEFS= $(POPEN) $(TMPNAM) $(DEGREES) $(LOADLIB) 8 | 9 | OBJS= lauxlib.o lbaselib.o ldblib.o liolib.o lmathlib.o ltablib.o lstrlib.o loadlib.o 10 | SRCS= lauxlib.c lbaselib.c ldblib.c liolib.c lmathlib.c ltablib.c lstrlib.c loadlib.c 11 | 12 | T= $(LIB)/liblualib.a 13 | 14 | all: $T 15 | 16 | $T: $(OBJS) 17 | $(AR) $@ $(OBJS) 18 | $(RANLIB) $@ 19 | 20 | clean: 21 | rm -f $(OBJS) $T 22 | 23 | co: 24 | co -q -f -M $(SRCS) 25 | 26 | klean: clean 27 | rm -f $(SRCS) 28 | -------------------------------------------------------------------------------- /src/lib/Makefile.cross: -------------------------------------------------------------------------------- 1 | # makefile for Lua standard library 2 | 3 | LUA= ../.. 4 | 5 | include $(LUA)/config.cross 6 | 7 | cross/%.o: %.c 8 | $(CC) $(CFLAGS) -c $< -o $@ 9 | 10 | EXTRA_DEFS= $(POPEN) $(TMPNAM) $(DEGREES) $(LOADLIB) 11 | 12 | OBJS= cross/lauxlib.o cross/lbaselib.o cross/ldblib.o cross/liolib.o cross/lmathlib.o cross/ltablib.o cross/lstrlib.o cross/loadlib.o cross/lbwlib.o cross/lpacklib.o 13 | SRCS= lauxlib.c lbaselib.c ldblib.c liolib.c lmathlib.c ltablib.c lstrlib.c loadlib.c lbwlib.c lpacklib.c 14 | 15 | T= $(LIB)/liblualib.a 16 | 17 | all: $T 18 | 19 | $T: $(OBJS) 20 | $(AR) $@ $(OBJS) 21 | $(RANLIB) $@ 22 | 23 | clean: 24 | rm -f $(OBJS) $T 25 | 26 | co: 27 | co -q -f -M $(SRCS) 28 | 29 | klean: clean 30 | rm -f $(SRCS) 31 | -------------------------------------------------------------------------------- /src/lib/Makefile.macos: -------------------------------------------------------------------------------- 1 | # makefile for Lua standard library 2 | 3 | LUA= ../.. 4 | 5 | include $(LUA)/config.macos 6 | 7 | macos/%.o: %.c 8 | $(CC) $(CFLAGS) -c $< -o $@ 9 | 10 | EXTRA_DEFS= $(POPEN) $(TMPNAM) $(DEGREES) $(LOADLIB) 11 | 12 | OBJS= macos/lauxlib.o macos/lbaselib.o macos/ldblib.o macos/liolib.o macos/lmathlib.o macos/ltablib.o macos/lstrlib.o macos/loadlib.o macos/lbwlib.o macos/lpacklib.o 13 | SRCS= lauxlib.c lbaselib.c ldblib.c liolib.c lmathlib.c ltablib.c lstrlib.c loadlib.c lbwlib.c lpacklib.c 14 | 15 | T= $(LIB)/liblualib.a 16 | 17 | all: $T 18 | 19 | $T: $(OBJS) 20 | $(AR) $@ $(OBJS) 21 | $(RANLIB) $@ 22 | 23 | clean: 24 | rm -f $(OBJS) $T 25 | 26 | co: 27 | co -q -f -M $(SRCS) 28 | 29 | klean: clean 30 | rm -f $(SRCS) 31 | -------------------------------------------------------------------------------- /src/lib/README: -------------------------------------------------------------------------------- 1 | This is the standard Lua library. 2 | 3 | The code of the standard library can be read as an example of how to export 4 | C functions to Lua. The easiest library to read is lmathlib.c. 5 | 6 | The library is implemented entirely on top of the official Lua API as declared 7 | in lua.h, using lauxlib.c, which contains several useful functions for writing 8 | libraries. We encourage developers to use lauxlib.c in their own libraries. 9 | -------------------------------------------------------------------------------- /src/lib/lbwlib.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | #include "lua.h" 4 | #include "lauxlib.h" 5 | #include "lualib.h" 6 | 7 | static int and_b (lua_State *L); 8 | static int or_b (lua_State *L); 9 | static int not_b (lua_State *L); 10 | static int xor_b (lua_State *L); 11 | 12 | static int and_b (lua_State *L) { 13 | unsigned long n1 = luaL_check_long(L, 1); 14 | unsigned long n2 = luaL_check_long(L, 2); 15 | lua_pushnumber(L, n1 & n2); 16 | return 1; 17 | } 18 | 19 | static int or_b (lua_State *L) { 20 | unsigned long n1 = luaL_check_long(L, 1); 21 | unsigned long n2 = luaL_check_long(L, 2); 22 | lua_pushnumber(L, n1 | n2); 23 | return 1; 24 | } 25 | 26 | static int not_b (lua_State *L) { 27 | unsigned long n = luaL_check_long(L, 1); 28 | lua_pushnumber(L, ~n); 29 | return 1; 30 | } 31 | 32 | static int xor_b (lua_State *L) { 33 | unsigned long n1 = luaL_check_long(L, 1); 34 | unsigned long n2 = luaL_check_long(L, 2); 35 | lua_pushnumber(L, n1 ^ n2); 36 | return 1; 37 | } 38 | 39 | static const struct luaL_reg bwlib[] = { 40 | {"andb", and_b}, 41 | {"orb", or_b}, 42 | {"notb", not_b}, 43 | {"xorb", xor_b}, 44 | {NULL, NULL} 45 | }; 46 | 47 | LUALIB_API int luaopen_bitwise (lua_State *L) { 48 | luaL_openlib(L, "bit", bwlib, 0); 49 | return 1; 50 | } 51 | -------------------------------------------------------------------------------- /src/lib/lmathlib.c: -------------------------------------------------------------------------------- 1 | /* 2 | ** $Id: lmathlib.c,v 1.56 2003/03/11 12:30:37 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 | 13 | #include "lua.h" 14 | 15 | #include "lauxlib.h" 16 | #include "lualib.h" 17 | 18 | 19 | #undef PI 20 | #define PI (3.14159265358979323846) 21 | #define RADIANS_PER_DEGREE (PI/180.0) 22 | 23 | 24 | 25 | /* 26 | ** If you want Lua to operate in degrees (instead of radians), 27 | ** define USE_DEGREES 28 | */ 29 | #ifdef USE_DEGREES 30 | #define FROMRAD(a) ((a)/RADIANS_PER_DEGREE) 31 | #define TORAD(a) ((a)*RADIANS_PER_DEGREE) 32 | #else 33 | #define FROMRAD(a) (a) 34 | #define TORAD(a) (a) 35 | #endif 36 | 37 | 38 | static int math_abs (lua_State *L) { 39 | lua_pushnumber(L, fabs(luaL_checknumber(L, 1))); 40 | return 1; 41 | } 42 | 43 | static int math_sin (lua_State *L) { 44 | lua_pushnumber(L, sin(TORAD(luaL_checknumber(L, 1)))); 45 | return 1; 46 | } 47 | 48 | static int math_cos (lua_State *L) { 49 | lua_pushnumber(L, cos(TORAD(luaL_checknumber(L, 1)))); 50 | return 1; 51 | } 52 | 53 | static int math_tan (lua_State *L) { 54 | lua_pushnumber(L, tan(TORAD(luaL_checknumber(L, 1)))); 55 | return 1; 56 | } 57 | 58 | static int math_asin (lua_State *L) { 59 | lua_pushnumber(L, FROMRAD(asin(luaL_checknumber(L, 1)))); 60 | return 1; 61 | } 62 | 63 | static int math_acos (lua_State *L) { 64 | lua_pushnumber(L, FROMRAD(acos(luaL_checknumber(L, 1)))); 65 | return 1; 66 | } 67 | 68 | static int math_atan (lua_State *L) { 69 | lua_pushnumber(L, FROMRAD(atan(luaL_checknumber(L, 1)))); 70 | return 1; 71 | } 72 | 73 | static int math_atan2 (lua_State *L) { 74 | lua_pushnumber(L, FROMRAD(atan2(luaL_checknumber(L, 1), luaL_checknumber(L, 2)))); 75 | return 1; 76 | } 77 | 78 | static int math_ceil (lua_State *L) { 79 | lua_pushnumber(L, ceil(luaL_checknumber(L, 1))); 80 | return 1; 81 | } 82 | 83 | static int math_floor (lua_State *L) { 84 | lua_pushnumber(L, floor(luaL_checknumber(L, 1))); 85 | return 1; 86 | } 87 | 88 | static int math_mod (lua_State *L) { 89 | lua_pushnumber(L, fmod(luaL_checknumber(L, 1), luaL_checknumber(L, 2))); 90 | return 1; 91 | } 92 | 93 | static int math_sqrt (lua_State *L) { 94 | lua_pushnumber(L, sqrt(luaL_checknumber(L, 1))); 95 | return 1; 96 | } 97 | 98 | static int math_pow (lua_State *L) { 99 | lua_pushnumber(L, pow(luaL_checknumber(L, 1), luaL_checknumber(L, 2))); 100 | return 1; 101 | } 102 | 103 | static int math_log (lua_State *L) { 104 | lua_pushnumber(L, log(luaL_checknumber(L, 1))); 105 | return 1; 106 | } 107 | 108 | static int math_log10 (lua_State *L) { 109 | lua_pushnumber(L, log10(luaL_checknumber(L, 1))); 110 | return 1; 111 | } 112 | 113 | static int math_exp (lua_State *L) { 114 | lua_pushnumber(L, exp(luaL_checknumber(L, 1))); 115 | return 1; 116 | } 117 | 118 | static int math_deg (lua_State *L) { 119 | lua_pushnumber(L, luaL_checknumber(L, 1)/RADIANS_PER_DEGREE); 120 | return 1; 121 | } 122 | 123 | static int math_rad (lua_State *L) { 124 | lua_pushnumber(L, luaL_checknumber(L, 1)*RADIANS_PER_DEGREE); 125 | return 1; 126 | } 127 | 128 | static int math_frexp (lua_State *L) { 129 | int e; 130 | lua_pushnumber(L, frexp(luaL_checknumber(L, 1), &e)); 131 | lua_pushnumber(L, e); 132 | return 2; 133 | } 134 | 135 | static int math_ldexp (lua_State *L) { 136 | lua_pushnumber(L, ldexp(luaL_checknumber(L, 1), luaL_checkint(L, 2))); 137 | return 1; 138 | } 139 | 140 | 141 | 142 | static int math_min (lua_State *L) { 143 | int n = lua_gettop(L); /* number of arguments */ 144 | lua_Number dmin = luaL_checknumber(L, 1); 145 | int i; 146 | for (i=2; i<=n; i++) { 147 | lua_Number d = luaL_checknumber(L, i); 148 | if (d < dmin) 149 | dmin = d; 150 | } 151 | lua_pushnumber(L, dmin); 152 | return 1; 153 | } 154 | 155 | 156 | static int math_max (lua_State *L) { 157 | int n = lua_gettop(L); /* number of arguments */ 158 | lua_Number dmax = luaL_checknumber(L, 1); 159 | int i; 160 | for (i=2; i<=n; i++) { 161 | lua_Number d = luaL_checknumber(L, i); 162 | if (d > dmax) 163 | dmax = d; 164 | } 165 | lua_pushnumber(L, dmax); 166 | return 1; 167 | } 168 | 169 | 170 | static int math_random (lua_State *L) { 171 | /* the `%' avoids the (rare) case of r==1, and is needed also because on 172 | some systems (SunOS!) `rand()' may return a value larger than RAND_MAX */ 173 | lua_Number r = (lua_Number)(rand()%RAND_MAX) / (lua_Number)RAND_MAX; 174 | switch (lua_gettop(L)) { /* check number of arguments */ 175 | case 0: { /* no arguments */ 176 | lua_pushnumber(L, r); /* Number between 0 and 1 */ 177 | break; 178 | } 179 | case 1: { /* only upper limit */ 180 | int u = luaL_checkint(L, 1); 181 | luaL_argcheck(L, 1<=u, 1, "interval is empty"); 182 | lua_pushnumber(L, (int)floor(r*u)+1); /* int between 1 and `u' */ 183 | break; 184 | } 185 | case 2: { /* lower and upper limits */ 186 | int l = luaL_checkint(L, 1); 187 | int u = luaL_checkint(L, 2); 188 | luaL_argcheck(L, l<=u, 2, "interval is empty"); 189 | lua_pushnumber(L, (int)floor(r*(u-l+1))+l); /* int between `l' and `u' */ 190 | break; 191 | } 192 | default: return luaL_error(L, "wrong number of arguments"); 193 | } 194 | return 1; 195 | } 196 | 197 | 198 | static int math_randomseed (lua_State *L) { 199 | srand(luaL_checkint(L, 1)); 200 | return 0; 201 | } 202 | 203 | 204 | static const luaL_reg mathlib[] = { 205 | {"abs", math_abs}, 206 | {"sin", math_sin}, 207 | {"cos", math_cos}, 208 | {"tan", math_tan}, 209 | {"asin", math_asin}, 210 | {"acos", math_acos}, 211 | {"atan", math_atan}, 212 | {"atan2", math_atan2}, 213 | {"ceil", math_ceil}, 214 | {"floor", math_floor}, 215 | {"mod", math_mod}, 216 | {"frexp", math_frexp}, 217 | {"ldexp", math_ldexp}, 218 | {"sqrt", math_sqrt}, 219 | {"min", math_min}, 220 | {"max", math_max}, 221 | {"log", math_log}, 222 | {"log10", math_log10}, 223 | {"exp", math_exp}, 224 | {"deg", math_deg}, 225 | {"pow", math_pow}, 226 | {"rad", math_rad}, 227 | {"random", math_random}, 228 | {"randomseed", math_randomseed}, 229 | {NULL, NULL} 230 | }; 231 | 232 | 233 | /* 234 | ** Open math library 235 | */ 236 | LUALIB_API int luaopen_math (lua_State *L) { 237 | luaL_openlib(L, LUA_MATHLIBNAME, mathlib, 0); 238 | lua_pushliteral(L, "pi"); 239 | lua_pushnumber(L, PI); 240 | lua_settable(L, -3); 241 | lua_pushliteral(L, "__pow"); 242 | lua_pushcfunction(L, math_pow); 243 | lua_settable(L, LUA_GLOBALSINDEX); 244 | return 1; 245 | } 246 | 247 | -------------------------------------------------------------------------------- /src/lib/loadlib.c: -------------------------------------------------------------------------------- 1 | /* 2 | ** $Id: loadlib.c,v 1.4 2003/04/07 20:11:53 roberto Exp $ 3 | ** Dynamic library loader for Lua 4 | ** See Copyright Notice in lua.h 5 | * 6 | * This Lua library exports a single function, called loadlib, which is 7 | * called from Lua as loadlib(lib,init), where lib is the full name of the 8 | * library to be loaded (including the complete path) and init is the name 9 | * of a function to be called after the library is loaded. Typically, this 10 | * function will register other functions, thus making the complete library 11 | * available to Lua. The init function is *not* automatically called by 12 | * loadlib. Instead, loadlib returns the init function as a Lua function 13 | * that the client can call when it thinks is appropriate. In the case of 14 | * errors, loadlib returns nil and two strings describing the error. 15 | * The first string is supplied by the operating system; it should be 16 | * informative and useful for error messages. The second string is "open", 17 | * "init", or "absent" to identify the error and is meant to be used for 18 | * making decisions without having to look into the first string (whose 19 | * format is system-dependent). 20 | * 21 | * This module contains an implementation of loadlib for Unix systems that 22 | * have dlfcn, an implementation for Windows, and a stub for other systems. 23 | * See the list at the end of this file for some links to available 24 | * implementations of dlfcn and interfaces to other native dynamic loaders 25 | * on top of which loadlib could be implemented. 26 | * 27 | */ 28 | 29 | #include "lua.h" 30 | #include "lauxlib.h" 31 | #include "lualib.h" 32 | 33 | 34 | #undef LOADLIB 35 | 36 | 37 | #ifdef USE_DLOPEN 38 | #define LOADLIB 39 | /* 40 | * This is an implementation of loadlib based on the dlfcn interface. 41 | * The dlfcn interface is available in Linux, SunOS, Solaris, IRIX, FreeBSD, 42 | * NetBSD, AIX 4.2, HPUX 11, and probably most other Unix flavors, at least 43 | * as an emulation layer on top of native functions. 44 | */ 45 | 46 | #include 47 | 48 | static int loadlib(lua_State *L) 49 | { 50 | const char *path=luaL_checkstring(L,1); 51 | const char *init=luaL_checkstring(L,2); 52 | void *lib=dlopen(path,RTLD_NOW); 53 | if (lib!=NULL) 54 | { 55 | lua_CFunction f=(lua_CFunction) dlsym(lib,init); 56 | if (f!=NULL) 57 | { 58 | lua_pushlightuserdata(L,lib); 59 | lua_pushcclosure(L,f,1); 60 | return 1; 61 | } 62 | } 63 | /* else return appropriate error messages */ 64 | lua_pushnil(L); 65 | lua_pushstring(L,dlerror()); 66 | lua_pushstring(L,(lib!=NULL) ? "init" : "open"); 67 | if (lib!=NULL) dlclose(lib); 68 | return 3; 69 | } 70 | 71 | #endif 72 | 73 | 74 | 75 | /* 76 | ** In Windows, default is to use dll; otherwise, default is not to use dll 77 | */ 78 | #ifndef USE_DLL 79 | #ifdef _WIN32 80 | #define USE_DLL 1 81 | #else 82 | #define USE_DLL 0 83 | #endif 84 | #endif 85 | 86 | 87 | #if USE_DLL 88 | #define LOADLIB 89 | /* 90 | * This is an implementation of loadlib for Windows using native functions. 91 | */ 92 | 93 | #include 94 | 95 | static void pusherror(lua_State *L) 96 | { 97 | int error=GetLastError(); 98 | char buffer[128]; 99 | if (FormatMessage(FORMAT_MESSAGE_IGNORE_INSERTS | FORMAT_MESSAGE_FROM_SYSTEM, 100 | 0, error, 0, buffer, sizeof(buffer), 0)) 101 | lua_pushstring(L,buffer); 102 | else 103 | lua_pushfstring(L,"system error %d\n",error); 104 | } 105 | 106 | static int loadlib(lua_State *L) 107 | { 108 | const char *path=luaL_checkstring(L,1); 109 | const char *init=luaL_checkstring(L,2); 110 | HINSTANCE lib=LoadLibrary(path); 111 | if (lib!=NULL) 112 | { 113 | lua_CFunction f=(lua_CFunction) GetProcAddress(lib,init); 114 | if (f!=NULL) 115 | { 116 | lua_pushlightuserdata(L,lib); 117 | lua_pushcclosure(L,f,1); 118 | return 1; 119 | } 120 | } 121 | lua_pushnil(L); 122 | pusherror(L); 123 | lua_pushstring(L,(lib!=NULL) ? "init" : "open"); 124 | if (lib!=NULL) FreeLibrary(lib); 125 | return 3; 126 | } 127 | 128 | #endif 129 | 130 | 131 | 132 | #ifndef LOADLIB 133 | /* Fallback for other systems */ 134 | 135 | /* 136 | ** Those systems support dlopen, so they should have defined USE_DLOPEN. 137 | ** The default (no)implementation gives them a special error message. 138 | */ 139 | #ifdef linux 140 | #define LOADLIB 141 | #endif 142 | 143 | #ifdef sun 144 | #define LOADLIB 145 | #endif 146 | 147 | #ifdef sgi 148 | #define LOADLIB 149 | #endif 150 | 151 | #ifdef BSD 152 | #define LOADLIB 153 | #endif 154 | 155 | #ifdef _WIN32 156 | #define LOADLIB 157 | #endif 158 | 159 | #ifdef LOADLIB 160 | #undef LOADLIB 161 | #define LOADLIB "`loadlib' not installed (check your Lua configuration)" 162 | #else 163 | #define LOADLIB "`loadlib' not supported" 164 | #endif 165 | 166 | static int loadlib(lua_State *L) 167 | { 168 | lua_pushnil(L); 169 | lua_pushliteral(L,LOADLIB); 170 | lua_pushliteral(L,"absent"); 171 | return 3; 172 | } 173 | #endif 174 | 175 | LUALIB_API int luaopen_loadlib (lua_State *L) 176 | { 177 | lua_register(L,"loadlib",loadlib); 178 | return 0; 179 | } 180 | 181 | /* 182 | * Here are some links to available implementations of dlfcn and 183 | * interfaces to other native dynamic loaders on top of which loadlib 184 | * could be implemented. Please send contributions and corrections to us. 185 | * 186 | * AIX 187 | * Starting with AIX 4.2, dlfcn is included in the base OS. 188 | * There is also an emulation package available. 189 | * http://www.faqs.org/faqs/aix-faq/part4/section-21.html 190 | * 191 | * HPUX 192 | * HPUX 11 has dlfcn. For HPUX 10 use shl_*. 193 | * http://www.geda.seul.org/mailinglist/geda-dev37/msg00094.html 194 | * http://www.stat.umn.edu/~luke/xls/projects/dlbasics/dlbasics.html 195 | * 196 | * Macintosh, Windows 197 | * http://www.stat.umn.edu/~luke/xls/projects/dlbasics/dlbasics.html 198 | * 199 | * Mac OS X/Darwin 200 | * http://www.opendarwin.org/projects/dlcompat/ 201 | * 202 | * GLIB has wrapper code for BeOS, OS2, Unix and Windows 203 | * http://cvs.gnome.org/lxr/source/glib/gmodule/ 204 | * 205 | */ 206 | -------------------------------------------------------------------------------- /src/llex.h: -------------------------------------------------------------------------------- 1 | /* 2 | ** $Id: llex.h,v 1.47 2003/02/28 17:19:47 roberto Exp $ 3 | ** Lexical Analyzer 4 | ** See Copyright Notice in lua.h 5 | */ 6 | 7 | #ifndef llex_h 8 | #define llex_h 9 | 10 | #include "lobject.h" 11 | #include "lzio.h" 12 | 13 | 14 | #define FIRST_RESERVED 257 15 | 16 | /* maximum length of a reserved word */ 17 | #define TOKEN_LEN (sizeof("function")/sizeof(char)) 18 | 19 | 20 | /* 21 | * WARNING: if you change the order of this enumeration, 22 | * grep "ORDER RESERVED" 23 | */ 24 | enum RESERVED { 25 | /* terminal symbols denoted by reserved words */ 26 | TK_AND = FIRST_RESERVED, TK_BREAK, 27 | TK_DO, TK_ELSE, TK_ELSEIF, TK_END, TK_FALSE, TK_FOR, TK_FUNCTION, 28 | TK_IF, TK_IN, TK_LOCAL, TK_NIL, TK_NOT, TK_OR, TK_REPEAT, 29 | TK_RETURN, TK_THEN, TK_TRUE, TK_UNTIL, TK_WHILE, 30 | /* other terminal symbols */ 31 | TK_NAME, TK_CONCAT, TK_DOTS, TK_EQ, TK_GE, TK_LE, TK_NE, TK_NUMBER, 32 | TK_STRING, TK_EOS 33 | }; 34 | 35 | /* number of reserved words */ 36 | #define NUM_RESERVED (cast(int, TK_WHILE-FIRST_RESERVED+1)) 37 | 38 | 39 | typedef union { 40 | lua_Number r; 41 | TString *ts; 42 | } SemInfo; /* semantics information */ 43 | 44 | 45 | typedef struct Token { 46 | int token; 47 | SemInfo seminfo; 48 | } Token; 49 | 50 | 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; /* `FuncState' is private to the parser */ 58 | struct lua_State *L; 59 | ZIO *z; /* input stream */ 60 | Mbuffer *buff; /* buffer for tokens */ 61 | TString *source; /* current source name */ 62 | int nestlevel; /* level of nested non-terminals */ 63 | } LexState; 64 | 65 | 66 | void luaX_init (lua_State *L); 67 | void luaX_setinput (lua_State *L, LexState *LS, ZIO *z, TString *source); 68 | int luaX_lex (LexState *LS, SemInfo *seminfo); 69 | void luaX_checklimit (LexState *ls, int val, int limit, const char *msg); 70 | void luaX_syntaxerror (LexState *ls, const char *s); 71 | void luaX_errorline (LexState *ls, const char *s, const char *token, int line); 72 | const char *luaX_token2str (LexState *ls, int token); 73 | 74 | 75 | #endif 76 | -------------------------------------------------------------------------------- /src/llimits.h: -------------------------------------------------------------------------------- 1 | /* 2 | ** $Id: llimits.h,v 1.52 2003/02/20 19:33:23 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 | #ifdef __LP64__ 18 | #include 19 | #endif 20 | 21 | /* 22 | ** try to find number of bits in an integer 23 | */ 24 | #ifndef BITS_INT 25 | /* avoid overflows in comparison */ 26 | #if INT_MAX-20 < 32760 27 | #define BITS_INT 16 28 | #else 29 | #if INT_MAX > 2147483640L 30 | /* machine has at least 32 bits */ 31 | #define BITS_INT 32 32 | #else 33 | #error "you must define BITS_INT with number of bits in an integer" 34 | #endif 35 | #endif 36 | #endif 37 | 38 | 39 | /* 40 | ** the following types define integer types for values that may not 41 | ** fit in a `small int' (16 bits), but may waste space in a 42 | ** `large long' (64 bits). The current definitions should work in 43 | ** any machine, but may not be optimal. 44 | */ 45 | 46 | /* an unsigned integer to hold hash values */ 47 | typedef unsigned int lu_hash; 48 | /* its signed equivalent */ 49 | typedef int ls_hash; 50 | 51 | /* an unsigned integer big enough to count the total memory used by Lua; */ 52 | /* it should be at least as large as size_t */ 53 | typedef unsigned long lu_mem; 54 | 55 | #define MAX_LUMEM ULONG_MAX 56 | 57 | 58 | /* an integer big enough to count the number of strings in use */ 59 | typedef long ls_nstr; 60 | 61 | /* chars used as small naturals (so that `char' is reserved for characters) */ 62 | typedef unsigned char lu_byte; 63 | 64 | 65 | #define MAX_SIZET ((size_t)(~(size_t)0)-2) 66 | 67 | 68 | #define MAX_INT (INT_MAX-2) /* maximum value of an int (-2 for safety) */ 69 | 70 | /* 71 | ** conversion of pointer to integer 72 | ** this is for hashing only; there is no problem if the integer 73 | ** cannot hold the whole pointer value 74 | */ 75 | #define IntPoint(p) ((lu_hash)(p)) 76 | 77 | 78 | 79 | /* type to ensure maximum alignment */ 80 | #ifndef LUSER_ALIGNMENT_T 81 | typedef union { double u; void *s; long l; } L_Umaxalign; 82 | #else 83 | typedef LUSER_ALIGNMENT_T L_Umaxalign; 84 | #endif 85 | 86 | 87 | /* result of `usual argument conversion' over lua_Number */ 88 | #ifndef LUA_UACNUMBER 89 | typedef double l_uacNumber; 90 | #else 91 | typedef LUA_UACNUMBER l_uacNumber; 92 | #endif 93 | 94 | 95 | #ifndef lua_assert 96 | #define lua_assert(c) /* empty */ 97 | #endif 98 | 99 | 100 | #ifndef check_exp 101 | #define check_exp(c,e) (e) 102 | #endif 103 | 104 | 105 | #ifndef UNUSED 106 | #define UNUSED(x) ((void)(x)) /* to avoid warnings */ 107 | #endif 108 | 109 | 110 | #ifndef cast 111 | #define cast(t, exp) ((t)(exp)) 112 | #endif 113 | 114 | 115 | 116 | /* 117 | ** type for virtual-machine instructions 118 | ** must be an unsigned with (at least) 4 bytes (see details in lopcodes.h) 119 | */ 120 | #ifdef __LP64__ 121 | typedef uint32_t Instruction; 122 | #else 123 | typedef unsigned long Instruction; 124 | #endif 125 | 126 | 127 | /* maximum depth for calls (unsigned short) */ 128 | #ifndef LUA_MAXCALLS 129 | #define LUA_MAXCALLS 4096 130 | #endif 131 | 132 | 133 | /* 134 | ** maximum depth for C calls (unsigned short): Not too big, or may 135 | ** overflow the C stack... 136 | */ 137 | 138 | #ifndef LUA_MAXCCALLS 139 | #define LUA_MAXCCALLS 200 140 | #endif 141 | 142 | 143 | /* maximum size for the C stack */ 144 | #ifndef LUA_MAXCSTACK 145 | #define LUA_MAXCSTACK 2048 146 | #endif 147 | 148 | 149 | /* maximum stack for a Lua function */ 150 | #define MAXSTACK 250 151 | 152 | 153 | /* maximum number of variables declared in a function */ 154 | #ifndef MAXVARS 155 | #define MAXVARS 200 /* arbitrary limit ( 9 | 10 | #define lmem_c 11 | 12 | #include "lua.h" 13 | 14 | #include "ldebug.h" 15 | #include "ldo.h" 16 | #include "lmem.h" 17 | #include "lobject.h" 18 | #include "lstate.h" 19 | 20 | 21 | 22 | /* 23 | ** definition for realloc function. It must assure that l_realloc(NULL, 24 | ** 0, x) allocates a new block (ANSI C assures that). (`os' is the old 25 | ** block size; some allocators may use that.) 26 | */ 27 | #ifndef l_realloc 28 | #define l_realloc(b,os,s) realloc(b,s) 29 | #endif 30 | 31 | /* 32 | ** definition for free function. (`os' is the old block size; some 33 | ** allocators may use that.) 34 | */ 35 | #ifndef l_free 36 | #define l_free(b,os) free(b) 37 | #endif 38 | 39 | 40 | #define MINSIZEARRAY 4 41 | 42 | 43 | void *luaM_growaux (lua_State *L, void *block, int *size, int size_elems, 44 | int limit, const char *errormsg) { 45 | void *newblock; 46 | int newsize = (*size)*2; 47 | if (newsize < MINSIZEARRAY) 48 | newsize = MINSIZEARRAY; /* minimum size */ 49 | else if (*size >= limit/2) { /* cannot double it? */ 50 | if (*size < limit - MINSIZEARRAY) /* try something smaller... */ 51 | newsize = limit; /* still have at least MINSIZEARRAY free places */ 52 | else luaG_runerror(L, errormsg); 53 | } 54 | newblock = luaM_realloc(L, block, 55 | cast(lu_mem, *size)*cast(lu_mem, size_elems), 56 | cast(lu_mem, newsize)*cast(lu_mem, size_elems)); 57 | *size = newsize; /* update only when everything else is OK */ 58 | return newblock; 59 | } 60 | 61 | 62 | /* 63 | ** generic allocation routine. 64 | */ 65 | void *luaM_realloc (lua_State *L, void *block, lu_mem oldsize, lu_mem size) { 66 | lua_assert((oldsize == 0) == (block == NULL)); 67 | if (size == 0) { 68 | if (block != NULL) { 69 | l_free(block, oldsize); 70 | block = NULL; 71 | } 72 | else return NULL; /* avoid `nblocks' computations when oldsize==size==0 */ 73 | } 74 | else if (size >= MAX_SIZET) 75 | luaG_runerror(L, "memory allocation error: block too big"); 76 | else { 77 | block = l_realloc(block, oldsize, size); 78 | if (block == NULL) { 79 | if (L) 80 | luaD_throw(L, LUA_ERRMEM); 81 | else return NULL; /* error before creating state! */ 82 | } 83 | } 84 | if (L) { 85 | lua_assert(G(L) != NULL && G(L)->nblocks > 0); 86 | G(L)->nblocks -= oldsize; 87 | G(L)->nblocks += size; 88 | } 89 | return block; 90 | } 91 | 92 | -------------------------------------------------------------------------------- /src/lmem.h: -------------------------------------------------------------------------------- 1 | /* 2 | ** $Id: lmem.h,v 1.26 2002/05/01 20:40:42 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 | #define MEMERRMSG "not enough memory" 17 | 18 | 19 | void *luaM_realloc (lua_State *L, void *oldblock, lu_mem oldsize, lu_mem size); 20 | 21 | void *luaM_growaux (lua_State *L, void *block, int *size, int size_elem, 22 | int limit, const char *errormsg); 23 | 24 | #define luaM_free(L, b, s) luaM_realloc(L, (b), (s), 0) 25 | #define luaM_freelem(L, b) luaM_realloc(L, (b), sizeof(*(b)), 0) 26 | #define luaM_freearray(L, b, n, t) luaM_realloc(L, (b), \ 27 | cast(lu_mem, n)*cast(lu_mem, sizeof(t)), 0) 28 | 29 | #define luaM_malloc(L, t) luaM_realloc(L, NULL, 0, (t)) 30 | #define luaM_new(L, t) cast(t *, luaM_malloc(L, sizeof(t))) 31 | #define luaM_newvector(L, n,t) cast(t *, luaM_malloc(L, \ 32 | cast(lu_mem, n)*cast(lu_mem, sizeof(t)))) 33 | 34 | #define luaM_growvector(L,v,nelems,size,t,limit,e) \ 35 | if (((nelems)+1) > (size)) \ 36 | ((v)=cast(t *, luaM_growaux(L,v,&(size),sizeof(t),limit,e))) 37 | 38 | #define luaM_reallocvector(L, v,oldn,n,t) \ 39 | ((v)=cast(t *, luaM_realloc(L, v,cast(lu_mem, oldn)*cast(lu_mem, sizeof(t)), \ 40 | cast(lu_mem, n)*cast(lu_mem, sizeof(t))))) 41 | 42 | 43 | #endif 44 | 45 | -------------------------------------------------------------------------------- /src/lobject.c: -------------------------------------------------------------------------------- 1 | /* 2 | ** $Id: lobject.c,v 1.97 2003/04/03 13:35:34 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 | 14 | #include "lua.h" 15 | 16 | #include "ldo.h" 17 | #include "lmem.h" 18 | #include "lobject.h" 19 | #include "lstate.h" 20 | #include "lstring.h" 21 | #include "lvm.h" 22 | 23 | 24 | /* function to convert a string to a lua_Number */ 25 | #ifndef lua_str2number 26 | #define lua_str2number(s,p) strtod((s), (p)) 27 | #endif 28 | 29 | 30 | const TObject luaO_nilobject = {LUA_TNIL, {NULL}}; 31 | 32 | 33 | /* 34 | ** converts an integer to a "floating point byte", represented as 35 | ** (mmmmmxxx), where the real value is (xxx) * 2^(mmmmm) 36 | */ 37 | int luaO_int2fb (unsigned int x) { 38 | int m = 0; /* mantissa */ 39 | while (x >= (1<<3)) { 40 | x = (x+1) >> 1; 41 | m++; 42 | } 43 | return (m << 3) | cast(int, x); 44 | } 45 | 46 | 47 | int luaO_log2 (unsigned int x) { 48 | static const lu_byte log_8[255] = { 49 | 0, 50 | 1,1, 51 | 2,2,2,2, 52 | 3,3,3,3,3,3,3,3, 53 | 4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4, 54 | 5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5, 55 | 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, 56 | 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, 57 | 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, 58 | 7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7, 59 | 7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7, 60 | 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 | }; 62 | if (x >= 0x00010000) { 63 | if (x >= 0x01000000) return log_8[((x>>24) & 0xff) - 1]+24; 64 | else return log_8[((x>>16) & 0xff) - 1]+16; 65 | } 66 | else { 67 | if (x >= 0x00000100) return log_8[((x>>8) & 0xff) - 1]+8; 68 | else if (x) return log_8[(x & 0xff) - 1]; 69 | return -1; /* special `log' for 0 */ 70 | } 71 | } 72 | 73 | 74 | int luaO_rawequalObj (const TObject *t1, const TObject *t2) { 75 | if (ttype(t1) != ttype(t2)) return 0; 76 | else switch (ttype(t1)) { 77 | case LUA_TNIL: 78 | return 1; 79 | case LUA_TNUMBER: 80 | return nvalue(t1) == nvalue(t2); 81 | case LUA_TBOOLEAN: 82 | return bvalue(t1) == bvalue(t2); /* boolean true must be 1 !! */ 83 | case LUA_TLIGHTUSERDATA: 84 | return pvalue(t1) == pvalue(t2); 85 | default: 86 | lua_assert(iscollectable(t1)); 87 | return gcvalue(t1) == gcvalue(t2); 88 | } 89 | } 90 | 91 | 92 | int luaO_str2d (const char *s, lua_Number *result) { 93 | char *endptr; 94 | lua_Number res = lua_str2number(s, &endptr); 95 | if (endptr == s) return 0; /* no conversion */ 96 | while (isspace((unsigned char)(*endptr))) endptr++; 97 | if (*endptr != '\0') return 0; /* invalid trailing characters? */ 98 | *result = res; 99 | return 1; 100 | } 101 | 102 | 103 | 104 | static void pushstr (lua_State *L, const char *str) { 105 | setsvalue2s(L->top, luaS_new(L, str)); 106 | incr_top(L); 107 | } 108 | 109 | 110 | /* this function handles only `%d', `%c', %f, and `%s' formats */ 111 | const char *luaO_pushvfstring (lua_State *L, const char *fmt, va_list argp) { 112 | int n = 1; 113 | pushstr(L, ""); 114 | for (;;) { 115 | const char *e = strchr(fmt, '%'); 116 | if (e == NULL) break; 117 | setsvalue2s(L->top, luaS_newlstr(L, fmt, e-fmt)); 118 | incr_top(L); 119 | switch (*(e+1)) { 120 | case 's': 121 | pushstr(L, va_arg(argp, char *)); 122 | break; 123 | case 'c': { 124 | char buff[2]; 125 | buff[0] = cast(char, va_arg(argp, int)); 126 | buff[1] = '\0'; 127 | pushstr(L, buff); 128 | break; 129 | } 130 | case 'd': 131 | setnvalue(L->top, cast(lua_Number, va_arg(argp, int))); 132 | incr_top(L); 133 | break; 134 | case 'f': 135 | setnvalue(L->top, cast(lua_Number, va_arg(argp, l_uacNumber))); 136 | incr_top(L); 137 | break; 138 | case '%': 139 | pushstr(L, "%"); 140 | break; 141 | default: lua_assert(0); 142 | } 143 | n += 2; 144 | fmt = e+2; 145 | } 146 | pushstr(L, fmt); 147 | luaV_concat(L, n+1, L->top - L->base - 1); 148 | L->top -= n; 149 | return svalue(L->top - 1); 150 | } 151 | 152 | 153 | const char *luaO_pushfstring (lua_State *L, const char *fmt, ...) { 154 | const char *msg; 155 | va_list argp; 156 | va_start(argp, fmt); 157 | msg = luaO_pushvfstring(L, fmt, argp); 158 | va_end(argp); 159 | return msg; 160 | } 161 | 162 | 163 | void luaO_chunkid (char *out, const char *source, int bufflen) { 164 | if (*source == '=') { 165 | strncpy(out, source+1, bufflen); /* remove first char */ 166 | out[bufflen-1] = '\0'; /* ensures null termination */ 167 | } 168 | else { /* out = "source", or "...source" */ 169 | if (*source == '@') { 170 | int l; 171 | source++; /* skip the `@' */ 172 | bufflen -= sizeof(" `...' "); 173 | l = strlen(source); 174 | strcpy(out, ""); 175 | if (l>bufflen) { 176 | source += (l-bufflen); /* get last part of file name */ 177 | strcat(out, "..."); 178 | } 179 | strcat(out, source); 180 | } 181 | else { /* out = [string "string"] */ 182 | int len = strcspn(source, "\n"); /* stop at first newline */ 183 | bufflen -= sizeof(" [string \"...\"] "); 184 | if (len > bufflen) len = bufflen; 185 | strcpy(out, "[string \""); 186 | if (source[len] != '\0') { /* must truncate? */ 187 | strncat(out, source, len); 188 | strcat(out, "..."); 189 | } 190 | else 191 | strcat(out, source); 192 | strcat(out, "\"]"); 193 | } 194 | } 195 | } 196 | -------------------------------------------------------------------------------- /src/lopcodes.c: -------------------------------------------------------------------------------- 1 | /* 2 | ** $Id: lopcodes.c,v 1.22 2002/12/04 17:38:31 roberto Exp $ 3 | ** extracted automatically from lopcodes.h by mkprint.lua 4 | ** DO NOT EDIT 5 | ** See Copyright Notice in lua.h 6 | */ 7 | 8 | 9 | #define lopcodes_c 10 | 11 | #include "lua.h" 12 | 13 | #include "lobject.h" 14 | #include "lopcodes.h" 15 | 16 | 17 | #ifdef LUA_OPNAMES 18 | 19 | const char *const luaP_opnames[] = { 20 | "MOVE", 21 | "LOADK", 22 | "LOADBOOL", 23 | "LOADNIL", 24 | "GETUPVAL", 25 | "GETGLOBAL", 26 | "GETTABLE", 27 | "SETGLOBAL", 28 | "SETUPVAL", 29 | "SETTABLE", 30 | "NEWTABLE", 31 | "SELF", 32 | "ADD", 33 | "SUB", 34 | "MUL", 35 | "DIV", 36 | "POW", 37 | "UNM", 38 | "NOT", 39 | "CONCAT", 40 | "JMP", 41 | "EQ", 42 | "LT", 43 | "LE", 44 | "TEST", 45 | "CALL", 46 | "TAILCALL", 47 | "RETURN", 48 | "FORLOOP", 49 | "TFORLOOP", 50 | "TFORPREP", 51 | "SETLIST", 52 | "SETLISTO", 53 | "CLOSE", 54 | "CLOSURE" 55 | }; 56 | 57 | #endif 58 | 59 | #define opmode(t,b,bk,ck,sa,k,m) (((t)< 9 | 10 | #define lstate_c 11 | 12 | #include "lua.h" 13 | 14 | #include "ldebug.h" 15 | #include "ldo.h" 16 | #include "lfunc.h" 17 | #include "lgc.h" 18 | #include "llex.h" 19 | #include "lmem.h" 20 | #include "lstate.h" 21 | #include "lstring.h" 22 | #include "ltable.h" 23 | #include "ltm.h" 24 | 25 | 26 | /* 27 | ** macro to allow the inclusion of user information in Lua state 28 | */ 29 | #ifndef LUA_USERSTATE 30 | #define EXTRASPACE 0 31 | #else 32 | union UEXTRASPACE {L_Umaxalign a; LUA_USERSTATE b;}; 33 | #define EXTRASPACE (sizeof(union UEXTRASPACE)) 34 | #endif 35 | 36 | 37 | 38 | /* 39 | ** you can change this function through the official API: 40 | ** call `lua_setpanicf' 41 | */ 42 | static int default_panic (lua_State *L) { 43 | UNUSED(L); 44 | return 0; 45 | } 46 | 47 | 48 | static lua_State *mallocstate (lua_State *L) { 49 | lu_byte *block = (lu_byte *)luaM_malloc(L, sizeof(lua_State) + EXTRASPACE); 50 | if (block == NULL) return NULL; 51 | else { 52 | block += EXTRASPACE; 53 | return cast(lua_State *, block); 54 | } 55 | } 56 | 57 | 58 | static void freestate (lua_State *L, lua_State *L1) { 59 | luaM_free(L, cast(lu_byte *, L1) - EXTRASPACE, 60 | sizeof(lua_State) + EXTRASPACE); 61 | } 62 | 63 | 64 | static void stack_init (lua_State *L1, lua_State *L) { 65 | L1->stack = luaM_newvector(L, BASIC_STACK_SIZE + EXTRA_STACK, TObject); 66 | L1->stacksize = BASIC_STACK_SIZE + EXTRA_STACK; 67 | L1->top = L1->stack; 68 | L1->stack_last = L1->stack+(L1->stacksize - EXTRA_STACK)-1; 69 | L1->base_ci = luaM_newvector(L, BASIC_CI_SIZE, CallInfo); 70 | L1->ci = L1->base_ci; 71 | L1->ci->state = CI_C; /* not a Lua function */ 72 | setnilvalue(L1->top++); /* `function' entry for this `ci' */ 73 | L1->base = L1->ci->base = L1->top; 74 | L1->ci->top = L1->top + LUA_MINSTACK; 75 | L1->size_ci = BASIC_CI_SIZE; 76 | L1->end_ci = L1->base_ci + L1->size_ci; 77 | } 78 | 79 | 80 | static void freestack (lua_State *L, lua_State *L1) { 81 | luaM_freearray(L, L1->base_ci, L1->size_ci, CallInfo); 82 | luaM_freearray(L, L1->stack, L1->stacksize, TObject); 83 | } 84 | 85 | 86 | /* 87 | ** open parts that may cause memory-allocation errors 88 | */ 89 | static void f_luaopen (lua_State *L, void *ud) { 90 | /* create a new global state */ 91 | global_State *g = luaM_new(NULL, global_State); 92 | UNUSED(ud); 93 | if (g == NULL) luaD_throw(L, LUA_ERRMEM); 94 | L->l_G = g; 95 | g->mainthread = L; 96 | g->GCthreshold = 0; /* mark it as unfinished state */ 97 | g->strt.size = 0; 98 | g->strt.nuse = 0; 99 | g->strt.hash = NULL; 100 | setnilvalue(defaultmeta(L)); 101 | setnilvalue(registry(L)); 102 | luaZ_initbuffer(L, &g->buff); 103 | g->panic = default_panic; 104 | g->rootgc = NULL; 105 | g->rootudata = NULL; 106 | g->tmudata = NULL; 107 | setnilvalue(gkey(g->dummynode)); 108 | setnilvalue(gval(g->dummynode)); 109 | g->dummynode->next = NULL; 110 | g->nblocks = sizeof(lua_State) + sizeof(global_State); 111 | stack_init(L, L); /* init stack */ 112 | /* create default meta table with a dummy table, and then close the loop */ 113 | defaultmeta(L)->tt = LUA_TTABLE; 114 | sethvalue(defaultmeta(L), luaH_new(L, 0, 0)); 115 | hvalue(defaultmeta(L))->metatable = hvalue(defaultmeta(L)); 116 | sethvalue(gt(L), luaH_new(L, 0, 4)); /* table of globals */ 117 | sethvalue(registry(L), luaH_new(L, 4, 4)); /* registry */ 118 | luaS_resize(L, MINSTRTABSIZE); /* initial size of string table */ 119 | luaT_init(L); 120 | luaX_init(L); 121 | luaS_fix(luaS_newliteral(L, MEMERRMSG)); 122 | g->GCthreshold = 4*G(L)->nblocks; 123 | } 124 | 125 | 126 | static void preinit_state (lua_State *L) { 127 | L->stack = NULL; 128 | L->stacksize = 0; 129 | L->errorJmp = NULL; 130 | L->hook = NULL; 131 | L->hookmask = L->hookinit = 0; 132 | L->basehookcount = 0; 133 | L->allowhook = 1; 134 | resethookcount(L); 135 | L->openupval = NULL; 136 | L->size_ci = 0; 137 | L->nCcalls = 0; 138 | L->base_ci = L->ci = NULL; 139 | L->errfunc = 0; 140 | setnilvalue(gt(L)); 141 | } 142 | 143 | 144 | static void close_state (lua_State *L) { 145 | luaF_close(L, L->stack); /* close all upvalues for this thread */ 146 | if (G(L)) { /* close global state */ 147 | luaC_sweep(L, 1); /* collect all elements */ 148 | lua_assert(G(L)->rootgc == NULL); 149 | lua_assert(G(L)->rootudata == NULL); 150 | luaS_freeall(L); 151 | luaZ_freebuffer(L, &G(L)->buff); 152 | } 153 | freestack(L, L); 154 | if (G(L)) { 155 | lua_assert(G(L)->nblocks == sizeof(lua_State) + sizeof(global_State)); 156 | luaM_freelem(NULL, G(L)); 157 | } 158 | freestate(NULL, L); 159 | } 160 | 161 | 162 | lua_State *luaE_newthread (lua_State *L) { 163 | lua_State *L1 = mallocstate(L); 164 | luaC_link(L, valtogco(L1), LUA_TTHREAD); 165 | preinit_state(L1); 166 | L1->l_G = L->l_G; 167 | stack_init(L1, L); /* init stack */ 168 | setobj2n(gt(L1), gt(L)); /* share table of globals */ 169 | return L1; 170 | } 171 | 172 | 173 | void luaE_freethread (lua_State *L, lua_State *L1) { 174 | luaF_close(L1, L1->stack); /* close all upvalues for this thread */ 175 | lua_assert(L1->openupval == NULL); 176 | freestack(L, L1); 177 | freestate(L, L1); 178 | } 179 | 180 | 181 | LUA_API lua_State *lua_open (void) { 182 | lua_State *L = mallocstate(NULL); 183 | if (L) { /* allocation OK? */ 184 | L->tt = LUA_TTHREAD; 185 | L->marked = 0; 186 | L->next = L->gclist = NULL; 187 | preinit_state(L); 188 | L->l_G = NULL; 189 | if (luaD_rawrunprotected(L, f_luaopen, NULL) != 0) { 190 | /* memory allocation error: free partial state */ 191 | close_state(L); 192 | L = NULL; 193 | } 194 | } 195 | lua_userstateopen(L); 196 | return L; 197 | } 198 | 199 | 200 | static void callallgcTM (lua_State *L, void *ud) { 201 | UNUSED(ud); 202 | luaC_callGCTM(L); /* call GC metamethods for all udata */ 203 | } 204 | 205 | 206 | LUA_API void lua_close (lua_State *L) { 207 | lua_lock(L); 208 | L = G(L)->mainthread; /* only the main thread can be closed */ 209 | luaF_close(L, L->stack); /* close all upvalues for this thread */ 210 | luaC_separateudata(L); /* separate udata that have GC metamethods */ 211 | L->errfunc = 0; /* no error function during GC metamethods */ 212 | do { /* repeat until no more errors */ 213 | L->ci = L->base_ci; 214 | L->base = L->top = L->ci->base; 215 | L->nCcalls = 0; 216 | } while (luaD_rawrunprotected(L, callallgcTM, NULL) != 0); 217 | lua_assert(G(L)->tmudata == NULL); 218 | close_state(L); 219 | } 220 | 221 | -------------------------------------------------------------------------------- /src/lstate.h: -------------------------------------------------------------------------------- 1 | /* 2 | ** $Id: lstate.h,v 1.109 2003/02/27 11:52:30 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 | ** macros for thread synchronization inside Lua core machine: 19 | ** all accesses to the global state and to global objects are synchronized. 20 | ** Because threads can read the stack of other threads 21 | ** (when running garbage collection), 22 | ** a thread must also synchronize any write-access to its own stack. 23 | ** Unsynchronized accesses are allowed only when reading its own stack, 24 | ** or when reading immutable fields from global objects 25 | ** (such as string values and udata values). 26 | */ 27 | #ifndef lua_lock 28 | #define lua_lock(L) ((void) 0) 29 | #endif 30 | 31 | #ifndef lua_unlock 32 | #define lua_unlock(L) ((void) 0) 33 | #endif 34 | 35 | 36 | #ifndef lua_userstateopen 37 | #define lua_userstateopen(l) 38 | #endif 39 | 40 | 41 | 42 | struct lua_longjmp; /* defined in ldo.c */ 43 | 44 | 45 | /* default meta table (both for tables and udata) */ 46 | #define defaultmeta(L) (&G(L)->_defaultmeta) 47 | 48 | /* table of globals */ 49 | #define gt(L) (&L->_gt) 50 | 51 | /* registry */ 52 | #define registry(L) (&G(L)->_registry) 53 | 54 | 55 | /* extra stack space to handle TM calls and some other extras */ 56 | #define EXTRA_STACK 5 57 | 58 | 59 | #define BASIC_CI_SIZE 8 60 | 61 | #define BASIC_STACK_SIZE (2*LUA_MINSTACK) 62 | 63 | 64 | 65 | typedef struct stringtable { 66 | GCObject **hash; 67 | ls_nstr nuse; /* number of elements */ 68 | int size; 69 | } stringtable; 70 | 71 | 72 | /* 73 | ** informations about a call 74 | */ 75 | typedef struct CallInfo { 76 | StkId base; /* base for called function */ 77 | StkId top; /* top for this function */ 78 | int state; /* bit fields; see below */ 79 | union { 80 | struct { /* for Lua functions */ 81 | const Instruction *savedpc; 82 | const Instruction **pc; /* points to `pc' variable in `luaV_execute' */ 83 | int tailcalls; /* number of tail calls lost under this entry */ 84 | } l; 85 | struct { /* for C functions */ 86 | int dummy; /* just to avoid an empty struct */ 87 | } c; 88 | } u; 89 | } CallInfo; 90 | 91 | 92 | /* 93 | ** bit fields for `CallInfo.state' 94 | */ 95 | #define CI_C (1<<0) /* 1 if function is a C function */ 96 | /* 1 if (Lua) function has an active `luaV_execute' running it */ 97 | #define CI_HASFRAME (1<<1) 98 | /* 1 if Lua function is calling another Lua function (and therefore its 99 | `pc' is being used by the other, and therefore CI_SAVEDPC is 1 too) */ 100 | #define CI_CALLING (1<<2) 101 | #define CI_SAVEDPC (1<<3) /* 1 if `savedpc' is updated */ 102 | #define CI_YIELD (1<<4) /* 1 if thread is suspended */ 103 | 104 | 105 | #define ci_func(ci) (clvalue((ci)->base - 1)) 106 | 107 | 108 | /* 109 | ** `global state', shared by all threads of this state 110 | */ 111 | typedef struct global_State { 112 | stringtable strt; /* hash table for strings */ 113 | GCObject *rootgc; /* list of (almost) all collectable objects */ 114 | GCObject *rootudata; /* (separated) list of all userdata */ 115 | GCObject *tmudata; /* list of userdata to be GC */ 116 | Mbuffer buff; /* temporary buffer for string concatentation */ 117 | lu_mem GCthreshold; 118 | lu_mem nblocks; /* number of `bytes' currently allocated */ 119 | lua_CFunction panic; /* to be called in unprotected errors */ 120 | TObject _registry; 121 | TObject _defaultmeta; 122 | struct lua_State *mainthread; 123 | Node dummynode[1]; /* common node array for all empty tables */ 124 | TString *tmname[TM_N]; /* array with tag-method names */ 125 | } global_State; 126 | 127 | 128 | /* 129 | ** `per thread' state 130 | */ 131 | struct lua_State { 132 | CommonHeader; 133 | StkId top; /* first free slot in the stack */ 134 | StkId base; /* base of current function */ 135 | global_State *l_G; 136 | CallInfo *ci; /* call info for current function */ 137 | StkId stack_last; /* last free slot in the stack */ 138 | StkId stack; /* stack base */ 139 | int stacksize; 140 | CallInfo *end_ci; /* points after end of ci array*/ 141 | CallInfo *base_ci; /* array of CallInfo's */ 142 | unsigned short size_ci; /* size of array `base_ci' */ 143 | unsigned short nCcalls; /* number of nested C calls */ 144 | lu_byte hookmask; 145 | lu_byte allowhook; 146 | lu_byte hookinit; 147 | int basehookcount; 148 | int hookcount; 149 | lua_Hook hook; 150 | TObject _gt; /* table of globals */ 151 | GCObject *openupval; /* list of open upvalues in this stack */ 152 | GCObject *gclist; 153 | struct lua_longjmp *errorJmp; /* current error recover point */ 154 | ptrdiff_t errfunc; /* current error handling function (stack index) */ 155 | }; 156 | 157 | 158 | #define G(L) (L->l_G) 159 | 160 | 161 | /* 162 | ** Union of all collectable objects 163 | */ 164 | union GCObject { 165 | GCheader gch; 166 | union TString ts; 167 | union Udata u; 168 | union Closure cl; 169 | struct Table h; 170 | struct Proto p; 171 | struct UpVal uv; 172 | struct lua_State th; /* thread */ 173 | }; 174 | 175 | 176 | /* macros to convert a GCObject into a specific value */ 177 | #define gcotots(o) check_exp((o)->gch.tt == LUA_TSTRING, &((o)->ts)) 178 | #define gcotou(o) check_exp((o)->gch.tt == LUA_TUSERDATA, &((o)->u)) 179 | #define gcotocl(o) check_exp((o)->gch.tt == LUA_TFUNCTION, &((o)->cl)) 180 | #define gcotoh(o) check_exp((o)->gch.tt == LUA_TTABLE, &((o)->h)) 181 | #define gcotop(o) check_exp((o)->gch.tt == LUA_TPROTO, &((o)->p)) 182 | #define gcotouv(o) check_exp((o)->gch.tt == LUA_TUPVAL, &((o)->uv)) 183 | #define ngcotouv(o) \ 184 | check_exp((o) == NULL || (o)->gch.tt == LUA_TUPVAL, &((o)->uv)) 185 | #define gcototh(o) check_exp((o)->gch.tt == LUA_TTHREAD, &((o)->th)) 186 | 187 | /* macro to convert any value into a GCObject */ 188 | #define valtogco(v) (cast(GCObject *, (v))) 189 | 190 | 191 | lua_State *luaE_newthread (lua_State *L); 192 | void luaE_freethread (lua_State *L, lua_State *L1); 193 | 194 | #endif 195 | 196 | -------------------------------------------------------------------------------- /src/lstring.c: -------------------------------------------------------------------------------- 1 | /* 2 | ** $Id: lstring.c,v 1.78 2002/12/04 17:38:31 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 | 12 | #include "lua.h" 13 | 14 | #include "lmem.h" 15 | #include "lobject.h" 16 | #include "lstate.h" 17 | #include "lstring.h" 18 | 19 | 20 | 21 | void luaS_freeall (lua_State *L) { 22 | lua_assert(G(L)->strt.nuse==0); 23 | luaM_freearray(L, G(L)->strt.hash, G(L)->strt.size, TString *); 24 | } 25 | 26 | 27 | void luaS_resize (lua_State *L, int newsize) { 28 | GCObject **newhash = luaM_newvector(L, newsize, GCObject *); 29 | stringtable *tb = &G(L)->strt; 30 | int i; 31 | for (i=0; isize; i++) { 34 | GCObject *p = tb->hash[i]; 35 | while (p) { /* for each node in the list */ 36 | GCObject *next = p->gch.next; /* save next */ 37 | lu_hash h = gcotots(p)->tsv.hash; 38 | int h1 = lmod(h, newsize); /* new position */ 39 | lua_assert(cast(int, h%newsize) == lmod(h, newsize)); 40 | p->gch.next = newhash[h1]; /* chain it */ 41 | newhash[h1] = p; 42 | p = next; 43 | } 44 | } 45 | luaM_freearray(L, tb->hash, tb->size, TString *); 46 | tb->size = newsize; 47 | tb->hash = newhash; 48 | } 49 | 50 | 51 | static TString *newlstr (lua_State *L, const char *str, size_t l, lu_hash h) { 52 | TString *ts = cast(TString *, luaM_malloc(L, sizestring(l))); 53 | stringtable *tb; 54 | ts->tsv.len = l; 55 | ts->tsv.hash = h; 56 | ts->tsv.marked = 0; 57 | ts->tsv.tt = LUA_TSTRING; 58 | ts->tsv.reserved = 0; 59 | memcpy(ts+1, str, l*sizeof(char)); 60 | ((char *)(ts+1))[l] = '\0'; /* ending 0 */ 61 | tb = &G(L)->strt; 62 | h = lmod(h, tb->size); 63 | ts->tsv.next = tb->hash[h]; /* chain new entry */ 64 | tb->hash[h] = valtogco(ts); 65 | tb->nuse++; 66 | if (tb->nuse > cast(ls_nstr, tb->size) && tb->size <= MAX_INT/2) 67 | luaS_resize(L, tb->size*2); /* too crowded */ 68 | return ts; 69 | } 70 | 71 | 72 | TString *luaS_newlstr (lua_State *L, const char *str, size_t l) { 73 | GCObject *o; 74 | lu_hash h = (lu_hash)l; /* seed */ 75 | size_t step = (l>>5)+1; /* if string is too long, don't hash all its chars */ 76 | size_t l1; 77 | for (l1=l; l1>=step; l1-=step) /* compute hash */ 78 | h = h ^ ((h<<5)+(h>>2)+(unsigned char)(str[l1-1])); 79 | for (o = G(L)->strt.hash[lmod(h, G(L)->strt.size)]; 80 | o != NULL; 81 | o = o->gch.next) { 82 | TString *ts = gcotots(o); 83 | if (ts->tsv.len == l && (memcmp(str, getstr(ts), l) == 0)) 84 | return ts; 85 | } 86 | return newlstr(L, str, l, h); /* not found */ 87 | } 88 | 89 | 90 | Udata *luaS_newudata (lua_State *L, size_t s) { 91 | Udata *u; 92 | u = cast(Udata *, luaM_malloc(L, sizeudata(s))); 93 | u->uv.marked = (1<<1); /* is not finalized */ 94 | u->uv.tt = LUA_TUSERDATA; 95 | u->uv.len = s; 96 | u->uv.metatable = hvalue(defaultmeta(L)); 97 | /* chain it on udata list */ 98 | u->uv.next = G(L)->rootudata; 99 | G(L)->rootudata = valtogco(u); 100 | return u; 101 | } 102 | 103 | -------------------------------------------------------------------------------- /src/lstring.h: -------------------------------------------------------------------------------- 1 | /* 2 | ** $Id: lstring.h,v 1.37 2002/08/16 14:45:55 roberto Exp $ 3 | ** String table (keep all strings handled by Lua) 4 | ** See Copyright Notice in lua.h 5 | */ 6 | 7 | #ifndef lstring_h 8 | #define lstring_h 9 | 10 | 11 | #include "lobject.h" 12 | #include "lstate.h" 13 | 14 | 15 | 16 | #define sizestring(l) (cast(lu_mem, sizeof(union TString))+ \ 17 | (cast(lu_mem, l)+1)*sizeof(char)) 18 | 19 | #define sizeudata(l) (cast(lu_mem, sizeof(union Udata))+(l)) 20 | 21 | #define luaS_new(L, s) (luaS_newlstr(L, s, strlen(s))) 22 | #define luaS_newliteral(L, s) (luaS_newlstr(L, "" s, \ 23 | (sizeof(s)/sizeof(char))-1)) 24 | 25 | #define luaS_fix(s) ((s)->tsv.marked |= (1<<4)) 26 | 27 | void luaS_resize (lua_State *L, int newsize); 28 | Udata *luaS_newudata (lua_State *L, size_t s); 29 | void luaS_freeall (lua_State *L); 30 | TString *luaS_newlstr (lua_State *L, const char *str, size_t l); 31 | 32 | 33 | #endif 34 | -------------------------------------------------------------------------------- /src/ltable.h: -------------------------------------------------------------------------------- 1 | /* 2 | ** $Id: ltable.h,v 1.44 2003/03/18 12:50:04 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) 15 | #define gval(n) (&(n)->i_val) 16 | 17 | 18 | const TObject *luaH_getnum (Table *t, int key); 19 | TObject *luaH_setnum (lua_State *L, Table *t, int key); 20 | const TObject *luaH_getstr (Table *t, TString *key); 21 | const TObject *luaH_get (Table *t, const TObject *key); 22 | TObject *luaH_set (lua_State *L, Table *t, const TObject *key); 23 | Table *luaH_new (lua_State *L, int narray, int lnhash); 24 | void luaH_free (lua_State *L, Table *t); 25 | int luaH_next (lua_State *L, Table *t, StkId key); 26 | 27 | /* exported only for debugging */ 28 | Node *luaH_mainposition (const Table *t, const TObject *key); 29 | 30 | 31 | #endif 32 | -------------------------------------------------------------------------------- /src/ltm.c: -------------------------------------------------------------------------------- 1 | /* 2 | ** $Id: ltm.c,v 1.106 2003/04/03 13:35:34 roberto Exp $ 3 | ** Tag methods 4 | ** See Copyright Notice in lua.h 5 | */ 6 | 7 | 8 | #include 9 | 10 | #define ltm_c 11 | 12 | #include "lua.h" 13 | 14 | #include "lobject.h" 15 | #include "lstate.h" 16 | #include "lstring.h" 17 | #include "ltable.h" 18 | #include "ltm.h" 19 | 20 | 21 | 22 | const char *const luaT_typenames[] = { 23 | "nil", "boolean", "userdata", "number", 24 | "string", "table", "function", "userdata", "thread" 25 | }; 26 | 27 | 28 | void luaT_init (lua_State *L) { 29 | static const char *const luaT_eventname[] = { /* ORDER TM */ 30 | "__index", "__newindex", 31 | "__gc", "__mode", "__eq", 32 | "__add", "__sub", "__mul", "__div", 33 | "__pow", "__unm", "__lt", "__le", 34 | "__concat", "__call" 35 | }; 36 | int i; 37 | for (i=0; itmname[i] = luaS_new(L, luaT_eventname[i]); 39 | luaS_fix(G(L)->tmname[i]); /* never collect these names */ 40 | } 41 | } 42 | 43 | 44 | /* 45 | ** function to be used with macro "fasttm": optimized for absence of 46 | ** tag methods 47 | */ 48 | const TObject *luaT_gettm (Table *events, TMS event, TString *ename) { 49 | const TObject *tm = luaH_getstr(events, ename); 50 | lua_assert(event <= TM_EQ); 51 | if (ttisnil(tm)) { /* no tag method? */ 52 | events->flags |= cast(lu_byte, 1u<tmname[event]; 61 | switch (ttype(o)) { 62 | case LUA_TTABLE: 63 | return luaH_getstr(hvalue(o)->metatable, ename); 64 | case LUA_TUSERDATA: 65 | return luaH_getstr(uvalue(o)->uv.metatable, ename); 66 | default: 67 | return &luaO_nilobject; 68 | } 69 | } 70 | 71 | -------------------------------------------------------------------------------- /src/ltm.h: -------------------------------------------------------------------------------- 1 | /* 2 | ** $Id: ltm.h,v 1.41 2002/11/14 11:51:50 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_EQ, /* last tag method with `fast' access */ 24 | TM_ADD, 25 | TM_SUB, 26 | TM_MUL, 27 | TM_DIV, 28 | TM_POW, 29 | TM_UNM, 30 | TM_LT, 31 | TM_LE, 32 | TM_CONCAT, 33 | TM_CALL, 34 | TM_N /* number of elements in the enum */ 35 | } TMS; 36 | 37 | 38 | 39 | #define gfasttm(g,et,e) \ 40 | (((et)->flags & (1u<<(e))) ? NULL : luaT_gettm(et, e, (g)->tmname[e])) 41 | 42 | #define fasttm(l,et,e) gfasttm(G(l), et, e) 43 | 44 | 45 | const TObject *luaT_gettm (Table *events, TMS event, TString *ename); 46 | const TObject *luaT_gettmbyobj (lua_State *L, const TObject *o, TMS event); 47 | void luaT_init (lua_State *L); 48 | 49 | extern const char *const luaT_typenames[]; 50 | 51 | #endif 52 | -------------------------------------------------------------------------------- /src/lua/Makefile: -------------------------------------------------------------------------------- 1 | # makefile for Lua interpreter 2 | 3 | LUA= ../.. 4 | 5 | include $(LUA)/config 6 | 7 | EXTRA_DEFS= $(USERCONF) 8 | OBJS= lua.o 9 | SRCS= lua.c 10 | 11 | T= $(BIN)/lua 12 | 13 | all: $T 14 | 15 | $T: $(OBJS) $(LIB)/liblua.a $(LIB)/liblualib.a 16 | $(CC) -o $@ $(MYLDFLAGS) $(OBJS) -L$(LIB) -llua -llualib $(EXTRA_LIBS) $(DLLIB) 17 | 18 | $(LIB)/liblua.a: 19 | cd ..; $(MAKE) 20 | 21 | $(LIB)/liblualib.a: 22 | cd ../lib; $(MAKE) 23 | 24 | clean: 25 | rm -f $(OBJS) $T 26 | 27 | co: 28 | co -q -f -M $(SRCS) 29 | 30 | klean: clean 31 | rm -f $(SRCS) 32 | -------------------------------------------------------------------------------- /src/lua/README: -------------------------------------------------------------------------------- 1 | This is lua, a sample Lua interpreter. 2 | It can be used as a batch interpreter and also interactively. 3 | There are man pages for it in both nroff and html in ../../doc. 4 | 5 | Usage: ./lua [options] [script [args]]. Available options are: 6 | - execute stdin as a file 7 | -e stat execute string `stat' 8 | -i enter interactive mode after executing `script' 9 | -l name load and run library `name' 10 | -v show version information 11 | -- stop handling options 12 | 13 | This interpreter is suitable for using Lua as a standalone language; it loads 14 | all standard libraries. For a minimal interpreter, see ../../etc/min.c. 15 | 16 | If your application simply exports new functions to Lua (which is common), 17 | then you can use this interpreter (almost) unmodified, as follows: 18 | 19 | * First, define a function 20 | void myinit (lua_State *L) 21 | in your own code. In this function, you should do whatever initializations 22 | are needed by your application, typically exporting your functions to Lua. 23 | (Of course, you can use any name instead of "myinit".) 24 | 25 | * Then, #define lua_userinit(L) to be "openstdlibs(L)+myinit(L)". 26 | Here, openstdlibs is a function in lua.c that opens all standard libraries. 27 | If you don't need them, just don't call openstdlibs and open any standard 28 | libraries that you do need in myinit. 29 | 30 | * Finally, remember to link your C code when building lua. 31 | 32 | For other customizations, see ../../etc/config.c. 33 | -------------------------------------------------------------------------------- /src/luac/Makefile: -------------------------------------------------------------------------------- 1 | # makefile for Lua compiler 2 | 3 | LUA= ../.. 4 | 5 | include $(LUA)/config 6 | 7 | INCS= -I$(INC) -I.. $(EXTRA_INCS) 8 | OBJS= luac.o print.o lopcodes.o 9 | SRCS= luac.c print.c 10 | POBJS= pluac.o pdb.o print.o lopcodes.o 11 | PSRCS= pluac.c pdb.c print.c 12 | 13 | T= $(BIN)/luac 14 | PT= $(BIN)/plua2c 15 | 16 | all: $T $(PT) 17 | 18 | $T: $(OBJS) $(LIB)/liblua.a $(LIB)/liblualib.a 19 | $(CC) -o $@ $(MYLDFLAGS) $(OBJS) -L$(LIB) -llua -llualib $(EXTRA_LIBS) $(DLLIB) 20 | 21 | $(PT): $(POBJS) $(LIB)/liblua.a $(LIB)/liblualib.a 22 | $(CC) -o $@ $(MYLDFLAGS) $(POBJS) -L$(LIB) -llua -llualib $(EXTRA_LIBS) 23 | $(DLLIB) 24 | 25 | # print.c needs opcode names from lopcodes.c 26 | lopcodes.o: ../lopcodes.c ../lopcodes.h 27 | $(CC) -o $@ -c $(CFLAGS) -DLUA_OPNAMES ../lopcodes.c 28 | 29 | $(LIB)/liblua.a: 30 | cd ..; $(MAKE) 31 | 32 | $(LIB)/liblualib.a: 33 | cd ../lib; $(MAKE) 34 | 35 | clean: 36 | rm -f $(OBJS) $T $(POBJS) $(PT) 37 | 38 | co: 39 | co -q -f -M $(SRCS) 40 | 41 | klean: clean 42 | rm -f $(SRCS) 43 | -------------------------------------------------------------------------------- /src/luac/Makefile.cross: -------------------------------------------------------------------------------- 1 | # makefile for Lua compiler 2 | 3 | LUA= ../.. 4 | 5 | include $(LUA)/config.cross 6 | 7 | POBJS= cross/pluac.o cross/pdb.o cross/print.o cross/lopcodes.o 8 | 9 | PT= $(BIN)/plua2c.exe 10 | 11 | cross/%.o: %.c 12 | $(CC) $(CFLAGS) -c $< -o $@ 13 | 14 | all: $(PT) 15 | 16 | $(PT): $(POBJS) $(LIB)/liblua.a $(LIB)/liblualib.a 17 | $(CC) -o $@ $(MYLDFLAGS) $(POBJS) -L$(LIB) -llua -llualib $(EXTRA_LIBS) $(DLLIB) 18 | $(STRIP) $@ 19 | 20 | # print.c needs opcode names from lopcodes.c 21 | cross/lopcodes.o: ../lopcodes.c ../lopcodes.h 22 | $(CC) -o $@ -c $(CFLAGS) -DLUA_OPNAMES ../lopcodes.c 23 | 24 | clean: 25 | rm -f $(POBJS) $(PT) 26 | -------------------------------------------------------------------------------- /src/luac/Makefile.macos: -------------------------------------------------------------------------------- 1 | # makefile for Lua compiler 2 | 3 | LUA= ../.. 4 | 5 | include $(LUA)/config.macos 6 | 7 | POBJS= macos/pluac.o macos/pdb.o macos/print.o macos/lopcodes.o 8 | 9 | PT= $(BIN)/plua2c 10 | 11 | macos/%.o: %.c 12 | $(CC) $(CFLAGS) -c $< -o $@ 13 | 14 | all: $(PT) 15 | 16 | $(PT): $(POBJS) $(LIB)/liblua.a $(LIB)/liblualib.a 17 | $(CC) -o $@ $(MYLDFLAGS) $(POBJS) -L$(LIB) -llua -llualib $(EXTRA_LIBS) $(DLLIB) 18 | $(STRIP) $@ 19 | 20 | # print.c needs opcode names from lopcodes.c 21 | macos/lopcodes.o: ../lopcodes.c ../lopcodes.h 22 | $(CC) -o $@ -c $(CFLAGS) -DLUA_OPNAMES ../lopcodes.c 23 | 24 | clean: 25 | rm -f $(POBJS) $(PT) 26 | -------------------------------------------------------------------------------- /src/luac/README: -------------------------------------------------------------------------------- 1 | This is luac, the Lua compiler. 2 | There are man pages for it in both nroff and html in ../../doc. 3 | 4 | luac translates Lua programs into binary files that can be loaded latter. 5 | The main advantages of pre-compiling chunks are: faster loading, protecting 6 | source code from user changes, and off-line syntax error detection. 7 | luac can also be used to learn about the Lua virtual machine. 8 | 9 | Usage: /l/luac/luac [options] [filenames]. Available options are: 10 | - process stdin 11 | -l list 12 | -o name output to file `name' (default is "luac.out") 13 | -p parse only 14 | -s strip debug information 15 | -v show version information 16 | -- stop handling options 17 | 18 | luac is also an example of how to use the internals of Lua (politely). 19 | -------------------------------------------------------------------------------- /src/luac/luac.c: -------------------------------------------------------------------------------- 1 | /* 2 | ** $Id: luac.c,v 1.44a 2003/04/07 20:34:20 lhf Exp $ 3 | ** Lua compiler (saves bytecodes to files; also list bytecodes) 4 | ** See Copyright Notice in lua.h 5 | */ 6 | 7 | #include 8 | #include 9 | #include 10 | 11 | #include "lua.h" 12 | #include "lauxlib.h" 13 | 14 | #include "lfunc.h" 15 | #include "lmem.h" 16 | #include "lobject.h" 17 | #include "lopcodes.h" 18 | #include "lstring.h" 19 | #include "lundump.h" 20 | 21 | #ifndef LUA_DEBUG 22 | #define luaB_opentests(L) 23 | #endif 24 | 25 | #ifndef PROGNAME 26 | #define PROGNAME "luac" /* program name */ 27 | #endif 28 | 29 | #define OUTPUT "luac.out" /* default output file */ 30 | 31 | static int listing=0; /* list bytecodes? */ 32 | static int dumping=1; /* dump bytecodes? */ 33 | static int stripping=0; /* strip debug information? */ 34 | static char Output[]={ OUTPUT }; /* default output file name */ 35 | static const char* output=Output; /* output file name */ 36 | static const char* progname=PROGNAME; /* actual program name */ 37 | 38 | static void fatal(const char* message) 39 | { 40 | fprintf(stderr,"%s: %s\n",progname,message); 41 | exit(EXIT_FAILURE); 42 | } 43 | 44 | static void cannot(const char* name, const char* what, const char* mode) 45 | { 46 | fprintf(stderr,"%s: cannot %s %sput file ",progname,what,mode); 47 | perror(name); 48 | exit(EXIT_FAILURE); 49 | } 50 | 51 | static void usage(const char* message, const char* arg) 52 | { 53 | if (message!=NULL) 54 | { 55 | fprintf(stderr,"%s: ",progname); fprintf(stderr,message,arg); fprintf(stderr,"\n"); 56 | } 57 | fprintf(stderr, 58 | "usage: %s [options] [filenames]. Available options are:\n" 59 | " - process stdin\n" 60 | " -l list\n" 61 | " -o name output to file `name' (default is \"" OUTPUT "\")\n" 62 | " -p parse only\n" 63 | " -s strip debug information\n" 64 | " -v show version information\n" 65 | " -- stop handling options\n", 66 | progname); 67 | exit(EXIT_FAILURE); 68 | } 69 | 70 | #define IS(s) (strcmp(argv[i],s)==0) 71 | 72 | static int doargs(int argc, char* argv[]) 73 | { 74 | int i; 75 | if (argv[0]!=NULL && *argv[0]!=0) progname=argv[0]; 76 | for (i=1; il.p; 118 | } 119 | 120 | static Proto* combine(lua_State* L, int n) 121 | { 122 | if (n==1) 123 | return toproto(L,-1); 124 | else 125 | { 126 | int i,pc=0; 127 | Proto* f=luaF_newproto(L); 128 | f->source=luaS_newliteral(L,"=(" PROGNAME ")"); 129 | f->maxstacksize=1; 130 | f->p=luaM_newvector(L,n,Proto*); 131 | f->sizep=n; 132 | f->sizecode=2*n+1; 133 | f->code=luaM_newvector(L,f->sizecode,Instruction); 134 | for (i=0; ip[i]=toproto(L,i-n); 137 | f->code[pc++]=CREATE_ABx(OP_CLOSURE,0,i); 138 | f->code[pc++]=CREATE_ABC(OP_CALL,0,1,1); 139 | } 140 | f->code[pc++]=CREATE_ABC(OP_RETURN,0,1,0); 141 | return f; 142 | } 143 | } 144 | 145 | static void strip(lua_State* L, Proto* f) 146 | { 147 | int i,n=f->sizep; 148 | luaM_freearray(L, f->lineinfo, f->sizelineinfo, int); 149 | luaM_freearray(L, f->locvars, f->sizelocvars, struct LocVar); 150 | luaM_freearray(L, f->upvalues, f->sizeupvalues, TString *); 151 | f->lineinfo=NULL; f->sizelineinfo=0; 152 | f->locvars=NULL; f->sizelocvars=0; 153 | f->upvalues=NULL; f->sizeupvalues=0; 154 | f->source=luaS_newliteral(L,"=(none)"); 155 | for (i=0; ip[i]); 156 | } 157 | 158 | static int writer(lua_State* L, const void* p, size_t size, void* u) 159 | { 160 | UNUSED(L); 161 | return fwrite(p,size,1,(FILE*)u)==1; 162 | } 163 | 164 | int main(int argc, char* argv[]) 165 | { 166 | lua_State* L; 167 | Proto* f; 168 | int i=doargs(argc,argv); 169 | argc-=i; argv+=i; 170 | if (argc<=0) usage("no input files given",NULL); 171 | L=lua_open(); 172 | luaB_opentests(L); 173 | for (i=0; i 2 | #include 3 | #include 4 | #include 5 | #include 6 | #include 7 | #include 8 | #include 9 | 10 | #include "pdb.h" 11 | 12 | #if defined(__LITTLE_ENDIAN__) 13 | #ifndef IS_LITTLE_ENDIAN 14 | #define IS_LITTLE_ENDIAN 1 15 | #endif 16 | #endif 17 | 18 | #if defined(__BYTE_ORDER) && (__BYTE_ORDER == __LITTLE_ENDIAN) 19 | #ifndef IS_LITTLE_ENDIAN 20 | #define IS_LITTLE_ENDIAN 1 21 | #endif 22 | #endif 23 | 24 | #if defined(__BYTE_ORDER__) && (__BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__) 25 | #ifndef IS_LITTLE_ENDIAN 26 | #define IS_LITTLE_ENDIAN 1 27 | #endif 28 | #endif 29 | 30 | void pdb_write16(void *addr, UInt16 data) 31 | { 32 | UInt8* to = (UInt8*)addr; 33 | UInt8* from = (UInt8*)&data; 34 | 35 | #ifdef IS_LITTLE_ENDIAN 36 | to[0] = from[1]; 37 | to[1] = from[0]; 38 | #else 39 | to[0] = from[0]; 40 | to[1] = from[1]; 41 | #endif 42 | } 43 | 44 | UInt16 pdb_read16(void *addr) 45 | { 46 | UInt16 data; 47 | UInt8* to = (UInt8*)&data; 48 | UInt8* from = (UInt8*)addr; 49 | 50 | #ifdef IS_LITTLE_ENDIAN 51 | to[0] = from[1]; 52 | to[1] = from[0]; 53 | #else 54 | to[0] = from[0]; 55 | to[1] = from[1]; 56 | #endif 57 | 58 | return data; 59 | } 60 | 61 | void pdb_write32(void *addr, UInt32 data) 62 | { 63 | UInt8* to = (UInt8*)addr; 64 | UInt8* from = (UInt8*)&data; 65 | 66 | #ifdef IS_LITTLE_ENDIAN 67 | to[0] = from[3]; 68 | to[1] = from[2]; 69 | to[2] = from[1]; 70 | to[3] = from[0]; 71 | #else 72 | to[0] = from[0]; 73 | to[1] = from[1]; 74 | to[2] = from[2]; 75 | to[3] = from[3]; 76 | #endif 77 | } 78 | 79 | UInt32 pdb_read32(void *addr) 80 | { 81 | UInt32 data; 82 | UInt8* to = (UInt8*)&data; 83 | UInt8* from = (UInt8*)addr; 84 | 85 | #ifdef IS_LITTLE_ENDIAN 86 | to[0] = from[3]; 87 | to[1] = from[2]; 88 | to[2] = from[1]; 89 | to[3] = from[0]; 90 | #else 91 | to[0] = from[0]; 92 | to[1] = from[1]; 93 | to[2] = from[2]; 94 | to[3] = from[3]; 95 | #endif 96 | 97 | return data; 98 | } 99 | 100 | pdb_t *pdb_open(char *filename, char *pdbname, char *creator, char *type, 101 | UInt16 attr) 102 | { 103 | pdb_t *pdb; 104 | time_t now; 105 | 106 | if ((pdb = calloc(1, sizeof(pdb_t))) == NULL) 107 | return NULL; 108 | 109 | pdb->filename = strdup(filename); 110 | pdb->resource = attr & 0x0001; 111 | 112 | now = time(0) + 0x7c25b080l; 113 | 114 | strncpy((char *)pdb->header.name, pdbname, 32); 115 | pdb_write16(&pdb->header.fileAttributes, attr); 116 | pdb_write16(&pdb->header.version, 0); 117 | pdb_write32(&pdb->header.creationDate, now); 118 | pdb_write32(&pdb->header.modificationDate, now); 119 | pdb_write32(&pdb->header.lastBackupDate, 0); 120 | pdb_write32(&pdb->header.modificationNumber, 0); 121 | pdb_write32(&pdb->header.appInfoArea, 0); 122 | pdb_write32(&pdb->header.sortInfoArea, 0); 123 | pdb->header.databaseType[0] = type[0]; 124 | pdb->header.databaseType[1] = type[1]; 125 | pdb->header.databaseType[2] = type[2]; 126 | pdb->header.databaseType[3] = type[3]; 127 | pdb->header.creatorID[0] = creator[0]; 128 | pdb->header.creatorID[1] = creator[1]; 129 | pdb->header.creatorID[2] = creator[2]; 130 | pdb->header.creatorID[3] = creator[3]; 131 | pdb_write32(&pdb->header.uniqueIDSeed, 0); 132 | pdb_write32(&pdb->header.nextRecordListID, 0); 133 | pdb_write16(&pdb->header.numberOfRecords, 0); 134 | 135 | return pdb; 136 | } 137 | 138 | int pdb_close(pdb_t *pdb) 139 | { 140 | int i, fd; 141 | UInt32 offset, size; 142 | recheader *rech; 143 | resheader *resh; 144 | 145 | if (!pdb) 146 | return -1; 147 | 148 | size = pdb->resource ? pdb->nrecs * sizeof(resheader): 149 | pdb->nrecs * sizeof(recheader); 150 | 151 | if ((rech = calloc(1, size)) == NULL) 152 | return -1; 153 | 154 | resh = (resheader *)rech; 155 | offset = sizeof(pdbheader) + size; 156 | 157 | for (i = 0; i < pdb->nrecs; i++) { 158 | if (pdb->resource) { 159 | resh[i].resourceType[0] = pdb->rec[i][0]; 160 | resh[i].resourceType[1] = pdb->rec[i][1]; 161 | resh[i].resourceType[2] = pdb->rec[i][2]; 162 | resh[i].resourceType[3] = pdb->rec[i][3]; 163 | resh[i].resourceID = *((UInt16 *)&pdb->rec[i][4]); 164 | pdb_write32(&resh[i].recordDataOffset, offset); 165 | } else { 166 | pdb_write32(&rech[i].recordDataOffset, offset); 167 | rech[i].recordAttributes = 0; 168 | } 169 | offset += pdb->len[i]; 170 | } 171 | 172 | pdb_write16(&pdb->header.numberOfRecords, pdb->nrecs); 173 | 174 | if ((fd = open(pdb->filename, O_CREAT | O_TRUNC | O_WRONLY | O_BINARY, 0644)) == -1) { 175 | free(pdb); 176 | return -1; 177 | } 178 | 179 | write(fd, &pdb->header, sizeof(pdbheader)); 180 | write(fd, rech, size); 181 | 182 | for (i = 0; i < pdb->nrecs; i++) { 183 | write(fd, pdb->resource ? pdb->rec[i]+6 : pdb->rec[i], pdb->len[i]); 184 | free(pdb->rec[i]); 185 | } 186 | 187 | close(fd); 188 | 189 | free(pdb->filename); 190 | free(pdb); 191 | 192 | return 0; 193 | } 194 | 195 | int pdb_addrec(pdb_t *pdb, UInt8 *buf, UInt32 len) 196 | { 197 | UInt16 index; 198 | 199 | if (!pdb || !buf || !len) 200 | return -1; 201 | 202 | if (pdb->nrecs == MAX_RECORDS) 203 | return -1; 204 | 205 | index = pdb->nrecs; 206 | 207 | if ((pdb->rec[index] = calloc(1, len)) == NULL) 208 | return -1; 209 | 210 | memcpy(pdb->rec[index], buf, len); 211 | pdb->len[index] = len; 212 | pdb->nrecs++; 213 | 214 | return index; 215 | } 216 | 217 | int pdb_addres(pdb_t *pdb, UInt8 *buf, UInt32 len, char *type, UInt16 id) 218 | { 219 | UInt16 index; 220 | 221 | if (!pdb || !buf || !len) 222 | return -1; 223 | 224 | if (pdb->nrecs == MAX_RECORDS) 225 | return -1; 226 | 227 | index = pdb->nrecs; 228 | 229 | if ((pdb->rec[index] = calloc(1, len+6)) == NULL) 230 | return -1; 231 | 232 | pdb->rec[index][0] = type[0]; 233 | pdb->rec[index][1] = type[1]; 234 | pdb->rec[index][2] = type[2]; 235 | pdb->rec[index][3] = type[3]; 236 | pdb_write16(pdb->rec[index]+4, id); 237 | memcpy(pdb->rec[index]+6, buf, len); 238 | pdb->len[index] = len; 239 | pdb->nrecs++; 240 | 241 | return index; 242 | } 243 | -------------------------------------------------------------------------------- /src/luac/pdb.h: -------------------------------------------------------------------------------- 1 | #define MAX_RECORDS 1024 2 | 3 | #ifndef O_BINARY 4 | #define O_BINARY 0 5 | #endif 6 | 7 | #ifdef __LP64__ 8 | #include 9 | typedef uint8_t UInt8; 10 | typedef uint16_t UInt16; 11 | typedef uint32_t UInt32; 12 | #else 13 | typedef unsigned char UInt8; 14 | typedef unsigned short UInt16; 15 | typedef unsigned long UInt32; 16 | #endif 17 | 18 | struct pdbheader_struct { 19 | UInt8 name[32]; 20 | UInt16 fileAttributes; 21 | UInt16 version; 22 | UInt32 creationDate; 23 | UInt32 modificationDate; 24 | UInt32 lastBackupDate; 25 | UInt32 modificationNumber; 26 | UInt32 appInfoArea; 27 | UInt32 sortInfoArea; 28 | UInt8 databaseType[4]; 29 | UInt8 creatorID[4]; 30 | UInt32 uniqueIDSeed; 31 | UInt32 nextRecordListID; 32 | UInt16 numberOfRecords; 33 | } 34 | 35 | #ifdef __GNUC__ 36 | __attribute__ ((packed)) 37 | #endif 38 | ; 39 | 40 | struct recheader_struct { 41 | UInt32 recordDataOffset; 42 | UInt8 recordAttributes; 43 | UInt8 uniqueID[3]; 44 | } 45 | 46 | #ifdef __GNUC__ 47 | __attribute__ ((packed)) 48 | #endif 49 | ; 50 | 51 | struct resheader_struct { 52 | UInt8 resourceType[4]; 53 | UInt16 resourceID; 54 | UInt32 recordDataOffset; 55 | } 56 | 57 | #ifdef __GNUC__ 58 | __attribute__ ((packed)) 59 | #endif 60 | ; 61 | 62 | typedef struct pdbheader_struct pdbheader; 63 | typedef struct recheader_struct recheader; 64 | typedef struct resheader_struct resheader; 65 | 66 | typedef struct { 67 | char *filename; 68 | #ifdef __LP64__ 69 | UInt32 nrecs, resource; 70 | #else 71 | int nrecs, resource; 72 | #endif 73 | pdbheader header; 74 | UInt8 *rec[MAX_RECORDS]; 75 | UInt32 len[MAX_RECORDS]; 76 | } pdb_t; 77 | 78 | pdb_t *pdb_open(char *filename, char *pdbname, char *creator, char *type, UInt16 attr); 79 | int pdb_close(pdb_t *pdb); 80 | int pdb_addrec(pdb_t *pdb, UInt8 *buf, UInt32 len); 81 | int pdb_addres(pdb_t *pdb, UInt8 *buf, UInt32 len, char *type, UInt16 id); 82 | 83 | void pdb_write32(void *addr, UInt32 data); 84 | void pdb_write16(void *addr, UInt16 data); 85 | UInt16 pdb_read16(void *addr); 86 | UInt32 pdb_read32(void *addr); 87 | -------------------------------------------------------------------------------- /src/luac/print.c: -------------------------------------------------------------------------------- 1 | /* 2 | ** $Id: print.c,v 1.44 2003/04/07 20:34:20 lhf Exp $ 3 | ** print bytecodes 4 | ** See Copyright Notice in lua.h 5 | */ 6 | 7 | #include 8 | 9 | #if 0 10 | #define DEBUG_PRINT 11 | #endif 12 | 13 | #ifndef LUA_OPNAMES 14 | #define LUA_OPNAMES 15 | #endif 16 | 17 | #include "ldebug.h" 18 | #include "lobject.h" 19 | #include "lopcodes.h" 20 | #include "lundump.h" 21 | 22 | #define Sizeof(x) ((int)sizeof(x)) 23 | #define VOID(p) ((const void*)(p)) 24 | 25 | static void PrintString(const Proto* f, int n) 26 | { 27 | const char* s=svalue(&f->k[n]); 28 | putchar('"'); 29 | for (; *s; s++) 30 | { 31 | switch (*s) 32 | { 33 | case '"': printf("\\\""); break; 34 | case '\a': printf("\\a"); break; 35 | case '\b': printf("\\b"); break; 36 | case '\f': printf("\\f"); break; 37 | case '\n': printf("\\n"); break; 38 | case '\r': printf("\\r"); break; 39 | case '\t': printf("\\t"); break; 40 | case '\v': printf("\\v"); break; 41 | default: putchar(*s); break; 42 | } 43 | } 44 | putchar('"'); 45 | } 46 | 47 | static void PrintConstant(const Proto* f, int i) 48 | { 49 | const TObject* o=&f->k[i]; 50 | switch (ttype(o)) 51 | { 52 | case LUA_TNUMBER: 53 | printf(LUA_NUMBER_FMT,nvalue(o)); 54 | break; 55 | case LUA_TSTRING: 56 | PrintString(f,i); 57 | break; 58 | case LUA_TNIL: 59 | printf("nil"); 60 | break; 61 | default: /* cannot happen */ 62 | printf("? type=%d",ttype(o)); 63 | break; 64 | } 65 | } 66 | 67 | static void PrintCode(const Proto* f) 68 | { 69 | const Instruction* code=f->code; 70 | int pc,n=f->sizecode; 71 | for (pc=0; pc0) printf("[%d]\t",line); else printf("[-]\t"); 86 | printf("%-9s\t",luaP_opnames[o]); 87 | switch (getOpMode(o)) 88 | { 89 | case iABC: printf("%d %d %d",a,b,c); break; 90 | case iABx: printf("%d %d",a,bc); break; 91 | case iAsBx: printf("%d %d",a,sbc); break; 92 | } 93 | switch (o) 94 | { 95 | case OP_LOADK: 96 | printf("\t; "); PrintConstant(f,bc); 97 | break; 98 | case OP_GETUPVAL: 99 | case OP_SETUPVAL: 100 | printf("\t; %s", (f->sizeupvalues>0) ? getstr(f->upvalues[b]) : "-"); 101 | break; 102 | case OP_GETGLOBAL: 103 | case OP_SETGLOBAL: 104 | printf("\t; %s",svalue(&f->k[bc])); 105 | break; 106 | case OP_GETTABLE: 107 | case OP_SELF: 108 | if (c>=MAXSTACK) { printf("\t; "); PrintConstant(f,c-MAXSTACK); } 109 | break; 110 | case OP_SETTABLE: 111 | case OP_ADD: 112 | case OP_SUB: 113 | case OP_MUL: 114 | case OP_DIV: 115 | case OP_POW: 116 | case OP_EQ: 117 | case OP_LT: 118 | case OP_LE: 119 | if (b>=MAXSTACK || c>=MAXSTACK) 120 | { 121 | printf("\t; "); 122 | if (b>=MAXSTACK) PrintConstant(f,b-MAXSTACK); else printf("-"); 123 | printf(" "); 124 | if (c>=MAXSTACK) PrintConstant(f,c-MAXSTACK); 125 | } 126 | break; 127 | case OP_JMP: 128 | case OP_FORLOOP: 129 | case OP_TFORPREP: 130 | printf("\t; to %d",sbc+pc+2); 131 | break; 132 | case OP_CLOSURE: 133 | printf("\t; %p",VOID(f->p[bc])); 134 | break; 135 | default: 136 | break; 137 | } 138 | printf("\n"); 139 | } 140 | } 141 | 142 | static const char* Source(const Proto* f) 143 | { 144 | const char* s=getstr(f->source); 145 | if (*s=='@' || *s=='=') 146 | return s+1; 147 | else if (*s==LUA_SIGNATURE[0]) 148 | return "(bstring)"; 149 | else 150 | return "(string)"; 151 | } 152 | 153 | #define IsMain(f) (f->lineDefined==0) 154 | 155 | #define SS(x) (x==1)?"":"s" 156 | #define S(x) x,SS(x) 157 | 158 | static void PrintHeader(const Proto* f) 159 | { 160 | printf("\n%s <%s:%d> (%d instruction%s, %d bytes at %p)\n", 161 | IsMain(f)?"main":"function",Source(f),f->lineDefined, 162 | S(f->sizecode),f->sizecode*Sizeof(Instruction),VOID(f)); 163 | printf("%d%s param%s, %d stack%s, %d upvalue%s, ", 164 | f->numparams,f->is_vararg?"+":"",SS(f->numparams),S(f->maxstacksize), 165 | S(f->nups)); 166 | printf("%d local%s, %d constant%s, %d function%s\n", 167 | S(f->sizelocvars),S(f->sizek),S(f->sizep)); 168 | } 169 | 170 | #ifdef DEBUG_PRINT 171 | static void PrintConstants(const Proto* f) 172 | { 173 | int i,n=f->sizek; 174 | printf("constants (%d) for %p:\n",n,VOID(f)); 175 | for (i=0; isizelocvars; 186 | printf("locals (%d) for %p:\n",n,VOID(f)); 187 | for (i=0; ilocvars[i].varname),f->locvars[i].startpc,f->locvars[i].endpc); 191 | } 192 | } 193 | 194 | static void PrintUpvalues(const Proto* f) 195 | { 196 | int i,n=f->sizeupvalues; 197 | printf("upvalues (%d) for %p:\n",n,VOID(f)); 198 | if (f->upvalues==NULL) return; 199 | for (i=0; iupvalues[i])); 202 | } 203 | } 204 | #endif 205 | 206 | void luaU_print(const Proto* f) 207 | { 208 | int i,n=f->sizep; 209 | PrintHeader(f); 210 | PrintCode(f); 211 | #ifdef DEBUG_PRINT 212 | PrintConstants(f); 213 | PrintLocals(f); 214 | PrintUpvalues(f); 215 | #endif 216 | for (i=0; ip[i]); 217 | } 218 | -------------------------------------------------------------------------------- /src/lundump.c: -------------------------------------------------------------------------------- 1 | /* 2 | ** $Id: lundump.c,v 1.49 2003/04/07 20:34:20 lhf Exp $ 3 | ** load pre-compiled Lua chunks 4 | ** See Copyright Notice in lua.h 5 | */ 6 | 7 | #define lundump_c 8 | 9 | #include "lua.h" 10 | 11 | #include "ldebug.h" 12 | #include "lfunc.h" 13 | #include "lmem.h" 14 | #include "lopcodes.h" 15 | #include "lstring.h" 16 | #include "lundump.h" 17 | #include "lzio.h" 18 | 19 | #define LoadByte (lu_byte) ezgetc 20 | 21 | typedef struct { 22 | lua_State* L; 23 | ZIO* Z; 24 | Mbuffer* b; 25 | int swap; 26 | const char* name; 27 | } LoadState; 28 | 29 | static void unexpectedEOZ (LoadState* S) 30 | { 31 | luaG_runerror(S->L,"unexpected end of file in %s",S->name); 32 | } 33 | 34 | static int ezgetc (LoadState* S) 35 | { 36 | int c=zgetc(S->Z); 37 | if (c==EOZ) unexpectedEOZ(S); 38 | return c; 39 | } 40 | 41 | static void ezread (LoadState* S, void* b, int n) 42 | { 43 | int r=luaZ_read(S->Z,b,n); 44 | if (r!=0) unexpectedEOZ(S); 45 | } 46 | 47 | static void LoadBlock (LoadState* S, void* b, size_t size) 48 | { 49 | if (S->swap) 50 | { 51 | char* p=(char*) b+size-1; 52 | int n=size; 53 | while (n--) *p--=(char)ezgetc(S); 54 | } 55 | else 56 | ezread(S,b,size); 57 | } 58 | 59 | static void LoadVector (LoadState* S, void* b, int m, size_t size) 60 | { 61 | if (S->swap) 62 | { 63 | char* q=(char*) b; 64 | while (m--) 65 | { 66 | char* p=q+size-1; 67 | int n=size; 68 | while (n--) *p--=(char)ezgetc(S); 69 | q+=size; 70 | } 71 | } 72 | else 73 | ezread(S,b,m*size); 74 | } 75 | 76 | static int LoadInt (LoadState* S) 77 | { 78 | int x; 79 | LoadBlock(S,&x,sizeof(x)); 80 | if (x<0) luaG_runerror(S->L,"bad integer in %s",S->name); 81 | return x; 82 | } 83 | 84 | static size_t LoadSize (LoadState* S) 85 | { 86 | size_t x; 87 | LoadBlock(S,&x,sizeof(x)); 88 | return x; 89 | } 90 | 91 | static lua_Number LoadNumber (LoadState* S) 92 | { 93 | lua_Number x; 94 | LoadBlock(S,&x,sizeof(x)); 95 | return x; 96 | } 97 | 98 | static TString* LoadString (LoadState* S) 99 | { 100 | size_t size=LoadSize(S); 101 | if (size==0) 102 | return NULL; 103 | else 104 | { 105 | char* s=luaZ_openspace(S->L,S->b,size); 106 | ezread(S,s,size); 107 | return luaS_newlstr(S->L,s,size-1); /* remove trailing '\0' */ 108 | } 109 | } 110 | 111 | static void LoadCode (LoadState* S, Proto* f) 112 | { 113 | int size=LoadInt(S); 114 | f->code=luaM_newvector(S->L,size,Instruction); 115 | f->sizecode=size; 116 | LoadVector(S,f->code,size,sizeof(*f->code)); 117 | } 118 | 119 | static void LoadLocals (LoadState* S, Proto* f) 120 | { 121 | int i,n; 122 | n=LoadInt(S); 123 | f->locvars=luaM_newvector(S->L,n,LocVar); 124 | f->sizelocvars=n; 125 | for (i=0; ilocvars[i].varname=LoadString(S); 128 | f->locvars[i].startpc=LoadInt(S); 129 | f->locvars[i].endpc=LoadInt(S); 130 | } 131 | } 132 | 133 | static void LoadLines (LoadState* S, Proto* f) 134 | { 135 | int size=LoadInt(S); 136 | f->lineinfo=luaM_newvector(S->L,size,int); 137 | f->sizelineinfo=size; 138 | LoadVector(S,f->lineinfo,size,sizeof(*f->lineinfo)); 139 | } 140 | 141 | static void LoadUpvalues (LoadState* S, Proto* f) 142 | { 143 | int i,n; 144 | n=LoadInt(S); 145 | if (n!=0 && n!=f->nups) 146 | luaG_runerror(S->L,"bad nupvalues in %s: read %d; expected %d", 147 | S->name,n,f->nups); 148 | f->upvalues=luaM_newvector(S->L,n,TString*); 149 | f->sizeupvalues=n; 150 | for (i=0; iupvalues[i]=LoadString(S); 151 | } 152 | 153 | static Proto* LoadFunction (LoadState* S, TString* p); 154 | 155 | static void LoadConstants (LoadState* S, Proto* f) 156 | { 157 | int i,n; 158 | n=LoadInt(S); 159 | f->k=luaM_newvector(S->L,n,TObject); 160 | f->sizek=n; 161 | for (i=0; ik[i]; 164 | int t=LoadByte(S); 165 | switch (t) 166 | { 167 | case LUA_TNUMBER: 168 | setnvalue(o,LoadNumber(S)); 169 | break; 170 | case LUA_TSTRING: 171 | setsvalue2n(o,LoadString(S)); 172 | break; 173 | case LUA_TNIL: 174 | setnilvalue(o); 175 | break; 176 | default: 177 | luaG_runerror(S->L,"bad constant type (%d) in %s",t,S->name); 178 | break; 179 | } 180 | } 181 | n=LoadInt(S); 182 | f->p=luaM_newvector(S->L,n,Proto*); 183 | f->sizep=n; 184 | for (i=0; ip[i]=LoadFunction(S,f->source); 185 | } 186 | 187 | static Proto* LoadFunction (LoadState* S, TString* p) 188 | { 189 | Proto* f=luaF_newproto(S->L); 190 | f->source=LoadString(S); if (f->source==NULL) f->source=p; 191 | f->lineDefined=LoadInt(S); 192 | f->nups=LoadByte(S); 193 | f->numparams=LoadByte(S); 194 | f->is_vararg=LoadByte(S); 195 | f->maxstacksize=LoadByte(S); 196 | LoadLines(S,f); 197 | LoadLocals(S,f); 198 | LoadUpvalues(S,f); 199 | LoadConstants(S,f); 200 | LoadCode(S,f); 201 | #ifndef TRUST_BINARIES 202 | if (!luaG_checkcode(f)) luaG_runerror(S->L,"bad code in %s",S->name); 203 | #endif 204 | return f; 205 | } 206 | 207 | static void LoadSignature (LoadState* S) 208 | { 209 | const char* s=LUA_SIGNATURE; 210 | while (*s!=0 && ezgetc(S)==*s) 211 | ++s; 212 | if (*s!=0) luaG_runerror(S->L,"bad signature in %s",S->name); 213 | } 214 | 215 | static void TestSize (LoadState* S, int s, const char* what) 216 | { 217 | int r=LoadByte(S); 218 | if (r!=s) 219 | luaG_runerror(S->L,"virtual machine mismatch in %s: " 220 | "size of %s is %d but read %d",S->name,what,s,r); 221 | } 222 | 223 | #define TESTSIZE(s,w) TestSize(S,s,w) 224 | #define V(v) v/16,v%16 225 | 226 | static void LoadHeader (LoadState* S) 227 | { 228 | int version; 229 | lua_Number x,tx=TEST_NUMBER; 230 | LoadSignature(S); 231 | version=LoadByte(S); 232 | if (version>VERSION) 233 | luaG_runerror(S->L,"%s too new: " 234 | "read version %d.%d; expected at most %d.%d", 235 | S->name,V(version),V(VERSION)); 236 | if (versionL,"%s too old: " 238 | "read version %d.%d; expected at least %d.%d", 239 | S->name,V(version),V(VERSION0)); 240 | S->swap=(luaU_endianness()!=LoadByte(S)); /* need to swap bytes? */ 241 | TESTSIZE(sizeof(int),"int"); 242 | TESTSIZE(sizeof(size_t), "size_t"); 243 | TESTSIZE(sizeof(Instruction), "Instruction"); 244 | TESTSIZE(SIZE_OP, "OP"); 245 | TESTSIZE(SIZE_A, "A"); 246 | TESTSIZE(SIZE_B, "B"); 247 | TESTSIZE(SIZE_C, "C"); 248 | TESTSIZE(sizeof(lua_Number), "number"); 249 | x=LoadNumber(S); 250 | if ((long)x!=(long)tx) /* disregard errors in last bits of fraction */ 251 | luaG_runerror(S->L,"unknown number format in %s",S->name); 252 | } 253 | 254 | static Proto* LoadChunk (LoadState* S) 255 | { 256 | LoadHeader(S); 257 | return LoadFunction(S,NULL); 258 | } 259 | 260 | /* 261 | ** load precompiled chunk 262 | */ 263 | Proto* luaU_undump (lua_State* L, ZIO* Z, Mbuffer* buff) 264 | { 265 | LoadState S; 266 | const char* s=zname(Z); 267 | if (*s=='@' || *s=='=') 268 | S.name=s+1; 269 | else if (*s==LUA_SIGNATURE[0]) 270 | S.name="binary string"; 271 | else 272 | S.name=s; 273 | S.L=L; 274 | S.Z=Z; 275 | S.b=buff; 276 | return LoadChunk(&S); 277 | } 278 | 279 | /* 280 | ** find byte order 281 | */ 282 | int luaU_endianness (void) 283 | { 284 | int x=1; 285 | return *(char*)&x; 286 | } 287 | -------------------------------------------------------------------------------- /src/lundump.h: -------------------------------------------------------------------------------- 1 | /* 2 | ** $Id: lundump.h,v 1.30 2003/04/07 20:34:20 lhf Exp $ 3 | ** load pre-compiled 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 | Proto* luaU_undump (lua_State* L, ZIO* Z, Mbuffer* buff); 15 | 16 | /* find byte order; from lundump.c */ 17 | int luaU_endianness (void); 18 | 19 | /* dump one chunk; from ldump.c */ 20 | void luaU_dump (lua_State* L, const Proto* Main, lua_Chunkwriter w, void* data); 21 | 22 | /* print one chunk; from print.c */ 23 | void luaU_print (const Proto* Main); 24 | 25 | /* definitions for headers of binary files */ 26 | #define LUA_SIGNATURE "\033Lua" /* binary files start with "Lua" */ 27 | #define VERSION 0x50 /* last format change was in 5.0 */ 28 | #define VERSION0 0x50 /* last major change was in 5.0 */ 29 | 30 | /* a multiple of PI for testing native format */ 31 | /* multiplying by 1E7 gives non-trivial integer values */ 32 | #define TEST_NUMBER ((lua_Number)3.14159265358979323846E7) 33 | 34 | #endif 35 | -------------------------------------------------------------------------------- /src/lvm.h: -------------------------------------------------------------------------------- 1 | /* 2 | ** $Id: lvm.h,v 1.47 2002/11/14 16:16:21 roberto Exp $ 3 | ** Lua virtual machine 4 | ** See Copyright Notice in lua.h 5 | */ 6 | 7 | #ifndef lvm_h 8 | #define lvm_h 9 | 10 | 11 | #include "ldo.h" 12 | #include "lobject.h" 13 | #include "ltm.h" 14 | 15 | 16 | #define tostring(L,o) ((ttype(o) == LUA_TSTRING) || (luaV_tostring(L, o))) 17 | 18 | #define tonumber(o,n) (ttype(o) == LUA_TNUMBER || \ 19 | (((o) = luaV_tonumber(o,n)) != NULL)) 20 | 21 | #define equalobj(L,o1,o2) \ 22 | (ttype(o1) == ttype(o2) && luaV_equalval(L, o1, o2)) 23 | 24 | 25 | int luaV_lessthan (lua_State *L, const TObject *l, const TObject *r); 26 | int luaV_equalval (lua_State *L, const TObject *t1, const TObject *t2); 27 | const TObject *luaV_tonumber (const TObject *obj, TObject *n); 28 | int luaV_tostring (lua_State *L, StkId obj); 29 | const TObject *luaV_gettable (lua_State *L, const TObject *t, TObject *key, 30 | int loop); 31 | void luaV_settable (lua_State *L, const TObject *t, TObject *key, StkId val); 32 | StkId luaV_execute (lua_State *L); 33 | void luaV_concat (lua_State *L, int total, int last); 34 | 35 | #endif 36 | -------------------------------------------------------------------------------- /src/lzio.c: -------------------------------------------------------------------------------- 1 | /* 2 | ** $Id: lzio.c,v 1.24 2003/03/20 16:00:56 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 | 12 | #include "lua.h" 13 | 14 | #include "llimits.h" 15 | #include "lmem.h" 16 | #include "lzio.h" 17 | 18 | 19 | int luaZ_fill (ZIO *z) { 20 | size_t size; 21 | const char *buff = z->reader(NULL, z->data, &size); 22 | if (buff == NULL || size == 0) return EOZ; 23 | z->n = size - 1; 24 | z->p = buff; 25 | return char2int(*(z->p++)); 26 | } 27 | 28 | 29 | int luaZ_lookahead (ZIO *z) { 30 | if (z->n == 0) { 31 | int c = luaZ_fill(z); 32 | if (c == EOZ) return c; 33 | z->n++; 34 | z->p--; 35 | } 36 | return char2int(*z->p); 37 | } 38 | 39 | 40 | void luaZ_init (ZIO *z, lua_Chunkreader reader, void *data, const char *name) { 41 | z->reader = reader; 42 | z->data = data; 43 | z->name = name; 44 | z->n = 0; 45 | z->p = NULL; 46 | } 47 | 48 | 49 | /* --------------------------------------------------------------- read --- */ 50 | size_t luaZ_read (ZIO *z, void *b, size_t n) { 51 | while (n) { 52 | size_t m; 53 | if (z->n == 0) { 54 | if (luaZ_fill(z) == EOZ) 55 | return n; /* return number of missing bytes */ 56 | else { 57 | ++z->n; /* filbuf removed first byte; put back it */ 58 | --z->p; 59 | } 60 | } 61 | m = (n <= z->n) ? n : z->n; /* min. between n and z->n */ 62 | memcpy(b, z->p, m); 63 | z->n -= m; 64 | z->p += m; 65 | b = (char *)b + m; 66 | n -= m; 67 | } 68 | return 0; 69 | } 70 | 71 | /* ------------------------------------------------------------------------ */ 72 | char *luaZ_openspace (lua_State *L, Mbuffer *buff, size_t n) { 73 | if (n > buff->buffsize) { 74 | if (n < LUA_MINBUFFER) n = LUA_MINBUFFER; 75 | luaM_reallocvector(L, buff->buffer, buff->buffsize, n, char); 76 | buff->buffsize = n; 77 | } 78 | return buff->buffer; 79 | } 80 | 81 | 82 | -------------------------------------------------------------------------------- /src/lzio.h: -------------------------------------------------------------------------------- 1 | /* 2 | ** $Id: lzio.h,v 1.15 2003/03/20 16:00:56 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 | 14 | #define EOZ (-1) /* end of stream */ 15 | 16 | typedef struct Zio ZIO; 17 | 18 | 19 | #define char2int(c) cast(int, cast(unsigned char, (c))) 20 | 21 | #define zgetc(z) (((z)->n--)>0 ? char2int(*(z)->p++) : luaZ_fill(z)) 22 | 23 | #define zname(z) ((z)->name) 24 | 25 | void luaZ_init (ZIO *z, lua_Chunkreader reader, void *data, const char *name); 26 | size_t luaZ_read (ZIO* z, void* b, size_t n); /* read next n bytes */ 27 | int luaZ_lookahead (ZIO *z); 28 | 29 | 30 | 31 | typedef struct Mbuffer { 32 | char *buffer; 33 | size_t buffsize; 34 | } Mbuffer; 35 | 36 | 37 | char *luaZ_openspace (lua_State *L, Mbuffer *buff, size_t n); 38 | 39 | #define luaZ_initbuffer(L, buff) ((buff)->buffer = NULL, (buff)->buffsize = 0) 40 | 41 | #define luaZ_sizebuffer(buff) ((buff)->buffsize) 42 | #define luaZ_buffer(buff) ((buff)->buffer) 43 | 44 | #define luaZ_resizebuffer(L, buff, size) \ 45 | (luaM_reallocvector(L, (buff)->buffer, (buff)->buffsize, size, char), \ 46 | (buff)->buffsize = size) 47 | 48 | #define luaZ_freebuffer(L, buff) luaZ_resizebuffer(L, buff, 0) 49 | 50 | 51 | /* --------- Private Part ------------------ */ 52 | 53 | struct Zio { 54 | size_t n; /* bytes still unread */ 55 | const char *p; /* current position in buffer */ 56 | lua_Chunkreader reader; 57 | void* data; /* additional data */ 58 | const char *name; 59 | }; 60 | 61 | 62 | int luaZ_fill (ZIO *z); 63 | 64 | #endif 65 | -------------------------------------------------------------------------------- /test/README: -------------------------------------------------------------------------------- 1 | These are simple tests for Lua. Some of them contain useful code. 2 | They are meant to be run to make sure Lua is built correctly and also 3 | to be read, to see how Lua programs look. 4 | 5 | Here is a one-line summary of each program: 6 | 7 | bisect.lua bisection method for solving non-linear equations 8 | cf.lua temperature conversion table (celsius to farenheit) 9 | echo.lua echo command line arguments 10 | env.lua environment variables as automatic global variables 11 | factorial.lua factorial without recursion 12 | fib.lua fibonacci function with cache 13 | fibfor.lua fibonacci numbers with coroutines and generators 14 | globals.lua report global variable usage 15 | hello.lua the first program in every language 16 | life.lua Conway's Game of Life 17 | luac.lua bare-bones luac 18 | printf.lua an implementation of printf 19 | readonly.lua make global variables readonly 20 | sieve.lua the sieve of of Eratosthenes programmed with coroutines 21 | sort.lua two implementations of a sort function 22 | table.lua make table, grouping all data for the same item 23 | trace-calls.lua trace calls 24 | trace-globals.lua trace assigments to global variables 25 | undefined.lua catch "undefined" global variables 26 | xd.lua hex dump 27 | 28 | -------------------------------------------------------------------------------- /test/bisect.lua: -------------------------------------------------------------------------------- 1 | -- bisection method for solving non-linear equations 2 | 3 | delta=1e-6 -- tolerance 4 | 5 | function bisect(f,a,b,fa,fb) 6 | local c=(a+b)/2 7 | io.write(n," c=",c," a=",a," b=",b,"\n") 8 | if c==a or c==b or math.abs(a-b)y end) 58 | show("after reverse selection sort",x) 59 | qsort(x,1,n,function (x,y) return x>> ",string.rep(" ",level)) 9 | if t~=nil and t.currentline>=0 then io.write(t.short_src,":",t.currentline," ") end 10 | t=debug.getinfo(2) 11 | if event=="call" then 12 | level=level+1 13 | else 14 | level=level-1 if level<0 then level=0 end 15 | end 16 | if t.what=="main" then 17 | if event=="call" then 18 | io.write("begin ",t.short_src) 19 | else 20 | io.write("end ",t.short_src) 21 | end 22 | elseif t.what=="Lua" then 23 | -- table.foreach(t,print) 24 | io.write(event," ",t.name or "(Lua)"," <",t.linedefined,":",t.short_src,">") 25 | else 26 | io.write(event," ",t.name or "(C)"," [",t.what,"] ") 27 | end 28 | io.write("\n") 29 | end 30 | 31 | debug.sethook(hook,"cr") 32 | level=0 33 | -------------------------------------------------------------------------------- /test/trace-globals.lua: -------------------------------------------------------------------------------- 1 | -- trace assigments to global variables 2 | 3 | do 4 | -- a tostring that quotes strings. note the use of the original tostring. 5 | local _tostring=tostring 6 | local tostring=function(a) 7 | if type(a)=="string" then 8 | return string.format("%q",a) 9 | else 10 | return _tostring(a) 11 | end 12 | end 13 | 14 | local log=function (name,old,new) 15 | local t=debug.getinfo(3,"Sl") 16 | local line=t.currentline 17 | io.write(t.short_src) 18 | if line>=0 then io.write(":",line) end 19 | io.write(": ",name," is now ",tostring(new)," (was ",tostring(old),")","\n") 20 | end 21 | 22 | local g={} 23 | local set=function (t,name,value) 24 | log(name,g[name],value) 25 | g[name]=value 26 | end 27 | setmetatable(getfenv(),{__index=g,__newindex=set}) 28 | end 29 | 30 | -- an example 31 | 32 | a=1 33 | b=2 34 | a=10 35 | b=20 36 | b=nil 37 | b=200 38 | print(a,b,c) 39 | -------------------------------------------------------------------------------- /test/undefined.lua: -------------------------------------------------------------------------------- 1 | -- catch "undefined" global variables 2 | 3 | local f=function (t,i) error("undefined global variable `"..i.."'",2) end 4 | setmetatable(getfenv(),{__index=f}) 5 | 6 | -- an example 7 | a=1 8 | c=3 9 | print(a,b,c) -- `b' is undefined 10 | -------------------------------------------------------------------------------- /test/xd.lua: -------------------------------------------------------------------------------- 1 | -- hex dump 2 | -- usage: lua xd.lua < file 3 | 4 | local offset=0 5 | 6 | while 1 do 7 | local s=io.read(16) 8 | if s==nil then return end 9 | io.write(string.format("%08X ",offset)) 10 | string.gsub(s,"(.)",function (c) io.write(string.format("%02X ",string.byte(c))) end) 11 | io.write(string.rep(" ",3*(16-string.len(s)))) 12 | io.write(" ",string.gsub(s,"%c","."),"\n") 13 | offset=offset+16 14 | end 15 | --------------------------------------------------------------------------------