├── .gitignore ├── COPYING ├── Makefile ├── README.md ├── bitfont.h ├── contrib ├── Pandora │ ├── Makefile │ ├── PND_Resources │ │ ├── DejaVuSansMono-Bold.ttf │ │ ├── PXML.xml │ │ ├── defconf │ │ │ ├── config.txt │ │ │ └── profile.txt │ │ ├── icon.png │ │ ├── picklelauncher │ │ └── runme.sh │ ├── PXML_schema.xsd │ ├── README.txt │ └── pnd_make.sh └── nacl │ ├── .gitignore │ ├── README.md │ ├── app.yaml │ ├── build.sh │ ├── chrome-app │ ├── icon_128.png │ └── manifest.json │ ├── load81.html │ ├── nacl.cc │ └── static │ ├── load81.css │ ├── load81.js │ └── load81.nmf ├── editor.c ├── editor.h ├── examples ├── 2dsim.lua ├── asteroids.lua ├── falldown.lua ├── flames.lua ├── helicopter.lua ├── keynames.lua ├── lines.lua ├── paint.lua ├── scorched.lua ├── sprite.lua ├── sprite.png ├── text.lua └── triangles.lua ├── framebuffer.c ├── framebuffer.h ├── load81.c ├── load81.h └── lua ├── COPYRIGHT ├── HISTORY ├── INSTALL ├── Makefile ├── README ├── doc ├── contents.html ├── cover.png ├── logo.gif ├── lua.1 ├── lua.css ├── lua.html ├── luac.1 ├── luac.html ├── manual.css ├── manual.html └── readme.html ├── etc ├── Makefile ├── README ├── all.c ├── lua.hpp ├── lua.ico ├── lua.pc ├── luavs.bat ├── min.c ├── noparser.c └── strict.lua ├── src ├── Makefile ├── lapi.c ├── lapi.h ├── lauxlib.c ├── lauxlib.h ├── lbaselib.c ├── lcode.c ├── lcode.h ├── ldblib.c ├── ldebug.c ├── ldebug.h ├── ldo.c ├── ldo.h ├── ldump.c ├── lfunc.c ├── lfunc.h ├── lgc.c ├── lgc.h ├── linit.c ├── liolib.c ├── llex.c ├── llex.h ├── llimits.h ├── lmathlib.c ├── lmem.c ├── lmem.h ├── loadlib.c ├── lobject.c ├── lobject.h ├── lopcodes.c ├── lopcodes.h ├── loslib.c ├── lparser.c ├── lparser.h ├── lstate.c ├── lstate.h ├── lstring.c ├── lstring.h ├── lstrlib.c ├── ltable.c ├── ltable.h ├── ltablib.c ├── ltm.c ├── ltm.h ├── lua.c ├── lua.h ├── luac.c ├── luaconf.h ├── lualib.h ├── lundump.c ├── lundump.h ├── lvm.c ├── lvm.h ├── lzio.c ├── lzio.h └── print.c └── test ├── README ├── bisect.lua ├── cf.lua ├── echo.lua ├── env.lua ├── factorial.lua ├── fib.lua ├── fibfor.lua ├── globals.lua ├── hello.lua ├── life.lua ├── luac.lua ├── printf.lua ├── readonly.lua ├── sieve.lua ├── sort.lua ├── table.lua ├── trace-calls.lua ├── trace-globals.lua └── xd.lua /.gitignore: -------------------------------------------------------------------------------- 1 | load81 2 | *.o 3 | lua/src/*.o 4 | lua/src/*.a 5 | lua/src/lua 6 | lua/src/luac 7 | -------------------------------------------------------------------------------- /COPYING: -------------------------------------------------------------------------------- 1 | Copyright (c) 2012, Salvatore Sanfilippo. All rights reserved. 2 | 3 | Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met: 4 | 5 | * Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer. 6 | * Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution. 7 | 8 | THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 9 | -------------------------------------------------------------------------------- /Makefile: -------------------------------------------------------------------------------- 1 | PKGS=sdl2 SDL2_gfx SDL2_image 2 | CFLAGS=-O2 -Wall -W -Ilua/src `pkg-config --cflags $(PKGS)` 3 | LDLIBS=lua/src/liblua.a -lm `pkg-config --libs $(PKGS)` 4 | 5 | all: load81 6 | 7 | load81: load81.o editor.o framebuffer.o lua/src/liblua.a 8 | editor.o: editor.c editor.h framebuffer.h 9 | framebuffer.o: framebuffer.c framebuffer.h bitfont.h 10 | load81.o: load81.c framebuffer.h editor.h load81.h 11 | 12 | lua/src/liblua.a: 13 | -(cd lua && $(MAKE) ansi) 14 | 15 | clean: 16 | rm -f load81 *.o 17 | 18 | distclean: clean 19 | -(cd lua && $(MAKE) clean) 20 | 21 | dep: 22 | $(CC) -MM *.c 23 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | README 2 | === 3 | 4 | Load81 is an attempt at creating a Codea-inspired environment to teach 5 | children how to write Lua programs. It features a graphical programming 6 | environment and a Commodore-64 style integrated editor so that the programmer 7 | is confined into a friendly environment with a simple editor: 8 | 9 | ![Load81 Editor](http://antirez.com/misc/codakido_screenshot_1.png) 10 | 11 | The following is a screenshot of the running program (examples/asteroids.lua). 12 | The programmer can currently switch between edit and play mode pressing the 13 | ESC key. 14 | 15 | ![Load81 Asteroids](http://antirez.com/misc/codakido_screenshot_3.png) 16 | 17 | Load81 is written in ANSI C and uses `SDL` and `SDL_gfx` and `SDL_image`, so 18 | should compile on Mac OS X and Linux without issues. It should not be hard 19 | to port it to Windows. 20 | 21 | The coordinate system and the basic drawing functions are compatible with 22 | Codea (check http://twolivesleft.com/Codea/ for more information), but there 23 | is no support for stroke. 24 | 25 | There is no aim at Codea compatibility, but who is familiar with Codea should 26 | feel at home with Load81 in terms of API and structure of the program. 27 | 28 | I wrote it mainly because I and my children have fun with Codea but we don't 29 | have an iPad at home, and using a real keyboard sometimes can be less 30 | frustrating. 31 | 32 | The name Load81 originates from the fact that in popular Commodore home 33 | computers the command `LOAD "*",8,1` would load the first program on the disk 34 | starting from the file-specified memory location. 35 | 36 | USAGE 37 | === 38 | 39 | Start Load81 with: 40 | 41 | ./load81 example.lua 42 | 43 | To switch between program and editor mode press the ESC key. 44 | 45 | Check the "examples" folder for small examples. 46 | 47 | PROGRAMMING INTERFACE 48 | === 49 | 50 | Drawing functions: 51 | 52 | * fill(r,g,b,alpha): select the drawing color. 53 | * background(r,g,b): paint the whole background with the specified color. 54 | * rect(x,y,width,height): draw a rectangle at x,y (left-bottom corner). 55 | * ellipse(x,y,width,height): draw an ellipse centered at x,y. 56 | * line(x1,y1,x2,y2): draw a line from x1,y1 to x2,y2. 57 | * text(x,y,string): print the specified text at x,y using a bitmap font. 58 | * triangle(x1,y1,x2,y2,x3,y3): draw a triangle with the specified vertex. 59 | * getpixel(x,y): return the red,gree,blue value of the specified pixel. 60 | * sprite(file,x,y,[rotation],[antialiasing]): draw sprite at coordinates with the specified rotation (in degrees, default 0) and antialiasing (default false). 61 | 62 | Control functions: 63 | 64 | * setFPS(fps): Set the frame rate. For default it's set to 30 frames per second. 65 | 66 | KEYBOARD EVENTS 67 | === 68 | 69 | To check if a key 'a' is pressed use: 70 | 71 | if keyboard.pressed['a'] then ... 72 | 73 | SDL Key symbol names are used. You can easily find how a given key is 74 | called using the following Lua program: 75 | 76 | function draw() 77 | for k,v in pairs(keyboard.pressed) do 78 | print(k) 79 | end 80 | end 81 | 82 | (You can find this program under the examples folder). 83 | 84 | LOW LEVEL KEYBOARD EVENTS 85 | === 86 | 87 | It is also possible to trap low level SDL events accessing keyboard.state 88 | and keyboard.key fields of the keyboard table. 89 | 90 | keyboard.state is one of: 91 | 92 | "down" -> KEYDOWN event 93 | "up" -> KEYUP event 94 | "none" -> No event 95 | 96 | keyboard.key is set to the key pressed or released when state is different 97 | than "none". 98 | 99 | MOUSE EVENTS 100 | === 101 | 102 | mouse.x and mouse.y gives you the current mouse coordinates. To check 103 | if a button is pressed use: 104 | 105 | if mouse.pressed['1'] then ... 106 | 107 | Mouse buttons are called '1', '2', '3', ... and so forth. 108 | 109 | LICENSE 110 | === 111 | 112 | Load81 was written by Salvatore Sanfilippo and is released under the 113 | BSD two-clause license, see the COPYING file for more information. 114 | 115 | The load81 project is dedicated to Jack Tramiel, founder of Commodore. 116 | -------------------------------------------------------------------------------- /contrib/Pandora/Makefile: -------------------------------------------------------------------------------- 1 | all: pnd-file 2 | 3 | pnd-file: 4 | rm -rf PND_Resources/examples/ && mkdir PND_Resources/examples/ 5 | cp -rfv ../../examples/* PND_Resources/examples/ 6 | cp ../../load81 PND_Resources/ 7 | ./pnd_make.sh -c -d PND_Resources/ -x PND_Resources/PXML.xml -i PND_Resources/icon.png -p ./LOAD81.pnd 8 | 9 | clean: 10 | rm -rf PND_Resources/examples/ 11 | rm -f PND_Resources/load81 12 | rm -f LOAD81.pnd 13 | -------------------------------------------------------------------------------- /contrib/Pandora/PND_Resources/DejaVuSansMono-Bold.ttf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/antirez/load81/f3b4404c2ba9fe24dcad3fdc8679de9662fcc1f5/contrib/Pandora/PND_Resources/DejaVuSansMono-Bold.ttf -------------------------------------------------------------------------------- /contrib/Pandora/PND_Resources/PXML.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | LOAD81 8 | 9 | 10 | SDL based Lua programming environment for kids, similar to Codea. 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | LOAD81 20 | 21 | LOAD81 22 | 23 | SDL based Lua programming environment for kids, similar to Codea. 24 | 25 | SDL based Lua programming environment for kids, similar to Codea. 26 | 27 | 28 | 29 | 30 | 31 | 32 | 33 | 34 | 35 | 36 | -------------------------------------------------------------------------------- /contrib/Pandora/PND_Resources/defconf/profile.txt: -------------------------------------------------------------------------------- 1 | # Global Settings 2 | targetapp=LOAD81 by antirez, Pandora build by torpor 3 | filepath=./examples/ 4 | 5 | # Command Settings 6 | 7 | # Extension Settings 8 | [lua] 9 | exepath=./load81 --full 10 | extarg=--width;0;%na%;800 11 | extarg=--height;0;%na%;480 12 | extarg=;0;%na%;%filename% 13 | 14 | # Custom Entries Settings 15 | -------------------------------------------------------------------------------- /contrib/Pandora/PND_Resources/icon.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/antirez/load81/f3b4404c2ba9fe24dcad3fdc8679de9662fcc1f5/contrib/Pandora/PND_Resources/icon.png -------------------------------------------------------------------------------- /contrib/Pandora/PND_Resources/picklelauncher: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/antirez/load81/f3b4404c2ba9fe24dcad3fdc8679de9662fcc1f5/contrib/Pandora/PND_Resources/picklelauncher -------------------------------------------------------------------------------- /contrib/Pandora/PND_Resources/runme.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | if [ ! -f "./profile.txt" ]; then 3 | cp -R defconf/* ./ 4 | fi 5 | if [ $# -eq 0 ] 6 | then 7 | ./picklelauncher 8 | fi 9 | -------------------------------------------------------------------------------- /contrib/Pandora/README.txt: -------------------------------------------------------------------------------- 1 | 2 | This is a build-tree for making a PND file for the Open Pandora game console. It includes a 3 | PickleLauncher binary pre-built for the OpenPandora. The purpose of this project is to 4 | create an easy-to-use distribution of LOAD81 for Open Pandora users. With this build-tree 5 | it should be very easy to make a distribution of LOAD81 as new features get added. 6 | 7 | References: 8 | 9 | Open Pandora console http://openpandora.org/ 10 | PND file info http://pandorawiki.org/Pnd 11 | PickleLauncher http://pandorawiki.org/PickleLauncher 12 | http://sourceforge.net/projects/picklelauncher/ 13 | 14 | Requirements: 15 | + An Open Pandora console 16 | + the CDEV tools PND file, available here: 17 | http://repo.openpandora.org/?page=detail&app=cdevtools.freamon.40n8e 18 | 19 | How to use: 20 | 21 | 1. Check out the LOAD81 sources onto your Open Pandora console somewhere sensible. 22 | 2. Execute the CDEV tools and get into the cdev shell (see CDEV docs) 23 | 3. Build LOAD81 on your Open Pandora (should be as simple as typing 'make' in the main LOAD81 24 | sources tree) & verify that the load81 binary is created for Open Pandora 25 | 4. 'cd contrib/Pandora' and type 'make' or 'make clean && make pnd-file' 26 | 27 | This will then produce the LOAD81.pnd file, bundled up and packaged with PickleLauncher for 28 | selecting files, the load81 executable itself, and the load81 examples/ tree for easy use. This 29 | PND file can then be shared on repo's/forums/etc. for Open Pandora users to get their LOAD81 30 | habits formed! :) 31 | 32 | Good luck! 33 | 34 | 35 | -- 36 | Questions / Comments about this contrib: seclorum@me.com (torpor on OPB forums) 37 | Questions / Comments about LOAD81: antirez@gmail.com 38 | Questions / Comments about PickleLauncher: http://sourceforge.net/projects/picklelauncher/ 39 | 40 | -------------------------------------------------------------------------------- /contrib/nacl/.gitignore: -------------------------------------------------------------------------------- 1 | *.nexe 2 | -------------------------------------------------------------------------------- /contrib/nacl/README.md: -------------------------------------------------------------------------------- 1 | # Load81 Port to Google Native Client 2 | 3 | ## Requirements 4 | 5 | * Google Chrome 15 or higher. 6 | * Google App Engine Python SDK. 7 | * Native Client SDK. 8 | * NaclPorts with SDL and SDL_gfx built for i686 and x86\_64. 9 | * NACL\_SDK\_ROOT environment variable set. 10 | 11 | ## Building 12 | 13 | Ensure the above requirements are met, then run contrib/nacl/build.sh. 14 | 15 | ## Testing 16 | 17 | Run dev_appserver.py contrib/nacl to start an HTTP server on port 8080. Follow 18 | the instructions in the [Native Client documentation][1] to enable Native 19 | Client in Chrome 15+. Now, open http://localhost:8080/ in Chrome and Load81 20 | should start. 21 | 22 | [1]: https://developers.google.com/native-client/pepper16/devguide/devcycle/running#Local 23 | 24 | ## Publishing 25 | 26 | You will need to create a Google App Engine application first. Then: 27 | 28 | appcfg.py -A $YOUR_APP_NAME update contrib/nacl 29 | 30 | Now you need to create a Chrome application. Follow the instructions at the 31 | [Chrome Developer's Guide][2], using contrib/nacl/chrome-app/manifest.json as 32 | a template. 33 | 34 | [2]: http://code.google.com/chrome/apps/docs/developers_guide.html 35 | 36 | ## TODO 37 | 38 | * Support loading and saving local files. 39 | -------------------------------------------------------------------------------- /contrib/nacl/app.yaml: -------------------------------------------------------------------------------- 1 | application: load81 2 | version: 1 3 | runtime: python27 4 | api_version: 1 5 | threadsafe: true 6 | 7 | handlers: 8 | - url: / 9 | static_files: load81.html 10 | upload: load81.html 11 | - url: /static 12 | static_dir: static 13 | - url: /examples 14 | static_dir: examples 15 | - url: /load81-i686-nacl.nexe 16 | static_files: load81-i686-nacl.nexe 17 | upload: load81-i686-nacl.nexe 18 | - url: /load81-x86_64-nacl.nexe 19 | static_files: load81-x86_64-nacl.nexe 20 | upload: load81-x86_64-nacl.nexe 21 | -------------------------------------------------------------------------------- /contrib/nacl/build.sh: -------------------------------------------------------------------------------- 1 | #!/bin/sh 2 | # TODO use the common Makefile instead of duplicating it. 3 | set -ex 4 | 5 | if [ -z $NACL_SDK_ROOT ] 6 | then 7 | echo "NACL_SDK_ROOT must be set." 8 | echo "example: NACL_SDK_ROOT=$HOME/nacl-sdk-update/pepper_15" 9 | exit 1 10 | fi 11 | 12 | TOOLCHAIN=$NACL_SDK_ROOT/toolchain/linux_x86_newlib 13 | NACL_ROOT=$(dirname $(which $0)) 14 | ROOT=$(readlink -f $NACL_ROOT/../..) 15 | PKGS="sdl SDL_gfx" 16 | 17 | rm -f $NACL_ROOT/examples 18 | ln -s $ROOT/examples $NACL_ROOT/examples 19 | 20 | for HOST in i686-nacl x86_64-nacl 21 | do 22 | cd $NACL_ROOT 23 | export PKG_CONFIG_PATH=$TOOLCHAIN/$HOST/usr/lib/pkgconfig 24 | export CC=$HOST-gcc 25 | BFDARCH=i386 26 | if [ $HOST == i686-nacl ]; then 27 | BFDNAME=elf32-nacl 28 | else 29 | BFDNAME=elf64-nacl 30 | fi 31 | CFLAGS="-O2 -Wall -W -D main=load81_main `pkg-config --cflags $PKGS`" 32 | LDFLAGS="" 33 | for X in read write open close seek mount; do 34 | LDFLAGS="$LDFLAGS -Xlinker --wrap -Xlinker $X" 35 | done 36 | LIBS="`pkg-config --libs $PKGS` -llua -lm -lppapi -lppapi_cpp -lnacl-mounts -lstdc++ -lnosys" 37 | SRCS="$ROOT/load81.c $ROOT/editor.c $ROOT/framebuffer.c nacl.cc" 38 | $CC $CFLAGS $LDFLAGS $SRCS $LIBS -o load81-$HOST.nexe 39 | $HOST-strip load81-$HOST.nexe 40 | done 41 | 42 | echo Built successfully 43 | -------------------------------------------------------------------------------- /contrib/nacl/chrome-app/icon_128.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/antirez/load81/f3b4404c2ba9fe24dcad3fdc8679de9662fcc1f5/contrib/nacl/chrome-app/icon_128.png -------------------------------------------------------------------------------- /contrib/nacl/chrome-app/manifest.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "rlane-load81", 3 | "description": "Lua programming environment for kids similar to Codea", 4 | "version": "0.0.0.1", 5 | "icons": { 6 | "128": "icon_128.png" 7 | }, 8 | "app": { 9 | "urls": [ 10 | "http://rlane-load81.appspot.com/" 11 | ], 12 | "launch": { 13 | "web_url": "http://rlane-load81.appspot.com/" 14 | } 15 | } 16 | } 17 | -------------------------------------------------------------------------------- /contrib/nacl/load81.html: -------------------------------------------------------------------------------- 1 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | Load81 13 | 14 | 15 | 16 | 17 | 23 | 24 |
25 |
26 | 27 |
28 |

Welcome to Load81

29 |
30 | 31 |
32 |

Load

33 | 34 |
35 | 36 |
Loading: 0%
37 | 38 | 39 | 40 | -------------------------------------------------------------------------------- /contrib/nacl/nacl.cc: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | 7 | #include 8 | #include 9 | #include 10 | #include 11 | #include 12 | #include 13 | #include 14 | 15 | #include 16 | #include 17 | #include 18 | 19 | extern "C" { 20 | extern int load81_main(int argc, char *argv[]); 21 | extern int mount(const char *type, const char *dir, int flags, void *data); 22 | extern char _binary_example_lua_start[]; 23 | extern int _binary_example_lua_size; 24 | } 25 | 26 | /* 27 | * Copied from earth sample in naclports. 28 | */ 29 | class Load81Instance : public pp::Instance { 30 | public: 31 | explicit Load81Instance(PP_Instance instance) 32 | : pp::Instance(instance), 33 | sdl_main_thread_(0), 34 | width_(0), 35 | height_(0) { 36 | printf("PluginInstance\n"); 37 | } 38 | 39 | ~Load81Instance() { 40 | if (sdl_main_thread_) { 41 | pthread_join(sdl_main_thread_, NULL); 42 | } 43 | } 44 | 45 | virtual void DidChangeView(const pp::Rect& position, const pp::Rect& clip) { 46 | printf("did change view, new %dx%d, old %dx%d\n", 47 | position.size().width(), position.size().height(), 48 | width_, height_); 49 | 50 | if (position.size().width() == width_ && 51 | position.size().height() == height_) 52 | return; // Size didn't change, no need to update anything. 53 | } 54 | 55 | bool HandleInputEvent(const pp::InputEvent& event) { 56 | SDL_NACL_PushEvent(event); 57 | return true; 58 | } 59 | 60 | void HandleMessage(const pp::Var& message) { 61 | fprintf(stderr, "HandleMessage\n"); 62 | std::string data = message.AsString(); 63 | int fd = open("program.lua", O_CREAT | O_WRONLY); 64 | if (fd < 0) { 65 | perror("open"); 66 | return; 67 | } 68 | 69 | if (write(fd, data.c_str(), data.length()) < 0) { 70 | perror("write"); 71 | return; 72 | } 73 | 74 | if (close(fd) < 0) { 75 | perror("close"); 76 | return; 77 | } 78 | 79 | SDL_NACL_SetInstance(pp_instance(), 800, 600); 80 | int lval = SDL_Init(SDL_INIT_VIDEO); 81 | assert(lval >= 0); 82 | pthread_create(&sdl_main_thread_, NULL, sdl_thread, this); 83 | } 84 | 85 | bool Init(uint32_t argc, const char* argn[], const char* argv[]) { 86 | if (RequestFilteringInputEvents(PP_INPUTEVENT_CLASS_KEYBOARD|PP_INPUTEVENT_CLASS_WHEEL)) { 87 | throw std::runtime_error("failed to request filtering input events"); 88 | } 89 | 90 | if (RequestInputEvents(PP_INPUTEVENT_CLASS_MOUSE)) { 91 | throw std::runtime_error("failed to request input events"); 92 | } 93 | 94 | return true; 95 | } 96 | 97 | private: 98 | pthread_t sdl_main_thread_; 99 | int width_; 100 | int height_; 101 | 102 | static void* sdl_thread(void* param) { 103 | char argv0[] = "load81"; 104 | char argv1[] = "program.lua"; 105 | char argv2[] = "--fps"; 106 | char *argv[] = { argv0, argv1, argv2, NULL }; 107 | load81_main(3, argv); 108 | return NULL; 109 | } 110 | }; 111 | 112 | class Load81Module : public pp::Module { 113 | public: 114 | Load81Module() : pp::Module() {} 115 | 116 | virtual ~Load81Module() { 117 | } 118 | 119 | virtual bool Init() { 120 | return true; 121 | } 122 | 123 | virtual pp::Instance* CreateInstance(PP_Instance instance) { 124 | return new Load81Instance(instance); 125 | } 126 | }; 127 | 128 | namespace pp { 129 | Module* CreateModule() { 130 | return new Load81Module(); 131 | } 132 | } 133 | -------------------------------------------------------------------------------- /contrib/nacl/static/load81.css: -------------------------------------------------------------------------------- 1 | body { 2 | margin: 0; 3 | padding: 0; 4 | background-color: #B2B0B0; 5 | } 6 | 7 | #menu { 8 | position: absolute; 9 | left: 0; 10 | top: 0; 11 | width: 100%; 12 | background-color: #181818; 13 | z-index: 70; 14 | box-shadow: 0px 3px 2px #808080; 15 | cursor: default; 16 | } 17 | 18 | 19 | #menu div { 20 | display: block; 21 | float: left; 22 | padding-left: 10px; 23 | padding-right: 10px; 24 | padding-top: 5px; 25 | padding-bottom: 5px; 26 | cursor: pointer; 27 | font-weight: bold; 28 | color: #FF7E00; 29 | } 30 | 31 | #menu div:hover { 32 | background-color: #282828; 33 | } 34 | 35 | .screen { 36 | position: absolute; 37 | opacity: 0.0; 38 | left: 50%; 39 | top: 50%; 40 | padding-left: 40px; 41 | padding-right: 40px; 42 | padding-top: 10px; 43 | padding-bottom: 10px; 44 | width: 720px; 45 | min-height: 580px; 46 | margin-left: -400px; 47 | margin-top: -300px; 48 | z-index: 50; 49 | background: white; 50 | border: solid 1px black; 51 | box-shadow: 3px 3px 2px #808080; 52 | } 53 | 54 | #play-screen { 55 | padding: 0; 56 | width: 800px; 57 | height: 600px; 58 | } 59 | 60 | #nacl_module { 61 | width: 800px; 62 | height: 600px; 63 | } 64 | 65 | #status { 66 | position: absolute; 67 | display: none; 68 | left: 50%; 69 | top: 50%; 70 | width: 300px; 71 | margin-left: -150px; 72 | margin-top: -20px; 73 | z-index: 100; 74 | text-align: center; 75 | background-color: rgba(100, 100, 100, 1); 76 | border: solid 1px black; 77 | padding: 4px; 78 | color: black; 79 | } 80 | -------------------------------------------------------------------------------- /contrib/nacl/static/load81.js: -------------------------------------------------------------------------------- 1 | var initial_script = "examples/asteroids.lua"; 2 | 3 | $(document).ready(function(){ 4 | var nacl_module = null; 5 | var src = null; 6 | 7 | function main() { 8 | xhr = new XMLHttpRequest(); 9 | xhr.open("GET", initial_script); 10 | xhr.onload = function (event) { 11 | // TODO check for failure 12 | start_game(xhr.responseText); 13 | } 14 | xhr.send(); 15 | } 16 | 17 | function switch_screen(id) { 18 | $(".screen").css({ "z-index": 50 }); 19 | $(".screen").animate({ opacity:0.0 }, { queue: false, duration: "slow" }); 20 | $("#"+id).css({ "z-index": 60 }); 21 | $("#"+id).animate({ opacity:1.0 }, { queue: false, duration: "slow" }); 22 | } 23 | 24 | function start_game(_src) { 25 | if (nacl_module) { 26 | $(nacl_module).remove(); 27 | nacl_module = null; 28 | } 29 | 30 | nacl_html = ''; 35 | 36 | var e = document.createElement("embed"); 37 | e.id = "nacl_module"; 38 | e.name = "nacl_module"; 39 | e.src = "static/load81.nmf"; 40 | e.type = "application/x-nacl"; 41 | e.tabIndex = -1; 42 | $("#play-screen").append(e); 43 | nacl_module = e; 44 | src = _src; 45 | } 46 | 47 | /* 48 | * Native Client events 49 | */ 50 | 51 | function moduleDidStartLoad() { 52 | $("#status").html("Loading..."); 53 | $("#status").show(); 54 | } 55 | 56 | function moduleLoadProgress(event) { 57 | if (event.total != 0) { 58 | var load_percent = Math.round(100.0 * event.loaded / event.total); 59 | $("#status").html("Loading: " + load_percent + "%"); 60 | } 61 | } 62 | 63 | function moduleLoadError() { 64 | } 65 | 66 | function moduleLoadAbort() { 67 | } 68 | 69 | function moduleDidLoad() { 70 | nacl_module.postMessage(src); 71 | switch_screen("play-screen"); 72 | nacl_module.focus(); 73 | } 74 | 75 | function moduleDidEndLoad() { 76 | var lastError = event.target.lastError; 77 | if (lastError != undefined && lastError != "") { 78 | $("#status").html(lastError) 79 | } else { 80 | $("#status").fadeOut("slow") 81 | switch_screen("play-screen"); 82 | } 83 | } 84 | 85 | function handleMessage(message_event) { 86 | console.log(message_event.data); 87 | } 88 | 89 | var listener = document.getElementById('play-screen') 90 | listener.addEventListener('loadstart', moduleDidStartLoad, true); 91 | listener.addEventListener('progress', moduleLoadProgress, true); 92 | listener.addEventListener('error', moduleLoadError, true); 93 | listener.addEventListener('abort', moduleLoadAbort, true); 94 | listener.addEventListener('load', moduleDidLoad, true); 95 | listener.addEventListener('loadend', moduleDidEndLoad, true); 96 | listener.addEventListener('message', handleMessage, true); 97 | 98 | /* 99 | * Menu buttons 100 | */ 101 | 102 | $("#play").click(function(event) { 103 | switch_screen("play-screen"); 104 | if (nacl_module) { 105 | nacl_module.focus(); 106 | } 107 | }); 108 | 109 | $("#help").click(function(event) { 110 | switch_screen("help-screen"); 111 | }); 112 | 113 | $("#load").click(function(event) { 114 | switch_screen("load-screen"); 115 | }); 116 | 117 | $("#save").click(function(event) { 118 | // TODO 119 | alert("save!"); 120 | }); 121 | 122 | 123 | /* 124 | * Load screen 125 | */ 126 | 127 | $("#uploader").change(function(event) { 128 | var file = event.target.files[0]; 129 | var reader = new FileReader(); 130 | reader.onload = function(evt) { 131 | console.log(["uploaded", evt]); 132 | var code = reader.result; 133 | start_game(code); 134 | }; 135 | reader.readAsBinaryString(file); 136 | }); 137 | 138 | 139 | main(); 140 | }); 141 | -------------------------------------------------------------------------------- /contrib/nacl/static/load81.nmf: -------------------------------------------------------------------------------- 1 | { 2 | "program": { 3 | "x86-64": {"url": "../load81-x86_64-nacl.nexe"}, 4 | "x86-32": {"url": "../load81-i686-nacl.nexe"} 5 | } 6 | } 7 | -------------------------------------------------------------------------------- /editor.h: -------------------------------------------------------------------------------- 1 | #ifndef EDITOR_H 2 | #define EDITOR_H 3 | 4 | #include "framebuffer.h" 5 | 6 | #define POWEROFF_BUTTON_X (E.fb->width-18) 7 | #define POWEROFF_BUTTON_Y 18 8 | #define SAVE_BUTTON_X (E.fb->width-E.margin_right-13) 9 | #define SAVE_BUTTON_Y (E.fb->height-16) 10 | #define EDITOR_FPS 30 11 | 12 | /* Syntax highlight types */ 13 | #define HL_NORMAL 0 14 | #define HL_ERROR 1 15 | #define HL_COMMENT 2 16 | #define HL_KEYWORD 3 17 | #define HL_STRING 4 18 | #define HL_NUMBER 5 19 | #define HL_FUNCDEF 6 20 | #define HL_LIB 7 21 | 22 | #define HL_NORMAL_COLOR {165,165,255} 23 | #define HL_ERROR_COLOR {255,0,0} 24 | #define HL_COMMENT_COLOR {180,180,0} 25 | #define HL_KEYWORD_COLOR {50,255,50} 26 | #define HL_STRING_COLOR {0,255,255} 27 | #define HL_NUMBER_COLOR {225,100,100} 28 | #define HL_FUNCDEF_COLOR {255,255,255} 29 | #define HL_LIB_COLOR {255,0,255} 30 | 31 | typedef struct erow { 32 | int size; /* Size of the row, excluding the null term. */ 33 | char *chars; /* Row content. */ 34 | unsigned char *hl; /* Syntax highlight type for each character. */ 35 | } erow; 36 | 37 | typedef struct keyState { 38 | int ksym; 39 | int counter; 40 | } keyState; 41 | 42 | typedef struct hlcolor { 43 | int r,g,b; 44 | } hlcolor; 45 | 46 | #define KEY_MAX 32 /* Max number of key events we can store at the same time. */ 47 | struct editorConfig { 48 | int cx,cy; /* Cursor x and y position in characters */ 49 | unsigned char cblink; /* Show cursor if (cblink & 0x80) == 0 */ 50 | int screenrows; /* Number of rows that we can show */ 51 | int screencols; /* Number of cols that we can show */ 52 | int margin_top, margin_bottom, margin_left, margin_right; 53 | int rowoff; /* Row offset on screen */ 54 | int coloff; /* Column offset on screen */ 55 | int numrows; /* Number of rows */ 56 | erow *row; /* Rows */ 57 | time_t lastevent; /* Last event time, so we can go standby */ 58 | keyState key[KEY_MAX]; /* Remember if a key is pressed / repeated. */ 59 | int dirty; /* File modified but not saved. */ 60 | char *filename; /* Currently open filename */ 61 | frameBuffer *fb; /* Framebuffer */ 62 | char *err; /* Error string to display, or NULL if no error. */ 63 | int errline; /* Error line to highlight if err != NULL. */ 64 | }; 65 | 66 | /* ================================ Prototypes ============================== */ 67 | 68 | /* editor.c */ 69 | void initEditor(frameBuffer *fb, int mt, int mb, int ml, int mr); 70 | char *editorRowsToString(int *buflen); 71 | int editorOpen(char *filename); 72 | int editorSave(char *filename); 73 | int editorEvents(void); 74 | void editorSetError(const char *err, int line); 75 | void editorClearError(void); 76 | int editorFileWasModified(void); 77 | void editorRun(void); 78 | 79 | #endif /* EDITOR_H */ 80 | -------------------------------------------------------------------------------- /examples/2dsim.lua: -------------------------------------------------------------------------------- 1 | -- 2dsim, very basic 2D physics simulator 2 | -- Written by Salvatore Sanfilippo and Enea Sanfilippo 3 | 4 | function setup() 5 | -- x and y are the current ball position. 6 | x = WIDTH/2 7 | y = HEIGHT/2 8 | -- vx, vy is the velocity. 9 | vx = 0 10 | vy = 0 11 | -- ax, ay is the acceleration. 12 | ax = 0 13 | ay = 0 14 | -- ground is the y coordinate of the ground line. 15 | -- radius is the radius of the ball. 16 | GROUND = 50 17 | RADIUS = 10 18 | end 19 | 20 | function draw() 21 | -- Retrace the scene, writing the ground as a blue box. 22 | background(0,0,0) 23 | fill(0,0,255,1) 24 | rect(0,0,WIDTH,GROUND-RADIUS) 25 | 26 | -- Recompute the position using the velocity, also recompute the 27 | -- velocity using the acceleration. 28 | x = (x + vx) % WIDTH 29 | y = y + vy 30 | vx = vx + ax 31 | vy = vy + ay 32 | 33 | -- Let the user accelerate the body using arrow keys. 34 | ax = 0 35 | ay = 0 36 | if keyboard.pressed['left'] then 37 | ax = -1 38 | end 39 | if keyboard.pressed['right'] then 40 | ax = 1 41 | end 42 | if keyboard.pressed['up'] then 43 | ay = 1 44 | end 45 | if keyboard.pressed['down'] then 46 | ay = -1 47 | end 48 | 49 | -- Simulate gravity, always adding a negative Y acceleration. 50 | ay = ay - 0.5 51 | 52 | -- Detect contact with the ground. 53 | if y < GROUND then 54 | -- When the ball hits the ground, invert the sign of the y 55 | -- velocity in order to simulate bouncing. 56 | -- However only let 50% of the speed to be retained. 57 | y = GROUND 58 | vy = -vy * 0.5 59 | -- Filter small y velocity to avoid the ball to bounce forever. 60 | if (math.abs(vy) < 1) then 61 | vy = 0 62 | end 63 | end 64 | 65 | -- Simulate friction with the ground by reducign the X speed when 66 | -- the ball touches the ground. 67 | if y == GROUND then 68 | vx = vx * 0.95 69 | end 70 | 71 | -- Draw the ball at the new position. 72 | fill(255,255,0,1) 73 | ellipse(x,y,RADIUS,RADIUS) 74 | end 75 | -------------------------------------------------------------------------------- /examples/asteroids.lua: -------------------------------------------------------------------------------- 1 | -- Simple Asteroids-alike game. 2 | -- Copyright (C) 2012 Salvatore Sanfilippo. 3 | -- This code is released under the BSD two-clause license. 4 | 5 | function setup() 6 | ticks = 0 -- Number of iteration of the program 7 | shipx = WIDTH/2 -- Ship x position 8 | shipy = HEIGHT/2 -- Ship y position 9 | shipa = 0 -- Ship rotation angle 10 | shipvx = 0 -- Ship x velocity 11 | shipvy = 0 -- Ship y velocity 12 | bullets = {} -- An array of bullets 13 | asteroids = {} -- An array of asteroids 14 | asteroids_num = 0; -- Number of asteroids on screen 15 | asteroids_max = 2; -- Max number of asteroids to show 16 | last_bullet_ticks = 0 -- Ticks at the time the last bullet was fired 17 | 18 | -- Populate the game with asteroids at start 19 | while(asteroids_num < asteroids_max) do 20 | addAsteroid() 21 | end 22 | end 23 | 24 | -- The following functions move objects adding the velocity 25 | -- to the position at every iteration. 26 | 27 | function moveShip() 28 | shipx = (shipx + shipvx) % WIDTH 29 | shipy = (shipy + shipvy) % HEIGHT 30 | end 31 | 32 | function moveBullets() 33 | local i,b 34 | for i,b in pairs(bullets) do 35 | b.x = b.x+b.vx 36 | b.y = b.y+b.vy 37 | b.ttl = b.ttl - 1 38 | if b.ttl == 0 then bullets[i] = nil end 39 | end 40 | end 41 | 42 | function moveAsteroids() 43 | local i,a 44 | for i,a in pairs(asteroids) do 45 | a.x = (a.x+a.vx) % WIDTH 46 | a.y = (a.y+a.vy) % HEIGHT 47 | end 48 | end 49 | 50 | -- Add an asteroid. Create a random asteroid so that it's 51 | -- not too close to the ship. 52 | 53 | function addAsteroid() 54 | local x,y,a,ray 55 | while true do 56 | x = math.random(WIDTH) 57 | y = math.random(HEIGHT) 58 | ray = math.random(20,40) 59 | if math.abs(x-shipx) > ray and 60 | math.abs(y-shipy) > ray then 61 | break 62 | end 63 | end 64 | a = { x = x, y = y, vx = math.random()*2, vy = math.random()*2, 65 | ray = ray } 66 | table.insert(asteroids,a) 67 | asteroids_num = asteroids_num + 1 68 | end 69 | 70 | -- Fire a bullet with velocity 2,2 and the same orientation 71 | -- as the ship orientation. 72 | 73 | function fire() 74 | local b 75 | -- Don't fire again if we already fired 76 | -- less than 5 iterations ago. 77 | if ticks - last_bullet_ticks < 5 then return end 78 | b = { x = shipx, y = shipy, 79 | vx = shipvx+(4*math.sin(shipa)), 80 | vy = shipvy+(4*math.cos(shipa)), 81 | ttl=300 } 82 | -- Make sure that the bullet originaes from ship head 83 | b.x = b.x+(20*math.sin(shipa)) 84 | b.y = b.y+(20*math.cos(shipa)) 85 | -- Finally insert the bullet in the table of bullets 86 | table.insert(bullets,b) 87 | last_bullet_ticks = ticks 88 | end 89 | 90 | -- Draw the screen, move objects, detect collisions. 91 | function draw() 92 | ticks = ticks+1 93 | 94 | -- Handle keyboard events. 95 | if keyboard.pressed['left'] then shipa = shipa - 0.1 end 96 | if keyboard.pressed['right'] then shipa = shipa + 0.1 end 97 | if keyboard.pressed['up'] then 98 | shipvx = shipvx + 0.15*math.sin(shipa) 99 | shipvy = shipvy + 0.15*math.cos(shipa) 100 | end 101 | if keyboard.pressed['space'] then fire() end 102 | 103 | -- Create a new asteroid from time to time if needed 104 | if asteroids_num < asteroids_max and (ticks % 200) == 0 then 105 | while(asteroids_num < asteroids_max) do addAsteroid() end 106 | end 107 | 108 | -- Move all the objects of the game. 109 | moveShip() 110 | moveBullets() 111 | moveAsteroids() 112 | checkBulletCollision() 113 | 114 | -- Draw the current game screen. 115 | background(0,0,0) 116 | drawBullets() 117 | drawAsteroids() 118 | drawShip(shipx,shipy,shipa) 119 | end 120 | 121 | -- Math formula to rotate a point counterclockwise, with 122 | -- rotation center at 0,0. 123 | 124 | function rotatePoint(x,y,a) 125 | return x*math.cos(a)-y*math.sin(a), 126 | y*math.cos(a)+x*math.sin(a) 127 | end 128 | 129 | -- Draw the ship, that is composed of three vertex, 130 | -- rotating the vertexes using rotatePoint(). 131 | 132 | function drawShip(x,y,a) 133 | local triangles = {} 134 | table.insert(triangles, 135 | {x0 = -10, y0 = -10, x1 = 0, y1 = 20, x2 = 10, y2 = -10, 136 | r = 255, g = 0, b = 0, alpha = 1 }) 137 | if keyboard.pressed['up'] then 138 | table.insert(triangles, 139 | {x0 = -5, y0 = -10, x1 = 0, y1 = math.random(-25,-20), x2 = 5, y2 = -10, 140 | r = 255, g = 255, b = 0, alpha = 1 }) 141 | end 142 | for i,t in pairs(triangles) do 143 | fill(t.r,t.g,t.b,t.alpha) 144 | t.x0,t.y0 = rotatePoint(t.x0,t.y0,-a); 145 | t.x1,t.y1 = rotatePoint(t.x1,t.y1,-a); 146 | t.x2,t.y2 = rotatePoint(t.x2,t.y2,-a); 147 | triangle(x+t.x0,y+t.y0,x+t.x1,y+t.y1,x+t.x2,y+t.y2) 148 | end 149 | end 150 | 151 | -- Draw a bullet, that's just a single pixel. 152 | 153 | function drawBullets() 154 | local i,b 155 | for i,b in pairs(bullets) do 156 | fill(255,255,255,1) 157 | rect(b.x-1,b.y-1,3,3) 158 | end 159 | end 160 | 161 | -- Draw an asteroid. 162 | 163 | function drawAsteroids() 164 | local i,a 165 | for i,a in pairs(asteroids) do 166 | fill(150,150,150,1) 167 | ellipse(a.x,a.y,a.ray,a.ray) 168 | end 169 | end 170 | 171 | -- This function detects the collision between a bullet 172 | -- and an asteroid, and removes both the bullet and the 173 | -- asteroid from the game when they collide. 174 | 175 | function checkBulletCollision() 176 | local i,j,b,a,del_asteroids,del_bullets 177 | del_asteroids = {} 178 | del_bullets = {} 179 | for i,b in pairs(bullets) do 180 | for j,a in pairs(asteroids) do 181 | local distance,dx,dy 182 | dx = a.x-b.x 183 | dy = a.y-b.y 184 | distance = math.sqrt((dx*dx)+(dy*dy)) 185 | if distance < a.ray then 186 | del_asteroids[j] = true 187 | del_bullets[i] = true 188 | break 189 | end 190 | end 191 | end 192 | for i,b in pairs(del_bullets) do 193 | table.remove(bullets,i) 194 | end 195 | for i,a in pairs(del_asteroids) do 196 | table.remove(asteroids,i) 197 | asteroids_num = asteroids_num - 1 198 | if asteroids_num == 0 then 199 | asteroids_max = asteroids_max + 1 200 | end 201 | end 202 | end 203 | -------------------------------------------------------------------------------- /examples/flames.lua: -------------------------------------------------------------------------------- 1 | -- Flames.lua, contributed by pmprog in the OpenPandora board. 2 | -- See http://boards.openpandora.org/index.php?/topic/7405-here-is-a-pnd-for-load81/ 3 | 4 | function setup() 5 | local x, y, l 6 | 7 | MaxFlames = 150 8 | FlameLife = 40 9 | FlameSize = 10 10 | refreshCount = 0 11 | skipCount = 0 12 | Flames = { } 13 | 14 | for i=1,MaxFlames do 15 | x = math.random(WIDTH/3) + (WIDTH/3) 16 | y = math.random(FlameSize) 17 | l = math.random(FlameLife) 18 | a = { x = x, y = y, l = l } 19 | table.insert(Flames,a) 20 | end 21 | end 22 | 23 | function draw() 24 | local i, f, minMove 25 | 26 | background(0,0,0) 27 | for i,f in pairs(Flames) do 28 | if f.l > 35 then 29 | fill(255, 255, 255, 0.9) 30 | minMove = 0 31 | elseif f.l > 30 then 32 | fill(255, 255, 192, 0.8) 33 | minMove = 1 34 | elseif f.l > 20 then 35 | fill(255, 192, 128, 0.7) 36 | minMove = 2 37 | elseif f.l > 10 then 38 | fill(220, 128, 100, 0.5) 39 | minMove = 3 40 | else 41 | fill(160, 128, 80, 0.3) 42 | minMove = 5 43 | end 44 | 45 | ellipse(f.x,f.y,FlameSize,FlameSize) 46 | f.l = f.l - math.random(3) 47 | if f.l <= 0 then 48 | f.x = math.random(WIDTH/3) + (WIDTH/3) 49 | f.y = math.random(FlameSize) 50 | f.l = FlameLife 51 | else 52 | f.y = f.y + (math.random(6) + minMove) 53 | f.x = f.x + math.random(7) - 3 54 | end 55 | end 56 | end 57 | 58 | -------------------------------------------------------------------------------- /examples/keynames.lua: -------------------------------------------------------------------------------- 1 | function setup() 2 | print("Press any key to see the corresponding name.") 3 | end 4 | 5 | function draw() 6 | for k,v in pairs(keyboard.pressed) do 7 | print(k) 8 | end 9 | end 10 | -------------------------------------------------------------------------------- /examples/lines.lua: -------------------------------------------------------------------------------- 1 | function setup() 2 | background(0,0,0); 3 | end 4 | 5 | function draw() 6 | fill(math.random(255),math.random(255),math.random(255),math.random()) 7 | line (math.random(WIDTH),math.random(HEIGHT), 8 | math.random(WIDTH),math.random(HEIGHT)) 9 | end 10 | -------------------------------------------------------------------------------- /examples/paint.lua: -------------------------------------------------------------------------------- 1 | function setup() 2 | background(0,0,0) 3 | end 4 | 5 | function draw() 6 | if mouse.pressed['1'] then 7 | fill(255,0,0,.2) 8 | else 9 | fill(0,0,255,.2) 10 | end 11 | ellipse(mouse.x,mouse.y,30,30) 12 | end 13 | -------------------------------------------------------------------------------- /examples/sprite.lua: -------------------------------------------------------------------------------- 1 | local t, x = 0, 0 2 | 3 | function draw() 4 | background(0, 0, 0) 5 | local c = math.abs(math.cos(2*3.14*t)); 6 | local y = 0.5*HEIGHT*c/math.exp(0.8*t); 7 | 8 | sprite("examples/sprite.png", x, y,t*-150); 9 | x = x + 3 10 | t = t + 0.01 11 | 12 | if (x > WIDTH) then 13 | t, x = 0, 0 14 | end 15 | end 16 | -------------------------------------------------------------------------------- /examples/sprite.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/antirez/load81/f3b4404c2ba9fe24dcad3fdc8679de9662fcc1f5/examples/sprite.png -------------------------------------------------------------------------------- /examples/text.lua: -------------------------------------------------------------------------------- 1 | function draw() 2 | background(0,0,0) 3 | fill(100,50,250,1) 4 | text(20,HEIGHT-40,"Please, enter this window with your mouse pointer.") 5 | fill(255,0,0,1) 6 | text(mouse.x,mouse.y,"Hello World!") 7 | fill(200,200,200,1) 8 | text(0,0,string.format("Mouse is at x:%s y:%s",mouse.x,mouse.y)) 9 | end 10 | -------------------------------------------------------------------------------- /examples/triangles.lua: -------------------------------------------------------------------------------- 1 | function setup() 2 | background(0,0,0); 3 | end 4 | 5 | function draw() 6 | fill(math.random(255),math.random(255),math.random(255),math.random()) 7 | triangle(math.random(WIDTH),math.random(HEIGHT), 8 | math.random(WIDTH),math.random(HEIGHT), 9 | math.random(WIDTH),math.random(HEIGHT)) 10 | end 11 | -------------------------------------------------------------------------------- /framebuffer.h: -------------------------------------------------------------------------------- 1 | #ifndef FRAMEBUFFER_H 2 | #define FRAMEBUFFER_H 3 | 4 | #include 5 | #include 6 | #include 7 | #include 8 | #include 9 | #include 10 | #include 11 | #include 12 | 13 | #define FONT_WIDTH 16 14 | #define FONT_HEIGHT 16 15 | #define FONT_KERNING 10 16 | 17 | typedef struct frameBuffer { 18 | int width; 19 | int height; 20 | SDL_Window *screen; 21 | SDL_Renderer *renderer; 22 | SDL_Texture *texture; 23 | FPSmanager fps_mgr; 24 | } frameBuffer; 25 | 26 | /* Frame buffer */ 27 | frameBuffer *createFrameBuffer(int width, int height, int fullscreen); 28 | void presentFrameBuffer(frameBuffer *fb); 29 | 30 | /* Drawing primitives */ 31 | void setPixelWithAlpha(frameBuffer *fb, int x, int y, int r, int g, int b, int alpha); 32 | void fillBackground(frameBuffer *fb, int r, int g, int b); 33 | void drawHline(frameBuffer *fb, int x1, int x2, int y, int r, int g, int b, int alpha); 34 | void drawEllipse(frameBuffer *fb, int xc, int yc, int radx, int rady, int r, int g, int b, int alpha); 35 | void drawBox(frameBuffer *fb, int x1, int y1, int x2, int y2, int r, int g, int b, int alpha); 36 | void drawTriangle(frameBuffer *fb, int x1, int y1, int x2, int y2, int x3, int y3, int r, int g, int b, int alpha); 37 | void drawLine(frameBuffer *fb, int x1, int y1, int x2, int y2, int r, int g, int b, int alpha); 38 | 39 | /* Bitmap font */ 40 | void bfLoadFont(char **c); 41 | void bfWriteChar(frameBuffer *fb, int xp, int yp, int c, int r, int g, int b, int alpha); 42 | void bfWriteString(frameBuffer *fb, int xp, int yp, const char *s, int len, int r, int g, int b, int alpha); 43 | 44 | /* Sprites */ 45 | void spriteBlit(frameBuffer *fb, void *sprite, int x, int y, int angle, int aa); 46 | void *spriteLoad(lua_State *L, const char *filename); 47 | void initSpriteEngine(lua_State *L); 48 | 49 | #endif /* FRAMEBUFFER_H */ 50 | -------------------------------------------------------------------------------- /load81.h: -------------------------------------------------------------------------------- 1 | #ifndef LOAD81_H 2 | #define LOAD81_H 3 | 4 | #include "framebuffer.h" 5 | 6 | /* ================================ Defaults ================================ */ 7 | 8 | #define DEFAULT_WIDTH 800 9 | #define DEFAULT_HEIGHT 600 10 | #define DEFAULT_BPP 24 11 | 12 | /* ============================= Data structures ============================ */ 13 | 14 | struct globalConfig { 15 | /* Runtime */ 16 | int r,g,b; 17 | int alpha; 18 | int fps; 19 | long long start_ms; 20 | long long epoch; 21 | frameBuffer *fb; 22 | char *filename; 23 | lua_State *L; 24 | int luaerr; /* True if there was an error in the latest iteration. */ 25 | /* Configuration */ 26 | int width; 27 | int height; 28 | int bpp; 29 | /* Command line switches */ 30 | int opt_show_fps; 31 | int opt_full_screen; 32 | }; 33 | 34 | extern struct globalConfig l81; 35 | 36 | #endif /* LOAD81_H */ 37 | -------------------------------------------------------------------------------- /lua/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) 1994-2012 Lua.org, 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 | -------------------------------------------------------------------------------- /lua/INSTALL: -------------------------------------------------------------------------------- 1 | INSTALL for Lua 5.1 2 | 3 | * Building Lua 4 | ------------ 5 | Lua is built in the src directory, but the build process can be 6 | controlled from the top-level Makefile. 7 | 8 | Building Lua on Unix systems should be very easy. First do "make" and 9 | see if your platform is listed. If so, just do "make xxx", where xxx 10 | is your platform name. The platforms currently supported are: 11 | aix ansi bsd freebsd generic linux macosx mingw posix solaris 12 | 13 | If your platform is not listed, try the closest one or posix, generic, 14 | ansi, in this order. 15 | 16 | See below for customization instructions and for instructions on how 17 | to build with other Windows compilers. 18 | 19 | If you want to check that Lua has been built correctly, do "make test" 20 | after building Lua. Also, have a look at the example programs in test. 21 | 22 | * Installing Lua 23 | -------------- 24 | Once you have built Lua, you may want to install it in an official 25 | place in your system. In this case, do "make install". The official 26 | place and the way to install files are defined in Makefile. You must 27 | have the right permissions to install files. 28 | 29 | If you want to build and install Lua in one step, do "make xxx install", 30 | where xxx is your platform name. 31 | 32 | If you want to install Lua locally, then do "make local". This will 33 | create directories bin, include, lib, man, and install Lua there as 34 | follows: 35 | 36 | bin: lua luac 37 | include: lua.h luaconf.h lualib.h lauxlib.h lua.hpp 38 | lib: liblua.a 39 | man/man1: lua.1 luac.1 40 | 41 | These are the only directories you need for development. 42 | 43 | There are man pages for lua and luac, in both nroff and html, and a 44 | reference manual in html in doc, some sample code in test, and some 45 | useful stuff in etc. You don't need these directories for development. 46 | 47 | If you want to install Lua locally, but in some other directory, do 48 | "make install INSTALL_TOP=xxx", where xxx is your chosen directory. 49 | 50 | See below for instructions for Windows and other systems. 51 | 52 | * Customization 53 | ------------- 54 | Three things can be customized by editing a file: 55 | - Where and how to install Lua -- edit Makefile. 56 | - How to build Lua -- edit src/Makefile. 57 | - Lua features -- edit src/luaconf.h. 58 | 59 | You don't actually need to edit the Makefiles because you may set the 60 | relevant variables when invoking make. 61 | 62 | On the other hand, if you need to select some Lua features, you'll need 63 | to edit src/luaconf.h. The edited file will be the one installed, and 64 | it will be used by any Lua clients that you build, to ensure consistency. 65 | 66 | We strongly recommend that you enable dynamic loading. This is done 67 | automatically for all platforms listed above that have this feature 68 | (and also Windows). See src/luaconf.h and also src/Makefile. 69 | 70 | * Building Lua on Windows and other systems 71 | ----------------------------------------- 72 | If you're not using the usual Unix tools, then the instructions for 73 | building Lua depend on the compiler you use. You'll need to create 74 | projects (or whatever your compiler uses) for building the library, 75 | the interpreter, and the compiler, as follows: 76 | 77 | library: lapi.c lcode.c ldebug.c ldo.c ldump.c lfunc.c lgc.c llex.c 78 | lmem.c lobject.c lopcodes.c lparser.c lstate.c lstring.c 79 | ltable.c ltm.c lundump.c lvm.c lzio.c 80 | lauxlib.c lbaselib.c ldblib.c liolib.c lmathlib.c loslib.c 81 | ltablib.c lstrlib.c loadlib.c linit.c 82 | 83 | interpreter: library, lua.c 84 | 85 | compiler: library, luac.c print.c 86 | 87 | If you use Visual Studio .NET, you can use etc/luavs.bat in its 88 | "Command Prompt". 89 | 90 | If all you want is to build the Lua interpreter, you may put all .c files 91 | in a single project, except for luac.c and print.c. Or just use etc/all.c. 92 | 93 | To use Lua as a library in your own programs, you'll need to know how to 94 | create and use libraries with your compiler. 95 | 96 | As mentioned above, you may edit luaconf.h to select some features before 97 | building Lua. 98 | 99 | (end of INSTALL) 100 | -------------------------------------------------------------------------------- /lua/Makefile: -------------------------------------------------------------------------------- 1 | # makefile for installing Lua 2 | # see INSTALL for installation instructions 3 | # see src/Makefile and src/luaconf.h for further customization 4 | 5 | # == CHANGE THE SETTINGS BELOW TO SUIT YOUR ENVIRONMENT ======================= 6 | 7 | # Your platform. See PLATS for possible values. 8 | PLAT= none 9 | 10 | # Where to install. The installation starts in the src and doc directories, 11 | # so take care if INSTALL_TOP is not an absolute path. 12 | INSTALL_TOP= /usr/local 13 | INSTALL_BIN= $(INSTALL_TOP)/bin 14 | INSTALL_INC= $(INSTALL_TOP)/include 15 | INSTALL_LIB= $(INSTALL_TOP)/lib 16 | INSTALL_MAN= $(INSTALL_TOP)/man/man1 17 | # 18 | # You probably want to make INSTALL_LMOD and INSTALL_CMOD consistent with 19 | # LUA_ROOT, LUA_LDIR, and LUA_CDIR in luaconf.h (and also with etc/lua.pc). 20 | INSTALL_LMOD= $(INSTALL_TOP)/share/lua/$V 21 | INSTALL_CMOD= $(INSTALL_TOP)/lib/lua/$V 22 | 23 | # How to install. If your install program does not support "-p", then you 24 | # may have to run ranlib on the installed liblua.a (do "make ranlib"). 25 | INSTALL= install -p 26 | INSTALL_EXEC= $(INSTALL) -m 0755 27 | INSTALL_DATA= $(INSTALL) -m 0644 28 | # 29 | # If you don't have install you can use cp instead. 30 | # INSTALL= cp -p 31 | # INSTALL_EXEC= $(INSTALL) 32 | # INSTALL_DATA= $(INSTALL) 33 | 34 | # Utilities. 35 | MKDIR= mkdir -p 36 | RANLIB= ranlib 37 | 38 | # == END OF USER SETTINGS. NO NEED TO CHANGE ANYTHING BELOW THIS LINE ========= 39 | 40 | # Convenience platforms targets. 41 | PLATS= aix ansi bsd freebsd generic linux macosx mingw posix solaris 42 | 43 | # What to install. 44 | TO_BIN= lua luac 45 | TO_INC= lua.h luaconf.h lualib.h lauxlib.h ../etc/lua.hpp 46 | TO_LIB= liblua.a 47 | TO_MAN= lua.1 luac.1 48 | 49 | # Lua version and release. 50 | V= 5.1 51 | R= 5.1.5 52 | 53 | all: $(PLAT) 54 | 55 | $(PLATS) clean: 56 | cd src && $(MAKE) $@ 57 | 58 | test: dummy 59 | src/lua test/hello.lua 60 | 61 | install: dummy 62 | cd src && $(MKDIR) $(INSTALL_BIN) $(INSTALL_INC) $(INSTALL_LIB) $(INSTALL_MAN) $(INSTALL_LMOD) $(INSTALL_CMOD) 63 | cd src && $(INSTALL_EXEC) $(TO_BIN) $(INSTALL_BIN) 64 | cd src && $(INSTALL_DATA) $(TO_INC) $(INSTALL_INC) 65 | cd src && $(INSTALL_DATA) $(TO_LIB) $(INSTALL_LIB) 66 | cd doc && $(INSTALL_DATA) $(TO_MAN) $(INSTALL_MAN) 67 | 68 | ranlib: 69 | cd src && cd $(INSTALL_LIB) && $(RANLIB) $(TO_LIB) 70 | 71 | local: 72 | $(MAKE) install INSTALL_TOP=.. 73 | 74 | none: 75 | @echo "Please do" 76 | @echo " make PLATFORM" 77 | @echo "where PLATFORM is one of these:" 78 | @echo " $(PLATS)" 79 | @echo "See INSTALL for complete instructions." 80 | 81 | # make may get confused with test/ and INSTALL in a case-insensitive OS 82 | dummy: 83 | 84 | # echo config parameters 85 | echo: 86 | @echo "" 87 | @echo "These are the parameters currently set in src/Makefile to build Lua $R:" 88 | @echo "" 89 | @cd src && $(MAKE) -s echo 90 | @echo "" 91 | @echo "These are the parameters currently set in Makefile to install Lua $R:" 92 | @echo "" 93 | @echo "PLAT = $(PLAT)" 94 | @echo "INSTALL_TOP = $(INSTALL_TOP)" 95 | @echo "INSTALL_BIN = $(INSTALL_BIN)" 96 | @echo "INSTALL_INC = $(INSTALL_INC)" 97 | @echo "INSTALL_LIB = $(INSTALL_LIB)" 98 | @echo "INSTALL_MAN = $(INSTALL_MAN)" 99 | @echo "INSTALL_LMOD = $(INSTALL_LMOD)" 100 | @echo "INSTALL_CMOD = $(INSTALL_CMOD)" 101 | @echo "INSTALL_EXEC = $(INSTALL_EXEC)" 102 | @echo "INSTALL_DATA = $(INSTALL_DATA)" 103 | @echo "" 104 | @echo "See also src/luaconf.h ." 105 | @echo "" 106 | 107 | # echo private config parameters 108 | pecho: 109 | @echo "V = $(V)" 110 | @echo "R = $(R)" 111 | @echo "TO_BIN = $(TO_BIN)" 112 | @echo "TO_INC = $(TO_INC)" 113 | @echo "TO_LIB = $(TO_LIB)" 114 | @echo "TO_MAN = $(TO_MAN)" 115 | 116 | # echo config parameters as Lua code 117 | # uncomment the last sed expression if you want nil instead of empty strings 118 | lecho: 119 | @echo "-- installation parameters for Lua $R" 120 | @echo "VERSION = '$V'" 121 | @echo "RELEASE = '$R'" 122 | @$(MAKE) echo | grep = | sed -e 's/= /= "/' -e 's/$$/"/' #-e 's/""/nil/' 123 | @echo "-- EOF" 124 | 125 | # list targets that do not create files (but not all makes understand .PHONY) 126 | .PHONY: all $(PLATS) clean test install local none dummy echo pecho lecho 127 | 128 | # (end of Makefile) 129 | -------------------------------------------------------------------------------- /lua/README: -------------------------------------------------------------------------------- 1 | README for Lua 5.1 2 | 3 | See INSTALL for installation instructions. 4 | See HISTORY for a summary of changes since the last released version. 5 | 6 | * What is Lua? 7 | ------------ 8 | Lua is a powerful, light-weight programming language designed for extending 9 | applications. Lua is also frequently used as a general-purpose, stand-alone 10 | language. Lua is free software. 11 | 12 | For complete information, visit Lua's web site at http://www.lua.org/ . 13 | For an executive summary, see http://www.lua.org/about.html . 14 | 15 | Lua has been used in many different projects around the world. 16 | For a short list, see http://www.lua.org/uses.html . 17 | 18 | * Availability 19 | ------------ 20 | Lua is freely available for both academic and commercial purposes. 21 | See COPYRIGHT and http://www.lua.org/license.html for details. 22 | Lua can be downloaded at http://www.lua.org/download.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. In most Unix-like platforms, simply 28 | do "make" with a suitable target. See INSTALL for detailed instructions. 29 | 30 | * Origin 31 | ------ 32 | Lua is developed at Lua.org, a laboratory of the Department of Computer 33 | Science of PUC-Rio (the Pontifical Catholic University of Rio de Janeiro 34 | in Brazil). 35 | For more information about the authors, see http://www.lua.org/authors.html . 36 | 37 | (end of README) 38 | -------------------------------------------------------------------------------- /lua/doc/cover.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/antirez/load81/f3b4404c2ba9fe24dcad3fdc8679de9662fcc1f5/lua/doc/cover.png -------------------------------------------------------------------------------- /lua/doc/logo.gif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/antirez/load81/f3b4404c2ba9fe24dcad3fdc8679de9662fcc1f5/lua/doc/logo.gif -------------------------------------------------------------------------------- /lua/doc/lua.1: -------------------------------------------------------------------------------- 1 | .\" $Id: lua.man,v 1.11 2006/01/06 16:03:34 lhf Exp $ 2 | .TH LUA 1 "$Date: 2006/01/06 16:03:34 $" 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 119 | (but in this case you have to be careful with quotes 120 | if the prompt string contains a space; otherwise you may confuse the shell.) 121 | The default prompts are "> " and ">> ". 122 | .SH OPTIONS 123 | .TP 124 | .B \- 125 | load and execute the standard input as a file, 126 | that is, 127 | not interactively, 128 | even when the standard input is a terminal. 129 | .TP 130 | .BI \-e " stat" 131 | execute statement 132 | .IR stat . 133 | You need to quote 134 | .I stat 135 | if it contains spaces, quotes, 136 | or other characters special to the shell. 137 | .TP 138 | .B \-i 139 | enter interactive mode after 140 | .I script 141 | is executed. 142 | .TP 143 | .BI \-l " name" 144 | call 145 | .BI require(' name ') 146 | before executing 147 | .IR script . 148 | Typically used to load libraries. 149 | .TP 150 | .B \-v 151 | show version information. 152 | .SH "SEE ALSO" 153 | .BR luac (1) 154 | .br 155 | http://www.lua.org/ 156 | .SH DIAGNOSTICS 157 | Error messages should be self explanatory. 158 | .SH AUTHORS 159 | R. Ierusalimschy, 160 | L. H. de Figueiredo, 161 | and 162 | W. Celes 163 | .\" EOF 164 | -------------------------------------------------------------------------------- /lua/doc/lua.css: -------------------------------------------------------------------------------- 1 | body { 2 | color: #000000 ; 3 | background-color: #FFFFFF ; 4 | font-family: Helvetica, Arial, sans-serif ; 5 | text-align: justify ; 6 | margin-right: 30px ; 7 | margin-left: 30px ; 8 | } 9 | 10 | h1, h2, h3, h4 { 11 | font-family: Verdana, Geneva, sans-serif ; 12 | font-weight: normal ; 13 | font-style: italic ; 14 | } 15 | 16 | h2 { 17 | padding-top: 0.4em ; 18 | padding-bottom: 0.4em ; 19 | padding-left: 30px ; 20 | padding-right: 30px ; 21 | margin-left: -30px ; 22 | background-color: #E0E0FF ; 23 | } 24 | 25 | h3 { 26 | padding-left: 0.5em ; 27 | border-left: solid #E0E0FF 1em ; 28 | } 29 | 30 | table h3 { 31 | padding-left: 0px ; 32 | border-left: none ; 33 | } 34 | 35 | a:link { 36 | color: #000080 ; 37 | background-color: inherit ; 38 | text-decoration: none ; 39 | } 40 | 41 | a:visited { 42 | background-color: inherit ; 43 | text-decoration: none ; 44 | } 45 | 46 | a:link:hover, a:visited:hover { 47 | color: #000080 ; 48 | background-color: #E0E0FF ; 49 | } 50 | 51 | a:link:active, a:visited:active { 52 | color: #FF0000 ; 53 | } 54 | 55 | hr { 56 | border: 0 ; 57 | height: 1px ; 58 | color: #a0a0a0 ; 59 | background-color: #a0a0a0 ; 60 | } 61 | 62 | :target { 63 | background-color: #F8F8F8 ; 64 | padding: 8px ; 65 | border: solid #a0a0a0 2px ; 66 | } 67 | 68 | .footer { 69 | color: gray ; 70 | font-size: small ; 71 | } 72 | 73 | input[type=text] { 74 | border: solid #a0a0a0 2px ; 75 | border-radius: 2em ; 76 | -moz-border-radius: 2em ; 77 | background-image: url('images/search.png') ; 78 | background-repeat: no-repeat; 79 | background-position: 4px center ; 80 | padding-left: 20px ; 81 | height: 2em ; 82 | } 83 | 84 | -------------------------------------------------------------------------------- /lua/doc/lua.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | LUA man page 5 | 6 | 7 | 8 | 9 | 10 |

NAME

11 | lua - Lua interpreter 12 |

SYNOPSIS

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

DESCRIPTION

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

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

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

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

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

94 | In interactive mode, 95 | lua 96 | prompts the user, 97 | reads lines from the standard input, 98 | and executes them as they are read. 99 | If a line does not contain a complete statement, 100 | then a secondary prompt is displayed and 101 | lines are read until a complete statement is formed or 102 | a syntax error is found. 103 | So, one way to interrupt the reading of an incomplete statement is 104 | to force a syntax error: 105 | adding a 106 | ';' 107 | in the middle of a statement is a sure way of forcing a syntax error 108 | (except inside multiline strings and comments; these must be closed explicitly). 109 | If a line starts with 110 | '=', 111 | then 112 | lua 113 | displays the values of all the expressions in the remainder of the 114 | line. The expressions must be separated by commas. 115 | The primary prompt is the value of the global variable 116 | _PROMPT, 117 | if this value is a string; 118 | otherwise, the default prompt is used. 119 | Similarly, the secondary prompt is the value of the global variable 120 | _PROMPT2. 121 | So, 122 | to change the prompts, 123 | set the corresponding variable to a string of your choice. 124 | You can do that after calling the interpreter 125 | or on the command line 126 | (but in this case you have to be careful with quotes 127 | if the prompt string contains a space; otherwise you may confuse the shell.) 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 name 151 | call 152 | require('name') 153 | before executing 154 | script. 155 | Typically used to load libraries. 156 |

157 | -v 158 | show version information. 159 |

SEE ALSO

160 | luac(1) 161 |
162 | http://www.lua.org/ 163 |

DIAGNOSTICS

164 | Error messages should be self explanatory. 165 |

AUTHORS

166 | R. Ierusalimschy, 167 | L. H. de Figueiredo, 168 | and 169 | W. Celes 170 | 171 | 172 | 173 | -------------------------------------------------------------------------------- /lua/doc/luac.1: -------------------------------------------------------------------------------- 1 | .\" $Id: luac.man,v 1.28 2006/01/06 16:03:34 lhf Exp $ 2 | .TH LUAC 1 "$Date: 2006/01/06 16:03:34 $" 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 later loaded and executed. 17 | .LP 18 | The main advantages of precompiling chunks are: 19 | faster loading, 20 | protecting source code from accidental 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 | Pre-compiled chunks are not necessarily smaller than the corresponding source. 30 | The main goal in pre-compiling is faster loading. 31 | .LP 32 | The binary files created by 33 | .B luac 34 | are portable only among architectures with the same word size and byte order. 35 | .LP 36 | .B luac 37 | produces a single output file containing the bytecodes 38 | for all source files given. 39 | By default, 40 | the output file is named 41 | .BR luac.out , 42 | but you can change this with the 43 | .B \-o 44 | option. 45 | .LP 46 | In the command line, 47 | you can mix 48 | text files containing Lua source and 49 | binary files containing precompiled chunks. 50 | This is useful to combine several precompiled chunks, 51 | even from different (but compatible) platforms, 52 | into a single precompiled chunk. 53 | .LP 54 | You can use 55 | .B "'\-'" 56 | to indicate the standard input as a source file 57 | and 58 | .B "'\--'" 59 | to signal the end of options 60 | (that is, 61 | all remaining arguments will be treated as files even if they start with 62 | .BR "'\-'" ). 63 | .LP 64 | The internal format of the binary files produced by 65 | .B luac 66 | is likely to change when a new version of Lua is released. 67 | So, 68 | save the source files of all Lua programs that you precompile. 69 | .LP 70 | .SH OPTIONS 71 | Options must be separate. 72 | .TP 73 | .B \-l 74 | produce a listing of the compiled bytecode for Lua's virtual machine. 75 | Listing bytecodes is useful to learn about Lua's virtual machine. 76 | If no files are given, then 77 | .B luac 78 | loads 79 | .B luac.out 80 | and lists its contents. 81 | .TP 82 | .BI \-o " file" 83 | output to 84 | .IR file , 85 | instead of the default 86 | .BR luac.out . 87 | (You can use 88 | .B "'\-'" 89 | for standard output, 90 | but not on platforms that open standard output in text mode.) 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 a stripped chunk, 116 | then the error messages may not contain the full information they usually do. 117 | For instance, 118 | line numbers and names of local variables are lost. 119 | .TP 120 | .B \-v 121 | show version information. 122 | .SH FILES 123 | .TP 15 124 | .B luac.out 125 | default output file 126 | .SH "SEE ALSO" 127 | .BR lua (1) 128 | .br 129 | http://www.lua.org/ 130 | .SH DIAGNOSTICS 131 | Error messages should be self explanatory. 132 | .SH AUTHORS 133 | L. H. de Figueiredo, 134 | R. Ierusalimschy and 135 | W. Celes 136 | .\" EOF 137 | -------------------------------------------------------------------------------- /lua/doc/luac.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | LUAC man page 5 | 6 | 7 | 8 | 9 | 10 |

NAME

11 | luac - Lua compiler 12 |

SYNOPSIS

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

DESCRIPTION

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

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

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

36 | Precompiled chunks are not necessarily smaller than the corresponding source. 37 | The main goal in precompiling is faster loading. 38 |

39 | The binary files created by 40 | luac 41 | are portable only among architectures with the same word size and byte order. 42 |

43 | luac 44 | produces a single output file containing the bytecodes 45 | for all source files given. 46 | By default, 47 | the output file is named 48 | luac.out, 49 | but you can change this with the 50 | -o 51 | option. 52 |

53 | In the command line, 54 | you can mix 55 | text files containing Lua source and 56 | binary files containing precompiled chunks. 57 | This is useful because several precompiled chunks, 58 | even from different (but compatible) platforms, 59 | can be combined into a single precompiled chunk. 60 |

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

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

77 |

OPTIONS

78 | Options must be separate. 79 |

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

89 | -o file 90 | output to 91 | file, 92 | instead of the default 93 | luac.out. 94 | (You can use 95 | '-' 96 | for standard output, 97 | but not on platforms that open standard output in text mode.) 98 | The output file may be a source file because 99 | all files are loaded before the output file is written. 100 | Be careful not to overwrite precious files. 101 |

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

119 | -s 120 | strip debug information before writing the output file. 121 | This saves some space in very large chunks, 122 | but if errors occur when running a stripped chunk, 123 | then the error messages may not contain the full information they usually do. 124 | For instance, 125 | line numbers and names of local variables are lost. 126 |

127 | -v 128 | show version information. 129 |

FILES

130 |

131 | luac.out 132 | default output file 133 |

SEE ALSO

134 | lua(1) 135 |
136 | http://www.lua.org/ 137 |

DIAGNOSTICS

138 | Error messages should be self explanatory. 139 |

AUTHORS

140 | L. H. de Figueiredo, 141 | R. Ierusalimschy and 142 | W. Celes 143 | 144 | 145 | 146 | -------------------------------------------------------------------------------- /lua/doc/manual.css: -------------------------------------------------------------------------------- 1 | h3 code { 2 | font-family: inherit ; 3 | font-size: inherit ; 4 | } 5 | 6 | pre, code { 7 | font-size: 12pt ; 8 | } 9 | 10 | span.apii { 11 | float: right ; 12 | font-family: inherit ; 13 | font-style: normal ; 14 | font-size: small ; 15 | color: gray ; 16 | } 17 | 18 | p+h1, ul+h1 { 19 | padding-top: 0.4em ; 20 | padding-bottom: 0.4em ; 21 | padding-left: 30px ; 22 | margin-left: -30px ; 23 | background-color: #E0E0FF ; 24 | } 25 | -------------------------------------------------------------------------------- /lua/doc/readme.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | Lua documentation 4 | 5 | 6 | 7 | 8 | 9 |
10 |

11 | Lua 12 | Documentation 13 |

14 | 15 | This is the documentation included in the source distribution of Lua 5.1.5. 16 | 17 | 25 | 26 | Lua's 27 | official web site 28 | contains updated documentation, 29 | especially the 30 | reference manual. 31 |

32 | 33 |


34 | 35 | Last update: 36 | Fri Feb 3 09:44:42 BRST 2012 37 | 38 | 39 | 40 | 41 | -------------------------------------------------------------------------------- /lua/etc/Makefile: -------------------------------------------------------------------------------- 1 | # makefile for Lua etc 2 | 3 | TOP= .. 4 | LIB= $(TOP)/src 5 | INC= $(TOP)/src 6 | BIN= $(TOP)/src 7 | SRC= $(TOP)/src 8 | TST= $(TOP)/test 9 | 10 | CC= gcc 11 | CFLAGS= -O2 -Wall -I$(INC) $(MYCFLAGS) 12 | MYCFLAGS= 13 | MYLDFLAGS= -Wl,-E 14 | MYLIBS= -lm 15 | #MYLIBS= -lm -Wl,-E -ldl -lreadline -lhistory -lncurses 16 | RM= rm -f 17 | 18 | default: 19 | @echo 'Please choose a target: min noparser one strict clean' 20 | 21 | min: min.c 22 | $(CC) $(CFLAGS) $@.c -L$(LIB) -llua $(MYLIBS) 23 | echo 'print"Hello there!"' | ./a.out 24 | 25 | noparser: noparser.o 26 | $(CC) noparser.o $(SRC)/lua.o -L$(LIB) -llua $(MYLIBS) 27 | $(BIN)/luac $(TST)/hello.lua 28 | -./a.out luac.out 29 | -./a.out -e'a=1' 30 | 31 | one: 32 | $(CC) $(CFLAGS) all.c $(MYLIBS) 33 | ./a.out $(TST)/hello.lua 34 | 35 | strict: 36 | -$(BIN)/lua -e 'print(a);b=2' 37 | -$(BIN)/lua -lstrict -e 'print(a)' 38 | -$(BIN)/lua -e 'function f() b=2 end f()' 39 | -$(BIN)/lua -lstrict -e 'function f() b=2 end f()' 40 | 41 | clean: 42 | $(RM) a.out core core.* *.o luac.out 43 | 44 | .PHONY: default min noparser one strict clean 45 | -------------------------------------------------------------------------------- /lua/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 | If any of the makes fail, you're probably not using the same libraries 5 | used to build Lua. Set MYLIBS in Makefile accordingly. 6 | 7 | all.c 8 | Full Lua interpreter in a single file. 9 | Do "make one" for a demo. 10 | 11 | lua.hpp 12 | Lua header files for C++ using 'extern "C"'. 13 | 14 | lua.ico 15 | A Lua icon for Windows (and web sites: save as favicon.ico). 16 | Drawn by hand by Markus Gritsch . 17 | 18 | lua.pc 19 | pkg-config data for Lua 20 | 21 | luavs.bat 22 | Script to build Lua under "Visual Studio .NET Command Prompt". 23 | Run it from the toplevel as etc\luavs.bat. 24 | 25 | min.c 26 | A minimal Lua interpreter. 27 | Good for learning and for starting your own. 28 | Do "make min" for a demo. 29 | 30 | noparser.c 31 | Linking with noparser.o avoids loading the parsing modules in lualib.a. 32 | Do "make noparser" for a demo. 33 | 34 | strict.lua 35 | Traps uses of undeclared global variables. 36 | Do "make strict" for a demo. 37 | 38 | -------------------------------------------------------------------------------- /lua/etc/all.c: -------------------------------------------------------------------------------- 1 | /* 2 | * all.c -- Lua core, libraries and interpreter in a single file 3 | */ 4 | 5 | #define luaall_c 6 | 7 | #include "lapi.c" 8 | #include "lcode.c" 9 | #include "ldebug.c" 10 | #include "ldo.c" 11 | #include "ldump.c" 12 | #include "lfunc.c" 13 | #include "lgc.c" 14 | #include "llex.c" 15 | #include "lmem.c" 16 | #include "lobject.c" 17 | #include "lopcodes.c" 18 | #include "lparser.c" 19 | #include "lstate.c" 20 | #include "lstring.c" 21 | #include "ltable.c" 22 | #include "ltm.c" 23 | #include "lundump.c" 24 | #include "lvm.c" 25 | #include "lzio.c" 26 | 27 | #include "lauxlib.c" 28 | #include "lbaselib.c" 29 | #include "ldblib.c" 30 | #include "liolib.c" 31 | #include "linit.c" 32 | #include "lmathlib.c" 33 | #include "loadlib.c" 34 | #include "loslib.c" 35 | #include "lstrlib.c" 36 | #include "ltablib.c" 37 | 38 | #include "lua.c" 39 | -------------------------------------------------------------------------------- /lua/etc/lua.hpp: -------------------------------------------------------------------------------- 1 | // lua.hpp 2 | // Lua header files for C++ 3 | // <> not supplied automatically because Lua also compiles as C++ 4 | 5 | extern "C" { 6 | #include "lua.h" 7 | #include "lualib.h" 8 | #include "lauxlib.h" 9 | } 10 | -------------------------------------------------------------------------------- /lua/etc/lua.ico: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/antirez/load81/f3b4404c2ba9fe24dcad3fdc8679de9662fcc1f5/lua/etc/lua.ico -------------------------------------------------------------------------------- /lua/etc/lua.pc: -------------------------------------------------------------------------------- 1 | # lua.pc -- pkg-config data for Lua 2 | 3 | # vars from install Makefile 4 | 5 | # grep '^V=' ../Makefile 6 | V= 5.1 7 | # grep '^R=' ../Makefile 8 | R= 5.1.5 9 | 10 | # grep '^INSTALL_.*=' ../Makefile | sed 's/INSTALL_TOP/prefix/' 11 | prefix= /usr/local 12 | INSTALL_BIN= ${prefix}/bin 13 | INSTALL_INC= ${prefix}/include 14 | INSTALL_LIB= ${prefix}/lib 15 | INSTALL_MAN= ${prefix}/man/man1 16 | INSTALL_LMOD= ${prefix}/share/lua/${V} 17 | INSTALL_CMOD= ${prefix}/lib/lua/${V} 18 | 19 | # canonical vars 20 | exec_prefix=${prefix} 21 | libdir=${exec_prefix}/lib 22 | includedir=${prefix}/include 23 | 24 | Name: Lua 25 | Description: An Extensible Extension Language 26 | Version: ${R} 27 | Requires: 28 | Libs: -L${libdir} -llua -lm 29 | Cflags: -I${includedir} 30 | 31 | # (end of lua.pc) 32 | -------------------------------------------------------------------------------- /lua/etc/luavs.bat: -------------------------------------------------------------------------------- 1 | @rem Script to build Lua under "Visual Studio .NET Command Prompt". 2 | @rem Do not run from this directory; run it from the toplevel: etc\luavs.bat . 3 | @rem It creates lua51.dll, lua51.lib, lua.exe, and luac.exe in src. 4 | @rem (contributed by David Manura and Mike Pall) 5 | 6 | @setlocal 7 | @set MYCOMPILE=cl /nologo /MD /O2 /W3 /c /D_CRT_SECURE_NO_DEPRECATE 8 | @set MYLINK=link /nologo 9 | @set MYMT=mt /nologo 10 | 11 | cd src 12 | %MYCOMPILE% /DLUA_BUILD_AS_DLL l*.c 13 | del lua.obj luac.obj 14 | %MYLINK% /DLL /out:lua51.dll l*.obj 15 | if exist lua51.dll.manifest^ 16 | %MYMT% -manifest lua51.dll.manifest -outputresource:lua51.dll;2 17 | %MYCOMPILE% /DLUA_BUILD_AS_DLL lua.c 18 | %MYLINK% /out:lua.exe lua.obj lua51.lib 19 | if exist lua.exe.manifest^ 20 | %MYMT% -manifest lua.exe.manifest -outputresource:lua.exe 21 | %MYCOMPILE% l*.c print.c 22 | del lua.obj linit.obj lbaselib.obj ldblib.obj liolib.obj lmathlib.obj^ 23 | loslib.obj ltablib.obj lstrlib.obj loadlib.obj 24 | %MYLINK% /out:luac.exe *.obj 25 | if exist luac.exe.manifest^ 26 | %MYMT% -manifest luac.exe.manifest -outputresource:luac.exe 27 | del *.obj *.manifest 28 | cd .. 29 | -------------------------------------------------------------------------------- /lua/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 | 9 | #include "lua.h" 10 | #include "lauxlib.h" 11 | 12 | static int print(lua_State *L) 13 | { 14 | int n=lua_gettop(L); 15 | int i; 16 | for (i=1; i<=n; i++) 17 | { 18 | if (i>1) printf("\t"); 19 | if (lua_isstring(L,i)) 20 | printf("%s",lua_tostring(L,i)); 21 | else if (lua_isnil(L,i)) 22 | printf("%s","nil"); 23 | else if (lua_isboolean(L,i)) 24 | printf("%s",lua_toboolean(L,i) ? "true" : "false"); 25 | else 26 | printf("%s:%p",luaL_typename(L,i),lua_topointer(L,i)); 27 | } 28 | printf("\n"); 29 | return 0; 30 | } 31 | 32 | int main(void) 33 | { 34 | lua_State *L=lua_open(); 35 | lua_register(L,"print",print); 36 | if (luaL_dofile(L,NULL)!=0) fprintf(stderr,"%s\n",lua_tostring(L,-1)); 37 | lua_close(L); 38 | return 0; 39 | } 40 | -------------------------------------------------------------------------------- /lua/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 list 8 | * its object file before the Lua libraries. The linker should then not load 9 | * the parsing modules. To try it, do "make luab". 10 | * 11 | * If you also want to avoid the dump module (ldump.o), define NODUMP. 12 | * #define NODUMP 13 | */ 14 | 15 | #define LUA_CORE 16 | 17 | #include "llex.h" 18 | #include "lparser.h" 19 | #include "lzio.h" 20 | 21 | LUAI_FUNC void luaX_init (lua_State *L) { 22 | UNUSED(L); 23 | } 24 | 25 | LUAI_FUNC Proto *luaY_parser (lua_State *L, ZIO *z, Mbuffer *buff, const char *name) { 26 | UNUSED(z); 27 | UNUSED(buff); 28 | UNUSED(name); 29 | lua_pushliteral(L,"parser not loaded"); 30 | lua_error(L); 31 | return NULL; 32 | } 33 | 34 | #ifdef NODUMP 35 | #include "lundump.h" 36 | 37 | LUAI_FUNC int luaU_dump (lua_State* L, const Proto* f, lua_Writer w, void* data, int strip) { 38 | UNUSED(f); 39 | UNUSED(w); 40 | UNUSED(data); 41 | UNUSED(strip); 42 | #if 1 43 | UNUSED(L); 44 | return 0; 45 | #else 46 | lua_pushliteral(L,"dumper not loaded"); 47 | lua_error(L); 48 | #endif 49 | } 50 | #endif 51 | -------------------------------------------------------------------------------- /lua/etc/strict.lua: -------------------------------------------------------------------------------- 1 | -- 2 | -- strict.lua 3 | -- checks uses of undeclared global variables 4 | -- All global variables must be 'declared' through a regular assignment 5 | -- (even assigning nil will do) in a main chunk before being used 6 | -- anywhere or assigned to inside a function. 7 | -- 8 | 9 | local getinfo, error, rawset, rawget = debug.getinfo, error, rawset, rawget 10 | 11 | local mt = getmetatable(_G) 12 | if mt == nil then 13 | mt = {} 14 | setmetatable(_G, mt) 15 | end 16 | 17 | mt.__declared = {} 18 | 19 | local function what () 20 | local d = getinfo(3, "S") 21 | return d and d.what or "C" 22 | end 23 | 24 | mt.__newindex = function (t, n, v) 25 | if not mt.__declared[n] then 26 | local w = what() 27 | if w ~= "main" and w ~= "C" then 28 | error("assign to undeclared variable '"..n.."'", 2) 29 | end 30 | mt.__declared[n] = true 31 | end 32 | rawset(t, n, v) 33 | end 34 | 35 | mt.__index = function (t, n) 36 | if not mt.__declared[n] and what() ~= "C" then 37 | error("variable '"..n.."' is not declared", 2) 38 | end 39 | return rawget(t, n) 40 | end 41 | 42 | -------------------------------------------------------------------------------- /lua/src/Makefile: -------------------------------------------------------------------------------- 1 | # makefile for building Lua 2 | # see ../INSTALL for installation instructions 3 | # see ../Makefile and luaconf.h for further customization 4 | 5 | # == CHANGE THE SETTINGS BELOW TO SUIT YOUR ENVIRONMENT ======================= 6 | 7 | # Your platform. See PLATS for possible values. 8 | PLAT= none 9 | 10 | CC= gcc 11 | CFLAGS= -O2 -Wall $(MYCFLAGS) 12 | AR= ar rcu 13 | RANLIB= ranlib 14 | RM= rm -f 15 | LIBS= -lm $(MYLIBS) 16 | 17 | MYCFLAGS= 18 | MYLDFLAGS= 19 | MYLIBS= 20 | 21 | # == END OF USER SETTINGS. NO NEED TO CHANGE ANYTHING BELOW THIS LINE ========= 22 | 23 | PLATS= aix ansi bsd freebsd generic linux macosx mingw posix solaris 24 | 25 | LUA_A= liblua.a 26 | CORE_O= lapi.o lcode.o ldebug.o ldo.o ldump.o lfunc.o lgc.o llex.o lmem.o \ 27 | lobject.o lopcodes.o lparser.o lstate.o lstring.o ltable.o ltm.o \ 28 | lundump.o lvm.o lzio.o 29 | LIB_O= lauxlib.o lbaselib.o ldblib.o liolib.o lmathlib.o loslib.o ltablib.o \ 30 | lstrlib.o loadlib.o linit.o 31 | 32 | LUA_T= lua 33 | LUA_O= lua.o 34 | 35 | LUAC_T= luac 36 | LUAC_O= luac.o print.o 37 | 38 | ALL_O= $(CORE_O) $(LIB_O) $(LUA_O) $(LUAC_O) 39 | ALL_T= $(LUA_A) $(LUA_T) $(LUAC_T) 40 | ALL_A= $(LUA_A) 41 | 42 | default: $(PLAT) 43 | 44 | all: $(ALL_T) 45 | 46 | o: $(ALL_O) 47 | 48 | a: $(ALL_A) 49 | 50 | $(LUA_A): $(CORE_O) $(LIB_O) 51 | $(AR) $@ $(CORE_O) $(LIB_O) # DLL needs all object files 52 | $(RANLIB) $@ 53 | 54 | $(LUA_T): $(LUA_O) $(LUA_A) 55 | $(CC) -o $@ $(MYLDFLAGS) $(LUA_O) $(LUA_A) $(LIBS) 56 | 57 | $(LUAC_T): $(LUAC_O) $(LUA_A) 58 | $(CC) -o $@ $(MYLDFLAGS) $(LUAC_O) $(LUA_A) $(LIBS) 59 | 60 | clean: 61 | $(RM) $(ALL_T) $(ALL_O) 62 | 63 | depend: 64 | @$(CC) $(CFLAGS) -MM l*.c print.c 65 | 66 | echo: 67 | @echo "PLAT = $(PLAT)" 68 | @echo "CC = $(CC)" 69 | @echo "CFLAGS = $(CFLAGS)" 70 | @echo "AR = $(AR)" 71 | @echo "RANLIB = $(RANLIB)" 72 | @echo "RM = $(RM)" 73 | @echo "MYCFLAGS = $(MYCFLAGS)" 74 | @echo "MYLDFLAGS = $(MYLDFLAGS)" 75 | @echo "MYLIBS = $(MYLIBS)" 76 | 77 | # convenience targets for popular platforms 78 | 79 | none: 80 | @echo "Please choose a platform:" 81 | @echo " $(PLATS)" 82 | 83 | aix: 84 | $(MAKE) all CC="xlc" CFLAGS="-O2 -DLUA_USE_POSIX -DLUA_USE_DLOPEN" MYLIBS="-ldl" MYLDFLAGS="-brtl -bexpall" 85 | 86 | ansi: 87 | $(MAKE) all MYCFLAGS=-DLUA_ANSI 88 | 89 | bsd: 90 | $(MAKE) all MYCFLAGS="-DLUA_USE_POSIX -DLUA_USE_DLOPEN" MYLIBS="-Wl,-E" 91 | 92 | freebsd: 93 | $(MAKE) all MYCFLAGS="-DLUA_USE_LINUX" MYLIBS="-Wl,-E -lreadline" 94 | 95 | generic: 96 | $(MAKE) all MYCFLAGS= 97 | 98 | linux: 99 | $(MAKE) all MYCFLAGS=-DLUA_USE_LINUX MYLIBS="-Wl,-E -ldl -lreadline -lhistory -lncurses" 100 | 101 | macosx: 102 | $(MAKE) all MYCFLAGS=-DLUA_USE_LINUX MYLIBS="-lreadline" 103 | # use this on Mac OS X 10.3- 104 | # $(MAKE) all MYCFLAGS=-DLUA_USE_MACOSX 105 | 106 | mingw: 107 | $(MAKE) "LUA_A=lua51.dll" "LUA_T=lua.exe" \ 108 | "AR=$(CC) -shared -o" "RANLIB=strip --strip-unneeded" \ 109 | "MYCFLAGS=-DLUA_BUILD_AS_DLL" "MYLIBS=" "MYLDFLAGS=-s" lua.exe 110 | $(MAKE) "LUAC_T=luac.exe" luac.exe 111 | 112 | posix: 113 | $(MAKE) all MYCFLAGS=-DLUA_USE_POSIX 114 | 115 | solaris: 116 | $(MAKE) all MYCFLAGS="-DLUA_USE_POSIX -DLUA_USE_DLOPEN" MYLIBS="-ldl" 117 | 118 | # list targets that do not create files (but not all makes understand .PHONY) 119 | .PHONY: all $(PLATS) default o a clean depend echo none 120 | 121 | # DO NOT DELETE 122 | 123 | lapi.o: lapi.c lua.h luaconf.h lapi.h lobject.h llimits.h ldebug.h \ 124 | lstate.h ltm.h lzio.h lmem.h ldo.h lfunc.h lgc.h lstring.h ltable.h \ 125 | lundump.h lvm.h 126 | lauxlib.o: lauxlib.c lua.h luaconf.h lauxlib.h 127 | lbaselib.o: lbaselib.c lua.h luaconf.h lauxlib.h lualib.h 128 | lcode.o: lcode.c lua.h luaconf.h lcode.h llex.h lobject.h llimits.h \ 129 | lzio.h lmem.h lopcodes.h lparser.h ldebug.h lstate.h ltm.h ldo.h lgc.h \ 130 | ltable.h 131 | ldblib.o: ldblib.c lua.h luaconf.h lauxlib.h lualib.h 132 | ldebug.o: ldebug.c lua.h luaconf.h lapi.h lobject.h llimits.h lcode.h \ 133 | llex.h lzio.h lmem.h lopcodes.h lparser.h ldebug.h lstate.h ltm.h ldo.h \ 134 | lfunc.h lstring.h lgc.h ltable.h lvm.h 135 | ldo.o: ldo.c lua.h luaconf.h ldebug.h lstate.h lobject.h llimits.h ltm.h \ 136 | lzio.h lmem.h ldo.h lfunc.h lgc.h lopcodes.h lparser.h lstring.h \ 137 | ltable.h lundump.h lvm.h 138 | ldump.o: ldump.c lua.h luaconf.h lobject.h llimits.h lstate.h ltm.h \ 139 | lzio.h lmem.h lundump.h 140 | lfunc.o: lfunc.c lua.h luaconf.h lfunc.h lobject.h llimits.h lgc.h lmem.h \ 141 | lstate.h ltm.h lzio.h 142 | lgc.o: lgc.c lua.h luaconf.h ldebug.h lstate.h lobject.h llimits.h ltm.h \ 143 | lzio.h lmem.h ldo.h lfunc.h lgc.h lstring.h ltable.h 144 | linit.o: linit.c lua.h luaconf.h lualib.h lauxlib.h 145 | liolib.o: liolib.c lua.h luaconf.h lauxlib.h lualib.h 146 | llex.o: llex.c lua.h luaconf.h ldo.h lobject.h llimits.h lstate.h ltm.h \ 147 | lzio.h lmem.h llex.h lparser.h lstring.h lgc.h ltable.h 148 | lmathlib.o: lmathlib.c lua.h luaconf.h lauxlib.h lualib.h 149 | lmem.o: lmem.c lua.h luaconf.h ldebug.h lstate.h lobject.h llimits.h \ 150 | ltm.h lzio.h lmem.h ldo.h 151 | loadlib.o: loadlib.c lua.h luaconf.h lauxlib.h lualib.h 152 | lobject.o: lobject.c lua.h luaconf.h ldo.h lobject.h llimits.h lstate.h \ 153 | ltm.h lzio.h lmem.h lstring.h lgc.h lvm.h 154 | lopcodes.o: lopcodes.c lopcodes.h llimits.h lua.h luaconf.h 155 | loslib.o: loslib.c lua.h luaconf.h lauxlib.h lualib.h 156 | lparser.o: lparser.c lua.h luaconf.h lcode.h llex.h lobject.h llimits.h \ 157 | lzio.h lmem.h lopcodes.h lparser.h ldebug.h lstate.h ltm.h ldo.h \ 158 | lfunc.h lstring.h lgc.h ltable.h 159 | lstate.o: lstate.c lua.h luaconf.h ldebug.h lstate.h lobject.h llimits.h \ 160 | ltm.h lzio.h lmem.h ldo.h lfunc.h lgc.h llex.h lstring.h ltable.h 161 | lstring.o: lstring.c lua.h luaconf.h lmem.h llimits.h lobject.h lstate.h \ 162 | ltm.h lzio.h lstring.h lgc.h 163 | lstrlib.o: lstrlib.c lua.h luaconf.h lauxlib.h lualib.h 164 | ltable.o: ltable.c lua.h luaconf.h ldebug.h lstate.h lobject.h llimits.h \ 165 | ltm.h lzio.h lmem.h ldo.h lgc.h ltable.h 166 | ltablib.o: ltablib.c lua.h luaconf.h lauxlib.h lualib.h 167 | ltm.o: ltm.c lua.h luaconf.h lobject.h llimits.h lstate.h ltm.h lzio.h \ 168 | lmem.h lstring.h lgc.h ltable.h 169 | lua.o: lua.c lua.h luaconf.h lauxlib.h lualib.h 170 | luac.o: luac.c lua.h luaconf.h lauxlib.h ldo.h lobject.h llimits.h \ 171 | lstate.h ltm.h lzio.h lmem.h lfunc.h lopcodes.h lstring.h lgc.h \ 172 | lundump.h 173 | lundump.o: lundump.c lua.h luaconf.h ldebug.h lstate.h lobject.h \ 174 | llimits.h ltm.h lzio.h lmem.h ldo.h lfunc.h lstring.h lgc.h lundump.h 175 | lvm.o: lvm.c lua.h luaconf.h ldebug.h lstate.h lobject.h llimits.h ltm.h \ 176 | lzio.h lmem.h ldo.h lfunc.h lgc.h lopcodes.h lstring.h ltable.h lvm.h 177 | lzio.o: lzio.c lua.h luaconf.h llimits.h lmem.h lstate.h lobject.h ltm.h \ 178 | lzio.h 179 | print.o: print.c ldebug.h lstate.h lua.h luaconf.h lobject.h llimits.h \ 180 | ltm.h lzio.h lmem.h lopcodes.h lundump.h 181 | 182 | # (end of Makefile) 183 | -------------------------------------------------------------------------------- /lua/src/lapi.h: -------------------------------------------------------------------------------- 1 | /* 2 | ** $Id: lapi.h,v 2.2.1.1 2007/12/27 13:02:25 roberto Exp $ 3 | ** Auxiliary functions from Lua API 4 | ** See Copyright Notice in lua.h 5 | */ 6 | 7 | #ifndef lapi_h 8 | #define lapi_h 9 | 10 | 11 | #include "lobject.h" 12 | 13 | 14 | LUAI_FUNC void luaA_pushobject (lua_State *L, const TValue *o); 15 | 16 | #endif 17 | -------------------------------------------------------------------------------- /lua/src/lauxlib.h: -------------------------------------------------------------------------------- 1 | /* 2 | ** $Id: lauxlib.h,v 1.88.1.1 2007/12/27 13:02:25 roberto Exp $ 3 | ** Auxiliary functions for building Lua libraries 4 | ** See Copyright Notice in lua.h 5 | */ 6 | 7 | 8 | #ifndef lauxlib_h 9 | #define lauxlib_h 10 | 11 | 12 | #include 13 | #include 14 | 15 | #include "lua.h" 16 | 17 | 18 | #if defined(LUA_COMPAT_GETN) 19 | LUALIB_API int (luaL_getn) (lua_State *L, int t); 20 | LUALIB_API void (luaL_setn) (lua_State *L, int t, int n); 21 | #else 22 | #define luaL_getn(L,i) ((int)lua_objlen(L, i)) 23 | #define luaL_setn(L,i,j) ((void)0) /* no op! */ 24 | #endif 25 | 26 | #if defined(LUA_COMPAT_OPENLIB) 27 | #define luaI_openlib luaL_openlib 28 | #endif 29 | 30 | 31 | /* extra error code for `luaL_load' */ 32 | #define LUA_ERRFILE (LUA_ERRERR+1) 33 | 34 | 35 | typedef struct luaL_Reg { 36 | const char *name; 37 | lua_CFunction func; 38 | } luaL_Reg; 39 | 40 | 41 | 42 | LUALIB_API void (luaI_openlib) (lua_State *L, const char *libname, 43 | const luaL_Reg *l, int nup); 44 | LUALIB_API void (luaL_register) (lua_State *L, const char *libname, 45 | const luaL_Reg *l); 46 | LUALIB_API int (luaL_getmetafield) (lua_State *L, int obj, const char *e); 47 | LUALIB_API int (luaL_callmeta) (lua_State *L, int obj, const char *e); 48 | LUALIB_API int (luaL_typerror) (lua_State *L, int narg, const char *tname); 49 | LUALIB_API int (luaL_argerror) (lua_State *L, int numarg, const char *extramsg); 50 | LUALIB_API const char *(luaL_checklstring) (lua_State *L, int numArg, 51 | size_t *l); 52 | LUALIB_API const char *(luaL_optlstring) (lua_State *L, int numArg, 53 | const char *def, size_t *l); 54 | LUALIB_API lua_Number (luaL_checknumber) (lua_State *L, int numArg); 55 | LUALIB_API lua_Number (luaL_optnumber) (lua_State *L, int nArg, lua_Number def); 56 | 57 | LUALIB_API lua_Integer (luaL_checkinteger) (lua_State *L, int numArg); 58 | LUALIB_API lua_Integer (luaL_optinteger) (lua_State *L, int nArg, 59 | lua_Integer def); 60 | 61 | LUALIB_API void (luaL_checkstack) (lua_State *L, int sz, const char *msg); 62 | LUALIB_API void (luaL_checktype) (lua_State *L, int narg, int t); 63 | LUALIB_API void (luaL_checkany) (lua_State *L, int narg); 64 | 65 | LUALIB_API int (luaL_newmetatable) (lua_State *L, const char *tname); 66 | LUALIB_API void *(luaL_checkudata) (lua_State *L, int ud, const char *tname); 67 | 68 | LUALIB_API void (luaL_where) (lua_State *L, int lvl); 69 | LUALIB_API int (luaL_error) (lua_State *L, const char *fmt, ...); 70 | 71 | LUALIB_API int (luaL_checkoption) (lua_State *L, int narg, const char *def, 72 | const char *const lst[]); 73 | 74 | LUALIB_API int (luaL_ref) (lua_State *L, int t); 75 | LUALIB_API void (luaL_unref) (lua_State *L, int t, int ref); 76 | 77 | LUALIB_API int (luaL_loadfile) (lua_State *L, const char *filename); 78 | LUALIB_API int (luaL_loadbuffer) (lua_State *L, const char *buff, size_t sz, 79 | const char *name); 80 | LUALIB_API int (luaL_loadstring) (lua_State *L, const char *s); 81 | 82 | LUALIB_API lua_State *(luaL_newstate) (void); 83 | 84 | 85 | LUALIB_API const char *(luaL_gsub) (lua_State *L, const char *s, const char *p, 86 | const char *r); 87 | 88 | LUALIB_API const char *(luaL_findtable) (lua_State *L, int idx, 89 | const char *fname, int szhint); 90 | 91 | 92 | 93 | 94 | /* 95 | ** =============================================================== 96 | ** some useful macros 97 | ** =============================================================== 98 | */ 99 | 100 | #define luaL_argcheck(L, cond,numarg,extramsg) \ 101 | ((void)((cond) || luaL_argerror(L, (numarg), (extramsg)))) 102 | #define luaL_checkstring(L,n) (luaL_checklstring(L, (n), NULL)) 103 | #define luaL_optstring(L,n,d) (luaL_optlstring(L, (n), (d), NULL)) 104 | #define luaL_checkint(L,n) ((int)luaL_checkinteger(L, (n))) 105 | #define luaL_optint(L,n,d) ((int)luaL_optinteger(L, (n), (d))) 106 | #define luaL_checklong(L,n) ((long)luaL_checkinteger(L, (n))) 107 | #define luaL_optlong(L,n,d) ((long)luaL_optinteger(L, (n), (d))) 108 | 109 | #define luaL_typename(L,i) lua_typename(L, lua_type(L,(i))) 110 | 111 | #define luaL_dofile(L, fn) \ 112 | (luaL_loadfile(L, fn) || lua_pcall(L, 0, LUA_MULTRET, 0)) 113 | 114 | #define luaL_dostring(L, s) \ 115 | (luaL_loadstring(L, s) || lua_pcall(L, 0, LUA_MULTRET, 0)) 116 | 117 | #define luaL_getmetatable(L,n) (lua_getfield(L, LUA_REGISTRYINDEX, (n))) 118 | 119 | #define luaL_opt(L,f,n,d) (lua_isnoneornil(L,(n)) ? (d) : f(L,(n))) 120 | 121 | /* 122 | ** {====================================================== 123 | ** Generic Buffer manipulation 124 | ** ======================================================= 125 | */ 126 | 127 | 128 | 129 | typedef struct luaL_Buffer { 130 | char *p; /* current position in buffer */ 131 | int lvl; /* number of strings in the stack (level) */ 132 | lua_State *L; 133 | char buffer[LUAL_BUFFERSIZE]; 134 | } luaL_Buffer; 135 | 136 | #define luaL_addchar(B,c) \ 137 | ((void)((B)->p < ((B)->buffer+LUAL_BUFFERSIZE) || luaL_prepbuffer(B)), \ 138 | (*(B)->p++ = (char)(c))) 139 | 140 | /* compatibility only */ 141 | #define luaL_putchar(B,c) luaL_addchar(B,c) 142 | 143 | #define luaL_addsize(B,n) ((B)->p += (n)) 144 | 145 | LUALIB_API void (luaL_buffinit) (lua_State *L, luaL_Buffer *B); 146 | LUALIB_API char *(luaL_prepbuffer) (luaL_Buffer *B); 147 | LUALIB_API void (luaL_addlstring) (luaL_Buffer *B, const char *s, size_t l); 148 | LUALIB_API void (luaL_addstring) (luaL_Buffer *B, const char *s); 149 | LUALIB_API void (luaL_addvalue) (luaL_Buffer *B); 150 | LUALIB_API void (luaL_pushresult) (luaL_Buffer *B); 151 | 152 | 153 | /* }====================================================== */ 154 | 155 | 156 | /* compatibility with ref system */ 157 | 158 | /* pre-defined references */ 159 | #define LUA_NOREF (-2) 160 | #define LUA_REFNIL (-1) 161 | 162 | #define lua_ref(L,lock) ((lock) ? luaL_ref(L, LUA_REGISTRYINDEX) : \ 163 | (lua_pushstring(L, "unlocked references are obsolete"), lua_error(L), 0)) 164 | 165 | #define lua_unref(L,ref) luaL_unref(L, LUA_REGISTRYINDEX, (ref)) 166 | 167 | #define lua_getref(L,ref) lua_rawgeti(L, LUA_REGISTRYINDEX, (ref)) 168 | 169 | 170 | #define luaL_reg luaL_Reg 171 | 172 | #endif 173 | 174 | 175 | -------------------------------------------------------------------------------- /lua/src/lcode.h: -------------------------------------------------------------------------------- 1 | /* 2 | ** $Id: lcode.h,v 1.48.1.1 2007/12/27 13:02:25 roberto Exp $ 3 | ** Code generator for Lua 4 | ** See Copyright Notice in lua.h 5 | */ 6 | 7 | #ifndef lcode_h 8 | #define lcode_h 9 | 10 | #include "llex.h" 11 | #include "lobject.h" 12 | #include "lopcodes.h" 13 | #include "lparser.h" 14 | 15 | 16 | /* 17 | ** Marks the end of a patch list. It is an invalid value both as an absolute 18 | ** address, and as a list link (would link an element to itself). 19 | */ 20 | #define NO_JUMP (-1) 21 | 22 | 23 | /* 24 | ** grep "ORDER OPR" if you change these enums 25 | */ 26 | typedef enum BinOpr { 27 | OPR_ADD, OPR_SUB, OPR_MUL, OPR_DIV, OPR_MOD, OPR_POW, 28 | OPR_CONCAT, 29 | OPR_NE, OPR_EQ, 30 | OPR_LT, OPR_LE, OPR_GT, OPR_GE, 31 | OPR_AND, OPR_OR, 32 | OPR_NOBINOPR 33 | } BinOpr; 34 | 35 | 36 | typedef enum UnOpr { OPR_MINUS, OPR_NOT, OPR_LEN, OPR_NOUNOPR } UnOpr; 37 | 38 | 39 | #define getcode(fs,e) ((fs)->f->code[(e)->u.s.info]) 40 | 41 | #define luaK_codeAsBx(fs,o,A,sBx) luaK_codeABx(fs,o,A,(sBx)+MAXARG_sBx) 42 | 43 | #define luaK_setmultret(fs,e) luaK_setreturns(fs, e, LUA_MULTRET) 44 | 45 | LUAI_FUNC int luaK_codeABx (FuncState *fs, OpCode o, int A, unsigned int Bx); 46 | LUAI_FUNC int luaK_codeABC (FuncState *fs, OpCode o, int A, int B, int C); 47 | LUAI_FUNC void luaK_fixline (FuncState *fs, int line); 48 | LUAI_FUNC void luaK_nil (FuncState *fs, int from, int n); 49 | LUAI_FUNC void luaK_reserveregs (FuncState *fs, int n); 50 | LUAI_FUNC void luaK_checkstack (FuncState *fs, int n); 51 | LUAI_FUNC int luaK_stringK (FuncState *fs, TString *s); 52 | LUAI_FUNC int luaK_numberK (FuncState *fs, lua_Number r); 53 | LUAI_FUNC void luaK_dischargevars (FuncState *fs, expdesc *e); 54 | LUAI_FUNC int luaK_exp2anyreg (FuncState *fs, expdesc *e); 55 | LUAI_FUNC void luaK_exp2nextreg (FuncState *fs, expdesc *e); 56 | LUAI_FUNC void luaK_exp2val (FuncState *fs, expdesc *e); 57 | LUAI_FUNC int luaK_exp2RK (FuncState *fs, expdesc *e); 58 | LUAI_FUNC void luaK_self (FuncState *fs, expdesc *e, expdesc *key); 59 | LUAI_FUNC void luaK_indexed (FuncState *fs, expdesc *t, expdesc *k); 60 | LUAI_FUNC void luaK_goiftrue (FuncState *fs, expdesc *e); 61 | LUAI_FUNC void luaK_storevar (FuncState *fs, expdesc *var, expdesc *e); 62 | LUAI_FUNC void luaK_setreturns (FuncState *fs, expdesc *e, int nresults); 63 | LUAI_FUNC void luaK_setoneret (FuncState *fs, expdesc *e); 64 | LUAI_FUNC int luaK_jump (FuncState *fs); 65 | LUAI_FUNC void luaK_ret (FuncState *fs, int first, int nret); 66 | LUAI_FUNC void luaK_patchlist (FuncState *fs, int list, int target); 67 | LUAI_FUNC void luaK_patchtohere (FuncState *fs, int list); 68 | LUAI_FUNC void luaK_concat (FuncState *fs, int *l1, int l2); 69 | LUAI_FUNC int luaK_getlabel (FuncState *fs); 70 | LUAI_FUNC void luaK_prefix (FuncState *fs, UnOpr op, expdesc *v); 71 | LUAI_FUNC void luaK_infix (FuncState *fs, BinOpr op, expdesc *v); 72 | LUAI_FUNC void luaK_posfix (FuncState *fs, BinOpr op, expdesc *v1, expdesc *v2); 73 | LUAI_FUNC void luaK_setlist (FuncState *fs, int base, int nelems, int tostore); 74 | 75 | 76 | #endif 77 | -------------------------------------------------------------------------------- /lua/src/ldebug.h: -------------------------------------------------------------------------------- 1 | /* 2 | ** $Id: ldebug.h,v 2.3.1.1 2007/12/27 13:02:25 roberto Exp $ 3 | ** Auxiliary functions from Debug Interface module 4 | ** See Copyright Notice in lua.h 5 | */ 6 | 7 | #ifndef ldebug_h 8 | #define ldebug_h 9 | 10 | 11 | #include "lstate.h" 12 | 13 | 14 | #define pcRel(pc, p) (cast(int, (pc) - (p)->code) - 1) 15 | 16 | #define getline(f,pc) (((f)->lineinfo) ? (f)->lineinfo[pc] : 0) 17 | 18 | #define resethookcount(L) (L->hookcount = L->basehookcount) 19 | 20 | 21 | LUAI_FUNC void luaG_typeerror (lua_State *L, const TValue *o, 22 | const char *opname); 23 | LUAI_FUNC void luaG_concaterror (lua_State *L, StkId p1, StkId p2); 24 | LUAI_FUNC void luaG_aritherror (lua_State *L, const TValue *p1, 25 | const TValue *p2); 26 | LUAI_FUNC int luaG_ordererror (lua_State *L, const TValue *p1, 27 | const TValue *p2); 28 | LUAI_FUNC void luaG_runerror (lua_State *L, const char *fmt, ...); 29 | LUAI_FUNC void luaG_errormsg (lua_State *L); 30 | LUAI_FUNC int luaG_checkcode (const Proto *pt); 31 | LUAI_FUNC int luaG_checkopenop (Instruction i); 32 | 33 | #endif 34 | -------------------------------------------------------------------------------- /lua/src/ldo.h: -------------------------------------------------------------------------------- 1 | /* 2 | ** $Id: ldo.h,v 2.7.1.1 2007/12/27 13:02:25 roberto Exp $ 3 | ** Stack and Call structure of Lua 4 | ** See Copyright Notice in lua.h 5 | */ 6 | 7 | #ifndef ldo_h 8 | #define ldo_h 9 | 10 | 11 | #include "lobject.h" 12 | #include "lstate.h" 13 | #include "lzio.h" 14 | 15 | 16 | #define luaD_checkstack(L,n) \ 17 | if ((char *)L->stack_last - (char *)L->top <= (n)*(int)sizeof(TValue)) \ 18 | luaD_growstack(L, n); \ 19 | else condhardstacktests(luaD_reallocstack(L, L->stacksize - EXTRA_STACK - 1)); 20 | 21 | 22 | #define incr_top(L) {luaD_checkstack(L,1); L->top++;} 23 | 24 | #define savestack(L,p) ((char *)(p) - (char *)L->stack) 25 | #define restorestack(L,n) ((TValue *)((char *)L->stack + (n))) 26 | 27 | #define saveci(L,p) ((char *)(p) - (char *)L->base_ci) 28 | #define restoreci(L,n) ((CallInfo *)((char *)L->base_ci + (n))) 29 | 30 | 31 | /* results from luaD_precall */ 32 | #define PCRLUA 0 /* initiated a call to a Lua function */ 33 | #define PCRC 1 /* did a call to a C function */ 34 | #define PCRYIELD 2 /* C funtion yielded */ 35 | 36 | 37 | /* type of protected functions, to be ran by `runprotected' */ 38 | typedef void (*Pfunc) (lua_State *L, void *ud); 39 | 40 | LUAI_FUNC int luaD_protectedparser (lua_State *L, ZIO *z, const char *name); 41 | LUAI_FUNC void luaD_callhook (lua_State *L, int event, int line); 42 | LUAI_FUNC int luaD_precall (lua_State *L, StkId func, int nresults); 43 | LUAI_FUNC void luaD_call (lua_State *L, StkId func, int nResults); 44 | LUAI_FUNC int luaD_pcall (lua_State *L, Pfunc func, void *u, 45 | ptrdiff_t oldtop, ptrdiff_t ef); 46 | LUAI_FUNC int luaD_poscall (lua_State *L, StkId firstResult); 47 | LUAI_FUNC void luaD_reallocCI (lua_State *L, int newsize); 48 | LUAI_FUNC void luaD_reallocstack (lua_State *L, int newsize); 49 | LUAI_FUNC void luaD_growstack (lua_State *L, int n); 50 | 51 | LUAI_FUNC void luaD_throw (lua_State *L, int errcode); 52 | LUAI_FUNC int luaD_rawrunprotected (lua_State *L, Pfunc f, void *ud); 53 | 54 | LUAI_FUNC void luaD_seterrorobj (lua_State *L, int errcode, StkId oldtop); 55 | 56 | #endif 57 | 58 | -------------------------------------------------------------------------------- /lua/src/ldump.c: -------------------------------------------------------------------------------- 1 | /* 2 | ** $Id: ldump.c,v 2.8.1.1 2007/12/27 13:02:25 roberto Exp $ 3 | ** save precompiled Lua chunks 4 | ** See Copyright Notice in lua.h 5 | */ 6 | 7 | #include 8 | 9 | #define ldump_c 10 | #define LUA_CORE 11 | 12 | #include "lua.h" 13 | 14 | #include "lobject.h" 15 | #include "lstate.h" 16 | #include "lundump.h" 17 | 18 | typedef struct { 19 | lua_State* L; 20 | lua_Writer writer; 21 | void* data; 22 | int strip; 23 | int status; 24 | } DumpState; 25 | 26 | #define DumpMem(b,n,size,D) DumpBlock(b,(n)*(size),D) 27 | #define DumpVar(x,D) DumpMem(&x,1,sizeof(x),D) 28 | 29 | static void DumpBlock(const void* b, size_t size, DumpState* D) 30 | { 31 | if (D->status==0) 32 | { 33 | lua_unlock(D->L); 34 | D->status=(*D->writer)(D->L,b,size,D->data); 35 | lua_lock(D->L); 36 | } 37 | } 38 | 39 | static void DumpChar(int y, DumpState* D) 40 | { 41 | char x=(char)y; 42 | DumpVar(x,D); 43 | } 44 | 45 | static void DumpInt(int x, DumpState* D) 46 | { 47 | DumpVar(x,D); 48 | } 49 | 50 | static void DumpNumber(lua_Number x, DumpState* D) 51 | { 52 | DumpVar(x,D); 53 | } 54 | 55 | static void DumpVector(const void* b, int n, size_t size, DumpState* D) 56 | { 57 | DumpInt(n,D); 58 | DumpMem(b,n,size,D); 59 | } 60 | 61 | static void DumpString(const TString* s, DumpState* D) 62 | { 63 | if (s==NULL || getstr(s)==NULL) 64 | { 65 | size_t size=0; 66 | DumpVar(size,D); 67 | } 68 | else 69 | { 70 | size_t size=s->tsv.len+1; /* include trailing '\0' */ 71 | DumpVar(size,D); 72 | DumpBlock(getstr(s),size,D); 73 | } 74 | } 75 | 76 | #define DumpCode(f,D) DumpVector(f->code,f->sizecode,sizeof(Instruction),D) 77 | 78 | static void DumpFunction(const Proto* f, const TString* p, DumpState* D); 79 | 80 | static void DumpConstants(const Proto* f, DumpState* D) 81 | { 82 | int i,n=f->sizek; 83 | DumpInt(n,D); 84 | for (i=0; ik[i]; 87 | DumpChar(ttype(o),D); 88 | switch (ttype(o)) 89 | { 90 | case LUA_TNIL: 91 | break; 92 | case LUA_TBOOLEAN: 93 | DumpChar(bvalue(o),D); 94 | break; 95 | case LUA_TNUMBER: 96 | DumpNumber(nvalue(o),D); 97 | break; 98 | case LUA_TSTRING: 99 | DumpString(rawtsvalue(o),D); 100 | break; 101 | default: 102 | lua_assert(0); /* cannot happen */ 103 | break; 104 | } 105 | } 106 | n=f->sizep; 107 | DumpInt(n,D); 108 | for (i=0; ip[i],f->source,D); 109 | } 110 | 111 | static void DumpDebug(const Proto* f, DumpState* D) 112 | { 113 | int i,n; 114 | n= (D->strip) ? 0 : f->sizelineinfo; 115 | DumpVector(f->lineinfo,n,sizeof(int),D); 116 | n= (D->strip) ? 0 : f->sizelocvars; 117 | DumpInt(n,D); 118 | for (i=0; ilocvars[i].varname,D); 121 | DumpInt(f->locvars[i].startpc,D); 122 | DumpInt(f->locvars[i].endpc,D); 123 | } 124 | n= (D->strip) ? 0 : f->sizeupvalues; 125 | DumpInt(n,D); 126 | for (i=0; iupvalues[i],D); 127 | } 128 | 129 | static void DumpFunction(const Proto* f, const TString* p, DumpState* D) 130 | { 131 | DumpString((f->source==p || D->strip) ? NULL : f->source,D); 132 | DumpInt(f->linedefined,D); 133 | DumpInt(f->lastlinedefined,D); 134 | DumpChar(f->nups,D); 135 | DumpChar(f->numparams,D); 136 | DumpChar(f->is_vararg,D); 137 | DumpChar(f->maxstacksize,D); 138 | DumpCode(f,D); 139 | DumpConstants(f,D); 140 | DumpDebug(f,D); 141 | } 142 | 143 | static void DumpHeader(DumpState* D) 144 | { 145 | char h[LUAC_HEADERSIZE]; 146 | luaU_header(h); 147 | DumpBlock(h,LUAC_HEADERSIZE,D); 148 | } 149 | 150 | /* 151 | ** dump Lua function as precompiled chunk 152 | */ 153 | int luaU_dump (lua_State* L, const Proto* f, lua_Writer w, void* data, int strip) 154 | { 155 | DumpState D; 156 | D.L=L; 157 | D.writer=w; 158 | D.data=data; 159 | D.strip=strip; 160 | D.status=0; 161 | DumpHeader(&D); 162 | DumpFunction(f,NULL,&D); 163 | return D.status; 164 | } 165 | -------------------------------------------------------------------------------- /lua/src/lfunc.c: -------------------------------------------------------------------------------- 1 | /* 2 | ** $Id: lfunc.c,v 2.12.1.2 2007/12/28 14:58:43 roberto Exp $ 3 | ** Auxiliary functions to manipulate prototypes and closures 4 | ** See Copyright Notice in lua.h 5 | */ 6 | 7 | 8 | #include 9 | 10 | #define lfunc_c 11 | #define LUA_CORE 12 | 13 | #include "lua.h" 14 | 15 | #include "lfunc.h" 16 | #include "lgc.h" 17 | #include "lmem.h" 18 | #include "lobject.h" 19 | #include "lstate.h" 20 | 21 | 22 | 23 | Closure *luaF_newCclosure (lua_State *L, int nelems, Table *e) { 24 | Closure *c = cast(Closure *, luaM_malloc(L, sizeCclosure(nelems))); 25 | luaC_link(L, obj2gco(c), LUA_TFUNCTION); 26 | c->c.isC = 1; 27 | c->c.env = e; 28 | c->c.nupvalues = cast_byte(nelems); 29 | return c; 30 | } 31 | 32 | 33 | Closure *luaF_newLclosure (lua_State *L, int nelems, Table *e) { 34 | Closure *c = cast(Closure *, luaM_malloc(L, sizeLclosure(nelems))); 35 | luaC_link(L, obj2gco(c), LUA_TFUNCTION); 36 | c->l.isC = 0; 37 | c->l.env = e; 38 | c->l.nupvalues = cast_byte(nelems); 39 | while (nelems--) c->l.upvals[nelems] = NULL; 40 | return c; 41 | } 42 | 43 | 44 | UpVal *luaF_newupval (lua_State *L) { 45 | UpVal *uv = luaM_new(L, UpVal); 46 | luaC_link(L, obj2gco(uv), LUA_TUPVAL); 47 | uv->v = &uv->u.value; 48 | setnilvalue(uv->v); 49 | return uv; 50 | } 51 | 52 | 53 | UpVal *luaF_findupval (lua_State *L, StkId level) { 54 | global_State *g = G(L); 55 | GCObject **pp = &L->openupval; 56 | UpVal *p; 57 | UpVal *uv; 58 | while (*pp != NULL && (p = ngcotouv(*pp))->v >= level) { 59 | lua_assert(p->v != &p->u.value); 60 | if (p->v == level) { /* found a corresponding upvalue? */ 61 | if (isdead(g, obj2gco(p))) /* is it dead? */ 62 | changewhite(obj2gco(p)); /* ressurect it */ 63 | return p; 64 | } 65 | pp = &p->next; 66 | } 67 | uv = luaM_new(L, UpVal); /* not found: create a new one */ 68 | uv->tt = LUA_TUPVAL; 69 | uv->marked = luaC_white(g); 70 | uv->v = level; /* current value lives in the stack */ 71 | uv->next = *pp; /* chain it in the proper position */ 72 | *pp = obj2gco(uv); 73 | uv->u.l.prev = &g->uvhead; /* double link it in `uvhead' list */ 74 | uv->u.l.next = g->uvhead.u.l.next; 75 | uv->u.l.next->u.l.prev = uv; 76 | g->uvhead.u.l.next = uv; 77 | lua_assert(uv->u.l.next->u.l.prev == uv && uv->u.l.prev->u.l.next == uv); 78 | return uv; 79 | } 80 | 81 | 82 | static void unlinkupval (UpVal *uv) { 83 | lua_assert(uv->u.l.next->u.l.prev == uv && uv->u.l.prev->u.l.next == uv); 84 | uv->u.l.next->u.l.prev = uv->u.l.prev; /* remove from `uvhead' list */ 85 | uv->u.l.prev->u.l.next = uv->u.l.next; 86 | } 87 | 88 | 89 | void luaF_freeupval (lua_State *L, UpVal *uv) { 90 | if (uv->v != &uv->u.value) /* is it open? */ 91 | unlinkupval(uv); /* remove from open list */ 92 | luaM_free(L, uv); /* free upvalue */ 93 | } 94 | 95 | 96 | void luaF_close (lua_State *L, StkId level) { 97 | UpVal *uv; 98 | global_State *g = G(L); 99 | while (L->openupval != NULL && (uv = ngcotouv(L->openupval))->v >= level) { 100 | GCObject *o = obj2gco(uv); 101 | lua_assert(!isblack(o) && uv->v != &uv->u.value); 102 | L->openupval = uv->next; /* remove from `open' list */ 103 | if (isdead(g, o)) 104 | luaF_freeupval(L, uv); /* free upvalue */ 105 | else { 106 | unlinkupval(uv); 107 | setobj(L, &uv->u.value, uv->v); 108 | uv->v = &uv->u.value; /* now current value lives here */ 109 | luaC_linkupval(L, uv); /* link upvalue into `gcroot' list */ 110 | } 111 | } 112 | } 113 | 114 | 115 | Proto *luaF_newproto (lua_State *L) { 116 | Proto *f = luaM_new(L, Proto); 117 | luaC_link(L, obj2gco(f), LUA_TPROTO); 118 | f->k = NULL; 119 | f->sizek = 0; 120 | f->p = NULL; 121 | f->sizep = 0; 122 | f->code = NULL; 123 | f->sizecode = 0; 124 | f->sizelineinfo = 0; 125 | f->sizeupvalues = 0; 126 | f->nups = 0; 127 | f->upvalues = NULL; 128 | f->numparams = 0; 129 | f->is_vararg = 0; 130 | f->maxstacksize = 0; 131 | f->lineinfo = NULL; 132 | f->sizelocvars = 0; 133 | f->locvars = NULL; 134 | f->linedefined = 0; 135 | f->lastlinedefined = 0; 136 | f->source = NULL; 137 | return f; 138 | } 139 | 140 | 141 | void luaF_freeproto (lua_State *L, Proto *f) { 142 | luaM_freearray(L, f->code, f->sizecode, Instruction); 143 | luaM_freearray(L, f->p, f->sizep, Proto *); 144 | luaM_freearray(L, f->k, f->sizek, TValue); 145 | luaM_freearray(L, f->lineinfo, f->sizelineinfo, int); 146 | luaM_freearray(L, f->locvars, f->sizelocvars, struct LocVar); 147 | luaM_freearray(L, f->upvalues, f->sizeupvalues, TString *); 148 | luaM_free(L, f); 149 | } 150 | 151 | 152 | void luaF_freeclosure (lua_State *L, Closure *c) { 153 | int size = (c->c.isC) ? sizeCclosure(c->c.nupvalues) : 154 | sizeLclosure(c->l.nupvalues); 155 | luaM_freemem(L, c, size); 156 | } 157 | 158 | 159 | /* 160 | ** Look for n-th local variable at line `line' in function `func'. 161 | ** Returns NULL if not found. 162 | */ 163 | const char *luaF_getlocalname (const Proto *f, int local_number, int pc) { 164 | int i; 165 | for (i = 0; isizelocvars && f->locvars[i].startpc <= pc; i++) { 166 | if (pc < f->locvars[i].endpc) { /* is variable active? */ 167 | local_number--; 168 | if (local_number == 0) 169 | return getstr(f->locvars[i].varname); 170 | } 171 | } 172 | return NULL; /* not found */ 173 | } 174 | 175 | -------------------------------------------------------------------------------- /lua/src/lfunc.h: -------------------------------------------------------------------------------- 1 | /* 2 | ** $Id: lfunc.h,v 2.4.1.1 2007/12/27 13:02:25 roberto Exp $ 3 | ** Auxiliary functions to manipulate prototypes and closures 4 | ** See Copyright Notice in lua.h 5 | */ 6 | 7 | #ifndef lfunc_h 8 | #define lfunc_h 9 | 10 | 11 | #include "lobject.h" 12 | 13 | 14 | #define sizeCclosure(n) (cast(int, sizeof(CClosure)) + \ 15 | cast(int, sizeof(TValue)*((n)-1))) 16 | 17 | #define sizeLclosure(n) (cast(int, sizeof(LClosure)) + \ 18 | cast(int, sizeof(TValue *)*((n)-1))) 19 | 20 | 21 | LUAI_FUNC Proto *luaF_newproto (lua_State *L); 22 | LUAI_FUNC Closure *luaF_newCclosure (lua_State *L, int nelems, Table *e); 23 | LUAI_FUNC Closure *luaF_newLclosure (lua_State *L, int nelems, Table *e); 24 | LUAI_FUNC UpVal *luaF_newupval (lua_State *L); 25 | LUAI_FUNC UpVal *luaF_findupval (lua_State *L, StkId level); 26 | LUAI_FUNC void luaF_close (lua_State *L, StkId level); 27 | LUAI_FUNC void luaF_freeproto (lua_State *L, Proto *f); 28 | LUAI_FUNC void luaF_freeclosure (lua_State *L, Closure *c); 29 | LUAI_FUNC void luaF_freeupval (lua_State *L, UpVal *uv); 30 | LUAI_FUNC const char *luaF_getlocalname (const Proto *func, int local_number, 31 | int pc); 32 | 33 | 34 | #endif 35 | -------------------------------------------------------------------------------- /lua/src/lgc.h: -------------------------------------------------------------------------------- 1 | /* 2 | ** $Id: lgc.h,v 2.15.1.1 2007/12/27 13:02:25 roberto Exp $ 3 | ** Garbage Collector 4 | ** See Copyright Notice in lua.h 5 | */ 6 | 7 | #ifndef lgc_h 8 | #define lgc_h 9 | 10 | 11 | #include "lobject.h" 12 | 13 | 14 | /* 15 | ** Possible states of the Garbage Collector 16 | */ 17 | #define GCSpause 0 18 | #define GCSpropagate 1 19 | #define GCSsweepstring 2 20 | #define GCSsweep 3 21 | #define GCSfinalize 4 22 | 23 | 24 | /* 25 | ** some userful bit tricks 26 | */ 27 | #define resetbits(x,m) ((x) &= cast(lu_byte, ~(m))) 28 | #define setbits(x,m) ((x) |= (m)) 29 | #define testbits(x,m) ((x) & (m)) 30 | #define bitmask(b) (1<<(b)) 31 | #define bit2mask(b1,b2) (bitmask(b1) | bitmask(b2)) 32 | #define l_setbit(x,b) setbits(x, bitmask(b)) 33 | #define resetbit(x,b) resetbits(x, bitmask(b)) 34 | #define testbit(x,b) testbits(x, bitmask(b)) 35 | #define set2bits(x,b1,b2) setbits(x, (bit2mask(b1, b2))) 36 | #define reset2bits(x,b1,b2) resetbits(x, (bit2mask(b1, b2))) 37 | #define test2bits(x,b1,b2) testbits(x, (bit2mask(b1, b2))) 38 | 39 | 40 | 41 | /* 42 | ** Layout for bit use in `marked' field: 43 | ** bit 0 - object is white (type 0) 44 | ** bit 1 - object is white (type 1) 45 | ** bit 2 - object is black 46 | ** bit 3 - for userdata: has been finalized 47 | ** bit 3 - for tables: has weak keys 48 | ** bit 4 - for tables: has weak values 49 | ** bit 5 - object is fixed (should not be collected) 50 | ** bit 6 - object is "super" fixed (only the main thread) 51 | */ 52 | 53 | 54 | #define WHITE0BIT 0 55 | #define WHITE1BIT 1 56 | #define BLACKBIT 2 57 | #define FINALIZEDBIT 3 58 | #define KEYWEAKBIT 3 59 | #define VALUEWEAKBIT 4 60 | #define FIXEDBIT 5 61 | #define SFIXEDBIT 6 62 | #define WHITEBITS bit2mask(WHITE0BIT, WHITE1BIT) 63 | 64 | 65 | #define iswhite(x) test2bits((x)->gch.marked, WHITE0BIT, WHITE1BIT) 66 | #define isblack(x) testbit((x)->gch.marked, BLACKBIT) 67 | #define isgray(x) (!isblack(x) && !iswhite(x)) 68 | 69 | #define otherwhite(g) (g->currentwhite ^ WHITEBITS) 70 | #define isdead(g,v) ((v)->gch.marked & otherwhite(g) & WHITEBITS) 71 | 72 | #define changewhite(x) ((x)->gch.marked ^= WHITEBITS) 73 | #define gray2black(x) l_setbit((x)->gch.marked, BLACKBIT) 74 | 75 | #define valiswhite(x) (iscollectable(x) && iswhite(gcvalue(x))) 76 | 77 | #define luaC_white(g) cast(lu_byte, (g)->currentwhite & WHITEBITS) 78 | 79 | 80 | #define luaC_checkGC(L) { \ 81 | condhardstacktests(luaD_reallocstack(L, L->stacksize - EXTRA_STACK - 1)); \ 82 | if (G(L)->totalbytes >= G(L)->GCthreshold) \ 83 | luaC_step(L); } 84 | 85 | 86 | #define luaC_barrier(L,p,v) { if (valiswhite(v) && isblack(obj2gco(p))) \ 87 | luaC_barrierf(L,obj2gco(p),gcvalue(v)); } 88 | 89 | #define luaC_barriert(L,t,v) { if (valiswhite(v) && isblack(obj2gco(t))) \ 90 | luaC_barrierback(L,t); } 91 | 92 | #define luaC_objbarrier(L,p,o) \ 93 | { if (iswhite(obj2gco(o)) && isblack(obj2gco(p))) \ 94 | luaC_barrierf(L,obj2gco(p),obj2gco(o)); } 95 | 96 | #define luaC_objbarriert(L,t,o) \ 97 | { if (iswhite(obj2gco(o)) && isblack(obj2gco(t))) luaC_barrierback(L,t); } 98 | 99 | LUAI_FUNC size_t luaC_separateudata (lua_State *L, int all); 100 | LUAI_FUNC void luaC_callGCTM (lua_State *L); 101 | LUAI_FUNC void luaC_freeall (lua_State *L); 102 | LUAI_FUNC void luaC_step (lua_State *L); 103 | LUAI_FUNC void luaC_fullgc (lua_State *L); 104 | LUAI_FUNC void luaC_link (lua_State *L, GCObject *o, lu_byte tt); 105 | LUAI_FUNC void luaC_linkupval (lua_State *L, UpVal *uv); 106 | LUAI_FUNC void luaC_barrierf (lua_State *L, GCObject *o, GCObject *v); 107 | LUAI_FUNC void luaC_barrierback (lua_State *L, Table *t); 108 | 109 | 110 | #endif 111 | -------------------------------------------------------------------------------- /lua/src/linit.c: -------------------------------------------------------------------------------- 1 | /* 2 | ** $Id: linit.c,v 1.14.1.1 2007/12/27 13:02:25 roberto Exp $ 3 | ** Initialization of libraries for lua.c 4 | ** See Copyright Notice in lua.h 5 | */ 6 | 7 | 8 | #define linit_c 9 | #define LUA_LIB 10 | 11 | #include "lua.h" 12 | 13 | #include "lualib.h" 14 | #include "lauxlib.h" 15 | 16 | 17 | static const luaL_Reg lualibs[] = { 18 | {"", luaopen_base}, 19 | {LUA_LOADLIBNAME, luaopen_package}, 20 | {LUA_TABLIBNAME, luaopen_table}, 21 | {LUA_IOLIBNAME, luaopen_io}, 22 | {LUA_OSLIBNAME, luaopen_os}, 23 | {LUA_STRLIBNAME, luaopen_string}, 24 | {LUA_MATHLIBNAME, luaopen_math}, 25 | {LUA_DBLIBNAME, luaopen_debug}, 26 | {NULL, NULL} 27 | }; 28 | 29 | 30 | LUALIB_API void luaL_openlibs (lua_State *L) { 31 | const luaL_Reg *lib = lualibs; 32 | for (; lib->func; lib++) { 33 | lua_pushcfunction(L, lib->func); 34 | lua_pushstring(L, lib->name); 35 | lua_call(L, 1, 0); 36 | } 37 | } 38 | 39 | -------------------------------------------------------------------------------- /lua/src/llex.h: -------------------------------------------------------------------------------- 1 | /* 2 | ** $Id: llex.h,v 1.58.1.1 2007/12/27 13:02:25 roberto Exp $ 3 | ** Lexical Analyzer 4 | ** See Copyright Notice in lua.h 5 | */ 6 | 7 | #ifndef llex_h 8 | #define llex_h 9 | 10 | #include "lobject.h" 11 | #include "lzio.h" 12 | 13 | 14 | #define FIRST_RESERVED 257 15 | 16 | /* maximum length of a reserved word */ 17 | #define TOKEN_LEN (sizeof("function")/sizeof(char)) 18 | 19 | 20 | /* 21 | * WARNING: if you change the order of this enumeration, 22 | * grep "ORDER RESERVED" 23 | */ 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_CONCAT, TK_DOTS, TK_EQ, TK_GE, TK_LE, TK_NE, TK_NUMBER, 32 | TK_NAME, TK_STRING, TK_EOS 33 | }; 34 | 35 | /* number of reserved words */ 36 | #define NUM_RESERVED (cast(int, TK_WHILE-FIRST_RESERVED+1)) 37 | 38 | 39 | /* array with token `names' */ 40 | LUAI_DATA const char *const luaX_tokens []; 41 | 42 | 43 | typedef union { 44 | lua_Number r; 45 | TString *ts; 46 | } SemInfo; /* semantics information */ 47 | 48 | 49 | typedef struct Token { 50 | int token; 51 | SemInfo seminfo; 52 | } Token; 53 | 54 | 55 | typedef struct LexState { 56 | int current; /* current character (charint) */ 57 | int linenumber; /* input line counter */ 58 | int lastline; /* line of last token `consumed' */ 59 | Token t; /* current token */ 60 | Token lookahead; /* look ahead token */ 61 | struct FuncState *fs; /* `FuncState' is private to the parser */ 62 | struct lua_State *L; 63 | ZIO *z; /* input stream */ 64 | Mbuffer *buff; /* buffer for tokens */ 65 | TString *source; /* current source name */ 66 | char decpoint; /* locale decimal point */ 67 | } LexState; 68 | 69 | 70 | LUAI_FUNC void luaX_init (lua_State *L); 71 | LUAI_FUNC void luaX_setinput (lua_State *L, LexState *ls, ZIO *z, 72 | TString *source); 73 | LUAI_FUNC TString *luaX_newstring (LexState *ls, const char *str, size_t l); 74 | LUAI_FUNC void luaX_next (LexState *ls); 75 | LUAI_FUNC void luaX_lookahead (LexState *ls); 76 | LUAI_FUNC void luaX_lexerror (LexState *ls, const char *msg, int token); 77 | LUAI_FUNC void luaX_syntaxerror (LexState *ls, const char *s); 78 | LUAI_FUNC const char *luaX_token2str (LexState *ls, int token); 79 | 80 | 81 | #endif 82 | -------------------------------------------------------------------------------- /lua/src/llimits.h: -------------------------------------------------------------------------------- 1 | /* 2 | ** $Id: llimits.h,v 1.69.1.1 2007/12/27 13:02:25 roberto Exp $ 3 | ** Limits, basic types, and some other `installation-dependent' definitions 4 | ** See Copyright Notice in lua.h 5 | */ 6 | 7 | #ifndef llimits_h 8 | #define llimits_h 9 | 10 | 11 | #include 12 | #include 13 | 14 | 15 | #include "lua.h" 16 | 17 | 18 | typedef LUAI_UINT32 lu_int32; 19 | 20 | typedef LUAI_UMEM lu_mem; 21 | 22 | typedef LUAI_MEM l_mem; 23 | 24 | 25 | 26 | /* chars used as small naturals (so that `char' is reserved for characters) */ 27 | typedef unsigned char lu_byte; 28 | 29 | 30 | #define MAX_SIZET ((size_t)(~(size_t)0)-2) 31 | 32 | #define MAX_LUMEM ((lu_mem)(~(lu_mem)0)-2) 33 | 34 | 35 | #define MAX_INT (INT_MAX-2) /* maximum value of an int (-2 for safety) */ 36 | 37 | /* 38 | ** conversion of pointer to integer 39 | ** this is for hashing only; there is no problem if the integer 40 | ** cannot hold the whole pointer value 41 | */ 42 | #define IntPoint(p) ((unsigned int)(lu_mem)(p)) 43 | 44 | 45 | 46 | /* type to ensure maximum alignment */ 47 | typedef LUAI_USER_ALIGNMENT_T L_Umaxalign; 48 | 49 | 50 | /* result of a `usual argument conversion' over lua_Number */ 51 | typedef LUAI_UACNUMBER l_uacNumber; 52 | 53 | 54 | /* internal assertions for in-house debugging */ 55 | #ifdef lua_assert 56 | 57 | #define check_exp(c,e) (lua_assert(c), (e)) 58 | #define api_check(l,e) lua_assert(e) 59 | 60 | #else 61 | 62 | #define lua_assert(c) ((void)0) 63 | #define check_exp(c,e) (e) 64 | #define api_check luai_apicheck 65 | 66 | #endif 67 | 68 | 69 | #ifndef UNUSED 70 | #define UNUSED(x) ((void)(x)) /* to avoid warnings */ 71 | #endif 72 | 73 | 74 | #ifndef cast 75 | #define cast(t, exp) ((t)(exp)) 76 | #endif 77 | 78 | #define cast_byte(i) cast(lu_byte, (i)) 79 | #define cast_num(i) cast(lua_Number, (i)) 80 | #define cast_int(i) cast(int, (i)) 81 | 82 | 83 | 84 | /* 85 | ** type for virtual-machine instructions 86 | ** must be an unsigned with (at least) 4 bytes (see details in lopcodes.h) 87 | */ 88 | typedef lu_int32 Instruction; 89 | 90 | 91 | 92 | /* maximum stack for a Lua function */ 93 | #define MAXSTACK 250 94 | 95 | 96 | 97 | /* minimum size for the string table (must be power of 2) */ 98 | #ifndef MINSTRTABSIZE 99 | #define MINSTRTABSIZE 32 100 | #endif 101 | 102 | 103 | /* minimum size for string buffer */ 104 | #ifndef LUA_MINBUFFER 105 | #define LUA_MINBUFFER 32 106 | #endif 107 | 108 | 109 | #ifndef lua_lock 110 | #define lua_lock(L) ((void) 0) 111 | #define lua_unlock(L) ((void) 0) 112 | #endif 113 | 114 | #ifndef luai_threadyield 115 | #define luai_threadyield(L) {lua_unlock(L); lua_lock(L);} 116 | #endif 117 | 118 | 119 | /* 120 | ** macro to control inclusion of some hard tests on stack reallocation 121 | */ 122 | #ifndef HARDSTACKTESTS 123 | #define condhardstacktests(x) ((void)0) 124 | #else 125 | #define condhardstacktests(x) x 126 | #endif 127 | 128 | #endif 129 | -------------------------------------------------------------------------------- /lua/src/lmathlib.c: -------------------------------------------------------------------------------- 1 | /* 2 | ** $Id: lmathlib.c,v 1.67.1.1 2007/12/27 13:02:25 roberto Exp $ 3 | ** Standard mathematical library 4 | ** See Copyright Notice in lua.h 5 | */ 6 | 7 | 8 | #include 9 | #include 10 | 11 | #define lmathlib_c 12 | #define LUA_LIB 13 | 14 | #include "lua.h" 15 | 16 | #include "lauxlib.h" 17 | #include "lualib.h" 18 | 19 | 20 | #undef PI 21 | #define PI (3.14159265358979323846) 22 | #define RADIANS_PER_DEGREE (PI/180.0) 23 | 24 | 25 | 26 | static int math_abs (lua_State *L) { 27 | lua_pushnumber(L, fabs(luaL_checknumber(L, 1))); 28 | return 1; 29 | } 30 | 31 | static int math_sin (lua_State *L) { 32 | lua_pushnumber(L, sin(luaL_checknumber(L, 1))); 33 | return 1; 34 | } 35 | 36 | static int math_sinh (lua_State *L) { 37 | lua_pushnumber(L, sinh(luaL_checknumber(L, 1))); 38 | return 1; 39 | } 40 | 41 | static int math_cos (lua_State *L) { 42 | lua_pushnumber(L, cos(luaL_checknumber(L, 1))); 43 | return 1; 44 | } 45 | 46 | static int math_cosh (lua_State *L) { 47 | lua_pushnumber(L, cosh(luaL_checknumber(L, 1))); 48 | return 1; 49 | } 50 | 51 | static int math_tan (lua_State *L) { 52 | lua_pushnumber(L, tan(luaL_checknumber(L, 1))); 53 | return 1; 54 | } 55 | 56 | static int math_tanh (lua_State *L) { 57 | lua_pushnumber(L, tanh(luaL_checknumber(L, 1))); 58 | return 1; 59 | } 60 | 61 | static int math_asin (lua_State *L) { 62 | lua_pushnumber(L, asin(luaL_checknumber(L, 1))); 63 | return 1; 64 | } 65 | 66 | static int math_acos (lua_State *L) { 67 | lua_pushnumber(L, acos(luaL_checknumber(L, 1))); 68 | return 1; 69 | } 70 | 71 | static int math_atan (lua_State *L) { 72 | lua_pushnumber(L, atan(luaL_checknumber(L, 1))); 73 | return 1; 74 | } 75 | 76 | static int math_atan2 (lua_State *L) { 77 | lua_pushnumber(L, atan2(luaL_checknumber(L, 1), luaL_checknumber(L, 2))); 78 | return 1; 79 | } 80 | 81 | static int math_ceil (lua_State *L) { 82 | lua_pushnumber(L, ceil(luaL_checknumber(L, 1))); 83 | return 1; 84 | } 85 | 86 | static int math_floor (lua_State *L) { 87 | lua_pushnumber(L, floor(luaL_checknumber(L, 1))); 88 | return 1; 89 | } 90 | 91 | static int math_fmod (lua_State *L) { 92 | lua_pushnumber(L, fmod(luaL_checknumber(L, 1), luaL_checknumber(L, 2))); 93 | return 1; 94 | } 95 | 96 | static int math_modf (lua_State *L) { 97 | double ip; 98 | double fp = modf(luaL_checknumber(L, 1), &ip); 99 | lua_pushnumber(L, ip); 100 | lua_pushnumber(L, fp); 101 | return 2; 102 | } 103 | 104 | static int math_sqrt (lua_State *L) { 105 | lua_pushnumber(L, sqrt(luaL_checknumber(L, 1))); 106 | return 1; 107 | } 108 | 109 | static int math_pow (lua_State *L) { 110 | lua_pushnumber(L, pow(luaL_checknumber(L, 1), luaL_checknumber(L, 2))); 111 | return 1; 112 | } 113 | 114 | static int math_log (lua_State *L) { 115 | lua_pushnumber(L, log(luaL_checknumber(L, 1))); 116 | return 1; 117 | } 118 | 119 | static int math_log10 (lua_State *L) { 120 | lua_pushnumber(L, log10(luaL_checknumber(L, 1))); 121 | return 1; 122 | } 123 | 124 | static int math_exp (lua_State *L) { 125 | lua_pushnumber(L, exp(luaL_checknumber(L, 1))); 126 | return 1; 127 | } 128 | 129 | static int math_deg (lua_State *L) { 130 | lua_pushnumber(L, luaL_checknumber(L, 1)/RADIANS_PER_DEGREE); 131 | return 1; 132 | } 133 | 134 | static int math_rad (lua_State *L) { 135 | lua_pushnumber(L, luaL_checknumber(L, 1)*RADIANS_PER_DEGREE); 136 | return 1; 137 | } 138 | 139 | static int math_frexp (lua_State *L) { 140 | int e; 141 | lua_pushnumber(L, frexp(luaL_checknumber(L, 1), &e)); 142 | lua_pushinteger(L, e); 143 | return 2; 144 | } 145 | 146 | static int math_ldexp (lua_State *L) { 147 | lua_pushnumber(L, ldexp(luaL_checknumber(L, 1), luaL_checkint(L, 2))); 148 | return 1; 149 | } 150 | 151 | 152 | 153 | static int math_min (lua_State *L) { 154 | int n = lua_gettop(L); /* number of arguments */ 155 | lua_Number dmin = luaL_checknumber(L, 1); 156 | int i; 157 | for (i=2; i<=n; i++) { 158 | lua_Number d = luaL_checknumber(L, i); 159 | if (d < dmin) 160 | dmin = d; 161 | } 162 | lua_pushnumber(L, dmin); 163 | return 1; 164 | } 165 | 166 | 167 | static int math_max (lua_State *L) { 168 | int n = lua_gettop(L); /* number of arguments */ 169 | lua_Number dmax = luaL_checknumber(L, 1); 170 | int i; 171 | for (i=2; i<=n; i++) { 172 | lua_Number d = luaL_checknumber(L, i); 173 | if (d > dmax) 174 | dmax = d; 175 | } 176 | lua_pushnumber(L, dmax); 177 | return 1; 178 | } 179 | 180 | 181 | static int math_random (lua_State *L) { 182 | /* the `%' avoids the (rare) case of r==1, and is needed also because on 183 | some systems (SunOS!) `rand()' may return a value larger than RAND_MAX */ 184 | lua_Number r = (lua_Number)(rand()%RAND_MAX) / (lua_Number)RAND_MAX; 185 | switch (lua_gettop(L)) { /* check number of arguments */ 186 | case 0: { /* no arguments */ 187 | lua_pushnumber(L, r); /* Number between 0 and 1 */ 188 | break; 189 | } 190 | case 1: { /* only upper limit */ 191 | int u = luaL_checkint(L, 1); 192 | luaL_argcheck(L, 1<=u, 1, "interval is empty"); 193 | lua_pushnumber(L, floor(r*u)+1); /* int between 1 and `u' */ 194 | break; 195 | } 196 | case 2: { /* lower and upper limits */ 197 | int l = luaL_checkint(L, 1); 198 | int u = luaL_checkint(L, 2); 199 | luaL_argcheck(L, l<=u, 2, "interval is empty"); 200 | lua_pushnumber(L, floor(r*(u-l+1))+l); /* int between `l' and `u' */ 201 | break; 202 | } 203 | default: return luaL_error(L, "wrong number of arguments"); 204 | } 205 | return 1; 206 | } 207 | 208 | 209 | static int math_randomseed (lua_State *L) { 210 | srand(luaL_checkint(L, 1)); 211 | return 0; 212 | } 213 | 214 | 215 | static const luaL_Reg mathlib[] = { 216 | {"abs", math_abs}, 217 | {"acos", math_acos}, 218 | {"asin", math_asin}, 219 | {"atan2", math_atan2}, 220 | {"atan", math_atan}, 221 | {"ceil", math_ceil}, 222 | {"cosh", math_cosh}, 223 | {"cos", math_cos}, 224 | {"deg", math_deg}, 225 | {"exp", math_exp}, 226 | {"floor", math_floor}, 227 | {"fmod", math_fmod}, 228 | {"frexp", math_frexp}, 229 | {"ldexp", math_ldexp}, 230 | {"log10", math_log10}, 231 | {"log", math_log}, 232 | {"max", math_max}, 233 | {"min", math_min}, 234 | {"modf", math_modf}, 235 | {"pow", math_pow}, 236 | {"rad", math_rad}, 237 | {"random", math_random}, 238 | {"randomseed", math_randomseed}, 239 | {"sinh", math_sinh}, 240 | {"sin", math_sin}, 241 | {"sqrt", math_sqrt}, 242 | {"tanh", math_tanh}, 243 | {"tan", math_tan}, 244 | {NULL, NULL} 245 | }; 246 | 247 | 248 | /* 249 | ** Open math library 250 | */ 251 | LUALIB_API int luaopen_math (lua_State *L) { 252 | luaL_register(L, LUA_MATHLIBNAME, mathlib); 253 | lua_pushnumber(L, PI); 254 | lua_setfield(L, -2, "pi"); 255 | lua_pushnumber(L, HUGE_VAL); 256 | lua_setfield(L, -2, "huge"); 257 | #if defined(LUA_COMPAT_MOD) 258 | lua_getfield(L, -1, "fmod"); 259 | lua_setfield(L, -2, "mod"); 260 | #endif 261 | return 1; 262 | } 263 | 264 | -------------------------------------------------------------------------------- /lua/src/lmem.c: -------------------------------------------------------------------------------- 1 | /* 2 | ** $Id: lmem.c,v 1.70.1.1 2007/12/27 13:02:25 roberto Exp $ 3 | ** Interface to Memory Manager 4 | ** See Copyright Notice in lua.h 5 | */ 6 | 7 | 8 | #include 9 | 10 | #define lmem_c 11 | #define LUA_CORE 12 | 13 | #include "lua.h" 14 | 15 | #include "ldebug.h" 16 | #include "ldo.h" 17 | #include "lmem.h" 18 | #include "lobject.h" 19 | #include "lstate.h" 20 | 21 | 22 | 23 | /* 24 | ** About the realloc function: 25 | ** void * frealloc (void *ud, void *ptr, size_t osize, size_t nsize); 26 | ** (`osize' is the old size, `nsize' is the new size) 27 | ** 28 | ** Lua ensures that (ptr == NULL) iff (osize == 0). 29 | ** 30 | ** * frealloc(ud, NULL, 0, x) creates a new block of size `x' 31 | ** 32 | ** * frealloc(ud, p, x, 0) frees the block `p' 33 | ** (in this specific case, frealloc must return NULL). 34 | ** particularly, frealloc(ud, NULL, 0, 0) does nothing 35 | ** (which is equivalent to free(NULL) in ANSI C) 36 | ** 37 | ** frealloc returns NULL if it cannot create or reallocate the area 38 | ** (any reallocation to an equal or smaller size cannot fail!) 39 | */ 40 | 41 | 42 | 43 | #define MINSIZEARRAY 4 44 | 45 | 46 | void *luaM_growaux_ (lua_State *L, void *block, int *size, size_t size_elems, 47 | int limit, const char *errormsg) { 48 | void *newblock; 49 | int newsize; 50 | if (*size >= limit/2) { /* cannot double it? */ 51 | if (*size >= limit) /* cannot grow even a little? */ 52 | luaG_runerror(L, errormsg); 53 | newsize = limit; /* still have at least one free place */ 54 | } 55 | else { 56 | newsize = (*size)*2; 57 | if (newsize < MINSIZEARRAY) 58 | newsize = MINSIZEARRAY; /* minimum size */ 59 | } 60 | newblock = luaM_reallocv(L, block, *size, newsize, size_elems); 61 | *size = newsize; /* update only when everything else is OK */ 62 | return newblock; 63 | } 64 | 65 | 66 | void *luaM_toobig (lua_State *L) { 67 | luaG_runerror(L, "memory allocation error: block too big"); 68 | return NULL; /* to avoid warnings */ 69 | } 70 | 71 | 72 | 73 | /* 74 | ** generic allocation routine. 75 | */ 76 | void *luaM_realloc_ (lua_State *L, void *block, size_t osize, size_t nsize) { 77 | global_State *g = G(L); 78 | lua_assert((osize == 0) == (block == NULL)); 79 | block = (*g->frealloc)(g->ud, block, osize, nsize); 80 | if (block == NULL && nsize > 0) 81 | luaD_throw(L, LUA_ERRMEM); 82 | lua_assert((nsize == 0) == (block == NULL)); 83 | g->totalbytes = (g->totalbytes - osize) + nsize; 84 | return block; 85 | } 86 | 87 | -------------------------------------------------------------------------------- /lua/src/lmem.h: -------------------------------------------------------------------------------- 1 | /* 2 | ** $Id: lmem.h,v 1.31.1.1 2007/12/27 13:02:25 roberto Exp $ 3 | ** Interface to Memory Manager 4 | ** See Copyright Notice in lua.h 5 | */ 6 | 7 | #ifndef lmem_h 8 | #define lmem_h 9 | 10 | 11 | #include 12 | 13 | #include "llimits.h" 14 | #include "lua.h" 15 | 16 | #define MEMERRMSG "not enough memory" 17 | 18 | 19 | #define luaM_reallocv(L,b,on,n,e) \ 20 | ((cast(size_t, (n)+1) <= MAX_SIZET/(e)) ? /* +1 to avoid warnings */ \ 21 | luaM_realloc_(L, (b), (on)*(e), (n)*(e)) : \ 22 | luaM_toobig(L)) 23 | 24 | #define luaM_freemem(L, b, s) luaM_realloc_(L, (b), (s), 0) 25 | #define luaM_free(L, b) luaM_realloc_(L, (b), sizeof(*(b)), 0) 26 | #define luaM_freearray(L, b, n, t) luaM_reallocv(L, (b), n, 0, sizeof(t)) 27 | 28 | #define luaM_malloc(L,t) luaM_realloc_(L, NULL, 0, (t)) 29 | #define luaM_new(L,t) cast(t *, luaM_malloc(L, sizeof(t))) 30 | #define luaM_newvector(L,n,t) \ 31 | cast(t *, luaM_reallocv(L, NULL, 0, n, sizeof(t))) 32 | 33 | #define luaM_growvector(L,v,nelems,size,t,limit,e) \ 34 | if ((nelems)+1 > (size)) \ 35 | ((v)=cast(t *, luaM_growaux_(L,v,&(size),sizeof(t),limit,e))) 36 | 37 | #define luaM_reallocvector(L, v,oldn,n,t) \ 38 | ((v)=cast(t *, luaM_reallocv(L, v, oldn, n, sizeof(t)))) 39 | 40 | 41 | LUAI_FUNC void *luaM_realloc_ (lua_State *L, void *block, size_t oldsize, 42 | size_t size); 43 | LUAI_FUNC void *luaM_toobig (lua_State *L); 44 | LUAI_FUNC void *luaM_growaux_ (lua_State *L, void *block, int *size, 45 | size_t size_elem, int limit, 46 | const char *errormsg); 47 | 48 | #endif 49 | 50 | -------------------------------------------------------------------------------- /lua/src/lobject.c: -------------------------------------------------------------------------------- 1 | /* 2 | ** $Id: lobject.c,v 2.22.1.1 2007/12/27 13:02:25 roberto Exp $ 3 | ** Some generic functions over Lua objects 4 | ** See Copyright Notice in lua.h 5 | */ 6 | 7 | #include 8 | #include 9 | #include 10 | #include 11 | #include 12 | 13 | #define lobject_c 14 | #define LUA_CORE 15 | 16 | #include "lua.h" 17 | 18 | #include "ldo.h" 19 | #include "lmem.h" 20 | #include "lobject.h" 21 | #include "lstate.h" 22 | #include "lstring.h" 23 | #include "lvm.h" 24 | 25 | 26 | 27 | const TValue luaO_nilobject_ = {{NULL}, LUA_TNIL}; 28 | 29 | 30 | /* 31 | ** converts an integer to a "floating point byte", represented as 32 | ** (eeeeexxx), where the real value is (1xxx) * 2^(eeeee - 1) if 33 | ** eeeee != 0 and (xxx) otherwise. 34 | */ 35 | int luaO_int2fb (unsigned int x) { 36 | int e = 0; /* expoent */ 37 | while (x >= 16) { 38 | x = (x+1) >> 1; 39 | e++; 40 | } 41 | if (x < 8) return x; 42 | else return ((e+1) << 3) | (cast_int(x) - 8); 43 | } 44 | 45 | 46 | /* converts back */ 47 | int luaO_fb2int (int x) { 48 | int e = (x >> 3) & 31; 49 | if (e == 0) return x; 50 | else return ((x & 7)+8) << (e - 1); 51 | } 52 | 53 | 54 | int luaO_log2 (unsigned int x) { 55 | static const lu_byte log_2[256] = { 56 | 0,1,2,2,3,3,3,3,4,4,4,4,4,4,4,4,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5, 57 | 6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6, 58 | 7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7, 59 | 7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7, 60 | 8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8, 61 | 8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8, 62 | 8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8, 63 | 8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8 64 | }; 65 | int l = -1; 66 | while (x >= 256) { l += 8; x >>= 8; } 67 | return l + log_2[x]; 68 | 69 | } 70 | 71 | 72 | int luaO_rawequalObj (const TValue *t1, const TValue *t2) { 73 | if (ttype(t1) != ttype(t2)) return 0; 74 | else switch (ttype(t1)) { 75 | case LUA_TNIL: 76 | return 1; 77 | case LUA_TNUMBER: 78 | return luai_numeq(nvalue(t1), nvalue(t2)); 79 | case LUA_TBOOLEAN: 80 | return bvalue(t1) == bvalue(t2); /* boolean true must be 1 !! */ 81 | case LUA_TLIGHTUSERDATA: 82 | return pvalue(t1) == pvalue(t2); 83 | default: 84 | lua_assert(iscollectable(t1)); 85 | return gcvalue(t1) == gcvalue(t2); 86 | } 87 | } 88 | 89 | 90 | int luaO_str2d (const char *s, lua_Number *result) { 91 | char *endptr; 92 | *result = lua_str2number(s, &endptr); 93 | if (endptr == s) return 0; /* conversion failed */ 94 | if (*endptr == 'x' || *endptr == 'X') /* maybe an hexadecimal constant? */ 95 | *result = cast_num(strtoul(s, &endptr, 16)); 96 | if (*endptr == '\0') return 1; /* most common case */ 97 | while (isspace(cast(unsigned char, *endptr))) endptr++; 98 | if (*endptr != '\0') return 0; /* invalid trailing characters? */ 99 | return 1; 100 | } 101 | 102 | 103 | 104 | static void pushstr (lua_State *L, const char *str) { 105 | setsvalue2s(L, L->top, luaS_new(L, str)); 106 | incr_top(L); 107 | } 108 | 109 | 110 | /* this function handles only `%d', `%c', %f, %p, and `%s' formats */ 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, L->top, luaS_newlstr(L, fmt, e-fmt)); 118 | incr_top(L); 119 | switch (*(e+1)) { 120 | case 's': { 121 | const char *s = va_arg(argp, char *); 122 | if (s == NULL) s = "(null)"; 123 | pushstr(L, s); 124 | break; 125 | } 126 | case 'c': { 127 | char buff[2]; 128 | buff[0] = cast(char, va_arg(argp, int)); 129 | buff[1] = '\0'; 130 | pushstr(L, buff); 131 | break; 132 | } 133 | case 'd': { 134 | setnvalue(L->top, cast_num(va_arg(argp, int))); 135 | incr_top(L); 136 | break; 137 | } 138 | case 'f': { 139 | setnvalue(L->top, cast_num(va_arg(argp, l_uacNumber))); 140 | incr_top(L); 141 | break; 142 | } 143 | case 'p': { 144 | char buff[4*sizeof(void *) + 8]; /* should be enough space for a `%p' */ 145 | sprintf(buff, "%p", va_arg(argp, void *)); 146 | pushstr(L, buff); 147 | break; 148 | } 149 | case '%': { 150 | pushstr(L, "%"); 151 | break; 152 | } 153 | default: { 154 | char buff[3]; 155 | buff[0] = '%'; 156 | buff[1] = *(e+1); 157 | buff[2] = '\0'; 158 | pushstr(L, buff); 159 | break; 160 | } 161 | } 162 | n += 2; 163 | fmt = e+2; 164 | } 165 | pushstr(L, fmt); 166 | luaV_concat(L, n+1, cast_int(L->top - L->base) - 1); 167 | L->top -= n; 168 | return svalue(L->top - 1); 169 | } 170 | 171 | 172 | const char *luaO_pushfstring (lua_State *L, const char *fmt, ...) { 173 | const char *msg; 174 | va_list argp; 175 | va_start(argp, fmt); 176 | msg = luaO_pushvfstring(L, fmt, argp); 177 | va_end(argp); 178 | return msg; 179 | } 180 | 181 | 182 | void luaO_chunkid (char *out, const char *source, size_t bufflen) { 183 | if (*source == '=') { 184 | strncpy(out, source+1, bufflen); /* remove first char */ 185 | out[bufflen-1] = '\0'; /* ensures null termination */ 186 | } 187 | else { /* out = "source", or "...source" */ 188 | if (*source == '@') { 189 | size_t l; 190 | source++; /* skip the `@' */ 191 | bufflen -= sizeof(" '...' "); 192 | l = strlen(source); 193 | strcpy(out, ""); 194 | if (l > bufflen) { 195 | source += (l-bufflen); /* get last part of file name */ 196 | strcat(out, "..."); 197 | } 198 | strcat(out, source); 199 | } 200 | else { /* out = [string "string"] */ 201 | size_t len = strcspn(source, "\n\r"); /* stop at first newline */ 202 | bufflen -= sizeof(" [string \"...\"] "); 203 | if (len > bufflen) len = bufflen; 204 | strcpy(out, "[string \""); 205 | if (source[len] != '\0') { /* must truncate? */ 206 | strncat(out, source, len); 207 | strcat(out, "..."); 208 | } 209 | else 210 | strcat(out, source); 211 | strcat(out, "\"]"); 212 | } 213 | } 214 | } 215 | -------------------------------------------------------------------------------- /lua/src/lopcodes.c: -------------------------------------------------------------------------------- 1 | /* 2 | ** $Id: lopcodes.c,v 1.37.1.1 2007/12/27 13:02:25 roberto Exp $ 3 | ** See Copyright Notice in lua.h 4 | */ 5 | 6 | 7 | #define lopcodes_c 8 | #define LUA_CORE 9 | 10 | 11 | #include "lopcodes.h" 12 | 13 | 14 | /* ORDER OP */ 15 | 16 | const char *const luaP_opnames[NUM_OPCODES+1] = { 17 | "MOVE", 18 | "LOADK", 19 | "LOADBOOL", 20 | "LOADNIL", 21 | "GETUPVAL", 22 | "GETGLOBAL", 23 | "GETTABLE", 24 | "SETGLOBAL", 25 | "SETUPVAL", 26 | "SETTABLE", 27 | "NEWTABLE", 28 | "SELF", 29 | "ADD", 30 | "SUB", 31 | "MUL", 32 | "DIV", 33 | "MOD", 34 | "POW", 35 | "UNM", 36 | "NOT", 37 | "LEN", 38 | "CONCAT", 39 | "JMP", 40 | "EQ", 41 | "LT", 42 | "LE", 43 | "TEST", 44 | "TESTSET", 45 | "CALL", 46 | "TAILCALL", 47 | "RETURN", 48 | "FORLOOP", 49 | "FORPREP", 50 | "TFORLOOP", 51 | "SETLIST", 52 | "CLOSE", 53 | "CLOSURE", 54 | "VARARG", 55 | NULL 56 | }; 57 | 58 | 59 | #define opmode(t,a,b,c,m) (((t)<<7) | ((a)<<6) | ((b)<<4) | ((c)<<2) | (m)) 60 | 61 | const lu_byte luaP_opmodes[NUM_OPCODES] = { 62 | /* T A B C mode opcode */ 63 | opmode(0, 1, OpArgR, OpArgN, iABC) /* OP_MOVE */ 64 | ,opmode(0, 1, OpArgK, OpArgN, iABx) /* OP_LOADK */ 65 | ,opmode(0, 1, OpArgU, OpArgU, iABC) /* OP_LOADBOOL */ 66 | ,opmode(0, 1, OpArgR, OpArgN, iABC) /* OP_LOADNIL */ 67 | ,opmode(0, 1, OpArgU, OpArgN, iABC) /* OP_GETUPVAL */ 68 | ,opmode(0, 1, OpArgK, OpArgN, iABx) /* OP_GETGLOBAL */ 69 | ,opmode(0, 1, OpArgR, OpArgK, iABC) /* OP_GETTABLE */ 70 | ,opmode(0, 0, OpArgK, OpArgN, iABx) /* OP_SETGLOBAL */ 71 | ,opmode(0, 0, OpArgU, OpArgN, iABC) /* OP_SETUPVAL */ 72 | ,opmode(0, 0, OpArgK, OpArgK, iABC) /* OP_SETTABLE */ 73 | ,opmode(0, 1, OpArgU, OpArgU, iABC) /* OP_NEWTABLE */ 74 | ,opmode(0, 1, OpArgR, OpArgK, iABC) /* OP_SELF */ 75 | ,opmode(0, 1, OpArgK, OpArgK, iABC) /* OP_ADD */ 76 | ,opmode(0, 1, OpArgK, OpArgK, iABC) /* OP_SUB */ 77 | ,opmode(0, 1, OpArgK, OpArgK, iABC) /* OP_MUL */ 78 | ,opmode(0, 1, OpArgK, OpArgK, iABC) /* OP_DIV */ 79 | ,opmode(0, 1, OpArgK, OpArgK, iABC) /* OP_MOD */ 80 | ,opmode(0, 1, OpArgK, OpArgK, iABC) /* OP_POW */ 81 | ,opmode(0, 1, OpArgR, OpArgN, iABC) /* OP_UNM */ 82 | ,opmode(0, 1, OpArgR, OpArgN, iABC) /* OP_NOT */ 83 | ,opmode(0, 1, OpArgR, OpArgN, iABC) /* OP_LEN */ 84 | ,opmode(0, 1, OpArgR, OpArgR, iABC) /* OP_CONCAT */ 85 | ,opmode(0, 0, OpArgR, OpArgN, iAsBx) /* OP_JMP */ 86 | ,opmode(1, 0, OpArgK, OpArgK, iABC) /* OP_EQ */ 87 | ,opmode(1, 0, OpArgK, OpArgK, iABC) /* OP_LT */ 88 | ,opmode(1, 0, OpArgK, OpArgK, iABC) /* OP_LE */ 89 | ,opmode(1, 1, OpArgR, OpArgU, iABC) /* OP_TEST */ 90 | ,opmode(1, 1, OpArgR, OpArgU, iABC) /* OP_TESTSET */ 91 | ,opmode(0, 1, OpArgU, OpArgU, iABC) /* OP_CALL */ 92 | ,opmode(0, 1, OpArgU, OpArgU, iABC) /* OP_TAILCALL */ 93 | ,opmode(0, 0, OpArgU, OpArgN, iABC) /* OP_RETURN */ 94 | ,opmode(0, 1, OpArgR, OpArgN, iAsBx) /* OP_FORLOOP */ 95 | ,opmode(0, 1, OpArgR, OpArgN, iAsBx) /* OP_FORPREP */ 96 | ,opmode(1, 0, OpArgN, OpArgU, iABC) /* OP_TFORLOOP */ 97 | ,opmode(0, 0, OpArgU, OpArgU, iABC) /* OP_SETLIST */ 98 | ,opmode(0, 0, OpArgN, OpArgN, iABC) /* OP_CLOSE */ 99 | ,opmode(0, 1, OpArgU, OpArgN, iABx) /* OP_CLOSURE */ 100 | ,opmode(0, 1, OpArgU, OpArgN, iABC) /* OP_VARARG */ 101 | }; 102 | 103 | -------------------------------------------------------------------------------- /lua/src/loslib.c: -------------------------------------------------------------------------------- 1 | /* 2 | ** $Id: loslib.c,v 1.19.1.3 2008/01/18 16:38:18 roberto Exp $ 3 | ** Standard Operating System library 4 | ** See Copyright Notice in lua.h 5 | */ 6 | 7 | 8 | #include 9 | #include 10 | #include 11 | #include 12 | #include 13 | 14 | #define loslib_c 15 | #define LUA_LIB 16 | 17 | #include "lua.h" 18 | 19 | #include "lauxlib.h" 20 | #include "lualib.h" 21 | 22 | 23 | static int os_pushresult (lua_State *L, int i, const char *filename) { 24 | int en = errno; /* calls to Lua API may change this value */ 25 | if (i) { 26 | lua_pushboolean(L, 1); 27 | return 1; 28 | } 29 | else { 30 | lua_pushnil(L); 31 | lua_pushfstring(L, "%s: %s", filename, strerror(en)); 32 | lua_pushinteger(L, en); 33 | return 3; 34 | } 35 | } 36 | 37 | 38 | static int os_execute (lua_State *L) { 39 | lua_pushinteger(L, system(luaL_optstring(L, 1, NULL))); 40 | return 1; 41 | } 42 | 43 | 44 | static int os_remove (lua_State *L) { 45 | const char *filename = luaL_checkstring(L, 1); 46 | return os_pushresult(L, remove(filename) == 0, filename); 47 | } 48 | 49 | 50 | static int os_rename (lua_State *L) { 51 | const char *fromname = luaL_checkstring(L, 1); 52 | const char *toname = luaL_checkstring(L, 2); 53 | return os_pushresult(L, rename(fromname, toname) == 0, fromname); 54 | } 55 | 56 | 57 | static int os_tmpname (lua_State *L) { 58 | char buff[LUA_TMPNAMBUFSIZE]; 59 | int err; 60 | lua_tmpnam(buff, err); 61 | if (err) 62 | return luaL_error(L, "unable to generate a unique filename"); 63 | lua_pushstring(L, buff); 64 | return 1; 65 | } 66 | 67 | 68 | static int os_getenv (lua_State *L) { 69 | lua_pushstring(L, getenv(luaL_checkstring(L, 1))); /* if NULL push nil */ 70 | return 1; 71 | } 72 | 73 | 74 | static int os_clock (lua_State *L) { 75 | lua_pushnumber(L, ((lua_Number)clock())/(lua_Number)CLOCKS_PER_SEC); 76 | return 1; 77 | } 78 | 79 | 80 | /* 81 | ** {====================================================== 82 | ** Time/Date operations 83 | ** { year=%Y, month=%m, day=%d, hour=%H, min=%M, sec=%S, 84 | ** wday=%w+1, yday=%j, isdst=? } 85 | ** ======================================================= 86 | */ 87 | 88 | static void setfield (lua_State *L, const char *key, int value) { 89 | lua_pushinteger(L, value); 90 | lua_setfield(L, -2, key); 91 | } 92 | 93 | static void setboolfield (lua_State *L, const char *key, int value) { 94 | if (value < 0) /* undefined? */ 95 | return; /* does not set field */ 96 | lua_pushboolean(L, value); 97 | lua_setfield(L, -2, key); 98 | } 99 | 100 | static int getboolfield (lua_State *L, const char *key) { 101 | int res; 102 | lua_getfield(L, -1, key); 103 | res = lua_isnil(L, -1) ? -1 : lua_toboolean(L, -1); 104 | lua_pop(L, 1); 105 | return res; 106 | } 107 | 108 | 109 | static int getfield (lua_State *L, const char *key, int d) { 110 | int res; 111 | lua_getfield(L, -1, key); 112 | if (lua_isnumber(L, -1)) 113 | res = (int)lua_tointeger(L, -1); 114 | else { 115 | if (d < 0) 116 | return luaL_error(L, "field " LUA_QS " missing in date table", key); 117 | res = d; 118 | } 119 | lua_pop(L, 1); 120 | return res; 121 | } 122 | 123 | 124 | static int os_date (lua_State *L) { 125 | const char *s = luaL_optstring(L, 1, "%c"); 126 | time_t t = luaL_opt(L, (time_t)luaL_checknumber, 2, time(NULL)); 127 | struct tm *stm; 128 | if (*s == '!') { /* UTC? */ 129 | stm = gmtime(&t); 130 | s++; /* skip `!' */ 131 | } 132 | else 133 | stm = localtime(&t); 134 | if (stm == NULL) /* invalid date? */ 135 | lua_pushnil(L); 136 | else if (strcmp(s, "*t") == 0) { 137 | lua_createtable(L, 0, 9); /* 9 = number of fields */ 138 | setfield(L, "sec", stm->tm_sec); 139 | setfield(L, "min", stm->tm_min); 140 | setfield(L, "hour", stm->tm_hour); 141 | setfield(L, "day", stm->tm_mday); 142 | setfield(L, "month", stm->tm_mon+1); 143 | setfield(L, "year", stm->tm_year+1900); 144 | setfield(L, "wday", stm->tm_wday+1); 145 | setfield(L, "yday", stm->tm_yday+1); 146 | setboolfield(L, "isdst", stm->tm_isdst); 147 | } 148 | else { 149 | char cc[3]; 150 | luaL_Buffer b; 151 | cc[0] = '%'; cc[2] = '\0'; 152 | luaL_buffinit(L, &b); 153 | for (; *s; s++) { 154 | if (*s != '%' || *(s + 1) == '\0') /* no conversion specifier? */ 155 | luaL_addchar(&b, *s); 156 | else { 157 | size_t reslen; 158 | char buff[200]; /* should be big enough for any conversion result */ 159 | cc[1] = *(++s); 160 | reslen = strftime(buff, sizeof(buff), cc, stm); 161 | luaL_addlstring(&b, buff, reslen); 162 | } 163 | } 164 | luaL_pushresult(&b); 165 | } 166 | return 1; 167 | } 168 | 169 | 170 | static int os_time (lua_State *L) { 171 | time_t t; 172 | if (lua_isnoneornil(L, 1)) /* called without args? */ 173 | t = time(NULL); /* get current time */ 174 | else { 175 | struct tm ts; 176 | luaL_checktype(L, 1, LUA_TTABLE); 177 | lua_settop(L, 1); /* make sure table is at the top */ 178 | ts.tm_sec = getfield(L, "sec", 0); 179 | ts.tm_min = getfield(L, "min", 0); 180 | ts.tm_hour = getfield(L, "hour", 12); 181 | ts.tm_mday = getfield(L, "day", -1); 182 | ts.tm_mon = getfield(L, "month", -1) - 1; 183 | ts.tm_year = getfield(L, "year", -1) - 1900; 184 | ts.tm_isdst = getboolfield(L, "isdst"); 185 | t = mktime(&ts); 186 | } 187 | if (t == (time_t)(-1)) 188 | lua_pushnil(L); 189 | else 190 | lua_pushnumber(L, (lua_Number)t); 191 | return 1; 192 | } 193 | 194 | 195 | static int os_difftime (lua_State *L) { 196 | lua_pushnumber(L, difftime((time_t)(luaL_checknumber(L, 1)), 197 | (time_t)(luaL_optnumber(L, 2, 0)))); 198 | return 1; 199 | } 200 | 201 | /* }====================================================== */ 202 | 203 | 204 | static int os_setlocale (lua_State *L) { 205 | static const int cat[] = {LC_ALL, LC_COLLATE, LC_CTYPE, LC_MONETARY, 206 | LC_NUMERIC, LC_TIME}; 207 | static const char *const catnames[] = {"all", "collate", "ctype", "monetary", 208 | "numeric", "time", NULL}; 209 | const char *l = luaL_optstring(L, 1, NULL); 210 | int op = luaL_checkoption(L, 2, "all", catnames); 211 | lua_pushstring(L, setlocale(cat[op], l)); 212 | return 1; 213 | } 214 | 215 | 216 | static int os_exit (lua_State *L) { 217 | exit(luaL_optint(L, 1, EXIT_SUCCESS)); 218 | } 219 | 220 | static const luaL_Reg syslib[] = { 221 | {"clock", os_clock}, 222 | {"date", os_date}, 223 | {"difftime", os_difftime}, 224 | {"execute", os_execute}, 225 | {"exit", os_exit}, 226 | {"getenv", os_getenv}, 227 | {"remove", os_remove}, 228 | {"rename", os_rename}, 229 | {"setlocale", os_setlocale}, 230 | {"time", os_time}, 231 | {"tmpname", os_tmpname}, 232 | {NULL, NULL} 233 | }; 234 | 235 | /* }====================================================== */ 236 | 237 | 238 | 239 | LUALIB_API int luaopen_os (lua_State *L) { 240 | luaL_register(L, LUA_OSLIBNAME, syslib); 241 | return 1; 242 | } 243 | 244 | -------------------------------------------------------------------------------- /lua/src/lparser.h: -------------------------------------------------------------------------------- 1 | /* 2 | ** $Id: lparser.h,v 1.57.1.1 2007/12/27 13:02:25 roberto Exp $ 3 | ** Lua Parser 4 | ** See Copyright Notice in lua.h 5 | */ 6 | 7 | #ifndef lparser_h 8 | #define lparser_h 9 | 10 | #include "llimits.h" 11 | #include "lobject.h" 12 | #include "lzio.h" 13 | 14 | 15 | /* 16 | ** Expression descriptor 17 | */ 18 | 19 | typedef enum { 20 | VVOID, /* no value */ 21 | VNIL, 22 | VTRUE, 23 | VFALSE, 24 | VK, /* info = index of constant in `k' */ 25 | VKNUM, /* nval = numerical value */ 26 | VLOCAL, /* info = local register */ 27 | VUPVAL, /* info = index of upvalue in `upvalues' */ 28 | VGLOBAL, /* info = index of table; aux = index of global name in `k' */ 29 | VINDEXED, /* info = table register; aux = index register (or `k') */ 30 | VJMP, /* info = instruction pc */ 31 | VRELOCABLE, /* info = instruction pc */ 32 | VNONRELOC, /* info = result register */ 33 | VCALL, /* info = instruction pc */ 34 | VVARARG /* info = instruction pc */ 35 | } expkind; 36 | 37 | typedef struct expdesc { 38 | expkind k; 39 | union { 40 | struct { int info, aux; } s; 41 | lua_Number nval; 42 | } u; 43 | int t; /* patch list of `exit when true' */ 44 | int f; /* patch list of `exit when false' */ 45 | } expdesc; 46 | 47 | 48 | typedef struct upvaldesc { 49 | lu_byte k; 50 | lu_byte info; 51 | } upvaldesc; 52 | 53 | 54 | struct BlockCnt; /* defined in lparser.c */ 55 | 56 | 57 | /* state needed to generate code for a given function */ 58 | typedef struct FuncState { 59 | Proto *f; /* current function header */ 60 | Table *h; /* table to find (and reuse) elements in `k' */ 61 | struct FuncState *prev; /* enclosing function */ 62 | struct LexState *ls; /* lexical state */ 63 | struct lua_State *L; /* copy of the Lua state */ 64 | struct BlockCnt *bl; /* chain of current blocks */ 65 | int pc; /* next position to code (equivalent to `ncode') */ 66 | int lasttarget; /* `pc' of last `jump target' */ 67 | int jpc; /* list of pending jumps to `pc' */ 68 | int freereg; /* first free register */ 69 | int nk; /* number of elements in `k' */ 70 | int np; /* number of elements in `p' */ 71 | short nlocvars; /* number of elements in `locvars' */ 72 | lu_byte nactvar; /* number of active local variables */ 73 | upvaldesc upvalues[LUAI_MAXUPVALUES]; /* upvalues */ 74 | unsigned short actvar[LUAI_MAXVARS]; /* declared-variable stack */ 75 | } FuncState; 76 | 77 | 78 | LUAI_FUNC Proto *luaY_parser (lua_State *L, ZIO *z, Mbuffer *buff, 79 | const char *name); 80 | 81 | 82 | #endif 83 | -------------------------------------------------------------------------------- /lua/src/lstate.c: -------------------------------------------------------------------------------- 1 | /* 2 | ** $Id: lstate.c,v 2.36.1.2 2008/01/03 15:20:39 roberto Exp $ 3 | ** Global State 4 | ** See Copyright Notice in lua.h 5 | */ 6 | 7 | 8 | #include 9 | 10 | #define lstate_c 11 | #define LUA_CORE 12 | 13 | #include "lua.h" 14 | 15 | #include "ldebug.h" 16 | #include "ldo.h" 17 | #include "lfunc.h" 18 | #include "lgc.h" 19 | #include "llex.h" 20 | #include "lmem.h" 21 | #include "lstate.h" 22 | #include "lstring.h" 23 | #include "ltable.h" 24 | #include "ltm.h" 25 | 26 | 27 | #define state_size(x) (sizeof(x) + LUAI_EXTRASPACE) 28 | #define fromstate(l) (cast(lu_byte *, (l)) - LUAI_EXTRASPACE) 29 | #define tostate(l) (cast(lua_State *, cast(lu_byte *, l) + LUAI_EXTRASPACE)) 30 | 31 | 32 | /* 33 | ** Main thread combines a thread state and the global state 34 | */ 35 | typedef struct LG { 36 | lua_State l; 37 | global_State g; 38 | } LG; 39 | 40 | 41 | 42 | static void stack_init (lua_State *L1, lua_State *L) { 43 | /* initialize CallInfo array */ 44 | L1->base_ci = luaM_newvector(L, BASIC_CI_SIZE, CallInfo); 45 | L1->ci = L1->base_ci; 46 | L1->size_ci = BASIC_CI_SIZE; 47 | L1->end_ci = L1->base_ci + L1->size_ci - 1; 48 | /* initialize stack array */ 49 | L1->stack = luaM_newvector(L, BASIC_STACK_SIZE + EXTRA_STACK, TValue); 50 | L1->stacksize = BASIC_STACK_SIZE + EXTRA_STACK; 51 | L1->top = L1->stack; 52 | L1->stack_last = L1->stack+(L1->stacksize - EXTRA_STACK)-1; 53 | /* initialize first ci */ 54 | L1->ci->func = L1->top; 55 | setnilvalue(L1->top++); /* `function' entry for this `ci' */ 56 | L1->base = L1->ci->base = L1->top; 57 | L1->ci->top = L1->top + LUA_MINSTACK; 58 | } 59 | 60 | 61 | static void freestack (lua_State *L, lua_State *L1) { 62 | luaM_freearray(L, L1->base_ci, L1->size_ci, CallInfo); 63 | luaM_freearray(L, L1->stack, L1->stacksize, TValue); 64 | } 65 | 66 | 67 | /* 68 | ** open parts that may cause memory-allocation errors 69 | */ 70 | static void f_luaopen (lua_State *L, void *ud) { 71 | global_State *g = G(L); 72 | UNUSED(ud); 73 | stack_init(L, L); /* init stack */ 74 | sethvalue(L, gt(L), luaH_new(L, 0, 2)); /* table of globals */ 75 | sethvalue(L, registry(L), luaH_new(L, 0, 2)); /* registry */ 76 | luaS_resize(L, MINSTRTABSIZE); /* initial size of string table */ 77 | luaT_init(L); 78 | luaX_init(L); 79 | luaS_fix(luaS_newliteral(L, MEMERRMSG)); 80 | g->GCthreshold = 4*g->totalbytes; 81 | } 82 | 83 | 84 | static void preinit_state (lua_State *L, global_State *g) { 85 | G(L) = g; 86 | L->stack = NULL; 87 | L->stacksize = 0; 88 | L->errorJmp = NULL; 89 | L->hook = NULL; 90 | L->hookmask = 0; 91 | L->basehookcount = 0; 92 | L->allowhook = 1; 93 | resethookcount(L); 94 | L->openupval = NULL; 95 | L->size_ci = 0; 96 | L->nCcalls = L->baseCcalls = 0; 97 | L->status = 0; 98 | L->base_ci = L->ci = NULL; 99 | L->savedpc = NULL; 100 | L->errfunc = 0; 101 | setnilvalue(gt(L)); 102 | } 103 | 104 | 105 | static void close_state (lua_State *L) { 106 | global_State *g = G(L); 107 | luaF_close(L, L->stack); /* close all upvalues for this thread */ 108 | luaC_freeall(L); /* collect all objects */ 109 | lua_assert(g->rootgc == obj2gco(L)); 110 | lua_assert(g->strt.nuse == 0); 111 | luaM_freearray(L, G(L)->strt.hash, G(L)->strt.size, TString *); 112 | luaZ_freebuffer(L, &g->buff); 113 | freestack(L, L); 114 | lua_assert(g->totalbytes == sizeof(LG)); 115 | (*g->frealloc)(g->ud, fromstate(L), state_size(LG), 0); 116 | } 117 | 118 | 119 | lua_State *luaE_newthread (lua_State *L) { 120 | lua_State *L1 = tostate(luaM_malloc(L, state_size(lua_State))); 121 | luaC_link(L, obj2gco(L1), LUA_TTHREAD); 122 | preinit_state(L1, G(L)); 123 | stack_init(L1, L); /* init stack */ 124 | setobj2n(L, gt(L1), gt(L)); /* share table of globals */ 125 | L1->hookmask = L->hookmask; 126 | L1->basehookcount = L->basehookcount; 127 | L1->hook = L->hook; 128 | resethookcount(L1); 129 | lua_assert(iswhite(obj2gco(L1))); 130 | return L1; 131 | } 132 | 133 | 134 | void luaE_freethread (lua_State *L, lua_State *L1) { 135 | luaF_close(L1, L1->stack); /* close all upvalues for this thread */ 136 | lua_assert(L1->openupval == NULL); 137 | luai_userstatefree(L1); 138 | freestack(L, L1); 139 | luaM_freemem(L, fromstate(L1), state_size(lua_State)); 140 | } 141 | 142 | 143 | LUA_API lua_State *lua_newstate (lua_Alloc f, void *ud) { 144 | int i; 145 | lua_State *L; 146 | global_State *g; 147 | void *l = (*f)(ud, NULL, 0, state_size(LG)); 148 | if (l == NULL) return NULL; 149 | L = tostate(l); 150 | g = &((LG *)L)->g; 151 | L->next = NULL; 152 | L->tt = LUA_TTHREAD; 153 | g->currentwhite = bit2mask(WHITE0BIT, FIXEDBIT); 154 | L->marked = luaC_white(g); 155 | set2bits(L->marked, FIXEDBIT, SFIXEDBIT); 156 | preinit_state(L, g); 157 | g->frealloc = f; 158 | g->ud = ud; 159 | g->mainthread = L; 160 | g->uvhead.u.l.prev = &g->uvhead; 161 | g->uvhead.u.l.next = &g->uvhead; 162 | g->GCthreshold = 0; /* mark it as unfinished state */ 163 | g->strt.size = 0; 164 | g->strt.nuse = 0; 165 | g->strt.hash = NULL; 166 | setnilvalue(registry(L)); 167 | luaZ_initbuffer(L, &g->buff); 168 | g->panic = NULL; 169 | g->gcstate = GCSpause; 170 | g->rootgc = obj2gco(L); 171 | g->sweepstrgc = 0; 172 | g->sweepgc = &g->rootgc; 173 | g->gray = NULL; 174 | g->grayagain = NULL; 175 | g->weak = NULL; 176 | g->tmudata = NULL; 177 | g->totalbytes = sizeof(LG); 178 | g->gcpause = LUAI_GCPAUSE; 179 | g->gcstepmul = LUAI_GCMUL; 180 | g->gcdept = 0; 181 | for (i=0; imt[i] = NULL; 182 | if (luaD_rawrunprotected(L, f_luaopen, NULL) != 0) { 183 | /* memory allocation error: free partial state */ 184 | close_state(L); 185 | L = NULL; 186 | } 187 | else 188 | luai_userstateopen(L); 189 | return L; 190 | } 191 | 192 | 193 | static void callallgcTM (lua_State *L, void *ud) { 194 | UNUSED(ud); 195 | luaC_callGCTM(L); /* call GC metamethods for all udata */ 196 | } 197 | 198 | 199 | LUA_API void lua_close (lua_State *L) { 200 | L = G(L)->mainthread; /* only the main thread can be closed */ 201 | lua_lock(L); 202 | luaF_close(L, L->stack); /* close all upvalues for this thread */ 203 | luaC_separateudata(L, 1); /* separate udata that have GC metamethods */ 204 | L->errfunc = 0; /* no error function during GC metamethods */ 205 | do { /* repeat until no more errors */ 206 | L->ci = L->base_ci; 207 | L->base = L->top = L->ci->base; 208 | L->nCcalls = L->baseCcalls = 0; 209 | } while (luaD_rawrunprotected(L, callallgcTM, NULL) != 0); 210 | lua_assert(G(L)->tmudata == NULL); 211 | luai_userstateclose(L); 212 | close_state(L); 213 | } 214 | 215 | -------------------------------------------------------------------------------- /lua/src/lstate.h: -------------------------------------------------------------------------------- 1 | /* 2 | ** $Id: lstate.h,v 2.24.1.2 2008/01/03 15:20:39 roberto Exp $ 3 | ** Global State 4 | ** See Copyright Notice in lua.h 5 | */ 6 | 7 | #ifndef lstate_h 8 | #define lstate_h 9 | 10 | #include "lua.h" 11 | 12 | #include "lobject.h" 13 | #include "ltm.h" 14 | #include "lzio.h" 15 | 16 | 17 | 18 | struct lua_longjmp; /* defined in ldo.c */ 19 | 20 | 21 | /* table of globals */ 22 | #define gt(L) (&L->l_gt) 23 | 24 | /* registry */ 25 | #define registry(L) (&G(L)->l_registry) 26 | 27 | 28 | /* extra stack space to handle TM calls and some other extras */ 29 | #define EXTRA_STACK 5 30 | 31 | 32 | #define BASIC_CI_SIZE 8 33 | 34 | #define BASIC_STACK_SIZE (2*LUA_MINSTACK) 35 | 36 | 37 | 38 | typedef struct stringtable { 39 | GCObject **hash; 40 | lu_int32 nuse; /* number of elements */ 41 | int size; 42 | } stringtable; 43 | 44 | 45 | /* 46 | ** informations about a call 47 | */ 48 | typedef struct CallInfo { 49 | StkId base; /* base for this function */ 50 | StkId func; /* function index in the stack */ 51 | StkId top; /* top for this function */ 52 | const Instruction *savedpc; 53 | int nresults; /* expected number of results from this function */ 54 | int tailcalls; /* number of tail calls lost under this entry */ 55 | } CallInfo; 56 | 57 | 58 | 59 | #define curr_func(L) (clvalue(L->ci->func)) 60 | #define ci_func(ci) (clvalue((ci)->func)) 61 | #define f_isLua(ci) (!ci_func(ci)->c.isC) 62 | #define isLua(ci) (ttisfunction((ci)->func) && f_isLua(ci)) 63 | 64 | 65 | /* 66 | ** `global state', shared by all threads of this state 67 | */ 68 | typedef struct global_State { 69 | stringtable strt; /* hash table for strings */ 70 | lua_Alloc frealloc; /* function to reallocate memory */ 71 | void *ud; /* auxiliary data to `frealloc' */ 72 | lu_byte currentwhite; 73 | lu_byte gcstate; /* state of garbage collector */ 74 | int sweepstrgc; /* position of sweep in `strt' */ 75 | GCObject *rootgc; /* list of all collectable objects */ 76 | GCObject **sweepgc; /* position of sweep in `rootgc' */ 77 | GCObject *gray; /* list of gray objects */ 78 | GCObject *grayagain; /* list of objects to be traversed atomically */ 79 | GCObject *weak; /* list of weak tables (to be cleared) */ 80 | GCObject *tmudata; /* last element of list of userdata to be GC */ 81 | Mbuffer buff; /* temporary buffer for string concatentation */ 82 | lu_mem GCthreshold; 83 | lu_mem totalbytes; /* number of bytes currently allocated */ 84 | lu_mem estimate; /* an estimate of number of bytes actually in use */ 85 | lu_mem gcdept; /* how much GC is `behind schedule' */ 86 | int gcpause; /* size of pause between successive GCs */ 87 | int gcstepmul; /* GC `granularity' */ 88 | lua_CFunction panic; /* to be called in unprotected errors */ 89 | TValue l_registry; 90 | struct lua_State *mainthread; 91 | UpVal uvhead; /* head of double-linked list of all open upvalues */ 92 | struct Table *mt[NUM_TAGS]; /* metatables for basic types */ 93 | TString *tmname[TM_N]; /* array with tag-method names */ 94 | } global_State; 95 | 96 | 97 | /* 98 | ** `per thread' state 99 | */ 100 | struct lua_State { 101 | CommonHeader; 102 | lu_byte status; 103 | StkId top; /* first free slot in the stack */ 104 | StkId base; /* base of current function */ 105 | global_State *l_G; 106 | CallInfo *ci; /* call info for current function */ 107 | const Instruction *savedpc; /* `savedpc' of current function */ 108 | StkId stack_last; /* last free slot in the stack */ 109 | StkId stack; /* stack base */ 110 | CallInfo *end_ci; /* points after end of ci array*/ 111 | CallInfo *base_ci; /* array of CallInfo's */ 112 | int stacksize; 113 | int size_ci; /* size of array `base_ci' */ 114 | unsigned short nCcalls; /* number of nested C calls */ 115 | unsigned short baseCcalls; /* nested C calls when resuming coroutine */ 116 | lu_byte hookmask; 117 | lu_byte allowhook; 118 | int basehookcount; 119 | int hookcount; 120 | lua_Hook hook; 121 | TValue l_gt; /* table of globals */ 122 | TValue env; /* temporary place for environments */ 123 | GCObject *openupval; /* list of open upvalues in this stack */ 124 | GCObject *gclist; 125 | struct lua_longjmp *errorJmp; /* current error recover point */ 126 | ptrdiff_t errfunc; /* current error handling function (stack index) */ 127 | }; 128 | 129 | 130 | #define G(L) (L->l_G) 131 | 132 | 133 | /* 134 | ** Union of all collectable objects 135 | */ 136 | union GCObject { 137 | GCheader gch; 138 | union TString ts; 139 | union Udata u; 140 | union Closure cl; 141 | struct Table h; 142 | struct Proto p; 143 | struct UpVal uv; 144 | struct lua_State th; /* thread */ 145 | }; 146 | 147 | 148 | /* macros to convert a GCObject into a specific value */ 149 | #define rawgco2ts(o) check_exp((o)->gch.tt == LUA_TSTRING, &((o)->ts)) 150 | #define gco2ts(o) (&rawgco2ts(o)->tsv) 151 | #define rawgco2u(o) check_exp((o)->gch.tt == LUA_TUSERDATA, &((o)->u)) 152 | #define gco2u(o) (&rawgco2u(o)->uv) 153 | #define gco2cl(o) check_exp((o)->gch.tt == LUA_TFUNCTION, &((o)->cl)) 154 | #define gco2h(o) check_exp((o)->gch.tt == LUA_TTABLE, &((o)->h)) 155 | #define gco2p(o) check_exp((o)->gch.tt == LUA_TPROTO, &((o)->p)) 156 | #define gco2uv(o) check_exp((o)->gch.tt == LUA_TUPVAL, &((o)->uv)) 157 | #define ngcotouv(o) \ 158 | check_exp((o) == NULL || (o)->gch.tt == LUA_TUPVAL, &((o)->uv)) 159 | #define gco2th(o) check_exp((o)->gch.tt == LUA_TTHREAD, &((o)->th)) 160 | 161 | /* macro to convert any Lua object into a GCObject */ 162 | #define obj2gco(v) (cast(GCObject *, (v))) 163 | 164 | 165 | LUAI_FUNC lua_State *luaE_newthread (lua_State *L); 166 | LUAI_FUNC void luaE_freethread (lua_State *L, lua_State *L1); 167 | 168 | #endif 169 | 170 | -------------------------------------------------------------------------------- /lua/src/lstring.c: -------------------------------------------------------------------------------- 1 | /* 2 | ** $Id: lstring.c,v 2.8.1.1 2007/12/27 13:02:25 roberto Exp $ 3 | ** String table (keeps all strings handled by Lua) 4 | ** See Copyright Notice in lua.h 5 | */ 6 | 7 | 8 | #include 9 | 10 | #define lstring_c 11 | #define LUA_CORE 12 | 13 | #include "lua.h" 14 | 15 | #include "lmem.h" 16 | #include "lobject.h" 17 | #include "lstate.h" 18 | #include "lstring.h" 19 | 20 | 21 | 22 | void luaS_resize (lua_State *L, int newsize) { 23 | GCObject **newhash; 24 | stringtable *tb; 25 | int i; 26 | if (G(L)->gcstate == GCSsweepstring) 27 | return; /* cannot resize during GC traverse */ 28 | newhash = luaM_newvector(L, newsize, GCObject *); 29 | tb = &G(L)->strt; 30 | for (i=0; isize; i++) { 33 | GCObject *p = tb->hash[i]; 34 | while (p) { /* for each node in the list */ 35 | GCObject *next = p->gch.next; /* save next */ 36 | unsigned int h = gco2ts(p)->hash; 37 | int h1 = lmod(h, newsize); /* new position */ 38 | lua_assert(cast_int(h%newsize) == lmod(h, newsize)); 39 | p->gch.next = newhash[h1]; /* chain it */ 40 | newhash[h1] = p; 41 | p = next; 42 | } 43 | } 44 | luaM_freearray(L, tb->hash, tb->size, TString *); 45 | tb->size = newsize; 46 | tb->hash = newhash; 47 | } 48 | 49 | 50 | static TString *newlstr (lua_State *L, const char *str, size_t l, 51 | unsigned int h) { 52 | TString *ts; 53 | stringtable *tb; 54 | if (l+1 > (MAX_SIZET - sizeof(TString))/sizeof(char)) 55 | luaM_toobig(L); 56 | ts = cast(TString *, luaM_malloc(L, (l+1)*sizeof(char)+sizeof(TString))); 57 | ts->tsv.len = l; 58 | ts->tsv.hash = h; 59 | ts->tsv.marked = luaC_white(G(L)); 60 | ts->tsv.tt = LUA_TSTRING; 61 | ts->tsv.reserved = 0; 62 | memcpy(ts+1, str, l*sizeof(char)); 63 | ((char *)(ts+1))[l] = '\0'; /* ending 0 */ 64 | tb = &G(L)->strt; 65 | h = lmod(h, tb->size); 66 | ts->tsv.next = tb->hash[h]; /* chain new entry */ 67 | tb->hash[h] = obj2gco(ts); 68 | tb->nuse++; 69 | if (tb->nuse > cast(lu_int32, tb->size) && tb->size <= MAX_INT/2) 70 | luaS_resize(L, tb->size*2); /* too crowded */ 71 | return ts; 72 | } 73 | 74 | 75 | TString *luaS_newlstr (lua_State *L, const char *str, size_t l) { 76 | GCObject *o; 77 | unsigned int h = cast(unsigned int, l); /* seed */ 78 | size_t step = (l>>5)+1; /* if string is too long, don't hash all its chars */ 79 | size_t l1; 80 | for (l1=l; l1>=step; l1-=step) /* compute hash */ 81 | h = h ^ ((h<<5)+(h>>2)+cast(unsigned char, str[l1-1])); 82 | for (o = G(L)->strt.hash[lmod(h, G(L)->strt.size)]; 83 | o != NULL; 84 | o = o->gch.next) { 85 | TString *ts = rawgco2ts(o); 86 | if (ts->tsv.len == l && (memcmp(str, getstr(ts), l) == 0)) { 87 | /* string may be dead */ 88 | if (isdead(G(L), o)) changewhite(o); 89 | return ts; 90 | } 91 | } 92 | return newlstr(L, str, l, h); /* not found */ 93 | } 94 | 95 | 96 | Udata *luaS_newudata (lua_State *L, size_t s, Table *e) { 97 | Udata *u; 98 | if (s > MAX_SIZET - sizeof(Udata)) 99 | luaM_toobig(L); 100 | u = cast(Udata *, luaM_malloc(L, s + sizeof(Udata))); 101 | u->uv.marked = luaC_white(G(L)); /* is not finalized */ 102 | u->uv.tt = LUA_TUSERDATA; 103 | u->uv.len = s; 104 | u->uv.metatable = NULL; 105 | u->uv.env = e; 106 | /* chain it on udata list (after main thread) */ 107 | u->uv.next = G(L)->mainthread->next; 108 | G(L)->mainthread->next = obj2gco(u); 109 | return u; 110 | } 111 | 112 | -------------------------------------------------------------------------------- /lua/src/lstring.h: -------------------------------------------------------------------------------- 1 | /* 2 | ** $Id: lstring.h,v 1.43.1.1 2007/12/27 13:02:25 roberto Exp $ 3 | ** String table (keep all strings handled by Lua) 4 | ** See Copyright Notice in lua.h 5 | */ 6 | 7 | #ifndef lstring_h 8 | #define lstring_h 9 | 10 | 11 | #include "lgc.h" 12 | #include "lobject.h" 13 | #include "lstate.h" 14 | 15 | 16 | #define sizestring(s) (sizeof(union TString)+((s)->len+1)*sizeof(char)) 17 | 18 | #define sizeudata(u) (sizeof(union Udata)+(u)->len) 19 | 20 | #define luaS_new(L, s) (luaS_newlstr(L, s, strlen(s))) 21 | #define luaS_newliteral(L, s) (luaS_newlstr(L, "" s, \ 22 | (sizeof(s)/sizeof(char))-1)) 23 | 24 | #define luaS_fix(s) l_setbit((s)->tsv.marked, FIXEDBIT) 25 | 26 | LUAI_FUNC void luaS_resize (lua_State *L, int newsize); 27 | LUAI_FUNC Udata *luaS_newudata (lua_State *L, size_t s, Table *e); 28 | LUAI_FUNC TString *luaS_newlstr (lua_State *L, const char *str, size_t l); 29 | 30 | 31 | #endif 32 | -------------------------------------------------------------------------------- /lua/src/ltable.h: -------------------------------------------------------------------------------- 1 | /* 2 | ** $Id: ltable.h,v 2.10.1.1 2007/12/27 13:02:25 roberto Exp $ 3 | ** Lua tables (hash) 4 | ** See Copyright Notice in lua.h 5 | */ 6 | 7 | #ifndef ltable_h 8 | #define ltable_h 9 | 10 | #include "lobject.h" 11 | 12 | 13 | #define gnode(t,i) (&(t)->node[i]) 14 | #define gkey(n) (&(n)->i_key.nk) 15 | #define gval(n) (&(n)->i_val) 16 | #define gnext(n) ((n)->i_key.nk.next) 17 | 18 | #define key2tval(n) (&(n)->i_key.tvk) 19 | 20 | 21 | LUAI_FUNC const TValue *luaH_getnum (Table *t, int key); 22 | LUAI_FUNC TValue *luaH_setnum (lua_State *L, Table *t, int key); 23 | LUAI_FUNC const TValue *luaH_getstr (Table *t, TString *key); 24 | LUAI_FUNC TValue *luaH_setstr (lua_State *L, Table *t, TString *key); 25 | LUAI_FUNC const TValue *luaH_get (Table *t, const TValue *key); 26 | LUAI_FUNC TValue *luaH_set (lua_State *L, Table *t, const TValue *key); 27 | LUAI_FUNC Table *luaH_new (lua_State *L, int narray, int lnhash); 28 | LUAI_FUNC void luaH_resizearray (lua_State *L, Table *t, int nasize); 29 | LUAI_FUNC void luaH_free (lua_State *L, Table *t); 30 | LUAI_FUNC int luaH_next (lua_State *L, Table *t, StkId key); 31 | LUAI_FUNC int luaH_getn (Table *t); 32 | 33 | 34 | #if defined(LUA_DEBUG) 35 | LUAI_FUNC Node *luaH_mainposition (const Table *t, const TValue *key); 36 | LUAI_FUNC int luaH_isdummy (Node *n); 37 | #endif 38 | 39 | 40 | #endif 41 | -------------------------------------------------------------------------------- /lua/src/ltm.c: -------------------------------------------------------------------------------- 1 | /* 2 | ** $Id: ltm.c,v 2.8.1.1 2007/12/27 13:02:25 roberto Exp $ 3 | ** Tag methods 4 | ** See Copyright Notice in lua.h 5 | */ 6 | 7 | 8 | #include 9 | 10 | #define ltm_c 11 | #define LUA_CORE 12 | 13 | #include "lua.h" 14 | 15 | #include "lobject.h" 16 | #include "lstate.h" 17 | #include "lstring.h" 18 | #include "ltable.h" 19 | #include "ltm.h" 20 | 21 | 22 | 23 | const char *const luaT_typenames[] = { 24 | "nil", "boolean", "userdata", "number", 25 | "string", "table", "function", "userdata", "thread", 26 | "proto", "upval" 27 | }; 28 | 29 | 30 | void luaT_init (lua_State *L) { 31 | static const char *const luaT_eventname[] = { /* ORDER TM */ 32 | "__index", "__newindex", 33 | "__gc", "__mode", "__eq", 34 | "__add", "__sub", "__mul", "__div", "__mod", 35 | "__pow", "__unm", "__len", "__lt", "__le", 36 | "__concat", "__call" 37 | }; 38 | int i; 39 | for (i=0; itmname[i] = luaS_new(L, luaT_eventname[i]); 41 | luaS_fix(G(L)->tmname[i]); /* never collect these names */ 42 | } 43 | } 44 | 45 | 46 | /* 47 | ** function to be used with macro "fasttm": optimized for absence of 48 | ** tag methods 49 | */ 50 | const TValue *luaT_gettm (Table *events, TMS event, TString *ename) { 51 | const TValue *tm = luaH_getstr(events, ename); 52 | lua_assert(event <= TM_EQ); 53 | if (ttisnil(tm)) { /* no tag method? */ 54 | events->flags |= cast_byte(1u<metatable; 66 | break; 67 | case LUA_TUSERDATA: 68 | mt = uvalue(o)->metatable; 69 | break; 70 | default: 71 | mt = G(L)->mt[ttype(o)]; 72 | } 73 | return (mt ? luaH_getstr(mt, G(L)->tmname[event]) : luaO_nilobject); 74 | } 75 | 76 | -------------------------------------------------------------------------------- /lua/src/ltm.h: -------------------------------------------------------------------------------- 1 | /* 2 | ** $Id: ltm.h,v 2.6.1.1 2007/12/27 13:02:25 roberto Exp $ 3 | ** Tag methods 4 | ** See Copyright Notice in lua.h 5 | */ 6 | 7 | #ifndef ltm_h 8 | #define ltm_h 9 | 10 | 11 | #include "lobject.h" 12 | 13 | 14 | /* 15 | * WARNING: if you change the order of this enumeration, 16 | * grep "ORDER TM" 17 | */ 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_MOD, 29 | TM_POW, 30 | TM_UNM, 31 | TM_LEN, 32 | TM_LT, 33 | TM_LE, 34 | TM_CONCAT, 35 | TM_CALL, 36 | TM_N /* number of elements in the enum */ 37 | } TMS; 38 | 39 | 40 | 41 | #define gfasttm(g,et,e) ((et) == NULL ? NULL : \ 42 | ((et)->flags & (1u<<(e))) ? NULL : luaT_gettm(et, e, (g)->tmname[e])) 43 | 44 | #define fasttm(l,et,e) gfasttm(G(l), et, e) 45 | 46 | LUAI_DATA const char *const luaT_typenames[]; 47 | 48 | 49 | LUAI_FUNC const TValue *luaT_gettm (Table *events, TMS event, TString *ename); 50 | LUAI_FUNC const TValue *luaT_gettmbyobj (lua_State *L, const TValue *o, 51 | TMS event); 52 | LUAI_FUNC void luaT_init (lua_State *L); 53 | 54 | #endif 55 | -------------------------------------------------------------------------------- /lua/src/luac.c: -------------------------------------------------------------------------------- 1 | /* 2 | ** $Id: luac.c,v 1.54 2006/06/02 17:37:11 lhf Exp $ 3 | ** Lua compiler (saves bytecodes to files; also list bytecodes) 4 | ** See Copyright Notice in lua.h 5 | */ 6 | 7 | #include 8 | #include 9 | #include 10 | #include 11 | 12 | #define luac_c 13 | #define LUA_CORE 14 | 15 | #include "lua.h" 16 | #include "lauxlib.h" 17 | 18 | #include "ldo.h" 19 | #include "lfunc.h" 20 | #include "lmem.h" 21 | #include "lobject.h" 22 | #include "lopcodes.h" 23 | #include "lstring.h" 24 | #include "lundump.h" 25 | 26 | #define PROGNAME "luac" /* default program name */ 27 | #define OUTPUT PROGNAME ".out" /* default output file */ 28 | 29 | static int listing=0; /* list bytecodes? */ 30 | static int dumping=1; /* dump bytecodes? */ 31 | static int stripping=0; /* strip debug information? */ 32 | static char Output[]={ OUTPUT }; /* default output file name */ 33 | static const char* output=Output; /* actual output file name */ 34 | static const char* progname=PROGNAME; /* actual program name */ 35 | 36 | static void fatal(const char* message) 37 | { 38 | fprintf(stderr,"%s: %s\n",progname,message); 39 | exit(EXIT_FAILURE); 40 | } 41 | 42 | static void cannot(const char* what) 43 | { 44 | fprintf(stderr,"%s: cannot %s %s: %s\n",progname,what,output,strerror(errno)); 45 | exit(EXIT_FAILURE); 46 | } 47 | 48 | static void usage(const char* message) 49 | { 50 | if (*message=='-') 51 | fprintf(stderr,"%s: unrecognized option " LUA_QS "\n",progname,message); 52 | else 53 | fprintf(stderr,"%s: %s\n",progname,message); 54 | fprintf(stderr, 55 | "usage: %s [options] [filenames].\n" 56 | "Available options are:\n" 57 | " - process stdin\n" 58 | " -l list\n" 59 | " -o name output to file " LUA_QL("name") " (default is \"%s\")\n" 60 | " -p parse only\n" 61 | " -s strip debug information\n" 62 | " -v show version information\n" 63 | " -- stop handling options\n", 64 | progname,Output); 65 | exit(EXIT_FAILURE); 66 | } 67 | 68 | #define IS(s) (strcmp(argv[i],s)==0) 69 | 70 | static int doargs(int argc, char* argv[]) 71 | { 72 | int i; 73 | int version=0; 74 | if (argv[0]!=NULL && *argv[0]!=0) progname=argv[0]; 75 | for (i=1; itop+(i))->l.p) 118 | 119 | static const Proto* combine(lua_State* L, int n) 120 | { 121 | if (n==1) 122 | return toproto(L,-1); 123 | else 124 | { 125 | int i,pc; 126 | Proto* f=luaF_newproto(L); 127 | setptvalue2s(L,L->top,f); incr_top(L); 128 | f->source=luaS_newliteral(L,"=(" PROGNAME ")"); 129 | f->maxstacksize=1; 130 | pc=2*n+1; 131 | f->code=luaM_newvector(L,pc,Instruction); 132 | f->sizecode=pc; 133 | f->p=luaM_newvector(L,n,Proto*); 134 | f->sizep=n; 135 | pc=0; 136 | for (i=0; ip[i]=toproto(L,i-n-1); 139 | f->code[pc++]=CREATE_ABx(OP_CLOSURE,0,i); 140 | f->code[pc++]=CREATE_ABC(OP_CALL,0,1,1); 141 | } 142 | f->code[pc++]=CREATE_ABC(OP_RETURN,0,1,0); 143 | return f; 144 | } 145 | } 146 | 147 | static int writer(lua_State* L, const void* p, size_t size, void* u) 148 | { 149 | UNUSED(L); 150 | return (fwrite(p,size,1,(FILE*)u)!=1) && (size!=0); 151 | } 152 | 153 | struct Smain { 154 | int argc; 155 | char** argv; 156 | }; 157 | 158 | static int pmain(lua_State* L) 159 | { 160 | struct Smain* s = (struct Smain*)lua_touserdata(L, 1); 161 | int argc=s->argc; 162 | char** argv=s->argv; 163 | const Proto* f; 164 | int i; 165 | if (!lua_checkstack(L,argc)) fatal("too many input files"); 166 | for (i=0; i1); 173 | if (dumping) 174 | { 175 | FILE* D= (output==NULL) ? stdout : fopen(output,"wb"); 176 | if (D==NULL) cannot("open"); 177 | lua_lock(L); 178 | luaU_dump(L,f,writer,D,stripping); 179 | lua_unlock(L); 180 | if (ferror(D)) cannot("write"); 181 | if (fclose(D)) cannot("close"); 182 | } 183 | return 0; 184 | } 185 | 186 | int main(int argc, char* argv[]) 187 | { 188 | lua_State* L; 189 | struct Smain s; 190 | int i=doargs(argc,argv); 191 | argc-=i; argv+=i; 192 | if (argc<=0) usage("no input files given"); 193 | L=lua_open(); 194 | if (L==NULL) fatal("not enough memory for state"); 195 | s.argc=argc; 196 | s.argv=argv; 197 | if (lua_cpcall(L,pmain,&s)!=0) fatal(lua_tostring(L,-1)); 198 | lua_close(L); 199 | return EXIT_SUCCESS; 200 | } 201 | -------------------------------------------------------------------------------- /lua/src/lualib.h: -------------------------------------------------------------------------------- 1 | /* 2 | ** $Id: lualib.h,v 1.36.1.1 2007/12/27 13:02:25 roberto Exp $ 3 | ** Lua standard libraries 4 | ** See Copyright Notice in lua.h 5 | */ 6 | 7 | 8 | #ifndef lualib_h 9 | #define lualib_h 10 | 11 | #include "lua.h" 12 | 13 | 14 | /* Key to file-handle type */ 15 | #define LUA_FILEHANDLE "FILE*" 16 | 17 | 18 | #define LUA_COLIBNAME "coroutine" 19 | LUALIB_API int (luaopen_base) (lua_State *L); 20 | 21 | #define LUA_TABLIBNAME "table" 22 | LUALIB_API int (luaopen_table) (lua_State *L); 23 | 24 | #define LUA_IOLIBNAME "io" 25 | LUALIB_API int (luaopen_io) (lua_State *L); 26 | 27 | #define LUA_OSLIBNAME "os" 28 | LUALIB_API int (luaopen_os) (lua_State *L); 29 | 30 | #define LUA_STRLIBNAME "string" 31 | LUALIB_API int (luaopen_string) (lua_State *L); 32 | 33 | #define LUA_MATHLIBNAME "math" 34 | LUALIB_API int (luaopen_math) (lua_State *L); 35 | 36 | #define LUA_DBLIBNAME "debug" 37 | LUALIB_API int (luaopen_debug) (lua_State *L); 38 | 39 | #define LUA_LOADLIBNAME "package" 40 | LUALIB_API int (luaopen_package) (lua_State *L); 41 | 42 | 43 | /* open all previous libraries */ 44 | LUALIB_API void (luaL_openlibs) (lua_State *L); 45 | 46 | 47 | 48 | #ifndef lua_assert 49 | #define lua_assert(x) ((void)0) 50 | #endif 51 | 52 | 53 | #endif 54 | -------------------------------------------------------------------------------- /lua/src/lundump.c: -------------------------------------------------------------------------------- 1 | /* 2 | ** $Id: lundump.c,v 2.7.1.4 2008/04/04 19:51:41 roberto Exp $ 3 | ** load precompiled Lua chunks 4 | ** See Copyright Notice in lua.h 5 | */ 6 | 7 | #include 8 | 9 | #define lundump_c 10 | #define LUA_CORE 11 | 12 | #include "lua.h" 13 | 14 | #include "ldebug.h" 15 | #include "ldo.h" 16 | #include "lfunc.h" 17 | #include "lmem.h" 18 | #include "lobject.h" 19 | #include "lstring.h" 20 | #include "lundump.h" 21 | #include "lzio.h" 22 | 23 | typedef struct { 24 | lua_State* L; 25 | ZIO* Z; 26 | Mbuffer* b; 27 | const char* name; 28 | } LoadState; 29 | 30 | #ifdef LUAC_TRUST_BINARIES 31 | #define IF(c,s) 32 | #define error(S,s) 33 | #else 34 | #define IF(c,s) if (c) error(S,s) 35 | 36 | static void error(LoadState* S, const char* why) 37 | { 38 | luaO_pushfstring(S->L,"%s: %s in precompiled chunk",S->name,why); 39 | luaD_throw(S->L,LUA_ERRSYNTAX); 40 | } 41 | #endif 42 | 43 | #define LoadMem(S,b,n,size) LoadBlock(S,b,(n)*(size)) 44 | #define LoadByte(S) (lu_byte)LoadChar(S) 45 | #define LoadVar(S,x) LoadMem(S,&x,1,sizeof(x)) 46 | #define LoadVector(S,b,n,size) LoadMem(S,b,n,size) 47 | 48 | static void LoadBlock(LoadState* S, void* b, size_t size) 49 | { 50 | size_t r=luaZ_read(S->Z,b,size); 51 | IF (r!=0, "unexpected end"); 52 | } 53 | 54 | static int LoadChar(LoadState* S) 55 | { 56 | char x; 57 | LoadVar(S,x); 58 | return x; 59 | } 60 | 61 | static int LoadInt(LoadState* S) 62 | { 63 | int x; 64 | LoadVar(S,x); 65 | IF (x<0, "bad integer"); 66 | return x; 67 | } 68 | 69 | static lua_Number LoadNumber(LoadState* S) 70 | { 71 | lua_Number x; 72 | LoadVar(S,x); 73 | return x; 74 | } 75 | 76 | static TString* LoadString(LoadState* S) 77 | { 78 | size_t size; 79 | LoadVar(S,size); 80 | if (size==0) 81 | return NULL; 82 | else 83 | { 84 | char* s=luaZ_openspace(S->L,S->b,size); 85 | LoadBlock(S,s,size); 86 | return luaS_newlstr(S->L,s,size-1); /* remove trailing '\0' */ 87 | } 88 | } 89 | 90 | static void LoadCode(LoadState* S, Proto* f) 91 | { 92 | int n=LoadInt(S); 93 | f->code=luaM_newvector(S->L,n,Instruction); 94 | f->sizecode=n; 95 | LoadVector(S,f->code,n,sizeof(Instruction)); 96 | } 97 | 98 | static Proto* LoadFunction(LoadState* S, TString* p); 99 | 100 | static void LoadConstants(LoadState* S, Proto* f) 101 | { 102 | int i,n; 103 | n=LoadInt(S); 104 | f->k=luaM_newvector(S->L,n,TValue); 105 | f->sizek=n; 106 | for (i=0; ik[i]); 107 | for (i=0; ik[i]; 110 | int t=LoadChar(S); 111 | switch (t) 112 | { 113 | case LUA_TNIL: 114 | setnilvalue(o); 115 | break; 116 | case LUA_TBOOLEAN: 117 | setbvalue(o,LoadChar(S)!=0); 118 | break; 119 | case LUA_TNUMBER: 120 | setnvalue(o,LoadNumber(S)); 121 | break; 122 | case LUA_TSTRING: 123 | setsvalue2n(S->L,o,LoadString(S)); 124 | break; 125 | default: 126 | error(S,"bad constant"); 127 | break; 128 | } 129 | } 130 | n=LoadInt(S); 131 | f->p=luaM_newvector(S->L,n,Proto*); 132 | f->sizep=n; 133 | for (i=0; ip[i]=NULL; 134 | for (i=0; ip[i]=LoadFunction(S,f->source); 135 | } 136 | 137 | static void LoadDebug(LoadState* S, Proto* f) 138 | { 139 | int i,n; 140 | n=LoadInt(S); 141 | f->lineinfo=luaM_newvector(S->L,n,int); 142 | f->sizelineinfo=n; 143 | LoadVector(S,f->lineinfo,n,sizeof(int)); 144 | n=LoadInt(S); 145 | f->locvars=luaM_newvector(S->L,n,LocVar); 146 | f->sizelocvars=n; 147 | for (i=0; ilocvars[i].varname=NULL; 148 | for (i=0; ilocvars[i].varname=LoadString(S); 151 | f->locvars[i].startpc=LoadInt(S); 152 | f->locvars[i].endpc=LoadInt(S); 153 | } 154 | n=LoadInt(S); 155 | f->upvalues=luaM_newvector(S->L,n,TString*); 156 | f->sizeupvalues=n; 157 | for (i=0; iupvalues[i]=NULL; 158 | for (i=0; iupvalues[i]=LoadString(S); 159 | } 160 | 161 | static Proto* LoadFunction(LoadState* S, TString* p) 162 | { 163 | Proto* f; 164 | if (++S->L->nCcalls > LUAI_MAXCCALLS) error(S,"code too deep"); 165 | f=luaF_newproto(S->L); 166 | setptvalue2s(S->L,S->L->top,f); incr_top(S->L); 167 | f->source=LoadString(S); if (f->source==NULL) f->source=p; 168 | f->linedefined=LoadInt(S); 169 | f->lastlinedefined=LoadInt(S); 170 | f->nups=LoadByte(S); 171 | f->numparams=LoadByte(S); 172 | f->is_vararg=LoadByte(S); 173 | f->maxstacksize=LoadByte(S); 174 | LoadCode(S,f); 175 | LoadConstants(S,f); 176 | LoadDebug(S,f); 177 | IF (!luaG_checkcode(f), "bad code"); 178 | S->L->top--; 179 | S->L->nCcalls--; 180 | return f; 181 | } 182 | 183 | static void LoadHeader(LoadState* S) 184 | { 185 | char h[LUAC_HEADERSIZE]; 186 | char s[LUAC_HEADERSIZE]; 187 | luaU_header(h); 188 | LoadBlock(S,s,LUAC_HEADERSIZE); 189 | IF (memcmp(h,s,LUAC_HEADERSIZE)!=0, "bad header"); 190 | } 191 | 192 | /* 193 | ** load precompiled chunk 194 | */ 195 | Proto* luaU_undump (lua_State* L, ZIO* Z, Mbuffer* buff, const char* name) 196 | { 197 | LoadState S; 198 | if (*name=='@' || *name=='=') 199 | S.name=name+1; 200 | else if (*name==LUA_SIGNATURE[0]) 201 | S.name="binary string"; 202 | else 203 | S.name=name; 204 | S.L=L; 205 | S.Z=Z; 206 | S.b=buff; 207 | LoadHeader(&S); 208 | return LoadFunction(&S,luaS_newliteral(L,"=?")); 209 | } 210 | 211 | /* 212 | * make header 213 | */ 214 | void luaU_header (char* h) 215 | { 216 | int x=1; 217 | memcpy(h,LUA_SIGNATURE,sizeof(LUA_SIGNATURE)-1); 218 | h+=sizeof(LUA_SIGNATURE)-1; 219 | *h++=(char)LUAC_VERSION; 220 | *h++=(char)LUAC_FORMAT; 221 | *h++=(char)*(char*)&x; /* endianness */ 222 | *h++=(char)sizeof(int); 223 | *h++=(char)sizeof(size_t); 224 | *h++=(char)sizeof(Instruction); 225 | *h++=(char)sizeof(lua_Number); 226 | *h++=(char)(((lua_Number)0.5)==0); /* is lua_Number integral? */ 227 | } 228 | -------------------------------------------------------------------------------- /lua/src/lundump.h: -------------------------------------------------------------------------------- 1 | /* 2 | ** $Id: lundump.h,v 1.37.1.1 2007/12/27 13:02:25 roberto Exp $ 3 | ** load precompiled Lua chunks 4 | ** See Copyright Notice in lua.h 5 | */ 6 | 7 | #ifndef lundump_h 8 | #define lundump_h 9 | 10 | #include "lobject.h" 11 | #include "lzio.h" 12 | 13 | /* load one chunk; from lundump.c */ 14 | LUAI_FUNC Proto* luaU_undump (lua_State* L, ZIO* Z, Mbuffer* buff, const char* name); 15 | 16 | /* make header; from lundump.c */ 17 | LUAI_FUNC void luaU_header (char* h); 18 | 19 | /* dump one chunk; from ldump.c */ 20 | LUAI_FUNC int luaU_dump (lua_State* L, const Proto* f, lua_Writer w, void* data, int strip); 21 | 22 | #ifdef luac_c 23 | /* print one chunk; from print.c */ 24 | LUAI_FUNC void luaU_print (const Proto* f, int full); 25 | #endif 26 | 27 | /* for header of binary files -- this is Lua 5.1 */ 28 | #define LUAC_VERSION 0x51 29 | 30 | /* for header of binary files -- this is the official format */ 31 | #define LUAC_FORMAT 0 32 | 33 | /* size of header of binary files */ 34 | #define LUAC_HEADERSIZE 12 35 | 36 | #endif 37 | -------------------------------------------------------------------------------- /lua/src/lvm.h: -------------------------------------------------------------------------------- 1 | /* 2 | ** $Id: lvm.h,v 2.5.1.1 2007/12/27 13:02:25 roberto Exp $ 3 | ** Lua virtual machine 4 | ** See Copyright Notice in lua.h 5 | */ 6 | 7 | #ifndef lvm_h 8 | #define lvm_h 9 | 10 | 11 | #include "ldo.h" 12 | #include "lobject.h" 13 | #include "ltm.h" 14 | 15 | 16 | #define tostring(L,o) ((ttype(o) == LUA_TSTRING) || (luaV_tostring(L, o))) 17 | 18 | #define tonumber(o,n) (ttype(o) == LUA_TNUMBER || \ 19 | (((o) = luaV_tonumber(o,n)) != NULL)) 20 | 21 | #define equalobj(L,o1,o2) \ 22 | (ttype(o1) == ttype(o2) && luaV_equalval(L, o1, o2)) 23 | 24 | 25 | LUAI_FUNC int luaV_lessthan (lua_State *L, const TValue *l, const TValue *r); 26 | LUAI_FUNC int luaV_equalval (lua_State *L, const TValue *t1, const TValue *t2); 27 | LUAI_FUNC const TValue *luaV_tonumber (const TValue *obj, TValue *n); 28 | LUAI_FUNC int luaV_tostring (lua_State *L, StkId obj); 29 | LUAI_FUNC void luaV_gettable (lua_State *L, const TValue *t, TValue *key, 30 | StkId val); 31 | LUAI_FUNC void luaV_settable (lua_State *L, const TValue *t, TValue *key, 32 | StkId val); 33 | LUAI_FUNC void luaV_execute (lua_State *L, int nexeccalls); 34 | LUAI_FUNC void luaV_concat (lua_State *L, int total, int last); 35 | 36 | #endif 37 | -------------------------------------------------------------------------------- /lua/src/lzio.c: -------------------------------------------------------------------------------- 1 | /* 2 | ** $Id: lzio.c,v 1.31.1.1 2007/12/27 13:02:25 roberto Exp $ 3 | ** a generic input stream interface 4 | ** See Copyright Notice in lua.h 5 | */ 6 | 7 | 8 | #include 9 | 10 | #define lzio_c 11 | #define LUA_CORE 12 | 13 | #include "lua.h" 14 | 15 | #include "llimits.h" 16 | #include "lmem.h" 17 | #include "lstate.h" 18 | #include "lzio.h" 19 | 20 | 21 | int luaZ_fill (ZIO *z) { 22 | size_t size; 23 | lua_State *L = z->L; 24 | const char *buff; 25 | lua_unlock(L); 26 | buff = z->reader(L, z->data, &size); 27 | lua_lock(L); 28 | if (buff == NULL || size == 0) return EOZ; 29 | z->n = size - 1; 30 | z->p = buff; 31 | return char2int(*(z->p++)); 32 | } 33 | 34 | 35 | int luaZ_lookahead (ZIO *z) { 36 | if (z->n == 0) { 37 | if (luaZ_fill(z) == EOZ) 38 | return EOZ; 39 | else { 40 | z->n++; /* luaZ_fill removed first byte; put back it */ 41 | z->p--; 42 | } 43 | } 44 | return char2int(*z->p); 45 | } 46 | 47 | 48 | void luaZ_init (lua_State *L, ZIO *z, lua_Reader reader, void *data) { 49 | z->L = L; 50 | z->reader = reader; 51 | z->data = data; 52 | z->n = 0; 53 | z->p = NULL; 54 | } 55 | 56 | 57 | /* --------------------------------------------------------------- read --- */ 58 | size_t luaZ_read (ZIO *z, void *b, size_t n) { 59 | while (n) { 60 | size_t m; 61 | if (luaZ_lookahead(z) == EOZ) 62 | return n; /* return number of missing bytes */ 63 | m = (n <= z->n) ? n : z->n; /* min. between n and z->n */ 64 | memcpy(b, z->p, m); 65 | z->n -= m; 66 | z->p += m; 67 | b = (char *)b + m; 68 | n -= m; 69 | } 70 | return 0; 71 | } 72 | 73 | /* ------------------------------------------------------------------------ */ 74 | char *luaZ_openspace (lua_State *L, Mbuffer *buff, size_t n) { 75 | if (n > buff->buffsize) { 76 | if (n < LUA_MINBUFFER) n = LUA_MINBUFFER; 77 | luaZ_resizebuffer(L, buff, n); 78 | } 79 | return buff->buffer; 80 | } 81 | 82 | 83 | -------------------------------------------------------------------------------- /lua/src/lzio.h: -------------------------------------------------------------------------------- 1 | /* 2 | ** $Id: lzio.h,v 1.21.1.1 2007/12/27 13:02:25 roberto Exp $ 3 | ** Buffered streams 4 | ** See Copyright Notice in lua.h 5 | */ 6 | 7 | 8 | #ifndef lzio_h 9 | #define lzio_h 10 | 11 | #include "lua.h" 12 | 13 | #include "lmem.h" 14 | 15 | 16 | #define EOZ (-1) /* end of stream */ 17 | 18 | typedef struct Zio ZIO; 19 | 20 | #define char2int(c) cast(int, cast(unsigned char, (c))) 21 | 22 | #define zgetc(z) (((z)->n--)>0 ? char2int(*(z)->p++) : luaZ_fill(z)) 23 | 24 | typedef struct Mbuffer { 25 | char *buffer; 26 | size_t n; 27 | size_t buffsize; 28 | } Mbuffer; 29 | 30 | #define luaZ_initbuffer(L, buff) ((buff)->buffer = NULL, (buff)->buffsize = 0) 31 | 32 | #define luaZ_buffer(buff) ((buff)->buffer) 33 | #define luaZ_sizebuffer(buff) ((buff)->buffsize) 34 | #define luaZ_bufflen(buff) ((buff)->n) 35 | 36 | #define luaZ_resetbuffer(buff) ((buff)->n = 0) 37 | 38 | 39 | #define luaZ_resizebuffer(L, buff, size) \ 40 | (luaM_reallocvector(L, (buff)->buffer, (buff)->buffsize, size, char), \ 41 | (buff)->buffsize = size) 42 | 43 | #define luaZ_freebuffer(L, buff) luaZ_resizebuffer(L, buff, 0) 44 | 45 | 46 | LUAI_FUNC char *luaZ_openspace (lua_State *L, Mbuffer *buff, size_t n); 47 | LUAI_FUNC void luaZ_init (lua_State *L, ZIO *z, lua_Reader reader, 48 | void *data); 49 | LUAI_FUNC size_t luaZ_read (ZIO* z, void* b, size_t n); /* read next n bytes */ 50 | LUAI_FUNC int luaZ_lookahead (ZIO *z); 51 | 52 | 53 | 54 | /* --------- Private Part ------------------ */ 55 | 56 | struct Zio { 57 | size_t n; /* bytes still unread */ 58 | const char *p; /* current position in buffer */ 59 | lua_Reader reader; 60 | void* data; /* additional data */ 61 | lua_State *L; /* Lua state (for reader) */ 62 | }; 63 | 64 | 65 | LUAI_FUNC int luaZ_fill (ZIO *z); 66 | 67 | #endif 68 | -------------------------------------------------------------------------------- /lua/src/print.c: -------------------------------------------------------------------------------- 1 | /* 2 | ** $Id: print.c,v 1.55a 2006/05/31 13:30:05 lhf Exp $ 3 | ** print bytecodes 4 | ** See Copyright Notice in lua.h 5 | */ 6 | 7 | #include 8 | #include 9 | 10 | #define luac_c 11 | #define LUA_CORE 12 | 13 | #include "ldebug.h" 14 | #include "lobject.h" 15 | #include "lopcodes.h" 16 | #include "lundump.h" 17 | 18 | #define PrintFunction luaU_print 19 | 20 | #define Sizeof(x) ((int)sizeof(x)) 21 | #define VOID(p) ((const void*)(p)) 22 | 23 | static void PrintString(const TString* ts) 24 | { 25 | const char* s=getstr(ts); 26 | size_t i,n=ts->tsv.len; 27 | putchar('"'); 28 | for (i=0; ik[i]; 54 | switch (ttype(o)) 55 | { 56 | case LUA_TNIL: 57 | printf("nil"); 58 | break; 59 | case LUA_TBOOLEAN: 60 | printf(bvalue(o) ? "true" : "false"); 61 | break; 62 | case LUA_TNUMBER: 63 | printf(LUA_NUMBER_FMT,nvalue(o)); 64 | break; 65 | case LUA_TSTRING: 66 | PrintString(rawtsvalue(o)); 67 | break; 68 | default: /* cannot happen */ 69 | printf("? type=%d",ttype(o)); 70 | break; 71 | } 72 | } 73 | 74 | static void PrintCode(const Proto* f) 75 | { 76 | const Instruction* code=f->code; 77 | int pc,n=f->sizecode; 78 | for (pc=0; pc0) printf("[%d]\t",line); else printf("[-]\t"); 90 | printf("%-9s\t",luaP_opnames[o]); 91 | switch (getOpMode(o)) 92 | { 93 | case iABC: 94 | printf("%d",a); 95 | if (getBMode(o)!=OpArgN) printf(" %d",ISK(b) ? (-1-INDEXK(b)) : b); 96 | if (getCMode(o)!=OpArgN) printf(" %d",ISK(c) ? (-1-INDEXK(c)) : c); 97 | break; 98 | case iABx: 99 | if (getBMode(o)==OpArgK) printf("%d %d",a,-1-bx); else printf("%d %d",a,bx); 100 | break; 101 | case iAsBx: 102 | if (o==OP_JMP) printf("%d",sbx); else printf("%d %d",a,sbx); 103 | break; 104 | } 105 | switch (o) 106 | { 107 | case OP_LOADK: 108 | printf("\t; "); PrintConstant(f,bx); 109 | break; 110 | case OP_GETUPVAL: 111 | case OP_SETUPVAL: 112 | printf("\t; %s", (f->sizeupvalues>0) ? getstr(f->upvalues[b]) : "-"); 113 | break; 114 | case OP_GETGLOBAL: 115 | case OP_SETGLOBAL: 116 | printf("\t; %s",svalue(&f->k[bx])); 117 | break; 118 | case OP_GETTABLE: 119 | case OP_SELF: 120 | if (ISK(c)) { printf("\t; "); PrintConstant(f,INDEXK(c)); } 121 | break; 122 | case OP_SETTABLE: 123 | case OP_ADD: 124 | case OP_SUB: 125 | case OP_MUL: 126 | case OP_DIV: 127 | case OP_POW: 128 | case OP_EQ: 129 | case OP_LT: 130 | case OP_LE: 131 | if (ISK(b) || ISK(c)) 132 | { 133 | printf("\t; "); 134 | if (ISK(b)) PrintConstant(f,INDEXK(b)); else printf("-"); 135 | printf(" "); 136 | if (ISK(c)) PrintConstant(f,INDEXK(c)); else printf("-"); 137 | } 138 | break; 139 | case OP_JMP: 140 | case OP_FORLOOP: 141 | case OP_FORPREP: 142 | printf("\t; to %d",sbx+pc+2); 143 | break; 144 | case OP_CLOSURE: 145 | printf("\t; %p",VOID(f->p[bx])); 146 | break; 147 | case OP_SETLIST: 148 | if (c==0) printf("\t; %d",(int)code[++pc]); 149 | else printf("\t; %d",c); 150 | break; 151 | default: 152 | break; 153 | } 154 | printf("\n"); 155 | } 156 | } 157 | 158 | #define SS(x) (x==1)?"":"s" 159 | #define S(x) x,SS(x) 160 | 161 | static void PrintHeader(const Proto* f) 162 | { 163 | const char* s=getstr(f->source); 164 | if (*s=='@' || *s=='=') 165 | s++; 166 | else if (*s==LUA_SIGNATURE[0]) 167 | s="(bstring)"; 168 | else 169 | s="(string)"; 170 | printf("\n%s <%s:%d,%d> (%d instruction%s, %d bytes at %p)\n", 171 | (f->linedefined==0)?"main":"function",s, 172 | f->linedefined,f->lastlinedefined, 173 | S(f->sizecode),f->sizecode*Sizeof(Instruction),VOID(f)); 174 | printf("%d%s param%s, %d slot%s, %d upvalue%s, ", 175 | f->numparams,f->is_vararg?"+":"",SS(f->numparams), 176 | S(f->maxstacksize),S(f->nups)); 177 | printf("%d local%s, %d constant%s, %d function%s\n", 178 | S(f->sizelocvars),S(f->sizek),S(f->sizep)); 179 | } 180 | 181 | static void PrintConstants(const Proto* f) 182 | { 183 | int i,n=f->sizek; 184 | printf("constants (%d) for %p:\n",n,VOID(f)); 185 | for (i=0; isizelocvars; 196 | printf("locals (%d) for %p:\n",n,VOID(f)); 197 | for (i=0; ilocvars[i].varname),f->locvars[i].startpc+1,f->locvars[i].endpc+1); 201 | } 202 | } 203 | 204 | static void PrintUpvalues(const Proto* f) 205 | { 206 | int i,n=f->sizeupvalues; 207 | printf("upvalues (%d) for %p:\n",n,VOID(f)); 208 | if (f->upvalues==NULL) return; 209 | for (i=0; iupvalues[i])); 212 | } 213 | } 214 | 215 | void PrintFunction(const Proto* f, int full) 216 | { 217 | int i,n=f->sizep; 218 | PrintHeader(f); 219 | PrintCode(f); 220 | if (full) 221 | { 222 | PrintConstants(f); 223 | PrintLocals(f); 224 | PrintUpvalues(f); 225 | } 226 | for (i=0; ip[i],full); 227 | } 228 | -------------------------------------------------------------------------------- /lua/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 | xd.lua hex dump 26 | 27 | -------------------------------------------------------------------------------- /lua/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 | -------------------------------------------------------------------------------- /lua/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 | -------------------------------------------------------------------------------- /lua/test/xd.lua: -------------------------------------------------------------------------------- 1 | -- hex dump 2 | -- usage: lua xd.lua < file 3 | 4 | local offset=0 5 | while true do 6 | local s=io.read(16) 7 | if s==nil then return end 8 | io.write(string.format("%08X ",offset)) 9 | string.gsub(s,"(.)", 10 | 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 | --------------------------------------------------------------------------------