├── .gitignore ├── AUTHORS ├── CMakeLists.txt ├── COPYRIGHT ├── HISTORY ├── INSTALL ├── README ├── README.lua ├── TODO ├── cmake └── CustomMacros.cmake ├── cmake_uninstall.cmake.in ├── compiler ├── CMakeLists.txt ├── COPYRIGHT.slua ├── TODO ├── bin2c.c ├── build_config.h.cmake ├── codegen.c ├── codegen.h ├── compile_all.sh ├── hook_parser.c ├── load_jit_proto.c ├── load_jit_proto.h ├── lua_compiler.c ├── lua_compiler.h ├── lua_core.c ├── lua_core.h ├── lua_interpreter.c ├── lua_interpreter.h ├── lua_main.c ├── lua_normal.c ├── lua_vm_ops.c ├── lua_vm_ops.h ├── lua_vm_ops_static.c ├── run_tests.sh ├── slua-compiler.in ├── slua-compiler_mingw.in ├── slua.c ├── slua_compiler.c ├── slua_compiler.h ├── slua_dumper.c ├── slua_dumper.h ├── slua_lmathlib.c ├── sluac.c └── tests │ ├── NOTES │ ├── add.lua │ ├── arg_test.lua │ ├── big_table.lua │ ├── coroutine.lua │ ├── dump.lua │ ├── for.lua │ ├── hash2.lua │ ├── loadk.lua │ ├── local_nil.lua │ ├── loops.lua │ ├── lua_tail.lua │ ├── nestedloop.lua │ ├── nestedloop2.lua │ ├── nums.lua │ ├── scimark_loop.lua │ ├── scimark_rand.lua │ ├── stress_for.lua │ ├── test.lua │ ├── test2.lua │ ├── test3.lua │ ├── test4.lua │ ├── test_lineerror.lua │ ├── test_math.lua │ ├── test_tail.lua │ ├── test_tail_nil_multret.lua │ └── test_varg_tail2.lua ├── doc ├── amazon.gif ├── contents.html ├── cover.png ├── logo.gif ├── lua.1 ├── lua.css ├── lua.html ├── luac.1 ├── luac.html ├── manual.css ├── manual.html └── readme.html ├── etc ├── README ├── all.c ├── embed_jit.c ├── embed_jit.cpp ├── lua.hpp ├── lua.ico ├── lua.pc ├── luavs.bat ├── min.c ├── noparser.c └── strict.lua ├── src ├── lapi.c ├── lapi.h ├── lauxlib.c ├── lauxlib.h ├── lbaselib.c ├── lcoco.c ├── lcoco.h ├── 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 | build 2 | compiler/*_c.h 3 | src/.libs 4 | src/*.o 5 | src/*.lo 6 | src/*.la 7 | src/*.a 8 | src/lua 9 | src/luac 10 | src/lua_test 11 | .*.swp 12 | -------------------------------------------------------------------------------- /AUTHORS: -------------------------------------------------------------------------------- 1 | bobby@neoawareness.com:Robert G. Jakabosky 2 | -------------------------------------------------------------------------------- /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-2008 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 | -------------------------------------------------------------------------------- /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 | -------------------------------------------------------------------------------- /README: -------------------------------------------------------------------------------- 1 | README for slua 2 | 3 | === Requires === 4 | 5 | === Compile === 6 | * mkdir build 7 | * cd build 8 | for Release build: 9 | * cmake .. -DCMAKE_BUILD_TYPE=Release 10 | for Debug build: 11 | * cmake .. -DCMAKE_BUILD_TYPE=Debug 12 | * make 13 | 14 | === Install === 15 | * make install 16 | 17 | === Patches to lua/src === 18 | * Emergency Garbage Collector: http://lua-users.org/wiki/EmergencyGarbageCollector 19 | * LuaCoco-1.1.6: http://luajit.org/coco.html + (x86_64 support added) 20 | * a few hooks where added to support static compiled functions. 21 | 22 | === Programs === 23 | * slua: This command can be used to run Lua script. It doesn't have JIT support! 24 | * sluac: This command compiles Lua scripts into C code which calls lua vm ops functions. 25 | * slua-compiler: This is a bash script that wraps sluac to compile Lua scripts into standalone executables or loadable modules. 26 | 27 | === Libraries === 28 | -- static & dynamic libraries for linking into host app. 29 | * libslua_static.a & libslua.so 30 | 31 | -- Used for compling Lua scripts to standalone executables. 32 | * liblua_main.a 33 | 34 | === Using slua === 35 | 'sluac' alone can only compile Lua scripts to Lua bytecode or C code. A wrapper script called 'slua-compiler' is provided that wraps 'sluac' and gcc. 36 | 37 | Compile standalone Lua script: 38 | slua-compiler script.lua 39 | outputs: ./script 40 | 41 | Compile Lua script as a module: 42 | slua-compiler -lua-module script.lua 43 | outputs: ./script.so 44 | 45 | -------------------------------------------------------------------------------- /README.lua: -------------------------------------------------------------------------------- 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 | -------------------------------------------------------------------------------- /TODO: -------------------------------------------------------------------------------- 1 | TODO: 2 | * handle coroutine c-stack alignment issues on x86_64 when compiling in DEBUG mode. 3 | * Add option to compile liblua_main.a without lua parser, lundump/ldump code. 4 | 5 | lua-compiler script: 6 | * fix linking bug reported by DigitalKiwi: 7 | #CFLAGS=" -ggdb -O3 -fomit-frame-pointer -pipe -Wall " 8 | CFLAGS=" -O3 -fomit-frame-pointer -pipe -Wl,-E " 9 | http://www.tecgraf.puc-rio.br/~lhf/ftp/lua/install.html 10 | 11 | -------------------------------------------------------------------------------- /cmake/CustomMacros.cmake: -------------------------------------------------------------------------------- 1 | 2 | macro(add_target_properties _target _name) 3 | set(_properties) 4 | foreach(_prop ${ARGN}) 5 | set(_properties "${_properties} ${_prop}") 6 | endforeach(_prop) 7 | get_target_property(_old_properties ${_target} ${_name}) 8 | if(NOT _old_properties) 9 | # in case it's NOTFOUND 10 | set(_old_properties) 11 | endif(NOT _old_properties) 12 | set_target_properties(${_target} PROPERTIES ${_name} "${_old_properties} ${_properties}") 13 | endmacro(add_target_properties) 14 | 15 | -------------------------------------------------------------------------------- /cmake_uninstall.cmake.in: -------------------------------------------------------------------------------- 1 | IF(NOT EXISTS "@CMAKE_BINARY_DIR@/install_manifest.txt") 2 | MESSAGE(FATAL_ERROR "Cannot find install manifest: \"@CMAKE_BINARY_DIR@/install_manifest.txt\"") 3 | ENDIF(NOT EXISTS "@CMAKE_BINARY_DIR@/install_manifest.txt") 4 | 5 | FILE(READ "@CMAKE_BINARY_DIR@/install_manifest.txt" files) 6 | STRING(REGEX REPLACE "\n" ";" files "${files}") 7 | FOREACH(file ${files}) 8 | MESSAGE(STATUS "Uninstalling \"$ENV{DESTDIR}${file}\"") 9 | EXEC_PROGRAM( 10 | "@CMAKE_COMMAND@" ARGS "-E remove \"$ENV{DESTDIR}${file}\"" 11 | OUTPUT_VARIABLE rm_out 12 | RETURN_VALUE rm_retval 13 | ) 14 | IF(NOT "${rm_retval}" STREQUAL 0) 15 | MESSAGE(FATAL_ERROR "Problem when removing \"$ENV{DESTDIR}${file}\"") 16 | ENDIF(NOT "${rm_retval}" STREQUAL 0) 17 | ENDFOREACH(file) 18 | 19 | -------------------------------------------------------------------------------- /compiler/COPYRIGHT.slua: -------------------------------------------------------------------------------- 1 | Copyright (c) 2008 Robert G. Jakabosky 2 | 3 | Permission is hereby granted, free of charge, to any person obtaining a copy 4 | of this software and associated documentation files (the "Software"), to deal 5 | in the Software without restriction, including without limitation the rights 6 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 7 | copies of the Software, and to permit persons to whom the Software is 8 | furnished to do so, subject to the following conditions: 9 | 10 | The above copyright notice and this permission notice shall be included in 11 | all copies or substantial portions of the Software. 12 | 13 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 14 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 15 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 16 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 17 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 18 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 19 | THE SOFTWARE. 20 | 21 | MIT License: http://www.opensource.org/licenses/mit-license.php 22 | 23 | -------------------------------------------------------------------------------- /compiler/TODO: -------------------------------------------------------------------------------- 1 | 2 | optimizations: 3 | * When the internal index/limit/step variable of a OP_FORLOOP get moved to the C-Stack the space on the Lua-stack is unused. We should be able to decrease the maxstacksize by re-using those stack slots. 4 | * Try to inline some table/global lookups if they have a constant key. 5 | 6 | -------------------------------------------------------------------------------- /compiler/bin2c.c: -------------------------------------------------------------------------------- 1 | // bin2c.c 2 | // 3 | // convert a binary file into a C source vector 4 | // 5 | // THE "BEER-WARE LICENSE" (Revision 3.1415): 6 | // sandro AT sigala DOT it wrote this file. As long as you retain this notice you can do 7 | // whatever you want with this stuff. If we meet some day, and you think this stuff is 8 | // worth it, you can buy me a beer in return. Sandro Sigala 9 | // 10 | // syntax: bin2c [-c] [-z] 11 | // 12 | // -c add the "const" keyword to definition 13 | // -z terminate the array with a zero (useful for embedded C strings) 14 | // 15 | // examples: 16 | // bin2c -c myimage.png myimage_png.cpp 17 | // bin2c -z sometext.txt sometext_txt.cpp 18 | 19 | #include 20 | #include 21 | #include 22 | #include 23 | 24 | #ifndef PATH_MAX 25 | #define PATH_MAX 1024 26 | #endif 27 | 28 | int useconst = 0; 29 | int zeroterminated = 0; 30 | 31 | int myfgetc(FILE *f) 32 | { 33 | int c = fgetc(f); 34 | if (c == EOF && zeroterminated) 35 | { 36 | zeroterminated = 0; 37 | return 0; 38 | } 39 | return c; 40 | } 41 | 42 | void process(const char *ifname, const char *ofname) 43 | { 44 | FILE *ifile, *ofile; 45 | ifile = fopen(ifname, "rb"); 46 | if (ifile == NULL) 47 | { 48 | fprintf(stderr, "cannot open %s for reading\n", ifname); 49 | exit(1); 50 | } 51 | ofile = fopen(ofname, "wb"); 52 | if (ofile == NULL) 53 | { 54 | fprintf(stderr, "cannot open %s for writing\n", ofname); 55 | exit(1); 56 | } 57 | char buf[PATH_MAX], *p; 58 | const char *cp; 59 | if ((cp = strrchr(ifname, '/')) != NULL) 60 | { 61 | ++cp; 62 | } else { 63 | if ((cp = strrchr(ifname, '\\')) != NULL) 64 | ++cp; 65 | else 66 | cp = ifname; 67 | } 68 | strcpy(buf, cp); 69 | for (p = buf; *p != '\0'; ++p) 70 | { 71 | if (!isalnum(*p)) 72 | *p = '_'; 73 | } 74 | fprintf(ofile, "static %sunsigned char %s[] = {\n", useconst ? "const " : "", buf); 75 | int c, col = 1; 76 | while ((c = myfgetc(ifile)) != EOF) 77 | { 78 | if (col >= 78 - 6) 79 | { 80 | fputc('\n', ofile); 81 | col = 1; 82 | } 83 | fprintf(ofile, "0x%.2x,", c); 84 | col += 6; 85 | } 86 | fprintf(ofile, "\n};\n"); 87 | 88 | fclose(ifile); 89 | fclose(ofile); 90 | } 91 | 92 | void usage(void) 93 | { 94 | fprintf(stderr, "usage: bin2c [-cz] \n"); 95 | exit(1); 96 | } 97 | 98 | int main(int argc, char **argv) 99 | { 100 | while (argc > 3) 101 | { 102 | if (!strcmp(argv[1], "-c")) 103 | { 104 | useconst = 1; 105 | --argc; 106 | ++argv; 107 | } else if (!strcmp(argv[1], "-z")) 108 | { 109 | zeroterminated = 1; 110 | --argc; 111 | ++argv; 112 | } else { 113 | usage(); 114 | } 115 | } 116 | if (argc != 3) 117 | { 118 | usage(); 119 | } 120 | process(argv[1], argv[2]); 121 | return 0; 122 | } 123 | 124 | -------------------------------------------------------------------------------- /compiler/build_config.h.cmake: -------------------------------------------------------------------------------- 1 | /* 2 | * CMake autogenerated build_config.h file. 3 | * Make changes to build_config.h.cmake instead of this file. 4 | */ 5 | 6 | /* 7 | Copyright (c) 2010 Robert G. Jakabosky 8 | 9 | Permission is hereby granted, free of charge, to any person obtaining a copy 10 | of this software and associated documentation files (the "Software"), to deal 11 | in the Software without restriction, including without limitation the rights 12 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 13 | copies of the Software, and to permit persons to whom the Software is 14 | furnished to do so, subject to the following conditions: 15 | 16 | The above copyright notice and this permission notice shall be included in 17 | all copies or substantial portions of the Software. 18 | 19 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 20 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 21 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 22 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 23 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 24 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 25 | THE SOFTWARE. 26 | 27 | MIT License: http://www.opensource.org/licenses/mit-license.php 28 | */ 29 | 30 | #ifndef __SLUA_BUILD_CONFIG_H__ 31 | #define __SLUA_BUILD_CONFIG_H__ 32 | 33 | /* lua config */ 34 | #cmakedefine LUA_BUILD_AS_DLL 35 | #cmakedefine LUA_USE_MACOSX 36 | #cmakedefine LUA_USE_DLOPEN 37 | #cmakedefine LUA_USE_MKSTEMP 38 | #cmakedefine LUA_USE_ISATTY 39 | #cmakedefine LUA_USE_POPEN 40 | #cmakedefine LUA_USE_ULONGJMP 41 | #cmakedefine LUA_USE_APICHECK 42 | #cmakedefine LUA_ANSI 43 | #cmakedefine LUA_USE_READLINE 44 | #cmakedefine LUA_CPP_SUPPORT 45 | 46 | /* Coco config. */ 47 | #cmakedefine COCO_DISABLE 48 | #cmakedefine COCO_USE_SETJMP 49 | #cmakedefine COCO_USE_UCONTEXT 50 | 51 | #ifndef _WIN32 52 | #cmakedefine COCO_DEFAULT_CSTACKSIZE 53 | #ifdef COCO_DEFAULT_CSTACKSIZE 54 | #undef COCO_DEFAULT_CSTACKSIZE 55 | #define COCO_DEFAULT_CSTACKSIZE @COCO_DEFAULT_CSTACKSIZE@ 56 | #endif 57 | #endif 58 | 59 | #endif /* __SLUA_BUILD_CONFIG_H__ */ 60 | -------------------------------------------------------------------------------- /compiler/compile_all.sh: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env bash 2 | # 3 | 4 | OPTS="" 5 | FILES="" 6 | # parse command line parameters. 7 | for arg in "$@" ; do 8 | case "$arg" in 9 | -*) OPTS="$OPTS $arg" ;; 10 | *) FILES="$FILES $arg" ;; 11 | esac 12 | done 13 | 14 | for script in $FILES; do 15 | echo "Compiling script: $script" 16 | slua-compiler $OPTS $script 17 | #./slua-compiler $OPTS $script 18 | done 19 | 20 | -------------------------------------------------------------------------------- /compiler/hook_parser.c: -------------------------------------------------------------------------------- 1 | /* 2 | ** See Copyright Notice in lua.h 3 | */ 4 | 5 | /* 6 | * hook_parser.c - Add a hook to the parser in ldo.c 7 | * 8 | * Most of this code is from ldo.c 9 | */ 10 | 11 | #if !ENABLE_PARSER_HOOK 12 | 13 | #include "ldo.c" 14 | 15 | int slua_precall_lua (lua_State *L, StkId func, int nresults) { 16 | return luaD_precall_lua(L, func, nresults); 17 | } 18 | 19 | #else 20 | 21 | #include "slua_compiler.h" 22 | 23 | #include "ldo.c" 24 | 25 | int slua_precall_jit (lua_State *L, StkId func, int nresults) { 26 | Closure *cl; 27 | ptrdiff_t funcr; 28 | CallInfo *ci; 29 | StkId st, base; 30 | Proto *p; 31 | 32 | funcr = savestack(L, func); 33 | cl = clvalue(func); 34 | p = cl->l.p; 35 | luaD_checkstack(L, p->maxstacksize); 36 | func = restorestack(L, funcr); 37 | base = func + 1; 38 | if (L->top > base + p->numparams) 39 | L->top = base + p->numparams; 40 | ci = L->ci; /* now `enter' new function */ 41 | ci->func = func; 42 | L->base = ci->base = base; 43 | ci->top = L->base + p->maxstacksize; 44 | lua_assert(ci->top <= L->stack_last); 45 | L->savedpc = p->code; /* starting point */ 46 | ci->nresults = nresults; 47 | for (st = L->top; st < ci->top; st++) 48 | setnilvalue(st); 49 | L->top = ci->top; 50 | if (L->hookmask & LUA_MASKCALL) { 51 | L->savedpc++; /* hooks assume 'pc' is already incremented */ 52 | luaD_callhook(L, LUA_HOOKCALL, -1); 53 | L->savedpc--; /* correct 'pc' */ 54 | } 55 | return (p->jit_func)(L); /* do the actual call */ 56 | } 57 | 58 | int slua_precall_jit_vararg (lua_State *L, StkId func, int nresults) { 59 | Closure *cl; 60 | ptrdiff_t funcr; 61 | CallInfo *ci; 62 | StkId st, base; 63 | Proto *p; 64 | int nargs; 65 | 66 | funcr = savestack(L, func); 67 | cl = clvalue(func); 68 | p = cl->l.p; 69 | luaD_checkstack(L, p->maxstacksize); 70 | func = restorestack(L, funcr); 71 | nargs = cast_int(L->top - func) - 1; 72 | base = adjust_varargs(L, p, nargs); 73 | func = restorestack(L, funcr); /* previous call may change the stack */ 74 | ci = L->ci; /* now `enter' new function */ 75 | ci->func = func; 76 | L->base = ci->base = base; 77 | ci->top = L->base + p->maxstacksize; 78 | lua_assert(ci->top <= L->stack_last); 79 | L->savedpc = p->code; /* starting point */ 80 | ci->nresults = nresults; 81 | for (st = L->top; st < ci->top; st++) 82 | setnilvalue(st); 83 | L->top = ci->top; 84 | if (L->hookmask & LUA_MASKCALL) { 85 | L->savedpc++; /* hooks assume 'pc' is already incremented */ 86 | luaD_callhook(L, LUA_HOOKCALL, -1); 87 | L->savedpc--; /* correct 'pc' */ 88 | } 89 | return (p->jit_func)(L); /* do the actual call */ 90 | } 91 | 92 | int slua_precall_lua (lua_State *L, StkId func, int nresults) { 93 | Closure *cl; 94 | Proto *p; 95 | 96 | cl = clvalue(func); 97 | p = cl->l.p; 98 | if(p->jit_func != NULL) { 99 | if (!p->is_vararg) { /* no varargs? */ 100 | cl->l.precall = slua_precall_jit; 101 | return slua_precall_jit(L, func, nresults); 102 | } else { 103 | cl->l.precall = slua_precall_jit_vararg; 104 | return slua_precall_jit_vararg(L, func, nresults); 105 | } 106 | } 107 | /* function didn't compile, fall-back to lua interpreter */ 108 | cl->l.precall = luaD_precall_lua; 109 | return luaD_precall_lua(L, func, nresults); 110 | } 111 | 112 | #endif 113 | 114 | -------------------------------------------------------------------------------- /compiler/load_jit_proto.c: -------------------------------------------------------------------------------- 1 | /* 2 | 3 | Copyright (c) 2009 Robert G. Jakabosky 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in 13 | all copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 21 | THE SOFTWARE. 22 | 23 | MIT License: http://www.opensource.org/licenses/mit-license.php 24 | */ 25 | 26 | #include 27 | #include "lua_core.h" 28 | #include "lobject.h" 29 | #include "lfunc.h" 30 | #include "ldo.h" 31 | #include "lstring.h" 32 | #include "lmem.h" 33 | #include "load_jit_proto.h" 34 | #include "lundump.h" 35 | 36 | #define DUMP_PROTOS 0 37 | #if DUMP_PROTOS 38 | #include 39 | 40 | #define luaU_print luaU_dump_proto 41 | #include "print.c" 42 | #endif 43 | 44 | Proto *load_jit_proto(lua_State *L, jit_proto *p) { 45 | Proto *f = luaF_newproto(L); 46 | unsigned int i; 47 | 48 | /* proto source */ 49 | f->source = luaS_new(L, p->name); 50 | /* jit_func */ 51 | f->jit_func = p->jit_func; 52 | /* linedefined */ 53 | f->linedefined = p->linedefined; 54 | /* lastlinedefined */ 55 | f->lastlinedefined = p->lastlinedefined; 56 | /* nups */ 57 | f->nups = p->nups; 58 | /* numparams */ 59 | f->numparams = p->numparams; 60 | /* is_vararg */ 61 | f->is_vararg = p->is_vararg; 62 | /* maxstacksize */ 63 | f->maxstacksize = p->maxstacksize; 64 | /* sizek */ 65 | f->sizek = p->sizek; 66 | /* k */ 67 | f->k=luaM_newvector(L,p->sizek,TValue); 68 | for(i = 0; i < p->sizek; i++) { 69 | TValue *o=&f->k[i]; 70 | switch(p->k[i].type) { 71 | case TYPE_STRING: 72 | setsvalue2n(L,o, luaS_newlstr(L, p->k[i].val.str, p->k[i].length)); 73 | break; 74 | case TYPE_BOOLEAN: 75 | setbvalue(o, p->k[i].val.b != 0); 76 | break; 77 | case TYPE_NUMBER: 78 | setnvalue(o, p->k[i].val.num); 79 | break; 80 | case TYPE_NIL: 81 | default: 82 | setnilvalue(o); 83 | break; 84 | } 85 | } 86 | /* sizep */ 87 | f->sizep = p->sizep; 88 | /* p */ 89 | f->p=luaM_newvector(L,(size_t)p->sizep,Proto*); 90 | for(i = 0; i < p->sizep; i++) { 91 | f->p[i] = load_jit_proto(L, &(p->p[i])); 92 | } 93 | /* sizecode */ 94 | f->sizecode = p->sizecode; 95 | /* code */ 96 | f->code=luaM_newvector(L,(size_t)p->sizecode,Instruction); 97 | for(i = 0; i < p->sizecode; i++) { 98 | f->code[i] = p->code[i]; 99 | } 100 | /* sizelineinfo */ 101 | f->sizelineinfo = p->sizelineinfo; 102 | /* lineinfo */ 103 | f->lineinfo=luaM_newvector(L,(size_t)p->sizelineinfo,int); 104 | for(i = 0; i < p->sizelineinfo; i++) { 105 | f->lineinfo[i] = p->lineinfo[i]; 106 | } 107 | /* sizelocvars */ 108 | f->sizelocvars = p->sizelocvars; 109 | /* locvars */ 110 | f->locvars=luaM_newvector(L,p->sizelocvars,LocVar); 111 | for(i = 0; i < p->sizelocvars; i++) { 112 | jit_LocVar *locvar = &(p->locvars[i]); 113 | f->locvars[i].varname = luaS_new(L, locvar->varname); 114 | f->locvars[i].startpc = locvar->startpc; 115 | f->locvars[i].endpc = locvar->endpc; 116 | } 117 | /* sizeupvalues */ 118 | f->sizeupvalues = p->sizeupvalues; 119 | /* upvalues */ 120 | f->upvalues=luaM_newvector(L,(size_t)p->sizeupvalues,TString*); 121 | for(i = 0; i < p->sizeupvalues; i++) { 122 | f->upvalues[i] = luaS_new(L, p->upvalues[i]); 123 | } 124 | return f; 125 | } 126 | 127 | LUALIB_API int load_compiled_protos(lua_State *L, jit_proto *p) { 128 | Closure *cl; 129 | Proto *tf; 130 | int i; 131 | 132 | // load compiled lua code. 133 | luaC_checkGC(L); 134 | set_block_gc(L); /* stop collector during jit function loading. */ 135 | tf = load_jit_proto(L, p); 136 | #if DUMP_PROTOS 137 | luaU_dump_proto(tf,2); 138 | #endif 139 | cl = luaF_newLclosure(L, tf->nups, hvalue(gt(L))); 140 | cl->l.p = tf; 141 | for (i = 0; i < tf->nups; i++) /* initialize eventual upvalues */ 142 | cl->l.upvals[i] = luaF_newupval(L); 143 | setclvalue(L, L->top, cl); 144 | incr_top(L); 145 | unset_block_gc(L); 146 | 147 | return 0; 148 | } 149 | 150 | 151 | LUALIB_API int load_compiled_module(lua_State *L, jit_proto *p) { 152 | // load compiled lua code. 153 | load_compiled_protos(L, p); 154 | // run compiled lua code. 155 | lua_insert(L, -2); // pass our first parameter to the lua module. 156 | lua_call(L, 1, 1); 157 | 158 | return 1; 159 | } 160 | 161 | -------------------------------------------------------------------------------- /compiler/load_jit_proto.h: -------------------------------------------------------------------------------- 1 | /* 2 | load_jit_proto.h -- load jit proto 3 | 4 | Copyright (c) 2009 Robert G. Jakabosky 5 | 6 | Permission is hereby granted, free of charge, to any person obtaining a copy 7 | of this software and associated documentation files (the "Software"), to deal 8 | in the Software without restriction, including without limitation the rights 9 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 10 | copies of the Software, and to permit persons to whom the Software is 11 | furnished to do so, subject to the following conditions: 12 | 13 | The above copyright notice and this permission notice shall be included in 14 | all copies or substantial portions of the Software. 15 | 16 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 17 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 18 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 19 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 20 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 21 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 22 | THE SOFTWARE. 23 | 24 | MIT License: http://www.opensource.org/licenses/mit-license.php 25 | */ 26 | 27 | #ifndef load_jit_proto_h 28 | #define load_jit_proto_h 29 | 30 | #include "lobject.h" 31 | #include 32 | 33 | #define TYPE_NIL 0 34 | #define TYPE_NUMBER 1 35 | #define TYPE_BOOLEAN 2 36 | #define TYPE_STRING 3 37 | 38 | typedef union constant_value { 39 | /* nil doesn't need a value. */ 40 | uint32_t b; /* Lua boolean */ 41 | LUA_NUMBER num; /* Lua numbers */ 42 | char *str; /* Lua string. */ 43 | } constant_value; 44 | 45 | typedef struct constant_type { 46 | uint32_t type; /* constant type. */ 47 | uint32_t length; /* string length */ 48 | constant_value val; /* value of Lua nil/boolean/number/string. */ 49 | } constant_type; 50 | 51 | /* simplified version of LocVar struct. */ 52 | typedef struct jit_LocVar { 53 | char *varname; 54 | uint32_t startpc; 55 | uint32_t endpc; 56 | } jit_LocVar; 57 | 58 | /* simplified version of Proto struct. */ 59 | typedef struct jit_proto jit_proto; 60 | struct jit_proto { 61 | char *name; 62 | lua_CFunction jit_func; 63 | uint32_t linedefined; 64 | uint32_t lastlinedefined; 65 | uint8_t nups; 66 | uint8_t numparams; 67 | uint8_t is_vararg; 68 | uint8_t maxstacksize; 69 | uint16_t sizek; 70 | uint16_t sizelocvars; 71 | uint32_t sizeupvalues; 72 | uint32_t sizep; 73 | uint32_t sizecode; 74 | uint32_t sizelineinfo; 75 | constant_type *k; 76 | jit_LocVar *locvars; 77 | char **upvalues; 78 | jit_proto *p; 79 | uint32_t *code; 80 | uint32_t *lineinfo; 81 | }; 82 | 83 | Proto *load_jit_proto(lua_State *L, jit_proto *p); 84 | 85 | LUALIB_API int load_compiled_protos(lua_State *L, jit_proto *p); 86 | LUALIB_API int load_compiled_module(lua_State *L, jit_proto *p); 87 | 88 | extern jit_proto jit_proto_init; 89 | 90 | #endif 91 | -------------------------------------------------------------------------------- /compiler/lua_compiler.h: -------------------------------------------------------------------------------- 1 | 2 | #ifndef lua_compiler_h 3 | #define lua_compiler_h 4 | 5 | #include "lua_core.h" 6 | 7 | extern int luac_main(int argc, char **argv); 8 | 9 | #endif 10 | -------------------------------------------------------------------------------- /compiler/lua_core.c: -------------------------------------------------------------------------------- 1 | /* 2 | lua_core.c -- Lua core, libraries and JIT hooks compiled into a single file 3 | 4 | Copyright (c) 2009 Robert G. Jakabosky 5 | 6 | Permission is hereby granted, free of charge, to any person obtaining a copy 7 | of this software and associated documentation files (the "Software"), to deal 8 | in the Software without restriction, including without limitation the rights 9 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 10 | copies of the Software, and to permit persons to whom the Software is 11 | furnished to do so, subject to the following conditions: 12 | 13 | The above copyright notice and this permission notice shall be included in 14 | all copies or substantial portions of the Software. 15 | 16 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 17 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 18 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 19 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 20 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 21 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 22 | THE SOFTWARE. 23 | 24 | MIT License: http://www.opensource.org/licenses/mit-license.php 25 | */ 26 | 27 | #include "lua_core.h" 28 | #include "lobject.h" 29 | #include "slua_compiler.h" 30 | 31 | void slua_newproto (lua_State *L, Proto *f); 32 | 33 | /* functions */ 34 | #define JIT_NEW_STATE(L) 35 | #define JIT_CLOSE_STATE(L) 36 | #define JIT_NEWPROTO(L,f) slua_newproto(L,f) 37 | #define JIT_FREEPROTO(L,f) 38 | #define JIT_PRECALL slua_precall_lua 39 | 40 | #include "lapi.c" 41 | #include "lcode.c" 42 | #include "ldebug.c" 43 | #include "ldump.c" 44 | #include "lfunc.c" 45 | #include "lgc.c" 46 | #include "llex.c" 47 | #include "lmem.c" 48 | #include "lobject.c" 49 | #include "lopcodes.c" 50 | #include "lparser.c" 51 | #include "lstate.c" 52 | #include "lstring.c" 53 | #include "ltable.c" 54 | #include "ltm.c" 55 | #include "lundump.c" 56 | #include "lvm.c" 57 | #include "lzio.c" 58 | 59 | #include "lbaselib.c" 60 | #include "lcoco.c" 61 | #include "ldblib.c" 62 | #include "liolib.c" 63 | #include "linit.c" 64 | #include "slua_lmathlib.c" 65 | #include "loadlib.c" 66 | #include "loslib.c" 67 | #include "lstrlib.c" 68 | #include "ltablib.c" 69 | 70 | #include "lauxlib.c" 71 | 72 | void slua_newproto (lua_State *L, Proto *f) { 73 | (void)L; 74 | f->jit_func = NULL; 75 | } 76 | 77 | -------------------------------------------------------------------------------- /compiler/lua_core.h: -------------------------------------------------------------------------------- 1 | 2 | #ifndef lua_core_h 3 | #define lua_core_h 4 | 5 | #define lua_c 6 | #define loslib_c 7 | #define LUA_CORE 8 | 9 | /* Lua interpreter with LLVM JIT support. */ 10 | #define JIT_SUPPORT 11 | 12 | /* extra variables for global_State */ 13 | #define JIT_COMPILER_STATE 14 | 15 | /* state */ 16 | #define JIT_PROTO_STATE \ 17 | lua_CFunction jit_func; /* jit compiled function */ 18 | 19 | #include "build_config.h" 20 | #include 21 | /* extern all lua core functions. */ 22 | #undef LUAI_FUNC 23 | #define LUAI_FUNC LUA_API 24 | 25 | #endif 26 | 27 | -------------------------------------------------------------------------------- /compiler/lua_interpreter.c: -------------------------------------------------------------------------------- 1 | /* 2 | * lua_interpreter.c -- Lua interpreter 3 | */ 4 | 5 | #include "lua_core.h" 6 | #include "lua_interpreter.h" 7 | 8 | #define ENABLE_PARSER_HOOK 1 9 | #include "hook_parser.c" 10 | 11 | #define main lua_main 12 | #include "lua.c" 13 | #undef main 14 | 15 | -------------------------------------------------------------------------------- /compiler/lua_interpreter.h: -------------------------------------------------------------------------------- 1 | 2 | #ifndef lua_interpreter_h 3 | #define lua_interpreter_h 4 | 5 | extern int lua_main(int argc, char **argv); 6 | 7 | #endif 8 | -------------------------------------------------------------------------------- /compiler/lua_main.c: -------------------------------------------------------------------------------- 1 | /* 2 | ** $Id: lua.c,v 1.160.1.2 2007/12/28 15:32:23 roberto Exp $ 3 | ** Lua stand-alone interpreter 4 | ** See Copyright Notice in lua.h 5 | */ 6 | 7 | /* 8 | * Most of this file is from lua.c 9 | */ 10 | 11 | #include 12 | #include 13 | #include 14 | #include 15 | 16 | #include "lua_core.h" 17 | #include "lua_interpreter.h" 18 | 19 | #include "lua.h" 20 | 21 | #include "lauxlib.h" 22 | #include "lualib.h" 23 | 24 | #include "load_jit_proto.h" 25 | 26 | #define ENABLE_PARSER_HOOK 1 27 | #include "hook_parser.c" 28 | 29 | static lua_State *globalL = NULL; 30 | 31 | static const char *progname = LUA_PROGNAME; 32 | 33 | 34 | 35 | static void lstop (lua_State *L, lua_Debug *ar) { 36 | (void)ar; /* unused arg. */ 37 | lua_sethook(L, NULL, 0, 0); 38 | luaL_error(L, "interrupted!"); 39 | } 40 | 41 | 42 | static void laction (int i) { 43 | signal(i, SIG_DFL); /* if another SIGINT happens before lstop, 44 | terminate process (default action) */ 45 | lua_sethook(globalL, lstop, LUA_MASKCALL | LUA_MASKRET | LUA_MASKCOUNT, 1); 46 | } 47 | 48 | 49 | static void l_message (const char *pname, const char *msg) { 50 | if (pname) fprintf(stderr, "%s: ", pname); 51 | fprintf(stderr, "%s\n", msg); 52 | fflush(stderr); 53 | } 54 | 55 | 56 | static int report (lua_State *L, int status) { 57 | if (status && !lua_isnil(L, -1)) { 58 | const char *msg = lua_tostring(L, -1); 59 | if (msg == NULL) msg = "(error object is not a string)"; 60 | l_message(progname, msg); 61 | lua_pop(L, 1); 62 | } 63 | return status; 64 | } 65 | 66 | 67 | static int traceback (lua_State *L) { 68 | if (!lua_isstring(L, 1)) /* 'message' not a string? */ 69 | return 1; /* keep it intact */ 70 | lua_getfield(L, LUA_GLOBALSINDEX, "debug"); 71 | if (!lua_istable(L, -1)) { 72 | lua_pop(L, 1); 73 | return 1; 74 | } 75 | lua_getfield(L, -1, "traceback"); 76 | if (!lua_isfunction(L, -1)) { 77 | lua_pop(L, 2); 78 | return 1; 79 | } 80 | lua_pushvalue(L, 1); /* pass error message */ 81 | lua_pushinteger(L, 2); /* skip this function and traceback */ 82 | lua_call(L, 2, 1); /* call debug.traceback */ 83 | return 1; 84 | } 85 | 86 | 87 | static int docall (lua_State *L, int narg, int clear) { 88 | int status; 89 | int base = lua_gettop(L) - narg; /* function index */ 90 | lua_pushcfunction(L, traceback); /* push traceback function */ 91 | lua_insert(L, base); /* put it under chunk and args */ 92 | signal(SIGINT, laction); 93 | status = lua_pcall(L, narg, (clear ? 0 : LUA_MULTRET), base); 94 | signal(SIGINT, SIG_DFL); 95 | lua_remove(L, base); /* remove traceback function */ 96 | /* force a complete garbage collection in case of errors */ 97 | if (status != 0) lua_gc(L, LUA_GCCOLLECT, 0); 98 | return status; 99 | } 100 | 101 | 102 | static int getargs (lua_State *L, int argc, char **argv) { 103 | int i; 104 | luaL_checkstack(L, argc + 3, "too many arguments to script"); 105 | for (i=0; i < argc; i++) 106 | lua_pushstring(L, argv[i]); 107 | lua_createtable(L, argc, 0); 108 | for (i=0; i < argc; i++) { 109 | lua_pushstring(L, argv[i]); 110 | lua_rawseti(L, -2, i); 111 | } 112 | return argc; 113 | } 114 | 115 | 116 | static int dofile (lua_State *L, const char *name) { 117 | int status = luaL_loadfile(L, name) || docall(L, 0, 1); 118 | return report(L, status); 119 | } 120 | 121 | 122 | static int dostring (lua_State *L, const char *s, const char *name) { 123 | int status = luaL_loadbuffer(L, s, strlen(s), name) || docall(L, 0, 1); 124 | return report(L, status); 125 | } 126 | 127 | static int handle_script (lua_State *L, int argc, char **argv) { 128 | int status; 129 | int narg = getargs(L, argc, argv); /* collect arguments */ 130 | lua_setglobal(L, "arg"); 131 | status = load_compiled_protos(L, &jit_proto_init); 132 | lua_insert(L, -(narg+1)); 133 | if (status == 0) 134 | status = docall(L, narg, 0); 135 | else 136 | lua_pop(L, narg); 137 | return report(L, status); 138 | } 139 | 140 | 141 | /* check that argument has no extra characters at the end */ 142 | #define notail(x) {if ((x)[2] != '\0') return -1;} 143 | 144 | 145 | static int handle_luainit (lua_State *L) { 146 | const char *init = getenv(LUA_INIT); 147 | if (init == NULL) return 0; /* status OK */ 148 | else if (init[0] == '@') 149 | return dofile(L, init+1); 150 | else 151 | return dostring(L, init, "=" LUA_INIT); 152 | } 153 | 154 | 155 | struct Smain { 156 | int argc; 157 | char **argv; 158 | int status; 159 | }; 160 | 161 | 162 | static int pmain (lua_State *L) { 163 | struct Smain *s = (struct Smain *)lua_touserdata(L, 1); 164 | char **argv = s->argv; 165 | globalL = L; 166 | if (argv[0] && argv[0][0]) progname = argv[0]; 167 | lua_gc(L, LUA_GCSTOP, 0); /* stop collector during initialization */ 168 | luaL_openlibs(L); /* open libraries */ 169 | lua_gc(L, LUA_GCRESTART, 0); 170 | s->status = handle_luainit(L); 171 | if (s->status != 0) return 0; 172 | s->status = handle_script(L, s->argc, argv); 173 | if (s->status != 0) return 0; 174 | return 0; 175 | } 176 | 177 | 178 | int main (int argc, char **argv) { 179 | int status; 180 | struct Smain s; 181 | lua_State *L = lua_open(); /* create state */ 182 | if (L == NULL) { 183 | l_message(argv[0], "cannot create state: not enough memory"); 184 | return EXIT_FAILURE; 185 | } 186 | s.argc = argc; 187 | s.argv = argv; 188 | status = lua_cpcall(L, &pmain, &s); 189 | report(L, status); 190 | lua_close(L); 191 | return (status || s.status) ? EXIT_FAILURE : EXIT_SUCCESS; 192 | } 193 | 194 | -------------------------------------------------------------------------------- /compiler/lua_normal.c: -------------------------------------------------------------------------------- 1 | /* 2 | ** See Copyright Notice in lua.h 3 | */ 4 | 5 | /* 6 | * lua_normal.c -- Lua core, libraries and interpreter in a single file 7 | */ 8 | 9 | #define lua_normal_c 10 | 11 | #define luaall_c 12 | #define LUA_CORE 13 | #include "lapi.c" 14 | #include "lcode.c" 15 | #include "ldebug.c" 16 | #include "ldump.c" 17 | #include "lfunc.c" 18 | #include "lgc.c" 19 | #include "llex.c" 20 | #include "lmem.c" 21 | #include "lobject.c" 22 | #include "lopcodes.c" 23 | #include "lparser.c" 24 | #include "lstate.c" 25 | #include "lstring.c" 26 | #include "ltable.c" 27 | #include "ltm.c" 28 | #include "lundump.c" 29 | #include "lvm.c" 30 | #include "lzio.c" 31 | 32 | #include "lbaselib.c" 33 | #include "lcoco.c" 34 | #include "ldblib.c" 35 | #include "liolib.c" 36 | #include "linit.c" 37 | #include "lmathlib.c" 38 | #include "loadlib.c" 39 | #include "loslib.c" 40 | #include "lstrlib.c" 41 | #include "ltablib.c" 42 | 43 | #include "lauxlib.c" 44 | 45 | #include "ldo.c" 46 | #include "lua.c" 47 | 48 | -------------------------------------------------------------------------------- /compiler/run_tests.sh: -------------------------------------------------------------------------------- 1 | #!/bin/sh 2 | # 3 | 4 | for script in `ls tests/*.lua`; do 5 | echo "run test: $script" 6 | llvm-lua -g -O0 $script >/dev/null 7 | #llvm-lua -g -O0 $script 8 | #lua $script 9 | #llvm-lua -O3 $script >/dev/null 10 | done 11 | 12 | -------------------------------------------------------------------------------- /compiler/slua-compiler.in: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env bash 2 | # 3 | 4 | CC=gcc 5 | LIBTOOL="libtool --tag=CC --silent" 6 | RPATH=`pwd` 7 | SLUAC="./sluac" 8 | PREFIX="@CMAKE_INSTALL_PREFIX@" 9 | COMMON_LDFLAGS="@COMMON_LDFLAGS@" 10 | 11 | # find sluac 12 | if [[ ! -x "$SLUAC" ]]; then 13 | SLUAC=`which sluac` 14 | fi 15 | 16 | #CPU=i686 17 | #CPU=pentium4 18 | CPU=athlon64 19 | FORCE_CPU="0" 20 | FILE= 21 | FILES="" 22 | OUTPUT_FILE="" 23 | DEBUG="0" 24 | KEEP_TMPS="0" 25 | STATIC="0" 26 | MODE="standalone" 27 | EXTRA_ARGS= 28 | LIBS= 29 | 30 | function version() { 31 | $SLUAC -version 32 | exit 0; 33 | } 34 | 35 | function usage() { 36 | echo "USAGE: $0 [options]