Update to Lua 5.2.3

develop
Alexander Gavrilov 2014-03-31 14:15:09 +04:00
parent 837da8ae98
commit 1132bea917
58 changed files with 513 additions and 386 deletions

@ -1,5 +1,5 @@
/* /*
** $Id: lapi.h,v 2.7 2009/11/27 15:37:59 roberto Exp $ ** $Id: lapi.h,v 2.7.1.1 2013/04/12 18:48:47 roberto Exp $
** Auxiliary functions from Lua API ** Auxiliary functions from Lua API
** See Copyright Notice in lua.h ** See Copyright Notice in lua.h
*/ */

@ -1,5 +1,5 @@
/* /*
** $Id: lauxlib.h,v 1.120 2011/11/29 15:55:08 roberto Exp $ ** $Id: lauxlib.h,v 1.120.1.1 2013/04/12 18:48:47 roberto Exp $
** Auxiliary functions for building Lua libraries ** Auxiliary functions for building Lua libraries
** See Copyright Notice in lua.h ** See Copyright Notice in lua.h
*/ */

@ -1,5 +1,5 @@
/* /*
** $Id: lcode.h,v 1.58 2011/08/30 16:26:41 roberto Exp $ ** $Id: lcode.h,v 1.58.1.1 2013/04/12 18:48:47 roberto Exp $
** Code generator for Lua ** Code generator for Lua
** See Copyright Notice in lua.h ** See Copyright Notice in lua.h
*/ */

@ -1,5 +1,5 @@
/* /*
** $Id: lctype.h,v 1.12 2011/07/15 12:50:29 roberto Exp $ ** $Id: lctype.h,v 1.12.1.1 2013/04/12 18:48:47 roberto Exp $
** 'ctype' functions for Lua ** 'ctype' functions for Lua
** See Copyright Notice in lua.h ** See Copyright Notice in lua.h
*/ */

@ -1,5 +1,5 @@
/* /*
** $Id: ldebug.h,v 2.7 2011/10/07 20:45:19 roberto Exp $ ** $Id: ldebug.h,v 2.7.1.1 2013/04/12 18:48:47 roberto Exp $
** Auxiliary functions from Debug Interface module ** Auxiliary functions from Debug Interface module
** See Copyright Notice in lua.h ** See Copyright Notice in lua.h
*/ */

@ -1,5 +1,5 @@
/* /*
** $Id: ldo.h,v 2.20 2011/11/29 15:55:08 roberto Exp $ ** $Id: ldo.h,v 2.20.1.1 2013/04/12 18:48:47 roberto Exp $
** Stack and Call structure of Lua ** Stack and Call structure of Lua
** See Copyright Notice in lua.h ** See Copyright Notice in lua.h
*/ */

@ -1,5 +1,5 @@
/* /*
** $Id: lfunc.h,v 2.8 2012/05/08 13:53:33 roberto Exp $ ** $Id: lfunc.h,v 2.8.1.1 2013/04/12 18:48:47 roberto Exp $
** Auxiliary functions to manipulate prototypes and closures ** Auxiliary functions to manipulate prototypes and closures
** See Copyright Notice in lua.h ** See Copyright Notice in lua.h
*/ */

@ -1,5 +1,5 @@
/* /*
** $Id: lgc.h,v 2.56 2012/05/23 15:43:14 roberto Exp $ ** $Id: lgc.h,v 2.58.1.1 2013/04/12 18:48:47 roberto Exp $
** Garbage Collector ** Garbage Collector
** See Copyright Notice in lua.h ** See Copyright Notice in lua.h
*/ */
@ -50,14 +50,24 @@
#define isgenerational(g) ((g)->gckind == KGC_GEN) #define isgenerational(g) ((g)->gckind == KGC_GEN)
/* /*
** macro to tell when main invariant (white objects cannot point to black ** macros to tell when main invariant (white objects cannot point to black
** ones) must be kept. During a non-generational collection, the sweep ** ones) must be kept. During a non-generational collection, the sweep
** phase may break the invariant, as objects turned white may point to ** phase may break the invariant, as objects turned white may point to
** still-black objects. The invariant is restored when sweep ends and ** still-black objects. The invariant is restored when sweep ends and
** all objects are white again. During a generational collection, the ** all objects are white again. During a generational collection, the
** invariant must be kept all times. ** invariant must be kept all times.
*/ */
#define keepinvariant(g) (isgenerational(g) || g->gcstate <= GCSatomic)
#define keepinvariant(g) (isgenerational(g) || g->gcstate <= GCSatomic)
/*
** Outside the collector, the state in generational mode is kept in
** 'propagate', so 'keepinvariant' is always true.
*/
#define keepinvariantout(g) \
check_exp(g->gcstate == GCSpropagate || !isgenerational(g), \
g->gcstate <= GCSatomic)
/* /*

@ -1,5 +1,5 @@
/* /*
** $Id: llex.h,v 1.72 2011/11/30 12:43:51 roberto Exp $ ** $Id: llex.h,v 1.72.1.1 2013/04/12 18:48:47 roberto Exp $
** Lexical Analyzer ** Lexical Analyzer
** See Copyright Notice in lua.h ** See Copyright Notice in lua.h
*/ */

@ -1,5 +1,5 @@
/* /*
** $Id: llimits.h,v 1.99 2012/05/28 20:32:28 roberto Exp $ ** $Id: llimits.h,v 1.103.1.1 2013/04/12 18:48:47 roberto Exp $
** Limits, basic types, and some other `installation-dependent' definitions ** Limits, basic types, and some other `installation-dependent' definitions
** See Copyright Notice in lua.h ** See Copyright Notice in lua.h
*/ */
@ -200,7 +200,7 @@ typedef lu_int32 Instruction;
** both small and large values (outside the range of integers). ** both small and large values (outside the range of integers).
*/ */
#if defined(MS_ASMTRICK) /* { */ #if defined(MS_ASMTRICK) || defined(LUA_MSASMTRICK) /* { */
/* trick with Microsoft assembler for X86 */ /* trick with Microsoft assembler for X86 */
#define lua_number2int(i,n) __asm {__asm fld n __asm fistp i} #define lua_number2int(i,n) __asm {__asm fld n __asm fistp i}
@ -256,7 +256,7 @@ union luai_Cast { double l_d; LUA_INT32 l_p[2]; };
#if !defined(lua_number2unsigned) /* { */ #if !defined(lua_number2unsigned) /* { */
/* the following definition assures proper modulo behavior */ /* the following definition assures proper modulo behavior */
#if defined(LUA_NUMBER_DOUBLE) #if defined(LUA_NUMBER_DOUBLE) || defined(LUA_NUMBER_FLOAT)
#include <math.h> #include <math.h>
#define SUPUNSIGNED ((lua_Number)(~(lua_Unsigned)0) + 1) #define SUPUNSIGNED ((lua_Number)(~(lua_Unsigned)0) + 1)
#define lua_number2unsigned(i,n) \ #define lua_number2unsigned(i,n) \
@ -282,7 +282,7 @@ union luai_Cast { double l_d; LUA_INT32 l_p[2]; };
#include <math.h> #include <math.h>
#define luai_hashnum(i,n) { int e; \ #define luai_hashnum(i,n) { int e; \
n = frexp(n, &e) * (lua_Number)(INT_MAX - DBL_MAX_EXP); \ n = l_mathop(frexp)(n, &e) * (lua_Number)(INT_MAX - DBL_MAX_EXP); \
lua_number2int(i, n); i += e; } lua_number2int(i, n); i += e; }
#endif #endif

@ -1,5 +1,5 @@
/* /*
** $Id: lmem.h,v 1.38 2011/12/02 13:26:54 roberto Exp $ ** $Id: lmem.h,v 1.40.1.1 2013/04/12 18:48:47 roberto Exp $
** Interface to Memory Manager ** Interface to Memory Manager
** See Copyright Notice in lua.h ** See Copyright Notice in lua.h
*/ */
@ -14,10 +14,17 @@
#include "lua.h" #include "lua.h"
/*
** This macro avoids the runtime division MAX_SIZET/(e), as 'e' is
** always constant.
** The macro is somewhat complex to avoid warnings:
** +1 avoids warnings of "comparison has constant result";
** cast to 'void' avoids warnings of "value unused".
*/
#define luaM_reallocv(L,b,on,n,e) \ #define luaM_reallocv(L,b,on,n,e) \
((cast(size_t, (n)+1) > MAX_SIZET/(e)) ? /* +1 to avoid warnings */ \ (cast(void, \
(luaM_toobig(L), (void *)0) : \ (cast(size_t, (n)+1) > MAX_SIZET/(e)) ? (luaM_toobig(L), 0) : 0), \
luaM_realloc_(L, (b), (on)*(e), (n)*(e))) luaM_realloc_(L, (b), (on)*(e), (n)*(e)))
#define luaM_freemem(L, b, s) luaM_realloc_(L, (b), (s), 0) #define luaM_freemem(L, b, s) luaM_realloc_(L, (b), (s), 0)
#define luaM_free(L, b) luaM_realloc_(L, (b), sizeof(*(b)), 0) #define luaM_free(L, b) luaM_realloc_(L, (b), sizeof(*(b)), 0)

@ -1,5 +1,5 @@
/* /*
** $Id: lobject.h,v 2.70 2012/05/11 14:10:50 roberto Exp $ ** $Id: lobject.h,v 2.71.1.1 2013/04/12 18:48:47 roberto Exp $
** Type definitions for Lua objects ** Type definitions for Lua objects
** See Copyright Notice in lua.h ** See Copyright Notice in lua.h
*/ */
@ -52,8 +52,7 @@
#define LUA_TCCL (LUA_TFUNCTION | (2 << 4)) /* C closure */ #define LUA_TCCL (LUA_TFUNCTION | (2 << 4)) /* C closure */
/* /* Variant tags for strings */
** LUA_TSTRING variants */
#define LUA_TSHRSTR (LUA_TSTRING | (0 << 4)) /* short strings */ #define LUA_TSHRSTR (LUA_TSTRING | (0 << 4)) /* short strings */
#define LUA_TLNGSTR (LUA_TSTRING | (1 << 4)) /* long strings */ #define LUA_TLNGSTR (LUA_TSTRING | (1 << 4)) /* long strings */
@ -188,8 +187,6 @@ typedef struct lua_TValue TValue;
#define setnvalue(obj,x) \ #define setnvalue(obj,x) \
{ TValue *io=(obj); num_(io)=(x); settt_(io, LUA_TNUMBER); } { TValue *io=(obj); num_(io)=(x); settt_(io, LUA_TNUMBER); }
#define changenvalue(o,x) check_exp(ttisnumber(o), num_(o)=(x))
#define setnilvalue(obj) settt_(obj, LUA_TNIL) #define setnilvalue(obj) settt_(obj, LUA_TNIL)
#define setfvalue(obj,x) \ #define setfvalue(obj,x) \

@ -1,5 +1,5 @@
/* /*
** $Id: lopcodes.h,v 1.142 2011/07/15 12:50:29 roberto Exp $ ** $Id: lopcodes.h,v 1.142.1.1 2013/04/12 18:48:47 roberto Exp $
** Opcodes for Lua virtual machine ** Opcodes for Lua virtual machine
** See Copyright Notice in lua.h ** See Copyright Notice in lua.h
*/ */

@ -1,5 +1,5 @@
/* /*
** $Id: lparser.h,v 1.70 2012/05/08 13:53:33 roberto Exp $ ** $Id: lparser.h,v 1.70.1.1 2013/04/12 18:48:47 roberto Exp $
** Lua Parser ** Lua Parser
** See Copyright Notice in lua.h ** See Copyright Notice in lua.h
*/ */

@ -1,5 +1,5 @@
/* /*
** $Id: lstate.h,v 2.81 2012/06/08 15:14:04 roberto Exp $ ** $Id: lstate.h,v 2.82.1.1 2013/04/12 18:48:47 roberto Exp $
** Global State ** Global State
** See Copyright Notice in lua.h ** See Copyright Notice in lua.h
*/ */
@ -137,7 +137,7 @@ typedef struct global_State {
UpVal uvhead; /* head of double-linked list of all open upvalues */ UpVal uvhead; /* head of double-linked list of all open upvalues */
Mbuffer buff; /* temporary buffer for string concatenation */ Mbuffer buff; /* temporary buffer for string concatenation */
int gcpause; /* size of pause between successive GCs */ int gcpause; /* size of pause between successive GCs */
int gcmajorinc; /* how much to wait for a major GC (only in gen. mode) */ int gcmajorinc; /* pause between major collections (only in gen. mode) */
int gcstepmul; /* GC `granularity' */ int gcstepmul; /* GC `granularity' */
lua_CFunction panic; /* to be called in unprotected errors */ lua_CFunction panic; /* to be called in unprotected errors */
struct lua_State *mainthread; struct lua_State *mainthread;

@ -1,5 +1,5 @@
/* /*
** $Id: lstring.h,v 1.49 2012/02/01 21:57:15 roberto Exp $ ** $Id: lstring.h,v 1.49.1.1 2013/04/12 18:48:47 roberto Exp $
** String table (keep all strings handled by Lua) ** String table (keep all strings handled by Lua)
** See Copyright Notice in lua.h ** See Copyright Notice in lua.h
*/ */

@ -1,5 +1,5 @@
/* /*
** $Id: ltable.h,v 2.16 2011/08/17 20:26:47 roberto Exp $ ** $Id: ltable.h,v 2.16.1.2 2013/08/30 15:49:41 roberto Exp $
** Lua tables (hash) ** Lua tables (hash)
** See Copyright Notice in lua.h ** See Copyright Notice in lua.h
*/ */
@ -17,6 +17,10 @@
#define invalidateTMcache(t) ((t)->flags = 0) #define invalidateTMcache(t) ((t)->flags = 0)
/* returns the key, given the value of a table entry */
#define keyfromval(v) \
(gkey(cast(Node *, cast(char *, (v)) - offsetof(Node, i_val))))
LUAI_FUNC const TValue *luaH_getint (Table *t, int key); LUAI_FUNC const TValue *luaH_getint (Table *t, int key);
LUAI_FUNC void luaH_setint (lua_State *L, Table *t, int key, TValue *value); LUAI_FUNC void luaH_setint (lua_State *L, Table *t, int key, TValue *value);

@ -1,5 +1,5 @@
/* /*
** $Id: ltm.h,v 2.11 2011/02/28 17:32:10 roberto Exp $ ** $Id: ltm.h,v 2.11.1.1 2013/04/12 18:48:47 roberto Exp $
** Tag methods ** Tag methods
** See Copyright Notice in lua.h ** See Copyright Notice in lua.h
*/ */

@ -1,5 +1,5 @@
/* /*
** $Id: lua.h,v 1.283 2012/04/20 13:18:26 roberto Exp $ ** $Id: lua.h,v 1.285.1.2 2013/11/11 12:09:16 roberto Exp $
** Lua - A Scripting Language ** Lua - A Scripting Language
** Lua.org, PUC-Rio, Brazil (http://www.lua.org) ** Lua.org, PUC-Rio, Brazil (http://www.lua.org)
** See Copyright Notice at the end of this file ** See Copyright Notice at the end of this file
@ -19,11 +19,11 @@
#define LUA_VERSION_MAJOR "5" #define LUA_VERSION_MAJOR "5"
#define LUA_VERSION_MINOR "2" #define LUA_VERSION_MINOR "2"
#define LUA_VERSION_NUM 502 #define LUA_VERSION_NUM 502
#define LUA_VERSION_RELEASE "1" #define LUA_VERSION_RELEASE "3"
#define LUA_VERSION "Lua " LUA_VERSION_MAJOR "." LUA_VERSION_MINOR #define LUA_VERSION "Lua " LUA_VERSION_MAJOR "." LUA_VERSION_MINOR
#define LUA_RELEASE LUA_VERSION "." LUA_VERSION_RELEASE #define LUA_RELEASE LUA_VERSION "." LUA_VERSION_RELEASE
#define LUA_COPYRIGHT LUA_RELEASE " Copyright (C) 1994-2012 Lua.org, PUC-Rio" #define LUA_COPYRIGHT LUA_RELEASE " Copyright (C) 1994-2013 Lua.org, PUC-Rio"
#define LUA_AUTHORS "R. Ierusalimschy, L. H. de Figueiredo, W. Celes" #define LUA_AUTHORS "R. Ierusalimschy, L. H. de Figueiredo, W. Celes"
@ -119,6 +119,11 @@ typedef LUA_UNSIGNED lua_Unsigned;
#endif #endif
/*
** RCS ident string
*/
extern const char lua_ident[];
/* /*
** state manipulation ** state manipulation
@ -413,7 +418,7 @@ struct lua_Debug {
/****************************************************************************** /******************************************************************************
* Copyright (C) 1994-2012 Lua.org, PUC-Rio. * Copyright (C) 1994-2013 Lua.org, PUC-Rio.
* *
* Permission is hereby granted, free of charge, to any person obtaining * Permission is hereby granted, free of charge, to any person obtaining
* a copy of this software and associated documentation files (the * a copy of this software and associated documentation files (the

@ -1,5 +1,5 @@
/* /*
** $Id: luaconf.h,v 1.172 2012/05/11 14:14:42 roberto Exp $ ** $Id: luaconf.h,v 1.176.1.1 2013/04/12 18:48:47 roberto Exp $
** Configuration file for Lua ** Configuration file for Lua
** See Copyright Notice in lua.h ** See Copyright Notice in lua.h
*/ */
@ -44,7 +44,7 @@
#define LUA_USE_POSIX #define LUA_USE_POSIX
#define LUA_USE_DLOPEN /* needs an extra library: -ldl */ #define LUA_USE_DLOPEN /* needs an extra library: -ldl */
#define LUA_USE_READLINE /* needs some extra libraries */ #define LUA_USE_READLINE /* needs some extra libraries */
#define LUA_USE_STRTODHEX /* assume 'strtod' handles hexa formats */ #define LUA_USE_STRTODHEX /* assume 'strtod' handles hex formats */
#define LUA_USE_AFORMAT /* assume 'printf' handles 'aA' specifiers */ #define LUA_USE_AFORMAT /* assume 'printf' handles 'aA' specifiers */
#define LUA_USE_LONGLONG /* assume support for long long */ #define LUA_USE_LONGLONG /* assume support for long long */
#endif #endif
@ -53,7 +53,7 @@
#define LUA_USE_POSIX #define LUA_USE_POSIX
#define LUA_USE_DLOPEN /* does not need -ldl */ #define LUA_USE_DLOPEN /* does not need -ldl */
#define LUA_USE_READLINE /* needs an extra library: -lreadline */ #define LUA_USE_READLINE /* needs an extra library: -lreadline */
#define LUA_USE_STRTODHEX /* assume 'strtod' handles hexa formats */ #define LUA_USE_STRTODHEX /* assume 'strtod' handles hex formats */
#define LUA_USE_AFORMAT /* assume 'printf' handles 'aA' specifiers */ #define LUA_USE_AFORMAT /* assume 'printf' handles 'aA' specifiers */
#define LUA_USE_LONGLONG /* assume support for long long */ #define LUA_USE_LONGLONG /* assume support for long long */
#endif #endif
@ -89,21 +89,25 @@
** In Windows, any exclamation mark ('!') in the path is replaced by the ** In Windows, any exclamation mark ('!') in the path is replaced by the
** path of the directory of the executable file of the current process. ** path of the directory of the executable file of the current process.
*/ */
#define LUA_LDIR "!\\hack\\lua\\" #define LUA_LDIR "!\\lua\\"
#define LUA_CDIR "!\\" #define LUA_CDIR "!\\"
#define LUA_PATH_DEFAULT \ #define LUA_PATH_DEFAULT \
LUA_LDIR"?.lua;" LUA_LDIR"?\\init.lua;" ".\\?.lua" LUA_LDIR"?.lua;" LUA_LDIR"?\\init.lua;" \
LUA_CDIR"?.lua;" LUA_CDIR"?\\init.lua;" ".\\?.lua"
#define LUA_CPATH_DEFAULT \ #define LUA_CPATH_DEFAULT \
LUA_CDIR"?.dll;" ".\\?.dll" LUA_CDIR"?.dll;" LUA_CDIR"loadall.dll;" ".\\?.dll"
#else /* }{ */ #else /* }{ */
#define LUA_LDIR "./hack/lua/" #define LUA_VDIR LUA_VERSION_MAJOR "." LUA_VERSION_MINOR "/"
#define LUA_CDIR "./hack/" #define LUA_ROOT "/usr/local/"
#define LUA_LDIR LUA_ROOT "share/lua/" LUA_VDIR
#define LUA_CDIR LUA_ROOT "lib/lua/" LUA_VDIR
#define LUA_PATH_DEFAULT \ #define LUA_PATH_DEFAULT \
LUA_LDIR"?.lua;" LUA_LDIR"?/init.lua;" "./?.lua" LUA_LDIR"?.lua;" LUA_LDIR"?/init.lua;" \
LUA_CDIR"?.lua;" LUA_CDIR"?/init.lua;" "./?.lua"
#define LUA_CPATH_DEFAULT \ #define LUA_CPATH_DEFAULT \
LUA_CDIR"?.so;" "./?.so" LUA_CDIR"?.so;" LUA_CDIR"loadall.so;" "./?.so"
#endif /* } */ #endif /* } */
@ -138,15 +142,11 @@
*/ */
#if defined(LUA_BUILD_AS_DLL) /* { */ #if defined(LUA_BUILD_AS_DLL) /* { */
#if defined(_MSC_VER)
#if defined(LUA_CORE) || defined(LUA_LIB) /* { */ #if defined(LUA_CORE) || defined(LUA_LIB) /* { */
#define LUA_API __declspec(dllexport) #define LUA_API __declspec(dllexport)
#else /* }{ */ #else /* }{ */
#define LUA_API __declspec(dllimport) #define LUA_API __declspec(dllimport)
#endif /* } */ #endif /* } */
#else
#define LUA_API __attribute__ ((visibility("default")))
#endif
#else /* }{ */ #else /* }{ */
@ -405,10 +405,16 @@
#define LUAI_MAXNUMBER2STR 32 /* 16 digits, sign, point, and \0 */ #define LUAI_MAXNUMBER2STR 32 /* 16 digits, sign, point, and \0 */
/*
@@ l_mathop allows the addition of an 'l' or 'f' to all math operations
*/
#define l_mathop(x) (x)
/* /*
@@ lua_str2number converts a decimal numeric string to a number. @@ lua_str2number converts a decimal numeric string to a number.
@@ lua_strx2number converts an hexadecimal numeric string to a number. @@ lua_strx2number converts an hexadecimal numeric string to a number.
** In C99, 'strtod' do both conversions. C89, however, has no function ** In C99, 'strtod' does both conversions. C89, however, has no function
** to convert floating hexadecimal strings to numbers. For these ** to convert floating hexadecimal strings to numbers. For these
** systems, you can leave 'lua_strx2number' undefined and Lua will ** systems, you can leave 'lua_strx2number' undefined and Lua will
** provide its own implementation. ** provide its own implementation.
@ -427,8 +433,8 @@
/* the following operations need the math library */ /* the following operations need the math library */
#if defined(lobject_c) || defined(lvm_c) #if defined(lobject_c) || defined(lvm_c)
#include <math.h> #include <math.h>
#define luai_nummod(L,a,b) ((a) - floor((a)/(b))*(b)) #define luai_nummod(L,a,b) ((a) - l_mathop(floor)((a)/(b))*(b))
#define luai_numpow(L,a,b) (pow(a,b)) #define luai_numpow(L,a,b) (l_mathop(pow)(a,b))
#endif #endif
/* these are quite standard operations */ /* these are quite standard operations */
@ -465,13 +471,12 @@
** Some tricks with doubles ** Some tricks with doubles
*/ */
#if defined(LUA_CORE) && \ #if defined(LUA_NUMBER_DOUBLE) && !defined(LUA_ANSI) /* { */
defined(LUA_NUMBER_DOUBLE) && !defined(LUA_ANSI) /* { */
/* /*
** The next definitions activate some tricks to speed up the ** The next definitions activate some tricks to speed up the
** conversion from doubles to integer types, mainly to LUA_UNSIGNED. ** conversion from doubles to integer types, mainly to LUA_UNSIGNED.
** **
@@ MS_ASMTRICK uses Microsoft assembler to avoid clashes with a @@ LUA_MSASMTRICK uses Microsoft assembler to avoid clashes with a
** DirectX idiosyncrasy. ** DirectX idiosyncrasy.
** **
@@ LUA_IEEE754TRICK uses a trick that should work on any machine @@ LUA_IEEE754TRICK uses a trick that should work on any machine
@ -495,7 +500,7 @@
/* Microsoft compiler on a Pentium (32 bit) ? */ /* Microsoft compiler on a Pentium (32 bit) ? */
#if defined(LUA_WIN) && defined(_MSC_VER) && defined(_M_IX86) /* { */ #if defined(LUA_WIN) && defined(_MSC_VER) && defined(_M_IX86) /* { */
#define MS_ASMTRICK #define LUA_MSASMTRICK
#define LUA_IEEEENDIAN 0 #define LUA_IEEEENDIAN 0
#define LUA_NANTRICK #define LUA_NANTRICK

@ -1,5 +1,5 @@
/* /*
** $Id: lualib.h,v 1.43 2011/12/08 12:11:37 roberto Exp $ ** $Id: lualib.h,v 1.43.1.1 2013/04/12 18:48:47 roberto Exp $
** Lua standard libraries ** Lua standard libraries
** See Copyright Notice in lua.h ** See Copyright Notice in lua.h
*/ */

@ -1,5 +1,5 @@
/* /*
** $Id: lundump.h,v 1.39 2012/05/08 13:53:33 roberto Exp $ ** $Id: lundump.h,v 1.39.1.1 2013/04/12 18:48:47 roberto Exp $
** load precompiled Lua chunks ** load precompiled Lua chunks
** See Copyright Notice in lua.h ** See Copyright Notice in lua.h
*/ */

@ -1,5 +1,5 @@
/* /*
** $Id: lvm.h,v 2.17 2011/05/31 18:27:56 roberto Exp $ ** $Id: lvm.h,v 2.18.1.1 2013/04/12 18:48:47 roberto Exp $
** Lua virtual machine ** Lua virtual machine
** See Copyright Notice in lua.h ** See Copyright Notice in lua.h
*/ */
@ -19,8 +19,7 @@
#define equalobj(L,o1,o2) (ttisequal(o1, o2) && luaV_equalobj_(L, o1, o2)) #define equalobj(L,o1,o2) (ttisequal(o1, o2) && luaV_equalobj_(L, o1, o2))
#define luaV_rawequalobj(t1,t2) \ #define luaV_rawequalobj(o1,o2) equalobj(NULL,o1,o2)
(ttisequal(t1,t2) && luaV_equalobj_(NULL,t1,t2))
/* not to called directly */ /* not to called directly */

@ -1,5 +1,5 @@
/* /*
** $Id: lzio.h,v 1.26 2011/07/15 12:48:03 roberto Exp $ ** $Id: lzio.h,v 1.26.1.1 2013/04/12 18:48:47 roberto Exp $
** Buffered streams ** Buffered streams
** See Copyright Notice in lua.h ** See Copyright Notice in lua.h
*/ */

@ -1,5 +1,5 @@
/* /*
** $Id: lapi.c,v 2.164 2012/06/08 15:14:04 roberto Exp $ ** $Id: lapi.c,v 2.171.1.1 2013/04/12 18:48:47 roberto Exp $
** Lua API ** Lua API
** See Copyright Notice in lua.h ** See Copyright Notice in lua.h
*/ */
@ -40,7 +40,16 @@ const char lua_ident[] =
/* corresponding test */ /* corresponding test */
#define isvalid(o) ((o) != luaO_nilobject) #define isvalid(o) ((o) != luaO_nilobject)
#define api_checkvalidindex(L, i) api_check(L, isvalid(i), "invalid index") /* test for pseudo index */
#define ispseudo(i) ((i) <= LUA_REGISTRYINDEX)
/* test for valid but not pseudo index */
#define isstackindex(i, o) (isvalid(o) && !ispseudo(i))
#define api_checkvalidindex(L, o) api_check(L, isvalid(o), "invalid index")
#define api_checkstackindex(L, i, o) \
api_check(L, isstackindex(i, o), "index not in the stack")
static TValue *index2addr (lua_State *L, int idx) { static TValue *index2addr (lua_State *L, int idx) {
@ -51,7 +60,7 @@ static TValue *index2addr (lua_State *L, int idx) {
if (o >= L->top) return NONVALIDVALUE; if (o >= L->top) return NONVALIDVALUE;
else return o; else return o;
} }
else if (idx > LUA_REGISTRYINDEX) { else if (!ispseudo(idx)) { /* negative index */
api_check(L, idx != 0 && -idx <= L->top - (ci->func + 1), "invalid index"); api_check(L, idx != 0 && -idx <= L->top - (ci->func + 1), "invalid index");
return L->top + idx; return L->top + idx;
} }
@ -142,7 +151,7 @@ LUA_API const lua_Number *lua_version (lua_State *L) {
** convert an acceptable stack index into an absolute index ** convert an acceptable stack index into an absolute index
*/ */
LUA_API int lua_absindex (lua_State *L, int idx) { LUA_API int lua_absindex (lua_State *L, int idx) {
return (idx > 0 || idx <= LUA_REGISTRYINDEX) return (idx > 0 || ispseudo(idx))
? idx ? idx
: cast_int(L->top - L->ci->func + idx); : cast_int(L->top - L->ci->func + idx);
} }
@ -174,7 +183,7 @@ LUA_API void lua_remove (lua_State *L, int idx) {
StkId p; StkId p;
lua_lock(L); lua_lock(L);
p = index2addr(L, idx); p = index2addr(L, idx);
api_checkvalidindex(L, p); api_checkstackindex(L, idx, p);
while (++p < L->top) setobjs2s(L, p-1, p); while (++p < L->top) setobjs2s(L, p-1, p);
L->top--; L->top--;
lua_unlock(L); lua_unlock(L);
@ -186,8 +195,9 @@ LUA_API void lua_insert (lua_State *L, int idx) {
StkId q; StkId q;
lua_lock(L); lua_lock(L);
p = index2addr(L, idx); p = index2addr(L, idx);
api_checkvalidindex(L, p); api_checkstackindex(L, idx, p);
for (q = L->top; q>p; q--) setobjs2s(L, q, q-1); for (q = L->top; q > p; q--) /* use L->top as a temporary */
setobjs2s(L, q, q - 1);
setobjs2s(L, p, L->top); setobjs2s(L, p, L->top);
lua_unlock(L); lua_unlock(L);
} }
@ -217,7 +227,6 @@ LUA_API void lua_copy (lua_State *L, int fromidx, int toidx) {
TValue *fr; TValue *fr;
lua_lock(L); lua_lock(L);
fr = index2addr(L, fromidx); fr = index2addr(L, fromidx);
api_checkvalidindex(L, fr);
moveto(L, fr, toidx); moveto(L, fr, toidx);
lua_unlock(L); lua_unlock(L);
} }
@ -281,7 +290,7 @@ LUA_API int lua_rawequal (lua_State *L, int index1, int index2) {
} }
LUA_API void lua_arith (lua_State *L, int op) { LUA_API void lua_arith (lua_State *L, int op) {
StkId o1; /* 1st operand */ StkId o1; /* 1st operand */
StkId o2; /* 2nd operand */ StkId o2; /* 2nd operand */
lua_lock(L); lua_lock(L);
@ -295,7 +304,7 @@ LUA_API void lua_arith (lua_State *L, int op) {
o1 = L->top - 2; o1 = L->top - 2;
o2 = L->top - 1; o2 = L->top - 1;
if (ttisnumber(o1) && ttisnumber(o2)) { if (ttisnumber(o1) && ttisnumber(o2)) {
changenvalue(o1, luaO_arith(op, nvalue(o1), nvalue(o2))); setnvalue(o1, luaO_arith(op, nvalue(o1), nvalue(o2)));
} }
else else
luaV_arith(L, o1, o1, o2, cast(TMS, op - LUA_OPADD + TM_ADD)); luaV_arith(L, o1, o1, o2, cast(TMS, op - LUA_OPADD + TM_ADD));
@ -611,7 +620,6 @@ LUA_API void lua_gettable (lua_State *L, int idx) {
StkId t; StkId t;
lua_lock(L); lua_lock(L);
t = index2addr(L, idx); t = index2addr(L, idx);
api_checkvalidindex(L, t);
luaV_gettable(L, t, L->top - 1, L->top - 1); luaV_gettable(L, t, L->top - 1, L->top - 1);
lua_unlock(L); lua_unlock(L);
} }
@ -621,7 +629,6 @@ LUA_API void lua_getfield (lua_State *L, int idx, const char *k) {
StkId t; StkId t;
lua_lock(L); lua_lock(L);
t = index2addr(L, idx); t = index2addr(L, idx);
api_checkvalidindex(L, t);
setsvalue2s(L, L->top, luaS_new(L, k)); setsvalue2s(L, L->top, luaS_new(L, k));
api_incr_top(L); api_incr_top(L);
luaV_gettable(L, t, L->top - 1, L->top - 1); luaV_gettable(L, t, L->top - 1, L->top - 1);
@ -709,7 +716,6 @@ LUA_API void lua_getuservalue (lua_State *L, int idx) {
StkId o; StkId o;
lua_lock(L); lua_lock(L);
o = index2addr(L, idx); o = index2addr(L, idx);
api_checkvalidindex(L, o);
api_check(L, ttisuserdata(o), "userdata expected"); api_check(L, ttisuserdata(o), "userdata expected");
if (uvalue(o)->env) { if (uvalue(o)->env) {
sethvalue(L, L->top, uvalue(o)->env); sethvalue(L, L->top, uvalue(o)->env);
@ -743,7 +749,6 @@ LUA_API void lua_settable (lua_State *L, int idx) {
lua_lock(L); lua_lock(L);
api_checknelems(L, 2); api_checknelems(L, 2);
t = index2addr(L, idx); t = index2addr(L, idx);
api_checkvalidindex(L, t);
luaV_settable(L, t, L->top - 2, L->top - 1); luaV_settable(L, t, L->top - 2, L->top - 1);
L->top -= 2; /* pop index and value */ L->top -= 2; /* pop index and value */
lua_unlock(L); lua_unlock(L);
@ -755,7 +760,6 @@ LUA_API void lua_setfield (lua_State *L, int idx, const char *k) {
lua_lock(L); lua_lock(L);
api_checknelems(L, 1); api_checknelems(L, 1);
t = index2addr(L, idx); t = index2addr(L, idx);
api_checkvalidindex(L, t);
setsvalue2s(L, L->top++, luaS_new(L, k)); setsvalue2s(L, L->top++, luaS_new(L, k));
luaV_settable(L, t, L->top - 1, L->top - 2); luaV_settable(L, t, L->top - 1, L->top - 2);
L->top -= 2; /* pop value and key */ L->top -= 2; /* pop value and key */
@ -811,7 +815,6 @@ LUA_API int lua_setmetatable (lua_State *L, int objindex) {
lua_lock(L); lua_lock(L);
api_checknelems(L, 1); api_checknelems(L, 1);
obj = index2addr(L, objindex); obj = index2addr(L, objindex);
api_checkvalidindex(L, obj);
if (ttisnil(L->top - 1)) if (ttisnil(L->top - 1))
mt = NULL; mt = NULL;
else { else {
@ -821,9 +824,10 @@ LUA_API int lua_setmetatable (lua_State *L, int objindex) {
switch (ttypenv(obj)) { switch (ttypenv(obj)) {
case LUA_TTABLE: { case LUA_TTABLE: {
hvalue(obj)->metatable = mt; hvalue(obj)->metatable = mt;
if (mt) if (mt) {
luaC_objbarrierback(L, gcvalue(obj), mt); luaC_objbarrierback(L, gcvalue(obj), mt);
luaC_checkfinalizer(L, gcvalue(obj), mt); luaC_checkfinalizer(L, gcvalue(obj), mt);
}
break; break;
} }
case LUA_TUSERDATA: { case LUA_TUSERDATA: {
@ -850,7 +854,6 @@ LUA_API void lua_setuservalue (lua_State *L, int idx) {
lua_lock(L); lua_lock(L);
api_checknelems(L, 1); api_checknelems(L, 1);
o = index2addr(L, idx); o = index2addr(L, idx);
api_checkvalidindex(L, o);
api_check(L, ttisuserdata(o), "userdata expected"); api_check(L, ttisuserdata(o), "userdata expected");
if (ttisnil(L->top - 1)) if (ttisnil(L->top - 1))
uvalue(o)->env = NULL; uvalue(o)->env = NULL;
@ -937,7 +940,7 @@ LUA_API int lua_pcallk (lua_State *L, int nargs, int nresults, int errfunc,
func = 0; func = 0;
else { else {
StkId o = index2addr(L, errfunc); StkId o = index2addr(L, errfunc);
api_checkvalidindex(L, o); api_checkstackindex(L, errfunc, o);
func = savestack(L, o); func = savestack(L, o);
} }
c.func = L->top - (nargs+1); /* function to be called */ c.func = L->top - (nargs+1); /* function to be called */
@ -1006,7 +1009,7 @@ LUA_API int lua_dump (lua_State *L, lua_Writer writer, void *data) {
} }
LUA_API int lua_status (lua_State *L) { LUA_API int lua_status (lua_State *L) {
return L->status; return L->status;
} }
@ -1103,7 +1106,7 @@ LUA_API int lua_error (lua_State *L) {
lua_lock(L); lua_lock(L);
api_checknelems(L, 1); api_checknelems(L, 1);
luaG_errormsg(L); luaG_errormsg(L);
lua_unlock(L); /* code unreachable; will unlock when control actually leaves the kernel */
return 0; /* to avoid warnings */ return 0; /* to avoid warnings */
} }

@ -1,5 +1,5 @@
/* /*
** $Id: lauxlib.c,v 1.244 2012/05/31 20:28:45 roberto Exp $ ** $Id: lauxlib.c,v 1.248.1.1 2013/04/12 18:48:47 roberto Exp $
** Auxiliary functions for building Lua libraries ** Auxiliary functions for building Lua libraries
** See Copyright Notice in lua.h ** See Copyright Notice in lua.h
*/ */
@ -84,7 +84,7 @@ static void pushfuncname (lua_State *L, lua_Debug *ar) {
if (*ar->namewhat != '\0') /* is there a name? */ if (*ar->namewhat != '\0') /* is there a name? */
lua_pushfstring(L, "function " LUA_QS, ar->name); lua_pushfstring(L, "function " LUA_QS, ar->name);
else if (*ar->what == 'm') /* main? */ else if (*ar->what == 'm') /* main? */
lua_pushfstring(L, "main chunk"); lua_pushliteral(L, "main chunk");
else if (*ar->what == 'C') { else if (*ar->what == 'C') {
if (pushglobalfuncname(L, ar)) { if (pushglobalfuncname(L, ar)) {
lua_pushfstring(L, "function " LUA_QS, lua_tostring(L, -1)); lua_pushfstring(L, "function " LUA_QS, lua_tostring(L, -1));
@ -158,7 +158,8 @@ LUALIB_API int luaL_argerror (lua_State *L, int narg, const char *extramsg) {
if (strcmp(ar.namewhat, "method") == 0) { if (strcmp(ar.namewhat, "method") == 0) {
narg--; /* do not count `self' */ narg--; /* do not count `self' */
if (narg == 0) /* error is in the self argument itself? */ if (narg == 0) /* error is in the self argument itself? */
return luaL_error(L, "calling " LUA_QS " on bad self", ar.name); return luaL_error(L, "calling " LUA_QS " on bad self (%s)",
ar.name, extramsg);
} }
if (ar.name == NULL) if (ar.name == NULL)
ar.name = (pushglobalfuncname(L, &ar)) ? lua_tostring(L, -1) : "?"; ar.name = (pushglobalfuncname(L, &ar)) ? lua_tostring(L, -1) : "?";
@ -214,7 +215,7 @@ LUALIB_API int luaL_fileresult (lua_State *L, int stat, const char *fname) {
if (fname) if (fname)
lua_pushfstring(L, "%s: %s", fname, strerror(en)); lua_pushfstring(L, "%s: %s", fname, strerror(en));
else else
lua_pushfstring(L, "%s", strerror(en)); lua_pushstring(L, strerror(en));
lua_pushinteger(L, en); lua_pushinteger(L, en);
return 3; return 3;
} }
@ -438,7 +439,7 @@ LUALIB_API char *luaL_prepbuffsize (luaL_Buffer *B, size_t sz) {
if (B->size - B->n < sz) { /* not enough space? */ if (B->size - B->n < sz) { /* not enough space? */
char *newbuff; char *newbuff;
size_t newsize = B->size * 2; /* double buffer size */ size_t newsize = B->size * 2; /* double buffer size */
if (newsize - B->n < sz) /* not bit enough? */ if (newsize - B->n < sz) /* not big enough? */
newsize = B->n + sz; newsize = B->n + sz;
if (newsize < B->n || newsize - B->n < sz) if (newsize < B->n || newsize - B->n < sz)
luaL_error(L, "buffer too large"); luaL_error(L, "buffer too large");
@ -598,7 +599,7 @@ static int skipBOM (LoadF *lf) {
lf->n = 0; lf->n = 0;
do { do {
c = getc(lf->f); c = getc(lf->f);
if (c == EOF || c != *(unsigned char *)p++) return c; if (c == EOF || c != *(const unsigned char *)p++) return c;
lf->buff[lf->n++] = c; /* to be read by the parser */ lf->buff[lf->n++] = c; /* to be read by the parser */
} while (*p != '\0'); } while (*p != '\0');
lf->n = 0; /* prefix matched; discard it */ lf->n = 0; /* prefix matched; discard it */

@ -1,5 +1,5 @@
/* /*
** $Id: lbaselib.c,v 1.274 2012/04/27 14:13:19 roberto Exp $ ** $Id: lbaselib.c,v 1.276.1.1 2013/04/12 18:48:47 roberto Exp $
** Basic library ** Basic library
** See Copyright Notice in lua.h ** See Copyright Notice in lua.h
*/ */
@ -242,10 +242,16 @@ static int luaB_ipairs (lua_State *L) {
} }
static int load_aux (lua_State *L, int status) { static int load_aux (lua_State *L, int status, int envidx) {
if (status == LUA_OK) if (status == LUA_OK) {
if (envidx != 0) { /* 'env' parameter? */
lua_pushvalue(L, envidx); /* environment for loaded function */
if (!lua_setupvalue(L, -2, 1)) /* set it as 1st upvalue */
lua_pop(L, 1); /* remove 'env' if not used by previous call */
}
return 1; return 1;
else { }
else { /* error (message is on top of the stack) */
lua_pushnil(L); lua_pushnil(L);
lua_insert(L, -2); /* put before error message */ lua_insert(L, -2); /* put before error message */
return 2; /* return nil plus error message */ return 2; /* return nil plus error message */
@ -256,13 +262,9 @@ static int load_aux (lua_State *L, int status) {
static int luaB_loadfile (lua_State *L) { static int luaB_loadfile (lua_State *L) {
const char *fname = luaL_optstring(L, 1, NULL); const char *fname = luaL_optstring(L, 1, NULL);
const char *mode = luaL_optstring(L, 2, NULL); const char *mode = luaL_optstring(L, 2, NULL);
int env = !lua_isnone(L, 3); /* 'env' parameter? */ int env = (!lua_isnone(L, 3) ? 3 : 0); /* 'env' index or 0 if no 'env' */
int status = luaL_loadfilex(L, fname, mode); int status = luaL_loadfilex(L, fname, mode);
if (status == LUA_OK && env) { /* 'env' parameter? */ return load_aux(L, status, env);
lua_pushvalue(L, 3);
lua_setupvalue(L, -2, 1); /* set it as 1st upvalue of loaded chunk */
}
return load_aux(L, status);
} }
@ -307,9 +309,9 @@ static const char *generic_reader (lua_State *L, void *ud, size_t *size) {
static int luaB_load (lua_State *L) { static int luaB_load (lua_State *L) {
int status; int status;
size_t l; size_t l;
int top = lua_gettop(L);
const char *s = lua_tolstring(L, 1, &l); const char *s = lua_tolstring(L, 1, &l);
const char *mode = luaL_optstring(L, 3, "bt"); const char *mode = luaL_optstring(L, 3, "bt");
int env = (!lua_isnone(L, 4) ? 4 : 0); /* 'env' index or 0 if no 'env' */
if (s != NULL) { /* loading a string? */ if (s != NULL) { /* loading a string? */
const char *chunkname = luaL_optstring(L, 2, s); const char *chunkname = luaL_optstring(L, 2, s);
status = luaL_loadbufferx(L, s, l, chunkname, mode); status = luaL_loadbufferx(L, s, l, chunkname, mode);
@ -320,11 +322,7 @@ static int luaB_load (lua_State *L) {
lua_settop(L, RESERVEDSLOT); /* create reserved slot */ lua_settop(L, RESERVEDSLOT); /* create reserved slot */
status = lua_load(L, generic_reader, NULL, chunkname, mode); status = lua_load(L, generic_reader, NULL, chunkname, mode);
} }
if (status == LUA_OK && top >= 4) { /* is there an 'env' argument */ return load_aux(L, status, env);
lua_pushvalue(L, 4); /* environment for loaded function */
lua_setupvalue(L, -2, 1); /* set it as 1st upvalue */
}
return load_aux(L, status);
} }
/* }====================================================== */ /* }====================================================== */
@ -338,7 +336,8 @@ static int dofilecont (lua_State *L) {
static int luaB_dofile (lua_State *L) { static int luaB_dofile (lua_State *L) {
const char *fname = luaL_optstring(L, 1, NULL); const char *fname = luaL_optstring(L, 1, NULL);
lua_settop(L, 1); lua_settop(L, 1);
if (luaL_loadfile(L, fname) != LUA_OK) lua_error(L); if (luaL_loadfile(L, fname) != LUA_OK)
return lua_error(L);
lua_callk(L, 0, LUA_MULTRET, 0, dofilecont); lua_callk(L, 0, LUA_MULTRET, 0, dofilecont);
return dofilecont(L); return dofilecont(L);
} }

@ -1,5 +1,5 @@
/* /*
** $Id: lbitlib.c,v 1.16 2011/06/20 16:35:23 roberto Exp $ ** $Id: lbitlib.c,v 1.18.1.2 2013/07/09 18:01:41 roberto Exp $
** Standard library for bitwise operations ** Standard library for bitwise operations
** See Copyright Notice in lua.h ** See Copyright Notice in lua.h
*/ */
@ -129,7 +129,8 @@ static int b_rot (lua_State *L, int i) {
b_uint r = luaL_checkunsigned(L, 1); b_uint r = luaL_checkunsigned(L, 1);
i &= (LUA_NBITS - 1); /* i = i % NBITS */ i &= (LUA_NBITS - 1); /* i = i % NBITS */
r = trim(r); r = trim(r);
r = (r << i) | (r >> (LUA_NBITS - i)); if (i != 0) /* avoid undefined shift of LUA_NBITS when i == 0 */
r = (r << i) | (r >> (LUA_NBITS - i));
lua_pushunsigned(L, trim(r)); lua_pushunsigned(L, trim(r));
return 1; return 1;
} }
@ -147,7 +148,9 @@ static int b_rrot (lua_State *L) {
/* /*
** get field and width arguments for field-manipulation functions, ** get field and width arguments for field-manipulation functions,
** checking whether they are valid ** checking whether they are valid.
** ('luaL_error' called without 'return' to avoid later warnings about
** 'width' being used uninitialized.)
*/ */
static int fieldargs (lua_State *L, int farg, int *width) { static int fieldargs (lua_State *L, int farg, int *width) {
int f = luaL_checkint(L, farg); int f = luaL_checkint(L, farg);

@ -1,5 +1,5 @@
/* /*
** $Id: lcode.c,v 2.60 2011/08/30 16:26:41 roberto Exp $ ** $Id: lcode.c,v 2.62.1.1 2013/04/12 18:48:47 roberto Exp $
** Code generator for Lua ** Code generator for Lua
** See Copyright Notice in lua.h ** See Copyright Notice in lua.h
*/ */
@ -330,10 +330,9 @@ int luaK_numberK (FuncState *fs, lua_Number r) {
setnvalue(&o, r); setnvalue(&o, r);
if (r == 0 || luai_numisnan(NULL, r)) { /* handle -0 and NaN */ if (r == 0 || luai_numisnan(NULL, r)) { /* handle -0 and NaN */
/* use raw representation as key to avoid numeric problems */ /* use raw representation as key to avoid numeric problems */
setsvalue(L, L->top, luaS_newlstr(L, (char *)&r, sizeof(r))); setsvalue(L, L->top++, luaS_newlstr(L, (char *)&r, sizeof(r)));
incr_top(L); n = addk(fs, L->top - 1, &o);
n = addk(fs, L->top - 1, &o); L->top--;
L->top--;
} }
else else
n = addk(fs, &o, &o); /* regular case */ n = addk(fs, &o, &o); /* regular case */
@ -426,7 +425,7 @@ static void discharge2reg (FuncState *fs, expdesc *e, int reg) {
luaK_nil(fs, reg, 1); luaK_nil(fs, reg, 1);
break; break;
} }
case VFALSE: case VTRUE: { case VFALSE: case VTRUE: {
luaK_codeABC(fs, OP_LOADBOOL, reg, e->k == VTRUE, 0); luaK_codeABC(fs, OP_LOADBOOL, reg, e->k == VTRUE, 0);
break; break;
} }

@ -1,5 +1,5 @@
/* /*
** $Id: lcorolib.c,v 1.4 2012/04/27 18:59:04 roberto Exp $ ** $Id: lcorolib.c,v 1.5.1.1 2013/04/12 18:48:47 roberto Exp $
** Coroutine Library ** Coroutine Library
** See Copyright Notice in lua.h ** See Copyright Notice in lua.h
*/ */
@ -73,7 +73,7 @@ static int luaB_auxwrap (lua_State *L) {
lua_insert(L, -2); lua_insert(L, -2);
lua_concat(L, 2); lua_concat(L, 2);
} }
lua_error(L); /* propagate error */ return lua_error(L); /* propagate error */
} }
return r; return r;
} }

@ -1,5 +1,5 @@
/* /*
** $Id: lctype.c,v 1.11 2011/10/03 16:19:23 roberto Exp $ ** $Id: lctype.c,v 1.11.1.1 2013/04/12 18:48:47 roberto Exp $
** 'ctype' functions for Lua ** 'ctype' functions for Lua
** See Copyright Notice in lua.h ** See Copyright Notice in lua.h
*/ */

@ -1,5 +1,5 @@
/* /*
** $Id: ldblib.c,v 1.132 2012/01/19 20:14:44 roberto Exp $ ** $Id: ldblib.c,v 1.132.1.1 2013/04/12 18:48:47 roberto Exp $
** Interface from Lua to its debug API ** Interface from Lua to its debug API
** See Copyright Notice in lua.h ** See Copyright Notice in lua.h
*/ */

@ -1,5 +1,5 @@
/* /*
** $Id: ldebug.c,v 2.89 2012/01/20 22:05:50 roberto Exp $ ** $Id: ldebug.c,v 2.90.1.3 2013/05/16 16:04:15 roberto Exp $
** Debug Interface ** Debug Interface
** See Copyright Notice in lua.h ** See Copyright Notice in lua.h
*/ */
@ -196,7 +196,7 @@ static void funcinfo (lua_Debug *ar, Closure *cl) {
static void collectvalidlines (lua_State *L, Closure *f) { static void collectvalidlines (lua_State *L, Closure *f) {
if (noLuaClosure(f)) { if (noLuaClosure(f)) {
setnilvalue(L->top); setnilvalue(L->top);
incr_top(L); api_incr_top(L);
} }
else { else {
int i; int i;
@ -204,7 +204,7 @@ static void collectvalidlines (lua_State *L, Closure *f) {
int *lineinfo = f->l.p->lineinfo; int *lineinfo = f->l.p->lineinfo;
Table *t = luaH_new(L); /* new table to store active lines */ Table *t = luaH_new(L); /* new table to store active lines */
sethvalue(L, L->top, t); /* push it on stack */ sethvalue(L, L->top, t); /* push it on stack */
incr_top(L); api_incr_top(L);
setbvalue(&v, 1); /* boolean 'true' to be the value of all indices */ setbvalue(&v, 1); /* boolean 'true' to be the value of all indices */
for (i = 0; i < f->l.p->sizelineinfo; i++) /* for all lines with code */ for (i = 0; i < f->l.p->sizelineinfo; i++) /* for all lines with code */
luaH_setint(L, t, lineinfo[i], &v); /* table[line] = true */ luaH_setint(L, t, lineinfo[i], &v); /* table[line] = true */
@ -285,7 +285,7 @@ LUA_API int lua_getinfo (lua_State *L, const char *what, lua_Debug *ar) {
status = auxgetinfo(L, what, ar, cl, ci); status = auxgetinfo(L, what, ar, cl, ci);
if (strchr(what, 'f')) { if (strchr(what, 'f')) {
setobjs2s(L, L->top, func); setobjs2s(L, L->top, func);
incr_top(L); api_incr_top(L);
} }
if (strchr(what, 'L')) if (strchr(what, 'L'))
collectvalidlines(L, cl); collectvalidlines(L, cl);
@ -327,12 +327,20 @@ static void kname (Proto *p, int pc, int c, const char **name) {
} }
static int filterpc (int pc, int jmptarget) {
if (pc < jmptarget) /* is code conditional (inside a jump)? */
return -1; /* cannot know who sets that register */
else return pc; /* current position sets that register */
}
/* /*
** try to find last instruction before 'lastpc' that modified register 'reg' ** try to find last instruction before 'lastpc' that modified register 'reg'
*/ */
static int findsetreg (Proto *p, int lastpc, int reg) { static int findsetreg (Proto *p, int lastpc, int reg) {
int pc; int pc;
int setreg = -1; /* keep last instruction that changed 'reg' */ int setreg = -1; /* keep last instruction that changed 'reg' */
int jmptarget = 0; /* any code before this address is conditional */
for (pc = 0; pc < lastpc; pc++) { for (pc = 0; pc < lastpc; pc++) {
Instruction i = p->code[pc]; Instruction i = p->code[pc];
OpCode op = GET_OPCODE(i); OpCode op = GET_OPCODE(i);
@ -341,33 +349,38 @@ static int findsetreg (Proto *p, int lastpc, int reg) {
case OP_LOADNIL: { case OP_LOADNIL: {
int b = GETARG_B(i); int b = GETARG_B(i);
if (a <= reg && reg <= a + b) /* set registers from 'a' to 'a+b' */ if (a <= reg && reg <= a + b) /* set registers from 'a' to 'a+b' */
setreg = pc; setreg = filterpc(pc, jmptarget);
break; break;
} }
case OP_TFORCALL: { case OP_TFORCALL: {
if (reg >= a + 2) setreg = pc; /* affect all regs above its base */ if (reg >= a + 2) /* affect all regs above its base */
setreg = filterpc(pc, jmptarget);
break; break;
} }
case OP_CALL: case OP_CALL:
case OP_TAILCALL: { case OP_TAILCALL: {
if (reg >= a) setreg = pc; /* affect all registers above base */ if (reg >= a) /* affect all registers above base */
setreg = filterpc(pc, jmptarget);
break; break;
} }
case OP_JMP: { case OP_JMP: {
int b = GETARG_sBx(i); int b = GETARG_sBx(i);
int dest = pc + 1 + b; int dest = pc + 1 + b;
/* jump is forward and do not skip `lastpc'? */ /* jump is forward and do not skip `lastpc'? */
if (pc < dest && dest <= lastpc) if (pc < dest && dest <= lastpc) {
pc += b; /* do the jump */ if (dest > jmptarget)
jmptarget = dest; /* update 'jmptarget' */
}
break; break;
} }
case OP_TEST: { case OP_TEST: {
if (reg == a) setreg = pc; /* jumped code can change 'a' */ if (reg == a) /* jumped code can change 'a' */
setreg = filterpc(pc, jmptarget);
break; break;
} }
default: default:
if (testAMode(op) && reg == a) /* any instruction that set A */ if (testAMode(op) && reg == a) /* any instruction that set A */
setreg = pc; setreg = filterpc(pc, jmptarget);
break; break;
} }
} }
@ -518,7 +531,7 @@ l_noret luaG_typeerror (lua_State *L, const TValue *o, const char *op) {
l_noret luaG_concaterror (lua_State *L, StkId p1, StkId p2) { l_noret luaG_concaterror (lua_State *L, StkId p1, StkId p2) {
if (ttisstring(p1) || ttisnumber(p1)) p1 = p2; if (ttisstring(p1) || ttisnumber(p1)) p1 = p2;
lua_assert(!ttisstring(p1) && !ttisnumber(p2)); lua_assert(!ttisstring(p1) && !ttisnumber(p1));
luaG_typeerror(L, p1, "concatenate"); luaG_typeerror(L, p1, "concatenate");
} }
@ -563,7 +576,7 @@ l_noret luaG_errormsg (lua_State *L) {
if (!ttisfunction(errfunc)) luaD_throw(L, LUA_ERRERR); if (!ttisfunction(errfunc)) luaD_throw(L, LUA_ERRERR);
setobjs2s(L, L->top, L->top - 1); /* move argument */ setobjs2s(L, L->top, L->top - 1); /* move argument */
setobjs2s(L, L->top - 1, errfunc); /* push function */ setobjs2s(L, L->top - 1, errfunc); /* push function */
incr_top(L); L->top++;
luaD_call(L, L->top - 2, 1, 0); /* call it */ luaD_call(L, L->top - 2, 1, 0); /* call it */
} }
luaD_throw(L, LUA_ERRRUN); luaD_throw(L, LUA_ERRRUN);

@ -1,5 +1,5 @@
/* /*
** $Id: ldo.c,v 2.105 2012/06/08 15:14:04 roberto Exp $ ** $Id: ldo.c,v 2.108.1.3 2013/11/08 18:22:50 roberto Exp $
** Stack and Call structure of Lua ** Stack and Call structure of Lua
** See Copyright Notice in lua.h ** See Copyright Notice in lua.h
*/ */
@ -260,6 +260,7 @@ static StkId adjust_varargs (lua_State *L, Proto *p, int actual) {
StkId base, fixed; StkId base, fixed;
lua_assert(actual >= nfixargs); lua_assert(actual >= nfixargs);
/* move fixed parameters to final position */ /* move fixed parameters to final position */
luaD_checkstack(L, p->maxstacksize); /* check again for new 'base' */
fixed = L->top - actual; /* first fixed argument */ fixed = L->top - actual; /* first fixed argument */
base = L->top; /* final position of first argument */ base = L->top; /* final position of first argument */
for (i=0; i<nfixargs; i++) { for (i=0; i<nfixargs; i++) {
@ -311,6 +312,7 @@ int luaD_precall (lua_State *L, StkId func, int nresults) {
ci->top = L->top + LUA_MINSTACK; ci->top = L->top + LUA_MINSTACK;
lua_assert(ci->top <= L->stack_last); lua_assert(ci->top <= L->stack_last);
ci->callstatus = 0; ci->callstatus = 0;
luaC_checkGC(L); /* stack grow uses memory */
if (L->hookmask & LUA_MASKCALL) if (L->hookmask & LUA_MASKCALL)
luaD_hook(L, LUA_HOOKCALL, -1); luaD_hook(L, LUA_HOOKCALL, -1);
lua_unlock(L); lua_unlock(L);
@ -323,12 +325,18 @@ int luaD_precall (lua_State *L, StkId func, int nresults) {
case LUA_TLCL: { /* Lua function: prepare its call */ case LUA_TLCL: { /* Lua function: prepare its call */
StkId base; StkId base;
Proto *p = clLvalue(func)->p; Proto *p = clLvalue(func)->p;
luaD_checkstack(L, p->maxstacksize);
func = restorestack(L, funcr);
n = cast_int(L->top - func) - 1; /* number of real arguments */ n = cast_int(L->top - func) - 1; /* number of real arguments */
luaD_checkstack(L, p->maxstacksize);
for (; n < p->numparams; n++) for (; n < p->numparams; n++)
setnilvalue(L->top++); /* complete missing arguments */ setnilvalue(L->top++); /* complete missing arguments */
base = (!p->is_vararg) ? func + 1 : adjust_varargs(L, p, n); if (!p->is_vararg) {
func = restorestack(L, funcr);
base = func + 1;
}
else {
base = adjust_varargs(L, p, n);
func = restorestack(L, funcr); /* previous call can change stack */
}
ci = next_ci(L); /* now 'enter' new function */ ci = next_ci(L); /* now 'enter' new function */
ci->nresults = nresults; ci->nresults = nresults;
ci->func = func; ci->func = func;
@ -338,6 +346,7 @@ int luaD_precall (lua_State *L, StkId func, int nresults) {
ci->u.l.savedpc = p->code; /* starting point */ ci->u.l.savedpc = p->code; /* starting point */
ci->callstatus = CIST_LUA; ci->callstatus = CIST_LUA;
L->top = ci->top; L->top = ci->top;
luaC_checkGC(L); /* stack grow uses memory */
if (L->hookmask & LUA_MASKCALL) if (L->hookmask & LUA_MASKCALL)
callhook(L, ci); callhook(L, ci);
return 0; return 0;
@ -393,7 +402,6 @@ void luaD_call (lua_State *L, StkId func, int nResults, int allowyield) {
luaV_execute(L); /* call it */ luaV_execute(L); /* call it */
if (!allowyield) L->nny--; if (!allowyield) L->nny--;
L->nCcalls--; L->nCcalls--;
luaC_checkGC(L);
} }
@ -402,10 +410,11 @@ static void finishCcall (lua_State *L) {
int n; int n;
lua_assert(ci->u.c.k != NULL); /* must have a continuation */ lua_assert(ci->u.c.k != NULL); /* must have a continuation */
lua_assert(L->nny == 0); lua_assert(L->nny == 0);
/* finish 'lua_pcallk' */ if (ci->callstatus & CIST_YPCALL) { /* was inside a pcall? */
if (ci->callstatus & CIST_YPCALL) ci->callstatus &= ~CIST_YPCALL; /* finish 'lua_pcall' */
L->errfunc = ci->u.c.old_errfunc; L->errfunc = ci->u.c.old_errfunc;
/* finish 'lua_callk' */ }
/* finish 'lua_callk'/'lua_pcall' */
adjustresults(L, ci->nresults); adjustresults(L, ci->nresults);
/* call continuation function */ /* call continuation function */
if (!(ci->callstatus & CIST_STAT)) /* no call status? */ if (!(ci->callstatus & CIST_STAT)) /* no call status? */
@ -476,7 +485,7 @@ static int recover (lua_State *L, int status) {
static l_noret resume_error (lua_State *L, const char *msg, StkId firstArg) { static l_noret resume_error (lua_State *L, const char *msg, StkId firstArg) {
L->top = firstArg; /* remove args from the stack */ L->top = firstArg; /* remove args from the stack */
setsvalue2s(L, L->top, luaS_new(L, msg)); /* push error message */ setsvalue2s(L, L->top, luaS_new(L, msg)); /* push error message */
incr_top(L); api_incr_top(L);
luaD_throw(L, -1); /* jump back to 'lua_resume' */ luaD_throw(L, -1); /* jump back to 'lua_resume' */
} }
@ -525,6 +534,7 @@ static void resume (lua_State *L, void *ud) {
LUA_API int lua_resume (lua_State *L, lua_State *from, int nargs) { LUA_API int lua_resume (lua_State *L, lua_State *from, int nargs) {
int status; int status;
int oldnny = L->nny; /* save 'nny' */
lua_lock(L); lua_lock(L);
luai_userstateresume(L, nargs); luai_userstateresume(L, nargs);
L->nCcalls = (from) ? from->nCcalls + 1 : 1; L->nCcalls = (from) ? from->nCcalls + 1 : 1;
@ -546,7 +556,7 @@ LUA_API int lua_resume (lua_State *L, lua_State *from, int nargs) {
} }
lua_assert(status == L->status); lua_assert(status == L->status);
} }
L->nny = 1; /* do not allow yields */ L->nny = oldnny; /* restore 'nny' */
L->nCcalls--; L->nCcalls--;
lua_assert(L->nCcalls == ((from) ? from->nCcalls : 0)); lua_assert(L->nCcalls == ((from) ? from->nCcalls : 0));
lua_unlock(L); lua_unlock(L);
@ -561,7 +571,7 @@ LUA_API int lua_yieldk (lua_State *L, int nresults, int ctx, lua_CFunction k) {
api_checknelems(L, nresults); api_checknelems(L, nresults);
if (L->nny > 0) { if (L->nny > 0) {
if (L != G(L)->mainthread) if (L != G(L)->mainthread)
luaG_runerror(L, "attempt to yield across metamethod/C-call boundary"); luaG_runerror(L, "attempt to yield across a C-call boundary");
else else
luaG_runerror(L, "attempt to yield from outside a coroutine"); luaG_runerror(L, "attempt to yield from outside a coroutine");
} }

@ -1,5 +1,5 @@
/* /*
** $Id: ldump.c,v 2.17 2012/01/23 23:02:10 roberto Exp $ ** $Id: ldump.c,v 2.17.1.1 2013/04/12 18:48:47 roberto Exp $
** save precompiled Lua chunks ** save precompiled Lua chunks
** See Copyright Notice in lua.h ** See Copyright Notice in lua.h
*/ */

@ -1,5 +1,5 @@
/* /*
** $Id: lfunc.c,v 2.29 2012/05/08 13:53:33 roberto Exp $ ** $Id: lfunc.c,v 2.30.1.1 2013/04/12 18:48:47 roberto Exp $
** Auxiliary functions to manipulate prototypes and closures ** Auxiliary functions to manipulate prototypes and closures
** See Copyright Notice in lua.h ** See Copyright Notice in lua.h
*/ */
@ -52,12 +52,12 @@ UpVal *luaF_findupval (lua_State *L, StkId level) {
while (*pp != NULL && (p = gco2uv(*pp))->v >= level) { while (*pp != NULL && (p = gco2uv(*pp))->v >= level) {
GCObject *o = obj2gco(p); GCObject *o = obj2gco(p);
lua_assert(p->v != &p->u.value); lua_assert(p->v != &p->u.value);
lua_assert(!isold(o) || isold(obj2gco(L)));
if (p->v == level) { /* found a corresponding upvalue? */ if (p->v == level) { /* found a corresponding upvalue? */
if (isdead(g, o)) /* is it dead? */ if (isdead(g, o)) /* is it dead? */
changewhite(o); /* resurrect it */ changewhite(o); /* resurrect it */
return p; return p;
} }
resetoldbit(o); /* may create a newer upval after this one */
pp = &p->next; pp = &p->next;
} }
/* not found: create a new one */ /* not found: create a new one */

@ -1,5 +1,5 @@
/* /*
** $Id: lgc.c,v 2.133 2012/05/31 21:28:59 roberto Exp $ ** $Id: lgc.c,v 2.140.1.2 2013/04/26 18:22:05 roberto Exp $
** Garbage Collector ** Garbage Collector
** See Copyright Notice in lua.h ** See Copyright Notice in lua.h
*/ */
@ -43,21 +43,12 @@
*/ */
#define STEPMULADJ 200 #define STEPMULADJ 200
/* /*
** macro to adjust 'pause': 'pause' is actually used like ** macro to adjust 'pause': 'pause' is actually used like
** 'pause / PAUSEADJ' (value chosen by tests) ** 'pause / PAUSEADJ' (value chosen by tests)
*/ */
#define PAUSEADJ 200 #define PAUSEADJ 100
/*
** standard negative debt for GC; a reasonable "time" to wait before
** starting a new cycle
*/
#define stddebtest(g,e) (-cast(l_mem, (e)/PAUSEADJ) * g->gcpause)
#define stddebt(g) stddebtest(g, gettotalbytes(g))
/* /*
@ -144,9 +135,9 @@ static int iscleared (global_State *g, const TValue *o) {
void luaC_barrier_ (lua_State *L, GCObject *o, GCObject *v) { void luaC_barrier_ (lua_State *L, GCObject *o, GCObject *v) {
global_State *g = G(L); global_State *g = G(L);
lua_assert(isblack(o) && iswhite(v) && !isdead(g, v) && !isdead(g, o)); lua_assert(isblack(o) && iswhite(v) && !isdead(g, v) && !isdead(g, o));
lua_assert(isgenerational(g) || g->gcstate != GCSpause); lua_assert(g->gcstate != GCSpause);
lua_assert(gch(o)->tt != LUA_TTABLE); lua_assert(gch(o)->tt != LUA_TTABLE);
if (keepinvariant(g)) /* must keep invariant? */ if (keepinvariantout(g)) /* must keep invariant? */
reallymarkobject(g, v); /* restore invariant */ reallymarkobject(g, v); /* restore invariant */
else { /* sweep phase */ else { /* sweep phase */
lua_assert(issweepphase(g)); lua_assert(issweepphase(g));
@ -343,7 +334,7 @@ static void remarkupvals (global_State *g) {
** mark root set and reset all gray lists, to start a new ** mark root set and reset all gray lists, to start a new
** incremental (or full) collection ** incremental (or full) collection
*/ */
static void markroot (global_State *g) { static void restartcollection (global_State *g) {
g->gray = g->grayagain = NULL; g->gray = g->grayagain = NULL;
g->weak = g->allweak = g->ephemeron = NULL; g->weak = g->allweak = g->ephemeron = NULL;
markobject(g, g->mainthread); markobject(g, g->mainthread);
@ -459,7 +450,7 @@ static lu_mem traversetable (global_State *g, Table *h) {
else /* not weak */ else /* not weak */
traversestrongtable(g, h); traversestrongtable(g, h);
return sizeof(Table) + sizeof(TValue) * h->sizearray + return sizeof(Table) + sizeof(TValue) * h->sizearray +
sizeof(Node) * sizenode(h); sizeof(Node) * cast(size_t, sizenode(h));
} }
@ -502,17 +493,24 @@ static lu_mem traverseLclosure (global_State *g, LClosure *cl) {
static lu_mem traversestack (global_State *g, lua_State *th) { static lu_mem traversestack (global_State *g, lua_State *th) {
int n = 0;
StkId o = th->stack; StkId o = th->stack;
if (o == NULL) if (o == NULL)
return 1; /* stack not completely built yet */ return 1; /* stack not completely built yet */
for (; o < th->top; o++) for (; o < th->top; o++) /* mark live elements in the stack */
markvalue(g, o); markvalue(g, o);
if (g->gcstate == GCSatomic) { /* final traversal? */ if (g->gcstate == GCSatomic) { /* final traversal? */
StkId lim = th->stack + th->stacksize; /* real end of stack */ StkId lim = th->stack + th->stacksize; /* real end of stack */
for (; o < lim; o++) /* clear not-marked stack slice */ for (; o < lim; o++) /* clear not-marked stack slice */
setnilvalue(o); setnilvalue(o);
} }
return sizeof(lua_State) + sizeof(TValue) * th->stacksize; else { /* count call infos to compute size */
CallInfo *ci;
for (ci = &th->base_ci; ci != th->ci; ci = ci->next)
n++;
}
return sizeof(lua_State) + sizeof(TValue) * th->stacksize +
sizeof(CallInfo) * n;
} }
@ -796,7 +794,7 @@ static GCObject *udata2finalize (global_State *g) {
g->allgc = o; g->allgc = o;
resetbit(gch(o)->marked, SEPARATED); /* mark that it is not in 'tobefnz' */ resetbit(gch(o)->marked, SEPARATED); /* mark that it is not in 'tobefnz' */
lua_assert(!isold(o)); /* see MOVE OLD rule */ lua_assert(!isold(o)); /* see MOVE OLD rule */
if (!keepinvariant(g)) /* not keeping invariant? */ if (!keepinvariantout(g)) /* not keeping invariant? */
makewhite(g, o); /* "sweep" object */ makewhite(g, o); /* "sweep" object */
return o; return o;
} }
@ -855,7 +853,7 @@ static void separatetobefnz (lua_State *L, int all) {
while ((curr = *p) != NULL) { /* traverse all finalizable objects */ while ((curr = *p) != NULL) { /* traverse all finalizable objects */
lua_assert(!isfinalized(curr)); lua_assert(!isfinalized(curr));
lua_assert(testbit(gch(curr)->marked, SEPARATED)); lua_assert(testbit(gch(curr)->marked, SEPARATED));
if (!(all || iswhite(curr))) /* not being collected? */ if (!(iswhite(curr) || all)) /* not being collected? */
p = &gch(curr)->next; /* don't bother with it */ p = &gch(curr)->next; /* don't bother with it */
else { else {
l_setbit(gch(curr)->marked, FINALIZEDBIT); /* won't be finalized again */ l_setbit(gch(curr)->marked, FINALIZEDBIT); /* won't be finalized again */
@ -891,7 +889,7 @@ void luaC_checkfinalizer (lua_State *L, GCObject *o, Table *mt) {
ho->next = g->finobj; /* link it in list 'finobj' */ ho->next = g->finobj; /* link it in list 'finobj' */
g->finobj = o; g->finobj = o;
l_setbit(ho->marked, SEPARATED); /* mark it as such */ l_setbit(ho->marked, SEPARATED); /* mark it as such */
if (!keepinvariant(g)) /* not keeping invariant? */ if (!keepinvariantout(g)) /* not keeping invariant? */
makewhite(g, o); /* "sweep" object */ makewhite(g, o); /* "sweep" object */
else else
resetoldbit(o); /* see MOVE OLD rule */ resetoldbit(o); /* see MOVE OLD rule */
@ -908,6 +906,21 @@ void luaC_checkfinalizer (lua_State *L, GCObject *o, Table *mt) {
*/ */
/*
** set a reasonable "time" to wait before starting a new GC cycle;
** cycle will start when memory use hits threshold
*/
static void setpause (global_State *g, l_mem estimate) {
l_mem debt, threshold;
estimate = estimate / PAUSEADJ; /* adjust 'estimate' */
threshold = (g->gcpause < MAX_LMEM / estimate) /* overflow? */
? estimate * g->gcpause /* no overflow */
: MAX_LMEM; /* overflow; truncate to maximum */
debt = -cast(l_mem, threshold - gettotalbytes(g));
luaE_setdebt(g, debt);
}
#define sweepphases \ #define sweepphases \
(bitmask(GCSsweepstring) | bitmask(GCSsweepudata) | bitmask(GCSsweep)) (bitmask(GCSsweepstring) | bitmask(GCSsweepudata) | bitmask(GCSsweep))
@ -918,7 +931,7 @@ void luaC_checkfinalizer (lua_State *L, GCObject *o, Table *mt) {
** object inside the list (instead of to the header), so that the real ** object inside the list (instead of to the header), so that the real
** sweep do not need to skip objects created between "now" and the start ** sweep do not need to skip objects created between "now" and the start
** of the real sweep. ** of the real sweep.
** Returns how many objects it sweeped. ** Returns how many objects it swept.
*/ */
static int entersweep (lua_State *L) { static int entersweep (lua_State *L) {
global_State *g = G(L); global_State *g = G(L);
@ -985,7 +998,7 @@ void luaC_freeallobjects (lua_State *L) {
static l_mem atomic (lua_State *L) { static l_mem atomic (lua_State *L) {
global_State *g = G(L); global_State *g = G(L);
l_mem work = -g->GCmemtrav; /* start counting work */ l_mem work = -cast(l_mem, g->GCmemtrav); /* start counting work */
GCObject *origweak, *origall; GCObject *origweak, *origall;
lua_assert(!iswhite(obj2gco(g->mainthread))); lua_assert(!iswhite(obj2gco(g->mainthread)));
markobject(g, L); /* mark running thread */ markobject(g, L); /* mark running thread */
@ -1028,12 +1041,10 @@ static lu_mem singlestep (lua_State *L) {
global_State *g = G(L); global_State *g = G(L);
switch (g->gcstate) { switch (g->gcstate) {
case GCSpause: { case GCSpause: {
g->GCmemtrav = 0; /* start to count memory traversed */ /* start to count memory traversed */
if (!isgenerational(g)) g->GCmemtrav = g->strt.size * sizeof(GCObject*);
markroot(g); /* start a new collection */ lua_assert(!isgenerational(g));
/* in any case, root must be marked at this point */ restartcollection(g);
lua_assert(!iswhite(obj2gco(g->mainthread))
&& !iswhite(gcvalue(&g->l_registry)));
g->gcstate = GCSpropagate; g->gcstate = GCSpropagate;
return g->GCmemtrav; return g->GCmemtrav;
} }
@ -1105,18 +1116,23 @@ void luaC_runtilstate (lua_State *L, int statesmask) {
static void generationalcollection (lua_State *L) { static void generationalcollection (lua_State *L) {
global_State *g = G(L); global_State *g = G(L);
lua_assert(g->gcstate == GCSpropagate);
if (g->GCestimate == 0) { /* signal for another major collection? */ if (g->GCestimate == 0) { /* signal for another major collection? */
luaC_fullgc(L, 0); /* perform a full regular collection */ luaC_fullgc(L, 0); /* perform a full regular collection */
g->GCestimate = gettotalbytes(g); /* update control */ g->GCestimate = gettotalbytes(g); /* update control */
} }
else { else {
lu_mem estimate = g->GCestimate; lu_mem estimate = g->GCestimate;
luaC_runtilstate(L, ~bitmask(GCSpause)); /* run complete cycle */ luaC_runtilstate(L, bitmask(GCSpause)); /* run complete (minor) cycle */
luaC_runtilstate(L, bitmask(GCSpause)); g->gcstate = GCSpropagate; /* skip restart */
if (gettotalbytes(g) > (estimate / 100) * g->gcmajorinc) if (gettotalbytes(g) > (estimate / 100) * g->gcmajorinc)
g->GCestimate = 0; /* signal for a major collection */ g->GCestimate = 0; /* signal for a major collection */
else
g->GCestimate = estimate; /* keep estimate from last major coll. */
} }
luaE_setdebt(g, stddebt(g)); setpause(g, gettotalbytes(g));
lua_assert(g->gcstate == GCSpropagate);
} }
@ -1124,7 +1140,7 @@ static void incstep (lua_State *L) {
global_State *g = G(L); global_State *g = G(L);
l_mem debt = g->GCdebt; l_mem debt = g->GCdebt;
int stepmul = g->gcstepmul; int stepmul = g->gcstepmul;
if (stepmul < 40) stepmul = 40; /* avoid ridiculous low values */ if (stepmul < 40) stepmul = 40; /* avoid ridiculous low values (and 0) */
/* convert debt from Kb to 'work units' (avoid zero debt and overflows) */ /* convert debt from Kb to 'work units' (avoid zero debt and overflows) */
debt = (debt / STEPMULADJ) + 1; debt = (debt / STEPMULADJ) + 1;
debt = (debt < MAX_LMEM / stepmul) ? debt * stepmul : MAX_LMEM; debt = (debt < MAX_LMEM / stepmul) ? debt * stepmul : MAX_LMEM;
@ -1133,10 +1149,11 @@ static void incstep (lua_State *L) {
debt -= work; debt -= work;
} while (debt > -GCSTEPSIZE && g->gcstate != GCSpause); } while (debt > -GCSTEPSIZE && g->gcstate != GCSpause);
if (g->gcstate == GCSpause) if (g->gcstate == GCSpause)
debt = stddebtest(g, g->GCestimate); /* pause until next cycle */ setpause(g, g->GCestimate); /* pause until next cycle */
else else {
debt = (debt / stepmul) * STEPMULADJ; /* convert 'work units' to Kb */ debt = (debt / stepmul) * STEPMULADJ; /* convert 'work units' to Kb */
luaE_setdebt(g, debt); luaE_setdebt(g, debt);
}
} }
@ -1172,7 +1189,6 @@ void luaC_step (lua_State *L) {
void luaC_fullgc (lua_State *L, int isemergency) { void luaC_fullgc (lua_State *L, int isemergency) {
global_State *g = G(L); global_State *g = G(L);
int origkind = g->gckind; int origkind = g->gckind;
int someblack = keepinvariant(g);
lua_assert(origkind != KGC_EMERGENCY); lua_assert(origkind != KGC_EMERGENCY);
if (isemergency) /* do not run finalizers during emergency GC */ if (isemergency) /* do not run finalizers during emergency GC */
g->gckind = KGC_EMERGENCY; g->gckind = KGC_EMERGENCY;
@ -1180,22 +1196,21 @@ void luaC_fullgc (lua_State *L, int isemergency) {
g->gckind = KGC_NORMAL; g->gckind = KGC_NORMAL;
callallpendingfinalizers(L, 1); callallpendingfinalizers(L, 1);
} }
if (someblack) { /* may there be some black objects? */ if (keepinvariant(g)) { /* may there be some black objects? */
/* must sweep all objects to turn them back to white /* must sweep all objects to turn them back to white
(as white has not changed, nothing will be collected) */ (as white has not changed, nothing will be collected) */
entersweep(L); entersweep(L);
} }
/* finish any pending sweep phase to start a new cycle */ /* finish any pending sweep phase to start a new cycle */
luaC_runtilstate(L, bitmask(GCSpause)); luaC_runtilstate(L, bitmask(GCSpause));
/* run entire collector */ luaC_runtilstate(L, ~bitmask(GCSpause)); /* start new collection */
luaC_runtilstate(L, ~bitmask(GCSpause)); luaC_runtilstate(L, bitmask(GCSpause)); /* run entire collection */
luaC_runtilstate(L, bitmask(GCSpause));
if (origkind == KGC_GEN) { /* generational mode? */ if (origkind == KGC_GEN) { /* generational mode? */
/* generational mode must always start in propagate phase */ /* generational mode must be kept in propagate phase */
luaC_runtilstate(L, bitmask(GCSpropagate)); luaC_runtilstate(L, bitmask(GCSpropagate));
} }
g->gckind = origkind; g->gckind = origkind;
luaE_setdebt(g, stddebt(g)); setpause(g, gettotalbytes(g));
if (!isemergency) /* do not run finalizers during emergency GC */ if (!isemergency) /* do not run finalizers during emergency GC */
callallpendingfinalizers(L, 1); callallpendingfinalizers(L, 1);
} }

@ -1,5 +1,5 @@
/* /*
** $Id: linit.c,v 1.32 2011/04/08 19:17:36 roberto Exp $ ** $Id: linit.c,v 1.32.1.1 2013/04/12 18:48:47 roberto Exp $
** Initialization of libraries for lua.c and other clients ** Initialization of libraries for lua.c and other clients
** See Copyright Notice in lua.h ** See Copyright Notice in lua.h
*/ */

@ -1,17 +1,17 @@
/* /*
** $Id: liolib.c,v 2.108 2011/11/25 12:50:03 roberto Exp $ ** $Id: liolib.c,v 2.112.1.1 2013/04/12 18:48:47 roberto Exp $
** Standard I/O (and system) library ** Standard I/O (and system) library
** See Copyright Notice in lua.h ** See Copyright Notice in lua.h
*/ */
/* /*
** POSIX idiosyncrasy!
** This definition must come before the inclusion of 'stdio.h'; it ** This definition must come before the inclusion of 'stdio.h'; it
** should not affect non-POSIX systems ** should not affect non-POSIX systems
*/ */
#if !defined(_FILE_OFFSET_BITS) #if !defined(_FILE_OFFSET_BITS)
#define _FILE_OFFSET_BITS 64 #define _LARGEFILE_SOURCE 1
#define _FILE_OFFSET_BITS 64
#endif #endif
@ -29,6 +29,20 @@
#include "lualib.h" #include "lualib.h"
#if !defined(lua_checkmode)
/*
** Check whether 'mode' matches '[rwa]%+?b?'.
** Change this macro to accept other modes for 'fopen' besides
** the standard ones.
*/
#define lua_checkmode(mode) \
(*mode != '\0' && strchr("rwa", *(mode++)) != NULL && \
(*mode != '+' || ++mode) && /* skip if char is '+' */ \
(*mode != 'b' || ++mode) && /* skip if char is 'b' */ \
(*mode == '\0'))
#endif
/* /*
** {====================================================== ** {======================================================
@ -66,36 +80,37 @@
/* /*
** {====================================================== ** {======================================================
** lua_fseek/lua_ftell: configuration for longer offsets ** lua_fseek: configuration for longer offsets
** ======================================================= ** =======================================================
*/ */
#if !defined(lua_fseek) /* { */ #if !defined(lua_fseek) && !defined(LUA_ANSI) /* { */
#if defined(LUA_USE_POSIX) #if defined(LUA_USE_POSIX) /* { */
#define l_fseek(f,o,w) fseeko(f,o,w) #define l_fseek(f,o,w) fseeko(f,o,w)
#define l_ftell(f) ftello(f) #define l_ftell(f) ftello(f)
#define l_seeknum off_t #define l_seeknum off_t
#elif defined(LUA_WIN) && !defined(_CRTIMP_TYPEINFO) \ #elif defined(LUA_WIN) && !defined(_CRTIMP_TYPEINFO) \
&& defined(_MSC_VER) && (_MSC_VER >= 1400) && defined(_MSC_VER) && (_MSC_VER >= 1400) /* }{ */
/* Windows (but not DDK) and Visual C++ 2005 or higher */ /* Windows (but not DDK) and Visual C++ 2005 or higher */
#define l_fseek(f,o,w) _fseeki64(f,o,w) #define l_fseek(f,o,w) _fseeki64(f,o,w)
#define l_ftell(f) _ftelli64(f) #define l_ftell(f) _ftelli64(f)
#define l_seeknum __int64 #define l_seeknum __int64
#else #endif /* } */
#endif /* } */
#if !defined(l_fseek) /* default definitions */
#define l_fseek(f,o,w) fseek(f,o,w) #define l_fseek(f,o,w) fseek(f,o,w)
#define l_ftell(f) ftell(f) #define l_ftell(f) ftell(f)
#define l_seeknum long #define l_seeknum long
#endif #endif
#endif /* } */
/* }====================================================== */ /* }====================================================== */
@ -212,14 +227,8 @@ static int io_open (lua_State *L) {
const char *filename = luaL_checkstring(L, 1); const char *filename = luaL_checkstring(L, 1);
const char *mode = luaL_optstring(L, 2, "r"); const char *mode = luaL_optstring(L, 2, "r");
LStream *p = newfile(L); LStream *p = newfile(L);
int i = 0; const char *md = mode; /* to traverse/check mode */
/* check whether 'mode' matches '[rwa]%+?b?' */ luaL_argcheck(L, lua_checkmode(md), 2, "invalid mode");
if (!(mode[i] != '\0' && strchr("rwa", mode[i++]) != NULL &&
(mode[i] != '+' || ++i) && /* skip if char is '+' */
(mode[i] != 'b' || ++i) && /* skip if char is 'b' */
(mode[i] == '\0')))
return luaL_error(L, "invalid mode " LUA_QS
" (should match " LUA_QL("[rwa]%%+?b?") ")", mode);
p->f = fopen(filename, mode); p->f = fopen(filename, mode);
return (p->f == NULL) ? luaL_fileresult(L, 0, filename) : 1; return (p->f == NULL) ? luaL_fileresult(L, 0, filename) : 1;
} }

@ -1,5 +1,5 @@
/* /*
** $Id: llex.c,v 2.61 2012/01/23 23:05:51 roberto Exp $ ** $Id: llex.c,v 2.63.1.2 2013/08/30 15:49:41 roberto Exp $
** Lexical Analyzer ** Lexical Analyzer
** See Copyright Notice in lua.h ** See Copyright Notice in lua.h
*/ */
@ -73,16 +73,16 @@ void luaX_init (lua_State *L) {
const char *luaX_token2str (LexState *ls, int token) { const char *luaX_token2str (LexState *ls, int token) {
if (token < FIRST_RESERVED) { if (token < FIRST_RESERVED) { /* single-byte symbols? */
lua_assert(token == cast(unsigned char, token)); lua_assert(token == cast(unsigned char, token));
return (lisprint(token)) ? luaO_pushfstring(ls->L, LUA_QL("%c"), token) : return (lisprint(token)) ? luaO_pushfstring(ls->L, LUA_QL("%c"), token) :
luaO_pushfstring(ls->L, "char(%d)", token); luaO_pushfstring(ls->L, "char(%d)", token);
} }
else { else {
const char *s = luaX_tokens[token - FIRST_RESERVED]; const char *s = luaX_tokens[token - FIRST_RESERVED];
if (token < TK_EOS) if (token < TK_EOS) /* fixed format (symbols and reserved words)? */
return luaO_pushfstring(ls->L, LUA_QS, s); return luaO_pushfstring(ls->L, LUA_QS, s);
else else /* names, strings, and numerals */
return s; return s;
} }
} }
@ -133,6 +133,9 @@ TString *luaX_newstring (LexState *ls, const char *str, size_t l) {
setbvalue(o, 1); /* t[string] = true */ setbvalue(o, 1); /* t[string] = true */
luaC_checkGC(L); luaC_checkGC(L);
} }
else { /* string already present */
ts = rawtsvalue(keyfromval(o)); /* re-use value previously stored */
}
L->top--; /* remove string from stack */ L->top--; /* remove string from stack */
return ts; return ts;
} }
@ -313,7 +316,7 @@ static int readhexaesc (LexState *ls) {
int c[3], i; /* keep input for error message */ int c[3], i; /* keep input for error message */
int r = 0; /* result accumulator */ int r = 0; /* result accumulator */
c[0] = 'x'; /* for error message */ c[0] = 'x'; /* for error message */
for (i = 1; i < 3; i++) { /* read two hexa digits */ for (i = 1; i < 3; i++) { /* read two hexadecimal digits */
c[i] = next(ls); c[i] = next(ls);
if (!lisxdigit(c[i])) if (!lisxdigit(c[i]))
escerror(ls, c, i + 1, "hexadecimal digit expected"); escerror(ls, c, i + 1, "hexadecimal digit expected");

@ -1,5 +1,5 @@
/* /*
** $Id: lmathlib.c,v 1.81 2012/05/18 17:47:53 roberto Exp $ ** $Id: lmathlib.c,v 1.83.1.1 2013/04/12 18:48:47 roberto Exp $
** Standard mathematical library ** Standard mathematical library
** See Copyright Notice in lua.h ** See Copyright Notice in lua.h
*/ */
@ -17,106 +17,101 @@
#include "lualib.h" #include "lualib.h"
/* macro 'l_tg' allows the addition of an 'l' or 'f' to all math operations */
#if !defined(l_tg)
#define l_tg(x) (x)
#endif
#undef PI #undef PI
#define PI (l_tg(3.1415926535897932384626433832795)) #define PI ((lua_Number)(3.1415926535897932384626433832795))
#define RADIANS_PER_DEGREE (PI/180.0) #define RADIANS_PER_DEGREE ((lua_Number)(PI/180.0))
static int math_abs (lua_State *L) { static int math_abs (lua_State *L) {
lua_pushnumber(L, l_tg(fabs)(luaL_checknumber(L, 1))); lua_pushnumber(L, l_mathop(fabs)(luaL_checknumber(L, 1)));
return 1; return 1;
} }
static int math_sin (lua_State *L) { static int math_sin (lua_State *L) {
lua_pushnumber(L, l_tg(sin)(luaL_checknumber(L, 1))); lua_pushnumber(L, l_mathop(sin)(luaL_checknumber(L, 1)));
return 1; return 1;
} }
static int math_sinh (lua_State *L) { static int math_sinh (lua_State *L) {
lua_pushnumber(L, l_tg(sinh)(luaL_checknumber(L, 1))); lua_pushnumber(L, l_mathop(sinh)(luaL_checknumber(L, 1)));
return 1; return 1;
} }
static int math_cos (lua_State *L) { static int math_cos (lua_State *L) {
lua_pushnumber(L, l_tg(cos)(luaL_checknumber(L, 1))); lua_pushnumber(L, l_mathop(cos)(luaL_checknumber(L, 1)));
return 1; return 1;
} }
static int math_cosh (lua_State *L) { static int math_cosh (lua_State *L) {
lua_pushnumber(L, l_tg(cosh)(luaL_checknumber(L, 1))); lua_pushnumber(L, l_mathop(cosh)(luaL_checknumber(L, 1)));
return 1; return 1;
} }
static int math_tan (lua_State *L) { static int math_tan (lua_State *L) {
lua_pushnumber(L, l_tg(tan)(luaL_checknumber(L, 1))); lua_pushnumber(L, l_mathop(tan)(luaL_checknumber(L, 1)));
return 1; return 1;
} }
static int math_tanh (lua_State *L) { static int math_tanh (lua_State *L) {
lua_pushnumber(L, l_tg(tanh)(luaL_checknumber(L, 1))); lua_pushnumber(L, l_mathop(tanh)(luaL_checknumber(L, 1)));
return 1; return 1;
} }
static int math_asin (lua_State *L) { static int math_asin (lua_State *L) {
lua_pushnumber(L, l_tg(asin)(luaL_checknumber(L, 1))); lua_pushnumber(L, l_mathop(asin)(luaL_checknumber(L, 1)));
return 1; return 1;
} }
static int math_acos (lua_State *L) { static int math_acos (lua_State *L) {
lua_pushnumber(L, l_tg(acos)(luaL_checknumber(L, 1))); lua_pushnumber(L, l_mathop(acos)(luaL_checknumber(L, 1)));
return 1; return 1;
} }
static int math_atan (lua_State *L) { static int math_atan (lua_State *L) {
lua_pushnumber(L, l_tg(atan)(luaL_checknumber(L, 1))); lua_pushnumber(L, l_mathop(atan)(luaL_checknumber(L, 1)));
return 1; return 1;
} }
static int math_atan2 (lua_State *L) { static int math_atan2 (lua_State *L) {
lua_pushnumber(L, l_tg(atan2)(luaL_checknumber(L, 1), lua_pushnumber(L, l_mathop(atan2)(luaL_checknumber(L, 1),
luaL_checknumber(L, 2))); luaL_checknumber(L, 2)));
return 1; return 1;
} }
static int math_ceil (lua_State *L) { static int math_ceil (lua_State *L) {
lua_pushnumber(L, l_tg(ceil)(luaL_checknumber(L, 1))); lua_pushnumber(L, l_mathop(ceil)(luaL_checknumber(L, 1)));
return 1; return 1;
} }
static int math_floor (lua_State *L) { static int math_floor (lua_State *L) {
lua_pushnumber(L, l_tg(floor)(luaL_checknumber(L, 1))); lua_pushnumber(L, l_mathop(floor)(luaL_checknumber(L, 1)));
return 1; return 1;
} }
static int math_fmod (lua_State *L) { static int math_fmod (lua_State *L) {
lua_pushnumber(L, l_tg(fmod)(luaL_checknumber(L, 1), lua_pushnumber(L, l_mathop(fmod)(luaL_checknumber(L, 1),
luaL_checknumber(L, 2))); luaL_checknumber(L, 2)));
return 1; return 1;
} }
static int math_modf (lua_State *L) { static int math_modf (lua_State *L) {
lua_Number ip; lua_Number ip;
lua_Number fp = l_tg(modf)(luaL_checknumber(L, 1), &ip); lua_Number fp = l_mathop(modf)(luaL_checknumber(L, 1), &ip);
lua_pushnumber(L, ip); lua_pushnumber(L, ip);
lua_pushnumber(L, fp); lua_pushnumber(L, fp);
return 2; return 2;
} }
static int math_sqrt (lua_State *L) { static int math_sqrt (lua_State *L) {
lua_pushnumber(L, l_tg(sqrt)(luaL_checknumber(L, 1))); lua_pushnumber(L, l_mathop(sqrt)(luaL_checknumber(L, 1)));
return 1; return 1;
} }
static int math_pow (lua_State *L) { static int math_pow (lua_State *L) {
lua_pushnumber(L, l_tg(pow)(luaL_checknumber(L, 1), lua_Number x = luaL_checknumber(L, 1);
luaL_checknumber(L, 2))); lua_Number y = luaL_checknumber(L, 2);
lua_pushnumber(L, l_mathop(pow)(x, y));
return 1; return 1;
} }
@ -124,11 +119,11 @@ static int math_log (lua_State *L) {
lua_Number x = luaL_checknumber(L, 1); lua_Number x = luaL_checknumber(L, 1);
lua_Number res; lua_Number res;
if (lua_isnoneornil(L, 2)) if (lua_isnoneornil(L, 2))
res = l_tg(log)(x); res = l_mathop(log)(x);
else { else {
lua_Number base = luaL_checknumber(L, 2); lua_Number base = luaL_checknumber(L, 2);
if (base == 10.0) res = l_tg(log10)(x); if (base == (lua_Number)10.0) res = l_mathop(log10)(x);
else res = l_tg(log)(x)/l_tg(log)(base); else res = l_mathop(log)(x)/l_mathop(log)(base);
} }
lua_pushnumber(L, res); lua_pushnumber(L, res);
return 1; return 1;
@ -136,13 +131,13 @@ static int math_log (lua_State *L) {
#if defined(LUA_COMPAT_LOG10) #if defined(LUA_COMPAT_LOG10)
static int math_log10 (lua_State *L) { static int math_log10 (lua_State *L) {
lua_pushnumber(L, l_tg(log10)(luaL_checknumber(L, 1))); lua_pushnumber(L, l_mathop(log10)(luaL_checknumber(L, 1)));
return 1; return 1;
} }
#endif #endif
static int math_exp (lua_State *L) { static int math_exp (lua_State *L) {
lua_pushnumber(L, l_tg(exp)(luaL_checknumber(L, 1))); lua_pushnumber(L, l_mathop(exp)(luaL_checknumber(L, 1)));
return 1; return 1;
} }
@ -158,14 +153,15 @@ static int math_rad (lua_State *L) {
static int math_frexp (lua_State *L) { static int math_frexp (lua_State *L) {
int e; int e;
lua_pushnumber(L, l_tg(frexp)(luaL_checknumber(L, 1), &e)); lua_pushnumber(L, l_mathop(frexp)(luaL_checknumber(L, 1), &e));
lua_pushinteger(L, e); lua_pushinteger(L, e);
return 2; return 2;
} }
static int math_ldexp (lua_State *L) { static int math_ldexp (lua_State *L) {
lua_pushnumber(L, l_tg(ldexp)(luaL_checknumber(L, 1), lua_Number x = luaL_checknumber(L, 1);
luaL_checkint(L, 2))); int ep = luaL_checkint(L, 2);
lua_pushnumber(L, l_mathop(ldexp)(x, ep));
return 1; return 1;
} }
@ -210,15 +206,15 @@ static int math_random (lua_State *L) {
} }
case 1: { /* only upper limit */ case 1: { /* only upper limit */
lua_Number u = luaL_checknumber(L, 1); lua_Number u = luaL_checknumber(L, 1);
luaL_argcheck(L, 1.0 <= u, 1, "interval is empty"); luaL_argcheck(L, (lua_Number)1.0 <= u, 1, "interval is empty");
lua_pushnumber(L, l_tg(floor)(r*u) + 1.0); /* int in [1, u] */ lua_pushnumber(L, l_mathop(floor)(r*u) + (lua_Number)(1.0)); /* [1, u] */
break; break;
} }
case 2: { /* lower and upper limits */ case 2: { /* lower and upper limits */
lua_Number l = luaL_checknumber(L, 1); lua_Number l = luaL_checknumber(L, 1);
lua_Number u = luaL_checknumber(L, 2); lua_Number u = luaL_checknumber(L, 2);
luaL_argcheck(L, l <= u, 2, "interval is empty"); luaL_argcheck(L, l <= u, 2, "interval is empty");
lua_pushnumber(L, l_tg(floor)(r*(u-l+1)) + l); /* int in [l, u] */ lua_pushnumber(L, l_mathop(floor)(r*(u-l+1)) + l); /* [l, u] */
break; break;
} }
default: return luaL_error(L, "wrong number of arguments"); default: return luaL_error(L, "wrong number of arguments");

@ -1,5 +1,5 @@
/* /*
** $Id: lmem.c,v 1.84 2012/05/23 15:41:53 roberto Exp $ ** $Id: lmem.c,v 1.84.1.1 2013/04/12 18:48:47 roberto Exp $
** Interface to Memory Manager ** Interface to Memory Manager
** See Copyright Notice in lua.h ** See Copyright Notice in lua.h
*/ */

@ -1,5 +1,5 @@
/* /*
** $Id: loadlib.c,v 1.111 2012/05/30 12:33:44 roberto Exp $ ** $Id: loadlib.c,v 1.111.1.1 2013/04/12 18:48:47 roberto Exp $
** Dynamic library loader for Lua ** Dynamic library loader for Lua
** See Copyright Notice in lua.h ** See Copyright Notice in lua.h
** **

@ -1,5 +1,5 @@
/* /*
** $Id: lobject.c,v 2.55 2011/11/30 19:30:16 roberto Exp $ ** $Id: lobject.c,v 2.58.1.1 2013/04/12 18:48:47 roberto Exp $
** Some generic functions over Lua objects ** Some generic functions over Lua objects
** See Copyright Notice in lua.h ** See Copyright Notice in lua.h
*/ */
@ -104,7 +104,7 @@ static int isneg (const char **s) {
static lua_Number readhexa (const char **s, lua_Number r, int *count) { static lua_Number readhexa (const char **s, lua_Number r, int *count) {
for (; lisxdigit(cast_uchar(**s)); (*s)++) { /* read integer part */ for (; lisxdigit(cast_uchar(**s)); (*s)++) { /* read integer part */
r = (r * 16.0) + cast_num(luaO_hexavalue(cast_uchar(**s))); r = (r * cast_num(16.0)) + cast_num(luaO_hexavalue(cast_uchar(**s)));
(*count)++; (*count)++;
} }
return r; return r;
@ -149,7 +149,7 @@ static lua_Number lua_strx2number (const char *s, char **endptr) {
*endptr = cast(char *, s); /* valid up to here */ *endptr = cast(char *, s); /* valid up to here */
ret: ret:
if (neg) r = -r; if (neg) r = -r;
return ldexp(r, e); return l_mathop(ldexp)(r, e);
} }
#endif #endif
@ -171,8 +171,7 @@ int luaO_str2d (const char *s, size_t len, lua_Number *result) {
static void pushstr (lua_State *L, const char *str, size_t l) { static void pushstr (lua_State *L, const char *str, size_t l) {
setsvalue2s(L, L->top, luaS_newlstr(L, str, l)); setsvalue2s(L, L->top++, luaS_newlstr(L, str, l));
incr_top(L);
} }
@ -182,8 +181,8 @@ const char *luaO_pushvfstring (lua_State *L, const char *fmt, va_list argp) {
for (;;) { for (;;) {
const char *e = strchr(fmt, '%'); const char *e = strchr(fmt, '%');
if (e == NULL) break; if (e == NULL) break;
setsvalue2s(L, L->top, luaS_newlstr(L, fmt, e-fmt)); luaD_checkstack(L, 2); /* fmt + item */
incr_top(L); pushstr(L, fmt, e - fmt);
switch (*(e+1)) { switch (*(e+1)) {
case 's': { case 's': {
const char *s = va_arg(argp, char *); const char *s = va_arg(argp, char *);
@ -198,13 +197,11 @@ const char *luaO_pushvfstring (lua_State *L, const char *fmt, va_list argp) {
break; break;
} }
case 'd': { case 'd': {
setnvalue(L->top, cast_num(va_arg(argp, int))); setnvalue(L->top++, cast_num(va_arg(argp, int)));
incr_top(L);
break; break;
} }
case 'f': { case 'f': {
setnvalue(L->top, cast_num(va_arg(argp, l_uacNumber))); setnvalue(L->top++, cast_num(va_arg(argp, l_uacNumber)));
incr_top(L);
break; break;
} }
case 'p': { case 'p': {
@ -226,6 +223,7 @@ const char *luaO_pushvfstring (lua_State *L, const char *fmt, va_list argp) {
n += 2; n += 2;
fmt = e+2; fmt = e+2;
} }
luaD_checkstack(L, 1);
pushstr(L, fmt, strlen(fmt)); pushstr(L, fmt, strlen(fmt));
if (n > 0) luaV_concat(L, n + 1); if (n > 0) luaV_concat(L, n + 1);
return svalue(L->top - 1); return svalue(L->top - 1);

@ -1,5 +1,5 @@
/* /*
** $Id: lopcodes.c,v 1.49 2012/05/14 13:34:18 roberto Exp $ ** $Id: lopcodes.c,v 1.49.1.1 2013/04/12 18:48:47 roberto Exp $
** Opcodes for Lua virtual machine ** Opcodes for Lua virtual machine
** See Copyright Notice in lua.h ** See Copyright Notice in lua.h
*/ */

@ -1,5 +1,5 @@
/* /*
** $Id: loslib.c,v 1.39 2012/05/23 15:37:09 roberto Exp $ ** $Id: loslib.c,v 1.40.1.1 2013/04/12 18:48:47 roberto Exp $
** Standard Operating System library ** Standard Operating System library
** See Copyright Notice in lua.h ** See Copyright Notice in lua.h
*/ */
@ -98,7 +98,7 @@ static int os_remove (lua_State *L) {
static int os_rename (lua_State *L) { static int os_rename (lua_State *L) {
const char *fromname = luaL_checkstring(L, 1); const char *fromname = luaL_checkstring(L, 1);
const char *toname = luaL_checkstring(L, 2); const char *toname = luaL_checkstring(L, 2);
return luaL_fileresult(L, rename(fromname, toname) == 0, fromname); return luaL_fileresult(L, rename(fromname, toname) == 0, NULL);
} }

@ -1,5 +1,5 @@
/* /*
** $Id: lparser.c,v 2.128 2012/05/20 14:51:23 roberto Exp $ ** $Id: lparser.c,v 2.130.1.1 2013/04/12 18:48:47 roberto Exp $
** Lua Parser ** Lua Parser
** See Copyright Notice in lua.h ** See Copyright Notice in lua.h
*/ */
@ -245,7 +245,7 @@ static int newupvalue (FuncState *fs, TString *name, expdesc *v) {
static int searchvar (FuncState *fs, TString *n) { static int searchvar (FuncState *fs, TString *n) {
int i; int i;
for (i=fs->nactvar-1; i >= 0; i--) { for (i = cast_int(fs->nactvar) - 1; i >= 0; i--) {
if (luaS_eqstr(n, getlocvar(fs, i)->varname)) if (luaS_eqstr(n, getlocvar(fs, i)->varname))
return i; return i;
} }
@ -512,12 +512,15 @@ static Proto *addprototype (LexState *ls) {
/* /*
** codes instruction to create new closure in parent function ** codes instruction to create new closure in parent function.
** The OP_CLOSURE instruction must use the last available register,
** so that, if it invokes the GC, the GC knows which registers
** are in use at that time.
*/ */
static void codeclosure (LexState *ls, expdesc *v) { static void codeclosure (LexState *ls, expdesc *v) {
FuncState *fs = ls->fs->prev; FuncState *fs = ls->fs->prev;
init_exp(v, VRELOCABLE, luaK_codeABx(fs, OP_CLOSURE, 0, fs->np - 1)); init_exp(v, VRELOCABLE, luaK_codeABx(fs, OP_CLOSURE, 0, fs->np - 1));
luaK_exp2nextreg(fs, v); /* fix it at stack top (for GC) */ luaK_exp2nextreg(fs, v); /* fix it at the last register */
} }

@ -1,5 +1,5 @@
/* /*
** $Id: lstate.c,v 2.98 2012/05/30 12:33:44 roberto Exp $ ** $Id: lstate.c,v 2.99.1.2 2013/11/08 17:45:31 roberto Exp $
** Global State ** Global State
** See Copyright Notice in lua.h ** See Copyright Notice in lua.h
*/ */
@ -48,7 +48,7 @@
*/ */
#if !defined(luai_makeseed) #if !defined(luai_makeseed)
#include <time.h> #include <time.h>
#define luai_makeseed() cast(size_t, time(NULL)) #define luai_makeseed() cast(unsigned int, time(NULL))
#endif #endif
@ -192,6 +192,8 @@ static void f_luaopen (lua_State *L, void *ud) {
g->memerrmsg = luaS_newliteral(L, MEMERRMSG); g->memerrmsg = luaS_newliteral(L, MEMERRMSG);
luaS_fix(g->memerrmsg); /* it should never be collected */ luaS_fix(g->memerrmsg); /* it should never be collected */
g->gcrunning = 1; /* allow gc */ g->gcrunning = 1; /* allow gc */
g->version = lua_version(NULL);
luai_userstateopen(L);
} }
@ -222,6 +224,8 @@ static void close_state (lua_State *L) {
global_State *g = G(L); global_State *g = G(L);
luaF_close(L, L->stack); /* close all upvalues for this thread */ luaF_close(L, L->stack); /* close all upvalues for this thread */
luaC_freeallobjects(L); /* collect all objects */ luaC_freeallobjects(L); /* collect all objects */
if (g->version) /* closing a fully built state? */
luai_userstateclose(L);
luaM_freearray(L, G(L)->strt.hash, G(L)->strt.size); luaM_freearray(L, G(L)->strt.hash, G(L)->strt.size);
luaZ_freebuffer(L, &g->buff); luaZ_freebuffer(L, &g->buff);
freestack(L); freestack(L);
@ -287,7 +291,7 @@ LUA_API lua_State *lua_newstate (lua_Alloc f, void *ud) {
setnilvalue(&g->l_registry); setnilvalue(&g->l_registry);
luaZ_initbuffer(L, &g->buff); luaZ_initbuffer(L, &g->buff);
g->panic = NULL; g->panic = NULL;
g->version = lua_version(NULL); g->version = NULL;
g->gcstate = GCSpause; g->gcstate = GCSpause;
g->allgc = NULL; g->allgc = NULL;
g->finobj = NULL; g->finobj = NULL;
@ -306,8 +310,6 @@ LUA_API lua_State *lua_newstate (lua_Alloc f, void *ud) {
close_state(L); close_state(L);
L = NULL; L = NULL;
} }
else
luai_userstateopen(L);
return L; return L;
} }
@ -315,7 +317,6 @@ LUA_API lua_State *lua_newstate (lua_Alloc f, void *ud) {
LUA_API void lua_close (lua_State *L) { LUA_API void lua_close (lua_State *L) {
L = G(L)->mainthread; /* only the main thread can be closed */ L = G(L)->mainthread; /* only the main thread can be closed */
lua_lock(L); lua_lock(L);
luai_userstateclose(L);
close_state(L); close_state(L);
} }

@ -1,5 +1,5 @@
/* /*
** $Id: lstring.c,v 2.24 2012/05/11 14:14:42 roberto Exp $ ** $Id: lstring.c,v 2.26.1.1 2013/04/12 18:48:47 roberto Exp $
** String table (keeps all strings handled by Lua) ** String table (keeps all strings handled by Lua)
** See Copyright Notice in lua.h ** See Copyright Notice in lua.h
*/ */
@ -49,7 +49,7 @@ int luaS_eqstr (TString *a, TString *b) {
unsigned int luaS_hash (const char *str, size_t l, unsigned int seed) { unsigned int luaS_hash (const char *str, size_t l, unsigned int seed) {
unsigned int h = seed ^ l; unsigned int h = seed ^ cast(unsigned int, l);
size_t l1; size_t l1;
size_t step = (l >> LUAI_HASHLIMIT) + 1; size_t step = (l >> LUAI_HASHLIMIT) + 1;
for (l1 = l; l1 >= step; l1 -= step) for (l1 = l; l1 >= step; l1 -= step)
@ -139,7 +139,7 @@ static TString *internshrstr (lua_State *L, const char *str, size_t l) {
o = gch(o)->next) { o = gch(o)->next) {
TString *ts = rawgco2ts(o); TString *ts = rawgco2ts(o);
if (h == ts->tsv.hash && if (h == ts->tsv.hash &&
ts->tsv.len == l && l == ts->tsv.len &&
(memcmp(str, getstr(ts), l * sizeof(char)) == 0)) { (memcmp(str, getstr(ts), l * sizeof(char)) == 0)) {
if (isdead(G(L), o)) /* string is dead (but was not collected yet)? */ if (isdead(G(L), o)) /* string is dead (but was not collected yet)? */
changewhite(o); /* resurrect it */ changewhite(o); /* resurrect it */

@ -1,5 +1,5 @@
/* /*
** $Id: lstrlib.c,v 1.176 2012/05/23 15:37:09 roberto Exp $ ** $Id: lstrlib.c,v 1.178.1.1 2013/04/12 18:48:47 roberto Exp $
** Standard library for string operations and pattern-matching ** Standard library for string operations and pattern-matching
** See Copyright Notice in lua.h ** See Copyright Notice in lua.h
*/ */
@ -194,7 +194,9 @@ static int str_dump (lua_State *L) {
#define CAP_UNFINISHED (-1) #define CAP_UNFINISHED (-1)
#define CAP_POSITION (-2) #define CAP_POSITION (-2)
typedef struct MatchState { typedef struct MatchState {
int matchdepth; /* control for recursive depth (to avoid C stack overflow) */
const char *src_init; /* init of source string */ const char *src_init; /* init of source string */
const char *src_end; /* end ('\0') of source string */ const char *src_end; /* end ('\0') of source string */
const char *p_end; /* end ('\0') of pattern */ const char *p_end; /* end ('\0') of pattern */
@ -207,6 +209,16 @@ typedef struct MatchState {
} MatchState; } MatchState;
/* recursive function */
static const char *match (MatchState *ms, const char *s, const char *p);
/* maximum recursion depth for 'match' */
#if !defined(MAXCCALLS)
#define MAXCCALLS 200
#endif
#define L_ESC '%' #define L_ESC '%'
#define SPECIALS "^$*+?.([%-" #define SPECIALS "^$*+?.([%-"
@ -294,19 +306,22 @@ static int matchbracketclass (int c, const char *p, const char *ec) {
} }
static int singlematch (int c, const char *p, const char *ep) { static int singlematch (MatchState *ms, const char *s, const char *p,
switch (*p) { const char *ep) {
case '.': return 1; /* matches any char */ if (s >= ms->src_end)
case L_ESC: return match_class(c, uchar(*(p+1))); return 0;
case '[': return matchbracketclass(c, p, ep-1); else {
default: return (uchar(*p) == c); int c = uchar(*s);
switch (*p) {
case '.': return 1; /* matches any char */
case L_ESC: return match_class(c, uchar(*(p+1)));
case '[': return matchbracketclass(c, p, ep-1);
default: return (uchar(*p) == c);
}
} }
} }
static const char *match (MatchState *ms, const char *s, const char *p);
static const char *matchbalance (MatchState *ms, const char *s, static const char *matchbalance (MatchState *ms, const char *s,
const char *p) { const char *p) {
if (p >= ms->p_end - 1) if (p >= ms->p_end - 1)
@ -331,7 +346,7 @@ static const char *matchbalance (MatchState *ms, const char *s,
static const char *max_expand (MatchState *ms, const char *s, static const char *max_expand (MatchState *ms, const char *s,
const char *p, const char *ep) { const char *p, const char *ep) {
ptrdiff_t i = 0; /* counts maximum expand for item */ ptrdiff_t i = 0; /* counts maximum expand for item */
while ((s+i)<ms->src_end && singlematch(uchar(*(s+i)), p, ep)) while (singlematch(ms, s + i, p, ep))
i++; i++;
/* keeps trying to match with the maximum repetitions */ /* keeps trying to match with the maximum repetitions */
while (i>=0) { while (i>=0) {
@ -349,7 +364,7 @@ static const char *min_expand (MatchState *ms, const char *s,
const char *res = match(ms, s, ep+1); const char *res = match(ms, s, ep+1);
if (res != NULL) if (res != NULL)
return res; return res;
else if (s<ms->src_end && singlematch(uchar(*s), p, ep)) else if (singlematch(ms, s, p, ep))
s++; /* try with one more repetition */ s++; /* try with one more repetition */
else return NULL; else return NULL;
} }
@ -393,79 +408,105 @@ static const char *match_capture (MatchState *ms, const char *s, int l) {
static const char *match (MatchState *ms, const char *s, const char *p) { static const char *match (MatchState *ms, const char *s, const char *p) {
if (ms->matchdepth-- == 0)
luaL_error(ms->L, "pattern too complex");
init: /* using goto's to optimize tail recursion */ init: /* using goto's to optimize tail recursion */
if (p == ms->p_end) /* end of pattern? */ if (p != ms->p_end) { /* end of pattern? */
return s; /* match succeeded */ switch (*p) {
switch (*p) { case '(': { /* start capture */
case '(': { /* start capture */ if (*(p + 1) == ')') /* position capture? */
if (*(p+1) == ')') /* position capture? */ s = start_capture(ms, s, p + 2, CAP_POSITION);
return start_capture(ms, s, p+2, CAP_POSITION); else
else s = start_capture(ms, s, p + 1, CAP_UNFINISHED);
return start_capture(ms, s, p+1, CAP_UNFINISHED); break;
}
case ')': { /* end capture */
return end_capture(ms, s, p+1);
}
case '$': {
if ((p+1) == ms->p_end) /* is the `$' the last char in pattern? */
return (s == ms->src_end) ? s : NULL; /* check end of string */
else goto dflt;
}
case L_ESC: { /* escaped sequences not in the format class[*+?-]? */
switch (*(p+1)) {
case 'b': { /* balanced string? */
s = matchbalance(ms, s, p+2);
if (s == NULL) return NULL;
p+=4; goto init; /* else return match(ms, s, p+4); */
}
case 'f': { /* frontier? */
const char *ep; char previous;
p += 2;
if (*p != '[')
luaL_error(ms->L, "missing " LUA_QL("[") " after "
LUA_QL("%%f") " in pattern");
ep = classend(ms, p); /* points to what is next */
previous = (s == ms->src_init) ? '\0' : *(s-1);
if (matchbracketclass(uchar(previous), p, ep-1) ||
!matchbracketclass(uchar(*s), p, ep-1)) return NULL;
p=ep; goto init; /* else return match(ms, s, ep); */
}
case '0': case '1': case '2': case '3':
case '4': case '5': case '6': case '7':
case '8': case '9': { /* capture results (%0-%9)? */
s = match_capture(ms, s, uchar(*(p+1)));
if (s == NULL) return NULL;
p+=2; goto init; /* else return match(ms, s, p+2) */
}
default: goto dflt;
} }
} case ')': { /* end capture */
default: dflt: { /* pattern class plus optional suffix */ s = end_capture(ms, s, p + 1);
const char *ep = classend(ms, p); /* points to what is next */ break;
int m = s < ms->src_end && singlematch(uchar(*s), p, ep); }
switch (*ep) { case '$': {
case '?': { /* optional */ if ((p + 1) != ms->p_end) /* is the `$' the last char in pattern? */
const char *res; goto dflt; /* no; go to default */
if (m && ((res=match(ms, s+1, ep+1)) != NULL)) s = (s == ms->src_end) ? s : NULL; /* check end of string */
return res; break;
p=ep+1; goto init; /* else return match(ms, s, ep+1); */ }
} case L_ESC: { /* escaped sequences not in the format class[*+?-]? */
case '*': { /* 0 or more repetitions */ switch (*(p + 1)) {
return max_expand(ms, s, p, ep); case 'b': { /* balanced string? */
} s = matchbalance(ms, s, p + 2);
case '+': { /* 1 or more repetitions */ if (s != NULL) {
return (m ? max_expand(ms, s+1, p, ep) : NULL); p += 4; goto init; /* return match(ms, s, p + 4); */
} /* else fail (s == NULL) */
break;
}
case 'f': { /* frontier? */
const char *ep; char previous;
p += 2;
if (*p != '[')
luaL_error(ms->L, "missing " LUA_QL("[") " after "
LUA_QL("%%f") " in pattern");
ep = classend(ms, p); /* points to what is next */
previous = (s == ms->src_init) ? '\0' : *(s - 1);
if (!matchbracketclass(uchar(previous), p, ep - 1) &&
matchbracketclass(uchar(*s), p, ep - 1)) {
p = ep; goto init; /* return match(ms, s, ep); */
}
s = NULL; /* match failed */
break;
}
case '0': case '1': case '2': case '3':
case '4': case '5': case '6': case '7':
case '8': case '9': { /* capture results (%0-%9)? */
s = match_capture(ms, s, uchar(*(p + 1)));
if (s != NULL) {
p += 2; goto init; /* return match(ms, s, p + 2) */
}
break;
}
default: goto dflt;
} }
case '-': { /* 0 or more repetitions (minimum) */ break;
return min_expand(ms, s, p, ep); }
default: dflt: { /* pattern class plus optional suffix */
const char *ep = classend(ms, p); /* points to optional suffix */
/* does not match at least once? */
if (!singlematch(ms, s, p, ep)) {
if (*ep == '*' || *ep == '?' || *ep == '-') { /* accept empty? */
p = ep + 1; goto init; /* return match(ms, s, ep + 1); */
}
else /* '+' or no suffix */
s = NULL; /* fail */
} }
default: { else { /* matched once */
if (!m) return NULL; switch (*ep) { /* handle optional suffix */
s++; p=ep; goto init; /* else return match(ms, s+1, ep); */ case '?': { /* optional */
const char *res;
if ((res = match(ms, s + 1, ep + 1)) != NULL)
s = res;
else {
p = ep + 1; goto init; /* else return match(ms, s, ep + 1); */
}
break;
}
case '+': /* 1 or more repetitions */
s++; /* 1 match already done */
/* go through */
case '*': /* 0 or more repetitions */
s = max_expand(ms, s, p, ep);
break;
case '-': /* 0 or more repetitions (minimum) */
s = min_expand(ms, s, p, ep);
break;
default: /* no suffix */
s++; p = ep; goto init; /* return match(ms, s + 1, ep); */
}
} }
break;
} }
} }
} }
ms->matchdepth++;
return s;
} }
@ -561,12 +602,14 @@ static int str_find_aux (lua_State *L, int find) {
p++; lp--; /* skip anchor character */ p++; lp--; /* skip anchor character */
} }
ms.L = L; ms.L = L;
ms.matchdepth = MAXCCALLS;
ms.src_init = s; ms.src_init = s;
ms.src_end = s + ls; ms.src_end = s + ls;
ms.p_end = p + lp; ms.p_end = p + lp;
do { do {
const char *res; const char *res;
ms.level = 0; ms.level = 0;
lua_assert(ms.matchdepth == MAXCCALLS);
if ((res=match(&ms, s1, p)) != NULL) { if ((res=match(&ms, s1, p)) != NULL) {
if (find) { if (find) {
lua_pushinteger(L, s1 - s + 1); /* start */ lua_pushinteger(L, s1 - s + 1); /* start */
@ -600,6 +643,7 @@ static int gmatch_aux (lua_State *L) {
const char *p = lua_tolstring(L, lua_upvalueindex(2), &lp); const char *p = lua_tolstring(L, lua_upvalueindex(2), &lp);
const char *src; const char *src;
ms.L = L; ms.L = L;
ms.matchdepth = MAXCCALLS;
ms.src_init = s; ms.src_init = s;
ms.src_end = s+ls; ms.src_end = s+ls;
ms.p_end = p + lp; ms.p_end = p + lp;
@ -608,6 +652,7 @@ static int gmatch_aux (lua_State *L) {
src++) { src++) {
const char *e; const char *e;
ms.level = 0; ms.level = 0;
lua_assert(ms.matchdepth == MAXCCALLS);
if ((e = match(&ms, src, p)) != NULL) { if ((e = match(&ms, src, p)) != NULL) {
lua_Integer newstart = e-s; lua_Integer newstart = e-s;
if (e == src) newstart++; /* empty match? go at least one position */ if (e == src) newstart++; /* empty match? go at least one position */
@ -705,12 +750,14 @@ static int str_gsub (lua_State *L) {
p++; lp--; /* skip anchor character */ p++; lp--; /* skip anchor character */
} }
ms.L = L; ms.L = L;
ms.matchdepth = MAXCCALLS;
ms.src_init = src; ms.src_init = src;
ms.src_end = src+srcl; ms.src_end = src+srcl;
ms.p_end = p + lp; ms.p_end = p + lp;
while (n < max_s) { while (n < max_s) {
const char *e; const char *e;
ms.level = 0; ms.level = 0;
lua_assert(ms.matchdepth == MAXCCALLS);
e = match(&ms, src, p); e = match(&ms, src, p);
if (e) { if (e) {
n++; n++;
@ -867,7 +914,7 @@ static int str_format (lua_State *L) {
nb = sprintf(buff, form, luaL_checkint(L, arg)); nb = sprintf(buff, form, luaL_checkint(L, arg));
break; break;
} }
case 'd': case 'i': { case 'd': case 'i': {
lua_Number n = luaL_checknumber(L, arg); lua_Number n = luaL_checknumber(L, arg);
LUA_INTFRM_T ni = (LUA_INTFRM_T)n; LUA_INTFRM_T ni = (LUA_INTFRM_T)n;
lua_Number diff = n - (lua_Number)ni; lua_Number diff = n - (lua_Number)ni;
@ -877,7 +924,7 @@ static int str_format (lua_State *L) {
nb = sprintf(buff, form, ni); nb = sprintf(buff, form, ni);
break; break;
} }
case 'o': case 'u': case 'x': case 'X': { case 'o': case 'u': case 'x': case 'X': {
lua_Number n = luaL_checknumber(L, arg); lua_Number n = luaL_checknumber(L, arg);
unsigned LUA_INTFRM_T ni = (unsigned LUA_INTFRM_T)n; unsigned LUA_INTFRM_T ni = (unsigned LUA_INTFRM_T)n;
lua_Number diff = n - (lua_Number)ni; lua_Number diff = n - (lua_Number)ni;
@ -887,7 +934,7 @@ static int str_format (lua_State *L) {
nb = sprintf(buff, form, ni); nb = sprintf(buff, form, ni);
break; break;
} }
case 'e': case 'E': case 'f': case 'e': case 'E': case 'f':
#if defined(LUA_USE_AFORMAT) #if defined(LUA_USE_AFORMAT)
case 'a': case 'A': case 'a': case 'A':
#endif #endif

@ -1,5 +1,5 @@
/* /*
** $Id: ltable.c,v 2.71 2012/05/23 15:37:09 roberto Exp $ ** $Id: ltable.c,v 2.72.1.1 2013/04/12 18:48:47 roberto Exp $
** Lua tables (hash) ** Lua tables (hash)
** See Copyright Notice in lua.h ** See Copyright Notice in lua.h
*/ */
@ -480,13 +480,13 @@ const TValue *luaH_getstr (Table *t, TString *key) {
*/ */
const TValue *luaH_get (Table *t, const TValue *key) { const TValue *luaH_get (Table *t, const TValue *key) {
switch (ttype(key)) { switch (ttype(key)) {
case LUA_TNIL: return luaO_nilobject;
case LUA_TSHRSTR: return luaH_getstr(t, rawtsvalue(key)); case LUA_TSHRSTR: return luaH_getstr(t, rawtsvalue(key));
case LUA_TNIL: return luaO_nilobject;
case LUA_TNUMBER: { case LUA_TNUMBER: {
int k; int k;
lua_Number n = nvalue(key); lua_Number n = nvalue(key);
lua_number2int(k, n); lua_number2int(k, n);
if (luai_numeq(cast_num(k), nvalue(key))) /* index is int? */ if (luai_numeq(cast_num(k), n)) /* index is int? */
return luaH_getint(t, k); /* use specialized version */ return luaH_getint(t, k); /* use specialized version */
/* else go through */ /* else go through */
} }

@ -1,5 +1,5 @@
/* /*
** $Id: ltablib.c,v 1.63 2011/11/28 17:26:30 roberto Exp $ ** $Id: ltablib.c,v 1.65.1.1 2013/04/12 18:48:47 roberto Exp $
** Library for Table Manipulation ** Library for Table Manipulation
** See Copyright Notice in lua.h ** See Copyright Notice in lua.h
*/ */
@ -16,8 +16,8 @@
#include "lualib.h" #include "lualib.h"
#define aux_getn(L,n) \ #define aux_getn(L,n) (luaL_checktype(L, n, LUA_TTABLE), luaL_len(L, n))
(luaL_checktype(L, n, LUA_TTABLE), luaL_len(L, n))
#if defined(LUA_COMPAT_MAXN) #if defined(LUA_COMPAT_MAXN)
@ -49,7 +49,7 @@ static int tinsert (lua_State *L) {
case 3: { case 3: {
int i; int i;
pos = luaL_checkint(L, 2); /* 2nd argument is the position */ pos = luaL_checkint(L, 2); /* 2nd argument is the position */
if (pos > e) e = pos; /* `grow' array if necessary */ luaL_argcheck(L, 1 <= pos && pos <= e, 2, "position out of bounds");
for (i = e; i > pos; i--) { /* move up elements */ for (i = e; i > pos; i--) { /* move up elements */
lua_rawgeti(L, 1, i-1); lua_rawgeti(L, 1, i-1);
lua_rawseti(L, 1, i); /* t[i] = t[i-1] */ lua_rawseti(L, 1, i); /* t[i] = t[i-1] */
@ -66,17 +66,17 @@ static int tinsert (lua_State *L) {
static int tremove (lua_State *L) { static int tremove (lua_State *L) {
int e = aux_getn(L, 1); int size = aux_getn(L, 1);
int pos = luaL_optint(L, 2, e); int pos = luaL_optint(L, 2, size);
if (!(1 <= pos && pos <= e)) /* position is outside bounds? */ if (pos != size) /* validate 'pos' if given */
return 0; /* nothing to remove */ luaL_argcheck(L, 1 <= pos && pos <= size + 1, 1, "position out of bounds");
lua_rawgeti(L, 1, pos); /* result = t[pos] */ lua_rawgeti(L, 1, pos); /* result = t[pos] */
for ( ;pos<e; pos++) { for ( ; pos < size; pos++) {
lua_rawgeti(L, 1, pos+1); lua_rawgeti(L, 1, pos+1);
lua_rawseti(L, 1, pos); /* t[pos] = t[pos+1] */ lua_rawseti(L, 1, pos); /* t[pos] = t[pos+1] */
} }
lua_pushnil(L); lua_pushnil(L);
lua_rawseti(L, 1, e); /* t[e] = nil */ lua_rawseti(L, 1, pos); /* t[pos] = nil */
return 1; return 1;
} }

@ -1,5 +1,5 @@
/* /*
** $Id: ltm.c,v 2.14 2011/06/02 19:31:40 roberto Exp $ ** $Id: ltm.c,v 2.14.1.1 2013/04/12 18:48:47 roberto Exp $
** Tag methods ** Tag methods
** See Copyright Notice in lua.h ** See Copyright Notice in lua.h
*/ */

@ -1,5 +1,5 @@
/* /*
** $Id: lua.c,v 1.205 2012/05/23 15:37:09 roberto Exp $ ** $Id: lua.c,v 1.206.1.1 2013/04/12 18:48:47 roberto Exp $
** Lua stand-alone interpreter ** Lua stand-alone interpreter
** See Copyright Notice in lua.h ** See Copyright Notice in lua.h
*/ */
@ -237,7 +237,6 @@ static const char *get_prompt (lua_State *L, int firstline) {
lua_getglobal(L, firstline ? "_PROMPT" : "_PROMPT2"); lua_getglobal(L, firstline ? "_PROMPT" : "_PROMPT2");
p = lua_tostring(L, -1); p = lua_tostring(L, -1);
if (p == NULL) p = (firstline ? LUA_PROMPT : LUA_PROMPT2); if (p == NULL) p = (firstline ? LUA_PROMPT : LUA_PROMPT2);
lua_pop(L, 1); /* remove global */
return p; return p;
} }
@ -263,7 +262,9 @@ static int pushline (lua_State *L, int firstline) {
char *b = buffer; char *b = buffer;
size_t l; size_t l;
const char *prmt = get_prompt(L, firstline); const char *prmt = get_prompt(L, firstline);
if (lua_readline(L, b, prmt) == 0) int readstatus = lua_readline(L, b, prmt);
lua_pop(L, 1); /* remove result from 'get_prompt' */
if (readstatus == 0)
return 0; /* no input */ return 0; /* no input */
l = strlen(b); l = strlen(b);
if (l > 0 && b[l-1] == '\n') /* line ends with newline? */ if (l > 0 && b[l-1] == '\n') /* line ends with newline? */

@ -203,7 +203,7 @@ int main(int argc, char* argv[])
} }
/* /*
** $Id: print.c,v 1.68 2011/09/30 10:21:20 lhf Exp $ ** $Id: print.c,v 1.69 2013/07/04 01:03:46 lhf Exp $
** print bytecodes ** print bytecodes
** See Copyright Notice in lua.h ** See Copyright Notice in lua.h
*/ */
@ -251,7 +251,7 @@ static void PrintString(const TString* ts)
static void PrintConstant(const Proto* f, int i) static void PrintConstant(const Proto* f, int i)
{ {
const TValue* o=&f->k[i]; const TValue* o=&f->k[i];
switch (ttype(o)) switch (ttypenv(o))
{ {
case LUA_TNIL: case LUA_TNIL:
printf("nil"); printf("nil");

@ -1,5 +1,5 @@
/* /*
** $Id: lundump.c,v 2.22 2012/05/08 13:53:33 roberto Exp $ ** $Id: lundump.c,v 2.22.1.1 2013/04/12 18:48:47 roberto Exp $
** load precompiled Lua chunks ** load precompiled Lua chunks
** See Copyright Notice in lua.h ** See Copyright Notice in lua.h
*/ */

@ -1,5 +1,5 @@
/* /*
** $Id: lvm.c,v 2.152 2012/06/08 15:14:04 roberto Exp $ ** $Id: lvm.c,v 2.155.1.1 2013/04/12 18:48:47 roberto Exp $
** Lua virtual machine ** Lua virtual machine
** See Copyright Notice in lua.h ** See Copyright Notice in lua.h
*/ */
@ -83,7 +83,7 @@ static void traceexec (lua_State *L) {
if (counthook) if (counthook)
L->hookcount = 1; /* undo decrement to zero */ L->hookcount = 1; /* undo decrement to zero */
ci->u.l.savedpc--; /* undo increment (resume will increment it again) */ ci->u.l.savedpc--; /* undo increment (resume will increment it again) */
ci->callstatus |= CIST_HOOKYIELD; /* mark that it yieled */ ci->callstatus |= CIST_HOOKYIELD; /* mark that it yielded */
ci->func = L->top - 1; /* protect stack below results */ ci->func = L->top - 1; /* protect stack below results */
luaD_throw(L, LUA_YIELD); luaD_throw(L, LUA_YIELD);
} }
@ -98,7 +98,6 @@ static void callTM (lua_State *L, const TValue *f, const TValue *p1,
setobj2s(L, L->top++, p2); /* 2nd argument */ setobj2s(L, L->top++, p2); /* 2nd argument */
if (!hasres) /* no result? 'p3' is third argument */ if (!hasres) /* no result? 'p3' is third argument */
setobj2s(L, L->top++, p3); /* 3rd argument */ setobj2s(L, L->top++, p3); /* 3rd argument */
luaD_checkstack(L, 0);
/* metamethod may yield only when called from Lua code */ /* metamethod may yield only when called from Lua code */
luaD_call(L, L->top - (4 - hasres), hasres, isLua(L->ci)); luaD_call(L, L->top - (4 - hasres), hasres, isLua(L->ci));
if (hasres) { /* if has result, move it to its place */ if (hasres) { /* if has result, move it to its place */
@ -470,7 +469,7 @@ void luaV_finishOp (lua_State *L) {
L->top = ci->top; /* adjust results */ L->top = ci->top; /* adjust results */
break; break;
} }
case OP_TAILCALL: case OP_SETTABUP: case OP_SETTABLE: case OP_TAILCALL: case OP_SETTABUP: case OP_SETTABLE:
break; break;
default: lua_assert(0); default: lua_assert(0);
} }

@ -1,5 +1,5 @@
/* /*
** $Id: lzio.c,v 1.35 2012/05/14 13:34:18 roberto Exp $ ** $Id: lzio.c,v 1.35.1.1 2013/04/12 18:48:47 roberto Exp $
** Buffered streams ** Buffered streams
** See Copyright Notice in lua.h ** See Copyright Notice in lua.h
*/ */