├── .gitignore ├── Lua.hx ├── README.md ├── build.hxml ├── haxelib.json ├── include.xml ├── ndll ├── Android │ └── liblua.so ├── Linux │ └── lua.ndll ├── Linux64 │ └── lua.ndll ├── Mac │ └── lua.ndll ├── Mac64 │ └── lua.ndll ├── Windows │ └── lua.ndll ├── Windows64 │ └── lua.ndll └── iPhone │ ├── liblua.iphoneos-v7.a │ ├── liblua.iphoneos.a │ └── liblua.iphonesim.a ├── project ├── Build.xml ├── common │ └── ExternalInterface.cpp └── lua │ ├── LICENSE │ ├── lapi.c │ ├── lapi.h │ ├── lauxlib.c │ ├── lauxlib.h │ ├── lbaselib.c │ ├── lbitlib.c │ ├── lcode.c │ ├── lcode.h │ ├── lcorolib.c │ ├── lctype.c │ ├── lctype.h │ ├── ldblib.c │ ├── ldebug.c │ ├── ldebug.h │ ├── ldo.c │ ├── ldo.h │ ├── ldump.c │ ├── lfunc.c │ ├── lfunc.h │ ├── lgc.c │ ├── lgc.h │ ├── linit.c │ ├── liolib.c │ ├── llex.c │ ├── llex.h │ ├── llimits.h │ ├── lmathlib.c │ ├── lmem.c │ ├── lmem.h │ ├── loadlib.c │ ├── lobject.c │ ├── lobject.h │ ├── lopcodes.c │ ├── lopcodes.h │ ├── loslib.c │ ├── lparser.c │ ├── lparser.h │ ├── lstate.c │ ├── lstate.h │ ├── lstring.c │ ├── lstring.h │ ├── lstrlib.c │ ├── ltable.c │ ├── ltable.h │ ├── ltablib.c │ ├── ltm.c │ ├── ltm.h │ ├── lua.c │ ├── lua.h │ ├── lua.hpp │ ├── luac.c │ ├── luaconf.h │ ├── lualib.h │ ├── lundump.c │ ├── lundump.h │ ├── lvm.c │ ├── lvm.h │ ├── lzio.c │ └── lzio.h └── test ├── TestLua.hx └── test.lua /.gitignore: -------------------------------------------------------------------------------- 1 | *.n 2 | obj/ 3 | bin/ 4 | all_objs 5 | *.pdb 6 | 7 | .DS_Store 8 | Thumbs.db 9 | -------------------------------------------------------------------------------- /Lua.hx: -------------------------------------------------------------------------------- 1 | #if cpp 2 | import cpp.Lib; 3 | #elseif neko 4 | import neko.Lib; 5 | #end 6 | 7 | class Lua 8 | { 9 | 10 | /** 11 | * Creates a new lua vm state 12 | */ 13 | public function new() 14 | { 15 | handle = lua_create(); 16 | } 17 | 18 | /** 19 | * Get the version string from Lua 20 | */ 21 | public static var version(get, never):String; 22 | private inline static function get_version():String 23 | { 24 | return lua_get_version(); 25 | } 26 | 27 | /** 28 | * Loads lua libraries (base, debug, io, math, os, package, string, table) 29 | * @param libs An array of library names to load 30 | */ 31 | public function loadLibs(libs:Array):Void 32 | { 33 | lua_load_libs(handle, libs); 34 | } 35 | 36 | /** 37 | * Defines variables in the lua vars 38 | * @param vars An object defining the lua variables to create 39 | */ 40 | public function setVars(vars:Dynamic):Void 41 | { 42 | lua_load_context(handle, vars); 43 | } 44 | 45 | /** 46 | * Runs a lua script 47 | * @param script The lua script to run in a string 48 | * @return The result from the lua script in Haxe 49 | */ 50 | public function execute(script:String):Dynamic 51 | { 52 | return lua_execute(handle, script); 53 | } 54 | 55 | /** 56 | * Calls a previously loaded lua function 57 | * @param func The lua function name (globals only) 58 | * @param args A single argument or array of arguments 59 | */ 60 | public function call(func:String, args:Dynamic):Dynamic 61 | { 62 | return lua_call_function(handle, func, args); 63 | } 64 | 65 | /** 66 | * Convienient way to run a lua script in Haxe without loading any libraries 67 | * @param script The lua script to run in a string 68 | * @param vars An object defining the lua variables to create 69 | * @return The result from the lua script in Haxe 70 | */ 71 | public static function run(script:String, ?vars:Dynamic):Dynamic 72 | { 73 | var lua = new Lua(); 74 | lua.setVars(vars); 75 | return lua.execute(script); 76 | } 77 | 78 | private static function load(func:String, numArgs:Int):Dynamic 79 | { 80 | #if neko 81 | if (!moduleInit) 82 | { 83 | // initialize neko 84 | var init = Lib.load("lua", "neko_init", 5); 85 | if (init != null) 86 | { 87 | init(function(s) return new String(s), function(len:Int) { var r = []; if (len > 0) r[len - 1] = null; return r; }, null, true, false); 88 | } 89 | else 90 | { 91 | throw("Could not find NekoAPI interface."); 92 | } 93 | 94 | moduleInit = true; 95 | } 96 | #end 97 | 98 | return Lib.load("lua", func, numArgs); 99 | } 100 | 101 | private var handle:Dynamic; 102 | 103 | private static var lua_create = load("lua_create", 0); 104 | private static var lua_get_version = load("lua_get_version", 0); 105 | private static var lua_call_function = load("lua_call_function", 3); 106 | private static var lua_execute = load("lua_execute", 2); 107 | private static var lua_load_context = load("lua_load_context", 2); 108 | private static var lua_load_libs = load("lua_load_libs", 2); 109 | private static var moduleInit:Bool = false; 110 | 111 | } 112 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | Run Lua code in Haxe 2 | ==================== 3 | 4 | Run any Lua code inside Haxe on neko/cpp targets. Has the option of passing a context object that will set variables before running the script. 5 | 6 | ```haxe 7 | var result = Lua.run("return true"); // returns a Bool to Haxe 8 | ``` 9 | 10 | Pass in values with a context object. The key names are used as variable names in the Lua script. 11 | ```haxe 12 | var result = Lua.run("if num > 14 then return 14 else return num end", {num: 15.3}); 13 | ``` 14 | 15 | What if you want to call a function created in Haxe from Lua? Just pass the function in the context! 16 | ```haxe 17 | var result = Lua.run("return plus1(15)", { plus1: function(val:Int) { return val + 1; } }); 18 | if (result == 16) 19 | trace("success!"); 20 | ``` 21 | 22 | Lua instances 23 | ============= 24 | 25 | It's possible to create multiple Lua instances to run scripts with different contexts/libraries. 26 | 27 | ```haxe 28 | var lua = new Lua(); 29 | lua.loadLibs(["base", "math"]); 30 | lua.setVars({ myVar: 1 }); 31 | var result = lua.execute("return myVar"); 32 | ``` 33 | 34 | Calling Lua functions 35 | --------------------- 36 | 37 | Calling global functions defined in lua can be done after executing a chunk of Lua code. You can either pass in a single value (for single argument functions) or an array (for multiple argument functions). 38 | 39 | ```haxe 40 | var lua = new Lua(); 41 | lua.execute("function add(a, b) return a + b end"); 42 | var result = lua.call("add", [1, 5]); // returns 6 43 | ``` 44 | -------------------------------------------------------------------------------- /build.hxml: -------------------------------------------------------------------------------- 1 | -x TestLua 2 | -cp test 3 | -lib lua 4 | 5 | --next 6 | 7 | -main TestLua 8 | -cpp bin 9 | -cp test 10 | -lib lua 11 | -cmd bin/TestLua 12 | -------------------------------------------------------------------------------- /haxelib.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "lua", 3 | "url": "https://github.com/MattTuttle/hx-lua", 4 | "license": "MIT", 5 | "tags": ["lua", "extension"], 6 | "description": "Simple lua binding in a haxe extension", 7 | "version": "0.1.0", 8 | "releasenote": "Initial version", 9 | "contributors": ["heardtheword"] 10 | } 11 | -------------------------------------------------------------------------------- /include.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | -------------------------------------------------------------------------------- /ndll/Android/liblua.so: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/MattTuttle/hx-lua/f206c71bca773b5857495dd9aaa81b353c5b8909/ndll/Android/liblua.so -------------------------------------------------------------------------------- /ndll/Linux/lua.ndll: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/MattTuttle/hx-lua/f206c71bca773b5857495dd9aaa81b353c5b8909/ndll/Linux/lua.ndll -------------------------------------------------------------------------------- /ndll/Linux64/lua.ndll: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/MattTuttle/hx-lua/f206c71bca773b5857495dd9aaa81b353c5b8909/ndll/Linux64/lua.ndll -------------------------------------------------------------------------------- /ndll/Mac/lua.ndll: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/MattTuttle/hx-lua/f206c71bca773b5857495dd9aaa81b353c5b8909/ndll/Mac/lua.ndll -------------------------------------------------------------------------------- /ndll/Mac64/lua.ndll: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/MattTuttle/hx-lua/f206c71bca773b5857495dd9aaa81b353c5b8909/ndll/Mac64/lua.ndll -------------------------------------------------------------------------------- /ndll/Windows/lua.ndll: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/MattTuttle/hx-lua/f206c71bca773b5857495dd9aaa81b353c5b8909/ndll/Windows/lua.ndll -------------------------------------------------------------------------------- /ndll/Windows64/lua.ndll: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/MattTuttle/hx-lua/f206c71bca773b5857495dd9aaa81b353c5b8909/ndll/Windows64/lua.ndll -------------------------------------------------------------------------------- /ndll/iPhone/liblua.iphoneos-v7.a: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/MattTuttle/hx-lua/f206c71bca773b5857495dd9aaa81b353c5b8909/ndll/iPhone/liblua.iphoneos-v7.a -------------------------------------------------------------------------------- /ndll/iPhone/liblua.iphoneos.a: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/MattTuttle/hx-lua/f206c71bca773b5857495dd9aaa81b353c5b8909/ndll/iPhone/liblua.iphoneos.a -------------------------------------------------------------------------------- /ndll/iPhone/liblua.iphonesim.a: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/MattTuttle/hx-lua/f206c71bca773b5857495dd9aaa81b353c5b8909/ndll/iPhone/liblua.iphonesim.a -------------------------------------------------------------------------------- /project/Build.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | 30 | 31 | 32 | 33 | 34 | 35 | 36 | 37 | 38 | 39 | 40 | 41 | 42 | 43 | 44 | 45 | 46 | 47 | 48 | 49 | 50 | 51 | 52 | 53 | 54 | 55 | 56 | 57 | 58 | 59 | 60 | 61 | 62 | 63 | 64 | 65 | 66 | 67 | 68 | 69 | 70 | 71 | 72 | 73 | 74 | 75 | 76 | 77 | 78 | 79 | 80 | -------------------------------------------------------------------------------- /project/common/ExternalInterface.cpp: -------------------------------------------------------------------------------- 1 | #ifndef STATIC_LINK 2 | #define IMPLEMENT_API 3 | #endif 4 | 5 | #if defined(HX_WINDOWS) || defined(HX_MACOS) || defined(HX_LINUX) 6 | #define NEKO_COMPATIBLE 7 | #endif 8 | 9 | #include 10 | #include 11 | #include 12 | 13 | extern "C" { 14 | #include "lua.h" 15 | #include "lauxlib.h" 16 | #include "lualib.h" 17 | } 18 | 19 | vkind kind_lua_vm; 20 | 21 | // HACK!!! For some reason this isn't properly defined in neko... 22 | #if !(defined(IPHONE) || defined(ANDROID)) 23 | #define val_fun_nargs(v) ((vfunction*)(v))->nargs 24 | #endif 25 | 26 | // forward declarations 27 | int haxe_to_lua(value v, lua_State *l); 28 | value lua_value_to_haxe(lua_State *l, int lua_v); 29 | 30 | #define BEGIN_TABLE_LOOP(l, v) lua_pushnil(l); \ 31 | while (lua_next(l, v) != 0) { 32 | #define END_TABLE_LOOP(l) lua_pop(l, 1); } 33 | 34 | inline value lua_table_to_haxe(lua_State *l, int lua_v) 35 | { 36 | value v; 37 | int field_count = 0; 38 | bool array = true; 39 | 40 | // count the number of key/value pairs and figure out if it's an array or object 41 | BEGIN_TABLE_LOOP(l, lua_v) 42 | // check for all number keys (array), otherwise it's an object 43 | if (lua_type(l, -2) != LUA_TNUMBER) array = false; 44 | 45 | field_count += 1; 46 | END_TABLE_LOOP(l) 47 | 48 | if (array) 49 | { 50 | v = alloc_array(field_count); 51 | value *arr = val_array_value(v); 52 | BEGIN_TABLE_LOOP(l, lua_v) 53 | int index = (int)(lua_tonumber(l, -2) - 1); // lua has 1 based indices instead of 0 54 | arr[index] = lua_value_to_haxe(l, lua_v+2); 55 | END_TABLE_LOOP(l) 56 | } 57 | else 58 | { 59 | v = alloc_empty_object(); 60 | BEGIN_TABLE_LOOP(l, lua_v) 61 | // TODO: don't assume string keys 62 | const char *key = lua_tostring(l, -2); 63 | alloc_field(v, val_id(key), lua_value_to_haxe(l, lua_v+2)); 64 | END_TABLE_LOOP(l) 65 | } 66 | 67 | return v; 68 | } 69 | 70 | value lua_value_to_haxe(lua_State *l, int lua_v) 71 | { 72 | lua_Number n; 73 | value v; 74 | switch (lua_type(l, lua_v)) 75 | { 76 | case LUA_TNIL: 77 | v = alloc_null(); 78 | break; 79 | case LUA_TNUMBER: 80 | n = lua_tonumber(l, lua_v); 81 | // check if number is int or float 82 | v = (fmod(n, 1) == 0) ? alloc_int(n) : alloc_float(n); 83 | break; 84 | case LUA_TTABLE: 85 | v = lua_table_to_haxe(l, lua_v); 86 | break; 87 | case LUA_TSTRING: 88 | v = alloc_string(lua_tostring(l, lua_v)); 89 | break; 90 | case LUA_TBOOLEAN: 91 | v = alloc_bool(lua_toboolean(l, lua_v)); 92 | break; 93 | case LUA_TFUNCTION: 94 | case LUA_TUSERDATA: 95 | case LUA_TTHREAD: 96 | case LUA_TLIGHTUSERDATA: 97 | printf("return value not supported"); 98 | break; 99 | } 100 | return v; 101 | } 102 | 103 | static int haxe_callback(lua_State *l) 104 | { 105 | int num_args = lua_gettop(l); 106 | AutoGCRoot *root = (AutoGCRoot *)lua_topointer(l, lua_upvalueindex(1)); 107 | int expected_args = lua_tonumber(l, lua_upvalueindex(2)); 108 | if (num_args != expected_args) 109 | { 110 | printf("Expected %d arguments, received %d", expected_args, num_args); 111 | } 112 | else 113 | { 114 | value *args = new value[num_args]; 115 | for (int i = 0; i < num_args; ++i) 116 | { 117 | args[i] = lua_value_to_haxe(l, i + 1); 118 | } 119 | value result = val_callN(root->get(), args, num_args); 120 | delete [] args; 121 | return haxe_to_lua(result, l); 122 | } 123 | return 0; 124 | } 125 | 126 | inline void haxe_array_to_lua(value v, lua_State *l) 127 | { 128 | int size = val_array_size(v); 129 | value *arr = val_array_value(v); 130 | lua_createtable(l, size, 0); 131 | for (int i = 0; i < size; i++) 132 | { 133 | lua_pushnumber(l, i + 1); // lua index is 1 based instead of 0 134 | haxe_to_lua(arr[i], l); 135 | lua_settable(l, -3); 136 | } 137 | } 138 | 139 | void haxe_iter_object(value v, field f, void *state) 140 | { 141 | lua_State *l = (lua_State *)state; 142 | const char *name = val_string(val_field_name(f)); 143 | lua_pushstring(l, name); 144 | haxe_to_lua(v, l); 145 | lua_settable(l, -3); 146 | } 147 | 148 | void haxe_iter_global(value v, field f, void *state) 149 | { 150 | lua_State *l = (lua_State *)state; 151 | const char *name = val_string(val_field_name(f)); 152 | haxe_to_lua(v, l); 153 | lua_setglobal(l, name); 154 | } 155 | 156 | // convert haxe values to lua 157 | int haxe_to_lua(value v, lua_State *l) 158 | { 159 | switch (val_type(v)) 160 | { 161 | case valtNull: 162 | lua_pushnil(l); 163 | break; 164 | case valtBool: 165 | lua_pushboolean(l, val_bool(v)); 166 | break; 167 | case valtFloat: 168 | lua_pushnumber(l, val_float(v)); 169 | break; 170 | case valtInt: 171 | lua_pushnumber(l, val_int(v)); 172 | break; 173 | case valtString: 174 | lua_pushstring(l, val_string(v)); 175 | break; 176 | case valtFunction: 177 | // TODO: figure out a way to delete/cleanup the AutoGCRoot pointers 178 | lua_pushlightuserdata(l, new AutoGCRoot(v)); 179 | lua_pushnumber(l, val_fun_nargs(v)); 180 | // using a closure instead of a function so we can add upvalues 181 | lua_pushcclosure(l, haxe_callback, 2); 182 | break; 183 | case valtArray: 184 | haxe_array_to_lua(v, l); 185 | break; 186 | case valtAbstractBase: // should abstracts be handled?? 187 | printf("abstracts not supported"); 188 | return 0; 189 | case valtObject: // falls through 190 | case valtEnum: // falls through 191 | case valtClass: 192 | lua_newtable(l); 193 | val_iter_fields(v, haxe_iter_object, l); 194 | break; 195 | } 196 | return 1; 197 | } 198 | 199 | static lua_State *lua_from_handle(value inHandle) 200 | { 201 | if (val_is_kind(inHandle, kind_lua_vm)) 202 | { 203 | lua_State *l = (lua_State *)val_to_kind(inHandle, kind_lua_vm); 204 | return l; 205 | } 206 | return NULL; 207 | } 208 | 209 | static void release_lua(value inHandle) 210 | { 211 | lua_State *l = lua_from_handle(inHandle); 212 | if (l) 213 | { 214 | lua_close(l); 215 | } 216 | } 217 | 218 | static value lua_create() 219 | { 220 | lua_State *l = luaL_newstate(); 221 | value result = alloc_abstract(kind_lua_vm, l); 222 | val_gc(result, release_lua); 223 | return result; 224 | } 225 | DEFINE_PRIM(lua_create, 0); 226 | 227 | static value lua_get_version() 228 | { 229 | return alloc_string(LUA_VERSION); 230 | } 231 | DEFINE_PRIM(lua_get_version, 0); 232 | 233 | static value lua_load_libs(value inHandle, value inLibs) 234 | { 235 | static const luaL_Reg lualibs[] = { 236 | { "base", luaopen_base }, 237 | { "debug", luaopen_debug }, 238 | { "io", luaopen_io }, 239 | { "math", luaopen_math }, 240 | { "os", luaopen_os }, 241 | { "package", luaopen_package }, 242 | { "string", luaopen_string }, 243 | { "table", luaopen_table }, 244 | { NULL, NULL } 245 | }; 246 | 247 | lua_State *l = lua_from_handle(inHandle); 248 | if (l) 249 | { 250 | int numLibs = val_array_size(inLibs); 251 | value *libs = val_array_value(inLibs); 252 | 253 | for (int i = 0; i < numLibs; i++) 254 | { 255 | const luaL_Reg *lib = lualibs; 256 | for (;lib->func != NULL; lib++) 257 | { 258 | if (strcmp(val_string(libs[i]), lib->name) == 0) 259 | { 260 | // printf("loading lua library %s\n", lib->name); 261 | luaL_requiref(l, lib->name, lib->func, 1); 262 | lua_settop(l, 0); 263 | break; 264 | } 265 | } 266 | } 267 | } 268 | return alloc_null(); 269 | } 270 | DEFINE_PRIM(lua_load_libs, 2); 271 | 272 | static value lua_load_context(value inHandle, value inContext) 273 | { 274 | lua_State *l = lua_from_handle(inHandle); 275 | if (l) 276 | { 277 | // load context, if any 278 | if (!val_is_null(inContext) && val_is_object(inContext)) 279 | { 280 | val_iter_fields(inContext, haxe_iter_global, l); 281 | } 282 | } 283 | return alloc_null(); 284 | } 285 | DEFINE_PRIM(lua_load_context, 2); 286 | 287 | static value lua_call_function(value inHandle, value inFunction, value inArgs) 288 | { 289 | const char *func = val_get_string(inFunction); 290 | lua_State *l = lua_from_handle(inHandle); 291 | if (l) 292 | { 293 | lua_getglobal(l, func); 294 | 295 | int numArgs = 1; 296 | if (val_is_array(inArgs)) 297 | { 298 | numArgs = val_array_size(inArgs); 299 | value *args = val_array_value(inArgs); 300 | for (int i = 0; i < numArgs; i++) 301 | { 302 | haxe_to_lua(args[i], l); 303 | } 304 | } 305 | else 306 | { 307 | haxe_to_lua(inArgs, l); 308 | } 309 | 310 | if (lua_pcall(l, numArgs, 1, 0) == 0) 311 | { 312 | value v = lua_value_to_haxe(l, -1); 313 | lua_pop(l, 1); 314 | return v; 315 | } 316 | } 317 | return alloc_null(); 318 | } 319 | DEFINE_PRIM(lua_call_function, 3); 320 | 321 | static value lua_execute(value inHandle, value inScript) 322 | { 323 | value v; 324 | lua_State *l = lua_from_handle(inHandle); 325 | if (l) 326 | { 327 | // run the script 328 | if (luaL_dostring(l, val_string(inScript)) == LUA_OK) 329 | { 330 | // convert the lua values to haxe 331 | int lua_v; 332 | while ((lua_v = lua_gettop(l)) != 0) 333 | { 334 | v = lua_value_to_haxe(l, lua_v); 335 | lua_pop(l, 1); 336 | } 337 | } 338 | else 339 | { 340 | // get error message 341 | v = alloc_string(lua_tostring(l, -1)); 342 | lua_pop(l, 1); 343 | } 344 | return v; 345 | } 346 | return alloc_null(); 347 | } 348 | DEFINE_PRIM(lua_execute, 2); 349 | 350 | extern "C" void lua_main() 351 | { 352 | kind_share(&kind_lua_vm, "lua::vm"); // Fix Neko init 353 | } 354 | DEFINE_ENTRY_POINT(lua_main); 355 | 356 | extern "C" int lua_register_prims() { return 0; } 357 | -------------------------------------------------------------------------------- /project/lua/LICENSE: -------------------------------------------------------------------------------- 1 | Copyright © 1994–2014 Lua.org, PUC-Rio. 2 | Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: 3 | 4 | The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. 5 | 6 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 7 | -------------------------------------------------------------------------------- /project/lua/lapi.h: -------------------------------------------------------------------------------- 1 | /* 2 | ** $Id: lapi.h,v 2.7.1.1 2013/04/12 18:48:47 roberto Exp $ 3 | ** Auxiliary functions from Lua API 4 | ** See Copyright Notice in lua.h 5 | */ 6 | 7 | #ifndef lapi_h 8 | #define lapi_h 9 | 10 | 11 | #include "llimits.h" 12 | #include "lstate.h" 13 | 14 | #define api_incr_top(L) {L->top++; api_check(L, L->top <= L->ci->top, \ 15 | "stack overflow");} 16 | 17 | #define adjustresults(L,nres) \ 18 | { if ((nres) == LUA_MULTRET && L->ci->top < L->top) L->ci->top = L->top; } 19 | 20 | #define api_checknelems(L,n) api_check(L, (n) < (L->top - L->ci->func), \ 21 | "not enough elements in the stack") 22 | 23 | 24 | #endif 25 | -------------------------------------------------------------------------------- /project/lua/lauxlib.h: -------------------------------------------------------------------------------- 1 | /* 2 | ** $Id: lauxlib.h,v 1.120.1.1 2013/04/12 18:48:47 roberto Exp $ 3 | ** Auxiliary functions for building Lua libraries 4 | ** See Copyright Notice in lua.h 5 | */ 6 | 7 | 8 | #ifndef lauxlib_h 9 | #define lauxlib_h 10 | 11 | 12 | #include 13 | #include 14 | 15 | #include "lua.h" 16 | 17 | 18 | 19 | /* extra error code for `luaL_load' */ 20 | #define LUA_ERRFILE (LUA_ERRERR+1) 21 | 22 | 23 | typedef struct luaL_Reg { 24 | const char *name; 25 | lua_CFunction func; 26 | } luaL_Reg; 27 | 28 | 29 | LUALIB_API void (luaL_checkversion_) (lua_State *L, lua_Number ver); 30 | #define luaL_checkversion(L) luaL_checkversion_(L, LUA_VERSION_NUM) 31 | 32 | LUALIB_API int (luaL_getmetafield) (lua_State *L, int obj, const char *e); 33 | LUALIB_API int (luaL_callmeta) (lua_State *L, int obj, const char *e); 34 | LUALIB_API const char *(luaL_tolstring) (lua_State *L, int idx, size_t *len); 35 | LUALIB_API int (luaL_argerror) (lua_State *L, int numarg, const char *extramsg); 36 | LUALIB_API const char *(luaL_checklstring) (lua_State *L, int numArg, 37 | size_t *l); 38 | LUALIB_API const char *(luaL_optlstring) (lua_State *L, int numArg, 39 | const char *def, size_t *l); 40 | LUALIB_API lua_Number (luaL_checknumber) (lua_State *L, int numArg); 41 | LUALIB_API lua_Number (luaL_optnumber) (lua_State *L, int nArg, lua_Number def); 42 | 43 | LUALIB_API lua_Integer (luaL_checkinteger) (lua_State *L, int numArg); 44 | LUALIB_API lua_Integer (luaL_optinteger) (lua_State *L, int nArg, 45 | lua_Integer def); 46 | LUALIB_API lua_Unsigned (luaL_checkunsigned) (lua_State *L, int numArg); 47 | LUALIB_API lua_Unsigned (luaL_optunsigned) (lua_State *L, int numArg, 48 | lua_Unsigned def); 49 | 50 | LUALIB_API void (luaL_checkstack) (lua_State *L, int sz, const char *msg); 51 | LUALIB_API void (luaL_checktype) (lua_State *L, int narg, int t); 52 | LUALIB_API void (luaL_checkany) (lua_State *L, int narg); 53 | 54 | LUALIB_API int (luaL_newmetatable) (lua_State *L, const char *tname); 55 | LUALIB_API void (luaL_setmetatable) (lua_State *L, const char *tname); 56 | LUALIB_API void *(luaL_testudata) (lua_State *L, int ud, const char *tname); 57 | LUALIB_API void *(luaL_checkudata) (lua_State *L, int ud, const char *tname); 58 | 59 | LUALIB_API void (luaL_where) (lua_State *L, int lvl); 60 | LUALIB_API int (luaL_error) (lua_State *L, const char *fmt, ...); 61 | 62 | LUALIB_API int (luaL_checkoption) (lua_State *L, int narg, const char *def, 63 | const char *const lst[]); 64 | 65 | LUALIB_API int (luaL_fileresult) (lua_State *L, int stat, const char *fname); 66 | LUALIB_API int (luaL_execresult) (lua_State *L, int stat); 67 | 68 | /* pre-defined references */ 69 | #define LUA_NOREF (-2) 70 | #define LUA_REFNIL (-1) 71 | 72 | LUALIB_API int (luaL_ref) (lua_State *L, int t); 73 | LUALIB_API void (luaL_unref) (lua_State *L, int t, int ref); 74 | 75 | LUALIB_API int (luaL_loadfilex) (lua_State *L, const char *filename, 76 | const char *mode); 77 | 78 | #define luaL_loadfile(L,f) luaL_loadfilex(L,f,NULL) 79 | 80 | LUALIB_API int (luaL_loadbufferx) (lua_State *L, const char *buff, size_t sz, 81 | const char *name, const char *mode); 82 | LUALIB_API int (luaL_loadstring) (lua_State *L, const char *s); 83 | 84 | LUALIB_API lua_State *(luaL_newstate) (void); 85 | 86 | LUALIB_API int (luaL_len) (lua_State *L, int idx); 87 | 88 | LUALIB_API const char *(luaL_gsub) (lua_State *L, const char *s, const char *p, 89 | const char *r); 90 | 91 | LUALIB_API void (luaL_setfuncs) (lua_State *L, const luaL_Reg *l, int nup); 92 | 93 | LUALIB_API int (luaL_getsubtable) (lua_State *L, int idx, const char *fname); 94 | 95 | LUALIB_API void (luaL_traceback) (lua_State *L, lua_State *L1, 96 | const char *msg, int level); 97 | 98 | LUALIB_API void (luaL_requiref) (lua_State *L, const char *modname, 99 | lua_CFunction openf, int glb); 100 | 101 | /* 102 | ** =============================================================== 103 | ** some useful macros 104 | ** =============================================================== 105 | */ 106 | 107 | 108 | #define luaL_newlibtable(L,l) \ 109 | lua_createtable(L, 0, sizeof(l)/sizeof((l)[0]) - 1) 110 | 111 | #define luaL_newlib(L,l) (luaL_newlibtable(L,l), luaL_setfuncs(L,l,0)) 112 | 113 | #define luaL_argcheck(L, cond,numarg,extramsg) \ 114 | ((void)((cond) || luaL_argerror(L, (numarg), (extramsg)))) 115 | #define luaL_checkstring(L,n) (luaL_checklstring(L, (n), NULL)) 116 | #define luaL_optstring(L,n,d) (luaL_optlstring(L, (n), (d), NULL)) 117 | #define luaL_checkint(L,n) ((int)luaL_checkinteger(L, (n))) 118 | #define luaL_optint(L,n,d) ((int)luaL_optinteger(L, (n), (d))) 119 | #define luaL_checklong(L,n) ((long)luaL_checkinteger(L, (n))) 120 | #define luaL_optlong(L,n,d) ((long)luaL_optinteger(L, (n), (d))) 121 | 122 | #define luaL_typename(L,i) lua_typename(L, lua_type(L,(i))) 123 | 124 | #define luaL_dofile(L, fn) \ 125 | (luaL_loadfile(L, fn) || lua_pcall(L, 0, LUA_MULTRET, 0)) 126 | 127 | #define luaL_dostring(L, s) \ 128 | (luaL_loadstring(L, s) || lua_pcall(L, 0, LUA_MULTRET, 0)) 129 | 130 | #define luaL_getmetatable(L,n) (lua_getfield(L, LUA_REGISTRYINDEX, (n))) 131 | 132 | #define luaL_opt(L,f,n,d) (lua_isnoneornil(L,(n)) ? (d) : f(L,(n))) 133 | 134 | #define luaL_loadbuffer(L,s,sz,n) luaL_loadbufferx(L,s,sz,n,NULL) 135 | 136 | 137 | /* 138 | ** {====================================================== 139 | ** Generic Buffer manipulation 140 | ** ======================================================= 141 | */ 142 | 143 | typedef struct luaL_Buffer { 144 | char *b; /* buffer address */ 145 | size_t size; /* buffer size */ 146 | size_t n; /* number of characters in buffer */ 147 | lua_State *L; 148 | char initb[LUAL_BUFFERSIZE]; /* initial buffer */ 149 | } luaL_Buffer; 150 | 151 | 152 | #define luaL_addchar(B,c) \ 153 | ((void)((B)->n < (B)->size || luaL_prepbuffsize((B), 1)), \ 154 | ((B)->b[(B)->n++] = (c))) 155 | 156 | #define luaL_addsize(B,s) ((B)->n += (s)) 157 | 158 | LUALIB_API void (luaL_buffinit) (lua_State *L, luaL_Buffer *B); 159 | LUALIB_API char *(luaL_prepbuffsize) (luaL_Buffer *B, size_t sz); 160 | LUALIB_API void (luaL_addlstring) (luaL_Buffer *B, const char *s, size_t l); 161 | LUALIB_API void (luaL_addstring) (luaL_Buffer *B, const char *s); 162 | LUALIB_API void (luaL_addvalue) (luaL_Buffer *B); 163 | LUALIB_API void (luaL_pushresult) (luaL_Buffer *B); 164 | LUALIB_API void (luaL_pushresultsize) (luaL_Buffer *B, size_t sz); 165 | LUALIB_API char *(luaL_buffinitsize) (lua_State *L, luaL_Buffer *B, size_t sz); 166 | 167 | #define luaL_prepbuffer(B) luaL_prepbuffsize(B, LUAL_BUFFERSIZE) 168 | 169 | /* }====================================================== */ 170 | 171 | 172 | 173 | /* 174 | ** {====================================================== 175 | ** File handles for IO library 176 | ** ======================================================= 177 | */ 178 | 179 | /* 180 | ** A file handle is a userdata with metatable 'LUA_FILEHANDLE' and 181 | ** initial structure 'luaL_Stream' (it may contain other fields 182 | ** after that initial structure). 183 | */ 184 | 185 | #define LUA_FILEHANDLE "FILE*" 186 | 187 | 188 | typedef struct luaL_Stream { 189 | FILE *f; /* stream (NULL for incompletely created streams) */ 190 | lua_CFunction closef; /* to close stream (NULL for closed streams) */ 191 | } luaL_Stream; 192 | 193 | /* }====================================================== */ 194 | 195 | 196 | 197 | /* compatibility with old module system */ 198 | #if defined(LUA_COMPAT_MODULE) 199 | 200 | LUALIB_API void (luaL_pushmodule) (lua_State *L, const char *modname, 201 | int sizehint); 202 | LUALIB_API void (luaL_openlib) (lua_State *L, const char *libname, 203 | const luaL_Reg *l, int nup); 204 | 205 | #define luaL_register(L,n,l) (luaL_openlib(L,(n),(l),0)) 206 | 207 | #endif 208 | 209 | 210 | #endif 211 | 212 | 213 | -------------------------------------------------------------------------------- /project/lua/lbaselib.c: -------------------------------------------------------------------------------- 1 | /* 2 | ** $Id: lbaselib.c,v 1.276.1.1 2013/04/12 18:48:47 roberto Exp $ 3 | ** Basic library 4 | ** See Copyright Notice in lua.h 5 | */ 6 | 7 | 8 | 9 | #include 10 | #include 11 | #include 12 | #include 13 | 14 | #define lbaselib_c 15 | #define LUA_LIB 16 | 17 | #include "lua.h" 18 | 19 | #include "lauxlib.h" 20 | #include "lualib.h" 21 | 22 | 23 | static int luaB_print (lua_State *L) { 24 | int n = lua_gettop(L); /* number of arguments */ 25 | int i; 26 | lua_getglobal(L, "tostring"); 27 | for (i=1; i<=n; i++) { 28 | const char *s; 29 | size_t l; 30 | lua_pushvalue(L, -1); /* function to be called */ 31 | lua_pushvalue(L, i); /* value to print */ 32 | lua_call(L, 1, 1); 33 | s = lua_tolstring(L, -1, &l); /* get result */ 34 | if (s == NULL) 35 | return luaL_error(L, 36 | LUA_QL("tostring") " must return a string to " LUA_QL("print")); 37 | if (i>1) luai_writestring("\t", 1); 38 | luai_writestring(s, l); 39 | lua_pop(L, 1); /* pop result */ 40 | } 41 | luai_writeline(); 42 | return 0; 43 | } 44 | 45 | 46 | #define SPACECHARS " \f\n\r\t\v" 47 | 48 | static int luaB_tonumber (lua_State *L) { 49 | if (lua_isnoneornil(L, 2)) { /* standard conversion */ 50 | int isnum; 51 | lua_Number n = lua_tonumberx(L, 1, &isnum); 52 | if (isnum) { 53 | lua_pushnumber(L, n); 54 | return 1; 55 | } /* else not a number; must be something */ 56 | luaL_checkany(L, 1); 57 | } 58 | else { 59 | size_t l; 60 | const char *s = luaL_checklstring(L, 1, &l); 61 | const char *e = s + l; /* end point for 's' */ 62 | int base = luaL_checkint(L, 2); 63 | int neg = 0; 64 | luaL_argcheck(L, 2 <= base && base <= 36, 2, "base out of range"); 65 | s += strspn(s, SPACECHARS); /* skip initial spaces */ 66 | if (*s == '-') { s++; neg = 1; } /* handle signal */ 67 | else if (*s == '+') s++; 68 | if (isalnum((unsigned char)*s)) { 69 | lua_Number n = 0; 70 | do { 71 | int digit = (isdigit((unsigned char)*s)) ? *s - '0' 72 | : toupper((unsigned char)*s) - 'A' + 10; 73 | if (digit >= base) break; /* invalid numeral; force a fail */ 74 | n = n * (lua_Number)base + (lua_Number)digit; 75 | s++; 76 | } while (isalnum((unsigned char)*s)); 77 | s += strspn(s, SPACECHARS); /* skip trailing spaces */ 78 | if (s == e) { /* no invalid trailing characters? */ 79 | lua_pushnumber(L, (neg) ? -n : n); 80 | return 1; 81 | } /* else not a number */ 82 | } /* else not a number */ 83 | } 84 | lua_pushnil(L); /* not a number */ 85 | return 1; 86 | } 87 | 88 | 89 | static int luaB_error (lua_State *L) { 90 | int level = luaL_optint(L, 2, 1); 91 | lua_settop(L, 1); 92 | if (lua_isstring(L, 1) && level > 0) { /* add extra information? */ 93 | luaL_where(L, level); 94 | lua_pushvalue(L, 1); 95 | lua_concat(L, 2); 96 | } 97 | return lua_error(L); 98 | } 99 | 100 | 101 | static int luaB_getmetatable (lua_State *L) { 102 | luaL_checkany(L, 1); 103 | if (!lua_getmetatable(L, 1)) { 104 | lua_pushnil(L); 105 | return 1; /* no metatable */ 106 | } 107 | luaL_getmetafield(L, 1, "__metatable"); 108 | return 1; /* returns either __metatable field (if present) or metatable */ 109 | } 110 | 111 | 112 | static int luaB_setmetatable (lua_State *L) { 113 | int t = lua_type(L, 2); 114 | luaL_checktype(L, 1, LUA_TTABLE); 115 | luaL_argcheck(L, t == LUA_TNIL || t == LUA_TTABLE, 2, 116 | "nil or table expected"); 117 | if (luaL_getmetafield(L, 1, "__metatable")) 118 | return luaL_error(L, "cannot change a protected metatable"); 119 | lua_settop(L, 2); 120 | lua_setmetatable(L, 1); 121 | return 1; 122 | } 123 | 124 | 125 | static int luaB_rawequal (lua_State *L) { 126 | luaL_checkany(L, 1); 127 | luaL_checkany(L, 2); 128 | lua_pushboolean(L, lua_rawequal(L, 1, 2)); 129 | return 1; 130 | } 131 | 132 | 133 | static int luaB_rawlen (lua_State *L) { 134 | int t = lua_type(L, 1); 135 | luaL_argcheck(L, t == LUA_TTABLE || t == LUA_TSTRING, 1, 136 | "table or string expected"); 137 | lua_pushinteger(L, lua_rawlen(L, 1)); 138 | return 1; 139 | } 140 | 141 | 142 | static int luaB_rawget (lua_State *L) { 143 | luaL_checktype(L, 1, LUA_TTABLE); 144 | luaL_checkany(L, 2); 145 | lua_settop(L, 2); 146 | lua_rawget(L, 1); 147 | return 1; 148 | } 149 | 150 | static int luaB_rawset (lua_State *L) { 151 | luaL_checktype(L, 1, LUA_TTABLE); 152 | luaL_checkany(L, 2); 153 | luaL_checkany(L, 3); 154 | lua_settop(L, 3); 155 | lua_rawset(L, 1); 156 | return 1; 157 | } 158 | 159 | 160 | static int luaB_collectgarbage (lua_State *L) { 161 | static const char *const opts[] = {"stop", "restart", "collect", 162 | "count", "step", "setpause", "setstepmul", 163 | "setmajorinc", "isrunning", "generational", "incremental", NULL}; 164 | static const int optsnum[] = {LUA_GCSTOP, LUA_GCRESTART, LUA_GCCOLLECT, 165 | LUA_GCCOUNT, LUA_GCSTEP, LUA_GCSETPAUSE, LUA_GCSETSTEPMUL, 166 | LUA_GCSETMAJORINC, LUA_GCISRUNNING, LUA_GCGEN, LUA_GCINC}; 167 | int o = optsnum[luaL_checkoption(L, 1, "collect", opts)]; 168 | int ex = luaL_optint(L, 2, 0); 169 | int res = lua_gc(L, o, ex); 170 | switch (o) { 171 | case LUA_GCCOUNT: { 172 | int b = lua_gc(L, LUA_GCCOUNTB, 0); 173 | lua_pushnumber(L, res + ((lua_Number)b/1024)); 174 | lua_pushinteger(L, b); 175 | return 2; 176 | } 177 | case LUA_GCSTEP: case LUA_GCISRUNNING: { 178 | lua_pushboolean(L, res); 179 | return 1; 180 | } 181 | default: { 182 | lua_pushinteger(L, res); 183 | return 1; 184 | } 185 | } 186 | } 187 | 188 | 189 | static int luaB_type (lua_State *L) { 190 | luaL_checkany(L, 1); 191 | lua_pushstring(L, luaL_typename(L, 1)); 192 | return 1; 193 | } 194 | 195 | 196 | static int pairsmeta (lua_State *L, const char *method, int iszero, 197 | lua_CFunction iter) { 198 | if (!luaL_getmetafield(L, 1, method)) { /* no metamethod? */ 199 | luaL_checktype(L, 1, LUA_TTABLE); /* argument must be a table */ 200 | lua_pushcfunction(L, iter); /* will return generator, */ 201 | lua_pushvalue(L, 1); /* state, */ 202 | if (iszero) lua_pushinteger(L, 0); /* and initial value */ 203 | else lua_pushnil(L); 204 | } 205 | else { 206 | lua_pushvalue(L, 1); /* argument 'self' to metamethod */ 207 | lua_call(L, 1, 3); /* get 3 values from metamethod */ 208 | } 209 | return 3; 210 | } 211 | 212 | 213 | static int luaB_next (lua_State *L) { 214 | luaL_checktype(L, 1, LUA_TTABLE); 215 | lua_settop(L, 2); /* create a 2nd argument if there isn't one */ 216 | if (lua_next(L, 1)) 217 | return 2; 218 | else { 219 | lua_pushnil(L); 220 | return 1; 221 | } 222 | } 223 | 224 | 225 | static int luaB_pairs (lua_State *L) { 226 | return pairsmeta(L, "__pairs", 0, luaB_next); 227 | } 228 | 229 | 230 | static int ipairsaux (lua_State *L) { 231 | int i = luaL_checkint(L, 2); 232 | luaL_checktype(L, 1, LUA_TTABLE); 233 | i++; /* next value */ 234 | lua_pushinteger(L, i); 235 | lua_rawgeti(L, 1, i); 236 | return (lua_isnil(L, -1)) ? 1 : 2; 237 | } 238 | 239 | 240 | static int luaB_ipairs (lua_State *L) { 241 | return pairsmeta(L, "__ipairs", 1, ipairsaux); 242 | } 243 | 244 | 245 | static int load_aux (lua_State *L, int status, int envidx) { 246 | if (status == LUA_OK) { 247 | if (envidx != 0) { /* 'env' parameter? */ 248 | lua_pushvalue(L, envidx); /* environment for loaded function */ 249 | if (!lua_setupvalue(L, -2, 1)) /* set it as 1st upvalue */ 250 | lua_pop(L, 1); /* remove 'env' if not used by previous call */ 251 | } 252 | return 1; 253 | } 254 | else { /* error (message is on top of the stack) */ 255 | lua_pushnil(L); 256 | lua_insert(L, -2); /* put before error message */ 257 | return 2; /* return nil plus error message */ 258 | } 259 | } 260 | 261 | 262 | static int luaB_loadfile (lua_State *L) { 263 | const char *fname = luaL_optstring(L, 1, NULL); 264 | const char *mode = luaL_optstring(L, 2, NULL); 265 | int env = (!lua_isnone(L, 3) ? 3 : 0); /* 'env' index or 0 if no 'env' */ 266 | int status = luaL_loadfilex(L, fname, mode); 267 | return load_aux(L, status, env); 268 | } 269 | 270 | 271 | /* 272 | ** {====================================================== 273 | ** Generic Read function 274 | ** ======================================================= 275 | */ 276 | 277 | 278 | /* 279 | ** reserved slot, above all arguments, to hold a copy of the returned 280 | ** string to avoid it being collected while parsed. 'load' has four 281 | ** optional arguments (chunk, source name, mode, and environment). 282 | */ 283 | #define RESERVEDSLOT 5 284 | 285 | 286 | /* 287 | ** Reader for generic `load' function: `lua_load' uses the 288 | ** stack for internal stuff, so the reader cannot change the 289 | ** stack top. Instead, it keeps its resulting string in a 290 | ** reserved slot inside the stack. 291 | */ 292 | static const char *generic_reader (lua_State *L, void *ud, size_t *size) { 293 | (void)(ud); /* not used */ 294 | luaL_checkstack(L, 2, "too many nested functions"); 295 | lua_pushvalue(L, 1); /* get function */ 296 | lua_call(L, 0, 1); /* call it */ 297 | if (lua_isnil(L, -1)) { 298 | lua_pop(L, 1); /* pop result */ 299 | *size = 0; 300 | return NULL; 301 | } 302 | else if (!lua_isstring(L, -1)) 303 | luaL_error(L, "reader function must return a string"); 304 | lua_replace(L, RESERVEDSLOT); /* save string in reserved slot */ 305 | return lua_tolstring(L, RESERVEDSLOT, size); 306 | } 307 | 308 | 309 | static int luaB_load (lua_State *L) { 310 | int status; 311 | size_t l; 312 | const char *s = lua_tolstring(L, 1, &l); 313 | const char *mode = luaL_optstring(L, 3, "bt"); 314 | int env = (!lua_isnone(L, 4) ? 4 : 0); /* 'env' index or 0 if no 'env' */ 315 | if (s != NULL) { /* loading a string? */ 316 | const char *chunkname = luaL_optstring(L, 2, s); 317 | status = luaL_loadbufferx(L, s, l, chunkname, mode); 318 | } 319 | else { /* loading from a reader function */ 320 | const char *chunkname = luaL_optstring(L, 2, "=(load)"); 321 | luaL_checktype(L, 1, LUA_TFUNCTION); 322 | lua_settop(L, RESERVEDSLOT); /* create reserved slot */ 323 | status = lua_load(L, generic_reader, NULL, chunkname, mode); 324 | } 325 | return load_aux(L, status, env); 326 | } 327 | 328 | /* }====================================================== */ 329 | 330 | 331 | static int dofilecont (lua_State *L) { 332 | return lua_gettop(L) - 1; 333 | } 334 | 335 | 336 | static int luaB_dofile (lua_State *L) { 337 | const char *fname = luaL_optstring(L, 1, NULL); 338 | lua_settop(L, 1); 339 | if (luaL_loadfile(L, fname) != LUA_OK) 340 | return lua_error(L); 341 | lua_callk(L, 0, LUA_MULTRET, 0, dofilecont); 342 | return dofilecont(L); 343 | } 344 | 345 | 346 | static int luaB_assert (lua_State *L) { 347 | if (!lua_toboolean(L, 1)) 348 | return luaL_error(L, "%s", luaL_optstring(L, 2, "assertion failed!")); 349 | return lua_gettop(L); 350 | } 351 | 352 | 353 | static int luaB_select (lua_State *L) { 354 | int n = lua_gettop(L); 355 | if (lua_type(L, 1) == LUA_TSTRING && *lua_tostring(L, 1) == '#') { 356 | lua_pushinteger(L, n-1); 357 | return 1; 358 | } 359 | else { 360 | int i = luaL_checkint(L, 1); 361 | if (i < 0) i = n + i; 362 | else if (i > n) i = n; 363 | luaL_argcheck(L, 1 <= i, 1, "index out of range"); 364 | return n - i; 365 | } 366 | } 367 | 368 | 369 | static int finishpcall (lua_State *L, int status) { 370 | if (!lua_checkstack(L, 1)) { /* no space for extra boolean? */ 371 | lua_settop(L, 0); /* create space for return values */ 372 | lua_pushboolean(L, 0); 373 | lua_pushstring(L, "stack overflow"); 374 | return 2; /* return false, msg */ 375 | } 376 | lua_pushboolean(L, status); /* first result (status) */ 377 | lua_replace(L, 1); /* put first result in first slot */ 378 | return lua_gettop(L); 379 | } 380 | 381 | 382 | static int pcallcont (lua_State *L) { 383 | int status = lua_getctx(L, NULL); 384 | return finishpcall(L, (status == LUA_YIELD)); 385 | } 386 | 387 | 388 | static int luaB_pcall (lua_State *L) { 389 | int status; 390 | luaL_checkany(L, 1); 391 | lua_pushnil(L); 392 | lua_insert(L, 1); /* create space for status result */ 393 | status = lua_pcallk(L, lua_gettop(L) - 2, LUA_MULTRET, 0, 0, pcallcont); 394 | return finishpcall(L, (status == LUA_OK)); 395 | } 396 | 397 | 398 | static int luaB_xpcall (lua_State *L) { 399 | int status; 400 | int n = lua_gettop(L); 401 | luaL_argcheck(L, n >= 2, 2, "value expected"); 402 | lua_pushvalue(L, 1); /* exchange function... */ 403 | lua_copy(L, 2, 1); /* ...and error handler */ 404 | lua_replace(L, 2); 405 | status = lua_pcallk(L, n - 2, LUA_MULTRET, 1, 0, pcallcont); 406 | return finishpcall(L, (status == LUA_OK)); 407 | } 408 | 409 | 410 | static int luaB_tostring (lua_State *L) { 411 | luaL_checkany(L, 1); 412 | luaL_tolstring(L, 1, NULL); 413 | return 1; 414 | } 415 | 416 | 417 | static const luaL_Reg base_funcs[] = { 418 | {"assert", luaB_assert}, 419 | {"collectgarbage", luaB_collectgarbage}, 420 | {"dofile", luaB_dofile}, 421 | {"error", luaB_error}, 422 | {"getmetatable", luaB_getmetatable}, 423 | {"ipairs", luaB_ipairs}, 424 | {"loadfile", luaB_loadfile}, 425 | {"load", luaB_load}, 426 | #if defined(LUA_COMPAT_LOADSTRING) 427 | {"loadstring", luaB_load}, 428 | #endif 429 | {"next", luaB_next}, 430 | {"pairs", luaB_pairs}, 431 | {"pcall", luaB_pcall}, 432 | {"print", luaB_print}, 433 | {"rawequal", luaB_rawequal}, 434 | {"rawlen", luaB_rawlen}, 435 | {"rawget", luaB_rawget}, 436 | {"rawset", luaB_rawset}, 437 | {"select", luaB_select}, 438 | {"setmetatable", luaB_setmetatable}, 439 | {"tonumber", luaB_tonumber}, 440 | {"tostring", luaB_tostring}, 441 | {"type", luaB_type}, 442 | {"xpcall", luaB_xpcall}, 443 | {NULL, NULL} 444 | }; 445 | 446 | 447 | LUAMOD_API int luaopen_base (lua_State *L) { 448 | /* set global _G */ 449 | lua_pushglobaltable(L); 450 | lua_pushglobaltable(L); 451 | lua_setfield(L, -2, "_G"); 452 | /* open lib into global table */ 453 | luaL_setfuncs(L, base_funcs, 0); 454 | lua_pushliteral(L, LUA_VERSION); 455 | lua_setfield(L, -2, "_VERSION"); /* set global _VERSION */ 456 | return 1; 457 | } 458 | 459 | -------------------------------------------------------------------------------- /project/lua/lbitlib.c: -------------------------------------------------------------------------------- 1 | /* 2 | ** $Id: lbitlib.c,v 1.18.1.2 2013/07/09 18:01:41 roberto Exp $ 3 | ** Standard library for bitwise operations 4 | ** See Copyright Notice in lua.h 5 | */ 6 | 7 | #define lbitlib_c 8 | #define LUA_LIB 9 | 10 | #include "lua.h" 11 | 12 | #include "lauxlib.h" 13 | #include "lualib.h" 14 | 15 | 16 | /* number of bits to consider in a number */ 17 | #if !defined(LUA_NBITS) 18 | #define LUA_NBITS 32 19 | #endif 20 | 21 | 22 | #define ALLONES (~(((~(lua_Unsigned)0) << (LUA_NBITS - 1)) << 1)) 23 | 24 | /* macro to trim extra bits */ 25 | #define trim(x) ((x) & ALLONES) 26 | 27 | 28 | /* builds a number with 'n' ones (1 <= n <= LUA_NBITS) */ 29 | #define mask(n) (~((ALLONES << 1) << ((n) - 1))) 30 | 31 | 32 | typedef lua_Unsigned b_uint; 33 | 34 | 35 | 36 | static b_uint andaux (lua_State *L) { 37 | int i, n = lua_gettop(L); 38 | b_uint r = ~(b_uint)0; 39 | for (i = 1; i <= n; i++) 40 | r &= luaL_checkunsigned(L, i); 41 | return trim(r); 42 | } 43 | 44 | 45 | static int b_and (lua_State *L) { 46 | b_uint r = andaux(L); 47 | lua_pushunsigned(L, r); 48 | return 1; 49 | } 50 | 51 | 52 | static int b_test (lua_State *L) { 53 | b_uint r = andaux(L); 54 | lua_pushboolean(L, r != 0); 55 | return 1; 56 | } 57 | 58 | 59 | static int b_or (lua_State *L) { 60 | int i, n = lua_gettop(L); 61 | b_uint r = 0; 62 | for (i = 1; i <= n; i++) 63 | r |= luaL_checkunsigned(L, i); 64 | lua_pushunsigned(L, trim(r)); 65 | return 1; 66 | } 67 | 68 | 69 | static int b_xor (lua_State *L) { 70 | int i, n = lua_gettop(L); 71 | b_uint r = 0; 72 | for (i = 1; i <= n; i++) 73 | r ^= luaL_checkunsigned(L, i); 74 | lua_pushunsigned(L, trim(r)); 75 | return 1; 76 | } 77 | 78 | 79 | static int b_not (lua_State *L) { 80 | b_uint r = ~luaL_checkunsigned(L, 1); 81 | lua_pushunsigned(L, trim(r)); 82 | return 1; 83 | } 84 | 85 | 86 | static int b_shift (lua_State *L, b_uint r, int i) { 87 | if (i < 0) { /* shift right? */ 88 | i = -i; 89 | r = trim(r); 90 | if (i >= LUA_NBITS) r = 0; 91 | else r >>= i; 92 | } 93 | else { /* shift left */ 94 | if (i >= LUA_NBITS) r = 0; 95 | else r <<= i; 96 | r = trim(r); 97 | } 98 | lua_pushunsigned(L, r); 99 | return 1; 100 | } 101 | 102 | 103 | static int b_lshift (lua_State *L) { 104 | return b_shift(L, luaL_checkunsigned(L, 1), luaL_checkint(L, 2)); 105 | } 106 | 107 | 108 | static int b_rshift (lua_State *L) { 109 | return b_shift(L, luaL_checkunsigned(L, 1), -luaL_checkint(L, 2)); 110 | } 111 | 112 | 113 | static int b_arshift (lua_State *L) { 114 | b_uint r = luaL_checkunsigned(L, 1); 115 | int i = luaL_checkint(L, 2); 116 | if (i < 0 || !(r & ((b_uint)1 << (LUA_NBITS - 1)))) 117 | return b_shift(L, r, -i); 118 | else { /* arithmetic shift for 'negative' number */ 119 | if (i >= LUA_NBITS) r = ALLONES; 120 | else 121 | r = trim((r >> i) | ~(~(b_uint)0 >> i)); /* add signal bit */ 122 | lua_pushunsigned(L, r); 123 | return 1; 124 | } 125 | } 126 | 127 | 128 | static int b_rot (lua_State *L, int i) { 129 | b_uint r = luaL_checkunsigned(L, 1); 130 | i &= (LUA_NBITS - 1); /* i = i % NBITS */ 131 | r = trim(r); 132 | if (i != 0) /* avoid undefined shift of LUA_NBITS when i == 0 */ 133 | r = (r << i) | (r >> (LUA_NBITS - i)); 134 | lua_pushunsigned(L, trim(r)); 135 | return 1; 136 | } 137 | 138 | 139 | static int b_lrot (lua_State *L) { 140 | return b_rot(L, luaL_checkint(L, 2)); 141 | } 142 | 143 | 144 | static int b_rrot (lua_State *L) { 145 | return b_rot(L, -luaL_checkint(L, 2)); 146 | } 147 | 148 | 149 | /* 150 | ** get field and width arguments for field-manipulation functions, 151 | ** checking whether they are valid. 152 | ** ('luaL_error' called without 'return' to avoid later warnings about 153 | ** 'width' being used uninitialized.) 154 | */ 155 | static int fieldargs (lua_State *L, int farg, int *width) { 156 | int f = luaL_checkint(L, farg); 157 | int w = luaL_optint(L, farg + 1, 1); 158 | luaL_argcheck(L, 0 <= f, farg, "field cannot be negative"); 159 | luaL_argcheck(L, 0 < w, farg + 1, "width must be positive"); 160 | if (f + w > LUA_NBITS) 161 | luaL_error(L, "trying to access non-existent bits"); 162 | *width = w; 163 | return f; 164 | } 165 | 166 | 167 | static int b_extract (lua_State *L) { 168 | int w; 169 | b_uint r = luaL_checkunsigned(L, 1); 170 | int f = fieldargs(L, 2, &w); 171 | r = (r >> f) & mask(w); 172 | lua_pushunsigned(L, r); 173 | return 1; 174 | } 175 | 176 | 177 | static int b_replace (lua_State *L) { 178 | int w; 179 | b_uint r = luaL_checkunsigned(L, 1); 180 | b_uint v = luaL_checkunsigned(L, 2); 181 | int f = fieldargs(L, 3, &w); 182 | int m = mask(w); 183 | v &= m; /* erase bits outside given width */ 184 | r = (r & ~(m << f)) | (v << f); 185 | lua_pushunsigned(L, r); 186 | return 1; 187 | } 188 | 189 | 190 | static const luaL_Reg bitlib[] = { 191 | {"arshift", b_arshift}, 192 | {"band", b_and}, 193 | {"bnot", b_not}, 194 | {"bor", b_or}, 195 | {"bxor", b_xor}, 196 | {"btest", b_test}, 197 | {"extract", b_extract}, 198 | {"lrotate", b_lrot}, 199 | {"lshift", b_lshift}, 200 | {"replace", b_replace}, 201 | {"rrotate", b_rrot}, 202 | {"rshift", b_rshift}, 203 | {NULL, NULL} 204 | }; 205 | 206 | 207 | 208 | LUAMOD_API int luaopen_bit32 (lua_State *L) { 209 | luaL_newlib(L, bitlib); 210 | return 1; 211 | } 212 | 213 | -------------------------------------------------------------------------------- /project/lua/lcode.h: -------------------------------------------------------------------------------- 1 | /* 2 | ** $Id: lcode.h,v 1.58.1.1 2013/04/12 18:48:47 roberto Exp $ 3 | ** Code generator for Lua 4 | ** See Copyright Notice in lua.h 5 | */ 6 | 7 | #ifndef lcode_h 8 | #define lcode_h 9 | 10 | #include "llex.h" 11 | #include "lobject.h" 12 | #include "lopcodes.h" 13 | #include "lparser.h" 14 | 15 | 16 | /* 17 | ** Marks the end of a patch list. It is an invalid value both as an absolute 18 | ** address, and as a list link (would link an element to itself). 19 | */ 20 | #define NO_JUMP (-1) 21 | 22 | 23 | /* 24 | ** grep "ORDER OPR" if you change these enums (ORDER OP) 25 | */ 26 | typedef enum BinOpr { 27 | OPR_ADD, OPR_SUB, OPR_MUL, OPR_DIV, OPR_MOD, OPR_POW, 28 | OPR_CONCAT, 29 | OPR_EQ, OPR_LT, OPR_LE, 30 | OPR_NE, OPR_GT, OPR_GE, 31 | OPR_AND, OPR_OR, 32 | OPR_NOBINOPR 33 | } BinOpr; 34 | 35 | 36 | typedef enum UnOpr { OPR_MINUS, OPR_NOT, OPR_LEN, OPR_NOUNOPR } UnOpr; 37 | 38 | 39 | #define getcode(fs,e) ((fs)->f->code[(e)->u.info]) 40 | 41 | #define luaK_codeAsBx(fs,o,A,sBx) luaK_codeABx(fs,o,A,(sBx)+MAXARG_sBx) 42 | 43 | #define luaK_setmultret(fs,e) luaK_setreturns(fs, e, LUA_MULTRET) 44 | 45 | #define luaK_jumpto(fs,t) luaK_patchlist(fs, luaK_jump(fs), t) 46 | 47 | LUAI_FUNC int luaK_codeABx (FuncState *fs, OpCode o, int A, unsigned int Bx); 48 | LUAI_FUNC int luaK_codeABC (FuncState *fs, OpCode o, int A, int B, int C); 49 | LUAI_FUNC int luaK_codek (FuncState *fs, int reg, int k); 50 | LUAI_FUNC void luaK_fixline (FuncState *fs, int line); 51 | LUAI_FUNC void luaK_nil (FuncState *fs, int from, int n); 52 | LUAI_FUNC void luaK_reserveregs (FuncState *fs, int n); 53 | LUAI_FUNC void luaK_checkstack (FuncState *fs, int n); 54 | LUAI_FUNC int luaK_stringK (FuncState *fs, TString *s); 55 | LUAI_FUNC int luaK_numberK (FuncState *fs, lua_Number r); 56 | LUAI_FUNC void luaK_dischargevars (FuncState *fs, expdesc *e); 57 | LUAI_FUNC int luaK_exp2anyreg (FuncState *fs, expdesc *e); 58 | LUAI_FUNC void luaK_exp2anyregup (FuncState *fs, expdesc *e); 59 | LUAI_FUNC void luaK_exp2nextreg (FuncState *fs, expdesc *e); 60 | LUAI_FUNC void luaK_exp2val (FuncState *fs, expdesc *e); 61 | LUAI_FUNC int luaK_exp2RK (FuncState *fs, expdesc *e); 62 | LUAI_FUNC void luaK_self (FuncState *fs, expdesc *e, expdesc *key); 63 | LUAI_FUNC void luaK_indexed (FuncState *fs, expdesc *t, expdesc *k); 64 | LUAI_FUNC void luaK_goiftrue (FuncState *fs, expdesc *e); 65 | LUAI_FUNC void luaK_goiffalse (FuncState *fs, expdesc *e); 66 | LUAI_FUNC void luaK_storevar (FuncState *fs, expdesc *var, expdesc *e); 67 | LUAI_FUNC void luaK_setreturns (FuncState *fs, expdesc *e, int nresults); 68 | LUAI_FUNC void luaK_setoneret (FuncState *fs, expdesc *e); 69 | LUAI_FUNC int luaK_jump (FuncState *fs); 70 | LUAI_FUNC void luaK_ret (FuncState *fs, int first, int nret); 71 | LUAI_FUNC void luaK_patchlist (FuncState *fs, int list, int target); 72 | LUAI_FUNC void luaK_patchtohere (FuncState *fs, int list); 73 | LUAI_FUNC void luaK_patchclose (FuncState *fs, int list, int level); 74 | LUAI_FUNC void luaK_concat (FuncState *fs, int *l1, int l2); 75 | LUAI_FUNC int luaK_getlabel (FuncState *fs); 76 | LUAI_FUNC void luaK_prefix (FuncState *fs, UnOpr op, expdesc *v, int line); 77 | LUAI_FUNC void luaK_infix (FuncState *fs, BinOpr op, expdesc *v); 78 | LUAI_FUNC void luaK_posfix (FuncState *fs, BinOpr op, expdesc *v1, 79 | expdesc *v2, int line); 80 | LUAI_FUNC void luaK_setlist (FuncState *fs, int base, int nelems, int tostore); 81 | 82 | 83 | #endif 84 | -------------------------------------------------------------------------------- /project/lua/lcorolib.c: -------------------------------------------------------------------------------- 1 | /* 2 | ** $Id: lcorolib.c,v 1.5.1.1 2013/04/12 18:48:47 roberto Exp $ 3 | ** Coroutine Library 4 | ** See Copyright Notice in lua.h 5 | */ 6 | 7 | 8 | #include 9 | 10 | 11 | #define lcorolib_c 12 | #define LUA_LIB 13 | 14 | #include "lua.h" 15 | 16 | #include "lauxlib.h" 17 | #include "lualib.h" 18 | 19 | 20 | static int auxresume (lua_State *L, lua_State *co, int narg) { 21 | int status; 22 | if (!lua_checkstack(co, narg)) { 23 | lua_pushliteral(L, "too many arguments to resume"); 24 | return -1; /* error flag */ 25 | } 26 | if (lua_status(co) == LUA_OK && lua_gettop(co) == 0) { 27 | lua_pushliteral(L, "cannot resume dead coroutine"); 28 | return -1; /* error flag */ 29 | } 30 | lua_xmove(L, co, narg); 31 | status = lua_resume(co, L, narg); 32 | if (status == LUA_OK || status == LUA_YIELD) { 33 | int nres = lua_gettop(co); 34 | if (!lua_checkstack(L, nres + 1)) { 35 | lua_pop(co, nres); /* remove results anyway */ 36 | lua_pushliteral(L, "too many results to resume"); 37 | return -1; /* error flag */ 38 | } 39 | lua_xmove(co, L, nres); /* move yielded values */ 40 | return nres; 41 | } 42 | else { 43 | lua_xmove(co, L, 1); /* move error message */ 44 | return -1; /* error flag */ 45 | } 46 | } 47 | 48 | 49 | static int luaB_coresume (lua_State *L) { 50 | lua_State *co = lua_tothread(L, 1); 51 | int r; 52 | luaL_argcheck(L, co, 1, "coroutine expected"); 53 | r = auxresume(L, co, lua_gettop(L) - 1); 54 | if (r < 0) { 55 | lua_pushboolean(L, 0); 56 | lua_insert(L, -2); 57 | return 2; /* return false + error message */ 58 | } 59 | else { 60 | lua_pushboolean(L, 1); 61 | lua_insert(L, -(r + 1)); 62 | return r + 1; /* return true + `resume' returns */ 63 | } 64 | } 65 | 66 | 67 | static int luaB_auxwrap (lua_State *L) { 68 | lua_State *co = lua_tothread(L, lua_upvalueindex(1)); 69 | int r = auxresume(L, co, lua_gettop(L)); 70 | if (r < 0) { 71 | if (lua_isstring(L, -1)) { /* error object is a string? */ 72 | luaL_where(L, 1); /* add extra info */ 73 | lua_insert(L, -2); 74 | lua_concat(L, 2); 75 | } 76 | return lua_error(L); /* propagate error */ 77 | } 78 | return r; 79 | } 80 | 81 | 82 | static int luaB_cocreate (lua_State *L) { 83 | lua_State *NL; 84 | luaL_checktype(L, 1, LUA_TFUNCTION); 85 | NL = lua_newthread(L); 86 | lua_pushvalue(L, 1); /* move function to top */ 87 | lua_xmove(L, NL, 1); /* move function from L to NL */ 88 | return 1; 89 | } 90 | 91 | 92 | static int luaB_cowrap (lua_State *L) { 93 | luaB_cocreate(L); 94 | lua_pushcclosure(L, luaB_auxwrap, 1); 95 | return 1; 96 | } 97 | 98 | 99 | static int luaB_yield (lua_State *L) { 100 | return lua_yield(L, lua_gettop(L)); 101 | } 102 | 103 | 104 | static int luaB_costatus (lua_State *L) { 105 | lua_State *co = lua_tothread(L, 1); 106 | luaL_argcheck(L, co, 1, "coroutine expected"); 107 | if (L == co) lua_pushliteral(L, "running"); 108 | else { 109 | switch (lua_status(co)) { 110 | case LUA_YIELD: 111 | lua_pushliteral(L, "suspended"); 112 | break; 113 | case LUA_OK: { 114 | lua_Debug ar; 115 | if (lua_getstack(co, 0, &ar) > 0) /* does it have frames? */ 116 | lua_pushliteral(L, "normal"); /* it is running */ 117 | else if (lua_gettop(co) == 0) 118 | lua_pushliteral(L, "dead"); 119 | else 120 | lua_pushliteral(L, "suspended"); /* initial state */ 121 | break; 122 | } 123 | default: /* some error occurred */ 124 | lua_pushliteral(L, "dead"); 125 | break; 126 | } 127 | } 128 | return 1; 129 | } 130 | 131 | 132 | static int luaB_corunning (lua_State *L) { 133 | int ismain = lua_pushthread(L); 134 | lua_pushboolean(L, ismain); 135 | return 2; 136 | } 137 | 138 | 139 | static const luaL_Reg co_funcs[] = { 140 | {"create", luaB_cocreate}, 141 | {"resume", luaB_coresume}, 142 | {"running", luaB_corunning}, 143 | {"status", luaB_costatus}, 144 | {"wrap", luaB_cowrap}, 145 | {"yield", luaB_yield}, 146 | {NULL, NULL} 147 | }; 148 | 149 | 150 | 151 | LUAMOD_API int luaopen_coroutine (lua_State *L) { 152 | luaL_newlib(L, co_funcs); 153 | return 1; 154 | } 155 | 156 | -------------------------------------------------------------------------------- /project/lua/lctype.c: -------------------------------------------------------------------------------- 1 | /* 2 | ** $Id: lctype.c,v 1.11.1.1 2013/04/12 18:48:47 roberto Exp $ 3 | ** 'ctype' functions for Lua 4 | ** See Copyright Notice in lua.h 5 | */ 6 | 7 | #define lctype_c 8 | #define LUA_CORE 9 | 10 | #include "lctype.h" 11 | 12 | #if !LUA_USE_CTYPE /* { */ 13 | 14 | #include 15 | 16 | LUAI_DDEF const lu_byte luai_ctype_[UCHAR_MAX + 2] = { 17 | 0x00, /* EOZ */ 18 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* 0. */ 19 | 0x00, 0x08, 0x08, 0x08, 0x08, 0x08, 0x00, 0x00, 20 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* 1. */ 21 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 22 | 0x0c, 0x04, 0x04, 0x04, 0x04, 0x04, 0x04, 0x04, /* 2. */ 23 | 0x04, 0x04, 0x04, 0x04, 0x04, 0x04, 0x04, 0x04, 24 | 0x16, 0x16, 0x16, 0x16, 0x16, 0x16, 0x16, 0x16, /* 3. */ 25 | 0x16, 0x16, 0x04, 0x04, 0x04, 0x04, 0x04, 0x04, 26 | 0x04, 0x15, 0x15, 0x15, 0x15, 0x15, 0x15, 0x05, /* 4. */ 27 | 0x05, 0x05, 0x05, 0x05, 0x05, 0x05, 0x05, 0x05, 28 | 0x05, 0x05, 0x05, 0x05, 0x05, 0x05, 0x05, 0x05, /* 5. */ 29 | 0x05, 0x05, 0x05, 0x04, 0x04, 0x04, 0x04, 0x05, 30 | 0x04, 0x15, 0x15, 0x15, 0x15, 0x15, 0x15, 0x05, /* 6. */ 31 | 0x05, 0x05, 0x05, 0x05, 0x05, 0x05, 0x05, 0x05, 32 | 0x05, 0x05, 0x05, 0x05, 0x05, 0x05, 0x05, 0x05, /* 7. */ 33 | 0x05, 0x05, 0x05, 0x04, 0x04, 0x04, 0x04, 0x00, 34 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* 8. */ 35 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 36 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* 9. */ 37 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 38 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* a. */ 39 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 40 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* b. */ 41 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 42 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* c. */ 43 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 44 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* d. */ 45 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 46 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* e. */ 47 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 48 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* f. */ 49 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 50 | }; 51 | 52 | #endif /* } */ 53 | -------------------------------------------------------------------------------- /project/lua/lctype.h: -------------------------------------------------------------------------------- 1 | /* 2 | ** $Id: lctype.h,v 1.12.1.1 2013/04/12 18:48:47 roberto Exp $ 3 | ** 'ctype' functions for Lua 4 | ** See Copyright Notice in lua.h 5 | */ 6 | 7 | #ifndef lctype_h 8 | #define lctype_h 9 | 10 | #include "lua.h" 11 | 12 | 13 | /* 14 | ** WARNING: the functions defined here do not necessarily correspond 15 | ** to the similar functions in the standard C ctype.h. They are 16 | ** optimized for the specific needs of Lua 17 | */ 18 | 19 | #if !defined(LUA_USE_CTYPE) 20 | 21 | #if 'A' == 65 && '0' == 48 22 | /* ASCII case: can use its own tables; faster and fixed */ 23 | #define LUA_USE_CTYPE 0 24 | #else 25 | /* must use standard C ctype */ 26 | #define LUA_USE_CTYPE 1 27 | #endif 28 | 29 | #endif 30 | 31 | 32 | #if !LUA_USE_CTYPE /* { */ 33 | 34 | #include 35 | 36 | #include "llimits.h" 37 | 38 | 39 | #define ALPHABIT 0 40 | #define DIGITBIT 1 41 | #define PRINTBIT 2 42 | #define SPACEBIT 3 43 | #define XDIGITBIT 4 44 | 45 | 46 | #define MASK(B) (1 << (B)) 47 | 48 | 49 | /* 50 | ** add 1 to char to allow index -1 (EOZ) 51 | */ 52 | #define testprop(c,p) (luai_ctype_[(c)+1] & (p)) 53 | 54 | /* 55 | ** 'lalpha' (Lua alphabetic) and 'lalnum' (Lua alphanumeric) both include '_' 56 | */ 57 | #define lislalpha(c) testprop(c, MASK(ALPHABIT)) 58 | #define lislalnum(c) testprop(c, (MASK(ALPHABIT) | MASK(DIGITBIT))) 59 | #define lisdigit(c) testprop(c, MASK(DIGITBIT)) 60 | #define lisspace(c) testprop(c, MASK(SPACEBIT)) 61 | #define lisprint(c) testprop(c, MASK(PRINTBIT)) 62 | #define lisxdigit(c) testprop(c, MASK(XDIGITBIT)) 63 | 64 | /* 65 | ** this 'ltolower' only works for alphabetic characters 66 | */ 67 | #define ltolower(c) ((c) | ('A' ^ 'a')) 68 | 69 | 70 | /* two more entries for 0 and -1 (EOZ) */ 71 | LUAI_DDEC const lu_byte luai_ctype_[UCHAR_MAX + 2]; 72 | 73 | 74 | #else /* }{ */ 75 | 76 | /* 77 | ** use standard C ctypes 78 | */ 79 | 80 | #include 81 | 82 | 83 | #define lislalpha(c) (isalpha(c) || (c) == '_') 84 | #define lislalnum(c) (isalnum(c) || (c) == '_') 85 | #define lisdigit(c) (isdigit(c)) 86 | #define lisspace(c) (isspace(c)) 87 | #define lisprint(c) (isprint(c)) 88 | #define lisxdigit(c) (isxdigit(c)) 89 | 90 | #define ltolower(c) (tolower(c)) 91 | 92 | #endif /* } */ 93 | 94 | #endif 95 | 96 | -------------------------------------------------------------------------------- /project/lua/ldblib.c: -------------------------------------------------------------------------------- 1 | /* 2 | ** $Id: ldblib.c,v 1.132.1.1 2013/04/12 18:48:47 roberto Exp $ 3 | ** Interface from Lua to its debug API 4 | ** See Copyright Notice in lua.h 5 | */ 6 | 7 | 8 | #include 9 | #include 10 | #include 11 | 12 | #define ldblib_c 13 | #define LUA_LIB 14 | 15 | #include "lua.h" 16 | 17 | #include "lauxlib.h" 18 | #include "lualib.h" 19 | 20 | 21 | #define HOOKKEY "_HKEY" 22 | 23 | 24 | 25 | static int db_getregistry (lua_State *L) { 26 | lua_pushvalue(L, LUA_REGISTRYINDEX); 27 | return 1; 28 | } 29 | 30 | 31 | static int db_getmetatable (lua_State *L) { 32 | luaL_checkany(L, 1); 33 | if (!lua_getmetatable(L, 1)) { 34 | lua_pushnil(L); /* no metatable */ 35 | } 36 | return 1; 37 | } 38 | 39 | 40 | static int db_setmetatable (lua_State *L) { 41 | int t = lua_type(L, 2); 42 | luaL_argcheck(L, t == LUA_TNIL || t == LUA_TTABLE, 2, 43 | "nil or table expected"); 44 | lua_settop(L, 2); 45 | lua_setmetatable(L, 1); 46 | return 1; /* return 1st argument */ 47 | } 48 | 49 | 50 | static int db_getuservalue (lua_State *L) { 51 | if (lua_type(L, 1) != LUA_TUSERDATA) 52 | lua_pushnil(L); 53 | else 54 | lua_getuservalue(L, 1); 55 | return 1; 56 | } 57 | 58 | 59 | static int db_setuservalue (lua_State *L) { 60 | if (lua_type(L, 1) == LUA_TLIGHTUSERDATA) 61 | luaL_argerror(L, 1, "full userdata expected, got light userdata"); 62 | luaL_checktype(L, 1, LUA_TUSERDATA); 63 | if (!lua_isnoneornil(L, 2)) 64 | luaL_checktype(L, 2, LUA_TTABLE); 65 | lua_settop(L, 2); 66 | lua_setuservalue(L, 1); 67 | return 1; 68 | } 69 | 70 | 71 | static void settabss (lua_State *L, const char *i, const char *v) { 72 | lua_pushstring(L, v); 73 | lua_setfield(L, -2, i); 74 | } 75 | 76 | 77 | static void settabsi (lua_State *L, const char *i, int v) { 78 | lua_pushinteger(L, v); 79 | lua_setfield(L, -2, i); 80 | } 81 | 82 | 83 | static void settabsb (lua_State *L, const char *i, int v) { 84 | lua_pushboolean(L, v); 85 | lua_setfield(L, -2, i); 86 | } 87 | 88 | 89 | static lua_State *getthread (lua_State *L, int *arg) { 90 | if (lua_isthread(L, 1)) { 91 | *arg = 1; 92 | return lua_tothread(L, 1); 93 | } 94 | else { 95 | *arg = 0; 96 | return L; 97 | } 98 | } 99 | 100 | 101 | static void treatstackoption (lua_State *L, lua_State *L1, const char *fname) { 102 | if (L == L1) { 103 | lua_pushvalue(L, -2); 104 | lua_remove(L, -3); 105 | } 106 | else 107 | lua_xmove(L1, L, 1); 108 | lua_setfield(L, -2, fname); 109 | } 110 | 111 | 112 | static int db_getinfo (lua_State *L) { 113 | lua_Debug ar; 114 | int arg; 115 | lua_State *L1 = getthread(L, &arg); 116 | const char *options = luaL_optstring(L, arg+2, "flnStu"); 117 | if (lua_isnumber(L, arg+1)) { 118 | if (!lua_getstack(L1, (int)lua_tointeger(L, arg+1), &ar)) { 119 | lua_pushnil(L); /* level out of range */ 120 | return 1; 121 | } 122 | } 123 | else if (lua_isfunction(L, arg+1)) { 124 | lua_pushfstring(L, ">%s", options); 125 | options = lua_tostring(L, -1); 126 | lua_pushvalue(L, arg+1); 127 | lua_xmove(L, L1, 1); 128 | } 129 | else 130 | return luaL_argerror(L, arg+1, "function or level expected"); 131 | if (!lua_getinfo(L1, options, &ar)) 132 | return luaL_argerror(L, arg+2, "invalid option"); 133 | lua_createtable(L, 0, 2); 134 | if (strchr(options, 'S')) { 135 | settabss(L, "source", ar.source); 136 | settabss(L, "short_src", ar.short_src); 137 | settabsi(L, "linedefined", ar.linedefined); 138 | settabsi(L, "lastlinedefined", ar.lastlinedefined); 139 | settabss(L, "what", ar.what); 140 | } 141 | if (strchr(options, 'l')) 142 | settabsi(L, "currentline", ar.currentline); 143 | if (strchr(options, 'u')) { 144 | settabsi(L, "nups", ar.nups); 145 | settabsi(L, "nparams", ar.nparams); 146 | settabsb(L, "isvararg", ar.isvararg); 147 | } 148 | if (strchr(options, 'n')) { 149 | settabss(L, "name", ar.name); 150 | settabss(L, "namewhat", ar.namewhat); 151 | } 152 | if (strchr(options, 't')) 153 | settabsb(L, "istailcall", ar.istailcall); 154 | if (strchr(options, 'L')) 155 | treatstackoption(L, L1, "activelines"); 156 | if (strchr(options, 'f')) 157 | treatstackoption(L, L1, "func"); 158 | return 1; /* return table */ 159 | } 160 | 161 | 162 | static int db_getlocal (lua_State *L) { 163 | int arg; 164 | lua_State *L1 = getthread(L, &arg); 165 | lua_Debug ar; 166 | const char *name; 167 | int nvar = luaL_checkint(L, arg+2); /* local-variable index */ 168 | if (lua_isfunction(L, arg + 1)) { /* function argument? */ 169 | lua_pushvalue(L, arg + 1); /* push function */ 170 | lua_pushstring(L, lua_getlocal(L, NULL, nvar)); /* push local name */ 171 | return 1; 172 | } 173 | else { /* stack-level argument */ 174 | if (!lua_getstack(L1, luaL_checkint(L, arg+1), &ar)) /* out of range? */ 175 | return luaL_argerror(L, arg+1, "level out of range"); 176 | name = lua_getlocal(L1, &ar, nvar); 177 | if (name) { 178 | lua_xmove(L1, L, 1); /* push local value */ 179 | lua_pushstring(L, name); /* push name */ 180 | lua_pushvalue(L, -2); /* re-order */ 181 | return 2; 182 | } 183 | else { 184 | lua_pushnil(L); /* no name (nor value) */ 185 | return 1; 186 | } 187 | } 188 | } 189 | 190 | 191 | static int db_setlocal (lua_State *L) { 192 | int arg; 193 | lua_State *L1 = getthread(L, &arg); 194 | lua_Debug ar; 195 | if (!lua_getstack(L1, luaL_checkint(L, arg+1), &ar)) /* out of range? */ 196 | return luaL_argerror(L, arg+1, "level out of range"); 197 | luaL_checkany(L, arg+3); 198 | lua_settop(L, arg+3); 199 | lua_xmove(L, L1, 1); 200 | lua_pushstring(L, lua_setlocal(L1, &ar, luaL_checkint(L, arg+2))); 201 | return 1; 202 | } 203 | 204 | 205 | static int auxupvalue (lua_State *L, int get) { 206 | const char *name; 207 | int n = luaL_checkint(L, 2); 208 | luaL_checktype(L, 1, LUA_TFUNCTION); 209 | name = get ? lua_getupvalue(L, 1, n) : lua_setupvalue(L, 1, n); 210 | if (name == NULL) return 0; 211 | lua_pushstring(L, name); 212 | lua_insert(L, -(get+1)); 213 | return get + 1; 214 | } 215 | 216 | 217 | static int db_getupvalue (lua_State *L) { 218 | return auxupvalue(L, 1); 219 | } 220 | 221 | 222 | static int db_setupvalue (lua_State *L) { 223 | luaL_checkany(L, 3); 224 | return auxupvalue(L, 0); 225 | } 226 | 227 | 228 | static int checkupval (lua_State *L, int argf, int argnup) { 229 | lua_Debug ar; 230 | int nup = luaL_checkint(L, argnup); 231 | luaL_checktype(L, argf, LUA_TFUNCTION); 232 | lua_pushvalue(L, argf); 233 | lua_getinfo(L, ">u", &ar); 234 | luaL_argcheck(L, 1 <= nup && nup <= ar.nups, argnup, "invalid upvalue index"); 235 | return nup; 236 | } 237 | 238 | 239 | static int db_upvalueid (lua_State *L) { 240 | int n = checkupval(L, 1, 2); 241 | lua_pushlightuserdata(L, lua_upvalueid(L, 1, n)); 242 | return 1; 243 | } 244 | 245 | 246 | static int db_upvaluejoin (lua_State *L) { 247 | int n1 = checkupval(L, 1, 2); 248 | int n2 = checkupval(L, 3, 4); 249 | luaL_argcheck(L, !lua_iscfunction(L, 1), 1, "Lua function expected"); 250 | luaL_argcheck(L, !lua_iscfunction(L, 3), 3, "Lua function expected"); 251 | lua_upvaluejoin(L, 1, n1, 3, n2); 252 | return 0; 253 | } 254 | 255 | 256 | #define gethooktable(L) luaL_getsubtable(L, LUA_REGISTRYINDEX, HOOKKEY) 257 | 258 | 259 | static void hookf (lua_State *L, lua_Debug *ar) { 260 | static const char *const hooknames[] = 261 | {"call", "return", "line", "count", "tail call"}; 262 | gethooktable(L); 263 | lua_pushthread(L); 264 | lua_rawget(L, -2); 265 | if (lua_isfunction(L, -1)) { 266 | lua_pushstring(L, hooknames[(int)ar->event]); 267 | if (ar->currentline >= 0) 268 | lua_pushinteger(L, ar->currentline); 269 | else lua_pushnil(L); 270 | lua_assert(lua_getinfo(L, "lS", ar)); 271 | lua_call(L, 2, 0); 272 | } 273 | } 274 | 275 | 276 | static int makemask (const char *smask, int count) { 277 | int mask = 0; 278 | if (strchr(smask, 'c')) mask |= LUA_MASKCALL; 279 | if (strchr(smask, 'r')) mask |= LUA_MASKRET; 280 | if (strchr(smask, 'l')) mask |= LUA_MASKLINE; 281 | if (count > 0) mask |= LUA_MASKCOUNT; 282 | return mask; 283 | } 284 | 285 | 286 | static char *unmakemask (int mask, char *smask) { 287 | int i = 0; 288 | if (mask & LUA_MASKCALL) smask[i++] = 'c'; 289 | if (mask & LUA_MASKRET) smask[i++] = 'r'; 290 | if (mask & LUA_MASKLINE) smask[i++] = 'l'; 291 | smask[i] = '\0'; 292 | return smask; 293 | } 294 | 295 | 296 | static int db_sethook (lua_State *L) { 297 | int arg, mask, count; 298 | lua_Hook func; 299 | lua_State *L1 = getthread(L, &arg); 300 | if (lua_isnoneornil(L, arg+1)) { 301 | lua_settop(L, arg+1); 302 | func = NULL; mask = 0; count = 0; /* turn off hooks */ 303 | } 304 | else { 305 | const char *smask = luaL_checkstring(L, arg+2); 306 | luaL_checktype(L, arg+1, LUA_TFUNCTION); 307 | count = luaL_optint(L, arg+3, 0); 308 | func = hookf; mask = makemask(smask, count); 309 | } 310 | if (gethooktable(L) == 0) { /* creating hook table? */ 311 | lua_pushstring(L, "k"); 312 | lua_setfield(L, -2, "__mode"); /** hooktable.__mode = "k" */ 313 | lua_pushvalue(L, -1); 314 | lua_setmetatable(L, -2); /* setmetatable(hooktable) = hooktable */ 315 | } 316 | lua_pushthread(L1); lua_xmove(L1, L, 1); 317 | lua_pushvalue(L, arg+1); 318 | lua_rawset(L, -3); /* set new hook */ 319 | lua_sethook(L1, func, mask, count); /* set hooks */ 320 | return 0; 321 | } 322 | 323 | 324 | static int db_gethook (lua_State *L) { 325 | int arg; 326 | lua_State *L1 = getthread(L, &arg); 327 | char buff[5]; 328 | int mask = lua_gethookmask(L1); 329 | lua_Hook hook = lua_gethook(L1); 330 | if (hook != NULL && hook != hookf) /* external hook? */ 331 | lua_pushliteral(L, "external hook"); 332 | else { 333 | gethooktable(L); 334 | lua_pushthread(L1); lua_xmove(L1, L, 1); 335 | lua_rawget(L, -2); /* get hook */ 336 | lua_remove(L, -2); /* remove hook table */ 337 | } 338 | lua_pushstring(L, unmakemask(mask, buff)); 339 | lua_pushinteger(L, lua_gethookcount(L1)); 340 | return 3; 341 | } 342 | 343 | 344 | static int db_debug (lua_State *L) { 345 | for (;;) { 346 | char buffer[250]; 347 | luai_writestringerror("%s", "lua_debug> "); 348 | if (fgets(buffer, sizeof(buffer), stdin) == 0 || 349 | strcmp(buffer, "cont\n") == 0) 350 | return 0; 351 | if (luaL_loadbuffer(L, buffer, strlen(buffer), "=(debug command)") || 352 | lua_pcall(L, 0, 0, 0)) 353 | luai_writestringerror("%s\n", lua_tostring(L, -1)); 354 | lua_settop(L, 0); /* remove eventual returns */ 355 | } 356 | } 357 | 358 | 359 | static int db_traceback (lua_State *L) { 360 | int arg; 361 | lua_State *L1 = getthread(L, &arg); 362 | const char *msg = lua_tostring(L, arg + 1); 363 | if (msg == NULL && !lua_isnoneornil(L, arg + 1)) /* non-string 'msg'? */ 364 | lua_pushvalue(L, arg + 1); /* return it untouched */ 365 | else { 366 | int level = luaL_optint(L, arg + 2, (L == L1) ? 1 : 0); 367 | luaL_traceback(L, L1, msg, level); 368 | } 369 | return 1; 370 | } 371 | 372 | 373 | static const luaL_Reg dblib[] = { 374 | {"debug", db_debug}, 375 | {"getuservalue", db_getuservalue}, 376 | {"gethook", db_gethook}, 377 | {"getinfo", db_getinfo}, 378 | {"getlocal", db_getlocal}, 379 | {"getregistry", db_getregistry}, 380 | {"getmetatable", db_getmetatable}, 381 | {"getupvalue", db_getupvalue}, 382 | {"upvaluejoin", db_upvaluejoin}, 383 | {"upvalueid", db_upvalueid}, 384 | {"setuservalue", db_setuservalue}, 385 | {"sethook", db_sethook}, 386 | {"setlocal", db_setlocal}, 387 | {"setmetatable", db_setmetatable}, 388 | {"setupvalue", db_setupvalue}, 389 | {"traceback", db_traceback}, 390 | {NULL, NULL} 391 | }; 392 | 393 | 394 | LUAMOD_API int luaopen_debug (lua_State *L) { 395 | luaL_newlib(L, dblib); 396 | return 1; 397 | } 398 | 399 | -------------------------------------------------------------------------------- /project/lua/ldebug.h: -------------------------------------------------------------------------------- 1 | /* 2 | ** $Id: ldebug.h,v 2.7.1.1 2013/04/12 18:48:47 roberto Exp $ 3 | ** Auxiliary functions from Debug Interface module 4 | ** See Copyright Notice in lua.h 5 | */ 6 | 7 | #ifndef ldebug_h 8 | #define ldebug_h 9 | 10 | 11 | #include "lstate.h" 12 | 13 | 14 | #define pcRel(pc, p) (cast(int, (pc) - (p)->code) - 1) 15 | 16 | #define getfuncline(f,pc) (((f)->lineinfo) ? (f)->lineinfo[pc] : 0) 17 | 18 | #define resethookcount(L) (L->hookcount = L->basehookcount) 19 | 20 | /* Active Lua function (given call info) */ 21 | #define ci_func(ci) (clLvalue((ci)->func)) 22 | 23 | 24 | LUAI_FUNC l_noret luaG_typeerror (lua_State *L, const TValue *o, 25 | const char *opname); 26 | LUAI_FUNC l_noret luaG_concaterror (lua_State *L, StkId p1, StkId p2); 27 | LUAI_FUNC l_noret luaG_aritherror (lua_State *L, const TValue *p1, 28 | const TValue *p2); 29 | LUAI_FUNC l_noret luaG_ordererror (lua_State *L, const TValue *p1, 30 | const TValue *p2); 31 | LUAI_FUNC l_noret luaG_runerror (lua_State *L, const char *fmt, ...); 32 | LUAI_FUNC l_noret luaG_errormsg (lua_State *L); 33 | 34 | #endif 35 | -------------------------------------------------------------------------------- /project/lua/ldo.h: -------------------------------------------------------------------------------- 1 | /* 2 | ** $Id: ldo.h,v 2.20.1.1 2013/04/12 18:48:47 roberto Exp $ 3 | ** Stack and Call structure of Lua 4 | ** See Copyright Notice in lua.h 5 | */ 6 | 7 | #ifndef ldo_h 8 | #define ldo_h 9 | 10 | 11 | #include "lobject.h" 12 | #include "lstate.h" 13 | #include "lzio.h" 14 | 15 | 16 | #define luaD_checkstack(L,n) if (L->stack_last - L->top <= (n)) \ 17 | luaD_growstack(L, n); else condmovestack(L); 18 | 19 | 20 | #define incr_top(L) {L->top++; luaD_checkstack(L,0);} 21 | 22 | #define savestack(L,p) ((char *)(p) - (char *)L->stack) 23 | #define restorestack(L,n) ((TValue *)((char *)L->stack + (n))) 24 | 25 | 26 | /* type of protected functions, to be ran by `runprotected' */ 27 | typedef void (*Pfunc) (lua_State *L, void *ud); 28 | 29 | LUAI_FUNC int luaD_protectedparser (lua_State *L, ZIO *z, const char *name, 30 | const char *mode); 31 | LUAI_FUNC void luaD_hook (lua_State *L, int event, int line); 32 | LUAI_FUNC int luaD_precall (lua_State *L, StkId func, int nresults); 33 | LUAI_FUNC void luaD_call (lua_State *L, StkId func, int nResults, 34 | int allowyield); 35 | LUAI_FUNC int luaD_pcall (lua_State *L, Pfunc func, void *u, 36 | ptrdiff_t oldtop, ptrdiff_t ef); 37 | LUAI_FUNC int luaD_poscall (lua_State *L, StkId firstResult); 38 | LUAI_FUNC void luaD_reallocstack (lua_State *L, int newsize); 39 | LUAI_FUNC void luaD_growstack (lua_State *L, int n); 40 | LUAI_FUNC void luaD_shrinkstack (lua_State *L); 41 | 42 | LUAI_FUNC l_noret luaD_throw (lua_State *L, int errcode); 43 | LUAI_FUNC int luaD_rawrunprotected (lua_State *L, Pfunc f, void *ud); 44 | 45 | #endif 46 | 47 | -------------------------------------------------------------------------------- /project/lua/ldump.c: -------------------------------------------------------------------------------- 1 | /* 2 | ** $Id: ldump.c,v 2.17.1.1 2013/04/12 18:48:47 roberto Exp $ 3 | ** save precompiled Lua chunks 4 | ** See Copyright Notice in lua.h 5 | */ 6 | 7 | #include 8 | 9 | #define ldump_c 10 | #define LUA_CORE 11 | 12 | #include "lua.h" 13 | 14 | #include "lobject.h" 15 | #include "lstate.h" 16 | #include "lundump.h" 17 | 18 | typedef struct { 19 | lua_State* L; 20 | lua_Writer writer; 21 | void* data; 22 | int strip; 23 | int status; 24 | } DumpState; 25 | 26 | #define DumpMem(b,n,size,D) DumpBlock(b,(n)*(size),D) 27 | #define DumpVar(x,D) DumpMem(&x,1,sizeof(x),D) 28 | 29 | static void DumpBlock(const void* b, size_t size, DumpState* D) 30 | { 31 | if (D->status==0) 32 | { 33 | lua_unlock(D->L); 34 | D->status=(*D->writer)(D->L,b,size,D->data); 35 | lua_lock(D->L); 36 | } 37 | } 38 | 39 | static void DumpChar(int y, DumpState* D) 40 | { 41 | char x=(char)y; 42 | DumpVar(x,D); 43 | } 44 | 45 | static void DumpInt(int x, DumpState* D) 46 | { 47 | DumpVar(x,D); 48 | } 49 | 50 | static void DumpNumber(lua_Number x, DumpState* D) 51 | { 52 | DumpVar(x,D); 53 | } 54 | 55 | static void DumpVector(const void* b, int n, size_t size, DumpState* D) 56 | { 57 | DumpInt(n,D); 58 | DumpMem(b,n,size,D); 59 | } 60 | 61 | static void DumpString(const TString* s, DumpState* D) 62 | { 63 | if (s==NULL) 64 | { 65 | size_t size=0; 66 | DumpVar(size,D); 67 | } 68 | else 69 | { 70 | size_t size=s->tsv.len+1; /* include trailing '\0' */ 71 | DumpVar(size,D); 72 | DumpBlock(getstr(s),size*sizeof(char),D); 73 | } 74 | } 75 | 76 | #define DumpCode(f,D) DumpVector(f->code,f->sizecode,sizeof(Instruction),D) 77 | 78 | static void DumpFunction(const Proto* f, DumpState* D); 79 | 80 | static void DumpConstants(const Proto* f, DumpState* D) 81 | { 82 | int i,n=f->sizek; 83 | DumpInt(n,D); 84 | for (i=0; ik[i]; 87 | DumpChar(ttypenv(o),D); 88 | switch (ttypenv(o)) 89 | { 90 | case LUA_TNIL: 91 | break; 92 | case LUA_TBOOLEAN: 93 | DumpChar(bvalue(o),D); 94 | break; 95 | case LUA_TNUMBER: 96 | DumpNumber(nvalue(o),D); 97 | break; 98 | case LUA_TSTRING: 99 | DumpString(rawtsvalue(o),D); 100 | break; 101 | default: lua_assert(0); 102 | } 103 | } 104 | n=f->sizep; 105 | DumpInt(n,D); 106 | for (i=0; ip[i],D); 107 | } 108 | 109 | static void DumpUpvalues(const Proto* f, DumpState* D) 110 | { 111 | int i,n=f->sizeupvalues; 112 | DumpInt(n,D); 113 | for (i=0; iupvalues[i].instack,D); 116 | DumpChar(f->upvalues[i].idx,D); 117 | } 118 | } 119 | 120 | static void DumpDebug(const Proto* f, DumpState* D) 121 | { 122 | int i,n; 123 | DumpString((D->strip) ? NULL : f->source,D); 124 | n= (D->strip) ? 0 : f->sizelineinfo; 125 | DumpVector(f->lineinfo,n,sizeof(int),D); 126 | n= (D->strip) ? 0 : f->sizelocvars; 127 | DumpInt(n,D); 128 | for (i=0; ilocvars[i].varname,D); 131 | DumpInt(f->locvars[i].startpc,D); 132 | DumpInt(f->locvars[i].endpc,D); 133 | } 134 | n= (D->strip) ? 0 : f->sizeupvalues; 135 | DumpInt(n,D); 136 | for (i=0; iupvalues[i].name,D); 137 | } 138 | 139 | static void DumpFunction(const Proto* f, DumpState* D) 140 | { 141 | DumpInt(f->linedefined,D); 142 | DumpInt(f->lastlinedefined,D); 143 | DumpChar(f->numparams,D); 144 | DumpChar(f->is_vararg,D); 145 | DumpChar(f->maxstacksize,D); 146 | DumpCode(f,D); 147 | DumpConstants(f,D); 148 | DumpUpvalues(f,D); 149 | DumpDebug(f,D); 150 | } 151 | 152 | static void DumpHeader(DumpState* D) 153 | { 154 | lu_byte h[LUAC_HEADERSIZE]; 155 | luaU_header(h); 156 | DumpBlock(h,LUAC_HEADERSIZE,D); 157 | } 158 | 159 | /* 160 | ** dump Lua function as precompiled chunk 161 | */ 162 | int luaU_dump (lua_State* L, const Proto* f, lua_Writer w, void* data, int strip) 163 | { 164 | DumpState D; 165 | D.L=L; 166 | D.writer=w; 167 | D.data=data; 168 | D.strip=strip; 169 | D.status=0; 170 | DumpHeader(&D); 171 | DumpFunction(f,&D); 172 | return D.status; 173 | } 174 | -------------------------------------------------------------------------------- /project/lua/lfunc.c: -------------------------------------------------------------------------------- 1 | /* 2 | ** $Id: lfunc.c,v 2.30.1.1 2013/04/12 18:48:47 roberto Exp $ 3 | ** Auxiliary functions to manipulate prototypes and closures 4 | ** See Copyright Notice in lua.h 5 | */ 6 | 7 | 8 | #include 9 | 10 | #define lfunc_c 11 | #define LUA_CORE 12 | 13 | #include "lua.h" 14 | 15 | #include "lfunc.h" 16 | #include "lgc.h" 17 | #include "lmem.h" 18 | #include "lobject.h" 19 | #include "lstate.h" 20 | 21 | 22 | 23 | Closure *luaF_newCclosure (lua_State *L, int n) { 24 | Closure *c = &luaC_newobj(L, LUA_TCCL, sizeCclosure(n), NULL, 0)->cl; 25 | c->c.nupvalues = cast_byte(n); 26 | return c; 27 | } 28 | 29 | 30 | Closure *luaF_newLclosure (lua_State *L, int n) { 31 | Closure *c = &luaC_newobj(L, LUA_TLCL, sizeLclosure(n), NULL, 0)->cl; 32 | c->l.p = NULL; 33 | c->l.nupvalues = cast_byte(n); 34 | while (n--) c->l.upvals[n] = NULL; 35 | return c; 36 | } 37 | 38 | 39 | UpVal *luaF_newupval (lua_State *L) { 40 | UpVal *uv = &luaC_newobj(L, LUA_TUPVAL, sizeof(UpVal), NULL, 0)->uv; 41 | uv->v = &uv->u.value; 42 | setnilvalue(uv->v); 43 | return uv; 44 | } 45 | 46 | 47 | UpVal *luaF_findupval (lua_State *L, StkId level) { 48 | global_State *g = G(L); 49 | GCObject **pp = &L->openupval; 50 | UpVal *p; 51 | UpVal *uv; 52 | while (*pp != NULL && (p = gco2uv(*pp))->v >= level) { 53 | GCObject *o = obj2gco(p); 54 | lua_assert(p->v != &p->u.value); 55 | lua_assert(!isold(o) || isold(obj2gco(L))); 56 | if (p->v == level) { /* found a corresponding upvalue? */ 57 | if (isdead(g, o)) /* is it dead? */ 58 | changewhite(o); /* resurrect it */ 59 | return p; 60 | } 61 | pp = &p->next; 62 | } 63 | /* not found: create a new one */ 64 | uv = &luaC_newobj(L, LUA_TUPVAL, sizeof(UpVal), pp, 0)->uv; 65 | uv->v = level; /* current value lives in the stack */ 66 | uv->u.l.prev = &g->uvhead; /* double link it in `uvhead' list */ 67 | uv->u.l.next = g->uvhead.u.l.next; 68 | uv->u.l.next->u.l.prev = uv; 69 | g->uvhead.u.l.next = uv; 70 | lua_assert(uv->u.l.next->u.l.prev == uv && uv->u.l.prev->u.l.next == uv); 71 | return uv; 72 | } 73 | 74 | 75 | static void unlinkupval (UpVal *uv) { 76 | lua_assert(uv->u.l.next->u.l.prev == uv && uv->u.l.prev->u.l.next == uv); 77 | uv->u.l.next->u.l.prev = uv->u.l.prev; /* remove from `uvhead' list */ 78 | uv->u.l.prev->u.l.next = uv->u.l.next; 79 | } 80 | 81 | 82 | void luaF_freeupval (lua_State *L, UpVal *uv) { 83 | if (uv->v != &uv->u.value) /* is it open? */ 84 | unlinkupval(uv); /* remove from open list */ 85 | luaM_free(L, uv); /* free upvalue */ 86 | } 87 | 88 | 89 | void luaF_close (lua_State *L, StkId level) { 90 | UpVal *uv; 91 | global_State *g = G(L); 92 | while (L->openupval != NULL && (uv = gco2uv(L->openupval))->v >= level) { 93 | GCObject *o = obj2gco(uv); 94 | lua_assert(!isblack(o) && uv->v != &uv->u.value); 95 | L->openupval = uv->next; /* remove from `open' list */ 96 | if (isdead(g, o)) 97 | luaF_freeupval(L, uv); /* free upvalue */ 98 | else { 99 | unlinkupval(uv); /* remove upvalue from 'uvhead' list */ 100 | setobj(L, &uv->u.value, uv->v); /* move value to upvalue slot */ 101 | uv->v = &uv->u.value; /* now current value lives here */ 102 | gch(o)->next = g->allgc; /* link upvalue into 'allgc' list */ 103 | g->allgc = o; 104 | luaC_checkupvalcolor(g, uv); 105 | } 106 | } 107 | } 108 | 109 | 110 | Proto *luaF_newproto (lua_State *L) { 111 | Proto *f = &luaC_newobj(L, LUA_TPROTO, sizeof(Proto), NULL, 0)->p; 112 | f->k = NULL; 113 | f->sizek = 0; 114 | f->p = NULL; 115 | f->sizep = 0; 116 | f->code = NULL; 117 | f->cache = NULL; 118 | f->sizecode = 0; 119 | f->lineinfo = NULL; 120 | f->sizelineinfo = 0; 121 | f->upvalues = NULL; 122 | f->sizeupvalues = 0; 123 | f->numparams = 0; 124 | f->is_vararg = 0; 125 | f->maxstacksize = 0; 126 | f->locvars = NULL; 127 | f->sizelocvars = 0; 128 | f->linedefined = 0; 129 | f->lastlinedefined = 0; 130 | f->source = NULL; 131 | return f; 132 | } 133 | 134 | 135 | void luaF_freeproto (lua_State *L, Proto *f) { 136 | luaM_freearray(L, f->code, f->sizecode); 137 | luaM_freearray(L, f->p, f->sizep); 138 | luaM_freearray(L, f->k, f->sizek); 139 | luaM_freearray(L, f->lineinfo, f->sizelineinfo); 140 | luaM_freearray(L, f->locvars, f->sizelocvars); 141 | luaM_freearray(L, f->upvalues, f->sizeupvalues); 142 | luaM_free(L, f); 143 | } 144 | 145 | 146 | /* 147 | ** Look for n-th local variable at line `line' in function `func'. 148 | ** Returns NULL if not found. 149 | */ 150 | const char *luaF_getlocalname (const Proto *f, int local_number, int pc) { 151 | int i; 152 | for (i = 0; isizelocvars && f->locvars[i].startpc <= pc; i++) { 153 | if (pc < f->locvars[i].endpc) { /* is variable active? */ 154 | local_number--; 155 | if (local_number == 0) 156 | return getstr(f->locvars[i].varname); 157 | } 158 | } 159 | return NULL; /* not found */ 160 | } 161 | 162 | -------------------------------------------------------------------------------- /project/lua/lfunc.h: -------------------------------------------------------------------------------- 1 | /* 2 | ** $Id: lfunc.h,v 2.8.1.1 2013/04/12 18:48:47 roberto Exp $ 3 | ** Auxiliary functions to manipulate prototypes and closures 4 | ** See Copyright Notice in lua.h 5 | */ 6 | 7 | #ifndef lfunc_h 8 | #define lfunc_h 9 | 10 | 11 | #include "lobject.h" 12 | 13 | 14 | #define sizeCclosure(n) (cast(int, sizeof(CClosure)) + \ 15 | cast(int, sizeof(TValue)*((n)-1))) 16 | 17 | #define sizeLclosure(n) (cast(int, sizeof(LClosure)) + \ 18 | cast(int, sizeof(TValue *)*((n)-1))) 19 | 20 | 21 | LUAI_FUNC Proto *luaF_newproto (lua_State *L); 22 | LUAI_FUNC Closure *luaF_newCclosure (lua_State *L, int nelems); 23 | LUAI_FUNC Closure *luaF_newLclosure (lua_State *L, int nelems); 24 | LUAI_FUNC UpVal *luaF_newupval (lua_State *L); 25 | LUAI_FUNC UpVal *luaF_findupval (lua_State *L, StkId level); 26 | LUAI_FUNC void luaF_close (lua_State *L, StkId level); 27 | LUAI_FUNC void luaF_freeproto (lua_State *L, Proto *f); 28 | LUAI_FUNC void luaF_freeupval (lua_State *L, UpVal *uv); 29 | LUAI_FUNC const char *luaF_getlocalname (const Proto *func, int local_number, 30 | int pc); 31 | 32 | 33 | #endif 34 | -------------------------------------------------------------------------------- /project/lua/lgc.h: -------------------------------------------------------------------------------- 1 | /* 2 | ** $Id: lgc.h,v 2.58.1.1 2013/04/12 18:48:47 roberto Exp $ 3 | ** Garbage Collector 4 | ** See Copyright Notice in lua.h 5 | */ 6 | 7 | #ifndef lgc_h 8 | #define lgc_h 9 | 10 | 11 | #include "lobject.h" 12 | #include "lstate.h" 13 | 14 | /* 15 | ** Collectable objects may have one of three colors: white, which 16 | ** means the object is not marked; gray, which means the 17 | ** object is marked, but its references may be not marked; and 18 | ** black, which means that the object and all its references are marked. 19 | ** The main invariant of the garbage collector, while marking objects, 20 | ** is that a black object can never point to a white one. Moreover, 21 | ** any gray object must be in a "gray list" (gray, grayagain, weak, 22 | ** allweak, ephemeron) so that it can be visited again before finishing 23 | ** the collection cycle. These lists have no meaning when the invariant 24 | ** is not being enforced (e.g., sweep phase). 25 | */ 26 | 27 | 28 | 29 | /* how much to allocate before next GC step */ 30 | #if !defined(GCSTEPSIZE) 31 | /* ~100 small strings */ 32 | #define GCSTEPSIZE (cast_int(100 * sizeof(TString))) 33 | #endif 34 | 35 | 36 | /* 37 | ** Possible states of the Garbage Collector 38 | */ 39 | #define GCSpropagate 0 40 | #define GCSatomic 1 41 | #define GCSsweepstring 2 42 | #define GCSsweepudata 3 43 | #define GCSsweep 4 44 | #define GCSpause 5 45 | 46 | 47 | #define issweepphase(g) \ 48 | (GCSsweepstring <= (g)->gcstate && (g)->gcstate <= GCSsweep) 49 | 50 | #define isgenerational(g) ((g)->gckind == KGC_GEN) 51 | 52 | /* 53 | ** macros to tell when main invariant (white objects cannot point to black 54 | ** ones) must be kept. During a non-generational collection, the sweep 55 | ** phase may break the invariant, as objects turned white may point to 56 | ** still-black objects. The invariant is restored when sweep ends and 57 | ** all objects are white again. During a generational collection, the 58 | ** invariant must be kept all times. 59 | */ 60 | 61 | #define keepinvariant(g) (isgenerational(g) || g->gcstate <= GCSatomic) 62 | 63 | 64 | /* 65 | ** Outside the collector, the state in generational mode is kept in 66 | ** 'propagate', so 'keepinvariant' is always true. 67 | */ 68 | #define keepinvariantout(g) \ 69 | check_exp(g->gcstate == GCSpropagate || !isgenerational(g), \ 70 | g->gcstate <= GCSatomic) 71 | 72 | 73 | /* 74 | ** some useful bit tricks 75 | */ 76 | #define resetbits(x,m) ((x) &= cast(lu_byte, ~(m))) 77 | #define setbits(x,m) ((x) |= (m)) 78 | #define testbits(x,m) ((x) & (m)) 79 | #define bitmask(b) (1<<(b)) 80 | #define bit2mask(b1,b2) (bitmask(b1) | bitmask(b2)) 81 | #define l_setbit(x,b) setbits(x, bitmask(b)) 82 | #define resetbit(x,b) resetbits(x, bitmask(b)) 83 | #define testbit(x,b) testbits(x, bitmask(b)) 84 | 85 | 86 | /* Layout for bit use in `marked' field: */ 87 | #define WHITE0BIT 0 /* object is white (type 0) */ 88 | #define WHITE1BIT 1 /* object is white (type 1) */ 89 | #define BLACKBIT 2 /* object is black */ 90 | #define FINALIZEDBIT 3 /* object has been separated for finalization */ 91 | #define SEPARATED 4 /* object is in 'finobj' list or in 'tobefnz' */ 92 | #define FIXEDBIT 5 /* object is fixed (should not be collected) */ 93 | #define OLDBIT 6 /* object is old (only in generational mode) */ 94 | /* bit 7 is currently used by tests (luaL_checkmemory) */ 95 | 96 | #define WHITEBITS bit2mask(WHITE0BIT, WHITE1BIT) 97 | 98 | 99 | #define iswhite(x) testbits((x)->gch.marked, WHITEBITS) 100 | #define isblack(x) testbit((x)->gch.marked, BLACKBIT) 101 | #define isgray(x) /* neither white nor black */ \ 102 | (!testbits((x)->gch.marked, WHITEBITS | bitmask(BLACKBIT))) 103 | 104 | #define isold(x) testbit((x)->gch.marked, OLDBIT) 105 | 106 | /* MOVE OLD rule: whenever an object is moved to the beginning of 107 | a GC list, its old bit must be cleared */ 108 | #define resetoldbit(o) resetbit((o)->gch.marked, OLDBIT) 109 | 110 | #define otherwhite(g) (g->currentwhite ^ WHITEBITS) 111 | #define isdeadm(ow,m) (!(((m) ^ WHITEBITS) & (ow))) 112 | #define isdead(g,v) isdeadm(otherwhite(g), (v)->gch.marked) 113 | 114 | #define changewhite(x) ((x)->gch.marked ^= WHITEBITS) 115 | #define gray2black(x) l_setbit((x)->gch.marked, BLACKBIT) 116 | 117 | #define valiswhite(x) (iscollectable(x) && iswhite(gcvalue(x))) 118 | 119 | #define luaC_white(g) cast(lu_byte, (g)->currentwhite & WHITEBITS) 120 | 121 | 122 | #define luaC_condGC(L,c) \ 123 | {if (G(L)->GCdebt > 0) {c;}; condchangemem(L);} 124 | #define luaC_checkGC(L) luaC_condGC(L, luaC_step(L);) 125 | 126 | 127 | #define luaC_barrier(L,p,v) { if (valiswhite(v) && isblack(obj2gco(p))) \ 128 | luaC_barrier_(L,obj2gco(p),gcvalue(v)); } 129 | 130 | #define luaC_barrierback(L,p,v) { if (valiswhite(v) && isblack(obj2gco(p))) \ 131 | luaC_barrierback_(L,p); } 132 | 133 | #define luaC_objbarrier(L,p,o) \ 134 | { if (iswhite(obj2gco(o)) && isblack(obj2gco(p))) \ 135 | luaC_barrier_(L,obj2gco(p),obj2gco(o)); } 136 | 137 | #define luaC_objbarrierback(L,p,o) \ 138 | { if (iswhite(obj2gco(o)) && isblack(obj2gco(p))) luaC_barrierback_(L,p); } 139 | 140 | #define luaC_barrierproto(L,p,c) \ 141 | { if (isblack(obj2gco(p))) luaC_barrierproto_(L,p,c); } 142 | 143 | LUAI_FUNC void luaC_freeallobjects (lua_State *L); 144 | LUAI_FUNC void luaC_step (lua_State *L); 145 | LUAI_FUNC void luaC_forcestep (lua_State *L); 146 | LUAI_FUNC void luaC_runtilstate (lua_State *L, int statesmask); 147 | LUAI_FUNC void luaC_fullgc (lua_State *L, int isemergency); 148 | LUAI_FUNC GCObject *luaC_newobj (lua_State *L, int tt, size_t sz, 149 | GCObject **list, int offset); 150 | LUAI_FUNC void luaC_barrier_ (lua_State *L, GCObject *o, GCObject *v); 151 | LUAI_FUNC void luaC_barrierback_ (lua_State *L, GCObject *o); 152 | LUAI_FUNC void luaC_barrierproto_ (lua_State *L, Proto *p, Closure *c); 153 | LUAI_FUNC void luaC_checkfinalizer (lua_State *L, GCObject *o, Table *mt); 154 | LUAI_FUNC void luaC_checkupvalcolor (global_State *g, UpVal *uv); 155 | LUAI_FUNC void luaC_changemode (lua_State *L, int mode); 156 | 157 | #endif 158 | -------------------------------------------------------------------------------- /project/lua/linit.c: -------------------------------------------------------------------------------- 1 | /* 2 | ** $Id: linit.c,v 1.32.1.1 2013/04/12 18:48:47 roberto Exp $ 3 | ** Initialization of libraries for lua.c and other clients 4 | ** See Copyright Notice in lua.h 5 | */ 6 | 7 | 8 | /* 9 | ** If you embed Lua in your program and need to open the standard 10 | ** libraries, call luaL_openlibs in your program. If you need a 11 | ** different set of libraries, copy this file to your project and edit 12 | ** it to suit your needs. 13 | */ 14 | 15 | 16 | #define linit_c 17 | #define LUA_LIB 18 | 19 | #include "lua.h" 20 | 21 | #include "lualib.h" 22 | #include "lauxlib.h" 23 | 24 | 25 | /* 26 | ** these libs are loaded by lua.c and are readily available to any Lua 27 | ** program 28 | */ 29 | static const luaL_Reg loadedlibs[] = { 30 | {"_G", luaopen_base}, 31 | {LUA_LOADLIBNAME, luaopen_package}, 32 | {LUA_COLIBNAME, luaopen_coroutine}, 33 | {LUA_TABLIBNAME, luaopen_table}, 34 | {LUA_IOLIBNAME, luaopen_io}, 35 | {LUA_OSLIBNAME, luaopen_os}, 36 | {LUA_STRLIBNAME, luaopen_string}, 37 | {LUA_BITLIBNAME, luaopen_bit32}, 38 | {LUA_MATHLIBNAME, luaopen_math}, 39 | {LUA_DBLIBNAME, luaopen_debug}, 40 | {NULL, NULL} 41 | }; 42 | 43 | 44 | /* 45 | ** these libs are preloaded and must be required before used 46 | */ 47 | static const luaL_Reg preloadedlibs[] = { 48 | {NULL, NULL} 49 | }; 50 | 51 | 52 | LUALIB_API void luaL_openlibs (lua_State *L) { 53 | const luaL_Reg *lib; 54 | /* call open functions from 'loadedlibs' and set results to global table */ 55 | for (lib = loadedlibs; lib->func; lib++) { 56 | luaL_requiref(L, lib->name, lib->func, 1); 57 | lua_pop(L, 1); /* remove lib */ 58 | } 59 | /* add open functions from 'preloadedlibs' into 'package.preload' table */ 60 | luaL_getsubtable(L, LUA_REGISTRYINDEX, "_PRELOAD"); 61 | for (lib = preloadedlibs; lib->func; lib++) { 62 | lua_pushcfunction(L, lib->func); 63 | lua_setfield(L, -2, lib->name); 64 | } 65 | lua_pop(L, 1); /* remove _PRELOAD table */ 66 | } 67 | 68 | -------------------------------------------------------------------------------- /project/lua/llex.h: -------------------------------------------------------------------------------- 1 | /* 2 | ** $Id: llex.h,v 1.72.1.1 2013/04/12 18:48:47 roberto Exp $ 3 | ** Lexical Analyzer 4 | ** See Copyright Notice in lua.h 5 | */ 6 | 7 | #ifndef llex_h 8 | #define llex_h 9 | 10 | #include "lobject.h" 11 | #include "lzio.h" 12 | 13 | 14 | #define FIRST_RESERVED 257 15 | 16 | 17 | 18 | /* 19 | * WARNING: if you change the order of this enumeration, 20 | * grep "ORDER RESERVED" 21 | */ 22 | enum RESERVED { 23 | /* terminal symbols denoted by reserved words */ 24 | TK_AND = FIRST_RESERVED, TK_BREAK, 25 | TK_DO, TK_ELSE, TK_ELSEIF, TK_END, TK_FALSE, TK_FOR, TK_FUNCTION, 26 | TK_GOTO, TK_IF, TK_IN, TK_LOCAL, TK_NIL, TK_NOT, TK_OR, TK_REPEAT, 27 | TK_RETURN, TK_THEN, TK_TRUE, TK_UNTIL, TK_WHILE, 28 | /* other terminal symbols */ 29 | TK_CONCAT, TK_DOTS, TK_EQ, TK_GE, TK_LE, TK_NE, TK_DBCOLON, TK_EOS, 30 | TK_NUMBER, TK_NAME, TK_STRING 31 | }; 32 | 33 | /* number of reserved words */ 34 | #define NUM_RESERVED (cast(int, TK_WHILE-FIRST_RESERVED+1)) 35 | 36 | 37 | typedef union { 38 | lua_Number r; 39 | TString *ts; 40 | } SemInfo; /* semantics information */ 41 | 42 | 43 | typedef struct Token { 44 | int token; 45 | SemInfo seminfo; 46 | } Token; 47 | 48 | 49 | /* state of the lexer plus state of the parser when shared by all 50 | functions */ 51 | typedef struct LexState { 52 | int current; /* current character (charint) */ 53 | int linenumber; /* input line counter */ 54 | int lastline; /* line of last token `consumed' */ 55 | Token t; /* current token */ 56 | Token lookahead; /* look ahead token */ 57 | struct FuncState *fs; /* current function (parser) */ 58 | struct lua_State *L; 59 | ZIO *z; /* input stream */ 60 | Mbuffer *buff; /* buffer for tokens */ 61 | struct Dyndata *dyd; /* dynamic structures used by the parser */ 62 | TString *source; /* current source name */ 63 | TString *envn; /* environment variable name */ 64 | char decpoint; /* locale decimal point */ 65 | } LexState; 66 | 67 | 68 | LUAI_FUNC void luaX_init (lua_State *L); 69 | LUAI_FUNC void luaX_setinput (lua_State *L, LexState *ls, ZIO *z, 70 | TString *source, int firstchar); 71 | LUAI_FUNC TString *luaX_newstring (LexState *ls, const char *str, size_t l); 72 | LUAI_FUNC void luaX_next (LexState *ls); 73 | LUAI_FUNC int luaX_lookahead (LexState *ls); 74 | LUAI_FUNC l_noret luaX_syntaxerror (LexState *ls, const char *s); 75 | LUAI_FUNC const char *luaX_token2str (LexState *ls, int token); 76 | 77 | 78 | #endif 79 | -------------------------------------------------------------------------------- /project/lua/llimits.h: -------------------------------------------------------------------------------- 1 | /* 2 | ** $Id: llimits.h,v 1.103.1.1 2013/04/12 18:48:47 roberto Exp $ 3 | ** Limits, basic types, and some other `installation-dependent' definitions 4 | ** See Copyright Notice in lua.h 5 | */ 6 | 7 | #ifndef llimits_h 8 | #define llimits_h 9 | 10 | 11 | #include 12 | #include 13 | 14 | 15 | #include "lua.h" 16 | 17 | 18 | typedef unsigned LUA_INT32 lu_int32; 19 | 20 | typedef LUAI_UMEM lu_mem; 21 | 22 | typedef LUAI_MEM l_mem; 23 | 24 | 25 | 26 | /* chars used as small naturals (so that `char' is reserved for characters) */ 27 | typedef unsigned char lu_byte; 28 | 29 | 30 | #define MAX_SIZET ((size_t)(~(size_t)0)-2) 31 | 32 | #define MAX_LUMEM ((lu_mem)(~(lu_mem)0)-2) 33 | 34 | #define MAX_LMEM ((l_mem) ((MAX_LUMEM >> 1) - 2)) 35 | 36 | 37 | #define MAX_INT (INT_MAX-2) /* maximum value of an int (-2 for safety) */ 38 | 39 | /* 40 | ** conversion of pointer to integer 41 | ** this is for hashing only; there is no problem if the integer 42 | ** cannot hold the whole pointer value 43 | */ 44 | #define IntPoint(p) ((unsigned int)(lu_mem)(p)) 45 | 46 | 47 | 48 | /* type to ensure maximum alignment */ 49 | #if !defined(LUAI_USER_ALIGNMENT_T) 50 | #define LUAI_USER_ALIGNMENT_T union { double u; void *s; long l; } 51 | #endif 52 | 53 | typedef LUAI_USER_ALIGNMENT_T L_Umaxalign; 54 | 55 | 56 | /* result of a `usual argument conversion' over lua_Number */ 57 | typedef LUAI_UACNUMBER l_uacNumber; 58 | 59 | 60 | /* internal assertions for in-house debugging */ 61 | #if defined(lua_assert) 62 | #define check_exp(c,e) (lua_assert(c), (e)) 63 | /* to avoid problems with conditions too long */ 64 | #define lua_longassert(c) { if (!(c)) lua_assert(0); } 65 | #else 66 | #define lua_assert(c) ((void)0) 67 | #define check_exp(c,e) (e) 68 | #define lua_longassert(c) ((void)0) 69 | #endif 70 | 71 | /* 72 | ** assertion for checking API calls 73 | */ 74 | #if !defined(luai_apicheck) 75 | 76 | #if defined(LUA_USE_APICHECK) 77 | #include 78 | #define luai_apicheck(L,e) assert(e) 79 | #else 80 | #define luai_apicheck(L,e) lua_assert(e) 81 | #endif 82 | 83 | #endif 84 | 85 | #define api_check(l,e,msg) luai_apicheck(l,(e) && msg) 86 | 87 | 88 | #if !defined(UNUSED) 89 | #define UNUSED(x) ((void)(x)) /* to avoid warnings */ 90 | #endif 91 | 92 | 93 | #define cast(t, exp) ((t)(exp)) 94 | 95 | #define cast_byte(i) cast(lu_byte, (i)) 96 | #define cast_num(i) cast(lua_Number, (i)) 97 | #define cast_int(i) cast(int, (i)) 98 | #define cast_uchar(i) cast(unsigned char, (i)) 99 | 100 | 101 | /* 102 | ** non-return type 103 | */ 104 | #if defined(__GNUC__) 105 | #define l_noret void __attribute__((noreturn)) 106 | #elif defined(_MSC_VER) 107 | #define l_noret void __declspec(noreturn) 108 | #else 109 | #define l_noret void 110 | #endif 111 | 112 | 113 | 114 | /* 115 | ** maximum depth for nested C calls and syntactical nested non-terminals 116 | ** in a program. (Value must fit in an unsigned short int.) 117 | */ 118 | #if !defined(LUAI_MAXCCALLS) 119 | #define LUAI_MAXCCALLS 200 120 | #endif 121 | 122 | /* 123 | ** maximum number of upvalues in a closure (both C and Lua). (Value 124 | ** must fit in an unsigned char.) 125 | */ 126 | #define MAXUPVAL UCHAR_MAX 127 | 128 | 129 | /* 130 | ** type for virtual-machine instructions 131 | ** must be an unsigned with (at least) 4 bytes (see details in lopcodes.h) 132 | */ 133 | typedef lu_int32 Instruction; 134 | 135 | 136 | 137 | /* maximum stack for a Lua function */ 138 | #define MAXSTACK 250 139 | 140 | 141 | 142 | /* minimum size for the string table (must be power of 2) */ 143 | #if !defined(MINSTRTABSIZE) 144 | #define MINSTRTABSIZE 32 145 | #endif 146 | 147 | 148 | /* minimum size for string buffer */ 149 | #if !defined(LUA_MINBUFFER) 150 | #define LUA_MINBUFFER 32 151 | #endif 152 | 153 | 154 | #if !defined(lua_lock) 155 | #define lua_lock(L) ((void) 0) 156 | #define lua_unlock(L) ((void) 0) 157 | #endif 158 | 159 | #if !defined(luai_threadyield) 160 | #define luai_threadyield(L) {lua_unlock(L); lua_lock(L);} 161 | #endif 162 | 163 | 164 | /* 165 | ** these macros allow user-specific actions on threads when you defined 166 | ** LUAI_EXTRASPACE and need to do something extra when a thread is 167 | ** created/deleted/resumed/yielded. 168 | */ 169 | #if !defined(luai_userstateopen) 170 | #define luai_userstateopen(L) ((void)L) 171 | #endif 172 | 173 | #if !defined(luai_userstateclose) 174 | #define luai_userstateclose(L) ((void)L) 175 | #endif 176 | 177 | #if !defined(luai_userstatethread) 178 | #define luai_userstatethread(L,L1) ((void)L) 179 | #endif 180 | 181 | #if !defined(luai_userstatefree) 182 | #define luai_userstatefree(L,L1) ((void)L) 183 | #endif 184 | 185 | #if !defined(luai_userstateresume) 186 | #define luai_userstateresume(L,n) ((void)L) 187 | #endif 188 | 189 | #if !defined(luai_userstateyield) 190 | #define luai_userstateyield(L,n) ((void)L) 191 | #endif 192 | 193 | /* 194 | ** lua_number2int is a macro to convert lua_Number to int. 195 | ** lua_number2integer is a macro to convert lua_Number to lua_Integer. 196 | ** lua_number2unsigned is a macro to convert a lua_Number to a lua_Unsigned. 197 | ** lua_unsigned2number is a macro to convert a lua_Unsigned to a lua_Number. 198 | ** luai_hashnum is a macro to hash a lua_Number value into an integer. 199 | ** The hash must be deterministic and give reasonable values for 200 | ** both small and large values (outside the range of integers). 201 | */ 202 | 203 | #if defined(MS_ASMTRICK) || defined(LUA_MSASMTRICK) /* { */ 204 | /* trick with Microsoft assembler for X86 */ 205 | 206 | #define lua_number2int(i,n) __asm {__asm fld n __asm fistp i} 207 | #define lua_number2integer(i,n) lua_number2int(i, n) 208 | #define lua_number2unsigned(i,n) \ 209 | {__int64 l; __asm {__asm fld n __asm fistp l} i = (unsigned int)l;} 210 | 211 | 212 | #elif defined(LUA_IEEE754TRICK) /* }{ */ 213 | /* the next trick should work on any machine using IEEE754 with 214 | a 32-bit int type */ 215 | 216 | union luai_Cast { double l_d; LUA_INT32 l_p[2]; }; 217 | 218 | #if !defined(LUA_IEEEENDIAN) /* { */ 219 | #define LUAI_EXTRAIEEE \ 220 | static const union luai_Cast ieeeendian = {-(33.0 + 6755399441055744.0)}; 221 | #define LUA_IEEEENDIANLOC (ieeeendian.l_p[1] == 33) 222 | #else 223 | #define LUA_IEEEENDIANLOC LUA_IEEEENDIAN 224 | #define LUAI_EXTRAIEEE /* empty */ 225 | #endif /* } */ 226 | 227 | #define lua_number2int32(i,n,t) \ 228 | { LUAI_EXTRAIEEE \ 229 | volatile union luai_Cast u; u.l_d = (n) + 6755399441055744.0; \ 230 | (i) = (t)u.l_p[LUA_IEEEENDIANLOC]; } 231 | 232 | #define luai_hashnum(i,n) \ 233 | { volatile union luai_Cast u; u.l_d = (n) + 1.0; /* avoid -0 */ \ 234 | (i) = u.l_p[0]; (i) += u.l_p[1]; } /* add double bits for his hash */ 235 | 236 | #define lua_number2int(i,n) lua_number2int32(i, n, int) 237 | #define lua_number2unsigned(i,n) lua_number2int32(i, n, lua_Unsigned) 238 | 239 | /* the trick can be expanded to lua_Integer when it is a 32-bit value */ 240 | #if defined(LUA_IEEELL) 241 | #define lua_number2integer(i,n) lua_number2int32(i, n, lua_Integer) 242 | #endif 243 | 244 | #endif /* } */ 245 | 246 | 247 | /* the following definitions always work, but may be slow */ 248 | 249 | #if !defined(lua_number2int) 250 | #define lua_number2int(i,n) ((i)=(int)(n)) 251 | #endif 252 | 253 | #if !defined(lua_number2integer) 254 | #define lua_number2integer(i,n) ((i)=(lua_Integer)(n)) 255 | #endif 256 | 257 | #if !defined(lua_number2unsigned) /* { */ 258 | /* the following definition assures proper modulo behavior */ 259 | #if defined(LUA_NUMBER_DOUBLE) || defined(LUA_NUMBER_FLOAT) 260 | #include 261 | #define SUPUNSIGNED ((lua_Number)(~(lua_Unsigned)0) + 1) 262 | #define lua_number2unsigned(i,n) \ 263 | ((i)=(lua_Unsigned)((n) - floor((n)/SUPUNSIGNED)*SUPUNSIGNED)) 264 | #else 265 | #define lua_number2unsigned(i,n) ((i)=(lua_Unsigned)(n)) 266 | #endif 267 | #endif /* } */ 268 | 269 | 270 | #if !defined(lua_unsigned2number) 271 | /* on several machines, coercion from unsigned to double is slow, 272 | so it may be worth to avoid */ 273 | #define lua_unsigned2number(u) \ 274 | (((u) <= (lua_Unsigned)INT_MAX) ? (lua_Number)(int)(u) : (lua_Number)(u)) 275 | #endif 276 | 277 | 278 | 279 | #if defined(ltable_c) && !defined(luai_hashnum) 280 | 281 | #include 282 | #include 283 | 284 | #define luai_hashnum(i,n) { int e; \ 285 | n = l_mathop(frexp)(n, &e) * (lua_Number)(INT_MAX - DBL_MAX_EXP); \ 286 | lua_number2int(i, n); i += e; } 287 | 288 | #endif 289 | 290 | 291 | 292 | /* 293 | ** macro to control inclusion of some hard tests on stack reallocation 294 | */ 295 | #if !defined(HARDSTACKTESTS) 296 | #define condmovestack(L) ((void)0) 297 | #else 298 | /* realloc stack keeping its size */ 299 | #define condmovestack(L) luaD_reallocstack((L), (L)->stacksize) 300 | #endif 301 | 302 | #if !defined(HARDMEMTESTS) 303 | #define condchangemem(L) condmovestack(L) 304 | #else 305 | #define condchangemem(L) \ 306 | ((void)(!(G(L)->gcrunning) || (luaC_fullgc(L, 0), 1))) 307 | #endif 308 | 309 | #endif 310 | -------------------------------------------------------------------------------- /project/lua/lmathlib.c: -------------------------------------------------------------------------------- 1 | /* 2 | ** $Id: lmathlib.c,v 1.83.1.1 2013/04/12 18:48:47 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 ((lua_Number)(3.1415926535897932384626433832795)) 22 | #define RADIANS_PER_DEGREE ((lua_Number)(PI/180.0)) 23 | 24 | 25 | 26 | static int math_abs (lua_State *L) { 27 | lua_pushnumber(L, l_mathop(fabs)(luaL_checknumber(L, 1))); 28 | return 1; 29 | } 30 | 31 | static int math_sin (lua_State *L) { 32 | lua_pushnumber(L, l_mathop(sin)(luaL_checknumber(L, 1))); 33 | return 1; 34 | } 35 | 36 | static int math_sinh (lua_State *L) { 37 | lua_pushnumber(L, l_mathop(sinh)(luaL_checknumber(L, 1))); 38 | return 1; 39 | } 40 | 41 | static int math_cos (lua_State *L) { 42 | lua_pushnumber(L, l_mathop(cos)(luaL_checknumber(L, 1))); 43 | return 1; 44 | } 45 | 46 | static int math_cosh (lua_State *L) { 47 | lua_pushnumber(L, l_mathop(cosh)(luaL_checknumber(L, 1))); 48 | return 1; 49 | } 50 | 51 | static int math_tan (lua_State *L) { 52 | lua_pushnumber(L, l_mathop(tan)(luaL_checknumber(L, 1))); 53 | return 1; 54 | } 55 | 56 | static int math_tanh (lua_State *L) { 57 | lua_pushnumber(L, l_mathop(tanh)(luaL_checknumber(L, 1))); 58 | return 1; 59 | } 60 | 61 | static int math_asin (lua_State *L) { 62 | lua_pushnumber(L, l_mathop(asin)(luaL_checknumber(L, 1))); 63 | return 1; 64 | } 65 | 66 | static int math_acos (lua_State *L) { 67 | lua_pushnumber(L, l_mathop(acos)(luaL_checknumber(L, 1))); 68 | return 1; 69 | } 70 | 71 | static int math_atan (lua_State *L) { 72 | lua_pushnumber(L, l_mathop(atan)(luaL_checknumber(L, 1))); 73 | return 1; 74 | } 75 | 76 | static int math_atan2 (lua_State *L) { 77 | lua_pushnumber(L, l_mathop(atan2)(luaL_checknumber(L, 1), 78 | luaL_checknumber(L, 2))); 79 | return 1; 80 | } 81 | 82 | static int math_ceil (lua_State *L) { 83 | lua_pushnumber(L, l_mathop(ceil)(luaL_checknumber(L, 1))); 84 | return 1; 85 | } 86 | 87 | static int math_floor (lua_State *L) { 88 | lua_pushnumber(L, l_mathop(floor)(luaL_checknumber(L, 1))); 89 | return 1; 90 | } 91 | 92 | static int math_fmod (lua_State *L) { 93 | lua_pushnumber(L, l_mathop(fmod)(luaL_checknumber(L, 1), 94 | luaL_checknumber(L, 2))); 95 | return 1; 96 | } 97 | 98 | static int math_modf (lua_State *L) { 99 | lua_Number ip; 100 | lua_Number fp = l_mathop(modf)(luaL_checknumber(L, 1), &ip); 101 | lua_pushnumber(L, ip); 102 | lua_pushnumber(L, fp); 103 | return 2; 104 | } 105 | 106 | static int math_sqrt (lua_State *L) { 107 | lua_pushnumber(L, l_mathop(sqrt)(luaL_checknumber(L, 1))); 108 | return 1; 109 | } 110 | 111 | static int math_pow (lua_State *L) { 112 | lua_Number x = luaL_checknumber(L, 1); 113 | lua_Number y = luaL_checknumber(L, 2); 114 | lua_pushnumber(L, l_mathop(pow)(x, y)); 115 | return 1; 116 | } 117 | 118 | static int math_log (lua_State *L) { 119 | lua_Number x = luaL_checknumber(L, 1); 120 | lua_Number res; 121 | if (lua_isnoneornil(L, 2)) 122 | res = l_mathop(log)(x); 123 | else { 124 | lua_Number base = luaL_checknumber(L, 2); 125 | if (base == (lua_Number)10.0) res = l_mathop(log10)(x); 126 | else res = l_mathop(log)(x)/l_mathop(log)(base); 127 | } 128 | lua_pushnumber(L, res); 129 | return 1; 130 | } 131 | 132 | #if defined(LUA_COMPAT_LOG10) 133 | static int math_log10 (lua_State *L) { 134 | lua_pushnumber(L, l_mathop(log10)(luaL_checknumber(L, 1))); 135 | return 1; 136 | } 137 | #endif 138 | 139 | static int math_exp (lua_State *L) { 140 | lua_pushnumber(L, l_mathop(exp)(luaL_checknumber(L, 1))); 141 | return 1; 142 | } 143 | 144 | static int math_deg (lua_State *L) { 145 | lua_pushnumber(L, luaL_checknumber(L, 1)/RADIANS_PER_DEGREE); 146 | return 1; 147 | } 148 | 149 | static int math_rad (lua_State *L) { 150 | lua_pushnumber(L, luaL_checknumber(L, 1)*RADIANS_PER_DEGREE); 151 | return 1; 152 | } 153 | 154 | static int math_frexp (lua_State *L) { 155 | int e; 156 | lua_pushnumber(L, l_mathop(frexp)(luaL_checknumber(L, 1), &e)); 157 | lua_pushinteger(L, e); 158 | return 2; 159 | } 160 | 161 | static int math_ldexp (lua_State *L) { 162 | lua_Number x = luaL_checknumber(L, 1); 163 | int ep = luaL_checkint(L, 2); 164 | lua_pushnumber(L, l_mathop(ldexp)(x, ep)); 165 | return 1; 166 | } 167 | 168 | 169 | 170 | static int math_min (lua_State *L) { 171 | int n = lua_gettop(L); /* number of arguments */ 172 | lua_Number dmin = luaL_checknumber(L, 1); 173 | int i; 174 | for (i=2; i<=n; i++) { 175 | lua_Number d = luaL_checknumber(L, i); 176 | if (d < dmin) 177 | dmin = d; 178 | } 179 | lua_pushnumber(L, dmin); 180 | return 1; 181 | } 182 | 183 | 184 | static int math_max (lua_State *L) { 185 | int n = lua_gettop(L); /* number of arguments */ 186 | lua_Number dmax = luaL_checknumber(L, 1); 187 | int i; 188 | for (i=2; i<=n; i++) { 189 | lua_Number d = luaL_checknumber(L, i); 190 | if (d > dmax) 191 | dmax = d; 192 | } 193 | lua_pushnumber(L, dmax); 194 | return 1; 195 | } 196 | 197 | 198 | static int math_random (lua_State *L) { 199 | /* the `%' avoids the (rare) case of r==1, and is needed also because on 200 | some systems (SunOS!) `rand()' may return a value larger than RAND_MAX */ 201 | lua_Number r = (lua_Number)(rand()%RAND_MAX) / (lua_Number)RAND_MAX; 202 | switch (lua_gettop(L)) { /* check number of arguments */ 203 | case 0: { /* no arguments */ 204 | lua_pushnumber(L, r); /* Number between 0 and 1 */ 205 | break; 206 | } 207 | case 1: { /* only upper limit */ 208 | lua_Number u = luaL_checknumber(L, 1); 209 | luaL_argcheck(L, (lua_Number)1.0 <= u, 1, "interval is empty"); 210 | lua_pushnumber(L, l_mathop(floor)(r*u) + (lua_Number)(1.0)); /* [1, u] */ 211 | break; 212 | } 213 | case 2: { /* lower and upper limits */ 214 | lua_Number l = luaL_checknumber(L, 1); 215 | lua_Number u = luaL_checknumber(L, 2); 216 | luaL_argcheck(L, l <= u, 2, "interval is empty"); 217 | lua_pushnumber(L, l_mathop(floor)(r*(u-l+1)) + l); /* [l, u] */ 218 | break; 219 | } 220 | default: return luaL_error(L, "wrong number of arguments"); 221 | } 222 | return 1; 223 | } 224 | 225 | 226 | static int math_randomseed (lua_State *L) { 227 | srand(luaL_checkunsigned(L, 1)); 228 | (void)rand(); /* discard first value to avoid undesirable correlations */ 229 | return 0; 230 | } 231 | 232 | 233 | static const luaL_Reg mathlib[] = { 234 | {"abs", math_abs}, 235 | {"acos", math_acos}, 236 | {"asin", math_asin}, 237 | {"atan2", math_atan2}, 238 | {"atan", math_atan}, 239 | {"ceil", math_ceil}, 240 | {"cosh", math_cosh}, 241 | {"cos", math_cos}, 242 | {"deg", math_deg}, 243 | {"exp", math_exp}, 244 | {"floor", math_floor}, 245 | {"fmod", math_fmod}, 246 | {"frexp", math_frexp}, 247 | {"ldexp", math_ldexp}, 248 | #if defined(LUA_COMPAT_LOG10) 249 | {"log10", math_log10}, 250 | #endif 251 | {"log", math_log}, 252 | {"max", math_max}, 253 | {"min", math_min}, 254 | {"modf", math_modf}, 255 | {"pow", math_pow}, 256 | {"rad", math_rad}, 257 | {"random", math_random}, 258 | {"randomseed", math_randomseed}, 259 | {"sinh", math_sinh}, 260 | {"sin", math_sin}, 261 | {"sqrt", math_sqrt}, 262 | {"tanh", math_tanh}, 263 | {"tan", math_tan}, 264 | {NULL, NULL} 265 | }; 266 | 267 | 268 | /* 269 | ** Open math library 270 | */ 271 | LUAMOD_API int luaopen_math (lua_State *L) { 272 | luaL_newlib(L, mathlib); 273 | lua_pushnumber(L, PI); 274 | lua_setfield(L, -2, "pi"); 275 | lua_pushnumber(L, HUGE_VAL); 276 | lua_setfield(L, -2, "huge"); 277 | return 1; 278 | } 279 | 280 | -------------------------------------------------------------------------------- /project/lua/lmem.c: -------------------------------------------------------------------------------- 1 | /* 2 | ** $Id: lmem.c,v 1.84.1.1 2013/04/12 18:48:47 roberto Exp $ 3 | ** Interface to Memory Manager 4 | ** See Copyright Notice in lua.h 5 | */ 6 | 7 | 8 | #include 9 | 10 | #define lmem_c 11 | #define LUA_CORE 12 | 13 | #include "lua.h" 14 | 15 | #include "ldebug.h" 16 | #include "ldo.h" 17 | #include "lgc.h" 18 | #include "lmem.h" 19 | #include "lobject.h" 20 | #include "lstate.h" 21 | 22 | 23 | 24 | /* 25 | ** About the realloc function: 26 | ** void * frealloc (void *ud, void *ptr, size_t osize, size_t nsize); 27 | ** (`osize' is the old size, `nsize' is the new size) 28 | ** 29 | ** * frealloc(ud, NULL, x, s) creates a new block of size `s' (no 30 | ** matter 'x'). 31 | ** 32 | ** * frealloc(ud, p, x, 0) frees the block `p' 33 | ** (in this specific case, frealloc must return NULL); 34 | ** particularly, frealloc(ud, NULL, 0, 0) does nothing 35 | ** (which is equivalent to free(NULL) in ANSI C) 36 | ** 37 | ** frealloc returns NULL if it cannot create or reallocate the area 38 | ** (any reallocation to an equal or smaller size cannot fail!) 39 | */ 40 | 41 | 42 | 43 | #define MINSIZEARRAY 4 44 | 45 | 46 | void *luaM_growaux_ (lua_State *L, void *block, int *size, size_t size_elems, 47 | int limit, const char *what) { 48 | void *newblock; 49 | int newsize; 50 | if (*size >= limit/2) { /* cannot double it? */ 51 | if (*size >= limit) /* cannot grow even a little? */ 52 | luaG_runerror(L, "too many %s (limit is %d)", what, limit); 53 | newsize = limit; /* still have at least one free place */ 54 | } 55 | else { 56 | newsize = (*size)*2; 57 | if (newsize < MINSIZEARRAY) 58 | newsize = MINSIZEARRAY; /* minimum size */ 59 | } 60 | newblock = luaM_reallocv(L, block, *size, newsize, size_elems); 61 | *size = newsize; /* update only when everything else is OK */ 62 | return newblock; 63 | } 64 | 65 | 66 | l_noret luaM_toobig (lua_State *L) { 67 | luaG_runerror(L, "memory allocation error: block too big"); 68 | } 69 | 70 | 71 | 72 | /* 73 | ** generic allocation routine. 74 | */ 75 | void *luaM_realloc_ (lua_State *L, void *block, size_t osize, size_t nsize) { 76 | void *newblock; 77 | global_State *g = G(L); 78 | size_t realosize = (block) ? osize : 0; 79 | lua_assert((realosize == 0) == (block == NULL)); 80 | #if defined(HARDMEMTESTS) 81 | if (nsize > realosize && g->gcrunning) 82 | luaC_fullgc(L, 1); /* force a GC whenever possible */ 83 | #endif 84 | newblock = (*g->frealloc)(g->ud, block, osize, nsize); 85 | if (newblock == NULL && nsize > 0) { 86 | api_check(L, nsize > realosize, 87 | "realloc cannot fail when shrinking a block"); 88 | if (g->gcrunning) { 89 | luaC_fullgc(L, 1); /* try to free some memory... */ 90 | newblock = (*g->frealloc)(g->ud, block, osize, nsize); /* try again */ 91 | } 92 | if (newblock == NULL) 93 | luaD_throw(L, LUA_ERRMEM); 94 | } 95 | lua_assert((nsize == 0) == (newblock == NULL)); 96 | g->GCdebt = (g->GCdebt + nsize) - realosize; 97 | return newblock; 98 | } 99 | 100 | -------------------------------------------------------------------------------- /project/lua/lmem.h: -------------------------------------------------------------------------------- 1 | /* 2 | ** $Id: lmem.h,v 1.40.1.1 2013/04/12 18:48:47 roberto Exp $ 3 | ** Interface to Memory Manager 4 | ** See Copyright Notice in lua.h 5 | */ 6 | 7 | #ifndef lmem_h 8 | #define lmem_h 9 | 10 | 11 | #include 12 | 13 | #include "llimits.h" 14 | #include "lua.h" 15 | 16 | 17 | /* 18 | ** This macro avoids the runtime division MAX_SIZET/(e), as 'e' is 19 | ** always constant. 20 | ** The macro is somewhat complex to avoid warnings: 21 | ** +1 avoids warnings of "comparison has constant result"; 22 | ** cast to 'void' avoids warnings of "value unused". 23 | */ 24 | #define luaM_reallocv(L,b,on,n,e) \ 25 | (cast(void, \ 26 | (cast(size_t, (n)+1) > MAX_SIZET/(e)) ? (luaM_toobig(L), 0) : 0), \ 27 | luaM_realloc_(L, (b), (on)*(e), (n)*(e))) 28 | 29 | #define luaM_freemem(L, b, s) luaM_realloc_(L, (b), (s), 0) 30 | #define luaM_free(L, b) luaM_realloc_(L, (b), sizeof(*(b)), 0) 31 | #define luaM_freearray(L, b, n) luaM_reallocv(L, (b), n, 0, sizeof((b)[0])) 32 | 33 | #define luaM_malloc(L,s) luaM_realloc_(L, NULL, 0, (s)) 34 | #define luaM_new(L,t) cast(t *, luaM_malloc(L, sizeof(t))) 35 | #define luaM_newvector(L,n,t) \ 36 | cast(t *, luaM_reallocv(L, NULL, 0, n, sizeof(t))) 37 | 38 | #define luaM_newobject(L,tag,s) luaM_realloc_(L, NULL, tag, (s)) 39 | 40 | #define luaM_growvector(L,v,nelems,size,t,limit,e) \ 41 | if ((nelems)+1 > (size)) \ 42 | ((v)=cast(t *, luaM_growaux_(L,v,&(size),sizeof(t),limit,e))) 43 | 44 | #define luaM_reallocvector(L, v,oldn,n,t) \ 45 | ((v)=cast(t *, luaM_reallocv(L, v, oldn, n, sizeof(t)))) 46 | 47 | LUAI_FUNC l_noret luaM_toobig (lua_State *L); 48 | 49 | /* not to be called directly */ 50 | LUAI_FUNC void *luaM_realloc_ (lua_State *L, void *block, size_t oldsize, 51 | size_t size); 52 | LUAI_FUNC void *luaM_growaux_ (lua_State *L, void *block, int *size, 53 | size_t size_elem, int limit, 54 | const char *what); 55 | 56 | #endif 57 | 58 | -------------------------------------------------------------------------------- /project/lua/lobject.c: -------------------------------------------------------------------------------- 1 | /* 2 | ** $Id: lobject.c,v 2.58.1.1 2013/04/12 18:48:47 roberto Exp $ 3 | ** Some generic functions over Lua objects 4 | ** See Copyright Notice in lua.h 5 | */ 6 | 7 | #include 8 | #include 9 | #include 10 | #include 11 | 12 | #define lobject_c 13 | #define LUA_CORE 14 | 15 | #include "lua.h" 16 | 17 | #include "lctype.h" 18 | #include "ldebug.h" 19 | #include "ldo.h" 20 | #include "lmem.h" 21 | #include "lobject.h" 22 | #include "lstate.h" 23 | #include "lstring.h" 24 | #include "lvm.h" 25 | 26 | 27 | 28 | LUAI_DDEF const TValue luaO_nilobject_ = {NILCONSTANT}; 29 | 30 | 31 | /* 32 | ** converts an integer to a "floating point byte", represented as 33 | ** (eeeeexxx), where the real value is (1xxx) * 2^(eeeee - 1) if 34 | ** eeeee != 0 and (xxx) otherwise. 35 | */ 36 | int luaO_int2fb (unsigned int x) { 37 | int e = 0; /* exponent */ 38 | if (x < 8) return x; 39 | while (x >= 0x10) { 40 | x = (x+1) >> 1; 41 | e++; 42 | } 43 | return ((e+1) << 3) | (cast_int(x) - 8); 44 | } 45 | 46 | 47 | /* converts back */ 48 | int luaO_fb2int (int x) { 49 | int e = (x >> 3) & 0x1f; 50 | if (e == 0) return x; 51 | else return ((x & 7) + 8) << (e - 1); 52 | } 53 | 54 | 55 | int luaO_ceillog2 (unsigned int x) { 56 | static const lu_byte log_2[256] = { 57 | 0,1,2,2,3,3,3,3,4,4,4,4,4,4,4,4,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5, 58 | 6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6, 59 | 7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7, 60 | 7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7, 61 | 8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8, 62 | 8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8, 63 | 8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8, 64 | 8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8 65 | }; 66 | int l = 0; 67 | x--; 68 | while (x >= 256) { l += 8; x >>= 8; } 69 | return l + log_2[x]; 70 | } 71 | 72 | 73 | lua_Number luaO_arith (int op, lua_Number v1, lua_Number v2) { 74 | switch (op) { 75 | case LUA_OPADD: return luai_numadd(NULL, v1, v2); 76 | case LUA_OPSUB: return luai_numsub(NULL, v1, v2); 77 | case LUA_OPMUL: return luai_nummul(NULL, v1, v2); 78 | case LUA_OPDIV: return luai_numdiv(NULL, v1, v2); 79 | case LUA_OPMOD: return luai_nummod(NULL, v1, v2); 80 | case LUA_OPPOW: return luai_numpow(NULL, v1, v2); 81 | case LUA_OPUNM: return luai_numunm(NULL, v1); 82 | default: lua_assert(0); return 0; 83 | } 84 | } 85 | 86 | 87 | int luaO_hexavalue (int c) { 88 | if (lisdigit(c)) return c - '0'; 89 | else return ltolower(c) - 'a' + 10; 90 | } 91 | 92 | 93 | #if !defined(lua_strx2number) 94 | 95 | #include 96 | 97 | 98 | static int isneg (const char **s) { 99 | if (**s == '-') { (*s)++; return 1; } 100 | else if (**s == '+') (*s)++; 101 | return 0; 102 | } 103 | 104 | 105 | static lua_Number readhexa (const char **s, lua_Number r, int *count) { 106 | for (; lisxdigit(cast_uchar(**s)); (*s)++) { /* read integer part */ 107 | r = (r * cast_num(16.0)) + cast_num(luaO_hexavalue(cast_uchar(**s))); 108 | (*count)++; 109 | } 110 | return r; 111 | } 112 | 113 | 114 | /* 115 | ** convert an hexadecimal numeric string to a number, following 116 | ** C99 specification for 'strtod' 117 | */ 118 | static lua_Number lua_strx2number (const char *s, char **endptr) { 119 | lua_Number r = 0.0; 120 | int e = 0, i = 0; 121 | int neg = 0; /* 1 if number is negative */ 122 | *endptr = cast(char *, s); /* nothing is valid yet */ 123 | while (lisspace(cast_uchar(*s))) s++; /* skip initial spaces */ 124 | neg = isneg(&s); /* check signal */ 125 | if (!(*s == '0' && (*(s + 1) == 'x' || *(s + 1) == 'X'))) /* check '0x' */ 126 | return 0.0; /* invalid format (no '0x') */ 127 | s += 2; /* skip '0x' */ 128 | r = readhexa(&s, r, &i); /* read integer part */ 129 | if (*s == '.') { 130 | s++; /* skip dot */ 131 | r = readhexa(&s, r, &e); /* read fractional part */ 132 | } 133 | if (i == 0 && e == 0) 134 | return 0.0; /* invalid format (no digit) */ 135 | e *= -4; /* each fractional digit divides value by 2^-4 */ 136 | *endptr = cast(char *, s); /* valid up to here */ 137 | if (*s == 'p' || *s == 'P') { /* exponent part? */ 138 | int exp1 = 0; 139 | int neg1; 140 | s++; /* skip 'p' */ 141 | neg1 = isneg(&s); /* signal */ 142 | if (!lisdigit(cast_uchar(*s))) 143 | goto ret; /* must have at least one digit */ 144 | while (lisdigit(cast_uchar(*s))) /* read exponent */ 145 | exp1 = exp1 * 10 + *(s++) - '0'; 146 | if (neg1) exp1 = -exp1; 147 | e += exp1; 148 | } 149 | *endptr = cast(char *, s); /* valid up to here */ 150 | ret: 151 | if (neg) r = -r; 152 | return l_mathop(ldexp)(r, e); 153 | } 154 | 155 | #endif 156 | 157 | 158 | int luaO_str2d (const char *s, size_t len, lua_Number *result) { 159 | char *endptr; 160 | if (strpbrk(s, "nN")) /* reject 'inf' and 'nan' */ 161 | return 0; 162 | else if (strpbrk(s, "xX")) /* hexa? */ 163 | *result = lua_strx2number(s, &endptr); 164 | else 165 | *result = lua_str2number(s, &endptr); 166 | if (endptr == s) return 0; /* nothing recognized */ 167 | while (lisspace(cast_uchar(*endptr))) endptr++; 168 | return (endptr == s + len); /* OK if no trailing characters */ 169 | } 170 | 171 | 172 | 173 | static void pushstr (lua_State *L, const char *str, size_t l) { 174 | setsvalue2s(L, L->top++, luaS_newlstr(L, str, l)); 175 | } 176 | 177 | 178 | /* this function handles only `%d', `%c', %f, %p, and `%s' formats */ 179 | const char *luaO_pushvfstring (lua_State *L, const char *fmt, va_list argp) { 180 | int n = 0; 181 | for (;;) { 182 | const char *e = strchr(fmt, '%'); 183 | if (e == NULL) break; 184 | luaD_checkstack(L, 2); /* fmt + item */ 185 | pushstr(L, fmt, e - fmt); 186 | switch (*(e+1)) { 187 | case 's': { 188 | const char *s = va_arg(argp, char *); 189 | if (s == NULL) s = "(null)"; 190 | pushstr(L, s, strlen(s)); 191 | break; 192 | } 193 | case 'c': { 194 | char buff; 195 | buff = cast(char, va_arg(argp, int)); 196 | pushstr(L, &buff, 1); 197 | break; 198 | } 199 | case 'd': { 200 | setnvalue(L->top++, cast_num(va_arg(argp, int))); 201 | break; 202 | } 203 | case 'f': { 204 | setnvalue(L->top++, cast_num(va_arg(argp, l_uacNumber))); 205 | break; 206 | } 207 | case 'p': { 208 | char buff[4*sizeof(void *) + 8]; /* should be enough space for a `%p' */ 209 | int l = sprintf(buff, "%p", va_arg(argp, void *)); 210 | pushstr(L, buff, l); 211 | break; 212 | } 213 | case '%': { 214 | pushstr(L, "%", 1); 215 | break; 216 | } 217 | default: { 218 | luaG_runerror(L, 219 | "invalid option " LUA_QL("%%%c") " to " LUA_QL("lua_pushfstring"), 220 | *(e + 1)); 221 | } 222 | } 223 | n += 2; 224 | fmt = e+2; 225 | } 226 | luaD_checkstack(L, 1); 227 | pushstr(L, fmt, strlen(fmt)); 228 | if (n > 0) luaV_concat(L, n + 1); 229 | return svalue(L->top - 1); 230 | } 231 | 232 | 233 | const char *luaO_pushfstring (lua_State *L, const char *fmt, ...) { 234 | const char *msg; 235 | va_list argp; 236 | va_start(argp, fmt); 237 | msg = luaO_pushvfstring(L, fmt, argp); 238 | va_end(argp); 239 | return msg; 240 | } 241 | 242 | 243 | /* number of chars of a literal string without the ending \0 */ 244 | #define LL(x) (sizeof(x)/sizeof(char) - 1) 245 | 246 | #define RETS "..." 247 | #define PRE "[string \"" 248 | #define POS "\"]" 249 | 250 | #define addstr(a,b,l) ( memcpy(a,b,(l) * sizeof(char)), a += (l) ) 251 | 252 | void luaO_chunkid (char *out, const char *source, size_t bufflen) { 253 | size_t l = strlen(source); 254 | if (*source == '=') { /* 'literal' source */ 255 | if (l <= bufflen) /* small enough? */ 256 | memcpy(out, source + 1, l * sizeof(char)); 257 | else { /* truncate it */ 258 | addstr(out, source + 1, bufflen - 1); 259 | *out = '\0'; 260 | } 261 | } 262 | else if (*source == '@') { /* file name */ 263 | if (l <= bufflen) /* small enough? */ 264 | memcpy(out, source + 1, l * sizeof(char)); 265 | else { /* add '...' before rest of name */ 266 | addstr(out, RETS, LL(RETS)); 267 | bufflen -= LL(RETS); 268 | memcpy(out, source + 1 + l - bufflen, bufflen * sizeof(char)); 269 | } 270 | } 271 | else { /* string; format as [string "source"] */ 272 | const char *nl = strchr(source, '\n'); /* find first new line (if any) */ 273 | addstr(out, PRE, LL(PRE)); /* add prefix */ 274 | bufflen -= LL(PRE RETS POS) + 1; /* save space for prefix+suffix+'\0' */ 275 | if (l < bufflen && nl == NULL) { /* small one-line source? */ 276 | addstr(out, source, l); /* keep it */ 277 | } 278 | else { 279 | if (nl != NULL) l = nl - source; /* stop at first newline */ 280 | if (l > bufflen) l = bufflen; 281 | addstr(out, source, l); 282 | addstr(out, RETS, LL(RETS)); 283 | } 284 | memcpy(out, POS, (LL(POS) + 1) * sizeof(char)); 285 | } 286 | } 287 | 288 | -------------------------------------------------------------------------------- /project/lua/lopcodes.c: -------------------------------------------------------------------------------- 1 | /* 2 | ** $Id: lopcodes.c,v 1.49.1.1 2013/04/12 18:48:47 roberto Exp $ 3 | ** Opcodes for Lua virtual machine 4 | ** See Copyright Notice in lua.h 5 | */ 6 | 7 | 8 | #define lopcodes_c 9 | #define LUA_CORE 10 | 11 | 12 | #include "lopcodes.h" 13 | 14 | 15 | /* ORDER OP */ 16 | 17 | LUAI_DDEF const char *const luaP_opnames[NUM_OPCODES+1] = { 18 | "MOVE", 19 | "LOADK", 20 | "LOADKX", 21 | "LOADBOOL", 22 | "LOADNIL", 23 | "GETUPVAL", 24 | "GETTABUP", 25 | "GETTABLE", 26 | "SETTABUP", 27 | "SETUPVAL", 28 | "SETTABLE", 29 | "NEWTABLE", 30 | "SELF", 31 | "ADD", 32 | "SUB", 33 | "MUL", 34 | "DIV", 35 | "MOD", 36 | "POW", 37 | "UNM", 38 | "NOT", 39 | "LEN", 40 | "CONCAT", 41 | "JMP", 42 | "EQ", 43 | "LT", 44 | "LE", 45 | "TEST", 46 | "TESTSET", 47 | "CALL", 48 | "TAILCALL", 49 | "RETURN", 50 | "FORLOOP", 51 | "FORPREP", 52 | "TFORCALL", 53 | "TFORLOOP", 54 | "SETLIST", 55 | "CLOSURE", 56 | "VARARG", 57 | "EXTRAARG", 58 | NULL 59 | }; 60 | 61 | 62 | #define opmode(t,a,b,c,m) (((t)<<7) | ((a)<<6) | ((b)<<4) | ((c)<<2) | (m)) 63 | 64 | LUAI_DDEF const lu_byte luaP_opmodes[NUM_OPCODES] = { 65 | /* T A B C mode opcode */ 66 | opmode(0, 1, OpArgR, OpArgN, iABC) /* OP_MOVE */ 67 | ,opmode(0, 1, OpArgK, OpArgN, iABx) /* OP_LOADK */ 68 | ,opmode(0, 1, OpArgN, OpArgN, iABx) /* OP_LOADKX */ 69 | ,opmode(0, 1, OpArgU, OpArgU, iABC) /* OP_LOADBOOL */ 70 | ,opmode(0, 1, OpArgU, OpArgN, iABC) /* OP_LOADNIL */ 71 | ,opmode(0, 1, OpArgU, OpArgN, iABC) /* OP_GETUPVAL */ 72 | ,opmode(0, 1, OpArgU, OpArgK, iABC) /* OP_GETTABUP */ 73 | ,opmode(0, 1, OpArgR, OpArgK, iABC) /* OP_GETTABLE */ 74 | ,opmode(0, 0, OpArgK, OpArgK, iABC) /* OP_SETTABUP */ 75 | ,opmode(0, 0, OpArgU, OpArgN, iABC) /* OP_SETUPVAL */ 76 | ,opmode(0, 0, OpArgK, OpArgK, iABC) /* OP_SETTABLE */ 77 | ,opmode(0, 1, OpArgU, OpArgU, iABC) /* OP_NEWTABLE */ 78 | ,opmode(0, 1, OpArgR, OpArgK, iABC) /* OP_SELF */ 79 | ,opmode(0, 1, OpArgK, OpArgK, iABC) /* OP_ADD */ 80 | ,opmode(0, 1, OpArgK, OpArgK, iABC) /* OP_SUB */ 81 | ,opmode(0, 1, OpArgK, OpArgK, iABC) /* OP_MUL */ 82 | ,opmode(0, 1, OpArgK, OpArgK, iABC) /* OP_DIV */ 83 | ,opmode(0, 1, OpArgK, OpArgK, iABC) /* OP_MOD */ 84 | ,opmode(0, 1, OpArgK, OpArgK, iABC) /* OP_POW */ 85 | ,opmode(0, 1, OpArgR, OpArgN, iABC) /* OP_UNM */ 86 | ,opmode(0, 1, OpArgR, OpArgN, iABC) /* OP_NOT */ 87 | ,opmode(0, 1, OpArgR, OpArgN, iABC) /* OP_LEN */ 88 | ,opmode(0, 1, OpArgR, OpArgR, iABC) /* OP_CONCAT */ 89 | ,opmode(0, 0, OpArgR, OpArgN, iAsBx) /* OP_JMP */ 90 | ,opmode(1, 0, OpArgK, OpArgK, iABC) /* OP_EQ */ 91 | ,opmode(1, 0, OpArgK, OpArgK, iABC) /* OP_LT */ 92 | ,opmode(1, 0, OpArgK, OpArgK, iABC) /* OP_LE */ 93 | ,opmode(1, 0, OpArgN, OpArgU, iABC) /* OP_TEST */ 94 | ,opmode(1, 1, OpArgR, OpArgU, iABC) /* OP_TESTSET */ 95 | ,opmode(0, 1, OpArgU, OpArgU, iABC) /* OP_CALL */ 96 | ,opmode(0, 1, OpArgU, OpArgU, iABC) /* OP_TAILCALL */ 97 | ,opmode(0, 0, OpArgU, OpArgN, iABC) /* OP_RETURN */ 98 | ,opmode(0, 1, OpArgR, OpArgN, iAsBx) /* OP_FORLOOP */ 99 | ,opmode(0, 1, OpArgR, OpArgN, iAsBx) /* OP_FORPREP */ 100 | ,opmode(0, 0, OpArgN, OpArgU, iABC) /* OP_TFORCALL */ 101 | ,opmode(0, 1, OpArgR, OpArgN, iAsBx) /* OP_TFORLOOP */ 102 | ,opmode(0, 0, OpArgU, OpArgU, iABC) /* OP_SETLIST */ 103 | ,opmode(0, 1, OpArgU, OpArgN, iABx) /* OP_CLOSURE */ 104 | ,opmode(0, 1, OpArgU, OpArgN, iABC) /* OP_VARARG */ 105 | ,opmode(0, 0, OpArgU, OpArgU, iAx) /* OP_EXTRAARG */ 106 | }; 107 | 108 | -------------------------------------------------------------------------------- /project/lua/lopcodes.h: -------------------------------------------------------------------------------- 1 | /* 2 | ** $Id: lopcodes.h,v 1.142.1.1 2013/04/12 18:48:47 roberto Exp $ 3 | ** Opcodes for Lua virtual machine 4 | ** See Copyright Notice in lua.h 5 | */ 6 | 7 | #ifndef lopcodes_h 8 | #define lopcodes_h 9 | 10 | #include "llimits.h" 11 | 12 | 13 | /*=========================================================================== 14 | We assume that instructions are unsigned numbers. 15 | All instructions have an opcode in the first 6 bits. 16 | Instructions can have the following fields: 17 | `A' : 8 bits 18 | `B' : 9 bits 19 | `C' : 9 bits 20 | 'Ax' : 26 bits ('A', 'B', and 'C' together) 21 | `Bx' : 18 bits (`B' and `C' together) 22 | `sBx' : signed Bx 23 | 24 | A signed argument is represented in excess K; that is, the number 25 | value is the unsigned value minus K. K is exactly the maximum value 26 | for that argument (so that -max is represented by 0, and +max is 27 | represented by 2*max), which is half the maximum for the corresponding 28 | unsigned argument. 29 | ===========================================================================*/ 30 | 31 | 32 | enum OpMode {iABC, iABx, iAsBx, iAx}; /* basic instruction format */ 33 | 34 | 35 | /* 36 | ** size and position of opcode arguments. 37 | */ 38 | #define SIZE_C 9 39 | #define SIZE_B 9 40 | #define SIZE_Bx (SIZE_C + SIZE_B) 41 | #define SIZE_A 8 42 | #define SIZE_Ax (SIZE_C + SIZE_B + SIZE_A) 43 | 44 | #define SIZE_OP 6 45 | 46 | #define POS_OP 0 47 | #define POS_A (POS_OP + SIZE_OP) 48 | #define POS_C (POS_A + SIZE_A) 49 | #define POS_B (POS_C + SIZE_C) 50 | #define POS_Bx POS_C 51 | #define POS_Ax POS_A 52 | 53 | 54 | /* 55 | ** limits for opcode arguments. 56 | ** we use (signed) int to manipulate most arguments, 57 | ** so they must fit in LUAI_BITSINT-1 bits (-1 for sign) 58 | */ 59 | #if SIZE_Bx < LUAI_BITSINT-1 60 | #define MAXARG_Bx ((1<>1) /* `sBx' is signed */ 62 | #else 63 | #define MAXARG_Bx MAX_INT 64 | #define MAXARG_sBx MAX_INT 65 | #endif 66 | 67 | #if SIZE_Ax < LUAI_BITSINT-1 68 | #define MAXARG_Ax ((1<>POS_OP) & MASK1(SIZE_OP,0))) 90 | #define SET_OPCODE(i,o) ((i) = (((i)&MASK0(SIZE_OP,POS_OP)) | \ 91 | ((cast(Instruction, o)<>pos) & MASK1(size,0))) 94 | #define setarg(i,v,pos,size) ((i) = (((i)&MASK0(size,pos)) | \ 95 | ((cast(Instruction, v)<= R(A) + 1 */ 200 | OP_EQ,/* A B C if ((RK(B) == RK(C)) ~= A) then pc++ */ 201 | OP_LT,/* A B C if ((RK(B) < RK(C)) ~= A) then pc++ */ 202 | OP_LE,/* A B C if ((RK(B) <= RK(C)) ~= A) then pc++ */ 203 | 204 | OP_TEST,/* A C if not (R(A) <=> C) then pc++ */ 205 | OP_TESTSET,/* A B C if (R(B) <=> C) then R(A) := R(B) else pc++ */ 206 | 207 | OP_CALL,/* A B C R(A), ... ,R(A+C-2) := R(A)(R(A+1), ... ,R(A+B-1)) */ 208 | OP_TAILCALL,/* A B C return R(A)(R(A+1), ... ,R(A+B-1)) */ 209 | OP_RETURN,/* A B return R(A), ... ,R(A+B-2) (see note) */ 210 | 211 | OP_FORLOOP,/* A sBx R(A)+=R(A+2); 212 | if R(A) > 4) & 3)) 276 | #define getCMode(m) (cast(enum OpArgMask, (luaP_opmodes[m] >> 2) & 3)) 277 | #define testAMode(m) (luaP_opmodes[m] & (1 << 6)) 278 | #define testTMode(m) (luaP_opmodes[m] & (1 << 7)) 279 | 280 | 281 | LUAI_DDEC const char *const luaP_opnames[NUM_OPCODES+1]; /* opcode names */ 282 | 283 | 284 | /* number of list items to accumulate before a SETLIST instruction */ 285 | #define LFIELDS_PER_FLUSH 50 286 | 287 | 288 | #endif 289 | -------------------------------------------------------------------------------- /project/lua/loslib.c: -------------------------------------------------------------------------------- 1 | /* 2 | ** $Id: loslib.c,v 1.40.1.1 2013/04/12 18:48:47 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 | /* 24 | ** list of valid conversion specifiers for the 'strftime' function 25 | */ 26 | #if !defined(LUA_STRFTIMEOPTIONS) 27 | 28 | #if !defined(LUA_USE_POSIX) 29 | #define LUA_STRFTIMEOPTIONS { "aAbBcdHIjmMpSUwWxXyYz%", "" } 30 | #else 31 | #define LUA_STRFTIMEOPTIONS \ 32 | { "aAbBcCdDeFgGhHIjmMnprRStTuUVwWxXyYzZ%", "" \ 33 | "", "E", "cCxXyY", \ 34 | "O", "deHImMSuUVwWy" } 35 | #endif 36 | 37 | #endif 38 | 39 | 40 | 41 | /* 42 | ** By default, Lua uses tmpnam except when POSIX is available, where it 43 | ** uses mkstemp. 44 | */ 45 | #if defined(LUA_USE_MKSTEMP) 46 | #include 47 | #define LUA_TMPNAMBUFSIZE 32 48 | #define lua_tmpnam(b,e) { \ 49 | strcpy(b, "/tmp/lua_XXXXXX"); \ 50 | e = mkstemp(b); \ 51 | if (e != -1) close(e); \ 52 | e = (e == -1); } 53 | 54 | #elif !defined(lua_tmpnam) 55 | 56 | #define LUA_TMPNAMBUFSIZE L_tmpnam 57 | #define lua_tmpnam(b,e) { e = (tmpnam(b) == NULL); } 58 | 59 | #endif 60 | 61 | 62 | /* 63 | ** By default, Lua uses gmtime/localtime, except when POSIX is available, 64 | ** where it uses gmtime_r/localtime_r 65 | */ 66 | #if defined(LUA_USE_GMTIME_R) 67 | 68 | #define l_gmtime(t,r) gmtime_r(t,r) 69 | #define l_localtime(t,r) localtime_r(t,r) 70 | 71 | #elif !defined(l_gmtime) 72 | 73 | #define l_gmtime(t,r) ((void)r, gmtime(t)) 74 | #define l_localtime(t,r) ((void)r, localtime(t)) 75 | 76 | #endif 77 | 78 | 79 | 80 | static int os_execute (lua_State *L) { 81 | const char *cmd = luaL_optstring(L, 1, NULL); 82 | int stat = system(cmd); 83 | if (cmd != NULL) 84 | return luaL_execresult(L, stat); 85 | else { 86 | lua_pushboolean(L, stat); /* true if there is a shell */ 87 | return 1; 88 | } 89 | } 90 | 91 | 92 | static int os_remove (lua_State *L) { 93 | const char *filename = luaL_checkstring(L, 1); 94 | return luaL_fileresult(L, remove(filename) == 0, filename); 95 | } 96 | 97 | 98 | static int os_rename (lua_State *L) { 99 | const char *fromname = luaL_checkstring(L, 1); 100 | const char *toname = luaL_checkstring(L, 2); 101 | return luaL_fileresult(L, rename(fromname, toname) == 0, NULL); 102 | } 103 | 104 | 105 | static int os_tmpname (lua_State *L) { 106 | char buff[LUA_TMPNAMBUFSIZE]; 107 | int err; 108 | lua_tmpnam(buff, err); 109 | if (err) 110 | return luaL_error(L, "unable to generate a unique filename"); 111 | lua_pushstring(L, buff); 112 | return 1; 113 | } 114 | 115 | 116 | static int os_getenv (lua_State *L) { 117 | lua_pushstring(L, getenv(luaL_checkstring(L, 1))); /* if NULL push nil */ 118 | return 1; 119 | } 120 | 121 | 122 | static int os_clock (lua_State *L) { 123 | lua_pushnumber(L, ((lua_Number)clock())/(lua_Number)CLOCKS_PER_SEC); 124 | return 1; 125 | } 126 | 127 | 128 | /* 129 | ** {====================================================== 130 | ** Time/Date operations 131 | ** { year=%Y, month=%m, day=%d, hour=%H, min=%M, sec=%S, 132 | ** wday=%w+1, yday=%j, isdst=? } 133 | ** ======================================================= 134 | */ 135 | 136 | static void setfield (lua_State *L, const char *key, int value) { 137 | lua_pushinteger(L, value); 138 | lua_setfield(L, -2, key); 139 | } 140 | 141 | static void setboolfield (lua_State *L, const char *key, int value) { 142 | if (value < 0) /* undefined? */ 143 | return; /* does not set field */ 144 | lua_pushboolean(L, value); 145 | lua_setfield(L, -2, key); 146 | } 147 | 148 | static int getboolfield (lua_State *L, const char *key) { 149 | int res; 150 | lua_getfield(L, -1, key); 151 | res = lua_isnil(L, -1) ? -1 : lua_toboolean(L, -1); 152 | lua_pop(L, 1); 153 | return res; 154 | } 155 | 156 | 157 | static int getfield (lua_State *L, const char *key, int d) { 158 | int res, isnum; 159 | lua_getfield(L, -1, key); 160 | res = (int)lua_tointegerx(L, -1, &isnum); 161 | if (!isnum) { 162 | if (d < 0) 163 | return luaL_error(L, "field " LUA_QS " missing in date table", key); 164 | res = d; 165 | } 166 | lua_pop(L, 1); 167 | return res; 168 | } 169 | 170 | 171 | static const char *checkoption (lua_State *L, const char *conv, char *buff) { 172 | static const char *const options[] = LUA_STRFTIMEOPTIONS; 173 | unsigned int i; 174 | for (i = 0; i < sizeof(options)/sizeof(options[0]); i += 2) { 175 | if (*conv != '\0' && strchr(options[i], *conv) != NULL) { 176 | buff[1] = *conv; 177 | if (*options[i + 1] == '\0') { /* one-char conversion specifier? */ 178 | buff[2] = '\0'; /* end buffer */ 179 | return conv + 1; 180 | } 181 | else if (*(conv + 1) != '\0' && 182 | strchr(options[i + 1], *(conv + 1)) != NULL) { 183 | buff[2] = *(conv + 1); /* valid two-char conversion specifier */ 184 | buff[3] = '\0'; /* end buffer */ 185 | return conv + 2; 186 | } 187 | } 188 | } 189 | luaL_argerror(L, 1, 190 | lua_pushfstring(L, "invalid conversion specifier '%%%s'", conv)); 191 | return conv; /* to avoid warnings */ 192 | } 193 | 194 | 195 | static int os_date (lua_State *L) { 196 | const char *s = luaL_optstring(L, 1, "%c"); 197 | time_t t = luaL_opt(L, (time_t)luaL_checknumber, 2, time(NULL)); 198 | struct tm tmr, *stm; 199 | if (*s == '!') { /* UTC? */ 200 | stm = l_gmtime(&t, &tmr); 201 | s++; /* skip `!' */ 202 | } 203 | else 204 | stm = l_localtime(&t, &tmr); 205 | if (stm == NULL) /* invalid date? */ 206 | lua_pushnil(L); 207 | else if (strcmp(s, "*t") == 0) { 208 | lua_createtable(L, 0, 9); /* 9 = number of fields */ 209 | setfield(L, "sec", stm->tm_sec); 210 | setfield(L, "min", stm->tm_min); 211 | setfield(L, "hour", stm->tm_hour); 212 | setfield(L, "day", stm->tm_mday); 213 | setfield(L, "month", stm->tm_mon+1); 214 | setfield(L, "year", stm->tm_year+1900); 215 | setfield(L, "wday", stm->tm_wday+1); 216 | setfield(L, "yday", stm->tm_yday+1); 217 | setboolfield(L, "isdst", stm->tm_isdst); 218 | } 219 | else { 220 | char cc[4]; 221 | luaL_Buffer b; 222 | cc[0] = '%'; 223 | luaL_buffinit(L, &b); 224 | while (*s) { 225 | if (*s != '%') /* no conversion specifier? */ 226 | luaL_addchar(&b, *s++); 227 | else { 228 | size_t reslen; 229 | char buff[200]; /* should be big enough for any conversion result */ 230 | s = checkoption(L, s + 1, cc); 231 | reslen = strftime(buff, sizeof(buff), cc, stm); 232 | luaL_addlstring(&b, buff, reslen); 233 | } 234 | } 235 | luaL_pushresult(&b); 236 | } 237 | return 1; 238 | } 239 | 240 | 241 | static int os_time (lua_State *L) { 242 | time_t t; 243 | if (lua_isnoneornil(L, 1)) /* called without args? */ 244 | t = time(NULL); /* get current time */ 245 | else { 246 | struct tm ts; 247 | luaL_checktype(L, 1, LUA_TTABLE); 248 | lua_settop(L, 1); /* make sure table is at the top */ 249 | ts.tm_sec = getfield(L, "sec", 0); 250 | ts.tm_min = getfield(L, "min", 0); 251 | ts.tm_hour = getfield(L, "hour", 12); 252 | ts.tm_mday = getfield(L, "day", -1); 253 | ts.tm_mon = getfield(L, "month", -1) - 1; 254 | ts.tm_year = getfield(L, "year", -1) - 1900; 255 | ts.tm_isdst = getboolfield(L, "isdst"); 256 | t = mktime(&ts); 257 | } 258 | if (t == (time_t)(-1)) 259 | lua_pushnil(L); 260 | else 261 | lua_pushnumber(L, (lua_Number)t); 262 | return 1; 263 | } 264 | 265 | 266 | static int os_difftime (lua_State *L) { 267 | lua_pushnumber(L, difftime((time_t)(luaL_checknumber(L, 1)), 268 | (time_t)(luaL_optnumber(L, 2, 0)))); 269 | return 1; 270 | } 271 | 272 | /* }====================================================== */ 273 | 274 | 275 | static int os_setlocale (lua_State *L) { 276 | static const int cat[] = {LC_ALL, LC_COLLATE, LC_CTYPE, LC_MONETARY, 277 | LC_NUMERIC, LC_TIME}; 278 | static const char *const catnames[] = {"all", "collate", "ctype", "monetary", 279 | "numeric", "time", NULL}; 280 | const char *l = luaL_optstring(L, 1, NULL); 281 | int op = luaL_checkoption(L, 2, "all", catnames); 282 | lua_pushstring(L, setlocale(cat[op], l)); 283 | return 1; 284 | } 285 | 286 | 287 | static int os_exit (lua_State *L) { 288 | int status; 289 | if (lua_isboolean(L, 1)) 290 | status = (lua_toboolean(L, 1) ? EXIT_SUCCESS : EXIT_FAILURE); 291 | else 292 | status = luaL_optint(L, 1, EXIT_SUCCESS); 293 | if (lua_toboolean(L, 2)) 294 | lua_close(L); 295 | if (L) exit(status); /* 'if' to avoid warnings for unreachable 'return' */ 296 | return 0; 297 | } 298 | 299 | 300 | static const luaL_Reg syslib[] = { 301 | {"clock", os_clock}, 302 | {"date", os_date}, 303 | {"difftime", os_difftime}, 304 | {"execute", os_execute}, 305 | {"exit", os_exit}, 306 | {"getenv", os_getenv}, 307 | {"remove", os_remove}, 308 | {"rename", os_rename}, 309 | {"setlocale", os_setlocale}, 310 | {"time", os_time}, 311 | {"tmpname", os_tmpname}, 312 | {NULL, NULL} 313 | }; 314 | 315 | /* }====================================================== */ 316 | 317 | 318 | 319 | LUAMOD_API int luaopen_os (lua_State *L) { 320 | luaL_newlib(L, syslib); 321 | return 1; 322 | } 323 | 324 | -------------------------------------------------------------------------------- /project/lua/lparser.h: -------------------------------------------------------------------------------- 1 | /* 2 | ** $Id: lparser.h,v 1.70.1.1 2013/04/12 18:48:47 roberto Exp $ 3 | ** Lua Parser 4 | ** See Copyright Notice in lua.h 5 | */ 6 | 7 | #ifndef lparser_h 8 | #define lparser_h 9 | 10 | #include "llimits.h" 11 | #include "lobject.h" 12 | #include "lzio.h" 13 | 14 | 15 | /* 16 | ** Expression descriptor 17 | */ 18 | 19 | typedef enum { 20 | VVOID, /* no value */ 21 | VNIL, 22 | VTRUE, 23 | VFALSE, 24 | VK, /* info = index of constant in `k' */ 25 | VKNUM, /* nval = numerical value */ 26 | VNONRELOC, /* info = result register */ 27 | VLOCAL, /* info = local register */ 28 | VUPVAL, /* info = index of upvalue in 'upvalues' */ 29 | VINDEXED, /* t = table register/upvalue; idx = index R/K */ 30 | VJMP, /* info = instruction pc */ 31 | VRELOCABLE, /* info = instruction pc */ 32 | VCALL, /* info = instruction pc */ 33 | VVARARG /* info = instruction pc */ 34 | } expkind; 35 | 36 | 37 | #define vkisvar(k) (VLOCAL <= (k) && (k) <= VINDEXED) 38 | #define vkisinreg(k) ((k) == VNONRELOC || (k) == VLOCAL) 39 | 40 | typedef struct expdesc { 41 | expkind k; 42 | union { 43 | struct { /* for indexed variables (VINDEXED) */ 44 | short idx; /* index (R/K) */ 45 | lu_byte t; /* table (register or upvalue) */ 46 | lu_byte vt; /* whether 't' is register (VLOCAL) or upvalue (VUPVAL) */ 47 | } ind; 48 | int info; /* for generic use */ 49 | lua_Number nval; /* for VKNUM */ 50 | } u; 51 | int t; /* patch list of `exit when true' */ 52 | int f; /* patch list of `exit when false' */ 53 | } expdesc; 54 | 55 | 56 | /* description of active local variable */ 57 | typedef struct Vardesc { 58 | short idx; /* variable index in stack */ 59 | } Vardesc; 60 | 61 | 62 | /* description of pending goto statements and label statements */ 63 | typedef struct Labeldesc { 64 | TString *name; /* label identifier */ 65 | int pc; /* position in code */ 66 | int line; /* line where it appeared */ 67 | lu_byte nactvar; /* local level where it appears in current block */ 68 | } Labeldesc; 69 | 70 | 71 | /* list of labels or gotos */ 72 | typedef struct Labellist { 73 | Labeldesc *arr; /* array */ 74 | int n; /* number of entries in use */ 75 | int size; /* array size */ 76 | } Labellist; 77 | 78 | 79 | /* dynamic structures used by the parser */ 80 | typedef struct Dyndata { 81 | struct { /* list of active local variables */ 82 | Vardesc *arr; 83 | int n; 84 | int size; 85 | } actvar; 86 | Labellist gt; /* list of pending gotos */ 87 | Labellist label; /* list of active labels */ 88 | } Dyndata; 89 | 90 | 91 | /* control of blocks */ 92 | struct BlockCnt; /* defined in lparser.c */ 93 | 94 | 95 | /* state needed to generate code for a given function */ 96 | typedef struct FuncState { 97 | Proto *f; /* current function header */ 98 | Table *h; /* table to find (and reuse) elements in `k' */ 99 | struct FuncState *prev; /* enclosing function */ 100 | struct LexState *ls; /* lexical state */ 101 | struct BlockCnt *bl; /* chain of current blocks */ 102 | int pc; /* next position to code (equivalent to `ncode') */ 103 | int lasttarget; /* 'label' of last 'jump label' */ 104 | int jpc; /* list of pending jumps to `pc' */ 105 | int nk; /* number of elements in `k' */ 106 | int np; /* number of elements in `p' */ 107 | int firstlocal; /* index of first local var (in Dyndata array) */ 108 | short nlocvars; /* number of elements in 'f->locvars' */ 109 | lu_byte nactvar; /* number of active local variables */ 110 | lu_byte nups; /* number of upvalues */ 111 | lu_byte freereg; /* first free register */ 112 | } FuncState; 113 | 114 | 115 | LUAI_FUNC Closure *luaY_parser (lua_State *L, ZIO *z, Mbuffer *buff, 116 | Dyndata *dyd, const char *name, int firstchar); 117 | 118 | 119 | #endif 120 | -------------------------------------------------------------------------------- /project/lua/lstate.c: -------------------------------------------------------------------------------- 1 | /* 2 | ** $Id: lstate.c,v 2.99.1.2 2013/11/08 17:45:31 roberto Exp $ 3 | ** Global State 4 | ** See Copyright Notice in lua.h 5 | */ 6 | 7 | 8 | #include 9 | #include 10 | 11 | #define lstate_c 12 | #define LUA_CORE 13 | 14 | #include "lua.h" 15 | 16 | #include "lapi.h" 17 | #include "ldebug.h" 18 | #include "ldo.h" 19 | #include "lfunc.h" 20 | #include "lgc.h" 21 | #include "llex.h" 22 | #include "lmem.h" 23 | #include "lstate.h" 24 | #include "lstring.h" 25 | #include "ltable.h" 26 | #include "ltm.h" 27 | 28 | 29 | #if !defined(LUAI_GCPAUSE) 30 | #define LUAI_GCPAUSE 200 /* 200% */ 31 | #endif 32 | 33 | #if !defined(LUAI_GCMAJOR) 34 | #define LUAI_GCMAJOR 200 /* 200% */ 35 | #endif 36 | 37 | #if !defined(LUAI_GCMUL) 38 | #define LUAI_GCMUL 200 /* GC runs 'twice the speed' of memory allocation */ 39 | #endif 40 | 41 | 42 | #define MEMERRMSG "not enough memory" 43 | 44 | 45 | /* 46 | ** a macro to help the creation of a unique random seed when a state is 47 | ** created; the seed is used to randomize hashes. 48 | */ 49 | #if !defined(luai_makeseed) 50 | #include 51 | #define luai_makeseed() cast(unsigned int, time(NULL)) 52 | #endif 53 | 54 | 55 | 56 | /* 57 | ** thread state + extra space 58 | */ 59 | typedef struct LX { 60 | #if defined(LUAI_EXTRASPACE) 61 | char buff[LUAI_EXTRASPACE]; 62 | #endif 63 | lua_State l; 64 | } LX; 65 | 66 | 67 | /* 68 | ** Main thread combines a thread state and the global state 69 | */ 70 | typedef struct LG { 71 | LX l; 72 | global_State g; 73 | } LG; 74 | 75 | 76 | 77 | #define fromstate(L) (cast(LX *, cast(lu_byte *, (L)) - offsetof(LX, l))) 78 | 79 | 80 | /* 81 | ** Compute an initial seed as random as possible. In ANSI, rely on 82 | ** Address Space Layout Randomization (if present) to increase 83 | ** randomness.. 84 | */ 85 | #define addbuff(b,p,e) \ 86 | { size_t t = cast(size_t, e); \ 87 | memcpy(buff + p, &t, sizeof(t)); p += sizeof(t); } 88 | 89 | static unsigned int makeseed (lua_State *L) { 90 | char buff[4 * sizeof(size_t)]; 91 | unsigned int h = luai_makeseed(); 92 | int p = 0; 93 | addbuff(buff, p, L); /* heap variable */ 94 | addbuff(buff, p, &h); /* local variable */ 95 | addbuff(buff, p, luaO_nilobject); /* global variable */ 96 | addbuff(buff, p, &lua_newstate); /* public function */ 97 | lua_assert(p == sizeof(buff)); 98 | return luaS_hash(buff, p, h); 99 | } 100 | 101 | 102 | /* 103 | ** set GCdebt to a new value keeping the value (totalbytes + GCdebt) 104 | ** invariant 105 | */ 106 | void luaE_setdebt (global_State *g, l_mem debt) { 107 | g->totalbytes -= (debt - g->GCdebt); 108 | g->GCdebt = debt; 109 | } 110 | 111 | 112 | CallInfo *luaE_extendCI (lua_State *L) { 113 | CallInfo *ci = luaM_new(L, CallInfo); 114 | lua_assert(L->ci->next == NULL); 115 | L->ci->next = ci; 116 | ci->previous = L->ci; 117 | ci->next = NULL; 118 | return ci; 119 | } 120 | 121 | 122 | void luaE_freeCI (lua_State *L) { 123 | CallInfo *ci = L->ci; 124 | CallInfo *next = ci->next; 125 | ci->next = NULL; 126 | while ((ci = next) != NULL) { 127 | next = ci->next; 128 | luaM_free(L, ci); 129 | } 130 | } 131 | 132 | 133 | static void stack_init (lua_State *L1, lua_State *L) { 134 | int i; CallInfo *ci; 135 | /* initialize stack array */ 136 | L1->stack = luaM_newvector(L, BASIC_STACK_SIZE, TValue); 137 | L1->stacksize = BASIC_STACK_SIZE; 138 | for (i = 0; i < BASIC_STACK_SIZE; i++) 139 | setnilvalue(L1->stack + i); /* erase new stack */ 140 | L1->top = L1->stack; 141 | L1->stack_last = L1->stack + L1->stacksize - EXTRA_STACK; 142 | /* initialize first ci */ 143 | ci = &L1->base_ci; 144 | ci->next = ci->previous = NULL; 145 | ci->callstatus = 0; 146 | ci->func = L1->top; 147 | setnilvalue(L1->top++); /* 'function' entry for this 'ci' */ 148 | ci->top = L1->top + LUA_MINSTACK; 149 | L1->ci = ci; 150 | } 151 | 152 | 153 | static void freestack (lua_State *L) { 154 | if (L->stack == NULL) 155 | return; /* stack not completely built yet */ 156 | L->ci = &L->base_ci; /* free the entire 'ci' list */ 157 | luaE_freeCI(L); 158 | luaM_freearray(L, L->stack, L->stacksize); /* free stack array */ 159 | } 160 | 161 | 162 | /* 163 | ** Create registry table and its predefined values 164 | */ 165 | static void init_registry (lua_State *L, global_State *g) { 166 | TValue mt; 167 | /* create registry */ 168 | Table *registry = luaH_new(L); 169 | sethvalue(L, &g->l_registry, registry); 170 | luaH_resize(L, registry, LUA_RIDX_LAST, 0); 171 | /* registry[LUA_RIDX_MAINTHREAD] = L */ 172 | setthvalue(L, &mt, L); 173 | luaH_setint(L, registry, LUA_RIDX_MAINTHREAD, &mt); 174 | /* registry[LUA_RIDX_GLOBALS] = table of globals */ 175 | sethvalue(L, &mt, luaH_new(L)); 176 | luaH_setint(L, registry, LUA_RIDX_GLOBALS, &mt); 177 | } 178 | 179 | 180 | /* 181 | ** open parts of the state that may cause memory-allocation errors 182 | */ 183 | static void f_luaopen (lua_State *L, void *ud) { 184 | global_State *g = G(L); 185 | UNUSED(ud); 186 | stack_init(L, L); /* init stack */ 187 | init_registry(L, g); 188 | luaS_resize(L, MINSTRTABSIZE); /* initial size of string table */ 189 | luaT_init(L); 190 | luaX_init(L); 191 | /* pre-create memory-error message */ 192 | g->memerrmsg = luaS_newliteral(L, MEMERRMSG); 193 | luaS_fix(g->memerrmsg); /* it should never be collected */ 194 | g->gcrunning = 1; /* allow gc */ 195 | g->version = lua_version(NULL); 196 | luai_userstateopen(L); 197 | } 198 | 199 | 200 | /* 201 | ** preinitialize a state with consistent values without allocating 202 | ** any memory (to avoid errors) 203 | */ 204 | static void preinit_state (lua_State *L, global_State *g) { 205 | G(L) = g; 206 | L->stack = NULL; 207 | L->ci = NULL; 208 | L->stacksize = 0; 209 | L->errorJmp = NULL; 210 | L->nCcalls = 0; 211 | L->hook = NULL; 212 | L->hookmask = 0; 213 | L->basehookcount = 0; 214 | L->allowhook = 1; 215 | resethookcount(L); 216 | L->openupval = NULL; 217 | L->nny = 1; 218 | L->status = LUA_OK; 219 | L->errfunc = 0; 220 | } 221 | 222 | 223 | static void close_state (lua_State *L) { 224 | global_State *g = G(L); 225 | luaF_close(L, L->stack); /* close all upvalues for this thread */ 226 | luaC_freeallobjects(L); /* collect all objects */ 227 | if (g->version) /* closing a fully built state? */ 228 | luai_userstateclose(L); 229 | luaM_freearray(L, G(L)->strt.hash, G(L)->strt.size); 230 | luaZ_freebuffer(L, &g->buff); 231 | freestack(L); 232 | lua_assert(gettotalbytes(g) == sizeof(LG)); 233 | (*g->frealloc)(g->ud, fromstate(L), sizeof(LG), 0); /* free main block */ 234 | } 235 | 236 | 237 | LUA_API lua_State *lua_newthread (lua_State *L) { 238 | lua_State *L1; 239 | lua_lock(L); 240 | luaC_checkGC(L); 241 | L1 = &luaC_newobj(L, LUA_TTHREAD, sizeof(LX), NULL, offsetof(LX, l))->th; 242 | setthvalue(L, L->top, L1); 243 | api_incr_top(L); 244 | preinit_state(L1, G(L)); 245 | L1->hookmask = L->hookmask; 246 | L1->basehookcount = L->basehookcount; 247 | L1->hook = L->hook; 248 | resethookcount(L1); 249 | luai_userstatethread(L, L1); 250 | stack_init(L1, L); /* init stack */ 251 | lua_unlock(L); 252 | return L1; 253 | } 254 | 255 | 256 | void luaE_freethread (lua_State *L, lua_State *L1) { 257 | LX *l = fromstate(L1); 258 | luaF_close(L1, L1->stack); /* close all upvalues for this thread */ 259 | lua_assert(L1->openupval == NULL); 260 | luai_userstatefree(L, L1); 261 | freestack(L1); 262 | luaM_free(L, l); 263 | } 264 | 265 | 266 | LUA_API lua_State *lua_newstate (lua_Alloc f, void *ud) { 267 | int i; 268 | lua_State *L; 269 | global_State *g; 270 | LG *l = cast(LG *, (*f)(ud, NULL, LUA_TTHREAD, sizeof(LG))); 271 | if (l == NULL) return NULL; 272 | L = &l->l.l; 273 | g = &l->g; 274 | L->next = NULL; 275 | L->tt = LUA_TTHREAD; 276 | g->currentwhite = bit2mask(WHITE0BIT, FIXEDBIT); 277 | L->marked = luaC_white(g); 278 | g->gckind = KGC_NORMAL; 279 | preinit_state(L, g); 280 | g->frealloc = f; 281 | g->ud = ud; 282 | g->mainthread = L; 283 | g->seed = makeseed(L); 284 | g->uvhead.u.l.prev = &g->uvhead; 285 | g->uvhead.u.l.next = &g->uvhead; 286 | g->gcrunning = 0; /* no GC while building state */ 287 | g->GCestimate = 0; 288 | g->strt.size = 0; 289 | g->strt.nuse = 0; 290 | g->strt.hash = NULL; 291 | setnilvalue(&g->l_registry); 292 | luaZ_initbuffer(L, &g->buff); 293 | g->panic = NULL; 294 | g->version = NULL; 295 | g->gcstate = GCSpause; 296 | g->allgc = NULL; 297 | g->finobj = NULL; 298 | g->tobefnz = NULL; 299 | g->sweepgc = g->sweepfin = NULL; 300 | g->gray = g->grayagain = NULL; 301 | g->weak = g->ephemeron = g->allweak = NULL; 302 | g->totalbytes = sizeof(LG); 303 | g->GCdebt = 0; 304 | g->gcpause = LUAI_GCPAUSE; 305 | g->gcmajorinc = LUAI_GCMAJOR; 306 | g->gcstepmul = LUAI_GCMUL; 307 | for (i=0; i < LUA_NUMTAGS; i++) g->mt[i] = NULL; 308 | if (luaD_rawrunprotected(L, f_luaopen, NULL) != LUA_OK) { 309 | /* memory allocation error: free partial state */ 310 | close_state(L); 311 | L = NULL; 312 | } 313 | return L; 314 | } 315 | 316 | 317 | LUA_API void lua_close (lua_State *L) { 318 | L = G(L)->mainthread; /* only the main thread can be closed */ 319 | lua_lock(L); 320 | close_state(L); 321 | } 322 | 323 | 324 | -------------------------------------------------------------------------------- /project/lua/lstate.h: -------------------------------------------------------------------------------- 1 | /* 2 | ** $Id: lstate.h,v 2.82.1.1 2013/04/12 18:48:47 roberto Exp $ 3 | ** Global State 4 | ** See Copyright Notice in lua.h 5 | */ 6 | 7 | #ifndef lstate_h 8 | #define lstate_h 9 | 10 | #include "lua.h" 11 | 12 | #include "lobject.h" 13 | #include "ltm.h" 14 | #include "lzio.h" 15 | 16 | 17 | /* 18 | 19 | ** Some notes about garbage-collected objects: All objects in Lua must 20 | ** be kept somehow accessible until being freed. 21 | ** 22 | ** Lua keeps most objects linked in list g->allgc. The link uses field 23 | ** 'next' of the CommonHeader. 24 | ** 25 | ** Strings are kept in several lists headed by the array g->strt.hash. 26 | ** 27 | ** Open upvalues are not subject to independent garbage collection. They 28 | ** are collected together with their respective threads. Lua keeps a 29 | ** double-linked list with all open upvalues (g->uvhead) so that it can 30 | ** mark objects referred by them. (They are always gray, so they must 31 | ** be remarked in the atomic step. Usually their contents would be marked 32 | ** when traversing the respective threads, but the thread may already be 33 | ** dead, while the upvalue is still accessible through closures.) 34 | ** 35 | ** Objects with finalizers are kept in the list g->finobj. 36 | ** 37 | ** The list g->tobefnz links all objects being finalized. 38 | 39 | */ 40 | 41 | 42 | struct lua_longjmp; /* defined in ldo.c */ 43 | 44 | 45 | 46 | /* extra stack space to handle TM calls and some other extras */ 47 | #define EXTRA_STACK 5 48 | 49 | 50 | #define BASIC_STACK_SIZE (2*LUA_MINSTACK) 51 | 52 | 53 | /* kinds of Garbage Collection */ 54 | #define KGC_NORMAL 0 55 | #define KGC_EMERGENCY 1 /* gc was forced by an allocation failure */ 56 | #define KGC_GEN 2 /* generational collection */ 57 | 58 | 59 | typedef struct stringtable { 60 | GCObject **hash; 61 | lu_int32 nuse; /* number of elements */ 62 | int size; 63 | } stringtable; 64 | 65 | 66 | /* 67 | ** information about a call 68 | */ 69 | typedef struct CallInfo { 70 | StkId func; /* function index in the stack */ 71 | StkId top; /* top for this function */ 72 | struct CallInfo *previous, *next; /* dynamic call link */ 73 | short nresults; /* expected number of results from this function */ 74 | lu_byte callstatus; 75 | ptrdiff_t extra; 76 | union { 77 | struct { /* only for Lua functions */ 78 | StkId base; /* base for this function */ 79 | const Instruction *savedpc; 80 | } l; 81 | struct { /* only for C functions */ 82 | int ctx; /* context info. in case of yields */ 83 | lua_CFunction k; /* continuation in case of yields */ 84 | ptrdiff_t old_errfunc; 85 | lu_byte old_allowhook; 86 | lu_byte status; 87 | } c; 88 | } u; 89 | } CallInfo; 90 | 91 | 92 | /* 93 | ** Bits in CallInfo status 94 | */ 95 | #define CIST_LUA (1<<0) /* call is running a Lua function */ 96 | #define CIST_HOOKED (1<<1) /* call is running a debug hook */ 97 | #define CIST_REENTRY (1<<2) /* call is running on same invocation of 98 | luaV_execute of previous call */ 99 | #define CIST_YIELDED (1<<3) /* call reentered after suspension */ 100 | #define CIST_YPCALL (1<<4) /* call is a yieldable protected call */ 101 | #define CIST_STAT (1<<5) /* call has an error status (pcall) */ 102 | #define CIST_TAIL (1<<6) /* call was tail called */ 103 | #define CIST_HOOKYIELD (1<<7) /* last hook called yielded */ 104 | 105 | 106 | #define isLua(ci) ((ci)->callstatus & CIST_LUA) 107 | 108 | 109 | /* 110 | ** `global state', shared by all threads of this state 111 | */ 112 | typedef struct global_State { 113 | lua_Alloc frealloc; /* function to reallocate memory */ 114 | void *ud; /* auxiliary data to `frealloc' */ 115 | lu_mem totalbytes; /* number of bytes currently allocated - GCdebt */ 116 | l_mem GCdebt; /* bytes allocated not yet compensated by the collector */ 117 | lu_mem GCmemtrav; /* memory traversed by the GC */ 118 | lu_mem GCestimate; /* an estimate of the non-garbage memory in use */ 119 | stringtable strt; /* hash table for strings */ 120 | TValue l_registry; 121 | unsigned int seed; /* randomized seed for hashes */ 122 | lu_byte currentwhite; 123 | lu_byte gcstate; /* state of garbage collector */ 124 | lu_byte gckind; /* kind of GC running */ 125 | lu_byte gcrunning; /* true if GC is running */ 126 | int sweepstrgc; /* position of sweep in `strt' */ 127 | GCObject *allgc; /* list of all collectable objects */ 128 | GCObject *finobj; /* list of collectable objects with finalizers */ 129 | GCObject **sweepgc; /* current position of sweep in list 'allgc' */ 130 | GCObject **sweepfin; /* current position of sweep in list 'finobj' */ 131 | GCObject *gray; /* list of gray objects */ 132 | GCObject *grayagain; /* list of objects to be traversed atomically */ 133 | GCObject *weak; /* list of tables with weak values */ 134 | GCObject *ephemeron; /* list of ephemeron tables (weak keys) */ 135 | GCObject *allweak; /* list of all-weak tables */ 136 | GCObject *tobefnz; /* list of userdata to be GC */ 137 | UpVal uvhead; /* head of double-linked list of all open upvalues */ 138 | Mbuffer buff; /* temporary buffer for string concatenation */ 139 | int gcpause; /* size of pause between successive GCs */ 140 | int gcmajorinc; /* pause between major collections (only in gen. mode) */ 141 | int gcstepmul; /* GC `granularity' */ 142 | lua_CFunction panic; /* to be called in unprotected errors */ 143 | struct lua_State *mainthread; 144 | const lua_Number *version; /* pointer to version number */ 145 | TString *memerrmsg; /* memory-error message */ 146 | TString *tmname[TM_N]; /* array with tag-method names */ 147 | struct Table *mt[LUA_NUMTAGS]; /* metatables for basic types */ 148 | } global_State; 149 | 150 | 151 | /* 152 | ** `per thread' state 153 | */ 154 | struct lua_State { 155 | CommonHeader; 156 | lu_byte status; 157 | StkId top; /* first free slot in the stack */ 158 | global_State *l_G; 159 | CallInfo *ci; /* call info for current function */ 160 | const Instruction *oldpc; /* last pc traced */ 161 | StkId stack_last; /* last free slot in the stack */ 162 | StkId stack; /* stack base */ 163 | int stacksize; 164 | unsigned short nny; /* number of non-yieldable calls in stack */ 165 | unsigned short nCcalls; /* number of nested C calls */ 166 | lu_byte hookmask; 167 | lu_byte allowhook; 168 | int basehookcount; 169 | int hookcount; 170 | lua_Hook hook; 171 | GCObject *openupval; /* list of open upvalues in this stack */ 172 | GCObject *gclist; 173 | struct lua_longjmp *errorJmp; /* current error recover point */ 174 | ptrdiff_t errfunc; /* current error handling function (stack index) */ 175 | CallInfo base_ci; /* CallInfo for first level (C calling Lua) */ 176 | }; 177 | 178 | 179 | #define G(L) (L->l_G) 180 | 181 | 182 | /* 183 | ** Union of all collectable objects 184 | */ 185 | union GCObject { 186 | GCheader gch; /* common header */ 187 | union TString ts; 188 | union Udata u; 189 | union Closure cl; 190 | struct Table h; 191 | struct Proto p; 192 | struct UpVal uv; 193 | struct lua_State th; /* thread */ 194 | }; 195 | 196 | 197 | #define gch(o) (&(o)->gch) 198 | 199 | /* macros to convert a GCObject into a specific value */ 200 | #define rawgco2ts(o) \ 201 | check_exp(novariant((o)->gch.tt) == LUA_TSTRING, &((o)->ts)) 202 | #define gco2ts(o) (&rawgco2ts(o)->tsv) 203 | #define rawgco2u(o) check_exp((o)->gch.tt == LUA_TUSERDATA, &((o)->u)) 204 | #define gco2u(o) (&rawgco2u(o)->uv) 205 | #define gco2lcl(o) check_exp((o)->gch.tt == LUA_TLCL, &((o)->cl.l)) 206 | #define gco2ccl(o) check_exp((o)->gch.tt == LUA_TCCL, &((o)->cl.c)) 207 | #define gco2cl(o) \ 208 | check_exp(novariant((o)->gch.tt) == LUA_TFUNCTION, &((o)->cl)) 209 | #define gco2t(o) check_exp((o)->gch.tt == LUA_TTABLE, &((o)->h)) 210 | #define gco2p(o) check_exp((o)->gch.tt == LUA_TPROTO, &((o)->p)) 211 | #define gco2uv(o) check_exp((o)->gch.tt == LUA_TUPVAL, &((o)->uv)) 212 | #define gco2th(o) check_exp((o)->gch.tt == LUA_TTHREAD, &((o)->th)) 213 | 214 | /* macro to convert any Lua object into a GCObject */ 215 | #define obj2gco(v) (cast(GCObject *, (v))) 216 | 217 | 218 | /* actual number of total bytes allocated */ 219 | #define gettotalbytes(g) ((g)->totalbytes + (g)->GCdebt) 220 | 221 | LUAI_FUNC void luaE_setdebt (global_State *g, l_mem debt); 222 | LUAI_FUNC void luaE_freethread (lua_State *L, lua_State *L1); 223 | LUAI_FUNC CallInfo *luaE_extendCI (lua_State *L); 224 | LUAI_FUNC void luaE_freeCI (lua_State *L); 225 | 226 | 227 | #endif 228 | 229 | -------------------------------------------------------------------------------- /project/lua/lstring.c: -------------------------------------------------------------------------------- 1 | /* 2 | ** $Id: lstring.c,v 2.26.1.1 2013/04/12 18:48:47 roberto Exp $ 3 | ** String table (keeps all strings handled by Lua) 4 | ** See Copyright Notice in lua.h 5 | */ 6 | 7 | 8 | #include 9 | 10 | #define lstring_c 11 | #define LUA_CORE 12 | 13 | #include "lua.h" 14 | 15 | #include "lmem.h" 16 | #include "lobject.h" 17 | #include "lstate.h" 18 | #include "lstring.h" 19 | 20 | 21 | /* 22 | ** Lua will use at most ~(2^LUAI_HASHLIMIT) bytes from a string to 23 | ** compute its hash 24 | */ 25 | #if !defined(LUAI_HASHLIMIT) 26 | #define LUAI_HASHLIMIT 5 27 | #endif 28 | 29 | 30 | /* 31 | ** equality for long strings 32 | */ 33 | int luaS_eqlngstr (TString *a, TString *b) { 34 | size_t len = a->tsv.len; 35 | lua_assert(a->tsv.tt == LUA_TLNGSTR && b->tsv.tt == LUA_TLNGSTR); 36 | return (a == b) || /* same instance or... */ 37 | ((len == b->tsv.len) && /* equal length and ... */ 38 | (memcmp(getstr(a), getstr(b), len) == 0)); /* equal contents */ 39 | } 40 | 41 | 42 | /* 43 | ** equality for strings 44 | */ 45 | int luaS_eqstr (TString *a, TString *b) { 46 | return (a->tsv.tt == b->tsv.tt) && 47 | (a->tsv.tt == LUA_TSHRSTR ? eqshrstr(a, b) : luaS_eqlngstr(a, b)); 48 | } 49 | 50 | 51 | unsigned int luaS_hash (const char *str, size_t l, unsigned int seed) { 52 | unsigned int h = seed ^ cast(unsigned int, l); 53 | size_t l1; 54 | size_t step = (l >> LUAI_HASHLIMIT) + 1; 55 | for (l1 = l; l1 >= step; l1 -= step) 56 | h = h ^ ((h<<5) + (h>>2) + cast_byte(str[l1 - 1])); 57 | return h; 58 | } 59 | 60 | 61 | /* 62 | ** resizes the string table 63 | */ 64 | void luaS_resize (lua_State *L, int newsize) { 65 | int i; 66 | stringtable *tb = &G(L)->strt; 67 | /* cannot resize while GC is traversing strings */ 68 | luaC_runtilstate(L, ~bitmask(GCSsweepstring)); 69 | if (newsize > tb->size) { 70 | luaM_reallocvector(L, tb->hash, tb->size, newsize, GCObject *); 71 | for (i = tb->size; i < newsize; i++) tb->hash[i] = NULL; 72 | } 73 | /* rehash */ 74 | for (i=0; isize; i++) { 75 | GCObject *p = tb->hash[i]; 76 | tb->hash[i] = NULL; 77 | while (p) { /* for each node in the list */ 78 | GCObject *next = gch(p)->next; /* save next */ 79 | unsigned int h = lmod(gco2ts(p)->hash, newsize); /* new position */ 80 | gch(p)->next = tb->hash[h]; /* chain it */ 81 | tb->hash[h] = p; 82 | resetoldbit(p); /* see MOVE OLD rule */ 83 | p = next; 84 | } 85 | } 86 | if (newsize < tb->size) { 87 | /* shrinking slice must be empty */ 88 | lua_assert(tb->hash[newsize] == NULL && tb->hash[tb->size - 1] == NULL); 89 | luaM_reallocvector(L, tb->hash, tb->size, newsize, GCObject *); 90 | } 91 | tb->size = newsize; 92 | } 93 | 94 | 95 | /* 96 | ** creates a new string object 97 | */ 98 | static TString *createstrobj (lua_State *L, const char *str, size_t l, 99 | int tag, unsigned int h, GCObject **list) { 100 | TString *ts; 101 | size_t totalsize; /* total size of TString object */ 102 | totalsize = sizeof(TString) + ((l + 1) * sizeof(char)); 103 | ts = &luaC_newobj(L, tag, totalsize, list, 0)->ts; 104 | ts->tsv.len = l; 105 | ts->tsv.hash = h; 106 | ts->tsv.extra = 0; 107 | memcpy(ts+1, str, l*sizeof(char)); 108 | ((char *)(ts+1))[l] = '\0'; /* ending 0 */ 109 | return ts; 110 | } 111 | 112 | 113 | /* 114 | ** creates a new short string, inserting it into string table 115 | */ 116 | static TString *newshrstr (lua_State *L, const char *str, size_t l, 117 | unsigned int h) { 118 | GCObject **list; /* (pointer to) list where it will be inserted */ 119 | stringtable *tb = &G(L)->strt; 120 | TString *s; 121 | if (tb->nuse >= cast(lu_int32, tb->size) && tb->size <= MAX_INT/2) 122 | luaS_resize(L, tb->size*2); /* too crowded */ 123 | list = &tb->hash[lmod(h, tb->size)]; 124 | s = createstrobj(L, str, l, LUA_TSHRSTR, h, list); 125 | tb->nuse++; 126 | return s; 127 | } 128 | 129 | 130 | /* 131 | ** checks whether short string exists and reuses it or creates a new one 132 | */ 133 | static TString *internshrstr (lua_State *L, const char *str, size_t l) { 134 | GCObject *o; 135 | global_State *g = G(L); 136 | unsigned int h = luaS_hash(str, l, g->seed); 137 | for (o = g->strt.hash[lmod(h, g->strt.size)]; 138 | o != NULL; 139 | o = gch(o)->next) { 140 | TString *ts = rawgco2ts(o); 141 | if (h == ts->tsv.hash && 142 | l == ts->tsv.len && 143 | (memcmp(str, getstr(ts), l * sizeof(char)) == 0)) { 144 | if (isdead(G(L), o)) /* string is dead (but was not collected yet)? */ 145 | changewhite(o); /* resurrect it */ 146 | return ts; 147 | } 148 | } 149 | return newshrstr(L, str, l, h); /* not found; create a new string */ 150 | } 151 | 152 | 153 | /* 154 | ** new string (with explicit length) 155 | */ 156 | TString *luaS_newlstr (lua_State *L, const char *str, size_t l) { 157 | if (l <= LUAI_MAXSHORTLEN) /* short string? */ 158 | return internshrstr(L, str, l); 159 | else { 160 | if (l + 1 > (MAX_SIZET - sizeof(TString))/sizeof(char)) 161 | luaM_toobig(L); 162 | return createstrobj(L, str, l, LUA_TLNGSTR, G(L)->seed, NULL); 163 | } 164 | } 165 | 166 | 167 | /* 168 | ** new zero-terminated string 169 | */ 170 | TString *luaS_new (lua_State *L, const char *str) { 171 | return luaS_newlstr(L, str, strlen(str)); 172 | } 173 | 174 | 175 | Udata *luaS_newudata (lua_State *L, size_t s, Table *e) { 176 | Udata *u; 177 | if (s > MAX_SIZET - sizeof(Udata)) 178 | luaM_toobig(L); 179 | u = &luaC_newobj(L, LUA_TUSERDATA, sizeof(Udata) + s, NULL, 0)->u; 180 | u->uv.len = s; 181 | u->uv.metatable = NULL; 182 | u->uv.env = e; 183 | return u; 184 | } 185 | 186 | -------------------------------------------------------------------------------- /project/lua/lstring.h: -------------------------------------------------------------------------------- 1 | /* 2 | ** $Id: lstring.h,v 1.49.1.1 2013/04/12 18:48:47 roberto Exp $ 3 | ** String table (keep all strings handled by Lua) 4 | ** See Copyright Notice in lua.h 5 | */ 6 | 7 | #ifndef lstring_h 8 | #define lstring_h 9 | 10 | #include "lgc.h" 11 | #include "lobject.h" 12 | #include "lstate.h" 13 | 14 | 15 | #define sizestring(s) (sizeof(union TString)+((s)->len+1)*sizeof(char)) 16 | 17 | #define sizeudata(u) (sizeof(union Udata)+(u)->len) 18 | 19 | #define luaS_newliteral(L, s) (luaS_newlstr(L, "" s, \ 20 | (sizeof(s)/sizeof(char))-1)) 21 | 22 | #define luaS_fix(s) l_setbit((s)->tsv.marked, FIXEDBIT) 23 | 24 | 25 | /* 26 | ** test whether a string is a reserved word 27 | */ 28 | #define isreserved(s) ((s)->tsv.tt == LUA_TSHRSTR && (s)->tsv.extra > 0) 29 | 30 | 31 | /* 32 | ** equality for short strings, which are always internalized 33 | */ 34 | #define eqshrstr(a,b) check_exp((a)->tsv.tt == LUA_TSHRSTR, (a) == (b)) 35 | 36 | 37 | LUAI_FUNC unsigned int luaS_hash (const char *str, size_t l, unsigned int seed); 38 | LUAI_FUNC int luaS_eqlngstr (TString *a, TString *b); 39 | LUAI_FUNC int luaS_eqstr (TString *a, TString *b); 40 | LUAI_FUNC void luaS_resize (lua_State *L, int newsize); 41 | LUAI_FUNC Udata *luaS_newudata (lua_State *L, size_t s, Table *e); 42 | LUAI_FUNC TString *luaS_newlstr (lua_State *L, const char *str, size_t l); 43 | LUAI_FUNC TString *luaS_new (lua_State *L, const char *str); 44 | 45 | 46 | #endif 47 | -------------------------------------------------------------------------------- /project/lua/ltable.h: -------------------------------------------------------------------------------- 1 | /* 2 | ** $Id: ltable.h,v 2.16.1.2 2013/08/30 15:49:41 roberto Exp $ 3 | ** Lua tables (hash) 4 | ** See Copyright Notice in lua.h 5 | */ 6 | 7 | #ifndef ltable_h 8 | #define ltable_h 9 | 10 | #include "lobject.h" 11 | 12 | 13 | #define gnode(t,i) (&(t)->node[i]) 14 | #define gkey(n) (&(n)->i_key.tvk) 15 | #define gval(n) (&(n)->i_val) 16 | #define gnext(n) ((n)->i_key.nk.next) 17 | 18 | #define invalidateTMcache(t) ((t)->flags = 0) 19 | 20 | /* returns the key, given the value of a table entry */ 21 | #define keyfromval(v) \ 22 | (gkey(cast(Node *, cast(char *, (v)) - offsetof(Node, i_val)))) 23 | 24 | 25 | LUAI_FUNC const TValue *luaH_getint (Table *t, int key); 26 | LUAI_FUNC void luaH_setint (lua_State *L, Table *t, int key, TValue *value); 27 | LUAI_FUNC const TValue *luaH_getstr (Table *t, TString *key); 28 | LUAI_FUNC const TValue *luaH_get (Table *t, const TValue *key); 29 | LUAI_FUNC TValue *luaH_newkey (lua_State *L, Table *t, const TValue *key); 30 | LUAI_FUNC TValue *luaH_set (lua_State *L, Table *t, const TValue *key); 31 | LUAI_FUNC Table *luaH_new (lua_State *L); 32 | LUAI_FUNC void luaH_resize (lua_State *L, Table *t, int nasize, int nhsize); 33 | LUAI_FUNC void luaH_resizearray (lua_State *L, Table *t, int nasize); 34 | LUAI_FUNC void luaH_free (lua_State *L, Table *t); 35 | LUAI_FUNC int luaH_next (lua_State *L, Table *t, StkId key); 36 | LUAI_FUNC int luaH_getn (Table *t); 37 | 38 | 39 | #if defined(LUA_DEBUG) 40 | LUAI_FUNC Node *luaH_mainposition (const Table *t, const TValue *key); 41 | LUAI_FUNC int luaH_isdummy (Node *n); 42 | #endif 43 | 44 | 45 | #endif 46 | -------------------------------------------------------------------------------- /project/lua/ltablib.c: -------------------------------------------------------------------------------- 1 | /* 2 | ** $Id: ltablib.c,v 1.65.1.1 2013/04/12 18:48:47 roberto Exp $ 3 | ** Library for Table Manipulation 4 | ** See Copyright Notice in lua.h 5 | */ 6 | 7 | 8 | #include 9 | 10 | #define ltablib_c 11 | #define LUA_LIB 12 | 13 | #include "lua.h" 14 | 15 | #include "lauxlib.h" 16 | #include "lualib.h" 17 | 18 | 19 | #define aux_getn(L,n) (luaL_checktype(L, n, LUA_TTABLE), luaL_len(L, n)) 20 | 21 | 22 | 23 | #if defined(LUA_COMPAT_MAXN) 24 | static int maxn (lua_State *L) { 25 | lua_Number max = 0; 26 | luaL_checktype(L, 1, LUA_TTABLE); 27 | lua_pushnil(L); /* first key */ 28 | while (lua_next(L, 1)) { 29 | lua_pop(L, 1); /* remove value */ 30 | if (lua_type(L, -1) == LUA_TNUMBER) { 31 | lua_Number v = lua_tonumber(L, -1); 32 | if (v > max) max = v; 33 | } 34 | } 35 | lua_pushnumber(L, max); 36 | return 1; 37 | } 38 | #endif 39 | 40 | 41 | static int tinsert (lua_State *L) { 42 | int e = aux_getn(L, 1) + 1; /* first empty element */ 43 | int pos; /* where to insert new element */ 44 | switch (lua_gettop(L)) { 45 | case 2: { /* called with only 2 arguments */ 46 | pos = e; /* insert new element at the end */ 47 | break; 48 | } 49 | case 3: { 50 | int i; 51 | pos = luaL_checkint(L, 2); /* 2nd argument is the position */ 52 | luaL_argcheck(L, 1 <= pos && pos <= e, 2, "position out of bounds"); 53 | for (i = e; i > pos; i--) { /* move up elements */ 54 | lua_rawgeti(L, 1, i-1); 55 | lua_rawseti(L, 1, i); /* t[i] = t[i-1] */ 56 | } 57 | break; 58 | } 59 | default: { 60 | return luaL_error(L, "wrong number of arguments to " LUA_QL("insert")); 61 | } 62 | } 63 | lua_rawseti(L, 1, pos); /* t[pos] = v */ 64 | return 0; 65 | } 66 | 67 | 68 | static int tremove (lua_State *L) { 69 | int size = aux_getn(L, 1); 70 | int pos = luaL_optint(L, 2, size); 71 | if (pos != size) /* validate 'pos' if given */ 72 | luaL_argcheck(L, 1 <= pos && pos <= size + 1, 1, "position out of bounds"); 73 | lua_rawgeti(L, 1, pos); /* result = t[pos] */ 74 | for ( ; pos < size; pos++) { 75 | lua_rawgeti(L, 1, pos+1); 76 | lua_rawseti(L, 1, pos); /* t[pos] = t[pos+1] */ 77 | } 78 | lua_pushnil(L); 79 | lua_rawseti(L, 1, pos); /* t[pos] = nil */ 80 | return 1; 81 | } 82 | 83 | 84 | static void addfield (lua_State *L, luaL_Buffer *b, int i) { 85 | lua_rawgeti(L, 1, i); 86 | if (!lua_isstring(L, -1)) 87 | luaL_error(L, "invalid value (%s) at index %d in table for " 88 | LUA_QL("concat"), luaL_typename(L, -1), i); 89 | luaL_addvalue(b); 90 | } 91 | 92 | 93 | static int tconcat (lua_State *L) { 94 | luaL_Buffer b; 95 | size_t lsep; 96 | int i, last; 97 | const char *sep = luaL_optlstring(L, 2, "", &lsep); 98 | luaL_checktype(L, 1, LUA_TTABLE); 99 | i = luaL_optint(L, 3, 1); 100 | last = luaL_opt(L, luaL_checkint, 4, luaL_len(L, 1)); 101 | luaL_buffinit(L, &b); 102 | for (; i < last; i++) { 103 | addfield(L, &b, i); 104 | luaL_addlstring(&b, sep, lsep); 105 | } 106 | if (i == last) /* add last value (if interval was not empty) */ 107 | addfield(L, &b, i); 108 | luaL_pushresult(&b); 109 | return 1; 110 | } 111 | 112 | 113 | /* 114 | ** {====================================================== 115 | ** Pack/unpack 116 | ** ======================================================= 117 | */ 118 | 119 | static int pack (lua_State *L) { 120 | int n = lua_gettop(L); /* number of elements to pack */ 121 | lua_createtable(L, n, 1); /* create result table */ 122 | lua_pushinteger(L, n); 123 | lua_setfield(L, -2, "n"); /* t.n = number of elements */ 124 | if (n > 0) { /* at least one element? */ 125 | int i; 126 | lua_pushvalue(L, 1); 127 | lua_rawseti(L, -2, 1); /* insert first element */ 128 | lua_replace(L, 1); /* move table into index 1 */ 129 | for (i = n; i >= 2; i--) /* assign other elements */ 130 | lua_rawseti(L, 1, i); 131 | } 132 | return 1; /* return table */ 133 | } 134 | 135 | 136 | static int unpack (lua_State *L) { 137 | int i, e, n; 138 | luaL_checktype(L, 1, LUA_TTABLE); 139 | i = luaL_optint(L, 2, 1); 140 | e = luaL_opt(L, luaL_checkint, 3, luaL_len(L, 1)); 141 | if (i > e) return 0; /* empty range */ 142 | n = e - i + 1; /* number of elements */ 143 | if (n <= 0 || !lua_checkstack(L, n)) /* n <= 0 means arith. overflow */ 144 | return luaL_error(L, "too many results to unpack"); 145 | lua_rawgeti(L, 1, i); /* push arg[i] (avoiding overflow problems) */ 146 | while (i++ < e) /* push arg[i + 1...e] */ 147 | lua_rawgeti(L, 1, i); 148 | return n; 149 | } 150 | 151 | /* }====================================================== */ 152 | 153 | 154 | 155 | /* 156 | ** {====================================================== 157 | ** Quicksort 158 | ** (based on `Algorithms in MODULA-3', Robert Sedgewick; 159 | ** Addison-Wesley, 1993.) 160 | ** ======================================================= 161 | */ 162 | 163 | 164 | static void set2 (lua_State *L, int i, int j) { 165 | lua_rawseti(L, 1, i); 166 | lua_rawseti(L, 1, j); 167 | } 168 | 169 | static int sort_comp (lua_State *L, int a, int b) { 170 | if (!lua_isnil(L, 2)) { /* function? */ 171 | int res; 172 | lua_pushvalue(L, 2); 173 | lua_pushvalue(L, a-1); /* -1 to compensate function */ 174 | lua_pushvalue(L, b-2); /* -2 to compensate function and `a' */ 175 | lua_call(L, 2, 1); 176 | res = lua_toboolean(L, -1); 177 | lua_pop(L, 1); 178 | return res; 179 | } 180 | else /* a < b? */ 181 | return lua_compare(L, a, b, LUA_OPLT); 182 | } 183 | 184 | static void auxsort (lua_State *L, int l, int u) { 185 | while (l < u) { /* for tail recursion */ 186 | int i, j; 187 | /* sort elements a[l], a[(l+u)/2] and a[u] */ 188 | lua_rawgeti(L, 1, l); 189 | lua_rawgeti(L, 1, u); 190 | if (sort_comp(L, -1, -2)) /* a[u] < a[l]? */ 191 | set2(L, l, u); /* swap a[l] - a[u] */ 192 | else 193 | lua_pop(L, 2); 194 | if (u-l == 1) break; /* only 2 elements */ 195 | i = (l+u)/2; 196 | lua_rawgeti(L, 1, i); 197 | lua_rawgeti(L, 1, l); 198 | if (sort_comp(L, -2, -1)) /* a[i]= P */ 217 | while (lua_rawgeti(L, 1, ++i), sort_comp(L, -1, -2)) { 218 | if (i>=u) luaL_error(L, "invalid order function for sorting"); 219 | lua_pop(L, 1); /* remove a[i] */ 220 | } 221 | /* repeat --j until a[j] <= P */ 222 | while (lua_rawgeti(L, 1, --j), sort_comp(L, -3, -1)) { 223 | if (j<=l) luaL_error(L, "invalid order function for sorting"); 224 | lua_pop(L, 1); /* remove a[j] */ 225 | } 226 | if (j 9 | 10 | #define ltm_c 11 | #define LUA_CORE 12 | 13 | #include "lua.h" 14 | 15 | #include "lobject.h" 16 | #include "lstate.h" 17 | #include "lstring.h" 18 | #include "ltable.h" 19 | #include "ltm.h" 20 | 21 | 22 | static const char udatatypename[] = "userdata"; 23 | 24 | LUAI_DDEF const char *const luaT_typenames_[LUA_TOTALTAGS] = { 25 | "no value", 26 | "nil", "boolean", udatatypename, "number", 27 | "string", "table", "function", udatatypename, "thread", 28 | "proto", "upval" /* these last two cases are used for tests only */ 29 | }; 30 | 31 | 32 | void luaT_init (lua_State *L) { 33 | static const char *const luaT_eventname[] = { /* ORDER TM */ 34 | "__index", "__newindex", 35 | "__gc", "__mode", "__len", "__eq", 36 | "__add", "__sub", "__mul", "__div", "__mod", 37 | "__pow", "__unm", "__lt", "__le", 38 | "__concat", "__call" 39 | }; 40 | int i; 41 | for (i=0; itmname[i] = luaS_new(L, luaT_eventname[i]); 43 | luaS_fix(G(L)->tmname[i]); /* never collect these names */ 44 | } 45 | } 46 | 47 | 48 | /* 49 | ** function to be used with macro "fasttm": optimized for absence of 50 | ** tag methods 51 | */ 52 | const TValue *luaT_gettm (Table *events, TMS event, TString *ename) { 53 | const TValue *tm = luaH_getstr(events, ename); 54 | lua_assert(event <= TM_EQ); 55 | if (ttisnil(tm)) { /* no tag method? */ 56 | events->flags |= cast_byte(1u<metatable; 68 | break; 69 | case LUA_TUSERDATA: 70 | mt = uvalue(o)->metatable; 71 | break; 72 | default: 73 | mt = G(L)->mt[ttypenv(o)]; 74 | } 75 | return (mt ? luaH_getstr(mt, G(L)->tmname[event]) : luaO_nilobject); 76 | } 77 | 78 | -------------------------------------------------------------------------------- /project/lua/ltm.h: -------------------------------------------------------------------------------- 1 | /* 2 | ** $Id: ltm.h,v 2.11.1.1 2013/04/12 18:48:47 roberto Exp $ 3 | ** Tag methods 4 | ** See Copyright Notice in lua.h 5 | */ 6 | 7 | #ifndef ltm_h 8 | #define ltm_h 9 | 10 | 11 | #include "lobject.h" 12 | 13 | 14 | /* 15 | * WARNING: if you change the order of this enumeration, 16 | * grep "ORDER TM" 17 | */ 18 | typedef enum { 19 | TM_INDEX, 20 | TM_NEWINDEX, 21 | TM_GC, 22 | TM_MODE, 23 | TM_LEN, 24 | TM_EQ, /* last tag method with `fast' access */ 25 | TM_ADD, 26 | TM_SUB, 27 | TM_MUL, 28 | TM_DIV, 29 | TM_MOD, 30 | TM_POW, 31 | TM_UNM, 32 | TM_LT, 33 | TM_LE, 34 | TM_CONCAT, 35 | TM_CALL, 36 | TM_N /* number of elements in the enum */ 37 | } TMS; 38 | 39 | 40 | 41 | #define gfasttm(g,et,e) ((et) == NULL ? NULL : \ 42 | ((et)->flags & (1u<<(e))) ? NULL : luaT_gettm(et, e, (g)->tmname[e])) 43 | 44 | #define fasttm(l,et,e) gfasttm(G(l), et, e) 45 | 46 | #define ttypename(x) luaT_typenames_[(x) + 1] 47 | #define objtypename(x) ttypename(ttypenv(x)) 48 | 49 | LUAI_DDEC const char *const luaT_typenames_[LUA_TOTALTAGS]; 50 | 51 | 52 | LUAI_FUNC const TValue *luaT_gettm (Table *events, TMS event, TString *ename); 53 | LUAI_FUNC const TValue *luaT_gettmbyobj (lua_State *L, const TValue *o, 54 | TMS event); 55 | LUAI_FUNC void luaT_init (lua_State *L); 56 | 57 | #endif 58 | -------------------------------------------------------------------------------- /project/lua/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 | -------------------------------------------------------------------------------- /project/lua/luac.c: -------------------------------------------------------------------------------- 1 | /* 2 | ** $Id: luac.c,v 1.69 2011/11/29 17:46:33 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 "lobject.h" 19 | #include "lstate.h" 20 | #include "lundump.h" 21 | 22 | static void PrintFunction(const Proto* f, int full); 23 | #define luaU_print PrintFunction 24 | 25 | #define PROGNAME "luac" /* default program name */ 26 | #define OUTPUT PROGNAME ".out" /* default output file */ 27 | 28 | static int listing=0; /* list bytecodes? */ 29 | static int dumping=1; /* dump bytecodes? */ 30 | static int stripping=0; /* strip debug information? */ 31 | static char Output[]={ OUTPUT }; /* default output file name */ 32 | static const char* output=Output; /* actual output file name */ 33 | static const char* progname=PROGNAME; /* actual program name */ 34 | 35 | static void fatal(const char* message) 36 | { 37 | fprintf(stderr,"%s: %s\n",progname,message); 38 | exit(EXIT_FAILURE); 39 | } 40 | 41 | static void cannot(const char* what) 42 | { 43 | fprintf(stderr,"%s: cannot %s %s: %s\n",progname,what,output,strerror(errno)); 44 | exit(EXIT_FAILURE); 45 | } 46 | 47 | static void usage(const char* message) 48 | { 49 | if (*message=='-') 50 | fprintf(stderr,"%s: unrecognized option " LUA_QS "\n",progname,message); 51 | else 52 | fprintf(stderr,"%s: %s\n",progname,message); 53 | fprintf(stderr, 54 | "usage: %s [options] [filenames]\n" 55 | "Available options are:\n" 56 | " -l list (use -l -l for full listing)\n" 57 | " -o name output to file " LUA_QL("name") " (default is \"%s\")\n" 58 | " -p parse only\n" 59 | " -s strip debug information\n" 60 | " -v show version information\n" 61 | " -- stop handling options\n" 62 | " - stop handling options and process stdin\n" 63 | ,progname,Output); 64 | exit(EXIT_FAILURE); 65 | } 66 | 67 | #define IS(s) (strcmp(argv[i],s)==0) 68 | 69 | static int doargs(int argc, char* argv[]) 70 | { 71 | int i; 72 | int version=0; 73 | if (argv[0]!=NULL && *argv[0]!=0) progname=argv[0]; 74 | for (i=1; itop+(i)) 135 | 136 | static const Proto* combine(lua_State* L, int n) 137 | { 138 | if (n==1) 139 | return toproto(L,-1); 140 | else 141 | { 142 | Proto* f; 143 | int i=n; 144 | if (lua_load(L,reader,&i,"=(" PROGNAME ")",NULL)!=LUA_OK) fatal(lua_tostring(L,-1)); 145 | f=toproto(L,-1); 146 | for (i=0; ip[i]=toproto(L,i-n-1); 149 | if (f->p[i]->sizeupvalues>0) f->p[i]->upvalues[0].instack=0; 150 | } 151 | f->sizelineinfo=0; 152 | return f; 153 | } 154 | } 155 | 156 | static int writer(lua_State* L, const void* p, size_t size, void* u) 157 | { 158 | UNUSED(L); 159 | return (fwrite(p,size,1,(FILE*)u)!=1) && (size!=0); 160 | } 161 | 162 | static int pmain(lua_State* L) 163 | { 164 | int argc=(int)lua_tointeger(L,1); 165 | char** argv=(char**)lua_touserdata(L,2); 166 | const Proto* f; 167 | int i; 168 | if (!lua_checkstack(L,argc)) fatal("too many input files"); 169 | for (i=0; i1); 176 | if (dumping) 177 | { 178 | FILE* D= (output==NULL) ? stdout : fopen(output,"wb"); 179 | if (D==NULL) cannot("open"); 180 | lua_lock(L); 181 | luaU_dump(L,f,writer,D,stripping); 182 | lua_unlock(L); 183 | if (ferror(D)) cannot("write"); 184 | if (fclose(D)) cannot("close"); 185 | } 186 | return 0; 187 | } 188 | 189 | int main(int argc, char* argv[]) 190 | { 191 | lua_State* L; 192 | int i=doargs(argc,argv); 193 | argc-=i; argv+=i; 194 | if (argc<=0) usage("no input files given"); 195 | L=luaL_newstate(); 196 | if (L==NULL) fatal("cannot create state: not enough memory"); 197 | lua_pushcfunction(L,&pmain); 198 | lua_pushinteger(L,argc); 199 | lua_pushlightuserdata(L,argv); 200 | if (lua_pcall(L,2,0,0)!=LUA_OK) fatal(lua_tostring(L,-1)); 201 | lua_close(L); 202 | return EXIT_SUCCESS; 203 | } 204 | 205 | /* 206 | ** $Id: print.c,v 1.69 2013/07/04 01:03:46 lhf Exp $ 207 | ** print bytecodes 208 | ** See Copyright Notice in lua.h 209 | */ 210 | 211 | #include 212 | #include 213 | 214 | #define luac_c 215 | #define LUA_CORE 216 | 217 | #include "ldebug.h" 218 | #include "lobject.h" 219 | #include "lopcodes.h" 220 | 221 | #define VOID(p) ((const void*)(p)) 222 | 223 | static void PrintString(const TString* ts) 224 | { 225 | const char* s=getstr(ts); 226 | size_t i,n=ts->tsv.len; 227 | printf("%c",'"'); 228 | for (i=0; ik[i]; 254 | switch (ttypenv(o)) 255 | { 256 | case LUA_TNIL: 257 | printf("nil"); 258 | break; 259 | case LUA_TBOOLEAN: 260 | printf(bvalue(o) ? "true" : "false"); 261 | break; 262 | case LUA_TNUMBER: 263 | printf(LUA_NUMBER_FMT,nvalue(o)); 264 | break; 265 | case LUA_TSTRING: 266 | PrintString(rawtsvalue(o)); 267 | break; 268 | default: /* cannot happen */ 269 | printf("? type=%d",ttype(o)); 270 | break; 271 | } 272 | } 273 | 274 | #define UPVALNAME(x) ((f->upvalues[x].name) ? getstr(f->upvalues[x].name) : "-") 275 | #define MYK(x) (-1-(x)) 276 | 277 | static void PrintCode(const Proto* f) 278 | { 279 | const Instruction* code=f->code; 280 | int pc,n=f->sizecode; 281 | for (pc=0; pc0) printf("[%d]\t",line); else printf("[-]\t"); 294 | printf("%-9s\t",luaP_opnames[o]); 295 | switch (getOpMode(o)) 296 | { 297 | case iABC: 298 | printf("%d",a); 299 | if (getBMode(o)!=OpArgN) printf(" %d",ISK(b) ? (MYK(INDEXK(b))) : b); 300 | if (getCMode(o)!=OpArgN) printf(" %d",ISK(c) ? (MYK(INDEXK(c))) : c); 301 | break; 302 | case iABx: 303 | printf("%d",a); 304 | if (getBMode(o)==OpArgK) printf(" %d",MYK(bx)); 305 | if (getBMode(o)==OpArgU) printf(" %d",bx); 306 | break; 307 | case iAsBx: 308 | printf("%d %d",a,sbx); 309 | break; 310 | case iAx: 311 | printf("%d",MYK(ax)); 312 | break; 313 | } 314 | switch (o) 315 | { 316 | case OP_LOADK: 317 | printf("\t; "); PrintConstant(f,bx); 318 | break; 319 | case OP_GETUPVAL: 320 | case OP_SETUPVAL: 321 | printf("\t; %s",UPVALNAME(b)); 322 | break; 323 | case OP_GETTABUP: 324 | printf("\t; %s",UPVALNAME(b)); 325 | if (ISK(c)) { printf(" "); PrintConstant(f,INDEXK(c)); } 326 | break; 327 | case OP_SETTABUP: 328 | printf("\t; %s",UPVALNAME(a)); 329 | if (ISK(b)) { printf(" "); PrintConstant(f,INDEXK(b)); } 330 | if (ISK(c)) { printf(" "); PrintConstant(f,INDEXK(c)); } 331 | break; 332 | case OP_GETTABLE: 333 | case OP_SELF: 334 | if (ISK(c)) { printf("\t; "); PrintConstant(f,INDEXK(c)); } 335 | break; 336 | case OP_SETTABLE: 337 | case OP_ADD: 338 | case OP_SUB: 339 | case OP_MUL: 340 | case OP_DIV: 341 | case OP_POW: 342 | case OP_EQ: 343 | case OP_LT: 344 | case OP_LE: 345 | if (ISK(b) || ISK(c)) 346 | { 347 | printf("\t; "); 348 | if (ISK(b)) PrintConstant(f,INDEXK(b)); else printf("-"); 349 | printf(" "); 350 | if (ISK(c)) PrintConstant(f,INDEXK(c)); else printf("-"); 351 | } 352 | break; 353 | case OP_JMP: 354 | case OP_FORLOOP: 355 | case OP_FORPREP: 356 | case OP_TFORLOOP: 357 | printf("\t; to %d",sbx+pc+2); 358 | break; 359 | case OP_CLOSURE: 360 | printf("\t; %p",VOID(f->p[bx])); 361 | break; 362 | case OP_SETLIST: 363 | if (c==0) printf("\t; %d",(int)code[++pc]); else printf("\t; %d",c); 364 | break; 365 | case OP_EXTRAARG: 366 | printf("\t; "); PrintConstant(f,ax); 367 | break; 368 | default: 369 | break; 370 | } 371 | printf("\n"); 372 | } 373 | } 374 | 375 | #define SS(x) ((x==1)?"":"s") 376 | #define S(x) (int)(x),SS(x) 377 | 378 | static void PrintHeader(const Proto* f) 379 | { 380 | const char* s=f->source ? getstr(f->source) : "=?"; 381 | if (*s=='@' || *s=='=') 382 | s++; 383 | else if (*s==LUA_SIGNATURE[0]) 384 | s="(bstring)"; 385 | else 386 | s="(string)"; 387 | printf("\n%s <%s:%d,%d> (%d instruction%s at %p)\n", 388 | (f->linedefined==0)?"main":"function",s, 389 | f->linedefined,f->lastlinedefined, 390 | S(f->sizecode),VOID(f)); 391 | printf("%d%s param%s, %d slot%s, %d upvalue%s, ", 392 | (int)(f->numparams),f->is_vararg?"+":"",SS(f->numparams), 393 | S(f->maxstacksize),S(f->sizeupvalues)); 394 | printf("%d local%s, %d constant%s, %d function%s\n", 395 | S(f->sizelocvars),S(f->sizek),S(f->sizep)); 396 | } 397 | 398 | static void PrintDebug(const Proto* f) 399 | { 400 | int i,n; 401 | n=f->sizek; 402 | printf("constants (%d) for %p:\n",n,VOID(f)); 403 | for (i=0; isizelocvars; 410 | printf("locals (%d) for %p:\n",n,VOID(f)); 411 | for (i=0; ilocvars[i].varname),f->locvars[i].startpc+1,f->locvars[i].endpc+1); 415 | } 416 | n=f->sizeupvalues; 417 | printf("upvalues (%d) for %p:\n",n,VOID(f)); 418 | for (i=0; iupvalues[i].instack,f->upvalues[i].idx); 422 | } 423 | } 424 | 425 | static void PrintFunction(const Proto* f, int full) 426 | { 427 | int i,n=f->sizep; 428 | PrintHeader(f); 429 | PrintCode(f); 430 | if (full) PrintDebug(f); 431 | for (i=0; ip[i],full); 432 | } 433 | -------------------------------------------------------------------------------- /project/lua/lualib.h: -------------------------------------------------------------------------------- 1 | /* 2 | ** $Id: lualib.h,v 1.43.1.1 2013/04/12 18:48:47 roberto Exp $ 3 | ** Lua standard libraries 4 | ** See Copyright Notice in lua.h 5 | */ 6 | 7 | 8 | #ifndef lualib_h 9 | #define lualib_h 10 | 11 | #include "lua.h" 12 | 13 | 14 | 15 | LUAMOD_API int (luaopen_base) (lua_State *L); 16 | 17 | #define LUA_COLIBNAME "coroutine" 18 | LUAMOD_API int (luaopen_coroutine) (lua_State *L); 19 | 20 | #define LUA_TABLIBNAME "table" 21 | LUAMOD_API int (luaopen_table) (lua_State *L); 22 | 23 | #define LUA_IOLIBNAME "io" 24 | LUAMOD_API int (luaopen_io) (lua_State *L); 25 | 26 | #define LUA_OSLIBNAME "os" 27 | LUAMOD_API int (luaopen_os) (lua_State *L); 28 | 29 | #define LUA_STRLIBNAME "string" 30 | LUAMOD_API int (luaopen_string) (lua_State *L); 31 | 32 | #define LUA_BITLIBNAME "bit32" 33 | LUAMOD_API int (luaopen_bit32) (lua_State *L); 34 | 35 | #define LUA_MATHLIBNAME "math" 36 | LUAMOD_API int (luaopen_math) (lua_State *L); 37 | 38 | #define LUA_DBLIBNAME "debug" 39 | LUAMOD_API int (luaopen_debug) (lua_State *L); 40 | 41 | #define LUA_LOADLIBNAME "package" 42 | LUAMOD_API int (luaopen_package) (lua_State *L); 43 | 44 | 45 | /* open all previous libraries */ 46 | LUALIB_API void (luaL_openlibs) (lua_State *L); 47 | 48 | 49 | 50 | #if !defined(lua_assert) 51 | #define lua_assert(x) ((void)0) 52 | #endif 53 | 54 | 55 | #endif 56 | -------------------------------------------------------------------------------- /project/lua/lundump.c: -------------------------------------------------------------------------------- 1 | /* 2 | ** $Id: lundump.c,v 2.22.1.1 2013/04/12 18:48:47 roberto Exp $ 3 | ** load precompiled Lua chunks 4 | ** See Copyright Notice in lua.h 5 | */ 6 | 7 | #include 8 | 9 | #define lundump_c 10 | #define LUA_CORE 11 | 12 | #include "lua.h" 13 | 14 | #include "ldebug.h" 15 | #include "ldo.h" 16 | #include "lfunc.h" 17 | #include "lmem.h" 18 | #include "lobject.h" 19 | #include "lstring.h" 20 | #include "lundump.h" 21 | #include "lzio.h" 22 | 23 | typedef struct { 24 | lua_State* L; 25 | ZIO* Z; 26 | Mbuffer* b; 27 | const char* name; 28 | } LoadState; 29 | 30 | static l_noret error(LoadState* S, const char* why) 31 | { 32 | luaO_pushfstring(S->L,"%s: %s precompiled chunk",S->name,why); 33 | luaD_throw(S->L,LUA_ERRSYNTAX); 34 | } 35 | 36 | #define LoadMem(S,b,n,size) LoadBlock(S,b,(n)*(size)) 37 | #define LoadByte(S) (lu_byte)LoadChar(S) 38 | #define LoadVar(S,x) LoadMem(S,&x,1,sizeof(x)) 39 | #define LoadVector(S,b,n,size) LoadMem(S,b,n,size) 40 | 41 | #if !defined(luai_verifycode) 42 | #define luai_verifycode(L,b,f) /* empty */ 43 | #endif 44 | 45 | static void LoadBlock(LoadState* S, void* b, size_t size) 46 | { 47 | if (luaZ_read(S->Z,b,size)!=0) error(S,"truncated"); 48 | } 49 | 50 | static int LoadChar(LoadState* S) 51 | { 52 | char x; 53 | LoadVar(S,x); 54 | return x; 55 | } 56 | 57 | static int LoadInt(LoadState* S) 58 | { 59 | int x; 60 | LoadVar(S,x); 61 | if (x<0) error(S,"corrupted"); 62 | return x; 63 | } 64 | 65 | static lua_Number LoadNumber(LoadState* S) 66 | { 67 | lua_Number x; 68 | LoadVar(S,x); 69 | return x; 70 | } 71 | 72 | static TString* LoadString(LoadState* S) 73 | { 74 | size_t size; 75 | LoadVar(S,size); 76 | if (size==0) 77 | return NULL; 78 | else 79 | { 80 | char* s=luaZ_openspace(S->L,S->b,size); 81 | LoadBlock(S,s,size*sizeof(char)); 82 | return luaS_newlstr(S->L,s,size-1); /* remove trailing '\0' */ 83 | } 84 | } 85 | 86 | static void LoadCode(LoadState* S, Proto* f) 87 | { 88 | int n=LoadInt(S); 89 | f->code=luaM_newvector(S->L,n,Instruction); 90 | f->sizecode=n; 91 | LoadVector(S,f->code,n,sizeof(Instruction)); 92 | } 93 | 94 | static void LoadFunction(LoadState* S, Proto* f); 95 | 96 | static void LoadConstants(LoadState* S, Proto* f) 97 | { 98 | int i,n; 99 | n=LoadInt(S); 100 | f->k=luaM_newvector(S->L,n,TValue); 101 | f->sizek=n; 102 | for (i=0; ik[i]); 103 | for (i=0; ik[i]; 106 | int t=LoadChar(S); 107 | switch (t) 108 | { 109 | case LUA_TNIL: 110 | setnilvalue(o); 111 | break; 112 | case LUA_TBOOLEAN: 113 | setbvalue(o,LoadChar(S)); 114 | break; 115 | case LUA_TNUMBER: 116 | setnvalue(o,LoadNumber(S)); 117 | break; 118 | case LUA_TSTRING: 119 | setsvalue2n(S->L,o,LoadString(S)); 120 | break; 121 | default: lua_assert(0); 122 | } 123 | } 124 | n=LoadInt(S); 125 | f->p=luaM_newvector(S->L,n,Proto*); 126 | f->sizep=n; 127 | for (i=0; ip[i]=NULL; 128 | for (i=0; ip[i]=luaF_newproto(S->L); 131 | LoadFunction(S,f->p[i]); 132 | } 133 | } 134 | 135 | static void LoadUpvalues(LoadState* S, Proto* f) 136 | { 137 | int i,n; 138 | n=LoadInt(S); 139 | f->upvalues=luaM_newvector(S->L,n,Upvaldesc); 140 | f->sizeupvalues=n; 141 | for (i=0; iupvalues[i].name=NULL; 142 | for (i=0; iupvalues[i].instack=LoadByte(S); 145 | f->upvalues[i].idx=LoadByte(S); 146 | } 147 | } 148 | 149 | static void LoadDebug(LoadState* S, Proto* f) 150 | { 151 | int i,n; 152 | f->source=LoadString(S); 153 | n=LoadInt(S); 154 | f->lineinfo=luaM_newvector(S->L,n,int); 155 | f->sizelineinfo=n; 156 | LoadVector(S,f->lineinfo,n,sizeof(int)); 157 | n=LoadInt(S); 158 | f->locvars=luaM_newvector(S->L,n,LocVar); 159 | f->sizelocvars=n; 160 | for (i=0; ilocvars[i].varname=NULL; 161 | for (i=0; ilocvars[i].varname=LoadString(S); 164 | f->locvars[i].startpc=LoadInt(S); 165 | f->locvars[i].endpc=LoadInt(S); 166 | } 167 | n=LoadInt(S); 168 | for (i=0; iupvalues[i].name=LoadString(S); 169 | } 170 | 171 | static void LoadFunction(LoadState* S, Proto* f) 172 | { 173 | f->linedefined=LoadInt(S); 174 | f->lastlinedefined=LoadInt(S); 175 | f->numparams=LoadByte(S); 176 | f->is_vararg=LoadByte(S); 177 | f->maxstacksize=LoadByte(S); 178 | LoadCode(S,f); 179 | LoadConstants(S,f); 180 | LoadUpvalues(S,f); 181 | LoadDebug(S,f); 182 | } 183 | 184 | /* the code below must be consistent with the code in luaU_header */ 185 | #define N0 LUAC_HEADERSIZE 186 | #define N1 (sizeof(LUA_SIGNATURE)-sizeof(char)) 187 | #define N2 N1+2 188 | #define N3 N2+6 189 | 190 | static void LoadHeader(LoadState* S) 191 | { 192 | lu_byte h[LUAC_HEADERSIZE]; 193 | lu_byte s[LUAC_HEADERSIZE]; 194 | luaU_header(h); 195 | memcpy(s,h,sizeof(char)); /* first char already read */ 196 | LoadBlock(S,s+sizeof(char),LUAC_HEADERSIZE-sizeof(char)); 197 | if (memcmp(h,s,N0)==0) return; 198 | if (memcmp(h,s,N1)!=0) error(S,"not a"); 199 | if (memcmp(h,s,N2)!=0) error(S,"version mismatch in"); 200 | if (memcmp(h,s,N3)!=0) error(S,"incompatible"); else error(S,"corrupted"); 201 | } 202 | 203 | /* 204 | ** load precompiled chunk 205 | */ 206 | Closure* luaU_undump (lua_State* L, ZIO* Z, Mbuffer* buff, const char* name) 207 | { 208 | LoadState S; 209 | Closure* cl; 210 | if (*name=='@' || *name=='=') 211 | S.name=name+1; 212 | else if (*name==LUA_SIGNATURE[0]) 213 | S.name="binary string"; 214 | else 215 | S.name=name; 216 | S.L=L; 217 | S.Z=Z; 218 | S.b=buff; 219 | LoadHeader(&S); 220 | cl=luaF_newLclosure(L,1); 221 | setclLvalue(L,L->top,cl); incr_top(L); 222 | cl->l.p=luaF_newproto(L); 223 | LoadFunction(&S,cl->l.p); 224 | if (cl->l.p->sizeupvalues != 1) 225 | { 226 | Proto* p=cl->l.p; 227 | cl=luaF_newLclosure(L,cl->l.p->sizeupvalues); 228 | cl->l.p=p; 229 | setclLvalue(L,L->top-1,cl); 230 | } 231 | luai_verifycode(L,buff,cl->l.p); 232 | return cl; 233 | } 234 | 235 | #define MYINT(s) (s[0]-'0') 236 | #define VERSION MYINT(LUA_VERSION_MAJOR)*16+MYINT(LUA_VERSION_MINOR) 237 | #define FORMAT 0 /* this is the official format */ 238 | 239 | /* 240 | * make header for precompiled chunks 241 | * if you change the code below be sure to update LoadHeader and FORMAT above 242 | * and LUAC_HEADERSIZE in lundump.h 243 | */ 244 | void luaU_header (lu_byte* h) 245 | { 246 | int x=1; 247 | memcpy(h,LUA_SIGNATURE,sizeof(LUA_SIGNATURE)-sizeof(char)); 248 | h+=sizeof(LUA_SIGNATURE)-sizeof(char); 249 | *h++=cast_byte(VERSION); 250 | *h++=cast_byte(FORMAT); 251 | *h++=cast_byte(*(char*)&x); /* endianness */ 252 | *h++=cast_byte(sizeof(int)); 253 | *h++=cast_byte(sizeof(size_t)); 254 | *h++=cast_byte(sizeof(Instruction)); 255 | *h++=cast_byte(sizeof(lua_Number)); 256 | *h++=cast_byte(((lua_Number)0.5)==0); /* is lua_Number integral? */ 257 | memcpy(h,LUAC_TAIL,sizeof(LUAC_TAIL)-sizeof(char)); 258 | } 259 | -------------------------------------------------------------------------------- /project/lua/lundump.h: -------------------------------------------------------------------------------- 1 | /* 2 | ** $Id: lundump.h,v 1.39.1.1 2013/04/12 18:48:47 roberto Exp $ 3 | ** load precompiled Lua chunks 4 | ** See Copyright Notice in lua.h 5 | */ 6 | 7 | #ifndef lundump_h 8 | #define lundump_h 9 | 10 | #include "lobject.h" 11 | #include "lzio.h" 12 | 13 | /* load one chunk; from lundump.c */ 14 | LUAI_FUNC Closure* luaU_undump (lua_State* L, ZIO* Z, Mbuffer* buff, const char* name); 15 | 16 | /* make header; from lundump.c */ 17 | LUAI_FUNC void luaU_header (lu_byte* h); 18 | 19 | /* dump one chunk; from ldump.c */ 20 | LUAI_FUNC int luaU_dump (lua_State* L, const Proto* f, lua_Writer w, void* data, int strip); 21 | 22 | /* data to catch conversion errors */ 23 | #define LUAC_TAIL "\x19\x93\r\n\x1a\n" 24 | 25 | /* size in bytes of header of binary files */ 26 | #define LUAC_HEADERSIZE (sizeof(LUA_SIGNATURE)-sizeof(char)+2+6+sizeof(LUAC_TAIL)-sizeof(char)) 27 | 28 | #endif 29 | -------------------------------------------------------------------------------- /project/lua/lvm.h: -------------------------------------------------------------------------------- 1 | /* 2 | ** $Id: lvm.h,v 2.18.1.1 2013/04/12 18:48:47 roberto Exp $ 3 | ** Lua virtual machine 4 | ** See Copyright Notice in lua.h 5 | */ 6 | 7 | #ifndef lvm_h 8 | #define lvm_h 9 | 10 | 11 | #include "ldo.h" 12 | #include "lobject.h" 13 | #include "ltm.h" 14 | 15 | 16 | #define tostring(L,o) (ttisstring(o) || (luaV_tostring(L, o))) 17 | 18 | #define tonumber(o,n) (ttisnumber(o) || (((o) = luaV_tonumber(o,n)) != NULL)) 19 | 20 | #define equalobj(L,o1,o2) (ttisequal(o1, o2) && luaV_equalobj_(L, o1, o2)) 21 | 22 | #define luaV_rawequalobj(o1,o2) equalobj(NULL,o1,o2) 23 | 24 | 25 | /* not to called directly */ 26 | LUAI_FUNC int luaV_equalobj_ (lua_State *L, const TValue *t1, const TValue *t2); 27 | 28 | 29 | LUAI_FUNC int luaV_lessthan (lua_State *L, const TValue *l, const TValue *r); 30 | LUAI_FUNC int luaV_lessequal (lua_State *L, const TValue *l, const TValue *r); 31 | LUAI_FUNC const TValue *luaV_tonumber (const TValue *obj, TValue *n); 32 | LUAI_FUNC int luaV_tostring (lua_State *L, StkId obj); 33 | LUAI_FUNC void luaV_gettable (lua_State *L, const TValue *t, TValue *key, 34 | StkId val); 35 | LUAI_FUNC void luaV_settable (lua_State *L, const TValue *t, TValue *key, 36 | StkId val); 37 | LUAI_FUNC void luaV_finishOp (lua_State *L); 38 | LUAI_FUNC void luaV_execute (lua_State *L); 39 | LUAI_FUNC void luaV_concat (lua_State *L, int total); 40 | LUAI_FUNC void luaV_arith (lua_State *L, StkId ra, const TValue *rb, 41 | const TValue *rc, TMS op); 42 | LUAI_FUNC void luaV_objlen (lua_State *L, StkId ra, const TValue *rb); 43 | 44 | #endif 45 | -------------------------------------------------------------------------------- /project/lua/lzio.c: -------------------------------------------------------------------------------- 1 | /* 2 | ** $Id: lzio.c,v 1.35.1.1 2013/04/12 18:48:47 roberto Exp $ 3 | ** Buffered streams 4 | ** See Copyright Notice in lua.h 5 | */ 6 | 7 | 8 | #include 9 | 10 | #define lzio_c 11 | #define LUA_CORE 12 | 13 | #include "lua.h" 14 | 15 | #include "llimits.h" 16 | #include "lmem.h" 17 | #include "lstate.h" 18 | #include "lzio.h" 19 | 20 | 21 | int luaZ_fill (ZIO *z) { 22 | size_t size; 23 | lua_State *L = z->L; 24 | const char *buff; 25 | lua_unlock(L); 26 | buff = z->reader(L, z->data, &size); 27 | lua_lock(L); 28 | if (buff == NULL || size == 0) 29 | return EOZ; 30 | z->n = size - 1; /* discount char being returned */ 31 | z->p = buff; 32 | return cast_uchar(*(z->p++)); 33 | } 34 | 35 | 36 | void luaZ_init (lua_State *L, ZIO *z, lua_Reader reader, void *data) { 37 | z->L = L; 38 | z->reader = reader; 39 | z->data = data; 40 | z->n = 0; 41 | z->p = NULL; 42 | } 43 | 44 | 45 | /* --------------------------------------------------------------- read --- */ 46 | size_t luaZ_read (ZIO *z, void *b, size_t n) { 47 | while (n) { 48 | size_t m; 49 | if (z->n == 0) { /* no bytes in buffer? */ 50 | if (luaZ_fill(z) == EOZ) /* try to read more */ 51 | return n; /* no more input; return number of missing bytes */ 52 | else { 53 | z->n++; /* luaZ_fill consumed first byte; put it back */ 54 | z->p--; 55 | } 56 | } 57 | m = (n <= z->n) ? n : z->n; /* min. between n and z->n */ 58 | memcpy(b, z->p, m); 59 | z->n -= m; 60 | z->p += m; 61 | b = (char *)b + m; 62 | n -= m; 63 | } 64 | return 0; 65 | } 66 | 67 | /* ------------------------------------------------------------------------ */ 68 | char *luaZ_openspace (lua_State *L, Mbuffer *buff, size_t n) { 69 | if (n > buff->buffsize) { 70 | if (n < LUA_MINBUFFER) n = LUA_MINBUFFER; 71 | luaZ_resizebuffer(L, buff, n); 72 | } 73 | return buff->buffer; 74 | } 75 | 76 | 77 | -------------------------------------------------------------------------------- /project/lua/lzio.h: -------------------------------------------------------------------------------- 1 | /* 2 | ** $Id: lzio.h,v 1.26.1.1 2013/04/12 18:48:47 roberto Exp $ 3 | ** Buffered streams 4 | ** See Copyright Notice in lua.h 5 | */ 6 | 7 | 8 | #ifndef lzio_h 9 | #define lzio_h 10 | 11 | #include "lua.h" 12 | 13 | #include "lmem.h" 14 | 15 | 16 | #define EOZ (-1) /* end of stream */ 17 | 18 | typedef struct Zio ZIO; 19 | 20 | #define zgetc(z) (((z)->n--)>0 ? cast_uchar(*(z)->p++) : luaZ_fill(z)) 21 | 22 | 23 | typedef struct Mbuffer { 24 | char *buffer; 25 | size_t n; 26 | size_t buffsize; 27 | } Mbuffer; 28 | 29 | #define luaZ_initbuffer(L, buff) ((buff)->buffer = NULL, (buff)->buffsize = 0) 30 | 31 | #define luaZ_buffer(buff) ((buff)->buffer) 32 | #define luaZ_sizebuffer(buff) ((buff)->buffsize) 33 | #define luaZ_bufflen(buff) ((buff)->n) 34 | 35 | #define luaZ_resetbuffer(buff) ((buff)->n = 0) 36 | 37 | 38 | #define luaZ_resizebuffer(L, buff, size) \ 39 | (luaM_reallocvector(L, (buff)->buffer, (buff)->buffsize, size, char), \ 40 | (buff)->buffsize = size) 41 | 42 | #define luaZ_freebuffer(L, buff) luaZ_resizebuffer(L, buff, 0) 43 | 44 | 45 | LUAI_FUNC char *luaZ_openspace (lua_State *L, Mbuffer *buff, size_t n); 46 | LUAI_FUNC void luaZ_init (lua_State *L, ZIO *z, lua_Reader reader, 47 | void *data); 48 | LUAI_FUNC size_t luaZ_read (ZIO* z, void* b, size_t n); /* read next n bytes */ 49 | 50 | 51 | 52 | /* --------- Private Part ------------------ */ 53 | 54 | struct Zio { 55 | size_t n; /* bytes still unread */ 56 | const char *p; /* current position in buffer */ 57 | lua_Reader reader; /* reader function */ 58 | void* data; /* additional data */ 59 | lua_State *L; /* Lua state (for reader) */ 60 | }; 61 | 62 | 63 | LUAI_FUNC int luaZ_fill (ZIO *z); 64 | 65 | #endif 66 | -------------------------------------------------------------------------------- /test/TestLua.hx: -------------------------------------------------------------------------------- 1 | class TestLua extends haxe.unit.TestCase 2 | { 3 | 4 | public function testVersion() 5 | { 6 | assertEquals("Lua 5.2", Lua.version); 7 | } 8 | 9 | public function testNull() 10 | { 11 | assertEquals(null, Lua.run("return null")); 12 | } 13 | 14 | public function testBoolean() 15 | { 16 | assertTrue(Lua.run("return true")); 17 | assertFalse(Lua.run("return false")); 18 | } 19 | 20 | public function testArray() 21 | { 22 | assertEquals(1, Lua.run("if arr[2] then return 1 else return 2 end", { 23 | arr: [false, true, false] 24 | })); 25 | } 26 | 27 | public function testInteger() 28 | { 29 | assertEquals(15, Lua.run("return num", {num: 15})); 30 | } 31 | 32 | public function testFloat() 33 | { 34 | assertEquals(15.3, Lua.run("return num", {num: 15.3})); 35 | } 36 | 37 | public function testObjectToTable() 38 | { 39 | assertTrue(Lua.run('return foo.bar', {foo: {bar: true}})); 40 | } 41 | 42 | public function testReturnTable() 43 | { 44 | var result:Array = Lua.run('return {"foo", true, 8.3, 6}'); 45 | assertEquals(4, result.length); 46 | assertEquals("foo", result[0]); 47 | assertEquals(true, result[1]); 48 | assertEquals(8.3, result[2]); 49 | assertEquals(6, result[3]); 50 | } 51 | 52 | public function testReturnObject() 53 | { 54 | var result = Lua.run('return {foo = true, bar = 95}'); 55 | assertEquals(true, result.foo); 56 | assertEquals(95, result.bar); 57 | } 58 | 59 | public function testEmbededObjects() 60 | { 61 | var result = Lua.run('return {foo = {bar = {baz = 30}, foo = 99}, bar = {14, 52, 25, 1, 7}}'); 62 | assertEquals(30, result.foo.bar.baz); 63 | assertEquals(99, result.foo.foo); 64 | assertEquals(5, cast(result.bar, Array).length); 65 | assertEquals(25, result.bar[2]); 66 | } 67 | 68 | public function testFunctionNoArgs() 69 | { 70 | assertEquals(true, Lua.run("return num()", {num: function() { return true; }})); 71 | } 72 | 73 | public function testFunctionArgs() 74 | { 75 | assertEquals(15, Lua.run("return num(true, 1)", { 76 | num: function(a:Bool, b:Int) { return 15; } 77 | })); 78 | } 79 | 80 | public function testFunctionPassThrough() 81 | { 82 | assertEquals("hello world", Lua.run('return greet(message)', { 83 | message: "hello world", 84 | greet: function(greeting:String) { return greeting; } 85 | })); 86 | } 87 | 88 | public function testMathLibrary() 89 | { 90 | var lua = new Lua(); 91 | lua.loadLibs(["math"]); 92 | assertEquals(3, lua.execute("return math.floor(3.6)")); 93 | } 94 | 95 | public function testMultipleInstances() 96 | { 97 | var l1 = new Lua(), 98 | l2 = new Lua(); 99 | 100 | var context = {foo: 1}; 101 | l1.setVars(context); 102 | 103 | assertEquals(1, l1.execute("return foo")); 104 | 105 | // change the context for l2 106 | context.foo = 2; 107 | l2.setVars(context); 108 | 109 | assertEquals(1, l1.execute("return foo")); 110 | assertEquals(2, l2.execute("return foo")); 111 | 112 | // change foo on l1 but not l2 113 | context.foo = 3; 114 | l1.setVars(context); 115 | 116 | assertEquals(3, l1.execute("return foo")); 117 | assertEquals(2, l2.execute("return foo")); 118 | } 119 | 120 | public function testCallLuaFunction() 121 | { 122 | var lua = new Lua(); 123 | lua.execute("-- comment line 124 | function add(a, b) 125 | return a + b 126 | end 127 | 128 | function sub(a, b) 129 | return a - b 130 | end"); 131 | 132 | assertEquals(8, lua.call("add", [2, 6])); 133 | assertEquals(29, lua.call("sub", [36, 7])); 134 | 135 | assertEquals(null, lua.call("fail", 3)); // fails due to missing function 136 | assertEquals(null, lua.call("sub", { fail: 3 })); // fails due to wrong number of arguments 137 | } 138 | 139 | public static function main() 140 | { 141 | var runner = new haxe.unit.TestRunner(); 142 | runner.add(new TestLua()); 143 | runner.run(); 144 | } 145 | 146 | } 147 | -------------------------------------------------------------------------------- /test/test.lua: -------------------------------------------------------------------------------- 1 | if arg[2] == 4 then 2 | return 102 3 | else 4 | return 103 5 | end 6 | --------------------------------------------------------------------------------