mirror of
https://github.com/lua/lua
synced 2026-06-08 15:34:49 +00:00
Compare commits
1 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| 063d4e4543 |
-15
@@ -1,15 +0,0 @@
|
|||||||
.gitattributes
|
|
||||||
|
|
||||||
*.so
|
|
||||||
*.o
|
|
||||||
*.a
|
|
||||||
|
|
||||||
manual/manual.html
|
|
||||||
|
|
||||||
testes/time.txt
|
|
||||||
testes/time-debug.txt
|
|
||||||
|
|
||||||
testes/libs/all
|
|
||||||
|
|
||||||
temp
|
|
||||||
lua
|
|
||||||
@@ -1,7 +0,0 @@
|
|||||||
# Lua
|
|
||||||
|
|
||||||
This is the repository of Lua development code, as seen by the Lua team. It contains the full history of all commits but is mirrored irregularly. For complete information about Lua, visit [Lua.org](https://www.lua.org/).
|
|
||||||
|
|
||||||
Please **do not** send pull requests. To report issues, post a message to the [Lua mailing list](https://www.lua.org/lua-l.html).
|
|
||||||
|
|
||||||
Download official Lua releases from [Lua.org](https://www.lua.org/download.html).
|
|
||||||
@@ -1,8 +1,6 @@
|
|||||||
make -s -j
|
cd testes
|
||||||
cd testes/libs; make -s
|
ulimit -S -s 2000
|
||||||
cd .. # back to directory 'testes'
|
if { ../lua all.lua; } then
|
||||||
ulimit -S -s 1100
|
|
||||||
if { ../lua -W all.lua; } then
|
|
||||||
echo -e "\n\n final OK!!!!\n\n"
|
echo -e "\n\n final OK!!!!\n\n"
|
||||||
else
|
else
|
||||||
echo -e "\n\n >>>> BUG!!!!\n\n"
|
echo -e "\n\n >>>> BUG!!!!\n\n"
|
||||||
|
|||||||
@@ -1,5 +1,5 @@
|
|||||||
/*
|
/*
|
||||||
** $Id: lapi.h $
|
** $Id: lapi.h,v 2.9.1.1 2017/04/19 17:20:42 roberto Exp $
|
||||||
** Auxiliary functions from Lua API
|
** Auxiliary functions from Lua API
|
||||||
** See Copyright Notice in lua.h
|
** See Copyright Notice in lua.h
|
||||||
*/
|
*/
|
||||||
@@ -11,55 +11,14 @@
|
|||||||
#include "llimits.h"
|
#include "llimits.h"
|
||||||
#include "lstate.h"
|
#include "lstate.h"
|
||||||
|
|
||||||
|
#define api_incr_top(L) {L->top++; api_check(L, L->top <= L->ci->top, \
|
||||||
|
"stack overflow");}
|
||||||
|
|
||||||
#if defined(LUA_USE_APICHECK)
|
|
||||||
#include <assert.h>
|
|
||||||
#define api_check(l,e,msg) assert(e)
|
|
||||||
#else /* for testing */
|
|
||||||
#define api_check(l,e,msg) ((void)(l), lua_assert((e) && msg))
|
|
||||||
#endif
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
/* Increments 'L->top.p', checking for stack overflows */
|
|
||||||
#define api_incr_top(L) \
|
|
||||||
(L->top.p++, api_check(L, L->top.p <= L->ci->top.p, "stack overflow"))
|
|
||||||
|
|
||||||
|
|
||||||
/*
|
|
||||||
** macros that are executed whenever program enters the Lua core
|
|
||||||
** ('lua_lock') and leaves the core ('lua_unlock')
|
|
||||||
*/
|
|
||||||
#if !defined(lua_lock)
|
|
||||||
#define lua_lock(L) ((void) 0)
|
|
||||||
#define lua_unlock(L) ((void) 0)
|
|
||||||
#endif
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
/*
|
|
||||||
** If a call returns too many multiple returns, the callee may not have
|
|
||||||
** stack space to accommodate all results. In this case, this macro
|
|
||||||
** increases its stack space ('L->ci->top.p').
|
|
||||||
*/
|
|
||||||
#define adjustresults(L,nres) \
|
#define adjustresults(L,nres) \
|
||||||
{ if ((nres) <= LUA_MULTRET && L->ci->top.p < L->top.p) \
|
{ if ((nres) == LUA_MULTRET && L->ci->top < L->top) L->ci->top = L->top; }
|
||||||
L->ci->top.p = L->top.p; }
|
|
||||||
|
|
||||||
|
#define api_checknelems(L,n) api_check(L, (n) < (L->top - L->ci->func), \
|
||||||
|
"not enough elements in the stack")
|
||||||
|
|
||||||
/* Ensure the stack has at least 'n' elements */
|
|
||||||
#define api_checknelems(L,n) \
|
|
||||||
api_check(L, (n) < (L->top.p - L->ci->func.p), \
|
|
||||||
"not enough elements in the stack")
|
|
||||||
|
|
||||||
|
|
||||||
/* Ensure the stack has at least 'n' elements to be popped. (Some
|
|
||||||
** functions only update a slot after checking it for popping, but that
|
|
||||||
** is only an optimization for a pop followed by a push.)
|
|
||||||
*/
|
|
||||||
#define api_checkpop(L,n) \
|
|
||||||
api_check(L, (n) < L->top.p - L->ci->func.p && \
|
|
||||||
L->tbclist.p < L->top.p - (n), \
|
|
||||||
"not enough free elements in the stack")
|
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
|||||||
@@ -1,5 +1,5 @@
|
|||||||
/*
|
/*
|
||||||
** $Id: lauxlib.h $
|
** $Id: lauxlib.h,v 1.131.1.1 2017/04/19 17:20:42 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
|
||||||
*/
|
*/
|
||||||
@@ -12,16 +12,9 @@
|
|||||||
#include <stddef.h>
|
#include <stddef.h>
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
|
|
||||||
#include "luaconf.h"
|
|
||||||
#include "lua.h"
|
#include "lua.h"
|
||||||
|
|
||||||
|
|
||||||
/* global table */
|
|
||||||
#define LUA_GNAME "_G"
|
|
||||||
|
|
||||||
|
|
||||||
typedef struct luaL_Buffer luaL_Buffer;
|
|
||||||
|
|
||||||
|
|
||||||
/* extra error code for 'luaL_loadfilex' */
|
/* extra error code for 'luaL_loadfilex' */
|
||||||
#define LUA_ERRFILE (LUA_ERRERR+1)
|
#define LUA_ERRFILE (LUA_ERRERR+1)
|
||||||
@@ -51,7 +44,6 @@ LUALIB_API int (luaL_getmetafield) (lua_State *L, int obj, const char *e);
|
|||||||
LUALIB_API int (luaL_callmeta) (lua_State *L, int obj, const char *e);
|
LUALIB_API int (luaL_callmeta) (lua_State *L, int obj, const char *e);
|
||||||
LUALIB_API const char *(luaL_tolstring) (lua_State *L, int idx, size_t *len);
|
LUALIB_API const char *(luaL_tolstring) (lua_State *L, int idx, size_t *len);
|
||||||
LUALIB_API int (luaL_argerror) (lua_State *L, int arg, const char *extramsg);
|
LUALIB_API int (luaL_argerror) (lua_State *L, int arg, const char *extramsg);
|
||||||
LUALIB_API int (luaL_typeerror) (lua_State *L, int arg, const char *tname);
|
|
||||||
LUALIB_API const char *(luaL_checklstring) (lua_State *L, int arg,
|
LUALIB_API const char *(luaL_checklstring) (lua_State *L, int arg,
|
||||||
size_t *l);
|
size_t *l);
|
||||||
LUALIB_API const char *(luaL_optlstring) (lua_State *L, int arg,
|
LUALIB_API const char *(luaL_optlstring) (lua_State *L, int arg,
|
||||||
@@ -81,10 +73,6 @@ LUALIB_API int (luaL_checkoption) (lua_State *L, int arg, const char *def,
|
|||||||
LUALIB_API int (luaL_fileresult) (lua_State *L, int stat, const char *fname);
|
LUALIB_API int (luaL_fileresult) (lua_State *L, int stat, const char *fname);
|
||||||
LUALIB_API int (luaL_execresult) (lua_State *L, int stat);
|
LUALIB_API int (luaL_execresult) (lua_State *L, int stat);
|
||||||
|
|
||||||
LUALIB_API void *luaL_alloc (void *ud, void *ptr, size_t osize,
|
|
||||||
size_t nsize);
|
|
||||||
|
|
||||||
|
|
||||||
/* predefined references */
|
/* predefined references */
|
||||||
#define LUA_NOREF (-2)
|
#define LUA_NOREF (-2)
|
||||||
#define LUA_REFNIL (-1)
|
#define LUA_REFNIL (-1)
|
||||||
@@ -103,14 +91,10 @@ LUALIB_API int (luaL_loadstring) (lua_State *L, const char *s);
|
|||||||
|
|
||||||
LUALIB_API lua_State *(luaL_newstate) (void);
|
LUALIB_API lua_State *(luaL_newstate) (void);
|
||||||
|
|
||||||
LUALIB_API unsigned luaL_makeseed (lua_State *L);
|
|
||||||
|
|
||||||
LUALIB_API lua_Integer (luaL_len) (lua_State *L, int idx);
|
LUALIB_API lua_Integer (luaL_len) (lua_State *L, int idx);
|
||||||
|
|
||||||
LUALIB_API void (luaL_addgsub) (luaL_Buffer *b, const char *s,
|
LUALIB_API const char *(luaL_gsub) (lua_State *L, const char *s, const char *p,
|
||||||
const char *p, const char *r);
|
const char *r);
|
||||||
LUALIB_API const char *(luaL_gsub) (lua_State *L, const char *s,
|
|
||||||
const char *p, const char *r);
|
|
||||||
|
|
||||||
LUALIB_API void (luaL_setfuncs) (lua_State *L, const luaL_Reg *l, int nup);
|
LUALIB_API void (luaL_setfuncs) (lua_State *L, const luaL_Reg *l, int nup);
|
||||||
|
|
||||||
@@ -136,11 +120,7 @@ LUALIB_API void (luaL_requiref) (lua_State *L, const char *modname,
|
|||||||
(luaL_checkversion(L), luaL_newlibtable(L,l), luaL_setfuncs(L,l,0))
|
(luaL_checkversion(L), luaL_newlibtable(L,l), luaL_setfuncs(L,l,0))
|
||||||
|
|
||||||
#define luaL_argcheck(L, cond,arg,extramsg) \
|
#define luaL_argcheck(L, cond,arg,extramsg) \
|
||||||
((void)(luai_likely(cond) || luaL_argerror(L, (arg), (extramsg))))
|
((void)((cond) || luaL_argerror(L, (arg), (extramsg))))
|
||||||
|
|
||||||
#define luaL_argexpected(L,cond,arg,tname) \
|
|
||||||
((void)(luai_likely(cond) || luaL_typeerror(L, (arg), (tname))))
|
|
||||||
|
|
||||||
#define luaL_checkstring(L,n) (luaL_checklstring(L, (n), NULL))
|
#define luaL_checkstring(L,n) (luaL_checklstring(L, (n), NULL))
|
||||||
#define luaL_optstring(L,n,d) (luaL_optlstring(L, (n), (d), NULL))
|
#define luaL_optstring(L,n,d) (luaL_optlstring(L, (n), (d), NULL))
|
||||||
|
|
||||||
@@ -159,43 +139,19 @@ LUALIB_API void (luaL_requiref) (lua_State *L, const char *modname,
|
|||||||
#define luaL_loadbuffer(L,s,sz,n) luaL_loadbufferx(L,s,sz,n,NULL)
|
#define luaL_loadbuffer(L,s,sz,n) luaL_loadbufferx(L,s,sz,n,NULL)
|
||||||
|
|
||||||
|
|
||||||
/*
|
|
||||||
** Perform arithmetic operations on lua_Integer values with wrap-around
|
|
||||||
** semantics, as the Lua core does.
|
|
||||||
*/
|
|
||||||
#define luaL_intop(op,v1,v2) \
|
|
||||||
((lua_Integer)((lua_Unsigned)(v1) op (lua_Unsigned)(v2)))
|
|
||||||
|
|
||||||
|
|
||||||
/* push the value used to represent failure/error */
|
|
||||||
#if defined(LUA_FAILISFALSE)
|
|
||||||
#define luaL_pushfail(L) lua_pushboolean(L, 0)
|
|
||||||
#else
|
|
||||||
#define luaL_pushfail(L) lua_pushnil(L)
|
|
||||||
#endif
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
/*
|
/*
|
||||||
** {======================================================
|
** {======================================================
|
||||||
** Generic Buffer manipulation
|
** Generic Buffer manipulation
|
||||||
** =======================================================
|
** =======================================================
|
||||||
*/
|
*/
|
||||||
|
|
||||||
struct luaL_Buffer {
|
typedef struct luaL_Buffer {
|
||||||
char *b; /* buffer address */
|
char *b; /* buffer address */
|
||||||
size_t size; /* buffer size */
|
size_t size; /* buffer size */
|
||||||
size_t n; /* number of characters in buffer */
|
size_t n; /* number of characters in buffer */
|
||||||
lua_State *L;
|
lua_State *L;
|
||||||
union {
|
char initb[LUAL_BUFFERSIZE]; /* initial buffer */
|
||||||
LUAI_MAXALIGN; /* ensure maximum alignment for buffer */
|
} luaL_Buffer;
|
||||||
char b[LUAL_BUFFERSIZE]; /* initial buffer */
|
|
||||||
} init;
|
|
||||||
};
|
|
||||||
|
|
||||||
|
|
||||||
#define luaL_bufflen(bf) ((bf)->n)
|
|
||||||
#define luaL_buffaddr(bf) ((bf)->b)
|
|
||||||
|
|
||||||
|
|
||||||
#define luaL_addchar(B,c) \
|
#define luaL_addchar(B,c) \
|
||||||
@@ -204,8 +160,6 @@ struct luaL_Buffer {
|
|||||||
|
|
||||||
#define luaL_addsize(B,s) ((B)->n += (s))
|
#define luaL_addsize(B,s) ((B)->n += (s))
|
||||||
|
|
||||||
#define luaL_buffsub(B,s) ((B)->n -= (s))
|
|
||||||
|
|
||||||
LUALIB_API void (luaL_buffinit) (lua_State *L, luaL_Buffer *B);
|
LUALIB_API void (luaL_buffinit) (lua_State *L, luaL_Buffer *B);
|
||||||
LUALIB_API char *(luaL_prepbuffsize) (luaL_Buffer *B, size_t sz);
|
LUALIB_API char *(luaL_prepbuffsize) (luaL_Buffer *B, size_t sz);
|
||||||
LUALIB_API void (luaL_addlstring) (luaL_Buffer *B, const char *s, size_t l);
|
LUALIB_API void (luaL_addlstring) (luaL_Buffer *B, const char *s, size_t l);
|
||||||
@@ -244,6 +198,45 @@ typedef struct luaL_Stream {
|
|||||||
/* }====================================================== */
|
/* }====================================================== */
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
/* compatibility with old module system */
|
||||||
|
#if defined(LUA_COMPAT_MODULE)
|
||||||
|
|
||||||
|
LUALIB_API void (luaL_pushmodule) (lua_State *L, const char *modname,
|
||||||
|
int sizehint);
|
||||||
|
LUALIB_API void (luaL_openlib) (lua_State *L, const char *libname,
|
||||||
|
const luaL_Reg *l, int nup);
|
||||||
|
|
||||||
|
#define luaL_register(L,n,l) (luaL_openlib(L,(n),(l),0))
|
||||||
|
|
||||||
|
#endif
|
||||||
|
|
||||||
|
|
||||||
|
/*
|
||||||
|
** {==================================================================
|
||||||
|
** "Abstraction Layer" for basic report of messages and errors
|
||||||
|
** ===================================================================
|
||||||
|
*/
|
||||||
|
|
||||||
|
/* print a string */
|
||||||
|
#if !defined(lua_writestring)
|
||||||
|
#define lua_writestring(s,l) fwrite((s), sizeof(char), (l), stdout)
|
||||||
|
#endif
|
||||||
|
|
||||||
|
/* print a newline and flush the output */
|
||||||
|
#if !defined(lua_writeline)
|
||||||
|
#define lua_writeline() (lua_writestring("\n", 1), fflush(stdout))
|
||||||
|
#endif
|
||||||
|
|
||||||
|
/* print an error message */
|
||||||
|
#if !defined(lua_writestringerror)
|
||||||
|
#define lua_writestringerror(s,p) \
|
||||||
|
(fprintf(stderr, (s), (p)), fflush(stderr))
|
||||||
|
#endif
|
||||||
|
|
||||||
|
/* }================================================================== */
|
||||||
|
|
||||||
|
|
||||||
/*
|
/*
|
||||||
** {============================================================
|
** {============================================================
|
||||||
** Compatibility with deprecated conversions
|
** Compatibility with deprecated conversions
|
||||||
|
|||||||
+74
-135
@@ -1,5 +1,5 @@
|
|||||||
/*
|
/*
|
||||||
** $Id: lbaselib.c $
|
** $Id: lbaselib.c,v 1.314.1.1 2017/04/19 17:39:34 roberto Exp $
|
||||||
** Basic library
|
** Basic library
|
||||||
** See Copyright Notice in lua.h
|
** See Copyright Notice in lua.h
|
||||||
*/
|
*/
|
||||||
@@ -19,18 +19,23 @@
|
|||||||
|
|
||||||
#include "lauxlib.h"
|
#include "lauxlib.h"
|
||||||
#include "lualib.h"
|
#include "lualib.h"
|
||||||
#include "llimits.h"
|
|
||||||
|
|
||||||
|
|
||||||
static int luaB_print (lua_State *L) {
|
static int luaB_print (lua_State *L) {
|
||||||
int n = lua_gettop(L); /* number of arguments */
|
int n = lua_gettop(L); /* number of arguments */
|
||||||
int i;
|
int i;
|
||||||
for (i = 1; i <= n; i++) { /* for each argument */
|
lua_getglobal(L, "tostring");
|
||||||
|
for (i=1; i<=n; i++) {
|
||||||
|
const char *s;
|
||||||
size_t l;
|
size_t l;
|
||||||
const char *s = luaL_tolstring(L, i, &l); /* convert it to string */
|
lua_pushvalue(L, -1); /* function to be called */
|
||||||
if (i > 1) /* not the first element? */
|
lua_pushvalue(L, i); /* value to print */
|
||||||
lua_writestring("\t", 1); /* add a tab before it */
|
lua_call(L, 1, 1);
|
||||||
lua_writestring(s, l); /* print it */
|
s = lua_tolstring(L, -1, &l); /* get result */
|
||||||
|
if (s == NULL)
|
||||||
|
return luaL_error(L, "'tostring' must return a string to 'print'");
|
||||||
|
if (i>1) lua_writestring("\t", 1);
|
||||||
|
lua_writestring(s, l);
|
||||||
lua_pop(L, 1); /* pop result */
|
lua_pop(L, 1); /* pop result */
|
||||||
}
|
}
|
||||||
lua_writeline();
|
lua_writeline();
|
||||||
@@ -38,42 +43,23 @@ static int luaB_print (lua_State *L) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/*
|
|
||||||
** Creates a warning with all given arguments.
|
|
||||||
** Check first for errors; otherwise an error may interrupt
|
|
||||||
** the composition of a warning, leaving it unfinished.
|
|
||||||
*/
|
|
||||||
static int luaB_warn (lua_State *L) {
|
|
||||||
int n = lua_gettop(L); /* number of arguments */
|
|
||||||
int i;
|
|
||||||
luaL_checkstring(L, 1); /* at least one argument */
|
|
||||||
for (i = 2; i <= n; i++)
|
|
||||||
luaL_checkstring(L, i); /* make sure all arguments are strings */
|
|
||||||
for (i = 1; i < n; i++) /* compose warning */
|
|
||||||
lua_warning(L, lua_tostring(L, i), 1);
|
|
||||||
lua_warning(L, lua_tostring(L, n), 0); /* close warning */
|
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
#define SPACECHARS " \f\n\r\t\v"
|
#define SPACECHARS " \f\n\r\t\v"
|
||||||
|
|
||||||
static const char *b_str2int (const char *s, unsigned base, lua_Integer *pn) {
|
static const char *b_str2int (const char *s, int base, lua_Integer *pn) {
|
||||||
lua_Unsigned n = 0;
|
lua_Unsigned n = 0;
|
||||||
int neg = 0;
|
int neg = 0;
|
||||||
s += strspn(s, SPACECHARS); /* skip initial spaces */
|
s += strspn(s, SPACECHARS); /* skip initial spaces */
|
||||||
if (*s == '-') { s++; neg = 1; } /* handle sign */
|
if (*s == '-') { s++; neg = 1; } /* handle signal */
|
||||||
else if (*s == '+') s++;
|
else if (*s == '+') s++;
|
||||||
if (!isalnum(cast_uchar(*s))) /* no digit? */
|
if (!isalnum((unsigned char)*s)) /* no digit? */
|
||||||
return NULL;
|
return NULL;
|
||||||
do {
|
do {
|
||||||
unsigned digit = cast_uint(isdigit(cast_uchar(*s))
|
int digit = (isdigit((unsigned char)*s)) ? *s - '0'
|
||||||
? *s - '0'
|
: (toupper((unsigned char)*s) - 'A') + 10;
|
||||||
: (toupper(cast_uchar(*s)) - 'A') + 10);
|
|
||||||
if (digit >= base) return NULL; /* invalid numeral */
|
if (digit >= base) return NULL; /* invalid numeral */
|
||||||
n = n * base + digit;
|
n = n * base + digit;
|
||||||
s++;
|
s++;
|
||||||
} while (isalnum(cast_uchar(*s)));
|
} while (isalnum((unsigned char)*s));
|
||||||
s += strspn(s, SPACECHARS); /* skip trailing spaces */
|
s += strspn(s, SPACECHARS); /* skip trailing spaces */
|
||||||
*pn = (lua_Integer)((neg) ? (0u - n) : n);
|
*pn = (lua_Integer)((neg) ? (0u - n) : n);
|
||||||
return s;
|
return s;
|
||||||
@@ -82,6 +68,7 @@ static const char *b_str2int (const char *s, unsigned base, lua_Integer *pn) {
|
|||||||
|
|
||||||
static int luaB_tonumber (lua_State *L) {
|
static int luaB_tonumber (lua_State *L) {
|
||||||
if (lua_isnoneornil(L, 2)) { /* standard conversion? */
|
if (lua_isnoneornil(L, 2)) { /* standard conversion? */
|
||||||
|
luaL_checkany(L, 1);
|
||||||
if (lua_type(L, 1) == LUA_TNUMBER) { /* already a number? */
|
if (lua_type(L, 1) == LUA_TNUMBER) { /* already a number? */
|
||||||
lua_settop(L, 1); /* yes; return it */
|
lua_settop(L, 1); /* yes; return it */
|
||||||
return 1;
|
return 1;
|
||||||
@@ -92,7 +79,6 @@ static int luaB_tonumber (lua_State *L) {
|
|||||||
if (s != NULL && lua_stringtonumber(L, s) == l + 1)
|
if (s != NULL && lua_stringtonumber(L, s) == l + 1)
|
||||||
return 1; /* successful conversion to number */
|
return 1; /* successful conversion to number */
|
||||||
/* else not a number */
|
/* else not a number */
|
||||||
luaL_checkany(L, 1); /* (but there must be some parameter) */
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
@@ -103,12 +89,12 @@ static int luaB_tonumber (lua_State *L) {
|
|||||||
luaL_checktype(L, 1, LUA_TSTRING); /* no numbers as strings */
|
luaL_checktype(L, 1, LUA_TSTRING); /* no numbers as strings */
|
||||||
s = lua_tolstring(L, 1, &l);
|
s = lua_tolstring(L, 1, &l);
|
||||||
luaL_argcheck(L, 2 <= base && base <= 36, 2, "base out of range");
|
luaL_argcheck(L, 2 <= base && base <= 36, 2, "base out of range");
|
||||||
if (b_str2int(s, cast_uint(base), &n) == s + l) {
|
if (b_str2int(s, (int)base, &n) == s + l) {
|
||||||
lua_pushinteger(L, n);
|
lua_pushinteger(L, n);
|
||||||
return 1;
|
return 1;
|
||||||
} /* else not a number */
|
} /* else not a number */
|
||||||
} /* else not a number */
|
} /* else not a number */
|
||||||
luaL_pushfail(L); /* not a number */
|
lua_pushnil(L); /* not a number */
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -139,8 +125,9 @@ static int luaB_getmetatable (lua_State *L) {
|
|||||||
static int luaB_setmetatable (lua_State *L) {
|
static int luaB_setmetatable (lua_State *L) {
|
||||||
int t = lua_type(L, 2);
|
int t = lua_type(L, 2);
|
||||||
luaL_checktype(L, 1, LUA_TTABLE);
|
luaL_checktype(L, 1, LUA_TTABLE);
|
||||||
luaL_argexpected(L, t == LUA_TNIL || t == LUA_TTABLE, 2, "nil or table");
|
luaL_argcheck(L, t == LUA_TNIL || t == LUA_TTABLE, 2,
|
||||||
if (l_unlikely(luaL_getmetafield(L, 1, "__metatable") != LUA_TNIL))
|
"nil or table expected");
|
||||||
|
if (luaL_getmetafield(L, 1, "__metatable") != LUA_TNIL)
|
||||||
return luaL_error(L, "cannot change a protected metatable");
|
return luaL_error(L, "cannot change a protected metatable");
|
||||||
lua_settop(L, 2);
|
lua_settop(L, 2);
|
||||||
lua_setmetatable(L, 1);
|
lua_setmetatable(L, 1);
|
||||||
@@ -158,9 +145,9 @@ static int luaB_rawequal (lua_State *L) {
|
|||||||
|
|
||||||
static int luaB_rawlen (lua_State *L) {
|
static int luaB_rawlen (lua_State *L) {
|
||||||
int t = lua_type(L, 1);
|
int t = lua_type(L, 1);
|
||||||
luaL_argexpected(L, t == LUA_TTABLE || t == LUA_TSTRING, 1,
|
luaL_argcheck(L, t == LUA_TTABLE || t == LUA_TSTRING, 1,
|
||||||
"table or string");
|
"table or string expected");
|
||||||
lua_pushinteger(L, l_castU2S(lua_rawlen(L, 1)));
|
lua_pushinteger(L, lua_rawlen(L, 1));
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -183,77 +170,31 @@ static int luaB_rawset (lua_State *L) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
static int pushmode (lua_State *L, int oldmode) {
|
|
||||||
if (oldmode == -1)
|
|
||||||
luaL_pushfail(L); /* invalid call to 'lua_gc' */
|
|
||||||
else
|
|
||||||
lua_pushstring(L, (oldmode == LUA_GCINC) ? "incremental"
|
|
||||||
: "generational");
|
|
||||||
return 1;
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
/*
|
|
||||||
** check whether call to 'lua_gc' was valid (not inside a finalizer)
|
|
||||||
*/
|
|
||||||
#define checkvalres(res) { if (res == -1) break; }
|
|
||||||
|
|
||||||
static int luaB_collectgarbage (lua_State *L) {
|
static int luaB_collectgarbage (lua_State *L) {
|
||||||
static const char *const opts[] = {"stop", "restart", "collect",
|
static const char *const opts[] = {"stop", "restart", "collect",
|
||||||
"count", "step", "isrunning", "generational", "incremental",
|
"count", "step", "setpause", "setstepmul",
|
||||||
"param", NULL};
|
"isrunning", NULL};
|
||||||
static const char optsnum[] = {LUA_GCSTOP, LUA_GCRESTART, LUA_GCCOLLECT,
|
static const int optsnum[] = {LUA_GCSTOP, LUA_GCRESTART, LUA_GCCOLLECT,
|
||||||
LUA_GCCOUNT, LUA_GCSTEP, LUA_GCISRUNNING, LUA_GCGEN, LUA_GCINC,
|
LUA_GCCOUNT, LUA_GCSTEP, LUA_GCSETPAUSE, LUA_GCSETSTEPMUL,
|
||||||
LUA_GCPARAM};
|
LUA_GCISRUNNING};
|
||||||
int o = optsnum[luaL_checkoption(L, 1, "collect", opts)];
|
int o = optsnum[luaL_checkoption(L, 1, "collect", opts)];
|
||||||
|
int ex = (int)luaL_optinteger(L, 2, 0);
|
||||||
|
int res = lua_gc(L, o, ex);
|
||||||
switch (o) {
|
switch (o) {
|
||||||
case LUA_GCCOUNT: {
|
case LUA_GCCOUNT: {
|
||||||
int k = lua_gc(L, o);
|
int b = lua_gc(L, LUA_GCCOUNTB, 0);
|
||||||
int b = lua_gc(L, LUA_GCCOUNTB);
|
lua_pushnumber(L, (lua_Number)res + ((lua_Number)b/1024));
|
||||||
checkvalres(k);
|
|
||||||
lua_pushnumber(L, (lua_Number)k + ((lua_Number)b/1024));
|
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
case LUA_GCSTEP: {
|
case LUA_GCSTEP: case LUA_GCISRUNNING: {
|
||||||
lua_Integer n = luaL_optinteger(L, 2, 0);
|
|
||||||
int res = lua_gc(L, o, cast_sizet(n));
|
|
||||||
checkvalres(res);
|
|
||||||
lua_pushboolean(L, res);
|
lua_pushboolean(L, res);
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
case LUA_GCISRUNNING: {
|
|
||||||
int res = lua_gc(L, o);
|
|
||||||
checkvalres(res);
|
|
||||||
lua_pushboolean(L, res);
|
|
||||||
return 1;
|
|
||||||
}
|
|
||||||
case LUA_GCGEN: {
|
|
||||||
return pushmode(L, lua_gc(L, o));
|
|
||||||
}
|
|
||||||
case LUA_GCINC: {
|
|
||||||
return pushmode(L, lua_gc(L, o));
|
|
||||||
}
|
|
||||||
case LUA_GCPARAM: {
|
|
||||||
static const char *const params[] = {
|
|
||||||
"minormul", "majorminor", "minormajor",
|
|
||||||
"pause", "stepmul", "stepsize", NULL};
|
|
||||||
static const char pnum[] = {
|
|
||||||
LUA_GCPMINORMUL, LUA_GCPMAJORMINOR, LUA_GCPMINORMAJOR,
|
|
||||||
LUA_GCPPAUSE, LUA_GCPSTEPMUL, LUA_GCPSTEPSIZE};
|
|
||||||
int p = pnum[luaL_checkoption(L, 2, NULL, params)];
|
|
||||||
lua_Integer value = luaL_optinteger(L, 3, -1);
|
|
||||||
lua_pushinteger(L, lua_gc(L, o, p, (int)value));
|
|
||||||
return 1;
|
|
||||||
}
|
|
||||||
default: {
|
default: {
|
||||||
int res = lua_gc(L, o);
|
|
||||||
checkvalres(res);
|
|
||||||
lua_pushinteger(L, res);
|
lua_pushinteger(L, res);
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
luaL_pushfail(L); /* invalid call (inside a finalizer) */
|
|
||||||
return 1;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@@ -265,6 +206,23 @@ static int luaB_type (lua_State *L) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
static int pairsmeta (lua_State *L, const char *method, int iszero,
|
||||||
|
lua_CFunction iter) {
|
||||||
|
luaL_checkany(L, 1);
|
||||||
|
if (luaL_getmetafield(L, 1, method) == LUA_TNIL) { /* no metamethod? */
|
||||||
|
lua_pushcfunction(L, iter); /* will return generator, */
|
||||||
|
lua_pushvalue(L, 1); /* state, */
|
||||||
|
if (iszero) lua_pushinteger(L, 0); /* and initial value */
|
||||||
|
else lua_pushnil(L);
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
lua_pushvalue(L, 1); /* argument 'self' to metamethod */
|
||||||
|
lua_call(L, 1, 3); /* get 3 values from metamethod */
|
||||||
|
}
|
||||||
|
return 3;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
static int luaB_next (lua_State *L) {
|
static int luaB_next (lua_State *L) {
|
||||||
luaL_checktype(L, 1, LUA_TTABLE);
|
luaL_checktype(L, 1, LUA_TTABLE);
|
||||||
lua_settop(L, 2); /* create a 2nd argument if there isn't one */
|
lua_settop(L, 2); /* create a 2nd argument if there isn't one */
|
||||||
@@ -277,24 +235,8 @@ static int luaB_next (lua_State *L) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
static int pairscont (lua_State *L, int status, lua_KContext k) {
|
|
||||||
(void)L; (void)status; (void)k; /* unused */
|
|
||||||
return 4; /* __pairs did all the work, just return its results */
|
|
||||||
}
|
|
||||||
|
|
||||||
static int luaB_pairs (lua_State *L) {
|
static int luaB_pairs (lua_State *L) {
|
||||||
luaL_checkany(L, 1);
|
return pairsmeta(L, "__pairs", 0, luaB_next);
|
||||||
if (luaL_getmetafield(L, 1, "__pairs") == LUA_TNIL) { /* no metamethod? */
|
|
||||||
lua_pushcfunction(L, luaB_next); /* will return generator and */
|
|
||||||
lua_pushvalue(L, 1); /* state */
|
|
||||||
lua_pushnil(L); /* initial value */
|
|
||||||
lua_pushnil(L); /* to-be-closed object */
|
|
||||||
}
|
|
||||||
else {
|
|
||||||
lua_pushvalue(L, 1); /* argument 'self' to metamethod */
|
|
||||||
lua_callk(L, 1, 4, 0, pairscont); /* get 4 values from metamethod */
|
|
||||||
}
|
|
||||||
return 4;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@@ -302,8 +244,7 @@ static int luaB_pairs (lua_State *L) {
|
|||||||
** Traversal function for 'ipairs'
|
** Traversal function for 'ipairs'
|
||||||
*/
|
*/
|
||||||
static int ipairsaux (lua_State *L) {
|
static int ipairsaux (lua_State *L) {
|
||||||
lua_Integer i = luaL_checkinteger(L, 2);
|
lua_Integer i = luaL_checkinteger(L, 2) + 1;
|
||||||
i = luaL_intop(+, i, 1);
|
|
||||||
lua_pushinteger(L, i);
|
lua_pushinteger(L, i);
|
||||||
return (lua_geti(L, 1, i) == LUA_TNIL) ? 1 : 2;
|
return (lua_geti(L, 1, i) == LUA_TNIL) ? 1 : 2;
|
||||||
}
|
}
|
||||||
@@ -314,16 +255,20 @@ static int ipairsaux (lua_State *L) {
|
|||||||
** (The given "table" may not be a table.)
|
** (The given "table" may not be a table.)
|
||||||
*/
|
*/
|
||||||
static int luaB_ipairs (lua_State *L) {
|
static int luaB_ipairs (lua_State *L) {
|
||||||
|
#if defined(LUA_COMPAT_IPAIRS)
|
||||||
|
return pairsmeta(L, "__ipairs", 1, ipairsaux);
|
||||||
|
#else
|
||||||
luaL_checkany(L, 1);
|
luaL_checkany(L, 1);
|
||||||
lua_pushcfunction(L, ipairsaux); /* iteration function */
|
lua_pushcfunction(L, ipairsaux); /* iteration function */
|
||||||
lua_pushvalue(L, 1); /* state */
|
lua_pushvalue(L, 1); /* state */
|
||||||
lua_pushinteger(L, 0); /* initial value */
|
lua_pushinteger(L, 0); /* initial value */
|
||||||
return 3;
|
return 3;
|
||||||
|
#endif
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
static int load_aux (lua_State *L, int status, int envidx) {
|
static int load_aux (lua_State *L, int status, int envidx) {
|
||||||
if (l_likely(status == LUA_OK)) {
|
if (status == LUA_OK) {
|
||||||
if (envidx != 0) { /* 'env' parameter? */
|
if (envidx != 0) { /* 'env' parameter? */
|
||||||
lua_pushvalue(L, envidx); /* environment for loaded function */
|
lua_pushvalue(L, envidx); /* environment for loaded function */
|
||||||
if (!lua_setupvalue(L, -2, 1)) /* set it as 1st upvalue */
|
if (!lua_setupvalue(L, -2, 1)) /* set it as 1st upvalue */
|
||||||
@@ -332,24 +277,16 @@ static int load_aux (lua_State *L, int status, int envidx) {
|
|||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
else { /* error (message is on top of the stack) */
|
else { /* error (message is on top of the stack) */
|
||||||
luaL_pushfail(L);
|
lua_pushnil(L);
|
||||||
lua_insert(L, -2); /* put before error message */
|
lua_insert(L, -2); /* put before error message */
|
||||||
return 2; /* return fail plus error message */
|
return 2; /* return nil plus error message */
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
static const char *getMode (lua_State *L, int idx) {
|
|
||||||
const char *mode = luaL_optstring(L, idx, "bt");
|
|
||||||
if (strchr(mode, 'B') != NULL) /* Lua code cannot use fixed buffers */
|
|
||||||
luaL_argerror(L, idx, "invalid mode");
|
|
||||||
return mode;
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
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 = getMode(L, 2);
|
const char *mode = luaL_optstring(L, 2, NULL);
|
||||||
int env = (!lua_isnone(L, 3) ? 3 : 0); /* 'env' index or 0 if no 'env' */
|
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);
|
||||||
return load_aux(L, status, env);
|
return load_aux(L, status, env);
|
||||||
@@ -387,7 +324,7 @@ static const char *generic_reader (lua_State *L, void *ud, size_t *size) {
|
|||||||
*size = 0;
|
*size = 0;
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
else if (l_unlikely(!lua_isstring(L, -1)))
|
else if (!lua_isstring(L, -1))
|
||||||
luaL_error(L, "reader function must return a string");
|
luaL_error(L, "reader function must return a string");
|
||||||
lua_replace(L, RESERVEDSLOT); /* save string in reserved slot */
|
lua_replace(L, RESERVEDSLOT); /* save string in reserved slot */
|
||||||
return lua_tolstring(L, RESERVEDSLOT, size);
|
return lua_tolstring(L, RESERVEDSLOT, size);
|
||||||
@@ -398,7 +335,7 @@ static int luaB_load (lua_State *L) {
|
|||||||
int status;
|
int status;
|
||||||
size_t l;
|
size_t l;
|
||||||
const char *s = lua_tolstring(L, 1, &l);
|
const char *s = lua_tolstring(L, 1, &l);
|
||||||
const char *mode = getMode(L, 3);
|
const char *mode = luaL_optstring(L, 3, "bt");
|
||||||
int env = (!lua_isnone(L, 4) ? 4 : 0); /* 'env' index or 0 if no 'env' */
|
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);
|
||||||
@@ -425,7 +362,7 @@ static int dofilecont (lua_State *L, int d1, lua_KContext d2) {
|
|||||||
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 (l_unlikely(luaL_loadfile(L, fname) != LUA_OK))
|
if (luaL_loadfile(L, fname) != LUA_OK)
|
||||||
return lua_error(L);
|
return lua_error(L);
|
||||||
lua_callk(L, 0, LUA_MULTRET, 0, dofilecont);
|
lua_callk(L, 0, LUA_MULTRET, 0, dofilecont);
|
||||||
return dofilecont(L, 0, 0);
|
return dofilecont(L, 0, 0);
|
||||||
@@ -433,7 +370,7 @@ static int luaB_dofile (lua_State *L) {
|
|||||||
|
|
||||||
|
|
||||||
static int luaB_assert (lua_State *L) {
|
static int luaB_assert (lua_State *L) {
|
||||||
if (l_likely(lua_toboolean(L, 1))) /* condition is true? */
|
if (lua_toboolean(L, 1)) /* condition is true? */
|
||||||
return lua_gettop(L); /* return all arguments */
|
return lua_gettop(L); /* return all arguments */
|
||||||
else { /* error */
|
else { /* error */
|
||||||
luaL_checkany(L, 1); /* there must be a condition */
|
luaL_checkany(L, 1); /* there must be a condition */
|
||||||
@@ -469,7 +406,7 @@ static int luaB_select (lua_State *L) {
|
|||||||
** ignored).
|
** ignored).
|
||||||
*/
|
*/
|
||||||
static int finishpcall (lua_State *L, int status, lua_KContext extra) {
|
static int finishpcall (lua_State *L, int status, lua_KContext extra) {
|
||||||
if (l_unlikely(status != LUA_OK && status != LUA_YIELD)) { /* error? */
|
if (status != LUA_OK && status != LUA_YIELD) { /* error? */
|
||||||
lua_pushboolean(L, 0); /* first result (false) */
|
lua_pushboolean(L, 0); /* first result (false) */
|
||||||
lua_pushvalue(L, -2); /* error message */
|
lua_pushvalue(L, -2); /* error message */
|
||||||
return 2; /* return false, msg */
|
return 2; /* return false, msg */
|
||||||
@@ -522,11 +459,13 @@ static const luaL_Reg base_funcs[] = {
|
|||||||
{"ipairs", luaB_ipairs},
|
{"ipairs", luaB_ipairs},
|
||||||
{"loadfile", luaB_loadfile},
|
{"loadfile", luaB_loadfile},
|
||||||
{"load", luaB_load},
|
{"load", luaB_load},
|
||||||
|
#if defined(LUA_COMPAT_LOADSTRING)
|
||||||
|
{"loadstring", luaB_load},
|
||||||
|
#endif
|
||||||
{"next", luaB_next},
|
{"next", luaB_next},
|
||||||
{"pairs", luaB_pairs},
|
{"pairs", luaB_pairs},
|
||||||
{"pcall", luaB_pcall},
|
{"pcall", luaB_pcall},
|
||||||
{"print", luaB_print},
|
{"print", luaB_print},
|
||||||
{"warn", luaB_warn},
|
|
||||||
{"rawequal", luaB_rawequal},
|
{"rawequal", luaB_rawequal},
|
||||||
{"rawlen", luaB_rawlen},
|
{"rawlen", luaB_rawlen},
|
||||||
{"rawget", luaB_rawget},
|
{"rawget", luaB_rawget},
|
||||||
@@ -538,7 +477,7 @@ static const luaL_Reg base_funcs[] = {
|
|||||||
{"type", luaB_type},
|
{"type", luaB_type},
|
||||||
{"xpcall", luaB_xpcall},
|
{"xpcall", luaB_xpcall},
|
||||||
/* placeholders */
|
/* placeholders */
|
||||||
{LUA_GNAME, NULL},
|
{"_G", NULL},
|
||||||
{"_VERSION", NULL},
|
{"_VERSION", NULL},
|
||||||
{NULL, NULL}
|
{NULL, NULL}
|
||||||
};
|
};
|
||||||
@@ -550,7 +489,7 @@ LUAMOD_API int luaopen_base (lua_State *L) {
|
|||||||
luaL_setfuncs(L, base_funcs, 0);
|
luaL_setfuncs(L, base_funcs, 0);
|
||||||
/* set global _G */
|
/* set global _G */
|
||||||
lua_pushvalue(L, -1);
|
lua_pushvalue(L, -1);
|
||||||
lua_setfield(L, -2, LUA_GNAME);
|
lua_setfield(L, -2, "_G");
|
||||||
/* set global _VERSION */
|
/* set global _VERSION */
|
||||||
lua_pushliteral(L, LUA_VERSION);
|
lua_pushliteral(L, LUA_VERSION);
|
||||||
lua_setfield(L, -2, "_VERSION");
|
lua_setfield(L, -2, "_VERSION");
|
||||||
|
|||||||
@@ -0,0 +1,233 @@
|
|||||||
|
/*
|
||||||
|
** $Id: lbitlib.c,v 1.30.1.1 2017/04/19 17:20:42 roberto Exp $
|
||||||
|
** Standard library for bitwise operations
|
||||||
|
** See Copyright Notice in lua.h
|
||||||
|
*/
|
||||||
|
|
||||||
|
#define lbitlib_c
|
||||||
|
#define LUA_LIB
|
||||||
|
|
||||||
|
#include "lprefix.h"
|
||||||
|
|
||||||
|
|
||||||
|
#include "lua.h"
|
||||||
|
|
||||||
|
#include "lauxlib.h"
|
||||||
|
#include "lualib.h"
|
||||||
|
|
||||||
|
|
||||||
|
#if defined(LUA_COMPAT_BITLIB) /* { */
|
||||||
|
|
||||||
|
|
||||||
|
#define pushunsigned(L,n) lua_pushinteger(L, (lua_Integer)(n))
|
||||||
|
#define checkunsigned(L,i) ((lua_Unsigned)luaL_checkinteger(L,i))
|
||||||
|
|
||||||
|
|
||||||
|
/* number of bits to consider in a number */
|
||||||
|
#if !defined(LUA_NBITS)
|
||||||
|
#define LUA_NBITS 32
|
||||||
|
#endif
|
||||||
|
|
||||||
|
|
||||||
|
/*
|
||||||
|
** a lua_Unsigned with its first LUA_NBITS bits equal to 1. (Shift must
|
||||||
|
** be made in two parts to avoid problems when LUA_NBITS is equal to the
|
||||||
|
** number of bits in a lua_Unsigned.)
|
||||||
|
*/
|
||||||
|
#define ALLONES (~(((~(lua_Unsigned)0) << (LUA_NBITS - 1)) << 1))
|
||||||
|
|
||||||
|
|
||||||
|
/* macro to trim extra bits */
|
||||||
|
#define trim(x) ((x) & ALLONES)
|
||||||
|
|
||||||
|
|
||||||
|
/* builds a number with 'n' ones (1 <= n <= LUA_NBITS) */
|
||||||
|
#define mask(n) (~((ALLONES << 1) << ((n) - 1)))
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
static lua_Unsigned andaux (lua_State *L) {
|
||||||
|
int i, n = lua_gettop(L);
|
||||||
|
lua_Unsigned r = ~(lua_Unsigned)0;
|
||||||
|
for (i = 1; i <= n; i++)
|
||||||
|
r &= checkunsigned(L, i);
|
||||||
|
return trim(r);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
static int b_and (lua_State *L) {
|
||||||
|
lua_Unsigned r = andaux(L);
|
||||||
|
pushunsigned(L, r);
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
static int b_test (lua_State *L) {
|
||||||
|
lua_Unsigned r = andaux(L);
|
||||||
|
lua_pushboolean(L, r != 0);
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
static int b_or (lua_State *L) {
|
||||||
|
int i, n = lua_gettop(L);
|
||||||
|
lua_Unsigned r = 0;
|
||||||
|
for (i = 1; i <= n; i++)
|
||||||
|
r |= checkunsigned(L, i);
|
||||||
|
pushunsigned(L, trim(r));
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
static int b_xor (lua_State *L) {
|
||||||
|
int i, n = lua_gettop(L);
|
||||||
|
lua_Unsigned r = 0;
|
||||||
|
for (i = 1; i <= n; i++)
|
||||||
|
r ^= checkunsigned(L, i);
|
||||||
|
pushunsigned(L, trim(r));
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
static int b_not (lua_State *L) {
|
||||||
|
lua_Unsigned r = ~checkunsigned(L, 1);
|
||||||
|
pushunsigned(L, trim(r));
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
static int b_shift (lua_State *L, lua_Unsigned r, lua_Integer i) {
|
||||||
|
if (i < 0) { /* shift right? */
|
||||||
|
i = -i;
|
||||||
|
r = trim(r);
|
||||||
|
if (i >= LUA_NBITS) r = 0;
|
||||||
|
else r >>= i;
|
||||||
|
}
|
||||||
|
else { /* shift left */
|
||||||
|
if (i >= LUA_NBITS) r = 0;
|
||||||
|
else r <<= i;
|
||||||
|
r = trim(r);
|
||||||
|
}
|
||||||
|
pushunsigned(L, r);
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
static int b_lshift (lua_State *L) {
|
||||||
|
return b_shift(L, checkunsigned(L, 1), luaL_checkinteger(L, 2));
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
static int b_rshift (lua_State *L) {
|
||||||
|
return b_shift(L, checkunsigned(L, 1), -luaL_checkinteger(L, 2));
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
static int b_arshift (lua_State *L) {
|
||||||
|
lua_Unsigned r = checkunsigned(L, 1);
|
||||||
|
lua_Integer i = luaL_checkinteger(L, 2);
|
||||||
|
if (i < 0 || !(r & ((lua_Unsigned)1 << (LUA_NBITS - 1))))
|
||||||
|
return b_shift(L, r, -i);
|
||||||
|
else { /* arithmetic shift for 'negative' number */
|
||||||
|
if (i >= LUA_NBITS) r = ALLONES;
|
||||||
|
else
|
||||||
|
r = trim((r >> i) | ~(trim(~(lua_Unsigned)0) >> i)); /* add signal bit */
|
||||||
|
pushunsigned(L, r);
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
static int b_rot (lua_State *L, lua_Integer d) {
|
||||||
|
lua_Unsigned r = checkunsigned(L, 1);
|
||||||
|
int i = d & (LUA_NBITS - 1); /* i = d % NBITS */
|
||||||
|
r = trim(r);
|
||||||
|
if (i != 0) /* avoid undefined shift of LUA_NBITS when i == 0 */
|
||||||
|
r = (r << i) | (r >> (LUA_NBITS - i));
|
||||||
|
pushunsigned(L, trim(r));
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
static int b_lrot (lua_State *L) {
|
||||||
|
return b_rot(L, luaL_checkinteger(L, 2));
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
static int b_rrot (lua_State *L) {
|
||||||
|
return b_rot(L, -luaL_checkinteger(L, 2));
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/*
|
||||||
|
** get field and width arguments for field-manipulation functions,
|
||||||
|
** 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) {
|
||||||
|
lua_Integer f = luaL_checkinteger(L, farg);
|
||||||
|
lua_Integer w = luaL_optinteger(L, farg + 1, 1);
|
||||||
|
luaL_argcheck(L, 0 <= f, farg, "field cannot be negative");
|
||||||
|
luaL_argcheck(L, 0 < w, farg + 1, "width must be positive");
|
||||||
|
if (f + w > LUA_NBITS)
|
||||||
|
luaL_error(L, "trying to access non-existent bits");
|
||||||
|
*width = (int)w;
|
||||||
|
return (int)f;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
static int b_extract (lua_State *L) {
|
||||||
|
int w;
|
||||||
|
lua_Unsigned r = trim(checkunsigned(L, 1));
|
||||||
|
int f = fieldargs(L, 2, &w);
|
||||||
|
r = (r >> f) & mask(w);
|
||||||
|
pushunsigned(L, r);
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
static int b_replace (lua_State *L) {
|
||||||
|
int w;
|
||||||
|
lua_Unsigned r = trim(checkunsigned(L, 1));
|
||||||
|
lua_Unsigned v = trim(checkunsigned(L, 2));
|
||||||
|
int f = fieldargs(L, 3, &w);
|
||||||
|
lua_Unsigned m = mask(w);
|
||||||
|
r = (r & ~(m << f)) | ((v & m) << f);
|
||||||
|
pushunsigned(L, r);
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
static const luaL_Reg bitlib[] = {
|
||||||
|
{"arshift", b_arshift},
|
||||||
|
{"band", b_and},
|
||||||
|
{"bnot", b_not},
|
||||||
|
{"bor", b_or},
|
||||||
|
{"bxor", b_xor},
|
||||||
|
{"btest", b_test},
|
||||||
|
{"extract", b_extract},
|
||||||
|
{"lrotate", b_lrot},
|
||||||
|
{"lshift", b_lshift},
|
||||||
|
{"replace", b_replace},
|
||||||
|
{"rrotate", b_rrot},
|
||||||
|
{"rshift", b_rshift},
|
||||||
|
{NULL, NULL}
|
||||||
|
};
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
LUAMOD_API int luaopen_bit32 (lua_State *L) {
|
||||||
|
luaL_newlib(L, bitlib);
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
#else /* }{ */
|
||||||
|
|
||||||
|
|
||||||
|
LUAMOD_API int luaopen_bit32 (lua_State *L) {
|
||||||
|
return luaL_error(L, "library 'bit32' has been deprecated");
|
||||||
|
}
|
||||||
|
|
||||||
|
#endif /* } */
|
||||||
@@ -1,5 +1,5 @@
|
|||||||
/*
|
/*
|
||||||
** $Id: lcode.h $
|
** $Id: lcode.h,v 1.64.1.1 2017/04/19 17:20:42 roberto Exp $
|
||||||
** Code generator for Lua
|
** Code generator for Lua
|
||||||
** See Copyright Notice in lua.h
|
** See Copyright Notice in lua.h
|
||||||
*/
|
*/
|
||||||
@@ -24,64 +24,50 @@
|
|||||||
** grep "ORDER OPR" if you change these enums (ORDER OP)
|
** grep "ORDER OPR" if you change these enums (ORDER OP)
|
||||||
*/
|
*/
|
||||||
typedef enum BinOpr {
|
typedef enum BinOpr {
|
||||||
/* arithmetic operators */
|
|
||||||
OPR_ADD, OPR_SUB, OPR_MUL, OPR_MOD, OPR_POW,
|
OPR_ADD, OPR_SUB, OPR_MUL, OPR_MOD, OPR_POW,
|
||||||
OPR_DIV, OPR_IDIV,
|
OPR_DIV,
|
||||||
/* bitwise operators */
|
OPR_IDIV,
|
||||||
OPR_BAND, OPR_BOR, OPR_BXOR,
|
OPR_BAND, OPR_BOR, OPR_BXOR,
|
||||||
OPR_SHL, OPR_SHR,
|
OPR_SHL, OPR_SHR,
|
||||||
/* string operator */
|
|
||||||
OPR_CONCAT,
|
OPR_CONCAT,
|
||||||
/* comparison operators */
|
|
||||||
OPR_EQ, OPR_LT, OPR_LE,
|
OPR_EQ, OPR_LT, OPR_LE,
|
||||||
OPR_NE, OPR_GT, OPR_GE,
|
OPR_NE, OPR_GT, OPR_GE,
|
||||||
/* logical operators */
|
|
||||||
OPR_AND, OPR_OR,
|
OPR_AND, OPR_OR,
|
||||||
OPR_NOBINOPR
|
OPR_NOBINOPR
|
||||||
} BinOpr;
|
} BinOpr;
|
||||||
|
|
||||||
|
|
||||||
/* true if operation is foldable (that is, it is arithmetic or bitwise) */
|
|
||||||
#define foldbinop(op) ((op) <= OPR_SHR)
|
|
||||||
|
|
||||||
|
|
||||||
#define luaK_codeABC(fs,o,a,b,c) luaK_codeABCk(fs,o,a,b,c,0)
|
|
||||||
|
|
||||||
|
|
||||||
typedef enum UnOpr { OPR_MINUS, OPR_BNOT, OPR_NOT, OPR_LEN, OPR_NOUNOPR } UnOpr;
|
typedef enum UnOpr { OPR_MINUS, OPR_BNOT, OPR_NOT, OPR_LEN, OPR_NOUNOPR } UnOpr;
|
||||||
|
|
||||||
|
|
||||||
/* get (pointer to) instruction of given 'expdesc' */
|
/* get (pointer to) instruction of given 'expdesc' */
|
||||||
#define getinstruction(fs,e) ((fs)->f->code[(e)->u.info])
|
#define getinstruction(fs,e) ((fs)->f->code[(e)->u.info])
|
||||||
|
|
||||||
|
#define luaK_codeAsBx(fs,o,A,sBx) luaK_codeABx(fs,o,A,(sBx)+MAXARG_sBx)
|
||||||
|
|
||||||
#define luaK_setmultret(fs,e) luaK_setreturns(fs, e, LUA_MULTRET)
|
#define luaK_setmultret(fs,e) luaK_setreturns(fs, e, LUA_MULTRET)
|
||||||
|
|
||||||
#define luaK_jumpto(fs,t) luaK_patchlist(fs, luaK_jump(fs), t)
|
#define luaK_jumpto(fs,t) luaK_patchlist(fs, luaK_jump(fs), t)
|
||||||
|
|
||||||
LUAI_FUNC int luaK_code (FuncState *fs, Instruction i);
|
LUAI_FUNC int luaK_codeABx (FuncState *fs, OpCode o, int A, unsigned int Bx);
|
||||||
LUAI_FUNC int luaK_codeABx (FuncState *fs, OpCode o, int A, int Bx);
|
LUAI_FUNC int luaK_codeABC (FuncState *fs, OpCode o, int A, int B, int C);
|
||||||
LUAI_FUNC int luaK_codeABCk (FuncState *fs, OpCode o, int A, int B, int C,
|
LUAI_FUNC int luaK_codek (FuncState *fs, int reg, int k);
|
||||||
int k);
|
|
||||||
LUAI_FUNC int luaK_codevABCk (FuncState *fs, OpCode o, int A, int B, int C,
|
|
||||||
int k);
|
|
||||||
LUAI_FUNC int luaK_exp2const (FuncState *fs, const expdesc *e, TValue *v);
|
|
||||||
LUAI_FUNC void luaK_fixline (FuncState *fs, int line);
|
LUAI_FUNC void luaK_fixline (FuncState *fs, int line);
|
||||||
LUAI_FUNC void luaK_nil (FuncState *fs, int from, int n);
|
LUAI_FUNC void luaK_nil (FuncState *fs, int from, int n);
|
||||||
LUAI_FUNC void luaK_codecheckglobal (FuncState *fs, expdesc *var, int k,
|
|
||||||
int line);
|
|
||||||
LUAI_FUNC void luaK_reserveregs (FuncState *fs, int n);
|
LUAI_FUNC void luaK_reserveregs (FuncState *fs, int n);
|
||||||
LUAI_FUNC void luaK_checkstack (FuncState *fs, int n);
|
LUAI_FUNC void luaK_checkstack (FuncState *fs, int n);
|
||||||
LUAI_FUNC void luaK_int (FuncState *fs, int reg, lua_Integer n);
|
LUAI_FUNC int luaK_stringK (FuncState *fs, TString *s);
|
||||||
LUAI_FUNC void luaK_vapar2local (FuncState *fs, expdesc *var);
|
LUAI_FUNC int luaK_intK (FuncState *fs, lua_Integer n);
|
||||||
LUAI_FUNC void luaK_dischargevars (FuncState *fs, expdesc *e);
|
LUAI_FUNC void luaK_dischargevars (FuncState *fs, expdesc *e);
|
||||||
LUAI_FUNC int luaK_exp2anyreg (FuncState *fs, expdesc *e);
|
LUAI_FUNC int luaK_exp2anyreg (FuncState *fs, expdesc *e);
|
||||||
LUAI_FUNC void luaK_exp2anyregup (FuncState *fs, expdesc *e);
|
LUAI_FUNC void luaK_exp2anyregup (FuncState *fs, expdesc *e);
|
||||||
LUAI_FUNC void luaK_exp2nextreg (FuncState *fs, expdesc *e);
|
LUAI_FUNC void luaK_exp2nextreg (FuncState *fs, expdesc *e);
|
||||||
LUAI_FUNC void luaK_exp2val (FuncState *fs, expdesc *e);
|
LUAI_FUNC void luaK_exp2val (FuncState *fs, expdesc *e);
|
||||||
|
LUAI_FUNC int luaK_exp2RK (FuncState *fs, expdesc *e);
|
||||||
LUAI_FUNC void luaK_self (FuncState *fs, expdesc *e, expdesc *key);
|
LUAI_FUNC void luaK_self (FuncState *fs, expdesc *e, expdesc *key);
|
||||||
LUAI_FUNC void luaK_indexed (FuncState *fs, expdesc *t, expdesc *k);
|
LUAI_FUNC void luaK_indexed (FuncState *fs, expdesc *t, expdesc *k);
|
||||||
LUAI_FUNC void luaK_goiftrue (FuncState *fs, expdesc *e);
|
LUAI_FUNC void luaK_goiftrue (FuncState *fs, expdesc *e);
|
||||||
|
LUAI_FUNC void luaK_goiffalse (FuncState *fs, expdesc *e);
|
||||||
LUAI_FUNC void luaK_storevar (FuncState *fs, expdesc *var, expdesc *e);
|
LUAI_FUNC void luaK_storevar (FuncState *fs, expdesc *var, expdesc *e);
|
||||||
LUAI_FUNC void luaK_setreturns (FuncState *fs, expdesc *e, int nresults);
|
LUAI_FUNC void luaK_setreturns (FuncState *fs, expdesc *e, int nresults);
|
||||||
LUAI_FUNC void luaK_setoneret (FuncState *fs, expdesc *e);
|
LUAI_FUNC void luaK_setoneret (FuncState *fs, expdesc *e);
|
||||||
@@ -89,17 +75,14 @@ LUAI_FUNC int luaK_jump (FuncState *fs);
|
|||||||
LUAI_FUNC void luaK_ret (FuncState *fs, int first, int nret);
|
LUAI_FUNC void luaK_ret (FuncState *fs, int first, int nret);
|
||||||
LUAI_FUNC void luaK_patchlist (FuncState *fs, int list, int target);
|
LUAI_FUNC void luaK_patchlist (FuncState *fs, int list, int target);
|
||||||
LUAI_FUNC void luaK_patchtohere (FuncState *fs, int list);
|
LUAI_FUNC void luaK_patchtohere (FuncState *fs, int list);
|
||||||
|
LUAI_FUNC void luaK_patchclose (FuncState *fs, int list, int level);
|
||||||
LUAI_FUNC void luaK_concat (FuncState *fs, int *l1, int l2);
|
LUAI_FUNC void luaK_concat (FuncState *fs, int *l1, int l2);
|
||||||
LUAI_FUNC int luaK_getlabel (FuncState *fs);
|
LUAI_FUNC int luaK_getlabel (FuncState *fs);
|
||||||
LUAI_FUNC void luaK_prefix (FuncState *fs, UnOpr op, expdesc *v, int line);
|
LUAI_FUNC void luaK_prefix (FuncState *fs, UnOpr op, expdesc *v, int line);
|
||||||
LUAI_FUNC void luaK_infix (FuncState *fs, BinOpr op, expdesc *v);
|
LUAI_FUNC void luaK_infix (FuncState *fs, BinOpr op, expdesc *v);
|
||||||
LUAI_FUNC void luaK_posfix (FuncState *fs, BinOpr op, expdesc *v1,
|
LUAI_FUNC void luaK_posfix (FuncState *fs, BinOpr op, expdesc *v1,
|
||||||
expdesc *v2, int line);
|
expdesc *v2, int line);
|
||||||
LUAI_FUNC void luaK_settablesize (FuncState *fs, int pc,
|
|
||||||
int ra, int asize, int hsize);
|
|
||||||
LUAI_FUNC void luaK_setlist (FuncState *fs, int base, int nelems, int tostore);
|
LUAI_FUNC void luaK_setlist (FuncState *fs, int base, int nelems, int tostore);
|
||||||
LUAI_FUNC void luaK_finish (FuncState *fs);
|
|
||||||
LUAI_FUNC l_noret luaK_semerror (LexState *ls, const char *fmt, ...);
|
|
||||||
|
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
|||||||
+29
-86
@@ -1,5 +1,5 @@
|
|||||||
/*
|
/*
|
||||||
** $Id: lcorolib.c $
|
** $Id: lcorolib.c,v 1.10.1.1 2017/04/19 17:20:42 roberto Exp $
|
||||||
** Coroutine Library
|
** Coroutine Library
|
||||||
** See Copyright Notice in lua.h
|
** See Copyright Notice in lua.h
|
||||||
*/
|
*/
|
||||||
@@ -16,30 +16,30 @@
|
|||||||
|
|
||||||
#include "lauxlib.h"
|
#include "lauxlib.h"
|
||||||
#include "lualib.h"
|
#include "lualib.h"
|
||||||
#include "llimits.h"
|
|
||||||
|
|
||||||
|
|
||||||
static lua_State *getco (lua_State *L) {
|
static lua_State *getco (lua_State *L) {
|
||||||
lua_State *co = lua_tothread(L, 1);
|
lua_State *co = lua_tothread(L, 1);
|
||||||
luaL_argexpected(L, co, 1, "thread");
|
luaL_argcheck(L, co, 1, "thread expected");
|
||||||
return co;
|
return co;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/*
|
|
||||||
** Resumes a coroutine. Returns the number of results for non-error
|
|
||||||
** cases or -1 for errors.
|
|
||||||
*/
|
|
||||||
static int auxresume (lua_State *L, lua_State *co, int narg) {
|
static int auxresume (lua_State *L, lua_State *co, int narg) {
|
||||||
int status, nres;
|
int status;
|
||||||
if (l_unlikely(!lua_checkstack(co, narg))) {
|
if (!lua_checkstack(co, narg)) {
|
||||||
lua_pushliteral(L, "too many arguments to resume");
|
lua_pushliteral(L, "too many arguments to resume");
|
||||||
return -1; /* error flag */
|
return -1; /* error flag */
|
||||||
}
|
}
|
||||||
|
if (lua_status(co) == LUA_OK && lua_gettop(co) == 0) {
|
||||||
|
lua_pushliteral(L, "cannot resume dead coroutine");
|
||||||
|
return -1; /* error flag */
|
||||||
|
}
|
||||||
lua_xmove(L, co, narg);
|
lua_xmove(L, co, narg);
|
||||||
status = lua_resume(co, L, narg, &nres);
|
status = lua_resume(co, L, narg);
|
||||||
if (l_likely(status == LUA_OK || status == LUA_YIELD)) {
|
if (status == LUA_OK || status == LUA_YIELD) {
|
||||||
if (l_unlikely(!lua_checkstack(L, nres + 1))) {
|
int nres = lua_gettop(co);
|
||||||
|
if (!lua_checkstack(L, nres + 1)) {
|
||||||
lua_pop(co, nres); /* remove results anyway */
|
lua_pop(co, nres); /* remove results anyway */
|
||||||
lua_pushliteral(L, "too many results to resume");
|
lua_pushliteral(L, "too many results to resume");
|
||||||
return -1; /* error flag */
|
return -1; /* error flag */
|
||||||
@@ -58,7 +58,7 @@ static int luaB_coresume (lua_State *L) {
|
|||||||
lua_State *co = getco(L);
|
lua_State *co = getco(L);
|
||||||
int r;
|
int r;
|
||||||
r = auxresume(L, co, lua_gettop(L) - 1);
|
r = auxresume(L, co, lua_gettop(L) - 1);
|
||||||
if (l_unlikely(r < 0)) {
|
if (r < 0) {
|
||||||
lua_pushboolean(L, 0);
|
lua_pushboolean(L, 0);
|
||||||
lua_insert(L, -2);
|
lua_insert(L, -2);
|
||||||
return 2; /* return false + error message */
|
return 2; /* return false + error message */
|
||||||
@@ -74,16 +74,9 @@ static int luaB_coresume (lua_State *L) {
|
|||||||
static int luaB_auxwrap (lua_State *L) {
|
static int luaB_auxwrap (lua_State *L) {
|
||||||
lua_State *co = lua_tothread(L, lua_upvalueindex(1));
|
lua_State *co = lua_tothread(L, lua_upvalueindex(1));
|
||||||
int r = auxresume(L, co, lua_gettop(L));
|
int r = auxresume(L, co, lua_gettop(L));
|
||||||
if (l_unlikely(r < 0)) { /* error? */
|
if (r < 0) {
|
||||||
int stat = lua_status(co);
|
if (lua_type(L, -1) == LUA_TSTRING) { /* error object is a string? */
|
||||||
if (stat != LUA_OK && stat != LUA_YIELD) { /* error in the coroutine? */
|
luaL_where(L, 1); /* add extra info */
|
||||||
stat = lua_closethread(co, L); /* close its tbc variables */
|
|
||||||
lua_assert(stat != LUA_OK);
|
|
||||||
lua_xmove(co, L, 1); /* move error message to the caller */
|
|
||||||
}
|
|
||||||
if (stat != LUA_ERRMEM && /* not a memory error and ... */
|
|
||||||
lua_type(L, -1) == LUA_TSTRING) { /* ... error object is a string? */
|
|
||||||
luaL_where(L, 1); /* add extra info, if available */
|
|
||||||
lua_insert(L, -2);
|
lua_insert(L, -2);
|
||||||
lua_concat(L, 2);
|
lua_concat(L, 2);
|
||||||
}
|
}
|
||||||
@@ -115,53 +108,35 @@ static int luaB_yield (lua_State *L) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
#define COS_RUN 0
|
static int luaB_costatus (lua_State *L) {
|
||||||
#define COS_DEAD 1
|
lua_State *co = getco(L);
|
||||||
#define COS_YIELD 2
|
if (L == co) lua_pushliteral(L, "running");
|
||||||
#define COS_NORM 3
|
|
||||||
|
|
||||||
|
|
||||||
static const char *const statname[] =
|
|
||||||
{"running", "dead", "suspended", "normal"};
|
|
||||||
|
|
||||||
|
|
||||||
static int auxstatus (lua_State *L, lua_State *co) {
|
|
||||||
if (L == co) return COS_RUN;
|
|
||||||
else {
|
else {
|
||||||
switch (lua_status(co)) {
|
switch (lua_status(co)) {
|
||||||
case LUA_YIELD:
|
case LUA_YIELD:
|
||||||
return COS_YIELD;
|
lua_pushliteral(L, "suspended");
|
||||||
|
break;
|
||||||
case LUA_OK: {
|
case LUA_OK: {
|
||||||
lua_Debug ar;
|
lua_Debug ar;
|
||||||
if (lua_getstack(co, 0, &ar)) /* does it have frames? */
|
if (lua_getstack(co, 0, &ar) > 0) /* does it have frames? */
|
||||||
return COS_NORM; /* it is running */
|
lua_pushliteral(L, "normal"); /* it is running */
|
||||||
else if (lua_gettop(co) == 0)
|
else if (lua_gettop(co) == 0)
|
||||||
return COS_DEAD;
|
lua_pushliteral(L, "dead");
|
||||||
else
|
else
|
||||||
return COS_YIELD; /* initial state */
|
lua_pushliteral(L, "suspended"); /* initial state */
|
||||||
|
break;
|
||||||
}
|
}
|
||||||
default: /* some error occurred */
|
default: /* some error occurred */
|
||||||
return COS_DEAD;
|
lua_pushliteral(L, "dead");
|
||||||
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
static int luaB_costatus (lua_State *L) {
|
|
||||||
lua_State *co = getco(L);
|
|
||||||
lua_pushstring(L, statname[auxstatus(L, co)]);
|
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
static lua_State *getoptco (lua_State *L) {
|
|
||||||
return (lua_isnone(L, 1) ? L : getco(L));
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
static int luaB_yieldable (lua_State *L) {
|
static int luaB_yieldable (lua_State *L) {
|
||||||
lua_State *co = getoptco(L);
|
lua_pushboolean(L, lua_isyieldable(L));
|
||||||
lua_pushboolean(L, lua_isyieldable(co));
|
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -173,37 +148,6 @@ static int luaB_corunning (lua_State *L) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
static int luaB_close (lua_State *L) {
|
|
||||||
lua_State *co = getoptco(L);
|
|
||||||
int status = auxstatus(L, co);
|
|
||||||
switch (status) {
|
|
||||||
case COS_DEAD: case COS_YIELD: {
|
|
||||||
status = lua_closethread(co, L);
|
|
||||||
if (status == LUA_OK) {
|
|
||||||
lua_pushboolean(L, 1);
|
|
||||||
return 1;
|
|
||||||
}
|
|
||||||
else {
|
|
||||||
lua_pushboolean(L, 0);
|
|
||||||
lua_xmove(co, L, 1); /* move error message */
|
|
||||||
return 2;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
case COS_NORM:
|
|
||||||
return luaL_error(L, "cannot close a %s coroutine", statname[status]);
|
|
||||||
case COS_RUN:
|
|
||||||
lua_geti(L, LUA_REGISTRYINDEX, LUA_RIDX_MAINTHREAD); /* get main */
|
|
||||||
if (lua_tothread(L, -1) == co)
|
|
||||||
return luaL_error(L, "cannot close main thread");
|
|
||||||
lua_closethread(co, L); /* close itself */
|
|
||||||
/* previous call does not return *//* FALLTHROUGH */
|
|
||||||
default:
|
|
||||||
lua_assert(0);
|
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
static const luaL_Reg co_funcs[] = {
|
static const luaL_Reg co_funcs[] = {
|
||||||
{"create", luaB_cocreate},
|
{"create", luaB_cocreate},
|
||||||
{"resume", luaB_coresume},
|
{"resume", luaB_coresume},
|
||||||
@@ -212,7 +156,6 @@ static const luaL_Reg co_funcs[] = {
|
|||||||
{"wrap", luaB_cowrap},
|
{"wrap", luaB_cowrap},
|
||||||
{"yield", luaB_yield},
|
{"yield", luaB_yield},
|
||||||
{"isyieldable", luaB_yieldable},
|
{"isyieldable", luaB_yieldable},
|
||||||
{"close", luaB_close},
|
|
||||||
{NULL, NULL}
|
{NULL, NULL}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|||||||
@@ -1,5 +1,5 @@
|
|||||||
/*
|
/*
|
||||||
** $Id: lctype.c $
|
** $Id: lctype.c,v 1.12.1.1 2017/04/19 17:20:42 roberto Exp $
|
||||||
** 'ctype' functions for Lua
|
** 'ctype' functions for Lua
|
||||||
** See Copyright Notice in lua.h
|
** See Copyright Notice in lua.h
|
||||||
*/
|
*/
|
||||||
@@ -16,15 +16,6 @@
|
|||||||
|
|
||||||
#include <limits.h>
|
#include <limits.h>
|
||||||
|
|
||||||
|
|
||||||
#if defined (LUA_UCID) /* accept UniCode IDentifiers? */
|
|
||||||
/* consider all non-ASCII codepoints to be alphabetic */
|
|
||||||
#define NONA 0x01
|
|
||||||
#else
|
|
||||||
#define NONA 0x00 /* default */
|
|
||||||
#endif
|
|
||||||
|
|
||||||
|
|
||||||
LUAI_DDEF const lu_byte luai_ctype_[UCHAR_MAX + 2] = {
|
LUAI_DDEF const lu_byte luai_ctype_[UCHAR_MAX + 2] = {
|
||||||
0x00, /* EOZ */
|
0x00, /* EOZ */
|
||||||
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* 0. */
|
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* 0. */
|
||||||
@@ -43,22 +34,22 @@ LUAI_DDEF const lu_byte luai_ctype_[UCHAR_MAX + 2] = {
|
|||||||
0x05, 0x05, 0x05, 0x05, 0x05, 0x05, 0x05, 0x05,
|
0x05, 0x05, 0x05, 0x05, 0x05, 0x05, 0x05, 0x05,
|
||||||
0x05, 0x05, 0x05, 0x05, 0x05, 0x05, 0x05, 0x05, /* 7. */
|
0x05, 0x05, 0x05, 0x05, 0x05, 0x05, 0x05, 0x05, /* 7. */
|
||||||
0x05, 0x05, 0x05, 0x04, 0x04, 0x04, 0x04, 0x00,
|
0x05, 0x05, 0x05, 0x04, 0x04, 0x04, 0x04, 0x00,
|
||||||
NONA, NONA, NONA, NONA, NONA, NONA, NONA, NONA, /* 8. */
|
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* 8. */
|
||||||
NONA, NONA, NONA, NONA, NONA, NONA, NONA, NONA,
|
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
|
||||||
NONA, NONA, NONA, NONA, NONA, NONA, NONA, NONA, /* 9. */
|
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* 9. */
|
||||||
NONA, NONA, NONA, NONA, NONA, NONA, NONA, NONA,
|
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
|
||||||
NONA, NONA, NONA, NONA, NONA, NONA, NONA, NONA, /* a. */
|
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* a. */
|
||||||
NONA, NONA, NONA, NONA, NONA, NONA, NONA, NONA,
|
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
|
||||||
NONA, NONA, NONA, NONA, NONA, NONA, NONA, NONA, /* b. */
|
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* b. */
|
||||||
NONA, NONA, NONA, NONA, NONA, NONA, NONA, NONA,
|
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
|
||||||
0x00, 0x00, NONA, NONA, NONA, NONA, NONA, NONA, /* c. */
|
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* c. */
|
||||||
NONA, NONA, NONA, NONA, NONA, NONA, NONA, NONA,
|
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
|
||||||
NONA, NONA, NONA, NONA, NONA, NONA, NONA, NONA, /* d. */
|
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* d. */
|
||||||
NONA, NONA, NONA, NONA, NONA, NONA, NONA, NONA,
|
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
|
||||||
NONA, NONA, NONA, NONA, NONA, NONA, NONA, NONA, /* e. */
|
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* e. */
|
||||||
NONA, NONA, NONA, NONA, NONA, NONA, NONA, NONA,
|
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
|
||||||
NONA, NONA, NONA, NONA, NONA, 0x00, 0x00, 0x00, /* f. */
|
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* f. */
|
||||||
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
|
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
|
||||||
};
|
};
|
||||||
|
|
||||||
#endif /* } */
|
#endif /* } */
|
||||||
|
|||||||
@@ -1,5 +1,5 @@
|
|||||||
/*
|
/*
|
||||||
** $Id: lctype.h $
|
** $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
|
||||||
*/
|
*/
|
||||||
@@ -13,7 +13,7 @@
|
|||||||
/*
|
/*
|
||||||
** WARNING: the functions defined here do not necessarily correspond
|
** WARNING: the functions defined here do not necessarily correspond
|
||||||
** to the similar functions in the standard C ctype.h. They are
|
** to the similar functions in the standard C ctype.h. They are
|
||||||
** optimized for the specific needs of Lua.
|
** optimized for the specific needs of Lua
|
||||||
*/
|
*/
|
||||||
|
|
||||||
#if !defined(LUA_USE_CTYPE)
|
#if !defined(LUA_USE_CTYPE)
|
||||||
@@ -61,20 +61,14 @@
|
|||||||
#define lisprint(c) testprop(c, MASK(PRINTBIT))
|
#define lisprint(c) testprop(c, MASK(PRINTBIT))
|
||||||
#define lisxdigit(c) testprop(c, MASK(XDIGITBIT))
|
#define lisxdigit(c) testprop(c, MASK(XDIGITBIT))
|
||||||
|
|
||||||
|
|
||||||
/*
|
/*
|
||||||
** In ASCII, this 'ltolower' is correct for alphabetic characters and
|
** this 'ltolower' only works for alphabetic characters
|
||||||
** for '.'. That is enough for Lua needs. ('check_exp' ensures that
|
|
||||||
** the character either is an upper-case letter or is unchanged by
|
|
||||||
** the transformation, which holds for lower-case letters and '.'.)
|
|
||||||
*/
|
*/
|
||||||
#define ltolower(c) \
|
#define ltolower(c) ((c) | ('A' ^ 'a'))
|
||||||
check_exp(('A' <= (c) && (c) <= 'Z') || (c) == ((c) | ('A' ^ 'a')), \
|
|
||||||
(c) | ('A' ^ 'a'))
|
|
||||||
|
|
||||||
|
|
||||||
/* one entry for each character and for -1 (EOZ) */
|
/* two more entries for 0 and -1 (EOZ) */
|
||||||
LUAI_DDEC(const lu_byte luai_ctype_[UCHAR_MAX + 2];)
|
LUAI_DDEC const lu_byte luai_ctype_[UCHAR_MAX + 2];
|
||||||
|
|
||||||
|
|
||||||
#else /* }{ */
|
#else /* }{ */
|
||||||
|
|||||||
@@ -1,5 +1,5 @@
|
|||||||
/*
|
/*
|
||||||
** $Id: ldblib.c $
|
** $Id: ldblib.c,v 1.151.1.1 2017/04/19 17:20:42 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
|
||||||
*/
|
*/
|
||||||
@@ -18,14 +18,13 @@
|
|||||||
|
|
||||||
#include "lauxlib.h"
|
#include "lauxlib.h"
|
||||||
#include "lualib.h"
|
#include "lualib.h"
|
||||||
#include "llimits.h"
|
|
||||||
|
|
||||||
|
|
||||||
/*
|
/*
|
||||||
** The hook table at registry[HOOKKEY] maps threads to their current
|
** The hook table at registry[&HOOKKEY] maps threads to their current
|
||||||
** hook function.
|
** hook function. (We only need the unique address of 'HOOKKEY'.)
|
||||||
*/
|
*/
|
||||||
static const char *const HOOKKEY = "_HOOKKEY";
|
static const int HOOKKEY = 0;
|
||||||
|
|
||||||
|
|
||||||
/*
|
/*
|
||||||
@@ -34,7 +33,7 @@ static const char *const HOOKKEY = "_HOOKKEY";
|
|||||||
** checked.
|
** checked.
|
||||||
*/
|
*/
|
||||||
static void checkstack (lua_State *L, lua_State *L1, int n) {
|
static void checkstack (lua_State *L, lua_State *L1, int n) {
|
||||||
if (l_unlikely(L != L1 && !lua_checkstack(L1, n)))
|
if (L != L1 && !lua_checkstack(L1, n))
|
||||||
luaL_error(L, "stack overflow");
|
luaL_error(L, "stack overflow");
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -56,7 +55,8 @@ static int db_getmetatable (lua_State *L) {
|
|||||||
|
|
||||||
static int db_setmetatable (lua_State *L) {
|
static int db_setmetatable (lua_State *L) {
|
||||||
int t = lua_type(L, 2);
|
int t = lua_type(L, 2);
|
||||||
luaL_argexpected(L, t == LUA_TNIL || t == LUA_TTABLE, 2, "nil or table");
|
luaL_argcheck(L, t == LUA_TNIL || t == LUA_TTABLE, 2,
|
||||||
|
"nil or table expected");
|
||||||
lua_settop(L, 2);
|
lua_settop(L, 2);
|
||||||
lua_setmetatable(L, 1);
|
lua_setmetatable(L, 1);
|
||||||
return 1; /* return 1st argument */
|
return 1; /* return 1st argument */
|
||||||
@@ -64,24 +64,19 @@ static int db_setmetatable (lua_State *L) {
|
|||||||
|
|
||||||
|
|
||||||
static int db_getuservalue (lua_State *L) {
|
static int db_getuservalue (lua_State *L) {
|
||||||
int n = (int)luaL_optinteger(L, 2, 1);
|
|
||||||
if (lua_type(L, 1) != LUA_TUSERDATA)
|
if (lua_type(L, 1) != LUA_TUSERDATA)
|
||||||
luaL_pushfail(L);
|
lua_pushnil(L);
|
||||||
else if (lua_getiuservalue(L, 1, n) != LUA_TNONE) {
|
else
|
||||||
lua_pushboolean(L, 1);
|
lua_getuservalue(L, 1);
|
||||||
return 2;
|
|
||||||
}
|
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
static int db_setuservalue (lua_State *L) {
|
static int db_setuservalue (lua_State *L) {
|
||||||
int n = (int)luaL_optinteger(L, 3, 1);
|
|
||||||
luaL_checktype(L, 1, LUA_TUSERDATA);
|
luaL_checktype(L, 1, LUA_TUSERDATA);
|
||||||
luaL_checkany(L, 2);
|
luaL_checkany(L, 2);
|
||||||
lua_settop(L, 2);
|
lua_settop(L, 2);
|
||||||
if (!lua_setiuservalue(L, 1, n))
|
lua_setuservalue(L, 1);
|
||||||
luaL_pushfail(L);
|
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -151,9 +146,8 @@ static int db_getinfo (lua_State *L) {
|
|||||||
lua_Debug ar;
|
lua_Debug ar;
|
||||||
int arg;
|
int arg;
|
||||||
lua_State *L1 = getthread(L, &arg);
|
lua_State *L1 = getthread(L, &arg);
|
||||||
const char *options = luaL_optstring(L, arg+2, "flnSrtu");
|
const char *options = luaL_optstring(L, arg+2, "flnStu");
|
||||||
checkstack(L, L1, 3);
|
checkstack(L, L1, 3);
|
||||||
luaL_argcheck(L, options[0] != '>', arg + 2, "invalid option '>'");
|
|
||||||
if (lua_isfunction(L, arg + 1)) { /* info about a function? */
|
if (lua_isfunction(L, arg + 1)) { /* info about a function? */
|
||||||
options = lua_pushfstring(L, ">%s", options); /* add '>' to 'options' */
|
options = lua_pushfstring(L, ">%s", options); /* add '>' to 'options' */
|
||||||
lua_pushvalue(L, arg + 1); /* move function to 'L1' stack */
|
lua_pushvalue(L, arg + 1); /* move function to 'L1' stack */
|
||||||
@@ -161,7 +155,7 @@ static int db_getinfo (lua_State *L) {
|
|||||||
}
|
}
|
||||||
else { /* stack level */
|
else { /* stack level */
|
||||||
if (!lua_getstack(L1, (int)luaL_checkinteger(L, arg + 1), &ar)) {
|
if (!lua_getstack(L1, (int)luaL_checkinteger(L, arg + 1), &ar)) {
|
||||||
luaL_pushfail(L); /* level out of range */
|
lua_pushnil(L); /* level out of range */
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -169,8 +163,7 @@ static int db_getinfo (lua_State *L) {
|
|||||||
return luaL_argerror(L, arg+2, "invalid option");
|
return luaL_argerror(L, arg+2, "invalid option");
|
||||||
lua_newtable(L); /* table to collect results */
|
lua_newtable(L); /* table to collect results */
|
||||||
if (strchr(options, 'S')) {
|
if (strchr(options, 'S')) {
|
||||||
lua_pushlstring(L, ar.source, ar.srclen);
|
settabss(L, "source", ar.source);
|
||||||
lua_setfield(L, -2, "source");
|
|
||||||
settabss(L, "short_src", ar.short_src);
|
settabss(L, "short_src", ar.short_src);
|
||||||
settabsi(L, "linedefined", ar.linedefined);
|
settabsi(L, "linedefined", ar.linedefined);
|
||||||
settabsi(L, "lastlinedefined", ar.lastlinedefined);
|
settabsi(L, "lastlinedefined", ar.lastlinedefined);
|
||||||
@@ -187,14 +180,8 @@ static int db_getinfo (lua_State *L) {
|
|||||||
settabss(L, "name", ar.name);
|
settabss(L, "name", ar.name);
|
||||||
settabss(L, "namewhat", ar.namewhat);
|
settabss(L, "namewhat", ar.namewhat);
|
||||||
}
|
}
|
||||||
if (strchr(options, 'r')) {
|
if (strchr(options, 't'))
|
||||||
settabsi(L, "ftransfer", ar.ftransfer);
|
|
||||||
settabsi(L, "ntransfer", ar.ntransfer);
|
|
||||||
}
|
|
||||||
if (strchr(options, 't')) {
|
|
||||||
settabsb(L, "istailcall", ar.istailcall);
|
settabsb(L, "istailcall", ar.istailcall);
|
||||||
settabsi(L, "extraargs", ar.extraargs);
|
|
||||||
}
|
|
||||||
if (strchr(options, 'L'))
|
if (strchr(options, 'L'))
|
||||||
treatstackoption(L, L1, "activelines");
|
treatstackoption(L, L1, "activelines");
|
||||||
if (strchr(options, 'f'))
|
if (strchr(options, 'f'))
|
||||||
@@ -206,6 +193,8 @@ static int db_getinfo (lua_State *L) {
|
|||||||
static int db_getlocal (lua_State *L) {
|
static int db_getlocal (lua_State *L) {
|
||||||
int arg;
|
int arg;
|
||||||
lua_State *L1 = getthread(L, &arg);
|
lua_State *L1 = getthread(L, &arg);
|
||||||
|
lua_Debug ar;
|
||||||
|
const char *name;
|
||||||
int nvar = (int)luaL_checkinteger(L, arg + 2); /* local-variable index */
|
int nvar = (int)luaL_checkinteger(L, arg + 2); /* local-variable index */
|
||||||
if (lua_isfunction(L, arg + 1)) { /* function argument? */
|
if (lua_isfunction(L, arg + 1)) { /* function argument? */
|
||||||
lua_pushvalue(L, arg + 1); /* push function */
|
lua_pushvalue(L, arg + 1); /* push function */
|
||||||
@@ -213,10 +202,8 @@ static int db_getlocal (lua_State *L) {
|
|||||||
return 1; /* return only name (there is no value) */
|
return 1; /* return only name (there is no value) */
|
||||||
}
|
}
|
||||||
else { /* stack-level argument */
|
else { /* stack-level argument */
|
||||||
lua_Debug ar;
|
|
||||||
const char *name;
|
|
||||||
int level = (int)luaL_checkinteger(L, arg + 1);
|
int level = (int)luaL_checkinteger(L, arg + 1);
|
||||||
if (l_unlikely(!lua_getstack(L1, level, &ar))) /* out of range? */
|
if (!lua_getstack(L1, level, &ar)) /* out of range? */
|
||||||
return luaL_argerror(L, arg+1, "level out of range");
|
return luaL_argerror(L, arg+1, "level out of range");
|
||||||
checkstack(L, L1, 1);
|
checkstack(L, L1, 1);
|
||||||
name = lua_getlocal(L1, &ar, nvar);
|
name = lua_getlocal(L1, &ar, nvar);
|
||||||
@@ -227,7 +214,7 @@ static int db_getlocal (lua_State *L) {
|
|||||||
return 2;
|
return 2;
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
luaL_pushfail(L); /* no name (nor value) */
|
lua_pushnil(L); /* no name (nor value) */
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -241,7 +228,7 @@ static int db_setlocal (lua_State *L) {
|
|||||||
lua_Debug ar;
|
lua_Debug ar;
|
||||||
int level = (int)luaL_checkinteger(L, arg + 1);
|
int level = (int)luaL_checkinteger(L, arg + 1);
|
||||||
int nvar = (int)luaL_checkinteger(L, arg + 2);
|
int nvar = (int)luaL_checkinteger(L, arg + 2);
|
||||||
if (l_unlikely(!lua_getstack(L1, level, &ar))) /* out of range? */
|
if (!lua_getstack(L1, level, &ar)) /* out of range? */
|
||||||
return luaL_argerror(L, arg+1, "level out of range");
|
return luaL_argerror(L, arg+1, "level out of range");
|
||||||
luaL_checkany(L, arg+3);
|
luaL_checkany(L, arg+3);
|
||||||
lua_settop(L, arg+3);
|
lua_settop(L, arg+3);
|
||||||
@@ -285,33 +272,25 @@ static int db_setupvalue (lua_State *L) {
|
|||||||
** Check whether a given upvalue from a given closure exists and
|
** Check whether a given upvalue from a given closure exists and
|
||||||
** returns its index
|
** returns its index
|
||||||
*/
|
*/
|
||||||
static void *checkupval (lua_State *L, int argf, int argnup, int *pnup) {
|
static int checkupval (lua_State *L, int argf, int argnup) {
|
||||||
void *id;
|
|
||||||
int nup = (int)luaL_checkinteger(L, argnup); /* upvalue index */
|
int nup = (int)luaL_checkinteger(L, argnup); /* upvalue index */
|
||||||
luaL_checktype(L, argf, LUA_TFUNCTION); /* closure */
|
luaL_checktype(L, argf, LUA_TFUNCTION); /* closure */
|
||||||
id = lua_upvalueid(L, argf, nup);
|
luaL_argcheck(L, (lua_getupvalue(L, argf, nup) != NULL), argnup,
|
||||||
if (pnup) {
|
"invalid upvalue index");
|
||||||
luaL_argcheck(L, id != NULL, argnup, "invalid upvalue index");
|
return nup;
|
||||||
*pnup = nup;
|
|
||||||
}
|
|
||||||
return id;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
static int db_upvalueid (lua_State *L) {
|
static int db_upvalueid (lua_State *L) {
|
||||||
void *id = checkupval(L, 1, 2, NULL);
|
int n = checkupval(L, 1, 2);
|
||||||
if (id != NULL)
|
lua_pushlightuserdata(L, lua_upvalueid(L, 1, n));
|
||||||
lua_pushlightuserdata(L, id);
|
|
||||||
else
|
|
||||||
luaL_pushfail(L);
|
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
static int db_upvaluejoin (lua_State *L) {
|
static int db_upvaluejoin (lua_State *L) {
|
||||||
int n1, n2;
|
int n1 = checkupval(L, 1, 2);
|
||||||
checkupval(L, 1, 2, &n1);
|
int n2 = checkupval(L, 3, 4);
|
||||||
checkupval(L, 3, 4, &n2);
|
|
||||||
luaL_argcheck(L, !lua_iscfunction(L, 1), 1, "Lua function expected");
|
luaL_argcheck(L, !lua_iscfunction(L, 1), 1, "Lua function expected");
|
||||||
luaL_argcheck(L, !lua_iscfunction(L, 3), 3, "Lua function expected");
|
luaL_argcheck(L, !lua_iscfunction(L, 3), 3, "Lua function expected");
|
||||||
lua_upvaluejoin(L, 1, n1, 3, n2);
|
lua_upvaluejoin(L, 1, n1, 3, n2);
|
||||||
@@ -326,7 +305,7 @@ static int db_upvaluejoin (lua_State *L) {
|
|||||||
static void hookf (lua_State *L, lua_Debug *ar) {
|
static void hookf (lua_State *L, lua_Debug *ar) {
|
||||||
static const char *const hooknames[] =
|
static const char *const hooknames[] =
|
||||||
{"call", "return", "line", "count", "tail call"};
|
{"call", "return", "line", "count", "tail call"};
|
||||||
lua_getfield(L, LUA_REGISTRYINDEX, HOOKKEY);
|
lua_rawgetp(L, LUA_REGISTRYINDEX, &HOOKKEY);
|
||||||
lua_pushthread(L);
|
lua_pushthread(L);
|
||||||
if (lua_rawget(L, -2) == LUA_TFUNCTION) { /* is there a hook function? */
|
if (lua_rawget(L, -2) == LUA_TFUNCTION) { /* is there a hook function? */
|
||||||
lua_pushstring(L, hooknames[(int)ar->event]); /* push event name */
|
lua_pushstring(L, hooknames[(int)ar->event]); /* push event name */
|
||||||
@@ -379,12 +358,14 @@ static int db_sethook (lua_State *L) {
|
|||||||
count = (int)luaL_optinteger(L, arg + 3, 0);
|
count = (int)luaL_optinteger(L, arg + 3, 0);
|
||||||
func = hookf; mask = makemask(smask, count);
|
func = hookf; mask = makemask(smask, count);
|
||||||
}
|
}
|
||||||
if (!luaL_getsubtable(L, LUA_REGISTRYINDEX, HOOKKEY)) {
|
if (lua_rawgetp(L, LUA_REGISTRYINDEX, &HOOKKEY) == LUA_TNIL) {
|
||||||
/* table just created; initialize it */
|
lua_createtable(L, 0, 2); /* create a hook table */
|
||||||
lua_pushliteral(L, "k");
|
lua_pushvalue(L, -1);
|
||||||
|
lua_rawsetp(L, LUA_REGISTRYINDEX, &HOOKKEY); /* set it in position */
|
||||||
|
lua_pushstring(L, "k");
|
||||||
lua_setfield(L, -2, "__mode"); /** hooktable.__mode = "k" */
|
lua_setfield(L, -2, "__mode"); /** hooktable.__mode = "k" */
|
||||||
lua_pushvalue(L, -1);
|
lua_pushvalue(L, -1);
|
||||||
lua_setmetatable(L, -2); /* metatable(hooktable) = hooktable */
|
lua_setmetatable(L, -2); /* setmetatable(hooktable) = hooktable */
|
||||||
}
|
}
|
||||||
checkstack(L, L1, 1);
|
checkstack(L, L1, 1);
|
||||||
lua_pushthread(L1); lua_xmove(L1, L, 1); /* key (thread) */
|
lua_pushthread(L1); lua_xmove(L1, L, 1); /* key (thread) */
|
||||||
@@ -401,14 +382,12 @@ static int db_gethook (lua_State *L) {
|
|||||||
char buff[5];
|
char buff[5];
|
||||||
int mask = lua_gethookmask(L1);
|
int mask = lua_gethookmask(L1);
|
||||||
lua_Hook hook = lua_gethook(L1);
|
lua_Hook hook = lua_gethook(L1);
|
||||||
if (hook == NULL) { /* no hook? */
|
if (hook == NULL) /* no hook? */
|
||||||
luaL_pushfail(L);
|
lua_pushnil(L);
|
||||||
return 1;
|
|
||||||
}
|
|
||||||
else if (hook != hookf) /* external hook? */
|
else if (hook != hookf) /* external hook? */
|
||||||
lua_pushliteral(L, "external hook");
|
lua_pushliteral(L, "external hook");
|
||||||
else { /* hook table must exist */
|
else { /* hook table must exist */
|
||||||
lua_getfield(L, LUA_REGISTRYINDEX, HOOKKEY);
|
lua_rawgetp(L, LUA_REGISTRYINDEX, &HOOKKEY);
|
||||||
checkstack(L, L1, 1);
|
checkstack(L, L1, 1);
|
||||||
lua_pushthread(L1); lua_xmove(L1, L, 1);
|
lua_pushthread(L1); lua_xmove(L1, L, 1);
|
||||||
lua_rawget(L, -2); /* 1st result = hooktable[L1] */
|
lua_rawget(L, -2); /* 1st result = hooktable[L1] */
|
||||||
@@ -424,12 +403,12 @@ static int db_debug (lua_State *L) {
|
|||||||
for (;;) {
|
for (;;) {
|
||||||
char buffer[250];
|
char buffer[250];
|
||||||
lua_writestringerror("%s", "lua_debug> ");
|
lua_writestringerror("%s", "lua_debug> ");
|
||||||
if (fgets(buffer, sizeof(buffer), stdin) == NULL ||
|
if (fgets(buffer, sizeof(buffer), stdin) == 0 ||
|
||||||
strcmp(buffer, "cont\n") == 0)
|
strcmp(buffer, "cont\n") == 0)
|
||||||
return 0;
|
return 0;
|
||||||
if (luaL_loadbuffer(L, buffer, strlen(buffer), "=(debug command)") ||
|
if (luaL_loadbuffer(L, buffer, strlen(buffer), "=(debug command)") ||
|
||||||
lua_pcall(L, 0, 0, 0))
|
lua_pcall(L, 0, 0, 0))
|
||||||
lua_writestringerror("%s\n", luaL_tolstring(L, -1, NULL));
|
lua_writestringerror("%s\n", lua_tostring(L, -1));
|
||||||
lua_settop(L, 0); /* remove eventual returns */
|
lua_settop(L, 0); /* remove eventual returns */
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,5 +1,5 @@
|
|||||||
/*
|
/*
|
||||||
** $Id: ldebug.h $
|
** $Id: ldebug.h,v 2.14.1.1 2017/04/19 17:20:42 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
|
||||||
*/
|
*/
|
||||||
@@ -11,39 +11,15 @@
|
|||||||
#include "lstate.h"
|
#include "lstate.h"
|
||||||
|
|
||||||
|
|
||||||
#define pcRel(pc, p) (cast_int((pc) - (p)->code) - 1)
|
#define pcRel(pc, p) (cast(int, (pc) - (p)->code) - 1)
|
||||||
|
|
||||||
|
|
||||||
/* Active Lua function (given call info) */
|
|
||||||
#define ci_func(ci) (clLvalue(s2v((ci)->func.p)))
|
|
||||||
|
|
||||||
|
#define getfuncline(f,pc) (((f)->lineinfo) ? (f)->lineinfo[pc] : -1)
|
||||||
|
|
||||||
#define resethookcount(L) (L->hookcount = L->basehookcount)
|
#define resethookcount(L) (L->hookcount = L->basehookcount)
|
||||||
|
|
||||||
/*
|
|
||||||
** mark for entries in 'lineinfo' array that has absolute information in
|
|
||||||
** 'abslineinfo' array
|
|
||||||
*/
|
|
||||||
#define ABSLINEINFO (-0x80)
|
|
||||||
|
|
||||||
|
|
||||||
/*
|
|
||||||
** MAXimum number of successive Instructions WiTHout ABSolute line
|
|
||||||
** information. (A power of two allows fast divisions.)
|
|
||||||
*/
|
|
||||||
#if !defined(MAXIWTHABS)
|
|
||||||
#define MAXIWTHABS 128
|
|
||||||
#endif
|
|
||||||
|
|
||||||
|
|
||||||
LUAI_FUNC int luaG_getfuncline (const Proto *f, int pc);
|
|
||||||
LUAI_FUNC const char *luaG_findlocal (lua_State *L, CallInfo *ci, int n,
|
|
||||||
StkId *pos);
|
|
||||||
LUAI_FUNC l_noret luaG_typeerror (lua_State *L, const TValue *o,
|
LUAI_FUNC l_noret luaG_typeerror (lua_State *L, const TValue *o,
|
||||||
const char *opname);
|
const char *opname);
|
||||||
LUAI_FUNC l_noret luaG_callerror (lua_State *L, const TValue *o);
|
|
||||||
LUAI_FUNC l_noret luaG_forerror (lua_State *L, const TValue *o,
|
|
||||||
const char *what);
|
|
||||||
LUAI_FUNC l_noret luaG_concaterror (lua_State *L, const TValue *p1,
|
LUAI_FUNC l_noret luaG_concaterror (lua_State *L, const TValue *p1,
|
||||||
const TValue *p2);
|
const TValue *p2);
|
||||||
LUAI_FUNC l_noret luaG_opinterror (lua_State *L, const TValue *p1,
|
LUAI_FUNC l_noret luaG_opinterror (lua_State *L, const TValue *p1,
|
||||||
@@ -53,13 +29,11 @@ LUAI_FUNC l_noret luaG_tointerror (lua_State *L, const TValue *p1,
|
|||||||
const TValue *p2);
|
const TValue *p2);
|
||||||
LUAI_FUNC l_noret luaG_ordererror (lua_State *L, const TValue *p1,
|
LUAI_FUNC l_noret luaG_ordererror (lua_State *L, const TValue *p1,
|
||||||
const TValue *p2);
|
const TValue *p2);
|
||||||
LUAI_FUNC l_noret luaG_errnnil (lua_State *L, LClosure *cl, int k);
|
|
||||||
LUAI_FUNC l_noret luaG_runerror (lua_State *L, const char *fmt, ...);
|
LUAI_FUNC l_noret luaG_runerror (lua_State *L, const char *fmt, ...);
|
||||||
LUAI_FUNC const char *luaG_addinfo (lua_State *L, const char *msg,
|
LUAI_FUNC const char *luaG_addinfo (lua_State *L, const char *msg,
|
||||||
TString *src, int line);
|
TString *src, int line);
|
||||||
LUAI_FUNC l_noret luaG_errormsg (lua_State *L);
|
LUAI_FUNC l_noret luaG_errormsg (lua_State *L);
|
||||||
LUAI_FUNC int luaG_traceexec (lua_State *L, const Instruction *pc);
|
LUAI_FUNC void luaG_traceexec (lua_State *L);
|
||||||
LUAI_FUNC int luaG_tracecall (lua_State *L);
|
|
||||||
|
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
|||||||
@@ -1,5 +1,5 @@
|
|||||||
/*
|
/*
|
||||||
** $Id: ldo.h $
|
** $Id: ldo.h,v 2.29.1.1 2017/04/19 17:20:42 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
|
||||||
*/
|
*/
|
||||||
@@ -8,7 +8,6 @@
|
|||||||
#define ldo_h
|
#define ldo_h
|
||||||
|
|
||||||
|
|
||||||
#include "llimits.h"
|
|
||||||
#include "lobject.h"
|
#include "lobject.h"
|
||||||
#include "lstate.h"
|
#include "lstate.h"
|
||||||
#include "lzio.h"
|
#include "lzio.h"
|
||||||
@@ -18,81 +17,42 @@
|
|||||||
** Macro to check stack size and grow stack if needed. Parameters
|
** Macro to check stack size and grow stack if needed. Parameters
|
||||||
** 'pre'/'pos' allow the macro to preserve a pointer into the
|
** 'pre'/'pos' allow the macro to preserve a pointer into the
|
||||||
** stack across reallocations, doing the work only when needed.
|
** stack across reallocations, doing the work only when needed.
|
||||||
** It also allows the running of one GC step when the stack is
|
|
||||||
** reallocated.
|
|
||||||
** 'condmovestack' is used in heavy tests to force a stack reallocation
|
** 'condmovestack' is used in heavy tests to force a stack reallocation
|
||||||
** at every check.
|
** at every check.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
#if !defined(HARDSTACKTESTS)
|
|
||||||
#define condmovestack(L,pre,pos) ((void)0)
|
|
||||||
#else
|
|
||||||
/* realloc stack keeping its size */
|
|
||||||
#define condmovestack(L,pre,pos) \
|
|
||||||
{ int sz_ = stacksize(L); pre; luaD_reallocstack((L), sz_, 0); pos; }
|
|
||||||
#endif
|
|
||||||
|
|
||||||
#define luaD_checkstackaux(L,n,pre,pos) \
|
#define luaD_checkstackaux(L,n,pre,pos) \
|
||||||
if (l_unlikely(L->stack_last.p - L->top.p <= (n))) \
|
if (L->stack_last - L->top <= (n)) \
|
||||||
{ pre; luaD_growstack(L, n, 1); pos; } \
|
{ pre; luaD_growstack(L, n); pos; } else { condmovestack(L,pre,pos); }
|
||||||
else { condmovestack(L,pre,pos); }
|
|
||||||
|
|
||||||
/* In general, 'pre'/'pos' are empty (nothing to save) */
|
/* In general, 'pre'/'pos' are empty (nothing to save) */
|
||||||
#define luaD_checkstack(L,n) luaD_checkstackaux(L,n,(void)0,(void)0)
|
#define luaD_checkstack(L,n) luaD_checkstackaux(L,n,(void)0,(void)0)
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
#define savestack(L,pt) (cast_charp(pt) - cast_charp(L->stack.p))
|
#define savestack(L,p) ((char *)(p) - (char *)L->stack)
|
||||||
#define restorestack(L,n) cast(StkId, cast_charp(L->stack.p) + (n))
|
#define restorestack(L,n) ((TValue *)((char *)L->stack + (n)))
|
||||||
|
|
||||||
|
|
||||||
/* macro to check stack size, preserving 'p' */
|
|
||||||
#define checkstackp(L,n,p) \
|
|
||||||
luaD_checkstackaux(L, n, \
|
|
||||||
ptrdiff_t t__ = savestack(L, p), /* save 'p' */ \
|
|
||||||
p = restorestack(L, t__)) /* 'pos' part: restore 'p' */
|
|
||||||
|
|
||||||
|
|
||||||
/*
|
|
||||||
** Maximum depth for nested C calls, syntactical nested non-terminals,
|
|
||||||
** and other features implemented through recursion in C. (Value must
|
|
||||||
** fit in a 16-bit unsigned integer. It must also be compatible with
|
|
||||||
** the size of the C stack.)
|
|
||||||
*/
|
|
||||||
#if !defined(LUAI_MAXCCALLS)
|
|
||||||
#define LUAI_MAXCCALLS 200
|
|
||||||
#endif
|
|
||||||
|
|
||||||
|
|
||||||
/* type of protected functions, to be ran by 'runprotected' */
|
/* type of protected functions, to be ran by 'runprotected' */
|
||||||
typedef void (*Pfunc) (lua_State *L, void *ud);
|
typedef void (*Pfunc) (lua_State *L, void *ud);
|
||||||
|
|
||||||
LUAI_FUNC l_noret luaD_errerr (lua_State *L);
|
LUAI_FUNC int luaD_protectedparser (lua_State *L, ZIO *z, const char *name,
|
||||||
LUAI_FUNC void luaD_seterrorobj (lua_State *L, TStatus errcode, StkId oldtop);
|
|
||||||
LUAI_FUNC TStatus luaD_protectedparser (lua_State *L, ZIO *z,
|
|
||||||
const char *name,
|
|
||||||
const char *mode);
|
const char *mode);
|
||||||
LUAI_FUNC void luaD_hook (lua_State *L, int event, int line,
|
LUAI_FUNC void luaD_hook (lua_State *L, int event, int line);
|
||||||
int fTransfer, int nTransfer);
|
LUAI_FUNC int luaD_precall (lua_State *L, StkId func, int nresults);
|
||||||
LUAI_FUNC void luaD_hookcall (lua_State *L, CallInfo *ci);
|
|
||||||
LUAI_FUNC int luaD_pretailcall (lua_State *L, CallInfo *ci, StkId func,
|
|
||||||
int narg1, int delta);
|
|
||||||
LUAI_FUNC CallInfo *luaD_precall (lua_State *L, StkId func, int nResults);
|
|
||||||
LUAI_FUNC void luaD_call (lua_State *L, StkId func, int nResults);
|
LUAI_FUNC void luaD_call (lua_State *L, StkId func, int nResults);
|
||||||
LUAI_FUNC void luaD_callnoyield (lua_State *L, StkId func, int nResults);
|
LUAI_FUNC void luaD_callnoyield (lua_State *L, StkId func, int nResults);
|
||||||
LUAI_FUNC TStatus luaD_closeprotected (lua_State *L, ptrdiff_t level,
|
LUAI_FUNC int luaD_pcall (lua_State *L, Pfunc func, void *u,
|
||||||
TStatus status);
|
|
||||||
LUAI_FUNC TStatus luaD_pcall (lua_State *L, Pfunc func, void *u,
|
|
||||||
ptrdiff_t oldtop, ptrdiff_t ef);
|
ptrdiff_t oldtop, ptrdiff_t ef);
|
||||||
LUAI_FUNC void luaD_poscall (lua_State *L, CallInfo *ci, int nres);
|
LUAI_FUNC int luaD_poscall (lua_State *L, CallInfo *ci, StkId firstResult,
|
||||||
LUAI_FUNC int luaD_reallocstack (lua_State *L, int newsize, int raiseerror);
|
int nres);
|
||||||
LUAI_FUNC int luaD_growstack (lua_State *L, int n, int raiseerror);
|
LUAI_FUNC void luaD_reallocstack (lua_State *L, int newsize);
|
||||||
|
LUAI_FUNC void luaD_growstack (lua_State *L, int n);
|
||||||
LUAI_FUNC void luaD_shrinkstack (lua_State *L);
|
LUAI_FUNC void luaD_shrinkstack (lua_State *L);
|
||||||
LUAI_FUNC void luaD_inctop (lua_State *L);
|
LUAI_FUNC void luaD_inctop (lua_State *L);
|
||||||
|
|
||||||
LUAI_FUNC l_noret luaD_throw (lua_State *L, TStatus errcode);
|
LUAI_FUNC l_noret luaD_throw (lua_State *L, int errcode);
|
||||||
LUAI_FUNC l_noret luaD_throwbaselevel (lua_State *L, TStatus errcode);
|
LUAI_FUNC int luaD_rawrunprotected (lua_State *L, Pfunc f, void *ud);
|
||||||
LUAI_FUNC TStatus luaD_rawrunprotected (lua_State *L, Pfunc f, void *ud);
|
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
|||||||
@@ -1,5 +1,5 @@
|
|||||||
/*
|
/*
|
||||||
** $Id: ldump.c $
|
** $Id: ldump.c,v 2.37.1.1 2017/04/19 17:20:42 roberto Exp $
|
||||||
** save precompiled Lua chunks
|
** save precompiled Lua chunks
|
||||||
** See Copyright Notice in lua.h
|
** See Copyright Notice in lua.h
|
||||||
*/
|
*/
|
||||||
@@ -10,16 +10,12 @@
|
|||||||
#include "lprefix.h"
|
#include "lprefix.h"
|
||||||
|
|
||||||
|
|
||||||
#include <limits.h>
|
|
||||||
#include <stddef.h>
|
#include <stddef.h>
|
||||||
|
|
||||||
#include "lua.h"
|
#include "lua.h"
|
||||||
|
|
||||||
#include "lapi.h"
|
|
||||||
#include "lgc.h"
|
|
||||||
#include "lobject.h"
|
#include "lobject.h"
|
||||||
#include "lstate.h"
|
#include "lstate.h"
|
||||||
#include "ltable.h"
|
|
||||||
#include "lundump.h"
|
#include "lundump.h"
|
||||||
|
|
||||||
|
|
||||||
@@ -27,281 +23,193 @@ typedef struct {
|
|||||||
lua_State *L;
|
lua_State *L;
|
||||||
lua_Writer writer;
|
lua_Writer writer;
|
||||||
void *data;
|
void *data;
|
||||||
size_t offset; /* current position relative to beginning of dump */
|
|
||||||
int strip;
|
int strip;
|
||||||
int status;
|
int status;
|
||||||
Table *h; /* table to track saved strings */
|
|
||||||
lua_Unsigned nstr; /* counter for counting saved strings */
|
|
||||||
} DumpState;
|
} DumpState;
|
||||||
|
|
||||||
|
|
||||||
/*
|
/*
|
||||||
** All high-level dumps go through dumpVector; you can change it to
|
** All high-level dumps go through DumpVector; you can change it to
|
||||||
** change the endianness of the result
|
** change the endianness of the result
|
||||||
*/
|
*/
|
||||||
#define dumpVector(D,v,n) dumpBlock(D,v,(n)*sizeof((v)[0]))
|
#define DumpVector(v,n,D) DumpBlock(v,(n)*sizeof((v)[0]),D)
|
||||||
|
|
||||||
#define dumpLiteral(D, s) dumpBlock(D,s,sizeof(s) - sizeof(char))
|
#define DumpLiteral(s,D) DumpBlock(s, sizeof(s) - sizeof(char), D)
|
||||||
|
|
||||||
|
|
||||||
/*
|
static void DumpBlock (const void *b, size_t size, DumpState *D) {
|
||||||
** Dump the block of memory pointed by 'b' with given 'size'.
|
if (D->status == 0 && size > 0) {
|
||||||
** 'b' should not be NULL, except for the last call signaling the end
|
|
||||||
** of the dump.
|
|
||||||
*/
|
|
||||||
static void dumpBlock (DumpState *D, const void *b, size_t size) {
|
|
||||||
if (D->status == 0) { /* do not write anything after an error */
|
|
||||||
lua_unlock(D->L);
|
lua_unlock(D->L);
|
||||||
D->status = (*D->writer)(D->L, b, size, D->data);
|
D->status = (*D->writer)(D->L, b, size, D->data);
|
||||||
lua_lock(D->L);
|
lua_lock(D->L);
|
||||||
D->offset += size;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/*
|
#define DumpVar(x,D) DumpVector(&x,1,D)
|
||||||
** Dump enough zeros to ensure that current position is a multiple of
|
|
||||||
** 'align'.
|
|
||||||
*/
|
|
||||||
static void dumpAlign (DumpState *D, unsigned align) {
|
|
||||||
unsigned padding = align - cast_uint(D->offset % align);
|
|
||||||
if (padding < align) { /* padding == align means no padding */
|
|
||||||
static lua_Integer paddingContent = 0;
|
|
||||||
lua_assert(align <= sizeof(lua_Integer));
|
|
||||||
dumpBlock(D, &paddingContent, padding);
|
|
||||||
}
|
|
||||||
lua_assert(D->offset % align == 0);
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
#define dumpVar(D,x) dumpVector(D,&x,1)
|
static void DumpByte (int y, DumpState *D) {
|
||||||
|
|
||||||
|
|
||||||
static void dumpByte (DumpState *D, int y) {
|
|
||||||
lu_byte x = (lu_byte)y;
|
lu_byte x = (lu_byte)y;
|
||||||
dumpVar(D, x);
|
DumpVar(x, D);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/*
|
static void DumpInt (int x, DumpState *D) {
|
||||||
** size for 'dumpVarint' buffer: each byte can store up to 7 bits.
|
DumpVar(x, D);
|
||||||
** (The "+6" rounds up the division.)
|
|
||||||
*/
|
|
||||||
#define DIBS ((l_numbits(lua_Unsigned) + 6) / 7)
|
|
||||||
|
|
||||||
/*
|
|
||||||
** Dumps an unsigned integer using the MSB Varint encoding
|
|
||||||
*/
|
|
||||||
static void dumpVarint (DumpState *D, lua_Unsigned x) {
|
|
||||||
lu_byte buff[DIBS];
|
|
||||||
unsigned n = 1;
|
|
||||||
buff[DIBS - 1] = x & 0x7f; /* fill least-significant byte */
|
|
||||||
while ((x >>= 7) != 0) /* fill other bytes in reverse order */
|
|
||||||
buff[DIBS - (++n)] = cast_byte((x & 0x7f) | 0x80);
|
|
||||||
dumpVector(D, buff + DIBS - n, n);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
static void dumpSize (DumpState *D, size_t sz) {
|
static void DumpNumber (lua_Number x, DumpState *D) {
|
||||||
dumpVarint(D, cast(lua_Unsigned, sz));
|
DumpVar(x, D);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
static void dumpInt (DumpState *D, int x) {
|
static void DumpInteger (lua_Integer x, DumpState *D) {
|
||||||
lua_assert(x >= 0);
|
DumpVar(x, D);
|
||||||
dumpVarint(D, cast_uint(x));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
static void dumpNumber (DumpState *D, lua_Number x) {
|
static void DumpString (const TString *s, DumpState *D) {
|
||||||
dumpVar(D, x);
|
if (s == NULL)
|
||||||
}
|
DumpByte(0, D);
|
||||||
|
|
||||||
|
|
||||||
/*
|
|
||||||
** Signed integers are coded to keep small values small. (Coding -1 as
|
|
||||||
** 0xfff...fff would use too many bytes to save a quite common value.)
|
|
||||||
** A non-negative x is coded as 2x; a negative x is coded as -2x - 1.
|
|
||||||
** (0 => 0; -1 => 1; 1 => 2; -2 => 3; 2 => 4; ...)
|
|
||||||
*/
|
|
||||||
static void dumpInteger (DumpState *D, lua_Integer x) {
|
|
||||||
lua_Unsigned cx = (x >= 0) ? 2u * l_castS2U(x)
|
|
||||||
: (2u * ~l_castS2U(x)) + 1;
|
|
||||||
dumpVarint(D, cx);
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
/*
|
|
||||||
** Dump a String. First dump its "size":
|
|
||||||
** size==0 is followed by an index and means "reuse saved string with
|
|
||||||
** that index"; index==0 means NULL.
|
|
||||||
** size>=1 is followed by the string contents with real size==size-1 and
|
|
||||||
** means that string, which will be saved with the next available index.
|
|
||||||
** The real size does not include the ending '\0' (which is not dumped),
|
|
||||||
** so adding 1 to it cannot overflow a size_t.
|
|
||||||
*/
|
|
||||||
static void dumpString (DumpState *D, TString *ts) {
|
|
||||||
if (ts == NULL) {
|
|
||||||
dumpVarint(D, 0); /* will "reuse" NULL */
|
|
||||||
dumpVarint(D, 0); /* special index for NULL */
|
|
||||||
}
|
|
||||||
else {
|
else {
|
||||||
TValue idx;
|
size_t size = tsslen(s) + 1; /* include trailing '\0' */
|
||||||
int tag = luaH_getstr(D->h, ts, &idx);
|
const char *str = getstr(s);
|
||||||
if (!tagisempty(tag)) { /* string already saved? */
|
if (size < 0xFF)
|
||||||
dumpVarint(D, 0); /* reuse a saved string */
|
DumpByte(cast_int(size), D);
|
||||||
dumpVarint(D, l_castS2U(ivalue(&idx))); /* index of saved string */
|
else {
|
||||||
}
|
DumpByte(0xFF, D);
|
||||||
else { /* must write and save the string */
|
DumpVar(size, D);
|
||||||
TValue key, value; /* to save the string in the hash */
|
|
||||||
size_t size;
|
|
||||||
const char *s = getlstr(ts, size);
|
|
||||||
dumpSize(D, size + 1);
|
|
||||||
dumpVector(D, s, size + 1); /* include ending '\0' */
|
|
||||||
D->nstr++; /* one more saved string */
|
|
||||||
setsvalue(D->L, &key, ts); /* the string is the key */
|
|
||||||
setivalue(&value, l_castU2S(D->nstr)); /* its index is the value */
|
|
||||||
luaH_set(D->L, D->h, &key, &value); /* h[ts] = nstr */
|
|
||||||
/* integer value does not need barrier */
|
|
||||||
}
|
}
|
||||||
|
DumpVector(str, size - 1, D); /* no need to save '\0' */
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
static void dumpCode (DumpState *D, const Proto *f) {
|
static void DumpCode (const Proto *f, DumpState *D) {
|
||||||
dumpInt(D, f->sizecode);
|
DumpInt(f->sizecode, D);
|
||||||
dumpAlign(D, sizeof(f->code[0]));
|
DumpVector(f->code, f->sizecode, D);
|
||||||
lua_assert(f->code != NULL);
|
|
||||||
dumpVector(D, f->code, cast_uint(f->sizecode));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
static void dumpFunction (DumpState *D, const Proto *f);
|
static void DumpFunction(const Proto *f, TString *psource, DumpState *D);
|
||||||
|
|
||||||
static void dumpConstants (DumpState *D, const Proto *f) {
|
static void DumpConstants (const Proto *f, DumpState *D) {
|
||||||
int i;
|
int i;
|
||||||
int n = f->sizek;
|
int n = f->sizek;
|
||||||
dumpInt(D, n);
|
DumpInt(n, D);
|
||||||
for (i = 0; i < n; i++) {
|
for (i = 0; i < n; i++) {
|
||||||
const TValue *o = &f->k[i];
|
const TValue *o = &f->k[i];
|
||||||
int tt = ttypetag(o);
|
DumpByte(ttype(o), D);
|
||||||
dumpByte(D, tt);
|
switch (ttype(o)) {
|
||||||
switch (tt) {
|
case LUA_TNIL:
|
||||||
case LUA_VNUMFLT:
|
break;
|
||||||
dumpNumber(D, fltvalue(o));
|
case LUA_TBOOLEAN:
|
||||||
break;
|
DumpByte(bvalue(o), D);
|
||||||
case LUA_VNUMINT:
|
break;
|
||||||
dumpInteger(D, ivalue(o));
|
case LUA_TNUMFLT:
|
||||||
break;
|
DumpNumber(fltvalue(o), D);
|
||||||
case LUA_VSHRSTR:
|
break;
|
||||||
case LUA_VLNGSTR:
|
case LUA_TNUMINT:
|
||||||
dumpString(D, tsvalue(o));
|
DumpInteger(ivalue(o), D);
|
||||||
break;
|
break;
|
||||||
default:
|
case LUA_TSHRSTR:
|
||||||
lua_assert(tt == LUA_VNIL || tt == LUA_VFALSE || tt == LUA_VTRUE);
|
case LUA_TLNGSTR:
|
||||||
|
DumpString(tsvalue(o), D);
|
||||||
|
break;
|
||||||
|
default:
|
||||||
|
lua_assert(0);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
static void dumpProtos (DumpState *D, const Proto *f) {
|
static void DumpProtos (const Proto *f, DumpState *D) {
|
||||||
int i;
|
int i;
|
||||||
int n = f->sizep;
|
int n = f->sizep;
|
||||||
dumpInt(D, n);
|
DumpInt(n, D);
|
||||||
for (i = 0; i < n; i++)
|
for (i = 0; i < n; i++)
|
||||||
dumpFunction(D, f->p[i]);
|
DumpFunction(f->p[i], f->source, D);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
static void dumpUpvalues (DumpState *D, const Proto *f) {
|
static void DumpUpvalues (const Proto *f, DumpState *D) {
|
||||||
int i, n = f->sizeupvalues;
|
int i, n = f->sizeupvalues;
|
||||||
dumpInt(D, n);
|
DumpInt(n, D);
|
||||||
for (i = 0; i < n; i++) {
|
for (i = 0; i < n; i++) {
|
||||||
dumpByte(D, f->upvalues[i].instack);
|
DumpByte(f->upvalues[i].instack, D);
|
||||||
dumpByte(D, f->upvalues[i].idx);
|
DumpByte(f->upvalues[i].idx, D);
|
||||||
dumpByte(D, f->upvalues[i].kind);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
static void dumpDebug (DumpState *D, const Proto *f) {
|
static void DumpDebug (const Proto *f, DumpState *D) {
|
||||||
int i, n;
|
int i, n;
|
||||||
n = (D->strip) ? 0 : f->sizelineinfo;
|
n = (D->strip) ? 0 : f->sizelineinfo;
|
||||||
dumpInt(D, n);
|
DumpInt(n, D);
|
||||||
if (f->lineinfo != NULL)
|
DumpVector(f->lineinfo, n, D);
|
||||||
dumpVector(D, f->lineinfo, cast_uint(n));
|
|
||||||
n = (D->strip) ? 0 : f->sizeabslineinfo;
|
|
||||||
dumpInt(D, n);
|
|
||||||
if (n > 0) {
|
|
||||||
/* 'abslineinfo' is an array of structures of int's */
|
|
||||||
dumpAlign(D, sizeof(int));
|
|
||||||
dumpVector(D, f->abslineinfo, cast_uint(n));
|
|
||||||
}
|
|
||||||
n = (D->strip) ? 0 : f->sizelocvars;
|
n = (D->strip) ? 0 : f->sizelocvars;
|
||||||
dumpInt(D, n);
|
DumpInt(n, D);
|
||||||
for (i = 0; i < n; i++) {
|
for (i = 0; i < n; i++) {
|
||||||
dumpString(D, f->locvars[i].varname);
|
DumpString(f->locvars[i].varname, D);
|
||||||
dumpInt(D, f->locvars[i].startpc);
|
DumpInt(f->locvars[i].startpc, D);
|
||||||
dumpInt(D, f->locvars[i].endpc);
|
DumpInt(f->locvars[i].endpc, D);
|
||||||
}
|
}
|
||||||
n = (D->strip) ? 0 : f->sizeupvalues;
|
n = (D->strip) ? 0 : f->sizeupvalues;
|
||||||
dumpInt(D, n);
|
DumpInt(n, D);
|
||||||
for (i = 0; i < n; i++)
|
for (i = 0; i < n; i++)
|
||||||
dumpString(D, f->upvalues[i].name);
|
DumpString(f->upvalues[i].name, D);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
static void dumpFunction (DumpState *D, const Proto *f) {
|
static void DumpFunction (const Proto *f, TString *psource, DumpState *D) {
|
||||||
dumpInt(D, f->linedefined);
|
if (D->strip || f->source == psource)
|
||||||
dumpInt(D, f->lastlinedefined);
|
DumpString(NULL, D); /* no debug info or same source as its parent */
|
||||||
dumpByte(D, f->numparams);
|
else
|
||||||
dumpByte(D, f->flag);
|
DumpString(f->source, D);
|
||||||
dumpByte(D, f->maxstacksize);
|
DumpInt(f->linedefined, D);
|
||||||
dumpCode(D, f);
|
DumpInt(f->lastlinedefined, D);
|
||||||
dumpConstants(D, f);
|
DumpByte(f->numparams, D);
|
||||||
dumpUpvalues(D, f);
|
DumpByte(f->is_vararg, D);
|
||||||
dumpProtos(D, f);
|
DumpByte(f->maxstacksize, D);
|
||||||
dumpString(D, D->strip ? NULL : f->source);
|
DumpCode(f, D);
|
||||||
dumpDebug(D, f);
|
DumpConstants(f, D);
|
||||||
|
DumpUpvalues(f, D);
|
||||||
|
DumpProtos(f, D);
|
||||||
|
DumpDebug(f, D);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
#define dumpNumInfo(D, tvar, value) \
|
static void DumpHeader (DumpState *D) {
|
||||||
{ tvar i = value; dumpByte(D, sizeof(tvar)); dumpVar(D, i); }
|
DumpLiteral(LUA_SIGNATURE, D);
|
||||||
|
DumpByte(LUAC_VERSION, D);
|
||||||
|
DumpByte(LUAC_FORMAT, D);
|
||||||
static void dumpHeader (DumpState *D) {
|
DumpLiteral(LUAC_DATA, D);
|
||||||
dumpLiteral(D, LUA_SIGNATURE);
|
DumpByte(sizeof(int), D);
|
||||||
dumpByte(D, LUAC_VERSION);
|
DumpByte(sizeof(size_t), D);
|
||||||
dumpByte(D, LUAC_FORMAT);
|
DumpByte(sizeof(Instruction), D);
|
||||||
dumpLiteral(D, LUAC_DATA);
|
DumpByte(sizeof(lua_Integer), D);
|
||||||
dumpNumInfo(D, int, LUAC_INT);
|
DumpByte(sizeof(lua_Number), D);
|
||||||
dumpNumInfo(D, Instruction, LUAC_INST);
|
DumpInteger(LUAC_INT, D);
|
||||||
dumpNumInfo(D, lua_Integer, LUAC_INT);
|
DumpNumber(LUAC_NUM, D);
|
||||||
dumpNumInfo(D, lua_Number, LUAC_NUM);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/*
|
/*
|
||||||
** dump Lua function as precompiled chunk
|
** dump Lua function as precompiled chunk
|
||||||
*/
|
*/
|
||||||
int luaU_dump (lua_State *L, const Proto *f, lua_Writer w, void *data,
|
int luaU_dump(lua_State *L, const Proto *f, lua_Writer w, void *data,
|
||||||
int strip) {
|
int strip) {
|
||||||
DumpState D;
|
DumpState D;
|
||||||
D.h = luaH_new(L); /* aux. table to keep strings already dumped */
|
|
||||||
sethvalue2s(L, L->top.p, D.h); /* anchor it */
|
|
||||||
L->top.p++;
|
|
||||||
D.L = L;
|
D.L = L;
|
||||||
D.writer = w;
|
D.writer = w;
|
||||||
D.offset = 0;
|
|
||||||
D.data = data;
|
D.data = data;
|
||||||
D.strip = strip;
|
D.strip = strip;
|
||||||
D.status = 0;
|
D.status = 0;
|
||||||
D.nstr = 0;
|
DumpHeader(&D);
|
||||||
dumpHeader(&D);
|
DumpByte(f->sizeupvalues, &D);
|
||||||
dumpByte(&D, f->sizeupvalues);
|
DumpFunction(f, NULL, &D);
|
||||||
dumpFunction(&D, f);
|
|
||||||
dumpBlock(&D, NULL, 0); /* signal end of dump */
|
|
||||||
return D.status;
|
return D.status;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -1,5 +1,5 @@
|
|||||||
/*
|
/*
|
||||||
** $Id: lfunc.c $
|
** $Id: lfunc.c,v 2.45.1.1 2017/04/19 17:39:34 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
|
||||||
*/
|
*/
|
||||||
@@ -14,8 +14,6 @@
|
|||||||
|
|
||||||
#include "lua.h"
|
#include "lua.h"
|
||||||
|
|
||||||
#include "ldebug.h"
|
|
||||||
#include "ldo.h"
|
|
||||||
#include "lfunc.h"
|
#include "lfunc.h"
|
||||||
#include "lgc.h"
|
#include "lgc.h"
|
||||||
#include "lmem.h"
|
#include "lmem.h"
|
||||||
@@ -24,54 +22,56 @@
|
|||||||
|
|
||||||
|
|
||||||
|
|
||||||
CClosure *luaF_newCclosure (lua_State *L, int nupvals) {
|
CClosure *luaF_newCclosure (lua_State *L, int n) {
|
||||||
GCObject *o = luaC_newobj(L, LUA_VCCL, sizeCclosure(nupvals));
|
GCObject *o = luaC_newobj(L, LUA_TCCL, sizeCclosure(n));
|
||||||
CClosure *c = gco2ccl(o);
|
CClosure *c = gco2ccl(o);
|
||||||
c->nupvalues = cast_byte(nupvals);
|
c->nupvalues = cast_byte(n);
|
||||||
return c;
|
return c;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
LClosure *luaF_newLclosure (lua_State *L, int nupvals) {
|
LClosure *luaF_newLclosure (lua_State *L, int n) {
|
||||||
GCObject *o = luaC_newobj(L, LUA_VLCL, sizeLclosure(nupvals));
|
GCObject *o = luaC_newobj(L, LUA_TLCL, sizeLclosure(n));
|
||||||
LClosure *c = gco2lcl(o);
|
LClosure *c = gco2lcl(o);
|
||||||
c->p = NULL;
|
c->p = NULL;
|
||||||
c->nupvalues = cast_byte(nupvals);
|
c->nupvalues = cast_byte(n);
|
||||||
while (nupvals--) c->upvals[nupvals] = NULL;
|
while (n--) c->upvals[n] = NULL;
|
||||||
return c;
|
return c;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/*
|
/*
|
||||||
** fill a closure with new closed upvalues
|
** fill a closure with new closed upvalues
|
||||||
*/
|
*/
|
||||||
void luaF_initupvals (lua_State *L, LClosure *cl) {
|
void luaF_initupvals (lua_State *L, LClosure *cl) {
|
||||||
int i;
|
int i;
|
||||||
for (i = 0; i < cl->nupvalues; i++) {
|
for (i = 0; i < cl->nupvalues; i++) {
|
||||||
GCObject *o = luaC_newobj(L, LUA_VUPVAL, sizeof(UpVal));
|
UpVal *uv = luaM_new(L, UpVal);
|
||||||
UpVal *uv = gco2upv(o);
|
uv->refcount = 1;
|
||||||
uv->v.p = &uv->u.value; /* make it closed */
|
uv->v = &uv->u.value; /* make it closed */
|
||||||
setnilvalue(uv->v.p);
|
setnilvalue(uv->v);
|
||||||
cl->upvals[i] = uv;
|
cl->upvals[i] = uv;
|
||||||
luaC_objbarrier(L, cl, uv);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/*
|
UpVal *luaF_findupval (lua_State *L, StkId level) {
|
||||||
** Create a new upvalue at the given level, and link it to the list of
|
UpVal **pp = &L->openupval;
|
||||||
** open upvalues of 'L' after entry 'prev'.
|
UpVal *p;
|
||||||
**/
|
UpVal *uv;
|
||||||
static UpVal *newupval (lua_State *L, StkId level, UpVal **prev) {
|
lua_assert(isintwups(L) || L->openupval == NULL);
|
||||||
GCObject *o = luaC_newobj(L, LUA_VUPVAL, sizeof(UpVal));
|
while (*pp != NULL && (p = *pp)->v >= level) {
|
||||||
UpVal *uv = gco2upv(o);
|
lua_assert(upisopen(p));
|
||||||
UpVal *next = *prev;
|
if (p->v == level) /* found a corresponding upvalue? */
|
||||||
uv->v.p = s2v(level); /* current value lives in the stack */
|
return p; /* return it */
|
||||||
uv->u.open.next = next; /* link it to list of open upvalues */
|
pp = &p->u.open.next;
|
||||||
uv->u.open.previous = prev;
|
}
|
||||||
if (next)
|
/* not found: create a new upvalue */
|
||||||
next->u.open.previous = &uv->u.open.next;
|
uv = luaM_new(L, UpVal);
|
||||||
*prev = uv;
|
uv->refcount = 0;
|
||||||
|
uv->u.open.next = *pp; /* link it to list of open upvalues */
|
||||||
|
uv->u.open.touched = 1;
|
||||||
|
*pp = uv;
|
||||||
|
uv->v = level; /* current value lives in the stack */
|
||||||
if (!isintwups(L)) { /* thread not in list of threads with upvalues? */
|
if (!isintwups(L)) { /* thread not in list of threads with upvalues? */
|
||||||
L->twups = G(L)->twups; /* link it to the list */
|
L->twups = G(L)->twups; /* link it to the list */
|
||||||
G(L)->twups = L;
|
G(L)->twups = L;
|
||||||
@@ -80,183 +80,38 @@ static UpVal *newupval (lua_State *L, StkId level, UpVal **prev) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/*
|
void luaF_close (lua_State *L, StkId level) {
|
||||||
** Find and reuse, or create if it does not exist, an upvalue
|
|
||||||
** at the given level.
|
|
||||||
*/
|
|
||||||
UpVal *luaF_findupval (lua_State *L, StkId level) {
|
|
||||||
UpVal **pp = &L->openupval;
|
|
||||||
UpVal *p;
|
|
||||||
lua_assert(isintwups(L) || L->openupval == NULL);
|
|
||||||
while ((p = *pp) != NULL && uplevel(p) >= level) { /* search for it */
|
|
||||||
lua_assert(!isdead(G(L), p));
|
|
||||||
if (uplevel(p) == level) /* corresponding upvalue? */
|
|
||||||
return p; /* return it */
|
|
||||||
pp = &p->u.open.next;
|
|
||||||
}
|
|
||||||
/* not found: create a new upvalue after 'pp' */
|
|
||||||
return newupval(L, level, pp);
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
/*
|
|
||||||
** Call closing method for object 'obj' with error object 'err'. The
|
|
||||||
** boolean 'yy' controls whether the call is yieldable.
|
|
||||||
** (This function assumes EXTRA_STACK.)
|
|
||||||
*/
|
|
||||||
static void callclosemethod (lua_State *L, TValue *obj, TValue *err, int yy) {
|
|
||||||
StkId top = L->top.p;
|
|
||||||
StkId func = top;
|
|
||||||
const TValue *tm = luaT_gettmbyobj(L, obj, TM_CLOSE);
|
|
||||||
setobj2s(L, top++, tm); /* will call metamethod... */
|
|
||||||
setobj2s(L, top++, obj); /* with 'self' as the 1st argument */
|
|
||||||
if (err != NULL) /* if there was an error... */
|
|
||||||
setobj2s(L, top++, err); /* then error object will be 2nd argument */
|
|
||||||
L->top.p = top; /* add function and arguments */
|
|
||||||
if (yy)
|
|
||||||
luaD_call(L, func, 0);
|
|
||||||
else
|
|
||||||
luaD_callnoyield(L, func, 0);
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
/*
|
|
||||||
** Check whether object at given level has a close metamethod and raise
|
|
||||||
** an error if not.
|
|
||||||
*/
|
|
||||||
static void checkclosemth (lua_State *L, StkId level) {
|
|
||||||
const TValue *tm = luaT_gettmbyobj(L, s2v(level), TM_CLOSE);
|
|
||||||
if (ttisnil(tm)) { /* no metamethod? */
|
|
||||||
int idx = cast_int(level - L->ci->func.p); /* variable index */
|
|
||||||
const char *vname = luaG_findlocal(L, L->ci, idx, NULL);
|
|
||||||
if (vname == NULL) vname = "?";
|
|
||||||
luaG_runerror(L, "variable '%s' got a non-closable value", vname);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
/*
|
|
||||||
** Prepare and call a closing method.
|
|
||||||
** If status is CLOSEKTOP, the call to the closing method will be pushed
|
|
||||||
** at the top of the stack. Otherwise, values can be pushed right after
|
|
||||||
** the 'level' of the upvalue being closed, as everything after that
|
|
||||||
** won't be used again.
|
|
||||||
*/
|
|
||||||
static void prepcallclosemth (lua_State *L, StkId level, TStatus status,
|
|
||||||
int yy) {
|
|
||||||
TValue *uv = s2v(level); /* value being closed */
|
|
||||||
TValue *errobj;
|
|
||||||
switch (status) {
|
|
||||||
case LUA_OK:
|
|
||||||
L->top.p = level + 1; /* call will be at this level */
|
|
||||||
/* FALLTHROUGH */
|
|
||||||
case CLOSEKTOP: /* don't need to change top */
|
|
||||||
errobj = NULL; /* no error object */
|
|
||||||
break;
|
|
||||||
default: /* 'luaD_seterrorobj' will set top to level + 2 */
|
|
||||||
errobj = s2v(level + 1); /* error object goes after 'uv' */
|
|
||||||
luaD_seterrorobj(L, status, level + 1); /* set error object */
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
callclosemethod(L, uv, errobj, yy);
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
/* Maximum value for deltas in 'tbclist' */
|
|
||||||
#define MAXDELTA USHRT_MAX
|
|
||||||
|
|
||||||
|
|
||||||
/*
|
|
||||||
** Insert a variable in the list of to-be-closed variables.
|
|
||||||
*/
|
|
||||||
void luaF_newtbcupval (lua_State *L, StkId level) {
|
|
||||||
lua_assert(level > L->tbclist.p);
|
|
||||||
if (l_isfalse(s2v(level)))
|
|
||||||
return; /* false doesn't need to be closed */
|
|
||||||
checkclosemth(L, level); /* value must have a close method */
|
|
||||||
while (cast_uint(level - L->tbclist.p) > MAXDELTA) {
|
|
||||||
L->tbclist.p += MAXDELTA; /* create a dummy node at maximum delta */
|
|
||||||
L->tbclist.p->tbclist.delta = 0;
|
|
||||||
}
|
|
||||||
level->tbclist.delta = cast(unsigned short, level - L->tbclist.p);
|
|
||||||
L->tbclist.p = level;
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
void luaF_unlinkupval (UpVal *uv) {
|
|
||||||
lua_assert(upisopen(uv));
|
|
||||||
*uv->u.open.previous = uv->u.open.next;
|
|
||||||
if (uv->u.open.next)
|
|
||||||
uv->u.open.next->u.open.previous = uv->u.open.previous;
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
/*
|
|
||||||
** Close all upvalues up to the given stack level.
|
|
||||||
*/
|
|
||||||
void luaF_closeupval (lua_State *L, StkId level) {
|
|
||||||
UpVal *uv;
|
UpVal *uv;
|
||||||
while ((uv = L->openupval) != NULL && uplevel(uv) >= level) {
|
while (L->openupval != NULL && (uv = L->openupval)->v >= level) {
|
||||||
TValue *slot = &uv->u.value; /* new position for value */
|
lua_assert(upisopen(uv));
|
||||||
lua_assert(uplevel(uv) < L->top.p);
|
L->openupval = uv->u.open.next; /* remove from 'open' list */
|
||||||
luaF_unlinkupval(uv); /* remove upvalue from 'openupval' list */
|
if (uv->refcount == 0) /* no references? */
|
||||||
setobj(L, slot, uv->v.p); /* move value to upvalue slot */
|
luaM_free(L, uv); /* free upvalue */
|
||||||
uv->v.p = slot; /* now current value lives here */
|
else {
|
||||||
if (!iswhite(uv)) { /* neither white nor dead? */
|
setobj(L, &uv->u.value, uv->v); /* move value to upvalue slot */
|
||||||
nw2black(uv); /* closed upvalues cannot be gray */
|
uv->v = &uv->u.value; /* now current value lives here */
|
||||||
luaC_barrier(L, uv, slot);
|
luaC_upvalbarrier(L, uv);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/*
|
|
||||||
** Remove first element from the tbclist plus its dummy nodes.
|
|
||||||
*/
|
|
||||||
static void poptbclist (lua_State *L) {
|
|
||||||
StkId tbc = L->tbclist.p;
|
|
||||||
lua_assert(tbc->tbclist.delta > 0); /* first element cannot be dummy */
|
|
||||||
tbc -= tbc->tbclist.delta;
|
|
||||||
while (tbc > L->stack.p && tbc->tbclist.delta == 0)
|
|
||||||
tbc -= MAXDELTA; /* remove dummy nodes */
|
|
||||||
L->tbclist.p = tbc;
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
/*
|
|
||||||
** Close all upvalues and to-be-closed variables up to the given stack
|
|
||||||
** level. Return restored 'level'.
|
|
||||||
*/
|
|
||||||
StkId luaF_close (lua_State *L, StkId level, TStatus status, int yy) {
|
|
||||||
ptrdiff_t levelrel = savestack(L, level);
|
|
||||||
luaF_closeupval(L, level); /* first, close the upvalues */
|
|
||||||
while (L->tbclist.p >= level) { /* traverse tbc's down to that level */
|
|
||||||
StkId tbc = L->tbclist.p; /* get variable index */
|
|
||||||
poptbclist(L); /* remove it from list */
|
|
||||||
prepcallclosemth(L, tbc, status, yy); /* close variable */
|
|
||||||
level = restorestack(L, levelrel);
|
|
||||||
}
|
|
||||||
return level;
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
Proto *luaF_newproto (lua_State *L) {
|
Proto *luaF_newproto (lua_State *L) {
|
||||||
GCObject *o = luaC_newobj(L, LUA_VPROTO, sizeof(Proto));
|
GCObject *o = luaC_newobj(L, LUA_TPROTO, sizeof(Proto));
|
||||||
Proto *f = gco2p(o);
|
Proto *f = gco2p(o);
|
||||||
f->k = NULL;
|
f->k = NULL;
|
||||||
f->sizek = 0;
|
f->sizek = 0;
|
||||||
f->p = NULL;
|
f->p = NULL;
|
||||||
f->sizep = 0;
|
f->sizep = 0;
|
||||||
f->code = NULL;
|
f->code = NULL;
|
||||||
|
f->cache = NULL;
|
||||||
f->sizecode = 0;
|
f->sizecode = 0;
|
||||||
f->lineinfo = NULL;
|
f->lineinfo = NULL;
|
||||||
f->sizelineinfo = 0;
|
f->sizelineinfo = 0;
|
||||||
f->abslineinfo = NULL;
|
|
||||||
f->sizeabslineinfo = 0;
|
|
||||||
f->upvalues = NULL;
|
f->upvalues = NULL;
|
||||||
f->sizeupvalues = 0;
|
f->sizeupvalues = 0;
|
||||||
f->numparams = 0;
|
f->numparams = 0;
|
||||||
f->flag = 0;
|
f->is_vararg = 0;
|
||||||
f->maxstacksize = 0;
|
f->maxstacksize = 0;
|
||||||
f->locvars = NULL;
|
f->locvars = NULL;
|
||||||
f->sizelocvars = 0;
|
f->sizelocvars = 0;
|
||||||
@@ -267,31 +122,13 @@ Proto *luaF_newproto (lua_State *L) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
lu_mem luaF_protosize (Proto *p) {
|
|
||||||
lu_mem sz = cast(lu_mem, sizeof(Proto))
|
|
||||||
+ cast_uint(p->sizep) * sizeof(Proto*)
|
|
||||||
+ cast_uint(p->sizek) * sizeof(TValue)
|
|
||||||
+ cast_uint(p->sizelocvars) * sizeof(LocVar)
|
|
||||||
+ cast_uint(p->sizeupvalues) * sizeof(Upvaldesc);
|
|
||||||
if (!(p->flag & PF_FIXED)) {
|
|
||||||
sz += cast_uint(p->sizecode) * sizeof(Instruction);
|
|
||||||
sz += cast_uint(p->sizelineinfo) * sizeof(lu_byte);
|
|
||||||
sz += cast_uint(p->sizeabslineinfo) * sizeof(AbsLineInfo);
|
|
||||||
}
|
|
||||||
return sz;
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
void luaF_freeproto (lua_State *L, Proto *f) {
|
void luaF_freeproto (lua_State *L, Proto *f) {
|
||||||
if (!(f->flag & PF_FIXED)) {
|
luaM_freearray(L, f->code, f->sizecode);
|
||||||
luaM_freearray(L, f->code, cast_sizet(f->sizecode));
|
luaM_freearray(L, f->p, f->sizep);
|
||||||
luaM_freearray(L, f->lineinfo, cast_sizet(f->sizelineinfo));
|
luaM_freearray(L, f->k, f->sizek);
|
||||||
luaM_freearray(L, f->abslineinfo, cast_sizet(f->sizeabslineinfo));
|
luaM_freearray(L, f->lineinfo, f->sizelineinfo);
|
||||||
}
|
luaM_freearray(L, f->locvars, f->sizelocvars);
|
||||||
luaM_freearray(L, f->p, cast_sizet(f->sizep));
|
luaM_freearray(L, f->upvalues, f->sizeupvalues);
|
||||||
luaM_freearray(L, f->k, cast_sizet(f->sizek));
|
|
||||||
luaM_freearray(L, f->locvars, cast_sizet(f->sizelocvars));
|
|
||||||
luaM_freearray(L, f->upvalues, cast_sizet(f->sizeupvalues));
|
|
||||||
luaM_free(L, f);
|
luaM_free(L, f);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -1,5 +1,5 @@
|
|||||||
/*
|
/*
|
||||||
** $Id: lfunc.h $
|
** $Id: lfunc.h,v 2.15.1.1 2017/04/19 17:39:34 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
|
||||||
*/
|
*/
|
||||||
@@ -11,11 +11,11 @@
|
|||||||
#include "lobject.h"
|
#include "lobject.h"
|
||||||
|
|
||||||
|
|
||||||
#define sizeCclosure(n) \
|
#define sizeCclosure(n) (cast(int, sizeof(CClosure)) + \
|
||||||
(offsetof(CClosure, upvalue) + sizeof(TValue) * cast_uint(n))
|
cast(int, sizeof(TValue)*((n)-1)))
|
||||||
|
|
||||||
#define sizeLclosure(n) \
|
#define sizeLclosure(n) (cast(int, sizeof(LClosure)) + \
|
||||||
(offsetof(LClosure, upvals) + sizeof(UpVal *) * cast_uint(n))
|
cast(int, sizeof(TValue *)*((n)-1)))
|
||||||
|
|
||||||
|
|
||||||
/* test whether thread is in 'twups' list */
|
/* test whether thread is in 'twups' list */
|
||||||
@@ -29,34 +29,30 @@
|
|||||||
#define MAXUPVAL 255
|
#define MAXUPVAL 255
|
||||||
|
|
||||||
|
|
||||||
#define upisopen(up) ((up)->v.p != &(up)->u.value)
|
|
||||||
|
|
||||||
|
|
||||||
#define uplevel(up) check_exp(upisopen(up), cast(StkId, (up)->v.p))
|
|
||||||
|
|
||||||
|
|
||||||
/*
|
/*
|
||||||
** maximum number of misses before giving up the cache of closures
|
** Upvalues for Lua closures
|
||||||
** in prototypes
|
|
||||||
*/
|
*/
|
||||||
#define MAXMISS 10
|
struct UpVal {
|
||||||
|
TValue *v; /* points to stack or to its own value */
|
||||||
|
lu_mem refcount; /* reference counter */
|
||||||
|
union {
|
||||||
|
struct { /* (when open) */
|
||||||
|
UpVal *next; /* linked list */
|
||||||
|
int touched; /* mark to avoid cycles with dead threads */
|
||||||
|
} open;
|
||||||
|
TValue value; /* the value (when closed) */
|
||||||
|
} u;
|
||||||
|
};
|
||||||
|
|
||||||
|
#define upisopen(up) ((up)->v != &(up)->u.value)
|
||||||
|
|
||||||
/* special status to close upvalues preserving the top of the stack */
|
|
||||||
#define CLOSEKTOP (LUA_ERRERR + 1)
|
|
||||||
|
|
||||||
|
|
||||||
LUAI_FUNC Proto *luaF_newproto (lua_State *L);
|
LUAI_FUNC Proto *luaF_newproto (lua_State *L);
|
||||||
LUAI_FUNC CClosure *luaF_newCclosure (lua_State *L, int nupvals);
|
LUAI_FUNC CClosure *luaF_newCclosure (lua_State *L, int nelems);
|
||||||
LUAI_FUNC LClosure *luaF_newLclosure (lua_State *L, int nupvals);
|
LUAI_FUNC LClosure *luaF_newLclosure (lua_State *L, int nelems);
|
||||||
LUAI_FUNC void luaF_initupvals (lua_State *L, LClosure *cl);
|
LUAI_FUNC void luaF_initupvals (lua_State *L, LClosure *cl);
|
||||||
LUAI_FUNC UpVal *luaF_findupval (lua_State *L, StkId level);
|
LUAI_FUNC UpVal *luaF_findupval (lua_State *L, StkId level);
|
||||||
LUAI_FUNC void luaF_newtbcupval (lua_State *L, StkId level);
|
LUAI_FUNC void luaF_close (lua_State *L, StkId level);
|
||||||
LUAI_FUNC void luaF_closeupval (lua_State *L, StkId level);
|
|
||||||
LUAI_FUNC StkId luaF_close (lua_State *L, StkId level, TStatus status, int yy);
|
|
||||||
LUAI_FUNC void luaF_unlinkupval (UpVal *uv);
|
|
||||||
LUAI_FUNC lu_mem luaF_protosize (Proto *p);
|
|
||||||
LUAI_FUNC void luaF_freeproto (lua_State *L, Proto *f);
|
LUAI_FUNC void luaF_freeproto (lua_State *L, Proto *f);
|
||||||
LUAI_FUNC const char *luaF_getlocalname (const Proto *func, int local_number,
|
LUAI_FUNC const char *luaF_getlocalname (const Proto *func, int local_number,
|
||||||
int pc);
|
int pc);
|
||||||
|
|||||||
@@ -1,5 +1,5 @@
|
|||||||
/*
|
/*
|
||||||
** $Id: lgc.h $
|
** $Id: lgc.h,v 2.91.1.1 2017/04/19 17:39:34 roberto Exp $
|
||||||
** Garbage Collector
|
** Garbage Collector
|
||||||
** See Copyright Notice in lua.h
|
** See Copyright Notice in lua.h
|
||||||
*/
|
*/
|
||||||
@@ -8,39 +8,42 @@
|
|||||||
#define lgc_h
|
#define lgc_h
|
||||||
|
|
||||||
|
|
||||||
#include <stddef.h>
|
|
||||||
|
|
||||||
|
|
||||||
#include "lobject.h"
|
#include "lobject.h"
|
||||||
#include "lstate.h"
|
#include "lstate.h"
|
||||||
|
|
||||||
/*
|
/*
|
||||||
** Collectable objects may have one of three colors: white, which means
|
** Collectable objects may have one of three colors: white, which
|
||||||
** the object is not marked; gray, which means the object is marked, but
|
** means the object is not marked; gray, which means the
|
||||||
** its references may be not marked; and black, which means that the
|
** object is marked, but its references may be not marked; and
|
||||||
** object and all its references are marked. The main invariant of the
|
** black, which means that the object and all its references are marked.
|
||||||
** garbage collector, while marking objects, is that a black object can
|
** The main invariant of the garbage collector, while marking objects,
|
||||||
** never point to a white one. Moreover, any gray object must be in a
|
** is that a black object can never point to a white one. Moreover,
|
||||||
** "gray list" (gray, grayagain, weak, allweak, ephemeron) so that it
|
** any gray object must be in a "gray list" (gray, grayagain, weak,
|
||||||
** can be visited again before finishing the collection cycle. (Open
|
** allweak, ephemeron) so that it can be visited again before finishing
|
||||||
** upvalues are an exception to this rule, as they are attached to
|
** the collection cycle. These lists have no meaning when the invariant
|
||||||
** a corresponding thread.) These lists have no meaning when the
|
** is not being enforced (e.g., sweep phase).
|
||||||
** invariant is not being enforced (e.g., sweep phase).
|
|
||||||
*/
|
*/
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
/* how much to allocate before next GC step */
|
||||||
|
#if !defined(GCSTEPSIZE)
|
||||||
|
/* ~100 small strings */
|
||||||
|
#define GCSTEPSIZE (cast_int(100 * sizeof(TString)))
|
||||||
|
#endif
|
||||||
|
|
||||||
|
|
||||||
/*
|
/*
|
||||||
** Possible states of the Garbage Collector
|
** Possible states of the Garbage Collector
|
||||||
*/
|
*/
|
||||||
#define GCSpropagate 0
|
#define GCSpropagate 0
|
||||||
#define GCSenteratomic 1
|
#define GCSatomic 1
|
||||||
#define GCSatomic 2
|
#define GCSswpallgc 2
|
||||||
#define GCSswpallgc 3
|
#define GCSswpfinobj 3
|
||||||
#define GCSswpfinobj 4
|
#define GCSswptobefnz 4
|
||||||
#define GCSswptobefnz 5
|
#define GCSswpend 5
|
||||||
#define GCSswpend 6
|
#define GCScallfin 6
|
||||||
#define GCScallfin 7
|
#define GCSpause 7
|
||||||
#define GCSpause 8
|
|
||||||
|
|
||||||
|
|
||||||
#define issweepphase(g) \
|
#define issweepphase(g) \
|
||||||
@@ -49,10 +52,10 @@
|
|||||||
|
|
||||||
/*
|
/*
|
||||||
** macro to tell when main invariant (white objects cannot point to black
|
** macro to tell when main invariant (white objects cannot point to black
|
||||||
** ones) must be kept. During a collection, the sweep phase may break
|
** ones) must be kept. During a collection, the sweep
|
||||||
** the invariant, as objects turned white may point to still-black
|
** phase may break the invariant, as objects turned white may point to
|
||||||
** objects. The invariant is restored when sweep ends and all objects
|
** still-black objects. The invariant is restored when sweep ends and
|
||||||
** are white again.
|
** all objects are white again.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
#define keepinvariant(g) ((g)->gcstate <= GCSatomic)
|
#define keepinvariant(g) ((g)->gcstate <= GCSatomic)
|
||||||
@@ -61,7 +64,7 @@
|
|||||||
/*
|
/*
|
||||||
** some useful bit tricks
|
** some useful bit tricks
|
||||||
*/
|
*/
|
||||||
#define resetbits(x,m) ((x) &= cast_byte(~(m)))
|
#define resetbits(x,m) ((x) &= cast(lu_byte, ~(m)))
|
||||||
#define setbits(x,m) ((x) |= (m))
|
#define setbits(x,m) ((x) |= (m))
|
||||||
#define testbits(x,m) ((x) & (m))
|
#define testbits(x,m) ((x) & (m))
|
||||||
#define bitmask(b) (1<<(b))
|
#define bitmask(b) (1<<(b))
|
||||||
@@ -71,19 +74,12 @@
|
|||||||
#define testbit(x,b) testbits(x, bitmask(b))
|
#define testbit(x,b) testbits(x, bitmask(b))
|
||||||
|
|
||||||
|
|
||||||
/*
|
/* Layout for bit use in 'marked' field: */
|
||||||
** Layout for bit use in 'marked' field. First three bits are
|
#define WHITE0BIT 0 /* object is white (type 0) */
|
||||||
** used for object "age" in generational mode. Last bit is used
|
#define WHITE1BIT 1 /* object is white (type 1) */
|
||||||
** by tests.
|
#define BLACKBIT 2 /* object is black */
|
||||||
*/
|
#define FINALIZEDBIT 3 /* object has been marked for finalization */
|
||||||
#define WHITE0BIT 3 /* object is white (type 0) */
|
/* bit 7 is currently used by tests (luaL_checkmemory) */
|
||||||
#define WHITE1BIT 4 /* object is white (type 1) */
|
|
||||||
#define BLACKBIT 5 /* object is black */
|
|
||||||
#define FINALIZEDBIT 6 /* object has been marked for finalization */
|
|
||||||
|
|
||||||
#define TESTBIT 7
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
#define WHITEBITS bit2mask(WHITE0BIT, WHITE1BIT)
|
#define WHITEBITS bit2mask(WHITE0BIT, WHITE1BIT)
|
||||||
|
|
||||||
@@ -96,173 +92,56 @@
|
|||||||
#define tofinalize(x) testbit((x)->marked, FINALIZEDBIT)
|
#define tofinalize(x) testbit((x)->marked, FINALIZEDBIT)
|
||||||
|
|
||||||
#define otherwhite(g) ((g)->currentwhite ^ WHITEBITS)
|
#define otherwhite(g) ((g)->currentwhite ^ WHITEBITS)
|
||||||
#define isdeadm(ow,m) ((m) & (ow))
|
#define isdeadm(ow,m) (!(((m) ^ WHITEBITS) & (ow)))
|
||||||
#define isdead(g,v) isdeadm(otherwhite(g), (v)->marked)
|
#define isdead(g,v) isdeadm(otherwhite(g), (v)->marked)
|
||||||
|
|
||||||
#define changewhite(x) ((x)->marked ^= WHITEBITS)
|
#define changewhite(x) ((x)->marked ^= WHITEBITS)
|
||||||
#define nw2black(x) \
|
#define gray2black(x) l_setbit((x)->marked, BLACKBIT)
|
||||||
check_exp(!iswhite(x), l_setbit((x)->marked, BLACKBIT))
|
|
||||||
|
|
||||||
#define luaC_white(g) cast_byte((g)->currentwhite & WHITEBITS)
|
#define luaC_white(g) cast(lu_byte, (g)->currentwhite & WHITEBITS)
|
||||||
|
|
||||||
|
|
||||||
/* object age in generational mode */
|
|
||||||
#define G_NEW 0 /* created in current cycle */
|
|
||||||
#define G_SURVIVAL 1 /* created in previous cycle */
|
|
||||||
#define G_OLD0 2 /* marked old by frw. barrier in this cycle */
|
|
||||||
#define G_OLD1 3 /* first full cycle as old */
|
|
||||||
#define G_OLD 4 /* really old object (not to be visited) */
|
|
||||||
#define G_TOUCHED1 5 /* old object touched this cycle */
|
|
||||||
#define G_TOUCHED2 6 /* old object touched in previous cycle */
|
|
||||||
|
|
||||||
#define AGEBITS 7 /* all age bits (111) */
|
|
||||||
|
|
||||||
#define getage(o) ((o)->marked & AGEBITS)
|
|
||||||
#define setage(o,a) ((o)->marked = cast_byte(((o)->marked & (~AGEBITS)) | a))
|
|
||||||
#define isold(o) (getage(o) > G_SURVIVAL)
|
|
||||||
|
|
||||||
|
|
||||||
/*
|
/*
|
||||||
** In generational mode, objects are created 'new'. After surviving one
|
** Does one step of collection when debt becomes positive. 'pre'/'pos'
|
||||||
** cycle, they become 'survival'. Both 'new' and 'survival' can point
|
|
||||||
** to any other object, as they are traversed at the end of the cycle.
|
|
||||||
** We call them both 'young' objects.
|
|
||||||
** If a survival object survives another cycle, it becomes 'old1'.
|
|
||||||
** 'old1' objects can still point to survival objects (but not to
|
|
||||||
** new objects), so they still must be traversed. After another cycle
|
|
||||||
** (that, being old, 'old1' objects will "survive" no matter what)
|
|
||||||
** finally the 'old1' object becomes really 'old', and then they
|
|
||||||
** are no more traversed.
|
|
||||||
**
|
|
||||||
** To keep its invariants, the generational mode uses the same barriers
|
|
||||||
** also used by the incremental mode. If a young object is caught in a
|
|
||||||
** forward barrier, it cannot become old immediately, because it can
|
|
||||||
** still point to other young objects. Instead, it becomes 'old0',
|
|
||||||
** which in the next cycle becomes 'old1'. So, 'old0' objects is
|
|
||||||
** old but can point to new and survival objects; 'old1' is old
|
|
||||||
** but cannot point to new objects; and 'old' cannot point to any
|
|
||||||
** young object.
|
|
||||||
**
|
|
||||||
** If any old object ('old0', 'old1', 'old') is caught in a back
|
|
||||||
** barrier, it becomes 'touched1' and goes into a gray list, to be
|
|
||||||
** visited at the end of the cycle. There it evolves to 'touched2',
|
|
||||||
** which can point to survivals but not to new objects. In yet another
|
|
||||||
** cycle then it becomes 'old' again.
|
|
||||||
**
|
|
||||||
** The generational mode must also control the colors of objects,
|
|
||||||
** because of the barriers. While the mutator is running, young objects
|
|
||||||
** are kept white. 'old', 'old1', and 'touched2' objects are kept black,
|
|
||||||
** as they cannot point to new objects; exceptions are threads and open
|
|
||||||
** upvalues, which age to 'old1' and 'old' but are kept gray. 'old0'
|
|
||||||
** objects may be gray or black, as in the incremental mode. 'touched1'
|
|
||||||
** objects are kept gray, as they must be visited again at the end of
|
|
||||||
** the cycle.
|
|
||||||
*/
|
|
||||||
|
|
||||||
|
|
||||||
/*
|
|
||||||
** {======================================================
|
|
||||||
** Default Values for GC parameters
|
|
||||||
** =======================================================
|
|
||||||
*/
|
|
||||||
|
|
||||||
/*
|
|
||||||
** Minor collections will shift to major ones after LUAI_MINORMAJOR%
|
|
||||||
** bytes become old.
|
|
||||||
*/
|
|
||||||
#define LUAI_MINORMAJOR 70
|
|
||||||
|
|
||||||
/*
|
|
||||||
** Major collections will shift to minor ones after a collection
|
|
||||||
** collects at least LUAI_MAJORMINOR% of the new bytes.
|
|
||||||
*/
|
|
||||||
#define LUAI_MAJORMINOR 50
|
|
||||||
|
|
||||||
/*
|
|
||||||
** A young (minor) collection will run after creating LUAI_GENMINORMUL%
|
|
||||||
** new bytes.
|
|
||||||
*/
|
|
||||||
#define LUAI_GENMINORMUL 20
|
|
||||||
|
|
||||||
|
|
||||||
/* incremental */
|
|
||||||
|
|
||||||
/* Number of bytes must be LUAI_GCPAUSE% before starting new cycle */
|
|
||||||
#define LUAI_GCPAUSE 250
|
|
||||||
|
|
||||||
/*
|
|
||||||
** Step multiplier: The collector handles LUAI_GCMUL% work units for
|
|
||||||
** each new allocated word. (Each "work unit" corresponds roughly to
|
|
||||||
** sweeping one object or traversing one slot.)
|
|
||||||
*/
|
|
||||||
#define LUAI_GCMUL 200
|
|
||||||
|
|
||||||
/* How many bytes to allocate before next GC step */
|
|
||||||
#define LUAI_GCSTEPSIZE (200 * sizeof(Table))
|
|
||||||
|
|
||||||
|
|
||||||
#define setgcparam(g,p,v) (g->gcparams[LUA_GCP##p] = luaO_codeparam(v))
|
|
||||||
#define applygcparam(g,p,x) luaO_applyparam(g->gcparams[LUA_GCP##p], x)
|
|
||||||
|
|
||||||
/* }====================================================== */
|
|
||||||
|
|
||||||
|
|
||||||
/*
|
|
||||||
** Control when GC is running:
|
|
||||||
*/
|
|
||||||
#define GCSTPUSR 1 /* bit true when GC stopped by user */
|
|
||||||
#define GCSTPGC 2 /* bit true when GC stopped by itself */
|
|
||||||
#define GCSTPCLS 4 /* bit true when closing Lua state */
|
|
||||||
#define gcrunning(g) ((g)->gcstp == 0)
|
|
||||||
|
|
||||||
|
|
||||||
/*
|
|
||||||
** Does one step of collection when debt becomes zero. 'pre'/'pos'
|
|
||||||
** allows some adjustments to be done only when needed. macro
|
** allows some adjustments to be done only when needed. macro
|
||||||
** 'condchangemem' is used only for heavy tests (forcing a full
|
** 'condchangemem' is used only for heavy tests (forcing a full
|
||||||
** GC cycle on every opportunity)
|
** GC cycle on every opportunity)
|
||||||
*/
|
*/
|
||||||
|
|
||||||
#if !defined(HARDMEMTESTS)
|
|
||||||
#define condchangemem(L,pre,pos,emg) ((void)0)
|
|
||||||
#else
|
|
||||||
#define condchangemem(L,pre,pos,emg) \
|
|
||||||
{ if (gcrunning(G(L))) { pre; luaC_fullgc(L, emg); pos; } }
|
|
||||||
#endif
|
|
||||||
|
|
||||||
#define luaC_condGC(L,pre,pos) \
|
#define luaC_condGC(L,pre,pos) \
|
||||||
{ if (G(L)->GCdebt <= 0) { pre; luaC_step(L); pos;}; \
|
{ if (G(L)->GCdebt > 0) { pre; luaC_step(L); pos;}; \
|
||||||
condchangemem(L,pre,pos,0); }
|
condchangemem(L,pre,pos); }
|
||||||
|
|
||||||
/* more often than not, 'pre'/'pos' are empty */
|
/* more often than not, 'pre'/'pos' are empty */
|
||||||
#define luaC_checkGC(L) luaC_condGC(L,(void)0,(void)0)
|
#define luaC_checkGC(L) luaC_condGC(L,(void)0,(void)0)
|
||||||
|
|
||||||
|
|
||||||
|
#define luaC_barrier(L,p,v) ( \
|
||||||
|
(iscollectable(v) && isblack(p) && iswhite(gcvalue(v))) ? \
|
||||||
|
luaC_barrier_(L,obj2gco(p),gcvalue(v)) : cast_void(0))
|
||||||
|
|
||||||
|
#define luaC_barrierback(L,p,v) ( \
|
||||||
|
(iscollectable(v) && isblack(p) && iswhite(gcvalue(v))) ? \
|
||||||
|
luaC_barrierback_(L,p) : cast_void(0))
|
||||||
|
|
||||||
#define luaC_objbarrier(L,p,o) ( \
|
#define luaC_objbarrier(L,p,o) ( \
|
||||||
(isblack(p) && iswhite(o)) ? \
|
(isblack(p) && iswhite(o)) ? \
|
||||||
luaC_barrier_(L,obj2gco(p),obj2gco(o)) : cast_void(0))
|
luaC_barrier_(L,obj2gco(p),obj2gco(o)) : cast_void(0))
|
||||||
|
|
||||||
#define luaC_barrier(L,p,v) ( \
|
#define luaC_upvalbarrier(L,uv) ( \
|
||||||
iscollectable(v) ? luaC_objbarrier(L,p,gcvalue(v)) : cast_void(0))
|
(iscollectable((uv)->v) && !upisopen(uv)) ? \
|
||||||
|
luaC_upvalbarrier_(L,uv) : cast_void(0))
|
||||||
#define luaC_objbarrierback(L,p,o) ( \
|
|
||||||
(isblack(p) && iswhite(o)) ? luaC_barrierback_(L,p) : cast_void(0))
|
|
||||||
|
|
||||||
#define luaC_barrierback(L,p,v) ( \
|
|
||||||
iscollectable(v) ? luaC_objbarrierback(L, p, gcvalue(v)) : cast_void(0))
|
|
||||||
|
|
||||||
LUAI_FUNC void luaC_fix (lua_State *L, GCObject *o);
|
LUAI_FUNC void luaC_fix (lua_State *L, GCObject *o);
|
||||||
LUAI_FUNC void luaC_freeallobjects (lua_State *L);
|
LUAI_FUNC void luaC_freeallobjects (lua_State *L);
|
||||||
LUAI_FUNC void luaC_step (lua_State *L);
|
LUAI_FUNC void luaC_step (lua_State *L);
|
||||||
LUAI_FUNC void luaC_runtilstate (lua_State *L, int state, int fast);
|
LUAI_FUNC void luaC_runtilstate (lua_State *L, int statesmask);
|
||||||
LUAI_FUNC void luaC_fullgc (lua_State *L, int isemergency);
|
LUAI_FUNC void luaC_fullgc (lua_State *L, int isemergency);
|
||||||
LUAI_FUNC GCObject *luaC_newobj (lua_State *L, lu_byte tt, size_t sz);
|
LUAI_FUNC GCObject *luaC_newobj (lua_State *L, int tt, size_t sz);
|
||||||
LUAI_FUNC GCObject *luaC_newobjdt (lua_State *L, lu_byte tt, size_t sz,
|
|
||||||
size_t offset);
|
|
||||||
LUAI_FUNC void luaC_barrier_ (lua_State *L, GCObject *o, GCObject *v);
|
LUAI_FUNC void luaC_barrier_ (lua_State *L, GCObject *o, GCObject *v);
|
||||||
LUAI_FUNC void luaC_barrierback_ (lua_State *L, GCObject *o);
|
LUAI_FUNC void luaC_barrierback_ (lua_State *L, Table *o);
|
||||||
|
LUAI_FUNC void luaC_upvalbarrier_ (lua_State *L, UpVal *uv);
|
||||||
LUAI_FUNC void luaC_checkfinalizer (lua_State *L, GCObject *o, Table *mt);
|
LUAI_FUNC void luaC_checkfinalizer (lua_State *L, GCObject *o, Table *mt);
|
||||||
LUAI_FUNC void luaC_changemode (lua_State *L, int newmode);
|
LUAI_FUNC void luaC_upvdeccount (lua_State *L, UpVal *uv);
|
||||||
|
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
|||||||
@@ -1,5 +1,5 @@
|
|||||||
/*
|
/*
|
||||||
** $Id: linit.c $
|
** $Id: linit.c,v 1.39.1.1 2017/04/19 17:20:42 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
|
||||||
*/
|
*/
|
||||||
@@ -8,6 +8,21 @@
|
|||||||
#define linit_c
|
#define linit_c
|
||||||
#define LUA_LIB
|
#define LUA_LIB
|
||||||
|
|
||||||
|
/*
|
||||||
|
** If you embed Lua in your program and need to open the standard
|
||||||
|
** libraries, call luaL_openlibs in your program. If you need a
|
||||||
|
** different set of libraries, copy this file to your project and edit
|
||||||
|
** it to suit your needs.
|
||||||
|
**
|
||||||
|
** You can also *preload* libraries, so that a later 'require' can
|
||||||
|
** open the library, which is already linked to the application.
|
||||||
|
** For that, do the following code:
|
||||||
|
**
|
||||||
|
** luaL_getsubtable(L, LUA_REGISTRYINDEX, LUA_PRELOAD_TABLE);
|
||||||
|
** lua_pushcfunction(L, luaopen_modname);
|
||||||
|
** lua_setfield(L, -2, modname);
|
||||||
|
** lua_pop(L, 1); // remove PRELOAD table
|
||||||
|
*/
|
||||||
|
|
||||||
#include "lprefix.h"
|
#include "lprefix.h"
|
||||||
|
|
||||||
@@ -18,46 +33,36 @@
|
|||||||
|
|
||||||
#include "lualib.h"
|
#include "lualib.h"
|
||||||
#include "lauxlib.h"
|
#include "lauxlib.h"
|
||||||
#include "llimits.h"
|
|
||||||
|
|
||||||
|
|
||||||
/*
|
/*
|
||||||
** Standard Libraries. (Must be listed in the same ORDER of their
|
** these libs are loaded by lua.c and are readily available to any Lua
|
||||||
** respective constants LUA_<libname>K.)
|
** program
|
||||||
*/
|
*/
|
||||||
static const luaL_Reg stdlibs[] = {
|
static const luaL_Reg loadedlibs[] = {
|
||||||
{LUA_GNAME, luaopen_base},
|
{"_G", luaopen_base},
|
||||||
{LUA_LOADLIBNAME, luaopen_package},
|
{LUA_LOADLIBNAME, luaopen_package},
|
||||||
{LUA_COLIBNAME, luaopen_coroutine},
|
{LUA_COLIBNAME, luaopen_coroutine},
|
||||||
{LUA_DBLIBNAME, luaopen_debug},
|
{LUA_TABLIBNAME, luaopen_table},
|
||||||
{LUA_IOLIBNAME, luaopen_io},
|
{LUA_IOLIBNAME, luaopen_io},
|
||||||
{LUA_MATHLIBNAME, luaopen_math},
|
|
||||||
{LUA_OSLIBNAME, luaopen_os},
|
{LUA_OSLIBNAME, luaopen_os},
|
||||||
{LUA_STRLIBNAME, luaopen_string},
|
{LUA_STRLIBNAME, luaopen_string},
|
||||||
{LUA_TABLIBNAME, luaopen_table},
|
{LUA_MATHLIBNAME, luaopen_math},
|
||||||
{LUA_UTF8LIBNAME, luaopen_utf8},
|
{LUA_UTF8LIBNAME, luaopen_utf8},
|
||||||
|
{LUA_DBLIBNAME, luaopen_debug},
|
||||||
|
#if defined(LUA_COMPAT_BITLIB)
|
||||||
|
{LUA_BITLIBNAME, luaopen_bit32},
|
||||||
|
#endif
|
||||||
{NULL, NULL}
|
{NULL, NULL}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
/*
|
LUALIB_API void luaL_openlibs (lua_State *L) {
|
||||||
** require and preload selected standard libraries
|
|
||||||
*/
|
|
||||||
LUALIB_API void luaL_openselectedlibs (lua_State *L, int load, int preload) {
|
|
||||||
int mask;
|
|
||||||
const luaL_Reg *lib;
|
const luaL_Reg *lib;
|
||||||
luaL_getsubtable(L, LUA_REGISTRYINDEX, LUA_PRELOAD_TABLE);
|
/* "require" functions from 'loadedlibs' and set results to global table */
|
||||||
for (lib = stdlibs, mask = 1; lib->name != NULL; lib++, mask <<= 1) {
|
for (lib = loadedlibs; lib->func; lib++) {
|
||||||
if (load & mask) { /* selected? */
|
luaL_requiref(L, lib->name, lib->func, 1);
|
||||||
luaL_requiref(L, lib->name, lib->func, 1); /* require library */
|
lua_pop(L, 1); /* remove lib */
|
||||||
lua_pop(L, 1); /* remove result from the stack */
|
|
||||||
}
|
|
||||||
else if (preload & mask) { /* selected? */
|
|
||||||
lua_pushcfunction(L, lib->func);
|
|
||||||
lua_setfield(L, -2, lib->name); /* add library to PRELOAD table */
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
lua_assert((mask >> 1) == LUA_UTF8LIBK);
|
|
||||||
lua_pop(L, 1); /* remove PRELOAD table */
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -1,5 +1,5 @@
|
|||||||
/*
|
/*
|
||||||
** $Id: liolib.c $
|
** $Id: liolib.c,v 2.151.1.1 2017/04/19 17:29:57 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
|
||||||
*/
|
*/
|
||||||
@@ -21,7 +21,8 @@
|
|||||||
|
|
||||||
#include "lauxlib.h"
|
#include "lauxlib.h"
|
||||||
#include "lualib.h"
|
#include "lualib.h"
|
||||||
#include "llimits.h"
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
/*
|
/*
|
||||||
@@ -38,7 +39,7 @@
|
|||||||
/* Check whether 'mode' matches '[rwa]%+?[L_MODEEXT]*' */
|
/* Check whether 'mode' matches '[rwa]%+?[L_MODEEXT]*' */
|
||||||
static int l_checkmode (const char *mode) {
|
static int l_checkmode (const char *mode) {
|
||||||
return (*mode != '\0' && strchr("rwa", *(mode++)) != NULL &&
|
return (*mode != '\0' && strchr("rwa", *(mode++)) != NULL &&
|
||||||
(*mode != '+' || ((void)(++mode), 1)) && /* skip if char is '+' */
|
(*mode != '+' || (++mode, 1)) && /* skip if char is '+' */
|
||||||
(strspn(mode, L_MODEEXT) == strlen(mode))); /* check extensions */
|
(strspn(mode, L_MODEEXT) == strlen(mode))); /* check extensions */
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -63,17 +64,11 @@ static int l_checkmode (const char *mode) {
|
|||||||
#define l_popen(L,c,m) (_popen(c,m))
|
#define l_popen(L,c,m) (_popen(c,m))
|
||||||
#define l_pclose(L,file) (_pclose(file))
|
#define l_pclose(L,file) (_pclose(file))
|
||||||
|
|
||||||
#if !defined(l_checkmodep)
|
|
||||||
/* Windows accepts "[rw][bt]?" as valid modes */
|
|
||||||
#define l_checkmodep(m) ((m[0] == 'r' || m[0] == 'w') && \
|
|
||||||
(m[1] == '\0' || ((m[1] == 'b' || m[1] == 't') && m[2] == '\0')))
|
|
||||||
#endif
|
|
||||||
|
|
||||||
#else /* }{ */
|
#else /* }{ */
|
||||||
|
|
||||||
/* ISO C definitions */
|
/* ISO C definitions */
|
||||||
#define l_popen(L,c,m) \
|
#define l_popen(L,c,m) \
|
||||||
((void)c, (void)m, \
|
((void)((void)c, m), \
|
||||||
luaL_error(L, "'popen' not supported"), \
|
luaL_error(L, "'popen' not supported"), \
|
||||||
(FILE*)0)
|
(FILE*)0)
|
||||||
#define l_pclose(L,file) ((void)L, (void)file, -1)
|
#define l_pclose(L,file) ((void)L, (void)file, -1)
|
||||||
@@ -82,12 +77,6 @@ static int l_checkmode (const char *mode) {
|
|||||||
|
|
||||||
#endif /* } */
|
#endif /* } */
|
||||||
|
|
||||||
|
|
||||||
#if !defined(l_checkmodep)
|
|
||||||
/* By default, Lua accepts only "r" or "w" as valid modes */
|
|
||||||
#define l_checkmodep(m) ((m[0] == 'r' || m[0] == 'w') && m[1] == '\0')
|
|
||||||
#endif
|
|
||||||
|
|
||||||
/* }====================================================== */
|
/* }====================================================== */
|
||||||
|
|
||||||
|
|
||||||
@@ -114,7 +103,7 @@ static int l_checkmode (const char *mode) {
|
|||||||
|
|
||||||
#if !defined(l_fseek) /* { */
|
#if !defined(l_fseek) /* { */
|
||||||
|
|
||||||
#if defined(LUA_USE_POSIX) || defined(LUA_USE_OFF_T) /* { */
|
#if defined(LUA_USE_POSIX) /* { */
|
||||||
|
|
||||||
#include <sys/types.h>
|
#include <sys/types.h>
|
||||||
|
|
||||||
@@ -144,7 +133,6 @@ static int l_checkmode (const char *mode) {
|
|||||||
/* }====================================================== */
|
/* }====================================================== */
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
#define IO_PREFIX "_IO_"
|
#define IO_PREFIX "_IO_"
|
||||||
#define IOPREF_LEN (sizeof(IO_PREFIX)/sizeof(char) - 1)
|
#define IOPREF_LEN (sizeof(IO_PREFIX)/sizeof(char) - 1)
|
||||||
#define IO_INPUT (IO_PREFIX "input")
|
#define IO_INPUT (IO_PREFIX "input")
|
||||||
@@ -164,7 +152,7 @@ static int io_type (lua_State *L) {
|
|||||||
luaL_checkany(L, 1);
|
luaL_checkany(L, 1);
|
||||||
p = (LStream *)luaL_testudata(L, 1, LUA_FILEHANDLE);
|
p = (LStream *)luaL_testudata(L, 1, LUA_FILEHANDLE);
|
||||||
if (p == NULL)
|
if (p == NULL)
|
||||||
luaL_pushfail(L); /* not a file */
|
lua_pushnil(L); /* not a file */
|
||||||
else if (isclosed(p))
|
else if (isclosed(p))
|
||||||
lua_pushliteral(L, "closed file");
|
lua_pushliteral(L, "closed file");
|
||||||
else
|
else
|
||||||
@@ -185,7 +173,7 @@ static int f_tostring (lua_State *L) {
|
|||||||
|
|
||||||
static FILE *tofile (lua_State *L) {
|
static FILE *tofile (lua_State *L) {
|
||||||
LStream *p = tolstream(L);
|
LStream *p = tolstream(L);
|
||||||
if (l_unlikely(isclosed(p)))
|
if (isclosed(p))
|
||||||
luaL_error(L, "attempt to use a closed file");
|
luaL_error(L, "attempt to use a closed file");
|
||||||
lua_assert(p->f);
|
lua_assert(p->f);
|
||||||
return p->f;
|
return p->f;
|
||||||
@@ -198,7 +186,7 @@ static FILE *tofile (lua_State *L) {
|
|||||||
** handle is in a consistent state.
|
** handle is in a consistent state.
|
||||||
*/
|
*/
|
||||||
static LStream *newprefile (lua_State *L) {
|
static LStream *newprefile (lua_State *L) {
|
||||||
LStream *p = (LStream *)lua_newuserdatauv(L, sizeof(LStream), 0);
|
LStream *p = (LStream *)lua_newuserdata(L, sizeof(LStream));
|
||||||
p->closef = NULL; /* mark file handle as 'closed' */
|
p->closef = NULL; /* mark file handle as 'closed' */
|
||||||
luaL_setmetatable(L, LUA_FILEHANDLE);
|
luaL_setmetatable(L, LUA_FILEHANDLE);
|
||||||
return p;
|
return p;
|
||||||
@@ -226,7 +214,7 @@ static int f_close (lua_State *L) {
|
|||||||
|
|
||||||
static int io_close (lua_State *L) {
|
static int io_close (lua_State *L) {
|
||||||
if (lua_isnone(L, 1)) /* no argument? */
|
if (lua_isnone(L, 1)) /* no argument? */
|
||||||
lua_getfield(L, LUA_REGISTRYINDEX, IO_OUTPUT); /* use default output */
|
lua_getfield(L, LUA_REGISTRYINDEX, IO_OUTPUT); /* use standard output */
|
||||||
return f_close(L);
|
return f_close(L);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -244,8 +232,8 @@ static int f_gc (lua_State *L) {
|
|||||||
*/
|
*/
|
||||||
static int io_fclose (lua_State *L) {
|
static int io_fclose (lua_State *L) {
|
||||||
LStream *p = tolstream(L);
|
LStream *p = tolstream(L);
|
||||||
errno = 0;
|
int res = fclose(p->f);
|
||||||
return luaL_fileresult(L, (fclose(p->f) == 0), NULL);
|
return luaL_fileresult(L, (res == 0), NULL);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@@ -260,7 +248,7 @@ static LStream *newfile (lua_State *L) {
|
|||||||
static void opencheck (lua_State *L, const char *fname, const char *mode) {
|
static void opencheck (lua_State *L, const char *fname, const char *mode) {
|
||||||
LStream *p = newfile(L);
|
LStream *p = newfile(L);
|
||||||
p->f = fopen(fname, mode);
|
p->f = fopen(fname, mode);
|
||||||
if (l_unlikely(p->f == NULL))
|
if (p->f == NULL)
|
||||||
luaL_error(L, "cannot open file '%s' (%s)", fname, strerror(errno));
|
luaL_error(L, "cannot open file '%s' (%s)", fname, strerror(errno));
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -271,7 +259,6 @@ static int io_open (lua_State *L) {
|
|||||||
LStream *p = newfile(L);
|
LStream *p = newfile(L);
|
||||||
const char *md = mode; /* to traverse/check mode */
|
const char *md = mode; /* to traverse/check mode */
|
||||||
luaL_argcheck(L, l_checkmode(md), 2, "invalid mode");
|
luaL_argcheck(L, l_checkmode(md), 2, "invalid mode");
|
||||||
errno = 0;
|
|
||||||
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;
|
||||||
}
|
}
|
||||||
@@ -282,7 +269,6 @@ static int io_open (lua_State *L) {
|
|||||||
*/
|
*/
|
||||||
static int io_pclose (lua_State *L) {
|
static int io_pclose (lua_State *L) {
|
||||||
LStream *p = tolstream(L);
|
LStream *p = tolstream(L);
|
||||||
errno = 0;
|
|
||||||
return luaL_execresult(L, l_pclose(L, p->f));
|
return luaL_execresult(L, l_pclose(L, p->f));
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -291,8 +277,6 @@ static int io_popen (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 = newprefile(L);
|
LStream *p = newprefile(L);
|
||||||
luaL_argcheck(L, l_checkmodep(mode), 2, "invalid mode");
|
|
||||||
errno = 0;
|
|
||||||
p->f = l_popen(L, filename, mode);
|
p->f = l_popen(L, filename, mode);
|
||||||
p->closef = &io_pclose;
|
p->closef = &io_pclose;
|
||||||
return (p->f == NULL) ? luaL_fileresult(L, 0, filename) : 1;
|
return (p->f == NULL) ? luaL_fileresult(L, 0, filename) : 1;
|
||||||
@@ -301,7 +285,6 @@ static int io_popen (lua_State *L) {
|
|||||||
|
|
||||||
static int io_tmpfile (lua_State *L) {
|
static int io_tmpfile (lua_State *L) {
|
||||||
LStream *p = newfile(L);
|
LStream *p = newfile(L);
|
||||||
errno = 0;
|
|
||||||
p->f = tmpfile();
|
p->f = tmpfile();
|
||||||
return (p->f == NULL) ? luaL_fileresult(L, 0, NULL) : 1;
|
return (p->f == NULL) ? luaL_fileresult(L, 0, NULL) : 1;
|
||||||
}
|
}
|
||||||
@@ -311,8 +294,8 @@ static FILE *getiofile (lua_State *L, const char *findex) {
|
|||||||
LStream *p;
|
LStream *p;
|
||||||
lua_getfield(L, LUA_REGISTRYINDEX, findex);
|
lua_getfield(L, LUA_REGISTRYINDEX, findex);
|
||||||
p = (LStream *)lua_touserdata(L, -1);
|
p = (LStream *)lua_touserdata(L, -1);
|
||||||
if (l_unlikely(isclosed(p)))
|
if (isclosed(p))
|
||||||
luaL_error(L, "default %s file is closed", findex + IOPREF_LEN);
|
luaL_error(L, "standard %s file is closed", findex + IOPREF_LEN);
|
||||||
return p->f;
|
return p->f;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -353,22 +336,12 @@ static int io_readline (lua_State *L);
|
|||||||
*/
|
*/
|
||||||
#define MAXARGLINE 250
|
#define MAXARGLINE 250
|
||||||
|
|
||||||
/*
|
|
||||||
** Auxiliary function to create the iteration function for 'lines'.
|
|
||||||
** The iteration function is a closure over 'io_readline', with
|
|
||||||
** the following upvalues:
|
|
||||||
** 1) The file being read (first value in the stack)
|
|
||||||
** 2) the number of arguments to read
|
|
||||||
** 3) a boolean, true iff file has to be closed when finished ('toclose')
|
|
||||||
** *) a variable number of format arguments (rest of the stack)
|
|
||||||
*/
|
|
||||||
static void aux_lines (lua_State *L, int toclose) {
|
static void aux_lines (lua_State *L, int toclose) {
|
||||||
int n = lua_gettop(L) - 1; /* number of arguments to read */
|
int n = lua_gettop(L) - 1; /* number of arguments to read */
|
||||||
luaL_argcheck(L, n <= MAXARGLINE, MAXARGLINE + 2, "too many arguments");
|
luaL_argcheck(L, n <= MAXARGLINE, MAXARGLINE + 2, "too many arguments");
|
||||||
lua_pushvalue(L, 1); /* file */
|
|
||||||
lua_pushinteger(L, n); /* number of arguments to read */
|
lua_pushinteger(L, n); /* number of arguments to read */
|
||||||
lua_pushboolean(L, toclose); /* close/not close file when finished */
|
lua_pushboolean(L, toclose); /* close/not close file when finished */
|
||||||
lua_rotate(L, 2, 3); /* move the three values to their positions */
|
lua_rotate(L, 2, 2); /* move 'n' and 'toclose' to their positions */
|
||||||
lua_pushcclosure(L, io_readline, 3 + n);
|
lua_pushcclosure(L, io_readline, 3 + n);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -380,11 +353,6 @@ static int f_lines (lua_State *L) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/*
|
|
||||||
** Return an iteration function for 'io.lines'. If file has to be
|
|
||||||
** closed, also returns the file itself as a second result (to be
|
|
||||||
** closed as the state at the exit of a generic for).
|
|
||||||
*/
|
|
||||||
static int io_lines (lua_State *L) {
|
static int io_lines (lua_State *L) {
|
||||||
int toclose;
|
int toclose;
|
||||||
if (lua_isnone(L, 1)) lua_pushnil(L); /* at least one argument */
|
if (lua_isnone(L, 1)) lua_pushnil(L); /* at least one argument */
|
||||||
@@ -400,15 +368,8 @@ static int io_lines (lua_State *L) {
|
|||||||
lua_replace(L, 1); /* put file at index 1 */
|
lua_replace(L, 1); /* put file at index 1 */
|
||||||
toclose = 1; /* close it after iteration */
|
toclose = 1; /* close it after iteration */
|
||||||
}
|
}
|
||||||
aux_lines(L, toclose); /* push iteration function */
|
aux_lines(L, toclose);
|
||||||
if (toclose) {
|
return 1;
|
||||||
lua_pushnil(L); /* state */
|
|
||||||
lua_pushnil(L); /* control */
|
|
||||||
lua_pushvalue(L, 1); /* file is the to-be-closed variable (4th result) */
|
|
||||||
return 4;
|
|
||||||
}
|
|
||||||
else
|
|
||||||
return 1;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@@ -438,12 +399,12 @@ typedef struct {
|
|||||||
** Add current char to buffer (if not out of space) and read next one
|
** Add current char to buffer (if not out of space) and read next one
|
||||||
*/
|
*/
|
||||||
static int nextc (RN *rn) {
|
static int nextc (RN *rn) {
|
||||||
if (l_unlikely(rn->n >= L_MAXLENNUM)) { /* buffer overflow? */
|
if (rn->n >= L_MAXLENNUM) { /* buffer overflow? */
|
||||||
rn->buff[0] = '\0'; /* invalidate result */
|
rn->buff[0] = '\0'; /* invalidate result */
|
||||||
return 0; /* fail */
|
return 0; /* fail */
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
rn->buff[rn->n++] = cast_char(rn->c); /* save current char */
|
rn->buff[rn->n++] = rn->c; /* save current char */
|
||||||
rn->c = l_getc(rn->f); /* read next one */
|
rn->c = l_getc(rn->f); /* read next one */
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
@@ -474,7 +435,7 @@ static int readdigits (RN *rn, int hex) {
|
|||||||
/*
|
/*
|
||||||
** Read a number: first reads a valid prefix of a numeral into a buffer.
|
** Read a number: first reads a valid prefix of a numeral into a buffer.
|
||||||
** Then it calls 'lua_stringtonumber' to check whether the format is
|
** Then it calls 'lua_stringtonumber' to check whether the format is
|
||||||
** correct and to convert it to a Lua number.
|
** correct and to convert it to a Lua number
|
||||||
*/
|
*/
|
||||||
static int read_number (lua_State *L, FILE *f) {
|
static int read_number (lua_State *L, FILE *f) {
|
||||||
RN rn;
|
RN rn;
|
||||||
@@ -486,7 +447,7 @@ static int read_number (lua_State *L, FILE *f) {
|
|||||||
decp[1] = '.'; /* always accept a dot */
|
decp[1] = '.'; /* always accept a dot */
|
||||||
l_lockfile(rn.f);
|
l_lockfile(rn.f);
|
||||||
do { rn.c = l_getc(rn.f); } while (isspace(rn.c)); /* skip spaces */
|
do { rn.c = l_getc(rn.f); } while (isspace(rn.c)); /* skip spaces */
|
||||||
test2(&rn, "-+"); /* optional sign */
|
test2(&rn, "-+"); /* optional signal */
|
||||||
if (test2(&rn, "00")) {
|
if (test2(&rn, "00")) {
|
||||||
if (test2(&rn, "xX")) hex = 1; /* numeral is hexadecimal */
|
if (test2(&rn, "xX")) hex = 1; /* numeral is hexadecimal */
|
||||||
else count = 1; /* count initial '0' as a valid digit */
|
else count = 1; /* count initial '0' as a valid digit */
|
||||||
@@ -495,14 +456,14 @@ static int read_number (lua_State *L, FILE *f) {
|
|||||||
if (test2(&rn, decp)) /* decimal point? */
|
if (test2(&rn, decp)) /* decimal point? */
|
||||||
count += readdigits(&rn, hex); /* fractional part */
|
count += readdigits(&rn, hex); /* fractional part */
|
||||||
if (count > 0 && test2(&rn, (hex ? "pP" : "eE"))) { /* exponent mark? */
|
if (count > 0 && test2(&rn, (hex ? "pP" : "eE"))) { /* exponent mark? */
|
||||||
test2(&rn, "-+"); /* exponent sign */
|
test2(&rn, "-+"); /* exponent signal */
|
||||||
readdigits(&rn, 0); /* exponent digits */
|
readdigits(&rn, 0); /* exponent digits */
|
||||||
}
|
}
|
||||||
ungetc(rn.c, rn.f); /* unread look-ahead char */
|
ungetc(rn.c, rn.f); /* unread look-ahead char */
|
||||||
l_unlockfile(rn.f);
|
l_unlockfile(rn.f);
|
||||||
rn.buff[rn.n] = '\0'; /* finish string */
|
rn.buff[rn.n] = '\0'; /* finish string */
|
||||||
if (l_likely(lua_stringtonumber(L, rn.buff)))
|
if (lua_stringtonumber(L, rn.buff)) /* is this a valid number? */
|
||||||
return 1; /* ok, it is a valid number */
|
return 1; /* ok */
|
||||||
else { /* invalid format */
|
else { /* invalid format */
|
||||||
lua_pushnil(L); /* "result" to be removed */
|
lua_pushnil(L); /* "result" to be removed */
|
||||||
return 0; /* read fails */
|
return 0; /* read fails */
|
||||||
@@ -520,19 +481,19 @@ static int test_eof (lua_State *L, FILE *f) {
|
|||||||
|
|
||||||
static int read_line (lua_State *L, FILE *f, int chop) {
|
static int read_line (lua_State *L, FILE *f, int chop) {
|
||||||
luaL_Buffer b;
|
luaL_Buffer b;
|
||||||
int c;
|
int c = '\0';
|
||||||
luaL_buffinit(L, &b);
|
luaL_buffinit(L, &b);
|
||||||
do { /* may need to read several chunks to get whole line */
|
while (c != EOF && c != '\n') { /* repeat until end of line */
|
||||||
char *buff = luaL_prepbuffer(&b); /* preallocate buffer space */
|
char *buff = luaL_prepbuffer(&b); /* preallocate buffer */
|
||||||
unsigned i = 0;
|
int i = 0;
|
||||||
l_lockfile(f); /* no memory errors can happen inside the lock */
|
l_lockfile(f); /* no memory errors can happen inside the lock */
|
||||||
while (i < LUAL_BUFFERSIZE && (c = l_getc(f)) != EOF && c != '\n')
|
while (i < LUAL_BUFFERSIZE && (c = l_getc(f)) != EOF && c != '\n')
|
||||||
buff[i++] = cast_char(c); /* read up to end of line or buffer limit */
|
buff[i++] = c;
|
||||||
l_unlockfile(f);
|
l_unlockfile(f);
|
||||||
luaL_addsize(&b, i);
|
luaL_addsize(&b, i);
|
||||||
} while (c != EOF && c != '\n'); /* repeat until end of line */
|
}
|
||||||
if (!chop && c == '\n') /* want a newline and have one? */
|
if (!chop && c == '\n') /* want a newline and have one? */
|
||||||
luaL_addchar(&b, '\n'); /* add ending newline to result */
|
luaL_addchar(&b, c); /* add ending newline to result */
|
||||||
luaL_pushresult(&b); /* close buffer */
|
luaL_pushresult(&b); /* close buffer */
|
||||||
/* return ok if read something (either a newline or something else) */
|
/* return ok if read something (either a newline or something else) */
|
||||||
return (c == '\n' || lua_rawlen(L, -1) > 0);
|
return (c == '\n' || lua_rawlen(L, -1) > 0);
|
||||||
@@ -567,15 +528,14 @@ static int read_chars (lua_State *L, FILE *f, size_t n) {
|
|||||||
|
|
||||||
static int g_read (lua_State *L, FILE *f, int first) {
|
static int g_read (lua_State *L, FILE *f, int first) {
|
||||||
int nargs = lua_gettop(L) - 1;
|
int nargs = lua_gettop(L) - 1;
|
||||||
int n, success;
|
int success;
|
||||||
|
int n;
|
||||||
clearerr(f);
|
clearerr(f);
|
||||||
errno = 0;
|
|
||||||
if (nargs == 0) { /* no arguments? */
|
if (nargs == 0) { /* no arguments? */
|
||||||
success = read_line(L, f, 1);
|
success = read_line(L, f, 1);
|
||||||
n = first + 1; /* to return 1 result */
|
n = first+1; /* to return 1 result */
|
||||||
}
|
}
|
||||||
else {
|
else { /* ensure stack space for all results and for auxlib's buffer */
|
||||||
/* ensure stack space for all results and for auxlib's buffer */
|
|
||||||
luaL_checkstack(L, nargs+LUA_MINSTACK, "too many arguments");
|
luaL_checkstack(L, nargs+LUA_MINSTACK, "too many arguments");
|
||||||
success = 1;
|
success = 1;
|
||||||
for (n = first; nargs-- && success; n++) {
|
for (n = first; nargs-- && success; n++) {
|
||||||
@@ -610,7 +570,7 @@ static int g_read (lua_State *L, FILE *f, int first) {
|
|||||||
return luaL_fileresult(L, 0, NULL);
|
return luaL_fileresult(L, 0, NULL);
|
||||||
if (!success) {
|
if (!success) {
|
||||||
lua_pop(L, 1); /* remove last result */
|
lua_pop(L, 1); /* remove last result */
|
||||||
luaL_pushfail(L); /* push nil instead */
|
lua_pushnil(L); /* push nil instead */
|
||||||
}
|
}
|
||||||
return n - first;
|
return n - first;
|
||||||
}
|
}
|
||||||
@@ -626,9 +586,6 @@ static int f_read (lua_State *L) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/*
|
|
||||||
** Iteration function for 'lines'.
|
|
||||||
*/
|
|
||||||
static int io_readline (lua_State *L) {
|
static int io_readline (lua_State *L) {
|
||||||
LStream *p = (LStream *)lua_touserdata(L, lua_upvalueindex(1));
|
LStream *p = (LStream *)lua_touserdata(L, lua_upvalueindex(1));
|
||||||
int i;
|
int i;
|
||||||
@@ -643,14 +600,14 @@ static int io_readline (lua_State *L) {
|
|||||||
lua_assert(n > 0); /* should return at least a nil */
|
lua_assert(n > 0); /* should return at least a nil */
|
||||||
if (lua_toboolean(L, -n)) /* read at least one value? */
|
if (lua_toboolean(L, -n)) /* read at least one value? */
|
||||||
return n; /* return them */
|
return n; /* return them */
|
||||||
else { /* first result is false: EOF or error */
|
else { /* first result is nil: EOF or error */
|
||||||
if (n > 1) { /* is there error information? */
|
if (n > 1) { /* is there error information? */
|
||||||
/* 2nd result is error message */
|
/* 2nd result is error message */
|
||||||
return luaL_error(L, "%s", lua_tostring(L, -n + 1));
|
return luaL_error(L, "%s", lua_tostring(L, -n + 1));
|
||||||
}
|
}
|
||||||
if (lua_toboolean(L, lua_upvalueindex(3))) { /* generator created file? */
|
if (lua_toboolean(L, lua_upvalueindex(3))) { /* generator created file? */
|
||||||
lua_settop(L, 0); /* clear stack */
|
lua_settop(L, 0);
|
||||||
lua_pushvalue(L, lua_upvalueindex(1)); /* push file at index 1 */
|
lua_pushvalue(L, lua_upvalueindex(1));
|
||||||
aux_close(L); /* close it */
|
aux_close(L); /* close it */
|
||||||
}
|
}
|
||||||
return 0;
|
return 0;
|
||||||
@@ -662,28 +619,25 @@ static int io_readline (lua_State *L) {
|
|||||||
|
|
||||||
static int g_write (lua_State *L, FILE *f, int arg) {
|
static int g_write (lua_State *L, FILE *f, int arg) {
|
||||||
int nargs = lua_gettop(L) - arg;
|
int nargs = lua_gettop(L) - arg;
|
||||||
size_t totalbytes = 0; /* total number of bytes written */
|
int status = 1;
|
||||||
errno = 0;
|
for (; nargs--; arg++) {
|
||||||
for (; nargs--; arg++) { /* for each argument */
|
if (lua_type(L, arg) == LUA_TNUMBER) {
|
||||||
char buff[LUA_N2SBUFFSZ];
|
/* optimization: could be done exactly as for strings */
|
||||||
const char *s;
|
int len = lua_isinteger(L, arg)
|
||||||
size_t numbytes; /* bytes written in one call to 'fwrite' */
|
? fprintf(f, LUA_INTEGER_FMT,
|
||||||
size_t len = lua_numbertocstring(L, arg, buff); /* try as a number */
|
(LUAI_UACINT)lua_tointeger(L, arg))
|
||||||
if (len > 0) { /* did conversion work (value was a number)? */
|
: fprintf(f, LUA_NUMBER_FMT,
|
||||||
s = buff;
|
(LUAI_UACNUMBER)lua_tonumber(L, arg));
|
||||||
len--;
|
status = status && (len > 0);
|
||||||
}
|
}
|
||||||
else /* must be a string */
|
else {
|
||||||
s = luaL_checklstring(L, arg, &len);
|
size_t l;
|
||||||
numbytes = fwrite(s, sizeof(char), len, f);
|
const char *s = luaL_checklstring(L, arg, &l);
|
||||||
totalbytes += numbytes;
|
status = status && (fwrite(s, sizeof(char), l, f) == l);
|
||||||
if (numbytes < len) { /* write error? */
|
|
||||||
int n = luaL_fileresult(L, 0, NULL);
|
|
||||||
lua_pushinteger(L, cast_st2S(totalbytes));
|
|
||||||
return n + 1; /* return fail, error msg., error code, and counter */
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return 1; /* no errors; file handle already on stack top */
|
if (status) return 1; /* file handle already on stack top */
|
||||||
|
else return luaL_fileresult(L, status, NULL);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@@ -708,9 +662,8 @@ static int f_seek (lua_State *L) {
|
|||||||
l_seeknum offset = (l_seeknum)p3;
|
l_seeknum offset = (l_seeknum)p3;
|
||||||
luaL_argcheck(L, (lua_Integer)offset == p3, 3,
|
luaL_argcheck(L, (lua_Integer)offset == p3, 3,
|
||||||
"not an integer in proper range");
|
"not an integer in proper range");
|
||||||
errno = 0;
|
|
||||||
op = l_fseek(f, offset, mode[op]);
|
op = l_fseek(f, offset, mode[op]);
|
||||||
if (l_unlikely(op))
|
if (op)
|
||||||
return luaL_fileresult(L, 0, NULL); /* error */
|
return luaL_fileresult(L, 0, NULL); /* error */
|
||||||
else {
|
else {
|
||||||
lua_pushinteger(L, (lua_Integer)l_ftell(f));
|
lua_pushinteger(L, (lua_Integer)l_ftell(f));
|
||||||
@@ -725,26 +678,19 @@ static int f_setvbuf (lua_State *L) {
|
|||||||
FILE *f = tofile(L);
|
FILE *f = tofile(L);
|
||||||
int op = luaL_checkoption(L, 2, NULL, modenames);
|
int op = luaL_checkoption(L, 2, NULL, modenames);
|
||||||
lua_Integer sz = luaL_optinteger(L, 3, LUAL_BUFFERSIZE);
|
lua_Integer sz = luaL_optinteger(L, 3, LUAL_BUFFERSIZE);
|
||||||
int res;
|
int res = setvbuf(f, NULL, mode[op], (size_t)sz);
|
||||||
errno = 0;
|
|
||||||
res = setvbuf(f, NULL, mode[op], (size_t)sz);
|
|
||||||
return luaL_fileresult(L, res == 0, NULL);
|
return luaL_fileresult(L, res == 0, NULL);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
static int aux_flush (lua_State *L, FILE *f) {
|
|
||||||
errno = 0;
|
static int io_flush (lua_State *L) {
|
||||||
return luaL_fileresult(L, fflush(f) == 0, NULL);
|
return luaL_fileresult(L, fflush(getiofile(L, IO_OUTPUT)) == 0, NULL);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
static int f_flush (lua_State *L) {
|
static int f_flush (lua_State *L) {
|
||||||
return aux_flush(L, tofile(L));
|
return luaL_fileresult(L, fflush(tofile(L)) == 0, NULL);
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
static int io_flush (lua_State *L) {
|
|
||||||
return aux_flush(L, getiofile(L, IO_OUTPUT));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@@ -770,37 +716,26 @@ static const luaL_Reg iolib[] = {
|
|||||||
/*
|
/*
|
||||||
** methods for file handles
|
** methods for file handles
|
||||||
*/
|
*/
|
||||||
static const luaL_Reg meth[] = {
|
static const luaL_Reg flib[] = {
|
||||||
{"read", f_read},
|
|
||||||
{"write", f_write},
|
|
||||||
{"lines", f_lines},
|
|
||||||
{"flush", f_flush},
|
|
||||||
{"seek", f_seek},
|
|
||||||
{"close", f_close},
|
{"close", f_close},
|
||||||
|
{"flush", f_flush},
|
||||||
|
{"lines", f_lines},
|
||||||
|
{"read", f_read},
|
||||||
|
{"seek", f_seek},
|
||||||
{"setvbuf", f_setvbuf},
|
{"setvbuf", f_setvbuf},
|
||||||
{NULL, NULL}
|
{"write", f_write},
|
||||||
};
|
|
||||||
|
|
||||||
|
|
||||||
/*
|
|
||||||
** metamethods for file handles
|
|
||||||
*/
|
|
||||||
static const luaL_Reg metameth[] = {
|
|
||||||
{"__index", NULL}, /* placeholder */
|
|
||||||
{"__gc", f_gc},
|
{"__gc", f_gc},
|
||||||
{"__close", f_gc},
|
|
||||||
{"__tostring", f_tostring},
|
{"__tostring", f_tostring},
|
||||||
{NULL, NULL}
|
{NULL, NULL}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
static void createmeta (lua_State *L) {
|
static void createmeta (lua_State *L) {
|
||||||
luaL_newmetatable(L, LUA_FILEHANDLE); /* metatable for file handles */
|
luaL_newmetatable(L, LUA_FILEHANDLE); /* create metatable for file handles */
|
||||||
luaL_setfuncs(L, metameth, 0); /* add metamethods to new metatable */
|
lua_pushvalue(L, -1); /* push metatable */
|
||||||
luaL_newlibtable(L, meth); /* create method table */
|
lua_setfield(L, -2, "__index"); /* metatable.__index = metatable */
|
||||||
luaL_setfuncs(L, meth, 0); /* add file methods to method table */
|
luaL_setfuncs(L, flib, 0); /* add file methods to new metatable */
|
||||||
lua_setfield(L, -2, "__index"); /* metatable.__index = method table */
|
lua_pop(L, 1); /* pop new metatable */
|
||||||
lua_pop(L, 1); /* pop metatable */
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@@ -810,7 +745,7 @@ static void createmeta (lua_State *L) {
|
|||||||
static int io_noclose (lua_State *L) {
|
static int io_noclose (lua_State *L) {
|
||||||
LStream *p = tolstream(L);
|
LStream *p = tolstream(L);
|
||||||
p->closef = &io_noclose; /* keep file opened */
|
p->closef = &io_noclose; /* keep file opened */
|
||||||
luaL_pushfail(L);
|
lua_pushnil(L);
|
||||||
lua_pushliteral(L, "cannot close standard file");
|
lua_pushliteral(L, "cannot close standard file");
|
||||||
return 2;
|
return 2;
|
||||||
}
|
}
|
||||||
|
|||||||
-114
@@ -1,114 +0,0 @@
|
|||||||
/*
|
|
||||||
** $Id: ljumptab.h $
|
|
||||||
** Jump Table for the Lua interpreter
|
|
||||||
** See Copyright Notice in lua.h
|
|
||||||
*/
|
|
||||||
|
|
||||||
|
|
||||||
#undef vmdispatch
|
|
||||||
#undef vmcase
|
|
||||||
#undef vmbreak
|
|
||||||
|
|
||||||
#define vmdispatch(x) goto *disptab[x];
|
|
||||||
|
|
||||||
#define vmcase(l) L_##l:
|
|
||||||
|
|
||||||
#define vmbreak vmfetch(); vmdispatch(GET_OPCODE(i));
|
|
||||||
|
|
||||||
|
|
||||||
static const void *const disptab[NUM_OPCODES] = {
|
|
||||||
|
|
||||||
#if 0
|
|
||||||
** you can update the following list with this command:
|
|
||||||
**
|
|
||||||
** sed -n '/^OP_/!d; s/OP_/\&\&L_OP_/ ; s/,.*/,/ ; s/\/.*// ; p' lopcodes.h
|
|
||||||
**
|
|
||||||
#endif
|
|
||||||
|
|
||||||
&&L_OP_MOVE,
|
|
||||||
&&L_OP_LOADI,
|
|
||||||
&&L_OP_LOADF,
|
|
||||||
&&L_OP_LOADK,
|
|
||||||
&&L_OP_LOADKX,
|
|
||||||
&&L_OP_LOADFALSE,
|
|
||||||
&&L_OP_LFALSESKIP,
|
|
||||||
&&L_OP_LOADTRUE,
|
|
||||||
&&L_OP_LOADNIL,
|
|
||||||
&&L_OP_GETUPVAL,
|
|
||||||
&&L_OP_SETUPVAL,
|
|
||||||
&&L_OP_GETTABUP,
|
|
||||||
&&L_OP_GETTABLE,
|
|
||||||
&&L_OP_GETI,
|
|
||||||
&&L_OP_GETFIELD,
|
|
||||||
&&L_OP_SETTABUP,
|
|
||||||
&&L_OP_SETTABLE,
|
|
||||||
&&L_OP_SETI,
|
|
||||||
&&L_OP_SETFIELD,
|
|
||||||
&&L_OP_NEWTABLE,
|
|
||||||
&&L_OP_SELF,
|
|
||||||
&&L_OP_ADDI,
|
|
||||||
&&L_OP_ADDK,
|
|
||||||
&&L_OP_SUBK,
|
|
||||||
&&L_OP_MULK,
|
|
||||||
&&L_OP_MODK,
|
|
||||||
&&L_OP_POWK,
|
|
||||||
&&L_OP_DIVK,
|
|
||||||
&&L_OP_IDIVK,
|
|
||||||
&&L_OP_BANDK,
|
|
||||||
&&L_OP_BORK,
|
|
||||||
&&L_OP_BXORK,
|
|
||||||
&&L_OP_SHLI,
|
|
||||||
&&L_OP_SHRI,
|
|
||||||
&&L_OP_ADD,
|
|
||||||
&&L_OP_SUB,
|
|
||||||
&&L_OP_MUL,
|
|
||||||
&&L_OP_MOD,
|
|
||||||
&&L_OP_POW,
|
|
||||||
&&L_OP_DIV,
|
|
||||||
&&L_OP_IDIV,
|
|
||||||
&&L_OP_BAND,
|
|
||||||
&&L_OP_BOR,
|
|
||||||
&&L_OP_BXOR,
|
|
||||||
&&L_OP_SHL,
|
|
||||||
&&L_OP_SHR,
|
|
||||||
&&L_OP_MMBIN,
|
|
||||||
&&L_OP_MMBINI,
|
|
||||||
&&L_OP_MMBINK,
|
|
||||||
&&L_OP_UNM,
|
|
||||||
&&L_OP_BNOT,
|
|
||||||
&&L_OP_NOT,
|
|
||||||
&&L_OP_LEN,
|
|
||||||
&&L_OP_CONCAT,
|
|
||||||
&&L_OP_CLOSE,
|
|
||||||
&&L_OP_TBC,
|
|
||||||
&&L_OP_JMP,
|
|
||||||
&&L_OP_EQ,
|
|
||||||
&&L_OP_LT,
|
|
||||||
&&L_OP_LE,
|
|
||||||
&&L_OP_EQK,
|
|
||||||
&&L_OP_EQI,
|
|
||||||
&&L_OP_LTI,
|
|
||||||
&&L_OP_LEI,
|
|
||||||
&&L_OP_GTI,
|
|
||||||
&&L_OP_GEI,
|
|
||||||
&&L_OP_TEST,
|
|
||||||
&&L_OP_TESTSET,
|
|
||||||
&&L_OP_CALL,
|
|
||||||
&&L_OP_TAILCALL,
|
|
||||||
&&L_OP_RETURN,
|
|
||||||
&&L_OP_RETURN0,
|
|
||||||
&&L_OP_RETURN1,
|
|
||||||
&&L_OP_FORLOOP,
|
|
||||||
&&L_OP_FORPREP,
|
|
||||||
&&L_OP_TFORPREP,
|
|
||||||
&&L_OP_TFORCALL,
|
|
||||||
&&L_OP_TFORLOOP,
|
|
||||||
&&L_OP_SETLIST,
|
|
||||||
&&L_OP_CLOSURE,
|
|
||||||
&&L_OP_VARARG,
|
|
||||||
&&L_OP_GETVARG,
|
|
||||||
&&L_OP_ERRNNIL,
|
|
||||||
&&L_OP_VARARGPREP,
|
|
||||||
&&L_OP_EXTRAARG
|
|
||||||
|
|
||||||
};
|
|
||||||
@@ -1,5 +1,5 @@
|
|||||||
/*
|
/*
|
||||||
** $Id: llex.c $
|
** $Id: llex.c,v 2.96.1.1 2017/04/19 17:20:42 roberto Exp $
|
||||||
** Lexical Analyzer
|
** Lexical Analyzer
|
||||||
** See Copyright Notice in lua.h
|
** See Copyright Notice in lua.h
|
||||||
*/
|
*/
|
||||||
@@ -29,14 +29,9 @@
|
|||||||
|
|
||||||
|
|
||||||
|
|
||||||
#define next(ls) (ls->current = zgetc(ls->z))
|
#define next(ls) (ls->current = zgetc(ls->z))
|
||||||
|
|
||||||
|
|
||||||
/* minimum size for string buffer */
|
|
||||||
#if !defined(LUA_MINBUFFER)
|
|
||||||
#define LUA_MINBUFFER 32
|
|
||||||
#endif
|
|
||||||
|
|
||||||
|
|
||||||
#define currIsNewline(ls) (ls->current == '\n' || ls->current == '\r')
|
#define currIsNewline(ls) (ls->current == '\n' || ls->current == '\r')
|
||||||
|
|
||||||
@@ -44,7 +39,7 @@
|
|||||||
/* ORDER RESERVED */
|
/* ORDER RESERVED */
|
||||||
static const char *const luaX_tokens [] = {
|
static const char *const luaX_tokens [] = {
|
||||||
"and", "break", "do", "else", "elseif",
|
"and", "break", "do", "else", "elseif",
|
||||||
"end", "false", "for", "function", "global", "goto", "if",
|
"end", "false", "for", "function", "goto", "if",
|
||||||
"in", "local", "nil", "not", "or", "repeat",
|
"in", "local", "nil", "not", "or", "repeat",
|
||||||
"return", "then", "true", "until", "while",
|
"return", "then", "true", "until", "while",
|
||||||
"//", "..", "...", "==", ">=", "<=", "~=",
|
"//", "..", "...", "==", ">=", "<=", "~=",
|
||||||
@@ -62,13 +57,13 @@ static l_noret lexerror (LexState *ls, const char *msg, int token);
|
|||||||
static void save (LexState *ls, int c) {
|
static void save (LexState *ls, int c) {
|
||||||
Mbuffer *b = ls->buff;
|
Mbuffer *b = ls->buff;
|
||||||
if (luaZ_bufflen(b) + 1 > luaZ_sizebuffer(b)) {
|
if (luaZ_bufflen(b) + 1 > luaZ_sizebuffer(b)) {
|
||||||
size_t newsize = luaZ_sizebuffer(b); /* get old size */;
|
size_t newsize;
|
||||||
if (newsize >= (MAX_SIZE/3 * 2)) /* larger than MAX_SIZE/1.5 ? */
|
if (luaZ_sizebuffer(b) >= MAX_SIZE/2)
|
||||||
lexerror(ls, "lexical element too long", 0);
|
lexerror(ls, "lexical element too long", 0);
|
||||||
newsize += (newsize >> 1); /* new size is 1.5 times the old one */
|
newsize = luaZ_sizebuffer(b) * 2;
|
||||||
luaZ_resizebuffer(ls->L, b, newsize);
|
luaZ_resizebuffer(ls->L, b, newsize);
|
||||||
}
|
}
|
||||||
b->buffer[luaZ_bufflen(b)++] = cast_char(c);
|
b->buffer[luaZ_bufflen(b)++] = cast(char, c);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@@ -86,10 +81,8 @@ 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) { /* single-byte symbols? */
|
if (token < FIRST_RESERVED) { /* single-byte symbols? */
|
||||||
if (lisprint(token))
|
lua_assert(token == cast_uchar(token));
|
||||||
return luaO_pushfstring(ls->L, "'%c'", token);
|
return luaO_pushfstring(ls->L, "'%c'", token);
|
||||||
else /* control character */
|
|
||||||
return luaO_pushfstring(ls->L, "'<\\%d>'", token);
|
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
const char *s = luaX_tokens[token - FIRST_RESERVED];
|
const char *s = luaX_tokens[token - FIRST_RESERVED];
|
||||||
@@ -127,34 +120,27 @@ l_noret luaX_syntaxerror (LexState *ls, const char *msg) {
|
|||||||
|
|
||||||
|
|
||||||
/*
|
/*
|
||||||
** Anchors a string in scanner's table so that it will not be collected
|
** creates a new string and anchors it in scanner's table so that
|
||||||
** until the end of the compilation; by that time it should be anchored
|
** it will not be collected until the end of the compilation
|
||||||
** somewhere. It also internalizes long strings, ensuring there is only
|
** (by that time it should be anchored somewhere)
|
||||||
** one copy of each unique string.
|
|
||||||
*/
|
|
||||||
static TString *anchorstr (LexState *ls, TString *ts) {
|
|
||||||
lua_State *L = ls->L;
|
|
||||||
TValue oldts;
|
|
||||||
int tag = luaH_getstr(ls->h, ts, &oldts);
|
|
||||||
if (!tagisempty(tag)) /* string already present? */
|
|
||||||
return tsvalue(&oldts); /* use stored value */
|
|
||||||
else { /* create a new entry */
|
|
||||||
TValue *stv = s2v(L->top.p++); /* reserve stack space for string */
|
|
||||||
setsvalue(L, stv, ts); /* push (anchor) the string on the stack */
|
|
||||||
luaH_set(L, ls->h, stv, stv); /* t[string] = string */
|
|
||||||
/* table is not a metatable, so it does not need to invalidate cache */
|
|
||||||
luaC_checkGC(L);
|
|
||||||
L->top.p--; /* remove string from stack */
|
|
||||||
return ts;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
/*
|
|
||||||
** Creates a new string and anchors it in scanner's table.
|
|
||||||
*/
|
*/
|
||||||
TString *luaX_newstring (LexState *ls, const char *str, size_t l) {
|
TString *luaX_newstring (LexState *ls, const char *str, size_t l) {
|
||||||
return anchorstr(ls, luaS_newlstr(ls->L, str, l));
|
lua_State *L = ls->L;
|
||||||
|
TValue *o; /* entry for 'str' */
|
||||||
|
TString *ts = luaS_newlstr(L, str, l); /* create new string */
|
||||||
|
setsvalue2s(L, L->top++, ts); /* temporarily anchor it in stack */
|
||||||
|
o = luaH_set(L, ls->h, L->top - 1);
|
||||||
|
if (ttisnil(o)) { /* not in use yet? */
|
||||||
|
/* boolean value does not need GC barrier;
|
||||||
|
table has no metatable, so it does not need to invalidate cache */
|
||||||
|
setbvalue(o, 1); /* t[string] = true */
|
||||||
|
luaC_checkGC(L);
|
||||||
|
}
|
||||||
|
else { /* string already present */
|
||||||
|
ts = tsvalue(keyfromval(o)); /* re-use value previously stored */
|
||||||
|
}
|
||||||
|
L->top--; /* remove string from stack */
|
||||||
|
return ts;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@@ -168,7 +154,7 @@ static void inclinenumber (LexState *ls) {
|
|||||||
next(ls); /* skip '\n' or '\r' */
|
next(ls); /* skip '\n' or '\r' */
|
||||||
if (currIsNewline(ls) && ls->current != old)
|
if (currIsNewline(ls) && ls->current != old)
|
||||||
next(ls); /* skip '\n\r' or '\r\n' */
|
next(ls); /* skip '\n\r' or '\r\n' */
|
||||||
if (++ls->linenumber >= INT_MAX)
|
if (++ls->linenumber >= MAX_INT)
|
||||||
lexerror(ls, "chunk has too many lines", 0);
|
lexerror(ls, "chunk has too many lines", 0);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -184,15 +170,7 @@ void luaX_setinput (lua_State *L, LexState *ls, ZIO *z, TString *source,
|
|||||||
ls->linenumber = 1;
|
ls->linenumber = 1;
|
||||||
ls->lastline = 1;
|
ls->lastline = 1;
|
||||||
ls->source = source;
|
ls->source = source;
|
||||||
/* all three strings here ("_ENV", "break", "global") were fixed,
|
ls->envn = luaS_newliteral(L, LUA_ENV); /* get env name */
|
||||||
so they cannot be collected */
|
|
||||||
ls->envn = luaS_newliteral(L, LUA_ENV); /* get env string */
|
|
||||||
ls->brkn = luaS_newliteral(L, "break"); /* get "break" string */
|
|
||||||
#if defined(LUA_COMPAT_GLOBAL)
|
|
||||||
/* compatibility mode: "global" is not a reserved word */
|
|
||||||
ls->glbn = luaS_newliteral(L, "global"); /* get "global" string */
|
|
||||||
ls->glbn->extra = 0; /* mark it as not reserved */
|
|
||||||
#endif
|
|
||||||
luaZ_resizebuffer(ls->L, ls->buff, LUA_MINBUFFER); /* initialize buffer */
|
luaZ_resizebuffer(ls->L, ls->buff, LUA_MINBUFFER); /* initialize buffer */
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -230,16 +208,8 @@ static int check_next2 (LexState *ls, const char *set) {
|
|||||||
|
|
||||||
/* LUA_NUMBER */
|
/* LUA_NUMBER */
|
||||||
/*
|
/*
|
||||||
** This function is quite liberal in what it accepts, as 'luaO_str2num'
|
** this function is quite liberal in what it accepts, as 'luaO_str2num'
|
||||||
** will reject ill-formed numerals. Roughly, it accepts the following
|
** will reject ill-formed numerals.
|
||||||
** pattern:
|
|
||||||
**
|
|
||||||
** %d(%x|%.|([Ee][+-]?))* | 0[Xx](%x|%.|([Pp][+-]?))*
|
|
||||||
**
|
|
||||||
** The only tricky part is to accept [+-] only after a valid exponent
|
|
||||||
** mark, to avoid reading '3-4' or '0xe+1' as a single number.
|
|
||||||
**
|
|
||||||
** The caller might have already read an initial dot.
|
|
||||||
*/
|
*/
|
||||||
static int read_numeral (LexState *ls, SemInfo *seminfo) {
|
static int read_numeral (LexState *ls, SemInfo *seminfo) {
|
||||||
TValue obj;
|
TValue obj;
|
||||||
@@ -250,14 +220,14 @@ static int read_numeral (LexState *ls, SemInfo *seminfo) {
|
|||||||
if (first == '0' && check_next2(ls, "xX")) /* hexadecimal? */
|
if (first == '0' && check_next2(ls, "xX")) /* hexadecimal? */
|
||||||
expo = "Pp";
|
expo = "Pp";
|
||||||
for (;;) {
|
for (;;) {
|
||||||
if (check_next2(ls, expo)) /* exponent mark? */
|
if (check_next2(ls, expo)) /* exponent part? */
|
||||||
check_next2(ls, "-+"); /* optional exponent sign */
|
check_next2(ls, "-+"); /* optional exponent sign */
|
||||||
else if (lisxdigit(ls->current) || ls->current == '.') /* '%x|%.' */
|
if (lisxdigit(ls->current))
|
||||||
|
save_and_next(ls);
|
||||||
|
else if (ls->current == '.')
|
||||||
save_and_next(ls);
|
save_and_next(ls);
|
||||||
else break;
|
else break;
|
||||||
}
|
}
|
||||||
if (lislalpha(ls->current)) /* is numeral touching a letter? */
|
|
||||||
save_and_next(ls); /* force an error */
|
|
||||||
save(ls, '\0');
|
save(ls, '\0');
|
||||||
if (luaO_str2num(luaZ_buffer(ls->buff), &obj) == 0) /* format error? */
|
if (luaO_str2num(luaZ_buffer(ls->buff), &obj) == 0) /* format error? */
|
||||||
lexerror(ls, "malformed number", TK_FLT);
|
lexerror(ls, "malformed number", TK_FLT);
|
||||||
@@ -274,13 +244,12 @@ static int read_numeral (LexState *ls, SemInfo *seminfo) {
|
|||||||
|
|
||||||
|
|
||||||
/*
|
/*
|
||||||
** read a sequence '[=*[' or ']=*]', leaving the last bracket. If
|
** skip a sequence '[=*[' or ']=*]'; if sequence is well formed, return
|
||||||
** sequence is well formed, return its number of '='s + 2; otherwise,
|
** its number of '='s; otherwise, return a negative number (-1 iff there
|
||||||
** return 1 if it is a single bracket (no '='s and no 2nd bracket);
|
** are no '='s after initial bracket)
|
||||||
** otherwise (an unfinished '[==...') return 0.
|
|
||||||
*/
|
*/
|
||||||
static size_t skip_sep (LexState *ls) {
|
static int skip_sep (LexState *ls) {
|
||||||
size_t count = 0;
|
int count = 0;
|
||||||
int s = ls->current;
|
int s = ls->current;
|
||||||
lua_assert(s == '[' || s == ']');
|
lua_assert(s == '[' || s == ']');
|
||||||
save_and_next(ls);
|
save_and_next(ls);
|
||||||
@@ -288,13 +257,11 @@ static size_t skip_sep (LexState *ls) {
|
|||||||
save_and_next(ls);
|
save_and_next(ls);
|
||||||
count++;
|
count++;
|
||||||
}
|
}
|
||||||
return (ls->current == s) ? count + 2
|
return (ls->current == s) ? count : (-count) - 1;
|
||||||
: (count == 0) ? 1
|
|
||||||
: 0;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
static void read_long_string (LexState *ls, SemInfo *seminfo, size_t sep) {
|
static void read_long_string (LexState *ls, SemInfo *seminfo, int sep) {
|
||||||
int line = ls->linenumber; /* initial line (for error message) */
|
int line = ls->linenumber; /* initial line (for error message) */
|
||||||
save_and_next(ls); /* skip 2nd '[' */
|
save_and_next(ls); /* skip 2nd '[' */
|
||||||
if (currIsNewline(ls)) /* string starts with a newline? */
|
if (currIsNewline(ls)) /* string starts with a newline? */
|
||||||
@@ -328,8 +295,8 @@ static void read_long_string (LexState *ls, SemInfo *seminfo, size_t sep) {
|
|||||||
}
|
}
|
||||||
} endloop:
|
} endloop:
|
||||||
if (seminfo)
|
if (seminfo)
|
||||||
seminfo->ts = luaX_newstring(ls, luaZ_buffer(ls->buff) + sep,
|
seminfo->ts = luaX_newstring(ls, luaZ_buffer(ls->buff) + (2 + sep),
|
||||||
luaZ_bufflen(ls->buff) - 2 * sep);
|
luaZ_bufflen(ls->buff) - 2*(2 + sep));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@@ -357,21 +324,16 @@ static int readhexaesc (LexState *ls) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/*
|
static unsigned long readutf8esc (LexState *ls) {
|
||||||
** When reading a UTF-8 escape sequence, save everything to the buffer
|
unsigned long r;
|
||||||
** for error reporting in case of errors; 'i' counts the number of
|
int i = 4; /* chars to be removed: '\', 'u', '{', and first digit */
|
||||||
** saved characters, so that they can be removed if case of success.
|
|
||||||
*/
|
|
||||||
static l_uint32 readutf8esc (LexState *ls) {
|
|
||||||
l_uint32 r;
|
|
||||||
int i = 4; /* number of chars to be removed: start with #"\u{X" */
|
|
||||||
save_and_next(ls); /* skip 'u' */
|
save_and_next(ls); /* skip 'u' */
|
||||||
esccheck(ls, ls->current == '{', "missing '{'");
|
esccheck(ls, ls->current == '{', "missing '{'");
|
||||||
r = cast_uint(gethexa(ls)); /* must have at least one digit */
|
r = gethexa(ls); /* must have at least one digit */
|
||||||
while (cast_void(save_and_next(ls)), lisxdigit(ls->current)) {
|
while ((save_and_next(ls), lisxdigit(ls->current))) {
|
||||||
i++;
|
i++;
|
||||||
esccheck(ls, r <= (0x7FFFFFFFu >> 4), "UTF-8 value too large");
|
|
||||||
r = (r << 4) + luaO_hexavalue(ls->current);
|
r = (r << 4) + luaO_hexavalue(ls->current);
|
||||||
|
esccheck(ls, r <= 0x10FFFF, "UTF-8 value too large");
|
||||||
}
|
}
|
||||||
esccheck(ls, ls->current == '}', "missing '}'");
|
esccheck(ls, ls->current == '}', "missing '}'");
|
||||||
next(ls); /* skip '}' */
|
next(ls); /* skip '}' */
|
||||||
@@ -482,9 +444,9 @@ static int llex (LexState *ls, SemInfo *seminfo) {
|
|||||||
/* else is a comment */
|
/* else is a comment */
|
||||||
next(ls);
|
next(ls);
|
||||||
if (ls->current == '[') { /* long comment? */
|
if (ls->current == '[') { /* long comment? */
|
||||||
size_t sep = skip_sep(ls);
|
int sep = skip_sep(ls);
|
||||||
luaZ_resetbuffer(ls->buff); /* 'skip_sep' may dirty the buffer */
|
luaZ_resetbuffer(ls->buff); /* 'skip_sep' may dirty the buffer */
|
||||||
if (sep >= 2) {
|
if (sep >= 0) {
|
||||||
read_long_string(ls, NULL, sep); /* skip long comment */
|
read_long_string(ls, NULL, sep); /* skip long comment */
|
||||||
luaZ_resetbuffer(ls->buff); /* previous call may dirty the buff. */
|
luaZ_resetbuffer(ls->buff); /* previous call may dirty the buff. */
|
||||||
break;
|
break;
|
||||||
@@ -496,45 +458,45 @@ static int llex (LexState *ls, SemInfo *seminfo) {
|
|||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
case '[': { /* long string or simply '[' */
|
case '[': { /* long string or simply '[' */
|
||||||
size_t sep = skip_sep(ls);
|
int sep = skip_sep(ls);
|
||||||
if (sep >= 2) {
|
if (sep >= 0) {
|
||||||
read_long_string(ls, seminfo, sep);
|
read_long_string(ls, seminfo, sep);
|
||||||
return TK_STRING;
|
return TK_STRING;
|
||||||
}
|
}
|
||||||
else if (sep == 0) /* '[=...' missing second bracket? */
|
else if (sep != -1) /* '[=...' missing second bracket */
|
||||||
lexerror(ls, "invalid long string delimiter", TK_STRING);
|
lexerror(ls, "invalid long string delimiter", TK_STRING);
|
||||||
return '[';
|
return '[';
|
||||||
}
|
}
|
||||||
case '=': {
|
case '=': {
|
||||||
next(ls);
|
next(ls);
|
||||||
if (check_next1(ls, '=')) return TK_EQ; /* '==' */
|
if (check_next1(ls, '=')) return TK_EQ;
|
||||||
else return '=';
|
else return '=';
|
||||||
}
|
}
|
||||||
case '<': {
|
case '<': {
|
||||||
next(ls);
|
next(ls);
|
||||||
if (check_next1(ls, '=')) return TK_LE; /* '<=' */
|
if (check_next1(ls, '=')) return TK_LE;
|
||||||
else if (check_next1(ls, '<')) return TK_SHL; /* '<<' */
|
else if (check_next1(ls, '<')) return TK_SHL;
|
||||||
else return '<';
|
else return '<';
|
||||||
}
|
}
|
||||||
case '>': {
|
case '>': {
|
||||||
next(ls);
|
next(ls);
|
||||||
if (check_next1(ls, '=')) return TK_GE; /* '>=' */
|
if (check_next1(ls, '=')) return TK_GE;
|
||||||
else if (check_next1(ls, '>')) return TK_SHR; /* '>>' */
|
else if (check_next1(ls, '>')) return TK_SHR;
|
||||||
else return '>';
|
else return '>';
|
||||||
}
|
}
|
||||||
case '/': {
|
case '/': {
|
||||||
next(ls);
|
next(ls);
|
||||||
if (check_next1(ls, '/')) return TK_IDIV; /* '//' */
|
if (check_next1(ls, '/')) return TK_IDIV;
|
||||||
else return '/';
|
else return '/';
|
||||||
}
|
}
|
||||||
case '~': {
|
case '~': {
|
||||||
next(ls);
|
next(ls);
|
||||||
if (check_next1(ls, '=')) return TK_NE; /* '~=' */
|
if (check_next1(ls, '=')) return TK_NE;
|
||||||
else return '~';
|
else return '~';
|
||||||
}
|
}
|
||||||
case ':': {
|
case ':': {
|
||||||
next(ls);
|
next(ls);
|
||||||
if (check_next1(ls, ':')) return TK_DBCOLON; /* '::' */
|
if (check_next1(ls, ':')) return TK_DBCOLON;
|
||||||
else return ':';
|
else return ':';
|
||||||
}
|
}
|
||||||
case '"': case '\'': { /* short literal strings */
|
case '"': case '\'': { /* short literal strings */
|
||||||
@@ -564,17 +526,16 @@ static int llex (LexState *ls, SemInfo *seminfo) {
|
|||||||
do {
|
do {
|
||||||
save_and_next(ls);
|
save_and_next(ls);
|
||||||
} while (lislalnum(ls->current));
|
} while (lislalnum(ls->current));
|
||||||
/* find or create string */
|
ts = luaX_newstring(ls, luaZ_buffer(ls->buff),
|
||||||
ts = luaS_newlstr(ls->L, luaZ_buffer(ls->buff),
|
luaZ_bufflen(ls->buff));
|
||||||
luaZ_bufflen(ls->buff));
|
seminfo->ts = ts;
|
||||||
if (isreserved(ts)) /* reserved word? */
|
if (isreserved(ts)) /* reserved word? */
|
||||||
return ts->extra - 1 + FIRST_RESERVED;
|
return ts->extra - 1 + FIRST_RESERVED;
|
||||||
else {
|
else {
|
||||||
seminfo->ts = anchorstr(ls, ts);
|
|
||||||
return TK_NAME;
|
return TK_NAME;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
else { /* single-char tokens ('+', '*', '%', '{', '}', ...) */
|
else { /* single-char tokens (+ - / ...) */
|
||||||
int c = ls->current;
|
int c = ls->current;
|
||||||
next(ls);
|
next(ls);
|
||||||
return c;
|
return c;
|
||||||
|
|||||||
@@ -1,5 +1,5 @@
|
|||||||
/*
|
/*
|
||||||
** $Id: llex.h $
|
** $Id: llex.h,v 1.79.1.1 2017/04/19 17:20:42 roberto Exp $
|
||||||
** Lexical Analyzer
|
** Lexical Analyzer
|
||||||
** See Copyright Notice in lua.h
|
** See Copyright Notice in lua.h
|
||||||
*/
|
*/
|
||||||
@@ -7,17 +7,11 @@
|
|||||||
#ifndef llex_h
|
#ifndef llex_h
|
||||||
#define llex_h
|
#define llex_h
|
||||||
|
|
||||||
#include <limits.h>
|
|
||||||
|
|
||||||
#include "lobject.h"
|
#include "lobject.h"
|
||||||
#include "lzio.h"
|
#include "lzio.h"
|
||||||
|
|
||||||
|
|
||||||
/*
|
#define FIRST_RESERVED 257
|
||||||
** Single-char tokens (terminal symbols) are represented by their own
|
|
||||||
** numeric code. Other tokens start at the following value.
|
|
||||||
*/
|
|
||||||
#define FIRST_RESERVED (UCHAR_MAX + 1)
|
|
||||||
|
|
||||||
|
|
||||||
#if !defined(LUA_ENV)
|
#if !defined(LUA_ENV)
|
||||||
@@ -33,8 +27,8 @@ enum RESERVED {
|
|||||||
/* terminal symbols denoted by reserved words */
|
/* terminal symbols denoted by reserved words */
|
||||||
TK_AND = FIRST_RESERVED, TK_BREAK,
|
TK_AND = FIRST_RESERVED, TK_BREAK,
|
||||||
TK_DO, TK_ELSE, TK_ELSEIF, TK_END, TK_FALSE, TK_FOR, TK_FUNCTION,
|
TK_DO, TK_ELSE, TK_ELSEIF, TK_END, TK_FALSE, TK_FOR, TK_FUNCTION,
|
||||||
TK_GLOBAL, TK_GOTO, TK_IF, TK_IN, TK_LOCAL, TK_NIL, TK_NOT, TK_OR,
|
TK_GOTO, TK_IF, TK_IN, TK_LOCAL, TK_NIL, TK_NOT, TK_OR, TK_REPEAT,
|
||||||
TK_REPEAT, TK_RETURN, TK_THEN, TK_TRUE, TK_UNTIL, TK_WHILE,
|
TK_RETURN, TK_THEN, TK_TRUE, TK_UNTIL, TK_WHILE,
|
||||||
/* other terminal symbols */
|
/* other terminal symbols */
|
||||||
TK_IDIV, TK_CONCAT, TK_DOTS, TK_EQ, TK_GE, TK_LE, TK_NE,
|
TK_IDIV, TK_CONCAT, TK_DOTS, TK_EQ, TK_GE, TK_LE, TK_NE,
|
||||||
TK_SHL, TK_SHR,
|
TK_SHL, TK_SHR,
|
||||||
@@ -43,7 +37,7 @@ enum RESERVED {
|
|||||||
};
|
};
|
||||||
|
|
||||||
/* number of reserved words */
|
/* number of reserved words */
|
||||||
#define NUM_RESERVED (cast_int(TK_WHILE-FIRST_RESERVED + 1))
|
#define NUM_RESERVED (cast(int, TK_WHILE-FIRST_RESERVED+1))
|
||||||
|
|
||||||
|
|
||||||
typedef union {
|
typedef union {
|
||||||
@@ -59,7 +53,7 @@ typedef struct Token {
|
|||||||
} Token;
|
} Token;
|
||||||
|
|
||||||
|
|
||||||
/* state of the scanner plus state of the parser when shared by all
|
/* state of the lexer plus state of the parser when shared by all
|
||||||
functions */
|
functions */
|
||||||
typedef struct LexState {
|
typedef struct LexState {
|
||||||
int current; /* current character (charint) */
|
int current; /* current character (charint) */
|
||||||
@@ -75,8 +69,6 @@ typedef struct LexState {
|
|||||||
struct Dyndata *dyd; /* dynamic structures used by the parser */
|
struct Dyndata *dyd; /* dynamic structures used by the parser */
|
||||||
TString *source; /* current source name */
|
TString *source; /* current source name */
|
||||||
TString *envn; /* environment variable name */
|
TString *envn; /* environment variable name */
|
||||||
TString *brkn; /* "break" name (used as a label) */
|
|
||||||
TString *glbn; /* "global" name (when not a reserved word) */
|
|
||||||
} LexState;
|
} LexState;
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
@@ -1,5 +1,5 @@
|
|||||||
/*
|
/*
|
||||||
** $Id: llimits.h $
|
** $Id: llimits.h,v 1.141.1.1 2017/04/19 17:20:42 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
|
||||||
*/
|
*/
|
||||||
@@ -14,80 +14,64 @@
|
|||||||
|
|
||||||
#include "lua.h"
|
#include "lua.h"
|
||||||
|
|
||||||
|
|
||||||
#define l_numbits(t) cast_int(sizeof(t) * CHAR_BIT)
|
|
||||||
|
|
||||||
/*
|
/*
|
||||||
** 'l_mem' is a signed integer big enough to count the total memory
|
** 'lu_mem' and 'l_mem' are unsigned/signed integers big enough to count
|
||||||
** used by Lua. (It is signed due to the use of debt in several
|
** the total memory used by Lua (in bytes). Usually, 'size_t' and
|
||||||
** computations.) 'lu_mem' is a corresponding unsigned type. Usually,
|
|
||||||
** 'ptrdiff_t' should work, but we use 'long' for 16-bit machines.
|
** 'ptrdiff_t' should work, but we use 'long' for 16-bit machines.
|
||||||
*/
|
*/
|
||||||
#if defined(LUAI_MEM) /* { external definitions? */
|
#if defined(LUAI_MEM) /* { external definitions? */
|
||||||
typedef LUAI_MEM l_mem;
|
|
||||||
typedef LUAI_UMEM lu_mem;
|
typedef LUAI_UMEM lu_mem;
|
||||||
#elif LUAI_IS32INT /* }{ */
|
typedef LUAI_MEM l_mem;
|
||||||
typedef ptrdiff_t l_mem;
|
#elif LUAI_BITSINT >= 32 /* }{ */
|
||||||
typedef size_t lu_mem;
|
typedef size_t lu_mem;
|
||||||
|
typedef ptrdiff_t l_mem;
|
||||||
#else /* 16-bit ints */ /* }{ */
|
#else /* 16-bit ints */ /* }{ */
|
||||||
typedef long l_mem;
|
|
||||||
typedef unsigned long lu_mem;
|
typedef unsigned long lu_mem;
|
||||||
|
typedef long l_mem;
|
||||||
#endif /* } */
|
#endif /* } */
|
||||||
|
|
||||||
#define MAX_LMEM \
|
|
||||||
cast(l_mem, (cast(lu_mem, 1) << (l_numbits(l_mem) - 1)) - 1)
|
|
||||||
|
|
||||||
|
|
||||||
/* chars used as small naturals (so that 'char' is reserved for characters) */
|
/* chars used as small naturals (so that 'char' is reserved for characters) */
|
||||||
typedef unsigned char lu_byte;
|
typedef unsigned char lu_byte;
|
||||||
typedef signed char ls_byte;
|
|
||||||
|
|
||||||
|
|
||||||
/* Type for thread status/error codes */
|
|
||||||
typedef lu_byte TStatus;
|
|
||||||
|
|
||||||
/* The C API still uses 'int' for status/error codes */
|
|
||||||
#define APIstatus(st) cast_int(st)
|
|
||||||
|
|
||||||
/* maximum value for size_t */
|
/* maximum value for size_t */
|
||||||
#define MAX_SIZET ((size_t)(~(size_t)0))
|
#define MAX_SIZET ((size_t)(~(size_t)0))
|
||||||
|
|
||||||
/*
|
/* maximum size visible for Lua (must be representable in a lua_Integer */
|
||||||
** Maximum size for strings and userdata visible for Lua; should be
|
|
||||||
** representable as a lua_Integer and as a size_t.
|
|
||||||
*/
|
|
||||||
#define MAX_SIZE (sizeof(size_t) < sizeof(lua_Integer) ? MAX_SIZET \
|
#define MAX_SIZE (sizeof(size_t) < sizeof(lua_Integer) ? MAX_SIZET \
|
||||||
: cast_sizet(LUA_MAXINTEGER))
|
: (size_t)(LUA_MAXINTEGER))
|
||||||
|
|
||||||
/*
|
|
||||||
** test whether an unsigned value is a power of 2 (or zero)
|
|
||||||
*/
|
|
||||||
#define ispow2(x) (((x) & ((x) - 1)) == 0)
|
|
||||||
|
|
||||||
|
|
||||||
/* number of chars of a literal string without the ending \0 */
|
#define MAX_LUMEM ((lu_mem)(~(lu_mem)0))
|
||||||
#define LL(x) (sizeof(x)/sizeof(char) - 1)
|
|
||||||
|
#define MAX_LMEM ((l_mem)(MAX_LUMEM >> 1))
|
||||||
|
|
||||||
|
|
||||||
|
#define MAX_INT INT_MAX /* maximum value of an int */
|
||||||
|
|
||||||
|
|
||||||
/*
|
/*
|
||||||
** conversion of pointer to unsigned integer: this is for hashing only;
|
** conversion of pointer to unsigned integer:
|
||||||
** there is no problem if the integer cannot hold the whole pointer
|
** this is for hashing only; there is no problem if the integer
|
||||||
** value. (In strict ISO C this may cause undefined behavior, but no
|
** cannot hold the whole pointer value
|
||||||
** actual machine seems to bother.)
|
|
||||||
*/
|
*/
|
||||||
#if !defined(LUA_USE_C89) && defined(__STDC_VERSION__) && \
|
#define point2uint(p) ((unsigned int)((size_t)(p) & UINT_MAX))
|
||||||
__STDC_VERSION__ >= 199901L
|
|
||||||
#include <stdint.h>
|
|
||||||
#if defined(UINTPTR_MAX) /* even in C99 this type is optional */
|
|
||||||
#define L_P2I uintptr_t
|
|
||||||
#else /* no 'intptr'? */
|
|
||||||
#define L_P2I uintmax_t /* use the largest available integer */
|
|
||||||
#endif
|
|
||||||
#else /* C89 option */
|
|
||||||
#define L_P2I size_t
|
|
||||||
#endif
|
|
||||||
|
|
||||||
#define point2uint(p) cast_uint((L_P2I)(p) & UINT_MAX)
|
|
||||||
|
|
||||||
|
/* type to ensure maximum alignment */
|
||||||
|
#if defined(LUAI_USER_ALIGNMENT_T)
|
||||||
|
typedef LUAI_USER_ALIGNMENT_T L_Umaxalign;
|
||||||
|
#else
|
||||||
|
typedef union {
|
||||||
|
lua_Number n;
|
||||||
|
double u;
|
||||||
|
void *s;
|
||||||
|
lua_Integer i;
|
||||||
|
long l;
|
||||||
|
} L_Umaxalign;
|
||||||
|
#endif
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
@@ -96,25 +80,25 @@ typedef LUAI_UACNUMBER l_uacNumber;
|
|||||||
typedef LUAI_UACINT l_uacInt;
|
typedef LUAI_UACINT l_uacInt;
|
||||||
|
|
||||||
|
|
||||||
/*
|
/* internal assertions for in-house debugging */
|
||||||
** Internal assertions for in-house debugging
|
|
||||||
*/
|
|
||||||
#if defined LUAI_ASSERT
|
|
||||||
#undef NDEBUG
|
|
||||||
#include <assert.h>
|
|
||||||
#define lua_assert(c) assert(c)
|
|
||||||
#define assert_code(c) c
|
|
||||||
#endif
|
|
||||||
|
|
||||||
#if defined(lua_assert)
|
#if defined(lua_assert)
|
||||||
#else
|
|
||||||
#define lua_assert(c) ((void)0)
|
|
||||||
#define assert_code(c) ((void)0)
|
|
||||||
#endif
|
|
||||||
|
|
||||||
#define check_exp(c,e) (lua_assert(c), (e))
|
#define check_exp(c,e) (lua_assert(c), (e))
|
||||||
/* to avoid problems with conditions too long */
|
/* to avoid problems with conditions too long */
|
||||||
#define lua_longassert(c) assert_code((c) ? (void)0 : lua_assert(0))
|
#define lua_longassert(c) ((c) ? (void)0 : lua_assert(0))
|
||||||
|
#else
|
||||||
|
#define lua_assert(c) ((void)0)
|
||||||
|
#define check_exp(c,e) (e)
|
||||||
|
#define lua_longassert(c) ((void)0)
|
||||||
|
#endif
|
||||||
|
|
||||||
|
/*
|
||||||
|
** assertion for checking API calls
|
||||||
|
*/
|
||||||
|
#if !defined(luai_apicheck)
|
||||||
|
#define luai_apicheck(l,e) lua_assert(e)
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#define api_check(l,e,msg) luai_apicheck(l,(e) && msg)
|
||||||
|
|
||||||
|
|
||||||
/* macro to avoid warnings about unused variables */
|
/* macro to avoid warnings about unused variables */
|
||||||
@@ -127,18 +111,10 @@ typedef LUAI_UACINT l_uacInt;
|
|||||||
#define cast(t, exp) ((t)(exp))
|
#define cast(t, exp) ((t)(exp))
|
||||||
|
|
||||||
#define cast_void(i) cast(void, (i))
|
#define cast_void(i) cast(void, (i))
|
||||||
#define cast_voidp(i) cast(void *, (i))
|
#define cast_byte(i) cast(lu_byte, (i))
|
||||||
#define cast_num(i) cast(lua_Number, (i))
|
#define cast_num(i) cast(lua_Number, (i))
|
||||||
#define cast_int(i) cast(int, (i))
|
#define cast_int(i) cast(int, (i))
|
||||||
#define cast_short(i) cast(short, (i))
|
|
||||||
#define cast_uint(i) cast(unsigned int, (i))
|
|
||||||
#define cast_byte(i) cast(lu_byte, (i))
|
|
||||||
#define cast_uchar(i) cast(unsigned char, (i))
|
#define cast_uchar(i) cast(unsigned char, (i))
|
||||||
#define cast_char(i) cast(char, (i))
|
|
||||||
#define cast_charp(i) cast(char *, (i))
|
|
||||||
#define cast_sizet(i) cast(size_t, (i))
|
|
||||||
#define cast_Integer(i) cast(lua_Integer, (i))
|
|
||||||
#define cast_Inst(i) cast(Instruction, (i))
|
|
||||||
|
|
||||||
|
|
||||||
/* cast a signed lua_Integer to lua_Unsigned */
|
/* cast a signed lua_Integer to lua_Unsigned */
|
||||||
@@ -155,44 +131,10 @@ typedef LUAI_UACINT l_uacInt;
|
|||||||
#define l_castU2S(i) ((lua_Integer)(i))
|
#define l_castU2S(i) ((lua_Integer)(i))
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
/*
|
|
||||||
** cast a size_t to lua_Integer: These casts are always valid for
|
|
||||||
** sizes of Lua objects (see MAX_SIZE)
|
|
||||||
*/
|
|
||||||
#define cast_st2S(sz) ((lua_Integer)(sz))
|
|
||||||
|
|
||||||
/* Cast a ptrdiff_t to size_t, when it is known that the minuend
|
|
||||||
** comes from the subtrahend (the base)
|
|
||||||
*/
|
|
||||||
#define ct_diff2sz(df) ((size_t)(df))
|
|
||||||
|
|
||||||
/* ptrdiff_t to lua_Integer */
|
|
||||||
#define ct_diff2S(df) cast_st2S(ct_diff2sz(df))
|
|
||||||
|
|
||||||
/*
|
|
||||||
** Special type equivalent to '(void*)' for functions (to suppress some
|
|
||||||
** warnings when converting function pointers)
|
|
||||||
*/
|
|
||||||
typedef void (*voidf)(void);
|
|
||||||
|
|
||||||
/*
|
|
||||||
** Macro to convert pointer-to-void* to pointer-to-function. This cast
|
|
||||||
** is undefined according to ISO C, but POSIX assumes that it works.
|
|
||||||
** (The '__extension__' in gnu compilers is only to avoid warnings.)
|
|
||||||
*/
|
|
||||||
#if defined(__GNUC__)
|
|
||||||
#define cast_func(p) (__extension__ (voidf)(p))
|
|
||||||
#else
|
|
||||||
#define cast_func(p) ((voidf)(p))
|
|
||||||
#endif
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
/*
|
/*
|
||||||
** non-return type
|
** non-return type
|
||||||
*/
|
*/
|
||||||
#if !defined(l_noret)
|
|
||||||
|
|
||||||
#if defined(__GNUC__)
|
#if defined(__GNUC__)
|
||||||
#define l_noret void __attribute__((noreturn))
|
#define l_noret void __attribute__((noreturn))
|
||||||
#elif defined(_MSC_VER) && _MSC_VER >= 1200
|
#elif defined(_MSC_VER) && _MSC_VER >= 1200
|
||||||
@@ -201,33 +143,118 @@ typedef void (*voidf)(void);
|
|||||||
#define l_noret void
|
#define l_noret void
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
/*
|
||||||
|
** maximum depth for nested C calls and syntactical nested non-terminals
|
||||||
|
** in a program. (Value must fit in an unsigned short int.)
|
||||||
|
*/
|
||||||
|
#if !defined(LUAI_MAXCCALLS)
|
||||||
|
#define LUAI_MAXCCALLS 200
|
||||||
|
#endif
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
/*
|
||||||
|
** type for virtual-machine instructions;
|
||||||
|
** must be an unsigned with (at least) 4 bytes (see details in lopcodes.h)
|
||||||
|
*/
|
||||||
|
#if LUAI_BITSINT >= 32
|
||||||
|
typedef unsigned int Instruction;
|
||||||
|
#else
|
||||||
|
typedef unsigned long Instruction;
|
||||||
|
#endif
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
/*
|
||||||
|
** Maximum length for short strings, that is, strings that are
|
||||||
|
** internalized. (Cannot be smaller than reserved words or tags for
|
||||||
|
** metamethods, as these strings must be internalized;
|
||||||
|
** #("function") = 8, #("__newindex") = 10.)
|
||||||
|
*/
|
||||||
|
#if !defined(LUAI_MAXSHORTLEN)
|
||||||
|
#define LUAI_MAXSHORTLEN 40
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
|
||||||
/*
|
/*
|
||||||
** Inline functions
|
** Initial size for the string table (must be power of 2).
|
||||||
|
** The Lua core alone registers ~50 strings (reserved words +
|
||||||
|
** metaevent keys + a few others). Libraries would typically add
|
||||||
|
** a few dozens more.
|
||||||
*/
|
*/
|
||||||
#if !defined(LUA_USE_C89)
|
#if !defined(MINSTRTABSIZE)
|
||||||
#define l_inline inline
|
#define MINSTRTABSIZE 128
|
||||||
#elif defined(__GNUC__)
|
|
||||||
#define l_inline __inline__
|
|
||||||
#else
|
|
||||||
#define l_inline /* empty */
|
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
#define l_sinline static l_inline
|
|
||||||
|
|
||||||
|
|
||||||
/*
|
/*
|
||||||
** An unsigned with (at least) 4 bytes
|
** Size of cache for strings in the API. 'N' is the number of
|
||||||
|
** sets (better be a prime) and "M" is the size of each set (M == 1
|
||||||
|
** makes a direct cache.)
|
||||||
*/
|
*/
|
||||||
#if LUAI_IS32INT
|
#if !defined(STRCACHE_N)
|
||||||
typedef unsigned int l_uint32;
|
#define STRCACHE_N 53
|
||||||
#else
|
#define STRCACHE_M 2
|
||||||
typedef unsigned long l_uint32;
|
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
|
||||||
|
/* minimum size for string buffer */
|
||||||
|
#if !defined(LUA_MINBUFFER)
|
||||||
|
#define LUA_MINBUFFER 32
|
||||||
|
#endif
|
||||||
|
|
||||||
|
|
||||||
|
/*
|
||||||
|
** macros that are executed whenever program enters the Lua core
|
||||||
|
** ('lua_lock') and leaves the core ('lua_unlock')
|
||||||
|
*/
|
||||||
|
#if !defined(lua_lock)
|
||||||
|
#define lua_lock(L) ((void) 0)
|
||||||
|
#define lua_unlock(L) ((void) 0)
|
||||||
|
#endif
|
||||||
|
|
||||||
|
/*
|
||||||
|
** macro executed during Lua functions at points where the
|
||||||
|
** function can yield.
|
||||||
|
*/
|
||||||
|
#if !defined(luai_threadyield)
|
||||||
|
#define luai_threadyield(L) {lua_unlock(L); lua_lock(L);}
|
||||||
|
#endif
|
||||||
|
|
||||||
|
|
||||||
|
/*
|
||||||
|
** these macros allow user-specific actions on threads when you defined
|
||||||
|
** LUAI_EXTRASPACE and need to do something extra when a thread is
|
||||||
|
** created/deleted/resumed/yielded.
|
||||||
|
*/
|
||||||
|
#if !defined(luai_userstateopen)
|
||||||
|
#define luai_userstateopen(L) ((void)L)
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#if !defined(luai_userstateclose)
|
||||||
|
#define luai_userstateclose(L) ((void)L)
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#if !defined(luai_userstatethread)
|
||||||
|
#define luai_userstatethread(L,L1) ((void)L)
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#if !defined(luai_userstatefree)
|
||||||
|
#define luai_userstatefree(L,L1) ((void)L)
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#if !defined(luai_userstateresume)
|
||||||
|
#define luai_userstateresume(L,n) ((void)L)
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#if !defined(luai_userstateyield)
|
||||||
|
#define luai_userstateyield(L,n) ((void)L)
|
||||||
|
#endif
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
/*
|
/*
|
||||||
** The luai_num* macros define the primitive operations over numbers.
|
** The luai_num* macros define the primitive operations over numbers.
|
||||||
*/
|
*/
|
||||||
@@ -243,26 +270,20 @@ typedef unsigned long l_uint32;
|
|||||||
#endif
|
#endif
|
||||||
|
|
||||||
/*
|
/*
|
||||||
** modulo: defined as 'a - floor(a/b)*b'; the direct computation
|
** modulo: defined as 'a - floor(a/b)*b'; this definition gives NaN when
|
||||||
** using this definition has several problems with rounding errors,
|
** 'b' is huge, but the result should be 'a'. 'fmod' gives the result of
|
||||||
** so it is better to use 'fmod'. 'fmod' gives the result of
|
** 'a - trunc(a/b)*b', and therefore must be corrected when 'trunc(a/b)
|
||||||
** 'a - trunc(a/b)*b', and therefore must be corrected when
|
** ~= floor(a/b)'. That happens when the division has a non-integer
|
||||||
** 'trunc(a/b) ~= floor(a/b)'. That happens when the division has a
|
** negative result, which is equivalent to the test below.
|
||||||
** non-integer negative result: non-integer result is equivalent to
|
|
||||||
** a non-zero remainder 'm'; negative result is equivalent to 'a' and
|
|
||||||
** 'b' with different signs, or 'm' and 'b' with different signs
|
|
||||||
** (as the result 'm' of 'fmod' has the same sign of 'a').
|
|
||||||
*/
|
*/
|
||||||
#if !defined(luai_nummod)
|
#if !defined(luai_nummod)
|
||||||
#define luai_nummod(L,a,b,m) \
|
#define luai_nummod(L,a,b,m) \
|
||||||
{ (void)L; (m) = l_mathop(fmod)(a,b); \
|
{ (m) = l_mathop(fmod)(a,b); if ((m)*(b) < 0) (m) += (b); }
|
||||||
if (((m) > 0) ? (b) < 0 : ((m) < 0 && (b) > 0)) (m) += (b); }
|
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
/* exponentiation */
|
/* exponentiation */
|
||||||
#if !defined(luai_numpow)
|
#if !defined(luai_numpow)
|
||||||
#define luai_numpow(L,a,b) \
|
#define luai_numpow(L,a,b) ((void)L, l_mathop(pow)(a,b))
|
||||||
((void)L, (b == 2) ? (a)*(a) : l_mathop(pow)(a,b))
|
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
/* the others are quite standard operations */
|
/* the others are quite standard operations */
|
||||||
@@ -274,84 +295,29 @@ typedef unsigned long l_uint32;
|
|||||||
#define luai_numeq(a,b) ((a)==(b))
|
#define luai_numeq(a,b) ((a)==(b))
|
||||||
#define luai_numlt(a,b) ((a)<(b))
|
#define luai_numlt(a,b) ((a)<(b))
|
||||||
#define luai_numle(a,b) ((a)<=(b))
|
#define luai_numle(a,b) ((a)<=(b))
|
||||||
#define luai_numgt(a,b) ((a)>(b))
|
|
||||||
#define luai_numge(a,b) ((a)>=(b))
|
|
||||||
#define luai_numisnan(a) (!luai_numeq((a), (a)))
|
#define luai_numisnan(a) (!luai_numeq((a), (a)))
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
/*
|
|
||||||
** lua_numbertointeger converts a float number with an integral value
|
|
||||||
** to an integer, or returns 0 if the float is not within the range of
|
|
||||||
** a lua_Integer. (The range comparisons are tricky because of
|
|
||||||
** rounding. The tests here assume a two-complement representation,
|
|
||||||
** where MININTEGER always has an exact representation as a float;
|
|
||||||
** MAXINTEGER may not have one, and therefore its conversion to float
|
|
||||||
** may have an ill-defined value.)
|
|
||||||
*/
|
|
||||||
#define lua_numbertointeger(n,p) \
|
|
||||||
((n) >= (LUA_NUMBER)(LUA_MININTEGER) && \
|
|
||||||
(n) < -(LUA_NUMBER)(LUA_MININTEGER) && \
|
|
||||||
(*(p) = (LUA_INTEGER)(n), 1))
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
/*
|
/*
|
||||||
** LUAI_FUNC is a mark for all extern functions that are not to be
|
** macro to control inclusion of some hard tests on stack reallocation
|
||||||
** exported to outside modules.
|
|
||||||
** LUAI_DDEF and LUAI_DDEC are marks for all extern (const) variables,
|
|
||||||
** none of which to be exported to outside modules (LUAI_DDEF for
|
|
||||||
** definitions and LUAI_DDEC for declarations).
|
|
||||||
** Elf and MACH/gcc (versions 3.2 and later) mark them as "hidden" to
|
|
||||||
** optimize access when Lua is compiled as a shared library. Not all elf
|
|
||||||
** targets support this attribute. Unfortunately, gcc does not offer
|
|
||||||
** a way to check whether the target offers that support, and those
|
|
||||||
** without support give a warning about it. To avoid these warnings,
|
|
||||||
** change to the default definition.
|
|
||||||
*/
|
*/
|
||||||
#if !defined(LUAI_FUNC)
|
#if !defined(HARDSTACKTESTS)
|
||||||
|
#define condmovestack(L,pre,pos) ((void)0)
|
||||||
#if defined(__GNUC__) && ((__GNUC__*100 + __GNUC_MINOR__) >= 302) && \
|
|
||||||
(defined(__ELF__) || defined(__MACH__))
|
|
||||||
#define LUAI_FUNC __attribute__((visibility("internal"))) extern
|
|
||||||
#else
|
#else
|
||||||
#define LUAI_FUNC extern
|
/* realloc stack keeping its size */
|
||||||
|
#define condmovestack(L,pre,pos) \
|
||||||
|
{ int sz_ = (L)->stacksize; pre; luaD_reallocstack((L), sz_); pos; }
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
#define LUAI_DDEC(dec) LUAI_FUNC dec
|
#if !defined(HARDMEMTESTS)
|
||||||
#define LUAI_DDEF /* empty */
|
#define condchangemem(L,pre,pos) ((void)0)
|
||||||
|
#else
|
||||||
|
#define condchangemem(L,pre,pos) \
|
||||||
|
{ if (G(L)->gcrunning) { pre; luaC_fullgc(L, 0); pos; } }
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
|
||||||
/* Give these macros simpler names for internal use */
|
|
||||||
#define l_likely(x) luai_likely(x)
|
|
||||||
#define l_unlikely(x) luai_unlikely(x)
|
|
||||||
|
|
||||||
/*
|
|
||||||
** {==================================================================
|
|
||||||
** "Abstraction Layer" for basic report of messages and errors
|
|
||||||
** ===================================================================
|
|
||||||
*/
|
|
||||||
|
|
||||||
/* print a string */
|
|
||||||
#if !defined(lua_writestring)
|
|
||||||
#define lua_writestring(s,l) fwrite((s), sizeof(char), (l), stdout)
|
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
/* print a newline and flush the output */
|
|
||||||
#if !defined(lua_writeline)
|
|
||||||
#define lua_writeline() (lua_writestring("\n", 1), fflush(stdout))
|
|
||||||
#endif
|
|
||||||
|
|
||||||
/* print an error message */
|
|
||||||
#if !defined(lua_writestringerror)
|
|
||||||
#define lua_writestringerror(s,p) \
|
|
||||||
(fprintf(stderr, (s), (p)), fflush(stderr))
|
|
||||||
#endif
|
|
||||||
|
|
||||||
/* }================================================================== */
|
|
||||||
|
|
||||||
#endif
|
|
||||||
|
|
||||||
|
|||||||
+59
-414
@@ -1,5 +1,5 @@
|
|||||||
/*
|
/*
|
||||||
** $Id: lmathlib.c $
|
** $Id: lmathlib.c,v 1.119.1.1 2017/04/19 17:20:42 roberto Exp $
|
||||||
** Standard mathematical library
|
** Standard mathematical library
|
||||||
** See Copyright Notice in lua.h
|
** See Copyright Notice in lua.h
|
||||||
*/
|
*/
|
||||||
@@ -10,23 +10,32 @@
|
|||||||
#include "lprefix.h"
|
#include "lprefix.h"
|
||||||
|
|
||||||
|
|
||||||
#include <float.h>
|
|
||||||
#include <limits.h>
|
|
||||||
#include <math.h>
|
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
#include <time.h>
|
#include <math.h>
|
||||||
|
|
||||||
#include "lua.h"
|
#include "lua.h"
|
||||||
|
|
||||||
#include "lauxlib.h"
|
#include "lauxlib.h"
|
||||||
#include "lualib.h"
|
#include "lualib.h"
|
||||||
#include "llimits.h"
|
|
||||||
|
|
||||||
|
|
||||||
#undef PI
|
#undef PI
|
||||||
#define PI (l_mathop(3.141592653589793238462643383279502884))
|
#define PI (l_mathop(3.141592653589793238462643383279502884))
|
||||||
|
|
||||||
|
|
||||||
|
#if !defined(l_rand) /* { */
|
||||||
|
#if defined(LUA_USE_POSIX)
|
||||||
|
#define l_rand() random()
|
||||||
|
#define l_srand(x) srandom(x)
|
||||||
|
#define L_RANDMAX 2147483647 /* (2^31 - 1), following POSIX */
|
||||||
|
#else
|
||||||
|
#define l_rand() rand()
|
||||||
|
#define l_srand(x) srand(x)
|
||||||
|
#define L_RANDMAX RAND_MAX
|
||||||
|
#endif
|
||||||
|
#endif /* } */
|
||||||
|
|
||||||
|
|
||||||
static int math_abs (lua_State *L) {
|
static int math_abs (lua_State *L) {
|
||||||
if (lua_isinteger(L, 1)) {
|
if (lua_isinteger(L, 1)) {
|
||||||
lua_Integer n = lua_tointeger(L, 1);
|
lua_Integer n = lua_tointeger(L, 1);
|
||||||
@@ -38,37 +47,31 @@ static int math_abs (lua_State *L) {
|
|||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
static int math_sin (lua_State *L) {
|
static int math_sin (lua_State *L) {
|
||||||
lua_pushnumber(L, l_mathop(sin)(luaL_checknumber(L, 1)));
|
lua_pushnumber(L, l_mathop(sin)(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_mathop(cos)(luaL_checknumber(L, 1)));
|
lua_pushnumber(L, l_mathop(cos)(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_mathop(tan)(luaL_checknumber(L, 1)));
|
lua_pushnumber(L, l_mathop(tan)(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_mathop(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_mathop(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_Number y = luaL_checknumber(L, 1);
|
lua_Number y = luaL_checknumber(L, 1);
|
||||||
lua_Number x = luaL_optnumber(L, 2, 1);
|
lua_Number x = luaL_optnumber(L, 2, 1);
|
||||||
@@ -80,11 +83,11 @@ static int math_atan (lua_State *L) {
|
|||||||
static int math_toint (lua_State *L) {
|
static int math_toint (lua_State *L) {
|
||||||
int valid;
|
int valid;
|
||||||
lua_Integer n = lua_tointegerx(L, 1, &valid);
|
lua_Integer n = lua_tointegerx(L, 1, &valid);
|
||||||
if (l_likely(valid))
|
if (valid)
|
||||||
lua_pushinteger(L, n);
|
lua_pushinteger(L, n);
|
||||||
else {
|
else {
|
||||||
luaL_checkany(L, 1);
|
luaL_checkany(L, 1);
|
||||||
luaL_pushfail(L); /* value is not convertible to integer */
|
lua_pushnil(L); /* value is not convertible to integer */
|
||||||
}
|
}
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
@@ -112,7 +115,7 @@ static int math_floor (lua_State *L) {
|
|||||||
|
|
||||||
static int math_ceil (lua_State *L) {
|
static int math_ceil (lua_State *L) {
|
||||||
if (lua_isinteger(L, 1))
|
if (lua_isinteger(L, 1))
|
||||||
lua_settop(L, 1); /* integer is its own ceiling */
|
lua_settop(L, 1); /* integer is its own ceil */
|
||||||
else {
|
else {
|
||||||
lua_Number d = l_mathop(ceil)(luaL_checknumber(L, 1));
|
lua_Number d = l_mathop(ceil)(luaL_checknumber(L, 1));
|
||||||
pushnumint(L, d);
|
pushnumint(L, d);
|
||||||
@@ -173,7 +176,6 @@ static int math_ult (lua_State *L) {
|
|||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
static int math_log (lua_State *L) {
|
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;
|
||||||
@@ -183,8 +185,7 @@ static int math_log (lua_State *L) {
|
|||||||
lua_Number base = luaL_checknumber(L, 2);
|
lua_Number base = luaL_checknumber(L, 2);
|
||||||
#if !defined(LUA_USE_C89)
|
#if !defined(LUA_USE_C89)
|
||||||
if (base == l_mathop(2.0))
|
if (base == l_mathop(2.0))
|
||||||
res = l_mathop(log2)(x);
|
res = l_mathop(log2)(x); else
|
||||||
else
|
|
||||||
#endif
|
#endif
|
||||||
if (base == l_mathop(10.0))
|
if (base == l_mathop(10.0))
|
||||||
res = l_mathop(log10)(x);
|
res = l_mathop(log10)(x);
|
||||||
@@ -195,42 +196,22 @@ static int math_log (lua_State *L) {
|
|||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
static int math_exp (lua_State *L) {
|
static int math_exp (lua_State *L) {
|
||||||
lua_pushnumber(L, l_mathop(exp)(luaL_checknumber(L, 1)));
|
lua_pushnumber(L, l_mathop(exp)(luaL_checknumber(L, 1)));
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
static int math_deg (lua_State *L) {
|
static int math_deg (lua_State *L) {
|
||||||
lua_pushnumber(L, luaL_checknumber(L, 1) * (l_mathop(180.0) / PI));
|
lua_pushnumber(L, luaL_checknumber(L, 1) * (l_mathop(180.0) / PI));
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
static int math_rad (lua_State *L) {
|
static int math_rad (lua_State *L) {
|
||||||
lua_pushnumber(L, luaL_checknumber(L, 1) * (PI / l_mathop(180.0)));
|
lua_pushnumber(L, luaL_checknumber(L, 1) * (PI / l_mathop(180.0)));
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
static int math_frexp (lua_State *L) {
|
|
||||||
lua_Number x = luaL_checknumber(L, 1);
|
|
||||||
int ep;
|
|
||||||
lua_pushnumber(L, l_mathop(frexp)(x, &ep));
|
|
||||||
lua_pushinteger(L, ep);
|
|
||||||
return 2;
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
static int math_ldexp (lua_State *L) {
|
|
||||||
lua_Number x = luaL_checknumber(L, 1);
|
|
||||||
int ep = (int)luaL_checkinteger(L, 2);
|
|
||||||
lua_pushnumber(L, l_mathop(ldexp)(x, ep));
|
|
||||||
return 1;
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
static int math_min (lua_State *L) {
|
static int math_min (lua_State *L) {
|
||||||
int n = lua_gettop(L); /* number of arguments */
|
int n = lua_gettop(L); /* number of arguments */
|
||||||
int imin = 1; /* index of current minimum value */
|
int imin = 1; /* index of current minimum value */
|
||||||
@@ -258,344 +239,22 @@ static int math_max (lua_State *L) {
|
|||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
static int math_type (lua_State *L) {
|
|
||||||
if (lua_type(L, 1) == LUA_TNUMBER)
|
|
||||||
lua_pushstring(L, (lua_isinteger(L, 1)) ? "integer" : "float");
|
|
||||||
else {
|
|
||||||
luaL_checkany(L, 1);
|
|
||||||
luaL_pushfail(L);
|
|
||||||
}
|
|
||||||
return 1;
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
/*
|
/*
|
||||||
** {==================================================================
|
** This function uses 'double' (instead of 'lua_Number') to ensure that
|
||||||
** Pseudo-Random Number Generator based on 'xoshiro256**'.
|
** all bits from 'l_rand' can be represented, and that 'RANDMAX + 1.0'
|
||||||
** ===================================================================
|
** will keep full precision (ensuring that 'r' is always less than 1.0.)
|
||||||
*/
|
*/
|
||||||
|
|
||||||
/*
|
|
||||||
** This code uses lots of shifts. ISO C does not allow shifts greater
|
|
||||||
** than or equal to the width of the type being shifted, so some shifts
|
|
||||||
** are written in convoluted ways to match that restriction. For
|
|
||||||
** preprocessor tests, it assumes a width of 32 bits, so the maximum
|
|
||||||
** shift there is 31 bits.
|
|
||||||
*/
|
|
||||||
|
|
||||||
|
|
||||||
/* number of binary digits in the mantissa of a float */
|
|
||||||
#define FIGS l_floatatt(MANT_DIG)
|
|
||||||
|
|
||||||
#if FIGS > 64
|
|
||||||
/* there are only 64 random bits; use them all */
|
|
||||||
#undef FIGS
|
|
||||||
#define FIGS 64
|
|
||||||
#endif
|
|
||||||
|
|
||||||
|
|
||||||
/*
|
|
||||||
** LUA_RAND32 forces the use of 32-bit integers in the implementation
|
|
||||||
** of the PRN generator (mainly for testing).
|
|
||||||
*/
|
|
||||||
#if !defined(LUA_RAND32) && !defined(Rand64)
|
|
||||||
|
|
||||||
/* try to find an integer type with at least 64 bits */
|
|
||||||
|
|
||||||
#if ((ULONG_MAX >> 31) >> 31) >= 3
|
|
||||||
|
|
||||||
/* 'long' has at least 64 bits */
|
|
||||||
#define Rand64 unsigned long
|
|
||||||
#define SRand64 long
|
|
||||||
|
|
||||||
#elif !defined(LUA_USE_C89) && defined(LLONG_MAX)
|
|
||||||
|
|
||||||
/* there is a 'long long' type (which must have at least 64 bits) */
|
|
||||||
#define Rand64 unsigned long long
|
|
||||||
#define SRand64 long long
|
|
||||||
|
|
||||||
#elif ((LUA_MAXUNSIGNED >> 31) >> 31) >= 3
|
|
||||||
|
|
||||||
/* 'lua_Unsigned' has at least 64 bits */
|
|
||||||
#define Rand64 lua_Unsigned
|
|
||||||
#define SRand64 lua_Integer
|
|
||||||
|
|
||||||
#endif
|
|
||||||
|
|
||||||
#endif
|
|
||||||
|
|
||||||
|
|
||||||
#if defined(Rand64) /* { */
|
|
||||||
|
|
||||||
/*
|
|
||||||
** Standard implementation, using 64-bit integers.
|
|
||||||
** If 'Rand64' has more than 64 bits, the extra bits do not interfere
|
|
||||||
** with the 64 initial bits, except in a right shift. Moreover, the
|
|
||||||
** final result has to discard the extra bits.
|
|
||||||
*/
|
|
||||||
|
|
||||||
/* avoid using extra bits when needed */
|
|
||||||
#define trim64(x) ((x) & 0xffffffffffffffffu)
|
|
||||||
|
|
||||||
|
|
||||||
/* rotate left 'x' by 'n' bits */
|
|
||||||
static Rand64 rotl (Rand64 x, int n) {
|
|
||||||
return (x << n) | (trim64(x) >> (64 - n));
|
|
||||||
}
|
|
||||||
|
|
||||||
static Rand64 nextrand (Rand64 *state) {
|
|
||||||
Rand64 state0 = state[0];
|
|
||||||
Rand64 state1 = state[1];
|
|
||||||
Rand64 state2 = state[2] ^ state0;
|
|
||||||
Rand64 state3 = state[3] ^ state1;
|
|
||||||
Rand64 res = rotl(state1 * 5, 7) * 9;
|
|
||||||
state[0] = state0 ^ state3;
|
|
||||||
state[1] = state1 ^ state2;
|
|
||||||
state[2] = state2 ^ (state1 << 17);
|
|
||||||
state[3] = rotl(state3, 45);
|
|
||||||
return res;
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
/*
|
|
||||||
** Convert bits from a random integer into a float in the
|
|
||||||
** interval [0,1), getting the higher FIG bits from the
|
|
||||||
** random unsigned integer and converting that to a float.
|
|
||||||
** Some old Microsoft compilers cannot cast an unsigned long
|
|
||||||
** to a floating-point number, so we use a signed long as an
|
|
||||||
** intermediary. When lua_Number is float or double, the shift ensures
|
|
||||||
** that 'sx' is non negative; in that case, a good compiler will remove
|
|
||||||
** the correction.
|
|
||||||
*/
|
|
||||||
|
|
||||||
/* must throw out the extra (64 - FIGS) bits */
|
|
||||||
#define shift64_FIG (64 - FIGS)
|
|
||||||
|
|
||||||
/* 2^(-FIGS) == 2^-1 / 2^(FIGS-1) */
|
|
||||||
#define scaleFIG (l_mathop(0.5) / ((Rand64)1 << (FIGS - 1)))
|
|
||||||
|
|
||||||
static lua_Number I2d (Rand64 x) {
|
|
||||||
SRand64 sx = (SRand64)(trim64(x) >> shift64_FIG);
|
|
||||||
lua_Number res = (lua_Number)(sx) * scaleFIG;
|
|
||||||
if (sx < 0)
|
|
||||||
res += l_mathop(1.0); /* correct the two's complement if negative */
|
|
||||||
lua_assert(0 <= res && res < 1);
|
|
||||||
return res;
|
|
||||||
}
|
|
||||||
|
|
||||||
/* convert a 'Rand64' to a 'lua_Unsigned' */
|
|
||||||
#define I2UInt(x) ((lua_Unsigned)trim64(x))
|
|
||||||
|
|
||||||
/* convert a 'lua_Unsigned' to a 'Rand64' */
|
|
||||||
#define Int2I(x) ((Rand64)(x))
|
|
||||||
|
|
||||||
|
|
||||||
#else /* no 'Rand64' }{ */
|
|
||||||
|
|
||||||
/*
|
|
||||||
** Use two 32-bit integers to represent a 64-bit quantity.
|
|
||||||
*/
|
|
||||||
typedef struct Rand64 {
|
|
||||||
l_uint32 h; /* higher half */
|
|
||||||
l_uint32 l; /* lower half */
|
|
||||||
} Rand64;
|
|
||||||
|
|
||||||
|
|
||||||
/*
|
|
||||||
** If 'l_uint32' has more than 32 bits, the extra bits do not interfere
|
|
||||||
** with the 32 initial bits, except in a right shift and comparisons.
|
|
||||||
** Moreover, the final result has to discard the extra bits.
|
|
||||||
*/
|
|
||||||
|
|
||||||
/* avoid using extra bits when needed */
|
|
||||||
#define trim32(x) ((x) & 0xffffffffu)
|
|
||||||
|
|
||||||
|
|
||||||
/*
|
|
||||||
** basic operations on 'Rand64' values
|
|
||||||
*/
|
|
||||||
|
|
||||||
/* build a new Rand64 value */
|
|
||||||
static Rand64 packI (l_uint32 h, l_uint32 l) {
|
|
||||||
Rand64 result;
|
|
||||||
result.h = h;
|
|
||||||
result.l = l;
|
|
||||||
return result;
|
|
||||||
}
|
|
||||||
|
|
||||||
/* return i << n */
|
|
||||||
static Rand64 Ishl (Rand64 i, int n) {
|
|
||||||
lua_assert(n > 0 && n < 32);
|
|
||||||
return packI((i.h << n) | (trim32(i.l) >> (32 - n)), i.l << n);
|
|
||||||
}
|
|
||||||
|
|
||||||
/* i1 ^= i2 */
|
|
||||||
static void Ixor (Rand64 *i1, Rand64 i2) {
|
|
||||||
i1->h ^= i2.h;
|
|
||||||
i1->l ^= i2.l;
|
|
||||||
}
|
|
||||||
|
|
||||||
/* return i1 + i2 */
|
|
||||||
static Rand64 Iadd (Rand64 i1, Rand64 i2) {
|
|
||||||
Rand64 result = packI(i1.h + i2.h, i1.l + i2.l);
|
|
||||||
if (trim32(result.l) < trim32(i1.l)) /* carry? */
|
|
||||||
result.h++;
|
|
||||||
return result;
|
|
||||||
}
|
|
||||||
|
|
||||||
/* return i * 5 */
|
|
||||||
static Rand64 times5 (Rand64 i) {
|
|
||||||
return Iadd(Ishl(i, 2), i); /* i * 5 == (i << 2) + i */
|
|
||||||
}
|
|
||||||
|
|
||||||
/* return i * 9 */
|
|
||||||
static Rand64 times9 (Rand64 i) {
|
|
||||||
return Iadd(Ishl(i, 3), i); /* i * 9 == (i << 3) + i */
|
|
||||||
}
|
|
||||||
|
|
||||||
/* return 'i' rotated left 'n' bits */
|
|
||||||
static Rand64 rotl (Rand64 i, int n) {
|
|
||||||
lua_assert(n > 0 && n < 32);
|
|
||||||
return packI((i.h << n) | (trim32(i.l) >> (32 - n)),
|
|
||||||
(trim32(i.h) >> (32 - n)) | (i.l << n));
|
|
||||||
}
|
|
||||||
|
|
||||||
/* for offsets larger than 32, rotate right by 64 - offset */
|
|
||||||
static Rand64 rotl1 (Rand64 i, int n) {
|
|
||||||
lua_assert(n > 32 && n < 64);
|
|
||||||
n = 64 - n;
|
|
||||||
return packI((trim32(i.h) >> n) | (i.l << (32 - n)),
|
|
||||||
(i.h << (32 - n)) | (trim32(i.l) >> n));
|
|
||||||
}
|
|
||||||
|
|
||||||
/*
|
|
||||||
** implementation of 'xoshiro256**' algorithm on 'Rand64' values
|
|
||||||
*/
|
|
||||||
static Rand64 nextrand (Rand64 *state) {
|
|
||||||
Rand64 res = times9(rotl(times5(state[1]), 7));
|
|
||||||
Rand64 t = Ishl(state[1], 17);
|
|
||||||
Ixor(&state[2], state[0]);
|
|
||||||
Ixor(&state[3], state[1]);
|
|
||||||
Ixor(&state[1], state[2]);
|
|
||||||
Ixor(&state[0], state[3]);
|
|
||||||
Ixor(&state[2], t);
|
|
||||||
state[3] = rotl1(state[3], 45);
|
|
||||||
return res;
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
/*
|
|
||||||
** Converts a 'Rand64' into a float.
|
|
||||||
*/
|
|
||||||
|
|
||||||
/* an unsigned 1 with proper type */
|
|
||||||
#define UONE ((l_uint32)1)
|
|
||||||
|
|
||||||
|
|
||||||
#if FIGS <= 32
|
|
||||||
|
|
||||||
/* 2^(-FIGS) */
|
|
||||||
#define scaleFIG (l_mathop(0.5) / (UONE << (FIGS - 1)))
|
|
||||||
|
|
||||||
/*
|
|
||||||
** get up to 32 bits from higher half, shifting right to
|
|
||||||
** throw out the extra bits.
|
|
||||||
*/
|
|
||||||
static lua_Number I2d (Rand64 x) {
|
|
||||||
lua_Number h = (lua_Number)(trim32(x.h) >> (32 - FIGS));
|
|
||||||
return h * scaleFIG;
|
|
||||||
}
|
|
||||||
|
|
||||||
#else /* 32 < FIGS <= 64 */
|
|
||||||
|
|
||||||
/* 2^(-FIGS) = 1.0 / 2^30 / 2^3 / 2^(FIGS-33) */
|
|
||||||
#define scaleFIG \
|
|
||||||
(l_mathop(1.0) / (UONE << 30) / l_mathop(8.0) / (UONE << (FIGS - 33)))
|
|
||||||
|
|
||||||
/*
|
|
||||||
** use FIGS - 32 bits from lower half, throwing out the other
|
|
||||||
** (32 - (FIGS - 32)) = (64 - FIGS) bits
|
|
||||||
*/
|
|
||||||
#define shiftLOW (64 - FIGS)
|
|
||||||
|
|
||||||
/*
|
|
||||||
** higher 32 bits go after those (FIGS - 32) bits: shiftHI = 2^(FIGS - 32)
|
|
||||||
*/
|
|
||||||
#define shiftHI ((lua_Number)(UONE << (FIGS - 33)) * l_mathop(2.0))
|
|
||||||
|
|
||||||
|
|
||||||
static lua_Number I2d (Rand64 x) {
|
|
||||||
lua_Number h = (lua_Number)trim32(x.h) * shiftHI;
|
|
||||||
lua_Number l = (lua_Number)(trim32(x.l) >> shiftLOW);
|
|
||||||
return (h + l) * scaleFIG;
|
|
||||||
}
|
|
||||||
|
|
||||||
#endif
|
|
||||||
|
|
||||||
|
|
||||||
/* convert a 'Rand64' to a 'lua_Unsigned' */
|
|
||||||
static lua_Unsigned I2UInt (Rand64 x) {
|
|
||||||
return (((lua_Unsigned)trim32(x.h) << 31) << 1) | (lua_Unsigned)trim32(x.l);
|
|
||||||
}
|
|
||||||
|
|
||||||
/* convert a 'lua_Unsigned' to a 'Rand64' */
|
|
||||||
static Rand64 Int2I (lua_Unsigned n) {
|
|
||||||
return packI((l_uint32)((n >> 31) >> 1), (l_uint32)n);
|
|
||||||
}
|
|
||||||
|
|
||||||
#endif /* } */
|
|
||||||
|
|
||||||
|
|
||||||
/*
|
|
||||||
** A state uses four 'Rand64' values.
|
|
||||||
*/
|
|
||||||
typedef struct {
|
|
||||||
Rand64 s[4];
|
|
||||||
} RanState;
|
|
||||||
|
|
||||||
|
|
||||||
/*
|
|
||||||
** Project the random integer 'ran' into the interval [0, n].
|
|
||||||
** Because 'ran' has 2^B possible values, the projection can only be
|
|
||||||
** uniform when the size of the interval is a power of 2 (exact
|
|
||||||
** division). So, to get a uniform projection into [0, n], we
|
|
||||||
** first compute 'lim', the smallest Mersenne number not smaller than
|
|
||||||
** 'n'. We then project 'ran' into the interval [0, lim]. If the result
|
|
||||||
** is inside [0, n], we are done. Otherwise, we try with another 'ran',
|
|
||||||
** until we have a result inside the interval.
|
|
||||||
*/
|
|
||||||
static lua_Unsigned project (lua_Unsigned ran, lua_Unsigned n,
|
|
||||||
RanState *state) {
|
|
||||||
lua_Unsigned lim = n; /* to compute the Mersenne number */
|
|
||||||
int sh; /* how much to spread bits to the right in 'lim' */
|
|
||||||
/* spread '1' bits in 'lim' until it becomes a Mersenne number */
|
|
||||||
for (sh = 1; (lim & (lim + 1)) != 0; sh *= 2)
|
|
||||||
lim |= (lim >> sh); /* spread '1's to the right */
|
|
||||||
while ((ran &= lim) > n) /* project 'ran' into [0..lim] and test */
|
|
||||||
ran = I2UInt(nextrand(state->s)); /* not inside [0..n]? try again */
|
|
||||||
return ran;
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
static int math_random (lua_State *L) {
|
static int math_random (lua_State *L) {
|
||||||
lua_Integer low, up;
|
lua_Integer low, up;
|
||||||
lua_Unsigned p;
|
double r = (double)l_rand() * (1.0 / ((double)L_RANDMAX + 1.0));
|
||||||
RanState *state = (RanState *)lua_touserdata(L, lua_upvalueindex(1));
|
|
||||||
Rand64 rv = nextrand(state->s); /* next pseudo-random value */
|
|
||||||
switch (lua_gettop(L)) { /* check number of arguments */
|
switch (lua_gettop(L)) { /* check number of arguments */
|
||||||
case 0: { /* no arguments */
|
case 0: { /* no arguments */
|
||||||
lua_pushnumber(L, I2d(rv)); /* float between 0 and 1 */
|
lua_pushnumber(L, (lua_Number)r); /* Number between 0 and 1 */
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
case 1: { /* only upper limit */
|
case 1: { /* only upper limit */
|
||||||
low = 1;
|
low = 1;
|
||||||
up = luaL_checkinteger(L, 1);
|
up = luaL_checkinteger(L, 1);
|
||||||
if (up == 0) { /* single 0 as argument? */
|
|
||||||
lua_pushinteger(L, l_castU2S(I2UInt(rv))); /* full random integer */
|
|
||||||
return 1;
|
|
||||||
}
|
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
case 2: { /* lower and upper limits */
|
case 2: { /* lower and upper limits */
|
||||||
@@ -607,63 +266,36 @@ static int math_random (lua_State *L) {
|
|||||||
}
|
}
|
||||||
/* random integer in the interval [low, up] */
|
/* random integer in the interval [low, up] */
|
||||||
luaL_argcheck(L, low <= up, 1, "interval is empty");
|
luaL_argcheck(L, low <= up, 1, "interval is empty");
|
||||||
/* project random integer into the interval [0, up - low] */
|
luaL_argcheck(L, low >= 0 || up <= LUA_MAXINTEGER + low, 1,
|
||||||
p = project(I2UInt(rv), l_castS2U(up) - l_castS2U(low), state);
|
"interval too large");
|
||||||
lua_pushinteger(L, l_castU2S(p + l_castS2U(low)));
|
r *= (double)(up - low) + 1.0;
|
||||||
|
lua_pushinteger(L, (lua_Integer)r + low);
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
static void setseed (lua_State *L, Rand64 *state,
|
static int math_randomseed (lua_State *L) {
|
||||||
lua_Unsigned n1, lua_Unsigned n2) {
|
l_srand((unsigned int)(lua_Integer)luaL_checknumber(L, 1));
|
||||||
int i;
|
(void)l_rand(); /* discard first value to avoid undesirable correlations */
|
||||||
state[0] = Int2I(n1);
|
return 0;
|
||||||
state[1] = Int2I(0xff); /* avoid a zero state */
|
|
||||||
state[2] = Int2I(n2);
|
|
||||||
state[3] = Int2I(0);
|
|
||||||
for (i = 0; i < 16; i++)
|
|
||||||
nextrand(state); /* discard initial values to "spread" seed */
|
|
||||||
lua_pushinteger(L, l_castU2S(n1));
|
|
||||||
lua_pushinteger(L, l_castU2S(n2));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
static int math_randomseed (lua_State *L) {
|
static int math_type (lua_State *L) {
|
||||||
RanState *state = (RanState *)lua_touserdata(L, lua_upvalueindex(1));
|
if (lua_type(L, 1) == LUA_TNUMBER) {
|
||||||
lua_Unsigned n1, n2;
|
if (lua_isinteger(L, 1))
|
||||||
if (lua_isnone(L, 1)) {
|
lua_pushliteral(L, "integer");
|
||||||
n1 = luaL_makeseed(L); /* "random" seed */
|
else
|
||||||
n2 = I2UInt(nextrand(state->s)); /* in case seed is not that random... */
|
lua_pushliteral(L, "float");
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
n1 = l_castS2U(luaL_checkinteger(L, 1));
|
luaL_checkany(L, 1);
|
||||||
n2 = l_castS2U(luaL_optinteger(L, 2, 0));
|
lua_pushnil(L);
|
||||||
}
|
}
|
||||||
setseed(L, state->s, n1, n2);
|
return 1;
|
||||||
return 2; /* return seeds */
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
static const luaL_Reg randfuncs[] = {
|
|
||||||
{"random", math_random},
|
|
||||||
{"randomseed", math_randomseed},
|
|
||||||
{NULL, NULL}
|
|
||||||
};
|
|
||||||
|
|
||||||
|
|
||||||
/*
|
|
||||||
** Register the random functions and initialize their state.
|
|
||||||
*/
|
|
||||||
static void setrandfunc (lua_State *L) {
|
|
||||||
RanState *state = (RanState *)lua_newuserdatauv(L, sizeof(RanState), 0);
|
|
||||||
setseed(L, state->s, luaL_makeseed(L), 0); /* initialize with random seed */
|
|
||||||
lua_pop(L, 2); /* remove pushed seeds */
|
|
||||||
luaL_setfuncs(L, randfuncs, 1);
|
|
||||||
}
|
|
||||||
|
|
||||||
/* }================================================================== */
|
|
||||||
|
|
||||||
|
|
||||||
/*
|
/*
|
||||||
** {==================================================================
|
** {==================================================================
|
||||||
** Deprecated functions (for compatibility only)
|
** Deprecated functions (for compatibility only)
|
||||||
@@ -693,6 +325,20 @@ static int math_pow (lua_State *L) {
|
|||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
static int math_frexp (lua_State *L) {
|
||||||
|
int e;
|
||||||
|
lua_pushnumber(L, l_mathop(frexp)(luaL_checknumber(L, 1), &e));
|
||||||
|
lua_pushinteger(L, e);
|
||||||
|
return 2;
|
||||||
|
}
|
||||||
|
|
||||||
|
static int math_ldexp (lua_State *L) {
|
||||||
|
lua_Number x = luaL_checknumber(L, 1);
|
||||||
|
int ep = (int)luaL_checkinteger(L, 2);
|
||||||
|
lua_pushnumber(L, l_mathop(ldexp)(x, ep));
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
|
||||||
static int math_log10 (lua_State *L) {
|
static int math_log10 (lua_State *L) {
|
||||||
lua_pushnumber(L, l_mathop(log10)(luaL_checknumber(L, 1)));
|
lua_pushnumber(L, l_mathop(log10)(luaL_checknumber(L, 1)));
|
||||||
return 1;
|
return 1;
|
||||||
@@ -715,14 +361,14 @@ static const luaL_Reg mathlib[] = {
|
|||||||
{"tointeger", math_toint},
|
{"tointeger", math_toint},
|
||||||
{"floor", math_floor},
|
{"floor", math_floor},
|
||||||
{"fmod", math_fmod},
|
{"fmod", math_fmod},
|
||||||
{"frexp", math_frexp},
|
|
||||||
{"ult", math_ult},
|
{"ult", math_ult},
|
||||||
{"ldexp", math_ldexp},
|
|
||||||
{"log", math_log},
|
{"log", math_log},
|
||||||
{"max", math_max},
|
{"max", math_max},
|
||||||
{"min", math_min},
|
{"min", math_min},
|
||||||
{"modf", math_modf},
|
{"modf", math_modf},
|
||||||
{"rad", math_rad},
|
{"rad", math_rad},
|
||||||
|
{"random", math_random},
|
||||||
|
{"randomseed", math_randomseed},
|
||||||
{"sin", math_sin},
|
{"sin", math_sin},
|
||||||
{"sqrt", math_sqrt},
|
{"sqrt", math_sqrt},
|
||||||
{"tan", math_tan},
|
{"tan", math_tan},
|
||||||
@@ -733,11 +379,11 @@ static const luaL_Reg mathlib[] = {
|
|||||||
{"sinh", math_sinh},
|
{"sinh", math_sinh},
|
||||||
{"tanh", math_tanh},
|
{"tanh", math_tanh},
|
||||||
{"pow", math_pow},
|
{"pow", math_pow},
|
||||||
|
{"frexp", math_frexp},
|
||||||
|
{"ldexp", math_ldexp},
|
||||||
{"log10", math_log10},
|
{"log10", math_log10},
|
||||||
#endif
|
#endif
|
||||||
/* placeholders */
|
/* placeholders */
|
||||||
{"random", NULL},
|
|
||||||
{"randomseed", NULL},
|
|
||||||
{"pi", NULL},
|
{"pi", NULL},
|
||||||
{"huge", NULL},
|
{"huge", NULL},
|
||||||
{"maxinteger", NULL},
|
{"maxinteger", NULL},
|
||||||
@@ -759,7 +405,6 @@ LUAMOD_API int luaopen_math (lua_State *L) {
|
|||||||
lua_setfield(L, -2, "maxinteger");
|
lua_setfield(L, -2, "maxinteger");
|
||||||
lua_pushinteger(L, LUA_MININTEGER);
|
lua_pushinteger(L, LUA_MININTEGER);
|
||||||
lua_setfield(L, -2, "mininteger");
|
lua_setfield(L, -2, "mininteger");
|
||||||
setrandfunc(L);
|
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -1,5 +1,5 @@
|
|||||||
/*
|
/*
|
||||||
** $Id: lmem.c $
|
** $Id: lmem.c,v 1.91.1.1 2017/04/19 17:20:42 roberto Exp $
|
||||||
** Interface to Memory Manager
|
** Interface to Memory Manager
|
||||||
** See Copyright Notice in lua.h
|
** See Copyright Notice in lua.h
|
||||||
*/
|
*/
|
||||||
@@ -25,191 +25,76 @@
|
|||||||
|
|
||||||
/*
|
/*
|
||||||
** About the realloc function:
|
** About the realloc function:
|
||||||
** void *frealloc (void *ud, void *ptr, size_t osize, size_t nsize);
|
** void * frealloc (void *ud, void *ptr, size_t osize, size_t nsize);
|
||||||
** ('osize' is the old size, 'nsize' is the new size)
|
** ('osize' is the old size, 'nsize' is the new size)
|
||||||
**
|
**
|
||||||
** - frealloc(ud, p, x, 0) frees the block 'p' and returns NULL.
|
** * frealloc(ud, NULL, x, s) creates a new block of size 's' (no
|
||||||
** Particularly, frealloc(ud, NULL, 0, 0) does nothing,
|
** matter 'x').
|
||||||
** which is equivalent to free(NULL) in ISO C.
|
|
||||||
**
|
**
|
||||||
** - frealloc(ud, NULL, x, s) creates a new block of size 's'
|
** * frealloc(ud, p, x, 0) frees the block 'p'
|
||||||
** (no matter 'x'). Returns NULL if it cannot create the new block.
|
** (in this specific case, frealloc must return NULL);
|
||||||
|
** particularly, frealloc(ud, NULL, 0, 0) does nothing
|
||||||
|
** (which is equivalent to free(NULL) in ISO C)
|
||||||
**
|
**
|
||||||
** - otherwise, frealloc(ud, b, x, y) reallocates the block 'b' from
|
** frealloc returns NULL if it cannot create or reallocate the area
|
||||||
** size 'x' to size 'y'. Returns NULL if it cannot reallocate the
|
** (any reallocation to an equal or smaller size cannot fail!)
|
||||||
** block to the new size.
|
|
||||||
*/
|
*/
|
||||||
|
|
||||||
|
|
||||||
/*
|
|
||||||
** Macro to call the allocation function.
|
|
||||||
*/
|
|
||||||
#define callfrealloc(g,block,os,ns) ((*g->frealloc)(g->ud, block, os, ns))
|
|
||||||
|
|
||||||
|
|
||||||
/*
|
|
||||||
** When an allocation fails, it will try again after an emergency
|
|
||||||
** collection, except when it cannot run a collection. The GC should
|
|
||||||
** not be called while the state is not fully built, as the collector
|
|
||||||
** is not yet fully initialized. Also, it should not be called when
|
|
||||||
** 'gcstopem' is true, because then the interpreter is in the middle of
|
|
||||||
** a collection step.
|
|
||||||
*/
|
|
||||||
#define cantryagain(g) (completestate(g) && !g->gcstopem)
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
#if defined(EMERGENCYGCTESTS)
|
|
||||||
/*
|
|
||||||
** First allocation will fail except when freeing a block (frees never
|
|
||||||
** fail) and when it cannot try again; this fail will trigger 'tryagain'
|
|
||||||
** and a full GC cycle at every allocation.
|
|
||||||
*/
|
|
||||||
static void *firsttry (global_State *g, void *block, size_t os, size_t ns) {
|
|
||||||
if (ns > 0 && cantryagain(g))
|
|
||||||
return NULL; /* fail */
|
|
||||||
else /* normal allocation */
|
|
||||||
return callfrealloc(g, block, os, ns);
|
|
||||||
}
|
|
||||||
#else
|
|
||||||
#define firsttry(g,block,os,ns) callfrealloc(g, block, os, ns)
|
|
||||||
#endif
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
/*
|
|
||||||
** {==================================================================
|
|
||||||
** Functions to allocate/deallocate arrays for the Parser
|
|
||||||
** ===================================================================
|
|
||||||
*/
|
|
||||||
|
|
||||||
/*
|
|
||||||
** Minimum size for arrays during parsing, to avoid overhead of
|
|
||||||
** reallocating to size 1, then 2, and then 4. All these arrays
|
|
||||||
** will be reallocated to exact sizes or erased when parsing ends.
|
|
||||||
*/
|
|
||||||
#define MINSIZEARRAY 4
|
#define MINSIZEARRAY 4
|
||||||
|
|
||||||
|
|
||||||
void *luaM_growaux_ (lua_State *L, void *block, int nelems, int *psize,
|
void *luaM_growaux_ (lua_State *L, void *block, int *size, size_t size_elems,
|
||||||
unsigned size_elems, int limit, const char *what) {
|
int limit, const char *what) {
|
||||||
void *newblock;
|
void *newblock;
|
||||||
int size = *psize;
|
int newsize;
|
||||||
if (nelems + 1 <= size) /* does one extra element still fit? */
|
if (*size >= limit/2) { /* cannot double it? */
|
||||||
return block; /* nothing to be done */
|
if (*size >= limit) /* cannot grow even a little? */
|
||||||
if (size >= limit / 2) { /* cannot double it? */
|
|
||||||
if (l_unlikely(size >= limit)) /* cannot grow even a little? */
|
|
||||||
luaG_runerror(L, "too many %s (limit is %d)", what, limit);
|
luaG_runerror(L, "too many %s (limit is %d)", what, limit);
|
||||||
size = limit; /* still have at least one free place */
|
newsize = limit; /* still have at least one free place */
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
size *= 2;
|
newsize = (*size)*2;
|
||||||
if (size < MINSIZEARRAY)
|
if (newsize < MINSIZEARRAY)
|
||||||
size = MINSIZEARRAY; /* minimum size */
|
newsize = MINSIZEARRAY; /* minimum size */
|
||||||
}
|
}
|
||||||
lua_assert(nelems + 1 <= size && size <= limit);
|
newblock = luaM_reallocv(L, block, *size, newsize, size_elems);
|
||||||
/* 'limit' ensures that multiplication will not overflow */
|
*size = newsize; /* update only when everything else is OK */
|
||||||
newblock = luaM_saferealloc_(L, block, cast_sizet(*psize) * size_elems,
|
|
||||||
cast_sizet(size) * size_elems);
|
|
||||||
*psize = size; /* update only when everything else is OK */
|
|
||||||
return newblock;
|
return newblock;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/*
|
|
||||||
** In prototypes, the size of the array is also its number of
|
|
||||||
** elements (to save memory). So, if it cannot shrink an array
|
|
||||||
** to its number of elements, the only option is to raise an
|
|
||||||
** error.
|
|
||||||
*/
|
|
||||||
void *luaM_shrinkvector_ (lua_State *L, void *block, int *size,
|
|
||||||
int final_n, unsigned size_elem) {
|
|
||||||
void *newblock;
|
|
||||||
size_t oldsize = cast_sizet(*size) * size_elem;
|
|
||||||
size_t newsize = cast_sizet(final_n) * size_elem;
|
|
||||||
lua_assert(newsize <= oldsize);
|
|
||||||
newblock = luaM_saferealloc_(L, block, oldsize, newsize);
|
|
||||||
*size = final_n;
|
|
||||||
return newblock;
|
|
||||||
}
|
|
||||||
|
|
||||||
/* }================================================================== */
|
|
||||||
|
|
||||||
|
|
||||||
l_noret luaM_toobig (lua_State *L) {
|
l_noret luaM_toobig (lua_State *L) {
|
||||||
luaG_runerror(L, "memory allocation error: block too big");
|
luaG_runerror(L, "memory allocation error: block too big");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/*
|
|
||||||
** Free memory
|
|
||||||
*/
|
|
||||||
void luaM_free_ (lua_State *L, void *block, size_t osize) {
|
|
||||||
global_State *g = G(L);
|
|
||||||
lua_assert((osize == 0) == (block == NULL));
|
|
||||||
callfrealloc(g, block, osize, 0);
|
|
||||||
g->GCdebt += cast(l_mem, osize);
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
/*
|
/*
|
||||||
** In case of allocation fail, this function will do an emergency
|
** generic allocation routine.
|
||||||
** collection to free some memory and then try the allocation again.
|
|
||||||
*/
|
|
||||||
static void *tryagain (lua_State *L, void *block,
|
|
||||||
size_t osize, size_t nsize) {
|
|
||||||
global_State *g = G(L);
|
|
||||||
if (cantryagain(g)) {
|
|
||||||
luaC_fullgc(L, 1); /* try to free some memory... */
|
|
||||||
return callfrealloc(g, block, osize, nsize); /* try again */
|
|
||||||
}
|
|
||||||
else return NULL; /* cannot run an emergency collection */
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
/*
|
|
||||||
** Generic allocation routine.
|
|
||||||
*/
|
*/
|
||||||
void *luaM_realloc_ (lua_State *L, void *block, size_t osize, size_t nsize) {
|
void *luaM_realloc_ (lua_State *L, void *block, size_t osize, size_t nsize) {
|
||||||
void *newblock;
|
void *newblock;
|
||||||
global_State *g = G(L);
|
global_State *g = G(L);
|
||||||
lua_assert((osize == 0) == (block == NULL));
|
size_t realosize = (block) ? osize : 0;
|
||||||
newblock = firsttry(g, block, osize, nsize);
|
lua_assert((realosize == 0) == (block == NULL));
|
||||||
if (l_unlikely(newblock == NULL && nsize > 0)) {
|
#if defined(HARDMEMTESTS)
|
||||||
newblock = tryagain(L, block, osize, nsize);
|
if (nsize > realosize && g->gcrunning)
|
||||||
if (newblock == NULL) /* still no memory? */
|
luaC_fullgc(L, 1); /* force a GC whenever possible */
|
||||||
return NULL; /* do not update 'GCdebt' */
|
#endif
|
||||||
|
newblock = (*g->frealloc)(g->ud, block, osize, nsize);
|
||||||
|
if (newblock == NULL && nsize > 0) {
|
||||||
|
lua_assert(nsize > realosize); /* cannot fail when shrinking a block */
|
||||||
|
if (g->version) { /* is state fully built? */
|
||||||
|
luaC_fullgc(L, 1); /* try to free some memory... */
|
||||||
|
newblock = (*g->frealloc)(g->ud, block, osize, nsize); /* try again */
|
||||||
|
}
|
||||||
|
if (newblock == NULL)
|
||||||
|
luaD_throw(L, LUA_ERRMEM);
|
||||||
}
|
}
|
||||||
lua_assert((nsize == 0) == (newblock == NULL));
|
lua_assert((nsize == 0) == (newblock == NULL));
|
||||||
g->GCdebt -= cast(l_mem, nsize) - cast(l_mem, osize);
|
g->GCdebt = (g->GCdebt + nsize) - realosize;
|
||||||
return newblock;
|
return newblock;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
void *luaM_saferealloc_ (lua_State *L, void *block, size_t osize,
|
|
||||||
size_t nsize) {
|
|
||||||
void *newblock = luaM_realloc_(L, block, osize, nsize);
|
|
||||||
if (l_unlikely(newblock == NULL && nsize > 0)) /* allocation failed? */
|
|
||||||
luaM_error(L);
|
|
||||||
return newblock;
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
void *luaM_malloc_ (lua_State *L, size_t size, int tag) {
|
|
||||||
if (size == 0)
|
|
||||||
return NULL; /* that's all */
|
|
||||||
else {
|
|
||||||
global_State *g = G(L);
|
|
||||||
void *newblock = firsttry(g, NULL, cast_sizet(tag), size);
|
|
||||||
if (l_unlikely(newblock == NULL)) {
|
|
||||||
newblock = tryagain(L, NULL, cast_sizet(tag), size);
|
|
||||||
if (newblock == NULL)
|
|
||||||
luaM_error(L);
|
|
||||||
}
|
|
||||||
g->GCdebt -= cast(l_mem, size);
|
|
||||||
return newblock;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|||||||
@@ -1,5 +1,5 @@
|
|||||||
/*
|
/*
|
||||||
** $Id: lmem.h $
|
** $Id: lmem.h,v 1.43.1.1 2017/04/19 17:20:42 roberto Exp $
|
||||||
** Interface to Memory Manager
|
** Interface to Memory Manager
|
||||||
** See Copyright Notice in lua.h
|
** See Copyright Notice in lua.h
|
||||||
*/
|
*/
|
||||||
@@ -14,13 +14,12 @@
|
|||||||
#include "lua.h"
|
#include "lua.h"
|
||||||
|
|
||||||
|
|
||||||
#define luaM_error(L) luaD_throw(L, LUA_ERRMEM)
|
|
||||||
|
|
||||||
|
|
||||||
/*
|
/*
|
||||||
** This macro tests whether it is safe to multiply 'n' by the size of
|
** This macro reallocs a vector 'b' from 'on' to 'n' elements, where
|
||||||
** type 't' without overflows. Because 'e' is always constant, it avoids
|
** each element has size 'e'. In case of arithmetic overflow of the
|
||||||
** the runtime division MAX_SIZET/(e).
|
** product 'n'*'e', it raises an error (calling 'luaM_toobig'). Because
|
||||||
|
** 'e' is always constant, it avoids the runtime division MAX_SIZET/(e).
|
||||||
|
**
|
||||||
** (The macro is somewhat complex to avoid warnings: The 'sizeof'
|
** (The macro is somewhat complex to avoid warnings: The 'sizeof'
|
||||||
** comparison avoids a runtime comparison when overflow cannot occur.
|
** comparison avoids a runtime comparison when overflow cannot occur.
|
||||||
** The compiler should be able to optimize the real test by itself, but
|
** The compiler should be able to optimize the real test by itself, but
|
||||||
@@ -28,69 +27,43 @@
|
|||||||
** false due to limited range of data type"; the +1 tricks the compiler,
|
** false due to limited range of data type"; the +1 tricks the compiler,
|
||||||
** avoiding this warning but also this optimization.)
|
** avoiding this warning but also this optimization.)
|
||||||
*/
|
*/
|
||||||
#define luaM_testsize(n,e) \
|
#define luaM_reallocv(L,b,on,n,e) \
|
||||||
(sizeof(n) >= sizeof(size_t) && cast_sizet((n)) + 1 > MAX_SIZET/(e))
|
(((sizeof(n) >= sizeof(size_t) && cast(size_t, (n)) + 1 > MAX_SIZET/(e)) \
|
||||||
|
? luaM_toobig(L) : cast_void(0)) , \
|
||||||
#define luaM_checksize(L,n,e) \
|
luaM_realloc_(L, (b), (on)*(e), (n)*(e)))
|
||||||
(luaM_testsize(n,e) ? luaM_toobig(L) : cast_void(0))
|
|
||||||
|
|
||||||
|
|
||||||
/*
|
|
||||||
** Computes the minimum between 'n' and 'MAX_SIZET/sizeof(t)', so that
|
|
||||||
** the result is not larger than 'n' and cannot overflow a 'size_t'
|
|
||||||
** when multiplied by the size of type 't'. (Assumes that 'n' is an
|
|
||||||
** 'int' and that 'int' is not larger than 'size_t'.)
|
|
||||||
*/
|
|
||||||
#define luaM_limitN(n,t) \
|
|
||||||
((cast_sizet(n) <= MAX_SIZET/sizeof(t)) ? (n) : \
|
|
||||||
cast_int((MAX_SIZET/sizeof(t))))
|
|
||||||
|
|
||||||
|
|
||||||
/*
|
/*
|
||||||
** Arrays of chars do not need any test
|
** Arrays of chars do not need any test
|
||||||
*/
|
*/
|
||||||
#define luaM_reallocvchar(L,b,on,n) \
|
#define luaM_reallocvchar(L,b,on,n) \
|
||||||
cast_charp(luaM_saferealloc_(L, (b), (on)*sizeof(char), (n)*sizeof(char)))
|
cast(char *, luaM_realloc_(L, (b), (on)*sizeof(char), (n)*sizeof(char)))
|
||||||
|
|
||||||
#define luaM_freemem(L, b, s) luaM_free_(L, (b), (s))
|
#define luaM_freemem(L, b, s) luaM_realloc_(L, (b), (s), 0)
|
||||||
#define luaM_free(L, b) luaM_free_(L, (b), sizeof(*(b)))
|
#define luaM_free(L, b) luaM_realloc_(L, (b), sizeof(*(b)), 0)
|
||||||
#define luaM_freearray(L, b, n) luaM_free_(L, (b), (n)*sizeof(*(b)))
|
#define luaM_freearray(L, b, n) luaM_realloc_(L, (b), (n)*sizeof(*(b)), 0)
|
||||||
|
|
||||||
#define luaM_new(L,t) cast(t*, luaM_malloc_(L, sizeof(t), 0))
|
#define luaM_malloc(L,s) luaM_realloc_(L, NULL, 0, (s))
|
||||||
#define luaM_newvector(L,n,t) \
|
#define luaM_new(L,t) cast(t *, luaM_malloc(L, sizeof(t)))
|
||||||
cast(t*, luaM_malloc_(L, cast_sizet(n)*sizeof(t), 0))
|
#define luaM_newvector(L,n,t) \
|
||||||
#define luaM_newvectorchecked(L,n,t) \
|
cast(t *, luaM_reallocv(L, NULL, 0, n, sizeof(t)))
|
||||||
(luaM_checksize(L,n,sizeof(t)), luaM_newvector(L,n,t))
|
|
||||||
|
|
||||||
#define luaM_newobject(L,tag,s) luaM_malloc_(L, (s), tag)
|
#define luaM_newobject(L,tag,s) luaM_realloc_(L, NULL, tag, (s))
|
||||||
|
|
||||||
#define luaM_newblock(L, size) luaM_newvector(L, size, char)
|
|
||||||
|
|
||||||
#define luaM_growvector(L,v,nelems,size,t,limit,e) \
|
#define luaM_growvector(L,v,nelems,size,t,limit,e) \
|
||||||
((v)=cast(t *, luaM_growaux_(L,v,nelems,&(size),sizeof(t), \
|
if ((nelems)+1 > (size)) \
|
||||||
luaM_limitN(limit,t),e)))
|
((v)=cast(t *, luaM_growaux_(L,v,&(size),sizeof(t),limit,e)))
|
||||||
|
|
||||||
#define luaM_reallocvector(L, v,oldn,n,t) \
|
#define luaM_reallocvector(L, v,oldn,n,t) \
|
||||||
(cast(t *, luaM_realloc_(L, v, cast_sizet(oldn) * sizeof(t), \
|
((v)=cast(t *, luaM_reallocv(L, v, oldn, n, sizeof(t))))
|
||||||
cast_sizet(n) * sizeof(t))))
|
|
||||||
|
|
||||||
#define luaM_shrinkvector(L,v,size,fs,t) \
|
|
||||||
((v)=cast(t *, luaM_shrinkvector_(L, v, &(size), fs, sizeof(t))))
|
|
||||||
|
|
||||||
LUAI_FUNC l_noret luaM_toobig (lua_State *L);
|
LUAI_FUNC l_noret luaM_toobig (lua_State *L);
|
||||||
|
|
||||||
/* not to be called directly */
|
/* not to be called directly */
|
||||||
LUAI_FUNC void *luaM_realloc_ (lua_State *L, void *block, size_t oldsize,
|
LUAI_FUNC void *luaM_realloc_ (lua_State *L, void *block, size_t oldsize,
|
||||||
size_t size);
|
size_t size);
|
||||||
LUAI_FUNC void *luaM_saferealloc_ (lua_State *L, void *block, size_t oldsize,
|
LUAI_FUNC void *luaM_growaux_ (lua_State *L, void *block, int *size,
|
||||||
size_t size);
|
size_t size_elem, int limit,
|
||||||
LUAI_FUNC void luaM_free_ (lua_State *L, void *block, size_t osize);
|
|
||||||
LUAI_FUNC void *luaM_growaux_ (lua_State *L, void *block, int nelems,
|
|
||||||
int *size, unsigned size_elem, int limit,
|
|
||||||
const char *what);
|
const char *what);
|
||||||
LUAI_FUNC void *luaM_shrinkvector_ (lua_State *L, void *block, int *nelem,
|
|
||||||
int final_n, unsigned size_elem);
|
|
||||||
LUAI_FUNC void *luaM_malloc_ (lua_State *L, size_t size, int tag);
|
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
|||||||
@@ -1,5 +1,5 @@
|
|||||||
/*
|
/*
|
||||||
** $Id: loadlib.c $
|
** $Id: loadlib.c,v 1.130.1.1 2017/04/19 17:20:42 roberto Exp $
|
||||||
** Dynamic library loader for Lua
|
** Dynamic library loader for Lua
|
||||||
** See Copyright Notice in lua.h
|
** See Copyright Notice in lua.h
|
||||||
**
|
**
|
||||||
@@ -22,7 +22,15 @@
|
|||||||
|
|
||||||
#include "lauxlib.h"
|
#include "lauxlib.h"
|
||||||
#include "lualib.h"
|
#include "lualib.h"
|
||||||
#include "llimits.h"
|
|
||||||
|
|
||||||
|
/*
|
||||||
|
** LUA_IGMARK is a mark to ignore all before it when building the
|
||||||
|
** luaopen_ function name.
|
||||||
|
*/
|
||||||
|
#if !defined (LUA_IGMARK)
|
||||||
|
#define LUA_IGMARK "-"
|
||||||
|
#endif
|
||||||
|
|
||||||
|
|
||||||
/*
|
/*
|
||||||
@@ -48,10 +56,10 @@
|
|||||||
|
|
||||||
|
|
||||||
/*
|
/*
|
||||||
** key for table in the registry that keeps handles
|
** unique key for table in the registry that keeps handles
|
||||||
** for all loaded C libraries
|
** for all loaded C libraries
|
||||||
*/
|
*/
|
||||||
static const char *const CLIBS = "_CLIBS";
|
static const int CLIBS = 0;
|
||||||
|
|
||||||
#define LIB_FAIL "open"
|
#define LIB_FAIL "open"
|
||||||
|
|
||||||
@@ -59,10 +67,6 @@ static const char *const CLIBS = "_CLIBS";
|
|||||||
#define setprogdir(L) ((void)0)
|
#define setprogdir(L) ((void)0)
|
||||||
|
|
||||||
|
|
||||||
/* cast void* to a Lua function */
|
|
||||||
#define cast_Lfunc(p) cast(lua_CFunction, cast_func(p))
|
|
||||||
|
|
||||||
|
|
||||||
/*
|
/*
|
||||||
** system-dependent functions
|
** system-dependent functions
|
||||||
*/
|
*/
|
||||||
@@ -93,13 +97,26 @@ static lua_CFunction lsys_sym (lua_State *L, void *lib, const char *sym);
|
|||||||
#if defined(LUA_USE_DLOPEN) /* { */
|
#if defined(LUA_USE_DLOPEN) /* { */
|
||||||
/*
|
/*
|
||||||
** {========================================================================
|
** {========================================================================
|
||||||
** This is an implementation of loadlib based on the dlfcn interface,
|
** This is an implementation of loadlib based on the dlfcn interface.
|
||||||
** which is available in all POSIX systems.
|
** The dlfcn interface is available in Linux, SunOS, Solaris, IRIX, FreeBSD,
|
||||||
|
** NetBSD, AIX 4.2, HPUX 11, and probably most other Unix flavors, at least
|
||||||
|
** as an emulation layer on top of native functions.
|
||||||
** =========================================================================
|
** =========================================================================
|
||||||
*/
|
*/
|
||||||
|
|
||||||
#include <dlfcn.h>
|
#include <dlfcn.h>
|
||||||
|
|
||||||
|
/*
|
||||||
|
** Macro to convert pointer-to-void* to pointer-to-function. This cast
|
||||||
|
** is undefined according to ISO C, but POSIX assumes that it works.
|
||||||
|
** (The '__extension__' in gnu compilers is only to avoid warnings.)
|
||||||
|
*/
|
||||||
|
#if defined(__GNUC__)
|
||||||
|
#define cast_func(p) (__extension__ (lua_CFunction)(p))
|
||||||
|
#else
|
||||||
|
#define cast_func(p) ((lua_CFunction)(p))
|
||||||
|
#endif
|
||||||
|
|
||||||
|
|
||||||
static void lsys_unloadlib (void *lib) {
|
static void lsys_unloadlib (void *lib) {
|
||||||
dlclose(lib);
|
dlclose(lib);
|
||||||
@@ -108,16 +125,14 @@ static void lsys_unloadlib (void *lib) {
|
|||||||
|
|
||||||
static void *lsys_load (lua_State *L, const char *path, int seeglb) {
|
static void *lsys_load (lua_State *L, const char *path, int seeglb) {
|
||||||
void *lib = dlopen(path, RTLD_NOW | (seeglb ? RTLD_GLOBAL : RTLD_LOCAL));
|
void *lib = dlopen(path, RTLD_NOW | (seeglb ? RTLD_GLOBAL : RTLD_LOCAL));
|
||||||
if (l_unlikely(lib == NULL))
|
if (lib == NULL) lua_pushstring(L, dlerror());
|
||||||
lua_pushstring(L, dlerror());
|
|
||||||
return lib;
|
return lib;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
static lua_CFunction lsys_sym (lua_State *L, void *lib, const char *sym) {
|
static lua_CFunction lsys_sym (lua_State *L, void *lib, const char *sym) {
|
||||||
lua_CFunction f = cast_Lfunc(dlsym(lib, sym));
|
lua_CFunction f = cast_func(dlsym(lib, sym));
|
||||||
if (l_unlikely(f == NULL))
|
if (f == NULL) lua_pushstring(L, dlerror());
|
||||||
lua_pushstring(L, dlerror());
|
|
||||||
return f;
|
return f;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -191,7 +206,7 @@ static void *lsys_load (lua_State *L, const char *path, int seeglb) {
|
|||||||
|
|
||||||
|
|
||||||
static lua_CFunction lsys_sym (lua_State *L, void *lib, const char *sym) {
|
static lua_CFunction lsys_sym (lua_State *L, void *lib, const char *sym) {
|
||||||
lua_CFunction f = cast_Lfunc(GetProcAddress((HMODULE)lib, sym));
|
lua_CFunction f = (lua_CFunction)GetProcAddress((HMODULE)lib, sym);
|
||||||
if (f == NULL) pusherror(L);
|
if (f == NULL) pusherror(L);
|
||||||
return f;
|
return f;
|
||||||
}
|
}
|
||||||
@@ -254,6 +269,8 @@ static lua_CFunction lsys_sym (lua_State *L, void *lib, const char *sym) {
|
|||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
|
||||||
|
#define AUXMARK "\1" /* auxiliary mark */
|
||||||
|
|
||||||
|
|
||||||
/*
|
/*
|
||||||
** return registry.LUA_NOENV as a boolean
|
** return registry.LUA_NOENV as a boolean
|
||||||
@@ -268,60 +285,38 @@ static int noenv (lua_State *L) {
|
|||||||
|
|
||||||
|
|
||||||
/*
|
/*
|
||||||
** Set a path. (If using the default path, assume it is a string
|
** Set a path
|
||||||
** literal in C and create it as an external string.)
|
|
||||||
*/
|
*/
|
||||||
static void setpath (lua_State *L, const char *fieldname,
|
static void setpath (lua_State *L, const char *fieldname,
|
||||||
const char *envname,
|
const char *envname,
|
||||||
const char *dft) {
|
const char *dft) {
|
||||||
const char *dftmark;
|
|
||||||
const char *nver = lua_pushfstring(L, "%s%s", envname, LUA_VERSUFFIX);
|
const char *nver = lua_pushfstring(L, "%s%s", envname, LUA_VERSUFFIX);
|
||||||
const char *path = getenv(nver); /* try versioned name */
|
const char *path = getenv(nver); /* use versioned name */
|
||||||
if (path == NULL) /* no versioned environment variable? */
|
if (path == NULL) /* no environment variable? */
|
||||||
path = getenv(envname); /* try unversioned name */
|
path = getenv(envname); /* try unversioned name */
|
||||||
if (path == NULL || noenv(L)) /* no environment variable? */
|
if (path == NULL || noenv(L)) /* no environment variable? */
|
||||||
lua_pushexternalstring(L, dft, strlen(dft), NULL, NULL); /* use default */
|
lua_pushstring(L, dft); /* use default */
|
||||||
else if ((dftmark = strstr(path, LUA_PATH_SEP LUA_PATH_SEP)) == NULL)
|
else {
|
||||||
lua_pushstring(L, path); /* nothing to change */
|
/* replace ";;" by ";AUXMARK;" and then AUXMARK by default path */
|
||||||
else { /* path contains a ";;": insert default path in its place */
|
path = luaL_gsub(L, path, LUA_PATH_SEP LUA_PATH_SEP,
|
||||||
size_t len = strlen(path);
|
LUA_PATH_SEP AUXMARK LUA_PATH_SEP);
|
||||||
luaL_Buffer b;
|
luaL_gsub(L, path, AUXMARK, dft);
|
||||||
luaL_buffinit(L, &b);
|
lua_remove(L, -2); /* remove result from 1st 'gsub' */
|
||||||
if (path < dftmark) { /* is there a prefix before ';;'? */
|
|
||||||
luaL_addlstring(&b, path, ct_diff2sz(dftmark - path)); /* add it */
|
|
||||||
luaL_addchar(&b, *LUA_PATH_SEP);
|
|
||||||
}
|
|
||||||
luaL_addstring(&b, dft); /* add default */
|
|
||||||
if (dftmark < path + len - 2) { /* is there a suffix after ';;'? */
|
|
||||||
luaL_addchar(&b, *LUA_PATH_SEP);
|
|
||||||
luaL_addlstring(&b, dftmark + 2, ct_diff2sz((path + len - 2) - dftmark));
|
|
||||||
}
|
|
||||||
luaL_pushresult(&b);
|
|
||||||
}
|
}
|
||||||
setprogdir(L);
|
setprogdir(L);
|
||||||
lua_setfield(L, -3, fieldname); /* package[fieldname] = path value */
|
lua_setfield(L, -3, fieldname); /* package[fieldname] = path value */
|
||||||
lua_pop(L, 1); /* pop versioned variable name ('nver') */
|
lua_pop(L, 1); /* pop versioned variable name */
|
||||||
}
|
}
|
||||||
|
|
||||||
/* }================================================================== */
|
/* }================================================================== */
|
||||||
|
|
||||||
|
|
||||||
/*
|
|
||||||
** External strings created by DLLs may need the DLL code to be
|
|
||||||
** deallocated. This implies that a DLL can only be unloaded after all
|
|
||||||
** its strings were deallocated. To ensure that, we create a 'library
|
|
||||||
** string' to represent each DLL, and when this string is deallocated
|
|
||||||
** it closes its corresponding DLL.
|
|
||||||
** (The string itself is irrelevant; its userdata is the DLL pointer.)
|
|
||||||
*/
|
|
||||||
|
|
||||||
|
|
||||||
/*
|
/*
|
||||||
** return registry.CLIBS[path]
|
** return registry.CLIBS[path]
|
||||||
*/
|
*/
|
||||||
static void *checkclib (lua_State *L, const char *path) {
|
static void *checkclib (lua_State *L, const char *path) {
|
||||||
void *plib;
|
void *plib;
|
||||||
lua_getfield(L, LUA_REGISTRYINDEX, CLIBS);
|
lua_rawgetp(L, LUA_REGISTRYINDEX, &CLIBS);
|
||||||
lua_getfield(L, -1, path);
|
lua_getfield(L, -1, path);
|
||||||
plib = lua_touserdata(L, -1); /* plib = CLIBS[path] */
|
plib = lua_touserdata(L, -1); /* plib = CLIBS[path] */
|
||||||
lua_pop(L, 2); /* pop CLIBS table and 'plib' */
|
lua_pop(L, 2); /* pop CLIBS table and 'plib' */
|
||||||
@@ -330,42 +325,35 @@ static void *checkclib (lua_State *L, const char *path) {
|
|||||||
|
|
||||||
|
|
||||||
/*
|
/*
|
||||||
** Deallocate function for library strings.
|
** registry.CLIBS[path] = plib -- for queries
|
||||||
** Unload the DLL associated with the string being deallocated.
|
** registry.CLIBS[#CLIBS + 1] = plib -- also keep a list of all libraries
|
||||||
*/
|
|
||||||
static void *freelib (void *ud, void *ptr, size_t osize, size_t nsize) {
|
|
||||||
/* string itself is irrelevant and static */
|
|
||||||
(void)ptr; (void)osize; (void)nsize;
|
|
||||||
lsys_unloadlib(ud); /* unload library represented by the string */
|
|
||||||
return NULL;
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
/*
|
|
||||||
** Create a library string that, when deallocated, will unload 'plib'
|
|
||||||
*/
|
|
||||||
static void createlibstr (lua_State *L, void *plib) {
|
|
||||||
/* common content for all library strings */
|
|
||||||
static const char dummy[] = "01234567890";
|
|
||||||
lua_pushexternalstring(L, dummy, sizeof(dummy) - 1, freelib, plib);
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
/*
|
|
||||||
** registry.CLIBS[path] = plib -- for queries.
|
|
||||||
** Also create a reference to strlib, so that the library string will
|
|
||||||
** only be collected when registry.CLIBS is collected.
|
|
||||||
*/
|
*/
|
||||||
static void addtoclib (lua_State *L, const char *path, void *plib) {
|
static void addtoclib (lua_State *L, const char *path, void *plib) {
|
||||||
lua_getfield(L, LUA_REGISTRYINDEX, CLIBS);
|
lua_rawgetp(L, LUA_REGISTRYINDEX, &CLIBS);
|
||||||
lua_pushlightuserdata(L, plib);
|
lua_pushlightuserdata(L, plib);
|
||||||
lua_setfield(L, -2, path); /* CLIBS[path] = plib */
|
lua_pushvalue(L, -1);
|
||||||
createlibstr(L, plib);
|
lua_setfield(L, -3, path); /* CLIBS[path] = plib */
|
||||||
luaL_ref(L, -2); /* keep library string in CLIBS */
|
lua_rawseti(L, -2, luaL_len(L, -2) + 1); /* CLIBS[#CLIBS + 1] = plib */
|
||||||
lua_pop(L, 1); /* pop CLIBS table */
|
lua_pop(L, 1); /* pop CLIBS table */
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/*
|
||||||
|
** __gc tag method for CLIBS table: calls 'lsys_unloadlib' for all lib
|
||||||
|
** handles in list CLIBS
|
||||||
|
*/
|
||||||
|
static int gctm (lua_State *L) {
|
||||||
|
lua_Integer n = luaL_len(L, 1);
|
||||||
|
for (; n >= 1; n--) { /* for each handle, in reverse order */
|
||||||
|
lua_rawgeti(L, 1, n); /* get handle CLIBS[n] */
|
||||||
|
lsys_unloadlib(lua_touserdata(L, -1));
|
||||||
|
lua_pop(L, 1); /* pop handle */
|
||||||
|
}
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
/* error codes for 'lookforfunc' */
|
/* error codes for 'lookforfunc' */
|
||||||
#define ERRLIB 1
|
#define ERRLIB 1
|
||||||
#define ERRFUNC 2
|
#define ERRFUNC 2
|
||||||
@@ -378,8 +366,8 @@ static void addtoclib (lua_State *L, const char *path, void *plib) {
|
|||||||
** Then, if 'sym' is '*', return true (as library has been loaded).
|
** Then, if 'sym' is '*', return true (as library has been loaded).
|
||||||
** Otherwise, look for symbol 'sym' in the library and push a
|
** Otherwise, look for symbol 'sym' in the library and push a
|
||||||
** C function with that symbol.
|
** C function with that symbol.
|
||||||
** Return 0 with 'true' or a function in the stack; in case of
|
** Return 0 and 'true' or a function in the stack; in case of
|
||||||
** errors, return an error code with an error message in the stack.
|
** errors, return an error code and an error message in the stack.
|
||||||
*/
|
*/
|
||||||
static int lookforfunc (lua_State *L, const char *path, const char *sym) {
|
static int lookforfunc (lua_State *L, const char *path, const char *sym) {
|
||||||
void *reg = checkclib(L, path); /* check loaded C libraries */
|
void *reg = checkclib(L, path); /* check loaded C libraries */
|
||||||
@@ -406,13 +394,13 @@ static int ll_loadlib (lua_State *L) {
|
|||||||
const char *path = luaL_checkstring(L, 1);
|
const char *path = luaL_checkstring(L, 1);
|
||||||
const char *init = luaL_checkstring(L, 2);
|
const char *init = luaL_checkstring(L, 2);
|
||||||
int stat = lookforfunc(L, path, init);
|
int stat = lookforfunc(L, path, init);
|
||||||
if (l_likely(stat == 0)) /* no errors? */
|
if (stat == 0) /* no errors? */
|
||||||
return 1; /* return the loaded function */
|
return 1; /* return the loaded function */
|
||||||
else { /* error; error message is on stack top */
|
else { /* error; error message is on stack top */
|
||||||
luaL_pushfail(L);
|
lua_pushnil(L);
|
||||||
lua_insert(L, -2);
|
lua_insert(L, -2);
|
||||||
lua_pushstring(L, (stat == ERRLIB) ? LIB_FAIL : "init");
|
lua_pushstring(L, (stat == ERRLIB) ? LIB_FAIL : "init");
|
||||||
return 3; /* return fail, error message, and where */
|
return 3; /* return nil, error message, and where */
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -433,42 +421,14 @@ static int readable (const char *filename) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/*
|
static const char *pushnexttemplate (lua_State *L, const char *path) {
|
||||||
** Get the next name in '*path' = 'name1;name2;name3;...', changing
|
const char *l;
|
||||||
** the ending ';' to '\0' to create a zero-terminated string. Return
|
while (*path == *LUA_PATH_SEP) path++; /* skip separators */
|
||||||
** NULL when list ends.
|
if (*path == '\0') return NULL; /* no more templates */
|
||||||
*/
|
l = strchr(path, *LUA_PATH_SEP); /* find next separator */
|
||||||
static const char *getnextfilename (char **path, char *end) {
|
if (l == NULL) l = path + strlen(path);
|
||||||
char *sep;
|
lua_pushlstring(L, path, l - path); /* template */
|
||||||
char *name = *path;
|
return l;
|
||||||
if (name == end)
|
|
||||||
return NULL; /* no more names */
|
|
||||||
else if (*name == '\0') { /* from previous iteration? */
|
|
||||||
*name = *LUA_PATH_SEP; /* restore separator */
|
|
||||||
name++; /* skip it */
|
|
||||||
}
|
|
||||||
sep = strchr(name, *LUA_PATH_SEP); /* find next separator */
|
|
||||||
if (sep == NULL) /* separator not found? */
|
|
||||||
sep = end; /* name goes until the end */
|
|
||||||
*sep = '\0'; /* finish file name */
|
|
||||||
*path = sep; /* will start next search from here */
|
|
||||||
return name;
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
/*
|
|
||||||
** Given a path such as ";blabla.so;blublu.so", pushes the string
|
|
||||||
**
|
|
||||||
** no file 'blabla.so'
|
|
||||||
** no file 'blublu.so'
|
|
||||||
*/
|
|
||||||
static void pusherrornotfound (lua_State *L, const char *path) {
|
|
||||||
luaL_Buffer b;
|
|
||||||
luaL_buffinit(L, &b);
|
|
||||||
luaL_addstring(&b, "no file '");
|
|
||||||
luaL_addgsub(&b, path, LUA_PATH_SEP, "'\n\tno file '");
|
|
||||||
luaL_addstring(&b, "'");
|
|
||||||
luaL_pushresult(&b);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@@ -476,25 +436,21 @@ static const char *searchpath (lua_State *L, const char *name,
|
|||||||
const char *path,
|
const char *path,
|
||||||
const char *sep,
|
const char *sep,
|
||||||
const char *dirsep) {
|
const char *dirsep) {
|
||||||
luaL_Buffer buff;
|
luaL_Buffer msg; /* to build error message */
|
||||||
char *pathname; /* path with name inserted */
|
luaL_buffinit(L, &msg);
|
||||||
char *endpathname; /* its end */
|
if (*sep != '\0') /* non-empty separator? */
|
||||||
const char *filename;
|
|
||||||
/* separator is non-empty and appears in 'name'? */
|
|
||||||
if (*sep != '\0' && strchr(name, *sep) != NULL)
|
|
||||||
name = luaL_gsub(L, name, sep, dirsep); /* replace it by 'dirsep' */
|
name = luaL_gsub(L, name, sep, dirsep); /* replace it by 'dirsep' */
|
||||||
luaL_buffinit(L, &buff);
|
while ((path = pushnexttemplate(L, path)) != NULL) {
|
||||||
/* add path to the buffer, replacing marks ('?') with the file name */
|
const char *filename = luaL_gsub(L, lua_tostring(L, -1),
|
||||||
luaL_addgsub(&buff, path, LUA_PATH_MARK, name);
|
LUA_PATH_MARK, name);
|
||||||
luaL_addchar(&buff, '\0');
|
lua_remove(L, -2); /* remove path template */
|
||||||
pathname = luaL_buffaddr(&buff); /* writable list of file names */
|
|
||||||
endpathname = pathname + luaL_bufflen(&buff) - 1;
|
|
||||||
while ((filename = getnextfilename(&pathname, endpathname)) != NULL) {
|
|
||||||
if (readable(filename)) /* does file exist and is readable? */
|
if (readable(filename)) /* does file exist and is readable? */
|
||||||
return lua_pushstring(L, filename); /* save and return name */
|
return filename; /* return that file name */
|
||||||
|
lua_pushfstring(L, "\n\tno file '%s'", filename);
|
||||||
|
lua_remove(L, -2); /* remove file name */
|
||||||
|
luaL_addvalue(&msg); /* concatenate error msg. entry */
|
||||||
}
|
}
|
||||||
luaL_pushresult(&buff); /* push path to create error message */
|
luaL_pushresult(&msg); /* create error message */
|
||||||
pusherrornotfound(L, lua_tostring(L, -1)); /* create error message */
|
|
||||||
return NULL; /* not found */
|
return NULL; /* not found */
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -506,9 +462,9 @@ static int ll_searchpath (lua_State *L) {
|
|||||||
luaL_optstring(L, 4, LUA_DIRSEP));
|
luaL_optstring(L, 4, LUA_DIRSEP));
|
||||||
if (f != NULL) return 1;
|
if (f != NULL) return 1;
|
||||||
else { /* error message is on top of the stack */
|
else { /* error message is on top of the stack */
|
||||||
luaL_pushfail(L);
|
lua_pushnil(L);
|
||||||
lua_insert(L, -2);
|
lua_insert(L, -2);
|
||||||
return 2; /* return fail + error message */
|
return 2; /* return nil + error message */
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -519,14 +475,14 @@ static const char *findfile (lua_State *L, const char *name,
|
|||||||
const char *path;
|
const char *path;
|
||||||
lua_getfield(L, lua_upvalueindex(1), pname);
|
lua_getfield(L, lua_upvalueindex(1), pname);
|
||||||
path = lua_tostring(L, -1);
|
path = lua_tostring(L, -1);
|
||||||
if (l_unlikely(path == NULL))
|
if (path == NULL)
|
||||||
luaL_error(L, "'package.%s' must be a string", pname);
|
luaL_error(L, "'package.%s' must be a string", pname);
|
||||||
return searchpath(L, name, path, ".", dirsep);
|
return searchpath(L, name, path, ".", dirsep);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
static int checkload (lua_State *L, int stat, const char *filename) {
|
static int checkload (lua_State *L, int stat, const char *filename) {
|
||||||
if (l_likely(stat)) { /* module loaded successfully? */
|
if (stat) { /* module loaded successfully? */
|
||||||
lua_pushstring(L, filename); /* will be 2nd argument to module */
|
lua_pushstring(L, filename); /* will be 2nd argument to module */
|
||||||
return 2; /* return open function and file name */
|
return 2; /* return open function and file name */
|
||||||
}
|
}
|
||||||
@@ -560,7 +516,7 @@ static int loadfunc (lua_State *L, const char *filename, const char *modname) {
|
|||||||
mark = strchr(modname, *LUA_IGMARK);
|
mark = strchr(modname, *LUA_IGMARK);
|
||||||
if (mark) {
|
if (mark) {
|
||||||
int stat;
|
int stat;
|
||||||
openfunc = lua_pushlstring(L, modname, ct_diff2sz(mark - modname));
|
openfunc = lua_pushlstring(L, modname, mark - modname);
|
||||||
openfunc = lua_pushfstring(L, LUA_POF"%s", openfunc);
|
openfunc = lua_pushfstring(L, LUA_POF"%s", openfunc);
|
||||||
stat = lookforfunc(L, filename, openfunc);
|
stat = lookforfunc(L, filename, openfunc);
|
||||||
if (stat != ERRFUNC) return stat;
|
if (stat != ERRFUNC) return stat;
|
||||||
@@ -585,14 +541,14 @@ static int searcher_Croot (lua_State *L) {
|
|||||||
const char *p = strchr(name, '.');
|
const char *p = strchr(name, '.');
|
||||||
int stat;
|
int stat;
|
||||||
if (p == NULL) return 0; /* is root */
|
if (p == NULL) return 0; /* is root */
|
||||||
lua_pushlstring(L, name, ct_diff2sz(p - name));
|
lua_pushlstring(L, name, p - name);
|
||||||
filename = findfile(L, lua_tostring(L, -1), "cpath", LUA_CSUBSEP);
|
filename = findfile(L, lua_tostring(L, -1), "cpath", LUA_CSUBSEP);
|
||||||
if (filename == NULL) return 1; /* root not found */
|
if (filename == NULL) return 1; /* root not found */
|
||||||
if ((stat = loadfunc(L, filename, name)) != 0) {
|
if ((stat = loadfunc(L, filename, name)) != 0) {
|
||||||
if (stat != ERRFUNC)
|
if (stat != ERRFUNC)
|
||||||
return checkload(L, 0, filename); /* real error */
|
return checkload(L, 0, filename); /* real error */
|
||||||
else { /* open function not found */
|
else { /* open function not found */
|
||||||
lua_pushfstring(L, "no module '%s' in file '%s'", name, filename);
|
lua_pushfstring(L, "\n\tno module '%s' in file '%s'", name, filename);
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -604,31 +560,23 @@ static int searcher_Croot (lua_State *L) {
|
|||||||
static int searcher_preload (lua_State *L) {
|
static int searcher_preload (lua_State *L) {
|
||||||
const char *name = luaL_checkstring(L, 1);
|
const char *name = luaL_checkstring(L, 1);
|
||||||
lua_getfield(L, LUA_REGISTRYINDEX, LUA_PRELOAD_TABLE);
|
lua_getfield(L, LUA_REGISTRYINDEX, LUA_PRELOAD_TABLE);
|
||||||
if (lua_getfield(L, -1, name) == LUA_TNIL) { /* not found? */
|
if (lua_getfield(L, -1, name) == LUA_TNIL) /* not found? */
|
||||||
lua_pushfstring(L, "no field package.preload['%s']", name);
|
lua_pushfstring(L, "\n\tno field package.preload['%s']", name);
|
||||||
return 1;
|
return 1;
|
||||||
}
|
|
||||||
else {
|
|
||||||
lua_pushliteral(L, ":preload:");
|
|
||||||
return 2;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
static void findloader (lua_State *L, const char *name) {
|
static void findloader (lua_State *L, const char *name) {
|
||||||
int i;
|
int i;
|
||||||
luaL_Buffer msg; /* to build error message */
|
luaL_Buffer msg; /* to build error message */
|
||||||
/* push 'package.searchers' to index 3 in the stack */
|
|
||||||
if (l_unlikely(lua_getfield(L, lua_upvalueindex(1), "searchers")
|
|
||||||
!= LUA_TTABLE))
|
|
||||||
luaL_error(L, "'package.searchers' must be a table");
|
|
||||||
luaL_buffinit(L, &msg);
|
luaL_buffinit(L, &msg);
|
||||||
luaL_addstring(&msg, "\n\t"); /* error-message prefix for first message */
|
/* push 'package.searchers' to index 3 in the stack */
|
||||||
|
if (lua_getfield(L, lua_upvalueindex(1), "searchers") != LUA_TTABLE)
|
||||||
|
luaL_error(L, "'package.searchers' must be a table");
|
||||||
/* iterate over available searchers to find a loader */
|
/* iterate over available searchers to find a loader */
|
||||||
for (i = 1; ; i++) {
|
for (i = 1; ; i++) {
|
||||||
if (l_unlikely(lua_rawgeti(L, 3, i) == LUA_TNIL)) { /* no more searchers? */
|
if (lua_rawgeti(L, 3, i) == LUA_TNIL) { /* no more searchers? */
|
||||||
lua_pop(L, 1); /* remove nil */
|
lua_pop(L, 1); /* remove nil */
|
||||||
luaL_buffsub(&msg, 2); /* remove last prefix */
|
|
||||||
luaL_pushresult(&msg); /* create error message */
|
luaL_pushresult(&msg); /* create error message */
|
||||||
luaL_error(L, "module '%s' not found:%s", name, lua_tostring(L, -1));
|
luaL_error(L, "module '%s' not found:%s", name, lua_tostring(L, -1));
|
||||||
}
|
}
|
||||||
@@ -639,9 +587,8 @@ static void findloader (lua_State *L, const char *name) {
|
|||||||
else if (lua_isstring(L, -2)) { /* searcher returned error message? */
|
else if (lua_isstring(L, -2)) { /* searcher returned error message? */
|
||||||
lua_pop(L, 1); /* remove extra return */
|
lua_pop(L, 1); /* remove extra return */
|
||||||
luaL_addvalue(&msg); /* concatenate error message */
|
luaL_addvalue(&msg); /* concatenate error message */
|
||||||
luaL_addstring(&msg, "\n\t"); /* prefix for next message */
|
|
||||||
}
|
}
|
||||||
else /* no error message */
|
else
|
||||||
lua_pop(L, 2); /* remove both returns */
|
lua_pop(L, 2); /* remove both returns */
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -657,33 +604,113 @@ static int ll_require (lua_State *L) {
|
|||||||
/* else must load package */
|
/* else must load package */
|
||||||
lua_pop(L, 1); /* remove 'getfield' result */
|
lua_pop(L, 1); /* remove 'getfield' result */
|
||||||
findloader(L, name);
|
findloader(L, name);
|
||||||
lua_rotate(L, -2, 1); /* function <-> loader data */
|
lua_pushstring(L, name); /* pass name as argument to module loader */
|
||||||
lua_pushvalue(L, 1); /* name is 1st argument to module loader */
|
lua_insert(L, -2); /* name is 1st argument (before search data) */
|
||||||
lua_pushvalue(L, -3); /* loader data is 2nd argument */
|
|
||||||
/* stack: ...; loader data; loader function; mod. name; loader data */
|
|
||||||
lua_call(L, 2, 1); /* run loader to load module */
|
lua_call(L, 2, 1); /* run loader to load module */
|
||||||
/* stack: ...; loader data; result from loader */
|
|
||||||
if (!lua_isnil(L, -1)) /* non-nil return? */
|
if (!lua_isnil(L, -1)) /* non-nil return? */
|
||||||
lua_setfield(L, 2, name); /* LOADED[name] = returned value */
|
lua_setfield(L, 2, name); /* LOADED[name] = returned value */
|
||||||
else
|
|
||||||
lua_pop(L, 1); /* pop nil */
|
|
||||||
if (lua_getfield(L, 2, name) == LUA_TNIL) { /* module set no value? */
|
if (lua_getfield(L, 2, name) == LUA_TNIL) { /* module set no value? */
|
||||||
lua_pushboolean(L, 1); /* use true as result */
|
lua_pushboolean(L, 1); /* use true as result */
|
||||||
lua_copy(L, -1, -2); /* replace loader result */
|
lua_pushvalue(L, -1); /* extra copy to be returned */
|
||||||
lua_setfield(L, 2, name); /* LOADED[name] = true */
|
lua_setfield(L, 2, name); /* LOADED[name] = true */
|
||||||
}
|
}
|
||||||
lua_rotate(L, -2, 1); /* loader data <-> module result */
|
return 1;
|
||||||
return 2; /* return module result and loader data */
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/* }====================================================== */
|
/* }====================================================== */
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
/*
|
||||||
|
** {======================================================
|
||||||
|
** 'module' function
|
||||||
|
** =======================================================
|
||||||
|
*/
|
||||||
|
#if defined(LUA_COMPAT_MODULE)
|
||||||
|
|
||||||
|
/*
|
||||||
|
** changes the environment variable of calling function
|
||||||
|
*/
|
||||||
|
static void set_env (lua_State *L) {
|
||||||
|
lua_Debug ar;
|
||||||
|
if (lua_getstack(L, 1, &ar) == 0 ||
|
||||||
|
lua_getinfo(L, "f", &ar) == 0 || /* get calling function */
|
||||||
|
lua_iscfunction(L, -1))
|
||||||
|
luaL_error(L, "'module' not called from a Lua function");
|
||||||
|
lua_pushvalue(L, -2); /* copy new environment table to top */
|
||||||
|
lua_setupvalue(L, -2, 1);
|
||||||
|
lua_pop(L, 1); /* remove function */
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
static void dooptions (lua_State *L, int n) {
|
||||||
|
int i;
|
||||||
|
for (i = 2; i <= n; i++) {
|
||||||
|
if (lua_isfunction(L, i)) { /* avoid 'calling' extra info. */
|
||||||
|
lua_pushvalue(L, i); /* get option (a function) */
|
||||||
|
lua_pushvalue(L, -2); /* module */
|
||||||
|
lua_call(L, 1, 0);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
static void modinit (lua_State *L, const char *modname) {
|
||||||
|
const char *dot;
|
||||||
|
lua_pushvalue(L, -1);
|
||||||
|
lua_setfield(L, -2, "_M"); /* module._M = module */
|
||||||
|
lua_pushstring(L, modname);
|
||||||
|
lua_setfield(L, -2, "_NAME");
|
||||||
|
dot = strrchr(modname, '.'); /* look for last dot in module name */
|
||||||
|
if (dot == NULL) dot = modname;
|
||||||
|
else dot++;
|
||||||
|
/* set _PACKAGE as package name (full module name minus last part) */
|
||||||
|
lua_pushlstring(L, modname, dot - modname);
|
||||||
|
lua_setfield(L, -2, "_PACKAGE");
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
static int ll_module (lua_State *L) {
|
||||||
|
const char *modname = luaL_checkstring(L, 1);
|
||||||
|
int lastarg = lua_gettop(L); /* last parameter */
|
||||||
|
luaL_pushmodule(L, modname, 1); /* get/create module table */
|
||||||
|
/* check whether table already has a _NAME field */
|
||||||
|
if (lua_getfield(L, -1, "_NAME") != LUA_TNIL)
|
||||||
|
lua_pop(L, 1); /* table is an initialized module */
|
||||||
|
else { /* no; initialize it */
|
||||||
|
lua_pop(L, 1);
|
||||||
|
modinit(L, modname);
|
||||||
|
}
|
||||||
|
lua_pushvalue(L, -1);
|
||||||
|
set_env(L);
|
||||||
|
dooptions(L, lastarg);
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
static int ll_seeall (lua_State *L) {
|
||||||
|
luaL_checktype(L, 1, LUA_TTABLE);
|
||||||
|
if (!lua_getmetatable(L, 1)) {
|
||||||
|
lua_createtable(L, 0, 1); /* create new metatable */
|
||||||
|
lua_pushvalue(L, -1);
|
||||||
|
lua_setmetatable(L, 1);
|
||||||
|
}
|
||||||
|
lua_pushglobaltable(L);
|
||||||
|
lua_setfield(L, -2, "__index"); /* mt.__index = _G */
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
#endif
|
||||||
|
/* }====================================================== */
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
static const luaL_Reg pk_funcs[] = {
|
static const luaL_Reg pk_funcs[] = {
|
||||||
{"loadlib", ll_loadlib},
|
{"loadlib", ll_loadlib},
|
||||||
{"searchpath", ll_searchpath},
|
{"searchpath", ll_searchpath},
|
||||||
|
#if defined(LUA_COMPAT_MODULE)
|
||||||
|
{"seeall", ll_seeall},
|
||||||
|
#endif
|
||||||
/* placeholders */
|
/* placeholders */
|
||||||
{"preload", NULL},
|
{"preload", NULL},
|
||||||
{"cpath", NULL},
|
{"cpath", NULL},
|
||||||
@@ -695,19 +722,17 @@ static const luaL_Reg pk_funcs[] = {
|
|||||||
|
|
||||||
|
|
||||||
static const luaL_Reg ll_funcs[] = {
|
static const luaL_Reg ll_funcs[] = {
|
||||||
|
#if defined(LUA_COMPAT_MODULE)
|
||||||
|
{"module", ll_module},
|
||||||
|
#endif
|
||||||
{"require", ll_require},
|
{"require", ll_require},
|
||||||
{NULL, NULL}
|
{NULL, NULL}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
static void createsearcherstable (lua_State *L) {
|
static void createsearcherstable (lua_State *L) {
|
||||||
static const lua_CFunction searchers[] = {
|
static const lua_CFunction searchers[] =
|
||||||
searcher_preload,
|
{searcher_preload, searcher_Lua, searcher_C, searcher_Croot, NULL};
|
||||||
searcher_Lua,
|
|
||||||
searcher_C,
|
|
||||||
searcher_Croot,
|
|
||||||
NULL
|
|
||||||
};
|
|
||||||
int i;
|
int i;
|
||||||
/* create 'searchers' table */
|
/* create 'searchers' table */
|
||||||
lua_createtable(L, sizeof(searchers)/sizeof(searchers[0]) - 1, 0);
|
lua_createtable(L, sizeof(searchers)/sizeof(searchers[0]) - 1, 0);
|
||||||
@@ -717,13 +742,30 @@ static void createsearcherstable (lua_State *L) {
|
|||||||
lua_pushcclosure(L, searchers[i], 1);
|
lua_pushcclosure(L, searchers[i], 1);
|
||||||
lua_rawseti(L, -2, i+1);
|
lua_rawseti(L, -2, i+1);
|
||||||
}
|
}
|
||||||
|
#if defined(LUA_COMPAT_LOADERS)
|
||||||
|
lua_pushvalue(L, -1); /* make a copy of 'searchers' table */
|
||||||
|
lua_setfield(L, -3, "loaders"); /* put it in field 'loaders' */
|
||||||
|
#endif
|
||||||
lua_setfield(L, -2, "searchers"); /* put it in field 'searchers' */
|
lua_setfield(L, -2, "searchers"); /* put it in field 'searchers' */
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/*
|
||||||
|
** create table CLIBS to keep track of loaded C libraries,
|
||||||
|
** setting a finalizer to close all libraries when closing state.
|
||||||
|
*/
|
||||||
|
static void createclibstable (lua_State *L) {
|
||||||
|
lua_newtable(L); /* create CLIBS table */
|
||||||
|
lua_createtable(L, 0, 1); /* create metatable for CLIBS */
|
||||||
|
lua_pushcfunction(L, gctm);
|
||||||
|
lua_setfield(L, -2, "__gc"); /* set finalizer for CLIBS table */
|
||||||
|
lua_setmetatable(L, -2);
|
||||||
|
lua_rawsetp(L, LUA_REGISTRYINDEX, &CLIBS); /* set CLIBS table in registry */
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
LUAMOD_API int luaopen_package (lua_State *L) {
|
LUAMOD_API int luaopen_package (lua_State *L) {
|
||||||
luaL_getsubtable(L, LUA_REGISTRYINDEX, CLIBS); /* create CLIBS table */
|
createclibstable(L);
|
||||||
lua_pop(L, 1); /* will not use it now */
|
|
||||||
luaL_newlib(L, pk_funcs); /* create 'package' table */
|
luaL_newlib(L, pk_funcs); /* create 'package' table */
|
||||||
createsearcherstable(L);
|
createsearcherstable(L);
|
||||||
/* set paths */
|
/* set paths */
|
||||||
|
|||||||
@@ -1,5 +1,5 @@
|
|||||||
/*
|
/*
|
||||||
** $Id: lobject.c $
|
** $Id: lobject.c,v 2.113.1.1 2017/04/19 17:29:57 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
|
||||||
*/
|
*/
|
||||||
@@ -10,7 +10,6 @@
|
|||||||
#include "lprefix.h"
|
#include "lprefix.h"
|
||||||
|
|
||||||
|
|
||||||
#include <float.h>
|
|
||||||
#include <locale.h>
|
#include <locale.h>
|
||||||
#include <math.h>
|
#include <math.h>
|
||||||
#include <stdarg.h>
|
#include <stdarg.h>
|
||||||
@@ -30,12 +29,41 @@
|
|||||||
#include "lvm.h"
|
#include "lvm.h"
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
LUAI_DDEF const TValue luaO_nilobject_ = {NILCONSTANT};
|
||||||
|
|
||||||
|
|
||||||
/*
|
/*
|
||||||
** Computes ceil(log2(x)), which is the smallest integer n such that
|
** converts an integer to a "floating point byte", represented as
|
||||||
** x <= (1 << n).
|
** (eeeeexxx), where the real value is (1xxx) * 2^(eeeee - 1) if
|
||||||
|
** eeeee != 0 and (xxx) otherwise.
|
||||||
*/
|
*/
|
||||||
lu_byte luaO_ceillog2 (unsigned int x) {
|
int luaO_int2fb (unsigned int x) {
|
||||||
static const lu_byte log_2[256] = { /* log_2[i - 1] = ceil(log2(i)) */
|
int e = 0; /* exponent */
|
||||||
|
if (x < 8) return x;
|
||||||
|
while (x >= (8 << 4)) { /* coarse steps */
|
||||||
|
x = (x + 0xf) >> 4; /* x = ceil(x / 16) */
|
||||||
|
e += 4;
|
||||||
|
}
|
||||||
|
while (x >= (8 << 1)) { /* fine steps */
|
||||||
|
x = (x + 1) >> 1; /* x = ceil(x / 2) */
|
||||||
|
e++;
|
||||||
|
}
|
||||||
|
return ((e+1) << 3) | (cast_int(x) - 8);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/* converts back */
|
||||||
|
int luaO_fb2int (int x) {
|
||||||
|
return (x < 8) ? x : ((x & 7) + 8) << ((x >> 3) - 1);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/*
|
||||||
|
** Computes ceil(log2(x))
|
||||||
|
*/
|
||||||
|
int luaO_ceillog2 (unsigned int x) {
|
||||||
|
static const lu_byte log_2[256] = { /* log_2[i] = ceil(log2(i - 1)) */
|
||||||
0,1,2,2,3,3,3,3,4,4,4,4,4,4,4,4,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,
|
0,1,2,2,3,3,3,3,4,4,4,4,4,4,4,4,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,
|
||||||
6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,
|
6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,
|
||||||
7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,
|
7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,
|
||||||
@@ -48,67 +76,7 @@ lu_byte luaO_ceillog2 (unsigned int x) {
|
|||||||
int l = 0;
|
int l = 0;
|
||||||
x--;
|
x--;
|
||||||
while (x >= 256) { l += 8; x >>= 8; }
|
while (x >= 256) { l += 8; x >>= 8; }
|
||||||
return cast_byte(l + log_2[x]);
|
return l + log_2[x];
|
||||||
}
|
|
||||||
|
|
||||||
/*
|
|
||||||
** Encodes 'p'% as a floating-point byte, represented as (eeeexxxx).
|
|
||||||
** The exponent is represented using excess-7. Mimicking IEEE 754, the
|
|
||||||
** representation normalizes the number when possible, assuming an extra
|
|
||||||
** 1 before the mantissa (xxxx) and adding one to the exponent (eeee)
|
|
||||||
** to signal that. So, the real value is (1xxxx) * 2^(eeee - 7 - 1) if
|
|
||||||
** eeee != 0, and (xxxx) * 2^-7 otherwise (subnormal numbers).
|
|
||||||
*/
|
|
||||||
lu_byte luaO_codeparam (unsigned int p) {
|
|
||||||
if (p >= (cast(lu_mem, 0x1F) << (0xF - 7 - 1)) * 100u) /* overflow? */
|
|
||||||
return 0xFF; /* return maximum value */
|
|
||||||
else {
|
|
||||||
p = (cast(l_uint32, p) * 128 + 99) / 100; /* round up the division */
|
|
||||||
if (p < 0x10) { /* subnormal number? */
|
|
||||||
/* exponent bits are already zero; nothing else to do */
|
|
||||||
return cast_byte(p);
|
|
||||||
}
|
|
||||||
else { /* p >= 0x10 implies ceil(log2(p + 1)) >= 5 */
|
|
||||||
/* preserve 5 bits in 'p' */
|
|
||||||
unsigned log = luaO_ceillog2(p + 1) - 5u;
|
|
||||||
return cast_byte(((p >> log) - 0x10) | ((log + 1) << 4));
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
/*
|
|
||||||
** Computes 'p' times 'x', where 'p' is a floating-point byte. Roughly,
|
|
||||||
** we have to multiply 'x' by the mantissa and then shift accordingly to
|
|
||||||
** the exponent. If the exponent is positive, both the multiplication
|
|
||||||
** and the shift increase 'x', so we have to care only about overflows.
|
|
||||||
** For negative exponents, however, multiplying before the shift keeps
|
|
||||||
** more significant bits, as long as the multiplication does not
|
|
||||||
** overflow, so we check which order is best.
|
|
||||||
*/
|
|
||||||
l_mem luaO_applyparam (lu_byte p, l_mem x) {
|
|
||||||
int m = p & 0xF; /* mantissa */
|
|
||||||
int e = (p >> 4); /* exponent */
|
|
||||||
if (e > 0) { /* normalized? */
|
|
||||||
e--; /* correct exponent */
|
|
||||||
m += 0x10; /* correct mantissa; maximum value is 0x1F */
|
|
||||||
}
|
|
||||||
e -= 7; /* correct excess-7 */
|
|
||||||
if (e >= 0) {
|
|
||||||
if (x < (MAX_LMEM / 0x1F) >> e) /* no overflow? */
|
|
||||||
return (x * m) << e; /* order doesn't matter here */
|
|
||||||
else /* real overflow */
|
|
||||||
return MAX_LMEM;
|
|
||||||
}
|
|
||||||
else { /* negative exponent */
|
|
||||||
e = -e;
|
|
||||||
if (x < MAX_LMEM / 0x1F) /* multiplication cannot overflow? */
|
|
||||||
return (x * m) >> e; /* multiplying first gives more precision */
|
|
||||||
else if ((x >> e) < MAX_LMEM / 0x1F) /* cannot overflow after shift? */
|
|
||||||
return (x >> e) * m;
|
|
||||||
else /* real overflow */
|
|
||||||
return MAX_LMEM;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@@ -119,12 +87,12 @@ static lua_Integer intarith (lua_State *L, int op, lua_Integer v1,
|
|||||||
case LUA_OPSUB:return intop(-, v1, v2);
|
case LUA_OPSUB:return intop(-, v1, v2);
|
||||||
case LUA_OPMUL:return intop(*, v1, v2);
|
case LUA_OPMUL:return intop(*, v1, v2);
|
||||||
case LUA_OPMOD: return luaV_mod(L, v1, v2);
|
case LUA_OPMOD: return luaV_mod(L, v1, v2);
|
||||||
case LUA_OPIDIV: return luaV_idiv(L, v1, v2);
|
case LUA_OPIDIV: return luaV_div(L, v1, v2);
|
||||||
case LUA_OPBAND: return intop(&, v1, v2);
|
case LUA_OPBAND: return intop(&, v1, v2);
|
||||||
case LUA_OPBOR: return intop(|, v1, v2);
|
case LUA_OPBOR: return intop(|, v1, v2);
|
||||||
case LUA_OPBXOR: return intop(^, v1, v2);
|
case LUA_OPBXOR: return intop(^, v1, v2);
|
||||||
case LUA_OPSHL: return luaV_shiftl(v1, v2);
|
case LUA_OPSHL: return luaV_shiftl(v1, v2);
|
||||||
case LUA_OPSHR: return luaV_shiftr(v1, v2);
|
case LUA_OPSHR: return luaV_shiftl(v1, -v2);
|
||||||
case LUA_OPUNM: return intop(-, 0, v1);
|
case LUA_OPUNM: return intop(-, 0, v1);
|
||||||
case LUA_OPBNOT: return intop(^, ~l_castS2U(0), v1);
|
case LUA_OPBNOT: return intop(^, ~l_castS2U(0), v1);
|
||||||
default: lua_assert(0); return 0;
|
default: lua_assert(0); return 0;
|
||||||
@@ -142,62 +110,59 @@ static lua_Number numarith (lua_State *L, int op, lua_Number v1,
|
|||||||
case LUA_OPPOW: return luai_numpow(L, v1, v2);
|
case LUA_OPPOW: return luai_numpow(L, v1, v2);
|
||||||
case LUA_OPIDIV: return luai_numidiv(L, v1, v2);
|
case LUA_OPIDIV: return luai_numidiv(L, v1, v2);
|
||||||
case LUA_OPUNM: return luai_numunm(L, v1);
|
case LUA_OPUNM: return luai_numunm(L, v1);
|
||||||
case LUA_OPMOD: return luaV_modf(L, v1, v2);
|
case LUA_OPMOD: {
|
||||||
|
lua_Number m;
|
||||||
|
luai_nummod(L, v1, v2, m);
|
||||||
|
return m;
|
||||||
|
}
|
||||||
default: lua_assert(0); return 0;
|
default: lua_assert(0); return 0;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
int luaO_rawarith (lua_State *L, int op, const TValue *p1, const TValue *p2,
|
void luaO_arith (lua_State *L, int op, const TValue *p1, const TValue *p2,
|
||||||
TValue *res) {
|
TValue *res) {
|
||||||
switch (op) {
|
switch (op) {
|
||||||
case LUA_OPBAND: case LUA_OPBOR: case LUA_OPBXOR:
|
case LUA_OPBAND: case LUA_OPBOR: case LUA_OPBXOR:
|
||||||
case LUA_OPSHL: case LUA_OPSHR:
|
case LUA_OPSHL: case LUA_OPSHR:
|
||||||
case LUA_OPBNOT: { /* operate only on integers */
|
case LUA_OPBNOT: { /* operate only on integers */
|
||||||
lua_Integer i1; lua_Integer i2;
|
lua_Integer i1; lua_Integer i2;
|
||||||
if (tointegerns(p1, &i1) && tointegerns(p2, &i2)) {
|
if (tointeger(p1, &i1) && tointeger(p2, &i2)) {
|
||||||
setivalue(res, intarith(L, op, i1, i2));
|
setivalue(res, intarith(L, op, i1, i2));
|
||||||
return 1;
|
return;
|
||||||
}
|
}
|
||||||
else return 0; /* fail */
|
else break; /* go to the end */
|
||||||
}
|
}
|
||||||
case LUA_OPDIV: case LUA_OPPOW: { /* operate only on floats */
|
case LUA_OPDIV: case LUA_OPPOW: { /* operate only on floats */
|
||||||
lua_Number n1; lua_Number n2;
|
lua_Number n1; lua_Number n2;
|
||||||
if (tonumberns(p1, n1) && tonumberns(p2, n2)) {
|
if (tonumber(p1, &n1) && tonumber(p2, &n2)) {
|
||||||
setfltvalue(res, numarith(L, op, n1, n2));
|
setfltvalue(res, numarith(L, op, n1, n2));
|
||||||
return 1;
|
return;
|
||||||
}
|
}
|
||||||
else return 0; /* fail */
|
else break; /* go to the end */
|
||||||
}
|
}
|
||||||
default: { /* other operations */
|
default: { /* other operations */
|
||||||
lua_Number n1; lua_Number n2;
|
lua_Number n1; lua_Number n2;
|
||||||
if (ttisinteger(p1) && ttisinteger(p2)) {
|
if (ttisinteger(p1) && ttisinteger(p2)) {
|
||||||
setivalue(res, intarith(L, op, ivalue(p1), ivalue(p2)));
|
setivalue(res, intarith(L, op, ivalue(p1), ivalue(p2)));
|
||||||
return 1;
|
return;
|
||||||
}
|
}
|
||||||
else if (tonumberns(p1, n1) && tonumberns(p2, n2)) {
|
else if (tonumber(p1, &n1) && tonumber(p2, &n2)) {
|
||||||
setfltvalue(res, numarith(L, op, n1, n2));
|
setfltvalue(res, numarith(L, op, n1, n2));
|
||||||
return 1;
|
return;
|
||||||
}
|
}
|
||||||
else return 0; /* fail */
|
else break; /* go to the end */
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
/* could not perform raw operation; try metamethod */
|
||||||
|
lua_assert(L != NULL); /* should not fail when folding (compile time) */
|
||||||
|
luaT_trybinTM(L, p1, p2, res, cast(TMS, (op - LUA_OPADD) + TM_ADD));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
void luaO_arith (lua_State *L, int op, const TValue *p1, const TValue *p2,
|
int luaO_hexavalue (int c) {
|
||||||
StkId res) {
|
if (lisdigit(c)) return c - '0';
|
||||||
if (!luaO_rawarith(L, op, p1, p2, s2v(res))) {
|
else return (ltolower(c) - 'a') + 10;
|
||||||
/* could not perform raw operation; try metamethod */
|
|
||||||
luaT_trybinTM(L, p1, p2, res, cast(TMS, (op - LUA_OPADD) + TM_ADD));
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
lu_byte luaO_hexavalue (int c) {
|
|
||||||
lua_assert(lisxdigit(c));
|
|
||||||
if (lisdigit(c)) return cast_byte(c - '0');
|
|
||||||
else return cast_byte((ltolower(c) - 'a') + 10);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@@ -222,22 +187,22 @@ static int isneg (const char **s) {
|
|||||||
#define MAXSIGDIG 30
|
#define MAXSIGDIG 30
|
||||||
|
|
||||||
/*
|
/*
|
||||||
** convert a hexadecimal numeric string to a number, following
|
** convert an hexadecimal numeric string to a number, following
|
||||||
** C99 specification for 'strtod'
|
** C99 specification for 'strtod'
|
||||||
*/
|
*/
|
||||||
static lua_Number lua_strx2number (const char *s, char **endptr) {
|
static lua_Number lua_strx2number (const char *s, char **endptr) {
|
||||||
int dot = lua_getlocaledecpoint();
|
int dot = lua_getlocaledecpoint();
|
||||||
lua_Number r = l_mathop(0.0); /* result (accumulator) */
|
lua_Number r = 0.0; /* result (accumulator) */
|
||||||
int sigdig = 0; /* number of significant digits */
|
int sigdig = 0; /* number of significant digits */
|
||||||
int nosigdig = 0; /* number of non-significant digits */
|
int nosigdig = 0; /* number of non-significant digits */
|
||||||
int e = 0; /* exponent correction */
|
int e = 0; /* exponent correction */
|
||||||
int neg; /* 1 if number is negative */
|
int neg; /* 1 if number is negative */
|
||||||
int hasdot = 0; /* true after seen a dot */
|
int hasdot = 0; /* true after seen a dot */
|
||||||
*endptr = cast_charp(s); /* nothing is valid yet */
|
*endptr = cast(char *, s); /* nothing is valid yet */
|
||||||
while (lisspace(cast_uchar(*s))) s++; /* skip initial spaces */
|
while (lisspace(cast_uchar(*s))) s++; /* skip initial spaces */
|
||||||
neg = isneg(&s); /* check sign */
|
neg = isneg(&s); /* check signal */
|
||||||
if (!(*s == '0' && (*(s + 1) == 'x' || *(s + 1) == 'X'))) /* check '0x' */
|
if (!(*s == '0' && (*(s + 1) == 'x' || *(s + 1) == 'X'))) /* check '0x' */
|
||||||
return l_mathop(0.0); /* invalid format (no '0x') */
|
return 0.0; /* invalid format (no '0x') */
|
||||||
for (s += 2; ; s++) { /* skip '0x' and read numeral */
|
for (s += 2; ; s++) { /* skip '0x' and read numeral */
|
||||||
if (*s == dot) {
|
if (*s == dot) {
|
||||||
if (hasdot) break; /* second dot? stop loop */
|
if (hasdot) break; /* second dot? stop loop */
|
||||||
@@ -247,28 +212,28 @@ static lua_Number lua_strx2number (const char *s, char **endptr) {
|
|||||||
if (sigdig == 0 && *s == '0') /* non-significant digit (zero)? */
|
if (sigdig == 0 && *s == '0') /* non-significant digit (zero)? */
|
||||||
nosigdig++;
|
nosigdig++;
|
||||||
else if (++sigdig <= MAXSIGDIG) /* can read it without overflow? */
|
else if (++sigdig <= MAXSIGDIG) /* can read it without overflow? */
|
||||||
r = (r * l_mathop(16.0)) + luaO_hexavalue(*s);
|
r = (r * cast_num(16.0)) + luaO_hexavalue(*s);
|
||||||
else e++; /* too many digits; ignore, but still count for exponent */
|
else e++; /* too many digits; ignore, but still count for exponent */
|
||||||
if (hasdot) e--; /* decimal digit? correct exponent */
|
if (hasdot) e--; /* decimal digit? correct exponent */
|
||||||
}
|
}
|
||||||
else break; /* neither a dot nor a digit */
|
else break; /* neither a dot nor a digit */
|
||||||
}
|
}
|
||||||
if (nosigdig + sigdig == 0) /* no digits? */
|
if (nosigdig + sigdig == 0) /* no digits? */
|
||||||
return l_mathop(0.0); /* invalid format */
|
return 0.0; /* invalid format */
|
||||||
*endptr = cast_charp(s); /* valid up to here */
|
*endptr = cast(char *, s); /* valid up to here */
|
||||||
e *= 4; /* each digit multiplies/divides value by 2^4 */
|
e *= 4; /* each digit multiplies/divides value by 2^4 */
|
||||||
if (*s == 'p' || *s == 'P') { /* exponent part? */
|
if (*s == 'p' || *s == 'P') { /* exponent part? */
|
||||||
int exp1 = 0; /* exponent value */
|
int exp1 = 0; /* exponent value */
|
||||||
int neg1; /* exponent sign */
|
int neg1; /* exponent signal */
|
||||||
s++; /* skip 'p' */
|
s++; /* skip 'p' */
|
||||||
neg1 = isneg(&s); /* sign */
|
neg1 = isneg(&s); /* signal */
|
||||||
if (!lisdigit(cast_uchar(*s)))
|
if (!lisdigit(cast_uchar(*s)))
|
||||||
return l_mathop(0.0); /* invalid; must have at least one digit */
|
return 0.0; /* invalid; must have at least one digit */
|
||||||
while (lisdigit(cast_uchar(*s))) /* read exponent */
|
while (lisdigit(cast_uchar(*s))) /* read exponent */
|
||||||
exp1 = exp1 * 10 + *(s++) - '0';
|
exp1 = exp1 * 10 + *(s++) - '0';
|
||||||
if (neg1) exp1 = -exp1;
|
if (neg1) exp1 = -exp1;
|
||||||
e += exp1;
|
e += exp1;
|
||||||
*endptr = cast_charp(s); /* valid up to here */
|
*endptr = cast(char *, s); /* valid up to here */
|
||||||
}
|
}
|
||||||
if (neg) r = -r;
|
if (neg) r = -r;
|
||||||
return l_mathop(ldexp)(r, e);
|
return l_mathop(ldexp)(r, e);
|
||||||
@@ -278,42 +243,37 @@ static lua_Number lua_strx2number (const char *s, char **endptr) {
|
|||||||
/* }====================================================== */
|
/* }====================================================== */
|
||||||
|
|
||||||
|
|
||||||
/* maximum length of a numeral to be converted to a number */
|
/* maximum length of a numeral */
|
||||||
#if !defined (L_MAXLENNUM)
|
#if !defined (L_MAXLENNUM)
|
||||||
#define L_MAXLENNUM 200
|
#define L_MAXLENNUM 200
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
/*
|
|
||||||
** Convert string 's' to a Lua number (put in 'result'). Return NULL on
|
|
||||||
** fail or the address of the ending '\0' on success. ('mode' == 'x')
|
|
||||||
** means a hexadecimal numeral.
|
|
||||||
*/
|
|
||||||
static const char *l_str2dloc (const char *s, lua_Number *result, int mode) {
|
static const char *l_str2dloc (const char *s, lua_Number *result, int mode) {
|
||||||
char *endptr;
|
char *endptr;
|
||||||
*result = (mode == 'x') ? lua_strx2number(s, &endptr) /* try to convert */
|
*result = (mode == 'x') ? lua_strx2number(s, &endptr) /* try to convert */
|
||||||
: lua_str2number(s, &endptr);
|
: lua_str2number(s, &endptr);
|
||||||
if (endptr == s) return NULL; /* nothing recognized? */
|
if (endptr == s) return NULL; /* nothing recognized? */
|
||||||
while (lisspace(cast_uchar(*endptr))) endptr++; /* skip trailing spaces */
|
while (lisspace(cast_uchar(*endptr))) endptr++; /* skip trailing spaces */
|
||||||
return (*endptr == '\0') ? endptr : NULL; /* OK iff no trailing chars */
|
return (*endptr == '\0') ? endptr : NULL; /* OK if no trailing characters */
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/*
|
/*
|
||||||
** Convert string 's' to a Lua number (put in 'result') handling the
|
** Convert string 's' to a Lua number (put in 'result'). Return NULL
|
||||||
** current locale.
|
** on fail or the address of the ending '\0' on success.
|
||||||
|
** 'pmode' points to (and 'mode' contains) special things in the string:
|
||||||
|
** - 'x'/'X' means an hexadecimal numeral
|
||||||
|
** - 'n'/'N' means 'inf' or 'nan' (which should be rejected)
|
||||||
|
** - '.' just optimizes the search for the common case (nothing special)
|
||||||
** This function accepts both the current locale or a dot as the radix
|
** This function accepts both the current locale or a dot as the radix
|
||||||
** mark. If the conversion fails, it may mean number has a dot but
|
** mark. If the convertion fails, it may mean number has a dot but
|
||||||
** locale accepts something else. In that case, the code copies 's'
|
** locale accepts something else. In that case, the code copies 's'
|
||||||
** to a buffer (because 's' is read-only), changes the dot to the
|
** to a buffer (because 's' is read-only), changes the dot to the
|
||||||
** current locale radix mark, and tries to convert again.
|
** current locale radix mark, and tries to convert again.
|
||||||
** The variable 'mode' checks for special characters in the string:
|
|
||||||
** - 'n' means 'inf' or 'nan' (which should be rejected)
|
|
||||||
** - 'x' means a hexadecimal numeral
|
|
||||||
** - '.' just optimizes the search for the common case (no special chars)
|
|
||||||
*/
|
*/
|
||||||
static const char *l_str2d (const char *s, lua_Number *result) {
|
static const char *l_str2d (const char *s, lua_Number *result) {
|
||||||
const char *endptr;
|
const char *endptr;
|
||||||
const char *pmode = strpbrk(s, ".xXnN"); /* look for special chars */
|
const char *pmode = strpbrk(s, ".xXnN");
|
||||||
int mode = pmode ? ltolower(cast_uchar(*pmode)) : 0;
|
int mode = pmode ? ltolower(cast_uchar(*pmode)) : 0;
|
||||||
if (mode == 'n') /* reject 'inf' and 'nan' */
|
if (mode == 'n') /* reject 'inf' and 'nan' */
|
||||||
return NULL;
|
return NULL;
|
||||||
@@ -321,7 +281,7 @@ static const char *l_str2d (const char *s, lua_Number *result) {
|
|||||||
if (endptr == NULL) { /* failed? may be a different locale */
|
if (endptr == NULL) { /* failed? may be a different locale */
|
||||||
char buff[L_MAXLENNUM + 1];
|
char buff[L_MAXLENNUM + 1];
|
||||||
const char *pdot = strchr(s, '.');
|
const char *pdot = strchr(s, '.');
|
||||||
if (pdot == NULL || strlen(s) > L_MAXLENNUM)
|
if (strlen(s) > L_MAXLENNUM || pdot == NULL)
|
||||||
return NULL; /* string too long or no dot; fail */
|
return NULL; /* string too long or no dot; fail */
|
||||||
strcpy(buff, s); /* copy string to buffer */
|
strcpy(buff, s); /* copy string to buffer */
|
||||||
buff[pdot - s] = lua_getlocaledecpoint(); /* correct decimal point */
|
buff[pdot - s] = lua_getlocaledecpoint(); /* correct decimal point */
|
||||||
@@ -355,7 +315,7 @@ static const char *l_str2int (const char *s, lua_Integer *result) {
|
|||||||
int d = *s - '0';
|
int d = *s - '0';
|
||||||
if (a >= MAXBY10 && (a > MAXBY10 || d > MAXLASTD + neg)) /* overflow? */
|
if (a >= MAXBY10 && (a > MAXBY10 || d > MAXLASTD + neg)) /* overflow? */
|
||||||
return NULL; /* do not accept it (as integer) */
|
return NULL; /* do not accept it (as integer) */
|
||||||
a = a * 10 + cast_uint(d);
|
a = a * 10 + d;
|
||||||
empty = 0;
|
empty = 0;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -379,283 +339,129 @@ size_t luaO_str2num (const char *s, TValue *o) {
|
|||||||
}
|
}
|
||||||
else
|
else
|
||||||
return 0; /* conversion failed */
|
return 0; /* conversion failed */
|
||||||
return ct_diff2sz(e - s) + 1; /* success; return string size */
|
return (e - s) + 1; /* success; return string size */
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
int luaO_utf8esc (char *buff, l_uint32 x) {
|
int luaO_utf8esc (char *buff, unsigned long x) {
|
||||||
int n = 1; /* number of bytes put in buffer (backwards) */
|
int n = 1; /* number of bytes put in buffer (backwards) */
|
||||||
lua_assert(x <= 0x7FFFFFFFu);
|
lua_assert(x <= 0x10FFFF);
|
||||||
if (x < 0x80) /* ASCII? */
|
if (x < 0x80) /* ascii? */
|
||||||
buff[UTF8BUFFSZ - 1] = cast_char(x);
|
buff[UTF8BUFFSZ - 1] = cast(char, x);
|
||||||
else { /* need continuation bytes */
|
else { /* need continuation bytes */
|
||||||
unsigned int mfb = 0x3f; /* maximum that fits in first byte */
|
unsigned int mfb = 0x3f; /* maximum that fits in first byte */
|
||||||
do { /* add continuation bytes */
|
do { /* add continuation bytes */
|
||||||
buff[UTF8BUFFSZ - (n++)] = cast_char(0x80 | (x & 0x3f));
|
buff[UTF8BUFFSZ - (n++)] = cast(char, 0x80 | (x & 0x3f));
|
||||||
x >>= 6; /* remove added bits */
|
x >>= 6; /* remove added bits */
|
||||||
mfb >>= 1; /* now there is one less bit available in first byte */
|
mfb >>= 1; /* now there is one less bit available in first byte */
|
||||||
} while (x > mfb); /* still needs continuation byte? */
|
} while (x > mfb); /* still needs continuation byte? */
|
||||||
buff[UTF8BUFFSZ - n] = cast_char((~mfb << 1) | x); /* add first byte */
|
buff[UTF8BUFFSZ - n] = cast(char, (~mfb << 1) | x); /* add first byte */
|
||||||
}
|
}
|
||||||
return n;
|
return n;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/*
|
/* maximum length of the conversion of a number to a string */
|
||||||
** The size of the buffer for the conversion of a number to a string
|
#define MAXNUMBER2STR 50
|
||||||
** 'LUA_N2SBUFFSZ' must be enough to accommodate both LUA_INTEGER_FMT
|
|
||||||
** and LUA_NUMBER_FMT. For a long long int, this is 19 digits plus a
|
|
||||||
** sign and a final '\0', adding to 21. For a long double, it can go to
|
|
||||||
** a sign, the dot, an exponent letter, an exponent sign, 4 exponent
|
|
||||||
** digits, the final '\0', plus the significant digits, which are
|
|
||||||
** approximately the *_DIG attribute.
|
|
||||||
*/
|
|
||||||
#if LUA_N2SBUFFSZ < (20 + l_floatatt(DIG))
|
|
||||||
#error "invalid value for LUA_N2SBUFFSZ"
|
|
||||||
#endif
|
|
||||||
|
|
||||||
|
|
||||||
/*
|
/*
|
||||||
** Convert a float to a string, adding it to a buffer. First try with
|
** Convert a number object to a string
|
||||||
** a not too large number of digits, to avoid noise (for instance,
|
|
||||||
** 1.1 going to "1.1000000000000001"). If that lose precision, so
|
|
||||||
** that reading the result back gives a different number, then do the
|
|
||||||
** conversion again with extra precision. Moreover, if the numeral looks
|
|
||||||
** like an integer (without a decimal point or an exponent), add ".0" to
|
|
||||||
** its end.
|
|
||||||
*/
|
*/
|
||||||
static int tostringbuffFloat (lua_Number n, char *buff) {
|
void luaO_tostring (lua_State *L, StkId obj) {
|
||||||
/* first conversion */
|
char buff[MAXNUMBER2STR];
|
||||||
int len = l_sprintf(buff, LUA_N2SBUFFSZ, LUA_NUMBER_FMT,
|
size_t len;
|
||||||
(LUAI_UACNUMBER)n);
|
|
||||||
lua_Number check = lua_str2number(buff, NULL); /* read it back */
|
|
||||||
if (check != n) { /* not enough precision? */
|
|
||||||
/* convert again with more precision */
|
|
||||||
len = l_sprintf(buff, LUA_N2SBUFFSZ, LUA_NUMBER_FMT_N,
|
|
||||||
(LUAI_UACNUMBER)n);
|
|
||||||
}
|
|
||||||
/* looks like an integer? */
|
|
||||||
if (buff[strspn(buff, "-0123456789")] == '\0') {
|
|
||||||
buff[len++] = lua_getlocaledecpoint();
|
|
||||||
buff[len++] = '0'; /* adds '.0' to result */
|
|
||||||
}
|
|
||||||
return len;
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
/*
|
|
||||||
** Convert a number object to a string, adding it to a buffer.
|
|
||||||
*/
|
|
||||||
unsigned luaO_tostringbuff (const TValue *obj, char *buff) {
|
|
||||||
int len;
|
|
||||||
lua_assert(ttisnumber(obj));
|
lua_assert(ttisnumber(obj));
|
||||||
if (ttisinteger(obj))
|
if (ttisinteger(obj))
|
||||||
len = lua_integer2str(buff, LUA_N2SBUFFSZ, ivalue(obj));
|
len = lua_integer2str(buff, sizeof(buff), ivalue(obj));
|
||||||
else
|
else {
|
||||||
len = tostringbuffFloat(fltvalue(obj), buff);
|
len = lua_number2str(buff, sizeof(buff), fltvalue(obj));
|
||||||
lua_assert(len < LUA_N2SBUFFSZ);
|
#if !defined(LUA_COMPAT_FLOATSTRING)
|
||||||
return cast_uint(len);
|
if (buff[strspn(buff, "-0123456789")] == '\0') { /* looks like an int? */
|
||||||
}
|
buff[len++] = lua_getlocaledecpoint();
|
||||||
|
buff[len++] = '0'; /* adds '.0' to result */
|
||||||
|
|
||||||
/*
|
|
||||||
** Convert a number object to a Lua string, replacing the value at 'obj'
|
|
||||||
*/
|
|
||||||
void luaO_tostring (lua_State *L, TValue *obj) {
|
|
||||||
char buff[LUA_N2SBUFFSZ];
|
|
||||||
unsigned len = luaO_tostringbuff(obj, buff);
|
|
||||||
setsvalue(L, obj, luaS_newlstr(L, buff, len));
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
/*
|
|
||||||
** {==================================================================
|
|
||||||
** 'luaO_pushvfstring'
|
|
||||||
** ===================================================================
|
|
||||||
*/
|
|
||||||
|
|
||||||
/*
|
|
||||||
** Size for buffer space used by 'luaO_pushvfstring'. It should be
|
|
||||||
** (LUA_IDSIZE + LUA_N2SBUFFSZ) + a minimal space for basic messages,
|
|
||||||
** so that 'luaG_addinfo' can work directly on the static buffer.
|
|
||||||
*/
|
|
||||||
#define BUFVFS cast_uint(LUA_IDSIZE + LUA_N2SBUFFSZ + 95)
|
|
||||||
|
|
||||||
/*
|
|
||||||
** Buffer used by 'luaO_pushvfstring'. 'err' signals an error while
|
|
||||||
** building result (memory error [1] or buffer overflow [2]).
|
|
||||||
*/
|
|
||||||
typedef struct BuffFS {
|
|
||||||
lua_State *L;
|
|
||||||
char *b;
|
|
||||||
size_t buffsize;
|
|
||||||
size_t blen; /* length of string in 'buff' */
|
|
||||||
int err;
|
|
||||||
char space[BUFVFS]; /* initial buffer */
|
|
||||||
} BuffFS;
|
|
||||||
|
|
||||||
|
|
||||||
static void initbuff (lua_State *L, BuffFS *buff) {
|
|
||||||
buff->L = L;
|
|
||||||
buff->b = buff->space;
|
|
||||||
buff->buffsize = sizeof(buff->space);
|
|
||||||
buff->blen = 0;
|
|
||||||
buff->err = 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
/*
|
|
||||||
** Push final result from 'luaO_pushvfstring'. This function may raise
|
|
||||||
** errors explicitly or through memory errors, so it must run protected.
|
|
||||||
*/
|
|
||||||
static void pushbuff (lua_State *L, void *ud) {
|
|
||||||
BuffFS *buff = cast(BuffFS*, ud);
|
|
||||||
switch (buff->err) {
|
|
||||||
case 1: /* memory error */
|
|
||||||
luaD_throw(L, LUA_ERRMEM);
|
|
||||||
break;
|
|
||||||
case 2: /* length overflow: Add "..." at the end of result */
|
|
||||||
if (buff->buffsize - buff->blen < 3)
|
|
||||||
strcpy(buff->b + buff->blen - 3, "..."); /* 'blen' must be > 3 */
|
|
||||||
else { /* there is enough space left for the "..." */
|
|
||||||
strcpy(buff->b + buff->blen, "...");
|
|
||||||
buff->blen += 3;
|
|
||||||
}
|
|
||||||
/* FALLTHROUGH */
|
|
||||||
default: { /* no errors, but it can raise one creating the new string */
|
|
||||||
TString *ts = luaS_newlstr(L, buff->b, buff->blen);
|
|
||||||
setsvalue2s(L, L->top.p, ts);
|
|
||||||
L->top.p++;
|
|
||||||
}
|
}
|
||||||
|
#endif
|
||||||
}
|
}
|
||||||
|
setsvalue2s(L, obj, luaS_newlstr(L, buff, len));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
static const char *clearbuff (BuffFS *buff) {
|
static void pushstr (lua_State *L, const char *str, size_t l) {
|
||||||
lua_State *L = buff->L;
|
setsvalue2s(L, L->top, luaS_newlstr(L, str, l));
|
||||||
const char *res;
|
luaD_inctop(L);
|
||||||
if (luaD_rawrunprotected(L, pushbuff, buff) != LUA_OK) /* errors? */
|
|
||||||
res = NULL; /* error message is on the top of the stack */
|
|
||||||
else
|
|
||||||
res = getstr(tsvalue(s2v(L->top.p - 1)));
|
|
||||||
if (buff->b != buff->space) /* using dynamic buffer? */
|
|
||||||
luaM_freearray(L, buff->b, buff->buffsize); /* free it */
|
|
||||||
return res;
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
static void addstr2buff (BuffFS *buff, const char *str, size_t slen) {
|
|
||||||
size_t left = buff->buffsize - buff->blen; /* space left in the buffer */
|
|
||||||
if (buff->err) /* do nothing else after an error */
|
|
||||||
return;
|
|
||||||
if (slen > left) { /* new string doesn't fit into current buffer? */
|
|
||||||
if (slen > ((MAX_SIZE/2) - buff->blen)) { /* overflow? */
|
|
||||||
memcpy(buff->b + buff->blen, str, left); /* copy what it can */
|
|
||||||
buff->blen = buff->buffsize;
|
|
||||||
buff->err = 2; /* doesn't add anything else */
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
else {
|
|
||||||
size_t newsize = buff->buffsize + slen; /* limited to MAX_SIZE/2 */
|
|
||||||
char *newb =
|
|
||||||
(buff->b == buff->space) /* still using static space? */
|
|
||||||
? luaM_reallocvector(buff->L, NULL, 0, newsize, char)
|
|
||||||
: luaM_reallocvector(buff->L, buff->b, buff->buffsize, newsize,
|
|
||||||
char);
|
|
||||||
if (newb == NULL) { /* allocation error? */
|
|
||||||
buff->err = 1; /* signal a memory error */
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
if (buff->b == buff->space) /* new buffer (not reallocated)? */
|
|
||||||
memcpy(newb, buff->b, buff->blen); /* copy previous content */
|
|
||||||
buff->b = newb; /* set new (larger) buffer... */
|
|
||||||
buff->buffsize = newsize; /* ...and its new size */
|
|
||||||
}
|
|
||||||
}
|
|
||||||
memcpy(buff->b + buff->blen, str, slen); /* copy new content */
|
|
||||||
buff->blen += slen;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/*
|
/*
|
||||||
** Add a numeral to the buffer.
|
** this function handles only '%d', '%c', '%f', '%p', and '%s'
|
||||||
*/
|
|
||||||
static void addnum2buff (BuffFS *buff, TValue *num) {
|
|
||||||
char numbuff[LUA_N2SBUFFSZ];
|
|
||||||
unsigned len = luaO_tostringbuff(num, numbuff);
|
|
||||||
addstr2buff(buff, numbuff, len);
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
/*
|
|
||||||
** this function handles only '%d', '%c', '%f', '%p', '%s', and '%%'
|
|
||||||
conventional formats, plus Lua-specific '%I' and '%U'
|
conventional formats, plus Lua-specific '%I' and '%U'
|
||||||
*/
|
*/
|
||||||
const char *luaO_pushvfstring (lua_State *L, const char *fmt, va_list argp) {
|
const char *luaO_pushvfstring (lua_State *L, const char *fmt, va_list argp) {
|
||||||
BuffFS buff; /* holds last part of the result */
|
int n = 0;
|
||||||
const char *e; /* points to next '%' */
|
for (;;) {
|
||||||
initbuff(L, &buff);
|
const char *e = strchr(fmt, '%');
|
||||||
while ((e = strchr(fmt, '%')) != NULL) {
|
if (e == NULL) break;
|
||||||
addstr2buff(&buff, fmt, ct_diff2sz(e - fmt)); /* add 'fmt' up to '%' */
|
pushstr(L, fmt, e - fmt);
|
||||||
switch (*(e + 1)) { /* conversion specifier */
|
switch (*(e+1)) {
|
||||||
case 's': { /* zero-terminated string */
|
case 's': { /* zero-terminated string */
|
||||||
const char *s = va_arg(argp, char *);
|
const char *s = va_arg(argp, char *);
|
||||||
if (s == NULL) s = "(null)";
|
if (s == NULL) s = "(null)";
|
||||||
addstr2buff(&buff, s, strlen(s));
|
pushstr(L, s, strlen(s));
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
case 'c': { /* an 'int' as a character */
|
case 'c': { /* an 'int' as a character */
|
||||||
char c = cast_char(va_arg(argp, int));
|
char buff = cast(char, va_arg(argp, int));
|
||||||
addstr2buff(&buff, &c, sizeof(char));
|
if (lisprint(cast_uchar(buff)))
|
||||||
|
pushstr(L, &buff, 1);
|
||||||
|
else /* non-printable character; print its code */
|
||||||
|
luaO_pushfstring(L, "<\\%d>", cast_uchar(buff));
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
case 'd': { /* an 'int' */
|
case 'd': { /* an 'int' */
|
||||||
TValue num;
|
setivalue(L->top, va_arg(argp, int));
|
||||||
setivalue(&num, va_arg(argp, int));
|
goto top2str;
|
||||||
addnum2buff(&buff, &num);
|
|
||||||
break;
|
|
||||||
}
|
}
|
||||||
case 'I': { /* a 'lua_Integer' */
|
case 'I': { /* a 'lua_Integer' */
|
||||||
TValue num;
|
setivalue(L->top, cast(lua_Integer, va_arg(argp, l_uacInt)));
|
||||||
setivalue(&num, cast_Integer(va_arg(argp, l_uacInt)));
|
goto top2str;
|
||||||
addnum2buff(&buff, &num);
|
|
||||||
break;
|
|
||||||
}
|
}
|
||||||
case 'f': { /* a 'lua_Number' */
|
case 'f': { /* a 'lua_Number' */
|
||||||
TValue num;
|
setfltvalue(L->top, cast_num(va_arg(argp, l_uacNumber)));
|
||||||
setfltvalue(&num, cast_num(va_arg(argp, l_uacNumber)));
|
top2str: /* convert the top element to a string */
|
||||||
addnum2buff(&buff, &num);
|
luaD_inctop(L);
|
||||||
|
luaO_tostring(L, L->top - 1);
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
case 'p': { /* a pointer */
|
case 'p': { /* a pointer */
|
||||||
char bf[LUA_N2SBUFFSZ]; /* enough space for '%p' */
|
char buff[4*sizeof(void *) + 8]; /* should be enough space for a '%p' */
|
||||||
void *p = va_arg(argp, void *);
|
void *p = va_arg(argp, void *);
|
||||||
int len = lua_pointer2str(bf, LUA_N2SBUFFSZ, p);
|
int l = lua_pointer2str(buff, sizeof(buff), p);
|
||||||
addstr2buff(&buff, bf, cast_uint(len));
|
pushstr(L, buff, l);
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
case 'U': { /* an 'unsigned long' as a UTF-8 sequence */
|
case 'U': { /* an 'int' as a UTF-8 sequence */
|
||||||
char bf[UTF8BUFFSZ];
|
char buff[UTF8BUFFSZ];
|
||||||
unsigned long arg = va_arg(argp, unsigned long);
|
int l = luaO_utf8esc(buff, cast(long, va_arg(argp, long)));
|
||||||
int len = luaO_utf8esc(bf, cast(l_uint32, arg));
|
pushstr(L, buff + UTF8BUFFSZ - l, l);
|
||||||
addstr2buff(&buff, bf + UTF8BUFFSZ - len, cast_uint(len));
|
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
case '%': {
|
case '%': {
|
||||||
addstr2buff(&buff, "%", 1);
|
pushstr(L, "%", 1);
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
default: {
|
default: {
|
||||||
addstr2buff(&buff, e, 2); /* keep unknown format in the result */
|
luaG_runerror(L, "invalid option '%%%c' to 'lua_pushfstring'",
|
||||||
break;
|
*(e + 1));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
fmt = e + 2; /* skip '%' and the specifier */
|
n += 2;
|
||||||
|
fmt = e+2;
|
||||||
}
|
}
|
||||||
addstr2buff(&buff, fmt, strlen(fmt)); /* rest of 'fmt' */
|
luaD_checkstack(L, 1);
|
||||||
return clearbuff(&buff); /* empty buffer into a new string */
|
pushstr(L, fmt, strlen(fmt));
|
||||||
|
if (n > 0) luaV_concat(L, n + 1);
|
||||||
|
return svalue(L->top - 1);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@@ -665,13 +471,12 @@ const char *luaO_pushfstring (lua_State *L, const char *fmt, ...) {
|
|||||||
va_start(argp, fmt);
|
va_start(argp, fmt);
|
||||||
msg = luaO_pushvfstring(L, fmt, argp);
|
msg = luaO_pushvfstring(L, fmt, argp);
|
||||||
va_end(argp);
|
va_end(argp);
|
||||||
if (msg == NULL) /* error? */
|
|
||||||
luaD_throw(L, LUA_ERRMEM);
|
|
||||||
return msg;
|
return msg;
|
||||||
}
|
}
|
||||||
|
|
||||||
/* }================================================================== */
|
|
||||||
|
|
||||||
|
/* number of chars of a literal string without the ending \0 */
|
||||||
|
#define LL(x) (sizeof(x)/sizeof(char) - 1)
|
||||||
|
|
||||||
#define RETS "..."
|
#define RETS "..."
|
||||||
#define PRE "[string \""
|
#define PRE "[string \""
|
||||||
@@ -679,37 +484,36 @@ const char *luaO_pushfstring (lua_State *L, const char *fmt, ...) {
|
|||||||
|
|
||||||
#define addstr(a,b,l) ( memcpy(a,b,(l) * sizeof(char)), a += (l) )
|
#define addstr(a,b,l) ( memcpy(a,b,(l) * sizeof(char)), a += (l) )
|
||||||
|
|
||||||
void luaO_chunkid (char *out, const char *source, size_t srclen) {
|
void luaO_chunkid (char *out, const char *source, size_t bufflen) {
|
||||||
size_t bufflen = LUA_IDSIZE; /* free space in buffer */
|
size_t l = strlen(source);
|
||||||
if (*source == '=') { /* 'literal' source */
|
if (*source == '=') { /* 'literal' source */
|
||||||
if (srclen <= bufflen) /* small enough? */
|
if (l <= bufflen) /* small enough? */
|
||||||
memcpy(out, source + 1, srclen * sizeof(char));
|
memcpy(out, source + 1, l * sizeof(char));
|
||||||
else { /* truncate it */
|
else { /* truncate it */
|
||||||
addstr(out, source + 1, bufflen - 1);
|
addstr(out, source + 1, bufflen - 1);
|
||||||
*out = '\0';
|
*out = '\0';
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
else if (*source == '@') { /* file name */
|
else if (*source == '@') { /* file name */
|
||||||
if (srclen <= bufflen) /* small enough? */
|
if (l <= bufflen) /* small enough? */
|
||||||
memcpy(out, source + 1, srclen * sizeof(char));
|
memcpy(out, source + 1, l * sizeof(char));
|
||||||
else { /* add '...' before rest of name */
|
else { /* add '...' before rest of name */
|
||||||
addstr(out, RETS, LL(RETS));
|
addstr(out, RETS, LL(RETS));
|
||||||
bufflen -= LL(RETS);
|
bufflen -= LL(RETS);
|
||||||
memcpy(out, source + 1 + srclen - bufflen, bufflen * sizeof(char));
|
memcpy(out, source + 1 + l - bufflen, bufflen * sizeof(char));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
else { /* string; format as [string "source"] */
|
else { /* string; format as [string "source"] */
|
||||||
const char *nl = strchr(source, '\n'); /* find first new line (if any) */
|
const char *nl = strchr(source, '\n'); /* find first new line (if any) */
|
||||||
addstr(out, PRE, LL(PRE)); /* add prefix */
|
addstr(out, PRE, LL(PRE)); /* add prefix */
|
||||||
bufflen -= LL(PRE RETS POS) + 1; /* save space for prefix+suffix+'\0' */
|
bufflen -= LL(PRE RETS POS) + 1; /* save space for prefix+suffix+'\0' */
|
||||||
if (srclen < bufflen && nl == NULL) { /* small one-line source? */
|
if (l < bufflen && nl == NULL) { /* small one-line source? */
|
||||||
addstr(out, source, srclen); /* keep it */
|
addstr(out, source, l); /* keep it */
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
if (nl != NULL)
|
if (nl != NULL) l = nl - source; /* stop at first newline */
|
||||||
srclen = ct_diff2sz(nl - source); /* stop at first newline */
|
if (l > bufflen) l = bufflen;
|
||||||
if (srclen > bufflen) srclen = bufflen;
|
addstr(out, source, l);
|
||||||
addstr(out, source, srclen);
|
|
||||||
addstr(out, RETS, LL(RETS));
|
addstr(out, RETS, LL(RETS));
|
||||||
}
|
}
|
||||||
memcpy(out, POS, (LL(POS) + 1) * sizeof(char));
|
memcpy(out, POS, (LL(POS) + 1) * sizeof(char));
|
||||||
|
|||||||
+103
-119
@@ -1,5 +1,5 @@
|
|||||||
/*
|
/*
|
||||||
** $Id: lopcodes.c $
|
** $Id: lopcodes.c,v 1.55.1.1 2017/04/19 17:20:42 roberto Exp $
|
||||||
** Opcodes for Lua virtual machine
|
** Opcodes for Lua virtual machine
|
||||||
** See Copyright Notice in lua.h
|
** See Copyright Notice in lua.h
|
||||||
*/
|
*/
|
||||||
@@ -10,131 +10,115 @@
|
|||||||
#include "lprefix.h"
|
#include "lprefix.h"
|
||||||
|
|
||||||
|
|
||||||
|
#include <stddef.h>
|
||||||
|
|
||||||
#include "lopcodes.h"
|
#include "lopcodes.h"
|
||||||
|
|
||||||
|
|
||||||
#define opmode(mm,ot,it,t,a,m) \
|
|
||||||
(((mm) << 7) | ((ot) << 6) | ((it) << 5) | ((t) << 4) | ((a) << 3) | (m))
|
|
||||||
|
|
||||||
|
|
||||||
/* ORDER OP */
|
/* ORDER OP */
|
||||||
|
|
||||||
LUAI_DDEF const lu_byte luaP_opmodes[NUM_OPCODES] = {
|
LUAI_DDEF const char *const luaP_opnames[NUM_OPCODES+1] = {
|
||||||
/* MM OT IT T A mode opcode */
|
"MOVE",
|
||||||
opmode(0, 0, 0, 0, 1, iABC) /* OP_MOVE */
|
"LOADK",
|
||||||
,opmode(0, 0, 0, 0, 1, iAsBx) /* OP_LOADI */
|
"LOADKX",
|
||||||
,opmode(0, 0, 0, 0, 1, iAsBx) /* OP_LOADF */
|
"LOADBOOL",
|
||||||
,opmode(0, 0, 0, 0, 1, iABx) /* OP_LOADK */
|
"LOADNIL",
|
||||||
,opmode(0, 0, 0, 0, 1, iABx) /* OP_LOADKX */
|
"GETUPVAL",
|
||||||
,opmode(0, 0, 0, 0, 1, iABC) /* OP_LOADFALSE */
|
"GETTABUP",
|
||||||
,opmode(0, 0, 0, 0, 1, iABC) /* OP_LFALSESKIP */
|
"GETTABLE",
|
||||||
,opmode(0, 0, 0, 0, 1, iABC) /* OP_LOADTRUE */
|
"SETTABUP",
|
||||||
,opmode(0, 0, 0, 0, 1, iABC) /* OP_LOADNIL */
|
"SETUPVAL",
|
||||||
,opmode(0, 0, 0, 0, 1, iABC) /* OP_GETUPVAL */
|
"SETTABLE",
|
||||||
,opmode(0, 0, 0, 0, 0, iABC) /* OP_SETUPVAL */
|
"NEWTABLE",
|
||||||
,opmode(0, 0, 0, 0, 1, iABC) /* OP_GETTABUP */
|
"SELF",
|
||||||
,opmode(0, 0, 0, 0, 1, iABC) /* OP_GETTABLE */
|
"ADD",
|
||||||
,opmode(0, 0, 0, 0, 1, iABC) /* OP_GETI */
|
"SUB",
|
||||||
,opmode(0, 0, 0, 0, 1, iABC) /* OP_GETFIELD */
|
"MUL",
|
||||||
,opmode(0, 0, 0, 0, 0, iABC) /* OP_SETTABUP */
|
"MOD",
|
||||||
,opmode(0, 0, 0, 0, 0, iABC) /* OP_SETTABLE */
|
"POW",
|
||||||
,opmode(0, 0, 0, 0, 0, iABC) /* OP_SETI */
|
"DIV",
|
||||||
,opmode(0, 0, 0, 0, 0, iABC) /* OP_SETFIELD */
|
"IDIV",
|
||||||
,opmode(0, 0, 0, 0, 1, ivABC) /* OP_NEWTABLE */
|
"BAND",
|
||||||
,opmode(0, 0, 0, 0, 1, iABC) /* OP_SELF */
|
"BOR",
|
||||||
,opmode(0, 0, 0, 0, 1, iABC) /* OP_ADDI */
|
"BXOR",
|
||||||
,opmode(0, 0, 0, 0, 1, iABC) /* OP_ADDK */
|
"SHL",
|
||||||
,opmode(0, 0, 0, 0, 1, iABC) /* OP_SUBK */
|
"SHR",
|
||||||
,opmode(0, 0, 0, 0, 1, iABC) /* OP_MULK */
|
"UNM",
|
||||||
,opmode(0, 0, 0, 0, 1, iABC) /* OP_MODK */
|
"BNOT",
|
||||||
,opmode(0, 0, 0, 0, 1, iABC) /* OP_POWK */
|
"NOT",
|
||||||
,opmode(0, 0, 0, 0, 1, iABC) /* OP_DIVK */
|
"LEN",
|
||||||
,opmode(0, 0, 0, 0, 1, iABC) /* OP_IDIVK */
|
"CONCAT",
|
||||||
,opmode(0, 0, 0, 0, 1, iABC) /* OP_BANDK */
|
"JMP",
|
||||||
,opmode(0, 0, 0, 0, 1, iABC) /* OP_BORK */
|
"EQ",
|
||||||
,opmode(0, 0, 0, 0, 1, iABC) /* OP_BXORK */
|
"LT",
|
||||||
,opmode(0, 0, 0, 0, 1, iABC) /* OP_SHLI */
|
"LE",
|
||||||
,opmode(0, 0, 0, 0, 1, iABC) /* OP_SHRI */
|
"TEST",
|
||||||
,opmode(0, 0, 0, 0, 1, iABC) /* OP_ADD */
|
"TESTSET",
|
||||||
,opmode(0, 0, 0, 0, 1, iABC) /* OP_SUB */
|
"CALL",
|
||||||
,opmode(0, 0, 0, 0, 1, iABC) /* OP_MUL */
|
"TAILCALL",
|
||||||
,opmode(0, 0, 0, 0, 1, iABC) /* OP_MOD */
|
"RETURN",
|
||||||
,opmode(0, 0, 0, 0, 1, iABC) /* OP_POW */
|
"FORLOOP",
|
||||||
,opmode(0, 0, 0, 0, 1, iABC) /* OP_DIV */
|
"FORPREP",
|
||||||
,opmode(0, 0, 0, 0, 1, iABC) /* OP_IDIV */
|
"TFORCALL",
|
||||||
,opmode(0, 0, 0, 0, 1, iABC) /* OP_BAND */
|
"TFORLOOP",
|
||||||
,opmode(0, 0, 0, 0, 1, iABC) /* OP_BOR */
|
"SETLIST",
|
||||||
,opmode(0, 0, 0, 0, 1, iABC) /* OP_BXOR */
|
"CLOSURE",
|
||||||
,opmode(0, 0, 0, 0, 1, iABC) /* OP_SHL */
|
"VARARG",
|
||||||
,opmode(0, 0, 0, 0, 1, iABC) /* OP_SHR */
|
"EXTRAARG",
|
||||||
,opmode(1, 0, 0, 0, 0, iABC) /* OP_MMBIN */
|
NULL
|
||||||
,opmode(1, 0, 0, 0, 0, iABC) /* OP_MMBINI */
|
|
||||||
,opmode(1, 0, 0, 0, 0, iABC) /* OP_MMBINK */
|
|
||||||
,opmode(0, 0, 0, 0, 1, iABC) /* OP_UNM */
|
|
||||||
,opmode(0, 0, 0, 0, 1, iABC) /* OP_BNOT */
|
|
||||||
,opmode(0, 0, 0, 0, 1, iABC) /* OP_NOT */
|
|
||||||
,opmode(0, 0, 0, 0, 1, iABC) /* OP_LEN */
|
|
||||||
,opmode(0, 0, 0, 0, 1, iABC) /* OP_CONCAT */
|
|
||||||
,opmode(0, 0, 0, 0, 0, iABC) /* OP_CLOSE */
|
|
||||||
,opmode(0, 0, 0, 0, 0, iABC) /* OP_TBC */
|
|
||||||
,opmode(0, 0, 0, 0, 0, isJ) /* OP_JMP */
|
|
||||||
,opmode(0, 0, 0, 1, 0, iABC) /* OP_EQ */
|
|
||||||
,opmode(0, 0, 0, 1, 0, iABC) /* OP_LT */
|
|
||||||
,opmode(0, 0, 0, 1, 0, iABC) /* OP_LE */
|
|
||||||
,opmode(0, 0, 0, 1, 0, iABC) /* OP_EQK */
|
|
||||||
,opmode(0, 0, 0, 1, 0, iABC) /* OP_EQI */
|
|
||||||
,opmode(0, 0, 0, 1, 0, iABC) /* OP_LTI */
|
|
||||||
,opmode(0, 0, 0, 1, 0, iABC) /* OP_LEI */
|
|
||||||
,opmode(0, 0, 0, 1, 0, iABC) /* OP_GTI */
|
|
||||||
,opmode(0, 0, 0, 1, 0, iABC) /* OP_GEI */
|
|
||||||
,opmode(0, 0, 0, 1, 0, iABC) /* OP_TEST */
|
|
||||||
,opmode(0, 0, 0, 1, 1, iABC) /* OP_TESTSET */
|
|
||||||
,opmode(0, 1, 1, 0, 1, iABC) /* OP_CALL */
|
|
||||||
,opmode(0, 1, 1, 0, 1, iABC) /* OP_TAILCALL */
|
|
||||||
,opmode(0, 0, 1, 0, 0, iABC) /* OP_RETURN */
|
|
||||||
,opmode(0, 0, 0, 0, 0, iABC) /* OP_RETURN0 */
|
|
||||||
,opmode(0, 0, 0, 0, 0, iABC) /* OP_RETURN1 */
|
|
||||||
,opmode(0, 0, 0, 0, 1, iABx) /* OP_FORLOOP */
|
|
||||||
,opmode(0, 0, 0, 0, 1, iABx) /* OP_FORPREP */
|
|
||||||
,opmode(0, 0, 0, 0, 0, iABx) /* OP_TFORPREP */
|
|
||||||
,opmode(0, 0, 0, 0, 0, iABC) /* OP_TFORCALL */
|
|
||||||
,opmode(0, 0, 0, 0, 1, iABx) /* OP_TFORLOOP */
|
|
||||||
,opmode(0, 0, 1, 0, 0, ivABC) /* OP_SETLIST */
|
|
||||||
,opmode(0, 0, 0, 0, 1, iABx) /* OP_CLOSURE */
|
|
||||||
,opmode(0, 1, 0, 0, 1, iABC) /* OP_VARARG */
|
|
||||||
,opmode(0, 0, 0, 0, 1, iABC) /* OP_GETVARG */
|
|
||||||
,opmode(0, 0, 0, 0, 0, iABx) /* OP_ERRNNIL */
|
|
||||||
,opmode(0, 0, 1, 0, 1, iABC) /* OP_VARARGPREP */
|
|
||||||
,opmode(0, 0, 0, 0, 0, iAx) /* OP_EXTRAARG */
|
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
|
#define opmode(t,a,b,c,m) (((t)<<7) | ((a)<<6) | ((b)<<4) | ((c)<<2) | (m))
|
||||||
|
|
||||||
/*
|
LUAI_DDEF const lu_byte luaP_opmodes[NUM_OPCODES] = {
|
||||||
** Check whether instruction sets top for next instruction, that is,
|
/* T A B C mode opcode */
|
||||||
** it results in multiple values.
|
opmode(0, 1, OpArgR, OpArgN, iABC) /* OP_MOVE */
|
||||||
*/
|
,opmode(0, 1, OpArgK, OpArgN, iABx) /* OP_LOADK */
|
||||||
int luaP_isOT (Instruction i) {
|
,opmode(0, 1, OpArgN, OpArgN, iABx) /* OP_LOADKX */
|
||||||
OpCode op = GET_OPCODE(i);
|
,opmode(0, 1, OpArgU, OpArgU, iABC) /* OP_LOADBOOL */
|
||||||
switch (op) {
|
,opmode(0, 1, OpArgU, OpArgN, iABC) /* OP_LOADNIL */
|
||||||
case OP_TAILCALL: return 1;
|
,opmode(0, 1, OpArgU, OpArgN, iABC) /* OP_GETUPVAL */
|
||||||
default:
|
,opmode(0, 1, OpArgU, OpArgK, iABC) /* OP_GETTABUP */
|
||||||
return testOTMode(op) && GETARG_C(i) == 0;
|
,opmode(0, 1, OpArgR, OpArgK, iABC) /* OP_GETTABLE */
|
||||||
}
|
,opmode(0, 0, OpArgK, OpArgK, iABC) /* OP_SETTABUP */
|
||||||
}
|
,opmode(0, 0, OpArgU, OpArgN, iABC) /* OP_SETUPVAL */
|
||||||
|
,opmode(0, 0, OpArgK, OpArgK, iABC) /* OP_SETTABLE */
|
||||||
|
,opmode(0, 1, OpArgU, OpArgU, iABC) /* OP_NEWTABLE */
|
||||||
/*
|
,opmode(0, 1, OpArgR, OpArgK, iABC) /* OP_SELF */
|
||||||
** Check whether instruction uses top from previous instruction, that is,
|
,opmode(0, 1, OpArgK, OpArgK, iABC) /* OP_ADD */
|
||||||
** it accepts multiple results.
|
,opmode(0, 1, OpArgK, OpArgK, iABC) /* OP_SUB */
|
||||||
*/
|
,opmode(0, 1, OpArgK, OpArgK, iABC) /* OP_MUL */
|
||||||
int luaP_isIT (Instruction i) {
|
,opmode(0, 1, OpArgK, OpArgK, iABC) /* OP_MOD */
|
||||||
OpCode op = GET_OPCODE(i);
|
,opmode(0, 1, OpArgK, OpArgK, iABC) /* OP_POW */
|
||||||
switch (op) {
|
,opmode(0, 1, OpArgK, OpArgK, iABC) /* OP_DIV */
|
||||||
case OP_SETLIST:
|
,opmode(0, 1, OpArgK, OpArgK, iABC) /* OP_IDIV */
|
||||||
return testITMode(GET_OPCODE(i)) && GETARG_vB(i) == 0;
|
,opmode(0, 1, OpArgK, OpArgK, iABC) /* OP_BAND */
|
||||||
default:
|
,opmode(0, 1, OpArgK, OpArgK, iABC) /* OP_BOR */
|
||||||
return testITMode(GET_OPCODE(i)) && GETARG_B(i) == 0;
|
,opmode(0, 1, OpArgK, OpArgK, iABC) /* OP_BXOR */
|
||||||
}
|
,opmode(0, 1, OpArgK, OpArgK, iABC) /* OP_SHL */
|
||||||
}
|
,opmode(0, 1, OpArgK, OpArgK, iABC) /* OP_SHR */
|
||||||
|
,opmode(0, 1, OpArgR, OpArgN, iABC) /* OP_UNM */
|
||||||
|
,opmode(0, 1, OpArgR, OpArgN, iABC) /* OP_BNOT */
|
||||||
|
,opmode(0, 1, OpArgR, OpArgN, iABC) /* OP_NOT */
|
||||||
|
,opmode(0, 1, OpArgR, OpArgN, iABC) /* OP_LEN */
|
||||||
|
,opmode(0, 1, OpArgR, OpArgR, iABC) /* OP_CONCAT */
|
||||||
|
,opmode(0, 0, OpArgR, OpArgN, iAsBx) /* OP_JMP */
|
||||||
|
,opmode(1, 0, OpArgK, OpArgK, iABC) /* OP_EQ */
|
||||||
|
,opmode(1, 0, OpArgK, OpArgK, iABC) /* OP_LT */
|
||||||
|
,opmode(1, 0, OpArgK, OpArgK, iABC) /* OP_LE */
|
||||||
|
,opmode(1, 0, OpArgN, OpArgU, iABC) /* OP_TEST */
|
||||||
|
,opmode(1, 1, OpArgR, OpArgU, iABC) /* OP_TESTSET */
|
||||||
|
,opmode(0, 1, OpArgU, OpArgU, iABC) /* OP_CALL */
|
||||||
|
,opmode(0, 1, OpArgU, OpArgU, iABC) /* OP_TAILCALL */
|
||||||
|
,opmode(0, 0, OpArgU, OpArgN, iABC) /* OP_RETURN */
|
||||||
|
,opmode(0, 1, OpArgR, OpArgN, iAsBx) /* OP_FORLOOP */
|
||||||
|
,opmode(0, 1, OpArgR, OpArgN, iAsBx) /* OP_FORPREP */
|
||||||
|
,opmode(0, 0, OpArgN, OpArgU, iABC) /* OP_TFORCALL */
|
||||||
|
,opmode(0, 1, OpArgR, OpArgN, iAsBx) /* OP_TFORLOOP */
|
||||||
|
,opmode(0, 0, OpArgU, OpArgU, iABC) /* OP_SETLIST */
|
||||||
|
,opmode(0, 1, OpArgU, OpArgN, iABx) /* OP_CLOSURE */
|
||||||
|
,opmode(0, 1, OpArgU, OpArgN, iABC) /* OP_VARARG */
|
||||||
|
,opmode(0, 0, OpArgU, OpArgU, iAx) /* OP_EXTRAARG */
|
||||||
|
};
|
||||||
|
|
||||||
|
|||||||
+157
-299
@@ -1,5 +1,5 @@
|
|||||||
/*
|
/*
|
||||||
** $Id: lopcodes.h $
|
** $Id: lopcodes.h,v 1.149.1.1 2017/04/19 17:20:42 roberto Exp $
|
||||||
** Opcodes for Lua virtual machine
|
** Opcodes for Lua virtual machine
|
||||||
** See Copyright Notice in lua.h
|
** See Copyright Notice in lua.h
|
||||||
*/
|
*/
|
||||||
@@ -8,110 +8,72 @@
|
|||||||
#define lopcodes_h
|
#define lopcodes_h
|
||||||
|
|
||||||
#include "llimits.h"
|
#include "llimits.h"
|
||||||
#include "lobject.h"
|
|
||||||
|
|
||||||
|
|
||||||
/*===========================================================================
|
/*===========================================================================
|
||||||
We assume that instructions are unsigned 32-bit integers.
|
We assume that instructions are unsigned numbers.
|
||||||
All instructions have an opcode in the first 7 bits.
|
All instructions have an opcode in the first 6 bits.
|
||||||
Instructions can have the following formats:
|
Instructions can have the following fields:
|
||||||
|
'A' : 8 bits
|
||||||
|
'B' : 9 bits
|
||||||
|
'C' : 9 bits
|
||||||
|
'Ax' : 26 bits ('A', 'B', and 'C' together)
|
||||||
|
'Bx' : 18 bits ('B' and 'C' together)
|
||||||
|
'sBx' : signed Bx
|
||||||
|
|
||||||
3 3 2 2 2 2 2 2 2 2 2 2 1 1 1 1 1 1 1 1 1 1 0 0 0 0 0 0 0 0 0 0
|
A signed argument is represented in excess K; that is, the number
|
||||||
1 0 9 8 7 6 5 4 3 2 1 0 9 8 7 6 5 4 3 2 1 0 9 8 7 6 5 4 3 2 1 0
|
value is the unsigned value minus K. K is exactly the maximum value
|
||||||
iABC C(8) | B(8) |k| A(8) | Op(7) |
|
for that argument (so that -max is represented by 0, and +max is
|
||||||
ivABC vC(10) | vB(6) |k| A(8) | Op(7) |
|
represented by 2*max), which is half the maximum for the corresponding
|
||||||
iABx Bx(17) | A(8) | Op(7) |
|
unsigned argument.
|
||||||
iAsBx sBx (signed)(17) | A(8) | Op(7) |
|
|
||||||
iAx Ax(25) | Op(7) |
|
|
||||||
isJ sJ (signed)(25) | Op(7) |
|
|
||||||
|
|
||||||
('v' stands for "variant", 's' for "signed", 'x' for "extended".)
|
|
||||||
A signed argument is represented in excess K: The represented value is
|
|
||||||
the written unsigned value minus K, where K is half (rounded down) the
|
|
||||||
maximum value for the corresponding unsigned argument.
|
|
||||||
===========================================================================*/
|
===========================================================================*/
|
||||||
|
|
||||||
|
|
||||||
/* basic instruction formats */
|
enum OpMode {iABC, iABx, iAsBx, iAx}; /* basic instruction format */
|
||||||
enum OpMode {iABC, ivABC, iABx, iAsBx, iAx, isJ};
|
|
||||||
|
|
||||||
|
|
||||||
/*
|
/*
|
||||||
** size and position of opcode arguments.
|
** size and position of opcode arguments.
|
||||||
*/
|
*/
|
||||||
#define SIZE_C 8
|
#define SIZE_C 9
|
||||||
#define SIZE_vC 10
|
#define SIZE_B 9
|
||||||
#define SIZE_B 8
|
#define SIZE_Bx (SIZE_C + SIZE_B)
|
||||||
#define SIZE_vB 6
|
|
||||||
#define SIZE_Bx (SIZE_C + SIZE_B + 1)
|
|
||||||
#define SIZE_A 8
|
#define SIZE_A 8
|
||||||
#define SIZE_Ax (SIZE_Bx + SIZE_A)
|
#define SIZE_Ax (SIZE_C + SIZE_B + SIZE_A)
|
||||||
#define SIZE_sJ (SIZE_Bx + SIZE_A)
|
|
||||||
|
|
||||||
#define SIZE_OP 7
|
#define SIZE_OP 6
|
||||||
|
|
||||||
#define POS_OP 0
|
#define POS_OP 0
|
||||||
|
|
||||||
#define POS_A (POS_OP + SIZE_OP)
|
#define POS_A (POS_OP + SIZE_OP)
|
||||||
#define POS_k (POS_A + SIZE_A)
|
#define POS_C (POS_A + SIZE_A)
|
||||||
#define POS_B (POS_k + 1)
|
#define POS_B (POS_C + SIZE_C)
|
||||||
#define POS_vB (POS_k + 1)
|
#define POS_Bx POS_C
|
||||||
#define POS_C (POS_B + SIZE_B)
|
|
||||||
#define POS_vC (POS_vB + SIZE_vB)
|
|
||||||
|
|
||||||
#define POS_Bx POS_k
|
|
||||||
|
|
||||||
#define POS_Ax POS_A
|
#define POS_Ax POS_A
|
||||||
|
|
||||||
#define POS_sJ POS_A
|
|
||||||
|
|
||||||
|
|
||||||
/*
|
/*
|
||||||
** limits for opcode arguments.
|
** limits for opcode arguments.
|
||||||
** we use (signed) 'int' to manipulate most arguments,
|
** we use (signed) int to manipulate most arguments,
|
||||||
** so they must fit in ints.
|
** so they must fit in LUAI_BITSINT-1 bits (-1 for sign)
|
||||||
*/
|
*/
|
||||||
|
#if SIZE_Bx < LUAI_BITSINT-1
|
||||||
/*
|
#define MAXARG_Bx ((1<<SIZE_Bx)-1)
|
||||||
** Check whether type 'int' has at least 'b' + 1 bits.
|
#define MAXARG_sBx (MAXARG_Bx>>1) /* 'sBx' is signed */
|
||||||
** 'b' < 32; +1 for the sign bit.
|
|
||||||
*/
|
|
||||||
#define L_INTHASBITS(b) ((UINT_MAX >> (b)) >= 1)
|
|
||||||
|
|
||||||
|
|
||||||
#if L_INTHASBITS(SIZE_Bx)
|
|
||||||
#define MAXARG_Bx ((1<<SIZE_Bx)-1)
|
|
||||||
#else
|
#else
|
||||||
#define MAXARG_Bx INT_MAX
|
#define MAXARG_Bx MAX_INT
|
||||||
|
#define MAXARG_sBx MAX_INT
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
#define OFFSET_sBx (MAXARG_Bx>>1) /* 'sBx' is signed */
|
#if SIZE_Ax < LUAI_BITSINT-1
|
||||||
|
|
||||||
|
|
||||||
#if L_INTHASBITS(SIZE_Ax)
|
|
||||||
#define MAXARG_Ax ((1<<SIZE_Ax)-1)
|
#define MAXARG_Ax ((1<<SIZE_Ax)-1)
|
||||||
#else
|
#else
|
||||||
#define MAXARG_Ax INT_MAX
|
#define MAXARG_Ax MAX_INT
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
#if L_INTHASBITS(SIZE_sJ)
|
|
||||||
#define MAXARG_sJ ((1 << SIZE_sJ) - 1)
|
|
||||||
#else
|
|
||||||
#define MAXARG_sJ INT_MAX
|
|
||||||
#endif
|
|
||||||
|
|
||||||
#define OFFSET_sJ (MAXARG_sJ >> 1)
|
#define MAXARG_A ((1<<SIZE_A)-1)
|
||||||
|
#define MAXARG_B ((1<<SIZE_B)-1)
|
||||||
|
#define MAXARG_C ((1<<SIZE_C)-1)
|
||||||
#define MAXARG_A ((1<<SIZE_A)-1)
|
|
||||||
#define MAXARG_B ((1<<SIZE_B)-1)
|
|
||||||
#define MAXARG_vB ((1<<SIZE_vB)-1)
|
|
||||||
#define MAXARG_C ((1<<SIZE_C)-1)
|
|
||||||
#define MAXARG_vC ((1<<SIZE_vC)-1)
|
|
||||||
#define OFFSET_sC (MAXARG_C >> 1)
|
|
||||||
|
|
||||||
#define int2sC(i) ((i) + OFFSET_sC)
|
|
||||||
#define sC2int(i) ((i) - OFFSET_sC)
|
|
||||||
|
|
||||||
|
|
||||||
/* creates a mask with 'n' 1 bits at position 'p' */
|
/* creates a mask with 'n' 1 bits at position 'p' */
|
||||||
@@ -126,314 +88,210 @@ enum OpMode {iABC, ivABC, iABx, iAsBx, iAx, isJ};
|
|||||||
|
|
||||||
#define GET_OPCODE(i) (cast(OpCode, ((i)>>POS_OP) & MASK1(SIZE_OP,0)))
|
#define GET_OPCODE(i) (cast(OpCode, ((i)>>POS_OP) & MASK1(SIZE_OP,0)))
|
||||||
#define SET_OPCODE(i,o) ((i) = (((i)&MASK0(SIZE_OP,POS_OP)) | \
|
#define SET_OPCODE(i,o) ((i) = (((i)&MASK0(SIZE_OP,POS_OP)) | \
|
||||||
((cast_Inst(o)<<POS_OP)&MASK1(SIZE_OP,POS_OP))))
|
((cast(Instruction, o)<<POS_OP)&MASK1(SIZE_OP,POS_OP))))
|
||||||
|
|
||||||
#define checkopm(i,m) (getOpMode(GET_OPCODE(i)) == m)
|
#define getarg(i,pos,size) (cast(int, ((i)>>pos) & MASK1(size,0)))
|
||||||
|
|
||||||
|
|
||||||
#define getarg(i,pos,size) (cast_int(((i)>>(pos)) & MASK1(size,0)))
|
|
||||||
#define setarg(i,v,pos,size) ((i) = (((i)&MASK0(size,pos)) | \
|
#define setarg(i,v,pos,size) ((i) = (((i)&MASK0(size,pos)) | \
|
||||||
((cast_Inst(v)<<pos)&MASK1(size,pos))))
|
((cast(Instruction, v)<<pos)&MASK1(size,pos))))
|
||||||
|
|
||||||
#define GETARG_A(i) getarg(i, POS_A, SIZE_A)
|
#define GETARG_A(i) getarg(i, POS_A, SIZE_A)
|
||||||
#define SETARG_A(i,v) setarg(i, v, POS_A, SIZE_A)
|
#define SETARG_A(i,v) setarg(i, v, POS_A, SIZE_A)
|
||||||
|
|
||||||
#define GETARG_B(i) \
|
#define GETARG_B(i) getarg(i, POS_B, SIZE_B)
|
||||||
check_exp(checkopm(i, iABC), getarg(i, POS_B, SIZE_B))
|
|
||||||
#define GETARG_vB(i) \
|
|
||||||
check_exp(checkopm(i, ivABC), getarg(i, POS_vB, SIZE_vB))
|
|
||||||
#define GETARG_sB(i) sC2int(GETARG_B(i))
|
|
||||||
#define SETARG_B(i,v) setarg(i, v, POS_B, SIZE_B)
|
#define SETARG_B(i,v) setarg(i, v, POS_B, SIZE_B)
|
||||||
#define SETARG_vB(i,v) setarg(i, v, POS_vB, SIZE_vB)
|
|
||||||
|
|
||||||
#define GETARG_C(i) \
|
#define GETARG_C(i) getarg(i, POS_C, SIZE_C)
|
||||||
check_exp(checkopm(i, iABC), getarg(i, POS_C, SIZE_C))
|
|
||||||
#define GETARG_vC(i) \
|
|
||||||
check_exp(checkopm(i, ivABC), getarg(i, POS_vC, SIZE_vC))
|
|
||||||
#define GETARG_sC(i) sC2int(GETARG_C(i))
|
|
||||||
#define SETARG_C(i,v) setarg(i, v, POS_C, SIZE_C)
|
#define SETARG_C(i,v) setarg(i, v, POS_C, SIZE_C)
|
||||||
#define SETARG_vC(i,v) setarg(i, v, POS_vC, SIZE_vC)
|
|
||||||
|
|
||||||
#define TESTARG_k(i) (cast_int(((i) & (1u << POS_k))))
|
#define GETARG_Bx(i) getarg(i, POS_Bx, SIZE_Bx)
|
||||||
#define GETARG_k(i) getarg(i, POS_k, 1)
|
|
||||||
#define SETARG_k(i,v) setarg(i, v, POS_k, 1)
|
|
||||||
|
|
||||||
#define GETARG_Bx(i) check_exp(checkopm(i, iABx), getarg(i, POS_Bx, SIZE_Bx))
|
|
||||||
#define SETARG_Bx(i,v) setarg(i, v, POS_Bx, SIZE_Bx)
|
#define SETARG_Bx(i,v) setarg(i, v, POS_Bx, SIZE_Bx)
|
||||||
|
|
||||||
#define GETARG_Ax(i) check_exp(checkopm(i, iAx), getarg(i, POS_Ax, SIZE_Ax))
|
#define GETARG_Ax(i) getarg(i, POS_Ax, SIZE_Ax)
|
||||||
#define SETARG_Ax(i,v) setarg(i, v, POS_Ax, SIZE_Ax)
|
#define SETARG_Ax(i,v) setarg(i, v, POS_Ax, SIZE_Ax)
|
||||||
|
|
||||||
#define GETARG_sBx(i) \
|
#define GETARG_sBx(i) (GETARG_Bx(i)-MAXARG_sBx)
|
||||||
check_exp(checkopm(i, iAsBx), getarg(i, POS_Bx, SIZE_Bx) - OFFSET_sBx)
|
#define SETARG_sBx(i,b) SETARG_Bx((i),cast(unsigned int, (b)+MAXARG_sBx))
|
||||||
#define SETARG_sBx(i,b) SETARG_Bx((i),cast_uint((b)+OFFSET_sBx))
|
|
||||||
|
|
||||||
#define GETARG_sJ(i) \
|
|
||||||
check_exp(checkopm(i, isJ), getarg(i, POS_sJ, SIZE_sJ) - OFFSET_sJ)
|
|
||||||
#define SETARG_sJ(i,j) \
|
|
||||||
setarg(i, cast_uint((j)+OFFSET_sJ), POS_sJ, SIZE_sJ)
|
|
||||||
|
|
||||||
|
|
||||||
#define CREATE_ABCk(o,a,b,c,k) ((cast_Inst(o)<<POS_OP) \
|
#define CREATE_ABC(o,a,b,c) ((cast(Instruction, o)<<POS_OP) \
|
||||||
| (cast_Inst(a)<<POS_A) \
|
| (cast(Instruction, a)<<POS_A) \
|
||||||
| (cast_Inst(b)<<POS_B) \
|
| (cast(Instruction, b)<<POS_B) \
|
||||||
| (cast_Inst(c)<<POS_C) \
|
| (cast(Instruction, c)<<POS_C))
|
||||||
| (cast_Inst(k)<<POS_k))
|
|
||||||
|
|
||||||
#define CREATE_vABCk(o,a,b,c,k) ((cast_Inst(o)<<POS_OP) \
|
#define CREATE_ABx(o,a,bc) ((cast(Instruction, o)<<POS_OP) \
|
||||||
| (cast_Inst(a)<<POS_A) \
|
| (cast(Instruction, a)<<POS_A) \
|
||||||
| (cast_Inst(b)<<POS_vB) \
|
| (cast(Instruction, bc)<<POS_Bx))
|
||||||
| (cast_Inst(c)<<POS_vC) \
|
|
||||||
| (cast_Inst(k)<<POS_k))
|
|
||||||
|
|
||||||
#define CREATE_ABx(o,a,bc) ((cast_Inst(o)<<POS_OP) \
|
#define CREATE_Ax(o,a) ((cast(Instruction, o)<<POS_OP) \
|
||||||
| (cast_Inst(a)<<POS_A) \
|
| (cast(Instruction, a)<<POS_Ax))
|
||||||
| (cast_Inst(bc)<<POS_Bx))
|
|
||||||
|
|
||||||
#define CREATE_Ax(o,a) ((cast_Inst(o)<<POS_OP) \
|
|
||||||
| (cast_Inst(a)<<POS_Ax))
|
|
||||||
|
|
||||||
#define CREATE_sJ(o,j,k) ((cast_Inst(o) << POS_OP) \
|
/*
|
||||||
| (cast_Inst(j) << POS_sJ) \
|
** Macros to operate RK indices
|
||||||
| (cast_Inst(k) << POS_k))
|
*/
|
||||||
|
|
||||||
|
/* this bit 1 means constant (0 means register) */
|
||||||
|
#define BITRK (1 << (SIZE_B - 1))
|
||||||
|
|
||||||
|
/* test whether value is a constant */
|
||||||
|
#define ISK(x) ((x) & BITRK)
|
||||||
|
|
||||||
|
/* gets the index of the constant */
|
||||||
|
#define INDEXK(r) ((int)(r) & ~BITRK)
|
||||||
|
|
||||||
#if !defined(MAXINDEXRK) /* (for debugging only) */
|
#if !defined(MAXINDEXRK) /* (for debugging only) */
|
||||||
#define MAXINDEXRK MAXARG_B
|
#define MAXINDEXRK (BITRK - 1)
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
/* code a constant index as a RK value */
|
||||||
|
#define RKASK(x) ((x) | BITRK)
|
||||||
|
|
||||||
|
|
||||||
/*
|
/*
|
||||||
** Maximum size for the stack of a Lua function. It must fit in 8 bits.
|
** invalid register that fits in 8 bits
|
||||||
** The highest valid register is one less than this value.
|
|
||||||
*/
|
*/
|
||||||
#define MAX_FSTACK MAXARG_A
|
#define NO_REG MAXARG_A
|
||||||
|
|
||||||
/*
|
|
||||||
** Invalid register (one more than last valid register).
|
|
||||||
*/
|
|
||||||
#define NO_REG MAX_FSTACK
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
/*
|
/*
|
||||||
** R[x] - register
|
** R(x) - register
|
||||||
** K[x] - constant (in constant table)
|
** Kst(x) - constant (in constant table)
|
||||||
** RK(x) == if k(i) then K[x] else R[x]
|
** RK(x) == if ISK(x) then Kst(INDEXK(x)) else R(x)
|
||||||
*/
|
*/
|
||||||
|
|
||||||
|
|
||||||
/*
|
/*
|
||||||
** Grep "ORDER OP" if you change this enum.
|
** grep "ORDER OP" if you change these enums
|
||||||
** See "Notes" below for more information about some instructions.
|
|
||||||
*/
|
*/
|
||||||
|
|
||||||
typedef enum {
|
typedef enum {
|
||||||
/*----------------------------------------------------------------------
|
/*----------------------------------------------------------------------
|
||||||
name args description
|
name args description
|
||||||
------------------------------------------------------------------------*/
|
------------------------------------------------------------------------*/
|
||||||
OP_MOVE,/* A B R[A] := R[B] */
|
OP_MOVE,/* A B R(A) := R(B) */
|
||||||
OP_LOADI,/* A sBx R[A] := sBx */
|
OP_LOADK,/* A Bx R(A) := Kst(Bx) */
|
||||||
OP_LOADF,/* A sBx R[A] := (lua_Number)sBx */
|
OP_LOADKX,/* A R(A) := Kst(extra arg) */
|
||||||
OP_LOADK,/* A Bx R[A] := K[Bx] */
|
OP_LOADBOOL,/* A B C R(A) := (Bool)B; if (C) pc++ */
|
||||||
OP_LOADKX,/* A R[A] := K[extra arg] */
|
OP_LOADNIL,/* A B R(A), R(A+1), ..., R(A+B) := nil */
|
||||||
OP_LOADFALSE,/* A R[A] := false */
|
OP_GETUPVAL,/* A B R(A) := UpValue[B] */
|
||||||
OP_LFALSESKIP,/*A R[A] := false; pc++ */
|
|
||||||
OP_LOADTRUE,/* A R[A] := true */
|
|
||||||
OP_LOADNIL,/* A B R[A], R[A+1], ..., R[A+B] := nil */
|
|
||||||
OP_GETUPVAL,/* A B R[A] := UpValue[B] */
|
|
||||||
OP_SETUPVAL,/* A B UpValue[B] := R[A] */
|
|
||||||
|
|
||||||
OP_GETTABUP,/* A B C R[A] := UpValue[B][K[C]:shortstring] */
|
OP_GETTABUP,/* A B C R(A) := UpValue[B][RK(C)] */
|
||||||
OP_GETTABLE,/* A B C R[A] := R[B][R[C]] */
|
OP_GETTABLE,/* A B C R(A) := R(B)[RK(C)] */
|
||||||
OP_GETI,/* A B C R[A] := R[B][C] */
|
|
||||||
OP_GETFIELD,/* A B C R[A] := R[B][K[C]:shortstring] */
|
|
||||||
|
|
||||||
OP_SETTABUP,/* A B C UpValue[A][K[B]:shortstring] := RK(C) */
|
OP_SETTABUP,/* A B C UpValue[A][RK(B)] := RK(C) */
|
||||||
OP_SETTABLE,/* A B C R[A][R[B]] := RK(C) */
|
OP_SETUPVAL,/* A B UpValue[B] := R(A) */
|
||||||
OP_SETI,/* A B C R[A][B] := RK(C) */
|
OP_SETTABLE,/* A B C R(A)[RK(B)] := RK(C) */
|
||||||
OP_SETFIELD,/* A B C R[A][K[B]:shortstring] := RK(C) */
|
|
||||||
|
|
||||||
OP_NEWTABLE,/* A vB vC k R[A] := {} */
|
OP_NEWTABLE,/* A B C R(A) := {} (size = B,C) */
|
||||||
|
|
||||||
OP_SELF,/* A B C R[A+1] := R[B]; R[A] := R[B][K[C]:shortstring] */
|
OP_SELF,/* A B C R(A+1) := R(B); R(A) := R(B)[RK(C)] */
|
||||||
|
|
||||||
OP_ADDI,/* A B sC R[A] := R[B] + sC */
|
OP_ADD,/* A B C R(A) := RK(B) + RK(C) */
|
||||||
|
OP_SUB,/* A B C R(A) := RK(B) - RK(C) */
|
||||||
|
OP_MUL,/* A B C R(A) := RK(B) * RK(C) */
|
||||||
|
OP_MOD,/* A B C R(A) := RK(B) % RK(C) */
|
||||||
|
OP_POW,/* A B C R(A) := RK(B) ^ RK(C) */
|
||||||
|
OP_DIV,/* A B C R(A) := RK(B) / RK(C) */
|
||||||
|
OP_IDIV,/* A B C R(A) := RK(B) // RK(C) */
|
||||||
|
OP_BAND,/* A B C R(A) := RK(B) & RK(C) */
|
||||||
|
OP_BOR,/* A B C R(A) := RK(B) | RK(C) */
|
||||||
|
OP_BXOR,/* A B C R(A) := RK(B) ~ RK(C) */
|
||||||
|
OP_SHL,/* A B C R(A) := RK(B) << RK(C) */
|
||||||
|
OP_SHR,/* A B C R(A) := RK(B) >> RK(C) */
|
||||||
|
OP_UNM,/* A B R(A) := -R(B) */
|
||||||
|
OP_BNOT,/* A B R(A) := ~R(B) */
|
||||||
|
OP_NOT,/* A B R(A) := not R(B) */
|
||||||
|
OP_LEN,/* A B R(A) := length of R(B) */
|
||||||
|
|
||||||
OP_ADDK,/* A B C R[A] := R[B] + K[C]:number */
|
OP_CONCAT,/* A B C R(A) := R(B).. ... ..R(C) */
|
||||||
OP_SUBK,/* A B C R[A] := R[B] - K[C]:number */
|
|
||||||
OP_MULK,/* A B C R[A] := R[B] * K[C]:number */
|
|
||||||
OP_MODK,/* A B C R[A] := R[B] % K[C]:number */
|
|
||||||
OP_POWK,/* A B C R[A] := R[B] ^ K[C]:number */
|
|
||||||
OP_DIVK,/* A B C R[A] := R[B] / K[C]:number */
|
|
||||||
OP_IDIVK,/* A B C R[A] := R[B] // K[C]:number */
|
|
||||||
|
|
||||||
OP_BANDK,/* A B C R[A] := R[B] & K[C]:integer */
|
OP_JMP,/* A sBx pc+=sBx; if (A) close all upvalues >= R(A - 1) */
|
||||||
OP_BORK,/* A B C R[A] := R[B] | K[C]:integer */
|
OP_EQ,/* A B C if ((RK(B) == RK(C)) ~= A) then pc++ */
|
||||||
OP_BXORK,/* A B C R[A] := R[B] ~ K[C]:integer */
|
OP_LT,/* A B C if ((RK(B) < RK(C)) ~= A) then pc++ */
|
||||||
|
OP_LE,/* A B C if ((RK(B) <= RK(C)) ~= A) then pc++ */
|
||||||
|
|
||||||
OP_SHLI,/* A B sC R[A] := sC << R[B] */
|
OP_TEST,/* A C if not (R(A) <=> C) then pc++ */
|
||||||
OP_SHRI,/* A B sC R[A] := R[B] >> sC */
|
OP_TESTSET,/* A B C if (R(B) <=> C) then R(A) := R(B) else pc++ */
|
||||||
|
|
||||||
OP_ADD,/* A B C R[A] := R[B] + R[C] */
|
OP_CALL,/* A B C R(A), ... ,R(A+C-2) := R(A)(R(A+1), ... ,R(A+B-1)) */
|
||||||
OP_SUB,/* A B C R[A] := R[B] - R[C] */
|
OP_TAILCALL,/* A B C return R(A)(R(A+1), ... ,R(A+B-1)) */
|
||||||
OP_MUL,/* A B C R[A] := R[B] * R[C] */
|
OP_RETURN,/* A B return R(A), ... ,R(A+B-2) (see note) */
|
||||||
OP_MOD,/* A B C R[A] := R[B] % R[C] */
|
|
||||||
OP_POW,/* A B C R[A] := R[B] ^ R[C] */
|
|
||||||
OP_DIV,/* A B C R[A] := R[B] / R[C] */
|
|
||||||
OP_IDIV,/* A B C R[A] := R[B] // R[C] */
|
|
||||||
|
|
||||||
OP_BAND,/* A B C R[A] := R[B] & R[C] */
|
OP_FORLOOP,/* A sBx R(A)+=R(A+2);
|
||||||
OP_BOR,/* A B C R[A] := R[B] | R[C] */
|
if R(A) <?= R(A+1) then { pc+=sBx; R(A+3)=R(A) }*/
|
||||||
OP_BXOR,/* A B C R[A] := R[B] ~ R[C] */
|
OP_FORPREP,/* A sBx R(A)-=R(A+2); pc+=sBx */
|
||||||
OP_SHL,/* A B C R[A] := R[B] << R[C] */
|
|
||||||
OP_SHR,/* A B C R[A] := R[B] >> R[C] */
|
|
||||||
|
|
||||||
OP_MMBIN,/* A B C call C metamethod over R[A] and R[B] */
|
OP_TFORCALL,/* A C R(A+3), ... ,R(A+2+C) := R(A)(R(A+1), R(A+2)); */
|
||||||
OP_MMBINI,/* A sB C k call C metamethod over R[A] and sB */
|
OP_TFORLOOP,/* A sBx if R(A+1) ~= nil then { R(A)=R(A+1); pc += sBx }*/
|
||||||
OP_MMBINK,/* A B C k call C metamethod over R[A] and K[B] */
|
|
||||||
|
|
||||||
OP_UNM,/* A B R[A] := -R[B] */
|
OP_SETLIST,/* A B C R(A)[(C-1)*FPF+i] := R(A+i), 1 <= i <= B */
|
||||||
OP_BNOT,/* A B R[A] := ~R[B] */
|
|
||||||
OP_NOT,/* A B R[A] := not R[B] */
|
|
||||||
OP_LEN,/* A B R[A] := #R[B] (length operator) */
|
|
||||||
|
|
||||||
OP_CONCAT,/* A B R[A] := R[A].. ... ..R[A + B - 1] */
|
OP_CLOSURE,/* A Bx R(A) := closure(KPROTO[Bx]) */
|
||||||
|
|
||||||
OP_CLOSE,/* A close all upvalues >= R[A] */
|
OP_VARARG,/* A B R(A), R(A+1), ..., R(A+B-2) = vararg */
|
||||||
OP_TBC,/* A mark variable A "to be closed" */
|
|
||||||
OP_JMP,/* sJ pc += sJ */
|
|
||||||
OP_EQ,/* A B k if ((R[A] == R[B]) ~= k) then pc++ */
|
|
||||||
OP_LT,/* A B k if ((R[A] < R[B]) ~= k) then pc++ */
|
|
||||||
OP_LE,/* A B k if ((R[A] <= R[B]) ~= k) then pc++ */
|
|
||||||
|
|
||||||
OP_EQK,/* A B k if ((R[A] == K[B]) ~= k) then pc++ */
|
|
||||||
OP_EQI,/* A sB k if ((R[A] == sB) ~= k) then pc++ */
|
|
||||||
OP_LTI,/* A sB k if ((R[A] < sB) ~= k) then pc++ */
|
|
||||||
OP_LEI,/* A sB k if ((R[A] <= sB) ~= k) then pc++ */
|
|
||||||
OP_GTI,/* A sB k if ((R[A] > sB) ~= k) then pc++ */
|
|
||||||
OP_GEI,/* A sB k if ((R[A] >= sB) ~= k) then pc++ */
|
|
||||||
|
|
||||||
OP_TEST,/* A k if (not R[A] == k) then pc++ */
|
|
||||||
OP_TESTSET,/* A B k if (not R[B] == k) then pc++ else R[A] := R[B] */
|
|
||||||
|
|
||||||
OP_CALL,/* A B C R[A], ... ,R[A+C-2] := R[A](R[A+1], ... ,R[A+B-1]) */
|
|
||||||
OP_TAILCALL,/* A B C k return R[A](R[A+1], ... ,R[A+B-1]) */
|
|
||||||
|
|
||||||
OP_RETURN,/* A B C k return R[A], ... ,R[A+B-2] */
|
|
||||||
OP_RETURN0,/* return */
|
|
||||||
OP_RETURN1,/* A return R[A] */
|
|
||||||
|
|
||||||
OP_FORLOOP,/* A Bx update counters; if loop continues then pc-=Bx; */
|
|
||||||
OP_FORPREP,/* A Bx <check values and prepare counters>;
|
|
||||||
if not to run then pc+=Bx+1; */
|
|
||||||
|
|
||||||
OP_TFORPREP,/* A Bx create upvalue for R[A + 3]; pc+=Bx */
|
|
||||||
OP_TFORCALL,/* A C R[A+4], ... ,R[A+3+C] := R[A](R[A+1], R[A+2]); */
|
|
||||||
OP_TFORLOOP,/* A Bx if R[A+2] ~= nil then { R[A]=R[A+2]; pc -= Bx } */
|
|
||||||
|
|
||||||
OP_SETLIST,/* A vB vC k R[A][vC+i] := R[A+i], 1 <= i <= vB */
|
|
||||||
|
|
||||||
OP_CLOSURE,/* A Bx R[A] := closure(KPROTO[Bx]) */
|
|
||||||
|
|
||||||
OP_VARARG,/* A B C k R[A], ..., R[A+C-2] = varargs */
|
|
||||||
|
|
||||||
OP_GETVARG, /* A B C R[A] := R[B][R[C]], R[B] is vararg parameter */
|
|
||||||
|
|
||||||
OP_ERRNNIL,/* A Bx raise error if R[A] ~= nil (K[Bx - 1] is global name)*/
|
|
||||||
|
|
||||||
OP_VARARGPREP,/* (adjust varargs) */
|
|
||||||
|
|
||||||
OP_EXTRAARG/* Ax extra (larger) argument for previous opcode */
|
OP_EXTRAARG/* Ax extra (larger) argument for previous opcode */
|
||||||
} OpCode;
|
} OpCode;
|
||||||
|
|
||||||
|
|
||||||
#define NUM_OPCODES ((int)(OP_EXTRAARG) + 1)
|
#define NUM_OPCODES (cast(int, OP_EXTRAARG) + 1)
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
/*===========================================================================
|
/*===========================================================================
|
||||||
Notes:
|
Notes:
|
||||||
|
(*) In OP_CALL, if (B == 0) then B = top. If (C == 0), then 'top' is
|
||||||
|
set to last_result+1, so next open instruction (OP_CALL, OP_RETURN,
|
||||||
|
OP_SETLIST) may use 'top'.
|
||||||
|
|
||||||
(*) Opcode OP_LFALSESKIP is used to convert a condition to a boolean
|
(*) In OP_VARARG, if (B == 0) then use actual number of varargs and
|
||||||
value, in a code equivalent to (not cond ? false : true). (It
|
set top (like in OP_CALL with C == 0).
|
||||||
produces false and skips the next instruction producing true.)
|
|
||||||
|
|
||||||
(*) Opcodes OP_MMBIN and variants follow each arithmetic and
|
|
||||||
bitwise opcode. If the operation succeeds, it skips this next
|
|
||||||
opcode. Otherwise, this opcode calls the corresponding metamethod.
|
|
||||||
|
|
||||||
(*) Opcode OP_TESTSET is used in short-circuit expressions that need
|
|
||||||
both to jump and to produce a value, such as (a = b or c).
|
|
||||||
|
|
||||||
(*) In OP_CALL, if (B == 0) then B = top - A. If (C == 0), then
|
|
||||||
'top' is set to last_result+1, so next open instruction (OP_CALL,
|
|
||||||
OP_RETURN*, OP_SETLIST) may use 'top'.
|
|
||||||
|
|
||||||
(*) In OP_VARARG, if (C == 0) then use actual number of varargs and
|
|
||||||
set top (like in OP_CALL with C == 0). 'k' means function has a
|
|
||||||
vararg table, which is in R[B].
|
|
||||||
|
|
||||||
(*) In OP_RETURN, if (B == 0) then return up to 'top'.
|
(*) In OP_RETURN, if (B == 0) then return up to 'top'.
|
||||||
|
|
||||||
(*) In OP_LOADKX and OP_NEWTABLE, the next instruction is always
|
(*) In OP_SETLIST, if (B == 0) then B = 'top'; if (C == 0) then next
|
||||||
OP_EXTRAARG.
|
'instruction' is EXTRAARG(real C).
|
||||||
|
|
||||||
(*) In OP_SETLIST, if (B == 0) then real B = 'top'; if k, then
|
(*) In OP_LOADKX, the next 'instruction' is always EXTRAARG.
|
||||||
real C = EXTRAARG _ C (the bits of EXTRAARG concatenated with the
|
|
||||||
bits of C).
|
|
||||||
|
|
||||||
(*) In OP_NEWTABLE, vB is log2 of the hash size (which is always a
|
(*) For comparisons, A specifies what condition the test should accept
|
||||||
power of 2) plus 1, or zero for size zero. If not k, the array size
|
|
||||||
is vC. Otherwise, the array size is EXTRAARG _ vC.
|
|
||||||
|
|
||||||
(*) In OP_ERRNNIL, (Bx == 0) means index of global name doesn't
|
|
||||||
fit in Bx. (So, that name is not available for the error message.)
|
|
||||||
|
|
||||||
(*) For comparisons, k specifies what condition the test should accept
|
|
||||||
(true or false).
|
(true or false).
|
||||||
|
|
||||||
(*) In OP_MMBINI/OP_MMBINK, k means the arguments were flipped
|
(*) All 'skips' (pc++) assume that next instruction is a jump.
|
||||||
(the constant is the first operand).
|
|
||||||
|
|
||||||
(*) All comparison and test instructions assume that the instruction
|
|
||||||
being skipped (pc++) is a jump.
|
|
||||||
|
|
||||||
(*) In instructions OP_RETURN/OP_TAILCALL, 'k' specifies that the
|
|
||||||
function builds upvalues, which may need to be closed. C > 0 means
|
|
||||||
the function has hidden vararg arguments, so that its 'func' must be
|
|
||||||
corrected before returning; in this case, (C - 1) is its number of
|
|
||||||
fixed parameters.
|
|
||||||
|
|
||||||
(*) In comparisons with an immediate operand, C signals whether the
|
|
||||||
original operand was a float. (It must be corrected in case of
|
|
||||||
metamethods.)
|
|
||||||
|
|
||||||
===========================================================================*/
|
===========================================================================*/
|
||||||
|
|
||||||
|
|
||||||
/*
|
/*
|
||||||
** masks for instruction properties. The format is:
|
** masks for instruction properties. The format is:
|
||||||
** bits 0-2: op mode
|
** bits 0-1: op mode
|
||||||
** bit 3: instruction set register A
|
** bits 2-3: C arg mode
|
||||||
** bit 4: operator is a test (next instruction must be a jump)
|
** bits 4-5: B arg mode
|
||||||
** bit 5: instruction uses 'L->top' set by previous instruction (when B == 0)
|
** bit 6: instruction set register A
|
||||||
** bit 6: instruction sets 'L->top' for next instruction (when C == 0)
|
** bit 7: operator is a test (next instruction must be a jump)
|
||||||
** bit 7: instruction is an MM instruction (call a metamethod)
|
|
||||||
*/
|
*/
|
||||||
|
|
||||||
LUAI_DDEC(const lu_byte luaP_opmodes[NUM_OPCODES];)
|
enum OpArgMask {
|
||||||
|
OpArgN, /* argument is not used */
|
||||||
|
OpArgU, /* argument is used */
|
||||||
|
OpArgR, /* argument is a register or a jump offset */
|
||||||
|
OpArgK /* argument is a constant or register/constant */
|
||||||
|
};
|
||||||
|
|
||||||
#define getOpMode(m) (cast(enum OpMode, luaP_opmodes[m] & 7))
|
LUAI_DDEC const lu_byte luaP_opmodes[NUM_OPCODES];
|
||||||
#define testAMode(m) (luaP_opmodes[m] & (1 << 3))
|
|
||||||
#define testTMode(m) (luaP_opmodes[m] & (1 << 4))
|
#define getOpMode(m) (cast(enum OpMode, luaP_opmodes[m] & 3))
|
||||||
#define testITMode(m) (luaP_opmodes[m] & (1 << 5))
|
#define getBMode(m) (cast(enum OpArgMask, (luaP_opmodes[m] >> 4) & 3))
|
||||||
#define testOTMode(m) (luaP_opmodes[m] & (1 << 6))
|
#define getCMode(m) (cast(enum OpArgMask, (luaP_opmodes[m] >> 2) & 3))
|
||||||
#define testMMMode(m) (luaP_opmodes[m] & (1 << 7))
|
#define testAMode(m) (luaP_opmodes[m] & (1 << 6))
|
||||||
|
#define testTMode(m) (luaP_opmodes[m] & (1 << 7))
|
||||||
|
|
||||||
|
|
||||||
LUAI_FUNC int luaP_isOT (Instruction i);
|
LUAI_DDEC const char *const luaP_opnames[NUM_OPCODES+1]; /* opcode names */
|
||||||
LUAI_FUNC int luaP_isIT (Instruction i);
|
|
||||||
|
|
||||||
|
/* number of list items to accumulate before a SETLIST instruction */
|
||||||
|
#define LFIELDS_PER_FLUSH 50
|
||||||
|
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
|||||||
-105
@@ -1,105 +0,0 @@
|
|||||||
/*
|
|
||||||
** $Id: lopnames.h $
|
|
||||||
** Opcode names
|
|
||||||
** See Copyright Notice in lua.h
|
|
||||||
*/
|
|
||||||
|
|
||||||
#if !defined(lopnames_h)
|
|
||||||
#define lopnames_h
|
|
||||||
|
|
||||||
#include <stddef.h>
|
|
||||||
|
|
||||||
|
|
||||||
/* ORDER OP */
|
|
||||||
|
|
||||||
static const char *const opnames[] = {
|
|
||||||
"MOVE",
|
|
||||||
"LOADI",
|
|
||||||
"LOADF",
|
|
||||||
"LOADK",
|
|
||||||
"LOADKX",
|
|
||||||
"LOADFALSE",
|
|
||||||
"LFALSESKIP",
|
|
||||||
"LOADTRUE",
|
|
||||||
"LOADNIL",
|
|
||||||
"GETUPVAL",
|
|
||||||
"SETUPVAL",
|
|
||||||
"GETTABUP",
|
|
||||||
"GETTABLE",
|
|
||||||
"GETI",
|
|
||||||
"GETFIELD",
|
|
||||||
"SETTABUP",
|
|
||||||
"SETTABLE",
|
|
||||||
"SETI",
|
|
||||||
"SETFIELD",
|
|
||||||
"NEWTABLE",
|
|
||||||
"SELF",
|
|
||||||
"ADDI",
|
|
||||||
"ADDK",
|
|
||||||
"SUBK",
|
|
||||||
"MULK",
|
|
||||||
"MODK",
|
|
||||||
"POWK",
|
|
||||||
"DIVK",
|
|
||||||
"IDIVK",
|
|
||||||
"BANDK",
|
|
||||||
"BORK",
|
|
||||||
"BXORK",
|
|
||||||
"SHLI",
|
|
||||||
"SHRI",
|
|
||||||
"ADD",
|
|
||||||
"SUB",
|
|
||||||
"MUL",
|
|
||||||
"MOD",
|
|
||||||
"POW",
|
|
||||||
"DIV",
|
|
||||||
"IDIV",
|
|
||||||
"BAND",
|
|
||||||
"BOR",
|
|
||||||
"BXOR",
|
|
||||||
"SHL",
|
|
||||||
"SHR",
|
|
||||||
"MMBIN",
|
|
||||||
"MMBINI",
|
|
||||||
"MMBINK",
|
|
||||||
"UNM",
|
|
||||||
"BNOT",
|
|
||||||
"NOT",
|
|
||||||
"LEN",
|
|
||||||
"CONCAT",
|
|
||||||
"CLOSE",
|
|
||||||
"TBC",
|
|
||||||
"JMP",
|
|
||||||
"EQ",
|
|
||||||
"LT",
|
|
||||||
"LE",
|
|
||||||
"EQK",
|
|
||||||
"EQI",
|
|
||||||
"LTI",
|
|
||||||
"LEI",
|
|
||||||
"GTI",
|
|
||||||
"GEI",
|
|
||||||
"TEST",
|
|
||||||
"TESTSET",
|
|
||||||
"CALL",
|
|
||||||
"TAILCALL",
|
|
||||||
"RETURN",
|
|
||||||
"RETURN0",
|
|
||||||
"RETURN1",
|
|
||||||
"FORLOOP",
|
|
||||||
"FORPREP",
|
|
||||||
"TFORPREP",
|
|
||||||
"TFORCALL",
|
|
||||||
"TFORLOOP",
|
|
||||||
"SETLIST",
|
|
||||||
"CLOSURE",
|
|
||||||
"VARARG",
|
|
||||||
"GETVARG",
|
|
||||||
"ERRNNIL",
|
|
||||||
"VARARGPREP",
|
|
||||||
"EXTRAARG",
|
|
||||||
NULL
|
|
||||||
};
|
|
||||||
|
|
||||||
#endif
|
|
||||||
|
|
||||||
@@ -1,5 +1,5 @@
|
|||||||
/*
|
/*
|
||||||
** $Id: loslib.c $
|
** $Id: loslib.c,v 1.65.1.1 2017/04/19 17:29:57 roberto Exp $
|
||||||
** Standard Operating System library
|
** Standard Operating System library
|
||||||
** See Copyright Notice in lua.h
|
** See Copyright Notice in lua.h
|
||||||
*/
|
*/
|
||||||
@@ -20,7 +20,6 @@
|
|||||||
|
|
||||||
#include "lauxlib.h"
|
#include "lauxlib.h"
|
||||||
#include "lualib.h"
|
#include "lualib.h"
|
||||||
#include "llimits.h"
|
|
||||||
|
|
||||||
|
|
||||||
/*
|
/*
|
||||||
@@ -31,14 +30,23 @@
|
|||||||
*/
|
*/
|
||||||
#if !defined(LUA_STRFTIMEOPTIONS) /* { */
|
#if !defined(LUA_STRFTIMEOPTIONS) /* { */
|
||||||
|
|
||||||
#if defined(LUA_USE_WINDOWS)
|
/* options for ANSI C 89 (only 1-char options) */
|
||||||
#define LUA_STRFTIMEOPTIONS "aAbBcdHIjmMpSUwWxXyYzZ%" \
|
#define L_STRFTIMEC89 "aAbBcdHIjmMpSUwWxXyYZ%"
|
||||||
"||" "#c#x#d#H#I#j#m#M#S#U#w#W#y#Y" /* two-char options */
|
|
||||||
#elif defined(LUA_USE_C89) /* C89 (only 1-char options) */
|
/* options for ISO C 99 and POSIX */
|
||||||
#define LUA_STRFTIMEOPTIONS "aAbBcdHIjmMpSUwWxXyYZ%"
|
#define L_STRFTIMEC99 "aAbBcCdDeFgGhHIjmMnprRStTuUVwWxXyYzZ%" \
|
||||||
#else /* C99 specification */
|
|
||||||
#define LUA_STRFTIMEOPTIONS "aAbBcCdDeFgGhHIjmMnprRStTuUVwWxXyYzZ%" \
|
|
||||||
"||" "EcECExEXEyEY" "OdOeOHOIOmOMOSOuOUOVOwOWOy" /* two-char options */
|
"||" "EcECExEXEyEY" "OdOeOHOIOmOMOSOuOUOVOwOWOy" /* two-char options */
|
||||||
|
|
||||||
|
/* options for Windows */
|
||||||
|
#define L_STRFTIMEWIN "aAbBcdHIjmMpSUwWxXyYzZ%" \
|
||||||
|
"||" "#c#x#d#H#I#j#m#M#S#U#w#W#y#Y" /* two-char options */
|
||||||
|
|
||||||
|
#if defined(LUA_USE_WINDOWS)
|
||||||
|
#define LUA_STRFTIMEOPTIONS L_STRFTIMEWIN
|
||||||
|
#elif defined(LUA_USE_C89)
|
||||||
|
#define LUA_STRFTIMEOPTIONS L_STRFTIMEC89
|
||||||
|
#else /* C99 specification */
|
||||||
|
#define LUA_STRFTIMEOPTIONS L_STRFTIMEC99
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
#endif /* } */
|
#endif /* } */
|
||||||
@@ -51,20 +59,18 @@
|
|||||||
** ===================================================================
|
** ===================================================================
|
||||||
*/
|
*/
|
||||||
|
|
||||||
|
#if !defined(l_time_t) /* { */
|
||||||
/*
|
/*
|
||||||
** type to represent time_t in Lua
|
** type to represent time_t in Lua
|
||||||
*/
|
*/
|
||||||
#if !defined(LUA_NUMTIME) /* { */
|
|
||||||
|
|
||||||
#define l_timet lua_Integer
|
#define l_timet lua_Integer
|
||||||
#define l_pushtime(L,t) lua_pushinteger(L,(lua_Integer)(t))
|
#define l_pushtime(L,t) lua_pushinteger(L,(lua_Integer)(t))
|
||||||
#define l_gettime(L,arg) luaL_checkinteger(L, arg)
|
|
||||||
|
|
||||||
#else /* }{ */
|
static time_t l_checktime (lua_State *L, int arg) {
|
||||||
|
lua_Integer t = luaL_checkinteger(L, arg);
|
||||||
#define l_timet lua_Number
|
luaL_argcheck(L, (time_t)t == t, arg, "time out-of-bounds");
|
||||||
#define l_pushtime(L,t) lua_pushnumber(L,(lua_Number)(t))
|
return (time_t)t;
|
||||||
#define l_gettime(L,arg) luaL_checknumber(L, arg)
|
}
|
||||||
|
|
||||||
#endif /* } */
|
#endif /* } */
|
||||||
|
|
||||||
@@ -84,7 +90,7 @@
|
|||||||
|
|
||||||
/* ISO C definitions */
|
/* ISO C definitions */
|
||||||
#define l_gmtime(t,r) ((void)(r)->tm_sec, gmtime(t))
|
#define l_gmtime(t,r) ((void)(r)->tm_sec, gmtime(t))
|
||||||
#define l_localtime(t,r) ((void)(r)->tm_sec, localtime(t))
|
#define l_localtime(t,r) ((void)(r)->tm_sec, localtime(t))
|
||||||
|
|
||||||
#endif /* } */
|
#endif /* } */
|
||||||
|
|
||||||
@@ -130,21 +136,11 @@
|
|||||||
/* }================================================================== */
|
/* }================================================================== */
|
||||||
|
|
||||||
|
|
||||||
#if !defined(l_system)
|
|
||||||
#if defined(LUA_USE_IOS)
|
|
||||||
/* Despite claiming to be ISO C, iOS does not implement 'system'. */
|
|
||||||
#define l_system(cmd) ((cmd) == NULL ? 0 : -1)
|
|
||||||
#else
|
|
||||||
#define l_system(cmd) system(cmd) /* default definition */
|
|
||||||
#endif
|
|
||||||
#endif
|
|
||||||
|
|
||||||
|
|
||||||
static int os_execute (lua_State *L) {
|
static int os_execute (lua_State *L) {
|
||||||
const char *cmd = luaL_optstring(L, 1, NULL);
|
const char *cmd = luaL_optstring(L, 1, NULL);
|
||||||
int stat;
|
int stat = system(cmd);
|
||||||
errno = 0;
|
|
||||||
stat = l_system(cmd);
|
|
||||||
if (cmd != NULL)
|
if (cmd != NULL)
|
||||||
return luaL_execresult(L, stat);
|
return luaL_execresult(L, stat);
|
||||||
else {
|
else {
|
||||||
@@ -156,7 +152,6 @@ static int os_execute (lua_State *L) {
|
|||||||
|
|
||||||
static int os_remove (lua_State *L) {
|
static int os_remove (lua_State *L) {
|
||||||
const char *filename = luaL_checkstring(L, 1);
|
const char *filename = luaL_checkstring(L, 1);
|
||||||
errno = 0;
|
|
||||||
return luaL_fileresult(L, remove(filename) == 0, filename);
|
return luaL_fileresult(L, remove(filename) == 0, filename);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -164,7 +159,6 @@ 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);
|
||||||
errno = 0;
|
|
||||||
return luaL_fileresult(L, rename(fromname, toname) == 0, NULL);
|
return luaL_fileresult(L, rename(fromname, toname) == 0, NULL);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -173,7 +167,7 @@ static int os_tmpname (lua_State *L) {
|
|||||||
char buff[LUA_TMPNAMBUFSIZE];
|
char buff[LUA_TMPNAMBUFSIZE];
|
||||||
int err;
|
int err;
|
||||||
lua_tmpnam(buff, err);
|
lua_tmpnam(buff, err);
|
||||||
if (l_unlikely(err))
|
if (err)
|
||||||
return luaL_error(L, "unable to generate a unique filename");
|
return luaL_error(L, "unable to generate a unique filename");
|
||||||
lua_pushstring(L, buff);
|
lua_pushstring(L, buff);
|
||||||
return 1;
|
return 1;
|
||||||
@@ -200,25 +194,11 @@ static int os_clock (lua_State *L) {
|
|||||||
** =======================================================
|
** =======================================================
|
||||||
*/
|
*/
|
||||||
|
|
||||||
/*
|
static void setfield (lua_State *L, const char *key, int value) {
|
||||||
** About the overflow check: an overflow cannot occur when time
|
lua_pushinteger(L, value);
|
||||||
** is represented by a lua_Integer, because either lua_Integer is
|
|
||||||
** large enough to represent all int fields or it is not large enough
|
|
||||||
** to represent a time that cause a field to overflow. However, if
|
|
||||||
** times are represented as doubles and lua_Integer is int, then the
|
|
||||||
** time 0x1.e1853b0d184f6p+55 would cause an overflow when adding 1900
|
|
||||||
** to compute the year.
|
|
||||||
*/
|
|
||||||
static void setfield (lua_State *L, const char *key, int value, int delta) {
|
|
||||||
#if (defined(LUA_NUMTIME) && LUA_MAXINTEGER <= INT_MAX)
|
|
||||||
if (l_unlikely(value > LUA_MAXINTEGER - delta))
|
|
||||||
luaL_error(L, "field '%s' is out-of-bound", key);
|
|
||||||
#endif
|
|
||||||
lua_pushinteger(L, (lua_Integer)value + delta);
|
|
||||||
lua_setfield(L, -2, key);
|
lua_setfield(L, -2, key);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
static void setboolfield (lua_State *L, const char *key, int value) {
|
static void setboolfield (lua_State *L, const char *key, int value) {
|
||||||
if (value < 0) /* undefined? */
|
if (value < 0) /* undefined? */
|
||||||
return; /* does not set field */
|
return; /* does not set field */
|
||||||
@@ -231,14 +211,14 @@ static void setboolfield (lua_State *L, const char *key, int value) {
|
|||||||
** Set all fields from structure 'tm' in the table on top of the stack
|
** Set all fields from structure 'tm' in the table on top of the stack
|
||||||
*/
|
*/
|
||||||
static void setallfields (lua_State *L, struct tm *stm) {
|
static void setallfields (lua_State *L, struct tm *stm) {
|
||||||
setfield(L, "year", stm->tm_year, 1900);
|
setfield(L, "sec", stm->tm_sec);
|
||||||
setfield(L, "month", stm->tm_mon, 1);
|
setfield(L, "min", stm->tm_min);
|
||||||
setfield(L, "day", stm->tm_mday, 0);
|
setfield(L, "hour", stm->tm_hour);
|
||||||
setfield(L, "hour", stm->tm_hour, 0);
|
setfield(L, "day", stm->tm_mday);
|
||||||
setfield(L, "min", stm->tm_min, 0);
|
setfield(L, "month", stm->tm_mon + 1);
|
||||||
setfield(L, "sec", stm->tm_sec, 0);
|
setfield(L, "year", stm->tm_year + 1900);
|
||||||
setfield(L, "yday", stm->tm_yday, 1);
|
setfield(L, "wday", stm->tm_wday + 1);
|
||||||
setfield(L, "wday", stm->tm_wday, 1);
|
setfield(L, "yday", stm->tm_yday + 1);
|
||||||
setboolfield(L, "isdst", stm->tm_isdst);
|
setboolfield(L, "isdst", stm->tm_isdst);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -251,19 +231,24 @@ static int getboolfield (lua_State *L, const char *key) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/* maximum value for date fields (to avoid arithmetic overflows with 'int') */
|
||||||
|
#if !defined(L_MAXDATEFIELD)
|
||||||
|
#define L_MAXDATEFIELD (INT_MAX / 2)
|
||||||
|
#endif
|
||||||
|
|
||||||
static int getfield (lua_State *L, const char *key, int d, int delta) {
|
static int getfield (lua_State *L, const char *key, int d, int delta) {
|
||||||
int isnum;
|
int isnum;
|
||||||
int t = lua_getfield(L, -1, key); /* get field and its type */
|
int t = lua_getfield(L, -1, key); /* get field and its type */
|
||||||
lua_Integer res = lua_tointegerx(L, -1, &isnum);
|
lua_Integer res = lua_tointegerx(L, -1, &isnum);
|
||||||
if (!isnum) { /* field is not an integer? */
|
if (!isnum) { /* field is not an integer? */
|
||||||
if (l_unlikely(t != LUA_TNIL)) /* some other value? */
|
if (t != LUA_TNIL) /* some other value? */
|
||||||
return luaL_error(L, "field '%s' is not an integer", key);
|
return luaL_error(L, "field '%s' is not an integer", key);
|
||||||
else if (l_unlikely(d < 0)) /* absent field; no default? */
|
else if (d < 0) /* absent field; no default? */
|
||||||
return luaL_error(L, "field '%s' missing in date table", key);
|
return luaL_error(L, "field '%s' missing in date table", key);
|
||||||
res = d;
|
res = d;
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
if (!(res >= 0 ? res - delta <= INT_MAX : INT_MIN + delta <= res))
|
if (!(-L_MAXDATEFIELD <= res && res <= L_MAXDATEFIELD))
|
||||||
return luaL_error(L, "field '%s' is out-of-bound", key);
|
return luaL_error(L, "field '%s' is out-of-bound", key);
|
||||||
res -= delta;
|
res -= delta;
|
||||||
}
|
}
|
||||||
@@ -273,9 +258,9 @@ static int getfield (lua_State *L, const char *key, int d, int delta) {
|
|||||||
|
|
||||||
|
|
||||||
static const char *checkoption (lua_State *L, const char *conv,
|
static const char *checkoption (lua_State *L, const char *conv,
|
||||||
size_t convlen, char *buff) {
|
ptrdiff_t convlen, char *buff) {
|
||||||
const char *option = LUA_STRFTIMEOPTIONS;
|
const char *option = LUA_STRFTIMEOPTIONS;
|
||||||
unsigned oplen = 1; /* length of options being checked */
|
int oplen = 1; /* length of options being checked */
|
||||||
for (; *option != '\0' && oplen <= convlen; option += oplen) {
|
for (; *option != '\0' && oplen <= convlen; option += oplen) {
|
||||||
if (*option == '|') /* next block? */
|
if (*option == '|') /* next block? */
|
||||||
oplen++; /* will check options with next length (+1) */
|
oplen++; /* will check options with next length (+1) */
|
||||||
@@ -291,13 +276,6 @@ static const char *checkoption (lua_State *L, const char *conv,
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
static time_t l_checktime (lua_State *L, int arg) {
|
|
||||||
l_timet t = l_gettime(L, arg);
|
|
||||||
luaL_argcheck(L, (time_t)t == t, arg, "time out-of-bounds");
|
|
||||||
return (time_t)t;
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
/* maximum size for an individual 'strftime' item */
|
/* maximum size for an individual 'strftime' item */
|
||||||
#define SIZETIMEFMT 250
|
#define SIZETIMEFMT 250
|
||||||
|
|
||||||
@@ -316,7 +294,7 @@ static int os_date (lua_State *L) {
|
|||||||
stm = l_localtime(&t, &tmr);
|
stm = l_localtime(&t, &tmr);
|
||||||
if (stm == NULL) /* invalid date? */
|
if (stm == NULL) /* invalid date? */
|
||||||
return luaL_error(L,
|
return luaL_error(L,
|
||||||
"date result cannot be represented in this installation");
|
"time result cannot be represented in this installation");
|
||||||
if (strcmp(s, "*t") == 0) {
|
if (strcmp(s, "*t") == 0) {
|
||||||
lua_createtable(L, 0, 9); /* 9 = number of fields */
|
lua_createtable(L, 0, 9); /* 9 = number of fields */
|
||||||
setallfields(L, stm);
|
setallfields(L, stm);
|
||||||
@@ -333,8 +311,7 @@ static int os_date (lua_State *L) {
|
|||||||
size_t reslen;
|
size_t reslen;
|
||||||
char *buff = luaL_prepbuffsize(&b, SIZETIMEFMT);
|
char *buff = luaL_prepbuffsize(&b, SIZETIMEFMT);
|
||||||
s++; /* skip '%' */
|
s++; /* skip '%' */
|
||||||
/* copy specifier to 'cc' */
|
s = checkoption(L, s, se - s, cc + 1); /* copy specifier to 'cc' */
|
||||||
s = checkoption(L, s, ct_diff2sz(se - s), cc + 1);
|
|
||||||
reslen = strftime(buff, SIZETIMEFMT, cc, stm);
|
reslen = strftime(buff, SIZETIMEFMT, cc, stm);
|
||||||
luaL_addsize(&b, reslen);
|
luaL_addsize(&b, reslen);
|
||||||
}
|
}
|
||||||
@@ -353,12 +330,12 @@ static int os_time (lua_State *L) {
|
|||||||
struct tm ts;
|
struct tm ts;
|
||||||
luaL_checktype(L, 1, LUA_TTABLE);
|
luaL_checktype(L, 1, LUA_TTABLE);
|
||||||
lua_settop(L, 1); /* make sure table is at the top */
|
lua_settop(L, 1); /* make sure table is at the top */
|
||||||
ts.tm_year = getfield(L, "year", -1, 1900);
|
|
||||||
ts.tm_mon = getfield(L, "month", -1, 1);
|
|
||||||
ts.tm_mday = getfield(L, "day", -1, 0);
|
|
||||||
ts.tm_hour = getfield(L, "hour", 12, 0);
|
|
||||||
ts.tm_min = getfield(L, "min", 0, 0);
|
|
||||||
ts.tm_sec = getfield(L, "sec", 0, 0);
|
ts.tm_sec = getfield(L, "sec", 0, 0);
|
||||||
|
ts.tm_min = getfield(L, "min", 0, 0);
|
||||||
|
ts.tm_hour = getfield(L, "hour", 12, 0);
|
||||||
|
ts.tm_mday = getfield(L, "day", -1, 0);
|
||||||
|
ts.tm_mon = getfield(L, "month", -1, 1);
|
||||||
|
ts.tm_year = getfield(L, "year", -1, 1900);
|
||||||
ts.tm_isdst = getboolfield(L, "isdst");
|
ts.tm_isdst = getboolfield(L, "isdst");
|
||||||
t = mktime(&ts);
|
t = mktime(&ts);
|
||||||
setallfields(L, &ts); /* update fields with normalized values */
|
setallfields(L, &ts); /* update fields with normalized values */
|
||||||
|
|||||||
@@ -1,5 +1,5 @@
|
|||||||
/*
|
/*
|
||||||
** $Id: lparser.h $
|
** $Id: lparser.h,v 1.76.1.1 2017/04/19 17:20:42 roberto Exp $
|
||||||
** Lua Parser
|
** Lua Parser
|
||||||
** See Copyright Notice in lua.h
|
** See Copyright Notice in lua.h
|
||||||
*/
|
*/
|
||||||
@@ -23,118 +23,63 @@
|
|||||||
|
|
||||||
/* kinds of variables/expressions */
|
/* kinds of variables/expressions */
|
||||||
typedef enum {
|
typedef enum {
|
||||||
VVOID, /* when 'expdesc' describes the last expression of a list,
|
VVOID, /* when 'expdesc' describes the last expression a list,
|
||||||
this kind means an empty list (so, no expression) */
|
this kind means an empty list (so, no expression) */
|
||||||
VNIL, /* constant nil */
|
VNIL, /* constant nil */
|
||||||
VTRUE, /* constant true */
|
VTRUE, /* constant true */
|
||||||
VFALSE, /* constant false */
|
VFALSE, /* constant false */
|
||||||
VK, /* constant in 'k'; info = index of constant in 'k' */
|
VK, /* constant in 'k'; info = index of constant in 'k' */
|
||||||
VKFLT, /* floating constant; nval = numerical float value */
|
VKFLT, /* floating constant; nval = numerical float value */
|
||||||
VKINT, /* integer constant; ival = numerical integer value */
|
VKINT, /* integer constant; nval = numerical integer value */
|
||||||
VKSTR, /* string constant; strval = TString address;
|
|
||||||
(string is fixed by the scanner) */
|
|
||||||
VNONRELOC, /* expression has its value in a fixed register;
|
VNONRELOC, /* expression has its value in a fixed register;
|
||||||
info = result register */
|
info = result register */
|
||||||
VLOCAL, /* local variable; var.ridx = register index;
|
VLOCAL, /* local variable; info = local register */
|
||||||
var.vidx = relative index in 'actvar.arr' */
|
|
||||||
VVARGVAR, /* vararg parameter; var.ridx = register index;
|
|
||||||
var.vidx = relative index in 'actvar.arr' */
|
|
||||||
VGLOBAL, /* global variable;
|
|
||||||
info = relative index in 'actvar.arr' (or -1 for
|
|
||||||
implicit declaration) */
|
|
||||||
VUPVAL, /* upvalue variable; info = index of upvalue in 'upvalues' */
|
VUPVAL, /* upvalue variable; info = index of upvalue in 'upvalues' */
|
||||||
VCONST, /* compile-time <const> variable;
|
|
||||||
info = absolute index in 'actvar.arr' */
|
|
||||||
VINDEXED, /* indexed variable;
|
VINDEXED, /* indexed variable;
|
||||||
ind.t = table register;
|
ind.vt = whether 't' is register or upvalue;
|
||||||
ind.idx = key's R index;
|
ind.t = table register or upvalue;
|
||||||
ind.ro = true if it represents a read-only global;
|
ind.idx = key's R/K index */
|
||||||
ind.keystr = if key is a string, index in 'k' of that string;
|
|
||||||
-1 if key is not a string */
|
|
||||||
VVARGIND, /* indexed vararg parameter;
|
|
||||||
ind.* as in VINDEXED */
|
|
||||||
VINDEXUP, /* indexed upvalue;
|
|
||||||
ind.idx = key's K index;
|
|
||||||
ind.* as in VINDEXED */
|
|
||||||
VINDEXI, /* indexed variable with constant integer;
|
|
||||||
ind.t = table register;
|
|
||||||
ind.idx = key's value */
|
|
||||||
VINDEXSTR, /* indexed variable with literal string;
|
|
||||||
ind.idx = key's K index;
|
|
||||||
ind.* as in VINDEXED */
|
|
||||||
VJMP, /* expression is a test/comparison;
|
VJMP, /* expression is a test/comparison;
|
||||||
info = pc of corresponding jump instruction */
|
info = pc of corresponding jump instruction */
|
||||||
VRELOC, /* expression can put result in any register;
|
VRELOCABLE, /* expression can put result in any register;
|
||||||
info = instruction pc */
|
info = instruction pc */
|
||||||
VCALL, /* expression is a function call; info = instruction pc */
|
VCALL, /* expression is a function call; info = instruction pc */
|
||||||
VVARARG /* vararg expression; info = instruction pc */
|
VVARARG /* vararg expression; info = instruction pc */
|
||||||
} expkind;
|
} expkind;
|
||||||
|
|
||||||
|
|
||||||
#define vkisvar(k) (VLOCAL <= (k) && (k) <= VINDEXSTR)
|
#define vkisvar(k) (VLOCAL <= (k) && (k) <= VINDEXED)
|
||||||
#define vkisindexed(k) (VINDEXED <= (k) && (k) <= VINDEXSTR)
|
#define vkisinreg(k) ((k) == VNONRELOC || (k) == VLOCAL)
|
||||||
|
|
||||||
|
|
||||||
typedef struct expdesc {
|
typedef struct expdesc {
|
||||||
expkind k;
|
expkind k;
|
||||||
union {
|
union {
|
||||||
lua_Integer ival; /* for VKINT */
|
lua_Integer ival; /* for VKINT */
|
||||||
lua_Number nval; /* for VKFLT */
|
lua_Number nval; /* for VKFLT */
|
||||||
TString *strval; /* for VKSTR */
|
|
||||||
int info; /* for generic use */
|
int info; /* for generic use */
|
||||||
struct { /* for indexed variables */
|
struct { /* for indexed variables (VINDEXED) */
|
||||||
short idx; /* index (R or "long" K) */
|
short idx; /* index (R/K) */
|
||||||
lu_byte t; /* table (register or upvalue) */
|
lu_byte t; /* table (register or upvalue) */
|
||||||
lu_byte ro; /* true if variable is read-only */
|
lu_byte vt; /* whether 't' is register (VLOCAL) or upvalue (VUPVAL) */
|
||||||
int keystr; /* index in 'k' of string key, or -1 if not a string */
|
|
||||||
} ind;
|
} ind;
|
||||||
struct { /* for local variables */
|
|
||||||
lu_byte ridx; /* register holding the variable */
|
|
||||||
short vidx; /* index in 'actvar.arr' */
|
|
||||||
} var;
|
|
||||||
} u;
|
} u;
|
||||||
int t; /* patch list of 'exit when true' */
|
int t; /* patch list of 'exit when true' */
|
||||||
int f; /* patch list of 'exit when false' */
|
int f; /* patch list of 'exit when false' */
|
||||||
} expdesc;
|
} expdesc;
|
||||||
|
|
||||||
|
|
||||||
/* kinds of variables */
|
/* description of active local variable */
|
||||||
#define VDKREG 0 /* regular local */
|
typedef struct Vardesc {
|
||||||
#define RDKCONST 1 /* local constant */
|
short idx; /* variable index in stack */
|
||||||
#define RDKVAVAR 2 /* vararg parameter */
|
|
||||||
#define RDKTOCLOSE 3 /* to-be-closed */
|
|
||||||
#define RDKCTC 4 /* local compile-time constant */
|
|
||||||
#define GDKREG 5 /* regular global */
|
|
||||||
#define GDKCONST 6 /* global constant */
|
|
||||||
|
|
||||||
/* variables that live in registers */
|
|
||||||
#define varinreg(v) ((v)->vd.kind <= RDKTOCLOSE)
|
|
||||||
|
|
||||||
/* test for global variables */
|
|
||||||
#define varglobal(v) ((v)->vd.kind >= GDKREG)
|
|
||||||
|
|
||||||
|
|
||||||
/* description of an active variable */
|
|
||||||
typedef union Vardesc {
|
|
||||||
struct {
|
|
||||||
TValuefields; /* constant value (if it is a compile-time constant) */
|
|
||||||
lu_byte kind;
|
|
||||||
lu_byte ridx; /* register holding the variable */
|
|
||||||
short pidx; /* index of the variable in the Proto's 'locvars' array */
|
|
||||||
TString *name; /* variable name */
|
|
||||||
} vd;
|
|
||||||
TValue k; /* constant value (if any) */
|
|
||||||
} Vardesc;
|
} Vardesc;
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
/* description of pending goto statements and label statements */
|
/* description of pending goto statements and label statements */
|
||||||
typedef struct Labeldesc {
|
typedef struct Labeldesc {
|
||||||
TString *name; /* label identifier */
|
TString *name; /* label identifier */
|
||||||
int pc; /* position in code */
|
int pc; /* position in code */
|
||||||
int line; /* line where it appeared */
|
int line; /* line where it appeared */
|
||||||
short nactvar; /* number of active variables in that position */
|
lu_byte nactvar; /* local level where it appears in current block */
|
||||||
lu_byte close; /* true for goto that escapes upvalues */
|
|
||||||
} Labeldesc;
|
} Labeldesc;
|
||||||
|
|
||||||
|
|
||||||
@@ -148,7 +93,7 @@ typedef struct Labellist {
|
|||||||
|
|
||||||
/* dynamic structures used by the parser */
|
/* dynamic structures used by the parser */
|
||||||
typedef struct Dyndata {
|
typedef struct Dyndata {
|
||||||
struct { /* list of all active local variables */
|
struct { /* list of active local variables */
|
||||||
Vardesc *arr;
|
Vardesc *arr;
|
||||||
int n;
|
int n;
|
||||||
int size;
|
int size;
|
||||||
@@ -168,27 +113,19 @@ typedef struct FuncState {
|
|||||||
struct FuncState *prev; /* enclosing function */
|
struct FuncState *prev; /* enclosing function */
|
||||||
struct LexState *ls; /* lexical state */
|
struct LexState *ls; /* lexical state */
|
||||||
struct BlockCnt *bl; /* chain of current blocks */
|
struct BlockCnt *bl; /* chain of current blocks */
|
||||||
Table *kcache; /* cache for reusing constants */
|
|
||||||
int pc; /* next position to code (equivalent to 'ncode') */
|
int pc; /* next position to code (equivalent to 'ncode') */
|
||||||
int lasttarget; /* 'label' of last 'jump label' */
|
int lasttarget; /* 'label' of last 'jump label' */
|
||||||
int previousline; /* last line that was saved in 'lineinfo' */
|
int jpc; /* list of pending jumps to 'pc' */
|
||||||
int nk; /* number of elements in 'k' */
|
int nk; /* number of elements in 'k' */
|
||||||
int np; /* number of elements in 'p' */
|
int np; /* number of elements in 'p' */
|
||||||
int nabslineinfo; /* number of elements in 'abslineinfo' */
|
|
||||||
int firstlocal; /* index of first local var (in Dyndata array) */
|
int firstlocal; /* index of first local var (in Dyndata array) */
|
||||||
int firstlabel; /* index of first label (in 'dyd->label->arr') */
|
short nlocvars; /* number of elements in 'f->locvars' */
|
||||||
short ndebugvars; /* number of elements in 'f->locvars' */
|
lu_byte nactvar; /* number of active local variables */
|
||||||
short nactvar; /* number of active variable declarations */
|
|
||||||
lu_byte nups; /* number of upvalues */
|
lu_byte nups; /* number of upvalues */
|
||||||
lu_byte freereg; /* first free register */
|
lu_byte freereg; /* first free register */
|
||||||
lu_byte iwthabs; /* instructions issued since last absolute line info */
|
|
||||||
lu_byte needclose; /* function needs to close upvalues when returning */
|
|
||||||
} FuncState;
|
} FuncState;
|
||||||
|
|
||||||
|
|
||||||
LUAI_FUNC lu_byte luaY_nvarstack (FuncState *fs);
|
|
||||||
LUAI_FUNC void luaY_checklimit (FuncState *fs, int v, int l,
|
|
||||||
const char *what);
|
|
||||||
LUAI_FUNC LClosure *luaY_parser (lua_State *L, ZIO *z, Mbuffer *buff,
|
LUAI_FUNC LClosure *luaY_parser (lua_State *L, ZIO *z, Mbuffer *buff,
|
||||||
Dyndata *dyd, const char *name, int firstchar);
|
Dyndata *dyd, const char *name, int firstchar);
|
||||||
|
|
||||||
|
|||||||
@@ -1,5 +1,5 @@
|
|||||||
/*
|
/*
|
||||||
** $Id: lprefix.h $
|
** $Id: lprefix.h,v 1.2.1.1 2017/04/19 17:20:42 roberto Exp $
|
||||||
** Definitions for Lua code that must come before any other header file
|
** Definitions for Lua code that must come before any other header file
|
||||||
** See Copyright Notice in lua.h
|
** See Copyright Notice in lua.h
|
||||||
*/
|
*/
|
||||||
@@ -33,7 +33,7 @@
|
|||||||
/*
|
/*
|
||||||
** Windows stuff
|
** Windows stuff
|
||||||
*/
|
*/
|
||||||
#if defined(_WIN32) /* { */
|
#if defined(_WIN32) /* { */
|
||||||
|
|
||||||
#if !defined(_CRT_SECURE_NO_WARNINGS)
|
#if !defined(_CRT_SECURE_NO_WARNINGS)
|
||||||
#define _CRT_SECURE_NO_WARNINGS /* avoid warnings about ISO C functions */
|
#define _CRT_SECURE_NO_WARNINGS /* avoid warnings about ISO C functions */
|
||||||
|
|||||||
@@ -1,5 +1,5 @@
|
|||||||
/*
|
/*
|
||||||
** $Id: lstate.c $
|
** $Id: lstate.c,v 2.133.1.1 2017/04/19 17:39:34 roberto Exp $
|
||||||
** Global State
|
** Global State
|
||||||
** See Copyright Notice in lua.h
|
** See Copyright Notice in lua.h
|
||||||
*/
|
*/
|
||||||
@@ -28,55 +28,89 @@
|
|||||||
#include "ltm.h"
|
#include "ltm.h"
|
||||||
|
|
||||||
|
|
||||||
|
#if !defined(LUAI_GCPAUSE)
|
||||||
|
#define LUAI_GCPAUSE 200 /* 200% */
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#if !defined(LUAI_GCMUL)
|
||||||
|
#define LUAI_GCMUL 200 /* GC runs 'twice the speed' of memory allocation */
|
||||||
|
#endif
|
||||||
|
|
||||||
|
|
||||||
|
/*
|
||||||
|
** a macro to help the creation of a unique random seed when a state is
|
||||||
|
** created; the seed is used to randomize hashes.
|
||||||
|
*/
|
||||||
|
#if !defined(luai_makeseed)
|
||||||
|
#include <time.h>
|
||||||
|
#define luai_makeseed() cast(unsigned int, time(NULL))
|
||||||
|
#endif
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
/*
|
||||||
|
** thread state + extra space
|
||||||
|
*/
|
||||||
|
typedef struct LX {
|
||||||
|
lu_byte extra_[LUA_EXTRASPACE];
|
||||||
|
lua_State l;
|
||||||
|
} LX;
|
||||||
|
|
||||||
|
|
||||||
|
/*
|
||||||
|
** Main thread combines a thread state and the global state
|
||||||
|
*/
|
||||||
|
typedef struct LG {
|
||||||
|
LX l;
|
||||||
|
global_State g;
|
||||||
|
} LG;
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
#define fromstate(L) (cast(LX *, cast(lu_byte *, (L)) - offsetof(LX, l)))
|
#define fromstate(L) (cast(LX *, cast(lu_byte *, (L)) - offsetof(LX, l)))
|
||||||
|
|
||||||
|
|
||||||
/*
|
/*
|
||||||
** these macros allow user-specific actions when a thread is
|
** Compute an initial seed as random as possible. Rely on Address Space
|
||||||
** created/deleted
|
** Layout Randomization (if present) to increase randomness..
|
||||||
*/
|
*/
|
||||||
#if !defined(luai_userstateopen)
|
#define addbuff(b,p,e) \
|
||||||
#define luai_userstateopen(L) ((void)L)
|
{ size_t t = cast(size_t, e); \
|
||||||
#endif
|
memcpy(b + p, &t, sizeof(t)); p += sizeof(t); }
|
||||||
|
|
||||||
#if !defined(luai_userstateclose)
|
static unsigned int makeseed (lua_State *L) {
|
||||||
#define luai_userstateclose(L) ((void)L)
|
char buff[4 * sizeof(size_t)];
|
||||||
#endif
|
unsigned int h = luai_makeseed();
|
||||||
|
int p = 0;
|
||||||
#if !defined(luai_userstatethread)
|
addbuff(buff, p, L); /* heap variable */
|
||||||
#define luai_userstatethread(L,L1) ((void)L)
|
addbuff(buff, p, &h); /* local variable */
|
||||||
#endif
|
addbuff(buff, p, luaO_nilobject); /* global variable */
|
||||||
|
addbuff(buff, p, &lua_newstate); /* public function */
|
||||||
#if !defined(luai_userstatefree)
|
lua_assert(p == sizeof(buff));
|
||||||
#define luai_userstatefree(L,L1) ((void)L)
|
return luaS_hash(buff, p, h);
|
||||||
#endif
|
}
|
||||||
|
|
||||||
|
|
||||||
/*
|
/*
|
||||||
** set GCdebt to a new value keeping the real number of allocated
|
** set GCdebt to a new value keeping the value (totalbytes + GCdebt)
|
||||||
** objects (GCtotalobjs - GCdebt) invariant and avoiding overflows in
|
** invariant (and avoiding underflows in 'totalbytes')
|
||||||
** 'GCtotalobjs'.
|
|
||||||
*/
|
*/
|
||||||
void luaE_setdebt (global_State *g, l_mem debt) {
|
void luaE_setdebt (global_State *g, l_mem debt) {
|
||||||
l_mem tb = gettotalbytes(g);
|
l_mem tb = gettotalbytes(g);
|
||||||
lua_assert(tb > 0);
|
lua_assert(tb > 0);
|
||||||
if (debt > MAX_LMEM - tb)
|
if (debt < tb - MAX_LMEM)
|
||||||
debt = MAX_LMEM - tb; /* will make GCtotalbytes == MAX_LMEM */
|
debt = tb - MAX_LMEM; /* will make 'totalbytes == MAX_LMEM' */
|
||||||
g->GCtotalbytes = tb + debt;
|
g->totalbytes = tb - debt;
|
||||||
g->GCdebt = debt;
|
g->GCdebt = debt;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
CallInfo *luaE_extendCI (lua_State *L) {
|
CallInfo *luaE_extendCI (lua_State *L) {
|
||||||
CallInfo *ci;
|
CallInfo *ci = luaM_new(L, CallInfo);
|
||||||
lua_assert(L->ci->next == NULL);
|
|
||||||
ci = luaM_new(L, CallInfo);
|
|
||||||
lua_assert(L->ci->next == NULL);
|
lua_assert(L->ci->next == NULL);
|
||||||
L->ci->next = ci;
|
L->ci->next = ci;
|
||||||
ci->previous = L->ci;
|
ci->previous = L->ci;
|
||||||
ci->next = NULL;
|
ci->next = NULL;
|
||||||
ci->u.l.trap = 0;
|
|
||||||
L->nci++;
|
L->nci++;
|
||||||
return ci;
|
return ci;
|
||||||
}
|
}
|
||||||
@@ -85,7 +119,7 @@ CallInfo *luaE_extendCI (lua_State *L) {
|
|||||||
/*
|
/*
|
||||||
** free all CallInfo structures not in use by a thread
|
** free all CallInfo structures not in use by a thread
|
||||||
*/
|
*/
|
||||||
static void freeCI (lua_State *L) {
|
void luaE_freeCI (lua_State *L) {
|
||||||
CallInfo *ci = L->ci;
|
CallInfo *ci = L->ci;
|
||||||
CallInfo *next = ci->next;
|
CallInfo *next = ci->next;
|
||||||
ci->next = NULL;
|
ci->next = NULL;
|
||||||
@@ -98,85 +132,49 @@ static void freeCI (lua_State *L) {
|
|||||||
|
|
||||||
|
|
||||||
/*
|
/*
|
||||||
** free half of the CallInfo structures not in use by a thread,
|
** free half of the CallInfo structures not in use by a thread
|
||||||
** keeping the first one.
|
|
||||||
*/
|
*/
|
||||||
void luaE_shrinkCI (lua_State *L) {
|
void luaE_shrinkCI (lua_State *L) {
|
||||||
CallInfo *ci = L->ci->next; /* first free CallInfo */
|
CallInfo *ci = L->ci;
|
||||||
CallInfo *next;
|
CallInfo *next2; /* next's next */
|
||||||
if (ci == NULL)
|
/* while there are two nexts */
|
||||||
return; /* no extra elements */
|
while (ci->next != NULL && (next2 = ci->next->next) != NULL) {
|
||||||
while ((next = ci->next) != NULL) { /* two extra elements? */
|
luaM_free(L, ci->next); /* free next */
|
||||||
CallInfo *next2 = next->next; /* next's next */
|
|
||||||
ci->next = next2; /* remove next from the list */
|
|
||||||
L->nci--;
|
L->nci--;
|
||||||
luaM_free(L, next); /* free next */
|
ci->next = next2; /* remove 'next' from the list */
|
||||||
if (next2 == NULL)
|
next2->previous = ci;
|
||||||
break; /* no more elements */
|
ci = next2; /* keep next's next */
|
||||||
else {
|
|
||||||
next2->previous = ci;
|
|
||||||
ci = next2; /* continue */
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/*
|
|
||||||
** Called when 'getCcalls(L)' larger or equal to LUAI_MAXCCALLS.
|
|
||||||
** If equal, raises an overflow error. If value is larger than
|
|
||||||
** LUAI_MAXCCALLS (which means it is handling an overflow) but
|
|
||||||
** not much larger, does not report an error (to allow overflow
|
|
||||||
** handling to work).
|
|
||||||
*/
|
|
||||||
void luaE_checkcstack (lua_State *L) {
|
|
||||||
if (getCcalls(L) == LUAI_MAXCCALLS)
|
|
||||||
luaG_runerror(L, "C stack overflow");
|
|
||||||
else if (getCcalls(L) >= (LUAI_MAXCCALLS / 10 * 11))
|
|
||||||
luaD_errerr(L); /* error while handling stack error */
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
LUAI_FUNC void luaE_incCstack (lua_State *L) {
|
|
||||||
L->nCcalls++;
|
|
||||||
if (l_unlikely(getCcalls(L) >= LUAI_MAXCCALLS))
|
|
||||||
luaE_checkcstack(L);
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
static void resetCI (lua_State *L) {
|
|
||||||
CallInfo *ci = L->ci = &L->base_ci;
|
|
||||||
ci->func.p = L->stack.p;
|
|
||||||
setnilvalue(s2v(ci->func.p)); /* 'function' entry for basic 'ci' */
|
|
||||||
ci->top.p = ci->func.p + 1 + LUA_MINSTACK; /* +1 for 'function' entry */
|
|
||||||
ci->u.c.k = NULL;
|
|
||||||
ci->callstatus = CIST_C;
|
|
||||||
L->status = LUA_OK;
|
|
||||||
L->errfunc = 0; /* stack unwind can "throw away" the error function */
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
static void stack_init (lua_State *L1, lua_State *L) {
|
static void stack_init (lua_State *L1, lua_State *L) {
|
||||||
int i;
|
int i; CallInfo *ci;
|
||||||
/* initialize stack array */
|
/* initialize stack array */
|
||||||
L1->stack.p = luaM_newvector(L, BASIC_STACK_SIZE + EXTRA_STACK, StackValue);
|
L1->stack = luaM_newvector(L, BASIC_STACK_SIZE, TValue);
|
||||||
L1->tbclist.p = L1->stack.p;
|
L1->stacksize = BASIC_STACK_SIZE;
|
||||||
for (i = 0; i < BASIC_STACK_SIZE + EXTRA_STACK; i++)
|
for (i = 0; i < BASIC_STACK_SIZE; i++)
|
||||||
setnilvalue(s2v(L1->stack.p + i)); /* erase new stack */
|
setnilvalue(L1->stack + i); /* erase new stack */
|
||||||
L1->stack_last.p = L1->stack.p + BASIC_STACK_SIZE;
|
L1->top = L1->stack;
|
||||||
|
L1->stack_last = L1->stack + L1->stacksize - EXTRA_STACK;
|
||||||
/* initialize first ci */
|
/* initialize first ci */
|
||||||
resetCI(L1);
|
ci = &L1->base_ci;
|
||||||
L1->top.p = L1->stack.p + 1; /* +1 for 'function' entry */
|
ci->next = ci->previous = NULL;
|
||||||
|
ci->callstatus = 0;
|
||||||
|
ci->func = L1->top;
|
||||||
|
setnilvalue(L1->top++); /* 'function' entry for this 'ci' */
|
||||||
|
ci->top = L1->top + LUA_MINSTACK;
|
||||||
|
L1->ci = ci;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
static void freestack (lua_State *L) {
|
static void freestack (lua_State *L) {
|
||||||
if (L->stack.p == NULL)
|
if (L->stack == NULL)
|
||||||
return; /* stack not completely built yet */
|
return; /* stack not completely built yet */
|
||||||
L->ci = &L->base_ci; /* free the entire 'ci' list */
|
L->ci = &L->base_ci; /* free the entire 'ci' list */
|
||||||
freeCI(L);
|
luaE_freeCI(L);
|
||||||
lua_assert(L->nci == 0);
|
lua_assert(L->nci == 0);
|
||||||
/* free stack */
|
luaM_freearray(L, L->stack, L->stacksize); /* free stack array */
|
||||||
luaM_freearray(L, L->stack.p, cast_sizet(stacksize(L) + EXTRA_STACK));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@@ -184,25 +182,23 @@ static void freestack (lua_State *L) {
|
|||||||
** Create registry table and its predefined values
|
** Create registry table and its predefined values
|
||||||
*/
|
*/
|
||||||
static void init_registry (lua_State *L, global_State *g) {
|
static void init_registry (lua_State *L, global_State *g) {
|
||||||
|
TValue temp;
|
||||||
/* create registry */
|
/* create registry */
|
||||||
TValue aux;
|
|
||||||
Table *registry = luaH_new(L);
|
Table *registry = luaH_new(L);
|
||||||
sethvalue(L, &g->l_registry, registry);
|
sethvalue(L, &g->l_registry, registry);
|
||||||
luaH_resize(L, registry, LUA_RIDX_LAST, 0);
|
luaH_resize(L, registry, LUA_RIDX_LAST, 0);
|
||||||
/* registry[1] = false */
|
|
||||||
setbfvalue(&aux);
|
|
||||||
luaH_setint(L, registry, 1, &aux);
|
|
||||||
/* registry[LUA_RIDX_MAINTHREAD] = L */
|
/* registry[LUA_RIDX_MAINTHREAD] = L */
|
||||||
setthvalue(L, &aux, L);
|
setthvalue(L, &temp, L); /* temp = L */
|
||||||
luaH_setint(L, registry, LUA_RIDX_MAINTHREAD, &aux);
|
luaH_setint(L, registry, LUA_RIDX_MAINTHREAD, &temp);
|
||||||
/* registry[LUA_RIDX_GLOBALS] = new table (table of globals) */
|
/* registry[LUA_RIDX_GLOBALS] = table of globals */
|
||||||
sethvalue(L, &aux, luaH_new(L));
|
sethvalue(L, &temp, luaH_new(L)); /* temp = new table (global table) */
|
||||||
luaH_setint(L, registry, LUA_RIDX_GLOBALS, &aux);
|
luaH_setint(L, registry, LUA_RIDX_GLOBALS, &temp);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/*
|
/*
|
||||||
** open parts of the state that may cause memory-allocation errors.
|
** open parts of the state that may cause memory-allocation errors.
|
||||||
|
** ('g->version' != NULL flags that the state was completely build)
|
||||||
*/
|
*/
|
||||||
static void f_luaopen (lua_State *L, void *ud) {
|
static void f_luaopen (lua_State *L, void *ud) {
|
||||||
global_State *g = G(L);
|
global_State *g = G(L);
|
||||||
@@ -212,8 +208,8 @@ static void f_luaopen (lua_State *L, void *ud) {
|
|||||||
luaS_init(L);
|
luaS_init(L);
|
||||||
luaT_init(L);
|
luaT_init(L);
|
||||||
luaX_init(L);
|
luaX_init(L);
|
||||||
g->gcstp = 0; /* allow gc */
|
g->gcrunning = 1; /* allow gc */
|
||||||
setnilvalue(&g->nilvalue); /* now state is complete */
|
g->version = lua_version(NULL);
|
||||||
luai_userstateopen(L);
|
luai_userstateopen(L);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -224,63 +220,52 @@ static void f_luaopen (lua_State *L, void *ud) {
|
|||||||
*/
|
*/
|
||||||
static void preinit_thread (lua_State *L, global_State *g) {
|
static void preinit_thread (lua_State *L, global_State *g) {
|
||||||
G(L) = g;
|
G(L) = g;
|
||||||
L->stack.p = NULL;
|
L->stack = NULL;
|
||||||
L->ci = NULL;
|
L->ci = NULL;
|
||||||
L->nci = 0;
|
L->nci = 0;
|
||||||
|
L->stacksize = 0;
|
||||||
L->twups = L; /* thread has no upvalues */
|
L->twups = L; /* thread has no upvalues */
|
||||||
L->nCcalls = 0;
|
|
||||||
L->errorJmp = NULL;
|
L->errorJmp = NULL;
|
||||||
|
L->nCcalls = 0;
|
||||||
L->hook = NULL;
|
L->hook = NULL;
|
||||||
L->hookmask = 0;
|
L->hookmask = 0;
|
||||||
L->basehookcount = 0;
|
L->basehookcount = 0;
|
||||||
L->allowhook = 1;
|
L->allowhook = 1;
|
||||||
resethookcount(L);
|
resethookcount(L);
|
||||||
L->openupval = NULL;
|
L->openupval = NULL;
|
||||||
|
L->nny = 1;
|
||||||
L->status = LUA_OK;
|
L->status = LUA_OK;
|
||||||
L->errfunc = 0;
|
L->errfunc = 0;
|
||||||
L->oldpc = 0;
|
|
||||||
L->base_ci.previous = L->base_ci.next = NULL;
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
lu_mem luaE_threadsize (lua_State *L) {
|
|
||||||
lu_mem sz = cast(lu_mem, sizeof(LX))
|
|
||||||
+ cast_uint(L->nci) * sizeof(CallInfo);
|
|
||||||
if (L->stack.p != NULL)
|
|
||||||
sz += cast_uint(stacksize(L) + EXTRA_STACK) * sizeof(StackValue);
|
|
||||||
return sz;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
static void close_state (lua_State *L) {
|
static void close_state (lua_State *L) {
|
||||||
global_State *g = G(L);
|
global_State *g = G(L);
|
||||||
if (!completestate(g)) /* closing a partially built state? */
|
luaF_close(L, L->stack); /* close all upvalues for this thread */
|
||||||
luaC_freeallobjects(L); /* just collect its objects */
|
luaC_freeallobjects(L); /* collect all objects */
|
||||||
else { /* closing a fully built state */
|
if (g->version) /* closing a fully built state? */
|
||||||
resetCI(L);
|
|
||||||
luaD_closeprotected(L, 1, LUA_OK); /* close all upvalues */
|
|
||||||
L->top.p = L->stack.p + 1; /* empty the stack to run finalizers */
|
|
||||||
luaC_freeallobjects(L); /* collect all objects */
|
|
||||||
luai_userstateclose(L);
|
luai_userstateclose(L);
|
||||||
}
|
luaM_freearray(L, G(L)->strt.hash, G(L)->strt.size);
|
||||||
luaM_freearray(L, G(L)->strt.hash, cast_sizet(G(L)->strt.size));
|
|
||||||
freestack(L);
|
freestack(L);
|
||||||
lua_assert(gettotalbytes(g) == sizeof(global_State));
|
lua_assert(gettotalbytes(g) == sizeof(LG));
|
||||||
(*g->frealloc)(g->ud, g, sizeof(global_State), 0); /* free main block */
|
(*g->frealloc)(g->ud, fromstate(L), sizeof(LG), 0); /* free main block */
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
LUA_API lua_State *lua_newthread (lua_State *L) {
|
LUA_API lua_State *lua_newthread (lua_State *L) {
|
||||||
global_State *g = G(L);
|
global_State *g = G(L);
|
||||||
GCObject *o;
|
|
||||||
lua_State *L1;
|
lua_State *L1;
|
||||||
lua_lock(L);
|
lua_lock(L);
|
||||||
luaC_checkGC(L);
|
luaC_checkGC(L);
|
||||||
/* create new thread */
|
/* create new thread */
|
||||||
o = luaC_newobjdt(L, LUA_TTHREAD, sizeof(LX), offsetof(LX, l));
|
L1 = &cast(LX *, luaM_newobject(L, LUA_TTHREAD, sizeof(LX)))->l;
|
||||||
L1 = gco2th(o);
|
L1->marked = luaC_white(g);
|
||||||
|
L1->tt = LUA_TTHREAD;
|
||||||
|
/* link it on list 'allgc' */
|
||||||
|
L1->next = g->allgc;
|
||||||
|
g->allgc = obj2gco(L1);
|
||||||
/* anchor it on L stack */
|
/* anchor it on L stack */
|
||||||
setthvalue2s(L, L->top.p, L1);
|
setthvalue(L, L->top, L1);
|
||||||
api_incr_top(L);
|
api_incr_top(L);
|
||||||
preinit_thread(L1, g);
|
preinit_thread(L1, g);
|
||||||
L1->hookmask = L->hookmask;
|
L1->hookmask = L->hookmask;
|
||||||
@@ -288,7 +273,7 @@ LUA_API lua_State *lua_newthread (lua_State *L) {
|
|||||||
L1->hook = L->hook;
|
L1->hook = L->hook;
|
||||||
resethookcount(L1);
|
resethookcount(L1);
|
||||||
/* initialize L1 extra space */
|
/* initialize L1 extra space */
|
||||||
memcpy(lua_getextraspace(L1), lua_getextraspace(mainthread(g)),
|
memcpy(lua_getextraspace(L1), lua_getextraspace(g->mainthread),
|
||||||
LUA_EXTRASPACE);
|
LUA_EXTRASPACE);
|
||||||
luai_userstatethread(L, L1);
|
luai_userstatethread(L, L1);
|
||||||
stack_init(L1, L); /* init stack */
|
stack_init(L1, L); /* init stack */
|
||||||
@@ -299,7 +284,7 @@ LUA_API lua_State *lua_newthread (lua_State *L) {
|
|||||||
|
|
||||||
void luaE_freethread (lua_State *L, lua_State *L1) {
|
void luaE_freethread (lua_State *L, lua_State *L1) {
|
||||||
LX *l = fromstate(L1);
|
LX *l = fromstate(L1);
|
||||||
luaF_closeupval(L1, L1->stack.p); /* close all upvalues */
|
luaF_close(L1, L1->stack); /* close all upvalues for this thread */
|
||||||
lua_assert(L1->openupval == NULL);
|
lua_assert(L1->openupval == NULL);
|
||||||
luai_userstatefree(L, L1);
|
luai_userstatefree(L, L1);
|
||||||
freestack(L1);
|
freestack(L1);
|
||||||
@@ -307,78 +292,43 @@ void luaE_freethread (lua_State *L, lua_State *L1) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
TStatus luaE_resetthread (lua_State *L, TStatus status) {
|
LUA_API lua_State *lua_newstate (lua_Alloc f, void *ud) {
|
||||||
resetCI(L);
|
|
||||||
if (status == LUA_YIELD)
|
|
||||||
status = LUA_OK;
|
|
||||||
status = luaD_closeprotected(L, 1, status);
|
|
||||||
if (status != LUA_OK) /* errors? */
|
|
||||||
luaD_seterrorobj(L, status, L->stack.p + 1);
|
|
||||||
else
|
|
||||||
L->top.p = L->stack.p + 1;
|
|
||||||
luaD_reallocstack(L, cast_int(L->ci->top.p - L->stack.p), 0);
|
|
||||||
return status;
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
LUA_API int lua_closethread (lua_State *L, lua_State *from) {
|
|
||||||
TStatus status;
|
|
||||||
lua_lock(L);
|
|
||||||
L->nCcalls = (from) ? getCcalls(from) : 0;
|
|
||||||
status = luaE_resetthread(L, L->status);
|
|
||||||
if (L == from) /* closing itself? */
|
|
||||||
luaD_throwbaselevel(L, status);
|
|
||||||
lua_unlock(L);
|
|
||||||
return APIstatus(status);
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
LUA_API lua_State *lua_newstate (lua_Alloc f, void *ud, unsigned seed) {
|
|
||||||
int i;
|
int i;
|
||||||
lua_State *L;
|
lua_State *L;
|
||||||
global_State *g = cast(global_State*,
|
global_State *g;
|
||||||
(*f)(ud, NULL, LUA_TTHREAD, sizeof(global_State)));
|
LG *l = cast(LG *, (*f)(ud, NULL, LUA_TTHREAD, sizeof(LG)));
|
||||||
if (g == NULL) return NULL;
|
if (l == NULL) return NULL;
|
||||||
L = &g->mainth.l;
|
L = &l->l.l;
|
||||||
L->tt = LUA_VTHREAD;
|
g = &l->g;
|
||||||
|
L->next = NULL;
|
||||||
|
L->tt = LUA_TTHREAD;
|
||||||
g->currentwhite = bitmask(WHITE0BIT);
|
g->currentwhite = bitmask(WHITE0BIT);
|
||||||
L->marked = luaC_white(g);
|
L->marked = luaC_white(g);
|
||||||
preinit_thread(L, g);
|
preinit_thread(L, g);
|
||||||
g->allgc = obj2gco(L); /* by now, only object is the main thread */
|
|
||||||
L->next = NULL;
|
|
||||||
incnny(L); /* main thread is always non yieldable */
|
|
||||||
g->frealloc = f;
|
g->frealloc = f;
|
||||||
g->ud = ud;
|
g->ud = ud;
|
||||||
g->warnf = NULL;
|
g->mainthread = L;
|
||||||
g->ud_warn = NULL;
|
g->seed = makeseed(L);
|
||||||
g->seed = seed;
|
g->gcrunning = 0; /* no GC while building state */
|
||||||
g->gcstp = GCSTPGC; /* no GC while building state */
|
g->GCestimate = 0;
|
||||||
g->strt.size = g->strt.nuse = 0;
|
g->strt.size = g->strt.nuse = 0;
|
||||||
g->strt.hash = NULL;
|
g->strt.hash = NULL;
|
||||||
setnilvalue(&g->l_registry);
|
setnilvalue(&g->l_registry);
|
||||||
g->panic = NULL;
|
g->panic = NULL;
|
||||||
|
g->version = NULL;
|
||||||
g->gcstate = GCSpause;
|
g->gcstate = GCSpause;
|
||||||
g->gckind = KGC_INC;
|
g->gckind = KGC_NORMAL;
|
||||||
g->gcstopem = 0;
|
g->allgc = g->finobj = g->tobefnz = g->fixedgc = NULL;
|
||||||
g->gcemergency = 0;
|
|
||||||
g->finobj = g->tobefnz = g->fixedgc = NULL;
|
|
||||||
g->firstold1 = g->survival = g->old1 = g->reallyold = NULL;
|
|
||||||
g->finobjsur = g->finobjold1 = g->finobjrold = NULL;
|
|
||||||
g->sweepgc = NULL;
|
g->sweepgc = NULL;
|
||||||
g->gray = g->grayagain = NULL;
|
g->gray = g->grayagain = NULL;
|
||||||
g->weak = g->ephemeron = g->allweak = NULL;
|
g->weak = g->ephemeron = g->allweak = NULL;
|
||||||
g->twups = NULL;
|
g->twups = NULL;
|
||||||
g->GCtotalbytes = sizeof(global_State);
|
g->totalbytes = sizeof(LG);
|
||||||
g->GCmarked = 0;
|
|
||||||
g->GCdebt = 0;
|
g->GCdebt = 0;
|
||||||
setivalue(&g->nilvalue, 0); /* to signal that state is not yet built */
|
g->gcfinnum = 0;
|
||||||
setgcparam(g, PAUSE, LUAI_GCPAUSE);
|
g->gcpause = LUAI_GCPAUSE;
|
||||||
setgcparam(g, STEPMUL, LUAI_GCMUL);
|
g->gcstepmul = LUAI_GCMUL;
|
||||||
setgcparam(g, STEPSIZE, LUAI_GCSTEPSIZE);
|
for (i=0; i < LUA_NUMTAGS; i++) g->mt[i] = NULL;
|
||||||
setgcparam(g, MINORMUL, LUAI_GENMINORMUL);
|
|
||||||
setgcparam(g, MINORMAJOR, LUAI_MINORMAJOR);
|
|
||||||
setgcparam(g, MAJORMINOR, LUAI_MAJORMINOR);
|
|
||||||
for (i=0; i < LUA_NUMTYPES; i++) g->mt[i] = NULL;
|
|
||||||
if (luaD_rawrunprotected(L, f_luaopen, NULL) != LUA_OK) {
|
if (luaD_rawrunprotected(L, f_luaopen, NULL) != LUA_OK) {
|
||||||
/* memory allocation error: free partial state */
|
/* memory allocation error: free partial state */
|
||||||
close_state(L);
|
close_state(L);
|
||||||
@@ -389,32 +339,9 @@ LUA_API lua_State *lua_newstate (lua_Alloc f, void *ud, unsigned seed) {
|
|||||||
|
|
||||||
|
|
||||||
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 */
|
||||||
lua_lock(L);
|
lua_lock(L);
|
||||||
L = mainthread(G(L)); /* only the main thread can be closed */
|
|
||||||
close_state(L);
|
close_state(L);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
void luaE_warning (lua_State *L, const char *msg, int tocont) {
|
|
||||||
lua_WarnFunction wf = G(L)->warnf;
|
|
||||||
if (wf != NULL)
|
|
||||||
wf(G(L)->ud_warn, msg, tocont);
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
/*
|
|
||||||
** Generate a warning from an error message
|
|
||||||
*/
|
|
||||||
void luaE_warnerror (lua_State *L, const char *where) {
|
|
||||||
TValue *errobj = s2v(L->top.p - 1); /* error object */
|
|
||||||
const char *msg = (ttisstring(errobj))
|
|
||||||
? getstr(tsvalue(errobj))
|
|
||||||
: "error object is not a string";
|
|
||||||
/* produce warning "error in %s (%s)" (where, msg) */
|
|
||||||
luaE_warning(L, "error in ", 1);
|
|
||||||
luaE_warning(L, where, 1);
|
|
||||||
luaE_warning(L, " (", 1);
|
|
||||||
luaE_warning(L, msg, 1);
|
|
||||||
luaE_warning(L, ")", 0);
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|||||||
@@ -1,5 +1,5 @@
|
|||||||
/*
|
/*
|
||||||
** $Id: lstate.h $
|
** $Id: lstate.h,v 2.133.1.1 2017/04/19 17:39:34 roberto Exp $
|
||||||
** Global State
|
** Global State
|
||||||
** See Copyright Notice in lua.h
|
** See Copyright Notice in lua.h
|
||||||
*/
|
*/
|
||||||
@@ -9,17 +9,13 @@
|
|||||||
|
|
||||||
#include "lua.h"
|
#include "lua.h"
|
||||||
|
|
||||||
|
|
||||||
/* Some header files included here need this definition */
|
|
||||||
typedef struct CallInfo CallInfo;
|
|
||||||
|
|
||||||
|
|
||||||
#include "lobject.h"
|
#include "lobject.h"
|
||||||
#include "ltm.h"
|
#include "ltm.h"
|
||||||
#include "lzio.h"
|
#include "lzio.h"
|
||||||
|
|
||||||
|
|
||||||
/*
|
/*
|
||||||
|
|
||||||
** Some notes about garbage-collected objects: All objects in Lua must
|
** Some notes about garbage-collected objects: All objects in Lua must
|
||||||
** be kept somehow accessible until being freed, so all objects always
|
** be kept somehow accessible until being freed, so all objects always
|
||||||
** belong to one (and only one) of these lists, using field 'next' of
|
** belong to one (and only one) of these lists, using field 'next' of
|
||||||
@@ -31,44 +27,12 @@ typedef struct CallInfo CallInfo;
|
|||||||
** 'fixedgc': all objects that are not to be collected (currently
|
** 'fixedgc': all objects that are not to be collected (currently
|
||||||
** only small strings, such as reserved words).
|
** only small strings, such as reserved words).
|
||||||
**
|
**
|
||||||
** For the generational collector, some of these lists have marks for
|
|
||||||
** generations. Each mark points to the first element in the list for
|
|
||||||
** that particular generation; that generation goes until the next mark.
|
|
||||||
**
|
|
||||||
** 'allgc' -> 'survival': new objects;
|
|
||||||
** 'survival' -> 'old': objects that survived one collection;
|
|
||||||
** 'old1' -> 'reallyold': objects that became old in last collection;
|
|
||||||
** 'reallyold' -> NULL: objects old for more than one cycle.
|
|
||||||
**
|
|
||||||
** 'finobj' -> 'finobjsur': new objects marked for finalization;
|
|
||||||
** 'finobjsur' -> 'finobjold1': survived """";
|
|
||||||
** 'finobjold1' -> 'finobjrold': just old """";
|
|
||||||
** 'finobjrold' -> NULL: really old """".
|
|
||||||
**
|
|
||||||
** All lists can contain elements older than their main ages, due
|
|
||||||
** to 'luaC_checkfinalizer' and 'udata2finalize', which move
|
|
||||||
** objects between the normal lists and the "marked for finalization"
|
|
||||||
** lists. Moreover, barriers can age young objects in young lists as
|
|
||||||
** OLD0, which then become OLD1. However, a list never contains
|
|
||||||
** elements younger than their main ages.
|
|
||||||
**
|
|
||||||
** The generational collector also uses a pointer 'firstold1', which
|
|
||||||
** points to the first OLD1 object in the list. It is used to optimize
|
|
||||||
** 'markold'. (Potentially OLD1 objects can be anywhere between 'allgc'
|
|
||||||
** and 'reallyold', but often the list has no OLD1 objects or they are
|
|
||||||
** after 'old1'.) Note the difference between it and 'old1':
|
|
||||||
** 'firstold1': no OLD1 objects before this point; there can be all
|
|
||||||
** ages after it.
|
|
||||||
** 'old1': no objects younger than OLD1 after this point.
|
|
||||||
*/
|
|
||||||
|
|
||||||
/*
|
|
||||||
** Moreover, there is another set of lists that control gray objects.
|
** Moreover, there is another set of lists that control gray objects.
|
||||||
** These lists are linked by fields 'gclist'. (All objects that
|
** These lists are linked by fields 'gclist'. (All objects that
|
||||||
** can become gray have such a field. The field is not the same
|
** can become gray have such a field. The field is not the same
|
||||||
** in all objects, but it always has this name.) Any gray object
|
** in all objects, but it always has this name.) Any gray object
|
||||||
** must belong to one of these lists, and all objects in these lists
|
** must belong to one of these lists, and all objects in these lists
|
||||||
** must be gray (with two exceptions explained below):
|
** must be gray:
|
||||||
**
|
**
|
||||||
** 'gray': regular gray objects, still waiting to be visited.
|
** 'gray': regular gray objects, still waiting to be visited.
|
||||||
** 'grayagain': objects that must be revisited at the atomic phase.
|
** 'grayagain': objects that must be revisited at the atomic phase.
|
||||||
@@ -79,46 +43,11 @@ typedef struct CallInfo CallInfo;
|
|||||||
** 'weak': tables with weak values to be cleared;
|
** 'weak': tables with weak values to be cleared;
|
||||||
** 'ephemeron': ephemeron tables with white->white entries;
|
** 'ephemeron': ephemeron tables with white->white entries;
|
||||||
** 'allweak': tables with weak keys and/or weak values to be cleared.
|
** 'allweak': tables with weak keys and/or weak values to be cleared.
|
||||||
**
|
** The last three lists are used only during the atomic phase.
|
||||||
** The exceptions to that "gray rule" are:
|
|
||||||
** - TOUCHED2 objects in generational mode stay in a gray list (because
|
|
||||||
** they must be visited again at the end of the cycle), but they are
|
|
||||||
** marked black because assignments to them must activate barriers (to
|
|
||||||
** move them back to TOUCHED1).
|
|
||||||
** - Open upvalues are kept gray to avoid barriers, but they stay out
|
|
||||||
** of gray lists. (They don't even have a 'gclist' field.)
|
|
||||||
*/
|
*/
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
/*
|
|
||||||
** About 'nCcalls': This count has two parts: the lower 16 bits counts
|
|
||||||
** the number of recursive invocations in the C stack; the higher
|
|
||||||
** 16 bits counts the number of non-yieldable calls in the stack.
|
|
||||||
** (They are together so that we can change and save both with one
|
|
||||||
** instruction.)
|
|
||||||
*/
|
|
||||||
|
|
||||||
|
|
||||||
/* true if this thread does not have non-yieldable calls in the stack */
|
|
||||||
#define yieldable(L) (((L)->nCcalls & 0xffff0000) == 0)
|
|
||||||
|
|
||||||
/* real number of C calls */
|
|
||||||
#define getCcalls(L) ((L)->nCcalls & 0xffff)
|
|
||||||
|
|
||||||
|
|
||||||
/* Increment the number of non-yieldable calls */
|
|
||||||
#define incnny(L) ((L)->nCcalls += 0x10000)
|
|
||||||
|
|
||||||
/* Decrement the number of non-yieldable calls */
|
|
||||||
#define decnny(L) ((L)->nCcalls -= 0x10000)
|
|
||||||
|
|
||||||
/* Non-yieldable call increment */
|
|
||||||
#define nyci (0x10000 | 1)
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
struct lua_longjmp; /* defined in ldo.c */
|
struct lua_longjmp; /* defined in ldo.c */
|
||||||
|
|
||||||
|
|
||||||
@@ -132,67 +61,42 @@ struct lua_longjmp; /* defined in ldo.c */
|
|||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
|
||||||
/*
|
/* extra stack space to handle TM calls and some other extras */
|
||||||
** Extra stack space to handle TM calls and some other extras. This
|
|
||||||
** space is not included in 'stack_last'. It is used only to avoid stack
|
|
||||||
** checks, either because the element will be promptly popped or because
|
|
||||||
** there will be a stack check soon after the push. Function frames
|
|
||||||
** never use this extra space, so it does not need to be kept clean.
|
|
||||||
*/
|
|
||||||
#define EXTRA_STACK 5
|
#define EXTRA_STACK 5
|
||||||
|
|
||||||
|
|
||||||
/*
|
|
||||||
** Size of cache for strings in the API. 'N' is the number of
|
|
||||||
** sets (better be a prime) and "M" is the size of each set.
|
|
||||||
** (M == 1 makes a direct cache.)
|
|
||||||
*/
|
|
||||||
#if !defined(STRCACHE_N)
|
|
||||||
#define STRCACHE_N 53
|
|
||||||
#define STRCACHE_M 2
|
|
||||||
#endif
|
|
||||||
|
|
||||||
|
|
||||||
#define BASIC_STACK_SIZE (2*LUA_MINSTACK)
|
#define BASIC_STACK_SIZE (2*LUA_MINSTACK)
|
||||||
|
|
||||||
#define stacksize(th) cast_int((th)->stack_last.p - (th)->stack.p)
|
|
||||||
|
|
||||||
|
|
||||||
/* kinds of Garbage Collection */
|
/* kinds of Garbage Collection */
|
||||||
#define KGC_INC 0 /* incremental gc */
|
#define KGC_NORMAL 0
|
||||||
#define KGC_GENMINOR 1 /* generational gc in minor (regular) mode */
|
#define KGC_EMERGENCY 1 /* gc was forced by an allocation failure */
|
||||||
#define KGC_GENMAJOR 2 /* generational in major mode */
|
|
||||||
|
|
||||||
|
|
||||||
typedef struct stringtable {
|
typedef struct stringtable {
|
||||||
TString **hash; /* array of buckets (linked lists of strings) */
|
TString **hash;
|
||||||
int nuse; /* number of elements */
|
int nuse; /* number of elements */
|
||||||
int size; /* number of buckets */
|
int size;
|
||||||
} stringtable;
|
} stringtable;
|
||||||
|
|
||||||
|
|
||||||
/*
|
/*
|
||||||
** Information about a call.
|
** Information about a call.
|
||||||
** About union 'u':
|
** When a thread yields, 'func' is adjusted to pretend that the
|
||||||
** - field 'l' is used only for Lua functions;
|
** top function has only the yielded values in its stack; in that
|
||||||
** - field 'c' is used only for C functions.
|
** case, the actual 'func' value is saved in field 'extra'.
|
||||||
** About union 'u2':
|
** When a function calls another with a continuation, 'extra' keeps
|
||||||
** - field 'funcidx' is used only by C functions while doing a
|
** the function index so that, in case of errors, the continuation
|
||||||
** protected call;
|
** function can be called with the correct top.
|
||||||
** - field 'nyield' is used only while a function is "doing" an
|
|
||||||
** yield (from the yield until the next resume);
|
|
||||||
** - field 'nres' is used only while closing tbc variables when
|
|
||||||
** returning from a function;
|
|
||||||
*/
|
*/
|
||||||
struct CallInfo {
|
typedef struct CallInfo {
|
||||||
StkIdRel func; /* function index in the stack */
|
StkId func; /* function index in the stack */
|
||||||
StkIdRel top; /* top for this function */
|
StkId top; /* top for this function */
|
||||||
struct CallInfo *previous, *next; /* dynamic call link */
|
struct CallInfo *previous, *next; /* dynamic call link */
|
||||||
union {
|
union {
|
||||||
struct { /* only for Lua functions */
|
struct { /* only for Lua functions */
|
||||||
|
StkId base; /* base for this function */
|
||||||
const Instruction *savedpc;
|
const Instruction *savedpc;
|
||||||
volatile l_signalT trap; /* function is tracing lines/counts */
|
|
||||||
int nextraargs; /* # of extra arguments in vararg functions */
|
|
||||||
} l;
|
} l;
|
||||||
struct { /* only for C functions */
|
struct { /* only for C functions */
|
||||||
lua_KFunction k; /* continuation in case of yields */
|
lua_KFunction k; /* continuation in case of yields */
|
||||||
@@ -200,125 +104,31 @@ struct CallInfo {
|
|||||||
lua_KContext ctx; /* context info. in case of yields */
|
lua_KContext ctx; /* context info. in case of yields */
|
||||||
} c;
|
} c;
|
||||||
} u;
|
} u;
|
||||||
union {
|
ptrdiff_t extra;
|
||||||
int funcidx; /* called-function index */
|
short nresults; /* expected number of results from this function */
|
||||||
int nyield; /* number of values yielded */
|
unsigned short callstatus;
|
||||||
int nres; /* number of values returned */
|
} CallInfo;
|
||||||
} u2;
|
|
||||||
l_uint32 callstatus;
|
|
||||||
};
|
|
||||||
|
|
||||||
|
|
||||||
/*
|
|
||||||
** Maximum expected number of results from a function
|
|
||||||
** (must fit in CIST_NRESULTS).
|
|
||||||
*/
|
|
||||||
#define MAXRESULTS 250
|
|
||||||
|
|
||||||
|
|
||||||
/*
|
/*
|
||||||
** Bits in CallInfo status
|
** Bits in CallInfo status
|
||||||
*/
|
*/
|
||||||
/* bits 0-7 are the expected number of results from this function + 1 */
|
#define CIST_OAH (1<<0) /* original value of 'allowhook' */
|
||||||
#define CIST_NRESULTS 0xffu
|
#define CIST_LUA (1<<1) /* call is running a Lua function */
|
||||||
|
#define CIST_HOOKED (1<<2) /* call is running a debug hook */
|
||||||
|
#define CIST_FRESH (1<<3) /* call is running on a fresh invocation
|
||||||
|
of luaV_execute */
|
||||||
|
#define CIST_YPCALL (1<<4) /* call is a yieldable protected call */
|
||||||
|
#define CIST_TAIL (1<<5) /* call was tail called */
|
||||||
|
#define CIST_HOOKYIELD (1<<6) /* last hook called yielded */
|
||||||
|
#define CIST_LEQ (1<<7) /* using __lt for __le */
|
||||||
|
#define CIST_FIN (1<<8) /* call is running a finalizer */
|
||||||
|
|
||||||
/* bits 8-11 count call metamethods (and their extra arguments) */
|
#define isLua(ci) ((ci)->callstatus & CIST_LUA)
|
||||||
#define CIST_CCMT 8 /* the offset, not the mask */
|
|
||||||
#define MAX_CCMT (0xfu << CIST_CCMT)
|
|
||||||
|
|
||||||
/* Bits 12-14 are used for CIST_RECST (see below) */
|
/* assume that CIST_OAH has offset 0 and that 'v' is strictly 0/1 */
|
||||||
#define CIST_RECST 12 /* the offset, not the mask */
|
#define setoah(st,v) ((st) = ((st) & ~CIST_OAH) | (v))
|
||||||
|
#define getoah(st) ((st) & CIST_OAH)
|
||||||
/* call is running a C function (still in first 16 bits) */
|
|
||||||
#define CIST_C (1u << (CIST_RECST + 3))
|
|
||||||
/* call is on a fresh "luaV_execute" frame */
|
|
||||||
#define CIST_FRESH (cast(l_uint32, CIST_C) << 1)
|
|
||||||
/* function is closing tbc variables */
|
|
||||||
#define CIST_CLSRET (CIST_FRESH << 1)
|
|
||||||
/* function has tbc variables to close */
|
|
||||||
#define CIST_TBC (CIST_CLSRET << 1)
|
|
||||||
/* original value of 'allowhook' */
|
|
||||||
#define CIST_OAH (CIST_TBC << 1)
|
|
||||||
/* call is running a debug hook */
|
|
||||||
#define CIST_HOOKED (CIST_OAH << 1)
|
|
||||||
/* doing a yieldable protected call */
|
|
||||||
#define CIST_YPCALL (CIST_HOOKED << 1)
|
|
||||||
/* call was tail called */
|
|
||||||
#define CIST_TAIL (CIST_YPCALL << 1)
|
|
||||||
/* last hook called yielded */
|
|
||||||
#define CIST_HOOKYIELD (CIST_TAIL << 1)
|
|
||||||
/* function "called" a finalizer */
|
|
||||||
#define CIST_FIN (CIST_HOOKYIELD << 1)
|
|
||||||
|
|
||||||
|
|
||||||
#define get_nresults(cs) (cast_int((cs) & CIST_NRESULTS) - 1)
|
|
||||||
|
|
||||||
/*
|
|
||||||
** Field CIST_RECST stores the "recover status", used to keep the error
|
|
||||||
** status while closing to-be-closed variables in coroutines, so that
|
|
||||||
** Lua can correctly resume after an yield from a __close method called
|
|
||||||
** because of an error. (Three bits are enough for error status.)
|
|
||||||
*/
|
|
||||||
#define getcistrecst(ci) (((ci)->callstatus >> CIST_RECST) & 7)
|
|
||||||
#define setcistrecst(ci,st) \
|
|
||||||
check_exp(((st) & 7) == (st), /* status must fit in three bits */ \
|
|
||||||
((ci)->callstatus = ((ci)->callstatus & ~(7u << CIST_RECST)) \
|
|
||||||
| (cast(l_uint32, st) << CIST_RECST)))
|
|
||||||
|
|
||||||
|
|
||||||
/* active function is a Lua function */
|
|
||||||
#define isLua(ci) (!((ci)->callstatus & CIST_C))
|
|
||||||
|
|
||||||
/* call is running Lua code (not a hook) */
|
|
||||||
#define isLuacode(ci) (!((ci)->callstatus & (CIST_C | CIST_HOOKED)))
|
|
||||||
|
|
||||||
|
|
||||||
#define setoah(ci,v) \
|
|
||||||
((ci)->callstatus = ((v) ? (ci)->callstatus | CIST_OAH \
|
|
||||||
: (ci)->callstatus & ~CIST_OAH))
|
|
||||||
#define getoah(ci) (((ci)->callstatus & CIST_OAH) ? 1 : 0)
|
|
||||||
|
|
||||||
|
|
||||||
/*
|
|
||||||
** 'per thread' state
|
|
||||||
*/
|
|
||||||
struct lua_State {
|
|
||||||
CommonHeader;
|
|
||||||
lu_byte allowhook;
|
|
||||||
TStatus status;
|
|
||||||
StkIdRel top; /* first free slot in the stack */
|
|
||||||
struct global_State *l_G;
|
|
||||||
CallInfo *ci; /* call info for current function */
|
|
||||||
StkIdRel stack_last; /* end of stack (last element + 1) */
|
|
||||||
StkIdRel stack; /* stack base */
|
|
||||||
UpVal *openupval; /* list of open upvalues in this stack */
|
|
||||||
StkIdRel tbclist; /* list of to-be-closed variables */
|
|
||||||
GCObject *gclist;
|
|
||||||
struct lua_State *twups; /* list of threads with open upvalues */
|
|
||||||
struct lua_longjmp *errorJmp; /* current error recover point */
|
|
||||||
CallInfo base_ci; /* CallInfo for first level (C host) */
|
|
||||||
volatile lua_Hook hook;
|
|
||||||
ptrdiff_t errfunc; /* current error handling function (stack index) */
|
|
||||||
l_uint32 nCcalls; /* number of nested non-yieldable or C calls */
|
|
||||||
int oldpc; /* last pc traced */
|
|
||||||
int nci; /* number of items in 'ci' list */
|
|
||||||
int basehookcount;
|
|
||||||
int hookcount;
|
|
||||||
volatile l_signalT hookmask;
|
|
||||||
struct { /* info about transferred values (for call/return hooks) */
|
|
||||||
int ftransfer; /* offset of first value transferred */
|
|
||||||
int ntransfer; /* number of values transferred */
|
|
||||||
} transferinfo;
|
|
||||||
};
|
|
||||||
|
|
||||||
|
|
||||||
/*
|
|
||||||
** thread state + extra space
|
|
||||||
*/
|
|
||||||
typedef struct LX {
|
|
||||||
lu_byte extra_[LUA_EXTRASPACE];
|
|
||||||
lua_State l;
|
|
||||||
} LX;
|
|
||||||
|
|
||||||
|
|
||||||
/*
|
/*
|
||||||
@@ -327,21 +137,17 @@ typedef struct LX {
|
|||||||
typedef struct global_State {
|
typedef struct global_State {
|
||||||
lua_Alloc frealloc; /* function to reallocate memory */
|
lua_Alloc frealloc; /* function to reallocate memory */
|
||||||
void *ud; /* auxiliary data to 'frealloc' */
|
void *ud; /* auxiliary data to 'frealloc' */
|
||||||
l_mem GCtotalbytes; /* number of bytes currently allocated + debt */
|
l_mem totalbytes; /* number of bytes currently allocated - GCdebt */
|
||||||
l_mem GCdebt; /* bytes counted but not yet allocated */
|
l_mem GCdebt; /* bytes allocated not yet compensated by the collector */
|
||||||
l_mem GCmarked; /* number of objects marked in a GC cycle */
|
lu_mem GCmemtrav; /* memory traversed by the GC */
|
||||||
l_mem GCmajorminor; /* auxiliary counter to control major-minor shifts */
|
lu_mem GCestimate; /* an estimate of the non-garbage memory in use */
|
||||||
stringtable strt; /* hash table for strings */
|
stringtable strt; /* hash table for strings */
|
||||||
TValue l_registry;
|
TValue l_registry;
|
||||||
TValue nilvalue; /* a nil value */
|
|
||||||
unsigned int seed; /* randomized seed for hashes */
|
unsigned int seed; /* randomized seed for hashes */
|
||||||
lu_byte gcparams[LUA_GCPN];
|
|
||||||
lu_byte currentwhite;
|
lu_byte currentwhite;
|
||||||
lu_byte gcstate; /* state of garbage collector */
|
lu_byte gcstate; /* state of garbage collector */
|
||||||
lu_byte gckind; /* kind of GC running */
|
lu_byte gckind; /* kind of GC running */
|
||||||
lu_byte gcstopem; /* stops emergency collections */
|
lu_byte gcrunning; /* true if GC is running */
|
||||||
lu_byte gcstp; /* control whether GC is running */
|
|
||||||
lu_byte gcemergency; /* true if this is an emergency collection */
|
|
||||||
GCObject *allgc; /* list of all collectable objects */
|
GCObject *allgc; /* list of all collectable objects */
|
||||||
GCObject **sweepgc; /* current position of sweep in list */
|
GCObject **sweepgc; /* current position of sweep in list */
|
||||||
GCObject *finobj; /* list of collectable objects with finalizers */
|
GCObject *finobj; /* list of collectable objects with finalizers */
|
||||||
@@ -352,44 +158,55 @@ typedef struct global_State {
|
|||||||
GCObject *allweak; /* list of all-weak tables */
|
GCObject *allweak; /* list of all-weak tables */
|
||||||
GCObject *tobefnz; /* list of userdata to be GC */
|
GCObject *tobefnz; /* list of userdata to be GC */
|
||||||
GCObject *fixedgc; /* list of objects not to be collected */
|
GCObject *fixedgc; /* list of objects not to be collected */
|
||||||
/* fields for generational collector */
|
|
||||||
GCObject *survival; /* start of objects that survived one GC cycle */
|
|
||||||
GCObject *old1; /* start of old1 objects */
|
|
||||||
GCObject *reallyold; /* objects more than one cycle old ("really old") */
|
|
||||||
GCObject *firstold1; /* first OLD1 object in the list (if any) */
|
|
||||||
GCObject *finobjsur; /* list of survival objects with finalizers */
|
|
||||||
GCObject *finobjold1; /* list of old1 objects with finalizers */
|
|
||||||
GCObject *finobjrold; /* list of really old objects with finalizers */
|
|
||||||
struct lua_State *twups; /* list of threads with open upvalues */
|
struct lua_State *twups; /* list of threads with open upvalues */
|
||||||
|
unsigned int gcfinnum; /* number of finalizers to call in each GC step */
|
||||||
|
int gcpause; /* size of pause between successive GCs */
|
||||||
|
int gcstepmul; /* GC 'granularity' */
|
||||||
lua_CFunction panic; /* to be called in unprotected errors */
|
lua_CFunction panic; /* to be called in unprotected errors */
|
||||||
TString *memerrmsg; /* message for memory-allocation errors */
|
struct lua_State *mainthread;
|
||||||
|
const lua_Number *version; /* pointer to version number */
|
||||||
|
TString *memerrmsg; /* memory-error message */
|
||||||
TString *tmname[TM_N]; /* array with tag-method names */
|
TString *tmname[TM_N]; /* array with tag-method names */
|
||||||
struct Table *mt[LUA_NUMTYPES]; /* metatables for basic types */
|
struct Table *mt[LUA_NUMTAGS]; /* metatables for basic types */
|
||||||
TString *strcache[STRCACHE_N][STRCACHE_M]; /* cache for strings in API */
|
TString *strcache[STRCACHE_N][STRCACHE_M]; /* cache for strings in API */
|
||||||
lua_WarnFunction warnf; /* warning function */
|
|
||||||
void *ud_warn; /* auxiliary data to 'warnf' */
|
|
||||||
LX mainth; /* main thread of this state */
|
|
||||||
} global_State;
|
} global_State;
|
||||||
|
|
||||||
|
|
||||||
#define G(L) (L->l_G)
|
|
||||||
#define mainthread(G) (&(G)->mainth.l)
|
|
||||||
|
|
||||||
/*
|
/*
|
||||||
** 'g->nilvalue' being a nil value flags that the state was completely
|
** 'per thread' state
|
||||||
** build.
|
|
||||||
*/
|
*/
|
||||||
#define completestate(g) ttisnil(&g->nilvalue)
|
struct lua_State {
|
||||||
|
CommonHeader;
|
||||||
|
unsigned short nci; /* number of items in 'ci' list */
|
||||||
|
lu_byte status;
|
||||||
|
StkId top; /* first free slot in the stack */
|
||||||
|
global_State *l_G;
|
||||||
|
CallInfo *ci; /* call info for current function */
|
||||||
|
const Instruction *oldpc; /* last pc traced */
|
||||||
|
StkId stack_last; /* last free slot in the stack */
|
||||||
|
StkId stack; /* stack base */
|
||||||
|
UpVal *openupval; /* list of open upvalues in this stack */
|
||||||
|
GCObject *gclist;
|
||||||
|
struct lua_State *twups; /* list of threads with open upvalues */
|
||||||
|
struct lua_longjmp *errorJmp; /* current error recover point */
|
||||||
|
CallInfo base_ci; /* CallInfo for first level (C calling Lua) */
|
||||||
|
volatile lua_Hook hook;
|
||||||
|
ptrdiff_t errfunc; /* current error handling function (stack index) */
|
||||||
|
int stacksize;
|
||||||
|
int basehookcount;
|
||||||
|
int hookcount;
|
||||||
|
unsigned short nny; /* number of non-yieldable calls in stack */
|
||||||
|
unsigned short nCcalls; /* number of nested C calls */
|
||||||
|
l_signalT hookmask;
|
||||||
|
lu_byte allowhook;
|
||||||
|
};
|
||||||
|
|
||||||
|
|
||||||
|
#define G(L) (L->l_G)
|
||||||
|
|
||||||
|
|
||||||
/*
|
/*
|
||||||
** Union of all collectable objects (only for conversions)
|
** Union of all collectable objects (only for conversions)
|
||||||
** ISO C99, 6.5.2.3 p.5:
|
|
||||||
** "if a union contains several structures that share a common initial
|
|
||||||
** sequence [...], and if the union object currently contains one
|
|
||||||
** of these structures, it is permitted to inspect the common initial
|
|
||||||
** part of any of them anywhere that a declaration of the complete type
|
|
||||||
** of the union is visible."
|
|
||||||
*/
|
*/
|
||||||
union GCUnion {
|
union GCUnion {
|
||||||
GCObject gc; /* common header */
|
GCObject gc; /* common header */
|
||||||
@@ -399,52 +216,37 @@ union GCUnion {
|
|||||||
struct Table h;
|
struct Table h;
|
||||||
struct Proto p;
|
struct Proto p;
|
||||||
struct lua_State th; /* thread */
|
struct lua_State th; /* thread */
|
||||||
struct UpVal upv;
|
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
/*
|
|
||||||
** ISO C99, 6.7.2.1 p.14:
|
|
||||||
** "A pointer to a union object, suitably converted, points to each of
|
|
||||||
** its members [...], and vice versa."
|
|
||||||
*/
|
|
||||||
#define cast_u(o) cast(union GCUnion *, (o))
|
#define cast_u(o) cast(union GCUnion *, (o))
|
||||||
|
|
||||||
/* macros to convert a GCObject into a specific value */
|
/* macros to convert a GCObject into a specific value */
|
||||||
#define gco2ts(o) \
|
#define gco2ts(o) \
|
||||||
check_exp(novariant((o)->tt) == LUA_TSTRING, &((cast_u(o))->ts))
|
check_exp(novariant((o)->tt) == LUA_TSTRING, &((cast_u(o))->ts))
|
||||||
#define gco2u(o) check_exp((o)->tt == LUA_VUSERDATA, &((cast_u(o))->u))
|
#define gco2u(o) check_exp((o)->tt == LUA_TUSERDATA, &((cast_u(o))->u))
|
||||||
#define gco2lcl(o) check_exp((o)->tt == LUA_VLCL, &((cast_u(o))->cl.l))
|
#define gco2lcl(o) check_exp((o)->tt == LUA_TLCL, &((cast_u(o))->cl.l))
|
||||||
#define gco2ccl(o) check_exp((o)->tt == LUA_VCCL, &((cast_u(o))->cl.c))
|
#define gco2ccl(o) check_exp((o)->tt == LUA_TCCL, &((cast_u(o))->cl.c))
|
||||||
#define gco2cl(o) \
|
#define gco2cl(o) \
|
||||||
check_exp(novariant((o)->tt) == LUA_TFUNCTION, &((cast_u(o))->cl))
|
check_exp(novariant((o)->tt) == LUA_TFUNCTION, &((cast_u(o))->cl))
|
||||||
#define gco2t(o) check_exp((o)->tt == LUA_VTABLE, &((cast_u(o))->h))
|
#define gco2t(o) check_exp((o)->tt == LUA_TTABLE, &((cast_u(o))->h))
|
||||||
#define gco2p(o) check_exp((o)->tt == LUA_VPROTO, &((cast_u(o))->p))
|
#define gco2p(o) check_exp((o)->tt == LUA_TPROTO, &((cast_u(o))->p))
|
||||||
#define gco2th(o) check_exp((o)->tt == LUA_VTHREAD, &((cast_u(o))->th))
|
#define gco2th(o) check_exp((o)->tt == LUA_TTHREAD, &((cast_u(o))->th))
|
||||||
#define gco2upv(o) check_exp((o)->tt == LUA_VUPVAL, &((cast_u(o))->upv))
|
|
||||||
|
|
||||||
|
|
||||||
/*
|
/* macro to convert a Lua object into a GCObject */
|
||||||
** macro to convert a Lua object into a GCObject
|
#define obj2gco(v) \
|
||||||
*/
|
check_exp(novariant((v)->tt) < LUA_TDEADKEY, (&(cast_u(v)->gc)))
|
||||||
#define obj2gco(v) \
|
|
||||||
check_exp(novariant((v)->tt) >= LUA_TSTRING, &(cast_u(v)->gc))
|
|
||||||
|
|
||||||
|
|
||||||
/* actual number of total memory allocated */
|
/* actual number of total bytes allocated */
|
||||||
#define gettotalbytes(g) ((g)->GCtotalbytes - (g)->GCdebt)
|
#define gettotalbytes(g) cast(lu_mem, (g)->totalbytes + (g)->GCdebt)
|
||||||
|
|
||||||
|
|
||||||
LUAI_FUNC void luaE_setdebt (global_State *g, l_mem debt);
|
LUAI_FUNC void luaE_setdebt (global_State *g, l_mem debt);
|
||||||
LUAI_FUNC void luaE_freethread (lua_State *L, lua_State *L1);
|
LUAI_FUNC void luaE_freethread (lua_State *L, lua_State *L1);
|
||||||
LUAI_FUNC lu_mem luaE_threadsize (lua_State *L);
|
|
||||||
LUAI_FUNC CallInfo *luaE_extendCI (lua_State *L);
|
LUAI_FUNC CallInfo *luaE_extendCI (lua_State *L);
|
||||||
|
LUAI_FUNC void luaE_freeCI (lua_State *L);
|
||||||
LUAI_FUNC void luaE_shrinkCI (lua_State *L);
|
LUAI_FUNC void luaE_shrinkCI (lua_State *L);
|
||||||
LUAI_FUNC void luaE_checkcstack (lua_State *L);
|
|
||||||
LUAI_FUNC void luaE_incCstack (lua_State *L);
|
|
||||||
LUAI_FUNC void luaE_warning (lua_State *L, const char *msg, int tocont);
|
|
||||||
LUAI_FUNC void luaE_warnerror (lua_State *L, const char *where);
|
|
||||||
LUAI_FUNC TStatus luaE_resetthread (lua_State *L, TStatus status);
|
|
||||||
|
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
|||||||
@@ -1,5 +1,5 @@
|
|||||||
/*
|
/*
|
||||||
** $Id: lstring.c $
|
** $Id: lstring.c,v 2.56.1.1 2017/04/19 17:20:42 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
|
||||||
*/
|
*/
|
||||||
@@ -22,94 +22,77 @@
|
|||||||
#include "lstring.h"
|
#include "lstring.h"
|
||||||
|
|
||||||
|
|
||||||
/*
|
#define MEMERRMSG "not enough memory"
|
||||||
** Maximum size for string table.
|
|
||||||
*/
|
|
||||||
#define MAXSTRTB cast_int(luaM_limitN(INT_MAX, TString*))
|
|
||||||
|
|
||||||
/*
|
/*
|
||||||
** Initial size for the string table (must be power of 2).
|
** Lua will use at most ~(2^LUAI_HASHLIMIT) bytes from a string to
|
||||||
** The Lua core alone registers ~50 strings (reserved words +
|
** compute its hash
|
||||||
** metaevent keys + a few others). Libraries would typically add
|
|
||||||
** a few dozens more.
|
|
||||||
*/
|
*/
|
||||||
#if !defined(MINSTRTABSIZE)
|
#if !defined(LUAI_HASHLIMIT)
|
||||||
#define MINSTRTABSIZE 128
|
#define LUAI_HASHLIMIT 5
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
|
||||||
/*
|
/*
|
||||||
** generic equality for strings
|
** equality for long strings
|
||||||
*/
|
*/
|
||||||
int luaS_eqstr (TString *a, TString *b) {
|
int luaS_eqlngstr (TString *a, TString *b) {
|
||||||
size_t len1, len2;
|
size_t len = a->u.lnglen;
|
||||||
const char *s1 = getlstr(a, len1);
|
lua_assert(a->tt == LUA_TLNGSTR && b->tt == LUA_TLNGSTR);
|
||||||
const char *s2 = getlstr(b, len2);
|
return (a == b) || /* same instance or... */
|
||||||
return ((len1 == len2) && /* equal length and ... */
|
((len == b->u.lnglen) && /* equal length and ... */
|
||||||
(memcmp(s1, s2, len1) == 0)); /* equal contents */
|
(memcmp(getstr(a), getstr(b), len) == 0)); /* equal contents */
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
static unsigned luaS_hash (const char *str, size_t l, unsigned seed) {
|
unsigned int luaS_hash (const char *str, size_t l, unsigned int seed) {
|
||||||
unsigned int h = seed ^ cast_uint(l);
|
unsigned int h = seed ^ cast(unsigned int, l);
|
||||||
for (; l > 0; l--)
|
size_t step = (l >> LUAI_HASHLIMIT) + 1;
|
||||||
|
for (; l >= step; l -= step)
|
||||||
h ^= ((h<<5) + (h>>2) + cast_byte(str[l - 1]));
|
h ^= ((h<<5) + (h>>2) + cast_byte(str[l - 1]));
|
||||||
return h;
|
return h;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
unsigned luaS_hashlongstr (TString *ts) {
|
unsigned int luaS_hashlongstr (TString *ts) {
|
||||||
lua_assert(ts->tt == LUA_VLNGSTR);
|
lua_assert(ts->tt == LUA_TLNGSTR);
|
||||||
if (ts->extra == 0) { /* no hash? */
|
if (ts->extra == 0) { /* no hash? */
|
||||||
size_t len = ts->u.lnglen;
|
ts->hash = luaS_hash(getstr(ts), ts->u.lnglen, ts->hash);
|
||||||
ts->hash = luaS_hash(getlngstr(ts), len, ts->hash);
|
|
||||||
ts->extra = 1; /* now it has its hash */
|
ts->extra = 1; /* now it has its hash */
|
||||||
}
|
}
|
||||||
return ts->hash;
|
return ts->hash;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
static void tablerehash (TString **vect, int osize, int nsize) {
|
/*
|
||||||
|
** resizes the string table
|
||||||
|
*/
|
||||||
|
void luaS_resize (lua_State *L, int newsize) {
|
||||||
int i;
|
int i;
|
||||||
for (i = osize; i < nsize; i++) /* clear new elements */
|
stringtable *tb = &G(L)->strt;
|
||||||
vect[i] = NULL;
|
if (newsize > tb->size) { /* grow table if needed */
|
||||||
for (i = 0; i < osize; i++) { /* rehash old part of the array */
|
luaM_reallocvector(L, tb->hash, tb->size, newsize, TString *);
|
||||||
TString *p = vect[i];
|
for (i = tb->size; i < newsize; i++)
|
||||||
vect[i] = NULL;
|
tb->hash[i] = NULL;
|
||||||
while (p) { /* for each string in the list */
|
}
|
||||||
|
for (i = 0; i < tb->size; i++) { /* rehash */
|
||||||
|
TString *p = tb->hash[i];
|
||||||
|
tb->hash[i] = NULL;
|
||||||
|
while (p) { /* for each node in the list */
|
||||||
TString *hnext = p->u.hnext; /* save next */
|
TString *hnext = p->u.hnext; /* save next */
|
||||||
unsigned int h = lmod(p->hash, nsize); /* new position */
|
unsigned int h = lmod(p->hash, newsize); /* new position */
|
||||||
p->u.hnext = vect[h]; /* chain it into array */
|
p->u.hnext = tb->hash[h]; /* chain it */
|
||||||
vect[h] = p;
|
tb->hash[h] = p;
|
||||||
p = hnext;
|
p = hnext;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
if (newsize < tb->size) { /* shrink table if needed */
|
||||||
|
/* vanishing slice should be empty */
|
||||||
|
lua_assert(tb->hash[newsize] == NULL && tb->hash[tb->size - 1] == NULL);
|
||||||
/*
|
luaM_reallocvector(L, tb->hash, tb->size, newsize, TString *);
|
||||||
** Resize the string table. If allocation fails, keep the current size.
|
|
||||||
** (This can degrade performance, but any non-zero size should work
|
|
||||||
** correctly.)
|
|
||||||
*/
|
|
||||||
void luaS_resize (lua_State *L, int nsize) {
|
|
||||||
stringtable *tb = &G(L)->strt;
|
|
||||||
int osize = tb->size;
|
|
||||||
TString **newvect;
|
|
||||||
if (nsize < osize) /* shrinking table? */
|
|
||||||
tablerehash(tb->hash, osize, nsize); /* depopulate shrinking part */
|
|
||||||
newvect = luaM_reallocvector(L, tb->hash, osize, nsize, TString*);
|
|
||||||
if (l_unlikely(newvect == NULL)) { /* reallocation failed? */
|
|
||||||
if (nsize < osize) /* was it shrinking table? */
|
|
||||||
tablerehash(tb->hash, nsize, osize); /* restore to original size */
|
|
||||||
/* leave table as it was */
|
|
||||||
}
|
|
||||||
else { /* allocation succeeded */
|
|
||||||
tb->hash = newvect;
|
|
||||||
tb->size = nsize;
|
|
||||||
if (nsize > osize)
|
|
||||||
tablerehash(newvect, osize, nsize); /* rehash for new size */
|
|
||||||
}
|
}
|
||||||
|
tb->size = newsize;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@@ -121,8 +104,8 @@ void luaS_clearcache (global_State *g) {
|
|||||||
int i, j;
|
int i, j;
|
||||||
for (i = 0; i < STRCACHE_N; i++)
|
for (i = 0; i < STRCACHE_N; i++)
|
||||||
for (j = 0; j < STRCACHE_M; j++) {
|
for (j = 0; j < STRCACHE_M; j++) {
|
||||||
if (iswhite(g->strcache[i][j])) /* will entry be collected? */
|
if (iswhite(g->strcache[i][j])) /* will entry be collected? */
|
||||||
g->strcache[i][j] = g->memerrmsg; /* replace it with something fixed */
|
g->strcache[i][j] = g->memerrmsg; /* replace it with something fixed */
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -133,10 +116,7 @@ void luaS_clearcache (global_State *g) {
|
|||||||
void luaS_init (lua_State *L) {
|
void luaS_init (lua_State *L) {
|
||||||
global_State *g = G(L);
|
global_State *g = G(L);
|
||||||
int i, j;
|
int i, j;
|
||||||
stringtable *tb = &G(L)->strt;
|
luaS_resize(L, MINSTRTABSIZE); /* initial size of string table */
|
||||||
tb->hash = luaM_newvector(L, MINSTRTABSIZE, TString*);
|
|
||||||
tablerehash(tb->hash, 0, MINSTRTABSIZE); /* clear array */
|
|
||||||
tb->size = MINSTRTABSIZE;
|
|
||||||
/* pre-create memory-error message */
|
/* pre-create memory-error message */
|
||||||
g->memerrmsg = luaS_newliteral(L, MEMERRMSG);
|
g->memerrmsg = luaS_newliteral(L, MEMERRMSG);
|
||||||
luaC_fix(L, obj2gco(g->memerrmsg)); /* it should never be collected */
|
luaC_fix(L, obj2gco(g->memerrmsg)); /* it should never be collected */
|
||||||
@@ -146,43 +126,27 @@ void luaS_init (lua_State *L) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
size_t luaS_sizelngstr (size_t len, int kind) {
|
|
||||||
switch (kind) {
|
|
||||||
case LSTRREG: /* regular long string */
|
|
||||||
/* don't need 'falloc'/'ud', but need space for content */
|
|
||||||
return offsetof(TString, falloc) + (len + 1) * sizeof(char);
|
|
||||||
case LSTRFIX: /* fixed external long string */
|
|
||||||
/* don't need 'falloc'/'ud' */
|
|
||||||
return offsetof(TString, falloc);
|
|
||||||
default: /* external long string with deallocation */
|
|
||||||
lua_assert(kind == LSTRMEM);
|
|
||||||
return sizeof(TString);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
/*
|
/*
|
||||||
** creates a new string object
|
** creates a new string object
|
||||||
*/
|
*/
|
||||||
static TString *createstrobj (lua_State *L, size_t totalsize, lu_byte tag,
|
static TString *createstrobj (lua_State *L, size_t l, int tag, unsigned int h) {
|
||||||
unsigned h) {
|
|
||||||
TString *ts;
|
TString *ts;
|
||||||
GCObject *o;
|
GCObject *o;
|
||||||
|
size_t totalsize; /* total size of TString object */
|
||||||
|
totalsize = sizelstring(l);
|
||||||
o = luaC_newobj(L, tag, totalsize);
|
o = luaC_newobj(L, tag, totalsize);
|
||||||
ts = gco2ts(o);
|
ts = gco2ts(o);
|
||||||
ts->hash = h;
|
ts->hash = h;
|
||||||
ts->extra = 0;
|
ts->extra = 0;
|
||||||
|
getstr(ts)[l] = '\0'; /* ending 0 */
|
||||||
return ts;
|
return ts;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
TString *luaS_createlngstrobj (lua_State *L, size_t l) {
|
TString *luaS_createlngstrobj (lua_State *L, size_t l) {
|
||||||
size_t totalsize = luaS_sizelngstr(l, LSTRREG);
|
TString *ts = createstrobj(L, l, LUA_TLNGSTR, G(L)->seed);
|
||||||
TString *ts = createstrobj(L, totalsize, LUA_VLNGSTR, G(L)->seed);
|
|
||||||
ts->u.lnglen = l;
|
ts->u.lnglen = l;
|
||||||
ts->shrlen = LSTRREG; /* signals that it is a regular long string */
|
|
||||||
ts->contents = cast_charp(ts) + offsetof(TString, falloc);
|
|
||||||
ts->contents[l] = '\0'; /* ending 0 */
|
|
||||||
return ts;
|
return ts;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -197,48 +161,34 @@ void luaS_remove (lua_State *L, TString *ts) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
static void growstrtab (lua_State *L, stringtable *tb) {
|
|
||||||
if (l_unlikely(tb->nuse == INT_MAX)) { /* too many strings? */
|
|
||||||
luaC_fullgc(L, 1); /* try to free some... */
|
|
||||||
if (tb->nuse == INT_MAX) /* still too many? */
|
|
||||||
luaM_error(L); /* cannot even create a message... */
|
|
||||||
}
|
|
||||||
if (tb->size <= MAXSTRTB / 2) /* can grow string table? */
|
|
||||||
luaS_resize(L, tb->size * 2);
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
/*
|
/*
|
||||||
** Checks whether short string exists and reuses it or creates a new one.
|
** checks whether short string exists and reuses it or creates a new one
|
||||||
*/
|
*/
|
||||||
static TString *internshrstr (lua_State *L, const char *str, size_t l) {
|
static TString *internshrstr (lua_State *L, const char *str, size_t l) {
|
||||||
TString *ts;
|
TString *ts;
|
||||||
global_State *g = G(L);
|
global_State *g = G(L);
|
||||||
stringtable *tb = &g->strt;
|
|
||||||
unsigned int h = luaS_hash(str, l, g->seed);
|
unsigned int h = luaS_hash(str, l, g->seed);
|
||||||
TString **list = &tb->hash[lmod(h, tb->size)];
|
TString **list = &g->strt.hash[lmod(h, g->strt.size)];
|
||||||
lua_assert(str != NULL); /* otherwise 'memcmp'/'memcpy' are undefined */
|
lua_assert(str != NULL); /* otherwise 'memcmp'/'memcpy' are undefined */
|
||||||
for (ts = *list; ts != NULL; ts = ts->u.hnext) {
|
for (ts = *list; ts != NULL; ts = ts->u.hnext) {
|
||||||
if (l == cast_uint(ts->shrlen) &&
|
if (l == ts->shrlen &&
|
||||||
(memcmp(str, getshrstr(ts), l * sizeof(char)) == 0)) {
|
(memcmp(str, getstr(ts), l * sizeof(char)) == 0)) {
|
||||||
/* found! */
|
/* found! */
|
||||||
if (isdead(g, ts)) /* dead (but not collected yet)? */
|
if (isdead(g, ts)) /* dead (but not collected yet)? */
|
||||||
changewhite(ts); /* resurrect it */
|
changewhite(ts); /* resurrect it */
|
||||||
return ts;
|
return ts;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
/* else must create a new string */
|
if (g->strt.nuse >= g->strt.size && g->strt.size <= MAX_INT/2) {
|
||||||
if (tb->nuse >= tb->size) { /* need to grow string table? */
|
luaS_resize(L, g->strt.size * 2);
|
||||||
growstrtab(L, tb);
|
list = &g->strt.hash[lmod(h, g->strt.size)]; /* recompute with new size */
|
||||||
list = &tb->hash[lmod(h, tb->size)]; /* rehash with new size */
|
|
||||||
}
|
}
|
||||||
ts = createstrobj(L, sizestrshr(l), LUA_VSHRSTR, h);
|
ts = createstrobj(L, l, LUA_TSHRSTR, h);
|
||||||
ts->shrlen = cast(ls_byte, l);
|
memcpy(getstr(ts), str, l * sizeof(char));
|
||||||
getshrstr(ts)[l] = '\0'; /* ending 0 */
|
ts->shrlen = cast_byte(l);
|
||||||
memcpy(getshrstr(ts), str, l * sizeof(char));
|
|
||||||
ts->u.hnext = *list;
|
ts->u.hnext = *list;
|
||||||
*list = ts;
|
*list = ts;
|
||||||
tb->nuse++;
|
g->strt.nuse++;
|
||||||
return ts;
|
return ts;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -251,10 +201,10 @@ TString *luaS_newlstr (lua_State *L, const char *str, size_t l) {
|
|||||||
return internshrstr(L, str, l);
|
return internshrstr(L, str, l);
|
||||||
else {
|
else {
|
||||||
TString *ts;
|
TString *ts;
|
||||||
if (l_unlikely(l * sizeof(char) >= (MAX_SIZE - sizeof(TString))))
|
if (l >= (MAX_SIZE - sizeof(TString))/sizeof(char))
|
||||||
luaM_toobig(L);
|
luaM_toobig(L);
|
||||||
ts = luaS_createlngstrobj(L, l);
|
ts = luaS_createlngstrobj(L, l);
|
||||||
memcpy(getlngstr(ts), str, l * sizeof(char));
|
memcpy(getstr(ts), str, l * sizeof(char));
|
||||||
return ts;
|
return ts;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -283,71 +233,16 @@ TString *luaS_new (lua_State *L, const char *str) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
Udata *luaS_newudata (lua_State *L, size_t s, unsigned short nuvalue) {
|
Udata *luaS_newudata (lua_State *L, size_t s) {
|
||||||
Udata *u;
|
Udata *u;
|
||||||
int i;
|
|
||||||
GCObject *o;
|
GCObject *o;
|
||||||
if (l_unlikely(s > MAX_SIZE - udatamemoffset(nuvalue)))
|
if (s > MAX_SIZE - sizeof(Udata))
|
||||||
luaM_toobig(L);
|
luaM_toobig(L);
|
||||||
o = luaC_newobj(L, LUA_VUSERDATA, sizeudata(nuvalue, s));
|
o = luaC_newobj(L, LUA_TUSERDATA, sizeludata(s));
|
||||||
u = gco2u(o);
|
u = gco2u(o);
|
||||||
u->len = s;
|
u->len = s;
|
||||||
u->nuvalue = nuvalue;
|
|
||||||
u->metatable = NULL;
|
u->metatable = NULL;
|
||||||
for (i = 0; i < nuvalue; i++)
|
setuservalue(L, u, luaO_nilobject);
|
||||||
setnilvalue(&u->uv[i].uv);
|
|
||||||
return u;
|
return u;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
struct NewExt {
|
|
||||||
ls_byte kind;
|
|
||||||
const char *s;
|
|
||||||
size_t len;
|
|
||||||
TString *ts; /* output */
|
|
||||||
};
|
|
||||||
|
|
||||||
|
|
||||||
static void f_newext (lua_State *L, void *ud) {
|
|
||||||
struct NewExt *ne = cast(struct NewExt *, ud);
|
|
||||||
size_t size = luaS_sizelngstr(0, ne->kind);
|
|
||||||
ne->ts = createstrobj(L, size, LUA_VLNGSTR, G(L)->seed);
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
TString *luaS_newextlstr (lua_State *L,
|
|
||||||
const char *s, size_t len, lua_Alloc falloc, void *ud) {
|
|
||||||
struct NewExt ne;
|
|
||||||
if (!falloc) {
|
|
||||||
ne.kind = LSTRFIX;
|
|
||||||
f_newext(L, &ne); /* just create header */
|
|
||||||
}
|
|
||||||
else {
|
|
||||||
ne.kind = LSTRMEM;
|
|
||||||
if (luaD_rawrunprotected(L, f_newext, &ne) != LUA_OK) { /* mem. error? */
|
|
||||||
(*falloc)(ud, cast_voidp(s), len + 1, 0); /* free external string */
|
|
||||||
luaM_error(L); /* re-raise memory error */
|
|
||||||
}
|
|
||||||
ne.ts->falloc = falloc;
|
|
||||||
ne.ts->ud = ud;
|
|
||||||
}
|
|
||||||
ne.ts->shrlen = ne.kind;
|
|
||||||
ne.ts->u.lnglen = len;
|
|
||||||
ne.ts->contents = cast_charp(s);
|
|
||||||
return ne.ts;
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
/*
|
|
||||||
** Normalize an external string: If it is short, internalize it.
|
|
||||||
*/
|
|
||||||
TString *luaS_normstr (lua_State *L, TString *ts) {
|
|
||||||
size_t len = ts->u.lnglen;
|
|
||||||
if (len > LUAI_MAXSHORTLEN)
|
|
||||||
return ts; /* long string; keep the original */
|
|
||||||
else {
|
|
||||||
const char *str = getlngstr(ts);
|
|
||||||
return internshrstr(L, str, len);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|||||||
@@ -1,5 +1,5 @@
|
|||||||
/*
|
/*
|
||||||
** $Id: lstring.h $
|
** $Id: lstring.h,v 1.61.1.1 2017/04/19 17:20:42 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
|
||||||
*/
|
*/
|
||||||
@@ -12,31 +12,10 @@
|
|||||||
#include "lstate.h"
|
#include "lstate.h"
|
||||||
|
|
||||||
|
|
||||||
/*
|
#define sizelstring(l) (sizeof(union UTString) + ((l) + 1) * sizeof(char))
|
||||||
** Memory-allocation error message must be preallocated (it cannot
|
|
||||||
** be created after memory is exhausted)
|
|
||||||
*/
|
|
||||||
#define MEMERRMSG "not enough memory"
|
|
||||||
|
|
||||||
|
|
||||||
/*
|
|
||||||
** Maximum length for short strings, that is, strings that are
|
|
||||||
** internalized. (Cannot be smaller than reserved words or tags for
|
|
||||||
** metamethods, as these strings must be internalized;
|
|
||||||
** #("function") = 8, #("__newindex") = 10.)
|
|
||||||
*/
|
|
||||||
#if !defined(LUAI_MAXSHORTLEN)
|
|
||||||
#define LUAI_MAXSHORTLEN 40
|
|
||||||
#endif
|
|
||||||
|
|
||||||
|
|
||||||
/*
|
|
||||||
** Size of a short TString: Size of the header plus space for the string
|
|
||||||
** itself (including final '\0').
|
|
||||||
*/
|
|
||||||
#define sizestrshr(l) \
|
|
||||||
(offsetof(TString, contents) + ((l) + 1) * sizeof(char))
|
|
||||||
|
|
||||||
|
#define sizeludata(l) (sizeof(union UUdata) + (l))
|
||||||
|
#define sizeudata(u) sizeludata((u)->len)
|
||||||
|
|
||||||
#define luaS_newliteral(L, s) (luaS_newlstr(L, "" s, \
|
#define luaS_newliteral(L, s) (luaS_newlstr(L, "" s, \
|
||||||
(sizeof(s)/sizeof(char))-1))
|
(sizeof(s)/sizeof(char))-1))
|
||||||
@@ -45,29 +24,26 @@
|
|||||||
/*
|
/*
|
||||||
** test whether a string is a reserved word
|
** test whether a string is a reserved word
|
||||||
*/
|
*/
|
||||||
#define isreserved(s) (strisshr(s) && (s)->extra > 0)
|
#define isreserved(s) ((s)->tt == LUA_TSHRSTR && (s)->extra > 0)
|
||||||
|
|
||||||
|
|
||||||
/*
|
/*
|
||||||
** equality for short strings, which are always internalized
|
** equality for short strings, which are always internalized
|
||||||
*/
|
*/
|
||||||
#define eqshrstr(a,b) check_exp((a)->tt == LUA_VSHRSTR, (a) == (b))
|
#define eqshrstr(a,b) check_exp((a)->tt == LUA_TSHRSTR, (a) == (b))
|
||||||
|
|
||||||
|
|
||||||
LUAI_FUNC unsigned luaS_hashlongstr (TString *ts);
|
LUAI_FUNC unsigned int luaS_hash (const char *str, size_t l, unsigned int seed);
|
||||||
LUAI_FUNC int luaS_eqstr (TString *a, TString *b);
|
LUAI_FUNC unsigned int luaS_hashlongstr (TString *ts);
|
||||||
|
LUAI_FUNC int luaS_eqlngstr (TString *a, TString *b);
|
||||||
LUAI_FUNC void luaS_resize (lua_State *L, int newsize);
|
LUAI_FUNC void luaS_resize (lua_State *L, int newsize);
|
||||||
LUAI_FUNC void luaS_clearcache (global_State *g);
|
LUAI_FUNC void luaS_clearcache (global_State *g);
|
||||||
LUAI_FUNC void luaS_init (lua_State *L);
|
LUAI_FUNC void luaS_init (lua_State *L);
|
||||||
LUAI_FUNC void luaS_remove (lua_State *L, TString *ts);
|
LUAI_FUNC void luaS_remove (lua_State *L, TString *ts);
|
||||||
LUAI_FUNC Udata *luaS_newudata (lua_State *L, size_t s,
|
LUAI_FUNC Udata *luaS_newudata (lua_State *L, size_t s);
|
||||||
unsigned short nuvalue);
|
|
||||||
LUAI_FUNC TString *luaS_newlstr (lua_State *L, const char *str, size_t l);
|
LUAI_FUNC TString *luaS_newlstr (lua_State *L, const char *str, size_t l);
|
||||||
LUAI_FUNC TString *luaS_new (lua_State *L, const char *str);
|
LUAI_FUNC TString *luaS_new (lua_State *L, const char *str);
|
||||||
LUAI_FUNC TString *luaS_createlngstrobj (lua_State *L, size_t l);
|
LUAI_FUNC TString *luaS_createlngstrobj (lua_State *L, size_t l);
|
||||||
LUAI_FUNC TString *luaS_newextlstr (lua_State *L,
|
|
||||||
const char *s, size_t len, lua_Alloc falloc, void *ud);
|
|
||||||
LUAI_FUNC size_t luaS_sizelngstr (size_t len, int kind);
|
|
||||||
LUAI_FUNC TString *luaS_normstr (lua_State *L, TString *ts);
|
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
|||||||
@@ -1,5 +1,5 @@
|
|||||||
/*
|
/*
|
||||||
** $Id: ltable.h $
|
** $Id: ltable.h,v 2.23.1.2 2018/05/24 19:39:05 roberto Exp $
|
||||||
** Lua tables (hash)
|
** Lua tables (hash)
|
||||||
** See Copyright Notice in lua.h
|
** See Copyright Notice in lua.h
|
||||||
*/
|
*/
|
||||||
@@ -12,172 +12,54 @@
|
|||||||
|
|
||||||
#define gnode(t,i) (&(t)->node[i])
|
#define gnode(t,i) (&(t)->node[i])
|
||||||
#define gval(n) (&(n)->i_val)
|
#define gval(n) (&(n)->i_val)
|
||||||
#define gnext(n) ((n)->u.next)
|
#define gnext(n) ((n)->i_key.nk.next)
|
||||||
|
|
||||||
|
|
||||||
|
/* 'const' to avoid wrong writings that can mess up field 'next' */
|
||||||
|
#define gkey(n) cast(const TValue*, (&(n)->i_key.tvk))
|
||||||
|
|
||||||
/*
|
/*
|
||||||
** Clear all bits of fast-access metamethods, which means that the table
|
** writable version of 'gkey'; allows updates to individual fields,
|
||||||
** may have any of these metamethods. (First access that fails after the
|
** but not to the whole (which has incompatible type)
|
||||||
** clearing will set the bit again.)
|
|
||||||
*/
|
*/
|
||||||
#define invalidateTMcache(t) ((t)->flags &= cast_byte(~maskflags))
|
#define wgkey(n) (&(n)->i_key.nk)
|
||||||
|
|
||||||
|
#define invalidateTMcache(t) ((t)->flags = 0)
|
||||||
|
|
||||||
|
|
||||||
/*
|
/* true when 't' is using 'dummynode' as its hash part */
|
||||||
** Bit BITDUMMY set in 'flags' means the table is using the dummy node
|
#define isdummy(t) ((t)->lastfree == NULL)
|
||||||
** for its hash part.
|
|
||||||
*/
|
|
||||||
|
|
||||||
#define BITDUMMY (1 << 6)
|
|
||||||
#define NOTBITDUMMY cast_byte(~BITDUMMY)
|
|
||||||
#define isdummy(t) ((t)->flags & BITDUMMY)
|
|
||||||
|
|
||||||
#define setnodummy(t) ((t)->flags &= NOTBITDUMMY)
|
|
||||||
#define setdummy(t) ((t)->flags |= BITDUMMY)
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
/* allocated size for hash nodes */
|
/* allocated size for hash nodes */
|
||||||
#define allocsizenode(t) (isdummy(t) ? 0 : sizenode(t))
|
#define allocsizenode(t) (isdummy(t) ? 0 : sizenode(t))
|
||||||
|
|
||||||
|
|
||||||
/* returns the Node, given the value of a table entry */
|
/* returns the key, given the value of a table entry */
|
||||||
#define nodefromval(v) cast(Node *, (v))
|
#define keyfromval(v) \
|
||||||
|
(gkey(cast(Node *, cast(char *, (v)) - offsetof(Node, i_val))))
|
||||||
|
|
||||||
|
|
||||||
|
LUAI_FUNC const TValue *luaH_getint (Table *t, lua_Integer key);
|
||||||
#define luaH_fastgeti(t,k,res,tag) \
|
|
||||||
{ Table *h = t; lua_Unsigned u = l_castS2U(k) - 1u; \
|
|
||||||
if ((u < h->asize)) { \
|
|
||||||
tag = *getArrTag(h, u); \
|
|
||||||
if (!tagisempty(tag)) { farr2val(h, u, tag, res); }} \
|
|
||||||
else { tag = luaH_getint(h, (k), res); }}
|
|
||||||
|
|
||||||
|
|
||||||
#define luaH_fastseti(t,k,val,hres) \
|
|
||||||
{ Table *h = t; lua_Unsigned u = l_castS2U(k) - 1u; \
|
|
||||||
if ((u < h->asize)) { \
|
|
||||||
lu_byte *tag = getArrTag(h, u); \
|
|
||||||
if (checknoTM(h->metatable, TM_NEWINDEX) || !tagisempty(*tag)) \
|
|
||||||
{ fval2arr(h, u, tag, val); hres = HOK; } \
|
|
||||||
else hres = ~cast_int(u); } \
|
|
||||||
else { hres = luaH_psetint(h, k, val); }}
|
|
||||||
|
|
||||||
|
|
||||||
/* results from pset */
|
|
||||||
#define HOK 0
|
|
||||||
#define HNOTFOUND 1
|
|
||||||
#define HNOTATABLE 2
|
|
||||||
#define HFIRSTNODE 3
|
|
||||||
|
|
||||||
/*
|
|
||||||
** 'luaH_get*' operations set 'res', unless the value is absent, and
|
|
||||||
** return the tag of the result.
|
|
||||||
** The 'luaH_pset*' (pre-set) operations set the given value and return
|
|
||||||
** HOK, unless the original value is absent. In that case, if the key
|
|
||||||
** is really absent, they return HNOTFOUND. Otherwise, if there is a
|
|
||||||
** slot with that key but with no value, 'luaH_pset*' return an encoding
|
|
||||||
** of where the key is (usually called 'hres'). (pset cannot set that
|
|
||||||
** value because there might be a metamethod.) If the slot is in the
|
|
||||||
** hash part, the encoding is (HFIRSTNODE + hash index); if the slot is
|
|
||||||
** in the array part, the encoding is (~array index), a negative value.
|
|
||||||
** The value HNOTATABLE is used by the fast macros to signal that the
|
|
||||||
** value being indexed is not a table.
|
|
||||||
** (The size for the array part is limited by the maximum power of two
|
|
||||||
** that fits in an unsigned integer; that is INT_MAX+1. So, the C-index
|
|
||||||
** ranges from 0, which encodes to -1, to INT_MAX, which encodes to
|
|
||||||
** INT_MIN. The size of the hash part is limited by the maximum power of
|
|
||||||
** two that fits in a signed integer; that is (INT_MAX+1)/2. So, it is
|
|
||||||
** safe to add HFIRSTNODE to any index there.)
|
|
||||||
*/
|
|
||||||
|
|
||||||
|
|
||||||
/*
|
|
||||||
** The array part of a table is represented by an inverted array of
|
|
||||||
** values followed by an array of tags, to avoid wasting space with
|
|
||||||
** padding. In between them there is an unsigned int, explained later.
|
|
||||||
** The 'array' pointer points between the two arrays, so that values are
|
|
||||||
** indexed with negative indices and tags with non-negative indices.
|
|
||||||
|
|
||||||
Values Tags
|
|
||||||
--------------------------------------------------------
|
|
||||||
... | Value 1 | Value 0 |unsigned|0|1|...
|
|
||||||
--------------------------------------------------------
|
|
||||||
^ t->array
|
|
||||||
|
|
||||||
** All accesses to 't->array' should be through the macros 'getArrTag'
|
|
||||||
** and 'getArrVal'.
|
|
||||||
*/
|
|
||||||
|
|
||||||
/* Computes the address of the tag for the abstract C-index 'k' */
|
|
||||||
#define getArrTag(t,k) (cast(lu_byte*, (t)->array) + sizeof(unsigned) + (k))
|
|
||||||
|
|
||||||
/* Computes the address of the value for the abstract C-index 'k' */
|
|
||||||
#define getArrVal(t,k) ((t)->array - 1 - (k))
|
|
||||||
|
|
||||||
|
|
||||||
/*
|
|
||||||
** The unsigned between the two arrays is used as a hint for #t;
|
|
||||||
** see luaH_getn. It is stored there to avoid wasting space in
|
|
||||||
** the structure Table for tables with no array part.
|
|
||||||
*/
|
|
||||||
#define lenhint(t) cast(unsigned*, (t)->array)
|
|
||||||
|
|
||||||
|
|
||||||
/*
|
|
||||||
** Move TValues to/from arrays, using C indices
|
|
||||||
*/
|
|
||||||
#define arr2obj(h,k,val) \
|
|
||||||
((val)->tt_ = *getArrTag(h,(k)), (val)->value_ = *getArrVal(h,(k)))
|
|
||||||
|
|
||||||
#define obj2arr(h,k,val) \
|
|
||||||
(*getArrTag(h,(k)) = (val)->tt_, *getArrVal(h,(k)) = (val)->value_)
|
|
||||||
|
|
||||||
|
|
||||||
/*
|
|
||||||
** Often, we need to check the tag of a value before moving it. The
|
|
||||||
** following macros also move TValues to/from arrays, but receive the
|
|
||||||
** precomputed tag value or address as an extra argument.
|
|
||||||
*/
|
|
||||||
#define farr2val(h,k,tag,res) \
|
|
||||||
((res)->tt_ = tag, (res)->value_ = *getArrVal(h,(k)))
|
|
||||||
|
|
||||||
#define fval2arr(h,k,tag,val) \
|
|
||||||
(*tag = (val)->tt_, *getArrVal(h,(k)) = (val)->value_)
|
|
||||||
|
|
||||||
|
|
||||||
LUAI_FUNC lu_byte luaH_get (Table *t, const TValue *key, TValue *res);
|
|
||||||
LUAI_FUNC lu_byte luaH_getshortstr (Table *t, TString *key, TValue *res);
|
|
||||||
LUAI_FUNC lu_byte luaH_getstr (Table *t, TString *key, TValue *res);
|
|
||||||
LUAI_FUNC lu_byte luaH_getint (Table *t, lua_Integer key, TValue *res);
|
|
||||||
|
|
||||||
/* Special get for metamethods */
|
|
||||||
LUAI_FUNC const TValue *luaH_Hgetshortstr (Table *t, TString *key);
|
|
||||||
|
|
||||||
LUAI_FUNC int luaH_psetint (Table *t, lua_Integer key, TValue *val);
|
|
||||||
LUAI_FUNC int luaH_psetshortstr (Table *t, TString *key, TValue *val);
|
|
||||||
LUAI_FUNC int luaH_psetstr (Table *t, TString *key, TValue *val);
|
|
||||||
LUAI_FUNC int luaH_pset (Table *t, const TValue *key, TValue *val);
|
|
||||||
|
|
||||||
LUAI_FUNC void luaH_setint (lua_State *L, Table *t, lua_Integer key,
|
LUAI_FUNC void luaH_setint (lua_State *L, Table *t, lua_Integer key,
|
||||||
TValue *value);
|
TValue *value);
|
||||||
LUAI_FUNC void luaH_set (lua_State *L, Table *t, const TValue *key,
|
LUAI_FUNC const TValue *luaH_getshortstr (Table *t, TString *key);
|
||||||
TValue *value);
|
LUAI_FUNC const TValue *luaH_getstr (Table *t, TString *key);
|
||||||
|
LUAI_FUNC const TValue *luaH_get (Table *t, const TValue *key);
|
||||||
LUAI_FUNC void luaH_finishset (lua_State *L, Table *t, const TValue *key,
|
LUAI_FUNC TValue *luaH_newkey (lua_State *L, Table *t, const TValue *key);
|
||||||
TValue *value, int hres);
|
LUAI_FUNC TValue *luaH_set (lua_State *L, Table *t, const TValue *key);
|
||||||
LUAI_FUNC Table *luaH_new (lua_State *L);
|
LUAI_FUNC Table *luaH_new (lua_State *L);
|
||||||
LUAI_FUNC void luaH_resize (lua_State *L, Table *t, unsigned nasize,
|
LUAI_FUNC void luaH_resize (lua_State *L, Table *t, unsigned int nasize,
|
||||||
unsigned nhsize);
|
unsigned int nhsize);
|
||||||
LUAI_FUNC void luaH_resizearray (lua_State *L, Table *t, unsigned nasize);
|
LUAI_FUNC void luaH_resizearray (lua_State *L, Table *t, unsigned int nasize);
|
||||||
LUAI_FUNC lu_mem luaH_size (Table *t);
|
|
||||||
LUAI_FUNC void luaH_free (lua_State *L, Table *t);
|
LUAI_FUNC void luaH_free (lua_State *L, Table *t);
|
||||||
LUAI_FUNC int luaH_next (lua_State *L, Table *t, StkId key);
|
LUAI_FUNC int luaH_next (lua_State *L, Table *t, StkId key);
|
||||||
LUAI_FUNC lua_Unsigned luaH_getn (lua_State *L, Table *t);
|
LUAI_FUNC lua_Unsigned luaH_getn (Table *t);
|
||||||
|
|
||||||
|
|
||||||
#if defined(LUA_DEBUG)
|
#if defined(LUA_DEBUG)
|
||||||
LUAI_FUNC Node *luaH_mainposition (const Table *t, const TValue *key);
|
LUAI_FUNC Node *luaH_mainposition (const Table *t, const TValue *key);
|
||||||
|
LUAI_FUNC int luaH_isdummy (const Table *t);
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
@@ -1,5 +1,5 @@
|
|||||||
/*
|
/*
|
||||||
** $Id: ltablib.c $
|
** $Id: ltablib.c,v 1.93.1.1 2017/04/19 17:20:42 roberto Exp $
|
||||||
** Library for Table Manipulation
|
** Library for Table Manipulation
|
||||||
** See Copyright Notice in lua.h
|
** See Copyright Notice in lua.h
|
||||||
*/
|
*/
|
||||||
@@ -18,7 +18,6 @@
|
|||||||
|
|
||||||
#include "lauxlib.h"
|
#include "lauxlib.h"
|
||||||
#include "lualib.h"
|
#include "lualib.h"
|
||||||
#include "llimits.h"
|
|
||||||
|
|
||||||
|
|
||||||
/*
|
/*
|
||||||
@@ -59,20 +58,27 @@ static void checktab (lua_State *L, int arg, int what) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
static int tcreate (lua_State *L) {
|
#if defined(LUA_COMPAT_MAXN)
|
||||||
lua_Unsigned sizeseq = (lua_Unsigned)luaL_checkinteger(L, 1);
|
static int maxn (lua_State *L) {
|
||||||
lua_Unsigned sizerest = (lua_Unsigned)luaL_optinteger(L, 2, 0);
|
lua_Number max = 0;
|
||||||
luaL_argcheck(L, sizeseq <= cast_uint(INT_MAX), 1, "out of range");
|
luaL_checktype(L, 1, LUA_TTABLE);
|
||||||
luaL_argcheck(L, sizerest <= cast_uint(INT_MAX), 2, "out of range");
|
lua_pushnil(L); /* first key */
|
||||||
lua_createtable(L, cast_int(sizeseq), cast_int(sizerest));
|
while (lua_next(L, 1)) {
|
||||||
|
lua_pop(L, 1); /* remove value */
|
||||||
|
if (lua_type(L, -1) == LUA_TNUMBER) {
|
||||||
|
lua_Number v = lua_tonumber(L, -1);
|
||||||
|
if (v > max) max = v;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
lua_pushnumber(L, max);
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
|
#endif
|
||||||
|
|
||||||
|
|
||||||
static int tinsert (lua_State *L) {
|
static int tinsert (lua_State *L) {
|
||||||
|
lua_Integer e = aux_getn(L, 1, TAB_RW) + 1; /* first empty element */
|
||||||
lua_Integer pos; /* where to insert new element */
|
lua_Integer pos; /* where to insert new element */
|
||||||
lua_Integer e = aux_getn(L, 1, TAB_RW);
|
|
||||||
e = luaL_intop(+, e, 1); /* first empty element */
|
|
||||||
switch (lua_gettop(L)) {
|
switch (lua_gettop(L)) {
|
||||||
case 2: { /* called with only 2 arguments */
|
case 2: { /* called with only 2 arguments */
|
||||||
pos = e; /* insert new element at the end */
|
pos = e; /* insert new element at the end */
|
||||||
@@ -81,9 +87,7 @@ static int tinsert (lua_State *L) {
|
|||||||
case 3: {
|
case 3: {
|
||||||
lua_Integer i;
|
lua_Integer i;
|
||||||
pos = luaL_checkinteger(L, 2); /* 2nd argument is the position */
|
pos = luaL_checkinteger(L, 2); /* 2nd argument is the position */
|
||||||
/* check whether 'pos' is in [1, e] */
|
luaL_argcheck(L, 1 <= pos && pos <= e, 2, "position out of bounds");
|
||||||
luaL_argcheck(L, (lua_Unsigned)pos - 1u < (lua_Unsigned)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_geti(L, 1, i - 1);
|
lua_geti(L, 1, i - 1);
|
||||||
lua_seti(L, 1, i); /* t[i] = t[i - 1] */
|
lua_seti(L, 1, i); /* t[i] = t[i - 1] */
|
||||||
@@ -103,16 +107,14 @@ static int tremove (lua_State *L) {
|
|||||||
lua_Integer size = aux_getn(L, 1, TAB_RW);
|
lua_Integer size = aux_getn(L, 1, TAB_RW);
|
||||||
lua_Integer pos = luaL_optinteger(L, 2, size);
|
lua_Integer pos = luaL_optinteger(L, 2, size);
|
||||||
if (pos != size) /* validate 'pos' if given */
|
if (pos != size) /* validate 'pos' if given */
|
||||||
/* check whether 'pos' is in [1, size + 1] */
|
luaL_argcheck(L, 1 <= pos && pos <= size + 1, 1, "position out of bounds");
|
||||||
luaL_argcheck(L, (lua_Unsigned)pos - 1u <= (lua_Unsigned)size, 2,
|
|
||||||
"position out of bounds");
|
|
||||||
lua_geti(L, 1, pos); /* result = t[pos] */
|
lua_geti(L, 1, pos); /* result = t[pos] */
|
||||||
for ( ; pos < size; pos++) {
|
for ( ; pos < size; pos++) {
|
||||||
lua_geti(L, 1, pos + 1);
|
lua_geti(L, 1, pos + 1);
|
||||||
lua_seti(L, 1, pos); /* t[pos] = t[pos + 1] */
|
lua_seti(L, 1, pos); /* t[pos] = t[pos + 1] */
|
||||||
}
|
}
|
||||||
lua_pushnil(L);
|
lua_pushnil(L);
|
||||||
lua_seti(L, 1, pos); /* remove entry t[pos] */
|
lua_seti(L, 1, pos); /* t[pos] = nil */
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -157,9 +159,9 @@ static int tmove (lua_State *L) {
|
|||||||
|
|
||||||
static void addfield (lua_State *L, luaL_Buffer *b, lua_Integer i) {
|
static void addfield (lua_State *L, luaL_Buffer *b, lua_Integer i) {
|
||||||
lua_geti(L, 1, i);
|
lua_geti(L, 1, i);
|
||||||
if (l_unlikely(!lua_isstring(L, -1)))
|
if (!lua_isstring(L, -1))
|
||||||
luaL_error(L, "invalid value (%s) at index %I in table for 'concat'",
|
luaL_error(L, "invalid value (%s) at index %d in table for 'concat'",
|
||||||
luaL_typename(L, -1), (LUAI_UACINT)i);
|
luaL_typename(L, -1), i);
|
||||||
luaL_addvalue(b);
|
luaL_addvalue(b);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -189,7 +191,7 @@ static int tconcat (lua_State *L) {
|
|||||||
** =======================================================
|
** =======================================================
|
||||||
*/
|
*/
|
||||||
|
|
||||||
static int tpack (lua_State *L) {
|
static int pack (lua_State *L) {
|
||||||
int i;
|
int i;
|
||||||
int n = lua_gettop(L); /* number of elements to pack */
|
int n = lua_gettop(L); /* number of elements to pack */
|
||||||
lua_createtable(L, n, 1); /* create result table */
|
lua_createtable(L, n, 1); /* create result table */
|
||||||
@@ -202,14 +204,13 @@ static int tpack (lua_State *L) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
static int tunpack (lua_State *L) {
|
static int unpack (lua_State *L) {
|
||||||
lua_Unsigned n;
|
lua_Unsigned n;
|
||||||
lua_Integer i = luaL_optinteger(L, 2, 1);
|
lua_Integer i = luaL_optinteger(L, 2, 1);
|
||||||
lua_Integer e = luaL_opt(L, luaL_checkinteger, 3, luaL_len(L, 1));
|
lua_Integer e = luaL_opt(L, luaL_checkinteger, 3, luaL_len(L, 1));
|
||||||
if (i > e) return 0; /* empty range */
|
if (i > e) return 0; /* empty range */
|
||||||
n = l_castS2U(e) - l_castS2U(i); /* number of elements minus 1 */
|
n = (lua_Unsigned)e - i; /* number of elements minus 1 (avoid overflows) */
|
||||||
if (l_unlikely(n >= (unsigned int)INT_MAX ||
|
if (n >= (unsigned int)INT_MAX || !lua_checkstack(L, (int)(++n)))
|
||||||
!lua_checkstack(L, (int)(++n))))
|
|
||||||
return luaL_error(L, "too many results to unpack");
|
return luaL_error(L, "too many results to unpack");
|
||||||
for (; i < e; i++) { /* push arg[i..e - 1] (to avoid overflows) */
|
for (; i < e; i++) { /* push arg[i..e - 1] (to avoid overflows) */
|
||||||
lua_geti(L, 1, i);
|
lua_geti(L, 1, i);
|
||||||
@@ -231,26 +232,41 @@ static int tunpack (lua_State *L) {
|
|||||||
*/
|
*/
|
||||||
|
|
||||||
|
|
||||||
/*
|
/* type for array indices */
|
||||||
** Type for array indices. These indices are always limited by INT_MAX,
|
|
||||||
** so it is safe to cast them to lua_Integer even for Lua 32 bits.
|
|
||||||
*/
|
|
||||||
typedef unsigned int IdxT;
|
typedef unsigned int IdxT;
|
||||||
|
|
||||||
|
|
||||||
/* Versions of lua_seti/lua_geti specialized for IdxT */
|
|
||||||
#define geti(L,idt,idx) lua_geti(L, idt, l_castU2S(idx))
|
|
||||||
#define seti(L,idt,idx) lua_seti(L, idt, l_castU2S(idx))
|
|
||||||
|
|
||||||
|
|
||||||
/*
|
/*
|
||||||
** Produce a "random" 'unsigned int' to randomize pivot choice. This
|
** Produce a "random" 'unsigned int' to randomize pivot choice. This
|
||||||
** macro is used only when 'sort' detects a big imbalance in the result
|
** macro is used only when 'sort' detects a big imbalance in the result
|
||||||
** of a partition. (If you don't want/need this "randomness", ~0 is a
|
** of a partition. (If you don't want/need this "randomness", ~0 is a
|
||||||
** good choice.)
|
** good choice.)
|
||||||
*/
|
*/
|
||||||
#if !defined(l_randomizePivot)
|
#if !defined(l_randomizePivot) /* { */
|
||||||
#define l_randomizePivot(L) luaL_makeseed(L)
|
|
||||||
|
#include <time.h>
|
||||||
|
|
||||||
|
/* size of 'e' measured in number of 'unsigned int's */
|
||||||
|
#define sof(e) (sizeof(e) / sizeof(unsigned int))
|
||||||
|
|
||||||
|
/*
|
||||||
|
** Use 'time' and 'clock' as sources of "randomness". Because we don't
|
||||||
|
** know the types 'clock_t' and 'time_t', we cannot cast them to
|
||||||
|
** anything without risking overflows. A safe way to use their values
|
||||||
|
** is to copy them to an array of a known type and use the array values.
|
||||||
|
*/
|
||||||
|
static unsigned int l_randomizePivot (void) {
|
||||||
|
clock_t c = clock();
|
||||||
|
time_t t = time(NULL);
|
||||||
|
unsigned int buff[sof(c) + sof(t)];
|
||||||
|
unsigned int i, rnd = 0;
|
||||||
|
memcpy(buff, &c, sof(c) * sizeof(unsigned int));
|
||||||
|
memcpy(buff + sof(c), &t, sof(t) * sizeof(unsigned int));
|
||||||
|
for (i = 0; i < sof(buff); i++)
|
||||||
|
rnd += buff[i];
|
||||||
|
return rnd;
|
||||||
|
}
|
||||||
|
|
||||||
#endif /* } */
|
#endif /* } */
|
||||||
|
|
||||||
|
|
||||||
@@ -259,8 +275,8 @@ typedef unsigned int IdxT;
|
|||||||
|
|
||||||
|
|
||||||
static void set2 (lua_State *L, IdxT i, IdxT j) {
|
static void set2 (lua_State *L, IdxT i, IdxT j) {
|
||||||
seti(L, 1, i);
|
lua_seti(L, 1, i);
|
||||||
seti(L, 1, j);
|
lua_seti(L, 1, j);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@@ -297,15 +313,15 @@ static IdxT partition (lua_State *L, IdxT lo, IdxT up) {
|
|||||||
/* loop invariant: a[lo .. i] <= P <= a[j .. up] */
|
/* loop invariant: a[lo .. i] <= P <= a[j .. up] */
|
||||||
for (;;) {
|
for (;;) {
|
||||||
/* next loop: repeat ++i while a[i] < P */
|
/* next loop: repeat ++i while a[i] < P */
|
||||||
while ((void)geti(L, 1, ++i), sort_comp(L, -1, -2)) {
|
while (lua_geti(L, 1, ++i), sort_comp(L, -1, -2)) {
|
||||||
if (l_unlikely(i == up - 1)) /* a[up - 1] < P == a[up - 1] */
|
if (i == up - 1) /* a[i] < P but a[up - 1] == P ?? */
|
||||||
luaL_error(L, "invalid order function for sorting");
|
luaL_error(L, "invalid order function for sorting");
|
||||||
lua_pop(L, 1); /* remove a[i] */
|
lua_pop(L, 1); /* remove a[i] */
|
||||||
}
|
}
|
||||||
/* after the loop, a[i] >= P and a[lo .. i - 1] < P (a) */
|
/* after the loop, a[i] >= P and a[lo .. i - 1] < P */
|
||||||
/* next loop: repeat --j while P < a[j] */
|
/* next loop: repeat --j while P < a[j] */
|
||||||
while ((void)geti(L, 1, --j), sort_comp(L, -3, -1)) {
|
while (lua_geti(L, 1, --j), sort_comp(L, -3, -1)) {
|
||||||
if (l_unlikely(j < i)) /* j <= i - 1 and a[j] > P, contradicts (a) */
|
if (j < i) /* j < i but a[j] > P ?? */
|
||||||
luaL_error(L, "invalid order function for sorting");
|
luaL_error(L, "invalid order function for sorting");
|
||||||
lua_pop(L, 1); /* remove a[j] */
|
lua_pop(L, 1); /* remove a[j] */
|
||||||
}
|
}
|
||||||
@@ -329,22 +345,23 @@ static IdxT partition (lua_State *L, IdxT lo, IdxT up) {
|
|||||||
*/
|
*/
|
||||||
static IdxT choosePivot (IdxT lo, IdxT up, unsigned int rnd) {
|
static IdxT choosePivot (IdxT lo, IdxT up, unsigned int rnd) {
|
||||||
IdxT r4 = (up - lo) / 4; /* range/4 */
|
IdxT r4 = (up - lo) / 4; /* range/4 */
|
||||||
IdxT p = (rnd ^ lo ^ up) % (r4 * 2) + (lo + r4);
|
IdxT p = rnd % (r4 * 2) + (lo + r4);
|
||||||
lua_assert(lo + r4 <= p && p <= up - r4);
|
lua_assert(lo + r4 <= p && p <= up - r4);
|
||||||
return p;
|
return p;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/*
|
/*
|
||||||
** Quicksort algorithm (recursive function)
|
** QuickSort algorithm (recursive function)
|
||||||
*/
|
*/
|
||||||
static void auxsort (lua_State *L, IdxT lo, IdxT up, unsigned rnd) {
|
static void auxsort (lua_State *L, IdxT lo, IdxT up,
|
||||||
|
unsigned int rnd) {
|
||||||
while (lo < up) { /* loop for tail recursion */
|
while (lo < up) { /* loop for tail recursion */
|
||||||
IdxT p; /* Pivot index */
|
IdxT p; /* Pivot index */
|
||||||
IdxT n; /* to be used later */
|
IdxT n; /* to be used later */
|
||||||
/* sort elements 'lo', 'p', and 'up' */
|
/* sort elements 'lo', 'p', and 'up' */
|
||||||
geti(L, 1, lo);
|
lua_geti(L, 1, lo);
|
||||||
geti(L, 1, up);
|
lua_geti(L, 1, up);
|
||||||
if (sort_comp(L, -1, -2)) /* a[up] < a[lo]? */
|
if (sort_comp(L, -1, -2)) /* a[up] < a[lo]? */
|
||||||
set2(L, lo, up); /* swap a[lo] - a[up] */
|
set2(L, lo, up); /* swap a[lo] - a[up] */
|
||||||
else
|
else
|
||||||
@@ -355,13 +372,13 @@ static void auxsort (lua_State *L, IdxT lo, IdxT up, unsigned rnd) {
|
|||||||
p = (lo + up)/2; /* middle element is a good pivot */
|
p = (lo + up)/2; /* middle element is a good pivot */
|
||||||
else /* for larger intervals, it is worth a random pivot */
|
else /* for larger intervals, it is worth a random pivot */
|
||||||
p = choosePivot(lo, up, rnd);
|
p = choosePivot(lo, up, rnd);
|
||||||
geti(L, 1, p);
|
lua_geti(L, 1, p);
|
||||||
geti(L, 1, lo);
|
lua_geti(L, 1, lo);
|
||||||
if (sort_comp(L, -2, -1)) /* a[p] < a[lo]? */
|
if (sort_comp(L, -2, -1)) /* a[p] < a[lo]? */
|
||||||
set2(L, p, lo); /* swap a[p] - a[lo] */
|
set2(L, p, lo); /* swap a[p] - a[lo] */
|
||||||
else {
|
else {
|
||||||
lua_pop(L, 1); /* remove a[lo] */
|
lua_pop(L, 1); /* remove a[lo] */
|
||||||
geti(L, 1, up);
|
lua_geti(L, 1, up);
|
||||||
if (sort_comp(L, -1, -2)) /* a[up] < a[p]? */
|
if (sort_comp(L, -1, -2)) /* a[up] < a[p]? */
|
||||||
set2(L, p, up); /* swap a[up] - a[p] */
|
set2(L, p, up); /* swap a[up] - a[p] */
|
||||||
else
|
else
|
||||||
@@ -369,9 +386,9 @@ static void auxsort (lua_State *L, IdxT lo, IdxT up, unsigned rnd) {
|
|||||||
}
|
}
|
||||||
if (up - lo == 2) /* only 3 elements? */
|
if (up - lo == 2) /* only 3 elements? */
|
||||||
return; /* already sorted */
|
return; /* already sorted */
|
||||||
geti(L, 1, p); /* get middle element (Pivot) */
|
lua_geti(L, 1, p); /* get middle element (Pivot) */
|
||||||
lua_pushvalue(L, -1); /* push Pivot */
|
lua_pushvalue(L, -1); /* push Pivot */
|
||||||
geti(L, 1, up - 1); /* push a[up - 1] */
|
lua_geti(L, 1, up - 1); /* push a[up - 1] */
|
||||||
set2(L, p, up - 1); /* swap Pivot (a[p]) with a[up - 1] */
|
set2(L, p, up - 1); /* swap Pivot (a[p]) with a[up - 1] */
|
||||||
p = partition(L, lo, up);
|
p = partition(L, lo, up);
|
||||||
/* a[lo .. p - 1] <= a[p] == P <= a[p + 1 .. up] */
|
/* a[lo .. p - 1] <= a[p] == P <= a[p + 1 .. up] */
|
||||||
@@ -386,7 +403,7 @@ static void auxsort (lua_State *L, IdxT lo, IdxT up, unsigned rnd) {
|
|||||||
up = p - 1; /* tail call for [lo .. p - 1] (lower interval) */
|
up = p - 1; /* tail call for [lo .. p - 1] (lower interval) */
|
||||||
}
|
}
|
||||||
if ((up - lo) / 128 > n) /* partition too imbalanced? */
|
if ((up - lo) / 128 > n) /* partition too imbalanced? */
|
||||||
rnd = l_randomizePivot(L); /* try a new randomization */
|
rnd = l_randomizePivot(); /* try a new randomization */
|
||||||
} /* tail call auxsort(L, lo, up, rnd) */
|
} /* tail call auxsort(L, lo, up, rnd) */
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -408,10 +425,12 @@ static int sort (lua_State *L) {
|
|||||||
|
|
||||||
static const luaL_Reg tab_funcs[] = {
|
static const luaL_Reg tab_funcs[] = {
|
||||||
{"concat", tconcat},
|
{"concat", tconcat},
|
||||||
{"create", tcreate},
|
#if defined(LUA_COMPAT_MAXN)
|
||||||
|
{"maxn", maxn},
|
||||||
|
#endif
|
||||||
{"insert", tinsert},
|
{"insert", tinsert},
|
||||||
{"pack", tpack},
|
{"pack", pack},
|
||||||
{"unpack", tunpack},
|
{"unpack", unpack},
|
||||||
{"remove", tremove},
|
{"remove", tremove},
|
||||||
{"move", tmove},
|
{"move", tmove},
|
||||||
{"sort", sort},
|
{"sort", sort},
|
||||||
@@ -421,6 +440,11 @@ static const luaL_Reg tab_funcs[] = {
|
|||||||
|
|
||||||
LUAMOD_API int luaopen_table (lua_State *L) {
|
LUAMOD_API int luaopen_table (lua_State *L) {
|
||||||
luaL_newlib(L, tab_funcs);
|
luaL_newlib(L, tab_funcs);
|
||||||
|
#if defined(LUA_COMPAT_UNPACK)
|
||||||
|
/* _G.unpack = table.unpack */
|
||||||
|
lua_getfield(L, -1, "unpack");
|
||||||
|
lua_setglobal(L, "unpack");
|
||||||
|
#endif
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -1,5 +1,5 @@
|
|||||||
/*
|
/*
|
||||||
** $Id: ltests.h $
|
** $Id: ltests.h,v 2.50.1.1 2017/04/19 17:20:42 roberto Exp $
|
||||||
** Internal Header for Debugging of the Lua Implementation
|
** Internal Header for Debugging of the Lua Implementation
|
||||||
** See Copyright Notice in lua.h
|
** See Copyright Notice in lua.h
|
||||||
*/
|
*/
|
||||||
@@ -8,19 +8,29 @@
|
|||||||
#define ltests_h
|
#define ltests_h
|
||||||
|
|
||||||
|
|
||||||
#include <stdio.h>
|
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
|
|
||||||
/* test Lua with compatibility code */
|
/* test Lua with no compatibility code */
|
||||||
#define LUA_COMPAT_MATHLIB
|
#undef LUA_COMPAT_MATHLIB
|
||||||
#undef LUA_COMPAT_GLOBAL
|
#undef LUA_COMPAT_IPAIRS
|
||||||
|
#undef LUA_COMPAT_BITLIB
|
||||||
|
#undef LUA_COMPAT_APIINTCASTS
|
||||||
|
#undef LUA_COMPAT_FLOATSTRING
|
||||||
|
#undef LUA_COMPAT_UNPACK
|
||||||
|
#undef LUA_COMPAT_LOADERS
|
||||||
|
#undef LUA_COMPAT_LOG10
|
||||||
|
#undef LUA_COMPAT_LOADSTRING
|
||||||
|
#undef LUA_COMPAT_MAXN
|
||||||
|
#undef LUA_COMPAT_MODULE
|
||||||
|
|
||||||
|
|
||||||
#define LUA_DEBUG
|
#define LUA_DEBUG
|
||||||
|
|
||||||
|
|
||||||
/* turn on assertions */
|
/* turn on assertions */
|
||||||
#define LUAI_ASSERT
|
#undef NDEBUG
|
||||||
|
#include <assert.h>
|
||||||
|
#define lua_assert(c) assert(c)
|
||||||
|
|
||||||
|
|
||||||
/* to avoid warnings, and to make sure value is really unused */
|
/* to avoid warnings, and to make sure value is really unused */
|
||||||
@@ -36,65 +46,29 @@
|
|||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
|
||||||
/* get a chance to test code without jump tables */
|
|
||||||
#define LUA_USE_JUMPTABLE 0
|
|
||||||
|
|
||||||
|
|
||||||
/* use 32-bit integers in random generator */
|
|
||||||
#define LUA_RAND32
|
|
||||||
|
|
||||||
|
|
||||||
/* test stack reallocation without strict address use */
|
|
||||||
#define LUAI_STRICT_ADDRESS 0
|
|
||||||
|
|
||||||
|
|
||||||
/* memory-allocator control variables */
|
/* memory-allocator control variables */
|
||||||
typedef struct Memcontrol {
|
typedef struct Memcontrol {
|
||||||
int failnext;
|
|
||||||
unsigned long numblocks;
|
unsigned long numblocks;
|
||||||
unsigned long total;
|
unsigned long total;
|
||||||
unsigned long maxmem;
|
unsigned long maxmem;
|
||||||
unsigned long memlimit;
|
unsigned long memlimit;
|
||||||
unsigned long countlimit;
|
unsigned long objcount[LUA_NUMTAGS];
|
||||||
unsigned long objcount[LUA_NUMTYPES];
|
|
||||||
} Memcontrol;
|
} Memcontrol;
|
||||||
|
|
||||||
LUA_API Memcontrol l_memcontrol;
|
LUA_API Memcontrol l_memcontrol;
|
||||||
|
|
||||||
|
|
||||||
#define luai_tracegc(L,f) luai_tracegctest(L, f)
|
|
||||||
extern void luai_tracegctest (lua_State *L, int first);
|
|
||||||
|
|
||||||
|
|
||||||
/*
|
/*
|
||||||
** generic variable for debug tricks
|
** generic variable for debug tricks
|
||||||
*/
|
*/
|
||||||
extern void *l_Trick;
|
extern void *l_Trick;
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
/*
|
/*
|
||||||
** Function to traverse and check all memory used by Lua
|
** Function to traverse and check all memory used by Lua
|
||||||
*/
|
*/
|
||||||
extern int lua_checkmemory (lua_State *L);
|
int lua_checkmemory (lua_State *L);
|
||||||
|
|
||||||
/*
|
|
||||||
** Function to print an object GC-friendly
|
|
||||||
*/
|
|
||||||
struct GCObject;
|
|
||||||
extern void lua_printobj (lua_State *L, struct GCObject *o);
|
|
||||||
|
|
||||||
|
|
||||||
/*
|
|
||||||
** Function to print a value
|
|
||||||
*/
|
|
||||||
struct TValue;
|
|
||||||
extern void lua_printvalue (struct TValue *v);
|
|
||||||
|
|
||||||
/*
|
|
||||||
** Function to print the stack
|
|
||||||
*/
|
|
||||||
extern void lua_printstack (lua_State *L);
|
|
||||||
extern int lua_printallstack (lua_State *L);
|
|
||||||
|
|
||||||
|
|
||||||
/* test for lock/unlock */
|
/* test for lock/unlock */
|
||||||
@@ -121,14 +95,13 @@ LUA_API int luaB_opentests (lua_State *L);
|
|||||||
LUA_API void *debug_realloc (void *ud, void *block,
|
LUA_API void *debug_realloc (void *ud, void *block,
|
||||||
size_t osize, size_t nsize);
|
size_t osize, size_t nsize);
|
||||||
|
|
||||||
|
#if defined(lua_c)
|
||||||
#define luaL_newstate() \
|
#define luaL_newstate() lua_newstate(debug_realloc, &l_memcontrol)
|
||||||
lua_newstate(debug_realloc, &l_memcontrol, luaL_makeseed(NULL))
|
#define luaL_openlibs(L) \
|
||||||
#define luai_openlibs(L) \
|
{ (luaL_openlibs)(L); \
|
||||||
{ luaL_openlibs(L); \
|
|
||||||
luaL_requiref(L, "T", luaB_opentests, 1); \
|
luaL_requiref(L, "T", luaB_opentests, 1); \
|
||||||
lua_pop(L, 1); }
|
lua_pop(L, 1); }
|
||||||
|
#endif
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
@@ -137,30 +110,20 @@ LUA_API void *debug_realloc (void *ud, void *block,
|
|||||||
#undef LUAL_BUFFERSIZE
|
#undef LUAL_BUFFERSIZE
|
||||||
#define LUAL_BUFFERSIZE 23
|
#define LUAL_BUFFERSIZE 23
|
||||||
#define MINSTRTABSIZE 2
|
#define MINSTRTABSIZE 2
|
||||||
#define MAXIWTHABS 3
|
#define MAXINDEXRK 1
|
||||||
|
|
||||||
|
|
||||||
|
/* make stack-overflow tests run faster */
|
||||||
|
#undef LUAI_MAXSTACK
|
||||||
|
#define LUAI_MAXSTACK 50000
|
||||||
|
|
||||||
|
|
||||||
|
#undef LUAI_USER_ALIGNMENT_T
|
||||||
|
#define LUAI_USER_ALIGNMENT_T union { char b[sizeof(void*) * 8]; }
|
||||||
|
|
||||||
|
|
||||||
#define STRCACHE_N 23
|
#define STRCACHE_N 23
|
||||||
#define STRCACHE_M 5
|
#define STRCACHE_M 5
|
||||||
|
|
||||||
#define MAXINDEXRK 1
|
|
||||||
|
|
||||||
|
|
||||||
/*
|
|
||||||
** Reduce maximum stack size to make stack-overflow tests run faster.
|
|
||||||
** (But value is still large enough to overflow smaller integers.)
|
|
||||||
*/
|
|
||||||
#define LUAI_MAXSTACK 68000
|
|
||||||
|
|
||||||
|
|
||||||
/* test mode uses more stack space */
|
|
||||||
#undef LUAI_MAXCCALLS
|
|
||||||
#define LUAI_MAXCCALLS 180
|
|
||||||
|
|
||||||
|
|
||||||
/* force Lua to use its own implementations */
|
|
||||||
#undef lua_strx2number
|
|
||||||
#undef lua_number2strx
|
|
||||||
|
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
|||||||
@@ -1,5 +1,5 @@
|
|||||||
/*
|
/*
|
||||||
** $Id: ltm.c $
|
** $Id: ltm.c,v 2.38.1.1 2017/04/19 17:39:34 roberto Exp $
|
||||||
** Tag methods
|
** Tag methods
|
||||||
** See Copyright Notice in lua.h
|
** See Copyright Notice in lua.h
|
||||||
*/
|
*/
|
||||||
@@ -16,7 +16,6 @@
|
|||||||
|
|
||||||
#include "ldebug.h"
|
#include "ldebug.h"
|
||||||
#include "ldo.h"
|
#include "ldo.h"
|
||||||
#include "lgc.h"
|
|
||||||
#include "lobject.h"
|
#include "lobject.h"
|
||||||
#include "lstate.h"
|
#include "lstate.h"
|
||||||
#include "lstring.h"
|
#include "lstring.h"
|
||||||
@@ -27,11 +26,11 @@
|
|||||||
|
|
||||||
static const char udatatypename[] = "userdata";
|
static const char udatatypename[] = "userdata";
|
||||||
|
|
||||||
LUAI_DDEF const char *const luaT_typenames_[LUA_TOTALTYPES] = {
|
LUAI_DDEF const char *const luaT_typenames_[LUA_TOTALTAGS] = {
|
||||||
"no value",
|
"no value",
|
||||||
"nil", "boolean", udatatypename, "number",
|
"nil", "boolean", udatatypename, "number",
|
||||||
"string", "table", "function", udatatypename, "thread",
|
"string", "table", "function", udatatypename, "thread",
|
||||||
"upvalue", "proto" /* these last cases are used for tests only */
|
"proto" /* this last case is used for tests only */
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
@@ -43,7 +42,7 @@ void luaT_init (lua_State *L) {
|
|||||||
"__div", "__idiv",
|
"__div", "__idiv",
|
||||||
"__band", "__bor", "__bxor", "__shl", "__shr",
|
"__band", "__bor", "__bxor", "__shl", "__shr",
|
||||||
"__unm", "__bnot", "__lt", "__le",
|
"__unm", "__bnot", "__lt", "__le",
|
||||||
"__concat", "__call", "__close"
|
"__concat", "__call"
|
||||||
};
|
};
|
||||||
int i;
|
int i;
|
||||||
for (i=0; i<TM_N; i++) {
|
for (i=0; i<TM_N; i++) {
|
||||||
@@ -58,9 +57,9 @@ void luaT_init (lua_State *L) {
|
|||||||
** tag methods
|
** tag methods
|
||||||
*/
|
*/
|
||||||
const TValue *luaT_gettm (Table *events, TMS event, TString *ename) {
|
const TValue *luaT_gettm (Table *events, TMS event, TString *ename) {
|
||||||
const TValue *tm = luaH_Hgetshortstr(events, ename);
|
const TValue *tm = luaH_getshortstr(events, ename);
|
||||||
lua_assert(event <= TM_EQ);
|
lua_assert(event <= TM_EQ);
|
||||||
if (notm(tm)) { /* no tag method? */
|
if (ttisnil(tm)) { /* no tag method? */
|
||||||
events->flags |= cast_byte(1u<<event); /* cache this fact */
|
events->flags |= cast_byte(1u<<event); /* cache this fact */
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
@@ -70,7 +69,7 @@ const TValue *luaT_gettm (Table *events, TMS event, TString *ename) {
|
|||||||
|
|
||||||
const TValue *luaT_gettmbyobj (lua_State *L, const TValue *o, TMS event) {
|
const TValue *luaT_gettmbyobj (lua_State *L, const TValue *o, TMS event) {
|
||||||
Table *mt;
|
Table *mt;
|
||||||
switch (ttype(o)) {
|
switch (ttnov(o)) {
|
||||||
case LUA_TTABLE:
|
case LUA_TTABLE:
|
||||||
mt = hvalue(o)->metatable;
|
mt = hvalue(o)->metatable;
|
||||||
break;
|
break;
|
||||||
@@ -78,9 +77,9 @@ const TValue *luaT_gettmbyobj (lua_State *L, const TValue *o, TMS event) {
|
|||||||
mt = uvalue(o)->metatable;
|
mt = uvalue(o)->metatable;
|
||||||
break;
|
break;
|
||||||
default:
|
default:
|
||||||
mt = G(L)->mt[ttype(o)];
|
mt = G(L)->mt[ttnov(o)];
|
||||||
}
|
}
|
||||||
return (mt ? luaH_Hgetshortstr(mt, G(L)->tmname[event]) : &G(L)->nilvalue);
|
return (mt ? luaH_getshortstr(mt, G(L)->tmname[event]) : luaO_nilobject);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@@ -92,68 +91,58 @@ const char *luaT_objtypename (lua_State *L, const TValue *o) {
|
|||||||
Table *mt;
|
Table *mt;
|
||||||
if ((ttistable(o) && (mt = hvalue(o)->metatable) != NULL) ||
|
if ((ttistable(o) && (mt = hvalue(o)->metatable) != NULL) ||
|
||||||
(ttisfulluserdata(o) && (mt = uvalue(o)->metatable) != NULL)) {
|
(ttisfulluserdata(o) && (mt = uvalue(o)->metatable) != NULL)) {
|
||||||
const TValue *name = luaH_Hgetshortstr(mt, luaS_new(L, "__name"));
|
const TValue *name = luaH_getshortstr(mt, luaS_new(L, "__name"));
|
||||||
if (ttisstring(name)) /* is '__name' a string? */
|
if (ttisstring(name)) /* is '__name' a string? */
|
||||||
return getstr(tsvalue(name)); /* use it as type name */
|
return getstr(tsvalue(name)); /* use it as type name */
|
||||||
}
|
}
|
||||||
return ttypename(ttype(o)); /* else use standard type name */
|
return ttypename(ttnov(o)); /* else use standard type name */
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
void luaT_callTM (lua_State *L, const TValue *f, const TValue *p1,
|
void luaT_callTM (lua_State *L, const TValue *f, const TValue *p1,
|
||||||
const TValue *p2, const TValue *p3) {
|
const TValue *p2, TValue *p3, int hasres) {
|
||||||
StkId func = L->top.p;
|
ptrdiff_t result = savestack(L, p3);
|
||||||
|
StkId func = L->top;
|
||||||
setobj2s(L, func, f); /* push function (assume EXTRA_STACK) */
|
setobj2s(L, func, f); /* push function (assume EXTRA_STACK) */
|
||||||
setobj2s(L, func + 1, p1); /* 1st argument */
|
setobj2s(L, func + 1, p1); /* 1st argument */
|
||||||
setobj2s(L, func + 2, p2); /* 2nd argument */
|
setobj2s(L, func + 2, p2); /* 2nd argument */
|
||||||
setobj2s(L, func + 3, p3); /* 3rd argument */
|
L->top += 3;
|
||||||
L->top.p = func + 4;
|
if (!hasres) /* no result? 'p3' is third argument */
|
||||||
|
setobj2s(L, L->top++, p3); /* 3rd argument */
|
||||||
/* metamethod may yield only when called from Lua code */
|
/* metamethod may yield only when called from Lua code */
|
||||||
if (isLuacode(L->ci))
|
if (isLua(L->ci))
|
||||||
luaD_call(L, func, 0);
|
luaD_call(L, func, hasres);
|
||||||
else
|
else
|
||||||
luaD_callnoyield(L, func, 0);
|
luaD_callnoyield(L, func, hasres);
|
||||||
|
if (hasres) { /* if has result, move it to its place */
|
||||||
|
p3 = restorestack(L, result);
|
||||||
|
setobjs2s(L, p3, --L->top);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
lu_byte luaT_callTMres (lua_State *L, const TValue *f, const TValue *p1,
|
int luaT_callbinTM (lua_State *L, const TValue *p1, const TValue *p2,
|
||||||
const TValue *p2, StkId res) {
|
StkId res, TMS event) {
|
||||||
ptrdiff_t result = savestack(L, res);
|
|
||||||
StkId func = L->top.p;
|
|
||||||
setobj2s(L, func, f); /* push function (assume EXTRA_STACK) */
|
|
||||||
setobj2s(L, func + 1, p1); /* 1st argument */
|
|
||||||
setobj2s(L, func + 2, p2); /* 2nd argument */
|
|
||||||
L->top.p += 3;
|
|
||||||
/* metamethod may yield only when called from Lua code */
|
|
||||||
if (isLuacode(L->ci))
|
|
||||||
luaD_call(L, func, 1);
|
|
||||||
else
|
|
||||||
luaD_callnoyield(L, func, 1);
|
|
||||||
res = restorestack(L, result);
|
|
||||||
setobjs2s(L, res, --L->top.p); /* move result to its place */
|
|
||||||
return ttypetag(s2v(res)); /* return tag of the result */
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
static int callbinTM (lua_State *L, const TValue *p1, const TValue *p2,
|
|
||||||
StkId res, TMS event) {
|
|
||||||
const TValue *tm = luaT_gettmbyobj(L, p1, event); /* try first operand */
|
const TValue *tm = luaT_gettmbyobj(L, p1, event); /* try first operand */
|
||||||
if (notm(tm))
|
if (ttisnil(tm))
|
||||||
tm = luaT_gettmbyobj(L, p2, event); /* try second operand */
|
tm = luaT_gettmbyobj(L, p2, event); /* try second operand */
|
||||||
if (notm(tm))
|
if (ttisnil(tm)) return 0;
|
||||||
return -1; /* tag method not found */
|
luaT_callTM(L, tm, p1, p2, res, 1);
|
||||||
else /* call tag method and return the tag of the result */
|
return 1;
|
||||||
return luaT_callTMres(L, tm, p1, p2, res);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
void luaT_trybinTM (lua_State *L, const TValue *p1, const TValue *p2,
|
void luaT_trybinTM (lua_State *L, const TValue *p1, const TValue *p2,
|
||||||
StkId res, TMS event) {
|
StkId res, TMS event) {
|
||||||
if (l_unlikely(callbinTM(L, p1, p2, res, event) < 0)) {
|
if (!luaT_callbinTM(L, p1, p2, res, event)) {
|
||||||
switch (event) {
|
switch (event) {
|
||||||
|
case TM_CONCAT:
|
||||||
|
luaG_concaterror(L, p1, p2);
|
||||||
|
/* call never returns, but to avoid warnings: *//* FALLTHROUGH */
|
||||||
case TM_BAND: case TM_BOR: case TM_BXOR:
|
case TM_BAND: case TM_BOR: case TM_BXOR:
|
||||||
case TM_SHL: case TM_SHR: case TM_BNOT: {
|
case TM_SHL: case TM_SHR: case TM_BNOT: {
|
||||||
if (ttisnumber(p1) && ttisnumber(p2))
|
lua_Number dummy;
|
||||||
|
if (tonumber(p1, &dummy) && tonumber(p2, &dummy))
|
||||||
luaG_tointerror(L, p1, p2);
|
luaG_tointerror(L, p1, p2);
|
||||||
else
|
else
|
||||||
luaG_opinterror(L, p1, p2, "perform bitwise operation on");
|
luaG_opinterror(L, p1, p2, "perform bitwise operation on");
|
||||||
@@ -166,199 +155,11 @@ void luaT_trybinTM (lua_State *L, const TValue *p1, const TValue *p2,
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/*
|
|
||||||
** The use of 'p1' after 'callbinTM' is safe because, when a tag
|
|
||||||
** method is not found, 'callbinTM' cannot change the stack.
|
|
||||||
*/
|
|
||||||
void luaT_tryconcatTM (lua_State *L) {
|
|
||||||
StkId p1 = L->top.p - 2; /* first argument */
|
|
||||||
if (l_unlikely(callbinTM(L, s2v(p1), s2v(p1 + 1), p1, TM_CONCAT) < 0))
|
|
||||||
luaG_concaterror(L, s2v(p1), s2v(p1 + 1));
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
void luaT_trybinassocTM (lua_State *L, const TValue *p1, const TValue *p2,
|
|
||||||
int flip, StkId res, TMS event) {
|
|
||||||
if (flip)
|
|
||||||
luaT_trybinTM(L, p2, p1, res, event);
|
|
||||||
else
|
|
||||||
luaT_trybinTM(L, p1, p2, res, event);
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
void luaT_trybiniTM (lua_State *L, const TValue *p1, lua_Integer i2,
|
|
||||||
int flip, StkId res, TMS event) {
|
|
||||||
TValue aux;
|
|
||||||
setivalue(&aux, i2);
|
|
||||||
luaT_trybinassocTM(L, p1, &aux, flip, res, event);
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
/*
|
|
||||||
** Calls an order tag method.
|
|
||||||
*/
|
|
||||||
int luaT_callorderTM (lua_State *L, const TValue *p1, const TValue *p2,
|
int luaT_callorderTM (lua_State *L, const TValue *p1, const TValue *p2,
|
||||||
TMS event) {
|
TMS event) {
|
||||||
int tag = callbinTM(L, p1, p2, L->top.p, event); /* try original event */
|
if (!luaT_callbinTM(L, p1, p2, L->top, event))
|
||||||
if (tag >= 0) /* found tag method? */
|
return -1; /* no metamethod */
|
||||||
return !tagisfalse(tag);
|
|
||||||
luaG_ordererror(L, p1, p2); /* no metamethod found */
|
|
||||||
return 0; /* to avoid warnings */
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
int luaT_callorderiTM (lua_State *L, const TValue *p1, int v2,
|
|
||||||
int flip, int isfloat, TMS event) {
|
|
||||||
TValue aux; const TValue *p2;
|
|
||||||
if (isfloat) {
|
|
||||||
setfltvalue(&aux, cast_num(v2));
|
|
||||||
}
|
|
||||||
else
|
else
|
||||||
setivalue(&aux, v2);
|
return !l_isfalse(L->top);
|
||||||
if (flip) { /* arguments were exchanged? */
|
|
||||||
p2 = p1; p1 = &aux; /* correct them */
|
|
||||||
}
|
|
||||||
else
|
|
||||||
p2 = &aux;
|
|
||||||
return luaT_callorderTM(L, p1, p2, event);
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
/*
|
|
||||||
** Create a vararg table at the top of the stack, with 'n' elements
|
|
||||||
** starting at 'f'.
|
|
||||||
*/
|
|
||||||
static void createvarargtab (lua_State *L, StkId f, int n) {
|
|
||||||
int i;
|
|
||||||
TValue key, value;
|
|
||||||
Table *t = luaH_new(L);
|
|
||||||
sethvalue(L, s2v(L->top.p), t);
|
|
||||||
L->top.p++;
|
|
||||||
luaH_resize(L, t, cast_uint(n), 1);
|
|
||||||
setsvalue(L, &key, luaS_new(L, "n")); /* key is "n" */
|
|
||||||
setivalue(&value, n); /* value is n */
|
|
||||||
/* No need to anchor the key: Due to the resize, the next operation
|
|
||||||
cannot trigger a garbage collection */
|
|
||||||
luaH_set(L, t, &key, &value); /* t.n = n */
|
|
||||||
for (i = 0; i < n; i++)
|
|
||||||
luaH_setint(L, t, i + 1, s2v(f + i));
|
|
||||||
luaC_checkGC(L);
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
/*
|
|
||||||
** initial stack: func arg1 ... argn extra1 ...
|
|
||||||
** ^ ci->func ^ L->top
|
|
||||||
** final stack: func nil ... nil extra1 ... func arg1 ... argn
|
|
||||||
** ^ ci->func
|
|
||||||
*/
|
|
||||||
static void buildhiddenargs (lua_State *L, CallInfo *ci, const Proto *p,
|
|
||||||
int totalargs, int nfixparams, int nextra) {
|
|
||||||
int i;
|
|
||||||
ci->u.l.nextraargs = nextra;
|
|
||||||
luaD_checkstack(L, p->maxstacksize + 1);
|
|
||||||
/* copy function to the top of the stack, after extra arguments */
|
|
||||||
setobjs2s(L, L->top.p++, ci->func.p);
|
|
||||||
/* move fixed parameters to after the copied function */
|
|
||||||
for (i = 1; i <= nfixparams; i++) {
|
|
||||||
setobjs2s(L, L->top.p++, ci->func.p + i);
|
|
||||||
setnilvalue(s2v(ci->func.p + i)); /* erase original parameter (for GC) */
|
|
||||||
}
|
|
||||||
ci->func.p += totalargs + 1; /* 'func' now lives after hidden arguments */
|
|
||||||
ci->top.p += totalargs + 1;
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
void luaT_adjustvarargs (lua_State *L, CallInfo *ci, const Proto *p) {
|
|
||||||
int totalargs = cast_int(L->top.p - ci->func.p) - 1;
|
|
||||||
int nfixparams = p->numparams;
|
|
||||||
int nextra = totalargs - nfixparams; /* number of extra arguments */
|
|
||||||
if (p->flag & PF_VATAB) { /* does it need a vararg table? */
|
|
||||||
lua_assert(!(p->flag & PF_VAHID));
|
|
||||||
createvarargtab(L, ci->func.p + nfixparams + 1, nextra);
|
|
||||||
/* move table to proper place (last parameter) */
|
|
||||||
setobjs2s(L, ci->func.p + nfixparams + 1, L->top.p - 1);
|
|
||||||
}
|
|
||||||
else { /* no table */
|
|
||||||
lua_assert(p->flag & PF_VAHID);
|
|
||||||
buildhiddenargs(L, ci, p, totalargs, nfixparams, nextra);
|
|
||||||
/* set vararg parameter to nil */
|
|
||||||
setnilvalue(s2v(ci->func.p + nfixparams + 1));
|
|
||||||
lua_assert(L->top.p <= ci->top.p && ci->top.p <= L->stack_last.p);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
void luaT_getvararg (CallInfo *ci, StkId ra, TValue *rc) {
|
|
||||||
int nextra = ci->u.l.nextraargs;
|
|
||||||
lua_Integer n;
|
|
||||||
if (tointegerns(rc, &n)) { /* integral value? */
|
|
||||||
if (l_castS2U(n) - 1 < cast_uint(nextra)) {
|
|
||||||
StkId slot = ci->func.p - nextra + cast_int(n) - 1;
|
|
||||||
setobjs2s(((lua_State*)NULL), ra, slot);
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
else if (ttisstring(rc)) { /* string value? */
|
|
||||||
size_t len;
|
|
||||||
const char *s = getlstr(tsvalue(rc), len);
|
|
||||||
if (len == 1 && s[0] == 'n') { /* key is "n"? */
|
|
||||||
setivalue(s2v(ra), nextra);
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
setnilvalue(s2v(ra)); /* else produce nil */
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
/*
|
|
||||||
** Get the number of extra arguments in a vararg function. If vararg
|
|
||||||
** table has been optimized away, that number is in the call info.
|
|
||||||
** Otherwise, get the field 'n' from the vararg table and check that it
|
|
||||||
** has a proper value (non-negative integer not larger than the stack
|
|
||||||
** limit).
|
|
||||||
*/
|
|
||||||
static int getnumargs (lua_State *L, CallInfo *ci, Table *h) {
|
|
||||||
if (h == NULL) /* no vararg table? */
|
|
||||||
return ci->u.l.nextraargs;
|
|
||||||
else {
|
|
||||||
TValue res;
|
|
||||||
if (luaH_getshortstr(h, luaS_new(L, "n"), &res) != LUA_VNUMINT ||
|
|
||||||
l_castS2U(ivalue(&res)) > cast_uint(INT_MAX/2))
|
|
||||||
luaG_runerror(L, "vararg table has no proper 'n'");
|
|
||||||
return cast_int(ivalue(&res));
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
/*
|
|
||||||
** Get 'wanted' vararg arguments and put them in 'where'. 'vatab' is
|
|
||||||
** the register of the vararg table or -1 if there is no vararg table.
|
|
||||||
*/
|
|
||||||
void luaT_getvarargs (lua_State *L, CallInfo *ci, StkId where, int wanted,
|
|
||||||
int vatab) {
|
|
||||||
Table *h = (vatab < 0) ? NULL : hvalue(s2v(ci->func.p + vatab + 1));
|
|
||||||
int nargs = getnumargs(L, ci, h); /* number of available vararg args. */
|
|
||||||
int i, touse; /* 'touse' is minimum between 'wanted' and 'nargs' */
|
|
||||||
if (wanted < 0) {
|
|
||||||
touse = wanted = nargs; /* get all extra arguments available */
|
|
||||||
checkstackp(L, nargs, where); /* ensure stack space */
|
|
||||||
L->top.p = where + nargs; /* next instruction will need top */
|
|
||||||
}
|
|
||||||
else
|
|
||||||
touse = (nargs > wanted) ? wanted : nargs;
|
|
||||||
if (h == NULL) { /* no vararg table? */
|
|
||||||
for (i = 0; i < touse; i++) /* get vararg values from the stack */
|
|
||||||
setobjs2s(L, where + i, ci->func.p - nargs + i);
|
|
||||||
}
|
|
||||||
else { /* get vararg values from vararg table */
|
|
||||||
for (i = 0; i < touse; i++) {
|
|
||||||
lu_byte tag = luaH_getint(h, i + 1, s2v(where + i));
|
|
||||||
if (tagisempty(tag))
|
|
||||||
setnilvalue(s2v(where + i));
|
|
||||||
}
|
|
||||||
}
|
|
||||||
for (; i < wanted; i++) /* complete required results with nil */
|
|
||||||
setnilvalue(s2v(where + i));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -1,5 +1,5 @@
|
|||||||
/*
|
/*
|
||||||
** $Id: ltm.h $
|
** $Id: ltm.h,v 2.22.1.1 2017/04/19 17:20:42 roberto Exp $
|
||||||
** Tag methods
|
** Tag methods
|
||||||
** See Copyright Notice in lua.h
|
** See Copyright Notice in lua.h
|
||||||
*/
|
*/
|
||||||
@@ -40,36 +40,19 @@ typedef enum {
|
|||||||
TM_LE,
|
TM_LE,
|
||||||
TM_CONCAT,
|
TM_CONCAT,
|
||||||
TM_CALL,
|
TM_CALL,
|
||||||
TM_CLOSE,
|
|
||||||
TM_N /* number of elements in the enum */
|
TM_N /* number of elements in the enum */
|
||||||
} TMS;
|
} TMS;
|
||||||
|
|
||||||
|
|
||||||
/*
|
|
||||||
** Mask with 1 in all fast-access methods. A 1 in any of these bits
|
|
||||||
** in the flag of a (meta)table means the metatable does not have the
|
|
||||||
** corresponding metamethod field. (Bit 6 of the flag indicates that
|
|
||||||
** the table is using the dummy node; bit 7 is used for 'isrealasize'.)
|
|
||||||
*/
|
|
||||||
#define maskflags cast_byte(~(~0u << (TM_EQ + 1)))
|
|
||||||
|
|
||||||
|
#define gfasttm(g,et,e) ((et) == NULL ? NULL : \
|
||||||
|
((et)->flags & (1u<<(e))) ? NULL : luaT_gettm(et, e, (g)->tmname[e]))
|
||||||
|
|
||||||
/*
|
#define fasttm(l,et,e) gfasttm(G(l), et, e)
|
||||||
** Test whether there is no tagmethod.
|
|
||||||
** (Because tagmethods use raw accesses, the result may be an "empty" nil.)
|
|
||||||
*/
|
|
||||||
#define notm(tm) ttisnil(tm)
|
|
||||||
|
|
||||||
#define checknoTM(mt,e) ((mt) == NULL || (mt)->flags & (1u<<(e)))
|
|
||||||
|
|
||||||
#define gfasttm(g,mt,e) \
|
|
||||||
(checknoTM(mt, e) ? NULL : luaT_gettm(mt, e, (g)->tmname[e]))
|
|
||||||
|
|
||||||
#define fasttm(l,mt,e) gfasttm(G(l), mt, e)
|
|
||||||
|
|
||||||
#define ttypename(x) luaT_typenames_[(x) + 1]
|
#define ttypename(x) luaT_typenames_[(x) + 1]
|
||||||
|
|
||||||
LUAI_DDEC(const char *const luaT_typenames_[LUA_TOTALTYPES];)
|
LUAI_DDEC const char *const luaT_typenames_[LUA_TOTALTAGS];
|
||||||
|
|
||||||
|
|
||||||
LUAI_FUNC const char *luaT_objtypename (lua_State *L, const TValue *o);
|
LUAI_FUNC const char *luaT_objtypename (lua_State *L, const TValue *o);
|
||||||
@@ -80,26 +63,14 @@ LUAI_FUNC const TValue *luaT_gettmbyobj (lua_State *L, const TValue *o,
|
|||||||
LUAI_FUNC void luaT_init (lua_State *L);
|
LUAI_FUNC void luaT_init (lua_State *L);
|
||||||
|
|
||||||
LUAI_FUNC void luaT_callTM (lua_State *L, const TValue *f, const TValue *p1,
|
LUAI_FUNC void luaT_callTM (lua_State *L, const TValue *f, const TValue *p1,
|
||||||
const TValue *p2, const TValue *p3);
|
const TValue *p2, TValue *p3, int hasres);
|
||||||
LUAI_FUNC lu_byte luaT_callTMres (lua_State *L, const TValue *f,
|
LUAI_FUNC int luaT_callbinTM (lua_State *L, const TValue *p1, const TValue *p2,
|
||||||
const TValue *p1, const TValue *p2, StkId p3);
|
StkId res, TMS event);
|
||||||
LUAI_FUNC void luaT_trybinTM (lua_State *L, const TValue *p1, const TValue *p2,
|
LUAI_FUNC void luaT_trybinTM (lua_State *L, const TValue *p1, const TValue *p2,
|
||||||
StkId res, TMS event);
|
StkId res, TMS event);
|
||||||
LUAI_FUNC void luaT_tryconcatTM (lua_State *L);
|
|
||||||
LUAI_FUNC void luaT_trybinassocTM (lua_State *L, const TValue *p1,
|
|
||||||
const TValue *p2, int inv, StkId res, TMS event);
|
|
||||||
LUAI_FUNC void luaT_trybiniTM (lua_State *L, const TValue *p1, lua_Integer i2,
|
|
||||||
int inv, StkId res, TMS event);
|
|
||||||
LUAI_FUNC int luaT_callorderTM (lua_State *L, const TValue *p1,
|
LUAI_FUNC int luaT_callorderTM (lua_State *L, const TValue *p1,
|
||||||
const TValue *p2, TMS event);
|
const TValue *p2, TMS event);
|
||||||
LUAI_FUNC int luaT_callorderiTM (lua_State *L, const TValue *p1, int v2,
|
|
||||||
int inv, int isfloat, TMS event);
|
|
||||||
|
|
||||||
LUAI_FUNC void luaT_adjustvarargs (lua_State *L, struct CallInfo *ci,
|
|
||||||
const Proto *p);
|
|
||||||
LUAI_FUNC void luaT_getvararg (CallInfo *ci, StkId ra, TValue *rc);
|
|
||||||
LUAI_FUNC void luaT_getvarargs (lua_State *L, struct CallInfo *ci, StkId where,
|
|
||||||
int wanted, int vatab);
|
|
||||||
|
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
|||||||
@@ -1,5 +1,5 @@
|
|||||||
/*
|
/*
|
||||||
** $Id: lua.c $
|
** $Id: lua.c,v 1.230.1.1 2017/04/19 17:29:57 roberto Exp $
|
||||||
** Lua stand-alone interpreter
|
** Lua stand-alone interpreter
|
||||||
** See Copyright Notice in lua.h
|
** See Copyright Notice in lua.h
|
||||||
*/
|
*/
|
||||||
@@ -9,23 +9,31 @@
|
|||||||
#include "lprefix.h"
|
#include "lprefix.h"
|
||||||
|
|
||||||
|
|
||||||
|
#include <signal.h>
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
|
|
||||||
#include <signal.h>
|
|
||||||
|
|
||||||
#include "lua.h"
|
#include "lua.h"
|
||||||
|
|
||||||
#include "lauxlib.h"
|
#include "lauxlib.h"
|
||||||
#include "lualib.h"
|
#include "lualib.h"
|
||||||
#include "llimits.h"
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
#if !defined(LUA_PROMPT)
|
||||||
|
#define LUA_PROMPT "> "
|
||||||
|
#define LUA_PROMPT2 ">> "
|
||||||
|
#endif
|
||||||
|
|
||||||
#if !defined(LUA_PROGNAME)
|
#if !defined(LUA_PROGNAME)
|
||||||
#define LUA_PROGNAME "lua"
|
#define LUA_PROGNAME "lua"
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
#if !defined(LUA_MAXINPUT)
|
||||||
|
#define LUA_MAXINPUT 512
|
||||||
|
#endif
|
||||||
|
|
||||||
#if !defined(LUA_INIT_VAR)
|
#if !defined(LUA_INIT_VAR)
|
||||||
#define LUA_INIT_VAR "LUA_INIT"
|
#define LUA_INIT_VAR "LUA_INIT"
|
||||||
#endif
|
#endif
|
||||||
@@ -33,31 +41,70 @@
|
|||||||
#define LUA_INITVARVERSION LUA_INIT_VAR LUA_VERSUFFIX
|
#define LUA_INITVARVERSION LUA_INIT_VAR LUA_VERSUFFIX
|
||||||
|
|
||||||
|
|
||||||
|
/*
|
||||||
|
** lua_stdin_is_tty detects whether the standard input is a 'tty' (that
|
||||||
|
** is, whether we're running lua interactively).
|
||||||
|
*/
|
||||||
|
#if !defined(lua_stdin_is_tty) /* { */
|
||||||
|
|
||||||
|
#if defined(LUA_USE_POSIX) /* { */
|
||||||
|
|
||||||
|
#include <unistd.h>
|
||||||
|
#define lua_stdin_is_tty() isatty(0)
|
||||||
|
|
||||||
|
#elif defined(LUA_USE_WINDOWS) /* }{ */
|
||||||
|
|
||||||
|
#include <io.h>
|
||||||
|
#include <windows.h>
|
||||||
|
|
||||||
|
#define lua_stdin_is_tty() _isatty(_fileno(stdin))
|
||||||
|
|
||||||
|
#else /* }{ */
|
||||||
|
|
||||||
|
/* ISO C definition */
|
||||||
|
#define lua_stdin_is_tty() 1 /* assume stdin is a tty */
|
||||||
|
|
||||||
|
#endif /* } */
|
||||||
|
|
||||||
|
#endif /* } */
|
||||||
|
|
||||||
|
|
||||||
|
/*
|
||||||
|
** lua_readline defines how to show a prompt and then read a line from
|
||||||
|
** the standard input.
|
||||||
|
** lua_saveline defines how to "save" a read line in a "history".
|
||||||
|
** lua_freeline defines how to free a line read by lua_readline.
|
||||||
|
*/
|
||||||
|
#if !defined(lua_readline) /* { */
|
||||||
|
|
||||||
|
#if defined(LUA_USE_READLINE) /* { */
|
||||||
|
|
||||||
|
#include <readline/readline.h>
|
||||||
|
#include <readline/history.h>
|
||||||
|
#define lua_readline(L,b,p) ((void)L, ((b)=readline(p)) != NULL)
|
||||||
|
#define lua_saveline(L,line) ((void)L, add_history(line))
|
||||||
|
#define lua_freeline(L,b) ((void)L, free(b))
|
||||||
|
|
||||||
|
#else /* }{ */
|
||||||
|
|
||||||
|
#define lua_readline(L,b,p) \
|
||||||
|
((void)L, fputs(p, stdout), fflush(stdout), /* show prompt */ \
|
||||||
|
fgets(b, LUA_MAXINPUT, stdin) != NULL) /* get line */
|
||||||
|
#define lua_saveline(L,line) { (void)L; (void)line; }
|
||||||
|
#define lua_freeline(L,b) { (void)L; (void)b; }
|
||||||
|
|
||||||
|
#endif /* } */
|
||||||
|
|
||||||
|
#endif /* } */
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
static lua_State *globalL = NULL;
|
static lua_State *globalL = NULL;
|
||||||
|
|
||||||
static const char *progname = LUA_PROGNAME;
|
static const char *progname = LUA_PROGNAME;
|
||||||
|
|
||||||
|
|
||||||
#if defined(LUA_USE_POSIX) /* { */
|
|
||||||
|
|
||||||
/*
|
|
||||||
** Use 'sigaction' when available.
|
|
||||||
*/
|
|
||||||
static void setsignal (int sig, void (*handler)(int)) {
|
|
||||||
struct sigaction sa;
|
|
||||||
sa.sa_handler = handler;
|
|
||||||
sa.sa_flags = 0;
|
|
||||||
sigemptyset(&sa.sa_mask); /* do not mask any signal */
|
|
||||||
sigaction(sig, &sa, NULL);
|
|
||||||
}
|
|
||||||
|
|
||||||
#else /* }{ */
|
|
||||||
|
|
||||||
#define setsignal signal
|
|
||||||
|
|
||||||
#endif /* } */
|
|
||||||
|
|
||||||
|
|
||||||
/*
|
/*
|
||||||
** Hook set by signal function to stop the interpreter.
|
** Hook set by signal function to stop the interpreter.
|
||||||
*/
|
*/
|
||||||
@@ -75,9 +122,8 @@ static void lstop (lua_State *L, lua_Debug *ar) {
|
|||||||
** interpreter.
|
** interpreter.
|
||||||
*/
|
*/
|
||||||
static void laction (int i) {
|
static void laction (int i) {
|
||||||
int flag = LUA_MASKCALL | LUA_MASKRET | LUA_MASKLINE | LUA_MASKCOUNT;
|
signal(i, SIG_DFL); /* if another SIGINT happens, terminate process */
|
||||||
setsignal(i, SIG_DFL); /* if another SIGINT happens, terminate process */
|
lua_sethook(globalL, lstop, LUA_MASKCALL | LUA_MASKRET | LUA_MASKCOUNT, 1);
|
||||||
lua_sethook(globalL, lstop, flag, 1);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@@ -90,15 +136,13 @@ static void print_usage (const char *badoption) {
|
|||||||
lua_writestringerror(
|
lua_writestringerror(
|
||||||
"usage: %s [options] [script [args]]\n"
|
"usage: %s [options] [script [args]]\n"
|
||||||
"Available options are:\n"
|
"Available options are:\n"
|
||||||
" -e stat execute string 'stat'\n"
|
" -e stat execute string 'stat'\n"
|
||||||
" -i enter interactive mode after executing 'script'\n"
|
" -i enter interactive mode after executing 'script'\n"
|
||||||
" -l mod require library 'mod' into global 'mod'\n"
|
" -l name require library 'name' into global 'name'\n"
|
||||||
" -l g=mod require library 'mod' into global 'g'\n"
|
" -v show version information\n"
|
||||||
" -v show version information\n"
|
" -E ignore environment variables\n"
|
||||||
" -E ignore environment variables\n"
|
" -- stop handling options\n"
|
||||||
" -W turn warnings on\n"
|
" - stop handling options and execute stdin\n"
|
||||||
" -- stop handling options\n"
|
|
||||||
" - stop handling options and execute stdin\n"
|
|
||||||
,
|
,
|
||||||
progname);
|
progname);
|
||||||
}
|
}
|
||||||
@@ -116,13 +160,12 @@ static void l_message (const char *pname, const char *msg) {
|
|||||||
|
|
||||||
/*
|
/*
|
||||||
** Check whether 'status' is not OK and, if so, prints the error
|
** Check whether 'status' is not OK and, if so, prints the error
|
||||||
** message on the top of the stack.
|
** message on the top of the stack. It assumes that the error object
|
||||||
|
** is a string, as it was either generated by Lua or by 'msghandler'.
|
||||||
*/
|
*/
|
||||||
static int report (lua_State *L, int status) {
|
static int report (lua_State *L, int status) {
|
||||||
if (status != LUA_OK) {
|
if (status != LUA_OK) {
|
||||||
const char *msg = lua_tostring(L, -1);
|
const char *msg = lua_tostring(L, -1);
|
||||||
if (msg == NULL)
|
|
||||||
msg = "(error message not a string)";
|
|
||||||
l_message(progname, msg);
|
l_message(progname, msg);
|
||||||
lua_pop(L, 1); /* remove message */
|
lua_pop(L, 1); /* remove message */
|
||||||
}
|
}
|
||||||
@@ -158,9 +201,9 @@ static int docall (lua_State *L, int narg, int nres) {
|
|||||||
lua_pushcfunction(L, msghandler); /* push message handler */
|
lua_pushcfunction(L, msghandler); /* push message handler */
|
||||||
lua_insert(L, base); /* put it under function and args */
|
lua_insert(L, base); /* put it under function and args */
|
||||||
globalL = L; /* to be available to 'laction' */
|
globalL = L; /* to be available to 'laction' */
|
||||||
setsignal(SIGINT, laction); /* set C-signal handler */
|
signal(SIGINT, laction); /* set C-signal handler */
|
||||||
status = lua_pcall(L, narg, nres, base);
|
status = lua_pcall(L, narg, nres, base);
|
||||||
setsignal(SIGINT, SIG_DFL); /* reset C-signal handler */
|
signal(SIGINT, SIG_DFL); /* reset C-signal handler */
|
||||||
lua_remove(L, base); /* remove message handler from the stack */
|
lua_remove(L, base); /* remove message handler from the stack */
|
||||||
return status;
|
return status;
|
||||||
}
|
}
|
||||||
@@ -179,11 +222,10 @@ static void print_version (void) {
|
|||||||
** to the script (everything after 'script') go to positive indices;
|
** to the script (everything after 'script') go to positive indices;
|
||||||
** other arguments (before the script name) go to negative indices.
|
** other arguments (before the script name) go to negative indices.
|
||||||
** If there is no script name, assume interpreter's name as base.
|
** If there is no script name, assume interpreter's name as base.
|
||||||
** (If there is no interpreter's name either, 'script' is -1, so
|
|
||||||
** table sizes are zero.)
|
|
||||||
*/
|
*/
|
||||||
static void createargtable (lua_State *L, char **argv, int argc, int script) {
|
static void createargtable (lua_State *L, char **argv, int argc, int script) {
|
||||||
int i, narg;
|
int i, narg;
|
||||||
|
if (script == argc) script = 0; /* no script name? */
|
||||||
narg = argc - (script + 1); /* number of positive indices */
|
narg = argc - (script + 1); /* number of positive indices */
|
||||||
lua_createtable(L, narg, script + 1);
|
lua_createtable(L, narg, script + 1);
|
||||||
for (i = 0; i < argc; i++) {
|
for (i = 0; i < argc; i++) {
|
||||||
@@ -211,34 +253,171 @@ static int dostring (lua_State *L, const char *s, const char *name) {
|
|||||||
|
|
||||||
|
|
||||||
/*
|
/*
|
||||||
** Receives 'globname[=modname]' and runs 'globname = require(modname)'.
|
** Calls 'require(name)' and stores the result in a global variable
|
||||||
** If there is no explicit modname and globname contains a '-', cut
|
** with the given name.
|
||||||
** the suffix after '-' (the "version") to make the global name.
|
|
||||||
*/
|
*/
|
||||||
static int dolibrary (lua_State *L, char *globname) {
|
static int dolibrary (lua_State *L, const char *name) {
|
||||||
int status;
|
int status;
|
||||||
char *suffix = NULL;
|
|
||||||
char *modname = strchr(globname, '=');
|
|
||||||
if (modname == NULL) { /* no explicit name? */
|
|
||||||
modname = globname; /* module name is equal to global name */
|
|
||||||
suffix = strchr(modname, *LUA_IGMARK); /* look for a suffix mark */
|
|
||||||
}
|
|
||||||
else {
|
|
||||||
*modname = '\0'; /* global name ends here */
|
|
||||||
modname++; /* module name starts after the '=' */
|
|
||||||
}
|
|
||||||
lua_getglobal(L, "require");
|
lua_getglobal(L, "require");
|
||||||
lua_pushstring(L, modname);
|
lua_pushstring(L, name);
|
||||||
status = docall(L, 1, 1); /* call 'require(modname)' */
|
status = docall(L, 1, 1); /* call 'require(name)' */
|
||||||
if (status == LUA_OK) {
|
if (status == LUA_OK)
|
||||||
if (suffix != NULL) /* is there a suffix mark? */
|
lua_setglobal(L, name); /* global[name] = require return */
|
||||||
*suffix = '\0'; /* remove suffix from global name */
|
|
||||||
lua_setglobal(L, globname); /* globname = require(modname) */
|
|
||||||
}
|
|
||||||
return report(L, status);
|
return report(L, status);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/*
|
||||||
|
** Returns the string to be used as a prompt by the interpreter.
|
||||||
|
*/
|
||||||
|
static const char *get_prompt (lua_State *L, int firstline) {
|
||||||
|
const char *p;
|
||||||
|
lua_getglobal(L, firstline ? "_PROMPT" : "_PROMPT2");
|
||||||
|
p = lua_tostring(L, -1);
|
||||||
|
if (p == NULL) p = (firstline ? LUA_PROMPT : LUA_PROMPT2);
|
||||||
|
return p;
|
||||||
|
}
|
||||||
|
|
||||||
|
/* mark in error messages for incomplete statements */
|
||||||
|
#define EOFMARK "<eof>"
|
||||||
|
#define marklen (sizeof(EOFMARK)/sizeof(char) - 1)
|
||||||
|
|
||||||
|
|
||||||
|
/*
|
||||||
|
** Check whether 'status' signals a syntax error and the error
|
||||||
|
** message at the top of the stack ends with the above mark for
|
||||||
|
** incomplete statements.
|
||||||
|
*/
|
||||||
|
static int incomplete (lua_State *L, int status) {
|
||||||
|
if (status == LUA_ERRSYNTAX) {
|
||||||
|
size_t lmsg;
|
||||||
|
const char *msg = lua_tolstring(L, -1, &lmsg);
|
||||||
|
if (lmsg >= marklen && strcmp(msg + lmsg - marklen, EOFMARK) == 0) {
|
||||||
|
lua_pop(L, 1);
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return 0; /* else... */
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/*
|
||||||
|
** Prompt the user, read a line, and push it into the Lua stack.
|
||||||
|
*/
|
||||||
|
static int pushline (lua_State *L, int firstline) {
|
||||||
|
char buffer[LUA_MAXINPUT];
|
||||||
|
char *b = buffer;
|
||||||
|
size_t l;
|
||||||
|
const char *prmt = get_prompt(L, firstline);
|
||||||
|
int readstatus = lua_readline(L, b, prmt);
|
||||||
|
if (readstatus == 0)
|
||||||
|
return 0; /* no input (prompt will be popped by caller) */
|
||||||
|
lua_pop(L, 1); /* remove prompt */
|
||||||
|
l = strlen(b);
|
||||||
|
if (l > 0 && b[l-1] == '\n') /* line ends with newline? */
|
||||||
|
b[--l] = '\0'; /* remove it */
|
||||||
|
if (firstline && b[0] == '=') /* for compatibility with 5.2, ... */
|
||||||
|
lua_pushfstring(L, "return %s", b + 1); /* change '=' to 'return' */
|
||||||
|
else
|
||||||
|
lua_pushlstring(L, b, l);
|
||||||
|
lua_freeline(L, b);
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/*
|
||||||
|
** Try to compile line on the stack as 'return <line>;'; on return, stack
|
||||||
|
** has either compiled chunk or original line (if compilation failed).
|
||||||
|
*/
|
||||||
|
static int addreturn (lua_State *L) {
|
||||||
|
const char *line = lua_tostring(L, -1); /* original line */
|
||||||
|
const char *retline = lua_pushfstring(L, "return %s;", line);
|
||||||
|
int status = luaL_loadbuffer(L, retline, strlen(retline), "=stdin");
|
||||||
|
if (status == LUA_OK) {
|
||||||
|
lua_remove(L, -2); /* remove modified line */
|
||||||
|
if (line[0] != '\0') /* non empty? */
|
||||||
|
lua_saveline(L, line); /* keep history */
|
||||||
|
}
|
||||||
|
else
|
||||||
|
lua_pop(L, 2); /* pop result from 'luaL_loadbuffer' and modified line */
|
||||||
|
return status;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/*
|
||||||
|
** Read multiple lines until a complete Lua statement
|
||||||
|
*/
|
||||||
|
static int multiline (lua_State *L) {
|
||||||
|
for (;;) { /* repeat until gets a complete statement */
|
||||||
|
size_t len;
|
||||||
|
const char *line = lua_tolstring(L, 1, &len); /* get what it has */
|
||||||
|
int status = luaL_loadbuffer(L, line, len, "=stdin"); /* try it */
|
||||||
|
if (!incomplete(L, status) || !pushline(L, 0)) {
|
||||||
|
lua_saveline(L, line); /* keep history */
|
||||||
|
return status; /* cannot or should not try to add continuation line */
|
||||||
|
}
|
||||||
|
lua_pushliteral(L, "\n"); /* add newline... */
|
||||||
|
lua_insert(L, -2); /* ...between the two lines */
|
||||||
|
lua_concat(L, 3); /* join them */
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/*
|
||||||
|
** Read a line and try to load (compile) it first as an expression (by
|
||||||
|
** adding "return " in front of it) and second as a statement. Return
|
||||||
|
** the final status of load/call with the resulting function (if any)
|
||||||
|
** in the top of the stack.
|
||||||
|
*/
|
||||||
|
static int loadline (lua_State *L) {
|
||||||
|
int status;
|
||||||
|
lua_settop(L, 0);
|
||||||
|
if (!pushline(L, 1))
|
||||||
|
return -1; /* no input */
|
||||||
|
if ((status = addreturn(L)) != LUA_OK) /* 'return ...' did not work? */
|
||||||
|
status = multiline(L); /* try as command, maybe with continuation lines */
|
||||||
|
lua_remove(L, 1); /* remove line from the stack */
|
||||||
|
lua_assert(lua_gettop(L) == 1);
|
||||||
|
return status;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/*
|
||||||
|
** Prints (calling the Lua 'print' function) any values on the stack
|
||||||
|
*/
|
||||||
|
static void l_print (lua_State *L) {
|
||||||
|
int n = lua_gettop(L);
|
||||||
|
if (n > 0) { /* any result to be printed? */
|
||||||
|
luaL_checkstack(L, LUA_MINSTACK, "too many results to print");
|
||||||
|
lua_getglobal(L, "print");
|
||||||
|
lua_insert(L, 1);
|
||||||
|
if (lua_pcall(L, n, 0, 0) != LUA_OK)
|
||||||
|
l_message(progname, lua_pushfstring(L, "error calling 'print' (%s)",
|
||||||
|
lua_tostring(L, -1)));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/*
|
||||||
|
** Do the REPL: repeatedly read (load) a line, evaluate (call) it, and
|
||||||
|
** print any results.
|
||||||
|
*/
|
||||||
|
static void doREPL (lua_State *L) {
|
||||||
|
int status;
|
||||||
|
const char *oldprogname = progname;
|
||||||
|
progname = NULL; /* no 'progname' on errors in interactive mode */
|
||||||
|
while ((status = loadline(L)) != -1) {
|
||||||
|
if (status == LUA_OK)
|
||||||
|
status = docall(L, 0, LUA_MULTRET);
|
||||||
|
if (status == LUA_OK) l_print(L);
|
||||||
|
else report(L, status);
|
||||||
|
}
|
||||||
|
lua_settop(L, 0); /* clear stack */
|
||||||
|
lua_writeline();
|
||||||
|
progname = oldprogname;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
/*
|
/*
|
||||||
** Push on the stack the contents of table 'arg' from 1 to #arg
|
** Push on the stack the contents of table 'arg' from 1 to #arg
|
||||||
*/
|
*/
|
||||||
@@ -269,6 +448,7 @@ static int handle_script (lua_State *L, char **argv) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
/* bits of various argument indicators in 'args' */
|
/* bits of various argument indicators in 'args' */
|
||||||
#define has_error 1 /* bad option */
|
#define has_error 1 /* bad option */
|
||||||
#define has_i 2 /* -i */
|
#define has_i 2 /* -i */
|
||||||
@@ -276,26 +456,16 @@ static int handle_script (lua_State *L, char **argv) {
|
|||||||
#define has_e 8 /* -e */
|
#define has_e 8 /* -e */
|
||||||
#define has_E 16 /* -E */
|
#define has_E 16 /* -E */
|
||||||
|
|
||||||
|
|
||||||
/*
|
/*
|
||||||
** Traverses all arguments from 'argv', returning a mask with those
|
** Traverses all arguments from 'argv', returning a mask with those
|
||||||
** needed before running any Lua code or an error code if it finds any
|
** needed before running any Lua code (or an error code if it finds
|
||||||
** invalid argument. In case of error, 'first' is the index of the bad
|
** any invalid argument). 'first' returns the first not-handled argument
|
||||||
** argument. Otherwise, 'first' is -1 if there is no program name,
|
** (either the script name or a bad argument in case of error).
|
||||||
** 0 if there is no script name, or the index of the script name.
|
|
||||||
*/
|
*/
|
||||||
static int collectargs (char **argv, int *first) {
|
static int collectargs (char **argv, int *first) {
|
||||||
int args = 0;
|
int args = 0;
|
||||||
int i;
|
int i;
|
||||||
if (argv[0] != NULL) { /* is there a program name? */
|
for (i = 1; argv[i] != NULL; i++) {
|
||||||
if (argv[0][0]) /* not empty? */
|
|
||||||
progname = argv[0]; /* save it */
|
|
||||||
}
|
|
||||||
else { /* no program name */
|
|
||||||
*first = -1;
|
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
for (i = 1; argv[i] != NULL; i++) { /* handle arguments */
|
|
||||||
*first = i;
|
*first = i;
|
||||||
if (argv[i][0] != '-') /* not an option? */
|
if (argv[i][0] != '-') /* not an option? */
|
||||||
return args; /* stop handling options */
|
return args; /* stop handling options */
|
||||||
@@ -303,24 +473,19 @@ static int collectargs (char **argv, int *first) {
|
|||||||
case '-': /* '--' */
|
case '-': /* '--' */
|
||||||
if (argv[i][2] != '\0') /* extra characters after '--'? */
|
if (argv[i][2] != '\0') /* extra characters after '--'? */
|
||||||
return has_error; /* invalid option */
|
return has_error; /* invalid option */
|
||||||
/* if there is a script name, it comes after '--' */
|
*first = i + 1;
|
||||||
*first = (argv[i + 1] != NULL) ? i + 1 : 0;
|
|
||||||
return args;
|
return args;
|
||||||
case '\0': /* '-' */
|
case '\0': /* '-' */
|
||||||
return args; /* script "name" is '-' */
|
return args; /* script "name" is '-' */
|
||||||
case 'E':
|
case 'E':
|
||||||
if (argv[i][2] != '\0') /* extra characters? */
|
if (argv[i][2] != '\0') /* extra characters after 1st? */
|
||||||
return has_error; /* invalid option */
|
return has_error; /* invalid option */
|
||||||
args |= has_E;
|
args |= has_E;
|
||||||
break;
|
break;
|
||||||
case 'W':
|
|
||||||
if (argv[i][2] != '\0') /* extra characters? */
|
|
||||||
return has_error; /* invalid option */
|
|
||||||
break;
|
|
||||||
case 'i':
|
case 'i':
|
||||||
args |= has_i; /* (-i implies -v) *//* FALLTHROUGH */
|
args |= has_i; /* (-i implies -v) *//* FALLTHROUGH */
|
||||||
case 'v':
|
case 'v':
|
||||||
if (argv[i][2] != '\0') /* extra characters? */
|
if (argv[i][2] != '\0') /* extra characters after 1st? */
|
||||||
return has_error; /* invalid option */
|
return has_error; /* invalid option */
|
||||||
args |= has_v;
|
args |= has_v;
|
||||||
break;
|
break;
|
||||||
@@ -337,14 +502,13 @@ static int collectargs (char **argv, int *first) {
|
|||||||
return has_error;
|
return has_error;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
*first = 0; /* no script name */
|
*first = i; /* no script name */
|
||||||
return args;
|
return args;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/*
|
/*
|
||||||
** Processes options 'e' and 'l', which involve running Lua code, and
|
** Processes options 'e' and 'l', which involve running Lua code.
|
||||||
** 'W', which also affects the state.
|
|
||||||
** Returns 0 if some code raises an error.
|
** Returns 0 if some code raises an error.
|
||||||
*/
|
*/
|
||||||
static int runargs (lua_State *L, char **argv, int n) {
|
static int runargs (lua_State *L, char **argv, int n) {
|
||||||
@@ -352,27 +516,22 @@ static int runargs (lua_State *L, char **argv, int n) {
|
|||||||
for (i = 1; i < n; i++) {
|
for (i = 1; i < n; i++) {
|
||||||
int option = argv[i][1];
|
int option = argv[i][1];
|
||||||
lua_assert(argv[i][0] == '-'); /* already checked */
|
lua_assert(argv[i][0] == '-'); /* already checked */
|
||||||
switch (option) {
|
if (option == 'e' || option == 'l') {
|
||||||
case 'e': case 'l': {
|
int status;
|
||||||
int status;
|
const char *extra = argv[i] + 2; /* both options need an argument */
|
||||||
char *extra = argv[i] + 2; /* both options need an argument */
|
if (*extra == '\0') extra = argv[++i];
|
||||||
if (*extra == '\0') extra = argv[++i];
|
lua_assert(extra != NULL);
|
||||||
lua_assert(extra != NULL);
|
status = (option == 'e')
|
||||||
status = (option == 'e')
|
? dostring(L, extra, "=(command line)")
|
||||||
? dostring(L, extra, "=(command line)")
|
: dolibrary(L, extra);
|
||||||
: dolibrary(L, extra);
|
if (status != LUA_OK) return 0;
|
||||||
if (status != LUA_OK) return 0;
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
case 'W':
|
|
||||||
lua_warning(L, "@on", 0); /* warnings on */
|
|
||||||
break;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
static int handle_luainit (lua_State *L) {
|
static int handle_luainit (lua_State *L) {
|
||||||
const char *name = "=" LUA_INITVARVERSION;
|
const char *name = "=" LUA_INITVARVERSION;
|
||||||
const char *init = getenv(name + 1);
|
const char *init = getenv(name + 1);
|
||||||
@@ -388,314 +547,6 @@ static int handle_luainit (lua_State *L) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/*
|
|
||||||
** {==================================================================
|
|
||||||
** Read-Eval-Print Loop (REPL)
|
|
||||||
** ===================================================================
|
|
||||||
*/
|
|
||||||
|
|
||||||
#if !defined(LUA_PROMPT)
|
|
||||||
#define LUA_PROMPT "> "
|
|
||||||
#define LUA_PROMPT2 ">> "
|
|
||||||
#endif
|
|
||||||
|
|
||||||
#if !defined(LUA_MAXINPUT)
|
|
||||||
#define LUA_MAXINPUT 512
|
|
||||||
#endif
|
|
||||||
|
|
||||||
|
|
||||||
/*
|
|
||||||
** lua_stdin_is_tty detects whether the standard input is a 'tty' (that
|
|
||||||
** is, whether we're running lua interactively).
|
|
||||||
*/
|
|
||||||
#if !defined(lua_stdin_is_tty) /* { */
|
|
||||||
|
|
||||||
#if defined(LUA_USE_POSIX) /* { */
|
|
||||||
|
|
||||||
#include <unistd.h>
|
|
||||||
#define lua_stdin_is_tty() isatty(0)
|
|
||||||
|
|
||||||
#elif defined(LUA_USE_WINDOWS) /* }{ */
|
|
||||||
|
|
||||||
#include <io.h>
|
|
||||||
#include <windows.h>
|
|
||||||
|
|
||||||
#define lua_stdin_is_tty() _isatty(_fileno(stdin))
|
|
||||||
|
|
||||||
#else /* }{ */
|
|
||||||
|
|
||||||
/* ISO C definition */
|
|
||||||
#define lua_stdin_is_tty() 1 /* assume stdin is a tty */
|
|
||||||
|
|
||||||
#endif /* } */
|
|
||||||
|
|
||||||
#endif /* } */
|
|
||||||
|
|
||||||
|
|
||||||
/*
|
|
||||||
** * lua_initreadline initializes the readline system.
|
|
||||||
** * lua_readline defines how to show a prompt and then read a line from
|
|
||||||
** the standard input.
|
|
||||||
** * lua_saveline defines how to "save" a read line in a "history".
|
|
||||||
** * lua_freeline defines how to free a line read by lua_readline.
|
|
||||||
*/
|
|
||||||
|
|
||||||
#if !defined(lua_readline) /* { */
|
|
||||||
/* Otherwise, all previously listed functions should be defined. */
|
|
||||||
|
|
||||||
#if defined(LUA_USE_READLINE) /* { */
|
|
||||||
/* Lua will be linked with '-lreadline' */
|
|
||||||
|
|
||||||
#include <readline/readline.h>
|
|
||||||
#include <readline/history.h>
|
|
||||||
|
|
||||||
#define lua_initreadline(L) ((void)L, rl_readline_name="lua")
|
|
||||||
#define lua_readline(buff,prompt) ((void)buff, readline(prompt))
|
|
||||||
#define lua_saveline(line) add_history(line)
|
|
||||||
#define lua_freeline(line) free(line)
|
|
||||||
|
|
||||||
#else /* }{ */
|
|
||||||
/* use dynamically loaded readline (or nothing) */
|
|
||||||
|
|
||||||
/* pointer to 'readline' function (if any) */
|
|
||||||
typedef char *(*l_readlineT) (const char *prompt);
|
|
||||||
static l_readlineT l_readline = NULL;
|
|
||||||
|
|
||||||
/* pointer to 'add_history' function (if any) */
|
|
||||||
typedef void (*l_addhistT) (const char *string);
|
|
||||||
static l_addhistT l_addhist = NULL;
|
|
||||||
|
|
||||||
|
|
||||||
static char *lua_readline (char *buff, const char *prompt) {
|
|
||||||
if (l_readline != NULL) /* is there a 'readline'? */
|
|
||||||
return (*l_readline)(prompt); /* use it */
|
|
||||||
else { /* emulate 'readline' over 'buff' */
|
|
||||||
fputs(prompt, stdout);
|
|
||||||
fflush(stdout); /* show prompt */
|
|
||||||
return fgets(buff, LUA_MAXINPUT, stdin); /* read line */
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
static void lua_saveline (const char *line) {
|
|
||||||
if (l_addhist != NULL) /* is there an 'add_history'? */
|
|
||||||
(*l_addhist)(line); /* use it */
|
|
||||||
/* else nothing to be done */
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
static void lua_freeline (char *line) {
|
|
||||||
if (l_readline != NULL) /* is there a 'readline'? */
|
|
||||||
free(line); /* free line created by it */
|
|
||||||
/* else 'lua_readline' used an automatic buffer; nothing to free */
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
#if defined(LUA_USE_DLOPEN) && defined(LUA_READLINELIB) /* { */
|
|
||||||
/* try to load 'readline' dynamically */
|
|
||||||
|
|
||||||
#include <dlfcn.h>
|
|
||||||
|
|
||||||
static void lua_initreadline (lua_State *L) {
|
|
||||||
void *lib = dlopen(LUA_READLINELIB, RTLD_NOW | RTLD_LOCAL);
|
|
||||||
if (lib == NULL)
|
|
||||||
lua_warning(L, "library '" LUA_READLINELIB "' not found", 0);
|
|
||||||
else {
|
|
||||||
const char **name = cast(const char**, dlsym(lib, "rl_readline_name"));
|
|
||||||
if (name != NULL)
|
|
||||||
*name = "lua";
|
|
||||||
l_readline = cast(l_readlineT, cast_func(dlsym(lib, "readline")));
|
|
||||||
l_addhist = cast(l_addhistT, cast_func(dlsym(lib, "add_history")));
|
|
||||||
if (l_readline == NULL)
|
|
||||||
lua_warning(L, "unable to load 'readline'", 0);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
#else /* }{ */
|
|
||||||
/* no dlopen or LUA_READLINELIB undefined */
|
|
||||||
|
|
||||||
/* Leave pointers with NULL */
|
|
||||||
#define lua_initreadline(L) ((void)L)
|
|
||||||
|
|
||||||
#endif /* } */
|
|
||||||
|
|
||||||
#endif /* } */
|
|
||||||
|
|
||||||
#endif /* } */
|
|
||||||
|
|
||||||
|
|
||||||
/*
|
|
||||||
** Return the string to be used as a prompt by the interpreter. Leave
|
|
||||||
** the string (or nil, if using the default value) on the stack, to keep
|
|
||||||
** it anchored.
|
|
||||||
*/
|
|
||||||
static const char *get_prompt (lua_State *L, int firstline) {
|
|
||||||
if (lua_getglobal(L, firstline ? "_PROMPT" : "_PROMPT2") == LUA_TNIL)
|
|
||||||
return (firstline ? LUA_PROMPT : LUA_PROMPT2); /* use the default */
|
|
||||||
else { /* apply 'tostring' over the value */
|
|
||||||
const char *p = luaL_tolstring(L, -1, NULL);
|
|
||||||
lua_remove(L, -2); /* remove original value */
|
|
||||||
return p;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/* mark in error messages for incomplete statements */
|
|
||||||
#define EOFMARK "<eof>"
|
|
||||||
#define marklen (sizeof(EOFMARK)/sizeof(char) - 1)
|
|
||||||
|
|
||||||
|
|
||||||
/*
|
|
||||||
** Check whether 'status' signals a syntax error and the error
|
|
||||||
** message at the top of the stack ends with the above mark for
|
|
||||||
** incomplete statements.
|
|
||||||
*/
|
|
||||||
static int incomplete (lua_State *L, int status) {
|
|
||||||
if (status == LUA_ERRSYNTAX) {
|
|
||||||
size_t lmsg;
|
|
||||||
const char *msg = lua_tolstring(L, -1, &lmsg);
|
|
||||||
if (lmsg >= marklen && strcmp(msg + lmsg - marklen, EOFMARK) == 0)
|
|
||||||
return 1;
|
|
||||||
}
|
|
||||||
return 0; /* else... */
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
/*
|
|
||||||
** Prompt the user, read a line, and push it into the Lua stack.
|
|
||||||
*/
|
|
||||||
static int pushline (lua_State *L, int firstline) {
|
|
||||||
char buffer[LUA_MAXINPUT];
|
|
||||||
size_t l;
|
|
||||||
const char *prmt = get_prompt(L, firstline);
|
|
||||||
char *b = lua_readline(buffer, prmt);
|
|
||||||
lua_pop(L, 1); /* remove prompt */
|
|
||||||
if (b == NULL)
|
|
||||||
return 0; /* no input */
|
|
||||||
l = strlen(b);
|
|
||||||
if (l > 0 && b[l-1] == '\n') /* line ends with newline? */
|
|
||||||
b[--l] = '\0'; /* remove it */
|
|
||||||
lua_pushlstring(L, b, l);
|
|
||||||
lua_freeline(b);
|
|
||||||
return 1;
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
/*
|
|
||||||
** Try to compile line on the stack as 'return <line>;'; on return, stack
|
|
||||||
** has either compiled chunk or original line (if compilation failed).
|
|
||||||
*/
|
|
||||||
static int addreturn (lua_State *L) {
|
|
||||||
const char *line = lua_tostring(L, -1); /* original line */
|
|
||||||
const char *retline = lua_pushfstring(L, "return %s;", line);
|
|
||||||
int status = luaL_loadbuffer(L, retline, strlen(retline), "=stdin");
|
|
||||||
if (status == LUA_OK)
|
|
||||||
lua_remove(L, -2); /* remove modified line */
|
|
||||||
else
|
|
||||||
lua_pop(L, 2); /* pop result from 'luaL_loadbuffer' and modified line */
|
|
||||||
return status;
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
static void checklocal (const char *line) {
|
|
||||||
static const size_t szloc = sizeof("local") - 1;
|
|
||||||
static const char space[] = " \t";
|
|
||||||
line += strspn(line, space); /* skip spaces */
|
|
||||||
if (strncmp(line, "local", szloc) == 0 && /* "local"? */
|
|
||||||
strchr(space, *(line + szloc)) != NULL) { /* followed by a space? */
|
|
||||||
lua_writestringerror("%s\n",
|
|
||||||
"warning: locals do not survive across lines in interactive mode");
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
/*
|
|
||||||
** Read multiple lines until a complete Lua statement or an error not
|
|
||||||
** for an incomplete statement. Start with first line already read in
|
|
||||||
** the stack.
|
|
||||||
*/
|
|
||||||
static int multiline (lua_State *L) {
|
|
||||||
size_t len;
|
|
||||||
const char *line = lua_tolstring(L, 1, &len); /* get first line */
|
|
||||||
checklocal(line);
|
|
||||||
for (;;) { /* repeat until gets a complete statement */
|
|
||||||
int status = luaL_loadbuffer(L, line, len, "=stdin"); /* try it */
|
|
||||||
if (!incomplete(L, status) || !pushline(L, 0))
|
|
||||||
return status; /* should not or cannot try to add continuation line */
|
|
||||||
lua_remove(L, -2); /* remove error message (from incomplete line) */
|
|
||||||
lua_pushliteral(L, "\n"); /* add newline... */
|
|
||||||
lua_insert(L, -2); /* ...between the two lines */
|
|
||||||
lua_concat(L, 3); /* join them */
|
|
||||||
line = lua_tolstring(L, 1, &len); /* get what is has */
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
/*
|
|
||||||
** Read a line and try to load (compile) it first as an expression (by
|
|
||||||
** adding "return " in front of it) and second as a statement. Return
|
|
||||||
** the final status of load/call with the resulting function (if any)
|
|
||||||
** in the top of the stack.
|
|
||||||
*/
|
|
||||||
static int loadline (lua_State *L) {
|
|
||||||
const char *line;
|
|
||||||
int status;
|
|
||||||
lua_settop(L, 0);
|
|
||||||
if (!pushline(L, 1))
|
|
||||||
return -1; /* no input */
|
|
||||||
if ((status = addreturn(L)) != LUA_OK) /* 'return ...' did not work? */
|
|
||||||
status = multiline(L); /* try as command, maybe with continuation lines */
|
|
||||||
line = lua_tostring(L, 1);
|
|
||||||
if (line[0] != '\0') /* non empty? */
|
|
||||||
lua_saveline(line); /* keep history */
|
|
||||||
lua_remove(L, 1); /* remove line from the stack */
|
|
||||||
lua_assert(lua_gettop(L) == 1);
|
|
||||||
return status;
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
/*
|
|
||||||
** Prints (calling the Lua 'print' function) any values on the stack
|
|
||||||
*/
|
|
||||||
static void l_print (lua_State *L) {
|
|
||||||
int n = lua_gettop(L);
|
|
||||||
if (n > 0) { /* any result to be printed? */
|
|
||||||
luaL_checkstack(L, LUA_MINSTACK, "too many results to print");
|
|
||||||
lua_getglobal(L, "print");
|
|
||||||
lua_insert(L, 1);
|
|
||||||
if (lua_pcall(L, n, 0, 0) != LUA_OK)
|
|
||||||
l_message(progname, lua_pushfstring(L, "error calling 'print' (%s)",
|
|
||||||
lua_tostring(L, -1)));
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
/*
|
|
||||||
** Do the REPL: repeatedly read (load) a line, evaluate (call) it, and
|
|
||||||
** print any results.
|
|
||||||
*/
|
|
||||||
static void doREPL (lua_State *L) {
|
|
||||||
int status;
|
|
||||||
const char *oldprogname = progname;
|
|
||||||
progname = NULL; /* no 'progname' on errors in interactive mode */
|
|
||||||
lua_initreadline(L);
|
|
||||||
while ((status = loadline(L)) != -1) {
|
|
||||||
if (status == LUA_OK)
|
|
||||||
status = docall(L, 0, LUA_MULTRET);
|
|
||||||
if (status == LUA_OK) l_print(L);
|
|
||||||
else report(L, status);
|
|
||||||
}
|
|
||||||
lua_settop(L, 0); /* clear stack */
|
|
||||||
lua_writeline();
|
|
||||||
progname = oldprogname;
|
|
||||||
}
|
|
||||||
|
|
||||||
/* }================================================================== */
|
|
||||||
|
|
||||||
#if !defined(luai_openlibs)
|
|
||||||
#define luai_openlibs(L) luaL_openselectedlibs(L, ~0, 0)
|
|
||||||
#endif
|
|
||||||
|
|
||||||
|
|
||||||
/*
|
/*
|
||||||
** Main body of stand-alone interpreter (to be called in protected mode).
|
** Main body of stand-alone interpreter (to be called in protected mode).
|
||||||
** Reads the options and handles them all.
|
** Reads the options and handles them all.
|
||||||
@@ -705,8 +556,8 @@ static int pmain (lua_State *L) {
|
|||||||
char **argv = (char **)lua_touserdata(L, 2);
|
char **argv = (char **)lua_touserdata(L, 2);
|
||||||
int script;
|
int script;
|
||||||
int args = collectargs(argv, &script);
|
int args = collectargs(argv, &script);
|
||||||
int optlim = (script > 0) ? script : argc; /* first argv not an option */
|
|
||||||
luaL_checkversion(L); /* check that interpreter has correct version */
|
luaL_checkversion(L); /* check that interpreter has correct version */
|
||||||
|
if (argv[0] && argv[0][0]) progname = argv[0];
|
||||||
if (args == has_error) { /* bad arg? */
|
if (args == has_error) { /* bad arg? */
|
||||||
print_usage(argv[script]); /* 'script' has index of bad arg. */
|
print_usage(argv[script]); /* 'script' has index of bad arg. */
|
||||||
return 0;
|
return 0;
|
||||||
@@ -717,23 +568,20 @@ static int pmain (lua_State *L) {
|
|||||||
lua_pushboolean(L, 1); /* signal for libraries to ignore env. vars. */
|
lua_pushboolean(L, 1); /* signal for libraries to ignore env. vars. */
|
||||||
lua_setfield(L, LUA_REGISTRYINDEX, "LUA_NOENV");
|
lua_setfield(L, LUA_REGISTRYINDEX, "LUA_NOENV");
|
||||||
}
|
}
|
||||||
luai_openlibs(L); /* open standard libraries */
|
luaL_openlibs(L); /* open standard libraries */
|
||||||
createargtable(L, argv, argc, script); /* create table 'arg' */
|
createargtable(L, argv, argc, script); /* create table 'arg' */
|
||||||
lua_gc(L, LUA_GCRESTART); /* start GC... */
|
|
||||||
lua_gc(L, LUA_GCGEN); /* ...in generational mode */
|
|
||||||
if (!(args & has_E)) { /* no option '-E'? */
|
if (!(args & has_E)) { /* no option '-E'? */
|
||||||
if (handle_luainit(L) != LUA_OK) /* run LUA_INIT */
|
if (handle_luainit(L) != LUA_OK) /* run LUA_INIT */
|
||||||
return 0; /* error running LUA_INIT */
|
return 0; /* error running LUA_INIT */
|
||||||
}
|
}
|
||||||
if (!runargs(L, argv, optlim)) /* execute arguments -e and -l */
|
if (!runargs(L, argv, script)) /* execute arguments -e and -l */
|
||||||
return 0; /* something failed */
|
return 0; /* something failed */
|
||||||
if (script > 0) { /* execute main script (if there is one) */
|
if (script < argc && /* execute main script (if there is one) */
|
||||||
if (handle_script(L, argv + script) != LUA_OK)
|
handle_script(L, argv + script) != LUA_OK)
|
||||||
return 0; /* interrupt in case of error */
|
return 0;
|
||||||
}
|
|
||||||
if (args & has_i) /* -i option? */
|
if (args & has_i) /* -i option? */
|
||||||
doREPL(L); /* do read-eval-print loop */
|
doREPL(L); /* do read-eval-print loop */
|
||||||
else if (script < 1 && !(args & (has_e | has_v))) { /* no active option? */
|
else if (script == argc && !(args & (has_e | has_v))) { /* no arguments? */
|
||||||
if (lua_stdin_is_tty()) { /* running in interactive mode? */
|
if (lua_stdin_is_tty()) { /* running in interactive mode? */
|
||||||
print_version();
|
print_version();
|
||||||
doREPL(L); /* do read-eval-print loop */
|
doREPL(L); /* do read-eval-print loop */
|
||||||
@@ -752,7 +600,6 @@ int main (int argc, char **argv) {
|
|||||||
l_message(argv[0], "cannot create state: not enough memory");
|
l_message(argv[0], "cannot create state: not enough memory");
|
||||||
return EXIT_FAILURE;
|
return EXIT_FAILURE;
|
||||||
}
|
}
|
||||||
lua_gc(L, LUA_GCSTOP); /* stop GC while building state */
|
|
||||||
lua_pushcfunction(L, &pmain); /* to call 'pmain' in protected mode */
|
lua_pushcfunction(L, &pmain); /* to call 'pmain' in protected mode */
|
||||||
lua_pushinteger(L, argc); /* 1st argument */
|
lua_pushinteger(L, argc); /* 1st argument */
|
||||||
lua_pushlightuserdata(L, argv); /* 2nd argument */
|
lua_pushlightuserdata(L, argv); /* 2nd argument */
|
||||||
|
|||||||
@@ -1,7 +1,7 @@
|
|||||||
/*
|
/*
|
||||||
** $Id: lua.h $
|
** $Id: lua.h,v 1.332.1.2 2018/06/13 16:58:17 roberto Exp $
|
||||||
** Lua - A Scripting Language
|
** Lua - A Scripting Language
|
||||||
** Lua.org, PUC-Rio, Brazil (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
|
||||||
*/
|
*/
|
||||||
|
|
||||||
@@ -13,21 +13,20 @@
|
|||||||
#include <stddef.h>
|
#include <stddef.h>
|
||||||
|
|
||||||
|
|
||||||
#define LUA_COPYRIGHT LUA_RELEASE " Copyright (C) 1994-2025 Lua.org, PUC-Rio"
|
|
||||||
#define LUA_AUTHORS "R. Ierusalimschy, L. H. de Figueiredo, W. Celes"
|
|
||||||
|
|
||||||
|
|
||||||
#define LUA_VERSION_MAJOR_N 5
|
|
||||||
#define LUA_VERSION_MINOR_N 5
|
|
||||||
#define LUA_VERSION_RELEASE_N 0
|
|
||||||
|
|
||||||
#define LUA_VERSION_NUM (LUA_VERSION_MAJOR_N * 100 + LUA_VERSION_MINOR_N)
|
|
||||||
#define LUA_VERSION_RELEASE_NUM (LUA_VERSION_NUM * 100 + LUA_VERSION_RELEASE_N)
|
|
||||||
|
|
||||||
|
|
||||||
#include "luaconf.h"
|
#include "luaconf.h"
|
||||||
|
|
||||||
|
|
||||||
|
#define LUA_VERSION_MAJOR "5"
|
||||||
|
#define LUA_VERSION_MINOR "3"
|
||||||
|
#define LUA_VERSION_NUM 503
|
||||||
|
#define LUA_VERSION_RELEASE "5"
|
||||||
|
|
||||||
|
#define LUA_VERSION "Lua " LUA_VERSION_MAJOR "." LUA_VERSION_MINOR
|
||||||
|
#define LUA_RELEASE LUA_VERSION "." LUA_VERSION_RELEASE
|
||||||
|
#define LUA_COPYRIGHT LUA_RELEASE " Copyright (C) 1994-2018 Lua.org, PUC-Rio"
|
||||||
|
#define LUA_AUTHORS "R. Ierusalimschy, L. H. de Figueiredo, W. Celes"
|
||||||
|
|
||||||
|
|
||||||
/* mark for precompiled code ('<esc>Lua') */
|
/* mark for precompiled code ('<esc>Lua') */
|
||||||
#define LUA_SIGNATURE "\x1bLua"
|
#define LUA_SIGNATURE "\x1bLua"
|
||||||
|
|
||||||
@@ -37,10 +36,10 @@
|
|||||||
|
|
||||||
/*
|
/*
|
||||||
** Pseudo-indices
|
** Pseudo-indices
|
||||||
** (The stack size is limited to INT_MAX/2; we keep some free empty
|
** (-LUAI_MAXSTACK is the minimum valid index; we keep some free empty
|
||||||
** space after that to help overflow detection.)
|
** space after that to help overflow detection)
|
||||||
*/
|
*/
|
||||||
#define LUA_REGISTRYINDEX (-(INT_MAX/2 + 1000))
|
#define LUA_REGISTRYINDEX (-LUAI_MAXSTACK - 1000)
|
||||||
#define lua_upvalueindex(i) (LUA_REGISTRYINDEX - (i))
|
#define lua_upvalueindex(i) (LUA_REGISTRYINDEX - (i))
|
||||||
|
|
||||||
|
|
||||||
@@ -50,7 +49,8 @@
|
|||||||
#define LUA_ERRRUN 2
|
#define LUA_ERRRUN 2
|
||||||
#define LUA_ERRSYNTAX 3
|
#define LUA_ERRSYNTAX 3
|
||||||
#define LUA_ERRMEM 4
|
#define LUA_ERRMEM 4
|
||||||
#define LUA_ERRERR 5
|
#define LUA_ERRGCMM 5
|
||||||
|
#define LUA_ERRERR 6
|
||||||
|
|
||||||
|
|
||||||
typedef struct lua_State lua_State;
|
typedef struct lua_State lua_State;
|
||||||
@@ -71,7 +71,7 @@ typedef struct lua_State lua_State;
|
|||||||
#define LUA_TUSERDATA 7
|
#define LUA_TUSERDATA 7
|
||||||
#define LUA_TTHREAD 8
|
#define LUA_TTHREAD 8
|
||||||
|
|
||||||
#define LUA_NUMTYPES 9
|
#define LUA_NUMTAGS 9
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
@@ -80,10 +80,9 @@ typedef struct lua_State lua_State;
|
|||||||
|
|
||||||
|
|
||||||
/* predefined values in the registry */
|
/* predefined values in the registry */
|
||||||
/* index 1 is reserved for the reference mechanism */
|
#define LUA_RIDX_MAINTHREAD 1
|
||||||
#define LUA_RIDX_GLOBALS 2
|
#define LUA_RIDX_GLOBALS 2
|
||||||
#define LUA_RIDX_MAINTHREAD 3
|
#define LUA_RIDX_LAST LUA_RIDX_GLOBALS
|
||||||
#define LUA_RIDX_LAST 3
|
|
||||||
|
|
||||||
|
|
||||||
/* type of numbers in Lua */
|
/* type of numbers in Lua */
|
||||||
@@ -125,23 +124,6 @@ typedef int (*lua_Writer) (lua_State *L, const void *p, size_t sz, void *ud);
|
|||||||
typedef void * (*lua_Alloc) (void *ud, void *ptr, size_t osize, size_t nsize);
|
typedef void * (*lua_Alloc) (void *ud, void *ptr, size_t osize, size_t nsize);
|
||||||
|
|
||||||
|
|
||||||
/*
|
|
||||||
** Type for warning functions
|
|
||||||
*/
|
|
||||||
typedef void (*lua_WarnFunction) (void *ud, const char *msg, int tocont);
|
|
||||||
|
|
||||||
|
|
||||||
/*
|
|
||||||
** Type used by the debug API to collect debug information
|
|
||||||
*/
|
|
||||||
typedef struct lua_Debug lua_Debug;
|
|
||||||
|
|
||||||
|
|
||||||
/*
|
|
||||||
** Functions to be called by the debugger in specific events
|
|
||||||
*/
|
|
||||||
typedef void (*lua_Hook) (lua_State *L, lua_Debug *ar);
|
|
||||||
|
|
||||||
|
|
||||||
/*
|
/*
|
||||||
** generic extra include file
|
** generic extra include file
|
||||||
@@ -160,15 +142,14 @@ extern const char lua_ident[];
|
|||||||
/*
|
/*
|
||||||
** state manipulation
|
** state manipulation
|
||||||
*/
|
*/
|
||||||
LUA_API lua_State *(lua_newstate) (lua_Alloc f, void *ud, unsigned seed);
|
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);
|
||||||
LUA_API lua_State *(lua_newthread) (lua_State *L);
|
LUA_API lua_State *(lua_newthread) (lua_State *L);
|
||||||
LUA_API int (lua_closethread) (lua_State *L, lua_State *from);
|
|
||||||
|
|
||||||
LUA_API lua_CFunction (lua_atpanic) (lua_State *L, lua_CFunction panicf);
|
LUA_API lua_CFunction (lua_atpanic) (lua_State *L, lua_CFunction panicf);
|
||||||
|
|
||||||
|
|
||||||
LUA_API lua_Number (lua_version) (lua_State *L);
|
LUA_API const lua_Number *(lua_version) (lua_State *L);
|
||||||
|
|
||||||
|
|
||||||
/*
|
/*
|
||||||
@@ -201,7 +182,7 @@ LUA_API lua_Number (lua_tonumberx) (lua_State *L, int idx, int *isnum);
|
|||||||
LUA_API lua_Integer (lua_tointegerx) (lua_State *L, int idx, int *isnum);
|
LUA_API lua_Integer (lua_tointegerx) (lua_State *L, int idx, int *isnum);
|
||||||
LUA_API int (lua_toboolean) (lua_State *L, int idx);
|
LUA_API int (lua_toboolean) (lua_State *L, int idx);
|
||||||
LUA_API const char *(lua_tolstring) (lua_State *L, int idx, size_t *len);
|
LUA_API const char *(lua_tolstring) (lua_State *L, int idx, size_t *len);
|
||||||
LUA_API lua_Unsigned (lua_rawlen) (lua_State *L, int idx);
|
LUA_API size_t (lua_rawlen) (lua_State *L, int idx);
|
||||||
LUA_API lua_CFunction (lua_tocfunction) (lua_State *L, int idx);
|
LUA_API lua_CFunction (lua_tocfunction) (lua_State *L, int idx);
|
||||||
LUA_API void *(lua_touserdata) (lua_State *L, int idx);
|
LUA_API void *(lua_touserdata) (lua_State *L, int idx);
|
||||||
LUA_API lua_State *(lua_tothread) (lua_State *L, int idx);
|
LUA_API lua_State *(lua_tothread) (lua_State *L, int idx);
|
||||||
@@ -244,8 +225,6 @@ LUA_API void (lua_pushnil) (lua_State *L);
|
|||||||
LUA_API void (lua_pushnumber) (lua_State *L, lua_Number n);
|
LUA_API void (lua_pushnumber) (lua_State *L, lua_Number n);
|
||||||
LUA_API void (lua_pushinteger) (lua_State *L, lua_Integer n);
|
LUA_API void (lua_pushinteger) (lua_State *L, lua_Integer n);
|
||||||
LUA_API const char *(lua_pushlstring) (lua_State *L, const char *s, size_t len);
|
LUA_API const char *(lua_pushlstring) (lua_State *L, const char *s, size_t len);
|
||||||
LUA_API const char *(lua_pushexternalstring) (lua_State *L,
|
|
||||||
const char *s, size_t len, lua_Alloc falloc, void *ud);
|
|
||||||
LUA_API const char *(lua_pushstring) (lua_State *L, const char *s);
|
LUA_API const char *(lua_pushstring) (lua_State *L, const char *s);
|
||||||
LUA_API const char *(lua_pushvfstring) (lua_State *L, const char *fmt,
|
LUA_API const char *(lua_pushvfstring) (lua_State *L, const char *fmt,
|
||||||
va_list argp);
|
va_list argp);
|
||||||
@@ -268,9 +247,9 @@ LUA_API int (lua_rawgeti) (lua_State *L, int idx, lua_Integer n);
|
|||||||
LUA_API int (lua_rawgetp) (lua_State *L, int idx, const void *p);
|
LUA_API int (lua_rawgetp) (lua_State *L, int idx, const void *p);
|
||||||
|
|
||||||
LUA_API void (lua_createtable) (lua_State *L, int narr, int nrec);
|
LUA_API void (lua_createtable) (lua_State *L, int narr, int nrec);
|
||||||
LUA_API void *(lua_newuserdatauv) (lua_State *L, size_t sz, int nuvalue);
|
LUA_API void *(lua_newuserdata) (lua_State *L, size_t sz);
|
||||||
LUA_API int (lua_getmetatable) (lua_State *L, int objindex);
|
LUA_API int (lua_getmetatable) (lua_State *L, int objindex);
|
||||||
LUA_API int (lua_getiuservalue) (lua_State *L, int idx, int n);
|
LUA_API int (lua_getuservalue) (lua_State *L, int idx);
|
||||||
|
|
||||||
|
|
||||||
/*
|
/*
|
||||||
@@ -284,7 +263,7 @@ LUA_API void (lua_rawset) (lua_State *L, int idx);
|
|||||||
LUA_API void (lua_rawseti) (lua_State *L, int idx, lua_Integer n);
|
LUA_API void (lua_rawseti) (lua_State *L, int idx, lua_Integer n);
|
||||||
LUA_API void (lua_rawsetp) (lua_State *L, int idx, const void *p);
|
LUA_API void (lua_rawsetp) (lua_State *L, int idx, const void *p);
|
||||||
LUA_API int (lua_setmetatable) (lua_State *L, int objindex);
|
LUA_API int (lua_setmetatable) (lua_State *L, int objindex);
|
||||||
LUA_API int (lua_setiuservalue) (lua_State *L, int idx, int n);
|
LUA_API void (lua_setuservalue) (lua_State *L, int idx);
|
||||||
|
|
||||||
|
|
||||||
/*
|
/*
|
||||||
@@ -309,8 +288,7 @@ LUA_API int (lua_dump) (lua_State *L, lua_Writer writer, void *data, int strip);
|
|||||||
*/
|
*/
|
||||||
LUA_API int (lua_yieldk) (lua_State *L, int nresults, lua_KContext ctx,
|
LUA_API int (lua_yieldk) (lua_State *L, int nresults, lua_KContext ctx,
|
||||||
lua_KFunction k);
|
lua_KFunction k);
|
||||||
LUA_API int (lua_resume) (lua_State *L, lua_State *from, int narg,
|
LUA_API int (lua_resume) (lua_State *L, lua_State *from, int narg);
|
||||||
int *nres);
|
|
||||||
LUA_API int (lua_status) (lua_State *L);
|
LUA_API int (lua_status) (lua_State *L);
|
||||||
LUA_API int (lua_isyieldable) (lua_State *L);
|
LUA_API int (lua_isyieldable) (lua_State *L);
|
||||||
|
|
||||||
@@ -318,14 +296,7 @@ LUA_API int (lua_isyieldable) (lua_State *L);
|
|||||||
|
|
||||||
|
|
||||||
/*
|
/*
|
||||||
** Warning-related functions
|
** garbage-collection function and options
|
||||||
*/
|
|
||||||
LUA_API void (lua_setwarnf) (lua_State *L, lua_WarnFunction f, void *ud);
|
|
||||||
LUA_API void (lua_warning) (lua_State *L, const char *msg, int tocont);
|
|
||||||
|
|
||||||
|
|
||||||
/*
|
|
||||||
** garbage-collection options
|
|
||||||
*/
|
*/
|
||||||
|
|
||||||
#define LUA_GCSTOP 0
|
#define LUA_GCSTOP 0
|
||||||
@@ -334,30 +305,11 @@ LUA_API void (lua_warning) (lua_State *L, const char *msg, int tocont);
|
|||||||
#define LUA_GCCOUNT 3
|
#define LUA_GCCOUNT 3
|
||||||
#define LUA_GCCOUNTB 4
|
#define LUA_GCCOUNTB 4
|
||||||
#define LUA_GCSTEP 5
|
#define LUA_GCSTEP 5
|
||||||
#define LUA_GCISRUNNING 6
|
#define LUA_GCSETPAUSE 6
|
||||||
#define LUA_GCGEN 7
|
#define LUA_GCSETSTEPMUL 7
|
||||||
#define LUA_GCINC 8
|
#define LUA_GCISRUNNING 9
|
||||||
#define LUA_GCPARAM 9
|
|
||||||
|
|
||||||
|
LUA_API int (lua_gc) (lua_State *L, int what, int data);
|
||||||
/*
|
|
||||||
** garbage-collection parameters
|
|
||||||
*/
|
|
||||||
/* parameters for generational mode */
|
|
||||||
#define LUA_GCPMINORMUL 0 /* control minor collections */
|
|
||||||
#define LUA_GCPMAJORMINOR 1 /* control shift major->minor */
|
|
||||||
#define LUA_GCPMINORMAJOR 2 /* control shift minor->major */
|
|
||||||
|
|
||||||
/* parameters for incremental mode */
|
|
||||||
#define LUA_GCPPAUSE 3 /* size of pause between successive GCs */
|
|
||||||
#define LUA_GCPSTEPMUL 4 /* GC "speed" */
|
|
||||||
#define LUA_GCPSTEPSIZE 5 /* GC granularity */
|
|
||||||
|
|
||||||
/* number of parameters */
|
|
||||||
#define LUA_GCPN 6
|
|
||||||
|
|
||||||
|
|
||||||
LUA_API int (lua_gc) (lua_State *L, int what, ...);
|
|
||||||
|
|
||||||
|
|
||||||
/*
|
/*
|
||||||
@@ -371,15 +323,11 @@ LUA_API int (lua_next) (lua_State *L, int idx);
|
|||||||
LUA_API void (lua_concat) (lua_State *L, int n);
|
LUA_API void (lua_concat) (lua_State *L, int n);
|
||||||
LUA_API void (lua_len) (lua_State *L, int idx);
|
LUA_API void (lua_len) (lua_State *L, int idx);
|
||||||
|
|
||||||
#define LUA_N2SBUFFSZ 64
|
LUA_API size_t (lua_stringtonumber) (lua_State *L, const char *s);
|
||||||
LUA_API unsigned (lua_numbertocstring) (lua_State *L, int idx, char *buff);
|
|
||||||
LUA_API size_t (lua_stringtonumber) (lua_State *L, const char *s);
|
|
||||||
|
|
||||||
LUA_API lua_Alloc (lua_getallocf) (lua_State *L, void **ud);
|
LUA_API lua_Alloc (lua_getallocf) (lua_State *L, void **ud);
|
||||||
LUA_API void (lua_setallocf) (lua_State *L, lua_Alloc f, void *ud);
|
LUA_API void (lua_setallocf) (lua_State *L, lua_Alloc f, void *ud);
|
||||||
|
|
||||||
LUA_API void (lua_toclose) (lua_State *L, int idx);
|
|
||||||
LUA_API void (lua_closeslot) (lua_State *L, int idx);
|
|
||||||
|
|
||||||
|
|
||||||
/*
|
/*
|
||||||
@@ -429,16 +377,16 @@ LUA_API void (lua_closeslot) (lua_State *L, int idx);
|
|||||||
|
|
||||||
/*
|
/*
|
||||||
** {==============================================================
|
** {==============================================================
|
||||||
** compatibility macros
|
** compatibility macros for unsigned conversions
|
||||||
** ===============================================================
|
** ===============================================================
|
||||||
*/
|
*/
|
||||||
|
#if defined(LUA_COMPAT_APIINTCASTS)
|
||||||
|
|
||||||
#define lua_newuserdata(L,s) lua_newuserdatauv(L,s,1)
|
#define lua_pushunsigned(L,n) lua_pushinteger(L, (lua_Integer)(n))
|
||||||
#define lua_getuservalue(L,idx) lua_getiuservalue(L,idx,1)
|
#define lua_tounsignedx(L,i,is) ((lua_Unsigned)lua_tointegerx(L,i,is))
|
||||||
#define lua_setuservalue(L,idx) lua_setiuservalue(L,idx,1)
|
#define lua_tounsigned(L,i) lua_tounsignedx(L,(i),NULL)
|
||||||
|
|
||||||
#define lua_resetthread(L) lua_closethread(L,NULL)
|
|
||||||
|
|
||||||
|
#endif
|
||||||
/* }============================================================== */
|
/* }============================================================== */
|
||||||
|
|
||||||
/*
|
/*
|
||||||
@@ -466,6 +414,12 @@ LUA_API void (lua_closeslot) (lua_State *L, int idx);
|
|||||||
#define LUA_MASKLINE (1 << LUA_HOOKLINE)
|
#define LUA_MASKLINE (1 << LUA_HOOKLINE)
|
||||||
#define LUA_MASKCOUNT (1 << LUA_HOOKCOUNT)
|
#define LUA_MASKCOUNT (1 << LUA_HOOKCOUNT)
|
||||||
|
|
||||||
|
typedef struct lua_Debug lua_Debug; /* activation record */
|
||||||
|
|
||||||
|
|
||||||
|
/* Functions to be called by the debugger in specific events */
|
||||||
|
typedef void (*lua_Hook) (lua_State *L, lua_Debug *ar);
|
||||||
|
|
||||||
|
|
||||||
LUA_API int (lua_getstack) (lua_State *L, int level, lua_Debug *ar);
|
LUA_API int (lua_getstack) (lua_State *L, int level, lua_Debug *ar);
|
||||||
LUA_API int (lua_getinfo) (lua_State *L, const char *what, lua_Debug *ar);
|
LUA_API int (lua_getinfo) (lua_State *L, const char *what, lua_Debug *ar);
|
||||||
@@ -490,17 +444,13 @@ struct lua_Debug {
|
|||||||
const char *namewhat; /* (n) 'global', 'local', 'field', 'method' */
|
const char *namewhat; /* (n) 'global', 'local', 'field', 'method' */
|
||||||
const char *what; /* (S) 'Lua', 'C', 'main', 'tail' */
|
const char *what; /* (S) 'Lua', 'C', 'main', 'tail' */
|
||||||
const char *source; /* (S) */
|
const char *source; /* (S) */
|
||||||
size_t srclen; /* (S) */
|
|
||||||
int currentline; /* (l) */
|
int currentline; /* (l) */
|
||||||
int linedefined; /* (S) */
|
int linedefined; /* (S) */
|
||||||
int lastlinedefined; /* (S) */
|
int lastlinedefined; /* (S) */
|
||||||
unsigned char nups; /* (u) number of upvalues */
|
unsigned char nups; /* (u) number of upvalues */
|
||||||
unsigned char nparams;/* (u) number of parameters */
|
unsigned char nparams;/* (u) number of parameters */
|
||||||
char isvararg; /* (u) */
|
char isvararg; /* (u) */
|
||||||
unsigned char extraargs; /* (t) number of extra arguments */
|
|
||||||
char istailcall; /* (t) */
|
char istailcall; /* (t) */
|
||||||
int ftransfer; /* (r) index of first value transferred */
|
|
||||||
int ntransfer; /* (r) number of transferred values */
|
|
||||||
char short_src[LUA_IDSIZE]; /* (S) */
|
char short_src[LUA_IDSIZE]; /* (S) */
|
||||||
/* private part */
|
/* private part */
|
||||||
struct CallInfo *i_ci; /* active function */
|
struct CallInfo *i_ci; /* active function */
|
||||||
@@ -509,19 +459,8 @@ struct lua_Debug {
|
|||||||
/* }====================================================================== */
|
/* }====================================================================== */
|
||||||
|
|
||||||
|
|
||||||
#define LUAI_TOSTRAUX(x) #x
|
|
||||||
#define LUAI_TOSTR(x) LUAI_TOSTRAUX(x)
|
|
||||||
|
|
||||||
#define LUA_VERSION_MAJOR LUAI_TOSTR(LUA_VERSION_MAJOR_N)
|
|
||||||
#define LUA_VERSION_MINOR LUAI_TOSTR(LUA_VERSION_MINOR_N)
|
|
||||||
#define LUA_VERSION_RELEASE LUAI_TOSTR(LUA_VERSION_RELEASE_N)
|
|
||||||
|
|
||||||
#define LUA_VERSION "Lua " LUA_VERSION_MAJOR "." LUA_VERSION_MINOR
|
|
||||||
#define LUA_RELEASE LUA_VERSION "." LUA_VERSION_RELEASE
|
|
||||||
|
|
||||||
|
|
||||||
/******************************************************************************
|
/******************************************************************************
|
||||||
* Copyright (C) 1994-2025 Lua.org, PUC-Rio.
|
* Copyright (C) 1994-2018 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 $
|
** $Id: luaconf.h,v 1.259.1.1 2017/04/19 17:29:57 roberto Exp $
|
||||||
** Configuration file for Lua
|
** Configuration file for Lua
|
||||||
** See Copyright Notice in lua.h
|
** See Copyright Notice in lua.h
|
||||||
*/
|
*/
|
||||||
@@ -14,16 +14,6 @@
|
|||||||
|
|
||||||
/*
|
/*
|
||||||
** ===================================================================
|
** ===================================================================
|
||||||
** General Configuration File for Lua
|
|
||||||
**
|
|
||||||
** Some definitions here can be changed externally, through the compiler
|
|
||||||
** (e.g., with '-D' options): They are commented out or protected
|
|
||||||
** by '#if !defined' guards. However, several other definitions
|
|
||||||
** should be changed directly here, either because they affect the
|
|
||||||
** Lua ABI (by making the changes here, you ensure that all software
|
|
||||||
** connected to Lua, such as C libraries, will be compiled with the same
|
|
||||||
** configuration); or because they are seldom changed.
|
|
||||||
**
|
|
||||||
** Search for "@@" to find all configurable definitions.
|
** Search for "@@" to find all configurable definitions.
|
||||||
** ===================================================================
|
** ===================================================================
|
||||||
*/
|
*/
|
||||||
@@ -32,10 +22,20 @@
|
|||||||
/*
|
/*
|
||||||
** {====================================================================
|
** {====================================================================
|
||||||
** System Configuration: macros to adapt (if needed) Lua to some
|
** System Configuration: macros to adapt (if needed) Lua to some
|
||||||
** particular platform, for instance restricting it to C89.
|
** particular platform, for instance compiling it with 32-bit numbers or
|
||||||
|
** restricting it to C89.
|
||||||
** =====================================================================
|
** =====================================================================
|
||||||
*/
|
*/
|
||||||
|
|
||||||
|
/*
|
||||||
|
@@ LUA_32BITS enables Lua with 32-bit integers and 32-bit floats. You
|
||||||
|
** can also define LUA_32BITS in the make file, but changing here you
|
||||||
|
** ensure that all software connected to Lua will be compiled with the
|
||||||
|
** same configuration.
|
||||||
|
*/
|
||||||
|
/* #define LUA_32BITS */
|
||||||
|
|
||||||
|
|
||||||
/*
|
/*
|
||||||
@@ LUA_USE_C89 controls the use of non-ISO-C89 features.
|
@@ LUA_USE_C89 controls the use of non-ISO-C89 features.
|
||||||
** Define it if you want Lua to avoid the use of a few C99 features
|
** Define it if you want Lua to avoid the use of a few C99 features
|
||||||
@@ -58,62 +58,48 @@
|
|||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
|
||||||
/*
|
|
||||||
** When POSIX DLL ('LUA_USE_DLOPEN') is enabled, the Lua stand-alone
|
|
||||||
** application will try to dynamically link a 'readline' facility
|
|
||||||
** for its REPL. In that case, LUA_READLINELIB is the name of the
|
|
||||||
** library it will look for those facilities. If lua.c cannot open
|
|
||||||
** the specified library, it will generate a warning and then run
|
|
||||||
** without 'readline'. If that macro is not defined, lua.c will not
|
|
||||||
** use 'readline'.
|
|
||||||
*/
|
|
||||||
#if defined(LUA_USE_LINUX)
|
#if defined(LUA_USE_LINUX)
|
||||||
#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_READLINELIB "libreadline.so"
|
#define LUA_USE_READLINE /* needs some extra libraries */
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
|
||||||
#if defined(LUA_USE_MACOSX)
|
#if defined(LUA_USE_MACOSX)
|
||||||
#define LUA_USE_POSIX
|
#define LUA_USE_POSIX
|
||||||
#define LUA_USE_DLOPEN /* macOS does not need -ldl */
|
#define LUA_USE_DLOPEN /* MacOS does not need -ldl */
|
||||||
#define LUA_READLINELIB "libedit.dylib"
|
#define LUA_USE_READLINE /* needs an extra library: -lreadline */
|
||||||
#endif
|
|
||||||
|
|
||||||
|
|
||||||
#if defined(LUA_USE_IOS)
|
|
||||||
#define LUA_USE_POSIX
|
|
||||||
#define LUA_USE_DLOPEN
|
|
||||||
#endif
|
|
||||||
|
|
||||||
|
|
||||||
#if defined(LUA_USE_C89) && defined(LUA_USE_POSIX)
|
|
||||||
#error "POSIX is not compatible with C89"
|
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
|
||||||
/*
|
/*
|
||||||
@@ LUAI_IS32INT is true iff 'int' has (at least) 32 bits.
|
@@ LUA_C89_NUMBERS ensures that Lua uses the largest types available for
|
||||||
|
** C89 ('long' and 'double'); Windows always has '__int64', so it does
|
||||||
|
** not need to use this case.
|
||||||
*/
|
*/
|
||||||
#define LUAI_IS32INT ((UINT_MAX >> 30) >= 3)
|
#if defined(LUA_USE_C89) && !defined(LUA_USE_WINDOWS)
|
||||||
|
#define LUA_C89_NUMBERS
|
||||||
/* }================================================================== */
|
#endif
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
/*
|
/*
|
||||||
** {==================================================================
|
@@ LUAI_BITSINT defines the (minimum) number of bits in an 'int'.
|
||||||
** Configuration for Number types. These options should not be
|
|
||||||
** set externally, because any other code connected to Lua must
|
|
||||||
** use the same configuration.
|
|
||||||
** ===================================================================
|
|
||||||
*/
|
*/
|
||||||
|
/* avoid undefined shifts */
|
||||||
|
#if ((INT_MAX >> 15) >> 15) >= 1
|
||||||
|
#define LUAI_BITSINT 32
|
||||||
|
#else
|
||||||
|
/* 'int' always must have at least 16 bits */
|
||||||
|
#define LUAI_BITSINT 16
|
||||||
|
#endif
|
||||||
|
|
||||||
|
|
||||||
/*
|
/*
|
||||||
@@ LUA_INT_TYPE defines the type for Lua integers.
|
@@ LUA_INT_TYPE defines the type for Lua integers.
|
||||||
@@ LUA_FLOAT_TYPE defines the type for Lua floats.
|
@@ LUA_FLOAT_TYPE defines the type for Lua floats.
|
||||||
** Lua should work fine with any mix of these options supported
|
** Lua should work fine with any mix of these options (if supported
|
||||||
** by your C compiler. The usual configurations are 64-bit integers
|
** by your C compiler). The usual configurations are 64-bit integers
|
||||||
** and 'double' (the default), 32-bit integers and 'float' (for
|
** and 'double' (the default), 32-bit integers and 'float' (for
|
||||||
** restricted platforms), and 'long'/'double' (for C compilers not
|
** restricted platforms), and 'long'/'double' (for C compilers not
|
||||||
** compliant with C99, which may not have support for 'long long').
|
** compliant with C99, which may not have support for 'long long').
|
||||||
@@ -129,61 +115,43 @@
|
|||||||
#define LUA_FLOAT_DOUBLE 2
|
#define LUA_FLOAT_DOUBLE 2
|
||||||
#define LUA_FLOAT_LONGDOUBLE 3
|
#define LUA_FLOAT_LONGDOUBLE 3
|
||||||
|
|
||||||
|
#if defined(LUA_32BITS) /* { */
|
||||||
/* Default configuration ('long long' and 'double', for 64-bit Lua) */
|
|
||||||
#define LUA_INT_DEFAULT LUA_INT_LONGLONG
|
|
||||||
#define LUA_FLOAT_DEFAULT LUA_FLOAT_DOUBLE
|
|
||||||
|
|
||||||
|
|
||||||
/*
|
|
||||||
@@ LUA_32BITS enables Lua with 32-bit integers and 32-bit floats.
|
|
||||||
*/
|
|
||||||
/* #define LUA_32BITS */
|
|
||||||
|
|
||||||
|
|
||||||
/*
|
|
||||||
@@ LUA_C89_NUMBERS ensures that Lua uses the largest types available for
|
|
||||||
** C89 ('long' and 'double'); Windows always has '__int64', so it does
|
|
||||||
** not need to use this case.
|
|
||||||
*/
|
|
||||||
#if defined(LUA_USE_C89) && !defined(LUA_USE_WINDOWS)
|
|
||||||
#define LUA_C89_NUMBERS 1
|
|
||||||
#else
|
|
||||||
#define LUA_C89_NUMBERS 0
|
|
||||||
#endif
|
|
||||||
|
|
||||||
|
|
||||||
#if defined(LUA_32BITS) /* { */
|
|
||||||
/*
|
/*
|
||||||
** 32-bit integers and 'float'
|
** 32-bit integers and 'float'
|
||||||
*/
|
*/
|
||||||
#if LUAI_IS32INT /* use 'int' if big enough */
|
#if LUAI_BITSINT >= 32 /* use 'int' if big enough */
|
||||||
#define LUA_INT_TYPE LUA_INT_INT
|
#define LUA_INT_TYPE LUA_INT_INT
|
||||||
#else /* otherwise use 'long' */
|
#else /* otherwise use 'long' */
|
||||||
#define LUA_INT_TYPE LUA_INT_LONG
|
#define LUA_INT_TYPE LUA_INT_LONG
|
||||||
#endif
|
#endif
|
||||||
#define LUA_FLOAT_TYPE LUA_FLOAT_FLOAT
|
#define LUA_FLOAT_TYPE LUA_FLOAT_FLOAT
|
||||||
|
|
||||||
#elif LUA_C89_NUMBERS /* }{ */
|
#elif defined(LUA_C89_NUMBERS) /* }{ */
|
||||||
/*
|
/*
|
||||||
** largest types available for C89 ('long' and 'double')
|
** largest types available for C89 ('long' and 'double')
|
||||||
*/
|
*/
|
||||||
#define LUA_INT_TYPE LUA_INT_LONG
|
#define LUA_INT_TYPE LUA_INT_LONG
|
||||||
#define LUA_FLOAT_TYPE LUA_FLOAT_DOUBLE
|
#define LUA_FLOAT_TYPE LUA_FLOAT_DOUBLE
|
||||||
|
|
||||||
#else /* }{ */
|
|
||||||
/* use defaults */
|
|
||||||
|
|
||||||
#define LUA_INT_TYPE LUA_INT_DEFAULT
|
|
||||||
#define LUA_FLOAT_TYPE LUA_FLOAT_DEFAULT
|
|
||||||
|
|
||||||
#endif /* } */
|
#endif /* } */
|
||||||
|
|
||||||
|
|
||||||
|
/*
|
||||||
|
** default configuration for 64-bit Lua ('long long' and 'double')
|
||||||
|
*/
|
||||||
|
#if !defined(LUA_INT_TYPE)
|
||||||
|
#define LUA_INT_TYPE LUA_INT_LONGLONG
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#if !defined(LUA_FLOAT_TYPE)
|
||||||
|
#define LUA_FLOAT_TYPE LUA_FLOAT_DOUBLE
|
||||||
|
#endif
|
||||||
|
|
||||||
/* }================================================================== */
|
/* }================================================================== */
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
/*
|
/*
|
||||||
** {==================================================================
|
** {==================================================================
|
||||||
** Configuration for Paths.
|
** Configuration for Paths.
|
||||||
@@ -211,7 +179,6 @@
|
|||||||
** hierarchy or if you want to install your libraries in
|
** hierarchy or if you want to install your libraries in
|
||||||
** non-conventional directories.
|
** non-conventional directories.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
#define LUA_VDIR LUA_VERSION_MAJOR "." LUA_VERSION_MINOR
|
#define LUA_VDIR LUA_VERSION_MAJOR "." LUA_VERSION_MINOR
|
||||||
#if defined(_WIN32) /* { */
|
#if defined(_WIN32) /* { */
|
||||||
/*
|
/*
|
||||||
@@ -221,40 +188,27 @@
|
|||||||
#define LUA_LDIR "!\\lua\\"
|
#define LUA_LDIR "!\\lua\\"
|
||||||
#define LUA_CDIR "!\\"
|
#define LUA_CDIR "!\\"
|
||||||
#define LUA_SHRDIR "!\\..\\share\\lua\\" LUA_VDIR "\\"
|
#define LUA_SHRDIR "!\\..\\share\\lua\\" LUA_VDIR "\\"
|
||||||
|
|
||||||
#if !defined(LUA_PATH_DEFAULT)
|
|
||||||
#define LUA_PATH_DEFAULT \
|
#define LUA_PATH_DEFAULT \
|
||||||
LUA_LDIR"?.lua;" LUA_LDIR"?\\init.lua;" \
|
LUA_LDIR"?.lua;" LUA_LDIR"?\\init.lua;" \
|
||||||
LUA_CDIR"?.lua;" LUA_CDIR"?\\init.lua;" \
|
LUA_CDIR"?.lua;" LUA_CDIR"?\\init.lua;" \
|
||||||
LUA_SHRDIR"?.lua;" LUA_SHRDIR"?\\init.lua;" \
|
LUA_SHRDIR"?.lua;" LUA_SHRDIR"?\\init.lua;" \
|
||||||
".\\?.lua;" ".\\?\\init.lua"
|
".\\?.lua;" ".\\?\\init.lua"
|
||||||
#endif
|
|
||||||
|
|
||||||
#if !defined(LUA_CPATH_DEFAULT)
|
|
||||||
#define LUA_CPATH_DEFAULT \
|
#define LUA_CPATH_DEFAULT \
|
||||||
LUA_CDIR"?.dll;" \
|
LUA_CDIR"?.dll;" \
|
||||||
LUA_CDIR"..\\lib\\lua\\" LUA_VDIR "\\?.dll;" \
|
LUA_CDIR"..\\lib\\lua\\" LUA_VDIR "\\?.dll;" \
|
||||||
LUA_CDIR"loadall.dll;" ".\\?.dll"
|
LUA_CDIR"loadall.dll;" ".\\?.dll"
|
||||||
#endif
|
|
||||||
|
|
||||||
#else /* }{ */
|
#else /* }{ */
|
||||||
|
|
||||||
#define LUA_ROOT "/usr/local/"
|
#define LUA_ROOT "/usr/local/"
|
||||||
#define LUA_LDIR LUA_ROOT "share/lua/" LUA_VDIR "/"
|
#define LUA_LDIR LUA_ROOT "share/lua/" LUA_VDIR "/"
|
||||||
#define LUA_CDIR LUA_ROOT "lib/lua/" LUA_VDIR "/"
|
#define LUA_CDIR LUA_ROOT "lib/lua/" LUA_VDIR "/"
|
||||||
|
|
||||||
#if !defined(LUA_PATH_DEFAULT)
|
|
||||||
#define LUA_PATH_DEFAULT \
|
#define LUA_PATH_DEFAULT \
|
||||||
LUA_LDIR"?.lua;" LUA_LDIR"?/init.lua;" \
|
LUA_LDIR"?.lua;" LUA_LDIR"?/init.lua;" \
|
||||||
LUA_CDIR"?.lua;" LUA_CDIR"?/init.lua;" \
|
LUA_CDIR"?.lua;" LUA_CDIR"?/init.lua;" \
|
||||||
"./?.lua;" "./?/init.lua"
|
"./?.lua;" "./?/init.lua"
|
||||||
#endif
|
|
||||||
|
|
||||||
#if !defined(LUA_CPATH_DEFAULT)
|
|
||||||
#define LUA_CPATH_DEFAULT \
|
#define LUA_CPATH_DEFAULT \
|
||||||
LUA_CDIR"?.so;" LUA_CDIR"loadall.so;" "./?.so"
|
LUA_CDIR"?.so;" LUA_CDIR"loadall.so;" "./?.so"
|
||||||
#endif
|
|
||||||
|
|
||||||
#endif /* } */
|
#endif /* } */
|
||||||
|
|
||||||
|
|
||||||
@@ -263,25 +217,12 @@
|
|||||||
** CHANGE it if your machine does not use "/" as the directory separator
|
** CHANGE it if your machine does not use "/" as the directory separator
|
||||||
** and is not Windows. (On Windows Lua automatically uses "\".)
|
** and is not Windows. (On Windows Lua automatically uses "\".)
|
||||||
*/
|
*/
|
||||||
#if !defined(LUA_DIRSEP)
|
|
||||||
|
|
||||||
#if defined(_WIN32)
|
#if defined(_WIN32)
|
||||||
#define LUA_DIRSEP "\\"
|
#define LUA_DIRSEP "\\"
|
||||||
#else
|
#else
|
||||||
#define LUA_DIRSEP "/"
|
#define LUA_DIRSEP "/"
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
#endif
|
|
||||||
|
|
||||||
|
|
||||||
/*
|
|
||||||
** LUA_IGMARK is a mark to ignore all after it when building the
|
|
||||||
** module name (e.g., used to build the luaopen_ function name).
|
|
||||||
** Typically, the suffix after the mark is the module version,
|
|
||||||
** as in "mod-v1.2.so".
|
|
||||||
*/
|
|
||||||
#define LUA_IGMARK "-"
|
|
||||||
|
|
||||||
/* }================================================================== */
|
/* }================================================================== */
|
||||||
|
|
||||||
|
|
||||||
@@ -315,17 +256,34 @@
|
|||||||
#endif /* } */
|
#endif /* } */
|
||||||
|
|
||||||
|
|
||||||
/*
|
/* more often than not the libs go together with the core */
|
||||||
** More often than not the libs go together with the core.
|
|
||||||
*/
|
|
||||||
#define LUALIB_API LUA_API
|
#define LUALIB_API LUA_API
|
||||||
|
#define LUAMOD_API LUALIB_API
|
||||||
|
|
||||||
#if defined(__cplusplus)
|
|
||||||
/* Lua uses the "C name" when calling open functions */
|
/*
|
||||||
#define LUAMOD_API extern "C"
|
@@ LUAI_FUNC is a mark for all extern functions that are not to be
|
||||||
#else
|
** exported to outside modules.
|
||||||
#define LUAMOD_API LUA_API
|
@@ LUAI_DDEF and LUAI_DDEC are marks for all extern (const) variables
|
||||||
#endif
|
** that are not to be exported to outside modules (LUAI_DDEF for
|
||||||
|
** definitions and LUAI_DDEC for declarations).
|
||||||
|
** CHANGE them if you need to mark them in some special way. Elf/gcc
|
||||||
|
** (versions 3.2 and later) mark them as "hidden" to optimize access
|
||||||
|
** when Lua is compiled as a shared library. Not all elf targets support
|
||||||
|
** this attribute. Unfortunately, gcc does not offer a way to check
|
||||||
|
** whether the target offers that support, and those without support
|
||||||
|
** give a warning about it. To avoid these warnings, change to the
|
||||||
|
** default definition.
|
||||||
|
*/
|
||||||
|
#if defined(__GNUC__) && ((__GNUC__*100 + __GNUC_MINOR__) >= 302) && \
|
||||||
|
defined(__ELF__) /* { */
|
||||||
|
#define LUAI_FUNC __attribute__((visibility("hidden"))) extern
|
||||||
|
#else /* }{ */
|
||||||
|
#define LUAI_FUNC extern
|
||||||
|
#endif /* } */
|
||||||
|
|
||||||
|
#define LUAI_DDEC LUAI_FUNC
|
||||||
|
#define LUAI_DDEF /* empty */
|
||||||
|
|
||||||
/* }================================================================== */
|
/* }================================================================== */
|
||||||
|
|
||||||
@@ -337,26 +295,88 @@
|
|||||||
*/
|
*/
|
||||||
|
|
||||||
/*
|
/*
|
||||||
@@ LUA_COMPAT_GLOBAL avoids 'global' being a reserved word
|
@@ LUA_COMPAT_5_2 controls other macros for compatibility with Lua 5.2.
|
||||||
|
@@ LUA_COMPAT_5_1 controls other macros for compatibility with Lua 5.1.
|
||||||
|
** You can define it to get all options, or change specific options
|
||||||
|
** to fit your specific needs.
|
||||||
*/
|
*/
|
||||||
#define LUA_COMPAT_GLOBAL
|
#if defined(LUA_COMPAT_5_2) /* { */
|
||||||
|
|
||||||
|
|
||||||
/*
|
/*
|
||||||
@@ LUA_COMPAT_MATHLIB controls the presence of several deprecated
|
@@ LUA_COMPAT_MATHLIB controls the presence of several deprecated
|
||||||
** functions in the mathematical library.
|
** functions in the mathematical library.
|
||||||
** (These functions were already officially removed in 5.3;
|
|
||||||
** nevertheless they are still available here.)
|
|
||||||
*/
|
*/
|
||||||
/* #define LUA_COMPAT_MATHLIB */
|
#define LUA_COMPAT_MATHLIB
|
||||||
|
|
||||||
|
/*
|
||||||
|
@@ LUA_COMPAT_BITLIB controls the presence of library 'bit32'.
|
||||||
|
*/
|
||||||
|
#define LUA_COMPAT_BITLIB
|
||||||
|
|
||||||
|
/*
|
||||||
|
@@ LUA_COMPAT_IPAIRS controls the effectiveness of the __ipairs metamethod.
|
||||||
|
*/
|
||||||
|
#define LUA_COMPAT_IPAIRS
|
||||||
|
|
||||||
|
/*
|
||||||
|
@@ LUA_COMPAT_APIINTCASTS controls the presence of macros for
|
||||||
|
** manipulating other integer types (lua_pushunsigned, lua_tounsigned,
|
||||||
|
** luaL_checkint, luaL_checklong, etc.)
|
||||||
|
*/
|
||||||
|
#define LUA_COMPAT_APIINTCASTS
|
||||||
|
|
||||||
|
#endif /* } */
|
||||||
|
|
||||||
|
|
||||||
|
#if defined(LUA_COMPAT_5_1) /* { */
|
||||||
|
|
||||||
|
/* Incompatibilities from 5.2 -> 5.3 */
|
||||||
|
#define LUA_COMPAT_MATHLIB
|
||||||
|
#define LUA_COMPAT_APIINTCASTS
|
||||||
|
|
||||||
|
/*
|
||||||
|
@@ LUA_COMPAT_UNPACK controls the presence of global 'unpack'.
|
||||||
|
** You can replace it with 'table.unpack'.
|
||||||
|
*/
|
||||||
|
#define LUA_COMPAT_UNPACK
|
||||||
|
|
||||||
|
/*
|
||||||
|
@@ LUA_COMPAT_LOADERS controls the presence of table 'package.loaders'.
|
||||||
|
** You can replace it with 'package.searchers'.
|
||||||
|
*/
|
||||||
|
#define LUA_COMPAT_LOADERS
|
||||||
|
|
||||||
|
/*
|
||||||
|
@@ macro 'lua_cpcall' emulates deprecated function lua_cpcall.
|
||||||
|
** You can call your C function directly (with light C functions).
|
||||||
|
*/
|
||||||
|
#define lua_cpcall(L,f,u) \
|
||||||
|
(lua_pushcfunction(L, (f)), \
|
||||||
|
lua_pushlightuserdata(L,(u)), \
|
||||||
|
lua_pcall(L,1,0,0))
|
||||||
|
|
||||||
|
|
||||||
|
/*
|
||||||
|
@@ LUA_COMPAT_LOG10 defines the function 'log10' in the math library.
|
||||||
|
** You can rewrite 'log10(x)' as 'log(x, 10)'.
|
||||||
|
*/
|
||||||
|
#define LUA_COMPAT_LOG10
|
||||||
|
|
||||||
|
/*
|
||||||
|
@@ LUA_COMPAT_LOADSTRING defines the function 'loadstring' in the base
|
||||||
|
** library. You can rewrite 'loadstring(s)' as 'load(s)'.
|
||||||
|
*/
|
||||||
|
#define LUA_COMPAT_LOADSTRING
|
||||||
|
|
||||||
|
/*
|
||||||
|
@@ LUA_COMPAT_MAXN defines the function 'maxn' in the table library.
|
||||||
|
*/
|
||||||
|
#define LUA_COMPAT_MAXN
|
||||||
|
|
||||||
/*
|
/*
|
||||||
@@ The following macros supply trivial compatibility for some
|
@@ The following macros supply trivial compatibility for some
|
||||||
** changes in the API. The macros themselves document how to
|
** changes in the API. The macros themselves document how to
|
||||||
** change your code to avoid using them.
|
** change your code to avoid using them.
|
||||||
** (Once more, these macros were officially removed in 5.3, but they are
|
|
||||||
** still available here.)
|
|
||||||
*/
|
*/
|
||||||
#define lua_strlen(L,i) lua_rawlen(L, (i))
|
#define lua_strlen(L,i) lua_rawlen(L, (i))
|
||||||
|
|
||||||
@@ -365,41 +385,70 @@
|
|||||||
#define lua_equal(L,idx1,idx2) lua_compare(L,(idx1),(idx2),LUA_OPEQ)
|
#define lua_equal(L,idx1,idx2) lua_compare(L,(idx1),(idx2),LUA_OPEQ)
|
||||||
#define lua_lessthan(L,idx1,idx2) lua_compare(L,(idx1),(idx2),LUA_OPLT)
|
#define lua_lessthan(L,idx1,idx2) lua_compare(L,(idx1),(idx2),LUA_OPLT)
|
||||||
|
|
||||||
|
/*
|
||||||
|
@@ LUA_COMPAT_MODULE controls compatibility with previous
|
||||||
|
** module functions 'module' (Lua) and 'luaL_register' (C).
|
||||||
|
*/
|
||||||
|
#define LUA_COMPAT_MODULE
|
||||||
|
|
||||||
|
#endif /* } */
|
||||||
|
|
||||||
|
|
||||||
|
/*
|
||||||
|
@@ LUA_COMPAT_FLOATSTRING makes Lua format integral floats without a
|
||||||
|
@@ a float mark ('.0').
|
||||||
|
** This macro is not on by default even in compatibility mode,
|
||||||
|
** because this is not really an incompatibility.
|
||||||
|
*/
|
||||||
|
/* #define LUA_COMPAT_FLOATSTRING */
|
||||||
|
|
||||||
/* }================================================================== */
|
/* }================================================================== */
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
/*
|
/*
|
||||||
** {==================================================================
|
** {==================================================================
|
||||||
** Configuration for Numbers (low-level part).
|
** Configuration for Numbers.
|
||||||
** Change these definitions if no predefined LUA_FLOAT_* / LUA_INT_*
|
** Change these definitions if no predefined LUA_FLOAT_* / LUA_INT_*
|
||||||
** satisfy your needs.
|
** satisfy your needs.
|
||||||
** ===================================================================
|
** ===================================================================
|
||||||
*/
|
*/
|
||||||
|
|
||||||
/*
|
/*
|
||||||
|
@@ LUA_NUMBER is the floating-point type used by Lua.
|
||||||
@@ LUAI_UACNUMBER is the result of a 'default argument promotion'
|
@@ LUAI_UACNUMBER is the result of a 'default argument promotion'
|
||||||
@@ over a floating number.
|
@@ over a floating number.
|
||||||
@@ l_floatatt(x) corrects float attribute 'x' to the proper float type
|
@@ l_mathlim(x) corrects limit name 'x' to the proper float type
|
||||||
** by prefixing it with one of FLT/DBL/LDBL.
|
** by prefixing it with one of FLT/DBL/LDBL.
|
||||||
@@ LUA_NUMBER_FRMLEN is the length modifier for writing floats.
|
@@ LUA_NUMBER_FRMLEN is the length modifier for writing floats.
|
||||||
@@ LUA_NUMBER_FMT is the format for writing floats with the maximum
|
@@ LUA_NUMBER_FMT is the format for writing floats.
|
||||||
** number of digits that respects tostring(tonumber(numeral)) == numeral.
|
@@ lua_number2str converts a float to a string.
|
||||||
** (That would be floor(log10(2^n)), where n is the number of bits in
|
|
||||||
** the float mantissa.)
|
|
||||||
@@ LUA_NUMBER_FMT_N is the format for writing floats with the minimum
|
|
||||||
** number of digits that ensures tonumber(tostring(number)) == number.
|
|
||||||
** (That would be LUA_NUMBER_FMT+2.)
|
|
||||||
@@ l_mathop allows the addition of an 'l' or 'f' to all math operations.
|
@@ l_mathop allows the addition of an 'l' or 'f' to all math operations.
|
||||||
@@ l_floor takes the floor of a float.
|
@@ l_floor takes the floor of a float.
|
||||||
@@ lua_str2number converts a decimal numeral to a number.
|
@@ lua_str2number converts a decimal numeric string to a number.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
|
|
||||||
/* The following definition is good for most cases here */
|
/* The following definitions are good for most cases here */
|
||||||
|
|
||||||
#define l_floor(x) (l_mathop(floor)(x))
|
#define l_floor(x) (l_mathop(floor)(x))
|
||||||
|
|
||||||
|
#define lua_number2str(s,sz,n) \
|
||||||
|
l_sprintf((s), sz, LUA_NUMBER_FMT, (LUAI_UACNUMBER)(n))
|
||||||
|
|
||||||
|
/*
|
||||||
|
@@ lua_numbertointeger converts a float number to an integer, or
|
||||||
|
** returns 0 if float is not within the range of a lua_Integer.
|
||||||
|
** (The range comparisons are tricky because of rounding. The tests
|
||||||
|
** here assume a two-complement representation, where MININTEGER always
|
||||||
|
** has an exact representation as a float; MAXINTEGER may not have one,
|
||||||
|
** and therefore its conversion to float may have an ill-defined value.)
|
||||||
|
*/
|
||||||
|
#define lua_numbertointeger(n,p) \
|
||||||
|
((n) >= (LUA_NUMBER)(LUA_MININTEGER) && \
|
||||||
|
(n) < -(LUA_NUMBER)(LUA_MININTEGER) && \
|
||||||
|
(*(p) = (LUA_INTEGER)(n), 1))
|
||||||
|
|
||||||
|
|
||||||
/* now the variable definitions */
|
/* now the variable definitions */
|
||||||
|
|
||||||
@@ -407,13 +456,12 @@
|
|||||||
|
|
||||||
#define LUA_NUMBER float
|
#define LUA_NUMBER float
|
||||||
|
|
||||||
#define l_floatatt(n) (FLT_##n)
|
#define l_mathlim(n) (FLT_##n)
|
||||||
|
|
||||||
#define LUAI_UACNUMBER double
|
#define LUAI_UACNUMBER double
|
||||||
|
|
||||||
#define LUA_NUMBER_FRMLEN ""
|
#define LUA_NUMBER_FRMLEN ""
|
||||||
#define LUA_NUMBER_FMT "%.7g"
|
#define LUA_NUMBER_FMT "%.7g"
|
||||||
#define LUA_NUMBER_FMT_N "%.9g"
|
|
||||||
|
|
||||||
#define l_mathop(op) op##f
|
#define l_mathop(op) op##f
|
||||||
|
|
||||||
@@ -424,13 +472,12 @@
|
|||||||
|
|
||||||
#define LUA_NUMBER long double
|
#define LUA_NUMBER long double
|
||||||
|
|
||||||
#define l_floatatt(n) (LDBL_##n)
|
#define l_mathlim(n) (LDBL_##n)
|
||||||
|
|
||||||
#define LUAI_UACNUMBER long double
|
#define LUAI_UACNUMBER long double
|
||||||
|
|
||||||
#define LUA_NUMBER_FRMLEN "L"
|
#define LUA_NUMBER_FRMLEN "L"
|
||||||
#define LUA_NUMBER_FMT "%.19Lg"
|
#define LUA_NUMBER_FMT "%.19Lg"
|
||||||
#define LUA_NUMBER_FMT_N "%.21Lg"
|
|
||||||
|
|
||||||
#define l_mathop(op) op##l
|
#define l_mathop(op) op##l
|
||||||
|
|
||||||
@@ -440,13 +487,12 @@
|
|||||||
|
|
||||||
#define LUA_NUMBER double
|
#define LUA_NUMBER double
|
||||||
|
|
||||||
#define l_floatatt(n) (DBL_##n)
|
#define l_mathlim(n) (DBL_##n)
|
||||||
|
|
||||||
#define LUAI_UACNUMBER double
|
#define LUAI_UACNUMBER double
|
||||||
|
|
||||||
#define LUA_NUMBER_FRMLEN ""
|
#define LUA_NUMBER_FRMLEN ""
|
||||||
#define LUA_NUMBER_FMT "%.15g"
|
#define LUA_NUMBER_FMT "%.14g"
|
||||||
#define LUA_NUMBER_FMT_N "%.17g"
|
|
||||||
|
|
||||||
#define l_mathop(op) op
|
#define l_mathop(op) op
|
||||||
|
|
||||||
@@ -461,14 +507,16 @@
|
|||||||
|
|
||||||
|
|
||||||
/*
|
/*
|
||||||
|
@@ LUA_INTEGER is the integer type used by Lua.
|
||||||
|
**
|
||||||
@@ LUA_UNSIGNED is the unsigned version of LUA_INTEGER.
|
@@ LUA_UNSIGNED is the unsigned version of LUA_INTEGER.
|
||||||
|
**
|
||||||
@@ LUAI_UACINT is the result of a 'default argument promotion'
|
@@ LUAI_UACINT is the result of a 'default argument promotion'
|
||||||
@@ over a LUA_INTEGER.
|
@@ over a lUA_INTEGER.
|
||||||
@@ LUA_INTEGER_FRMLEN is the length modifier for reading/writing integers.
|
@@ LUA_INTEGER_FRMLEN is the length modifier for reading/writing integers.
|
||||||
@@ LUA_INTEGER_FMT is the format for writing integers.
|
@@ LUA_INTEGER_FMT is the format for writing integers.
|
||||||
@@ LUA_MAXINTEGER is the maximum value for a LUA_INTEGER.
|
@@ LUA_MAXINTEGER is the maximum value for a LUA_INTEGER.
|
||||||
@@ LUA_MININTEGER is the minimum value for a LUA_INTEGER.
|
@@ LUA_MININTEGER is the minimum value for a LUA_INTEGER.
|
||||||
@@ LUA_MAXUNSIGNED is the maximum value for a LUA_UNSIGNED.
|
|
||||||
@@ lua_integer2str converts an integer to a string.
|
@@ lua_integer2str converts an integer to a string.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
@@ -499,8 +547,6 @@
|
|||||||
#define LUA_MAXINTEGER INT_MAX
|
#define LUA_MAXINTEGER INT_MAX
|
||||||
#define LUA_MININTEGER INT_MIN
|
#define LUA_MININTEGER INT_MIN
|
||||||
|
|
||||||
#define LUA_MAXUNSIGNED UINT_MAX
|
|
||||||
|
|
||||||
#elif LUA_INT_TYPE == LUA_INT_LONG /* }{ long */
|
#elif LUA_INT_TYPE == LUA_INT_LONG /* }{ long */
|
||||||
|
|
||||||
#define LUA_INTEGER long
|
#define LUA_INTEGER long
|
||||||
@@ -509,8 +555,6 @@
|
|||||||
#define LUA_MAXINTEGER LONG_MAX
|
#define LUA_MAXINTEGER LONG_MAX
|
||||||
#define LUA_MININTEGER LONG_MIN
|
#define LUA_MININTEGER LONG_MIN
|
||||||
|
|
||||||
#define LUA_MAXUNSIGNED ULONG_MAX
|
|
||||||
|
|
||||||
#elif LUA_INT_TYPE == LUA_INT_LONGLONG /* }{ long long */
|
#elif LUA_INT_TYPE == LUA_INT_LONGLONG /* }{ long long */
|
||||||
|
|
||||||
/* use presence of macro LLONG_MAX as proxy for C99 compliance */
|
/* use presence of macro LLONG_MAX as proxy for C99 compliance */
|
||||||
@@ -523,8 +567,6 @@
|
|||||||
#define LUA_MAXINTEGER LLONG_MAX
|
#define LUA_MAXINTEGER LLONG_MAX
|
||||||
#define LUA_MININTEGER LLONG_MIN
|
#define LUA_MININTEGER LLONG_MIN
|
||||||
|
|
||||||
#define LUA_MAXUNSIGNED ULLONG_MAX
|
|
||||||
|
|
||||||
#elif defined(LUA_USE_WINDOWS) /* }{ */
|
#elif defined(LUA_USE_WINDOWS) /* }{ */
|
||||||
/* in Windows, can use specific Windows types */
|
/* in Windows, can use specific Windows types */
|
||||||
|
|
||||||
@@ -534,8 +576,6 @@
|
|||||||
#define LUA_MAXINTEGER _I64_MAX
|
#define LUA_MAXINTEGER _I64_MAX
|
||||||
#define LUA_MININTEGER _I64_MIN
|
#define LUA_MININTEGER _I64_MIN
|
||||||
|
|
||||||
#define LUA_MAXUNSIGNED _UI64_MAX
|
|
||||||
|
|
||||||
#else /* }{ */
|
#else /* }{ */
|
||||||
|
|
||||||
#error "Compiler does not support 'long long'. Use option '-DLUA_32BITS' \
|
#error "Compiler does not support 'long long'. Use option '-DLUA_32BITS' \
|
||||||
@@ -570,7 +610,7 @@
|
|||||||
|
|
||||||
|
|
||||||
/*
|
/*
|
||||||
@@ lua_strx2number converts a hexadecimal numeral to a number.
|
@@ lua_strx2number converts an hexadecimal numeric string to a number.
|
||||||
** In C99, 'strtod' does that conversion. Otherwise, you can
|
** In C99, 'strtod' does that conversion. Otherwise, you can
|
||||||
** leave 'lua_strx2number' undefined and Lua will provide its own
|
** leave 'lua_strx2number' undefined and Lua will provide its own
|
||||||
** implementation.
|
** implementation.
|
||||||
@@ -588,7 +628,7 @@
|
|||||||
|
|
||||||
|
|
||||||
/*
|
/*
|
||||||
@@ lua_number2strx converts a float to a hexadecimal numeral.
|
@@ lua_number2strx converts a float to an hexadecimal numeric string.
|
||||||
** In C99, 'sprintf' (with format specifiers '%a'/'%A') does that.
|
** In C99, 'sprintf' (with format specifiers '%a'/'%A') does that.
|
||||||
** Otherwise, you can leave 'lua_number2strx' undefined and Lua will
|
** Otherwise, you can leave 'lua_number2strx' undefined and Lua will
|
||||||
** provide its own implementation.
|
** provide its own implementation.
|
||||||
@@ -634,33 +674,12 @@
|
|||||||
/*
|
/*
|
||||||
@@ lua_getlocaledecpoint gets the locale "radix character" (decimal point).
|
@@ lua_getlocaledecpoint gets the locale "radix character" (decimal point).
|
||||||
** Change that if you do not want to use C locales. (Code using this
|
** Change that if you do not want to use C locales. (Code using this
|
||||||
** macro must include the header 'locale.h'.)
|
** macro must include header 'locale.h'.)
|
||||||
*/
|
*/
|
||||||
#if !defined(lua_getlocaledecpoint)
|
#if !defined(lua_getlocaledecpoint)
|
||||||
#define lua_getlocaledecpoint() (localeconv()->decimal_point[0])
|
#define lua_getlocaledecpoint() (localeconv()->decimal_point[0])
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
|
||||||
/*
|
|
||||||
** macros to improve jump prediction, used mostly for error handling
|
|
||||||
** and debug facilities. (Some macros in the Lua API use these macros.
|
|
||||||
** Define LUA_NOBUILTIN if you do not want '__builtin_expect' in your
|
|
||||||
** code.)
|
|
||||||
*/
|
|
||||||
#if !defined(luai_likely)
|
|
||||||
|
|
||||||
#if defined(__GNUC__) && !defined(LUA_NOBUILTIN)
|
|
||||||
#define luai_likely(x) (__builtin_expect(((x) != 0), 1))
|
|
||||||
#define luai_unlikely(x) (__builtin_expect(((x) != 0), 0))
|
|
||||||
#else
|
|
||||||
#define luai_likely(x) (x)
|
|
||||||
#define luai_unlikely(x) (x)
|
|
||||||
#endif
|
|
||||||
|
|
||||||
#endif
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
/* }================================================================== */
|
/* }================================================================== */
|
||||||
|
|
||||||
|
|
||||||
@@ -684,7 +703,10 @@
|
|||||||
@@ LUA_USE_APICHECK turns on several consistency checks on the C API.
|
@@ LUA_USE_APICHECK turns on several consistency checks on the C API.
|
||||||
** Define it as a help when debugging C code.
|
** Define it as a help when debugging C code.
|
||||||
*/
|
*/
|
||||||
/* #define LUA_USE_APICHECK */
|
#if defined(LUA_USE_APICHECK)
|
||||||
|
#include <assert.h>
|
||||||
|
#define luai_apicheck(l,e) assert(e)
|
||||||
|
#endif
|
||||||
|
|
||||||
/* }================================================================== */
|
/* }================================================================== */
|
||||||
|
|
||||||
@@ -693,10 +715,23 @@
|
|||||||
** {==================================================================
|
** {==================================================================
|
||||||
** Macros that affect the API and must be stable (that is, must be the
|
** Macros that affect the API and must be stable (that is, must be the
|
||||||
** same when you compile Lua and when you compile code that links to
|
** same when you compile Lua and when you compile code that links to
|
||||||
** Lua).
|
** Lua). You probably do not want/need to change them.
|
||||||
** =====================================================================
|
** =====================================================================
|
||||||
*/
|
*/
|
||||||
|
|
||||||
|
/*
|
||||||
|
@@ LUAI_MAXSTACK limits the size of the Lua stack.
|
||||||
|
** CHANGE it if you need a different limit. This limit is arbitrary;
|
||||||
|
** its only purpose is to stop Lua from consuming unlimited stack
|
||||||
|
** space (and to reserve some numbers for pseudo-indices).
|
||||||
|
*/
|
||||||
|
#if LUAI_BITSINT >= 32
|
||||||
|
#define LUAI_MAXSTACK 1000000
|
||||||
|
#else
|
||||||
|
#define LUAI_MAXSTACK 15000
|
||||||
|
#endif
|
||||||
|
|
||||||
|
|
||||||
/*
|
/*
|
||||||
@@ LUA_EXTRASPACE defines the size of a raw memory area associated with
|
@@ LUA_EXTRASPACE defines the size of a raw memory area associated with
|
||||||
** a Lua state with very fast access.
|
** a Lua state with very fast access.
|
||||||
@@ -707,28 +742,36 @@
|
|||||||
|
|
||||||
/*
|
/*
|
||||||
@@ LUA_IDSIZE gives the maximum size for the description of the source
|
@@ LUA_IDSIZE gives the maximum size for the description of the source
|
||||||
** of a function in debug information.
|
@@ of a function in debug information.
|
||||||
** CHANGE it if you want a different size.
|
** CHANGE it if you want a different size.
|
||||||
*/
|
*/
|
||||||
#define LUA_IDSIZE 60
|
#define LUA_IDSIZE 60
|
||||||
|
|
||||||
|
|
||||||
/*
|
/*
|
||||||
@@ LUAL_BUFFERSIZE is the initial buffer size used by the lauxlib
|
@@ LUAL_BUFFERSIZE is the buffer size used by the lauxlib buffer system.
|
||||||
** buffer system.
|
** CHANGE it if it uses too much C-stack space. (For long double,
|
||||||
|
** 'string.format("%.99f", -1e4932)' needs 5034 bytes, so a
|
||||||
|
** smaller buffer would force a memory allocation for each call to
|
||||||
|
** 'string.format'.)
|
||||||
*/
|
*/
|
||||||
#define LUAL_BUFFERSIZE ((int)(16 * sizeof(void*) * sizeof(lua_Number)))
|
#if LUA_FLOAT_TYPE == LUA_FLOAT_LONGDOUBLE
|
||||||
|
#define LUAL_BUFFERSIZE 8192
|
||||||
|
#else
|
||||||
/*
|
#define LUAL_BUFFERSIZE ((int)(0x80 * sizeof(void*) * sizeof(lua_Integer)))
|
||||||
@@ LUAI_MAXALIGN defines fields that, when used in a union, ensure
|
#endif
|
||||||
** maximum alignment for the other items in that union.
|
|
||||||
*/
|
|
||||||
#define LUAI_MAXALIGN lua_Number n; double u; void *s; lua_Integer i; long l
|
|
||||||
|
|
||||||
/* }================================================================== */
|
/* }================================================================== */
|
||||||
|
|
||||||
|
|
||||||
|
/*
|
||||||
|
@@ LUA_QL describes how error messages quote program elements.
|
||||||
|
** Lua does not use these macros anymore; they are here for
|
||||||
|
** compatibility only.
|
||||||
|
*/
|
||||||
|
#define LUA_QL(x) "'" x "'"
|
||||||
|
#define LUA_QS LUA_QL("%s")
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
@@ -741,5 +784,7 @@
|
|||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
|||||||
@@ -1,5 +1,5 @@
|
|||||||
/*
|
/*
|
||||||
** $Id: lualib.h $
|
** $Id: lualib.h,v 1.45.1.1 2017/04/19 17:20:42 roberto Exp $
|
||||||
** Lua standard libraries
|
** Lua standard libraries
|
||||||
** See Copyright Notice in lua.h
|
** See Copyright Notice in lua.h
|
||||||
*/
|
*/
|
||||||
@@ -14,52 +14,48 @@
|
|||||||
/* version suffix for environment variable names */
|
/* version suffix for environment variable names */
|
||||||
#define LUA_VERSUFFIX "_" LUA_VERSION_MAJOR "_" LUA_VERSION_MINOR
|
#define LUA_VERSUFFIX "_" LUA_VERSION_MAJOR "_" LUA_VERSION_MINOR
|
||||||
|
|
||||||
#define LUA_GLIBK 1
|
|
||||||
LUAMOD_API int (luaopen_base) (lua_State *L);
|
LUAMOD_API int (luaopen_base) (lua_State *L);
|
||||||
|
|
||||||
#define LUA_LOADLIBNAME "package"
|
|
||||||
#define LUA_LOADLIBK (LUA_GLIBK << 1)
|
|
||||||
LUAMOD_API int (luaopen_package) (lua_State *L);
|
|
||||||
|
|
||||||
|
|
||||||
#define LUA_COLIBNAME "coroutine"
|
#define LUA_COLIBNAME "coroutine"
|
||||||
#define LUA_COLIBK (LUA_LOADLIBK << 1)
|
|
||||||
LUAMOD_API int (luaopen_coroutine) (lua_State *L);
|
LUAMOD_API int (luaopen_coroutine) (lua_State *L);
|
||||||
|
|
||||||
#define LUA_DBLIBNAME "debug"
|
#define LUA_TABLIBNAME "table"
|
||||||
#define LUA_DBLIBK (LUA_COLIBK << 1)
|
LUAMOD_API int (luaopen_table) (lua_State *L);
|
||||||
LUAMOD_API int (luaopen_debug) (lua_State *L);
|
|
||||||
|
|
||||||
#define LUA_IOLIBNAME "io"
|
#define LUA_IOLIBNAME "io"
|
||||||
#define LUA_IOLIBK (LUA_DBLIBK << 1)
|
|
||||||
LUAMOD_API int (luaopen_io) (lua_State *L);
|
LUAMOD_API int (luaopen_io) (lua_State *L);
|
||||||
|
|
||||||
#define LUA_MATHLIBNAME "math"
|
|
||||||
#define LUA_MATHLIBK (LUA_IOLIBK << 1)
|
|
||||||
LUAMOD_API int (luaopen_math) (lua_State *L);
|
|
||||||
|
|
||||||
#define LUA_OSLIBNAME "os"
|
#define LUA_OSLIBNAME "os"
|
||||||
#define LUA_OSLIBK (LUA_MATHLIBK << 1)
|
|
||||||
LUAMOD_API int (luaopen_os) (lua_State *L);
|
LUAMOD_API int (luaopen_os) (lua_State *L);
|
||||||
|
|
||||||
#define LUA_STRLIBNAME "string"
|
#define LUA_STRLIBNAME "string"
|
||||||
#define LUA_STRLIBK (LUA_OSLIBK << 1)
|
|
||||||
LUAMOD_API int (luaopen_string) (lua_State *L);
|
LUAMOD_API int (luaopen_string) (lua_State *L);
|
||||||
|
|
||||||
#define LUA_TABLIBNAME "table"
|
|
||||||
#define LUA_TABLIBK (LUA_STRLIBK << 1)
|
|
||||||
LUAMOD_API int (luaopen_table) (lua_State *L);
|
|
||||||
|
|
||||||
#define LUA_UTF8LIBNAME "utf8"
|
#define LUA_UTF8LIBNAME "utf8"
|
||||||
#define LUA_UTF8LIBK (LUA_TABLIBK << 1)
|
|
||||||
LUAMOD_API int (luaopen_utf8) (lua_State *L);
|
LUAMOD_API int (luaopen_utf8) (lua_State *L);
|
||||||
|
|
||||||
|
#define LUA_BITLIBNAME "bit32"
|
||||||
|
LUAMOD_API int (luaopen_bit32) (lua_State *L);
|
||||||
|
|
||||||
/* open selected libraries */
|
#define LUA_MATHLIBNAME "math"
|
||||||
LUALIB_API void (luaL_openselectedlibs) (lua_State *L, int load, int preload);
|
LUAMOD_API int (luaopen_math) (lua_State *L);
|
||||||
|
|
||||||
/* open all libraries */
|
#define LUA_DBLIBNAME "debug"
|
||||||
#define luaL_openlibs(L) luaL_openselectedlibs(L, ~0, 0)
|
LUAMOD_API int (luaopen_debug) (lua_State *L);
|
||||||
|
|
||||||
|
#define LUA_LOADLIBNAME "package"
|
||||||
|
LUAMOD_API int (luaopen_package) (lua_State *L);
|
||||||
|
|
||||||
|
|
||||||
|
/* open all previous libraries */
|
||||||
|
LUALIB_API void (luaL_openlibs) (lua_State *L);
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
#if !defined(lua_assert)
|
||||||
|
#define lua_assert(x) ((void)0)
|
||||||
|
#endif
|
||||||
|
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
|||||||
@@ -1,5 +1,5 @@
|
|||||||
/*
|
/*
|
||||||
** $Id: lundump.c $
|
** $Id: lundump.c,v 2.44.1.1 2017/04/19 17:20:42 roberto Exp $
|
||||||
** load precompiled Lua chunks
|
** load precompiled Lua chunks
|
||||||
** See Copyright Notice in lua.h
|
** See Copyright Notice in lua.h
|
||||||
*/
|
*/
|
||||||
@@ -10,7 +10,6 @@
|
|||||||
#include "lprefix.h"
|
#include "lprefix.h"
|
||||||
|
|
||||||
|
|
||||||
#include <limits.h>
|
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
|
|
||||||
#include "lua.h"
|
#include "lua.h"
|
||||||
@@ -21,13 +20,12 @@
|
|||||||
#include "lmem.h"
|
#include "lmem.h"
|
||||||
#include "lobject.h"
|
#include "lobject.h"
|
||||||
#include "lstring.h"
|
#include "lstring.h"
|
||||||
#include "ltable.h"
|
|
||||||
#include "lundump.h"
|
#include "lundump.h"
|
||||||
#include "lzio.h"
|
#include "lzio.h"
|
||||||
|
|
||||||
|
|
||||||
#if !defined(luai_verifycode)
|
#if !defined(luai_verifycode)
|
||||||
#define luai_verifycode(L,f) /* empty */
|
#define luai_verifycode(L,b,f) /* empty */
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
|
||||||
@@ -35,390 +33,247 @@ typedef struct {
|
|||||||
lua_State *L;
|
lua_State *L;
|
||||||
ZIO *Z;
|
ZIO *Z;
|
||||||
const char *name;
|
const char *name;
|
||||||
Table *h; /* list for string reuse */
|
|
||||||
size_t offset; /* current position relative to beginning of dump */
|
|
||||||
lua_Unsigned nstr; /* number of strings in the list */
|
|
||||||
lu_byte fixed; /* dump is fixed in memory */
|
|
||||||
} LoadState;
|
} LoadState;
|
||||||
|
|
||||||
|
|
||||||
static l_noret error (LoadState *S, const char *why) {
|
static l_noret error(LoadState *S, const char *why) {
|
||||||
luaO_pushfstring(S->L, "%s: bad binary format (%s)", S->name, why);
|
luaO_pushfstring(S->L, "%s: %s precompiled chunk", S->name, why);
|
||||||
luaD_throw(S->L, LUA_ERRSYNTAX);
|
luaD_throw(S->L, LUA_ERRSYNTAX);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/*
|
/*
|
||||||
** All high-level loads go through loadVector; you can change it to
|
** All high-level loads go through LoadVector; you can change it to
|
||||||
** adapt to the endianness of the input
|
** adapt to the endianness of the input
|
||||||
*/
|
*/
|
||||||
#define loadVector(S,b,n) loadBlock(S,b,cast_sizet(n)*sizeof((b)[0]))
|
#define LoadVector(S,b,n) LoadBlock(S,b,(n)*sizeof((b)[0]))
|
||||||
|
|
||||||
static void loadBlock (LoadState *S, void *b, size_t size) {
|
static void LoadBlock (LoadState *S, void *b, size_t size) {
|
||||||
if (luaZ_read(S->Z, b, size) != 0)
|
if (luaZ_read(S->Z, b, size) != 0)
|
||||||
error(S, "truncated chunk");
|
error(S, "truncated");
|
||||||
S->offset += size;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
static void loadAlign (LoadState *S, unsigned align) {
|
#define LoadVar(S,x) LoadVector(S,&x,1)
|
||||||
unsigned padding = align - cast_uint(S->offset % align);
|
|
||||||
if (padding < align) { /* (padding == align) means no padding */
|
|
||||||
lua_Integer paddingContent;
|
|
||||||
loadBlock(S, &paddingContent, padding);
|
|
||||||
lua_assert(S->offset % align == 0);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
#define getaddr(S,n,t) cast(t *, getaddr_(S,cast_sizet(n) * sizeof(t)))
|
static lu_byte LoadByte (LoadState *S) {
|
||||||
|
lu_byte x;
|
||||||
static const void *getaddr_ (LoadState *S, size_t size) {
|
LoadVar(S, x);
|
||||||
const void *block = luaZ_getaddr(S->Z, size);
|
|
||||||
S->offset += size;
|
|
||||||
if (block == NULL)
|
|
||||||
error(S, "truncated fixed buffer");
|
|
||||||
return block;
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
#define loadVar(S,x) loadVector(S,&x,1)
|
|
||||||
|
|
||||||
|
|
||||||
static lu_byte loadByte (LoadState *S) {
|
|
||||||
int b = zgetc(S->Z);
|
|
||||||
if (b == EOZ)
|
|
||||||
error(S, "truncated chunk");
|
|
||||||
S->offset++;
|
|
||||||
return cast_byte(b);
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
static lua_Unsigned loadVarint (LoadState *S, lua_Unsigned limit) {
|
|
||||||
lua_Unsigned x = 0;
|
|
||||||
int b;
|
|
||||||
limit >>= 7;
|
|
||||||
do {
|
|
||||||
b = loadByte(S);
|
|
||||||
if (x > limit)
|
|
||||||
error(S, "integer overflow");
|
|
||||||
x = (x << 7) | (b & 0x7f);
|
|
||||||
} while ((b & 0x80) != 0);
|
|
||||||
return x;
|
return x;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
static size_t loadSize (LoadState *S) {
|
static int LoadInt (LoadState *S) {
|
||||||
return cast_sizet(loadVarint(S, MAX_SIZE));
|
int x;
|
||||||
|
LoadVar(S, x);
|
||||||
|
return x;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
static int loadInt (LoadState *S) {
|
static lua_Number LoadNumber (LoadState *S) {
|
||||||
return cast_int(loadVarint(S, cast_sizet(INT_MAX)));
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
static lua_Number loadNumber (LoadState *S) {
|
|
||||||
lua_Number x;
|
lua_Number x;
|
||||||
loadVar(S, x);
|
LoadVar(S, x);
|
||||||
return x;
|
return x;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
static lua_Integer loadInteger (LoadState *S) {
|
static lua_Integer LoadInteger (LoadState *S) {
|
||||||
lua_Unsigned cx = loadVarint(S, LUA_MAXUNSIGNED);
|
lua_Integer x;
|
||||||
/* decode unsigned to signed */
|
LoadVar(S, x);
|
||||||
if ((cx & 1) != 0)
|
return x;
|
||||||
return l_castU2S(~(cx >> 1));
|
|
||||||
else
|
|
||||||
return l_castU2S(cx >> 1);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/*
|
static TString *LoadString (LoadState *S) {
|
||||||
** Load a nullable string into slot 'sl' from prototype 'p'. The
|
size_t size = LoadByte(S);
|
||||||
** assignment to the slot and the barrier must be performed before any
|
if (size == 0xFF)
|
||||||
** possible GC activity, to anchor the string. (Both 'loadVector' and
|
LoadVar(S, size);
|
||||||
** 'luaH_setint' can call the GC.)
|
if (size == 0)
|
||||||
*/
|
return NULL;
|
||||||
static void loadString (LoadState *S, Proto *p, TString **sl) {
|
else if (--size <= LUAI_MAXSHORTLEN) { /* short string? */
|
||||||
lua_State *L = S->L;
|
char buff[LUAI_MAXSHORTLEN];
|
||||||
TString *ts;
|
LoadVector(S, buff, size);
|
||||||
TValue sv;
|
return luaS_newlstr(S->L, buff, size);
|
||||||
size_t size = loadSize(S);
|
|
||||||
if (size == 0) { /* previously saved string? */
|
|
||||||
lua_Unsigned idx = loadVarint(S, LUA_MAXUNSIGNED); /* get its index */
|
|
||||||
TValue stv;
|
|
||||||
if (idx == 0) { /* no string? */
|
|
||||||
lua_assert(*sl == NULL); /* must be prefilled */
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
if (novariant(luaH_getint(S->h, l_castU2S(idx), &stv)) != LUA_TSTRING)
|
|
||||||
error(S, "invalid string index");
|
|
||||||
*sl = ts = tsvalue(&stv); /* get its value */
|
|
||||||
luaC_objbarrier(L, p, ts);
|
|
||||||
return; /* do not save it again */
|
|
||||||
}
|
}
|
||||||
else if ((size -= 1) <= LUAI_MAXSHORTLEN) { /* short string? */
|
else { /* long string */
|
||||||
char buff[LUAI_MAXSHORTLEN + 1]; /* extra space for '\0' */
|
TString *ts = luaS_createlngstrobj(S->L, size);
|
||||||
loadVector(S, buff, size + 1); /* load string into buffer */
|
LoadVector(S, getstr(ts), size); /* load directly in final place */
|
||||||
*sl = ts = luaS_newlstr(L, buff, size); /* create string */
|
return ts;
|
||||||
luaC_objbarrier(L, p, ts);
|
|
||||||
}
|
|
||||||
else if (S->fixed) { /* for a fixed buffer, use a fixed string */
|
|
||||||
const char *s = getaddr(S, size + 1, char); /* get content address */
|
|
||||||
*sl = ts = luaS_newextlstr(L, s, size, NULL, NULL);
|
|
||||||
luaC_objbarrier(L, p, ts);
|
|
||||||
}
|
|
||||||
else { /* create internal copy */
|
|
||||||
*sl = ts = luaS_createlngstrobj(L, size); /* create string */
|
|
||||||
luaC_objbarrier(L, p, ts);
|
|
||||||
loadVector(S, getlngstr(ts), size + 1); /* load directly in final place */
|
|
||||||
}
|
|
||||||
/* add string to list of saved strings */
|
|
||||||
S->nstr++;
|
|
||||||
setsvalue(L, &sv, ts);
|
|
||||||
luaH_setint(L, S->h, l_castU2S(S->nstr), &sv);
|
|
||||||
luaC_objbarrierback(L, obj2gco(S->h), ts);
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
static void loadCode (LoadState *S, Proto *f) {
|
|
||||||
int n = loadInt(S);
|
|
||||||
loadAlign(S, sizeof(f->code[0]));
|
|
||||||
if (S->fixed) {
|
|
||||||
f->code = getaddr(S, n, Instruction);
|
|
||||||
f->sizecode = n;
|
|
||||||
}
|
|
||||||
else {
|
|
||||||
f->code = luaM_newvectorchecked(S->L, n, Instruction);
|
|
||||||
f->sizecode = n;
|
|
||||||
loadVector(S, f->code, n);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
static void loadFunction(LoadState *S, Proto *f);
|
static void LoadCode (LoadState *S, Proto *f) {
|
||||||
|
int n = LoadInt(S);
|
||||||
|
f->code = luaM_newvector(S->L, n, Instruction);
|
||||||
|
f->sizecode = n;
|
||||||
|
LoadVector(S, f->code, n);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
static void loadConstants (LoadState *S, Proto *f) {
|
static void LoadFunction(LoadState *S, Proto *f, TString *psource);
|
||||||
|
|
||||||
|
|
||||||
|
static void LoadConstants (LoadState *S, Proto *f) {
|
||||||
int i;
|
int i;
|
||||||
int n = loadInt(S);
|
int n = LoadInt(S);
|
||||||
f->k = luaM_newvectorchecked(S->L, n, TValue);
|
f->k = luaM_newvector(S->L, n, TValue);
|
||||||
f->sizek = n;
|
f->sizek = n;
|
||||||
for (i = 0; i < n; i++)
|
for (i = 0; i < n; i++)
|
||||||
setnilvalue(&f->k[i]);
|
setnilvalue(&f->k[i]);
|
||||||
for (i = 0; i < n; i++) {
|
for (i = 0; i < n; i++) {
|
||||||
TValue *o = &f->k[i];
|
TValue *o = &f->k[i];
|
||||||
int t = loadByte(S);
|
int t = LoadByte(S);
|
||||||
switch (t) {
|
switch (t) {
|
||||||
case LUA_VNIL:
|
case LUA_TNIL:
|
||||||
setnilvalue(o);
|
setnilvalue(o);
|
||||||
break;
|
break;
|
||||||
case LUA_VFALSE:
|
case LUA_TBOOLEAN:
|
||||||
setbfvalue(o);
|
setbvalue(o, LoadByte(S));
|
||||||
break;
|
break;
|
||||||
case LUA_VTRUE:
|
case LUA_TNUMFLT:
|
||||||
setbtvalue(o);
|
setfltvalue(o, LoadNumber(S));
|
||||||
break;
|
break;
|
||||||
case LUA_VNUMFLT:
|
case LUA_TNUMINT:
|
||||||
setfltvalue(o, loadNumber(S));
|
setivalue(o, LoadInteger(S));
|
||||||
break;
|
break;
|
||||||
case LUA_VNUMINT:
|
case LUA_TSHRSTR:
|
||||||
setivalue(o, loadInteger(S));
|
case LUA_TLNGSTR:
|
||||||
break;
|
setsvalue2n(S->L, o, LoadString(S));
|
||||||
case LUA_VSHRSTR:
|
break;
|
||||||
case LUA_VLNGSTR: {
|
default:
|
||||||
lua_assert(f->source == NULL);
|
lua_assert(0);
|
||||||
loadString(S, f, &f->source); /* use 'source' to anchor string */
|
|
||||||
if (f->source == NULL)
|
|
||||||
error(S, "bad format for constant string");
|
|
||||||
setsvalue2n(S->L, o, f->source); /* save it in the right place */
|
|
||||||
f->source = NULL;
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
default: error(S, "invalid constant");
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
static void loadProtos (LoadState *S, Proto *f) {
|
static void LoadProtos (LoadState *S, Proto *f) {
|
||||||
int i;
|
int i;
|
||||||
int n = loadInt(S);
|
int n = LoadInt(S);
|
||||||
f->p = luaM_newvectorchecked(S->L, n, Proto *);
|
f->p = luaM_newvector(S->L, n, Proto *);
|
||||||
f->sizep = n;
|
f->sizep = n;
|
||||||
for (i = 0; i < n; i++)
|
for (i = 0; i < n; i++)
|
||||||
f->p[i] = NULL;
|
f->p[i] = NULL;
|
||||||
for (i = 0; i < n; i++) {
|
for (i = 0; i < n; i++) {
|
||||||
f->p[i] = luaF_newproto(S->L);
|
f->p[i] = luaF_newproto(S->L);
|
||||||
luaC_objbarrier(S->L, f, f->p[i]);
|
LoadFunction(S, f->p[i], f->source);
|
||||||
loadFunction(S, f->p[i]);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/*
|
static void LoadUpvalues (LoadState *S, Proto *f) {
|
||||||
** Load the upvalues for a function. The names must be filled first,
|
int i, n;
|
||||||
** because the filling of the other fields can raise read errors and
|
n = LoadInt(S);
|
||||||
** the creation of the error message can call an emergency collection;
|
f->upvalues = luaM_newvector(S->L, n, Upvaldesc);
|
||||||
** in that case all prototypes must be consistent for the GC.
|
|
||||||
*/
|
|
||||||
static void loadUpvalues (LoadState *S, Proto *f) {
|
|
||||||
int i;
|
|
||||||
int n = loadInt(S);
|
|
||||||
f->upvalues = luaM_newvectorchecked(S->L, n, Upvaldesc);
|
|
||||||
f->sizeupvalues = n;
|
f->sizeupvalues = n;
|
||||||
for (i = 0; i < n; i++) /* make array valid for GC */
|
for (i = 0; i < n; i++)
|
||||||
f->upvalues[i].name = NULL;
|
f->upvalues[i].name = NULL;
|
||||||
for (i = 0; i < n; i++) { /* following calls can raise errors */
|
for (i = 0; i < n; i++) {
|
||||||
f->upvalues[i].instack = loadByte(S);
|
f->upvalues[i].instack = LoadByte(S);
|
||||||
f->upvalues[i].idx = loadByte(S);
|
f->upvalues[i].idx = LoadByte(S);
|
||||||
f->upvalues[i].kind = loadByte(S);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
static void loadDebug (LoadState *S, Proto *f) {
|
static void LoadDebug (LoadState *S, Proto *f) {
|
||||||
int i;
|
int i, n;
|
||||||
int n = loadInt(S);
|
n = LoadInt(S);
|
||||||
if (S->fixed) {
|
f->lineinfo = luaM_newvector(S->L, n, int);
|
||||||
f->lineinfo = getaddr(S, n, ls_byte);
|
f->sizelineinfo = n;
|
||||||
f->sizelineinfo = n;
|
LoadVector(S, f->lineinfo, n);
|
||||||
}
|
n = LoadInt(S);
|
||||||
else {
|
f->locvars = luaM_newvector(S->L, n, LocVar);
|
||||||
f->lineinfo = luaM_newvectorchecked(S->L, n, ls_byte);
|
|
||||||
f->sizelineinfo = n;
|
|
||||||
loadVector(S, f->lineinfo, n);
|
|
||||||
}
|
|
||||||
n = loadInt(S);
|
|
||||||
if (n > 0) {
|
|
||||||
loadAlign(S, sizeof(int));
|
|
||||||
if (S->fixed) {
|
|
||||||
f->abslineinfo = getaddr(S, n, AbsLineInfo);
|
|
||||||
f->sizeabslineinfo = n;
|
|
||||||
}
|
|
||||||
else {
|
|
||||||
f->abslineinfo = luaM_newvectorchecked(S->L, n, AbsLineInfo);
|
|
||||||
f->sizeabslineinfo = n;
|
|
||||||
loadVector(S, f->abslineinfo, n);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
n = loadInt(S);
|
|
||||||
f->locvars = luaM_newvectorchecked(S->L, n, LocVar);
|
|
||||||
f->sizelocvars = n;
|
f->sizelocvars = n;
|
||||||
for (i = 0; i < n; i++)
|
for (i = 0; i < n; i++)
|
||||||
f->locvars[i].varname = NULL;
|
f->locvars[i].varname = NULL;
|
||||||
for (i = 0; i < n; i++) {
|
for (i = 0; i < n; i++) {
|
||||||
loadString(S, f, &f->locvars[i].varname);
|
f->locvars[i].varname = LoadString(S);
|
||||||
f->locvars[i].startpc = loadInt(S);
|
f->locvars[i].startpc = LoadInt(S);
|
||||||
f->locvars[i].endpc = loadInt(S);
|
f->locvars[i].endpc = LoadInt(S);
|
||||||
}
|
}
|
||||||
n = loadInt(S);
|
n = LoadInt(S);
|
||||||
if (n != 0) /* does it have debug information? */
|
|
||||||
n = f->sizeupvalues; /* must be this many */
|
|
||||||
for (i = 0; i < n; i++)
|
for (i = 0; i < n; i++)
|
||||||
loadString(S, f, &f->upvalues[i].name);
|
f->upvalues[i].name = LoadString(S);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
static void loadFunction (LoadState *S, Proto *f) {
|
static void LoadFunction (LoadState *S, Proto *f, TString *psource) {
|
||||||
f->linedefined = loadInt(S);
|
f->source = LoadString(S);
|
||||||
f->lastlinedefined = loadInt(S);
|
if (f->source == NULL) /* no source in dump? */
|
||||||
f->numparams = loadByte(S);
|
f->source = psource; /* reuse parent's source */
|
||||||
/* get only the meaningful flags */
|
f->linedefined = LoadInt(S);
|
||||||
f->flag = cast_byte(loadByte(S) & ~PF_FIXED);
|
f->lastlinedefined = LoadInt(S);
|
||||||
if (S->fixed)
|
f->numparams = LoadByte(S);
|
||||||
f->flag |= PF_FIXED; /* signal that code is fixed */
|
f->is_vararg = LoadByte(S);
|
||||||
f->maxstacksize = loadByte(S);
|
f->maxstacksize = LoadByte(S);
|
||||||
loadCode(S, f);
|
LoadCode(S, f);
|
||||||
loadConstants(S, f);
|
LoadConstants(S, f);
|
||||||
loadUpvalues(S, f);
|
LoadUpvalues(S, f);
|
||||||
loadProtos(S, f);
|
LoadProtos(S, f);
|
||||||
loadString(S, f, &f->source);
|
LoadDebug(S, f);
|
||||||
loadDebug(S, f);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
static void checkliteral (LoadState *S, const char *s, const char *msg) {
|
static void checkliteral (LoadState *S, const char *s, const char *msg) {
|
||||||
char buff[sizeof(LUA_SIGNATURE) + sizeof(LUAC_DATA)]; /* larger than both */
|
char buff[sizeof(LUA_SIGNATURE) + sizeof(LUAC_DATA)]; /* larger than both */
|
||||||
size_t len = strlen(s);
|
size_t len = strlen(s);
|
||||||
loadVector(S, buff, len);
|
LoadVector(S, buff, len);
|
||||||
if (memcmp(s, buff, len) != 0)
|
if (memcmp(s, buff, len) != 0)
|
||||||
error(S, msg);
|
error(S, msg);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
static l_noret numerror (LoadState *S, const char *what, const char *tname) {
|
static void fchecksize (LoadState *S, size_t size, const char *tname) {
|
||||||
const char *msg = luaO_pushfstring(S->L, "%s %s mismatch", tname, what);
|
if (LoadByte(S) != size)
|
||||||
error(S, msg);
|
error(S, luaO_pushfstring(S->L, "%s size mismatch in", tname));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
static void checknumsize (LoadState *S, int size, const char *tname) {
|
#define checksize(S,t) fchecksize(S,sizeof(t),#t)
|
||||||
if (size != loadByte(S))
|
|
||||||
numerror(S, "size", tname);
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
static void checknumformat (LoadState *S, int eq, const char *tname) {
|
|
||||||
if (!eq)
|
|
||||||
numerror(S, "format", tname);
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
#define checknum(S,tvar,value,tname) \
|
|
||||||
{ tvar i; checknumsize(S, sizeof(i), tname); \
|
|
||||||
loadVar(S, i); \
|
|
||||||
checknumformat(S, i == value, tname); }
|
|
||||||
|
|
||||||
|
|
||||||
static void checkHeader (LoadState *S) {
|
static void checkHeader (LoadState *S) {
|
||||||
/* skip 1st char (already read and checked) */
|
checkliteral(S, LUA_SIGNATURE + 1, "not a"); /* 1st char already checked */
|
||||||
checkliteral(S, &LUA_SIGNATURE[1], "not a binary chunk");
|
if (LoadByte(S) != LUAC_VERSION)
|
||||||
if (loadByte(S) != LUAC_VERSION)
|
error(S, "version mismatch in");
|
||||||
error(S, "version mismatch");
|
if (LoadByte(S) != LUAC_FORMAT)
|
||||||
if (loadByte(S) != LUAC_FORMAT)
|
error(S, "format mismatch in");
|
||||||
error(S, "format mismatch");
|
checkliteral(S, LUAC_DATA, "corrupted");
|
||||||
checkliteral(S, LUAC_DATA, "corrupted chunk");
|
checksize(S, int);
|
||||||
checknum(S, int, LUAC_INT, "int");
|
checksize(S, size_t);
|
||||||
checknum(S, Instruction, LUAC_INST, "instruction");
|
checksize(S, Instruction);
|
||||||
checknum(S, lua_Integer, LUAC_INT, "Lua integer");
|
checksize(S, lua_Integer);
|
||||||
checknum(S, lua_Number, LUAC_NUM, "Lua number");
|
checksize(S, lua_Number);
|
||||||
|
if (LoadInteger(S) != LUAC_INT)
|
||||||
|
error(S, "endianness mismatch in");
|
||||||
|
if (LoadNumber(S) != LUAC_NUM)
|
||||||
|
error(S, "float format mismatch in");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/*
|
/*
|
||||||
** Load precompiled chunk.
|
** load precompiled chunk
|
||||||
*/
|
*/
|
||||||
LClosure *luaU_undump (lua_State *L, ZIO *Z, const char *name, int fixed) {
|
LClosure *luaU_undump(lua_State *L, ZIO *Z, const char *name) {
|
||||||
LoadState S;
|
LoadState S;
|
||||||
LClosure *cl;
|
LClosure *cl;
|
||||||
if (*name == '@' || *name == '=')
|
if (*name == '@' || *name == '=')
|
||||||
name = name + 1;
|
S.name = name + 1;
|
||||||
else if (*name == LUA_SIGNATURE[0])
|
else if (*name == LUA_SIGNATURE[0])
|
||||||
name = "binary string";
|
S.name = "binary string";
|
||||||
S.name = name;
|
else
|
||||||
|
S.name = name;
|
||||||
S.L = L;
|
S.L = L;
|
||||||
S.Z = Z;
|
S.Z = Z;
|
||||||
S.fixed = cast_byte(fixed);
|
|
||||||
S.offset = 1; /* fist byte was already read */
|
|
||||||
checkHeader(&S);
|
checkHeader(&S);
|
||||||
cl = luaF_newLclosure(L, loadByte(&S));
|
cl = luaF_newLclosure(L, LoadByte(&S));
|
||||||
setclLvalue2s(L, L->top.p, cl);
|
setclLvalue(L, L->top, cl);
|
||||||
luaD_inctop(L);
|
|
||||||
S.h = luaH_new(L); /* create list of saved strings */
|
|
||||||
S.nstr = 0;
|
|
||||||
sethvalue2s(L, L->top.p, S.h); /* anchor it */
|
|
||||||
luaD_inctop(L);
|
luaD_inctop(L);
|
||||||
cl->p = luaF_newproto(L);
|
cl->p = luaF_newproto(L);
|
||||||
luaC_objbarrier(L, cl, cl->p);
|
LoadFunction(&S, cl->p, NULL);
|
||||||
loadFunction(&S, cl->p);
|
lua_assert(cl->nupvalues == cl->p->sizeupvalues);
|
||||||
if (cl->nupvalues != cl->p->sizeupvalues)
|
luai_verifycode(L, buff, cl->p);
|
||||||
error(&S, "corrupted chunk");
|
|
||||||
luai_verifycode(L, cl->p);
|
|
||||||
L->top.p--; /* pop table */
|
|
||||||
return cl;
|
return cl;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -1,5 +1,5 @@
|
|||||||
/*
|
/*
|
||||||
** $Id: lundump.h $
|
** $Id: lundump.h,v 1.45.1.1 2017/04/19 17:20:42 roberto Exp $
|
||||||
** load precompiled Lua chunks
|
** load precompiled Lua chunks
|
||||||
** See Copyright Notice in lua.h
|
** See Copyright Notice in lua.h
|
||||||
*/
|
*/
|
||||||
@@ -7,8 +7,6 @@
|
|||||||
#ifndef lundump_h
|
#ifndef lundump_h
|
||||||
#define lundump_h
|
#define lundump_h
|
||||||
|
|
||||||
#include <limits.h>
|
|
||||||
|
|
||||||
#include "llimits.h"
|
#include "llimits.h"
|
||||||
#include "lobject.h"
|
#include "lobject.h"
|
||||||
#include "lzio.h"
|
#include "lzio.h"
|
||||||
@@ -17,21 +15,15 @@
|
|||||||
/* data to catch conversion errors */
|
/* data to catch conversion errors */
|
||||||
#define LUAC_DATA "\x19\x93\r\n\x1a\n"
|
#define LUAC_DATA "\x19\x93\r\n\x1a\n"
|
||||||
|
|
||||||
#define LUAC_INT -0x5678
|
#define LUAC_INT 0x5678
|
||||||
#define LUAC_INST 0x12345678
|
#define LUAC_NUM cast_num(370.5)
|
||||||
#define LUAC_NUM cast_num(-370.5)
|
|
||||||
|
|
||||||
/*
|
|
||||||
** Encode major-minor version in one byte, one nibble for each
|
|
||||||
*/
|
|
||||||
#define LUAC_VERSION (LUA_VERSION_MAJOR_N*16+LUA_VERSION_MINOR_N)
|
|
||||||
|
|
||||||
|
#define MYINT(s) (s[0]-'0')
|
||||||
|
#define LUAC_VERSION (MYINT(LUA_VERSION_MAJOR)*16+MYINT(LUA_VERSION_MINOR))
|
||||||
#define LUAC_FORMAT 0 /* this is the official format */
|
#define LUAC_FORMAT 0 /* this is the official format */
|
||||||
|
|
||||||
|
|
||||||
/* load one chunk; from lundump.c */
|
/* load one chunk; from lundump.c */
|
||||||
LUAI_FUNC LClosure* luaU_undump (lua_State* L, ZIO* Z, const char* name,
|
LUAI_FUNC LClosure* luaU_undump (lua_State* L, ZIO* Z, const char* name);
|
||||||
int fixed);
|
|
||||||
|
|
||||||
/* dump one chunk; from ldump.c */
|
/* dump one chunk; from ldump.c */
|
||||||
LUAI_FUNC int luaU_dump (lua_State* L, const Proto* f, lua_Writer w,
|
LUAI_FUNC int luaU_dump (lua_State* L, const Proto* f, lua_Writer w,
|
||||||
|
|||||||
+85
-120
@@ -1,5 +1,5 @@
|
|||||||
/*
|
/*
|
||||||
** $Id: lutf8lib.c $
|
** $Id: lutf8lib.c,v 1.16.1.1 2017/04/19 17:29:57 roberto Exp $
|
||||||
** Standard library for UTF-8 manipulation
|
** Standard library for UTF-8 manipulation
|
||||||
** See Copyright Notice in lua.h
|
** See Copyright Notice in lua.h
|
||||||
*/
|
*/
|
||||||
@@ -10,6 +10,7 @@
|
|||||||
#include "lprefix.h"
|
#include "lprefix.h"
|
||||||
|
|
||||||
|
|
||||||
|
#include <assert.h>
|
||||||
#include <limits.h>
|
#include <limits.h>
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
@@ -18,19 +19,10 @@
|
|||||||
|
|
||||||
#include "lauxlib.h"
|
#include "lauxlib.h"
|
||||||
#include "lualib.h"
|
#include "lualib.h"
|
||||||
#include "llimits.h"
|
|
||||||
|
|
||||||
|
#define MAXUNICODE 0x10FFFF
|
||||||
|
|
||||||
#define MAXUNICODE 0x10FFFFu
|
#define iscont(p) ((*(p) & 0xC0) == 0x80)
|
||||||
|
|
||||||
#define MAXUTF 0x7FFFFFFFu
|
|
||||||
|
|
||||||
|
|
||||||
#define MSGInvalid "invalid UTF-8 code"
|
|
||||||
|
|
||||||
|
|
||||||
#define iscont(c) (((c) & 0xC0) == 0x80)
|
|
||||||
#define iscontp(p) iscont(*(p))
|
|
||||||
|
|
||||||
|
|
||||||
/* from strlib */
|
/* from strlib */
|
||||||
@@ -43,66 +35,57 @@ static lua_Integer u_posrelat (lua_Integer pos, size_t len) {
|
|||||||
|
|
||||||
|
|
||||||
/*
|
/*
|
||||||
** Decode one UTF-8 sequence, returning NULL if byte sequence is
|
** Decode one UTF-8 sequence, returning NULL if byte sequence is invalid.
|
||||||
** invalid. The array 'limits' stores the minimum value for each
|
|
||||||
** sequence length, to check for overlong representations. Its first
|
|
||||||
** entry forces an error for non-ASCII bytes with no continuation
|
|
||||||
** bytes (count == 0).
|
|
||||||
*/
|
*/
|
||||||
static const char *utf8_decode (const char *s, l_uint32 *val, int strict) {
|
static const char *utf8_decode (const char *o, int *val) {
|
||||||
static const l_uint32 limits[] =
|
static const unsigned int limits[] = {0xFF, 0x7F, 0x7FF, 0xFFFF};
|
||||||
{~(l_uint32)0, 0x80, 0x800, 0x10000u, 0x200000u, 0x4000000u};
|
const unsigned char *s = (const unsigned char *)o;
|
||||||
unsigned int c = (unsigned char)s[0];
|
unsigned int c = s[0];
|
||||||
l_uint32 res = 0; /* final result */
|
unsigned int res = 0; /* final result */
|
||||||
if (c < 0x80) /* ASCII? */
|
if (c < 0x80) /* ascii? */
|
||||||
res = c;
|
res = c;
|
||||||
else {
|
else {
|
||||||
int count = 0; /* to count number of continuation bytes */
|
int count = 0; /* to count number of continuation bytes */
|
||||||
for (; c & 0x40; c <<= 1) { /* while it needs continuation bytes... */
|
while (c & 0x40) { /* still have continuation bytes? */
|
||||||
unsigned int cc = (unsigned char)s[++count]; /* read next byte */
|
int cc = s[++count]; /* read next byte */
|
||||||
if (!iscont(cc)) /* not a continuation byte? */
|
if ((cc & 0xC0) != 0x80) /* not a continuation byte? */
|
||||||
return NULL; /* invalid byte sequence */
|
return NULL; /* invalid byte sequence */
|
||||||
res = (res << 6) | (cc & 0x3F); /* add lower 6 bits from cont. byte */
|
res = (res << 6) | (cc & 0x3F); /* add lower 6 bits from cont. byte */
|
||||||
|
c <<= 1; /* to test next bit */
|
||||||
}
|
}
|
||||||
res |= ((l_uint32)(c & 0x7F) << (count * 5)); /* add first byte */
|
res |= ((c & 0x7F) << (count * 5)); /* add first byte */
|
||||||
if (count > 5 || res > MAXUTF || res < limits[count])
|
if (count > 3 || res > MAXUNICODE || res <= limits[count])
|
||||||
return NULL; /* invalid byte sequence */
|
return NULL; /* invalid byte sequence */
|
||||||
s += count; /* skip continuation bytes read */
|
s += count; /* skip continuation bytes read */
|
||||||
}
|
}
|
||||||
if (strict) {
|
|
||||||
/* check for invalid code points; too large or surrogates */
|
|
||||||
if (res > MAXUNICODE || (0xD800u <= res && res <= 0xDFFFu))
|
|
||||||
return NULL;
|
|
||||||
}
|
|
||||||
if (val) *val = res;
|
if (val) *val = res;
|
||||||
return s + 1; /* +1 to include first byte */
|
return (const char *)s + 1; /* +1 to include first byte */
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/*
|
/*
|
||||||
** utf8len(s [, i [, j [, lax]]]) --> number of characters that
|
** utf8len(s [, i [, j]]) --> number of characters that start in the
|
||||||
** start in the range [i,j], or nil + current position if 's' is not
|
** range [i,j], or nil + current position if 's' is not well formed in
|
||||||
** well formed in that interval
|
** that interval
|
||||||
*/
|
*/
|
||||||
static int utflen (lua_State *L) {
|
static int utflen (lua_State *L) {
|
||||||
lua_Integer n = 0; /* counter for the number of characters */
|
int n = 0;
|
||||||
size_t len; /* string length in bytes */
|
size_t len;
|
||||||
const char *s = luaL_checklstring(L, 1, &len);
|
const char *s = luaL_checklstring(L, 1, &len);
|
||||||
lua_Integer posi = u_posrelat(luaL_optinteger(L, 2, 1), len);
|
lua_Integer posi = u_posrelat(luaL_optinteger(L, 2, 1), len);
|
||||||
lua_Integer posj = u_posrelat(luaL_optinteger(L, 3, -1), len);
|
lua_Integer posj = u_posrelat(luaL_optinteger(L, 3, -1), len);
|
||||||
int lax = lua_toboolean(L, 4);
|
|
||||||
luaL_argcheck(L, 1 <= posi && --posi <= (lua_Integer)len, 2,
|
luaL_argcheck(L, 1 <= posi && --posi <= (lua_Integer)len, 2,
|
||||||
"initial position out of bounds");
|
"initial position out of string");
|
||||||
luaL_argcheck(L, --posj < (lua_Integer)len, 3,
|
luaL_argcheck(L, --posj < (lua_Integer)len, 3,
|
||||||
"final position out of bounds");
|
"final position out of string");
|
||||||
while (posi <= posj) {
|
while (posi <= posj) {
|
||||||
const char *s1 = utf8_decode(s + posi, NULL, !lax);
|
const char *s1 = utf8_decode(s + posi, NULL);
|
||||||
if (s1 == NULL) { /* conversion error? */
|
if (s1 == NULL) { /* conversion error? */
|
||||||
luaL_pushfail(L); /* return fail ... */
|
lua_pushnil(L); /* return nil ... */
|
||||||
lua_pushinteger(L, posi + 1); /* ... and current position */
|
lua_pushinteger(L, posi + 1); /* ... and current position */
|
||||||
return 2;
|
return 2;
|
||||||
}
|
}
|
||||||
posi = ct_diff2S(s1 - s);
|
posi = s1 - s;
|
||||||
n++;
|
n++;
|
||||||
}
|
}
|
||||||
lua_pushinteger(L, n);
|
lua_pushinteger(L, n);
|
||||||
@@ -111,32 +94,31 @@ static int utflen (lua_State *L) {
|
|||||||
|
|
||||||
|
|
||||||
/*
|
/*
|
||||||
** codepoint(s, [i, [j [, lax]]]) -> returns codepoints for all
|
** codepoint(s, [i, [j]]) -> returns codepoints for all characters
|
||||||
** characters that start in the range [i,j]
|
** that start in the range [i,j]
|
||||||
*/
|
*/
|
||||||
static int codepoint (lua_State *L) {
|
static int codepoint (lua_State *L) {
|
||||||
size_t len;
|
size_t len;
|
||||||
const char *s = luaL_checklstring(L, 1, &len);
|
const char *s = luaL_checklstring(L, 1, &len);
|
||||||
lua_Integer posi = u_posrelat(luaL_optinteger(L, 2, 1), len);
|
lua_Integer posi = u_posrelat(luaL_optinteger(L, 2, 1), len);
|
||||||
lua_Integer pose = u_posrelat(luaL_optinteger(L, 3, posi), len);
|
lua_Integer pose = u_posrelat(luaL_optinteger(L, 3, posi), len);
|
||||||
int lax = lua_toboolean(L, 4);
|
|
||||||
int n;
|
int n;
|
||||||
const char *se;
|
const char *se;
|
||||||
luaL_argcheck(L, posi >= 1, 2, "out of bounds");
|
luaL_argcheck(L, posi >= 1, 2, "out of range");
|
||||||
luaL_argcheck(L, pose <= (lua_Integer)len, 3, "out of bounds");
|
luaL_argcheck(L, pose <= (lua_Integer)len, 3, "out of range");
|
||||||
if (posi > pose) return 0; /* empty interval; return no values */
|
if (posi > pose) return 0; /* empty interval; return no values */
|
||||||
if (pose - posi >= INT_MAX) /* (lua_Integer -> int) overflow? */
|
if (pose - posi >= INT_MAX) /* (lua_Integer -> int) overflow? */
|
||||||
return luaL_error(L, "string slice too long");
|
return luaL_error(L, "string slice too long");
|
||||||
n = (int)(pose - posi) + 1; /* upper bound for number of returns */
|
n = (int)(pose - posi) + 1;
|
||||||
luaL_checkstack(L, n, "string slice too long");
|
luaL_checkstack(L, n, "string slice too long");
|
||||||
n = 0; /* count the number of returns */
|
n = 0;
|
||||||
se = s + pose; /* string end */
|
se = s + pose;
|
||||||
for (s += posi - 1; s < se;) {
|
for (s += posi - 1; s < se;) {
|
||||||
l_uint32 code;
|
int code;
|
||||||
s = utf8_decode(s, &code, !lax);
|
s = utf8_decode(s, &code);
|
||||||
if (s == NULL)
|
if (s == NULL)
|
||||||
return luaL_error(L, MSGInvalid);
|
return luaL_error(L, "invalid UTF-8 code");
|
||||||
lua_pushinteger(L, l_castU2S(code));
|
lua_pushinteger(L, code);
|
||||||
n++;
|
n++;
|
||||||
}
|
}
|
||||||
return n;
|
return n;
|
||||||
@@ -144,8 +126,8 @@ static int codepoint (lua_State *L) {
|
|||||||
|
|
||||||
|
|
||||||
static void pushutfchar (lua_State *L, int arg) {
|
static void pushutfchar (lua_State *L, int arg) {
|
||||||
lua_Unsigned code = (lua_Unsigned)luaL_checkinteger(L, arg);
|
lua_Integer code = luaL_checkinteger(L, arg);
|
||||||
luaL_argcheck(L, code <= MAXUTF, arg, "value out of range");
|
luaL_argcheck(L, 0 <= code && code <= MAXUNICODE, arg, "value out of range");
|
||||||
lua_pushfstring(L, "%U", (long)code);
|
lua_pushfstring(L, "%U", (long)code);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -172,94 +154,77 @@ static int utfchar (lua_State *L) {
|
|||||||
|
|
||||||
|
|
||||||
/*
|
/*
|
||||||
** offset(s, n, [i]) -> indices where n-th character counting from
|
** offset(s, n, [i]) -> index where n-th character counting from
|
||||||
** position 'i' starts and ends; 0 means character at 'i'.
|
** position 'i' starts; 0 means character at 'i'.
|
||||||
*/
|
*/
|
||||||
static int byteoffset (lua_State *L) {
|
static int byteoffset (lua_State *L) {
|
||||||
size_t len;
|
size_t len;
|
||||||
const char *s = luaL_checklstring(L, 1, &len);
|
const char *s = luaL_checklstring(L, 1, &len);
|
||||||
lua_Integer n = luaL_checkinteger(L, 2);
|
lua_Integer n = luaL_checkinteger(L, 2);
|
||||||
lua_Integer posi = (n >= 0) ? 1 : cast_st2S(len) + 1;
|
lua_Integer posi = (n >= 0) ? 1 : len + 1;
|
||||||
posi = u_posrelat(luaL_optinteger(L, 3, posi), len);
|
posi = u_posrelat(luaL_optinteger(L, 3, posi), len);
|
||||||
luaL_argcheck(L, 1 <= posi && --posi <= (lua_Integer)len, 3,
|
luaL_argcheck(L, 1 <= posi && --posi <= (lua_Integer)len, 3,
|
||||||
"position out of bounds");
|
"position out of range");
|
||||||
if (n == 0) {
|
if (n == 0) {
|
||||||
/* find beginning of current byte sequence */
|
/* find beginning of current byte sequence */
|
||||||
while (posi > 0 && iscontp(s + posi)) posi--;
|
while (posi > 0 && iscont(s + posi)) posi--;
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
if (iscontp(s + posi))
|
if (iscont(s + posi))
|
||||||
return luaL_error(L, "initial position is a continuation byte");
|
return luaL_error(L, "initial position is a continuation byte");
|
||||||
if (n < 0) {
|
if (n < 0) {
|
||||||
while (n < 0 && posi > 0) { /* move back */
|
while (n < 0 && posi > 0) { /* move back */
|
||||||
do { /* find beginning of previous character */
|
do { /* find beginning of previous character */
|
||||||
posi--;
|
posi--;
|
||||||
} while (posi > 0 && iscontp(s + posi));
|
} while (posi > 0 && iscont(s + posi));
|
||||||
n++;
|
n++;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
n--; /* do not move for 1st character */
|
n--; /* do not move for 1st character */
|
||||||
while (n > 0 && posi < (lua_Integer)len) {
|
while (n > 0 && posi < (lua_Integer)len) {
|
||||||
do { /* find beginning of next character */
|
do { /* find beginning of next character */
|
||||||
posi++;
|
posi++;
|
||||||
} while (iscontp(s + posi)); /* (cannot pass final '\0') */
|
} while (iscont(s + posi)); /* (cannot pass final '\0') */
|
||||||
n--;
|
n--;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if (n != 0) { /* did not find given character? */
|
if (n == 0) /* did it find given character? */
|
||||||
luaL_pushfail(L);
|
lua_pushinteger(L, posi + 1);
|
||||||
return 1;
|
else /* no such character */
|
||||||
}
|
lua_pushnil(L);
|
||||||
lua_pushinteger(L, posi + 1); /* initial position */
|
return 1;
|
||||||
if ((s[posi] & 0x80) != 0) { /* multi-byte character? */
|
|
||||||
if (iscont(s[posi]))
|
|
||||||
return luaL_error(L, "initial position is a continuation byte");
|
|
||||||
while (iscontp(s + posi + 1))
|
|
||||||
posi++; /* skip to last continuation byte */
|
|
||||||
}
|
|
||||||
/* else one-byte character: final position is the initial one */
|
|
||||||
lua_pushinteger(L, posi + 1); /* 'posi' now is the final position */
|
|
||||||
return 2;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
static int iter_aux (lua_State *L, int strict) {
|
static int iter_aux (lua_State *L) {
|
||||||
size_t len;
|
size_t len;
|
||||||
const char *s = luaL_checklstring(L, 1, &len);
|
const char *s = luaL_checklstring(L, 1, &len);
|
||||||
lua_Unsigned n = (lua_Unsigned)lua_tointeger(L, 2);
|
lua_Integer n = lua_tointeger(L, 2) - 1;
|
||||||
if (n < len) {
|
if (n < 0) /* first iteration? */
|
||||||
while (iscontp(s + n)) n++; /* go to next character */
|
n = 0; /* start from here */
|
||||||
|
else if (n < (lua_Integer)len) {
|
||||||
|
n++; /* skip current byte */
|
||||||
|
while (iscont(s + n)) n++; /* and its continuations */
|
||||||
}
|
}
|
||||||
if (n >= len) /* (also handles original 'n' being negative) */
|
if (n >= (lua_Integer)len)
|
||||||
return 0; /* no more codepoints */
|
return 0; /* no more codepoints */
|
||||||
else {
|
else {
|
||||||
l_uint32 code;
|
int code;
|
||||||
const char *next = utf8_decode(s + n, &code, strict);
|
const char *next = utf8_decode(s + n, &code);
|
||||||
if (next == NULL || iscontp(next))
|
if (next == NULL || iscont(next))
|
||||||
return luaL_error(L, MSGInvalid);
|
return luaL_error(L, "invalid UTF-8 code");
|
||||||
lua_pushinteger(L, l_castU2S(n + 1));
|
lua_pushinteger(L, n + 1);
|
||||||
lua_pushinteger(L, l_castU2S(code));
|
lua_pushinteger(L, code);
|
||||||
return 2;
|
return 2;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
static int iter_auxstrict (lua_State *L) {
|
|
||||||
return iter_aux(L, 1);
|
|
||||||
}
|
|
||||||
|
|
||||||
static int iter_auxlax (lua_State *L) {
|
|
||||||
return iter_aux(L, 0);
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
static int iter_codes (lua_State *L) {
|
static int iter_codes (lua_State *L) {
|
||||||
int lax = lua_toboolean(L, 2);
|
luaL_checkstring(L, 1);
|
||||||
const char *s = luaL_checkstring(L, 1);
|
lua_pushcfunction(L, iter_aux);
|
||||||
luaL_argcheck(L, !iscontp(s), 1, MSGInvalid);
|
|
||||||
lua_pushcfunction(L, lax ? iter_auxlax : iter_auxstrict);
|
|
||||||
lua_pushvalue(L, 1);
|
lua_pushvalue(L, 1);
|
||||||
lua_pushinteger(L, 0);
|
lua_pushinteger(L, 0);
|
||||||
return 3;
|
return 3;
|
||||||
@@ -267,7 +232,7 @@ static int iter_codes (lua_State *L) {
|
|||||||
|
|
||||||
|
|
||||||
/* pattern to match a single UTF-8 character */
|
/* pattern to match a single UTF-8 character */
|
||||||
#define UTF8PATT "[\0-\x7F\xC2-\xFD][\x80-\xBF]*"
|
#define UTF8PATT "[\0-\x7F\xC2-\xF4][\x80-\xBF]*"
|
||||||
|
|
||||||
|
|
||||||
static const luaL_Reg funcs[] = {
|
static const luaL_Reg funcs[] = {
|
||||||
|
|||||||
@@ -1,5 +1,5 @@
|
|||||||
/*
|
/*
|
||||||
** $Id: lvm.h $
|
** $Id: lvm.h,v 2.41.1.1 2017/04/19 17:20:42 roberto Exp $
|
||||||
** Lua virtual machine
|
** Lua virtual machine
|
||||||
** See Copyright Notice in lua.h
|
** See Copyright Notice in lua.h
|
||||||
*/
|
*/
|
||||||
@@ -33,42 +33,15 @@
|
|||||||
** integral values)
|
** integral values)
|
||||||
*/
|
*/
|
||||||
#if !defined(LUA_FLOORN2I)
|
#if !defined(LUA_FLOORN2I)
|
||||||
#define LUA_FLOORN2I F2Ieq
|
#define LUA_FLOORN2I 0
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
|
||||||
/*
|
|
||||||
** Rounding modes for float->integer coercion
|
|
||||||
*/
|
|
||||||
typedef enum {
|
|
||||||
F2Ieq, /* no rounding; accepts only integral values */
|
|
||||||
F2Ifloor, /* takes the floor of the number */
|
|
||||||
F2Iceil /* takes the ceiling of the number */
|
|
||||||
} F2Imod;
|
|
||||||
|
|
||||||
|
|
||||||
/* convert an object to a float (including string coercion) */
|
|
||||||
#define tonumber(o,n) \
|
#define tonumber(o,n) \
|
||||||
(ttisfloat(o) ? (*(n) = fltvalue(o), 1) : luaV_tonumber_(o,n))
|
(ttisfloat(o) ? (*(n) = fltvalue(o), 1) : luaV_tonumber_(o,n))
|
||||||
|
|
||||||
|
|
||||||
/* convert an object to a float (without string coercion) */
|
|
||||||
#define tonumberns(o,n) \
|
|
||||||
(ttisfloat(o) ? ((n) = fltvalue(o), 1) : \
|
|
||||||
(ttisinteger(o) ? ((n) = cast_num(ivalue(o)), 1) : 0))
|
|
||||||
|
|
||||||
|
|
||||||
/* convert an object to an integer (including string coercion) */
|
|
||||||
#define tointeger(o,i) \
|
#define tointeger(o,i) \
|
||||||
(l_likely(ttisinteger(o)) ? (*(i) = ivalue(o), 1) \
|
(ttisinteger(o) ? (*(i) = ivalue(o), 1) : luaV_tointeger(o,i,LUA_FLOORN2I))
|
||||||
: luaV_tointeger(o,i,LUA_FLOORN2I))
|
|
||||||
|
|
||||||
|
|
||||||
/* convert an object to an integer (without string coercion) */
|
|
||||||
#define tointegerns(o,i) \
|
|
||||||
(l_likely(ttisinteger(o)) ? (*(i) = ivalue(o), 1) \
|
|
||||||
: luaV_tointegerns(o,i,LUA_FLOORN2I))
|
|
||||||
|
|
||||||
|
|
||||||
#define intop(op,v1,v2) l_castU2S(l_castS2U(v1) op l_castS2U(v2))
|
#define intop(op,v1,v2) l_castU2S(l_castS2U(v1) op l_castS2U(v2))
|
||||||
|
|
||||||
@@ -76,39 +49,47 @@ typedef enum {
|
|||||||
|
|
||||||
|
|
||||||
/*
|
/*
|
||||||
** fast track for 'gettable'
|
** fast track for 'gettable': if 't' is a table and 't[k]' is not nil,
|
||||||
|
** return 1 with 'slot' pointing to 't[k]' (final result). Otherwise,
|
||||||
|
** return 0 (meaning it will have to check metamethod) with 'slot'
|
||||||
|
** pointing to a nil 't[k]' (if 't' is a table) or NULL (otherwise).
|
||||||
|
** 'f' is the raw get function to use.
|
||||||
*/
|
*/
|
||||||
#define luaV_fastget(t,k,res,f, tag) \
|
#define luaV_fastget(L,t,k,slot,f) \
|
||||||
(tag = (!ttistable(t) ? LUA_VNOTABLE : f(hvalue(t), k, res)))
|
(!ttistable(t) \
|
||||||
|
? (slot = NULL, 0) /* not a table; 'slot' is NULL and result is 0 */ \
|
||||||
|
: (slot = f(hvalue(t), k), /* else, do raw access */ \
|
||||||
|
!ttisnil(slot))) /* result not nil? */
|
||||||
|
|
||||||
|
/*
|
||||||
|
** standard implementation for 'gettable'
|
||||||
|
*/
|
||||||
|
#define luaV_gettable(L,t,k,v) { const TValue *slot; \
|
||||||
|
if (luaV_fastget(L,t,k,slot,luaH_get)) { setobj2s(L, v, slot); } \
|
||||||
|
else luaV_finishget(L,t,k,v,slot); }
|
||||||
|
|
||||||
|
|
||||||
/*
|
/*
|
||||||
** Special case of 'luaV_fastget' for integers, inlining the fast case
|
** Fast track for set table. If 't' is a table and 't[k]' is not nil,
|
||||||
** of 'luaH_getint'.
|
** call GC barrier, do a raw 't[k]=v', and return true; otherwise,
|
||||||
|
** return false with 'slot' equal to NULL (if 't' is not a table) or
|
||||||
|
** 'nil'. (This is needed by 'luaV_finishget'.) Note that, if the macro
|
||||||
|
** returns true, there is no need to 'invalidateTMcache', because the
|
||||||
|
** call is not creating a new entry.
|
||||||
*/
|
*/
|
||||||
#define luaV_fastgeti(t,k,res,tag) \
|
#define luaV_fastset(L,t,k,slot,f,v) \
|
||||||
if (!ttistable(t)) tag = LUA_VNOTABLE; \
|
(!ttistable(t) \
|
||||||
else { luaH_fastgeti(hvalue(t), k, res, tag); }
|
? (slot = NULL, 0) \
|
||||||
|
: (slot = f(hvalue(t), k), \
|
||||||
|
ttisnil(slot) ? 0 \
|
||||||
|
: (luaC_barrierback(L, hvalue(t), v), \
|
||||||
|
setobj2t(L, cast(TValue *,slot), v), \
|
||||||
|
1)))
|
||||||
|
|
||||||
|
|
||||||
#define luaV_fastset(t,k,val,hres,f) \
|
#define luaV_settable(L,t,k,v) { const TValue *slot; \
|
||||||
(hres = (!ttistable(t) ? HNOTATABLE : f(hvalue(t), k, val)))
|
if (!luaV_fastset(L,t,k,slot,luaH_get,v)) \
|
||||||
|
luaV_finishset(L,t,k,v,slot); }
|
||||||
#define luaV_fastseti(t,k,val,hres) \
|
|
||||||
if (!ttistable(t)) hres = HNOTATABLE; \
|
|
||||||
else { luaH_fastseti(hvalue(t), k, val, hres); }
|
|
||||||
|
|
||||||
|
|
||||||
/*
|
|
||||||
** Finish a fast set operation (when fast set succeeds).
|
|
||||||
*/
|
|
||||||
#define luaV_finishfastset(L,t,v) luaC_barrierback(L, gcvalue(t), v)
|
|
||||||
|
|
||||||
|
|
||||||
/*
|
|
||||||
** Shift right is the same as shift left with a negative 'y'
|
|
||||||
*/
|
|
||||||
#define luaV_shiftr(x,y) luaV_shiftl(x,intop(-, 0, y))
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
@@ -116,20 +97,16 @@ LUAI_FUNC int luaV_equalobj (lua_State *L, const TValue *t1, const TValue *t2);
|
|||||||
LUAI_FUNC int luaV_lessthan (lua_State *L, const TValue *l, const TValue *r);
|
LUAI_FUNC int luaV_lessthan (lua_State *L, const TValue *l, const TValue *r);
|
||||||
LUAI_FUNC int luaV_lessequal (lua_State *L, const TValue *l, const TValue *r);
|
LUAI_FUNC int luaV_lessequal (lua_State *L, const TValue *l, const TValue *r);
|
||||||
LUAI_FUNC int luaV_tonumber_ (const TValue *obj, lua_Number *n);
|
LUAI_FUNC int luaV_tonumber_ (const TValue *obj, lua_Number *n);
|
||||||
LUAI_FUNC int luaV_tointeger (const TValue *obj, lua_Integer *p, F2Imod mode);
|
LUAI_FUNC int luaV_tointeger (const TValue *obj, lua_Integer *p, int mode);
|
||||||
LUAI_FUNC int luaV_tointegerns (const TValue *obj, lua_Integer *p,
|
LUAI_FUNC void luaV_finishget (lua_State *L, const TValue *t, TValue *key,
|
||||||
F2Imod mode);
|
StkId val, const TValue *slot);
|
||||||
LUAI_FUNC int luaV_flttointeger (lua_Number n, lua_Integer *p, F2Imod mode);
|
|
||||||
LUAI_FUNC lu_byte luaV_finishget (lua_State *L, const TValue *t, TValue *key,
|
|
||||||
StkId val, lu_byte tag);
|
|
||||||
LUAI_FUNC void luaV_finishset (lua_State *L, const TValue *t, TValue *key,
|
LUAI_FUNC void luaV_finishset (lua_State *L, const TValue *t, TValue *key,
|
||||||
TValue *val, int aux);
|
StkId val, const TValue *slot);
|
||||||
LUAI_FUNC void luaV_finishOp (lua_State *L);
|
LUAI_FUNC void luaV_finishOp (lua_State *L);
|
||||||
LUAI_FUNC void luaV_execute (lua_State *L, CallInfo *ci);
|
LUAI_FUNC void luaV_execute (lua_State *L);
|
||||||
LUAI_FUNC void luaV_concat (lua_State *L, int total);
|
LUAI_FUNC void luaV_concat (lua_State *L, int total);
|
||||||
LUAI_FUNC lua_Integer luaV_idiv (lua_State *L, lua_Integer x, lua_Integer y);
|
LUAI_FUNC lua_Integer luaV_div (lua_State *L, lua_Integer x, lua_Integer y);
|
||||||
LUAI_FUNC lua_Integer luaV_mod (lua_State *L, lua_Integer x, lua_Integer y);
|
LUAI_FUNC lua_Integer luaV_mod (lua_State *L, lua_Integer x, lua_Integer y);
|
||||||
LUAI_FUNC lua_Number luaV_modf (lua_State *L, lua_Number x, lua_Number y);
|
|
||||||
LUAI_FUNC lua_Integer luaV_shiftl (lua_Integer x, lua_Integer y);
|
LUAI_FUNC lua_Integer luaV_shiftl (lua_Integer x, lua_Integer y);
|
||||||
LUAI_FUNC void luaV_objlen (lua_State *L, StkId ra, const TValue *rb);
|
LUAI_FUNC void luaV_objlen (lua_State *L, StkId ra, const TValue *rb);
|
||||||
|
|
||||||
|
|||||||
@@ -1,5 +1,5 @@
|
|||||||
/*
|
/*
|
||||||
** $Id: lzio.c $
|
** $Id: lzio.c,v 1.37.1.1 2017/04/19 17:20:42 roberto Exp $
|
||||||
** Buffered streams
|
** Buffered streams
|
||||||
** See Copyright Notice in lua.h
|
** See Copyright Notice in lua.h
|
||||||
*/
|
*/
|
||||||
@@ -14,7 +14,6 @@
|
|||||||
|
|
||||||
#include "lua.h"
|
#include "lua.h"
|
||||||
|
|
||||||
#include "lapi.h"
|
|
||||||
#include "llimits.h"
|
#include "llimits.h"
|
||||||
#include "lmem.h"
|
#include "lmem.h"
|
||||||
#include "lstate.h"
|
#include "lstate.h"
|
||||||
@@ -46,25 +45,17 @@ void luaZ_init (lua_State *L, ZIO *z, lua_Reader reader, void *data) {
|
|||||||
|
|
||||||
|
|
||||||
/* --------------------------------------------------------------- read --- */
|
/* --------------------------------------------------------------- read --- */
|
||||||
|
|
||||||
static int checkbuffer (ZIO *z) {
|
|
||||||
if (z->n == 0) { /* no bytes in buffer? */
|
|
||||||
if (luaZ_fill(z) == EOZ) /* try to read more */
|
|
||||||
return 0; /* no more input */
|
|
||||||
else {
|
|
||||||
z->n++; /* luaZ_fill consumed first byte; put it back */
|
|
||||||
z->p--;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return 1; /* now buffer has something */
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
size_t luaZ_read (ZIO *z, void *b, size_t n) {
|
size_t luaZ_read (ZIO *z, void *b, size_t n) {
|
||||||
while (n) {
|
while (n) {
|
||||||
size_t m;
|
size_t m;
|
||||||
if (!checkbuffer(z))
|
if (z->n == 0) { /* no bytes in buffer? */
|
||||||
return n; /* no more input; return number of missing bytes */
|
if (luaZ_fill(z) == EOZ) /* try to read more */
|
||||||
|
return n; /* no more input; return number of missing bytes */
|
||||||
|
else {
|
||||||
|
z->n++; /* luaZ_fill consumed first byte; put it back */
|
||||||
|
z->p--;
|
||||||
|
}
|
||||||
|
}
|
||||||
m = (n <= z->n) ? n : z->n; /* min. between n and z->n */
|
m = (n <= z->n) ? n : z->n; /* min. between n and z->n */
|
||||||
memcpy(b, z->p, m);
|
memcpy(b, z->p, m);
|
||||||
z->n -= m;
|
z->n -= m;
|
||||||
@@ -75,15 +66,3 @@ size_t luaZ_read (ZIO *z, void *b, size_t n) {
|
|||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
const void *luaZ_getaddr (ZIO* z, size_t n) {
|
|
||||||
const void *res;
|
|
||||||
if (!checkbuffer(z))
|
|
||||||
return NULL; /* no more input */
|
|
||||||
if (z->n < n) /* not enough bytes? */
|
|
||||||
return NULL; /* block not whole; cannot give an address */
|
|
||||||
res = z->p; /* get block address */
|
|
||||||
z->n -= n; /* consume these bytes */
|
|
||||||
z->p += n;
|
|
||||||
return res;
|
|
||||||
}
|
|
||||||
|
|||||||
@@ -1,5 +1,5 @@
|
|||||||
/*
|
/*
|
||||||
** $Id: lzio.h $
|
** $Id: lzio.h,v 1.31.1.1 2017/04/19 17:20:42 roberto Exp $
|
||||||
** Buffered streams
|
** Buffered streams
|
||||||
** See Copyright Notice in lua.h
|
** See Copyright Notice in lua.h
|
||||||
*/
|
*/
|
||||||
@@ -32,7 +32,7 @@ typedef struct Mbuffer {
|
|||||||
#define luaZ_sizebuffer(buff) ((buff)->buffsize)
|
#define luaZ_sizebuffer(buff) ((buff)->buffsize)
|
||||||
#define luaZ_bufflen(buff) ((buff)->n)
|
#define luaZ_bufflen(buff) ((buff)->n)
|
||||||
|
|
||||||
#define luaZ_buffremove(buff,i) ((buff)->n -= cast_sizet(i))
|
#define luaZ_buffremove(buff,i) ((buff)->n -= (i))
|
||||||
#define luaZ_resetbuffer(buff) ((buff)->n = 0)
|
#define luaZ_resetbuffer(buff) ((buff)->n = 0)
|
||||||
|
|
||||||
|
|
||||||
@@ -48,7 +48,6 @@ LUAI_FUNC void luaZ_init (lua_State *L, ZIO *z, lua_Reader reader,
|
|||||||
void *data);
|
void *data);
|
||||||
LUAI_FUNC size_t luaZ_read (ZIO* z, void *b, size_t n); /* read next n bytes */
|
LUAI_FUNC size_t luaZ_read (ZIO* z, void *b, size_t n); /* read next n bytes */
|
||||||
|
|
||||||
LUAI_FUNC const void *luaZ_getaddr (ZIO* z, size_t n);
|
|
||||||
|
|
||||||
|
|
||||||
/* --------- Private Part ------------------ */
|
/* --------- Private Part ------------------ */
|
||||||
|
|||||||
@@ -1,35 +1,31 @@
|
|||||||
# Developer's makefile for building Lua
|
# makefile for building Lua
|
||||||
# see luaconf.h for further customization
|
# see INSTALL for installation instructions
|
||||||
|
# see ../Makefile and luaconf.h for further customization
|
||||||
|
|
||||||
# == CHANGE THE SETTINGS BELOW TO SUIT YOUR ENVIRONMENT =======================
|
# == CHANGE THE SETTINGS BELOW TO SUIT YOUR ENVIRONMENT =======================
|
||||||
|
|
||||||
# Warnings valid for both C and C++
|
# Warnings valid for both C and C++
|
||||||
CWARNSCPP= \
|
CWARNSCPP= \
|
||||||
-Wfatal-errors \
|
-pedantic \
|
||||||
-Wextra \
|
-Wextra \
|
||||||
-Wshadow \
|
-Wshadow \
|
||||||
|
-Wsign-compare \
|
||||||
-Wundef \
|
-Wundef \
|
||||||
-Wwrite-strings \
|
-Wwrite-strings \
|
||||||
-Wredundant-decls \
|
-Wredundant-decls \
|
||||||
-Wdisabled-optimization \
|
-Wdisabled-optimization \
|
||||||
|
-Waggregate-return \
|
||||||
-Wdouble-promotion \
|
-Wdouble-promotion \
|
||||||
-Wmissing-declarations \
|
#-Wno-aggressive-loop-optimizations # not accepted by clang \
|
||||||
-Wconversion \
|
#-Wlogical-op # not accepted by clang \
|
||||||
# the next warnings might be useful sometimes,
|
# the next warnings generate too much noise, so they are disabled
|
||||||
# but usually they generate too much noise
|
# -Wconversion -Wno-sign-conversion \
|
||||||
|
# -Wsign-conversion \
|
||||||
|
# -Wconversion \
|
||||||
# -Wstrict-overflow=2 \
|
# -Wstrict-overflow=2 \
|
||||||
# -Werror \
|
|
||||||
# -pedantic # warns if we use jump tables \
|
|
||||||
# -Wformat=2 \
|
# -Wformat=2 \
|
||||||
# -Wcast-qual \
|
# -Wcast-qual \
|
||||||
|
|
||||||
|
|
||||||
# Warnings for gcc, not valid for clang
|
|
||||||
CWARNGCC= \
|
|
||||||
-Wlogical-op \
|
|
||||||
-Wno-aggressive-loop-optimizations \
|
|
||||||
|
|
||||||
|
|
||||||
# The next warnings are neither valid nor needed for C++
|
# The next warnings are neither valid nor needed for C++
|
||||||
CWARNSC= -Wdeclaration-after-statement \
|
CWARNSC= -Wdeclaration-after-statement \
|
||||||
-Wmissing-prototypes \
|
-Wmissing-prototypes \
|
||||||
@@ -39,46 +35,32 @@ CWARNSC= -Wdeclaration-after-statement \
|
|||||||
-Wold-style-definition \
|
-Wold-style-definition \
|
||||||
|
|
||||||
|
|
||||||
CWARNS= $(CWARNSCPP) $(CWARNSC) $(CWARNGCC)
|
CWARNS= $(CWARNSCPP) $(CWARNSC)
|
||||||
|
|
||||||
# Some useful compiler options for internal tests:
|
|
||||||
# -DLUAI_ASSERT turns on all assertions inside Lua.
|
|
||||||
# -DHARDSTACKTESTS forces a reallocation of the stack at every point where
|
|
||||||
# the stack can be reallocated.
|
|
||||||
# -DHARDMEMTESTS forces a full collection at all points where the collector
|
|
||||||
# can run.
|
|
||||||
# -DEMERGENCYGCTESTS forces an emergency collection at every single allocation.
|
|
||||||
# -DEXTERNMEMCHECK removes internal consistency checking of blocks being
|
|
||||||
# deallocated (useful when an external tool like valgrind does the check).
|
|
||||||
# -DMAXINDEXRK=k limits range of constants in RK instruction operands.
|
|
||||||
# -DLUA_COMPAT_5_3
|
|
||||||
|
|
||||||
|
# -DEXTERNMEMCHECK -DHARDSTACKTESTS -DHARDMEMTESTS -DTRACEMEM='"tempmem"'
|
||||||
|
# -g -DLUA_USER_H='"ltests.h"'
|
||||||
# -pg -malign-double
|
# -pg -malign-double
|
||||||
# -DLUA_USE_CTYPE -DLUA_USE_APICHECK
|
# -DLUA_USE_CTYPE -DLUA_USE_APICHECK
|
||||||
|
# (in clang, '-ftrapv' for runtime checks of integer overflows)
|
||||||
|
# -fsanitize=undefined -ftrapv
|
||||||
|
TESTS= -DLUA_USER_H='"ltests.h"'
|
||||||
|
|
||||||
# The following options help detect "undefined behavior"s that seldom
|
# -mtune=native -fomit-frame-pointer
|
||||||
# create problems; some are only available in newer gcc versions. To
|
# -fno-stack-protector
|
||||||
# use some of them, we also have to define an environment variable
|
LOCAL = $(TESTS) $(CWARNS) -g
|
||||||
# ASAN_OPTIONS="detect_invalid_pointer_pairs=2".
|
|
||||||
# -fsanitize=undefined
|
|
||||||
# -fsanitize=pointer-subtract -fsanitize=address -fsanitize=pointer-compare
|
|
||||||
# TESTS= -DLUA_USER_H='"ltests.h"' -Og -g
|
|
||||||
|
|
||||||
|
|
||||||
LOCAL = $(TESTS) $(CWARNS)
|
|
||||||
|
# enable Linux goodies
|
||||||
|
MYCFLAGS= $(LOCAL) -std=c99 -DLUA_USE_LINUX -DLUA_COMPAT_5_2
|
||||||
|
MYLDFLAGS= $(LOCAL) -Wl,-E
|
||||||
|
MYLIBS= -ldl -lreadline
|
||||||
|
|
||||||
|
|
||||||
# To enable Linux goodies, -DLUA_USE_LINUX
|
CC= clang-3.8
|
||||||
# For C89, "-std=c89 -DLUA_USE_C89"
|
CFLAGS= -Wall -O2 $(MYCFLAGS)
|
||||||
# Note that Linux/Posix options are not compatible with C89
|
AR= ar rcu
|
||||||
MYCFLAGS= $(LOCAL) -std=c99 -DLUA_USE_LINUX
|
|
||||||
MYLDFLAGS= -Wl,-E
|
|
||||||
MYLIBS= -ldl
|
|
||||||
|
|
||||||
|
|
||||||
CC= gcc
|
|
||||||
CFLAGS= -Wall -O2 $(MYCFLAGS) -fno-stack-protector -fno-common -march=native
|
|
||||||
AR= ar rc
|
|
||||||
RANLIB= ranlib
|
RANLIB= ranlib
|
||||||
RM= rm -f
|
RM= rm -f
|
||||||
|
|
||||||
@@ -95,18 +77,19 @@ CORE_O= lapi.o lcode.o lctype.o ldebug.o ldo.o ldump.o lfunc.o lgc.o llex.o \
|
|||||||
ltm.o lundump.o lvm.o lzio.o ltests.o
|
ltm.o lundump.o lvm.o lzio.o ltests.o
|
||||||
AUX_O= lauxlib.o
|
AUX_O= lauxlib.o
|
||||||
LIB_O= lbaselib.o ldblib.o liolib.o lmathlib.o loslib.o ltablib.o lstrlib.o \
|
LIB_O= lbaselib.o ldblib.o liolib.o lmathlib.o loslib.o ltablib.o lstrlib.o \
|
||||||
lutf8lib.o loadlib.o lcorolib.o linit.o
|
lutf8lib.o lbitlib.o loadlib.o lcorolib.o linit.o
|
||||||
|
|
||||||
LUA_T= lua
|
LUA_T= lua
|
||||||
LUA_O= lua.o
|
LUA_O= lua.o
|
||||||
|
|
||||||
|
# LUAC_T= luac
|
||||||
|
# LUAC_O= luac.o print.o
|
||||||
|
|
||||||
ALL_T= $(CORE_T) $(LUA_T)
|
ALL_T= $(CORE_T) $(LUA_T) $(LUAC_T)
|
||||||
ALL_O= $(CORE_O) $(LUA_O) $(AUX_O) $(LIB_O)
|
ALL_O= $(CORE_O) $(LUA_O) $(LUAC_O) $(AUX_O) $(LIB_O)
|
||||||
ALL_A= $(CORE_T)
|
ALL_A= $(CORE_T)
|
||||||
|
|
||||||
all: $(ALL_T)
|
all: $(ALL_T)
|
||||||
touch all
|
|
||||||
|
|
||||||
o: $(ALL_O)
|
o: $(ALL_O)
|
||||||
|
|
||||||
@@ -119,8 +102,11 @@ $(CORE_T): $(CORE_O) $(AUX_O) $(LIB_O)
|
|||||||
$(LUA_T): $(LUA_O) $(CORE_T)
|
$(LUA_T): $(LUA_O) $(CORE_T)
|
||||||
$(CC) -o $@ $(MYLDFLAGS) $(LUA_O) $(CORE_T) $(LIBS) $(MYLIBS) $(DL)
|
$(CC) -o $@ $(MYLDFLAGS) $(LUA_O) $(CORE_T) $(LIBS) $(MYLIBS) $(DL)
|
||||||
|
|
||||||
|
$(LUAC_T): $(LUAC_O) $(CORE_T)
|
||||||
|
$(CC) -o $@ $(MYLDFLAGS) $(LUAC_O) $(CORE_T) $(LIBS) $(MYLIBS)
|
||||||
|
|
||||||
clean:
|
clean:
|
||||||
|
rcsclean -u
|
||||||
$(RM) $(ALL_T) $(ALL_O)
|
$(RM) $(ALL_T) $(ALL_O)
|
||||||
|
|
||||||
depend:
|
depend:
|
||||||
@@ -137,7 +123,7 @@ echo:
|
|||||||
@echo "MYLIBS = $(MYLIBS)"
|
@echo "MYLIBS = $(MYLIBS)"
|
||||||
@echo "DL = $(DL)"
|
@echo "DL = $(DL)"
|
||||||
|
|
||||||
$(ALL_O): makefile ltests.h
|
$(ALL_O): makefile
|
||||||
|
|
||||||
# DO NOT EDIT
|
# DO NOT EDIT
|
||||||
# automatically made with 'gcc -MM l*.c'
|
# automatically made with 'gcc -MM l*.c'
|
||||||
@@ -145,45 +131,41 @@ $(ALL_O): makefile ltests.h
|
|||||||
lapi.o: lapi.c lprefix.h lua.h luaconf.h lapi.h llimits.h lstate.h \
|
lapi.o: lapi.c lprefix.h lua.h luaconf.h lapi.h llimits.h lstate.h \
|
||||||
lobject.h ltm.h lzio.h lmem.h ldebug.h ldo.h lfunc.h lgc.h lstring.h \
|
lobject.h ltm.h lzio.h lmem.h ldebug.h ldo.h lfunc.h lgc.h lstring.h \
|
||||||
ltable.h lundump.h lvm.h
|
ltable.h lundump.h lvm.h
|
||||||
lauxlib.o: lauxlib.c lprefix.h lua.h luaconf.h lauxlib.h llimits.h
|
lauxlib.o: lauxlib.c lprefix.h lua.h luaconf.h lauxlib.h
|
||||||
lbaselib.o: lbaselib.c lprefix.h lua.h luaconf.h lauxlib.h lualib.h \
|
lbaselib.o: lbaselib.c lprefix.h lua.h luaconf.h lauxlib.h lualib.h
|
||||||
llimits.h
|
lbitlib.o: lbitlib.c lprefix.h lua.h luaconf.h lauxlib.h lualib.h
|
||||||
lcode.o: lcode.c lprefix.h lua.h luaconf.h lcode.h llex.h lobject.h \
|
lcode.o: lcode.c lprefix.h lua.h luaconf.h lcode.h llex.h lobject.h \
|
||||||
llimits.h lzio.h lmem.h lopcodes.h lparser.h ldebug.h lstate.h ltm.h \
|
llimits.h lzio.h lmem.h lopcodes.h lparser.h ldebug.h lstate.h ltm.h \
|
||||||
ldo.h lgc.h lstring.h ltable.h lvm.h lopnames.h
|
ldo.h lgc.h lstring.h ltable.h lvm.h
|
||||||
lcorolib.o: lcorolib.c lprefix.h lua.h luaconf.h lauxlib.h lualib.h \
|
lcorolib.o: lcorolib.c lprefix.h lua.h luaconf.h lauxlib.h lualib.h
|
||||||
llimits.h
|
|
||||||
lctype.o: lctype.c lprefix.h lctype.h lua.h luaconf.h llimits.h
|
lctype.o: lctype.c lprefix.h lctype.h lua.h luaconf.h llimits.h
|
||||||
ldblib.o: ldblib.c lprefix.h lua.h luaconf.h lauxlib.h lualib.h llimits.h
|
ldblib.o: ldblib.c lprefix.h lua.h luaconf.h lauxlib.h lualib.h
|
||||||
ldebug.o: ldebug.c lprefix.h lua.h luaconf.h lapi.h llimits.h lstate.h \
|
ldebug.o: ldebug.c lprefix.h lua.h luaconf.h lapi.h llimits.h lstate.h \
|
||||||
lobject.h ltm.h lzio.h lmem.h lcode.h llex.h lopcodes.h lparser.h \
|
lobject.h ltm.h lzio.h lmem.h lcode.h llex.h lopcodes.h lparser.h \
|
||||||
ldebug.h ldo.h lfunc.h lstring.h lgc.h ltable.h lvm.h
|
ldebug.h ldo.h lfunc.h lstring.h lgc.h ltable.h lvm.h
|
||||||
ldo.o: ldo.c lprefix.h lua.h luaconf.h lapi.h llimits.h lstate.h \
|
ldo.o: ldo.c lprefix.h lua.h luaconf.h lapi.h llimits.h lstate.h \
|
||||||
lobject.h ltm.h lzio.h lmem.h ldebug.h ldo.h lfunc.h lgc.h lopcodes.h \
|
lobject.h ltm.h lzio.h lmem.h ldebug.h ldo.h lfunc.h lgc.h lopcodes.h \
|
||||||
lparser.h lstring.h ltable.h lundump.h lvm.h
|
lparser.h lstring.h ltable.h lundump.h lvm.h
|
||||||
ldump.o: ldump.c lprefix.h lua.h luaconf.h lapi.h llimits.h lstate.h \
|
ldump.o: ldump.c lprefix.h lua.h luaconf.h lobject.h llimits.h lstate.h \
|
||||||
lobject.h ltm.h lzio.h lmem.h lgc.h ltable.h lundump.h
|
ltm.h lzio.h lmem.h lundump.h
|
||||||
lfunc.o: lfunc.c lprefix.h lua.h luaconf.h ldebug.h lstate.h lobject.h \
|
lfunc.o: lfunc.c lprefix.h lua.h luaconf.h lfunc.h lobject.h llimits.h \
|
||||||
llimits.h ltm.h lzio.h lmem.h ldo.h lfunc.h lgc.h
|
lgc.h lstate.h ltm.h lzio.h lmem.h
|
||||||
lgc.o: lgc.c lprefix.h lua.h luaconf.h ldebug.h lstate.h lobject.h \
|
lgc.o: lgc.c lprefix.h lua.h luaconf.h ldebug.h lstate.h lobject.h \
|
||||||
llimits.h ltm.h lzio.h lmem.h ldo.h lfunc.h lgc.h lstring.h ltable.h
|
llimits.h ltm.h lzio.h lmem.h ldo.h lfunc.h lgc.h lstring.h ltable.h
|
||||||
linit.o: linit.c lprefix.h lua.h luaconf.h lualib.h lauxlib.h llimits.h
|
linit.o: linit.c lprefix.h lua.h luaconf.h lualib.h lauxlib.h
|
||||||
liolib.o: liolib.c lprefix.h lua.h luaconf.h lauxlib.h lualib.h llimits.h
|
liolib.o: liolib.c lprefix.h lua.h luaconf.h lauxlib.h lualib.h
|
||||||
llex.o: llex.c lprefix.h lua.h luaconf.h lctype.h llimits.h ldebug.h \
|
llex.o: llex.c lprefix.h lua.h luaconf.h lctype.h llimits.h ldebug.h \
|
||||||
lstate.h lobject.h ltm.h lzio.h lmem.h ldo.h lgc.h llex.h lparser.h \
|
lstate.h lobject.h ltm.h lzio.h lmem.h ldo.h lgc.h llex.h lparser.h \
|
||||||
lstring.h ltable.h
|
lstring.h ltable.h
|
||||||
lmathlib.o: lmathlib.c lprefix.h lua.h luaconf.h lauxlib.h lualib.h \
|
lmathlib.o: lmathlib.c lprefix.h lua.h luaconf.h lauxlib.h lualib.h
|
||||||
llimits.h
|
|
||||||
lmem.o: lmem.c lprefix.h lua.h luaconf.h ldebug.h lstate.h lobject.h \
|
lmem.o: lmem.c lprefix.h lua.h luaconf.h ldebug.h lstate.h lobject.h \
|
||||||
llimits.h ltm.h lzio.h lmem.h ldo.h lgc.h
|
llimits.h ltm.h lzio.h lmem.h ldo.h lgc.h
|
||||||
loadlib.o: loadlib.c lprefix.h lua.h luaconf.h lauxlib.h lualib.h \
|
loadlib.o: loadlib.c lprefix.h lua.h luaconf.h lauxlib.h lualib.h
|
||||||
llimits.h
|
|
||||||
lobject.o: lobject.c lprefix.h lua.h luaconf.h lctype.h llimits.h \
|
lobject.o: lobject.c lprefix.h lua.h luaconf.h lctype.h llimits.h \
|
||||||
ldebug.h lstate.h lobject.h ltm.h lzio.h lmem.h ldo.h lstring.h lgc.h \
|
ldebug.h lstate.h lobject.h ltm.h lzio.h lmem.h ldo.h lstring.h lgc.h \
|
||||||
lvm.h
|
lvm.h
|
||||||
lopcodes.o: lopcodes.c lprefix.h lopcodes.h llimits.h lua.h luaconf.h \
|
lopcodes.o: lopcodes.c lprefix.h lopcodes.h llimits.h lua.h luaconf.h
|
||||||
lobject.h
|
loslib.o: loslib.c lprefix.h lua.h luaconf.h lauxlib.h lualib.h
|
||||||
loslib.o: loslib.c lprefix.h lua.h luaconf.h lauxlib.h lualib.h llimits.h
|
|
||||||
lparser.o: lparser.c lprefix.h lua.h luaconf.h lcode.h llex.h lobject.h \
|
lparser.o: lparser.c lprefix.h lua.h luaconf.h lcode.h llex.h lobject.h \
|
||||||
llimits.h lzio.h lmem.h lopcodes.h lparser.h ldebug.h lstate.h ltm.h \
|
llimits.h lzio.h lmem.h lopcodes.h lparser.h ldebug.h lstate.h ltm.h \
|
||||||
ldo.h lfunc.h lstring.h lgc.h ltable.h
|
ldo.h lfunc.h lstring.h lgc.h ltable.h
|
||||||
@@ -192,28 +174,25 @@ lstate.o: lstate.c lprefix.h lua.h luaconf.h lapi.h llimits.h lstate.h \
|
|||||||
lstring.h ltable.h
|
lstring.h ltable.h
|
||||||
lstring.o: lstring.c lprefix.h lua.h luaconf.h ldebug.h lstate.h \
|
lstring.o: lstring.c lprefix.h lua.h luaconf.h ldebug.h lstate.h \
|
||||||
lobject.h llimits.h ltm.h lzio.h lmem.h ldo.h lstring.h lgc.h
|
lobject.h llimits.h ltm.h lzio.h lmem.h ldo.h lstring.h lgc.h
|
||||||
lstrlib.o: lstrlib.c lprefix.h lua.h luaconf.h lauxlib.h lualib.h \
|
lstrlib.o: lstrlib.c lprefix.h lua.h luaconf.h lauxlib.h lualib.h
|
||||||
llimits.h
|
|
||||||
ltable.o: ltable.c lprefix.h lua.h luaconf.h ldebug.h lstate.h lobject.h \
|
ltable.o: ltable.c lprefix.h lua.h luaconf.h ldebug.h lstate.h lobject.h \
|
||||||
llimits.h ltm.h lzio.h lmem.h ldo.h lgc.h lstring.h ltable.h lvm.h
|
llimits.h ltm.h lzio.h lmem.h ldo.h lgc.h lstring.h ltable.h lvm.h
|
||||||
ltablib.o: ltablib.c lprefix.h lua.h luaconf.h lauxlib.h lualib.h \
|
ltablib.o: ltablib.c lprefix.h lua.h luaconf.h lauxlib.h lualib.h
|
||||||
llimits.h
|
|
||||||
ltests.o: ltests.c lprefix.h lua.h luaconf.h lapi.h llimits.h lstate.h \
|
ltests.o: ltests.c lprefix.h lua.h luaconf.h lapi.h llimits.h lstate.h \
|
||||||
lobject.h ltm.h lzio.h lmem.h lauxlib.h lcode.h llex.h lopcodes.h \
|
lobject.h ltm.h lzio.h lmem.h lauxlib.h lcode.h llex.h lopcodes.h \
|
||||||
lparser.h lctype.h ldebug.h ldo.h lfunc.h lopnames.h lstring.h lgc.h \
|
lparser.h lctype.h ldebug.h ldo.h lfunc.h lstring.h lgc.h ltable.h \
|
||||||
ltable.h lualib.h
|
lualib.h
|
||||||
ltm.o: ltm.c lprefix.h lua.h luaconf.h ldebug.h lstate.h lobject.h \
|
ltm.o: ltm.c lprefix.h lua.h luaconf.h ldebug.h lstate.h lobject.h \
|
||||||
llimits.h ltm.h lzio.h lmem.h ldo.h lgc.h lstring.h ltable.h lvm.h
|
llimits.h ltm.h lzio.h lmem.h ldo.h lstring.h lgc.h ltable.h lvm.h
|
||||||
lua.o: lua.c lprefix.h lua.h luaconf.h lauxlib.h lualib.h llimits.h
|
lua.o: lua.c lprefix.h lua.h luaconf.h lauxlib.h lualib.h
|
||||||
lundump.o: lundump.c lprefix.h lua.h luaconf.h ldebug.h lstate.h \
|
lundump.o: lundump.c lprefix.h lua.h luaconf.h ldebug.h lstate.h \
|
||||||
lobject.h llimits.h ltm.h lzio.h lmem.h ldo.h lfunc.h lstring.h lgc.h \
|
lobject.h llimits.h ltm.h lzio.h lmem.h ldo.h lfunc.h lstring.h lgc.h \
|
||||||
ltable.h lundump.h
|
lundump.h
|
||||||
lutf8lib.o: lutf8lib.c lprefix.h lua.h luaconf.h lauxlib.h lualib.h \
|
lutf8lib.o: lutf8lib.c lprefix.h lua.h luaconf.h lauxlib.h lualib.h
|
||||||
llimits.h
|
lvm.o: lvm.c lprefix.h lua.h luaconf.h ldebug.h lstate.h lobject.h \
|
||||||
lvm.o: lvm.c lprefix.h lua.h luaconf.h lapi.h llimits.h lstate.h \
|
llimits.h ltm.h lzio.h lmem.h ldo.h lfunc.h lgc.h lopcodes.h lstring.h \
|
||||||
lobject.h ltm.h lzio.h lmem.h ldebug.h ldo.h lfunc.h lgc.h lopcodes.h \
|
ltable.h lvm.h
|
||||||
lstring.h ltable.h lvm.h ljumptab.h
|
lzio.o: lzio.c lprefix.h lua.h luaconf.h llimits.h lmem.h lstate.h \
|
||||||
lzio.o: lzio.c lprefix.h lua.h luaconf.h lapi.h llimits.h lstate.h \
|
lobject.h ltm.h lzio.h
|
||||||
lobject.h ltm.h lzio.h lmem.h
|
|
||||||
|
|
||||||
# (end of Makefile)
|
# (end of Makefile)
|
||||||
|
|||||||
+6
-7
@@ -1,4 +1,4 @@
|
|||||||
#!/usr/bin/env lua5.3
|
#!/usr/bin/env lua5.2
|
||||||
|
|
||||||
|
|
||||||
-- special marks:
|
-- special marks:
|
||||||
@@ -8,11 +8,11 @@
|
|||||||
|
|
||||||
---------------------------------------------------------------
|
---------------------------------------------------------------
|
||||||
header = [[
|
header = [[
|
||||||
<!DOCTYPE html>
|
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 3.2 Final//EN">
|
||||||
<html>
|
<html>
|
||||||
|
|
||||||
<head>
|
<head>
|
||||||
<title>Lua 5.5 Reference Manual</title>
|
<title>Lua 5.3 Reference Manual</title>
|
||||||
<meta http-equiv="Content-Type" content="text/html;charset=utf-8">
|
<meta http-equiv="Content-Type" content="text/html;charset=utf-8">
|
||||||
<link rel="stylesheet" href="lua.css">
|
<link rel="stylesheet" href="lua.css">
|
||||||
<link rel="stylesheet" href="manual.css">
|
<link rel="stylesheet" href="manual.css">
|
||||||
@@ -23,14 +23,14 @@ header = [[
|
|||||||
<hr>
|
<hr>
|
||||||
<h1>
|
<h1>
|
||||||
<a href="http://www.lua.org/home.html"><img src="logo.gif" alt="[Lua logo]" border="0"></a>
|
<a href="http://www.lua.org/home.html"><img src="logo.gif" alt="[Lua logo]" border="0"></a>
|
||||||
Lua 5.5 Reference Manual
|
Lua 5.3 Reference Manual
|
||||||
</h1>
|
</h1>
|
||||||
|
|
||||||
by Roberto Ierusalimschy, Luiz Henrique de Figueiredo, Waldemar Celes
|
by Roberto Ierusalimschy, Luiz Henrique de Figueiredo, Waldemar Celes
|
||||||
<p>
|
<p>
|
||||||
<small>
|
<small>
|
||||||
<a href="http://www.lua.org/copyright.html">Copyright</a>
|
<a href="http://www.lua.org/copyright.html">Copyright</a>
|
||||||
© 2025 Lua.org, PUC-Rio. All rights reserved.
|
© 2015 Lua.org, PUC-Rio. All rights reserved.
|
||||||
</small>
|
</small>
|
||||||
<hr>
|
<hr>
|
||||||
|
|
||||||
@@ -324,7 +324,6 @@ N = function (s) return (string.gsub(s, " ", " ")) end,
|
|||||||
NE = id, -- tag"foreignphrase",
|
NE = id, -- tag"foreignphrase",
|
||||||
num = id,
|
num = id,
|
||||||
["nil"] = fixed(Tag.b"nil"),
|
["nil"] = fixed(Tag.b"nil"),
|
||||||
fail = fixed(Tag.b"fail"),
|
|
||||||
Open = fixed"{",
|
Open = fixed"{",
|
||||||
part = section("h1", true),
|
part = section("h1", true),
|
||||||
Pat = compose(verbfixed, prepos("'", "'")),
|
Pat = compose(verbfixed, prepos("'", "'")),
|
||||||
@@ -358,7 +357,7 @@ item = function (s)
|
|||||||
local t, p = string.match(s, "^([^\n|]+)|()")
|
local t, p = string.match(s, "^([^\n|]+)|()")
|
||||||
if t then
|
if t then
|
||||||
s = string.sub(s, p)
|
s = string.sub(s, p)
|
||||||
s = Tag.b(t) ..": " .. s
|
s = Tag.b(t..": ") .. s
|
||||||
end
|
end
|
||||||
return Tag.li(fixpara(s))
|
return Tag.li(fixpara(s))
|
||||||
end,
|
end,
|
||||||
|
|||||||
+1468
-2663
File diff suppressed because it is too large
Load Diff
@@ -1,136 +0,0 @@
|
|||||||
/*
|
|
||||||
** Lua core, libraries, and interpreter in a single file.
|
|
||||||
** Compiling just this file generates a complete Lua stand-alone
|
|
||||||
** program:
|
|
||||||
**
|
|
||||||
** $ gcc -O2 -std=c99 -o lua onelua.c -lm
|
|
||||||
**
|
|
||||||
** or (for C89)
|
|
||||||
**
|
|
||||||
** $ gcc -O2 -std=c89 -DLUA_USE_C89 -o lua onelua.c -lm
|
|
||||||
**
|
|
||||||
** or (for Linux)
|
|
||||||
**
|
|
||||||
** gcc -O2 -o lua -DLUA_USE_LINUX -Wl,-E onelua.c -lm -ldl
|
|
||||||
**
|
|
||||||
*/
|
|
||||||
|
|
||||||
/* default is to build the full interpreter */
|
|
||||||
#ifndef MAKE_LIB
|
|
||||||
#ifndef MAKE_LUAC
|
|
||||||
#ifndef MAKE_LUA
|
|
||||||
#define MAKE_LUA
|
|
||||||
#endif
|
|
||||||
#endif
|
|
||||||
#endif
|
|
||||||
|
|
||||||
|
|
||||||
/*
|
|
||||||
** Choose suitable platform-specific features. Default is no
|
|
||||||
** platform-specific features. Some of these options may need extra
|
|
||||||
** libraries such as -ldl -lreadline -lncurses
|
|
||||||
*/
|
|
||||||
#if 0
|
|
||||||
#define LUA_USE_LINUX
|
|
||||||
#define LUA_USE_MACOSX
|
|
||||||
#define LUA_USE_POSIX
|
|
||||||
#endif
|
|
||||||
|
|
||||||
|
|
||||||
/*
|
|
||||||
** Other specific features
|
|
||||||
*/
|
|
||||||
#if 0
|
|
||||||
#define LUA_32BITS
|
|
||||||
#define LUA_USE_C89
|
|
||||||
#endif
|
|
||||||
|
|
||||||
|
|
||||||
/* no need to change anything below this line ----------------------------- */
|
|
||||||
|
|
||||||
#include "lprefix.h"
|
|
||||||
|
|
||||||
#include <assert.h>
|
|
||||||
#include <ctype.h>
|
|
||||||
#include <errno.h>
|
|
||||||
#include <float.h>
|
|
||||||
#include <limits.h>
|
|
||||||
#include <locale.h>
|
|
||||||
#include <math.h>
|
|
||||||
#include <setjmp.h>
|
|
||||||
#include <signal.h>
|
|
||||||
#include <stdarg.h>
|
|
||||||
#include <stddef.h>
|
|
||||||
#include <stdio.h>
|
|
||||||
#include <stdlib.h>
|
|
||||||
#include <string.h>
|
|
||||||
#include <time.h>
|
|
||||||
|
|
||||||
/* setup for luaconf.h */
|
|
||||||
#define LUA_CORE
|
|
||||||
#define LUA_LIB
|
|
||||||
|
|
||||||
#include "luaconf.h"
|
|
||||||
|
|
||||||
/* do not export internal symbols */
|
|
||||||
#undef LUAI_FUNC
|
|
||||||
#undef LUAI_DDEC
|
|
||||||
#undef LUAI_DDEF
|
|
||||||
#define LUAI_FUNC static
|
|
||||||
#define LUAI_DDEC(def) /* empty */
|
|
||||||
#define LUAI_DDEF static
|
|
||||||
|
|
||||||
/* core -- used by all */
|
|
||||||
#include "lzio.c"
|
|
||||||
#include "lctype.c"
|
|
||||||
#include "lopcodes.c"
|
|
||||||
#include "lmem.c"
|
|
||||||
#include "lundump.c"
|
|
||||||
#include "ldump.c"
|
|
||||||
#include "lstate.c"
|
|
||||||
#include "lgc.c"
|
|
||||||
#include "llex.c"
|
|
||||||
#include "lcode.c"
|
|
||||||
#include "lparser.c"
|
|
||||||
#include "ldebug.c"
|
|
||||||
#include "lfunc.c"
|
|
||||||
#include "lobject.c"
|
|
||||||
#include "ltm.c"
|
|
||||||
#include "lstring.c"
|
|
||||||
#include "ltable.c"
|
|
||||||
#include "ldo.c"
|
|
||||||
#include "lvm.c"
|
|
||||||
#include "lapi.c"
|
|
||||||
|
|
||||||
/* auxiliary library -- used by all */
|
|
||||||
#include "lauxlib.c"
|
|
||||||
|
|
||||||
/* standard library -- not used by luac */
|
|
||||||
#ifndef MAKE_LUAC
|
|
||||||
#include "lbaselib.c"
|
|
||||||
#include "lcorolib.c"
|
|
||||||
#include "ldblib.c"
|
|
||||||
#include "liolib.c"
|
|
||||||
#include "lmathlib.c"
|
|
||||||
#include "loadlib.c"
|
|
||||||
#include "loslib.c"
|
|
||||||
#include "lstrlib.c"
|
|
||||||
#include "ltablib.c"
|
|
||||||
#include "lutf8lib.c"
|
|
||||||
#include "linit.c"
|
|
||||||
#endif
|
|
||||||
|
|
||||||
/* test library -- used only for internal development */
|
|
||||||
#if defined(LUA_DEBUG)
|
|
||||||
#include "ltests.c"
|
|
||||||
#endif
|
|
||||||
|
|
||||||
/* lua */
|
|
||||||
#ifdef MAKE_LUA
|
|
||||||
#include "lua.c"
|
|
||||||
#endif
|
|
||||||
|
|
||||||
/* luac */
|
|
||||||
#ifdef MAKE_LUAC
|
|
||||||
#include "luac.c"
|
|
||||||
#endif
|
|
||||||
+62
-60
@@ -1,21 +1,17 @@
|
|||||||
#!../lua
|
#!../lua
|
||||||
-- $Id: testes/all.lua $
|
-- $Id: all.lua,v 1.95 2016/11/07 13:11:28 roberto Exp $
|
||||||
-- See Copyright Notice in file lua.h
|
-- See Copyright Notice at the end of this file
|
||||||
|
|
||||||
global <const> *
|
|
||||||
|
|
||||||
global _soft, _port, _nomsg
|
local version = "Lua 5.3"
|
||||||
global T
|
|
||||||
|
|
||||||
local version = "Lua 5.5"
|
|
||||||
if _VERSION ~= version then
|
if _VERSION ~= version then
|
||||||
io.stderr:write("This test suite is for ", version,
|
io.stderr:write("\nThis test suite is for ", version, ", not for ", _VERSION,
|
||||||
", not for ", _VERSION, "\nExiting tests")
|
"\nExiting tests\n")
|
||||||
return
|
return
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
||||||
_G.ARG = arg -- save arg for other tests
|
_G._ARG = arg -- save arg for other tests
|
||||||
|
|
||||||
|
|
||||||
-- next variables control the execution of some tests
|
-- next variables control the execution of some tests
|
||||||
@@ -32,14 +28,14 @@ _nomsg = rawget(_G, "_nomsg") or false
|
|||||||
local usertests = rawget(_G, "_U")
|
local usertests = rawget(_G, "_U")
|
||||||
|
|
||||||
if usertests then
|
if usertests then
|
||||||
_soft = true -- avoid tests that take too long
|
-- tests for sissies ;) Avoid problems
|
||||||
_port = true -- avoid non-portable tests
|
_soft = true
|
||||||
_nomsg = true -- avoid messages about tests not performed
|
_port = true
|
||||||
|
_nomsg = true
|
||||||
end
|
end
|
||||||
|
|
||||||
-- tests should require debug when needed
|
-- tests should require debug when needed
|
||||||
global debug; debug = nil
|
debug = nil
|
||||||
|
|
||||||
|
|
||||||
if usertests then
|
if usertests then
|
||||||
T = nil -- no "internal" tests for user tests
|
T = nil -- no "internal" tests for user tests
|
||||||
@@ -47,6 +43,7 @@ else
|
|||||||
T = rawget(_G, "T") -- avoid problems with 'strict' module
|
T = rawget(_G, "T") -- avoid problems with 'strict' module
|
||||||
end
|
end
|
||||||
|
|
||||||
|
math.randomseed(0)
|
||||||
|
|
||||||
--[=[
|
--[=[
|
||||||
example of a long [comment],
|
example of a long [comment],
|
||||||
@@ -54,14 +51,6 @@ end
|
|||||||
|
|
||||||
]=]
|
]=]
|
||||||
|
|
||||||
print("\n\tStarting Tests")
|
|
||||||
|
|
||||||
do
|
|
||||||
-- set random seed
|
|
||||||
local random_x, random_y = math.randomseed()
|
|
||||||
print(string.format("random seeds: %d, %d", random_x, random_y))
|
|
||||||
end
|
|
||||||
|
|
||||||
print("current path:\n****" .. package.path .. "****\n")
|
print("current path:\n****" .. package.path .. "****\n")
|
||||||
|
|
||||||
|
|
||||||
@@ -75,7 +64,7 @@ do -- (
|
|||||||
|
|
||||||
-- track messages for tests not performed
|
-- track messages for tests not performed
|
||||||
local msgs = {}
|
local msgs = {}
|
||||||
global function Message (m)
|
function Message (m)
|
||||||
if not _nomsg then
|
if not _nomsg then
|
||||||
print(m)
|
print(m)
|
||||||
msgs[#msgs+1] = string.sub(m, 3, -3)
|
msgs[#msgs+1] = string.sub(m, 3, -3)
|
||||||
@@ -103,8 +92,6 @@ local function F (m)
|
|||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
local Cstacklevel
|
|
||||||
|
|
||||||
local showmem
|
local showmem
|
||||||
if not T then
|
if not T then
|
||||||
local max = 0
|
local max = 0
|
||||||
@@ -114,7 +101,6 @@ if not T then
|
|||||||
print(format(" ---- total memory: %s, max memory: %s ----\n",
|
print(format(" ---- total memory: %s, max memory: %s ----\n",
|
||||||
F(m), F(max)))
|
F(m), F(max)))
|
||||||
end
|
end
|
||||||
Cstacklevel = function () return 0 end -- no info about stack level
|
|
||||||
else
|
else
|
||||||
showmem = function ()
|
showmem = function ()
|
||||||
T.checkmemory()
|
T.checkmemory()
|
||||||
@@ -128,16 +114,9 @@ else
|
|||||||
T.totalmem"string", T.totalmem"table", T.totalmem"function",
|
T.totalmem"string", T.totalmem"table", T.totalmem"function",
|
||||||
T.totalmem"userdata", T.totalmem"thread"))
|
T.totalmem"userdata", T.totalmem"thread"))
|
||||||
end
|
end
|
||||||
|
|
||||||
Cstacklevel = function ()
|
|
||||||
local _, _, ncalls = T.stacklevel()
|
|
||||||
return ncalls -- number of C calls
|
|
||||||
end
|
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
||||||
local Cstack = Cstacklevel()
|
|
||||||
|
|
||||||
--
|
--
|
||||||
-- redefine dofile to run files through dump/undump
|
-- redefine dofile to run files through dump/undump
|
||||||
--
|
--
|
||||||
@@ -157,8 +136,18 @@ end
|
|||||||
|
|
||||||
dofile('main.lua')
|
dofile('main.lua')
|
||||||
|
|
||||||
-- trace GC cycles
|
do
|
||||||
require"tracegc".start()
|
local next, setmetatable, stderr = next, setmetatable, io.stderr
|
||||||
|
-- track collections
|
||||||
|
local mt = {}
|
||||||
|
-- each time a table is collected, remark it for finalization
|
||||||
|
-- on next cycle
|
||||||
|
mt.__gc = function (o)
|
||||||
|
stderr:write'.' -- mark progress
|
||||||
|
local n = setmetatable(o, mt) -- remark it
|
||||||
|
end
|
||||||
|
local n = setmetatable({}, mt) -- create object
|
||||||
|
end
|
||||||
|
|
||||||
report"gc.lua"
|
report"gc.lua"
|
||||||
local f = assert(loadfile('gc.lua'))
|
local f = assert(loadfile('gc.lua'))
|
||||||
@@ -166,12 +155,11 @@ f()
|
|||||||
|
|
||||||
dofile('db.lua')
|
dofile('db.lua')
|
||||||
assert(dofile('calls.lua') == deep and deep)
|
assert(dofile('calls.lua') == deep and deep)
|
||||||
_G.deep = nil
|
|
||||||
olddofile('strings.lua')
|
olddofile('strings.lua')
|
||||||
olddofile('literals.lua')
|
olddofile('literals.lua')
|
||||||
dofile('tpack.lua')
|
dofile('tpack.lua')
|
||||||
assert(dofile('attrib.lua') == 27)
|
assert(dofile('attrib.lua') == 27)
|
||||||
dofile('gengc.lua')
|
|
||||||
assert(dofile('locals.lua') == 5)
|
assert(dofile('locals.lua') == 5)
|
||||||
dofile('constructs.lua')
|
dofile('constructs.lua')
|
||||||
dofile('code.lua', true)
|
dofile('code.lua', true)
|
||||||
@@ -181,12 +169,10 @@ if not _G._soft then
|
|||||||
assert(f() == 'b')
|
assert(f() == 'b')
|
||||||
assert(f() == 'a')
|
assert(f() == 'a')
|
||||||
end
|
end
|
||||||
dofile('cstack.lua')
|
|
||||||
dofile('nextvar.lua')
|
dofile('nextvar.lua')
|
||||||
dofile('pm.lua')
|
dofile('pm.lua')
|
||||||
dofile('utf8.lua')
|
dofile('utf8.lua')
|
||||||
dofile('api.lua')
|
dofile('api.lua')
|
||||||
dofile('memerr.lua')
|
|
||||||
assert(dofile('events.lua') == 12)
|
assert(dofile('events.lua') == 12)
|
||||||
dofile('vararg.lua')
|
dofile('vararg.lua')
|
||||||
dofile('closure.lua')
|
dofile('closure.lua')
|
||||||
@@ -200,19 +186,13 @@ assert(dofile('verybig.lua', true) == 10); collectgarbage()
|
|||||||
dofile('files.lua')
|
dofile('files.lua')
|
||||||
|
|
||||||
if #msgs > 0 then
|
if #msgs > 0 then
|
||||||
local m = table.concat(msgs, "\n ")
|
print("\ntests not performed:")
|
||||||
warn("#tests not performed:\n ", m, "\n")
|
for i=1,#msgs do
|
||||||
|
print(msgs[i])
|
||||||
|
end
|
||||||
|
print()
|
||||||
end
|
end
|
||||||
|
|
||||||
print("(there should be two warnings now)")
|
|
||||||
warn("@on")
|
|
||||||
warn("#This is ", "an expected", " warning")
|
|
||||||
warn("@off")
|
|
||||||
warn("******** THIS WARNING SHOULD NOT APPEAR **********")
|
|
||||||
warn("******** THIS WARNING ALSO SHOULD NOT APPEAR **********")
|
|
||||||
warn("@on")
|
|
||||||
warn("#This is", " another one")
|
|
||||||
|
|
||||||
-- no test module should define 'debug'
|
-- no test module should define 'debug'
|
||||||
assert(debug == nil)
|
assert(debug == nil)
|
||||||
|
|
||||||
@@ -226,16 +206,11 @@ debug.sethook(function (a) assert(type(a) == 'string') end, "cr")
|
|||||||
-- to survive outside block
|
-- to survive outside block
|
||||||
_G.showmem = showmem
|
_G.showmem = showmem
|
||||||
|
|
||||||
|
|
||||||
assert(Cstack == Cstacklevel(),
|
|
||||||
"should be at the same C-stack level it was when started the tests")
|
|
||||||
|
|
||||||
end --)
|
end --)
|
||||||
|
|
||||||
local _G, showmem, print, format, clock, time, difftime,
|
local _G, showmem, print, format, clock, time, difftime, assert, open =
|
||||||
assert, open, warn =
|
|
||||||
_G, showmem, print, string.format, os.clock, os.time, os.difftime,
|
_G, showmem, print, string.format, os.clock, os.time, os.difftime,
|
||||||
assert, io.open, warn
|
assert, io.open
|
||||||
|
|
||||||
-- file with time of last performed test
|
-- file with time of last performed test
|
||||||
local fname = T and "time-debug.txt" or "time.txt"
|
local fname = T and "time-debug.txt" or "time.txt"
|
||||||
@@ -256,7 +231,7 @@ end
|
|||||||
print('cleaning all!!!!')
|
print('cleaning all!!!!')
|
||||||
for n in pairs(_G) do
|
for n in pairs(_G) do
|
||||||
if not ({___Glob = 1, tostring = 1})[n] then
|
if not ({___Glob = 1, tostring = 1})[n] then
|
||||||
_G[n] = undef
|
_G[n] = nil
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
@@ -279,7 +254,7 @@ if not usertests then
|
|||||||
local diff = (clocktime - lasttime) / lasttime
|
local diff = (clocktime - lasttime) / lasttime
|
||||||
local tolerance = 0.05 -- 5%
|
local tolerance = 0.05 -- 5%
|
||||||
if (diff >= tolerance or diff <= -tolerance) then
|
if (diff >= tolerance or diff <= -tolerance) then
|
||||||
warn(format("#time difference from previous test: %+.1f%%",
|
print(format("WARNING: time difference from previous test: %+.1f%%",
|
||||||
diff * 100))
|
diff * 100))
|
||||||
end
|
end
|
||||||
assert(open(fname, "w")):write(clocktime):close()
|
assert(open(fname, "w")):write(clocktime):close()
|
||||||
@@ -287,3 +262,30 @@ end
|
|||||||
|
|
||||||
print("final OK !!!")
|
print("final OK !!!")
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
--[[
|
||||||
|
*****************************************************************************
|
||||||
|
* Copyright (C) 1994-2016 Lua.org, PUC-Rio.
|
||||||
|
*
|
||||||
|
* Permission is hereby granted, free of charge, to any person obtaining
|
||||||
|
* a copy of this software and associated documentation files (the
|
||||||
|
* "Software"), to deal in the Software without restriction, including
|
||||||
|
* without limitation the rights to use, copy, modify, merge, publish,
|
||||||
|
* distribute, sublicense, and/or sell copies of the Software, and to
|
||||||
|
* permit persons to whom the Software is furnished to do so, subject to
|
||||||
|
* the following conditions:
|
||||||
|
*
|
||||||
|
* The above copyright notice and this permission notice shall be
|
||||||
|
* included in all copies or substantial portions of the Software.
|
||||||
|
*
|
||||||
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
||||||
|
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
||||||
|
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
|
||||||
|
* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
|
||||||
|
* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
|
||||||
|
* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
|
||||||
|
* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
||||||
|
*****************************************************************************
|
||||||
|
]]
|
||||||
|
|
||||||
|
|||||||
+262
-506
File diff suppressed because it is too large
Load Diff
+36
-113
@@ -1,5 +1,5 @@
|
|||||||
-- $Id: testes/attrib.lua $
|
-- $Id: attrib.lua,v 1.65 2016/11/07 13:11:28 roberto Exp $
|
||||||
-- See Copyright Notice in file lua.h
|
-- See Copyright Notice in file all.lua
|
||||||
|
|
||||||
print "testing require"
|
print "testing require"
|
||||||
|
|
||||||
@@ -28,7 +28,7 @@ do
|
|||||||
local path = table.concat(t, ";")
|
local path = table.concat(t, ";")
|
||||||
-- use that path in a search
|
-- use that path in a search
|
||||||
local s, err = package.searchpath("xuxu", path)
|
local s, err = package.searchpath("xuxu", path)
|
||||||
-- search fails; check that message has an occurrence of
|
-- search fails; check that message has an occurence of
|
||||||
-- '??????????' with ? replaced by xuxu and at least 'max' lines
|
-- '??????????' with ? replaced by xuxu and at least 'max' lines
|
||||||
assert(not s and
|
assert(not s and
|
||||||
string.find(err, string.rep("xuxu", 10)) and
|
string.find(err, string.rep("xuxu", 10)) and
|
||||||
@@ -47,29 +47,6 @@ do
|
|||||||
package.path = oldpath
|
package.path = oldpath
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
||||||
do print"testing 'require' message"
|
|
||||||
local oldpath = package.path
|
|
||||||
local oldcpath = package.cpath
|
|
||||||
|
|
||||||
package.path = "?.lua;?/?"
|
|
||||||
package.cpath = "?.so;?/init"
|
|
||||||
|
|
||||||
local st, msg = pcall(require, 'XXX')
|
|
||||||
|
|
||||||
local expected = [[module 'XXX' not found:
|
|
||||||
no field package.preload['XXX']
|
|
||||||
no file 'XXX.lua'
|
|
||||||
no file 'XXX/XXX'
|
|
||||||
no file 'XXX.so'
|
|
||||||
no file 'XXX/init']]
|
|
||||||
|
|
||||||
assert(msg == expected)
|
|
||||||
|
|
||||||
package.path = oldpath
|
|
||||||
package.cpath = oldcpath
|
|
||||||
end
|
|
||||||
|
|
||||||
print('+')
|
print('+')
|
||||||
|
|
||||||
|
|
||||||
@@ -85,7 +62,7 @@ local DIR = "libs" .. dirsep
|
|||||||
|
|
||||||
-- prepend DIR to a name and correct directory separators
|
-- prepend DIR to a name and correct directory separators
|
||||||
local function D (x)
|
local function D (x)
|
||||||
local x = string.gsub(x, "/", dirsep)
|
x = string.gsub(x, "/", dirsep)
|
||||||
return DIR .. x
|
return DIR .. x
|
||||||
end
|
end
|
||||||
|
|
||||||
@@ -106,7 +83,7 @@ local function createfiles (files, preextras, posextras)
|
|||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
local function removefiles (files)
|
function removefiles (files)
|
||||||
for n in pairs(files) do
|
for n in pairs(files) do
|
||||||
os.remove(D(n))
|
os.remove(D(n))
|
||||||
end
|
end
|
||||||
@@ -145,18 +122,18 @@ local oldpath = package.path
|
|||||||
|
|
||||||
package.path = string.gsub("D/?.lua;D/?.lc;D/?;D/??x?;D/L", "D/", DIR)
|
package.path = string.gsub("D/?.lua;D/?.lc;D/?;D/??x?;D/L", "D/", DIR)
|
||||||
|
|
||||||
local try = function (p, n, r, ext)
|
local try = function (p, n, r)
|
||||||
NAME = nil
|
NAME = nil
|
||||||
local rr, x = require(p)
|
local rr = require(p)
|
||||||
assert(NAME == n)
|
assert(NAME == n)
|
||||||
assert(REQUIRED == p)
|
assert(REQUIRED == p)
|
||||||
assert(rr == r)
|
assert(rr == r)
|
||||||
assert(ext == x)
|
|
||||||
end
|
end
|
||||||
|
|
||||||
local a = require"names"
|
a = require"names"
|
||||||
assert(a[1] == "names" and a[2] == D"names.lua")
|
assert(a[1] == "names" and a[2] == D"names.lua")
|
||||||
|
|
||||||
|
_G.a = nil
|
||||||
local st, msg = pcall(require, "err")
|
local st, msg = pcall(require, "err")
|
||||||
assert(not st and string.find(msg, "arithmetic") and B == 15)
|
assert(not st and string.find(msg, "arithmetic") and B == 15)
|
||||||
st, msg = pcall(require, "synerr")
|
st, msg = pcall(require, "synerr")
|
||||||
@@ -166,31 +143,30 @@ assert(package.searchpath("C", package.path) == D"C.lua")
|
|||||||
assert(require"C" == 25)
|
assert(require"C" == 25)
|
||||||
assert(require"C" == 25)
|
assert(require"C" == 25)
|
||||||
AA = nil
|
AA = nil
|
||||||
try('B', 'B.lua', true, "libs/B.lua")
|
try('B', 'B.lua', true)
|
||||||
assert(package.loaded.B)
|
assert(package.loaded.B)
|
||||||
assert(require"B" == true)
|
assert(require"B" == true)
|
||||||
assert(package.loaded.A)
|
assert(package.loaded.A)
|
||||||
assert(require"C" == 25)
|
assert(require"C" == 25)
|
||||||
package.loaded.A = nil
|
package.loaded.A = nil
|
||||||
try('B', nil, true, nil) -- should not reload package
|
try('B', nil, true) -- should not reload package
|
||||||
try('A', 'A.lua', true, "libs/A.lua")
|
try('A', 'A.lua', true)
|
||||||
package.loaded.A = nil
|
package.loaded.A = nil
|
||||||
os.remove(D'A.lua')
|
os.remove(D'A.lua')
|
||||||
AA = {}
|
AA = {}
|
||||||
try('A', 'A.lc', AA, "libs/A.lc") -- now must find second option
|
try('A', 'A.lc', AA) -- now must find second option
|
||||||
assert(package.searchpath("A", package.path) == D"A.lc")
|
assert(package.searchpath("A", package.path) == D"A.lc")
|
||||||
assert(require("A") == AA)
|
assert(require("A") == AA)
|
||||||
AA = false
|
AA = false
|
||||||
try('K', 'L', false, "libs/L") -- default option
|
try('K', 'L', false) -- default option
|
||||||
try('K', 'L', false, "libs/L") -- default option (should reload it)
|
try('K', 'L', false) -- default option (should reload it)
|
||||||
assert(rawget(_G, "_REQUIREDNAME") == nil)
|
assert(rawget(_G, "_REQUIREDNAME") == nil)
|
||||||
|
|
||||||
AA = "x"
|
AA = "x"
|
||||||
try("X", "XXxX", AA, "libs/XXxX")
|
try("X", "XXxX", AA)
|
||||||
|
|
||||||
|
|
||||||
removefiles(files)
|
removefiles(files)
|
||||||
NAME, REQUIRED, AA, B = nil
|
|
||||||
|
|
||||||
|
|
||||||
-- testing require of sub-packages
|
-- testing require of sub-packages
|
||||||
@@ -207,23 +183,21 @@ files = {
|
|||||||
createfiles(files, "_ENV = {}\n", "\nreturn _ENV\n")
|
createfiles(files, "_ENV = {}\n", "\nreturn _ENV\n")
|
||||||
AA = 0
|
AA = 0
|
||||||
|
|
||||||
local m, ext = assert(require"P1")
|
local m = assert(require"P1")
|
||||||
assert(ext == "libs/P1/init.lua")
|
|
||||||
assert(AA == 0 and m.AA == 10)
|
assert(AA == 0 and m.AA == 10)
|
||||||
assert(require"P1" == m)
|
assert(require"P1" == m)
|
||||||
assert(require"P1" == m)
|
assert(require"P1" == m)
|
||||||
|
|
||||||
assert(package.searchpath("P1.xuxu", package.path) == D"P1/xuxu.lua")
|
assert(package.searchpath("P1.xuxu", package.path) == D"P1/xuxu.lua")
|
||||||
m.xuxu, ext = assert(require"P1.xuxu")
|
m.xuxu = assert(require"P1.xuxu")
|
||||||
assert(AA == 0 and m.xuxu.AA == 20)
|
assert(AA == 0 and m.xuxu.AA == 20)
|
||||||
assert(ext == "libs/P1/xuxu.lua")
|
|
||||||
assert(require"P1.xuxu" == m.xuxu)
|
assert(require"P1.xuxu" == m.xuxu)
|
||||||
assert(require"P1.xuxu" == m.xuxu)
|
assert(require"P1.xuxu" == m.xuxu)
|
||||||
assert(require"P1" == m and m.AA == 10)
|
assert(require"P1" == m and m.AA == 10)
|
||||||
|
|
||||||
|
|
||||||
removefiles(files)
|
removefiles(files)
|
||||||
AA = nil
|
|
||||||
|
|
||||||
package.path = ""
|
package.path = ""
|
||||||
assert(not pcall(require, "file_does_not_exist"))
|
assert(not pcall(require, "file_does_not_exist"))
|
||||||
@@ -236,7 +210,7 @@ package.path = oldpath
|
|||||||
local fname = "file_does_not_exist2"
|
local fname = "file_does_not_exist2"
|
||||||
local m, err = pcall(require, fname)
|
local m, err = pcall(require, fname)
|
||||||
for t in string.gmatch(package.path..";"..package.cpath, "[^;]+") do
|
for t in string.gmatch(package.path..";"..package.cpath, "[^;]+") do
|
||||||
local t = string.gsub(t, "?", fname)
|
t = string.gsub(t, "?", fname)
|
||||||
assert(string.find(err, t, 1, true))
|
assert(string.find(err, t, 1, true))
|
||||||
end
|
end
|
||||||
|
|
||||||
@@ -293,26 +267,23 @@ else
|
|||||||
|
|
||||||
-- test C modules with prefixes in names
|
-- test C modules with prefixes in names
|
||||||
package.cpath = DC"?"
|
package.cpath = DC"?"
|
||||||
local lib2, ext = require"lib2-v2"
|
local lib2 = require"lib2-v2"
|
||||||
assert(string.find(ext, "libs/lib2-v2", 1, true))
|
|
||||||
-- check correct access to global environment and correct
|
-- check correct access to global environment and correct
|
||||||
-- parameters
|
-- parameters
|
||||||
assert(_ENV.x == "lib2-v2" and _ENV.y == DC"lib2-v2")
|
assert(_ENV.x == "lib2-v2" and _ENV.y == DC"lib2-v2")
|
||||||
assert(lib2.id("x") == true) -- a different "id" implementation
|
assert(lib2.id("x") == "x")
|
||||||
|
|
||||||
-- test C submodules
|
-- test C submodules
|
||||||
local fs, ext = require"lib1.sub"
|
local fs = require"lib1.sub"
|
||||||
assert(_ENV.x == "lib1.sub" and _ENV.y == DC"lib1")
|
assert(_ENV.x == "lib1.sub" and _ENV.y == DC"lib1")
|
||||||
assert(string.find(ext, "libs/lib1", 1, true))
|
|
||||||
assert(fs.id(45) == 45)
|
assert(fs.id(45) == 45)
|
||||||
_ENV.x, _ENV.y = nil
|
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
||||||
_ENV = _G
|
_ENV = _G
|
||||||
|
|
||||||
|
|
||||||
-- testing preload
|
-- testing preload
|
||||||
|
|
||||||
do
|
do
|
||||||
local p = package
|
local p = package
|
||||||
package = {}
|
package = {}
|
||||||
@@ -322,35 +293,15 @@ do
|
|||||||
return _ENV
|
return _ENV
|
||||||
end
|
end
|
||||||
|
|
||||||
local pl, ext = require"pl"
|
local pl = require"pl"
|
||||||
assert(require"pl" == pl)
|
assert(require"pl" == pl)
|
||||||
assert(pl.xuxu(10) == 30)
|
assert(pl.xuxu(10) == 30)
|
||||||
assert(pl[1] == "pl" and pl[2] == ":preload:" and ext == ":preload:")
|
assert(pl[1] == "pl" and pl[2] == nil)
|
||||||
|
|
||||||
package = p
|
package = p
|
||||||
assert(type(package.path) == "string")
|
assert(type(package.path) == "string")
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
||||||
do print("testing external strings")
|
|
||||||
package.cpath = DC"?"
|
|
||||||
local lib2 = require"lib2-v2"
|
|
||||||
local t = {}
|
|
||||||
for _, len in ipairs{0, 10, 39, 40, 41, 1000} do
|
|
||||||
local str = string.rep("a", len)
|
|
||||||
local str1 = lib2.newstr(str)
|
|
||||||
assert(str == str1)
|
|
||||||
assert(not T or T.hash(str) == T.hash(str1))
|
|
||||||
t[str1] = 20; assert(t[str] == 20 and t[str1] == 20)
|
|
||||||
t[str] = 10; assert(t[str1] == 10)
|
|
||||||
local tt = {[str1] = str1}
|
|
||||||
assert(next(tt) == str1 and next(tt, str1) == nil)
|
|
||||||
assert(tt[str] == str)
|
|
||||||
local str2 = lib2.newstr(str1)
|
|
||||||
assert(str == str2 and t[str2] == 10 and tt[str2] == str)
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
print('+')
|
print('+')
|
||||||
|
|
||||||
end --]
|
end --]
|
||||||
@@ -359,10 +310,10 @@ print("testing assignments, logical operators, and constructors")
|
|||||||
|
|
||||||
local res, res2 = 27
|
local res, res2 = 27
|
||||||
|
|
||||||
local a, b = 1, 2+3
|
a, b = 1, 2+3
|
||||||
assert(a==1 and b==5)
|
assert(a==1 and b==5)
|
||||||
a={}
|
a={}
|
||||||
local function f() return 10, 11, 12 end
|
function f() return 10, 11, 12 end
|
||||||
a.x, b, a[1] = 1, 2, f()
|
a.x, b, a[1] = 1, 2, f()
|
||||||
assert(a.x==1 and b==2 and a[1]==10)
|
assert(a.x==1 and b==2 and a[1]==10)
|
||||||
a[f()], b, a[f()+3] = f(), a, 'x'
|
a[f()], b, a[f()+3] = f(), a, 'x'
|
||||||
@@ -374,15 +325,15 @@ do
|
|||||||
local a,b,c
|
local a,b,c
|
||||||
a,b = 0, f(1)
|
a,b = 0, f(1)
|
||||||
assert(a == 0 and b == 1)
|
assert(a == 0 and b == 1)
|
||||||
a,b = 0, f(1)
|
A,b = 0, f(1)
|
||||||
assert(a == 0 and b == 1)
|
assert(A == 0 and b == 1)
|
||||||
a,b,c = 0,5,f(4)
|
a,b,c = 0,5,f(4)
|
||||||
assert(a==0 and b==5 and c==1)
|
assert(a==0 and b==5 and c==1)
|
||||||
a,b,c = 0,5,f(0)
|
a,b,c = 0,5,f(0)
|
||||||
assert(a==0 and b==5 and c==nil)
|
assert(a==0 and b==5 and c==nil)
|
||||||
end
|
end
|
||||||
|
|
||||||
local a, b, c, d = 1 and nil, 1 or nil, (1 and (nil or 1)), 6
|
a, b, c, d = 1 and nil, 1 or nil, (1 and (nil or 1)), 6
|
||||||
assert(not a and b and c and d==6)
|
assert(not a and b and c and d==6)
|
||||||
|
|
||||||
d = 20
|
d = 20
|
||||||
@@ -437,50 +388,28 @@ assert(a[a][a][a][a][print] == assert)
|
|||||||
a[print](a[a[f]] == a[print])
|
a[print](a[a[f]] == a[print])
|
||||||
assert(not pcall(function () local a = {}; a[nil] = 10 end))
|
assert(not pcall(function () local a = {}; a[nil] = 10 end))
|
||||||
assert(not pcall(function () local a = {[nil] = 10} end))
|
assert(not pcall(function () local a = {[nil] = 10} end))
|
||||||
assert(a[nil] == undef)
|
assert(a[nil] == nil)
|
||||||
a = nil
|
a = nil
|
||||||
|
|
||||||
local a, b, c
|
|
||||||
a = {10,9,8,7,6,5,4,3,2; [-3]='a', [f]=print, a='a', b='ab'}
|
a = {10,9,8,7,6,5,4,3,2; [-3]='a', [f]=print, a='a', b='ab'}
|
||||||
a, a.x, a.y = a, a[-3]
|
a, a.x, a.y = a, a[-3]
|
||||||
assert(a[1]==10 and a[-3]==a.a and a[f]==print and a.x=='a' and not a.y)
|
assert(a[1]==10 and a[-3]==a.a and a[f]==print and a.x=='a' and not a.y)
|
||||||
a[1], f(a)[2], b, c = {['alo']=assert}, 10, a[1], a[f], 6, 10, 23, f(a), 2
|
a[1], f(a)[2], b, c = {['alo']=assert}, 10, a[1], a[f], 6, 10, 23, f(a), 2
|
||||||
a[1].alo(a[2]==10 and b==10 and c==print)
|
a[1].alo(a[2]==10 and b==10 and c==print)
|
||||||
|
|
||||||
a.aVeryLongName012345678901234567890123456789012345678901234567890123456789 = 10
|
|
||||||
local function foo ()
|
|
||||||
return a.aVeryLongName012345678901234567890123456789012345678901234567890123456789
|
|
||||||
end
|
|
||||||
assert(foo() == 10 and
|
|
||||||
a.aVeryLongName012345678901234567890123456789012345678901234567890123456789 ==
|
|
||||||
10)
|
|
||||||
|
|
||||||
|
-- test of large float/integer indices
|
||||||
do
|
|
||||||
-- _ENV constant
|
|
||||||
local function foo ()
|
|
||||||
local _ENV <const> = 11
|
|
||||||
X = "hi"
|
|
||||||
end
|
|
||||||
local st, msg = pcall(foo)
|
|
||||||
assert(not st and string.find(msg, "number"))
|
|
||||||
end
|
|
||||||
|
|
||||||
|
|
||||||
-- test of large float/integer indices
|
|
||||||
|
|
||||||
-- compute maximum integer where all bits fit in a float
|
-- compute maximum integer where all bits fit in a float
|
||||||
local maxint = math.maxinteger
|
local maxint = math.maxinteger
|
||||||
|
|
||||||
-- trim (if needed) to fit in a float
|
while maxint - 1.0 == maxint - 0.0 do -- trim (if needed) to fit in a float
|
||||||
while maxint ~= (maxint + 0.0) or (maxint - 1) ~= (maxint - 1.0) do
|
|
||||||
maxint = maxint // 2
|
maxint = maxint // 2
|
||||||
end
|
end
|
||||||
|
|
||||||
local maxintF = maxint + 0.0 -- float version
|
maxintF = maxint + 0.0 -- float version
|
||||||
|
|
||||||
assert(maxintF == maxint and math.type(maxintF) == "float" and
|
assert(math.type(maxintF) == "float" and maxintF >= 2.0^14)
|
||||||
maxintF >= 2.0^14)
|
|
||||||
|
|
||||||
-- floats and integers must index the same places
|
-- floats and integers must index the same places
|
||||||
a[maxintF] = 10; a[maxintF - 1.0] = 11;
|
a[maxintF] = 10; a[maxintF - 1.0] = 11;
|
||||||
@@ -505,12 +434,6 @@ do
|
|||||||
i, a[i], a, j, a[j], a[i+j] = j, i, i, b, j, i
|
i, a[i], a, j, a[j], a[i+j] = j, i, i, b, j, i
|
||||||
assert(i == 2 and b[1] == 1 and a == 1 and j == b and b[2] == 2 and
|
assert(i == 2 and b[1] == 1 and a == 1 and j == b and b[2] == 2 and
|
||||||
b[3] == 1)
|
b[3] == 1)
|
||||||
a = {}
|
|
||||||
local function foo () -- assigining to upvalues
|
|
||||||
b, a.x, a = a, 10, 20
|
|
||||||
end
|
|
||||||
foo()
|
|
||||||
assert(a == 20 and b.x == 10)
|
|
||||||
end
|
end
|
||||||
|
|
||||||
-- repeat test with upvalues
|
-- repeat test with upvalues
|
||||||
|
|||||||
+6
-6
@@ -1,5 +1,5 @@
|
|||||||
-- $Id: testes/big.lua $
|
-- $Id: big.lua,v 1.32 2016/11/07 13:11:28 roberto Exp $
|
||||||
-- See Copyright Notice in file lua.h
|
-- See Copyright Notice in file all.lua
|
||||||
|
|
||||||
if _soft then
|
if _soft then
|
||||||
return 'a'
|
return 'a'
|
||||||
@@ -23,7 +23,7 @@ local f = assert(load(prog, nil, nil, env))
|
|||||||
|
|
||||||
f()
|
f()
|
||||||
assert(env.X[lim] == lim - 1 and env.X[lim + 1] == lim)
|
assert(env.X[lim] == lim - 1 and env.X[lim + 1] == lim)
|
||||||
for k in pairs(env) do env[k] = undef end
|
for k in pairs(env) do env[k] = nil end
|
||||||
|
|
||||||
-- yields during accesses larger than K (in RK)
|
-- yields during accesses larger than K (in RK)
|
||||||
setmetatable(env, {
|
setmetatable(env, {
|
||||||
@@ -32,7 +32,7 @@ setmetatable(env, {
|
|||||||
})
|
})
|
||||||
|
|
||||||
X = nil
|
X = nil
|
||||||
local co = coroutine.wrap(f)
|
co = coroutine.wrap(f)
|
||||||
assert(co() == 's')
|
assert(co() == 's')
|
||||||
assert(co() == 'g')
|
assert(co() == 'g')
|
||||||
assert(co() == 'g')
|
assert(co() == 'g')
|
||||||
@@ -49,7 +49,7 @@ assert(not e and m:find("global 'X'"))
|
|||||||
-- errors in metamethods
|
-- errors in metamethods
|
||||||
getmetatable(env).__newindex = function () error("hi") end
|
getmetatable(env).__newindex = function () error("hi") end
|
||||||
local e, m = xpcall(f, debug.traceback)
|
local e, m = xpcall(f, debug.traceback)
|
||||||
assert(not e and m:find("'newindex'"))
|
assert(not e and m:find("'__newindex'"))
|
||||||
|
|
||||||
f, X = nil
|
f, X = nil
|
||||||
|
|
||||||
@@ -66,7 +66,7 @@ assert(repstrings * ssize > 2.0^32) -- it should be larger than maximum size
|
|||||||
|
|
||||||
local longs = string.rep("\0", ssize) -- create one long string
|
local longs = string.rep("\0", ssize) -- create one long string
|
||||||
|
|
||||||
-- create function to concatenate 'repstrings' copies of its argument
|
-- create function to concatentate 'repstrings' copies of its argument
|
||||||
local rep = assert(load(
|
local rep = assert(load(
|
||||||
"local a = ...; return " .. string.rep("a", repstrings, "..")))
|
"local a = ...; return " .. string.rep("a", repstrings, "..")))
|
||||||
|
|
||||||
|
|||||||
Regular → Executable
+2
-37
@@ -1,10 +1,8 @@
|
|||||||
-- $Id: testes/bitwise.lua $
|
-- $Id: bitwise.lua,v 1.26 2016/11/07 13:11:28 roberto Exp $
|
||||||
-- See Copyright Notice in file lua.h
|
-- See Copyright Notice in file all.lua
|
||||||
|
|
||||||
print("testing bitwise operations")
|
print("testing bitwise operations")
|
||||||
|
|
||||||
require "bwcoercion"
|
|
||||||
|
|
||||||
local numbits = string.packsize('j') * 8
|
local numbits = string.packsize('j') * 8
|
||||||
|
|
||||||
assert(~0 == -1)
|
assert(~0 == -1)
|
||||||
@@ -38,18 +36,6 @@ d = d << 32
|
|||||||
assert(a | b ~ c & d == 0xF4000000 << 32)
|
assert(a | b ~ c & d == 0xF4000000 << 32)
|
||||||
assert(~~a == a and ~a == -1 ~ a and -d == ~d + 1)
|
assert(~~a == a and ~a == -1 ~ a and -d == ~d + 1)
|
||||||
|
|
||||||
|
|
||||||
do -- constant folding
|
|
||||||
local code = string.format("return -1 >> %d", math.maxinteger)
|
|
||||||
assert(load(code)() == 0)
|
|
||||||
local code = string.format("return -1 >> %d", math.mininteger)
|
|
||||||
assert(load(code)() == 0)
|
|
||||||
local code = string.format("return -1 << %d", math.maxinteger)
|
|
||||||
assert(load(code)() == 0)
|
|
||||||
local code = string.format("return -1 << %d", math.mininteger)
|
|
||||||
assert(load(code)() == 0)
|
|
||||||
end
|
|
||||||
|
|
||||||
assert(-1 >> 1 == (1 << (numbits - 1)) - 1 and 1 << 31 == 0x80000000)
|
assert(-1 >> 1 == (1 << (numbits - 1)) - 1 and 1 << 31 == 0x80000000)
|
||||||
assert(-1 >> (numbits - 1) == 1)
|
assert(-1 >> (numbits - 1) == 1)
|
||||||
assert(-1 >> numbits == 0 and
|
assert(-1 >> numbits == 0 and
|
||||||
@@ -57,11 +43,6 @@ assert(-1 >> numbits == 0 and
|
|||||||
-1 << numbits == 0 and
|
-1 << numbits == 0 and
|
||||||
-1 << -numbits == 0)
|
-1 << -numbits == 0)
|
||||||
|
|
||||||
assert(1 >> math.mininteger == 0)
|
|
||||||
assert(1 >> math.maxinteger == 0)
|
|
||||||
assert(1 << math.mininteger == 0)
|
|
||||||
assert(1 << math.maxinteger == 0)
|
|
||||||
|
|
||||||
assert((2^30 - 1) << 2^30 == 0)
|
assert((2^30 - 1) << 2^30 == 0)
|
||||||
assert((2^30 - 1) >> 2^30 == 0)
|
assert((2^30 - 1) >> 2^30 == 0)
|
||||||
|
|
||||||
@@ -73,22 +54,6 @@ assert("0xffffffffffffffff" | 0 == -1)
|
|||||||
assert("0xfffffffffffffffe" & "-1" == -2)
|
assert("0xfffffffffffffffe" & "-1" == -2)
|
||||||
assert(" \t-0xfffffffffffffffe\n\t" & "-1" == 2)
|
assert(" \t-0xfffffffffffffffe\n\t" & "-1" == 2)
|
||||||
assert(" \n -45 \t " >> " -2 " == -45 * 4)
|
assert(" \n -45 \t " >> " -2 " == -45 * 4)
|
||||||
assert("1234.0" << "5.0" == 1234 * 32)
|
|
||||||
assert("0xffff.0" ~ "0xAAAA" == 0x5555)
|
|
||||||
assert(~"0x0.000p4" == -1)
|
|
||||||
|
|
||||||
assert(("7" .. 3) << 1 == 146)
|
|
||||||
assert(0xffffffff >> (1 .. "9") == 0x1fff)
|
|
||||||
assert(10 | (1 .. "9") == 27)
|
|
||||||
|
|
||||||
do
|
|
||||||
local st, msg = pcall(function () return 4 & "a" end)
|
|
||||||
assert(string.find(msg, "'band'"))
|
|
||||||
|
|
||||||
local st, msg = pcall(function () return ~"a" end)
|
|
||||||
assert(string.find(msg, "'bnot'"))
|
|
||||||
end
|
|
||||||
|
|
||||||
|
|
||||||
-- out of range number
|
-- out of range number
|
||||||
assert(not pcall(function () return "0xffffffffffffffff.0" | 0 end))
|
assert(not pcall(function () return "0xffffffffffffffff.0" | 0 end))
|
||||||
|
|||||||
@@ -4,7 +4,7 @@ local strsub = string.sub
|
|||||||
|
|
||||||
local print = print
|
local print = print
|
||||||
|
|
||||||
global none
|
_ENV = nil
|
||||||
|
|
||||||
-- Try to convert a value to an integer, without assuming any coercion.
|
-- Try to convert a value to an integer, without assuming any coercion.
|
||||||
local function toint (x)
|
local function toint (x)
|
||||||
|
|||||||
+64
-240
@@ -1,7 +1,5 @@
|
|||||||
-- $Id: testes/calls.lua $
|
-- $Id: calls.lua,v 1.60 2016/11/07 13:11:28 roberto Exp $
|
||||||
-- See Copyright Notice in file lua.h
|
-- See Copyright Notice in file all.lua
|
||||||
|
|
||||||
global <const> *
|
|
||||||
|
|
||||||
print("testing functions and calls")
|
print("testing functions and calls")
|
||||||
|
|
||||||
@@ -18,13 +16,28 @@ assert(type(nil) == 'nil'
|
|||||||
and type(type) == 'function')
|
and type(type) == 'function')
|
||||||
|
|
||||||
assert(type(assert) == type(print))
|
assert(type(assert) == type(print))
|
||||||
local function f (x) return a:x (x) end
|
function f (x) return a:x (x) end
|
||||||
assert(type(f) == 'function')
|
assert(type(f) == 'function')
|
||||||
assert(not pcall(type))
|
assert(not pcall(type))
|
||||||
|
|
||||||
|
|
||||||
|
do -- test error in 'print' too...
|
||||||
|
local tostring = _ENV.tostring
|
||||||
|
|
||||||
|
_ENV.tostring = nil
|
||||||
|
local st, msg = pcall(print, 1)
|
||||||
|
assert(st == false and string.find(msg, "attempt to call a nil value"))
|
||||||
|
|
||||||
|
_ENV.tostring = function () return {} end
|
||||||
|
local st, msg = pcall(print, 1)
|
||||||
|
assert(st == false and string.find(msg, "must return a string"))
|
||||||
|
|
||||||
|
_ENV.tostring = tostring
|
||||||
|
end
|
||||||
|
|
||||||
|
|
||||||
-- testing local-function recursion
|
-- testing local-function recursion
|
||||||
global fact = false
|
fact = false
|
||||||
do
|
do
|
||||||
local res = 1
|
local res = 1
|
||||||
local function fact (n)
|
local function fact (n)
|
||||||
@@ -35,11 +48,10 @@ do
|
|||||||
assert(fact(5) == 120)
|
assert(fact(5) == 120)
|
||||||
end
|
end
|
||||||
assert(fact == false)
|
assert(fact == false)
|
||||||
fact = nil
|
|
||||||
|
|
||||||
-- testing declarations
|
-- testing declarations
|
||||||
local a = {i = 10}
|
a = {i = 10}
|
||||||
local self = 20
|
self = 20
|
||||||
function a:x (x) return x+self.i end
|
function a:x (x) return x+self.i end
|
||||||
function a.y (x) return x+self end
|
function a.y (x) return x+self end
|
||||||
|
|
||||||
@@ -65,7 +77,7 @@ a.b.c:f2('k', 12); assert(a.b.c.k == 12)
|
|||||||
|
|
||||||
print('+')
|
print('+')
|
||||||
|
|
||||||
global t = nil -- 'declare' t
|
t = nil -- 'declare' t
|
||||||
function f(a,b,c) local d = 'a'; t={a,b,c,d} end
|
function f(a,b,c) local d = 'a'; t={a,b,c,d} end
|
||||||
|
|
||||||
f( -- this line change must be valid
|
f( -- this line change must be valid
|
||||||
@@ -75,9 +87,7 @@ f(1,2, -- this one too
|
|||||||
3,4)
|
3,4)
|
||||||
assert(t[1] == 1 and t[2] == 2 and t[3] == 3 and t[4] == 'a')
|
assert(t[1] == 1 and t[2] == 2 and t[3] == 3 and t[4] == 'a')
|
||||||
|
|
||||||
t = nil -- delete 't'
|
function fat(x)
|
||||||
|
|
||||||
global function fat(x)
|
|
||||||
if x <= 1 then return 1
|
if x <= 1 then return 1
|
||||||
else return x*load("return fat(" .. x-1 .. ")", "")()
|
else return x*load("return fat(" .. x-1 .. ")", "")()
|
||||||
end
|
end
|
||||||
@@ -85,158 +95,43 @@ end
|
|||||||
|
|
||||||
assert(load "load 'assert(fat(6)==720)' () ")()
|
assert(load "load 'assert(fat(6)==720)' () ")()
|
||||||
a = load('return fat(5), 3')
|
a = load('return fat(5), 3')
|
||||||
local a,b = a()
|
a,b = a()
|
||||||
assert(a == 120 and b == 3)
|
assert(a == 120 and b == 3)
|
||||||
fat = nil
|
|
||||||
print('+')
|
print('+')
|
||||||
|
|
||||||
local function err_on_n (n)
|
function err_on_n (n)
|
||||||
if n==0 then error(); exit(1);
|
if n==0 then error(); exit(1);
|
||||||
else err_on_n (n-1); exit(1);
|
else err_on_n (n-1); exit(1);
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
do
|
do
|
||||||
local function dummy (n)
|
function dummy (n)
|
||||||
if n > 0 then
|
if n > 0 then
|
||||||
assert(not pcall(err_on_n, n))
|
assert(not pcall(err_on_n, n))
|
||||||
dummy(n-1)
|
dummy(n-1)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
dummy(10)
|
|
||||||
end
|
end
|
||||||
|
|
||||||
_G.deep = nil -- "declaration" (used by 'all.lua')
|
dummy(10)
|
||||||
|
|
||||||
global function deep (n)
|
function deep (n)
|
||||||
if n>0 then deep(n-1) end
|
if n>0 then deep(n-1) end
|
||||||
end
|
end
|
||||||
deep(10)
|
deep(10)
|
||||||
deep(180)
|
deep(200)
|
||||||
|
|
||||||
|
|
||||||
print"testing tail calls"
|
|
||||||
|
|
||||||
|
-- testing tail call
|
||||||
function deep (n) if n>0 then return deep(n-1) else return 101 end end
|
function deep (n) if n>0 then return deep(n-1) else return 101 end end
|
||||||
assert(deep(30000) == 101)
|
assert(deep(30000) == 101)
|
||||||
a = {}
|
a = {}
|
||||||
function a:deep (n) if n>0 then return self:deep(n-1) else return 101 end end
|
function a:deep (n) if n>0 then return self:deep(n-1) else return 101 end end
|
||||||
assert(a:deep(30000) == 101)
|
assert(a:deep(30000) == 101)
|
||||||
|
|
||||||
do -- tail calls x varargs
|
|
||||||
local function foo (x, ...) local a = {...}; return x, a[1], a[2] end
|
|
||||||
|
|
||||||
local function foo1 (x) return foo(10, x, x + 1) end
|
|
||||||
|
|
||||||
local a, b, c = foo1(-2)
|
|
||||||
assert(a == 10 and b == -2 and c == -1)
|
|
||||||
|
|
||||||
-- tail calls x metamethods
|
|
||||||
local t = setmetatable({}, {__call = foo})
|
|
||||||
local function foo2 (x) return t(10, x) end
|
|
||||||
a, b, c = foo2(100)
|
|
||||||
assert(a == t and b == 10 and c == 100)
|
|
||||||
|
|
||||||
a, b = (function () return foo() end)()
|
|
||||||
assert(a == nil and b == nil)
|
|
||||||
|
|
||||||
local X, Y, A
|
|
||||||
local function foo (x, y, ...) X = x; Y = y; A = {...} end
|
|
||||||
local function foo1 (...) return foo(...) end
|
|
||||||
|
|
||||||
local a, b, c = foo1()
|
|
||||||
assert(X == nil and Y == nil and #A == 0)
|
|
||||||
|
|
||||||
a, b, c = foo1(10)
|
|
||||||
assert(X == 10 and Y == nil and #A == 0)
|
|
||||||
|
|
||||||
a, b, c = foo1(10, 20)
|
|
||||||
assert(X == 10 and Y == 20 and #A == 0)
|
|
||||||
|
|
||||||
a, b, c = foo1(10, 20, 30)
|
|
||||||
assert(X == 10 and Y == 20 and #A == 1 and A[1] == 30)
|
|
||||||
end
|
|
||||||
|
|
||||||
|
|
||||||
do -- C-stack overflow while handling C-stack overflow
|
|
||||||
local function loop ()
|
|
||||||
assert(pcall(loop))
|
|
||||||
end
|
|
||||||
|
|
||||||
local err, msg = xpcall(loop, loop)
|
|
||||||
assert(not err and string.find(msg, "error"))
|
|
||||||
end
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
do -- tail calls x chain of __call
|
|
||||||
local n = 10000 -- depth
|
|
||||||
|
|
||||||
local function foo ()
|
|
||||||
if n == 0 then return 1023
|
|
||||||
else n = n - 1; return foo()
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
-- build a chain of __call metamethods ending in function 'foo'
|
|
||||||
for i = 1, 15 do
|
|
||||||
foo = setmetatable({}, {__call = foo})
|
|
||||||
end
|
|
||||||
|
|
||||||
-- call the first one as a tail call in a new coroutine
|
|
||||||
-- (to ensure stack is not preallocated)
|
|
||||||
assert(coroutine.wrap(function() return foo() end)() == 1023)
|
|
||||||
end
|
|
||||||
|
|
||||||
print('+')
|
print('+')
|
||||||
|
|
||||||
|
|
||||||
do print"testing chains of '__call'"
|
|
||||||
local N = 15
|
|
||||||
local u = table.pack
|
|
||||||
for i = 1, N do
|
|
||||||
u = setmetatable({i}, {__call = u})
|
|
||||||
end
|
|
||||||
|
|
||||||
local Res = u("a", "b", "c")
|
|
||||||
|
|
||||||
assert(Res.n == N + 3)
|
|
||||||
for i = 1, N do
|
|
||||||
assert(Res[i][1] == i)
|
|
||||||
end
|
|
||||||
assert(Res[N + 1] == "a" and Res[N + 2] == "b" and Res[N + 3] == "c")
|
|
||||||
|
|
||||||
local function u (...)
|
|
||||||
local n = debug.getinfo(1, 't').extraargs
|
|
||||||
assert(select("#", ...) == n)
|
|
||||||
return n
|
|
||||||
end
|
|
||||||
|
|
||||||
for i = 0, N do
|
|
||||||
assert(u() == i)
|
|
||||||
u = setmetatable({}, {__call = u})
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
|
|
||||||
do -- testing chains too long
|
|
||||||
local a = {}
|
|
||||||
for i = 1, 16 do -- one too many
|
|
||||||
a = setmetatable({}, {__call = a})
|
|
||||||
end
|
|
||||||
local status, msg = pcall(a)
|
|
||||||
assert(not status and string.find(msg, "too long"))
|
|
||||||
|
|
||||||
setmetatable(a, {__call = a}) -- infinite chain
|
|
||||||
local status, msg = pcall(a)
|
|
||||||
assert(not status and string.find(msg, "too long"))
|
|
||||||
|
|
||||||
-- again, with a tail call
|
|
||||||
local status, msg = pcall(function () return a() end)
|
|
||||||
assert(not status and string.find(msg, "too long"))
|
|
||||||
end
|
|
||||||
|
|
||||||
a = nil
|
a = nil
|
||||||
(function (x) a=x end)(23)
|
(function (x) a=x end)(23)
|
||||||
assert(a == 23 and (function (x) return x*2 end)(20) == 40)
|
assert(a == 23 and (function (x) return x*2 end)(20) == 40)
|
||||||
@@ -245,7 +140,7 @@ assert(a == 23 and (function (x) return x*2 end)(20) == 40)
|
|||||||
-- testing closures
|
-- testing closures
|
||||||
|
|
||||||
-- fixed-point operator
|
-- fixed-point operator
|
||||||
local Z = function (le)
|
Z = function (le)
|
||||||
local function a (f)
|
local function a (f)
|
||||||
return le(function (x) return f(f)(x) end)
|
return le(function (x) return f(f)(x) end)
|
||||||
end
|
end
|
||||||
@@ -255,14 +150,14 @@ local Z = function (le)
|
|||||||
|
|
||||||
-- non-recursive factorial
|
-- non-recursive factorial
|
||||||
|
|
||||||
local F = function (f)
|
F = function (f)
|
||||||
return function (n)
|
return function (n)
|
||||||
if n == 0 then return 1
|
if n == 0 then return 1
|
||||||
else return n*f(n-1) end
|
else return n*f(n-1) end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
local fat = Z(F)
|
fat = Z(F)
|
||||||
|
|
||||||
assert(fat(0) == 1 and fat(4) == 24 and Z(F)(5)==5*Z(F)(4))
|
assert(fat(0) == 1 and fat(4) == 24 and Z(F)(5)==5*Z(F)(4))
|
||||||
|
|
||||||
@@ -273,21 +168,22 @@ local function g (z)
|
|||||||
return f(z,z+1,z+2,z+3)
|
return f(z,z+1,z+2,z+3)
|
||||||
end
|
end
|
||||||
|
|
||||||
local f = g(10)
|
f = g(10)
|
||||||
assert(f(9, 16) == 10+11+12+13+10+9+16+10)
|
assert(f(9, 16) == 10+11+12+13+10+9+16+10)
|
||||||
|
|
||||||
|
Z, F, f = nil
|
||||||
print('+')
|
print('+')
|
||||||
|
|
||||||
-- testing multiple returns
|
-- testing multiple returns
|
||||||
|
|
||||||
local function unlpack (t, i)
|
function unlpack (t, i)
|
||||||
i = i or 1
|
i = i or 1
|
||||||
if (i <= #t) then
|
if (i <= #t) then
|
||||||
return t[i], unlpack(t, i+1)
|
return t[i], unlpack(t, i+1)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
local function equaltab (t1, t2)
|
function equaltab (t1, t2)
|
||||||
assert(#t1 == #t2)
|
assert(#t1 == #t2)
|
||||||
for i = 1, #t1 do
|
for i = 1, #t1 do
|
||||||
assert(t1[i] == t2[i])
|
assert(t1[i] == t2[i])
|
||||||
@@ -296,8 +192,8 @@ end
|
|||||||
|
|
||||||
local pack = function (...) return (table.pack(...)) end
|
local pack = function (...) return (table.pack(...)) end
|
||||||
|
|
||||||
local function f() return 1,2,30,4 end
|
function f() return 1,2,30,4 end
|
||||||
local function ret2 (a,b) return a,b end
|
function ret2 (a,b) return a,b end
|
||||||
|
|
||||||
local a,b,c,d = unlpack{1,2,3}
|
local a,b,c,d = unlpack{1,2,3}
|
||||||
assert(a==1 and b==2 and c==3 and d==nil)
|
assert(a==1 and b==2 and c==3 and d==nil)
|
||||||
@@ -326,7 +222,7 @@ table.sort({10,9,8,4,19,23,0,0}, function (a,b) return a<b end, "extra arg")
|
|||||||
local x = "-- a comment\0\0\0\n x = 10 + \n23; \
|
local x = "-- a comment\0\0\0\n x = 10 + \n23; \
|
||||||
local a = function () x = 'hi' end; \
|
local a = function () x = 'hi' end; \
|
||||||
return '\0'"
|
return '\0'"
|
||||||
local function read1 (x)
|
function read1 (x)
|
||||||
local i = 0
|
local i = 0
|
||||||
return function ()
|
return function ()
|
||||||
collectgarbage()
|
collectgarbage()
|
||||||
@@ -335,7 +231,7 @@ local function read1 (x)
|
|||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
local function cannotload (msg, a,b)
|
function cannotload (msg, a,b)
|
||||||
assert(not a and string.find(b, msg))
|
assert(not a and string.find(b, msg))
|
||||||
end
|
end
|
||||||
|
|
||||||
@@ -354,7 +250,7 @@ assert(not load(function () return true end))
|
|||||||
|
|
||||||
-- small bug
|
-- small bug
|
||||||
local t = {nil, "return ", "3"}
|
local t = {nil, "return ", "3"}
|
||||||
local f, msg = load(function () return table.remove(t, 1) end)
|
f, msg = load(function () return table.remove(t, 1) end)
|
||||||
assert(f() == nil) -- should read the empty chunk
|
assert(f() == nil) -- should read the empty chunk
|
||||||
|
|
||||||
-- another small bug (in 5.2.1)
|
-- another small bug (in 5.2.1)
|
||||||
@@ -362,22 +258,11 @@ f = load(string.dump(function () return 1 end), nil, "b", {})
|
|||||||
assert(type(f) == "function" and f() == 1)
|
assert(type(f) == "function" and f() == 1)
|
||||||
|
|
||||||
|
|
||||||
do -- another bug (in 5.4.0)
|
|
||||||
-- loading a binary long string interrupted by GC cycles
|
|
||||||
local f = string.dump(function ()
|
|
||||||
return '01234567890123456789012345678901234567890123456789'
|
|
||||||
end)
|
|
||||||
f = load(read1(f))
|
|
||||||
assert(f() == '01234567890123456789012345678901234567890123456789')
|
|
||||||
end
|
|
||||||
|
|
||||||
|
|
||||||
x = string.dump(load("x = 1; return x"))
|
x = string.dump(load("x = 1; return x"))
|
||||||
a = assert(load(read1(x), nil, "b"))
|
a = assert(load(read1(x), nil, "b"))
|
||||||
assert(a() == 1 and _G.x == 1)
|
assert(a() == 1 and _G.x == 1)
|
||||||
cannotload("attempt to load a binary chunk", load(read1(x), nil, "t"))
|
cannotload("attempt to load a binary chunk", load(read1(x), nil, "t"))
|
||||||
cannotload("attempt to load a binary chunk", load(x, nil, "t"))
|
cannotload("attempt to load a binary chunk", load(x, nil, "t"))
|
||||||
_G.x = nil
|
|
||||||
|
|
||||||
assert(not pcall(string.dump, print)) -- no dump of C functions
|
assert(not pcall(string.dump, print)) -- no dump of C functions
|
||||||
|
|
||||||
@@ -390,8 +275,7 @@ assert(load("return _ENV", nil, nil, 123)() == 123)
|
|||||||
|
|
||||||
|
|
||||||
-- load when _ENV is not first upvalue
|
-- load when _ENV is not first upvalue
|
||||||
global XX; local x
|
local x; XX = 123
|
||||||
XX = 123
|
|
||||||
local function h ()
|
local function h ()
|
||||||
local y=x -- use 'x', so that it becomes 1st upvalue
|
local y=x -- use 'x', so that it becomes 1st upvalue
|
||||||
return XX -- global name
|
return XX -- global name
|
||||||
@@ -403,7 +287,7 @@ debug.setupvalue(x, 2, _G)
|
|||||||
assert(x() == 123)
|
assert(x() == 123)
|
||||||
|
|
||||||
assert(assert(load("return XX + ...", nil, nil, {XX = 13}))(4) == 17)
|
assert(assert(load("return XX + ...", nil, nil, {XX = 13}))(4) == 17)
|
||||||
XX = nil
|
|
||||||
|
|
||||||
-- test generic load with nested functions
|
-- test generic load with nested functions
|
||||||
x = [[
|
x = [[
|
||||||
@@ -415,12 +299,8 @@ x = [[
|
|||||||
end
|
end
|
||||||
end
|
end
|
||||||
]]
|
]]
|
||||||
a = assert(load(read1(x), "read", "t"))
|
|
||||||
assert(a()(2)(3)(10) == 15)
|
|
||||||
|
|
||||||
-- repeat the test loading a binary chunk
|
a = assert(load(read1(x)))
|
||||||
x = string.dump(a)
|
|
||||||
a = assert(load(read1(x), "read", "b"))
|
|
||||||
assert(a()(2)(3)(10) == 15)
|
assert(a()(2)(3)(10) == 15)
|
||||||
|
|
||||||
|
|
||||||
@@ -483,50 +363,29 @@ assert((function (a) return a end)() == nil)
|
|||||||
|
|
||||||
print("testing binary chunks")
|
print("testing binary chunks")
|
||||||
do
|
do
|
||||||
local headformat = "c4BBc6BiBI4BjBn"
|
local header = string.pack("c4BBc6BBBBBj",
|
||||||
local header = { -- header components
|
"\27Lua", -- signature
|
||||||
"\27Lua", -- signature
|
5*16 + 3, -- version 5.3
|
||||||
0x55, -- version 5.5 (0x55)
|
0, -- format
|
||||||
0, -- format
|
"\x19\x93\r\n\x1a\n", -- data
|
||||||
"\x19\x93\r\n\x1a\n", -- a binary string
|
string.packsize("i"), -- sizeof(int)
|
||||||
string.packsize("i"), -- size of an int
|
string.packsize("T"), -- sizeof(size_t)
|
||||||
-0x5678, -- an int
|
4, -- size of instruction
|
||||||
4, -- size of an instruction
|
string.packsize("j"), -- sizeof(lua integer)
|
||||||
0x12345678, -- an instruction (4 bytes)
|
string.packsize("n"), -- sizeof(lua number)
|
||||||
string.packsize("j"), -- size of a Lua integer
|
0x5678 -- LUAC_INT
|
||||||
-0x5678, -- a Lua integer
|
-- LUAC_NUM may not have a unique binary representation (padding...)
|
||||||
string.packsize("n"), -- size of a Lua float
|
)
|
||||||
-370.5, -- a Lua float
|
local c = string.dump(function () local a = 1; local b = 3; return a+b*3 end)
|
||||||
}
|
|
||||||
|
|
||||||
local c = string.dump(function ()
|
assert(string.sub(c, 1, #header) == header)
|
||||||
local a = 1; local b = 3;
|
|
||||||
local f = function () return a + b + _ENV.c; end -- upvalues
|
|
||||||
local s1 = "a constant"
|
|
||||||
local s2 = "another constant"
|
|
||||||
return a + b * 3
|
|
||||||
end)
|
|
||||||
|
|
||||||
assert(assert(load(c))() == 10)
|
-- corrupted header
|
||||||
|
|
||||||
-- check header
|
|
||||||
local t = {string.unpack(headformat, c)}
|
|
||||||
for i = 1, #header do
|
for i = 1, #header do
|
||||||
assert(t[i] == header[i])
|
|
||||||
end
|
|
||||||
|
|
||||||
-- Testing corrupted header.
|
|
||||||
-- A single wrong byte in the head invalidates the chunk,
|
|
||||||
-- except for the Lua float check. (If numbers are long double,
|
|
||||||
-- the representation may need padding, and changing that padding
|
|
||||||
-- will not invalidate the chunk.)
|
|
||||||
local headlen = string.packsize(headformat)
|
|
||||||
headlen = headlen - string.packsize("n") -- remove float check
|
|
||||||
for i = 1, headlen do
|
|
||||||
local s = string.sub(c, 1, i - 1) ..
|
local s = string.sub(c, 1, i - 1) ..
|
||||||
string.char((string.byte(string.sub(c, i, i)) + 1) & 0xFF) ..
|
string.char(string.byte(string.sub(c, i, i)) + 1) ..
|
||||||
string.sub(c, i + 1, -1)
|
string.sub(c, i + 1, -1)
|
||||||
assert(#s == #c and s ~= c)
|
assert(#s == #c)
|
||||||
assert(not load(s))
|
assert(not load(s))
|
||||||
end
|
end
|
||||||
|
|
||||||
@@ -535,42 +394,7 @@ do
|
|||||||
local st, msg = load(string.sub(c, 1, i))
|
local st, msg = load(string.sub(c, 1, i))
|
||||||
assert(not st and string.find(msg, "truncated"))
|
assert(not st and string.find(msg, "truncated"))
|
||||||
end
|
end
|
||||||
end
|
assert(assert(load(c))() == 10)
|
||||||
|
|
||||||
|
|
||||||
do -- check reuse of strings in dumps
|
|
||||||
local str = "|" .. string.rep("X", 50) .. "|"
|
|
||||||
local foo = load(string.format([[
|
|
||||||
local str <const> = "%s"
|
|
||||||
return {
|
|
||||||
function () return str end,
|
|
||||||
function () return str end,
|
|
||||||
function () return str end
|
|
||||||
}
|
|
||||||
]], str))
|
|
||||||
-- count occurrences of 'str' inside the dump
|
|
||||||
local dump = string.dump(foo)
|
|
||||||
local _, count = string.gsub(dump, str, {})
|
|
||||||
-- there should be only two occurrences:
|
|
||||||
-- one inside the source, other the string itself.
|
|
||||||
assert(count == 2)
|
|
||||||
|
|
||||||
if T then -- check reuse of strings in undump
|
|
||||||
local funcs = load(dump)()
|
|
||||||
assert(string.format("%p", T.listk(funcs[1])[1]) ==
|
|
||||||
string.format("%p", T.listk(funcs[3])[1]))
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
|
|
||||||
do -- test limit of multiple returns (254 values)
|
|
||||||
local code = "return 10" .. string.rep(",10", 253)
|
|
||||||
local res = {assert(load(code))()}
|
|
||||||
assert(#res == 254 and res[254] == 10)
|
|
||||||
|
|
||||||
code = code .. ",10"
|
|
||||||
local status, msg = load(code)
|
|
||||||
assert(not status and string.find(msg, "too many returns"))
|
|
||||||
end
|
end
|
||||||
|
|
||||||
print('OK')
|
print('OK')
|
||||||
|
|||||||
+22
-54
@@ -1,20 +1,10 @@
|
|||||||
-- $Id: testes/closure.lua $
|
-- $Id: closure.lua,v 1.59 2016/11/07 13:11:28 roberto Exp $
|
||||||
-- See Copyright Notice in file lua.h
|
-- See Copyright Notice in file all.lua
|
||||||
|
|
||||||
global <const> *
|
|
||||||
|
|
||||||
print "testing closures"
|
print "testing closures"
|
||||||
|
|
||||||
do -- bug in 5.4.7
|
|
||||||
_ENV[true] = 10
|
|
||||||
local function aux () return _ENV[1 < 2] end
|
|
||||||
assert(aux() == 10)
|
|
||||||
_ENV[true] = nil
|
|
||||||
end
|
|
||||||
|
|
||||||
|
|
||||||
local A,B = 0,{g=10}
|
local A,B = 0,{g=10}
|
||||||
local function f(x)
|
function f(x)
|
||||||
local a = {}
|
local a = {}
|
||||||
for i=1,1000 do
|
for i=1,1000 do
|
||||||
local y = 0
|
local y = 0
|
||||||
@@ -54,49 +44,50 @@ assert(B.g == 19)
|
|||||||
|
|
||||||
-- testing equality
|
-- testing equality
|
||||||
a = {}
|
a = {}
|
||||||
|
for i = 1, 5 do a[i] = function (x) return x + a + _ENV end end
|
||||||
|
assert(a[3] == a[4] and a[4] == a[5])
|
||||||
|
|
||||||
for i = 1, 5 do a[i] = function (x) return i + a + _ENV end end
|
for i = 1, 5 do a[i] = function (x) return i + a + _ENV end end
|
||||||
assert(a[3] ~= a[4] and a[4] ~= a[5])
|
assert(a[3] ~= a[4] and a[4] ~= a[5])
|
||||||
|
|
||||||
do
|
local function f()
|
||||||
local a = function (x) return math.sin(_ENV[x]) end
|
return function (x) return math.sin(_ENV[x]) end
|
||||||
local function f()
|
|
||||||
return a
|
|
||||||
end
|
|
||||||
assert(f() == f())
|
|
||||||
end
|
end
|
||||||
|
assert(f() == f())
|
||||||
|
|
||||||
|
|
||||||
-- testing closures with 'for' control variable
|
-- testing closures with 'for' control variable
|
||||||
a = {}
|
a = {}
|
||||||
for i=1,10 do
|
for i=1,10 do
|
||||||
a[i] = function () return i end
|
a[i] = {set = function(x) i=x end, get = function () return i end}
|
||||||
if i == 3 then break end
|
if i == 3 then break end
|
||||||
end
|
end
|
||||||
assert(a[4] == undef)
|
assert(a[4] == nil)
|
||||||
assert(a[2]() == 2)
|
a[1].set(10)
|
||||||
assert(a[3]() == 3)
|
assert(a[2].get() == 2)
|
||||||
|
a[2].set('a')
|
||||||
|
assert(a[3].get() == 3)
|
||||||
|
assert(a[2].get() == 'a')
|
||||||
|
|
||||||
a = {}
|
a = {}
|
||||||
local t = {"a", "b"}
|
local t = {"a", "b"}
|
||||||
for i = 1, #t do
|
for i = 1, #t do
|
||||||
local k = t[i]
|
local k = t[i]
|
||||||
a[i] = {set = function(x) k=x end,
|
a[i] = {set = function(x, y) i=x; k=y end,
|
||||||
get = function () return i, k end}
|
get = function () return i, k end}
|
||||||
if i == 2 then break end
|
if i == 2 then break end
|
||||||
end
|
end
|
||||||
a[1].set(10)
|
a[1].set(10, 20)
|
||||||
local r,s = a[2].get()
|
local r,s = a[2].get()
|
||||||
assert(r == 2 and s == 'b')
|
assert(r == 2 and s == 'b')
|
||||||
r,s = a[1].get()
|
r,s = a[1].get()
|
||||||
assert(r == 1 and s == 10)
|
assert(r == 10 and s == 20)
|
||||||
a[2].set('a')
|
a[2].set('a', 'b')
|
||||||
r,s = a[2].get()
|
r,s = a[2].get()
|
||||||
assert(r == 2 and s == "a")
|
assert(r == "a" and s == "b")
|
||||||
|
|
||||||
|
|
||||||
-- testing closures with 'for' control variable x break
|
-- testing closures with 'for' control variable x break
|
||||||
local f
|
|
||||||
for i=1,3 do
|
for i=1,3 do
|
||||||
f = function () return i end
|
f = function () return i end
|
||||||
break
|
break
|
||||||
@@ -147,7 +138,7 @@ assert(b('get') == 'xuxu')
|
|||||||
b('set', 10); assert(b('get') == 14)
|
b('set', 10); assert(b('get') == 14)
|
||||||
|
|
||||||
|
|
||||||
local y, w
|
local w
|
||||||
-- testing multi-level closure
|
-- testing multi-level closure
|
||||||
function f(x)
|
function f(x)
|
||||||
return function (y)
|
return function (y)
|
||||||
@@ -159,28 +150,6 @@ y = f(10)
|
|||||||
w = 1.345
|
w = 1.345
|
||||||
assert(y(20)(30) == 60+w)
|
assert(y(20)(30) == 60+w)
|
||||||
|
|
||||||
|
|
||||||
-- testing closures x break
|
|
||||||
do
|
|
||||||
local X, Y
|
|
||||||
local a = math.sin(0)
|
|
||||||
|
|
||||||
while a do
|
|
||||||
local b = 10
|
|
||||||
X = function () return b end -- closure with upvalue
|
|
||||||
if a then break end
|
|
||||||
end
|
|
||||||
|
|
||||||
do
|
|
||||||
local b = 20
|
|
||||||
Y = function () return b end -- closure with upvalue
|
|
||||||
end
|
|
||||||
|
|
||||||
-- upvalues must be different
|
|
||||||
assert(X() == 10 and Y() == 20)
|
|
||||||
end
|
|
||||||
|
|
||||||
|
|
||||||
-- testing closures x repeat-until
|
-- testing closures x repeat-until
|
||||||
|
|
||||||
local a = {}
|
local a = {}
|
||||||
@@ -238,7 +207,6 @@ t()
|
|||||||
-- test for debug manipulation of upvalues
|
-- test for debug manipulation of upvalues
|
||||||
local debug = require'debug'
|
local debug = require'debug'
|
||||||
|
|
||||||
local foo1, foo2, foo3
|
|
||||||
do
|
do
|
||||||
local a , b, c = 3, 5, 7
|
local a , b, c = 3, 5, 7
|
||||||
foo1 = function () return a+b end;
|
foo1 = function () return a+b end;
|
||||||
@@ -251,7 +219,7 @@ end
|
|||||||
|
|
||||||
assert(debug.upvalueid(foo1, 1))
|
assert(debug.upvalueid(foo1, 1))
|
||||||
assert(debug.upvalueid(foo1, 2))
|
assert(debug.upvalueid(foo1, 2))
|
||||||
assert(not debug.upvalueid(foo1, 3))
|
assert(not pcall(debug.upvalueid, foo1, 3))
|
||||||
assert(debug.upvalueid(foo1, 1) == debug.upvalueid(foo2, 2))
|
assert(debug.upvalueid(foo1, 1) == debug.upvalueid(foo2, 2))
|
||||||
assert(debug.upvalueid(foo1, 2) == debug.upvalueid(foo2, 1))
|
assert(debug.upvalueid(foo1, 2) == debug.upvalueid(foo2, 1))
|
||||||
assert(debug.upvalueid(foo3, 1))
|
assert(debug.upvalueid(foo3, 1))
|
||||||
|
|||||||
+86
-351
@@ -1,7 +1,5 @@
|
|||||||
-- $Id: testes/code.lua $
|
-- $Id: code.lua,v 1.42 2016/11/07 13:04:32 roberto Exp $
|
||||||
-- See Copyright Notice in file lua.h
|
-- See Copyright Notice in file all.lua
|
||||||
|
|
||||||
global <const> *
|
|
||||||
|
|
||||||
if T==nil then
|
if T==nil then
|
||||||
(Message or print)('\n >>> testC not active: skipping opcode tests <<<\n')
|
(Message or print)('\n >>> testC not active: skipping opcode tests <<<\n')
|
||||||
@@ -9,23 +7,6 @@ if T==nil then
|
|||||||
end
|
end
|
||||||
print "testing code generation and optimizations"
|
print "testing code generation and optimizations"
|
||||||
|
|
||||||
-- to test constant propagation
|
|
||||||
local k0aux <const> = 0
|
|
||||||
local k0 <const> = k0aux
|
|
||||||
local k1 <const> = 1
|
|
||||||
local k3 <const> = 3
|
|
||||||
local k6 <const> = k3 + (k3 << k0)
|
|
||||||
local kFF0 <const> = 0xFF0
|
|
||||||
local k3_78 <const> = 3.78
|
|
||||||
local x, k3_78_4 <const> = 10, k3_78 / 4
|
|
||||||
assert(x == 10)
|
|
||||||
|
|
||||||
local kx <const> = "x"
|
|
||||||
|
|
||||||
local kTrue <const> = true
|
|
||||||
local kFalse <const> = false
|
|
||||||
|
|
||||||
local kNil <const> = nil
|
|
||||||
|
|
||||||
-- this code gave an error for the code checker
|
-- this code gave an error for the code checker
|
||||||
do
|
do
|
||||||
@@ -46,73 +27,33 @@ end
|
|||||||
|
|
||||||
local function foo ()
|
local function foo ()
|
||||||
local a
|
local a
|
||||||
a = k3;
|
a = 3;
|
||||||
a = 0; a = 0.0; a = -7 + 7
|
a = 0; a = 0.0; a = -7 + 7
|
||||||
a = k3_78/4; a = k3_78_4
|
a = 3.78/4; a = 3.78/4
|
||||||
a = -k3_78/4; a = k3_78/4; a = -3.78/4
|
a = -3.78/4; a = 3.78/4; a = -3.78/4
|
||||||
a = -3.79/4; a = 0.0; a = -0;
|
a = -3.79/4; a = 0.0; a = -0;
|
||||||
a = k3; a = 3.0; a = 3; a = 3.0
|
a = 3; a = 3.0; a = 3; a = 3.0
|
||||||
end
|
end
|
||||||
|
|
||||||
checkKlist(foo, {3.78/4, -3.78/4, -3.79/4})
|
checkKlist(foo, {3, 0, 0.0, 3.78/4, -3.78/4, -3.79/4, 3.0})
|
||||||
|
|
||||||
|
|
||||||
foo = function (f, a)
|
|
||||||
f(100 * 1000)
|
|
||||||
f(100.0 * 1000)
|
|
||||||
f(-100 * 1000)
|
|
||||||
f(-100 * 1000.0)
|
|
||||||
f(100000)
|
|
||||||
f(100000.0)
|
|
||||||
f(-100000)
|
|
||||||
f(-100000.0)
|
|
||||||
end
|
|
||||||
|
|
||||||
checkKlist(foo, {100000, 100000.0, -100000, -100000.0})
|
|
||||||
|
|
||||||
|
|
||||||
-- floats x integers
|
|
||||||
foo = function (t, a)
|
|
||||||
t[a] = 1; t[a] = 1.0
|
|
||||||
t[a] = 1; t[a] = 1.0
|
|
||||||
t[a] = 2; t[a] = 2.0
|
|
||||||
t[a] = 0; t[a] = 0.0
|
|
||||||
t[a] = 1; t[a] = 1.0
|
|
||||||
t[a] = 2; t[a] = 2.0
|
|
||||||
t[a] = 0; t[a] = 0.0
|
|
||||||
end
|
|
||||||
|
|
||||||
checkKlist(foo, {1, 1.0, 2, 2.0, 0, 0.0})
|
|
||||||
|
|
||||||
|
|
||||||
-- testing opcodes
|
-- testing opcodes
|
||||||
|
|
||||||
-- check that 'f' opcodes match '...'
|
function check (f, ...)
|
||||||
local function check (f, ...)
|
|
||||||
local arg = {...}
|
local arg = {...}
|
||||||
local c = T.listcode(f)
|
local c = T.listcode(f)
|
||||||
for i=1, #arg do
|
for i=1, #arg do
|
||||||
local opcode = string.match(c[i], "%u%w+")
|
-- print(arg[i], c[i])
|
||||||
-- print(arg[i], opcode)
|
assert(string.find(c[i], '- '..arg[i]..' *%d'))
|
||||||
assert(arg[i] == opcode)
|
|
||||||
end
|
end
|
||||||
assert(c[#arg+2] == undef)
|
assert(c[#arg+2] == nil)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
||||||
-- check that 'f' opcodes match '...' and that 'f(p) == r'.
|
function checkequal (a, b)
|
||||||
local function checkR (f, p, r, ...)
|
|
||||||
local r1 = f(p)
|
|
||||||
assert(r == r1 and math.type(r) == math.type(r1))
|
|
||||||
check(f, ...)
|
|
||||||
end
|
|
||||||
|
|
||||||
|
|
||||||
-- check that 'a' and 'b' has the same opcodes
|
|
||||||
local function checkequal (a, b)
|
|
||||||
a = T.listcode(a)
|
a = T.listcode(a)
|
||||||
b = T.listcode(b)
|
b = T.listcode(b)
|
||||||
assert(#a == #b)
|
|
||||||
for i = 1, #a do
|
for i = 1, #a do
|
||||||
a[i] = string.gsub(a[i], '%b()', '') -- remove line number
|
a[i] = string.gsub(a[i], '%b()', '') -- remove line number
|
||||||
b[i] = string.gsub(b[i], '%b()', '') -- remove line number
|
b[i] = string.gsub(b[i], '%b()', '') -- remove line number
|
||||||
@@ -122,30 +63,23 @@ end
|
|||||||
|
|
||||||
|
|
||||||
-- some basic instructions
|
-- some basic instructions
|
||||||
check(function () -- function does not create upvalues
|
check(function ()
|
||||||
(function () end){f()}
|
(function () end){f()}
|
||||||
end, 'CLOSURE', 'NEWTABLE', 'EXTRAARG', 'GETTABUP', 'CALL',
|
end, 'CLOSURE', 'NEWTABLE', 'GETTABUP', 'CALL', 'SETLIST', 'CALL', 'RETURN')
|
||||||
'SETLIST', 'CALL', 'RETURN0')
|
|
||||||
|
|
||||||
check(function (x) -- function creates upvalues
|
|
||||||
(function () return x end){f()}
|
|
||||||
end, 'CLOSURE', 'NEWTABLE', 'EXTRAARG', 'GETTABUP', 'CALL',
|
|
||||||
'SETLIST', 'CALL', 'RETURN')
|
|
||||||
|
|
||||||
|
|
||||||
-- sequence of LOADNILs
|
-- sequence of LOADNILs
|
||||||
check(function ()
|
check(function ()
|
||||||
local kNil <const> = nil
|
|
||||||
local a,b,c
|
local a,b,c
|
||||||
local d; local e;
|
local d; local e;
|
||||||
local f,g,h;
|
local f,g,h;
|
||||||
d = nil; d=nil; b=nil; a=kNil; c=nil;
|
d = nil; d=nil; b=nil; a=nil; c=nil;
|
||||||
end, 'LOADNIL', 'RETURN0')
|
end, 'LOADNIL', 'RETURN')
|
||||||
|
|
||||||
check(function ()
|
check(function ()
|
||||||
local a,b,c,d = 1,1,1,1
|
local a,b,c,d = 1,1,1,1
|
||||||
d=nil;c=nil;b=nil;a=nil
|
d=nil;c=nil;b=nil;a=nil
|
||||||
end, 'LOADI', 'LOADI', 'LOADI', 'LOADI', 'LOADNIL', 'RETURN0')
|
end, 'LOADK', 'LOADK', 'LOADK', 'LOADK', 'LOADNIL', 'RETURN')
|
||||||
|
|
||||||
do
|
do
|
||||||
local a,b,c,d = 1,1,1,1
|
local a,b,c,d = 1,1,1,1
|
||||||
@@ -155,225 +89,97 @@ end
|
|||||||
|
|
||||||
|
|
||||||
-- single return
|
-- single return
|
||||||
check (function (a,b,c) return a end, 'RETURN1')
|
check (function (a,b,c) return a end, 'RETURN')
|
||||||
|
|
||||||
|
|
||||||
-- infinite loops
|
-- infinite loops
|
||||||
check(function () while kTrue do local a = -1 end end,
|
check(function () while true do local a = -1 end end,
|
||||||
'LOADI', 'JMP', 'RETURN0')
|
'LOADK', 'JMP', 'RETURN')
|
||||||
|
|
||||||
check(function () while 1 do local a = -1 end end,
|
check(function () while 1 do local a = -1 end end,
|
||||||
'LOADI', 'JMP', 'RETURN0')
|
'LOADK', 'JMP', 'RETURN')
|
||||||
|
|
||||||
check(function () repeat local x = 1 until true end,
|
check(function () repeat local x = 1 until true end,
|
||||||
'LOADI', 'RETURN0')
|
'LOADK', 'RETURN')
|
||||||
|
|
||||||
|
|
||||||
-- concat optimization
|
-- concat optimization
|
||||||
check(function (a,b,c,d) return a..b..c..d end,
|
check(function (a,b,c,d) return a..b..c..d end,
|
||||||
'MOVE', 'MOVE', 'MOVE', 'MOVE', 'CONCAT', 'RETURN1')
|
'MOVE', 'MOVE', 'MOVE', 'MOVE', 'CONCAT', 'RETURN')
|
||||||
|
|
||||||
-- not
|
-- not
|
||||||
check(function () return not not nil end, 'LOADFALSE', 'RETURN1')
|
check(function () return not not nil end, 'LOADBOOL', 'RETURN')
|
||||||
check(function () return not not kFalse end, 'LOADFALSE', 'RETURN1')
|
check(function () return not not false end, 'LOADBOOL', 'RETURN')
|
||||||
check(function () return not not true end, 'LOADTRUE', 'RETURN1')
|
check(function () return not not true end, 'LOADBOOL', 'RETURN')
|
||||||
check(function () return not not k3 end, 'LOADTRUE', 'RETURN1')
|
check(function () return not not 1 end, 'LOADBOOL', 'RETURN')
|
||||||
|
|
||||||
-- direct access to locals
|
-- direct access to locals
|
||||||
check(function ()
|
check(function ()
|
||||||
local a,b,c,d
|
local a,b,c,d
|
||||||
a = b*a
|
a = b*2
|
||||||
c.x, a[b] = -((a + d/b - a[b]) ^ a.x), b
|
c[2], a[b] = -((a + d/2 - a[b]) ^ a.x), b
|
||||||
end,
|
end,
|
||||||
'LOADNIL',
|
'LOADNIL',
|
||||||
'MUL', 'MMBIN',
|
'MUL',
|
||||||
'DIV', 'MMBIN', 'ADD', 'MMBIN', 'GETTABLE', 'SUB', 'MMBIN',
|
'DIV', 'ADD', 'GETTABLE', 'SUB', 'GETTABLE', 'POW',
|
||||||
'GETFIELD', 'POW', 'MMBIN', 'UNM', 'SETTABLE', 'SETFIELD', 'RETURN0')
|
'UNM', 'SETTABLE', 'SETTABLE', 'RETURN')
|
||||||
|
|
||||||
|
|
||||||
-- direct access to constants
|
-- direct access to constants
|
||||||
check(function ()
|
check(function ()
|
||||||
local a,b
|
local a,b
|
||||||
local c = kNil
|
a.x = 3.2
|
||||||
a[kx] = 3.2
|
|
||||||
a.x = b
|
a.x = b
|
||||||
a[b] = 'x'
|
a[b] = 'x'
|
||||||
end,
|
end,
|
||||||
'LOADNIL', 'SETFIELD', 'SETFIELD', 'SETTABLE', 'RETURN0')
|
'LOADNIL', 'SETTABLE', 'SETTABLE', 'SETTABLE', 'RETURN')
|
||||||
|
|
||||||
-- "get/set table" with numeric indices
|
|
||||||
check(function (a)
|
|
||||||
local k255 <const> = 255
|
|
||||||
a[1] = a[100]
|
|
||||||
a[k255] = a[256]
|
|
||||||
a[256] = 5
|
|
||||||
end,
|
|
||||||
'GETI', 'SETI',
|
|
||||||
'LOADI', 'GETTABLE', 'SETI',
|
|
||||||
'LOADI', 'SETTABLE', 'RETURN0')
|
|
||||||
|
|
||||||
check(function ()
|
check(function ()
|
||||||
local a,b
|
local a,b
|
||||||
a = a - a
|
a = 1 - a
|
||||||
b = a/a
|
b = 1/a
|
||||||
b = 5-4
|
b = 5-4
|
||||||
end,
|
end,
|
||||||
'LOADNIL', 'SUB', 'MMBIN', 'DIV', 'MMBIN', 'LOADI', 'RETURN0')
|
'LOADNIL', 'SUB', 'DIV', 'LOADK', 'RETURN')
|
||||||
|
|
||||||
check(function ()
|
check(function ()
|
||||||
local a,b
|
local a,b
|
||||||
a[kTrue] = false
|
a[true] = false
|
||||||
end,
|
end,
|
||||||
'LOADNIL', 'LOADTRUE', 'SETTABLE', 'RETURN0')
|
'LOADNIL', 'SETTABLE', 'RETURN')
|
||||||
|
|
||||||
|
|
||||||
-- equalities
|
|
||||||
checkR(function (a) if a == 1 then return 2 end end, 1, 2,
|
|
||||||
'EQI', 'JMP', 'LOADI', 'RETURN1')
|
|
||||||
|
|
||||||
checkR(function (a) if -4.0 == a then return 2 end end, -4, 2,
|
|
||||||
'EQI', 'JMP', 'LOADI', 'RETURN1')
|
|
||||||
|
|
||||||
checkR(function (a) if a == "hi" then return 2 end end, 10, nil,
|
|
||||||
'EQK', 'JMP', 'LOADI', 'RETURN1')
|
|
||||||
|
|
||||||
checkR(function (a) if a == 10000 then return 2 end end, 1, nil,
|
|
||||||
'EQK', 'JMP', 'LOADI', 'RETURN1') -- number too large
|
|
||||||
|
|
||||||
checkR(function (a) if -10000 == a then return 2 end end, -10000, 2,
|
|
||||||
'EQK', 'JMP', 'LOADI', 'RETURN1') -- number too large
|
|
||||||
|
|
||||||
-- comparisons
|
|
||||||
|
|
||||||
checkR(function (a) if -10 <= a then return 2 end end, -10, 2,
|
|
||||||
'GEI', 'JMP', 'LOADI', 'RETURN1')
|
|
||||||
|
|
||||||
checkR(function (a) if 128.0 > a then return 2 end end, 129, nil,
|
|
||||||
'LTI', 'JMP', 'LOADI', 'RETURN1')
|
|
||||||
|
|
||||||
checkR(function (a) if -127.0 < a then return 2 end end, -127, nil,
|
|
||||||
'GTI', 'JMP', 'LOADI', 'RETURN1')
|
|
||||||
|
|
||||||
checkR(function (a) if 10 < a then return 2 end end, 11, 2,
|
|
||||||
'GTI', 'JMP', 'LOADI', 'RETURN1')
|
|
||||||
|
|
||||||
checkR(function (a) if 129 < a then return 2 end end, 130, 2,
|
|
||||||
'LOADI', 'LT', 'JMP', 'LOADI', 'RETURN1')
|
|
||||||
|
|
||||||
checkR(function (a) if a >= 23.0 then return 2 end end, 25, 2,
|
|
||||||
'GEI', 'JMP', 'LOADI', 'RETURN1')
|
|
||||||
|
|
||||||
checkR(function (a) if a >= 23.1 then return 2 end end, 0, nil,
|
|
||||||
'LOADK', 'LE', 'JMP', 'LOADI', 'RETURN1')
|
|
||||||
|
|
||||||
checkR(function (a) if a > 2300.0 then return 2 end end, 0, nil,
|
|
||||||
'LOADF', 'LT', 'JMP', 'LOADI', 'RETURN1')
|
|
||||||
|
|
||||||
|
|
||||||
-- constant folding
|
-- constant folding
|
||||||
local function checkK (func, val)
|
local function checkK (func, val)
|
||||||
check(func, 'LOADK', 'RETURN1')
|
check(func, 'LOADK', 'RETURN')
|
||||||
checkKlist(func, {val})
|
local k = T.listk(func)
|
||||||
|
assert(#k == 1 and k[1] == val and math.type(k[1]) == math.type(val))
|
||||||
assert(func() == val)
|
assert(func() == val)
|
||||||
end
|
end
|
||||||
|
checkK(function () return 0.0 end, 0.0)
|
||||||
local function checkI (func, val)
|
checkK(function () return 0 end, 0)
|
||||||
check(func, 'LOADI', 'RETURN1')
|
checkK(function () return -0//1 end, 0)
|
||||||
checkKlist(func, {})
|
|
||||||
assert(func() == val)
|
|
||||||
end
|
|
||||||
|
|
||||||
local function checkF (func, val)
|
|
||||||
check(func, 'LOADF', 'RETURN1')
|
|
||||||
checkKlist(func, {})
|
|
||||||
assert(func() == val)
|
|
||||||
end
|
|
||||||
|
|
||||||
checkF(function () return 0.0 end, 0.0)
|
|
||||||
checkI(function () return k0 end, 0)
|
|
||||||
checkI(function () return -k0//1 end, 0)
|
|
||||||
checkK(function () return 3^-1 end, 1/3)
|
checkK(function () return 3^-1 end, 1/3)
|
||||||
checkK(function () return (1 + 1)^(50 + 50) end, 2^100)
|
checkK(function () return (1 + 1)^(50 + 50) end, 2^100)
|
||||||
checkK(function () return (-2)^(31 - 2) end, -0x20000000 + 0.0)
|
checkK(function () return (-2)^(31 - 2) end, -0x20000000 + 0.0)
|
||||||
checkF(function () return (-k3^0 + 5) // 3.0 end, 1.0)
|
checkK(function () return (-3^0 + 5) // 3.0 end, 1.0)
|
||||||
checkI(function () return -k3 % 5 end, 2)
|
checkK(function () return -3 % 5 end, 2)
|
||||||
checkF(function () return -((2.0^8 + -(-1)) % 8)/2 * 4 - 3 end, -5.0)
|
checkK(function () return -((2.0^8 + -(-1)) % 8)/2 * 4 - 3 end, -5.0)
|
||||||
checkF(function () return -((2^8 + -(-1)) % 8)//2 * 4 - 3 end, -7.0)
|
checkK(function () return -((2^8 + -(-1)) % 8)//2 * 4 - 3 end, -7.0)
|
||||||
checkI(function () return 0xF0.0 | 0xCC.0 ~ 0xAA & 0xFD end, 0xF4)
|
checkK(function () return 0xF0.0 | 0xCC.0 ~ 0xAA & 0xFD end, 0xF4)
|
||||||
checkI(function () return ~(~kFF0 | kFF0) end, 0)
|
checkK(function () return ~(~0xFF0 | 0xFF0) end, 0)
|
||||||
checkI(function () return ~~-1024.0 end, -1024)
|
checkK(function () return ~~-100024.0 end, -100024)
|
||||||
checkI(function () return ((100 << k6) << -4) >> 2 end, 100)
|
checkK(function () return ((100 << 6) << -4) >> 2 end, 100)
|
||||||
|
|
||||||
-- borders around MAXARG_sBx ((((1 << 17) - 1) >> 1) == 65535)
|
|
||||||
local a = 17; local sbx = ((1 << a) - 1) >> 1 -- avoid folding
|
|
||||||
local border <const> = 65535
|
|
||||||
checkI(function () return border end, sbx)
|
|
||||||
checkI(function () return -border end, -sbx)
|
|
||||||
checkI(function () return border + 1 end, sbx + 1)
|
|
||||||
checkK(function () return border + 2 end, sbx + 2)
|
|
||||||
checkK(function () return -(border + 1) end, -(sbx + 1))
|
|
||||||
|
|
||||||
local border <const> = 65535.0
|
|
||||||
checkF(function () return border end, sbx + 0.0)
|
|
||||||
checkF(function () return -border end, -sbx + 0.0)
|
|
||||||
checkF(function () return border + 1 end, (sbx + 1.0))
|
|
||||||
checkK(function () return border + 2 end, (sbx + 2.0))
|
|
||||||
checkK(function () return -(border + 1) end, -(sbx + 1.0))
|
|
||||||
|
|
||||||
|
|
||||||
-- immediate operands
|
-- no foldings
|
||||||
checkR(function (x) return x + k1 end, 10, 11, 'ADDI', 'MMBINI', 'RETURN1')
|
check(function () return -0.0 end, 'LOADK', 'UNM', 'RETURN')
|
||||||
checkR(function (x) return x - 127 end, 10, -117, 'ADDI', 'MMBINI', 'RETURN1')
|
check(function () return 3/0 end, 'DIV', 'RETURN')
|
||||||
checkR(function (x) return 128 + x end, 0.0, 128.0,
|
check(function () return 0%0 end, 'MOD', 'RETURN')
|
||||||
'ADDI', 'MMBINI', 'RETURN1')
|
check(function () return -4//0 end, 'IDIV', 'RETURN')
|
||||||
checkR(function (x) return x * -127 end, -1.0, 127.0,
|
|
||||||
'MULK', 'MMBINK', 'RETURN1')
|
|
||||||
checkR(function (x) return 20 * x end, 2, 40, 'MULK', 'MMBINK', 'RETURN1')
|
|
||||||
checkR(function (x) return x ^ -2 end, 2, 0.25, 'POWK', 'MMBINK', 'RETURN1')
|
|
||||||
checkR(function (x) return x / 40 end, 40, 1.0, 'DIVK', 'MMBINK', 'RETURN1')
|
|
||||||
checkR(function (x) return x // 1 end, 10.0, 10.0,
|
|
||||||
'IDIVK', 'MMBINK', 'RETURN1')
|
|
||||||
checkR(function (x) return x % (100 - 10) end, 91, 1,
|
|
||||||
'MODK', 'MMBINK', 'RETURN1')
|
|
||||||
checkR(function (x) return k1 << x end, 3, 8, 'SHLI', 'MMBINI', 'RETURN1')
|
|
||||||
checkR(function (x) return x << 127 end, 10, 0, 'SHRI', 'MMBINI', 'RETURN1')
|
|
||||||
checkR(function (x) return x << -127 end, 10, 0, 'SHRI', 'MMBINI', 'RETURN1')
|
|
||||||
checkR(function (x) return x >> 128 end, 8, 0, 'SHRI', 'MMBINI', 'RETURN1')
|
|
||||||
checkR(function (x) return x >> -127 end, 8, 0, 'SHRI', 'MMBINI', 'RETURN1')
|
|
||||||
checkR(function (x) return x & 1 end, 9, 1, 'BANDK', 'MMBINK', 'RETURN1')
|
|
||||||
checkR(function (x) return 10 | x end, 1, 11, 'BORK', 'MMBINK', 'RETURN1')
|
|
||||||
checkR(function (x) return -10 ~ x end, -1, 9, 'BXORK', 'MMBINK', 'RETURN1')
|
|
||||||
|
|
||||||
-- K operands in arithmetic operations
|
|
||||||
checkR(function (x) return x + 0.0 end, 1, 1.0, 'ADDK', 'MMBINK', 'RETURN1')
|
|
||||||
-- check(function (x) return 128 + x end, 'ADDK', 'MMBINK', 'RETURN1')
|
|
||||||
checkR(function (x) return x * -10000 end, 2, -20000,
|
|
||||||
'MULK', 'MMBINK', 'RETURN1')
|
|
||||||
-- check(function (x) return 20 * x end, 'MULK', 'MMBINK', 'RETURN1')
|
|
||||||
checkR(function (x) return x ^ 0.5 end, 4, 2.0, 'POWK', 'MMBINK', 'RETURN1')
|
|
||||||
checkR(function (x) return x / 2.0 end, 4, 2.0, 'DIVK', 'MMBINK', 'RETURN1')
|
|
||||||
checkR(function (x) return x // 10000 end, 10000, 1,
|
|
||||||
'IDIVK', 'MMBINK', 'RETURN1')
|
|
||||||
checkR(function (x) return x % (100.0 - 10) end, 91, 1.0,
|
|
||||||
'MODK', 'MMBINK', 'RETURN1')
|
|
||||||
|
|
||||||
-- no foldings (and immediate operands)
|
|
||||||
check(function () return -0.0 end, 'LOADF', 'UNM', 'RETURN1')
|
|
||||||
check(function () return k3/0 end, 'LOADI', 'DIVK', 'MMBINK', 'RETURN1')
|
|
||||||
check(function () return 0%0 end, 'LOADI', 'MODK', 'MMBINK', 'RETURN1')
|
|
||||||
check(function () return -4//0 end, 'LOADI', 'IDIVK', 'MMBINK', 'RETURN1')
|
|
||||||
check(function (x) return x >> 2.0 end, 'LOADF', 'SHR', 'MMBIN', 'RETURN1')
|
|
||||||
check(function (x) return x << 128 end, 'LOADI', 'SHL', 'MMBIN', 'RETURN1')
|
|
||||||
check(function (x) return x & 2.0 end, 'LOADF', 'BAND', 'MMBIN', 'RETURN1')
|
|
||||||
|
|
||||||
-- basic 'for' loops
|
|
||||||
check(function () for i = -10, 10.5 do end end,
|
|
||||||
'LOADI', 'LOADK', 'LOADI', 'FORPREP', 'FORLOOP', 'RETURN0')
|
|
||||||
check(function () for i = 0xfffffff, 10.0, 1 do end end,
|
|
||||||
'LOADK', 'LOADF', 'LOADI', 'FORPREP', 'FORLOOP', 'RETURN0')
|
|
||||||
|
|
||||||
-- bug in constant folding for 5.1
|
-- bug in constant folding for 5.1
|
||||||
check(function () return -nil end, 'LOADNIL', 'UNM', 'RETURN1')
|
check(function () return -nil end, 'LOADNIL', 'UNM', 'RETURN')
|
||||||
|
|
||||||
|
|
||||||
check(function ()
|
check(function ()
|
||||||
@@ -382,123 +188,52 @@ check(function ()
|
|||||||
b[a], a = c, b
|
b[a], a = c, b
|
||||||
a, b = c, a
|
a, b = c, a
|
||||||
a = a
|
a = a
|
||||||
end,
|
end,
|
||||||
'LOADNIL',
|
'LOADNIL',
|
||||||
'MOVE', 'MOVE', 'SETTABLE',
|
'MOVE', 'MOVE', 'SETTABLE',
|
||||||
'MOVE', 'MOVE', 'MOVE', 'SETTABLE',
|
'MOVE', 'MOVE', 'MOVE', 'SETTABLE',
|
||||||
'MOVE', 'MOVE', 'MOVE',
|
'MOVE', 'MOVE', 'MOVE',
|
||||||
-- no code for a = a
|
-- no code for a = a
|
||||||
'RETURN0')
|
'RETURN')
|
||||||
|
|
||||||
|
|
||||||
-- x == nil , x ~= nil
|
-- x == nil , x ~= nil
|
||||||
-- checkequal(function (b) if (a==nil) then a=1 end; if a~=nil then a=1 end end,
|
checkequal(function () if (a==nil) then a=1 end; if a~=nil then a=1 end end,
|
||||||
-- function () if (a==9) then a=1 end; if a~=9 then a=1 end end)
|
function () if (a==9) then a=1 end; if a~=9 then a=1 end end)
|
||||||
|
|
||||||
-- check(function () if a==nil then a='a' end end,
|
check(function () if a==nil then a='a' end end,
|
||||||
-- 'GETTABUP', 'EQ', 'JMP', 'SETTABUP', 'RETURN')
|
'GETTABUP', 'EQ', 'JMP', 'SETTABUP', 'RETURN')
|
||||||
|
|
||||||
do -- tests for table access in upvalues
|
|
||||||
local t
|
|
||||||
check(function () t[kx] = t.y end, 'GETTABUP', 'SETTABUP')
|
|
||||||
check(function (a) t[a()] = t[a()] end,
|
|
||||||
'MOVE', 'CALL', 'GETUPVAL', 'MOVE', 'CALL',
|
|
||||||
'GETUPVAL', 'GETTABLE', 'SETTABLE')
|
|
||||||
end
|
|
||||||
|
|
||||||
-- de morgan
|
-- de morgan
|
||||||
checkequal(function () local a, b; if not (a or b) then b=a end end,
|
checkequal(function () local a; if not (a or b) then b=a end end,
|
||||||
function () local a, b; if (not a and not b) then b=a end end)
|
function () local a; if (not a and not b) then b=a end end)
|
||||||
|
|
||||||
checkequal(function (l) local a; return 0 <= a and a <= l end,
|
checkequal(function (l) local a; return 0 <= a and a <= l end,
|
||||||
function (l) local a; return not (not(a >= 0) or not(a <= l)) end)
|
function (l) local a; return not (not(a >= 0) or not(a <= l)) end)
|
||||||
|
|
||||||
|
|
||||||
check(function (a, b)
|
-- if-goto optimizations
|
||||||
while a do
|
check(function (a, b, c, d, e)
|
||||||
if b then break else a = a + 1 end
|
if a == b then goto l1
|
||||||
|
elseif a == c then goto l2
|
||||||
|
elseif a == d then goto l2
|
||||||
|
else if a == e then goto l3
|
||||||
|
else goto l3
|
||||||
|
end
|
||||||
end
|
end
|
||||||
end,
|
::l1:: ::l2:: ::l3:: ::l4::
|
||||||
'TEST', 'JMP', 'TEST', 'JMP', 'JMP', 'CLOSE', 'JMP', 'ADDI', 'MMBINI', 'JMP', 'RETURN0')
|
end, 'EQ', 'JMP', 'EQ', 'JMP', 'EQ', 'JMP', 'EQ', 'JMP', 'JMP', 'RETURN')
|
||||||
|
|
||||||
check(function ()
|
checkequal(
|
||||||
do
|
function (a) while a < 10 do a = a + 1 end end,
|
||||||
goto exit -- don't need to close
|
function (a) ::L2:: if not(a < 10) then goto L1 end; a = a + 1;
|
||||||
local x <close> = nil
|
goto L2; ::L1:: end
|
||||||
goto exit -- must close
|
)
|
||||||
end
|
|
||||||
::exit::
|
|
||||||
end, 'JMP', 'CLOSE', 'LOADNIL', 'TBC',
|
|
||||||
'CLOSE', 'JMP', 'CLOSE', 'RETURN')
|
|
||||||
|
|
||||||
checkequal(function () return 6 or true or nil end,
|
checkequal(
|
||||||
function () return k6 or kTrue or kNil end)
|
function (a) while a < 10 do a = a + 1 end end,
|
||||||
|
function (a) while true do if not(a < 10) then break end; a = a + 1; end end
|
||||||
checkequal(function () return 6 and true or nil end,
|
)
|
||||||
function () return k6 and kTrue or kNil end)
|
|
||||||
|
|
||||||
|
|
||||||
do -- string constants
|
|
||||||
local k0 <const> = "00000000000000000000000000000000000000000000000000"
|
|
||||||
local function f1 ()
|
|
||||||
local k <const> = k0
|
|
||||||
return function ()
|
|
||||||
return function () return k end
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
local f2 = f1()
|
|
||||||
local f3 = f2()
|
|
||||||
assert(f3() == k0)
|
|
||||||
checkK(f3, k0)
|
|
||||||
-- string is not needed by other functions
|
|
||||||
assert(T.listk(f1)[1] == nil)
|
|
||||||
assert(T.listk(f2)[1] == nil)
|
|
||||||
end
|
|
||||||
|
|
||||||
|
|
||||||
do -- check number of available registers
|
|
||||||
-- 1 register for local + 1 for function + 252 arguments
|
|
||||||
local source = "local a; return a(" .. string.rep("a, ", 252) .. "a)"
|
|
||||||
local prog = T.listcode(assert(load(source)))
|
|
||||||
-- maximum valid register is 254
|
|
||||||
for i = 1, 254 do
|
|
||||||
assert(string.find(prog[2 + i], "MOVE%s*" .. i))
|
|
||||||
end
|
|
||||||
-- one more argument would need register #255 (but that is reserved)
|
|
||||||
source = "local a; return a(" .. string.rep("a, ", 253) .. "a)"
|
|
||||||
local _, msg = load(source)
|
|
||||||
assert(string.find(msg, "too many registers"))
|
|
||||||
end
|
|
||||||
|
|
||||||
|
|
||||||
do -- basic check for SETLIST
|
|
||||||
-- create a list constructor with 50 elements
|
|
||||||
local source = "local a; return {" .. string.rep("a, ", 50) .. "}"
|
|
||||||
local func = assert(load(source))
|
|
||||||
local code = table.concat(T.listcode(func), "\n")
|
|
||||||
local _, count = string.gsub(code, "SETLIST", "")
|
|
||||||
-- code uses only 1 SETLIST for the constructor
|
|
||||||
assert(count == 1)
|
|
||||||
end
|
|
||||||
|
|
||||||
|
|
||||||
do print("testing code for integer limits")
|
|
||||||
local function checkints (n)
|
|
||||||
local source = string.format(
|
|
||||||
"local a = {[true] = 0X%x}; return a[true]", n)
|
|
||||||
local f = assert(load(source))
|
|
||||||
checkKlist(f, {n})
|
|
||||||
assert(f() == n)
|
|
||||||
f = load(string.dump(f))
|
|
||||||
assert(f() == n)
|
|
||||||
end
|
|
||||||
|
|
||||||
checkints(math.maxinteger)
|
|
||||||
checkints(math.mininteger)
|
|
||||||
checkints(-1)
|
|
||||||
|
|
||||||
end
|
|
||||||
|
|
||||||
print 'OK'
|
print 'OK'
|
||||||
|
|
||||||
|
|||||||
+30
-123
@@ -1,5 +1,5 @@
|
|||||||
-- $Id: testes/constructs.lua $
|
-- $Id: constructs.lua,v 1.41 2016/11/07 13:11:28 roberto Exp $
|
||||||
-- See Copyright Notice in file lua.h
|
-- See Copyright Notice in file all.lua
|
||||||
|
|
||||||
;;print "testing syntax";;
|
;;print "testing syntax";;
|
||||||
|
|
||||||
@@ -11,7 +11,6 @@ local function checkload (s, msg)
|
|||||||
end
|
end
|
||||||
|
|
||||||
-- testing semicollons
|
-- testing semicollons
|
||||||
local a
|
|
||||||
do ;;; end
|
do ;;; end
|
||||||
; do ; a = 3; assert(a == 3) end;
|
; do ; a = 3; assert(a == 3) end;
|
||||||
;
|
;
|
||||||
@@ -33,6 +32,10 @@ assert(-3%5 == 2 and -3+5 == 2)
|
|||||||
assert(2*1+3/3 == 3 and 1+2 .. 3*1 == "33");
|
assert(2*1+3/3 == 3 and 1+2 .. 3*1 == "33");
|
||||||
assert(not(2+1 > 3*1) and "a".."b" > "a");
|
assert(not(2+1 > 3*1) and "a".."b" > "a");
|
||||||
|
|
||||||
|
assert("7" .. 3 << 1 == 146)
|
||||||
|
assert(10 >> 1 .. "9" == 0)
|
||||||
|
assert(10 | 1 .. "9" == 27)
|
||||||
|
|
||||||
assert(0xF0 | 0xCC ~ 0xAA & 0xFD == 0xF4)
|
assert(0xF0 | 0xCC ~ 0xAA & 0xFD == 0xF4)
|
||||||
assert(0xFD & 0xAA ~ 0xCC | 0xF0 == 0xF4)
|
assert(0xFD & 0xAA ~ 0xCC | 0xF0 == 0xF4)
|
||||||
assert(0xF0 & 0x0F + 1 == 0x10)
|
assert(0xF0 & 0x0F + 1 == 0x10)
|
||||||
@@ -50,87 +53,26 @@ assert((((nil and true) or false) and true) == false)
|
|||||||
|
|
||||||
local a,b = 1,nil;
|
local a,b = 1,nil;
|
||||||
assert(-(1 or 2) == -1 and (1 and 2)+(-1.25 or -4) == 0.75);
|
assert(-(1 or 2) == -1 and (1 and 2)+(-1.25 or -4) == 0.75);
|
||||||
local x = ((b or a)+1 == 2 and (10 or a)+1 == 11); assert(x);
|
x = ((b or a)+1 == 2 and (10 or a)+1 == 11); assert(x);
|
||||||
x = (((2<3) or 1) == true and (2<3 and 4) == 4); assert(x);
|
x = (((2<3) or 1) == true and (2<3 and 4) == 4); assert(x);
|
||||||
|
|
||||||
local x, y = 1, 2;
|
x,y=1,2;
|
||||||
assert((x>y) and x or y == 2);
|
assert((x>y) and x or y == 2);
|
||||||
x,y=2,1;
|
x,y=2,1;
|
||||||
assert((x>y) and x or y == 2);
|
assert((x>y) and x or y == 2);
|
||||||
|
|
||||||
assert(1234567890 == tonumber('1234567890') and 1234567890+1 == 1234567891)
|
assert(1234567890 == tonumber('1234567890') and 1234567890+1 == 1234567891)
|
||||||
|
|
||||||
do -- testing operators with different kinds of constants
|
|
||||||
-- operands to consider:
|
|
||||||
-- * fit in register
|
|
||||||
-- * constant doesn't fit in register
|
|
||||||
-- * floats with integral values
|
|
||||||
local operand = {3, 100, 5.0, -10, -5.0, 10000, -10000}
|
|
||||||
local operator = {"+", "-", "*", "/", "//", "%", "^",
|
|
||||||
"&", "|", "^", "<<", ">>",
|
|
||||||
"==", "~=", "<", ">", "<=", ">=",}
|
|
||||||
for _, op in ipairs(operator) do
|
|
||||||
local f = assert(load(string.format([[return function (x,y)
|
|
||||||
return x %s y
|
|
||||||
end]], op)))();
|
|
||||||
for _, o1 in ipairs(operand) do
|
|
||||||
for _, o2 in ipairs(operand) do
|
|
||||||
local gab = f(o1, o2)
|
|
||||||
|
|
||||||
_ENV.XX = o1
|
|
||||||
local code = string.format("return XX %s %s", op, o2)
|
|
||||||
local res = assert(load(code))()
|
|
||||||
assert(res == gab)
|
|
||||||
|
|
||||||
_ENV.XX = o2
|
|
||||||
code = string.format("return (%s) %s XX", o1, op)
|
|
||||||
res = assert(load(code))()
|
|
||||||
assert(res == gab)
|
|
||||||
|
|
||||||
code = string.format("return (%s) %s %s", o1, op, o2)
|
|
||||||
res = assert(load(code))()
|
|
||||||
assert(res == gab)
|
|
||||||
end
|
|
||||||
end
|
|
||||||
end
|
|
||||||
_ENV.XX = nil
|
|
||||||
end
|
|
||||||
|
|
||||||
|
|
||||||
-- silly loops
|
-- silly loops
|
||||||
repeat until 1; repeat until true;
|
repeat until 1; repeat until true;
|
||||||
while false do end; while nil do end;
|
while false do end; while nil do end;
|
||||||
|
|
||||||
do -- test old bug (first name could not be an `upvalue')
|
do -- test old bug (first name could not be an `upvalue')
|
||||||
local a; local function f(x) x={a=1}; x={x=1}; x={G=1} end
|
local a; function f(x) x={a=1}; x={x=1}; x={G=1} end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
function f (i)
|
||||||
do -- bug since 5.4.0
|
|
||||||
-- create code with a table using more than 256 constants
|
|
||||||
local code = {"local x = {"}
|
|
||||||
for i = 1, 257 do
|
|
||||||
code[#code + 1] = i .. ".1,"
|
|
||||||
end
|
|
||||||
code[#code + 1] = "};"
|
|
||||||
code = table.concat(code)
|
|
||||||
|
|
||||||
-- add "ret" to the end of that code and checks that
|
|
||||||
-- it produces the expected value "val"
|
|
||||||
local function check (ret, val)
|
|
||||||
local code = code .. ret
|
|
||||||
code = load(code)
|
|
||||||
assert(code() == val)
|
|
||||||
end
|
|
||||||
|
|
||||||
check("return (1 ~ (2 or 3))", 1 ~ 2)
|
|
||||||
check("return (1 | (2 or 3))", 1 | 2)
|
|
||||||
check("return (1 + (2 or 3))", 1 + 2)
|
|
||||||
check("return (1 << (2 or 3))", 1 << 2)
|
|
||||||
end
|
|
||||||
|
|
||||||
|
|
||||||
local function f (i)
|
|
||||||
if type(i) ~= 'number' then return i,'jojo'; end;
|
if type(i) ~= 'number' then return i,'jojo'; end;
|
||||||
if i > 0 then return i, f(i-1); end;
|
if i > 0 then return i, f(i-1); end;
|
||||||
end
|
end
|
||||||
@@ -156,10 +98,10 @@ end
|
|||||||
assert(f(3) == 'a' and f(12) == 'b' and f(26) == 'c' and f(100) == nil)
|
assert(f(3) == 'a' and f(12) == 'b' and f(26) == 'c' and f(100) == nil)
|
||||||
|
|
||||||
for i=1,1000 do break; end;
|
for i=1,1000 do break; end;
|
||||||
local n=100;
|
n=100;
|
||||||
local i=3;
|
i=3;
|
||||||
local t = {};
|
t = {};
|
||||||
local a=nil
|
a=nil
|
||||||
while not a do
|
while not a do
|
||||||
a=0; for i=1,n do for i=i,1,-1 do a=a+1; t[i]=1; end; end;
|
a=0; for i=1,n do for i=i,1,-1 do a=a+1; t[i]=1; end; end;
|
||||||
end
|
end
|
||||||
@@ -202,14 +144,14 @@ a={y=1}
|
|||||||
x = {a.y}
|
x = {a.y}
|
||||||
assert(x[1] == 1)
|
assert(x[1] == 1)
|
||||||
|
|
||||||
local function f (i)
|
function f(i)
|
||||||
while 1 do
|
while 1 do
|
||||||
if i>0 then i=i-1;
|
if i>0 then i=i-1;
|
||||||
else return; end;
|
else return; end;
|
||||||
end;
|
end;
|
||||||
end;
|
end;
|
||||||
|
|
||||||
local function g(i)
|
function g(i)
|
||||||
while 1 do
|
while 1 do
|
||||||
if i>0 then i=i-1
|
if i>0 then i=i-1
|
||||||
else return end
|
else return end
|
||||||
@@ -237,28 +179,6 @@ assert(a==1 and b==nil)
|
|||||||
|
|
||||||
print'+';
|
print'+';
|
||||||
|
|
||||||
do -- testing constants
|
|
||||||
local prog <const> = [[local x <XXX> = 10]]
|
|
||||||
checkload(prog, "unknown attribute 'XXX'")
|
|
||||||
|
|
||||||
checkload([[local xxx <const> = 20; xxx = 10]],
|
|
||||||
":1: attempt to assign to const variable 'xxx'")
|
|
||||||
|
|
||||||
checkload([[
|
|
||||||
local xx;
|
|
||||||
local xxx <const> = 20;
|
|
||||||
local yyy;
|
|
||||||
local function foo ()
|
|
||||||
local abc = xx + yyy + xxx;
|
|
||||||
return function () return function () xxx = yyy end end
|
|
||||||
end
|
|
||||||
]], ":6: attempt to assign to const variable 'xxx'")
|
|
||||||
|
|
||||||
checkload([[
|
|
||||||
local x <close> = nil
|
|
||||||
x = io.open()
|
|
||||||
]], ":2: attempt to assign to const variable 'x'")
|
|
||||||
end
|
|
||||||
|
|
||||||
f = [[
|
f = [[
|
||||||
return function ( a , b , c , d , e )
|
return function ( a , b , c , d , e )
|
||||||
@@ -274,7 +194,7 @@ function g (a,b,c,d,e)
|
|||||||
if not (a>=b or c or d and e or nil) then return 0; else return 1; end;
|
if not (a>=b or c or d and e or nil) then return 0; else return 1; end;
|
||||||
end
|
end
|
||||||
|
|
||||||
local function h (a,b,c,d,e)
|
function h (a,b,c,d,e)
|
||||||
while (a>=b or c or (d and e) or nil) do return 1; end;
|
while (a>=b or c or (d and e) or nil) do return 1; end;
|
||||||
return 0;
|
return 0;
|
||||||
end;
|
end;
|
||||||
@@ -302,7 +222,7 @@ do
|
|||||||
assert(a==2)
|
assert(a==2)
|
||||||
end
|
end
|
||||||
|
|
||||||
local function F (a)
|
function F(a)
|
||||||
assert(debug.getinfo(1, "n").name == 'F')
|
assert(debug.getinfo(1, "n").name == 'F')
|
||||||
return a,2,3
|
return a,2,3
|
||||||
end
|
end
|
||||||
@@ -314,7 +234,7 @@ a,b = F(nil)==nil; assert(a == true and b == nil)
|
|||||||
------------------------------------------------------------------
|
------------------------------------------------------------------
|
||||||
|
|
||||||
-- sometimes will be 0, sometimes will not...
|
-- sometimes will be 0, sometimes will not...
|
||||||
_ENV.GLOB1 = math.random(0, 1)
|
_ENV.GLOB1 = math.floor(os.time()) % 2
|
||||||
|
|
||||||
-- basic expressions with their respective values
|
-- basic expressions with their respective values
|
||||||
local basiccases = {
|
local basiccases = {
|
||||||
@@ -325,36 +245,16 @@ local basiccases = {
|
|||||||
{"(0==_ENV.GLOB1)", 0 == _ENV.GLOB1},
|
{"(0==_ENV.GLOB1)", 0 == _ENV.GLOB1},
|
||||||
}
|
}
|
||||||
|
|
||||||
local prog
|
|
||||||
|
|
||||||
if _ENV.GLOB1 == 0 then
|
|
||||||
basiccases[2][1] = "F" -- constant false
|
|
||||||
|
|
||||||
prog = [[
|
|
||||||
local F <const> = false
|
|
||||||
if %s then IX = true end
|
|
||||||
return %s
|
|
||||||
]]
|
|
||||||
else
|
|
||||||
basiccases[4][1] = "k10" -- constant 10
|
|
||||||
|
|
||||||
prog = [[
|
|
||||||
local k10 <const> = 10
|
|
||||||
if %s then IX = true end
|
|
||||||
return %s
|
|
||||||
]]
|
|
||||||
end
|
|
||||||
|
|
||||||
print('testing short-circuit optimizations (' .. _ENV.GLOB1 .. ')')
|
print('testing short-circuit optimizations (' .. _ENV.GLOB1 .. ')')
|
||||||
|
|
||||||
|
|
||||||
-- operators with their respective values
|
-- operators with their respective values
|
||||||
local binops <const> = {
|
local binops = {
|
||||||
{" and ", function (a,b) if not a then return a else return b end end},
|
{" and ", function (a,b) if not a then return a else return b end end},
|
||||||
{" or ", function (a,b) if a then return a else return b end end},
|
{" or ", function (a,b) if a then return a else return b end end},
|
||||||
}
|
}
|
||||||
|
|
||||||
local cases <const> = {}
|
local cases = {}
|
||||||
|
|
||||||
-- creates all combinations of '(cases[i] op cases[n-i])' plus
|
-- creates all combinations of '(cases[i] op cases[n-i])' plus
|
||||||
-- 'not(cases[i] op cases[n-i])' (syntax + value)
|
-- 'not(cases[i] op cases[n-i])' (syntax + value)
|
||||||
@@ -384,6 +284,8 @@ cases[1] = basiccases
|
|||||||
for i = 2, level do cases[i] = createcases(i) end
|
for i = 2, level do cases[i] = createcases(i) end
|
||||||
print("+")
|
print("+")
|
||||||
|
|
||||||
|
local prog = [[if %s then IX = true end; return %s]]
|
||||||
|
|
||||||
local i = 0
|
local i = 0
|
||||||
for n = 1, level do
|
for n = 1, level do
|
||||||
for _, v in pairs(cases[n]) do
|
for _, v in pairs(cases[n]) do
|
||||||
@@ -395,12 +297,17 @@ for n = 1, level do
|
|||||||
if i % 60000 == 0 then print('+') end
|
if i % 60000 == 0 then print('+') end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
IX = nil
|
|
||||||
_G.GLOB1 = nil
|
|
||||||
------------------------------------------------------------------
|
------------------------------------------------------------------
|
||||||
|
|
||||||
-- testing some syntax errors (chosen through 'gcov')
|
-- testing some syntax errors (chosen through 'gcov')
|
||||||
checkload("for x do", "expected")
|
checkload("for x do", "expected")
|
||||||
checkload("x:call", "expected")
|
checkload("x:call", "expected")
|
||||||
|
|
||||||
|
if not _soft then
|
||||||
|
-- control structure too long
|
||||||
|
local s = string.rep("a = a + 1\n", 2^18)
|
||||||
|
s = "while true do " .. s .. "end"
|
||||||
|
checkload(s, "too long")
|
||||||
|
end
|
||||||
|
|
||||||
print'OK'
|
print'OK'
|
||||||
|
|||||||
+89
-478
@@ -1,5 +1,5 @@
|
|||||||
-- $Id: testes/coroutine.lua $
|
-- $Id: coroutine.lua,v 1.42 2016/11/07 13:03:20 roberto Exp $
|
||||||
-- See Copyright Notice in file lua.h
|
-- See Copyright Notice in file all.lua
|
||||||
|
|
||||||
print "testing coroutines"
|
print "testing coroutines"
|
||||||
|
|
||||||
@@ -10,7 +10,7 @@ local f
|
|||||||
local main, ismain = coroutine.running()
|
local main, ismain = coroutine.running()
|
||||||
assert(type(main) == "thread" and ismain)
|
assert(type(main) == "thread" and ismain)
|
||||||
assert(not coroutine.resume(main))
|
assert(not coroutine.resume(main))
|
||||||
assert(not coroutine.isyieldable(main) and not coroutine.isyieldable())
|
assert(not coroutine.isyieldable())
|
||||||
assert(not pcall(coroutine.yield))
|
assert(not pcall(coroutine.yield))
|
||||||
|
|
||||||
|
|
||||||
@@ -30,8 +30,7 @@ local function eqtab (t1, t2)
|
|||||||
end
|
end
|
||||||
|
|
||||||
_G.x = nil -- declare x
|
_G.x = nil -- declare x
|
||||||
_G.f = nil -- declare f
|
function foo (a, ...)
|
||||||
local function foo (a, ...)
|
|
||||||
local x, y = coroutine.running()
|
local x, y = coroutine.running()
|
||||||
assert(x == f and y == false)
|
assert(x == f and y == false)
|
||||||
-- next call should not corrupt coroutine (but must fail,
|
-- next call should not corrupt coroutine (but must fail,
|
||||||
@@ -39,7 +38,7 @@ local function foo (a, ...)
|
|||||||
assert(coroutine.resume(f) == false)
|
assert(coroutine.resume(f) == false)
|
||||||
assert(coroutine.status(f) == "running")
|
assert(coroutine.status(f) == "running")
|
||||||
local arg = {...}
|
local arg = {...}
|
||||||
assert(coroutine.isyieldable(x))
|
assert(coroutine.isyieldable())
|
||||||
for i=1,#arg do
|
for i=1,#arg do
|
||||||
_G.x = {coroutine.yield(table.unpack(arg[i]))}
|
_G.x = {coroutine.yield(table.unpack(arg[i]))}
|
||||||
end
|
end
|
||||||
@@ -47,17 +46,14 @@ local function foo (a, ...)
|
|||||||
end
|
end
|
||||||
|
|
||||||
f = coroutine.create(foo)
|
f = coroutine.create(foo)
|
||||||
assert(coroutine.isyieldable(f))
|
|
||||||
assert(type(f) == "thread" and coroutine.status(f) == "suspended")
|
assert(type(f) == "thread" and coroutine.status(f) == "suspended")
|
||||||
assert(string.find(tostring(f), "thread"))
|
assert(string.find(tostring(f), "thread"))
|
||||||
local s,a,b,c,d
|
local s,a,b,c,d
|
||||||
s,a,b,c,d = coroutine.resume(f, {1,2,3}, {}, {1}, {'a', 'b', 'c'})
|
s,a,b,c,d = coroutine.resume(f, {1,2,3}, {}, {1}, {'a', 'b', 'c'})
|
||||||
assert(coroutine.isyieldable(f))
|
|
||||||
assert(s and a == nil and coroutine.status(f) == "suspended")
|
assert(s and a == nil and coroutine.status(f) == "suspended")
|
||||||
s,a,b,c,d = coroutine.resume(f)
|
s,a,b,c,d = coroutine.resume(f)
|
||||||
eqtab(_G.x, {})
|
eqtab(_G.x, {})
|
||||||
assert(s and a == 1 and b == nil)
|
assert(s and a == 1 and b == nil)
|
||||||
assert(coroutine.isyieldable(f))
|
|
||||||
s,a,b,c,d = coroutine.resume(f, 1, 2, 3)
|
s,a,b,c,d = coroutine.resume(f, 1, 2, 3)
|
||||||
eqtab(_G.x, {1, 2, 3})
|
eqtab(_G.x, {1, 2, 3})
|
||||||
assert(s and a == 'a' and b == 'b' and c == 'c' and d == nil)
|
assert(s and a == 'a' and b == 'b' and c == 'c' and d == nil)
|
||||||
@@ -68,11 +64,10 @@ assert(coroutine.status(f) == "dead")
|
|||||||
s, a = coroutine.resume(f, "xuxu")
|
s, a = coroutine.resume(f, "xuxu")
|
||||||
assert(not s and string.find(a, "dead") and coroutine.status(f) == "dead")
|
assert(not s and string.find(a, "dead") and coroutine.status(f) == "dead")
|
||||||
|
|
||||||
_G.f = nil
|
|
||||||
|
|
||||||
-- yields in tail calls
|
-- yields in tail calls
|
||||||
local function foo (i) return coroutine.yield(i) end
|
local function foo (i) return coroutine.yield(i) end
|
||||||
local f = coroutine.wrap(function ()
|
f = coroutine.wrap(function ()
|
||||||
for i=1,10 do
|
for i=1,10 do
|
||||||
assert(foo(i) == _G.x)
|
assert(foo(i) == _G.x)
|
||||||
end
|
end
|
||||||
@@ -81,10 +76,8 @@ end)
|
|||||||
for i=1,10 do _G.x = i; assert(f(i) == i) end
|
for i=1,10 do _G.x = i; assert(f(i) == i) end
|
||||||
_G.x = 'xuxu'; assert(f('xuxu') == 'a')
|
_G.x = 'xuxu'; assert(f('xuxu') == 'a')
|
||||||
|
|
||||||
_G.x = nil
|
|
||||||
|
|
||||||
-- recursive
|
-- recursive
|
||||||
local function pf (n, i)
|
function pf (n, i)
|
||||||
coroutine.yield(n)
|
coroutine.yield(n)
|
||||||
pf(n*i, i+1)
|
pf(n*i, i+1)
|
||||||
end
|
end
|
||||||
@@ -97,14 +90,14 @@ for i=1,10 do
|
|||||||
end
|
end
|
||||||
|
|
||||||
-- sieve
|
-- sieve
|
||||||
local function gen (n)
|
function gen (n)
|
||||||
return coroutine.wrap(function ()
|
return coroutine.wrap(function ()
|
||||||
for i=2,n do coroutine.yield(i) end
|
for i=2,n do coroutine.yield(i) end
|
||||||
end)
|
end)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
||||||
local function filter (p, g)
|
function filter (p, g)
|
||||||
return coroutine.wrap(function ()
|
return coroutine.wrap(function ()
|
||||||
while 1 do
|
while 1 do
|
||||||
local n = g()
|
local n = g()
|
||||||
@@ -114,7 +107,7 @@ local function filter (p, g)
|
|||||||
end)
|
end)
|
||||||
end
|
end
|
||||||
|
|
||||||
local x = gen(80)
|
local x = gen(100)
|
||||||
local a = {}
|
local a = {}
|
||||||
while 1 do
|
while 1 do
|
||||||
local n = x()
|
local n = x()
|
||||||
@@ -123,224 +116,12 @@ while 1 do
|
|||||||
x = filter(n, x)
|
x = filter(n, x)
|
||||||
end
|
end
|
||||||
|
|
||||||
assert(#a == 22 and a[#a] == 79)
|
assert(#a == 25 and a[#a] == 97)
|
||||||
x, a = nil
|
x, a = nil
|
||||||
|
|
||||||
|
|
||||||
do -- "bug" in 5.4.2
|
|
||||||
local function foo () foo () end -- just create a stack overflow
|
|
||||||
local co = coroutine.create(foo)
|
|
||||||
-- running this coroutine would overflow the unsigned short 'nci', the
|
|
||||||
-- counter of CallInfo structures available to the thread.
|
|
||||||
-- (The issue only manifests in an 'assert'.)
|
|
||||||
local st, msg = coroutine.resume(co)
|
|
||||||
assert(string.find(msg, "stack overflow"))
|
|
||||||
assert(coroutine.status(co) == "dead")
|
|
||||||
end
|
|
||||||
|
|
||||||
|
|
||||||
print("to-be-closed variables in coroutines")
|
|
||||||
|
|
||||||
local function func2close (f)
|
|
||||||
return setmetatable({}, {__close = f})
|
|
||||||
end
|
|
||||||
|
|
||||||
do
|
|
||||||
-- ok to close a dead coroutine
|
|
||||||
local co = coroutine.create(print)
|
|
||||||
assert(coroutine.resume(co, "testing 'coroutine.close'"))
|
|
||||||
assert(coroutine.status(co) == "dead")
|
|
||||||
local st, msg = coroutine.close(co)
|
|
||||||
assert(st and msg == nil)
|
|
||||||
-- also ok to close it again
|
|
||||||
st, msg = coroutine.close(co)
|
|
||||||
assert(st and msg == nil)
|
|
||||||
|
|
||||||
local main = coroutine.running()
|
|
||||||
|
|
||||||
-- cannot close 'main'
|
|
||||||
local st, msg = pcall(coroutine.close, main);
|
|
||||||
assert(not st and string.find(msg, "main"))
|
|
||||||
|
|
||||||
|
|
||||||
-- cannot close a "normal" coroutine
|
|
||||||
;(coroutine.wrap(function ()
|
|
||||||
local st, msg = pcall(coroutine.close, main)
|
|
||||||
assert(not st and string.find(msg, "normal"))
|
|
||||||
end))()
|
|
||||||
|
|
||||||
do -- close a coroutine while closing it
|
|
||||||
local co
|
|
||||||
co = coroutine.create(
|
|
||||||
function()
|
|
||||||
local x <close> = func2close(function()
|
|
||||||
coroutine.close(co) -- close it again
|
|
||||||
end)
|
|
||||||
coroutine.yield(20)
|
|
||||||
end)
|
|
||||||
local st, msg = coroutine.resume(co)
|
|
||||||
assert(st and msg == 20)
|
|
||||||
st, msg = coroutine.close(co)
|
|
||||||
assert(st and msg == nil)
|
|
||||||
end
|
|
||||||
|
|
||||||
-- to-be-closed variables in coroutines
|
|
||||||
local X
|
|
||||||
|
|
||||||
-- closing a coroutine after an error
|
|
||||||
local co = coroutine.create(error)
|
|
||||||
local st, msg = coroutine.resume(co, 100)
|
|
||||||
assert(not st and msg == 100)
|
|
||||||
st, msg = coroutine.close(co)
|
|
||||||
assert(not st and msg == 100)
|
|
||||||
-- after closing, no more errors
|
|
||||||
st, msg = coroutine.close(co)
|
|
||||||
assert(st and msg == nil)
|
|
||||||
|
|
||||||
co = coroutine.create(function ()
|
|
||||||
local x <close> = func2close(function (self, err)
|
|
||||||
assert(err == nil); X = false
|
|
||||||
end)
|
|
||||||
X = true
|
|
||||||
coroutine.yield()
|
|
||||||
end)
|
|
||||||
coroutine.resume(co)
|
|
||||||
assert(X)
|
|
||||||
assert(coroutine.close(co))
|
|
||||||
assert(not X and coroutine.status(co) == "dead")
|
|
||||||
|
|
||||||
-- error closing a coroutine
|
|
||||||
local x = 0
|
|
||||||
co = coroutine.create(function()
|
|
||||||
local y <close> = func2close(function (self,err)
|
|
||||||
assert(err == 111)
|
|
||||||
x = 200
|
|
||||||
error(200)
|
|
||||||
end)
|
|
||||||
local x <close> = func2close(function (self, err)
|
|
||||||
assert(err == nil); error(111)
|
|
||||||
end)
|
|
||||||
coroutine.yield()
|
|
||||||
end)
|
|
||||||
coroutine.resume(co)
|
|
||||||
assert(x == 0)
|
|
||||||
local st, msg = coroutine.close(co)
|
|
||||||
assert(st == false and coroutine.status(co) == "dead" and msg == 200)
|
|
||||||
assert(x == 200)
|
|
||||||
-- after closing, no more errors
|
|
||||||
st, msg = coroutine.close(co)
|
|
||||||
assert(st and msg == nil)
|
|
||||||
end
|
|
||||||
|
|
||||||
do
|
|
||||||
-- <close> versus pcall in coroutines
|
|
||||||
local X = false
|
|
||||||
local Y = false
|
|
||||||
local function foo ()
|
|
||||||
local x <close> = func2close(function (self, err)
|
|
||||||
Y = debug.getinfo(2)
|
|
||||||
X = err
|
|
||||||
end)
|
|
||||||
error(43)
|
|
||||||
end
|
|
||||||
local co = coroutine.create(function () return pcall(foo) end)
|
|
||||||
local st1, st2, err = coroutine.resume(co)
|
|
||||||
assert(st1 and not st2 and err == 43)
|
|
||||||
assert(X == 43 and Y.what == "C")
|
|
||||||
|
|
||||||
-- recovering from errors in __close metamethods
|
|
||||||
local track = {}
|
|
||||||
|
|
||||||
local function h (o)
|
|
||||||
local hv <close> = o
|
|
||||||
return 1
|
|
||||||
end
|
|
||||||
|
|
||||||
local function foo ()
|
|
||||||
local x <close> = func2close(function(_,msg)
|
|
||||||
track[#track + 1] = msg or false
|
|
||||||
error(20)
|
|
||||||
end)
|
|
||||||
local y <close> = func2close(function(_,msg)
|
|
||||||
track[#track + 1] = msg or false
|
|
||||||
return 1000
|
|
||||||
end)
|
|
||||||
local z <close> = func2close(function(_,msg)
|
|
||||||
track[#track + 1] = msg or false
|
|
||||||
error(10)
|
|
||||||
end)
|
|
||||||
coroutine.yield(1)
|
|
||||||
h(func2close(function(_,msg)
|
|
||||||
track[#track + 1] = msg or false
|
|
||||||
error(2)
|
|
||||||
end))
|
|
||||||
end
|
|
||||||
|
|
||||||
local co = coroutine.create(pcall)
|
|
||||||
|
|
||||||
local st, res = coroutine.resume(co, foo) -- call 'foo' protected
|
|
||||||
assert(st and res == 1) -- yield 1
|
|
||||||
local st, res1, res2 = coroutine.resume(co) -- continue
|
|
||||||
assert(coroutine.status(co) == "dead")
|
|
||||||
assert(st and not res1 and res2 == 20) -- last error (20)
|
|
||||||
assert(track[1] == false and track[2] == 2 and track[3] == 10 and
|
|
||||||
track[4] == 10)
|
|
||||||
end
|
|
||||||
|
|
||||||
|
|
||||||
do print("coroutines closing itself")
|
|
||||||
global <const> coroutine, string, os
|
|
||||||
global <const> assert, error, pcall
|
|
||||||
|
|
||||||
local X = nil
|
|
||||||
|
|
||||||
local function new ()
|
|
||||||
return coroutine.create(function (what)
|
|
||||||
|
|
||||||
local <close>var = func2close(function (t, err)
|
|
||||||
if what == "yield" then
|
|
||||||
coroutine.yield()
|
|
||||||
elseif what == "error" then
|
|
||||||
error(200)
|
|
||||||
else
|
|
||||||
X = "Ok"
|
|
||||||
return X
|
|
||||||
end
|
|
||||||
end)
|
|
||||||
|
|
||||||
-- do an unprotected call so that coroutine becomes non-yieldable
|
|
||||||
string.gsub("a", "a", function ()
|
|
||||||
assert(not coroutine.isyieldable())
|
|
||||||
-- do protected calls while non-yieldable, to add recovery
|
|
||||||
-- entries (setjmp) to the stack
|
|
||||||
assert(pcall(pcall, function ()
|
|
||||||
-- 'close' works even while non-yieldable
|
|
||||||
coroutine.close() -- close itself
|
|
||||||
os.exit(false) -- not reacheable
|
|
||||||
end))
|
|
||||||
end)
|
|
||||||
end)
|
|
||||||
end
|
|
||||||
|
|
||||||
local co = new()
|
|
||||||
local st, msg = coroutine.resume(co, "ret")
|
|
||||||
assert(st and msg == nil)
|
|
||||||
assert(X == "Ok")
|
|
||||||
|
|
||||||
local co = new()
|
|
||||||
local st, msg = coroutine.resume(co, "error")
|
|
||||||
assert(not st and msg == 200)
|
|
||||||
|
|
||||||
local co = new()
|
|
||||||
local st, msg = coroutine.resume(co, "yield")
|
|
||||||
assert(not st and string.find(msg, "attempt to yield"))
|
|
||||||
|
|
||||||
end
|
|
||||||
|
|
||||||
|
|
||||||
-- yielding across C boundaries
|
-- yielding across C boundaries
|
||||||
|
|
||||||
local co = coroutine.wrap(function()
|
co = coroutine.wrap(function()
|
||||||
assert(not pcall(table.sort,{1,2,3}, coroutine.yield))
|
assert(not pcall(table.sort,{1,2,3}, coroutine.yield))
|
||||||
assert(coroutine.isyieldable())
|
assert(coroutine.isyieldable())
|
||||||
coroutine.yield(20)
|
coroutine.yield(20)
|
||||||
@@ -368,15 +149,15 @@ local r1, r2, v = f1(nil)
|
|||||||
assert(r1 and not r2 and v[1] == (10 + 1)*10/2)
|
assert(r1 and not r2 and v[1] == (10 + 1)*10/2)
|
||||||
|
|
||||||
|
|
||||||
local function f (a, b) a = coroutine.yield(a); error{a + b} end
|
function f (a, b) a = coroutine.yield(a); error{a + b} end
|
||||||
local function g(x) return x[1]*2 end
|
function g(x) return x[1]*2 end
|
||||||
|
|
||||||
co = coroutine.wrap(function ()
|
co = coroutine.wrap(function ()
|
||||||
coroutine.yield(xpcall(f, g, 10, 20))
|
coroutine.yield(xpcall(f, g, 10, 20))
|
||||||
end)
|
end)
|
||||||
|
|
||||||
assert(co() == 10)
|
assert(co() == 10)
|
||||||
local r, msg = co(100)
|
r, msg = co(100)
|
||||||
assert(not r and msg == 240)
|
assert(not r and msg == 240)
|
||||||
|
|
||||||
|
|
||||||
@@ -396,26 +177,6 @@ do
|
|||||||
end
|
end
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
do -- testing single trace of coroutines
|
|
||||||
local X
|
|
||||||
local co = coroutine.create(function ()
|
|
||||||
coroutine.yield(10)
|
|
||||||
return 20;
|
|
||||||
end)
|
|
||||||
local trace = {}
|
|
||||||
local function dotrace (event)
|
|
||||||
trace[#trace + 1] = event
|
|
||||||
end
|
|
||||||
debug.sethook(co, dotrace, "clr")
|
|
||||||
repeat until not coroutine.resume(co)
|
|
||||||
local correcttrace = {"call", "line", "call", "return", "line", "return"}
|
|
||||||
assert(#trace == #correcttrace)
|
|
||||||
for k, v in pairs(trace) do
|
|
||||||
assert(v == correcttrace[k])
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
-- errors in coroutines
|
-- errors in coroutines
|
||||||
function foo ()
|
function foo ()
|
||||||
assert(debug.getinfo(1).currentline == debug.getinfo(foo).linedefined + 1)
|
assert(debug.getinfo(1).currentline == debug.getinfo(foo).linedefined + 1)
|
||||||
@@ -438,10 +199,9 @@ assert(not a and b == foo and coroutine.status(x) == "dead")
|
|||||||
a,b = coroutine.resume(x)
|
a,b = coroutine.resume(x)
|
||||||
assert(not a and string.find(b, "dead") and coroutine.status(x) == "dead")
|
assert(not a and string.find(b, "dead") and coroutine.status(x) == "dead")
|
||||||
|
|
||||||
goo = nil
|
|
||||||
|
|
||||||
-- co-routines x for loop
|
-- co-routines x for loop
|
||||||
local function all (a, n, k)
|
function all (a, n, k)
|
||||||
if k == 0 then coroutine.yield(a)
|
if k == 0 then coroutine.yield(a)
|
||||||
else
|
else
|
||||||
for i=1,n do
|
for i=1,n do
|
||||||
@@ -475,13 +235,13 @@ local f = x()
|
|||||||
assert(f() == 21 and x()() == 32 and x() == f)
|
assert(f() == 21 and x()() == 32 and x() == f)
|
||||||
x = nil
|
x = nil
|
||||||
collectgarbage()
|
collectgarbage()
|
||||||
assert(C[1] == undef)
|
assert(C[1] == nil)
|
||||||
assert(f() == 43 and f() == 53)
|
assert(f() == 43 and f() == 53)
|
||||||
|
|
||||||
|
|
||||||
-- old bug: attempt to resume itself
|
-- old bug: attempt to resume itself
|
||||||
|
|
||||||
local function co_func (current_co)
|
function co_func (current_co)
|
||||||
assert(coroutine.running() == current_co)
|
assert(coroutine.running() == current_co)
|
||||||
assert(coroutine.resume(current_co) == false)
|
assert(coroutine.resume(current_co) == false)
|
||||||
coroutine.yield(10, 20)
|
coroutine.yield(10, 20)
|
||||||
@@ -509,29 +269,9 @@ do
|
|||||||
local st, res = coroutine.resume(B)
|
local st, res = coroutine.resume(B)
|
||||||
assert(st == true and res == false)
|
assert(st == true and res == false)
|
||||||
|
|
||||||
local X = false
|
A = coroutine.wrap(function() return pcall(A, 1) end)
|
||||||
A = coroutine.wrap(function()
|
|
||||||
local _ <close> = func2close(function () X = true end)
|
|
||||||
return pcall(A, 1)
|
|
||||||
end)
|
|
||||||
st, res = A()
|
st, res = A()
|
||||||
assert(not st and string.find(res, "non%-suspended") and X == true)
|
assert(not st and string.find(res, "non%-suspended"))
|
||||||
end
|
|
||||||
|
|
||||||
|
|
||||||
-- bug in 5.4.1
|
|
||||||
do
|
|
||||||
-- coroutine ran close metamethods with invalid status during a
|
|
||||||
-- reset.
|
|
||||||
local co
|
|
||||||
co = coroutine.wrap(function()
|
|
||||||
local x <close> = func2close(function() return pcall(co) end)
|
|
||||||
error(111)
|
|
||||||
end)
|
|
||||||
local st, errobj = pcall(co)
|
|
||||||
assert(not st and errobj == 111)
|
|
||||||
st, errobj = pcall(co)
|
|
||||||
assert(not st and string.find(errobj, "dead coroutine"))
|
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
||||||
@@ -554,49 +294,28 @@ assert(not pcall(a, a))
|
|||||||
a = nil
|
a = nil
|
||||||
|
|
||||||
|
|
||||||
do
|
|
||||||
-- bug in 5.4: thread can use message handler higher in the stack
|
|
||||||
-- than the variable being closed
|
|
||||||
local c = coroutine.create(function()
|
|
||||||
local clo <close> = setmetatable({}, {__close=function()
|
|
||||||
local x = 134 -- will overwrite message handler
|
|
||||||
error(x)
|
|
||||||
end})
|
|
||||||
-- yields coroutine but leaves a new message handler for it,
|
|
||||||
-- that would be used when closing the coroutine (except that it
|
|
||||||
-- will be overwritten)
|
|
||||||
xpcall(coroutine.yield, function() return "XXX" end)
|
|
||||||
end)
|
|
||||||
|
|
||||||
assert(coroutine.resume(c)) -- start coroutine
|
|
||||||
local st, msg = coroutine.close(c)
|
|
||||||
assert(not st and msg == 134)
|
|
||||||
end
|
|
||||||
|
|
||||||
-- access to locals of erroneous coroutines
|
-- access to locals of erroneous coroutines
|
||||||
local x = coroutine.create (function ()
|
local x = coroutine.create (function ()
|
||||||
local a = 10
|
local a = 10
|
||||||
_G.F = function () a=a+1; return a end
|
_G.f = function () a=a+1; return a end
|
||||||
error('x')
|
error('x')
|
||||||
end)
|
end)
|
||||||
|
|
||||||
assert(not coroutine.resume(x))
|
assert(not coroutine.resume(x))
|
||||||
-- overwrite previous position of local `a'
|
-- overwrite previous position of local `a'
|
||||||
assert(not coroutine.resume(x, 1, 1, 1, 1, 1, 1, 1))
|
assert(not coroutine.resume(x, 1, 1, 1, 1, 1, 1, 1))
|
||||||
assert(_G.F() == 11)
|
assert(_G.f() == 11)
|
||||||
assert(_G.F() == 12)
|
assert(_G.f() == 12)
|
||||||
_G.F = nil
|
|
||||||
|
|
||||||
|
|
||||||
if not T then
|
if not T then
|
||||||
(Message or print)
|
(Message or print)('\n >>> testC not active: skipping yield/hook tests <<<\n')
|
||||||
('\n >>> testC not active: skipping coroutine API tests <<<\n')
|
|
||||||
else
|
else
|
||||||
print "testing yields inside hooks"
|
print "testing yields inside hooks"
|
||||||
|
|
||||||
local turn
|
local turn
|
||||||
|
|
||||||
local function fact (t, x)
|
function fact (t, x)
|
||||||
assert(turn == t)
|
assert(turn == t)
|
||||||
if x == 0 then return 1
|
if x == 0 then return 1
|
||||||
else return x*fact(t, x-1)
|
else return x*fact(t, x-1)
|
||||||
@@ -618,36 +337,10 @@ else
|
|||||||
while A==0 or B==0 do -- A ~= 0 when 'x' finishes (similar for 'B','y')
|
while A==0 or B==0 do -- A ~= 0 when 'x' finishes (similar for 'B','y')
|
||||||
if A==0 then turn = "A"; assert(T.resume(x)) end
|
if A==0 then turn = "A"; assert(T.resume(x)) end
|
||||||
if B==0 then turn = "B"; assert(T.resume(y)) end
|
if B==0 then turn = "B"; assert(T.resume(y)) end
|
||||||
|
|
||||||
-- check that traceback works correctly after yields inside hooks
|
|
||||||
debug.traceback(x)
|
|
||||||
debug.traceback(y)
|
|
||||||
end
|
end
|
||||||
|
|
||||||
assert(B // A == 7) -- fact(7) // fact(6)
|
assert(B // A == 7) -- fact(7) // fact(6)
|
||||||
|
|
||||||
do -- hooks vs. multiple values
|
|
||||||
local done
|
|
||||||
local function test (n)
|
|
||||||
done = false
|
|
||||||
return coroutine.wrap(function ()
|
|
||||||
local a = {}
|
|
||||||
for i = 1, n do a[i] = i end
|
|
||||||
-- 'pushint' just to perturb the stack
|
|
||||||
T.sethook("pushint 10; yield 0", "", 1) -- yield at each op.
|
|
||||||
local a1 = {table.unpack(a)} -- must keep top between ops.
|
|
||||||
assert(#a1 == n)
|
|
||||||
for i = 1, n do assert(a[i] == i) end
|
|
||||||
done = true
|
|
||||||
end)
|
|
||||||
end
|
|
||||||
-- arguments to the coroutine are just to perturb its stack
|
|
||||||
local co = test(0); while not done do co(30) end
|
|
||||||
co = test(1); while not done do co(20, 10) end
|
|
||||||
co = test(3); while not done do co() end
|
|
||||||
co = test(100); while not done do co() end
|
|
||||||
end
|
|
||||||
|
|
||||||
local line = debug.getinfo(1, "l").currentline + 2 -- get line number
|
local line = debug.getinfo(1, "l").currentline + 2 -- get line number
|
||||||
local function foo ()
|
local function foo ()
|
||||||
local x = 10 --<< this line is 'line'
|
local x = 10 --<< this line is 'line'
|
||||||
@@ -665,7 +358,6 @@ else
|
|||||||
_G.X = nil; co(); assert(_G.X == line + 2 and _G.XX == nil)
|
_G.X = nil; co(); assert(_G.X == line + 2 and _G.XX == nil)
|
||||||
_G.X = nil; co(); assert(_G.X == line + 3 and _G.XX == 20)
|
_G.X = nil; co(); assert(_G.X == line + 3 and _G.XX == 20)
|
||||||
assert(co() == 10)
|
assert(co() == 10)
|
||||||
_G.X = nil
|
|
||||||
|
|
||||||
-- testing yields in count hook
|
-- testing yields in count hook
|
||||||
co = coroutine.wrap(function ()
|
co = coroutine.wrap(function ()
|
||||||
@@ -690,24 +382,25 @@ else
|
|||||||
-- (bug in 5.2/5.3)
|
-- (bug in 5.2/5.3)
|
||||||
c = coroutine.create(function (a, ...)
|
c = coroutine.create(function (a, ...)
|
||||||
T.sethook("yield 0", "l") -- will yield on next two lines
|
T.sethook("yield 0", "l") -- will yield on next two lines
|
||||||
local b = a
|
assert(a == 10)
|
||||||
return ...
|
return ...
|
||||||
end)
|
end)
|
||||||
|
|
||||||
assert(coroutine.resume(c, 1, 2, 3)) -- start coroutine
|
assert(coroutine.resume(c, 1, 2, 3)) -- start coroutine
|
||||||
local n,v = debug.getlocal(c, 0, 1) -- check its local
|
local n,v = debug.getlocal(c, 0, 1) -- check its local
|
||||||
assert(n == "a" and v == 1 and debug.getlocal(c, 0, 2) ~= "b")
|
assert(n == "a" and v == 1)
|
||||||
|
n,v = debug.getlocal(c, 0, -1) -- check varargs
|
||||||
|
assert(v == 2)
|
||||||
|
n,v = debug.getlocal(c, 0, -2)
|
||||||
|
assert(v == 3)
|
||||||
assert(debug.setlocal(c, 0, 1, 10)) -- test 'setlocal'
|
assert(debug.setlocal(c, 0, 1, 10)) -- test 'setlocal'
|
||||||
|
assert(debug.setlocal(c, 0, -2, 20))
|
||||||
local t = debug.getinfo(c, 0) -- test 'getinfo'
|
local t = debug.getinfo(c, 0) -- test 'getinfo'
|
||||||
assert(t.currentline == t.linedefined + 2)
|
assert(t.currentline == t.linedefined + 1)
|
||||||
assert(not debug.getinfo(c, 1)) -- no other level
|
assert(not debug.getinfo(c, 1)) -- no other level
|
||||||
assert(coroutine.resume(c)) -- run next line
|
assert(coroutine.resume(c)) -- run next line
|
||||||
local n,v = debug.getlocal(c, 0, 2) -- check vararg table
|
|
||||||
assert(n == "(vararg table)" and v == nil)
|
|
||||||
local n,v = debug.getlocal(c, 0, 3) -- check next local
|
|
||||||
assert(n == "b" and v == 10)
|
|
||||||
v = {coroutine.resume(c)} -- finish coroutine
|
v = {coroutine.resume(c)} -- finish coroutine
|
||||||
assert(v[1] == true and v[2] == 2 and v[3] == 3 and v[4] == undef)
|
assert(v[1] == true and v[2] == 2 and v[3] == 20 and v[4] == nil)
|
||||||
assert(not coroutine.resume(c))
|
assert(not coroutine.resume(c))
|
||||||
end
|
end
|
||||||
|
|
||||||
@@ -724,7 +417,7 @@ else
|
|||||||
|
|
||||||
|
|
||||||
print "testing coroutine API"
|
print "testing coroutine API"
|
||||||
|
|
||||||
-- reusing a thread
|
-- reusing a thread
|
||||||
assert(T.testC([[
|
assert(T.testC([[
|
||||||
newthread # create thread
|
newthread # create thread
|
||||||
@@ -747,8 +440,6 @@ else
|
|||||||
|
|
||||||
assert(X == 'a a a' and Y == 'OK')
|
assert(X == 'a a a' and Y == 'OK')
|
||||||
|
|
||||||
X, Y = nil
|
|
||||||
|
|
||||||
|
|
||||||
-- resuming running coroutine
|
-- resuming running coroutine
|
||||||
C = coroutine.create(function ()
|
C = coroutine.create(function ()
|
||||||
@@ -765,7 +456,7 @@ else
|
|||||||
c == "ERRRUN" and d == 4)
|
c == "ERRRUN" and d == 4)
|
||||||
|
|
||||||
a, b, c, d = T.testC([[
|
a, b, c, d = T.testC([[
|
||||||
rawgeti R !M # get main thread
|
rawgeti R 1 # get main thread
|
||||||
pushnum 10;
|
pushnum 10;
|
||||||
pushnum 20;
|
pushnum 20;
|
||||||
resume -3 2;
|
resume -3 2;
|
||||||
@@ -776,26 +467,17 @@ else
|
|||||||
c == "ERRRUN" and d == 4)
|
c == "ERRRUN" and d == 4)
|
||||||
|
|
||||||
|
|
||||||
-- using a main thread as a coroutine (dubious use!)
|
-- using a main thread as a coroutine
|
||||||
local state = T.newstate()
|
local state = T.newstate()
|
||||||
|
T.loadlib(state)
|
||||||
-- check that yielddable is working correctly
|
|
||||||
assert(T.testC(state, "newthread; isyieldable -1; remove 1; return 1"))
|
|
||||||
|
|
||||||
-- main thread is not yieldable
|
|
||||||
assert(not T.testC(state, "rawgeti R !M; isyieldable -1; remove 1; return 1"))
|
|
||||||
|
|
||||||
T.testC(state, "settop 0")
|
|
||||||
|
|
||||||
T.loadlib(state, 1 | 2, 4) -- load _G and 'package', preload 'coroutine'
|
|
||||||
|
|
||||||
assert(T.doremote(state, [[
|
assert(T.doremote(state, [[
|
||||||
coroutine = require'coroutine';
|
coroutine = require'coroutine';
|
||||||
X = function (x) coroutine.yield(x, 'BB'); return 'CC' end;
|
X = function (x) coroutine.yield(x, 'BB'); return 'CC' end;
|
||||||
return 'ok']]))
|
return 'ok']]))
|
||||||
|
|
||||||
local t = table.pack(T.testC(state, [[
|
t = table.pack(T.testC(state, [[
|
||||||
rawgeti R !M # get main thread
|
rawgeti R 1 # get main thread
|
||||||
pushstring 'XX'
|
pushstring 'XX'
|
||||||
getglobal X # get function for body
|
getglobal X # get function for body
|
||||||
pushstring AA # arg
|
pushstring AA # arg
|
||||||
@@ -804,7 +486,7 @@ else
|
|||||||
setglobal T # top
|
setglobal T # top
|
||||||
setglobal B # second yielded value
|
setglobal B # second yielded value
|
||||||
setglobal A # fist yielded value
|
setglobal A # fist yielded value
|
||||||
rawgeti R !M # get main thread
|
rawgeti R 1 # get main thread
|
||||||
pushnum 5 # arg (noise)
|
pushnum 5 # arg (noise)
|
||||||
resume 1 1 # after coroutine ends, previous stack is back
|
resume 1 1 # after coroutine ends, previous stack is back
|
||||||
pushstatus
|
pushstatus
|
||||||
@@ -823,28 +505,31 @@ end
|
|||||||
|
|
||||||
|
|
||||||
-- leaving a pending coroutine open
|
-- leaving a pending coroutine open
|
||||||
_G.TO_SURVIVE = coroutine.wrap(function ()
|
_X = coroutine.wrap(function ()
|
||||||
local a = 10
|
local a = 10
|
||||||
local x = function () a = a+1 end
|
local x = function () a = a+1 end
|
||||||
coroutine.yield()
|
coroutine.yield()
|
||||||
end)
|
end)
|
||||||
|
|
||||||
_G.TO_SURVIVE()
|
_X()
|
||||||
|
|
||||||
|
|
||||||
if not _soft then
|
if not _soft then
|
||||||
-- bug (stack overflow)
|
-- bug (stack overflow)
|
||||||
local lim = 1000000 -- stack limit; assume 32-bit machine
|
local j = 2^9
|
||||||
local t = {lim - 10, lim - 5, lim - 1, lim, lim + 1, lim + 5}
|
local lim = 1000000 -- (C stack limit; assume 32-bit machine)
|
||||||
|
local t = {lim - 10, lim - 5, lim - 1, lim, lim + 1}
|
||||||
for i = 1, #t do
|
for i = 1, #t do
|
||||||
local j = t[i]
|
local j = t[i]
|
||||||
local co = coroutine.create(function()
|
co = coroutine.create(function()
|
||||||
return table.unpack({}, 1, j)
|
local t = {}
|
||||||
|
for i = 1, j do t[i] = i end
|
||||||
|
return table.unpack(t)
|
||||||
end)
|
end)
|
||||||
local r, msg = coroutine.resume(co)
|
local r, msg = coroutine.resume(co)
|
||||||
-- must fail for unpacking larger than stack limit
|
assert(not r)
|
||||||
assert(j < lim or not r)
|
|
||||||
end
|
end
|
||||||
|
co = nil
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
||||||
@@ -855,41 +540,31 @@ print"+"
|
|||||||
|
|
||||||
print"testing yields inside metamethods"
|
print"testing yields inside metamethods"
|
||||||
|
|
||||||
local function val(x)
|
|
||||||
if type(x) == "table" then return x.x else return x end
|
|
||||||
end
|
|
||||||
|
|
||||||
local mt = {
|
local mt = {
|
||||||
__eq = function(a,b) coroutine.yield(nil, "eq"); return val(a) == val(b) end,
|
__eq = function(a,b) coroutine.yield(nil, "eq"); return a.x == b.x end,
|
||||||
__lt = function(a,b) coroutine.yield(nil, "lt"); return val(a) < val(b) end,
|
__lt = function(a,b) coroutine.yield(nil, "lt"); return a.x < b.x end,
|
||||||
__le = function(a,b) coroutine.yield(nil, "le"); return a - b <= 0 end,
|
__le = function(a,b) coroutine.yield(nil, "le"); return a - b <= 0 end,
|
||||||
__add = function(a,b) coroutine.yield(nil, "add");
|
__add = function(a,b) coroutine.yield(nil, "add"); return a.x + b.x end,
|
||||||
return val(a) + val(b) end,
|
__sub = function(a,b) coroutine.yield(nil, "sub"); return a.x - b.x end,
|
||||||
__sub = function(a,b) coroutine.yield(nil, "sub"); return val(a) - val(b) end,
|
__mod = function(a,b) coroutine.yield(nil, "mod"); return a.x % b.x end,
|
||||||
__mul = function(a,b) coroutine.yield(nil, "mul"); return val(a) * val(b) end,
|
__unm = function(a,b) coroutine.yield(nil, "unm"); return -a.x end,
|
||||||
__div = function(a,b) coroutine.yield(nil, "div"); return val(a) / val(b) end,
|
__bnot = function(a,b) coroutine.yield(nil, "bnot"); return ~a.x end,
|
||||||
__idiv = function(a,b) coroutine.yield(nil, "idiv");
|
__shl = function(a,b) coroutine.yield(nil, "shl"); return a.x << b.x end,
|
||||||
return val(a) // val(b) end,
|
__shr = function(a,b) coroutine.yield(nil, "shr"); return a.x >> b.x end,
|
||||||
__pow = function(a,b) coroutine.yield(nil, "pow"); return val(a) ^ val(b) end,
|
|
||||||
__mod = function(a,b) coroutine.yield(nil, "mod"); return val(a) % val(b) end,
|
|
||||||
__unm = function(a,b) coroutine.yield(nil, "unm"); return -val(a) end,
|
|
||||||
__bnot = function(a,b) coroutine.yield(nil, "bnot"); return ~val(a) end,
|
|
||||||
__shl = function(a,b) coroutine.yield(nil, "shl");
|
|
||||||
return val(a) << val(b) end,
|
|
||||||
__shr = function(a,b) coroutine.yield(nil, "shr");
|
|
||||||
return val(a) >> val(b) end,
|
|
||||||
__band = function(a,b)
|
__band = function(a,b)
|
||||||
|
a = type(a) == "table" and a.x or a
|
||||||
|
b = type(b) == "table" and b.x or b
|
||||||
coroutine.yield(nil, "band")
|
coroutine.yield(nil, "band")
|
||||||
return val(a) & val(b)
|
return a & b
|
||||||
end,
|
end,
|
||||||
__bor = function(a,b) coroutine.yield(nil, "bor");
|
__bor = function(a,b) coroutine.yield(nil, "bor"); return a.x | b.x end,
|
||||||
return val(a) | val(b) end,
|
__bxor = function(a,b) coroutine.yield(nil, "bxor"); return a.x ~ b.x end,
|
||||||
__bxor = function(a,b) coroutine.yield(nil, "bxor");
|
|
||||||
return val(a) ~ val(b) end,
|
|
||||||
|
|
||||||
__concat = function(a,b)
|
__concat = function(a,b)
|
||||||
coroutine.yield(nil, "concat");
|
coroutine.yield(nil, "concat");
|
||||||
return val(a) .. val(b)
|
a = type(a) == "table" and a.x or a
|
||||||
|
b = type(b) == "table" and b.x or b
|
||||||
|
return a .. b
|
||||||
end,
|
end,
|
||||||
__index = function (t,k) coroutine.yield(nil, "idx"); return t.k[k] end,
|
__index = function (t,k) coroutine.yield(nil, "idx"); return t.k[k] end,
|
||||||
__newindex = function (t,k,v) coroutine.yield(nil, "nidx"); t.k[k] = v end,
|
__newindex = function (t,k,v) coroutine.yield(nil, "nidx"); t.k[k] = v end,
|
||||||
@@ -910,7 +585,7 @@ local function run (f, t)
|
|||||||
local c = coroutine.wrap(f)
|
local c = coroutine.wrap(f)
|
||||||
while true do
|
while true do
|
||||||
local res, stat = c()
|
local res, stat = c()
|
||||||
if res then assert(t[i] == undef); return res, t end
|
if res then assert(t[i] == nil); return res, t end
|
||||||
assert(stat == t[i])
|
assert(stat == t[i])
|
||||||
i = i + 1
|
i = i + 1
|
||||||
end
|
end
|
||||||
@@ -919,41 +594,15 @@ end
|
|||||||
|
|
||||||
assert(run(function () if (a>=b) then return '>=' else return '<' end end,
|
assert(run(function () if (a>=b) then return '>=' else return '<' end end,
|
||||||
{"le", "sub"}) == "<")
|
{"le", "sub"}) == "<")
|
||||||
|
-- '<=' using '<'
|
||||||
|
mt.__le = nil
|
||||||
assert(run(function () if (a<=b) then return '<=' else return '>' end end,
|
assert(run(function () if (a<=b) then return '<=' else return '>' end end,
|
||||||
{"le", "sub"}) == "<=")
|
{"lt"}) == "<=")
|
||||||
assert(run(function () if (a==b) then return '==' else return '~=' end end,
|
assert(run(function () if (a==b) then return '==' else return '~=' end end,
|
||||||
{"eq"}) == "~=")
|
{"eq"}) == "~=")
|
||||||
|
|
||||||
assert(run(function () return a & b + a end, {"add", "band"}) == 2)
|
assert(run(function () return a & b + a end, {"add", "band"}) == 2)
|
||||||
|
|
||||||
assert(run(function () return 1 + a end, {"add"}) == 11)
|
|
||||||
assert(run(function () return a - 25 end, {"sub"}) == -15)
|
|
||||||
assert(run(function () return 2 * a end, {"mul"}) == 20)
|
|
||||||
assert(run(function () return a ^ 2 end, {"pow"}) == 100)
|
|
||||||
assert(run(function () return a / 2 end, {"div"}) == 5)
|
|
||||||
assert(run(function () return a % 6 end, {"mod"}) == 4)
|
|
||||||
assert(run(function () return a // 3 end, {"idiv"}) == 3)
|
|
||||||
|
|
||||||
assert(run(function () return a + b end, {"add"}) == 22)
|
|
||||||
assert(run(function () return a - b end, {"sub"}) == -2)
|
|
||||||
assert(run(function () return a * b end, {"mul"}) == 120)
|
|
||||||
assert(run(function () return a ^ b end, {"pow"}) == 10^12)
|
|
||||||
assert(run(function () return a / b end, {"div"}) == 10/12)
|
|
||||||
assert(run(function () return a % b end, {"mod"}) == 10)
|
|
||||||
assert(run(function () return a // b end, {"idiv"}) == 0)
|
|
||||||
|
|
||||||
-- repeat tests with larger constants (to use 'K' opcodes)
|
|
||||||
local a1000 = new(1000)
|
|
||||||
|
|
||||||
assert(run(function () return a1000 + 1000 end, {"add"}) == 2000)
|
|
||||||
assert(run(function () return a1000 - 25000 end, {"sub"}) == -24000)
|
|
||||||
assert(run(function () return 2000 * a end, {"mul"}) == 20000)
|
|
||||||
assert(run(function () return a1000 / 1000 end, {"div"}) == 1)
|
|
||||||
assert(run(function () return a1000 % 600 end, {"mod"}) == 400)
|
|
||||||
assert(run(function () return a1000 // 500 end, {"idiv"}) == 2)
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
assert(run(function () return a % b end, {"mod"}) == 10)
|
assert(run(function () return a % b end, {"mod"}) == 10)
|
||||||
|
|
||||||
assert(run(function () return ~a & b end, {"bnot", "band"}) == ~10 & 12)
|
assert(run(function () return ~a & b end, {"bnot", "band"}) == ~10 & 12)
|
||||||
@@ -962,16 +611,6 @@ assert(run(function () return a ~ b end, {"bxor"}) == 10 ~ 12)
|
|||||||
assert(run(function () return a << b end, {"shl"}) == 10 << 12)
|
assert(run(function () return a << b end, {"shl"}) == 10 << 12)
|
||||||
assert(run(function () return a >> b end, {"shr"}) == 10 >> 12)
|
assert(run(function () return a >> b end, {"shr"}) == 10 >> 12)
|
||||||
|
|
||||||
assert(run(function () return 10 & b end, {"band"}) == 10 & 12)
|
|
||||||
assert(run(function () return a | 2 end, {"bor"}) == 10 | 2)
|
|
||||||
assert(run(function () return a ~ 2 end, {"bxor"}) == 10 ~ 2)
|
|
||||||
assert(run(function () return a >> 2 end, {"shr"}) == 10 >> 2)
|
|
||||||
assert(run(function () return 1 >> a end, {"shr"}) == 1 >> 10)
|
|
||||||
assert(run(function () return a << 2 end, {"shl"}) == 10 << 2)
|
|
||||||
assert(run(function () return 1 << a end, {"shl"}) == 1 << 10)
|
|
||||||
assert(run(function () return 2 ~ a end, {"bxor"}) == 2 ~ 10)
|
|
||||||
|
|
||||||
|
|
||||||
assert(run(function () return a..b end, {"concat"}) == "1012")
|
assert(run(function () return a..b end, {"concat"}) == "1012")
|
||||||
|
|
||||||
assert(run(function() return a .. b .. c .. a end,
|
assert(run(function() return a .. b .. c .. a end,
|
||||||
@@ -981,18 +620,20 @@ assert(run(function() return "a" .. "b" .. a .. "c" .. c .. b .. "x" end,
|
|||||||
{"concat", "concat", "concat"}) == "ab10chello12x")
|
{"concat", "concat", "concat"}) == "ab10chello12x")
|
||||||
|
|
||||||
|
|
||||||
do -- a few more tests for comparison operators
|
do -- a few more tests for comparsion operators
|
||||||
local mt1 = {
|
local mt1 = {
|
||||||
__le = function (a,b)
|
__le = function (a,b)
|
||||||
coroutine.yield(10)
|
coroutine.yield(10)
|
||||||
return (val(a) <= val(b))
|
return
|
||||||
|
(type(a) == "table" and a.x or a) <= (type(b) == "table" and b.x or b)
|
||||||
end,
|
end,
|
||||||
__lt = function (a,b)
|
__lt = function (a,b)
|
||||||
coroutine.yield(10)
|
coroutine.yield(10)
|
||||||
return val(a) < val(b)
|
return
|
||||||
|
(type(a) == "table" and a.x or a) < (type(b) == "table" and b.x or b)
|
||||||
end,
|
end,
|
||||||
}
|
}
|
||||||
local mt2 = { __lt = mt1.__lt, __le = mt1.__le }
|
local mt2 = { __lt = mt1.__lt } -- no __le
|
||||||
|
|
||||||
local function run (f)
|
local function run (f)
|
||||||
local co = coroutine.wrap(f)
|
local co = coroutine.wrap(f)
|
||||||
@@ -1002,7 +643,7 @@ do -- a few more tests for comparison operators
|
|||||||
until res ~= 10
|
until res ~= 10
|
||||||
return res
|
return res
|
||||||
end
|
end
|
||||||
|
|
||||||
local function test ()
|
local function test ()
|
||||||
local a1 = setmetatable({x=1}, mt1)
|
local a1 = setmetatable({x=1}, mt1)
|
||||||
local a2 = setmetatable({x=2}, mt2)
|
local a2 = setmetatable({x=2}, mt2)
|
||||||
@@ -1014,7 +655,7 @@ do -- a few more tests for comparison operators
|
|||||||
assert(2 >= a2)
|
assert(2 >= a2)
|
||||||
return true
|
return true
|
||||||
end
|
end
|
||||||
|
|
||||||
run(test)
|
run(test)
|
||||||
|
|
||||||
end
|
end
|
||||||
@@ -1028,7 +669,7 @@ assert(run(function ()
|
|||||||
do local _ENV = _ENV
|
do local _ENV = _ENV
|
||||||
f = function () AAA = BBB + 1; return AAA end
|
f = function () AAA = BBB + 1; return AAA end
|
||||||
end
|
end
|
||||||
local g = new(10); g.k.BBB = 10;
|
g = new(10); g.k.BBB = 10;
|
||||||
debug.setupvalue(f, 1, g)
|
debug.setupvalue(f, 1, g)
|
||||||
assert(run(f, {"idx", "nidx", "idx"}) == 11)
|
assert(run(f, {"idx", "nidx", "idx"}) == 11)
|
||||||
assert(g.k.AAA == 11)
|
assert(g.k.AAA == 11)
|
||||||
@@ -1053,7 +694,7 @@ assert(run(function ()
|
|||||||
-- tests for coroutine API
|
-- tests for coroutine API
|
||||||
if T==nil then
|
if T==nil then
|
||||||
(Message or print)('\n >>> testC not active: skipping coroutine API tests <<<\n')
|
(Message or print)('\n >>> testC not active: skipping coroutine API tests <<<\n')
|
||||||
print "OK"; return
|
return
|
||||||
end
|
end
|
||||||
|
|
||||||
print('testing coroutine API')
|
print('testing coroutine API')
|
||||||
@@ -1119,31 +760,6 @@ f = T.makeCfunc([[
|
|||||||
return *
|
return *
|
||||||
]], 23, "huu")
|
]], 23, "huu")
|
||||||
|
|
||||||
|
|
||||||
do -- testing bug introduced in commit f407b3c4a
|
|
||||||
local X = false -- flag "to be closed"
|
|
||||||
local coro = coroutine.wrap(T.testC)
|
|
||||||
-- runs it until 'pcallk' (that yields)
|
|
||||||
-- 4th argument (at index 4): object to be closed
|
|
||||||
local res1, res2 = coro(
|
|
||||||
[[
|
|
||||||
toclose 3 # this could break the next 'pcallk'
|
|
||||||
pushvalue 2 # push function 'yield' to call it
|
|
||||||
pushint 22; pushint 33 # arguments to yield
|
|
||||||
# calls 'yield' (2 args; 2 results; continuation function at index 4)
|
|
||||||
pcallk 2 2 4
|
|
||||||
invalid command (should not arrive here)
|
|
||||||
]], -- 1st argument (at index 1): code;
|
|
||||||
coroutine.yield, -- (at index 2): function to be called
|
|
||||||
func2close(function () X = true end), -- (index 3): TBC slot
|
|
||||||
"pushint 43; return 3" -- (index 4): code for continuation function
|
|
||||||
)
|
|
||||||
|
|
||||||
assert(res1 == 22 and res2 == 33 and not X)
|
|
||||||
local res1, res2, res3 = coro(34, "hi") -- runs continuation function
|
|
||||||
assert(res1 == 34 and res2 == "hi" and res3 == 43 and X)
|
|
||||||
end
|
|
||||||
|
|
||||||
x = coroutine.wrap(f)
|
x = coroutine.wrap(f)
|
||||||
assert(x() == 102)
|
assert(x() == 102)
|
||||||
eqtab({x()}, {23, "huu"})
|
eqtab({x()}, {23, "huu"})
|
||||||
@@ -1193,19 +809,17 @@ assert(#a == 3 and a[1] == a[2] and a[2] == a[3] and a[3] == 34)
|
|||||||
|
|
||||||
-- testing yields with continuations
|
-- testing yields with continuations
|
||||||
|
|
||||||
local y
|
|
||||||
|
|
||||||
co = coroutine.wrap(function (...) return
|
co = coroutine.wrap(function (...) return
|
||||||
T.testC([[ # initial function
|
T.testC([[ # initial function
|
||||||
yieldk 1 2
|
yieldk 1 2
|
||||||
cannot be here!
|
cannot be here!
|
||||||
]],
|
]],
|
||||||
[[ # 1st continuation
|
[[ # 1st continuation
|
||||||
yieldk 0 3
|
yieldk 0 3
|
||||||
cannot be here!
|
cannot be here!
|
||||||
]],
|
]],
|
||||||
[[ # 2nd continuation
|
[[ # 2nd continuation
|
||||||
yieldk 0 4
|
yieldk 0 4
|
||||||
cannot be here!
|
cannot be here!
|
||||||
]],
|
]],
|
||||||
[[ # 3th continuation
|
[[ # 3th continuation
|
||||||
@@ -1228,9 +842,9 @@ co = coroutine.wrap(function (...) return
|
|||||||
end)
|
end)
|
||||||
|
|
||||||
local a = {co(3,4,6)}
|
local a = {co(3,4,6)}
|
||||||
assert(a[1] == 6 and a[2] == undef)
|
assert(a[1] == 6 and a[2] == nil)
|
||||||
a = {co()}; assert(a[1] == undef and _G.status == "YIELD" and _G.ctx == 2)
|
a = {co()}; assert(a[1] == nil and _G.status == "YIELD" and _G.ctx == 2)
|
||||||
a = {co()}; assert(a[1] == undef and _G.status == "YIELD" and _G.ctx == 3)
|
a = {co()}; assert(a[1] == nil and _G.status == "YIELD" and _G.ctx == 3)
|
||||||
a = {co(7,8)};
|
a = {co(7,8)};
|
||||||
-- original arguments
|
-- original arguments
|
||||||
assert(type(a[1]) == 'string' and type(a[2]) == 'string' and
|
assert(type(a[1]) == 'string' and type(a[2]) == 'string' and
|
||||||
@@ -1247,9 +861,6 @@ assert(x == "YIELD" and y == 4)
|
|||||||
|
|
||||||
assert(not pcall(co)) -- coroutine should be dead
|
assert(not pcall(co)) -- coroutine should be dead
|
||||||
|
|
||||||
_G.ctx = nil
|
|
||||||
_G.status = nil
|
|
||||||
|
|
||||||
|
|
||||||
-- bug in nCcalls
|
-- bug in nCcalls
|
||||||
local co = coroutine.wrap(function ()
|
local co = coroutine.wrap(function ()
|
||||||
|
|||||||
@@ -1,197 +0,0 @@
|
|||||||
-- $Id: testes/cstack.lua $
|
|
||||||
-- See Copyright Notice in file lua.h
|
|
||||||
|
|
||||||
|
|
||||||
local tracegc = require"tracegc"
|
|
||||||
|
|
||||||
print"testing stack overflow detection"
|
|
||||||
|
|
||||||
-- Segmentation faults in these tests probably result from a C-stack
|
|
||||||
-- overflow. To avoid these errors, you should set a smaller limit for
|
|
||||||
-- the use of C stack by Lua, by changing the constant 'LUAI_MAXCCALLS'.
|
|
||||||
-- Alternatively, you can ensure a larger stack for the program.
|
|
||||||
|
|
||||||
|
|
||||||
local function checkerror (msg, f, ...)
|
|
||||||
local s, err = pcall(f, ...)
|
|
||||||
assert(not s and string.find(err, msg))
|
|
||||||
end
|
|
||||||
|
|
||||||
do print("testing stack overflow in message handling")
|
|
||||||
local count = 0
|
|
||||||
local function loop (x, y, z)
|
|
||||||
count = count + 1
|
|
||||||
return 1 + loop(x, y, z)
|
|
||||||
end
|
|
||||||
tracegc.stop() -- __gc should not be called with a full stack
|
|
||||||
local res, msg = xpcall(loop, loop)
|
|
||||||
tracegc.start()
|
|
||||||
assert(msg == "error in error handling")
|
|
||||||
print("final count: ", count)
|
|
||||||
end
|
|
||||||
|
|
||||||
|
|
||||||
-- bug since 2.5 (C-stack overflow in recursion inside pattern matching)
|
|
||||||
do print("testing recursion inside pattern matching")
|
|
||||||
local function f (size)
|
|
||||||
local s = string.rep("a", size)
|
|
||||||
local p = string.rep(".?", size)
|
|
||||||
return string.match(s, p)
|
|
||||||
end
|
|
||||||
local m = f(80)
|
|
||||||
assert(#m == 80)
|
|
||||||
checkerror("too complex", f, 2000)
|
|
||||||
end
|
|
||||||
|
|
||||||
|
|
||||||
do print("testing stack-overflow in recursive 'gsub'")
|
|
||||||
local count = 0
|
|
||||||
local function foo ()
|
|
||||||
count = count + 1
|
|
||||||
string.gsub("a", ".", foo)
|
|
||||||
end
|
|
||||||
checkerror("stack overflow", foo)
|
|
||||||
print("final count: ", count)
|
|
||||||
|
|
||||||
print("testing stack-overflow in recursive 'gsub' with metatables")
|
|
||||||
local count = 0
|
|
||||||
local t = setmetatable({}, {__index = foo})
|
|
||||||
foo = function ()
|
|
||||||
count = count + 1
|
|
||||||
string.gsub("a", ".", t)
|
|
||||||
end
|
|
||||||
checkerror("stack overflow", foo)
|
|
||||||
print("final count: ", count)
|
|
||||||
end
|
|
||||||
|
|
||||||
|
|
||||||
do -- bug in 5.4.0
|
|
||||||
print("testing limits in coroutines inside deep calls")
|
|
||||||
local count = 0
|
|
||||||
local lim = 1000
|
|
||||||
local function stack (n)
|
|
||||||
if n > 0 then return stack(n - 1) + 1
|
|
||||||
else coroutine.wrap(function ()
|
|
||||||
count = count + 1
|
|
||||||
stack(lim)
|
|
||||||
end)()
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
local st, msg = xpcall(stack, function () return "ok" end, lim)
|
|
||||||
assert(not st and msg == "ok")
|
|
||||||
print("final count: ", count)
|
|
||||||
end
|
|
||||||
|
|
||||||
|
|
||||||
do -- bug since 5.4.0
|
|
||||||
local count = 0
|
|
||||||
print("chain of 'coroutine.close'")
|
|
||||||
-- create N coroutines forming a list so that each one, when closed,
|
|
||||||
-- closes the previous one. (With a large enough N, previous Lua
|
|
||||||
-- versions crash in this test.)
|
|
||||||
local coro = false
|
|
||||||
for i = 1, 1000 do
|
|
||||||
local previous = coro
|
|
||||||
coro = coroutine.create(function()
|
|
||||||
local cc <close> = setmetatable({}, {__close=function()
|
|
||||||
count = count + 1
|
|
||||||
if previous then
|
|
||||||
assert(coroutine.close(previous))
|
|
||||||
end
|
|
||||||
end})
|
|
||||||
coroutine.yield() -- leaves 'cc' pending to be closed
|
|
||||||
end)
|
|
||||||
assert(coroutine.resume(coro)) -- start it and run until it yields
|
|
||||||
end
|
|
||||||
local st, msg = coroutine.close(coro)
|
|
||||||
assert(not st and string.find(msg, "C stack overflow"))
|
|
||||||
print("final count: ", count)
|
|
||||||
end
|
|
||||||
|
|
||||||
|
|
||||||
do
|
|
||||||
print("nesting of resuming yielded coroutines")
|
|
||||||
local count = 0
|
|
||||||
|
|
||||||
local function body ()
|
|
||||||
coroutine.yield()
|
|
||||||
local f = coroutine.wrap(body)
|
|
||||||
f(); -- start new coroutine (will stop in previous yield)
|
|
||||||
count = count + 1
|
|
||||||
f() -- call it recursively
|
|
||||||
end
|
|
||||||
|
|
||||||
local f = coroutine.wrap(body)
|
|
||||||
f()
|
|
||||||
assert(not pcall(f))
|
|
||||||
print("final count: ", count)
|
|
||||||
end
|
|
||||||
|
|
||||||
|
|
||||||
do -- bug in 5.4.2
|
|
||||||
print("nesting coroutines running after recoverable errors")
|
|
||||||
local count = 0
|
|
||||||
local function foo()
|
|
||||||
count = count + 1
|
|
||||||
pcall(1) -- create an error
|
|
||||||
-- running now inside 'precover' ("protected recover")
|
|
||||||
coroutine.wrap(foo)() -- call another coroutine
|
|
||||||
end
|
|
||||||
checkerror("C stack overflow", foo)
|
|
||||||
print("final count: ", count)
|
|
||||||
end
|
|
||||||
|
|
||||||
|
|
||||||
if T then
|
|
||||||
print("testing stack recovery")
|
|
||||||
local N = 0 -- trace number of calls
|
|
||||||
local LIM = -1 -- will store N just before stack overflow
|
|
||||||
|
|
||||||
-- trace stack size; after stack overflow, it should be
|
|
||||||
-- the maximum allowed stack size.
|
|
||||||
local stack1
|
|
||||||
local dummy
|
|
||||||
|
|
||||||
local function err(msg)
|
|
||||||
assert(string.find(msg, "stack overflow"))
|
|
||||||
local _, stacknow = T.stacklevel()
|
|
||||||
assert(stacknow == stack1 + 200)
|
|
||||||
end
|
|
||||||
|
|
||||||
-- When LIM==-1, the 'if' is not executed, so this function only
|
|
||||||
-- counts and stores the stack limits up to overflow. Then, LIM
|
|
||||||
-- becomes N, and then the 'if' code is run when the stack is
|
|
||||||
-- full. Then, there is a stack overflow inside 'xpcall', after which
|
|
||||||
-- the stack must have been restored back to its maximum normal size.
|
|
||||||
local function f()
|
|
||||||
dummy, stack1 = T.stacklevel()
|
|
||||||
if N == LIM then
|
|
||||||
xpcall(f, err)
|
|
||||||
local _, stacknow = T.stacklevel()
|
|
||||||
assert(stacknow == stack1)
|
|
||||||
return
|
|
||||||
end
|
|
||||||
N = N + 1
|
|
||||||
f()
|
|
||||||
end
|
|
||||||
|
|
||||||
local topB, sizeB -- top and size Before overflow
|
|
||||||
local topA, sizeA -- top and size After overflow
|
|
||||||
topB, sizeB = T.stacklevel()
|
|
||||||
tracegc.stop() -- __gc should not be called with a full stack
|
|
||||||
xpcall(f, err)
|
|
||||||
tracegc.start()
|
|
||||||
topA, sizeA = T.stacklevel()
|
|
||||||
-- sizes should be comparable
|
|
||||||
assert(topA == topB and sizeA < sizeB * 2)
|
|
||||||
print(string.format("maximum stack size: %d", stack1))
|
|
||||||
LIM = N -- will stop recursion at maximum level
|
|
||||||
N = 0 -- to count again
|
|
||||||
tracegc.stop() -- __gc should not be called with a full stack
|
|
||||||
f()
|
|
||||||
tracegc.start()
|
|
||||||
print"+"
|
|
||||||
end
|
|
||||||
|
|
||||||
print'OK'
|
|
||||||
+58
-267
@@ -1,5 +1,5 @@
|
|||||||
-- $Id: testes/db.lua $
|
-- $Id: db.lua,v 1.79 2016/11/07 13:02:34 roberto Exp $
|
||||||
-- See Copyright Notice in file lua.h
|
-- See Copyright Notice in file all.lua
|
||||||
|
|
||||||
-- testing debug library
|
-- testing debug library
|
||||||
|
|
||||||
@@ -16,7 +16,7 @@ end
|
|||||||
assert(not debug.gethook())
|
assert(not debug.gethook())
|
||||||
|
|
||||||
local testline = 19 -- line where 'test' is defined
|
local testline = 19 -- line where 'test' is defined
|
||||||
local function test (s, l, p) -- this must be line 19
|
function test (s, l, p) -- this must be line 19
|
||||||
collectgarbage() -- avoid gc during trace
|
collectgarbage() -- avoid gc during trace
|
||||||
local function f (event, line)
|
local function f (event, line)
|
||||||
assert(event == 'line')
|
assert(event == 'line')
|
||||||
@@ -31,7 +31,6 @@ end
|
|||||||
|
|
||||||
do
|
do
|
||||||
assert(not pcall(debug.getinfo, print, "X")) -- invalid option
|
assert(not pcall(debug.getinfo, print, "X")) -- invalid option
|
||||||
assert(not pcall(debug.getinfo, 0, ">")) -- invalid option
|
|
||||||
assert(not debug.getinfo(1000)) -- out of range level
|
assert(not debug.getinfo(1000)) -- out of range level
|
||||||
assert(not debug.getinfo(-1)) -- out of range level
|
assert(not debug.getinfo(-1)) -- out of range level
|
||||||
local a = debug.getinfo(print)
|
local a = debug.getinfo(print)
|
||||||
@@ -49,17 +48,8 @@ do
|
|||||||
end
|
end
|
||||||
|
|
||||||
|
|
||||||
-- bug in 5.4.4-5.4.6: activelines in vararg functions
|
|
||||||
-- without debug information
|
|
||||||
do
|
|
||||||
local func = load(string.dump(load("print(10)"), true))
|
|
||||||
local actl = debug.getinfo(func, "L").activelines
|
|
||||||
assert(#actl == 0) -- no line info
|
|
||||||
end
|
|
||||||
|
|
||||||
|
|
||||||
-- test file and string names truncation
|
-- test file and string names truncation
|
||||||
local a = "function f () end"
|
a = "function f () end"
|
||||||
local function dostring (s, x) return load(s, x)() end
|
local function dostring (s, x) return load(s, x)() end
|
||||||
dostring(a)
|
dostring(a)
|
||||||
assert(debug.getinfo(f).short_src == string.format('[string "%s"]', a))
|
assert(debug.getinfo(f).short_src == string.format('[string "%s"]', a))
|
||||||
@@ -81,8 +71,7 @@ dostring(a, string.format("=%s", string.rep('x', 500)))
|
|||||||
assert(string.find(debug.getinfo(f).short_src, "^x*$"))
|
assert(string.find(debug.getinfo(f).short_src, "^x*$"))
|
||||||
dostring(a, "=")
|
dostring(a, "=")
|
||||||
assert(debug.getinfo(f).short_src == "")
|
assert(debug.getinfo(f).short_src == "")
|
||||||
_G.a = nil; _G.f = nil;
|
a = nil; f = nil;
|
||||||
_G[string.rep("p", 400)] = nil
|
|
||||||
|
|
||||||
|
|
||||||
repeat
|
repeat
|
||||||
@@ -128,19 +117,7 @@ then
|
|||||||
else
|
else
|
||||||
a=2
|
a=2
|
||||||
end
|
end
|
||||||
]], {2,4,7})
|
]], {2,3,4,7})
|
||||||
|
|
||||||
|
|
||||||
test([[
|
|
||||||
local function foo()
|
|
||||||
end
|
|
||||||
foo()
|
|
||||||
A = 1
|
|
||||||
A = 2
|
|
||||||
A = 3
|
|
||||||
]], {2, 3, 2, 4, 5, 6})
|
|
||||||
_G.A = nil
|
|
||||||
|
|
||||||
|
|
||||||
test([[--
|
test([[--
|
||||||
if nil then
|
if nil then
|
||||||
@@ -185,73 +162,9 @@ test([[for i,v in pairs{'a','b'} do
|
|||||||
end
|
end
|
||||||
]], {1,2,1,2,1,3})
|
]], {1,2,1,2,1,3})
|
||||||
|
|
||||||
test([[for i=1,4 do a=1 end]], {1,1,1,1})
|
test([[for i=1,4 do a=1 end]], {1,1,1,1,1})
|
||||||
|
|
||||||
_G.a = nil
|
|
||||||
|
|
||||||
|
|
||||||
do -- testing line info/trace with large gaps in source
|
|
||||||
|
|
||||||
local a = {1, 2, 3, 10, 124, 125, 126, 127, 128, 129, 130,
|
|
||||||
255, 256, 257, 500, 1000}
|
|
||||||
local s = [[
|
|
||||||
local b = {10}
|
|
||||||
a = b[1] X + Y b[1]
|
|
||||||
b = 4
|
|
||||||
]]
|
|
||||||
for _, i in ipairs(a) do
|
|
||||||
local subs = {X = string.rep("\n", i)}
|
|
||||||
for _, j in ipairs(a) do
|
|
||||||
subs.Y = string.rep("\n", j)
|
|
||||||
local s = string.gsub(s, "[XY]", subs)
|
|
||||||
test(s, {1, 2 + i, 2 + i + j, 2 + i, 2 + i + j, 3 + i + j})
|
|
||||||
end
|
|
||||||
end
|
|
||||||
end
|
|
||||||
_G.a = nil
|
|
||||||
|
|
||||||
|
|
||||||
do -- testing active lines
|
|
||||||
local function checkactivelines (f, lines)
|
|
||||||
local t = debug.getinfo(f, "SL")
|
|
||||||
for _, l in pairs(lines) do
|
|
||||||
l = l + t.linedefined
|
|
||||||
assert(t.activelines[l])
|
|
||||||
t.activelines[l] = undef
|
|
||||||
end
|
|
||||||
assert(next(t.activelines) == nil) -- no extra lines
|
|
||||||
end
|
|
||||||
|
|
||||||
checkactivelines(function (...) -- vararg function
|
|
||||||
-- 1st line is empty
|
|
||||||
-- 2nd line is empty
|
|
||||||
-- 3th line is empty
|
|
||||||
local a = 20
|
|
||||||
-- 5th line is empty
|
|
||||||
local b = 30
|
|
||||||
-- 7th line is empty
|
|
||||||
end, {4, 6, 8})
|
|
||||||
|
|
||||||
checkactivelines(function (a)
|
|
||||||
-- 1st line is empty
|
|
||||||
-- 2nd line is empty
|
|
||||||
local a = 20
|
|
||||||
local b = 30
|
|
||||||
-- 5th line is empty
|
|
||||||
end, {3, 4, 6})
|
|
||||||
|
|
||||||
checkactivelines(function (a, b, ...) end, {0})
|
|
||||||
|
|
||||||
checkactivelines(function (a, b)
|
|
||||||
end, {1})
|
|
||||||
|
|
||||||
for _, n in pairs{0, 1, 2, 10, 50, 100, 1000, 10000} do
|
|
||||||
checkactivelines(
|
|
||||||
load(string.format("%s return 1", string.rep("\n", n))),
|
|
||||||
{n + 1})
|
|
||||||
end
|
|
||||||
|
|
||||||
end
|
|
||||||
|
|
||||||
print'+'
|
print'+'
|
||||||
|
|
||||||
@@ -274,23 +187,19 @@ assert(not debug.getlocal(co, foo, 3))
|
|||||||
assert(not debug.getlocal(print, 1))
|
assert(not debug.getlocal(print, 1))
|
||||||
|
|
||||||
|
|
||||||
local function foo () return (debug.getlocal(1, -1)) end
|
|
||||||
assert(not foo(10))
|
|
||||||
|
|
||||||
|
|
||||||
-- varargs
|
-- varargs
|
||||||
local function foo (a, ...)
|
local function foo (a, ...)
|
||||||
local t = table.pack(...)
|
local t = table.pack(...)
|
||||||
for i = 1, t.n do
|
for i = 1, t.n do
|
||||||
local n, v = debug.getlocal(1, -i)
|
local n, v = debug.getlocal(1, -i)
|
||||||
assert(n == "(vararg)" and v == t[i])
|
assert(n == "(*vararg)" and v == t[i])
|
||||||
end
|
end
|
||||||
assert(not debug.getlocal(1, -(t.n + 1)))
|
assert(not debug.getlocal(1, -(t.n + 1)))
|
||||||
assert(not debug.setlocal(1, -(t.n + 1), 30))
|
assert(not debug.setlocal(1, -(t.n + 1), 30))
|
||||||
if t.n > 0 then
|
if t.n > 0 then
|
||||||
(function (x)
|
(function (x)
|
||||||
assert(debug.setlocal(2, -1, x) == "(vararg)")
|
assert(debug.setlocal(2, -1, x) == "(*vararg)")
|
||||||
assert(debug.setlocal(2, -t.n, x) == "(vararg)")
|
assert(debug.setlocal(2, -t.n, x) == "(*vararg)")
|
||||||
end)(430)
|
end)(430)
|
||||||
assert(... == 430)
|
assert(... == 430)
|
||||||
end
|
end
|
||||||
@@ -302,7 +211,11 @@ foo(200, 3, 4)
|
|||||||
local a = {}
|
local a = {}
|
||||||
for i = 1, (_soft and 100 or 1000) do a[i] = i end
|
for i = 1, (_soft and 100 or 1000) do a[i] = i end
|
||||||
foo(table.unpack(a))
|
foo(table.unpack(a))
|
||||||
|
a = nil
|
||||||
|
|
||||||
|
-- access to vararg in non-vararg function
|
||||||
|
local function foo () return debug.getlocal(1, -1) end
|
||||||
|
assert(not foo(10))
|
||||||
|
|
||||||
|
|
||||||
do -- test hook presence in debug info
|
do -- test hook presence in debug info
|
||||||
@@ -321,14 +234,9 @@ do -- test hook presence in debug info
|
|||||||
debug.sethook()
|
debug.sethook()
|
||||||
assert(count == 4)
|
assert(count == 4)
|
||||||
end
|
end
|
||||||
_ENV.a = nil
|
|
||||||
|
|
||||||
|
|
||||||
-- hook table has weak keys
|
a = {}; L = nil
|
||||||
assert(getmetatable(debug.getregistry()._HOOKKEY).__mode == 'k')
|
|
||||||
|
|
||||||
|
|
||||||
a = {}; local L = nil
|
|
||||||
local glob = 1
|
local glob = 1
|
||||||
local oldglob = glob
|
local oldglob = glob
|
||||||
debug.sethook(function (e,l)
|
debug.sethook(function (e,l)
|
||||||
@@ -349,15 +257,12 @@ end, "crl")
|
|||||||
|
|
||||||
|
|
||||||
function f(a,b)
|
function f(a,b)
|
||||||
-- declare some globals to check that they don't interfere with 'getlocal'
|
|
||||||
global collectgarbage
|
|
||||||
collectgarbage()
|
collectgarbage()
|
||||||
local _, x = debug.getlocal(1, 1)
|
local _, x = debug.getlocal(1, 1)
|
||||||
global assert, g, string
|
|
||||||
local _, y = debug.getlocal(1, 2)
|
local _, y = debug.getlocal(1, 2)
|
||||||
assert(x == a and y == b)
|
assert(x == a and y == b)
|
||||||
assert(debug.setlocal(2, 4, "pera") == "AA".."AA")
|
assert(debug.setlocal(2, 3, "pera") == "AA".."AA")
|
||||||
assert(debug.setlocal(2, 5, "manga") == "B")
|
assert(debug.setlocal(2, 4, "maçã") == "B")
|
||||||
x = debug.getinfo(2)
|
x = debug.getinfo(2)
|
||||||
assert(x.func == g and x.what == "Lua" and x.name == 'g' and
|
assert(x.func == g and x.what == "Lua" and x.name == 'g' and
|
||||||
x.nups == 2 and string.find(x.source, "^@.*db%.lua$"))
|
x.nups == 2 and string.find(x.source, "^@.*db%.lua$"))
|
||||||
@@ -372,7 +277,7 @@ function foo()
|
|||||||
end; foo() -- set L
|
end; foo() -- set L
|
||||||
-- check line counting inside strings and empty lines
|
-- check line counting inside strings and empty lines
|
||||||
|
|
||||||
local _ = 'alo\
|
_ = 'alo\
|
||||||
alo' .. [[
|
alo' .. [[
|
||||||
|
|
||||||
]]
|
]]
|
||||||
@@ -381,18 +286,16 @@ alo' .. [[
|
|||||||
assert(debug.getinfo(1, "l").currentline == L+11) -- check count of lines
|
assert(debug.getinfo(1, "l").currentline == L+11) -- check count of lines
|
||||||
|
|
||||||
|
|
||||||
function g (...)
|
function g(...)
|
||||||
local arg = {...}
|
local arg = {...}
|
||||||
do local a,b,c; a=math.sin(40); end
|
do local a,b,c; a=math.sin(40); end
|
||||||
local feijao
|
local feijao
|
||||||
local AAAA,B = "xuxu", "abacate"
|
local AAAA,B = "xuxu", "mamão"
|
||||||
f(AAAA,B)
|
f(AAAA,B)
|
||||||
assert(AAAA == "pera" and B == "manga")
|
assert(AAAA == "pera" and B == "maçã")
|
||||||
do
|
do
|
||||||
global *
|
|
||||||
local B = 13
|
local B = 13
|
||||||
global<const> assert
|
local x,y = debug.getlocal(1,5)
|
||||||
local x,y = debug.getlocal(1,6)
|
|
||||||
assert(x == 'B' and y == 13)
|
assert(x == 'B' and y == 13)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
@@ -406,9 +309,9 @@ assert(a[f] and a[g] and a[assert] and a[debug.getlocal] and not a[print])
|
|||||||
-- tests for manipulating non-registered locals (C and Lua temporaries)
|
-- tests for manipulating non-registered locals (C and Lua temporaries)
|
||||||
|
|
||||||
local n, v = debug.getlocal(0, 1)
|
local n, v = debug.getlocal(0, 1)
|
||||||
assert(v == 0 and n == "(C temporary)")
|
assert(v == 0 and n == "(*temporary)")
|
||||||
local n, v = debug.getlocal(0, 2)
|
local n, v = debug.getlocal(0, 2)
|
||||||
assert(v == 2 and n == "(C temporary)")
|
assert(v == 2 and n == "(*temporary)")
|
||||||
assert(not debug.getlocal(0, 3))
|
assert(not debug.getlocal(0, 3))
|
||||||
assert(not debug.getlocal(0, 0))
|
assert(not debug.getlocal(0, 0))
|
||||||
|
|
||||||
@@ -423,33 +326,9 @@ function g(a,b) return (a+1) + f() end
|
|||||||
|
|
||||||
assert(g(0,0) == 30)
|
assert(g(0,0) == 30)
|
||||||
|
|
||||||
_G.f, _G.g = nil
|
|
||||||
|
|
||||||
debug.sethook(nil);
|
debug.sethook(nil);
|
||||||
assert(not debug.gethook())
|
assert(debug.gethook() == nil)
|
||||||
|
|
||||||
|
|
||||||
-- minimal tests for setuservalue/getuservalue
|
|
||||||
do
|
|
||||||
assert(not debug.setuservalue(io.stdin, 10))
|
|
||||||
local a, b = debug.getuservalue(io.stdin, 10)
|
|
||||||
assert(a == nil and not b)
|
|
||||||
end
|
|
||||||
|
|
||||||
-- testing interaction between multiple values x hooks
|
|
||||||
do
|
|
||||||
local function f(...) return 3, ... end
|
|
||||||
local count = 0
|
|
||||||
local a = {}
|
|
||||||
for i = 1, 100 do a[i] = i end
|
|
||||||
debug.sethook(function () count = count + 1 end, "", 1)
|
|
||||||
local t = {table.unpack(a)}
|
|
||||||
assert(#t == 100)
|
|
||||||
t = {table.unpack(a, 1, 3)}
|
|
||||||
assert(#t == 3)
|
|
||||||
t = {f(table.unpack(a, 1, 30))}
|
|
||||||
assert(#t == 31)
|
|
||||||
end
|
|
||||||
|
|
||||||
|
|
||||||
-- testing access to function arguments
|
-- testing access to function arguments
|
||||||
@@ -458,8 +337,7 @@ local function collectlocals (level)
|
|||||||
local tab = {}
|
local tab = {}
|
||||||
for i = 1, math.huge do
|
for i = 1, math.huge do
|
||||||
local n, v = debug.getlocal(level + 1, i)
|
local n, v = debug.getlocal(level + 1, i)
|
||||||
if not (n and string.find(n, "^[a-zA-Z0-9_]+$") or
|
if not (n and string.find(n, "^[a-zA-Z0-9_]+$")) then
|
||||||
n == "(vararg table)") then
|
|
||||||
break -- consider only real variables
|
break -- consider only real variables
|
||||||
end
|
end
|
||||||
tab[n] = v
|
tab[n] = v
|
||||||
@@ -468,7 +346,7 @@ local function collectlocals (level)
|
|||||||
end
|
end
|
||||||
|
|
||||||
|
|
||||||
local X = nil
|
X = nil
|
||||||
a = {}
|
a = {}
|
||||||
function a:f (a, b, ...) local arg = {...}; local c = 13 end
|
function a:f (a, b, ...) local arg = {...}; local c = 13 end
|
||||||
debug.sethook(function (e)
|
debug.sethook(function (e)
|
||||||
@@ -490,15 +368,12 @@ end, "c")
|
|||||||
a:f(1,2,3,4,5)
|
a:f(1,2,3,4,5)
|
||||||
assert(X.self == a and X.a == 1 and X.b == 2 and X.c == nil)
|
assert(X.self == a and X.a == 1 and X.b == 2 and X.c == nil)
|
||||||
assert(XX == 12)
|
assert(XX == 12)
|
||||||
assert(not debug.gethook())
|
assert(debug.gethook() == nil)
|
||||||
_G.XX = nil
|
|
||||||
|
|
||||||
|
|
||||||
-- testing access to local variables in return hook (bug in 5.2)
|
-- testing access to local variables in return hook (bug in 5.2)
|
||||||
do
|
do
|
||||||
local X = false
|
local function foo (a, b)
|
||||||
|
|
||||||
local function foo (a, b, ...)
|
|
||||||
do local x,y,z end
|
do local x,y,z end
|
||||||
local c, d = 10, 20
|
local c, d = 10, 20
|
||||||
return
|
return
|
||||||
@@ -506,67 +381,20 @@ do
|
|||||||
|
|
||||||
local function aux ()
|
local function aux ()
|
||||||
if debug.getinfo(2).name == "foo" then
|
if debug.getinfo(2).name == "foo" then
|
||||||
X = true -- to signal that it found 'foo'
|
foo = nil -- to signal that it found 'foo'
|
||||||
local tab = {a = 100, b = 200, c = 10, d = 20}
|
local tab = {a = 100, b = 200, c = 10, d = 20}
|
||||||
for n, v in pairs(collectlocals(2)) do
|
for n, v in pairs(collectlocals(2)) do
|
||||||
assert(tab[n] == v)
|
assert(tab[n] == v)
|
||||||
tab[n] = undef
|
tab[n] = nil
|
||||||
end
|
end
|
||||||
assert(next(tab) == nil) -- 'tab' must be empty
|
assert(next(tab) == nil) -- 'tab' must be empty
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
debug.sethook(aux, "r"); foo(100, 200); debug.sethook()
|
debug.sethook(aux, "r"); foo(100, 200); debug.sethook()
|
||||||
assert(X)
|
assert(foo == nil)
|
||||||
|
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
||||||
local function eqseq (t1, t2)
|
|
||||||
assert(#t1 == #t2)
|
|
||||||
for i = 1, #t1 do
|
|
||||||
assert(t1[i] == t2[i])
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
|
|
||||||
do print("testing inspection of parameters/returned values")
|
|
||||||
local on = false
|
|
||||||
local inp, out
|
|
||||||
|
|
||||||
local function hook (event)
|
|
||||||
if not on then return end
|
|
||||||
local ar = debug.getinfo(2, "ruS")
|
|
||||||
local t = {}
|
|
||||||
for i = ar.ftransfer, ar.ftransfer + ar.ntransfer - 1 do
|
|
||||||
local _, v = debug.getlocal(2, i)
|
|
||||||
t[#t + 1] = v
|
|
||||||
end
|
|
||||||
if event == "return" then
|
|
||||||
out = t
|
|
||||||
else
|
|
||||||
inp = t
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
debug.sethook(hook, "cr")
|
|
||||||
|
|
||||||
on = true; math.sin(3); on = false
|
|
||||||
eqseq(inp, {3}); eqseq(out, {math.sin(3)})
|
|
||||||
|
|
||||||
on = true; select(2, 10, 20, 30, 40); on = false
|
|
||||||
eqseq(inp, {2, 10, 20, 30, 40}); eqseq(out, {20, 30, 40})
|
|
||||||
|
|
||||||
local function foo (a, ...) return ... end
|
|
||||||
local function foo1 () on = not on; return foo(20, 10, 0) end
|
|
||||||
foo1(); on = false
|
|
||||||
eqseq(inp, {20}); eqseq(out, {10, 0})
|
|
||||||
|
|
||||||
debug.sethook()
|
|
||||||
end
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
-- testing upvalue access
|
-- testing upvalue access
|
||||||
local function getupvalues (f)
|
local function getupvalues (f)
|
||||||
local t = {}
|
local t = {}
|
||||||
@@ -593,7 +421,7 @@ t = getupvalues(foo2)
|
|||||||
assert(t.a == 1 and t.b == 2 and t.c == 3)
|
assert(t.a == 1 and t.b == 2 and t.c == 3)
|
||||||
assert(debug.setupvalue(foo1, 1, "xuxu") == "b")
|
assert(debug.setupvalue(foo1, 1, "xuxu") == "b")
|
||||||
assert(({debug.getupvalue(foo2, 3)})[2] == "xuxu")
|
assert(({debug.getupvalue(foo2, 3)})[2] == "xuxu")
|
||||||
-- upvalues of C functions are always named "" (the empty string)
|
-- upvalues of C functions are allways "called" "" (the empty string)
|
||||||
assert(debug.getupvalue(string.gmatch("x", "x"), 1) == "")
|
assert(debug.getupvalue(string.gmatch("x", "x"), 1) == "")
|
||||||
|
|
||||||
|
|
||||||
@@ -616,7 +444,6 @@ end
|
|||||||
|
|
||||||
debug.sethook()
|
debug.sethook()
|
||||||
|
|
||||||
local g, g1
|
|
||||||
|
|
||||||
-- tests for tail calls
|
-- tests for tail calls
|
||||||
local function f (x)
|
local function f (x)
|
||||||
@@ -630,9 +457,6 @@ local function f (x)
|
|||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
assert(debug.getinfo(print, 't').istailcall == false)
|
|
||||||
assert(debug.getinfo(print, 't').extraargs == 0)
|
|
||||||
|
|
||||||
function g(x) return f(x) end
|
function g(x) return f(x) end
|
||||||
|
|
||||||
function g1(x) g(x) end
|
function g1(x) g(x) end
|
||||||
@@ -665,7 +489,7 @@ h(false)
|
|||||||
debug.sethook()
|
debug.sethook()
|
||||||
assert(b == 2) -- two tail calls
|
assert(b == 2) -- two tail calls
|
||||||
|
|
||||||
local lim = _soft and 3000 or 30000
|
lim = _soft and 3000 or 30000
|
||||||
local function foo (x)
|
local function foo (x)
|
||||||
if x==0 then
|
if x==0 then
|
||||||
assert(debug.getinfo(2).what == "main")
|
assert(debug.getinfo(2).what == "main")
|
||||||
@@ -692,7 +516,7 @@ co = load[[
|
|||||||
local a = 0
|
local a = 0
|
||||||
-- 'A' should be visible to debugger only after its complete definition
|
-- 'A' should be visible to debugger only after its complete definition
|
||||||
debug.sethook(function (e, l)
|
debug.sethook(function (e, l)
|
||||||
if l == 3 then a = a + 1; assert(debug.getlocal(2, 1) == "(temporary)")
|
if l == 3 then a = a + 1; assert(debug.getlocal(2, 1) == "(*temporary)")
|
||||||
elseif l == 4 then a = a + 1; assert(debug.getlocal(2, 1) == "A")
|
elseif l == 4 then a = a + 1; assert(debug.getlocal(2, 1) == "A")
|
||||||
end
|
end
|
||||||
end, "l")
|
end, "l")
|
||||||
@@ -707,7 +531,7 @@ assert(debug.traceback(print, 4) == print)
|
|||||||
assert(string.find(debug.traceback("hi", 4), "^hi\n"))
|
assert(string.find(debug.traceback("hi", 4), "^hi\n"))
|
||||||
assert(string.find(debug.traceback("hi"), "^hi\n"))
|
assert(string.find(debug.traceback("hi"), "^hi\n"))
|
||||||
assert(not string.find(debug.traceback("hi"), "'debug.traceback'"))
|
assert(not string.find(debug.traceback("hi"), "'debug.traceback'"))
|
||||||
assert(string.find(debug.traceback("hi", 0), "'traceback'"))
|
assert(string.find(debug.traceback("hi", 0), "'debug.traceback'"))
|
||||||
assert(string.find(debug.traceback(), "^stack traceback:\n"))
|
assert(string.find(debug.traceback(), "^stack traceback:\n"))
|
||||||
|
|
||||||
do -- C-function names in traceback
|
do -- C-function names in traceback
|
||||||
@@ -726,18 +550,10 @@ assert(t.isvararg == false and t.nparams == 3 and t.nups == 0)
|
|||||||
t = debug.getinfo(function (a,b,...) return t[a] end, "u")
|
t = debug.getinfo(function (a,b,...) return t[a] end, "u")
|
||||||
assert(t.isvararg == true and t.nparams == 2 and t.nups == 1)
|
assert(t.isvararg == true and t.nparams == 2 and t.nups == 1)
|
||||||
|
|
||||||
t = debug.getinfo(function (a,b,...t) t.n = 2; return t[a] end, "u")
|
|
||||||
assert(t.isvararg == true and t.nparams == 2 and t.nups == 0)
|
|
||||||
|
|
||||||
t = debug.getinfo(1) -- main
|
t = debug.getinfo(1) -- main
|
||||||
assert(t.isvararg == true and t.nparams == 0 and t.nups == 1 and
|
assert(t.isvararg == true and t.nparams == 0 and t.nups == 1 and
|
||||||
debug.getupvalue(t.func, 1) == "_ENV")
|
debug.getupvalue(t.func, 1) == "_ENV")
|
||||||
|
|
||||||
t = debug.getinfo(math.sin) -- C function
|
|
||||||
assert(t.isvararg == true and t.nparams == 0 and t.nups == 0)
|
|
||||||
|
|
||||||
t = debug.getinfo(string.gmatch("abc", "a")) -- C closure
|
|
||||||
assert(t.isvararg == true and t.nparams == 0 and t.nups > 0)
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
@@ -750,7 +566,7 @@ local function checktraceback (co, p, level)
|
|||||||
assert(i == 0 or string.find(l, p[i]))
|
assert(i == 0 or string.find(l, p[i]))
|
||||||
i = i+1
|
i = i+1
|
||||||
end
|
end
|
||||||
assert(p[i] == undef)
|
assert(p[i] == nil)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
||||||
@@ -785,7 +601,7 @@ assert(x.currentline == l.currentline and x.activelines[x.currentline])
|
|||||||
assert(type(x.func) == "function")
|
assert(type(x.func) == "function")
|
||||||
for i=x.linedefined + 1, x.lastlinedefined do
|
for i=x.linedefined + 1, x.lastlinedefined do
|
||||||
assert(x.activelines[i])
|
assert(x.activelines[i])
|
||||||
x.activelines[i] = undef
|
x.activelines[i] = nil
|
||||||
end
|
end
|
||||||
assert(next(x.activelines) == nil) -- no 'extra' elements
|
assert(next(x.activelines) == nil) -- no 'extra' elements
|
||||||
assert(not debug.getinfo(co, 2))
|
assert(not debug.getinfo(co, 2))
|
||||||
@@ -829,16 +645,11 @@ assert(a and b == 30)
|
|||||||
|
|
||||||
-- check traceback of suspended (or dead with error) coroutines
|
-- check traceback of suspended (or dead with error) coroutines
|
||||||
|
|
||||||
function f(i)
|
function f(i) if i==0 then error(i) else coroutine.yield(); f(i-1) end end
|
||||||
if i == 0 then error(i)
|
|
||||||
else coroutine.yield(); f(i-1)
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
|
|
||||||
co = coroutine.create(function (x) f(x) end)
|
co = coroutine.create(function (x) f(x) end)
|
||||||
a, b = coroutine.resume(co, 3)
|
a, b = coroutine.resume(co, 3)
|
||||||
t = {"'yield'", "'f'", "in function <"}
|
t = {"'coroutine.yield'", "'f'", "in function <"}
|
||||||
while coroutine.status(co) == "suspended" do
|
while coroutine.status(co) == "suspended" do
|
||||||
checktraceback(co, t)
|
checktraceback(co, t)
|
||||||
a, b = coroutine.resume(co)
|
a, b = coroutine.resume(co)
|
||||||
@@ -848,7 +659,7 @@ t[1] = "'error'"
|
|||||||
checktraceback(co, t)
|
checktraceback(co, t)
|
||||||
|
|
||||||
|
|
||||||
-- test accessing line numbers of a coroutine from a resume inside
|
-- test acessing line numbers of a coroutine from a resume inside
|
||||||
-- a C function (this is a known bug in Lua 5.0)
|
-- a C function (this is a known bug in Lua 5.0)
|
||||||
|
|
||||||
local function g(x)
|
local function g(x)
|
||||||
@@ -889,19 +700,16 @@ setmetatable(a, {
|
|||||||
|
|
||||||
local b = setmetatable({}, getmetatable(a))
|
local b = setmetatable({}, getmetatable(a))
|
||||||
|
|
||||||
assert(a[3] == "index" and a^3 == "pow" and a..a == "concat")
|
assert(a[3] == "__index" and a^3 == "__pow" and a..a == "__concat")
|
||||||
assert(a/3 == "div" and 3%a == "mod")
|
assert(a/3 == "__div" and 3%a == "__mod")
|
||||||
assert(a+3 == "add" and 3-a == "sub" and a*3 == "mul" and
|
assert(a+3 == "__add" and 3-a == "__sub" and a*3 == "__mul" and
|
||||||
-a == "unm" and #a == "len" and a&3 == "band")
|
-a == "__unm" and #a == "__len" and a&3 == "__band")
|
||||||
assert(a + 30000 == "add" and a - 3.0 == "sub" and a * 3.0 == "mul" and
|
assert(a|3 == "__bor" and 3~a == "__bxor" and a<<3 == "__shl" and
|
||||||
-a == "unm" and #a == "len" and a & 3 == "band")
|
a>>1 == "__shr")
|
||||||
assert(a|3 == "bor" and 3~a == "bxor" and a<<3 == "shl" and a>>1 == "shr")
|
assert (a==b and a.op == "__eq")
|
||||||
assert (a==b and a.op == "eq")
|
assert (a>=b and a.op == "__le")
|
||||||
assert (a>=b and a.op == "le")
|
assert (a>b and a.op == "__lt")
|
||||||
assert ("x">=a and a.op == "le")
|
assert(~a == "__bnot")
|
||||||
assert (a>b and a.op == "lt")
|
|
||||||
assert (a>10 and a.op == "lt")
|
|
||||||
assert(~a == "bnot")
|
|
||||||
|
|
||||||
do -- testing for-iterator name
|
do -- testing for-iterator name
|
||||||
local function f()
|
local function f()
|
||||||
@@ -917,7 +725,7 @@ do -- testing debug info for finalizers
|
|||||||
|
|
||||||
-- create a piece of garbage with a finalizer
|
-- create a piece of garbage with a finalizer
|
||||||
setmetatable({}, {__gc = function ()
|
setmetatable({}, {__gc = function ()
|
||||||
local t = debug.getinfo(1) -- get function information
|
local t = debug.getinfo(2) -- get callee information
|
||||||
assert(t.namewhat == "metamethod")
|
assert(t.namewhat == "metamethod")
|
||||||
name = t.name
|
name = t.name
|
||||||
end})
|
end})
|
||||||
@@ -949,7 +757,7 @@ do
|
|||||||
local cl = countlines(rest)
|
local cl = countlines(rest)
|
||||||
-- at most 10 lines in first part, 11 in second, plus '...'
|
-- at most 10 lines in first part, 11 in second, plus '...'
|
||||||
assert(cl <= 10 + 11 + 1)
|
assert(cl <= 10 + 11 + 1)
|
||||||
local brk = string.find(rest, "%.%.%.\t%(skip")
|
local brk = string.find(rest, "%.%.%.")
|
||||||
if brk then -- does message have '...'?
|
if brk then -- does message have '...'?
|
||||||
local rest1 = string.sub(rest, 1, brk)
|
local rest1 = string.sub(rest, 1, brk)
|
||||||
local rest2 = string.sub(rest, brk, #rest)
|
local rest2 = string.sub(rest, brk, #rest)
|
||||||
@@ -970,20 +778,20 @@ end
|
|||||||
|
|
||||||
|
|
||||||
print("testing debug functions on chunk without debug info")
|
print("testing debug functions on chunk without debug info")
|
||||||
local prog = [[-- program to be loaded without debug information (strip)
|
prog = [[-- program to be loaded without debug information
|
||||||
local debug = require'debug'
|
local debug = require'debug'
|
||||||
local a = 12 -- a local variable
|
local a = 12 -- a local variable
|
||||||
|
|
||||||
local n, v = debug.getlocal(1, 1)
|
local n, v = debug.getlocal(1, 1)
|
||||||
assert(n == "(temporary)" and v == debug) -- unknown name but known value
|
assert(n == "(*temporary)" and v == debug) -- unkown name but known value
|
||||||
n, v = debug.getlocal(1, 2)
|
n, v = debug.getlocal(1, 2)
|
||||||
assert(n == "(temporary)" and v == 12) -- unknown name but known value
|
assert(n == "(*temporary)" and v == 12) -- unkown name but known value
|
||||||
|
|
||||||
-- a function with an upvalue
|
-- a function with an upvalue
|
||||||
local f = function () local x; return a end
|
local f = function () local x; return a end
|
||||||
n, v = debug.getupvalue(f, 1)
|
n, v = debug.getupvalue(f, 1)
|
||||||
assert(n == "(no name)" and v == 12)
|
assert(n == "(*no name)" and v == 12)
|
||||||
assert(debug.setupvalue(f, 1, 13) == "(no name)")
|
assert(debug.setupvalue(f, 1, 13) == "(*no name)")
|
||||||
assert(a == 13)
|
assert(a == 13)
|
||||||
|
|
||||||
local t = debug.getinfo(f)
|
local t = debug.getinfo(f)
|
||||||
@@ -1013,23 +821,6 @@ local f = assert(load(string.dump(load(prog), true)))
|
|||||||
|
|
||||||
assert(f() == 13)
|
assert(f() == 13)
|
||||||
|
|
||||||
do -- bug in 5.4.0: line hooks in stripped code
|
|
||||||
local function foo ()
|
|
||||||
local a = 1
|
|
||||||
local b = 2
|
|
||||||
return b
|
|
||||||
end
|
|
||||||
|
|
||||||
local s = load(string.dump(foo, true))
|
|
||||||
local line = true
|
|
||||||
debug.sethook(function (e, l)
|
|
||||||
assert(e == "line")
|
|
||||||
line = l
|
|
||||||
end, "l")
|
|
||||||
assert(s() == 2); debug.sethook(nil)
|
|
||||||
assert(line == nil) -- hook called without debug info for 1st instruction
|
|
||||||
end
|
|
||||||
|
|
||||||
do -- tests for 'source' in binary dumps
|
do -- tests for 'source' in binary dumps
|
||||||
local prog = [[
|
local prog = [[
|
||||||
return function (x)
|
return function (x)
|
||||||
|
|||||||
+89
-331
@@ -1,5 +1,5 @@
|
|||||||
-- $Id: testes/errors.lua $
|
-- $Id: errors.lua,v 1.94 2016/12/21 19:23:02 roberto Exp $
|
||||||
-- See Copyright Notice in file lua.h
|
-- See Copyright Notice in file all.lua
|
||||||
|
|
||||||
print("testing errors")
|
print("testing errors")
|
||||||
|
|
||||||
@@ -18,15 +18,14 @@ end
|
|||||||
|
|
||||||
local function doit (s)
|
local function doit (s)
|
||||||
local f, msg = load(s)
|
local f, msg = load(s)
|
||||||
if not f then return msg end
|
if f == nil then return msg end
|
||||||
local cond, msg = pcall(f)
|
local cond, msg = pcall(f)
|
||||||
return (not cond) and msg
|
return (not cond) and msg
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
||||||
local function checkmessage (prog, msg, debug)
|
local function checkmessage (prog, msg)
|
||||||
local m = doit(prog)
|
local m = doit(prog)
|
||||||
if debug then print(m, msg) end
|
|
||||||
assert(string.find(m, msg, 1, true))
|
assert(string.find(m, msg, 1, true))
|
||||||
end
|
end
|
||||||
|
|
||||||
@@ -45,8 +44,8 @@ end
|
|||||||
-- test error message with no extra info
|
-- test error message with no extra info
|
||||||
assert(doit("error('hi', 0)") == 'hi')
|
assert(doit("error('hi', 0)") == 'hi')
|
||||||
|
|
||||||
-- test nil error message
|
-- test error message with no info
|
||||||
assert(doit("error()") == "<no error object>")
|
assert(doit("error()") == nil)
|
||||||
|
|
||||||
|
|
||||||
-- test common errors/errors that crashed in the past
|
-- test common errors/errors that crashed in the past
|
||||||
@@ -68,83 +67,6 @@ checksyntax([[
|
|||||||
]], "'}' expected (to close '{' at line 1)", "<eof>", 3)
|
]], "'}' expected (to close '{' at line 1)", "<eof>", 3)
|
||||||
|
|
||||||
|
|
||||||
do -- testing errors in goto/break
|
|
||||||
local function checksyntax (prog, msg, line)
|
|
||||||
local st, err = load(prog)
|
|
||||||
assert(string.find(err, "line " .. line))
|
|
||||||
assert(string.find(err, msg, 1, true))
|
|
||||||
end
|
|
||||||
|
|
||||||
checksyntax([[
|
|
||||||
::A:: a = 1
|
|
||||||
::A::
|
|
||||||
]], "label 'A' already defined", 1)
|
|
||||||
|
|
||||||
checksyntax([[
|
|
||||||
a = 1
|
|
||||||
goto A
|
|
||||||
do ::A:: end
|
|
||||||
]], "no visible label 'A'", 2)
|
|
||||||
|
|
||||||
end
|
|
||||||
|
|
||||||
|
|
||||||
if not T then
|
|
||||||
(Message or print)
|
|
||||||
('\n >>> testC not active: skipping tests for messages in C <<<\n')
|
|
||||||
else
|
|
||||||
print "testing memory error message"
|
|
||||||
local a = {}
|
|
||||||
for i = 1, 10000 do a[i] = true end -- preallocate array
|
|
||||||
collectgarbage()
|
|
||||||
T.totalmem(T.totalmem() + 10000)
|
|
||||||
-- force a memory error (by a small margin)
|
|
||||||
local st, msg = pcall(function()
|
|
||||||
for i = 1, 100000 do a[i] = tostring(i) end
|
|
||||||
end)
|
|
||||||
T.totalmem(0)
|
|
||||||
assert(not st and msg == "not enough" .. " memory")
|
|
||||||
|
|
||||||
-- stack space for luaL_traceback (bug in 5.4.6)
|
|
||||||
local res = T.testC[[
|
|
||||||
# push 16 elements on the stack
|
|
||||||
pushnum 1; pushnum 1; pushnum 1; pushnum 1; pushnum 1;
|
|
||||||
pushnum 1; pushnum 1; pushnum 1; pushnum 1; pushnum 1;
|
|
||||||
pushnum 1; pushnum 1; pushnum 1; pushnum 1; pushnum 1;
|
|
||||||
pushnum 1;
|
|
||||||
# traceback should work with 4 remaining slots
|
|
||||||
traceback xuxu 1;
|
|
||||||
return 1
|
|
||||||
]]
|
|
||||||
assert(string.find(res, "xuxu.-main chunk"))
|
|
||||||
|
|
||||||
do -- tests for error messages about extra arguments from __call
|
|
||||||
local function createobj (n)
|
|
||||||
-- function that raises an error on its n-th argument
|
|
||||||
local code = string.format("argerror %d 'msg'", n)
|
|
||||||
local func = T.makeCfunc(code)
|
|
||||||
-- create a chain of 2 __call objects
|
|
||||||
local M = setmetatable({}, {__call = func})
|
|
||||||
M = setmetatable({}, {__call = M})
|
|
||||||
-- put it as a method for a new object
|
|
||||||
return {foo = M}
|
|
||||||
end
|
|
||||||
|
|
||||||
_G.a = createobj(1) -- error in first (extra) argument
|
|
||||||
checkmessage("a:foo()", "bad extra argument #1")
|
|
||||||
|
|
||||||
_G.a = createobj(2) -- error in second (extra) argument
|
|
||||||
checkmessage("a:foo()", "bad extra argument #2")
|
|
||||||
|
|
||||||
_G.a = createobj(3) -- error in self (after two extra arguments)
|
|
||||||
checkmessage("a:foo()", "bad self")
|
|
||||||
|
|
||||||
_G.a = createobj(4) -- error in first regular argument (after self)
|
|
||||||
checkmessage("a:foo()", "bad argument #1")
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
|
|
||||||
-- tests for better error messages
|
-- tests for better error messages
|
||||||
|
|
||||||
checkmessage("a = {} + 1", "arithmetic")
|
checkmessage("a = {} + 1", "arithmetic")
|
||||||
@@ -152,45 +74,21 @@ checkmessage("a = {} | 1", "bitwise operation")
|
|||||||
checkmessage("a = {} < 1", "attempt to compare")
|
checkmessage("a = {} < 1", "attempt to compare")
|
||||||
checkmessage("a = {} <= 1", "attempt to compare")
|
checkmessage("a = {} <= 1", "attempt to compare")
|
||||||
|
|
||||||
checkmessage("aaa=1; bbbb=2; aaa=math.sin(3)+bbbb(3)", "global 'bbbb'")
|
checkmessage("a=1; bbbb=2; a=math.sin(3)+bbbb(3)", "global 'bbbb'")
|
||||||
checkmessage("aaa={}; do local aaa=1 end aaa:bbbb(3)", "method 'bbbb'")
|
checkmessage("a={}; do local a=1 end a:bbbb(3)", "method 'bbbb'")
|
||||||
checkmessage("local a={}; a.bbbb(3)", "field 'bbbb'")
|
checkmessage("local a={}; a.bbbb(3)", "field 'bbbb'")
|
||||||
assert(not string.find(doit"aaa={13}; local bbbb=1; aaa[bbbb](3)", "'bbbb'"))
|
assert(not string.find(doit"a={13}; local bbbb=1; a[bbbb](3)", "'bbbb'"))
|
||||||
checkmessage("aaa={13}; local bbbb=1; aaa[bbbb](3)", "number")
|
checkmessage("a={13}; local bbbb=1; a[bbbb](3)", "number")
|
||||||
checkmessage("aaa=(1)..{}", "a table value")
|
checkmessage("a=(1)..{}", "a table value")
|
||||||
|
|
||||||
-- bug in 5.4.6
|
checkmessage("a = #print", "length of a function value")
|
||||||
checkmessage("a = {_ENV = {}}; print(a._ENV.x + 1)", "field 'x'")
|
checkmessage("a = #3", "length of a number value")
|
||||||
|
|
||||||
-- a similar bug, since 5.4.0
|
|
||||||
checkmessage("print(('_ENV').x + 1)", "field 'x'")
|
|
||||||
|
|
||||||
_G.aaa, _G.bbbb = nil
|
|
||||||
|
|
||||||
-- calls
|
|
||||||
checkmessage("local a; a(13)", "local 'a'")
|
|
||||||
checkmessage([[
|
|
||||||
local a = setmetatable({}, {__add = 34})
|
|
||||||
a = a + 1
|
|
||||||
]], "metamethod 'add'")
|
|
||||||
checkmessage([[
|
|
||||||
local a = setmetatable({}, {__lt = {}})
|
|
||||||
a = a > a
|
|
||||||
]], "metamethod 'lt'")
|
|
||||||
|
|
||||||
-- tail calls
|
|
||||||
checkmessage("local a={}; return a.bbbb(3)", "field 'bbbb'")
|
|
||||||
checkmessage("aaa={}; do local aaa=1 end; return aaa:bbbb(3)", "method 'bbbb'")
|
|
||||||
|
|
||||||
checkmessage("aaa = #print", "length of a function value")
|
|
||||||
checkmessage("aaa = #3", "length of a number value")
|
|
||||||
|
|
||||||
_G.aaa = nil
|
|
||||||
|
|
||||||
|
aaa = nil
|
||||||
checkmessage("aaa.bbb:ddd(9)", "global 'aaa'")
|
checkmessage("aaa.bbb:ddd(9)", "global 'aaa'")
|
||||||
checkmessage("local aaa={bbb=1}; aaa.bbb:ddd(9)", "field 'bbb'")
|
checkmessage("local aaa={bbb=1}; aaa.bbb:ddd(9)", "field 'bbb'")
|
||||||
checkmessage("local aaa={bbb={}}; aaa.bbb:ddd(9)", "method 'ddd'")
|
checkmessage("local aaa={bbb={}}; aaa.bbb:ddd(9)", "method 'ddd'")
|
||||||
checkmessage("local a,b,c; (function () a = b+1.1 end)()", "upvalue 'b'")
|
checkmessage("local a,b,c; (function () a = b+1 end)()", "upvalue 'b'")
|
||||||
assert(not doit"local aaa={bbb={ddd=next}}; aaa.bbb:ddd(nil)")
|
assert(not doit"local aaa={bbb={ddd=next}}; aaa.bbb:ddd(nil)")
|
||||||
|
|
||||||
-- upvalues being indexed do not go to the stack
|
-- upvalues being indexed do not go to the stack
|
||||||
@@ -199,16 +97,15 @@ checkmessage("local a,b,cc; (function () a.x = 1 end)()", "upvalue 'a'")
|
|||||||
|
|
||||||
checkmessage("local _ENV = {x={}}; a = a + 1", "global 'a'")
|
checkmessage("local _ENV = {x={}}; a = a + 1", "global 'a'")
|
||||||
|
|
||||||
checkmessage("BB=1; local aaa={}; x=aaa+BB", "local 'aaa'")
|
checkmessage("b=1; local aaa='a'; x=aaa+b", "local 'aaa'")
|
||||||
checkmessage("aaa={}; x=3.3/aaa", "global 'aaa'")
|
checkmessage("aaa={}; x=3/aaa", "global 'aaa'")
|
||||||
checkmessage("aaa=2; BB=nil;x=aaa*BB", "global 'BB'")
|
checkmessage("aaa='2'; b=nil;x=aaa*b", "global 'b'")
|
||||||
checkmessage("aaa={}; x=-aaa", "global 'aaa'")
|
checkmessage("aaa={}; x=-aaa", "global 'aaa'")
|
||||||
|
|
||||||
-- short circuit
|
-- short circuit
|
||||||
checkmessage("aaa=1; local aaa,bbbb=2,3; aaa = math.sin(1) and bbbb(3)",
|
checkmessage("a=1; local a,bbbb=2,3; a = math.sin(1) and bbbb(3)",
|
||||||
"local 'bbbb'")
|
"local 'bbbb'")
|
||||||
checkmessage("aaa=1; local aaa,bbbb=2,3; aaa = bbbb(1) or aaa(3)",
|
checkmessage("a=1; local a,bbbb=2,3; a = bbbb(1) or a(3)", "local 'bbbb'")
|
||||||
"local 'bbbb'")
|
|
||||||
checkmessage("local a,b,c,f = 1,1,1; f((a and b) or c)", "local 'f'")
|
checkmessage("local a,b,c,f = 1,1,1; f((a and b) or c)", "local 'f'")
|
||||||
checkmessage("local a,b,c = 1,1,1; ((a and b) or c)()", "call a number value")
|
checkmessage("local a,b,c = 1,1,1; ((a and b) or c)()", "call a number value")
|
||||||
assert(not string.find(doit"aaa={}; x=(aaa or aaa)+(aaa and aaa)", "'aaa'"))
|
assert(not string.find(doit"aaa={}; x=(aaa or aaa)+(aaa and aaa)", "'aaa'"))
|
||||||
@@ -222,9 +119,9 @@ checkmessage("print(10 < '23')", "number with string")
|
|||||||
-- float->integer conversions
|
-- float->integer conversions
|
||||||
checkmessage("local a = 2.0^100; x = a << 2", "local a")
|
checkmessage("local a = 2.0^100; x = a << 2", "local a")
|
||||||
checkmessage("local a = 1 >> 2.0^100", "has no integer representation")
|
checkmessage("local a = 1 >> 2.0^100", "has no integer representation")
|
||||||
checkmessage("local a = 10.1 << 2.0^100", "has no integer representation")
|
checkmessage("local a = '10' << 2.0^100", "has no integer representation")
|
||||||
checkmessage("local a = 2.0^100 & 1", "has no integer representation")
|
checkmessage("local a = 2.0^100 & 1", "has no integer representation")
|
||||||
checkmessage("local a = 2.0^100 & 1e100", "has no integer representation")
|
checkmessage("local a = 2.0^100 & '1'", "has no integer representation")
|
||||||
checkmessage("local a = 2.0 | 1e40", "has no integer representation")
|
checkmessage("local a = 2.0 | 1e40", "has no integer representation")
|
||||||
checkmessage("local a = 2e100 ~ 1", "has no integer representation")
|
checkmessage("local a = 2e100 ~ 1", "has no integer representation")
|
||||||
checkmessage("string.sub('a', 2.0^100)", "has no integer representation")
|
checkmessage("string.sub('a', 2.0^100)", "has no integer representation")
|
||||||
@@ -235,27 +132,10 @@ checkmessage("return ~-3e40", "has no integer representation")
|
|||||||
checkmessage("return ~-3.009", "has no integer representation")
|
checkmessage("return ~-3.009", "has no integer representation")
|
||||||
checkmessage("return 3.009 & 1", "has no integer representation")
|
checkmessage("return 3.009 & 1", "has no integer representation")
|
||||||
checkmessage("return 34 >> {}", "table value")
|
checkmessage("return 34 >> {}", "table value")
|
||||||
checkmessage("aaa = 24 // 0", "divide by zero")
|
checkmessage("a = 24 // 0", "divide by zero")
|
||||||
checkmessage("aaa = 1 % 0", "'n%0'")
|
checkmessage("a = 1 % 0", "'n%0'")
|
||||||
|
|
||||||
|
|
||||||
-- type error for an object which is neither in an upvalue nor a register.
|
|
||||||
-- The following code will try to index the value 10 that is stored in
|
|
||||||
-- the metatable, without moving it to a register.
|
|
||||||
checkmessage("local a = setmetatable({}, {__index = 10}).x",
|
|
||||||
"attempt to index a number value")
|
|
||||||
|
|
||||||
|
|
||||||
-- numeric for loops
|
|
||||||
checkmessage("for i = {}, 10 do end", "table")
|
|
||||||
checkmessage("for i = io.stdin, 10 do end", "FILE")
|
|
||||||
checkmessage("for i = {}, 10 do end", "initial value")
|
|
||||||
checkmessage("for i = 1, 'x', 10 do end", "string")
|
|
||||||
checkmessage("for i = 1, {}, 10 do end", "limit")
|
|
||||||
checkmessage("for i = 1, {} do end", "limit")
|
|
||||||
checkmessage("for i = 1, 10, print do end", "step")
|
|
||||||
checkmessage("for i = 1, 10, print do end", "function")
|
|
||||||
|
|
||||||
-- passing light userdata instead of full userdata
|
-- passing light userdata instead of full userdata
|
||||||
_G.D = debug
|
_G.D = debug
|
||||||
checkmessage([[
|
checkmessage([[
|
||||||
@@ -276,22 +156,6 @@ do -- named objects (field '__name')
|
|||||||
checkmessage("return {} < XX", "table with My Type")
|
checkmessage("return {} < XX", "table with My Type")
|
||||||
checkmessage("return XX < io.stdin", "My Type with FILE*")
|
checkmessage("return XX < io.stdin", "My Type with FILE*")
|
||||||
_G.XX = nil
|
_G.XX = nil
|
||||||
|
|
||||||
if T then -- extra tests for 'luaL_tolstring'
|
|
||||||
-- bug in 5.4.3; 'luaL_tolstring' with negative indices
|
|
||||||
local x = setmetatable({}, {__name="TABLE"})
|
|
||||||
assert(T.testC("Ltolstring -1; return 1", x) == tostring(x))
|
|
||||||
|
|
||||||
local a, b = T.testC("pushint 10; Ltolstring -2; return 2", x)
|
|
||||||
assert(a == 10 and b == tostring(x))
|
|
||||||
|
|
||||||
setmetatable(x, {__tostring=function (o)
|
|
||||||
assert(o == x)
|
|
||||||
return "ABC"
|
|
||||||
end})
|
|
||||||
local a, b, c = T.testC("pushint 10; Ltolstring -2; return 3", x)
|
|
||||||
assert(a == x and b == 10 and c == "ABC")
|
|
||||||
end
|
|
||||||
end
|
end
|
||||||
|
|
||||||
-- global functions
|
-- global functions
|
||||||
@@ -303,29 +167,28 @@ do
|
|||||||
local f = function (a) return a + 1 end
|
local f = function (a) return a + 1 end
|
||||||
f = assert(load(string.dump(f, true)))
|
f = assert(load(string.dump(f, true)))
|
||||||
assert(f(3) == 4)
|
assert(f(3) == 4)
|
||||||
checkerr("^%?:%?:", f, {})
|
checkerr("^%?:%-1:", f, {})
|
||||||
|
|
||||||
-- code with a move to a local var ('OP_MOV A B' with A<B)
|
-- code with a move to a local var ('OP_MOV A B' with A<B)
|
||||||
f = function () local a; a = {}; return a + 2 end
|
f = function () local a; a = {}; return a + 2 end
|
||||||
-- no debug info (so that 'a' is unknown)
|
-- no debug info (so that 'a' is unknown)
|
||||||
f = assert(load(string.dump(f, true)))
|
f = assert(load(string.dump(f, true)))
|
||||||
-- symbolic execution should not get lost
|
-- symbolic execution should not get lost
|
||||||
checkerr("^%?:%?:.*table value", f)
|
checkerr("^%?:%-1:.*table value", f)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
||||||
-- tests for field accesses after RK limit
|
-- tests for field accesses after RK limit
|
||||||
local t = {}
|
local t = {}
|
||||||
for i = 1, 1000 do
|
for i = 1, 1000 do
|
||||||
t[i] = "aaa = x" .. i
|
t[i] = "a = x" .. i
|
||||||
end
|
end
|
||||||
local s = table.concat(t, "; ")
|
local s = table.concat(t, "; ")
|
||||||
t = nil
|
t = nil
|
||||||
checkmessage(s.."; aaa = bbb + 1", "global 'bbb'")
|
checkmessage(s.."; a = bbb + 1", "global 'bbb'")
|
||||||
checkmessage("local _ENV=_ENV;"..s.."; aaa = bbb + 1", "global 'bbb'")
|
checkmessage("local _ENV=_ENV;"..s.."; a = bbb + 1", "global 'bbb'")
|
||||||
checkmessage(s.."; local t = {}; aaa = t.bbb + 1", "field 'bbb'")
|
checkmessage(s.."; local t = {}; a = t.bbb + 1", "field 'bbb'")
|
||||||
-- cannot use 'self' opcode
|
checkmessage(s.."; local t = {}; t:bbb()", "method 'bbb'")
|
||||||
checkmessage(s.."; local t = {}; t:bbb()", "field 'bbb'")
|
|
||||||
|
|
||||||
checkmessage([[aaa=9
|
checkmessage([[aaa=9
|
||||||
repeat until 3==3
|
repeat until 3==3
|
||||||
@@ -354,7 +217,7 @@ end]], "global 'insert'")
|
|||||||
|
|
||||||
checkmessage([[ -- tail call
|
checkmessage([[ -- tail call
|
||||||
return math.sin("a")
|
return math.sin("a")
|
||||||
]], "sin")
|
]], "'sin'")
|
||||||
|
|
||||||
checkmessage([[collectgarbage("nooption")]], "invalid option")
|
checkmessage([[collectgarbage("nooption")]], "invalid option")
|
||||||
|
|
||||||
@@ -373,17 +236,14 @@ main()
|
|||||||
]], "global 'NoSuchName'")
|
]], "global 'NoSuchName'")
|
||||||
print'+'
|
print'+'
|
||||||
|
|
||||||
aaa = {}; setmetatable(aaa, {__index = string})
|
a = {}; setmetatable(a, {__index = string})
|
||||||
checkmessage("aaa:sub()", "bad self")
|
checkmessage("a:sub()", "bad self")
|
||||||
checkmessage("string.sub('a', {})", "#2")
|
checkmessage("string.sub('a', {})", "#2")
|
||||||
checkmessage("('a'):sub{}", "#1")
|
checkmessage("('a'):sub{}", "#1")
|
||||||
|
|
||||||
checkmessage("table.sort({1,2,3}, table.sort)", "'table.sort'")
|
checkmessage("table.sort({1,2,3}, table.sort)", "'table.sort'")
|
||||||
checkmessage("string.gsub('s', 's', setmetatable)", "'setmetatable'")
|
checkmessage("string.gsub('s', 's', setmetatable)", "'setmetatable'")
|
||||||
|
|
||||||
_G.aaa = nil
|
|
||||||
|
|
||||||
|
|
||||||
-- tests for errors in coroutines
|
-- tests for errors in coroutines
|
||||||
|
|
||||||
local function f (n)
|
local function f (n)
|
||||||
@@ -401,7 +261,7 @@ checkerr("yield across", f)
|
|||||||
|
|
||||||
-- testing size of 'source' info; size of buffer for that info is
|
-- testing size of 'source' info; size of buffer for that info is
|
||||||
-- LUA_IDSIZE, declared as 60 in luaconf. Get one position for '\0'.
|
-- LUA_IDSIZE, declared as 60 in luaconf. Get one position for '\0'.
|
||||||
local idsize = 60 - 1
|
idsize = 60 - 1
|
||||||
local function checksize (source)
|
local function checksize (source)
|
||||||
-- syntax error
|
-- syntax error
|
||||||
local _, msg = load("x", source)
|
local _, msg = load("x", source)
|
||||||
@@ -418,38 +278,38 @@ end
|
|||||||
|
|
||||||
-- testing line error
|
-- testing line error
|
||||||
|
|
||||||
local function lineerror (s, l, w)
|
local function lineerror (s, l)
|
||||||
local err,msg = pcall(load(s))
|
local err,msg = pcall(load(s))
|
||||||
local line = tonumber(string.match(msg, ":(%d+):"))
|
local line = string.match(msg, ":(%d+):")
|
||||||
assert((line == l or (not line and not l)) and string.find(msg, w))
|
assert((line and line+0) == l)
|
||||||
end
|
end
|
||||||
|
|
||||||
lineerror("local a\n for i=1,'a' do \n print(i) \n end", 2, "limit")
|
lineerror("local a\n for i=1,'a' do \n print(i) \n end", 2)
|
||||||
lineerror("\n local a \n for k,v in 3 \n do \n print(k) \n end", 3, "to call")
|
lineerror("\n local a \n for k,v in 3 \n do \n print(k) \n end", 3)
|
||||||
lineerror("\n\n for k,v in \n 3 \n do \n print(k) \n end", 4, "to call")
|
lineerror("\n\n for k,v in \n 3 \n do \n print(k) \n end", 4)
|
||||||
lineerror("function a.x.y ()\na=a+1\nend", 1, "index")
|
lineerror("function a.x.y ()\na=a+1\nend", 1)
|
||||||
|
|
||||||
lineerror("a = \na\n+\n{}", 3, "arithmetic")
|
lineerror("a = \na\n+\n{}", 3)
|
||||||
lineerror("a = \n3\n+\n(\n4\n/\nprint)", 6, "arithmetic")
|
lineerror("a = \n3\n+\n(\n4\n/\nprint)", 6)
|
||||||
lineerror("a = \nprint\n+\n(\n4\n/\n7)", 3, "arithmetic")
|
lineerror("a = \nprint\n+\n(\n4\n/\n7)", 3)
|
||||||
|
|
||||||
lineerror("a\n=\n-\n\nprint\n;", 3, "arithmetic")
|
lineerror("a\n=\n-\n\nprint\n;", 3)
|
||||||
|
|
||||||
lineerror([[
|
lineerror([[
|
||||||
a
|
a
|
||||||
( -- <<
|
(
|
||||||
23)
|
23)
|
||||||
]], 2, "call")
|
]], 1)
|
||||||
|
|
||||||
lineerror([[
|
lineerror([[
|
||||||
local a = {x = 13}
|
local a = {x = 13}
|
||||||
a
|
a
|
||||||
.
|
.
|
||||||
x
|
x
|
||||||
( -- <<
|
(
|
||||||
23
|
23
|
||||||
)
|
)
|
||||||
]], 5, "call")
|
]], 2)
|
||||||
|
|
||||||
lineerror([[
|
lineerror([[
|
||||||
local a = {x = 13}
|
local a = {x = 13}
|
||||||
@@ -459,115 +319,43 @@ x
|
|||||||
(
|
(
|
||||||
23 + a
|
23 + a
|
||||||
)
|
)
|
||||||
]], 6, "arithmetic")
|
]], 6)
|
||||||
|
|
||||||
local p = [[
|
local p = [[
|
||||||
function g() f() end
|
function g() f() end
|
||||||
function f(x) error('a', XX) end
|
function f(x) error('a', X) end
|
||||||
g()
|
g()
|
||||||
]]
|
]]
|
||||||
XX=3;lineerror((p), 3, "a")
|
X=3;lineerror((p), 3)
|
||||||
XX=0;lineerror((p), false, "a")
|
X=0;lineerror((p), nil)
|
||||||
XX=1;lineerror((p), 2, "a")
|
X=1;lineerror((p), 2)
|
||||||
XX=2;lineerror((p), 1, "a")
|
X=2;lineerror((p), 1)
|
||||||
_G.XX, _G.g, _G.f = nil
|
|
||||||
|
|
||||||
|
|
||||||
lineerror([[
|
|
||||||
local b = false
|
|
||||||
if not b then
|
|
||||||
error 'test'
|
|
||||||
end]], 3, "test")
|
|
||||||
|
|
||||||
lineerror([[
|
|
||||||
local b = false
|
|
||||||
if not b then
|
|
||||||
if not b then
|
|
||||||
if not b then
|
|
||||||
error 'test'
|
|
||||||
end
|
|
||||||
end
|
|
||||||
end]], 5, "test")
|
|
||||||
|
|
||||||
lineerror([[
|
|
||||||
_ENV = 1
|
|
||||||
global function foo ()
|
|
||||||
local a = 10
|
|
||||||
return a
|
|
||||||
end
|
|
||||||
]], 2, "index")
|
|
||||||
|
|
||||||
|
|
||||||
-- bug in 5.4.0
|
|
||||||
lineerror([[
|
|
||||||
local a = 0
|
|
||||||
local b = 1
|
|
||||||
local c = b % a
|
|
||||||
]], 3, "perform")
|
|
||||||
|
|
||||||
do
|
|
||||||
-- Force a negative estimate for base line. Error in instruction 2
|
|
||||||
-- (after VARARGPREP, GETGLOBAL), with first absolute line information
|
|
||||||
-- (forced by too many lines) in instruction 0.
|
|
||||||
local s = string.format("%s return __A.x", string.rep("\n", 300))
|
|
||||||
lineerror(s, 301, "index")
|
|
||||||
end
|
|
||||||
|
|
||||||
|
|
||||||
local function stxlineerror (s, l, w)
|
|
||||||
local err,msg = load(s)
|
|
||||||
local line = tonumber(string.match(msg, ":(%d+):"))
|
|
||||||
assert((line == l or (not line and not l)) and string.find(msg, w, 1, true))
|
|
||||||
end
|
|
||||||
|
|
||||||
stxlineerror([[
|
|
||||||
::L1::
|
|
||||||
::L1::
|
|
||||||
]], 2, "already defined")
|
|
||||||
|
|
||||||
stxlineerror([[
|
|
||||||
global none
|
|
||||||
local x = b
|
|
||||||
]], 2, "not declared")
|
|
||||||
|
|
||||||
stxlineerror([[
|
|
||||||
local <close> a, b
|
|
||||||
]], 1, "multiple")
|
|
||||||
|
|
||||||
if not _soft then
|
if not _soft then
|
||||||
-- several tests that exhaust the Lua stack
|
-- several tests that exaust the Lua stack
|
||||||
collectgarbage()
|
collectgarbage()
|
||||||
print"testing stack overflow"
|
print"testing stack overflow"
|
||||||
local C = 0
|
C = 0
|
||||||
-- get line where stack overflow will happen
|
local l = debug.getinfo(1, "l").currentline; function y () C=C+1; y() end
|
||||||
local l = debug.getinfo(1, "l").currentline + 1
|
|
||||||
local function auxy () C=C+1; auxy() end -- produce a stack overflow
|
|
||||||
function YY ()
|
|
||||||
collectgarbage("stop") -- avoid running finalizers without stack space
|
|
||||||
auxy()
|
|
||||||
collectgarbage("restart")
|
|
||||||
end
|
|
||||||
|
|
||||||
local function checkstackmessage (m)
|
local function checkstackmessage (m)
|
||||||
print("(expected stack overflow after " .. C .. " calls)")
|
return (string.find(m, "^.-:%d+: stack overflow"))
|
||||||
C = 0 -- prepare next count
|
|
||||||
return (string.find(m, "stack overflow"))
|
|
||||||
end
|
end
|
||||||
-- repeated stack overflows (to check stack recovery)
|
-- repeated stack overflows (to check stack recovery)
|
||||||
assert(checkstackmessage(doit('YY()')))
|
assert(checkstackmessage(doit('y()')))
|
||||||
assert(checkstackmessage(doit('YY()')))
|
print('+')
|
||||||
assert(checkstackmessage(doit('YY()')))
|
assert(checkstackmessage(doit('y()')))
|
||||||
|
print('+')
|
||||||
_G.YY = nil
|
assert(checkstackmessage(doit('y()')))
|
||||||
|
print('+')
|
||||||
|
|
||||||
|
|
||||||
-- error lines in stack overflow
|
-- error lines in stack overflow
|
||||||
|
C = 0
|
||||||
local l1
|
local l1
|
||||||
local function g(x)
|
local function g(x)
|
||||||
l1 = debug.getinfo(x, "l").currentline + 2
|
l1 = debug.getinfo(x, "l").currentline; y()
|
||||||
collectgarbage("stop") -- avoid running finalizers without stack space
|
|
||||||
auxy()
|
|
||||||
collectgarbage("restart")
|
|
||||||
end
|
end
|
||||||
local _, stackmsg = xpcall(g, debug.traceback, 1)
|
local _, stackmsg = xpcall(g, debug.traceback, 1)
|
||||||
print('+')
|
print('+')
|
||||||
@@ -586,7 +374,7 @@ if not _soft then
|
|||||||
|
|
||||||
-- error in error handling
|
-- error in error handling
|
||||||
local res, msg = xpcall(error, error)
|
local res, msg = xpcall(error, error)
|
||||||
assert(not res and msg == 'error in error handling')
|
assert(not res and type(msg) == 'string')
|
||||||
print('+')
|
print('+')
|
||||||
|
|
||||||
local function f (x)
|
local function f (x)
|
||||||
@@ -617,27 +405,6 @@ if not _soft then
|
|||||||
end
|
end
|
||||||
|
|
||||||
|
|
||||||
do -- errors in error handle that not necessarily go forever
|
|
||||||
local function err (n) -- function to be used as message handler
|
|
||||||
-- generate an error unless n is zero, so that there is a limited
|
|
||||||
-- loop of errors
|
|
||||||
if type(n) ~= "number" then -- some other error?
|
|
||||||
return n -- report it
|
|
||||||
elseif n == 0 then
|
|
||||||
return "END" -- that will be the final message
|
|
||||||
else error(n - 1) -- does the loop
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
local res, msg = xpcall(error, err, 170)
|
|
||||||
assert(not res and msg == "END")
|
|
||||||
|
|
||||||
-- too many levels
|
|
||||||
local res, msg = xpcall(error, err, 300)
|
|
||||||
assert(not res and msg == "C stack overflow")
|
|
||||||
end
|
|
||||||
|
|
||||||
|
|
||||||
do
|
do
|
||||||
-- non string messages
|
-- non string messages
|
||||||
local t = {}
|
local t = {}
|
||||||
@@ -645,7 +412,7 @@ do
|
|||||||
assert(not res and msg == t)
|
assert(not res and msg == t)
|
||||||
|
|
||||||
res, msg = pcall(function () error(nil) end)
|
res, msg = pcall(function () error(nil) end)
|
||||||
assert(not res and msg == "<no error object>")
|
assert(not res and msg == nil)
|
||||||
|
|
||||||
local function f() error{msg='x'} end
|
local function f() error{msg='x'} end
|
||||||
res, msg = xpcall(f, function (r) return {msg=r.msg..'y'} end)
|
res, msg = xpcall(f, function (r) return {msg=r.msg..'y'} end)
|
||||||
@@ -665,7 +432,7 @@ do
|
|||||||
assert(not res and msg == t)
|
assert(not res and msg == t)
|
||||||
|
|
||||||
res, msg = pcall(assert, nil, nil)
|
res, msg = pcall(assert, nil, nil)
|
||||||
assert(not res and type(msg) == "string")
|
assert(not res and msg == nil)
|
||||||
|
|
||||||
-- 'assert' without arguments
|
-- 'assert' without arguments
|
||||||
res, msg = pcall(assert)
|
res, msg = pcall(assert)
|
||||||
@@ -673,7 +440,7 @@ do
|
|||||||
end
|
end
|
||||||
|
|
||||||
-- xpcall with arguments
|
-- xpcall with arguments
|
||||||
local a, b, c = xpcall(string.find, error, "alo", "al")
|
a, b, c = xpcall(string.find, error, "alo", "al")
|
||||||
assert(a and b == 1 and c == 2)
|
assert(a and b == 1 and c == 2)
|
||||||
a, b, c = xpcall(string.find, function (x) return {} end, true, "al")
|
a, b, c = xpcall(string.find, function (x) return {} end, true, "al")
|
||||||
assert(not a and type(b) == "table" and c == nil)
|
assert(not a and type(b) == "table" and c == nil)
|
||||||
@@ -693,12 +460,11 @@ checksyntax("a\1a = 1", "", "<\\1>", 1)
|
|||||||
-- test 255 as first char in a chunk
|
-- test 255 as first char in a chunk
|
||||||
checksyntax("\255a = 1", "", "<\\255>", 1)
|
checksyntax("\255a = 1", "", "<\\255>", 1)
|
||||||
|
|
||||||
doit('I = load("a=9+"); aaa=3')
|
doit('I = load("a=9+"); a=3')
|
||||||
assert(_G.aaa==3 and not _G.I)
|
assert(a==3 and I == nil)
|
||||||
_G.I,_G.aaa = nil
|
|
||||||
print('+')
|
print('+')
|
||||||
|
|
||||||
local lim = 1000
|
lim = 1000
|
||||||
if _soft then lim = 100 end
|
if _soft then lim = 100 end
|
||||||
for i=1,lim do
|
for i=1,lim do
|
||||||
doit('a = ')
|
doit('a = ')
|
||||||
@@ -708,34 +474,26 @@ end
|
|||||||
|
|
||||||
-- testing syntax limits
|
-- testing syntax limits
|
||||||
|
|
||||||
local function testrep (init, rep, close, repc, finalresult)
|
local maxClevel = 200 -- LUAI_MAXCCALLS (in llimits.h)
|
||||||
local function gencode (n)
|
|
||||||
return init .. string.rep(rep, n) .. close .. string.rep(repc, n)
|
local function testrep (init, rep, close, repc)
|
||||||
end
|
local s = init .. string.rep(rep, maxClevel - 10) .. close ..
|
||||||
local res, msg = load(gencode(100)) -- 100 levels is OK
|
string.rep(repc, maxClevel - 10)
|
||||||
assert(res)
|
assert(load(s)) -- 190 levels is OK
|
||||||
if (finalresult) then
|
s = init .. string.rep(rep, maxClevel + 1)
|
||||||
assert(res() == finalresult)
|
checkmessage(s, "too many C levels")
|
||||||
end
|
|
||||||
local res, msg = load(gencode(500)) -- 500 levels not ok
|
|
||||||
assert(not res and (string.find(msg, "too many") or
|
|
||||||
string.find(msg, "overflow")))
|
|
||||||
end
|
end
|
||||||
|
|
||||||
testrep("local a", ",a", ";", "") -- local variables
|
|
||||||
testrep("local a", ",a", "= 1", ",1") -- local variables initialized
|
|
||||||
testrep("local a", ",a", "= f()", "") -- local variables initialized
|
|
||||||
testrep("local a; a", ",a", "= 1", ",1") -- multiple assignment
|
testrep("local a; a", ",a", "= 1", ",1") -- multiple assignment
|
||||||
testrep("local a; a=", "{", "0", "}") -- constructors
|
testrep("local a; a=", "{", "0", "}")
|
||||||
testrep("return ", "(", "2", ")", 2) -- parentheses
|
testrep("local a; a=", "(", "2", ")")
|
||||||
-- nested calls (a(a(a(a(...)))))
|
testrep("local a; ", "a(", "2", ")")
|
||||||
testrep("local function a (x) return x end; return ", "a(", "2.2", ")", 2.2)
|
|
||||||
testrep("", "do ", "", " end")
|
testrep("", "do ", "", " end")
|
||||||
testrep("", "while a do ", "", " end")
|
testrep("", "while a do ", "", " end")
|
||||||
testrep("local a; ", "if a then else ", "", " end")
|
testrep("local a; ", "if a then else ", "", " end")
|
||||||
testrep("", "function foo () ", "", " end")
|
testrep("", "function foo () ", "", " end")
|
||||||
testrep("local a = ''; return ", "a..", "'a'", "", "a")
|
testrep("local a; a=", "a..", "a", "")
|
||||||
testrep("local a = 1; return ", "a^", "a", "", 1)
|
testrep("local a; a=", "a^", "a", "")
|
||||||
|
|
||||||
checkmessage("a = f(x" .. string.rep(",x", 260) .. ")", "too many registers")
|
checkmessage("a = f(x" .. string.rep(",x", 260) .. ")", "too many registers")
|
||||||
|
|
||||||
@@ -767,7 +525,7 @@ assert(c > 255 and string.find(b, "too many upvalues") and
|
|||||||
|
|
||||||
-- local variables
|
-- local variables
|
||||||
s = "\nfunction foo ()\n local "
|
s = "\nfunction foo ()\n local "
|
||||||
for j = 1,200 do
|
for j = 1,300 do
|
||||||
s = s.."a"..j..", "
|
s = s.."a"..j..", "
|
||||||
end
|
end
|
||||||
s = s.."b\n"
|
s = s.."b\n"
|
||||||
|
|||||||
+83
-138
@@ -1,5 +1,5 @@
|
|||||||
-- $Id: testes/events.lua $
|
-- $Id: events.lua,v 1.45 2016/12/21 19:23:02 roberto Exp $
|
||||||
-- See Copyright Notice in file lua.h
|
-- See Copyright Notice in file all.lua
|
||||||
|
|
||||||
print('testing metatables')
|
print('testing metatables')
|
||||||
|
|
||||||
@@ -15,7 +15,7 @@ X = X+10
|
|||||||
assert(X == 30 and _G.X == 20)
|
assert(X == 30 and _G.X == 20)
|
||||||
B = false
|
B = false
|
||||||
assert(B == false)
|
assert(B == false)
|
||||||
_ENV["B"] = undef
|
B = nil
|
||||||
assert(B == 30)
|
assert(B == 30)
|
||||||
|
|
||||||
assert(getmetatable{} == nil)
|
assert(getmetatable{} == nil)
|
||||||
@@ -70,12 +70,8 @@ end
|
|||||||
local c = {}
|
local c = {}
|
||||||
a = setmetatable({}, t)
|
a = setmetatable({}, t)
|
||||||
t.__newindex = c
|
t.__newindex = c
|
||||||
t.__index = c
|
a[1] = 10; a[2] = 20; a[3] = 90
|
||||||
a[1] = 10; a[2] = 20; a[3] = 90;
|
assert(c[1] == 10 and c[2] == 20 and c[3] == 90)
|
||||||
for i = 4, 20 do a[i] = i * 10 end
|
|
||||||
assert(a[1] == 10 and a[2] == 20 and a[3] == 90)
|
|
||||||
for i = 4, 20 do assert(a[i] == i * 10) end
|
|
||||||
assert(next(a) == nil)
|
|
||||||
|
|
||||||
|
|
||||||
do
|
do
|
||||||
@@ -99,8 +95,7 @@ do -- newindex
|
|||||||
foi = false; a['a1']=0; assert(not foi)
|
foi = false; a['a1']=0; assert(not foi)
|
||||||
foi = false; a['a11']=0; assert(foi)
|
foi = false; a['a11']=0; assert(foi)
|
||||||
foi = false; a[11]=0; assert(foi)
|
foi = false; a[11]=0; assert(foi)
|
||||||
foi = false; a[1]=undef; assert(not foi)
|
foi = false; a[1]=nil; assert(not foi)
|
||||||
a[1] = undef
|
|
||||||
foi = false; a[1]=nil; assert(foi)
|
foi = false; a[1]=nil; assert(foi)
|
||||||
end
|
end
|
||||||
|
|
||||||
@@ -111,9 +106,9 @@ t.__call = f
|
|||||||
|
|
||||||
do
|
do
|
||||||
local x,y = a(table.unpack{'a', 1})
|
local x,y = a(table.unpack{'a', 1})
|
||||||
assert(x==a and y[1]=='a' and y[2]==1 and y[3]==undef)
|
assert(x==a and y[1]=='a' and y[2]==1 and y[3]==nil)
|
||||||
x,y = a()
|
x,y = a()
|
||||||
assert(x==a and y[1]==undef)
|
assert(x==a and y[1]==nil)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
||||||
@@ -138,55 +133,52 @@ t.__bxor = f("bxor")
|
|||||||
t.__shl = f("shl")
|
t.__shl = f("shl")
|
||||||
t.__shr = f("shr")
|
t.__shr = f("shr")
|
||||||
t.__bnot = f("bnot")
|
t.__bnot = f("bnot")
|
||||||
t.__lt = f("lt")
|
|
||||||
t.__le = f("le")
|
|
||||||
|
|
||||||
|
assert(b+5 == b)
|
||||||
local function checkcap (t)
|
assert(cap[0] == "add" and cap[1] == b and cap[2] == 5 and cap[3]==nil)
|
||||||
assert(#cap + 1 == #t)
|
assert(b+'5' == b)
|
||||||
for i = 1, #t do
|
assert(cap[0] == "add" and cap[1] == b and cap[2] == '5' and cap[3]==nil)
|
||||||
assert(cap[i - 1] == t[i])
|
assert(5+b == 5)
|
||||||
assert(math.type(cap[i - 1]) == math.type(t[i]))
|
assert(cap[0] == "add" and cap[1] == 5 and cap[2] == b and cap[3]==nil)
|
||||||
end
|
assert('5'+b == '5')
|
||||||
end
|
assert(cap[0] == "add" and cap[1] == '5' and cap[2] == b and cap[3]==nil)
|
||||||
|
b=b-3; assert(getmetatable(b) == t)
|
||||||
-- Some tests are done inside small anonymous functions to ensure
|
assert(5-a == 5)
|
||||||
-- that constants go to constant table even in debug compilation,
|
assert(cap[0] == "sub" and cap[1] == 5 and cap[2] == a and cap[3]==nil)
|
||||||
-- when the constant table is very small.
|
assert('5'-a == '5')
|
||||||
assert(b+5 == b); checkcap{"add", b, 5}
|
assert(cap[0] == "sub" and cap[1] == '5' and cap[2] == a and cap[3]==nil)
|
||||||
assert(5.2 + b == 5.2); checkcap{"add", 5.2, b}
|
assert(a*a == a)
|
||||||
assert(b+'5' == b); checkcap{"add", b, '5'}
|
assert(cap[0] == "mul" and cap[1] == a and cap[2] == a and cap[3]==nil)
|
||||||
assert(5+b == 5); checkcap{"add", 5, b}
|
assert(a/0 == a)
|
||||||
assert('5'+b == '5'); checkcap{"add", '5', b}
|
assert(cap[0] == "div" and cap[1] == a and cap[2] == 0 and cap[3]==nil)
|
||||||
b=b-3; assert(getmetatable(b) == t); checkcap{"sub", b, 3}
|
assert(a%2 == a)
|
||||||
assert(5-a == 5); checkcap{"sub", 5, a}
|
assert(cap[0] == "mod" and cap[1] == a and cap[2] == 2 and cap[3]==nil)
|
||||||
assert('5'-a == '5'); checkcap{"sub", '5', a}
|
assert(a // (1/0) == a)
|
||||||
assert(a*a == a); checkcap{"mul", a, a}
|
assert(cap[0] == "idiv" and cap[1] == a and cap[2] == 1/0 and cap[3]==nil)
|
||||||
assert(a/0 == a); checkcap{"div", a, 0}
|
assert(a & "hi" == a)
|
||||||
assert(a/0.0 == a); checkcap{"div", a, 0.0}
|
assert(cap[0] == "band" and cap[1] == a and cap[2] == "hi" and cap[3]==nil)
|
||||||
assert(a%2 == a); checkcap{"mod", a, 2}
|
assert(a | "hi" == a)
|
||||||
assert(a // (1/0) == a); checkcap{"idiv", a, 1/0}
|
assert(cap[0] == "bor" and cap[1] == a and cap[2] == "hi" and cap[3]==nil)
|
||||||
;(function () assert(a & "hi" == a) end)(); checkcap{"band", a, "hi"}
|
assert("hi" ~ a == "hi")
|
||||||
;(function () assert(10 & a == 10) end)(); checkcap{"band", 10, a}
|
assert(cap[0] == "bxor" and cap[1] == "hi" and cap[2] == a and cap[3]==nil)
|
||||||
;(function () assert(a | 10 == a) end)(); checkcap{"bor", a, 10}
|
assert(-a == a)
|
||||||
assert(a | "hi" == a); checkcap{"bor", a, "hi"}
|
assert(cap[0] == "unm" and cap[1] == a)
|
||||||
assert("hi" ~ a == "hi"); checkcap{"bxor", "hi", a}
|
assert(a^4 == a)
|
||||||
;(function () assert(10 ~ a == 10) end)(); checkcap{"bxor", 10, a}
|
assert(cap[0] == "pow" and cap[1] == a and cap[2] == 4 and cap[3]==nil)
|
||||||
assert(-a == a); checkcap{"unm", a, a}
|
assert(a^'4' == a)
|
||||||
assert(a^4.0 == a); checkcap{"pow", a, 4.0}
|
assert(cap[0] == "pow" and cap[1] == a and cap[2] == '4' and cap[3]==nil)
|
||||||
assert(a^'4' == a); checkcap{"pow", a, '4'}
|
assert(4^a == 4)
|
||||||
assert(4^a == 4); checkcap{"pow", 4, a}
|
assert(cap[0] == "pow" and cap[1] == 4 and cap[2] == a and cap[3]==nil)
|
||||||
assert('4'^a == '4'); checkcap{"pow", '4', a}
|
assert('4'^a == '4')
|
||||||
assert(#a == a); checkcap{"len", a, a}
|
assert(cap[0] == "pow" and cap[1] == '4' and cap[2] == a and cap[3]==nil)
|
||||||
assert(~a == a); checkcap{"bnot", a, a}
|
assert(#a == a)
|
||||||
assert(a << 3 == a); checkcap{"shl", a, 3}
|
assert(cap[0] == "len" and cap[1] == a)
|
||||||
assert(1.5 >> a == 1.5); checkcap{"shr", 1.5, a}
|
assert(~a == a)
|
||||||
|
assert(cap[0] == "bnot" and cap[1] == a)
|
||||||
-- for comparison operators, all results are true
|
assert(a << 3 == a)
|
||||||
assert(5.0 > a); checkcap{"lt", a, 5.0}
|
assert(cap[0] == "shl" and cap[1] == a and cap[2] == 3)
|
||||||
assert(a >= 10); checkcap{"le", 10, a}
|
assert(1.5 >> a == 1.5)
|
||||||
assert(a <= -10.0); checkcap{"le", a, -10.0}
|
assert(cap[0] == "shr" and cap[1] == 1.5 and cap[2] == a)
|
||||||
assert(a < -10); checkcap{"lt", a, -10}
|
|
||||||
|
|
||||||
|
|
||||||
-- test for rawlen
|
-- test for rawlen
|
||||||
@@ -210,23 +202,9 @@ t.__lt = function (a,b,c)
|
|||||||
return a<b, "dummy"
|
return a<b, "dummy"
|
||||||
end
|
end
|
||||||
|
|
||||||
t.__le = function (a,b,c)
|
|
||||||
assert(c == nil)
|
|
||||||
if type(a) == 'table' then a = a.x end
|
|
||||||
if type(b) == 'table' then b = b.x end
|
|
||||||
return a<=b, "dummy"
|
|
||||||
end
|
|
||||||
|
|
||||||
t.__eq = function (a,b,c)
|
|
||||||
assert(c == nil)
|
|
||||||
if type(a) == 'table' then a = a.x end
|
|
||||||
if type(b) == 'table' then b = b.x end
|
|
||||||
return a == b, "dummy"
|
|
||||||
end
|
|
||||||
|
|
||||||
function Op(x) return setmetatable({x=x}, t) end
|
function Op(x) return setmetatable({x=x}, t) end
|
||||||
|
|
||||||
local function test (a, b, c)
|
local function test ()
|
||||||
assert(not(Op(1)<Op(1)) and (Op(1)<Op(2)) and not(Op(2)<Op(1)))
|
assert(not(Op(1)<Op(1)) and (Op(1)<Op(2)) and not(Op(2)<Op(1)))
|
||||||
assert(not(1 < Op(1)) and (Op(1) < 2) and not(2 < Op(1)))
|
assert(not(1 < Op(1)) and (Op(1) < 2) and not(2 < Op(1)))
|
||||||
assert(not(Op('a')<Op('a')) and (Op('a')<Op('b')) and not(Op('b')<Op('a')))
|
assert(not(Op('a')<Op('a')) and (Op('a')<Op('b')) and not(Op('b')<Op('a')))
|
||||||
@@ -239,23 +217,19 @@ local function test (a, b, c)
|
|||||||
assert((1 >= Op(1)) and not(1 >= Op(2)) and (Op(2) >= 1))
|
assert((1 >= Op(1)) and not(1 >= Op(2)) and (Op(2) >= 1))
|
||||||
assert((Op('a')>=Op('a')) and not(Op('a')>=Op('b')) and (Op('b')>=Op('a')))
|
assert((Op('a')>=Op('a')) and not(Op('a')>=Op('b')) and (Op('b')>=Op('a')))
|
||||||
assert(('a' >= Op('a')) and not(Op('a') >= 'b') and (Op('b') >= Op('a')))
|
assert(('a' >= Op('a')) and not(Op('a') >= 'b') and (Op('b') >= Op('a')))
|
||||||
assert(Op(1) == Op(1) and Op(1) ~= Op(2))
|
|
||||||
assert(Op('a') == Op('a') and Op('a') ~= Op('b'))
|
|
||||||
assert(a == a and a ~= b)
|
|
||||||
assert(Op(3) == c)
|
|
||||||
end
|
end
|
||||||
|
|
||||||
test(Op(1), Op(2), Op(3))
|
test()
|
||||||
|
|
||||||
|
t.__le = function (a,b,c)
|
||||||
do -- test nil as false
|
assert(c == nil)
|
||||||
local x = setmetatable({12}, {__eq= function (a,b)
|
if type(a) == 'table' then a = a.x end
|
||||||
return a[1] == b[1] or nil
|
if type(b) == 'table' then b = b.x end
|
||||||
end})
|
return a<=b, "dummy"
|
||||||
assert(not (x == {20}))
|
|
||||||
assert(x == {12})
|
|
||||||
end
|
end
|
||||||
|
|
||||||
|
test() -- retest comparisons, now using both `lt' and `le'
|
||||||
|
|
||||||
|
|
||||||
-- test `partial order'
|
-- test `partial order'
|
||||||
|
|
||||||
@@ -272,11 +246,19 @@ end
|
|||||||
t.__lt = function (a,b)
|
t.__lt = function (a,b)
|
||||||
for k in pairs(a) do
|
for k in pairs(a) do
|
||||||
if not b[k] then return false end
|
if not b[k] then return false end
|
||||||
b[k] = undef
|
b[k] = nil
|
||||||
end
|
end
|
||||||
return next(b) ~= nil
|
return next(b) ~= nil
|
||||||
end
|
end
|
||||||
|
|
||||||
|
t.__le = nil
|
||||||
|
|
||||||
|
assert(Set{1,2,3} < Set{1,2,3,4})
|
||||||
|
assert(not(Set{1,2,3,4} < Set{1,2,3,4}))
|
||||||
|
assert((Set{1,2,3,4} <= Set{1,2,3,4}))
|
||||||
|
assert((Set{1,2,3,4} >= Set{1,2,3,4}))
|
||||||
|
assert((Set{1,3} <= Set{3,5})) -- wrong!! model needs a `le' method ;-)
|
||||||
|
|
||||||
t.__le = function (a,b)
|
t.__le = function (a,b)
|
||||||
for k in pairs(a) do
|
for k in pairs(a) do
|
||||||
if not b[k] then return false end
|
if not b[k] then return false end
|
||||||
@@ -284,19 +266,14 @@ t.__le = function (a,b)
|
|||||||
return true
|
return true
|
||||||
end
|
end
|
||||||
|
|
||||||
assert(Set{1,2,3} < Set{1,2,3,4})
|
assert(not (Set{1,3} <= Set{3,5})) -- now its OK!
|
||||||
assert(not(Set{1,2,3,4} < Set{1,2,3,4}))
|
|
||||||
assert((Set{1,2,3,4} <= Set{1,2,3,4}))
|
|
||||||
assert((Set{1,2,3,4} >= Set{1,2,3,4}))
|
|
||||||
assert(not (Set{1,3} <= Set{3,5}))
|
|
||||||
assert(not(Set{1,3} <= Set{3,5}))
|
assert(not(Set{1,3} <= Set{3,5}))
|
||||||
assert(not(Set{1,3} >= Set{3,5}))
|
assert(not(Set{1,3} >= Set{3,5}))
|
||||||
|
|
||||||
|
|
||||||
t.__eq = function (a,b)
|
t.__eq = function (a,b)
|
||||||
for k in pairs(a) do
|
for k in pairs(a) do
|
||||||
if not b[k] then return false end
|
if not b[k] then return false end
|
||||||
b[k] = undef
|
b[k] = nil
|
||||||
end
|
end
|
||||||
return next(b) == nil
|
return next(b) == nil
|
||||||
end
|
end
|
||||||
@@ -311,27 +288,16 @@ assert(Set{1,3,5} ~= Set{3,5,1,6})
|
|||||||
|
|
||||||
-- '__eq' is not used for table accesses
|
-- '__eq' is not used for table accesses
|
||||||
t[Set{1,3,5}] = 1
|
t[Set{1,3,5}] = 1
|
||||||
assert(t[Set{1,3,5}] == undef)
|
assert(t[Set{1,3,5}] == nil)
|
||||||
|
|
||||||
|
|
||||||
do -- test invalidating flags
|
|
||||||
local mt = {__eq = true}
|
|
||||||
local a = setmetatable({10}, mt)
|
|
||||||
local b = setmetatable({10}, mt)
|
|
||||||
mt.__eq = nil
|
|
||||||
assert(a ~= b) -- no metamethod
|
|
||||||
mt.__eq = function (x,y) return x[1] == y[1] end
|
|
||||||
assert(a == b) -- must use metamethod now
|
|
||||||
end
|
|
||||||
|
|
||||||
|
|
||||||
if not T then
|
if not T then
|
||||||
(Message or print)('\n >>> testC not active: skipping tests for \z
|
(Message or print)('\n >>> testC not active: skipping tests for \z
|
||||||
userdata <<<\n')
|
userdata equality <<<\n')
|
||||||
else
|
else
|
||||||
local u1 = T.newuserdata(0, 1)
|
local u1 = T.newuserdata(0)
|
||||||
local u2 = T.newuserdata(0, 1)
|
local u2 = T.newuserdata(0)
|
||||||
local u3 = T.newuserdata(0, 1)
|
local u3 = T.newuserdata(0)
|
||||||
assert(u1 ~= u2 and u1 ~= u3)
|
assert(u1 ~= u2 and u1 ~= u3)
|
||||||
debug.setuservalue(u1, 1);
|
debug.setuservalue(u1, 1);
|
||||||
debug.setuservalue(u2, 2);
|
debug.setuservalue(u2, 2);
|
||||||
@@ -345,12 +311,6 @@ else
|
|||||||
assert(u1 == u3 and u3 == u1 and u1 ~= u2)
|
assert(u1 == u3 and u3 == u1 and u1 ~= u2)
|
||||||
assert(u2 == u1 and u2 == u3 and u3 == u2)
|
assert(u2 == u1 and u2 == u3 and u3 == u2)
|
||||||
assert(u2 ~= {}) -- different types cannot be equal
|
assert(u2 ~= {}) -- different types cannot be equal
|
||||||
assert(rawequal(u1, u1) and not rawequal(u1, u3))
|
|
||||||
|
|
||||||
local mirror = {}
|
|
||||||
debug.setmetatable(u3, {__index = mirror, __newindex = mirror})
|
|
||||||
for i = 1, 10 do u3[i] = i end
|
|
||||||
for i = 1, 10 do assert(u3[i] == i) end
|
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
||||||
@@ -379,17 +339,6 @@ x = 0 .."a".."b"..c..d.."e".."f".."g"
|
|||||||
assert(x.val == "0abcdefg")
|
assert(x.val == "0abcdefg")
|
||||||
|
|
||||||
|
|
||||||
do
|
|
||||||
-- bug since 5.4.1 (test needs T)
|
|
||||||
local mt = setmetatable({__newindex={}}, {__mode='v'})
|
|
||||||
local t = setmetatable({}, mt)
|
|
||||||
|
|
||||||
if T then T.allocfailnext() end
|
|
||||||
|
|
||||||
-- seg. fault
|
|
||||||
for i=1, 10 do t[i] = 1 end
|
|
||||||
end
|
|
||||||
|
|
||||||
-- concat metamethod x numbers (bug in 5.1.1)
|
-- concat metamethod x numbers (bug in 5.1.1)
|
||||||
c = {}
|
c = {}
|
||||||
local x
|
local x
|
||||||
@@ -407,7 +356,6 @@ t1 = {}; c = {}; setmetatable(c, t1)
|
|||||||
d = {}
|
d = {}
|
||||||
t1.__eq = function () return true end
|
t1.__eq = function () return true end
|
||||||
t1.__lt = function () return true end
|
t1.__lt = function () return true end
|
||||||
t1.__le = function () return false end
|
|
||||||
setmetatable(d, t1)
|
setmetatable(d, t1)
|
||||||
assert(c == d and c < d and not(d <= c))
|
assert(c == d and c < d and not(d <= c))
|
||||||
t2 = {}
|
t2 = {}
|
||||||
@@ -440,9 +388,6 @@ assert(i == 3 and x[1] == 3 and x[3] == 5)
|
|||||||
|
|
||||||
assert(_G.X == 20)
|
assert(_G.X == 20)
|
||||||
|
|
||||||
_G.X, _G.B = nil
|
|
||||||
|
|
||||||
|
|
||||||
print'+'
|
print'+'
|
||||||
|
|
||||||
local _g = _G
|
local _g = _G
|
||||||
@@ -476,10 +421,10 @@ assert(getmetatable(true) == nil)
|
|||||||
|
|
||||||
debug.setmetatable(nil, mt)
|
debug.setmetatable(nil, mt)
|
||||||
assert(getmetatable(nil) == mt)
|
assert(getmetatable(nil) == mt)
|
||||||
mt.__add = function (a,b) return (a or 1) + (b or 2) end
|
mt.__add = function (a,b) return (a or 0) + (b or 0) end
|
||||||
assert(10 + nil == 12)
|
assert(10 + nil == 10)
|
||||||
assert(nil + 23 == 24)
|
assert(nil + 23 == 23)
|
||||||
assert(nil + nil == 3)
|
assert(nil + nil == 0)
|
||||||
debug.setmetatable(nil, nil)
|
debug.setmetatable(nil, nil)
|
||||||
assert(getmetatable(nil) == nil)
|
assert(getmetatable(nil) == nil)
|
||||||
|
|
||||||
@@ -492,7 +437,7 @@ assert(not pcall(function (a,b) return a[b] end, a, 10))
|
|||||||
assert(not pcall(function (a,b,c) a[b] = c end, a, 10, true))
|
assert(not pcall(function (a,b,c) a[b] = c end, a, 10, true))
|
||||||
|
|
||||||
-- bug in 5.1
|
-- bug in 5.1
|
||||||
local T, K, V = nil
|
T, K, V = nil
|
||||||
grandparent = {}
|
grandparent = {}
|
||||||
grandparent.__newindex = function(t,k,v) T=t; K=k; V=v end
|
grandparent.__newindex = function(t,k,v) T=t; K=k; V=v end
|
||||||
|
|
||||||
|
|||||||
+68
-279
@@ -1,7 +1,5 @@
|
|||||||
-- $Id: testes/files.lua $
|
-- $Id: files.lua,v 1.95 2016/11/07 13:11:28 roberto Exp $
|
||||||
-- See Copyright Notice in file lua.h
|
-- See Copyright Notice in file all.lua
|
||||||
|
|
||||||
global <const> *
|
|
||||||
|
|
||||||
local debug = require "debug"
|
local debug = require "debug"
|
||||||
|
|
||||||
@@ -30,9 +28,6 @@ assert(not io.close(io.stdin) and
|
|||||||
not io.stdout:close() and
|
not io.stdout:close() and
|
||||||
not io.stderr:close())
|
not io.stderr:close())
|
||||||
|
|
||||||
-- cannot call close method without an argument (new in 5.3.5)
|
|
||||||
checkerr("got no value", io.stdin.close)
|
|
||||||
|
|
||||||
|
|
||||||
assert(type(io.input()) == "userdata" and io.type(io.output()) == "file")
|
assert(type(io.input()) == "userdata" and io.type(io.output()) == "file")
|
||||||
assert(type(io.stdin) == "userdata" and io.type(io.stderr) == "file")
|
assert(type(io.stdin) == "userdata" and io.type(io.stderr) == "file")
|
||||||
@@ -76,8 +71,6 @@ io.input(io.stdin); io.output(io.stdout);
|
|||||||
|
|
||||||
os.remove(file)
|
os.remove(file)
|
||||||
assert(not loadfile(file))
|
assert(not loadfile(file))
|
||||||
-- Lua code cannot use chunks with fixed buffers
|
|
||||||
checkerr("invalid mode", load, "", "", "B")
|
|
||||||
checkerr("", dofile, file)
|
checkerr("", dofile, file)
|
||||||
assert(not io.open(file))
|
assert(not io.open(file))
|
||||||
io.output(file)
|
io.output(file)
|
||||||
@@ -96,8 +89,8 @@ assert(io.output():seek("end") == string.len("alo joao"))
|
|||||||
|
|
||||||
assert(io.output():seek("set") == 0)
|
assert(io.output():seek("set") == 0)
|
||||||
|
|
||||||
assert(io.write('"alo"', "{a}\n", "second line\n", "third line \n"))
|
assert(io.write('"álo"', "{a}\n", "second line\n", "third line \n"))
|
||||||
assert(io.write('Xfourth_line'))
|
assert(io.write('çfourth_line'))
|
||||||
io.output(io.stdout)
|
io.output(io.stdout)
|
||||||
collectgarbage() -- file should be closed by GC
|
collectgarbage() -- file should be closed by GC
|
||||||
assert(io.input() == io.stdin and rawequal(io.output(), io.stdout))
|
assert(io.input() == io.stdin and rawequal(io.output(), io.stdout))
|
||||||
@@ -124,76 +117,27 @@ io.output(io.open(otherfile, "ab"))
|
|||||||
assert(io.write("\n\n\t\t ", 3450, "\n"));
|
assert(io.write("\n\n\t\t ", 3450, "\n"));
|
||||||
io.close()
|
io.close()
|
||||||
|
|
||||||
|
-- test writing/reading numbers
|
||||||
do
|
f = assert(io.open(file, "w"))
|
||||||
-- closing file by scope
|
f:write(maxint, '\n')
|
||||||
local F = nil
|
f:write(string.format("0X%x\n", maxint))
|
||||||
do
|
f:write("0xABCp-3", '\n')
|
||||||
local f <close> = assert(io.open(file, "w"))
|
f:write(0, '\n')
|
||||||
F = f
|
f:write(-maxint, '\n')
|
||||||
end
|
f:write(string.format("0x%X\n", -maxint))
|
||||||
assert(tostring(F) == "file (closed)")
|
f:write("-0xABCp-3", '\n')
|
||||||
end
|
assert(f:close())
|
||||||
|
f = assert(io.open(file, "r"))
|
||||||
|
assert(f:read("n") == maxint)
|
||||||
|
assert(f:read("n") == maxint)
|
||||||
|
assert(f:read("n") == 0xABCp-3)
|
||||||
|
assert(f:read("n") == 0)
|
||||||
|
assert(f:read("*n") == -maxint) -- test old format (with '*')
|
||||||
|
assert(f:read("n") == -maxint)
|
||||||
|
assert(f:read("*n") == -0xABCp-3) -- test old format (with '*')
|
||||||
|
assert(f:close())
|
||||||
assert(os.remove(file))
|
assert(os.remove(file))
|
||||||
|
|
||||||
|
|
||||||
do
|
|
||||||
-- test writing/reading numbers
|
|
||||||
local f <close> = assert(io.open(file, "w"))
|
|
||||||
f:write(maxint, '\n')
|
|
||||||
f:write(string.format("0X%x\n", maxint))
|
|
||||||
f:write("0xABCp-3", '\n')
|
|
||||||
f:write(0, '\n')
|
|
||||||
f:write(-maxint, '\n')
|
|
||||||
f:write(string.format("0x%X\n", -maxint))
|
|
||||||
f:write("-0xABCp-3", '\n')
|
|
||||||
assert(f:close())
|
|
||||||
local f <close> = assert(io.open(file, "r"))
|
|
||||||
assert(f:read("n") == maxint)
|
|
||||||
assert(f:read("n") == maxint)
|
|
||||||
assert(f:read("n") == 0xABCp-3)
|
|
||||||
assert(f:read("n") == 0)
|
|
||||||
assert(f:read("*n") == -maxint) -- test old format (with '*')
|
|
||||||
assert(f:read("n") == -maxint)
|
|
||||||
assert(f:read("*n") == -0xABCp-3) -- test old format (with '*')
|
|
||||||
end
|
|
||||||
assert(os.remove(file))
|
|
||||||
|
|
||||||
|
|
||||||
-- testing multiple arguments to io.read
|
|
||||||
do
|
|
||||||
local f <close> = assert(io.open(file, "w"))
|
|
||||||
f:write[[
|
|
||||||
a line
|
|
||||||
another line
|
|
||||||
1234
|
|
||||||
3.45
|
|
||||||
one
|
|
||||||
two
|
|
||||||
three
|
|
||||||
]]
|
|
||||||
local l1, l2, l3, l4, n1, n2, c, dummy
|
|
||||||
assert(f:close())
|
|
||||||
local f <close> = assert(io.open(file, "r"))
|
|
||||||
l1, l2, n1, n2, dummy = f:read("l", "L", "n", "n")
|
|
||||||
assert(l1 == "a line" and l2 == "another line\n" and
|
|
||||||
n1 == 1234 and n2 == 3.45 and dummy == nil)
|
|
||||||
assert(f:close())
|
|
||||||
local f <close> = assert(io.open(file, "r"))
|
|
||||||
l1, l2, n1, n2, c, l3, l4, dummy = f:read(7, "l", "n", "n", 1, "l", "l")
|
|
||||||
assert(l1 == "a line\n" and l2 == "another line" and c == '\n' and
|
|
||||||
n1 == 1234 and n2 == 3.45 and l3 == "one" and l4 == "two"
|
|
||||||
and dummy == nil)
|
|
||||||
assert(f:close())
|
|
||||||
local f <close> = assert(io.open(file, "r"))
|
|
||||||
-- second item failing
|
|
||||||
l1, n1, n2, dummy = f:read("l", "n", "n", "l")
|
|
||||||
assert(l1 == "a line" and not n1)
|
|
||||||
end
|
|
||||||
assert(os.remove(file))
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
-- test yielding during 'dofile'
|
-- test yielding during 'dofile'
|
||||||
f = assert(io.open(file, "w"))
|
f = assert(io.open(file, "w"))
|
||||||
f:write[[
|
f:write[[
|
||||||
@@ -204,7 +148,7 @@ return x + y * z
|
|||||||
assert(f:close())
|
assert(f:close())
|
||||||
f = coroutine.wrap(dofile)
|
f = coroutine.wrap(dofile)
|
||||||
assert(f(file) == 10)
|
assert(f(file) == 10)
|
||||||
assert(f(100, 101) == 20)
|
print(f(100, 101) == 20)
|
||||||
assert(f(200) == 100 + 200 * 101)
|
assert(f(200) == 100 + 200 * 101)
|
||||||
assert(os.remove(file))
|
assert(os.remove(file))
|
||||||
|
|
||||||
@@ -232,7 +176,7 @@ assert(f:read("n") == 0Xdeadbeefdeadbeef); assert(f:read(2) == "x\n")
|
|||||||
assert(f:read("n") == 0x1.13aP3); assert(f:read(1) == "e")
|
assert(f:read("n") == 0x1.13aP3); assert(f:read(1) == "e")
|
||||||
|
|
||||||
do -- attempt to read too long number
|
do -- attempt to read too long number
|
||||||
assert(not f:read("n")) -- fails
|
assert(f:read("n") == nil) -- fails
|
||||||
local s = f:read("L") -- read rest of line
|
local s = f:read("L") -- read rest of line
|
||||||
assert(string.find(s, "^00*\n$")) -- lots of 0's left
|
assert(string.find(s, "^00*\n$")) -- lots of 0's left
|
||||||
end
|
end
|
||||||
@@ -304,28 +248,28 @@ do -- test error returns
|
|||||||
end
|
end
|
||||||
checkerr("invalid format", io.read, "x")
|
checkerr("invalid format", io.read, "x")
|
||||||
assert(io.read(0) == "") -- not eof
|
assert(io.read(0) == "") -- not eof
|
||||||
assert(io.read(5, 'l') == '"alo"')
|
assert(io.read(5, 'l') == '"álo"')
|
||||||
assert(io.read(0) == "")
|
assert(io.read(0) == "")
|
||||||
assert(io.read() == "second line")
|
assert(io.read() == "second line")
|
||||||
local x = io.input():seek()
|
local x = io.input():seek()
|
||||||
assert(io.read() == "third line ")
|
assert(io.read() == "third line ")
|
||||||
assert(io.input():seek("set", x))
|
assert(io.input():seek("set", x))
|
||||||
assert(io.read('L') == "third line \n")
|
assert(io.read('L') == "third line \n")
|
||||||
assert(io.read(1) == "X")
|
assert(io.read(1) == "ç")
|
||||||
assert(io.read(string.len"fourth_line") == "fourth_line")
|
assert(io.read(string.len"fourth_line") == "fourth_line")
|
||||||
assert(io.input():seek("cur", -string.len"fourth_line"))
|
assert(io.input():seek("cur", -string.len"fourth_line"))
|
||||||
assert(io.read() == "fourth_line")
|
assert(io.read() == "fourth_line")
|
||||||
assert(io.read() == "") -- empty line
|
assert(io.read() == "") -- empty line
|
||||||
assert(io.read('n') == 3450)
|
assert(io.read('n') == 3450)
|
||||||
assert(io.read(1) == '\n')
|
assert(io.read(1) == '\n')
|
||||||
assert(not io.read(0)) -- end of file
|
assert(io.read(0) == nil) -- end of file
|
||||||
assert(not io.read(1)) -- end of file
|
assert(io.read(1) == nil) -- end of file
|
||||||
assert(not io.read(30000)) -- end of file
|
assert(io.read(30000) == nil) -- end of file
|
||||||
assert(({io.read(1)})[2] == undef)
|
assert(({io.read(1)})[2] == nil)
|
||||||
assert(not io.read()) -- end of file
|
assert(io.read() == nil) -- end of file
|
||||||
assert(({io.read()})[2] == undef)
|
assert(({io.read()})[2] == nil)
|
||||||
assert(not io.read('n')) -- end of file
|
assert(io.read('n') == nil) -- end of file
|
||||||
assert(({io.read('n')})[2] == undef)
|
assert(({io.read('n')})[2] == nil)
|
||||||
assert(io.read('a') == '') -- end of file (OK for 'a')
|
assert(io.read('a') == '') -- end of file (OK for 'a')
|
||||||
assert(io.read('a') == '') -- end of file (OK for 'a')
|
assert(io.read('a') == '') -- end of file (OK for 'a')
|
||||||
collectgarbage()
|
collectgarbage()
|
||||||
@@ -349,7 +293,7 @@ collectgarbage()
|
|||||||
|
|
||||||
assert(io.write(' ' .. t .. ' '))
|
assert(io.write(' ' .. t .. ' '))
|
||||||
assert(io.write(';', 'end of file\n'))
|
assert(io.write(';', 'end of file\n'))
|
||||||
assert(f:flush()); assert(io.flush())
|
f:flush(); io.flush()
|
||||||
f:close()
|
f:close()
|
||||||
print('+')
|
print('+')
|
||||||
|
|
||||||
@@ -360,7 +304,7 @@ assert(io.read(string.len(t)) == t)
|
|||||||
assert(io.read(1) == ' ')
|
assert(io.read(1) == ' ')
|
||||||
assert(io.read(0))
|
assert(io.read(0))
|
||||||
assert(io.read('a') == ';end of file\n')
|
assert(io.read('a') == ';end of file\n')
|
||||||
assert(not io.read(0))
|
assert(io.read(0) == nil)
|
||||||
assert(io.close(io.input()))
|
assert(io.close(io.input()))
|
||||||
|
|
||||||
|
|
||||||
@@ -368,7 +312,7 @@ assert(io.close(io.input()))
|
|||||||
do
|
do
|
||||||
local function ismsg (m)
|
local function ismsg (m)
|
||||||
-- error message is not a code number
|
-- error message is not a code number
|
||||||
return (type(m) == "string" and not tonumber(m))
|
return (type(m) == "string" and tonumber(m) == nil)
|
||||||
end
|
end
|
||||||
|
|
||||||
-- read
|
-- read
|
||||||
@@ -397,7 +341,7 @@ assert(io.read"L" == "\n")
|
|||||||
assert(io.read"L" == "\n")
|
assert(io.read"L" == "\n")
|
||||||
assert(io.read"L" == "line\n")
|
assert(io.read"L" == "line\n")
|
||||||
assert(io.read"L" == "other")
|
assert(io.read"L" == "other")
|
||||||
assert(not io.read"L")
|
assert(io.read"L" == nil)
|
||||||
io.input():close()
|
io.input():close()
|
||||||
|
|
||||||
local f = assert(io.open(file))
|
local f = assert(io.open(file))
|
||||||
@@ -422,68 +366,14 @@ assert(s == "lineother")
|
|||||||
|
|
||||||
io.output(file); io.write"a = 10 + 34\na = 2*a\na = -a\n":close()
|
io.output(file); io.write"a = 10 + 34\na = 2*a\na = -a\n":close()
|
||||||
local t = {}
|
local t = {}
|
||||||
assert(load(io.lines(file, "L"), nil, nil, t))()
|
load(io.lines(file, "L"), nil, nil, t)()
|
||||||
assert(t.a == -((10 + 34) * 2))
|
assert(t.a == -((10 + 34) * 2))
|
||||||
|
|
||||||
|
|
||||||
do -- testing closing file in line iteration
|
-- test for multipe arguments in 'lines'
|
||||||
|
|
||||||
-- get the to-be-closed variable from a loop
|
|
||||||
local function gettoclose (lv)
|
|
||||||
lv = lv + 1
|
|
||||||
local stvar = 0 -- to-be-closed is 3th state variable in the loop
|
|
||||||
for i = 1, 1000 do
|
|
||||||
local n, v = debug.getlocal(lv, i)
|
|
||||||
if n == "(for state)" then
|
|
||||||
stvar = stvar + 1
|
|
||||||
if stvar == 3 then return v end
|
|
||||||
end
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
local f
|
|
||||||
for l in io.lines(file) do
|
|
||||||
f = gettoclose(1)
|
|
||||||
assert(io.type(f) == "file")
|
|
||||||
break
|
|
||||||
end
|
|
||||||
assert(io.type(f) == "closed file")
|
|
||||||
|
|
||||||
f = nil
|
|
||||||
local function foo (name)
|
|
||||||
for l in io.lines(name) do
|
|
||||||
f = gettoclose(1)
|
|
||||||
assert(io.type(f) == "file")
|
|
||||||
error(f) -- exit loop with an error
|
|
||||||
end
|
|
||||||
end
|
|
||||||
local st, msg = pcall(foo, file)
|
|
||||||
assert(st == false and io.type(msg) == "closed file")
|
|
||||||
|
|
||||||
end
|
|
||||||
|
|
||||||
|
|
||||||
do print("testing flush")
|
|
||||||
local f = io.output("/dev/null")
|
|
||||||
assert(f:write("abcd")) -- write to buffer
|
|
||||||
assert(f:flush()) -- write to device
|
|
||||||
assert(f:write("abcd")) -- write to buffer
|
|
||||||
assert(io.flush()) -- write to device
|
|
||||||
assert(f:close())
|
|
||||||
|
|
||||||
local f = io.output("/dev/full")
|
|
||||||
assert(f:write("abcd")) -- write to buffer
|
|
||||||
assert(not f:flush()) -- cannot write to device
|
|
||||||
assert(f:write("abcd")) -- write to buffer
|
|
||||||
assert(not io.flush()) -- cannot write to device
|
|
||||||
assert(f:close())
|
|
||||||
end
|
|
||||||
|
|
||||||
|
|
||||||
-- test for multiple arguments in 'lines'
|
|
||||||
io.output(file); io.write"0123456789\n":close()
|
io.output(file); io.write"0123456789\n":close()
|
||||||
for a,b in io.lines(file, 1, 1) do
|
for a,b in io.lines(file, 1, 1) do
|
||||||
if a == "\n" then assert(not b)
|
if a == "\n" then assert(b == nil)
|
||||||
else assert(tonumber(a) == tonumber(b) - 1)
|
else assert(tonumber(a) == tonumber(b) - 1)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
@@ -494,13 +384,13 @@ end
|
|||||||
|
|
||||||
for a,b,c in io.lines(file, "a", 0, 1) do
|
for a,b,c in io.lines(file, "a", 0, 1) do
|
||||||
if a == "" then break end
|
if a == "" then break end
|
||||||
assert(a == "0123456789\n" and not b and not c)
|
assert(a == "0123456789\n" and b == nil and c == nil)
|
||||||
end
|
end
|
||||||
collectgarbage() -- to close file in previous iteration
|
collectgarbage() -- to close file in previous iteration
|
||||||
|
|
||||||
io.output(file); io.write"00\n10\n20\n30\n40\n":close()
|
io.output(file); io.write"00\n10\n20\n30\n40\n":close()
|
||||||
for a, b in io.lines(file, "n", "n") do
|
for a, b in io.lines(file, "n", "n") do
|
||||||
if a == 40 then assert(not b)
|
if a == 40 then assert(b == nil)
|
||||||
else assert(a == b - 10)
|
else assert(a == b - 10)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
@@ -520,25 +410,23 @@ X
|
|||||||
- y;
|
- y;
|
||||||
]]:close()
|
]]:close()
|
||||||
_G.X = 1
|
_G.X = 1
|
||||||
assert(not load((io.lines(file))))
|
assert(not load(io.lines(file)))
|
||||||
collectgarbage() -- to close file in previous iteration
|
collectgarbage() -- to close file in previous iteration
|
||||||
load((io.lines(file, "L")))()
|
load(io.lines(file, "L"))()
|
||||||
assert(_G.X == 2)
|
assert(_G.X == 2)
|
||||||
load((io.lines(file, 1)))()
|
load(io.lines(file, 1))()
|
||||||
assert(_G.X == 4)
|
assert(_G.X == 4)
|
||||||
load((io.lines(file, 3)))()
|
load(io.lines(file, 3))()
|
||||||
assert(_G.X == 8)
|
assert(_G.X == 8)
|
||||||
_G.X = nil
|
|
||||||
|
|
||||||
print('+')
|
print('+')
|
||||||
|
|
||||||
local x1 = "string\n\n\\com \"\"''coisas [[estranhas]] ]]'"
|
local x1 = "string\n\n\\com \"\"''coisas [[estranhas]] ]]'"
|
||||||
io.output(file)
|
io.output(file)
|
||||||
assert(io.write(string.format("X2 = %q\n-- comment without ending EOS", x1)))
|
assert(io.write(string.format("x2 = %q\n-- comment without ending EOS", x1)))
|
||||||
io.close()
|
io.close()
|
||||||
assert(loadfile(file))()
|
assert(loadfile(file))()
|
||||||
assert(x1 == _G.X2)
|
assert(x1 == x2)
|
||||||
_G.X2 = nil
|
|
||||||
print('+')
|
print('+')
|
||||||
assert(os.remove(file))
|
assert(os.remove(file))
|
||||||
assert(not os.remove(file))
|
assert(not os.remove(file))
|
||||||
@@ -677,7 +565,7 @@ and the rest of the file
|
|||||||
io.input(file)
|
io.input(file)
|
||||||
local _,a,b,c,d,e,h,__ = io.read(1, 'n', 'n', 'l', 'l', 'l', 'a', 10)
|
local _,a,b,c,d,e,h,__ = io.read(1, 'n', 'n', 'l', 'l', 'l', 'a', 10)
|
||||||
assert(io.close(io.input()))
|
assert(io.close(io.input()))
|
||||||
assert(_ == ' ' and not __)
|
assert(_ == ' ' and __ == nil)
|
||||||
assert(type(a) == 'number' and a==123.4 and b==-56e-2)
|
assert(type(a) == 'number' and a==123.4 and b==-56e-2)
|
||||||
assert(d=='second line' and e=='third line')
|
assert(d=='second line' and e=='third line')
|
||||||
assert(h==[[
|
assert(h==[[
|
||||||
@@ -715,37 +603,6 @@ do
|
|||||||
end
|
end
|
||||||
|
|
||||||
|
|
||||||
if T and T.nonblock and not _port then
|
|
||||||
print("testing failed write")
|
|
||||||
|
|
||||||
-- unable to write anything to /dev/full
|
|
||||||
local f = io.open("/dev/full", "w")
|
|
||||||
assert(f:setvbuf("no"))
|
|
||||||
local _, _, err, count = f:write("abcd")
|
|
||||||
assert(err > 0 and count == 0)
|
|
||||||
assert(f:close())
|
|
||||||
|
|
||||||
-- receiver will read a "few" bytes (enough to empty a large buffer)
|
|
||||||
local receiver = [[
|
|
||||||
lua -e 'assert(io.stdin:setvbuf("no")); assert(#io.read(1e4) == 1e4)' ]]
|
|
||||||
|
|
||||||
local f = io.popen(receiver, "w")
|
|
||||||
assert(f:setvbuf("no"))
|
|
||||||
T.nonblock(f)
|
|
||||||
|
|
||||||
-- able to write a few bytes
|
|
||||||
assert(f:write(string.rep("a", 1e2)))
|
|
||||||
|
|
||||||
-- Unable to write more bytes than the pipe buffer supports.
|
|
||||||
-- (In Linux, the pipe buffer size is 64K (2^16). Posix requires at
|
|
||||||
-- least 512 bytes.)
|
|
||||||
local _, _, err, count = f:write("abcd", string.rep("a", 2^17))
|
|
||||||
assert(err > 0 and count >= 512 and count < 2^17)
|
|
||||||
|
|
||||||
assert(f:close())
|
|
||||||
end
|
|
||||||
|
|
||||||
|
|
||||||
if not _soft then
|
if not _soft then
|
||||||
print("testing large files (> BUFSIZ)")
|
print("testing large files (> BUFSIZ)")
|
||||||
io.output(file)
|
io.output(file)
|
||||||
@@ -760,7 +617,7 @@ if not _soft then
|
|||||||
io.input():seek('set', 0)
|
io.input():seek('set', 0)
|
||||||
y = io.read() -- huge line
|
y = io.read() -- huge line
|
||||||
assert(x == y..'\n'..io.read())
|
assert(x == y..'\n'..io.read())
|
||||||
assert(not io.read())
|
assert(io.read() == nil)
|
||||||
io.close(io.input())
|
io.close(io.input())
|
||||||
assert(os.remove(file))
|
assert(os.remove(file))
|
||||||
x = nil; y = nil
|
x = nil; y = nil
|
||||||
@@ -769,27 +626,12 @@ end
|
|||||||
if not _port then
|
if not _port then
|
||||||
local progname
|
local progname
|
||||||
do -- get name of running executable
|
do -- get name of running executable
|
||||||
local arg = arg or ARG
|
local arg = arg or _ARG
|
||||||
local i = 0
|
local i = 0
|
||||||
while arg[i] do i = i - 1 end
|
while arg[i] do i = i - 1 end
|
||||||
progname = '"' .. arg[i + 1] .. '"'
|
progname = '"' .. arg[i + 1] .. '"'
|
||||||
end
|
end
|
||||||
print("testing popen/pclose and execute")
|
print("testing popen/pclose and execute")
|
||||||
-- invalid mode for popen
|
|
||||||
checkerr("invalid mode", io.popen, "cat", "")
|
|
||||||
checkerr("invalid mode", io.popen, "cat", "r+")
|
|
||||||
checkerr("invalid mode", io.popen, "cat", "rw")
|
|
||||||
do -- basic tests for popen
|
|
||||||
local file = os.tmpname()
|
|
||||||
local f = assert(io.popen("cat - > " .. file, "w"))
|
|
||||||
f:write("a line")
|
|
||||||
assert(f:close())
|
|
||||||
local f = assert(io.popen("cat - < " .. file, "r"))
|
|
||||||
assert(f:read("a") == "a line")
|
|
||||||
assert(f:close())
|
|
||||||
assert(os.remove(file))
|
|
||||||
end
|
|
||||||
|
|
||||||
local tests = {
|
local tests = {
|
||||||
-- command, what, code
|
-- command, what, code
|
||||||
{"ls > /dev/null", "ok"},
|
{"ls > /dev/null", "ok"},
|
||||||
@@ -816,7 +658,6 @@ if not _port then
|
|||||||
assert((v[3] == nil and z > 0) or v[3] == z)
|
assert((v[3] == nil and z > 0) or v[3] == z)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
print("(done)")
|
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
||||||
@@ -840,29 +681,16 @@ assert(os.date("!\0\0") == "\0\0")
|
|||||||
local x = string.rep("a", 10000)
|
local x = string.rep("a", 10000)
|
||||||
assert(os.date(x) == x)
|
assert(os.date(x) == x)
|
||||||
local t = os.time()
|
local t = os.time()
|
||||||
global D = os.date("*t", t)
|
D = os.date("*t", t)
|
||||||
assert(os.date(string.rep("%d", 1000), t) ==
|
assert(os.date(string.rep("%d", 1000), t) ==
|
||||||
string.rep(os.date("%d", t), 1000))
|
string.rep(os.date("%d", t), 1000))
|
||||||
assert(os.date(string.rep("%", 200)) == string.rep("%", 100))
|
assert(os.date(string.rep("%", 200)) == string.rep("%", 100))
|
||||||
|
|
||||||
local function checkDateTable (t)
|
local t = os.time()
|
||||||
D = os.date("*t", t)
|
D = os.date("*t", t)
|
||||||
assert(os.time(D) == t)
|
load(os.date([[assert(D.year==%Y and D.month==%m and D.day==%d and
|
||||||
load(os.date([[assert(D.year==%Y and D.month==%m and D.day==%d and
|
D.hour==%H and D.min==%M and D.sec==%S and
|
||||||
D.hour==%H and D.min==%M and D.sec==%S and
|
D.wday==%w+1 and D.yday==%j and type(D.isdst) == 'boolean')]], t))()
|
||||||
D.wday==%w+1 and D.yday==%j)]], t))()
|
|
||||||
_G.D = nil
|
|
||||||
end
|
|
||||||
|
|
||||||
checkDateTable(os.time())
|
|
||||||
if not _port then
|
|
||||||
-- assume that time_t can represent these values
|
|
||||||
checkDateTable(0)
|
|
||||||
checkDateTable(1)
|
|
||||||
checkDateTable(1000)
|
|
||||||
checkDateTable(0x7fffffff)
|
|
||||||
checkDateTable(0x80000000)
|
|
||||||
end
|
|
||||||
|
|
||||||
checkerr("invalid conversion specifier", os.date, "%")
|
checkerr("invalid conversion specifier", os.date, "%")
|
||||||
checkerr("invalid conversion specifier", os.date, "%9")
|
checkerr("invalid conversion specifier", os.date, "%9")
|
||||||
@@ -876,33 +704,11 @@ checkerr("not an integer", os.time, {year=1000, month=1, day=1, hour=1.5})
|
|||||||
|
|
||||||
checkerr("missing", os.time, {hour = 12}) -- missing date
|
checkerr("missing", os.time, {hour = 12}) -- missing date
|
||||||
|
|
||||||
|
|
||||||
if string.packsize("i") == 4 then -- 4-byte ints
|
|
||||||
checkerr("field 'year' is out-of-bound", os.time,
|
|
||||||
{year = -(1 << 31) + 1899, month = 1, day = 1})
|
|
||||||
|
|
||||||
checkerr("field 'year' is out-of-bound", os.time,
|
|
||||||
{year = -(1 << 31), month = 1, day = 1})
|
|
||||||
|
|
||||||
if math.maxinteger > 2^31 then -- larger lua_integer?
|
|
||||||
checkerr("field 'year' is out-of-bound", os.time,
|
|
||||||
{year = (1 << 31) + 1900, month = 1, day = 1})
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
|
|
||||||
if not _port then
|
if not _port then
|
||||||
-- test Posix-specific modifiers
|
-- test Posix-specific modifiers
|
||||||
assert(type(os.date("%Ex")) == 'string')
|
assert(type(os.date("%Ex")) == 'string')
|
||||||
assert(type(os.date("%Oy")) == 'string')
|
assert(type(os.date("%Oy")) == 'string')
|
||||||
|
|
||||||
-- test large dates (assume at least 4-byte ints and time_t)
|
|
||||||
local t0 = os.time{year = 1970, month = 1, day = 0}
|
|
||||||
local t1 = os.time{year = 1970, month = 1, day = 0, sec = (1 << 31) - 1}
|
|
||||||
assert(t1 - t0 == (1 << 31) - 1)
|
|
||||||
t0 = os.time{year = 1970, month = 1, day = 1}
|
|
||||||
t1 = os.time{year = 1970, month = 1, day = 1, sec = -(1 << 31)}
|
|
||||||
assert(t1 - t0 == -(1 << 31))
|
|
||||||
|
|
||||||
-- test out-of-range dates (at least for Unix)
|
-- test out-of-range dates (at least for Unix)
|
||||||
if maxint >= 2^62 then -- cannot do these tests in Small Lua
|
if maxint >= 2^62 then -- cannot do these tests in Small Lua
|
||||||
@@ -917,51 +723,34 @@ if not _port then
|
|||||||
-- time_t has 8 bytes; an int year cannot represent a huge time
|
-- time_t has 8 bytes; an int year cannot represent a huge time
|
||||||
print(" 8-byte time_t")
|
print(" 8-byte time_t")
|
||||||
checkerr("cannot be represented", os.date, "%Y", 2^60)
|
checkerr("cannot be represented", os.date, "%Y", 2^60)
|
||||||
|
-- it should have no problems with year 4000
|
||||||
-- this is the maximum year
|
assert(tonumber(os.time{year=4000, month=1, day=1}))
|
||||||
assert(tonumber(os.time
|
|
||||||
{year=(1 << 31) + 1899, month=12, day=31, hour=23, min=59, sec=59}))
|
|
||||||
|
|
||||||
-- this is too much
|
|
||||||
checkerr("represented", os.time,
|
|
||||||
{year=(1 << 31) + 1899, month=12, day=31, hour=23, min=59, sec=60})
|
|
||||||
end
|
end
|
||||||
|
|
||||||
-- internal 'int' fields cannot hold these values
|
|
||||||
checkerr("field 'day' is out-of-bound", os.time,
|
|
||||||
{year = 0, month = 1, day = 2^32})
|
|
||||||
|
|
||||||
checkerr("field 'month' is out-of-bound", os.time,
|
|
||||||
{year = 0, month = -((1 << 31) + 1), day = 1})
|
|
||||||
|
|
||||||
checkerr("field 'year' is out-of-bound", os.time,
|
|
||||||
{year = (1 << 31) + 1900, month = 1, day = 1})
|
|
||||||
|
|
||||||
else -- 8-byte ints
|
else -- 8-byte ints
|
||||||
-- assume time_t has 8 bytes too
|
-- assume time_t has 8 bytes too
|
||||||
print(" 8-byte time_t")
|
print(" 8-byte time_t")
|
||||||
assert(tonumber(os.date("%Y", 2^60)))
|
assert(tonumber(os.date("%Y", 2^60)))
|
||||||
|
|
||||||
-- but still cannot represent a huge year
|
-- but still cannot represent a huge year
|
||||||
checkerr("cannot be represented", os.time, {year=2^60, month=1, day=1})
|
checkerr("cannot be represented", os.time, {year=2^60, month=1, day=1})
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
||||||
|
D = os.date("!*t", t)
|
||||||
|
load(os.date([[!assert(D.year==%Y and D.month==%m and D.day==%d and
|
||||||
|
D.hour==%H and D.min==%M and D.sec==%S and
|
||||||
|
D.wday==%w+1 and D.yday==%j and type(D.isdst) == 'boolean')]], t))()
|
||||||
|
|
||||||
do
|
do
|
||||||
local D = os.date("*t")
|
local D = os.date("*t")
|
||||||
local t = os.time(D)
|
local t = os.time(D)
|
||||||
if D.isdst == nil then
|
assert(type(D.isdst) == 'boolean')
|
||||||
print("no daylight saving information")
|
|
||||||
else
|
|
||||||
assert(type(D.isdst) == 'boolean')
|
|
||||||
end
|
|
||||||
D.isdst = nil
|
D.isdst = nil
|
||||||
local t1 = os.time(D)
|
local t1 = os.time(D)
|
||||||
assert(t == t1) -- if isdst is absent uses correct default
|
assert(t == t1) -- if isdst is absent uses correct default
|
||||||
end
|
end
|
||||||
|
|
||||||
local D = os.date("*t")
|
|
||||||
t = os.time(D)
|
t = os.time(D)
|
||||||
D.year = D.year-1;
|
D.year = D.year-1;
|
||||||
local t1 = os.time(D)
|
local t1 = os.time(D)
|
||||||
|
|||||||
+166
-252
@@ -1,89 +1,48 @@
|
|||||||
-- $Id: testes/gc.lua $
|
-- $Id: gc.lua,v 1.72 2016/11/07 13:11:28 roberto Exp $
|
||||||
-- See Copyright Notice in file lua.h
|
-- See Copyright Notice in file all.lua
|
||||||
|
|
||||||
print('testing incremental garbage collection')
|
print('testing garbage collection')
|
||||||
|
|
||||||
local debug = require"debug"
|
local debug = require"debug"
|
||||||
|
|
||||||
assert(collectgarbage("isrunning"))
|
|
||||||
|
|
||||||
collectgarbage()
|
collectgarbage()
|
||||||
|
|
||||||
local oldmode = collectgarbage("incremental")
|
assert(collectgarbage("isrunning"))
|
||||||
|
|
||||||
-- changing modes should return previous mode
|
local function gcinfo () return collectgarbage"count" * 1024 end
|
||||||
assert(collectgarbage("generational") == "incremental")
|
|
||||||
assert(collectgarbage("generational") == "generational")
|
|
||||||
assert(collectgarbage("incremental") == "generational")
|
|
||||||
assert(collectgarbage("incremental") == "incremental")
|
|
||||||
|
|
||||||
|
|
||||||
local function nop () end
|
-- test weird parameters
|
||||||
|
|
||||||
local function gcinfo ()
|
|
||||||
return collectgarbage"count" * 1024
|
|
||||||
end
|
|
||||||
|
|
||||||
|
|
||||||
-- test weird parameters to 'collectgarbage'
|
|
||||||
do
|
do
|
||||||
collectgarbage("incremental")
|
-- save original parameters
|
||||||
local opause = collectgarbage("param", "pause", 100)
|
local a = collectgarbage("setpause", 200)
|
||||||
local ostepmul = collectgarbage("param", "stepmul", 100)
|
local b = collectgarbage("setstepmul", 200)
|
||||||
assert(collectgarbage("param", "pause") == 100)
|
|
||||||
assert(collectgarbage("param", "stepmul") == 100)
|
|
||||||
local t = {0, 2, 10, 90, 500, 5000, 30000, 0x7ffffffe}
|
local t = {0, 2, 10, 90, 500, 5000, 30000, 0x7ffffffe}
|
||||||
for i = 1, #t do
|
for i = 1, #t do
|
||||||
collectgarbage("param", "pause", t[i])
|
local p = t[i]
|
||||||
for j = 1, #t do
|
for j = 1, #t do
|
||||||
collectgarbage("param", "stepmul", t[j])
|
local m = t[j]
|
||||||
collectgarbage("step", t[j])
|
collectgarbage("setpause", p)
|
||||||
|
collectgarbage("setstepmul", m)
|
||||||
|
collectgarbage("step", 0)
|
||||||
|
collectgarbage("step", 10000)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
-- restore original parameters
|
-- restore original parameters
|
||||||
collectgarbage("param", "pause", opause)
|
collectgarbage("setpause", a)
|
||||||
collectgarbage("param", "stepmul", ostepmul)
|
collectgarbage("setstepmul", b)
|
||||||
collectgarbage()
|
collectgarbage()
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
||||||
--
|
|
||||||
-- test the "size" of basic GC steps (whatever they mean...)
|
|
||||||
--
|
|
||||||
do print("steps")
|
|
||||||
|
|
||||||
local function dosteps (siz)
|
|
||||||
collectgarbage()
|
|
||||||
local a = {}
|
|
||||||
for i=1,100 do a[i] = {{}}; local b = {} end
|
|
||||||
local x = gcinfo()
|
|
||||||
local i = 0
|
|
||||||
repeat -- do steps until it completes a collection cycle
|
|
||||||
i = i+1
|
|
||||||
until collectgarbage("step", siz)
|
|
||||||
assert(gcinfo() < x)
|
|
||||||
return i -- number of steps
|
|
||||||
end
|
|
||||||
|
|
||||||
|
|
||||||
if not _port then
|
|
||||||
collectgarbage"stop"
|
|
||||||
assert(dosteps(10) < dosteps(2))
|
|
||||||
collectgarbage"restart"
|
|
||||||
end
|
|
||||||
|
|
||||||
end
|
|
||||||
|
|
||||||
|
|
||||||
_G["while"] = 234
|
_G["while"] = 234
|
||||||
|
|
||||||
|
limit = 5000
|
||||||
|
|
||||||
|
|
||||||
--
|
|
||||||
-- tests for GC activation when creating different kinds of objects
|
|
||||||
--
|
|
||||||
local function GC1 ()
|
local function GC1 ()
|
||||||
local u
|
local u
|
||||||
local b -- (above 'u' it in the stack)
|
local b -- must be declared after 'u' (to be above it in the stack)
|
||||||
local finish = false
|
local finish = false
|
||||||
u = setmetatable({}, {__gc = function () finish = true end})
|
u = setmetatable({}, {__gc = function () finish = true end})
|
||||||
b = {34}
|
b = {34}
|
||||||
@@ -105,7 +64,7 @@ local function GC2 ()
|
|||||||
local u
|
local u
|
||||||
local finish = false
|
local finish = false
|
||||||
u = {setmetatable({}, {__gc = function () finish = true end})}
|
u = {setmetatable({}, {__gc = function () finish = true end})}
|
||||||
local b = {34}
|
b = {34}
|
||||||
repeat u = {{}} until finish
|
repeat u = {{}} until finish
|
||||||
assert(b[1] == 34) -- 'u' was collected, but 'b' was not
|
assert(b[1] == 34) -- 'u' was collected, but 'b' was not
|
||||||
|
|
||||||
@@ -123,45 +82,47 @@ end
|
|||||||
local function GC() GC1(); GC2() end
|
local function GC() GC1(); GC2() end
|
||||||
|
|
||||||
|
|
||||||
do
|
contCreate = 0
|
||||||
print("creating many objects")
|
|
||||||
|
|
||||||
local limit = 5000
|
print('tables')
|
||||||
|
while contCreate <= limit do
|
||||||
for i = 1, limit do
|
local a = {}; a = nil
|
||||||
local a = {}; a = nil
|
contCreate = contCreate+1
|
||||||
end
|
|
||||||
|
|
||||||
local a = "a"
|
|
||||||
|
|
||||||
for i = 1, limit do
|
|
||||||
a = i .. "b";
|
|
||||||
a = string.gsub(a, '(%d%d*)', "%1 %1")
|
|
||||||
a = "a"
|
|
||||||
end
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
a = {}
|
|
||||||
|
|
||||||
function a:test ()
|
|
||||||
for i = 1, limit do
|
|
||||||
load(string.format("function temp(a) return 'a%d' end", i), "")()
|
|
||||||
assert(temp() == string.format('a%d', i))
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
a:test()
|
|
||||||
_G.temp = nil
|
|
||||||
end
|
end
|
||||||
|
|
||||||
|
a = "a"
|
||||||
|
|
||||||
|
contCreate = 0
|
||||||
|
print('strings')
|
||||||
|
while contCreate <= limit do
|
||||||
|
a = contCreate .. "b";
|
||||||
|
a = string.gsub(a, '(%d%d*)', string.upper)
|
||||||
|
a = "a"
|
||||||
|
contCreate = contCreate+1
|
||||||
|
end
|
||||||
|
|
||||||
|
|
||||||
|
contCreate = 0
|
||||||
|
|
||||||
|
a = {}
|
||||||
|
|
||||||
|
print('functions')
|
||||||
|
function a:test ()
|
||||||
|
while contCreate <= limit do
|
||||||
|
load(string.format("function temp(a) return 'a%d' end", contCreate), "")()
|
||||||
|
assert(temp() == string.format('a%d', contCreate))
|
||||||
|
contCreate = contCreate+1
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
a:test()
|
||||||
|
|
||||||
-- collection of functions without locals, globals, etc.
|
-- collection of functions without locals, globals, etc.
|
||||||
do local f = function () end end
|
do local f = function () end end
|
||||||
|
|
||||||
|
|
||||||
print("functions with errors")
|
print("functions with errors")
|
||||||
local prog = [[
|
prog = [[
|
||||||
do
|
do
|
||||||
a = 10;
|
a = 10;
|
||||||
function foo(x,y)
|
function foo(x,y)
|
||||||
@@ -180,32 +141,68 @@ do
|
|||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
rawset(_G, "a", nil)
|
|
||||||
_G.x = nil
|
|
||||||
|
|
||||||
do
|
foo = nil
|
||||||
foo = nil
|
print('long strings')
|
||||||
print('long strings')
|
x = "01234567890123456789012345678901234567890123456789012345678901234567890123456789"
|
||||||
local x = "01234567890123456789012345678901234567890123456789012345678901234567890123456789"
|
assert(string.len(x)==80)
|
||||||
assert(string.len(x)==80)
|
s = ''
|
||||||
local s = ''
|
n = 0
|
||||||
local k = math.min(300, (math.maxinteger // 80) // 2)
|
k = math.min(300, (math.maxinteger // 80) // 2)
|
||||||
for n = 1, k do s = s..x; local j=tostring(n) end
|
while n < k do s = s..x; n=n+1; j=tostring(n) end
|
||||||
assert(string.len(s) == k*80)
|
assert(string.len(s) == k*80)
|
||||||
s = string.sub(s, 1, 10000)
|
s = string.sub(s, 1, 10000)
|
||||||
local s, i = string.gsub(s, '(%d%d%d%d)', '')
|
s, i = string.gsub(s, '(%d%d%d%d)', '')
|
||||||
assert(i==10000 // 4)
|
assert(i==10000 // 4)
|
||||||
|
s = nil
|
||||||
|
x = nil
|
||||||
|
|
||||||
assert(_G["while"] == 234)
|
assert(_G["while"] == 234)
|
||||||
_G["while"] = nil
|
|
||||||
|
|
||||||
|
print("steps")
|
||||||
|
|
||||||
|
print("steps (2)")
|
||||||
|
|
||||||
|
local function dosteps (siz)
|
||||||
|
assert(not collectgarbage("isrunning"))
|
||||||
|
collectgarbage()
|
||||||
|
assert(not collectgarbage("isrunning"))
|
||||||
|
local a = {}
|
||||||
|
for i=1,100 do a[i] = {{}}; local b = {} end
|
||||||
|
local x = gcinfo()
|
||||||
|
local i = 0
|
||||||
|
repeat -- do steps until it completes a collection cycle
|
||||||
|
i = i+1
|
||||||
|
until collectgarbage("step", siz)
|
||||||
|
assert(gcinfo() < x)
|
||||||
|
return i
|
||||||
end
|
end
|
||||||
|
|
||||||
|
collectgarbage"stop"
|
||||||
|
|
||||||
|
if not _port then
|
||||||
|
-- test the "size" of basic GC steps (whatever they mean...)
|
||||||
|
assert(dosteps(0) > 10)
|
||||||
|
assert(dosteps(10) < dosteps(2))
|
||||||
|
end
|
||||||
|
|
||||||
|
-- collector should do a full collection with so many steps
|
||||||
|
assert(dosteps(20000) == 1)
|
||||||
|
assert(collectgarbage("step", 20000) == true)
|
||||||
|
assert(collectgarbage("step", 20000) == true)
|
||||||
|
|
||||||
|
assert(not collectgarbage("isrunning"))
|
||||||
|
collectgarbage"restart"
|
||||||
|
assert(collectgarbage("isrunning"))
|
||||||
|
|
||||||
|
|
||||||
if not _port then
|
if not _port then
|
||||||
-- test the pace of the collector
|
-- test the pace of the collector
|
||||||
collectgarbage(); collectgarbage()
|
collectgarbage(); collectgarbage()
|
||||||
local x = gcinfo()
|
local x = gcinfo()
|
||||||
collectgarbage"stop"
|
collectgarbage"stop"
|
||||||
|
assert(not collectgarbage("isrunning"))
|
||||||
repeat
|
repeat
|
||||||
local a = {}
|
local a = {}
|
||||||
until gcinfo() > 3 * x
|
until gcinfo() > 3 * x
|
||||||
@@ -218,15 +215,15 @@ end
|
|||||||
|
|
||||||
|
|
||||||
print("clearing tables")
|
print("clearing tables")
|
||||||
local lim = 15
|
lim = 15
|
||||||
local a = {}
|
a = {}
|
||||||
-- fill a with `collectable' indices
|
-- fill a with `collectable' indices
|
||||||
for i=1,lim do a[{}] = i end
|
for i=1,lim do a[{}] = i end
|
||||||
b = {}
|
b = {}
|
||||||
for k,v in pairs(a) do b[k]=v end
|
for k,v in pairs(a) do b[k]=v end
|
||||||
-- remove all indices and collect them
|
-- remove all indices and collect them
|
||||||
for n in pairs(b) do
|
for n in pairs(b) do
|
||||||
a[n] = undef
|
a[n] = nil
|
||||||
assert(type(n) == 'table' and next(n) == nil)
|
assert(type(n) == 'table' and next(n) == nil)
|
||||||
collectgarbage()
|
collectgarbage()
|
||||||
end
|
end
|
||||||
@@ -253,7 +250,7 @@ a = {}; setmetatable(a, {__mode = 'v'});
|
|||||||
a[1] = string.rep('b', 21)
|
a[1] = string.rep('b', 21)
|
||||||
collectgarbage()
|
collectgarbage()
|
||||||
assert(a[1]) -- strings are *values*
|
assert(a[1]) -- strings are *values*
|
||||||
a[1] = undef
|
a[1] = nil
|
||||||
-- fill a with some `collectable' values (in both parts of the table)
|
-- fill a with some `collectable' values (in both parts of the table)
|
||||||
for i=1,lim do a[i] = {} end
|
for i=1,lim do a[i] = {} end
|
||||||
for i=1,lim do a[i..'x'] = {} end
|
for i=1,lim do a[i..'x'] = {} end
|
||||||
@@ -265,7 +262,7 @@ local i = 0
|
|||||||
for k,v in pairs(a) do assert(k==v or k-lim..'x' == v); i=i+1 end
|
for k,v in pairs(a) do assert(k==v or k-lim..'x' == v); i=i+1 end
|
||||||
assert(i == 2*lim)
|
assert(i == 2*lim)
|
||||||
|
|
||||||
a = {}; setmetatable(a, {__mode = 'kv'});
|
a = {}; setmetatable(a, {__mode = 'vk'});
|
||||||
local x, y, z = {}, {}, {}
|
local x, y, z = {}, {}, {}
|
||||||
-- keep only some items
|
-- keep only some items
|
||||||
a[1], a[2], a[3] = x, y, z
|
a[1], a[2], a[3] = x, y, z
|
||||||
@@ -288,21 +285,6 @@ x,y,z=nil
|
|||||||
collectgarbage()
|
collectgarbage()
|
||||||
assert(next(a) == string.rep('$', 11))
|
assert(next(a) == string.rep('$', 11))
|
||||||
|
|
||||||
do -- invalid mode
|
|
||||||
local a = setmetatable({}, {__mode = 34})
|
|
||||||
collectgarbage()
|
|
||||||
end
|
|
||||||
|
|
||||||
|
|
||||||
if T then -- bug since 5.3: all-weak tables are not being revisited
|
|
||||||
T.gcstate("propagate")
|
|
||||||
local t = setmetatable({}, {__mode = "kv"})
|
|
||||||
T.gcstate("enteratomic") -- 't' was visited
|
|
||||||
setmetatable(t, {__mode = "kv"})
|
|
||||||
T.gcstate("pause") -- its new metatable is not being visited
|
|
||||||
assert(getmetatable(t).__mode == "kv")
|
|
||||||
end
|
|
||||||
|
|
||||||
|
|
||||||
-- 'bug' in 5.1
|
-- 'bug' in 5.1
|
||||||
a = {}
|
a = {}
|
||||||
@@ -336,7 +318,7 @@ while n do n = a[n].k[1]; i = i + 1 end
|
|||||||
assert(i == 100)
|
assert(i == 100)
|
||||||
x = nil
|
x = nil
|
||||||
GC()
|
GC()
|
||||||
for i = 1, 4 do assert(a[i][1] == i * 10); a[i] = undef end
|
for i = 1, 4 do assert(a[i][1] == i * 10); a[i] = nil end
|
||||||
assert(next(a) == nil)
|
assert(next(a) == nil)
|
||||||
|
|
||||||
local K = {}
|
local K = {}
|
||||||
@@ -359,38 +341,40 @@ GC()
|
|||||||
|
|
||||||
|
|
||||||
-- testing errors during GC
|
-- testing errors during GC
|
||||||
if T then
|
do
|
||||||
collectgarbage("stop") -- stop collection
|
collectgarbage("stop") -- stop collection
|
||||||
local u = {}
|
local u = {}
|
||||||
local s = {}; setmetatable(s, {__mode = 'k'})
|
local s = {}; setmetatable(s, {__mode = 'k'})
|
||||||
setmetatable(u, {__gc = function (o)
|
setmetatable(u, {__gc = function (o)
|
||||||
local i = s[o]
|
local i = s[o]
|
||||||
s[i] = true
|
s[i] = true
|
||||||
assert(not s[i - 1]) -- check proper finalization order
|
assert(not s[i - 1]) -- check proper finalization order
|
||||||
if i == 8 then error("@expected@") end -- error during GC
|
if i == 8 then error("here") end -- error during GC
|
||||||
end})
|
end})
|
||||||
|
|
||||||
for i = 6, 10 do
|
for i = 6, 10 do
|
||||||
local n = setmetatable({}, getmetatable(u))
|
local n = setmetatable({}, getmetatable(u))
|
||||||
s[n] = i
|
s[n] = i
|
||||||
end
|
end
|
||||||
|
|
||||||
warn("@on"); warn("@store")
|
assert(not pcall(collectgarbage))
|
||||||
collectgarbage()
|
for i = 8, 10 do assert(s[i]) end
|
||||||
assert(string.find(_WARN, "error in __gc"))
|
|
||||||
assert(string.match(_WARN, "@(.-)@") == "expected"); _WARN = false
|
|
||||||
for i = 8, 10 do assert(s[i]) end
|
|
||||||
|
|
||||||
for i = 1, 5 do
|
for i = 1, 5 do
|
||||||
local n = setmetatable({}, getmetatable(u))
|
local n = setmetatable({}, getmetatable(u))
|
||||||
s[n] = i
|
s[n] = i
|
||||||
end
|
end
|
||||||
|
|
||||||
collectgarbage()
|
collectgarbage()
|
||||||
for i = 1, 10 do assert(s[i]) end
|
for i = 1, 10 do assert(s[i]) end
|
||||||
|
|
||||||
getmetatable(u).__gc = nil
|
getmetatable(u).__gc = false
|
||||||
warn("@normal")
|
|
||||||
|
|
||||||
|
-- __gc errors with non-string messages
|
||||||
|
setmetatable({}, {__gc = function () error{} end})
|
||||||
|
local a, b = pcall(collectgarbage)
|
||||||
|
assert(not a and type(b) == "string" and string.find(b, "error in __gc"))
|
||||||
|
|
||||||
end
|
end
|
||||||
print '+'
|
print '+'
|
||||||
@@ -422,7 +406,7 @@ else
|
|||||||
local u = u
|
local u = u
|
||||||
getmetatable(u).__gc = function (o)
|
getmetatable(u).__gc = function (o)
|
||||||
assert(a[o] == 10-s)
|
assert(a[o] == 10-s)
|
||||||
assert(a[10-s] == undef) -- udata already removed from weak table
|
assert(a[10-s] == nil) -- udata already removed from weak table
|
||||||
assert(getmetatable(o) == getmetatable(u))
|
assert(getmetatable(o) == getmetatable(u))
|
||||||
assert(getmetatable(o).a[o] == 10-s)
|
assert(getmetatable(o).a[o] == 10-s)
|
||||||
s=s+1
|
s=s+1
|
||||||
@@ -456,46 +440,19 @@ u, m = nil
|
|||||||
collectgarbage()
|
collectgarbage()
|
||||||
assert(m==10)
|
assert(m==10)
|
||||||
|
|
||||||
do -- tests for string keys in weak tables
|
|
||||||
collectgarbage(); collectgarbage()
|
|
||||||
local m = collectgarbage("count") -- current memory
|
|
||||||
local a = setmetatable({}, {__mode = "kv"})
|
|
||||||
a[string.rep("a", 2^22)] = 25 -- long string key -> number value
|
|
||||||
a[string.rep("b", 2^22)] = {} -- long string key -> collectable value
|
|
||||||
a[{}] = 14 -- collectable key
|
|
||||||
collectgarbage()
|
|
||||||
local k, v = next(a) -- string key with number value preserved
|
|
||||||
assert(k == string.rep("a", 2^22) and v == 25)
|
|
||||||
assert(next(a, k) == nil) -- everything else cleared
|
|
||||||
assert(a[string.rep("b", 2^22)] == undef)
|
|
||||||
a[k] = undef -- erase this last entry
|
|
||||||
k = nil
|
|
||||||
collectgarbage()
|
|
||||||
assert(next(a) == nil)
|
|
||||||
-- make sure will not try to compare with dead key
|
|
||||||
assert(a[string.rep("b", 100)] == undef)
|
|
||||||
assert(collectgarbage("count") <= m + 1) -- everything collected
|
|
||||||
end
|
|
||||||
|
|
||||||
|
|
||||||
-- errors during collection
|
-- errors during collection
|
||||||
if T then
|
u = setmetatable({}, {__gc = function () error "!!!" end})
|
||||||
warn("@store")
|
u = nil
|
||||||
u = setmetatable({}, {__gc = function () error "@expected error" end})
|
assert(not pcall(collectgarbage))
|
||||||
u = nil
|
|
||||||
collectgarbage()
|
|
||||||
assert(string.find(_WARN, "@expected error")); _WARN = false
|
|
||||||
warn("@normal")
|
|
||||||
end
|
|
||||||
|
|
||||||
|
|
||||||
if not _soft then
|
if not _soft then
|
||||||
print("long list")
|
print("deep structures")
|
||||||
local a = {}
|
local a = {}
|
||||||
for i = 1,200000 do
|
for i = 1,200000 do
|
||||||
a = {next = a}
|
a = {next = a}
|
||||||
end
|
end
|
||||||
a = nil
|
|
||||||
collectgarbage()
|
collectgarbage()
|
||||||
end
|
end
|
||||||
|
|
||||||
@@ -528,7 +485,7 @@ do
|
|||||||
local collected = false -- to detect collection
|
local collected = false -- to detect collection
|
||||||
collectgarbage(); collectgarbage("stop")
|
collectgarbage(); collectgarbage("stop")
|
||||||
do
|
do
|
||||||
local function f (param)
|
local function f (param)
|
||||||
;(function ()
|
;(function ()
|
||||||
assert(type(f) == 'function' and type(param) == 'thread')
|
assert(type(f) == 'function' and type(param) == 'thread')
|
||||||
param = {param, f}
|
param = {param, f}
|
||||||
@@ -539,7 +496,10 @@ do
|
|||||||
local co = coroutine.create(f)
|
local co = coroutine.create(f)
|
||||||
assert(coroutine.resume(co, co))
|
assert(coroutine.resume(co, co))
|
||||||
end
|
end
|
||||||
-- Now, thread and closure are not reachable any more.
|
-- Now, thread and closure are not reacheable any more;
|
||||||
|
-- two collections are needed to break cycle
|
||||||
|
collectgarbage()
|
||||||
|
assert(not collected)
|
||||||
collectgarbage()
|
collectgarbage()
|
||||||
assert(collected)
|
assert(collected)
|
||||||
collectgarbage("restart")
|
collectgarbage("restart")
|
||||||
@@ -549,13 +509,12 @@ end
|
|||||||
do
|
do
|
||||||
collectgarbage()
|
collectgarbage()
|
||||||
collectgarbage"stop"
|
collectgarbage"stop"
|
||||||
collectgarbage("step") -- steps should not unblock the collector
|
|
||||||
local x = gcinfo()
|
local x = gcinfo()
|
||||||
repeat
|
repeat
|
||||||
for i=1,1000 do _ENV.a = {} end -- no collection during the loop
|
for i=1,1000 do _ENV.a = {} end
|
||||||
|
collectgarbage("step", 0) -- steps should not unblock the collector
|
||||||
until gcinfo() > 2 * x
|
until gcinfo() > 2 * x
|
||||||
collectgarbage"restart"
|
collectgarbage"restart"
|
||||||
_ENV.a = nil
|
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
||||||
@@ -575,8 +534,8 @@ if T then -- tests for weird cases collecting upvalues
|
|||||||
-- create coroutine in a weak table, so it will never be marked
|
-- create coroutine in a weak table, so it will never be marked
|
||||||
t.co = coroutine.wrap(foo)
|
t.co = coroutine.wrap(foo)
|
||||||
local f = t.co() -- create function to access local 'a'
|
local f = t.co() -- create function to access local 'a'
|
||||||
T.gcstate("enteratomic") -- ensure all objects are traversed
|
T.gcstate("atomic") -- ensure all objects are traversed
|
||||||
assert(T.gcstate() == "enteratomic")
|
assert(T.gcstate() == "atomic")
|
||||||
assert(t.co() == 100) -- resume coroutine, creating new table for 'a'
|
assert(t.co() == 100) -- resume coroutine, creating new table for 'a'
|
||||||
assert(T.gccolor(t.co) == "white") -- thread was not traversed
|
assert(T.gccolor(t.co) == "white") -- thread was not traversed
|
||||||
T.gcstate("pause") -- collect thread, but should mark 'a' before that
|
T.gcstate("pause") -- collect thread, but should mark 'a' before that
|
||||||
@@ -584,18 +543,17 @@ if T then -- tests for weird cases collecting upvalues
|
|||||||
|
|
||||||
collectgarbage("restart")
|
collectgarbage("restart")
|
||||||
|
|
||||||
-- test barrier in sweep phase (backing userdata to gray)
|
-- test barrier in sweep phase (advance cleaning of upvalue to white)
|
||||||
local u = T.newuserdata(0, 1) -- create a userdata
|
local u = T.newuserdata(0) -- create a userdata
|
||||||
collectgarbage()
|
collectgarbage()
|
||||||
collectgarbage"stop"
|
collectgarbage"stop"
|
||||||
local a = {} -- avoid 'u' as first element in 'allgc'
|
T.gcstate"atomic"
|
||||||
T.gcstate"enteratomic"
|
|
||||||
T.gcstate"sweepallgc"
|
T.gcstate"sweepallgc"
|
||||||
local x = {}
|
local x = {}
|
||||||
assert(T.gccolor(u) == "black") -- userdata is "old" (black)
|
assert(T.gccolor(u) == "black") -- upvalue is "old" (black)
|
||||||
assert(T.gccolor(x) == "white") -- table is "new" (white)
|
assert(T.gccolor(x) == "white") -- table is "new" (white)
|
||||||
debug.setuservalue(u, x) -- trigger barrier
|
debug.setuservalue(u, x) -- trigger barrier
|
||||||
assert(T.gccolor(u) == "gray") -- userdata changed back to gray
|
assert(T.gccolor(u) == "white") -- upvalue changed to white
|
||||||
collectgarbage"restart"
|
collectgarbage"restart"
|
||||||
|
|
||||||
print"+"
|
print"+"
|
||||||
@@ -607,29 +565,14 @@ if T then
|
|||||||
collectgarbage("stop")
|
collectgarbage("stop")
|
||||||
local x = T.newuserdata(0)
|
local x = T.newuserdata(0)
|
||||||
local y = T.newuserdata(0)
|
local y = T.newuserdata(0)
|
||||||
debug.setmetatable(y, {__gc = nop}) -- bless the new udata before...
|
debug.setmetatable(y, {__gc = true}) -- bless the new udata before...
|
||||||
debug.setmetatable(x, {__gc = nop}) -- ...the old one
|
debug.setmetatable(x, {__gc = true}) -- ...the old one
|
||||||
assert(T.gccolor(y) == "white")
|
assert(T.gccolor(y) == "white")
|
||||||
T.checkmemory()
|
T.checkmemory()
|
||||||
collectgarbage("restart")
|
collectgarbage("restart")
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
||||||
if T then
|
|
||||||
collectgarbage("stop")
|
|
||||||
T.gcstate("pause")
|
|
||||||
local sup = {x = 0}
|
|
||||||
local a = setmetatable({}, {__newindex = sup})
|
|
||||||
T.gcstate("enteratomic")
|
|
||||||
assert(T.gccolor(sup) == "black")
|
|
||||||
a.x = {} -- should not break the invariant
|
|
||||||
assert(not (T.gccolor(sup) == "black" and T.gccolor(sup.x) == "white"))
|
|
||||||
T.gcstate("pause") -- complete the GC cycle
|
|
||||||
sup.x.y = 10
|
|
||||||
collectgarbage("restart")
|
|
||||||
end
|
|
||||||
|
|
||||||
|
|
||||||
if T then
|
if T then
|
||||||
print("emergency collections")
|
print("emergency collections")
|
||||||
collectgarbage()
|
collectgarbage()
|
||||||
@@ -649,7 +592,6 @@ if T then
|
|||||||
assert(T.totalmem("thread") == t + 1)
|
assert(T.totalmem("thread") == t + 1)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
||||||
-- create an object to be collected when state is closed
|
-- create an object to be collected when state is closed
|
||||||
do
|
do
|
||||||
local setmetatable,assert,type,print,getmetatable =
|
local setmetatable,assert,type,print,getmetatable =
|
||||||
@@ -659,7 +601,7 @@ do
|
|||||||
assert(getmetatable(o) == tt)
|
assert(getmetatable(o) == tt)
|
||||||
-- create new objects during GC
|
-- create new objects during GC
|
||||||
local a = 'xuxu'..(10+3)..'joao', {}
|
local a = 'xuxu'..(10+3)..'joao', {}
|
||||||
___Glob = o -- resurrect object!
|
___Glob = o -- ressurect object!
|
||||||
setmetatable({}, tt) -- creates a new one with same metatable
|
setmetatable({}, tt) -- creates a new one with same metatable
|
||||||
print(">>> closing state " .. "<<<\n")
|
print(">>> closing state " .. "<<<\n")
|
||||||
end
|
end
|
||||||
@@ -668,43 +610,15 @@ do
|
|||||||
end
|
end
|
||||||
|
|
||||||
-- create several objects to raise errors when collected while closing state
|
-- create several objects to raise errors when collected while closing state
|
||||||
if T then
|
do
|
||||||
local error, assert, find, warn = error, assert, string.find, warn
|
local mt = {__gc = function (o) return o + 1 end}
|
||||||
local n = 0
|
for i = 1,10 do
|
||||||
local lastmsg
|
|
||||||
local mt = {__gc = function (o)
|
|
||||||
n = n + 1
|
|
||||||
assert(n == o[1])
|
|
||||||
if n == 1 then
|
|
||||||
_WARN = false
|
|
||||||
elseif n == 2 then
|
|
||||||
assert(find(_WARN, "@expected warning"))
|
|
||||||
lastmsg = _WARN -- get message from previous error (first 'o')
|
|
||||||
else
|
|
||||||
assert(lastmsg == _WARN) -- subsequent error messages are equal
|
|
||||||
end
|
|
||||||
warn("@store"); _WARN = false
|
|
||||||
error"@expected warning"
|
|
||||||
end}
|
|
||||||
for i = 10, 1, -1 do
|
|
||||||
-- create object and preserve it until the end
|
-- create object and preserve it until the end
|
||||||
table.insert(___Glob, setmetatable({i}, mt))
|
table.insert(___Glob, setmetatable({}, mt))
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
-- just to make sure
|
-- just to make sure
|
||||||
assert(collectgarbage'isrunning')
|
assert(collectgarbage'isrunning')
|
||||||
|
|
||||||
do -- check that the collector is not reentrant in incremental mode
|
|
||||||
local res = true
|
|
||||||
setmetatable({}, {__gc = function ()
|
|
||||||
res = collectgarbage()
|
|
||||||
end})
|
|
||||||
collectgarbage()
|
|
||||||
assert(not res)
|
|
||||||
end
|
|
||||||
|
|
||||||
|
|
||||||
collectgarbage(oldmode)
|
|
||||||
|
|
||||||
print('OK')
|
print('OK')
|
||||||
|
|||||||
@@ -1,196 +0,0 @@
|
|||||||
-- $Id: testes/gengc.lua $
|
|
||||||
-- See Copyright Notice in file lua.h
|
|
||||||
|
|
||||||
print('testing generational garbage collection')
|
|
||||||
|
|
||||||
local debug = require"debug"
|
|
||||||
|
|
||||||
assert(collectgarbage("isrunning"))
|
|
||||||
|
|
||||||
collectgarbage()
|
|
||||||
|
|
||||||
local oldmode = collectgarbage("generational")
|
|
||||||
|
|
||||||
|
|
||||||
-- ensure that table barrier evolves correctly
|
|
||||||
do
|
|
||||||
local U = {}
|
|
||||||
-- full collection makes 'U' old
|
|
||||||
collectgarbage()
|
|
||||||
assert(not T or T.gcage(U) == "old")
|
|
||||||
|
|
||||||
-- U refers to a new table, so it becomes 'touched1'
|
|
||||||
U[1] = {x = {234}}
|
|
||||||
assert(not T or (T.gcage(U) == "touched1" and T.gcage(U[1]) == "new"))
|
|
||||||
|
|
||||||
-- both U and the table survive one more collection
|
|
||||||
collectgarbage("step")
|
|
||||||
assert(not T or (T.gcage(U) == "touched2" and T.gcage(U[1]) == "survival"))
|
|
||||||
|
|
||||||
-- both U and the table survive yet another collection
|
|
||||||
-- now everything is old
|
|
||||||
collectgarbage("step")
|
|
||||||
assert(not T or (T.gcage(U) == "old" and T.gcage(U[1]) == "old1"))
|
|
||||||
|
|
||||||
-- data was not corrupted
|
|
||||||
assert(U[1].x[1] == 234)
|
|
||||||
end
|
|
||||||
|
|
||||||
|
|
||||||
do
|
|
||||||
-- ensure that 'firstold1' is corrected when object is removed from
|
|
||||||
-- the 'allgc' list
|
|
||||||
local function foo () end
|
|
||||||
local old = {10}
|
|
||||||
collectgarbage() -- make 'old' old
|
|
||||||
assert(not T or T.gcage(old) == "old")
|
|
||||||
setmetatable(old, {}) -- new table becomes OLD0 (barrier)
|
|
||||||
assert(not T or T.gcage(getmetatable(old)) == "old0")
|
|
||||||
collectgarbage("step") -- new table becomes OLD1 and firstold1
|
|
||||||
assert(not T or T.gcage(getmetatable(old)) == "old1")
|
|
||||||
setmetatable(getmetatable(old), {__gc = foo}) -- get it out of allgc list
|
|
||||||
collectgarbage("step") -- should not seg. fault
|
|
||||||
end
|
|
||||||
|
|
||||||
|
|
||||||
do -- bug in 5.4.0
|
|
||||||
-- When an object aged OLD1 is finalized, it is moved from the list
|
|
||||||
-- 'finobj' to the *beginning* of the list 'allgc', but that part of the
|
|
||||||
-- list was not being visited by 'markold'.
|
|
||||||
local A = {}
|
|
||||||
A[1] = false -- old anchor for object
|
|
||||||
|
|
||||||
-- obj finalizer
|
|
||||||
local function gcf (obj)
|
|
||||||
A[1] = obj -- anchor object
|
|
||||||
assert(not T or T.gcage(obj) == "old1")
|
|
||||||
obj = nil -- remove it from the stack
|
|
||||||
collectgarbage("step") -- do a young collection
|
|
||||||
print(getmetatable(A[1]).x) -- metatable was collected
|
|
||||||
end
|
|
||||||
|
|
||||||
collectgarbage() -- make A old
|
|
||||||
local obj = {} -- create a new object
|
|
||||||
collectgarbage("step") -- make it a survival
|
|
||||||
assert(not T or T.gcage(obj) == "survival")
|
|
||||||
setmetatable(obj, {__gc = gcf, x = "+"}) -- create its metatable
|
|
||||||
assert(not T or T.gcage(getmetatable(obj)) == "new")
|
|
||||||
obj = nil -- clear object
|
|
||||||
collectgarbage("step") -- will call obj's finalizer
|
|
||||||
end
|
|
||||||
|
|
||||||
|
|
||||||
do -- another bug in 5.4.0
|
|
||||||
local old = {10}
|
|
||||||
collectgarbage() -- make 'old' old
|
|
||||||
local co = coroutine.create(
|
|
||||||
function ()
|
|
||||||
local x = nil
|
|
||||||
local f = function ()
|
|
||||||
return x[1]
|
|
||||||
end
|
|
||||||
x = coroutine.yield(f)
|
|
||||||
coroutine.yield()
|
|
||||||
end
|
|
||||||
)
|
|
||||||
local _, f = coroutine.resume(co) -- create closure over 'x' in coroutine
|
|
||||||
collectgarbage("step") -- make upvalue a survival
|
|
||||||
old[1] = {"hello"} -- 'old' go to grayagain as 'touched1'
|
|
||||||
coroutine.resume(co, {123}) -- its value will be new
|
|
||||||
co = nil
|
|
||||||
collectgarbage("step") -- hit the barrier
|
|
||||||
assert(f() == 123 and old[1][1] == "hello")
|
|
||||||
collectgarbage("step") -- run the collector once more
|
|
||||||
-- make sure old[1] was not collected
|
|
||||||
assert(f() == 123 and old[1][1] == "hello")
|
|
||||||
end
|
|
||||||
|
|
||||||
|
|
||||||
do -- bug introduced in commit 9cf3299fa
|
|
||||||
local t = setmetatable({}, {__mode = "kv"}) -- all-weak table
|
|
||||||
collectgarbage() -- full collection
|
|
||||||
assert(not T or T.gcage(t) == "old")
|
|
||||||
t[1] = {10}
|
|
||||||
assert(not T or (T.gcage(t) == "touched1" and T.gccolor(t) == "gray"))
|
|
||||||
collectgarbage("step") -- minor collection
|
|
||||||
assert(not T or (T.gcage(t) == "touched2" and T.gccolor(t) == "black"))
|
|
||||||
collectgarbage("step") -- minor collection
|
|
||||||
assert(not T or T.gcage(t) == "old") -- t should be black, but it was gray
|
|
||||||
t[1] = {10} -- no barrier here, so t was still old
|
|
||||||
collectgarbage("step") -- minor collection
|
|
||||||
-- t, being old, is ignored by the collection, so it is not cleared
|
|
||||||
assert(t[1] == nil) -- fails with the bug
|
|
||||||
end
|
|
||||||
|
|
||||||
|
|
||||||
if T == nil then
|
|
||||||
(Message or print)('\n >>> testC not active: \z
|
|
||||||
skipping some generational tests <<<\n')
|
|
||||||
print 'OK'
|
|
||||||
return
|
|
||||||
end
|
|
||||||
|
|
||||||
|
|
||||||
-- ensure that userdata barrier evolves correctly
|
|
||||||
do
|
|
||||||
local U = T.newuserdata(0, 1)
|
|
||||||
-- full collection makes 'U' old
|
|
||||||
collectgarbage()
|
|
||||||
assert(T.gcage(U) == "old")
|
|
||||||
|
|
||||||
-- U refers to a new table, so it becomes 'touched1'
|
|
||||||
debug.setuservalue(U, {x = {234}})
|
|
||||||
assert(T.gcage(U) == "touched1" and
|
|
||||||
T.gcage(debug.getuservalue(U)) == "new")
|
|
||||||
|
|
||||||
-- both U and the table survive one more collection
|
|
||||||
collectgarbage("step")
|
|
||||||
assert(T.gcage(U) == "touched2" and
|
|
||||||
T.gcage(debug.getuservalue(U)) == "survival")
|
|
||||||
|
|
||||||
-- both U and the table survive yet another collection
|
|
||||||
-- now everything is old
|
|
||||||
collectgarbage("step")
|
|
||||||
assert(T.gcage(U) == "old" and
|
|
||||||
T.gcage(debug.getuservalue(U)) == "old1")
|
|
||||||
|
|
||||||
-- data was not corrupted
|
|
||||||
assert(debug.getuservalue(U).x[1] == 234)
|
|
||||||
end
|
|
||||||
|
|
||||||
-- just to make sure
|
|
||||||
assert(collectgarbage'isrunning')
|
|
||||||
|
|
||||||
|
|
||||||
do print"testing stop-the-world collection"
|
|
||||||
local step = collectgarbage("param", "stepsize", 0);
|
|
||||||
collectgarbage("incremental")
|
|
||||||
assert(collectgarbage("param", "stepsize") == 0)
|
|
||||||
|
|
||||||
-- each step does a complete cycle
|
|
||||||
assert(collectgarbage("step"))
|
|
||||||
assert(collectgarbage("step"))
|
|
||||||
|
|
||||||
-- back to default value
|
|
||||||
collectgarbage("param", "stepsize", step);
|
|
||||||
assert(collectgarbage("param", "stepsize") == step)
|
|
||||||
end
|
|
||||||
|
|
||||||
|
|
||||||
if T then -- test GC parameter codification
|
|
||||||
for _, percentage in ipairs{5, 10, 12, 20, 50, 100, 200, 500} do
|
|
||||||
local param = T.codeparam(percentage) -- codify percentage
|
|
||||||
for _, value in ipairs{1, 2, 10, 100, 257, 1023, 6500, 100000} do
|
|
||||||
local exact = value*percentage // 100
|
|
||||||
local aprox = T.applyparam(param, value) -- apply percentage
|
|
||||||
-- difference is at most 10% (+1 compensates difference due to
|
|
||||||
-- rounding to integers)
|
|
||||||
assert(math.abs(aprox - exact) <= exact/10 + 1)
|
|
||||||
end
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
collectgarbage(oldmode)
|
|
||||||
|
|
||||||
print('OK')
|
|
||||||
|
|
||||||
+10
-255
@@ -1,11 +1,5 @@
|
|||||||
-- $Id: testes/goto.lua $
|
-- $Id: goto.lua,v 1.13 2016/11/07 13:11:28 roberto Exp $
|
||||||
-- See Copyright Notice in file lua.h
|
-- See Copyright Notice in file all.lua
|
||||||
|
|
||||||
global<const> require
|
|
||||||
global<const> print, load, assert, string, setmetatable
|
|
||||||
global<const> collectgarbage, error
|
|
||||||
|
|
||||||
print("testing goto and global declarations")
|
|
||||||
|
|
||||||
collectgarbage()
|
collectgarbage()
|
||||||
|
|
||||||
@@ -20,21 +14,17 @@ errmsg([[ do ::l1:: end goto l1; ]], "label 'l1'")
|
|||||||
|
|
||||||
-- repeated label
|
-- repeated label
|
||||||
errmsg([[ ::l1:: ::l1:: ]], "label 'l1'")
|
errmsg([[ ::l1:: ::l1:: ]], "label 'l1'")
|
||||||
errmsg([[ ::l1:: do ::l1:: end]], "label 'l1'")
|
|
||||||
|
|
||||||
|
|
||||||
|
-- undefined label
|
||||||
|
errmsg([[ goto l1; local aa ::l1:: ::l2:: print(3) ]], "local 'aa'")
|
||||||
|
|
||||||
-- jumping over variable declaration
|
-- jumping over variable definition
|
||||||
errmsg([[ goto l1; local aa ::l1:: ::l2:: print(3) ]], "scope of 'aa'")
|
|
||||||
|
|
||||||
errmsg([[ goto l2; global *; ::l1:: ::l2:: print(3) ]], "scope of '*'")
|
|
||||||
|
|
||||||
errmsg([[
|
errmsg([[
|
||||||
do local bb, cc; goto l1; end
|
do local bb, cc; goto l1; end
|
||||||
local aa
|
local aa
|
||||||
::l1:: print(3)
|
::l1:: print(3)
|
||||||
]], "scope of 'aa'")
|
]], "local 'aa'")
|
||||||
|
|
||||||
|
|
||||||
-- jumping into a block
|
-- jumping into a block
|
||||||
errmsg([[ do ::l1:: end goto l1 ]], "label 'l1'")
|
errmsg([[ do ::l1:: end goto l1 ]], "label 'l1'")
|
||||||
@@ -47,7 +37,7 @@ errmsg([[
|
|||||||
local xuxu = 10
|
local xuxu = 10
|
||||||
::cont::
|
::cont::
|
||||||
until xuxu < x
|
until xuxu < x
|
||||||
]], "scope of 'xuxu'")
|
]], "local 'xuxu'")
|
||||||
|
|
||||||
-- simple gotos
|
-- simple gotos
|
||||||
local x
|
local x
|
||||||
@@ -77,6 +67,8 @@ do
|
|||||||
assert(assert(load(prog))() == 31)
|
assert(assert(load(prog))() == 31)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
-- goto to correct label when nested
|
||||||
|
do goto l3; ::l3:: end -- does not loop jumping to previous label 'l3'
|
||||||
|
|
||||||
-- ok to jump over local dec. to end of block
|
-- ok to jump over local dec. to end of block
|
||||||
do
|
do
|
||||||
@@ -136,30 +128,6 @@ do -- bug in 5.2 -> 5.3.2
|
|||||||
assert(x == 2 and y == true)
|
assert(x == 2 and y == true)
|
||||||
end
|
end
|
||||||
|
|
||||||
-- bug in 5.3
|
|
||||||
do
|
|
||||||
local first = true
|
|
||||||
local a = false
|
|
||||||
if true then
|
|
||||||
goto LBL
|
|
||||||
::loop::
|
|
||||||
a = true
|
|
||||||
::LBL::
|
|
||||||
if first then
|
|
||||||
first = false
|
|
||||||
goto loop
|
|
||||||
end
|
|
||||||
end
|
|
||||||
assert(a)
|
|
||||||
end
|
|
||||||
|
|
||||||
do -- compiling infinite loops
|
|
||||||
goto escape -- do not run the infinite loops
|
|
||||||
::a:: goto a
|
|
||||||
::b:: goto c
|
|
||||||
::c:: goto b
|
|
||||||
end
|
|
||||||
::escape::
|
|
||||||
--------------------------------------------------------------------------------
|
--------------------------------------------------------------------------------
|
||||||
-- testing closing of upvalues
|
-- testing closing of upvalues
|
||||||
|
|
||||||
@@ -258,220 +226,7 @@ assert(testG(2) == "2")
|
|||||||
assert(testG(3) == "3")
|
assert(testG(3) == "3")
|
||||||
assert(testG(4) == 5)
|
assert(testG(4) == 5)
|
||||||
assert(testG(5) == 10)
|
assert(testG(5) == 10)
|
||||||
|
--------------------------------------------------------------------------------
|
||||||
|
|
||||||
do -- test goto's around to-be-closed variable
|
|
||||||
|
|
||||||
global *
|
|
||||||
|
|
||||||
-- set 'var' and return an object that will reset 'var' when
|
|
||||||
-- it goes out of scope
|
|
||||||
local function newobj (var)
|
|
||||||
_ENV[var] = true
|
|
||||||
return setmetatable({}, {__close = function ()
|
|
||||||
_ENV[var] = nil
|
|
||||||
end})
|
|
||||||
end
|
|
||||||
|
|
||||||
goto L1
|
|
||||||
|
|
||||||
::L4:: assert(not varX); goto L5 -- varX dead here
|
|
||||||
|
|
||||||
::L1::
|
|
||||||
local varX <close> = newobj("X")
|
|
||||||
assert(varX); goto L2 -- varX alive here
|
|
||||||
|
|
||||||
::L3::
|
|
||||||
assert(varX); goto L4 -- varX alive here
|
|
||||||
|
|
||||||
::L2:: assert(varX); goto L3 -- varX alive here
|
|
||||||
|
|
||||||
::L5:: -- return
|
|
||||||
end
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
foo()
|
|
||||||
--------------------------------------------------------------------------
|
|
||||||
|
|
||||||
-- check for compilation errors
|
|
||||||
local function checkerr (code, err)
|
|
||||||
local st, msg = load(code)
|
|
||||||
assert(not st and string.find(msg, err))
|
|
||||||
end
|
|
||||||
|
|
||||||
do
|
|
||||||
global T<const>
|
|
||||||
|
|
||||||
-- globals must be declared, after a global declaration
|
|
||||||
checkerr("global none; X = 1", "variable 'X'")
|
|
||||||
checkerr("global none; function XX() end", "variable 'XX'")
|
|
||||||
|
|
||||||
-- global variables cannot be to-be-closed
|
|
||||||
checkerr("global X<close>", "cannot be")
|
|
||||||
checkerr("global <close> *", "cannot be")
|
|
||||||
|
|
||||||
do
|
|
||||||
local X = 10
|
|
||||||
do global X; X = 20 end
|
|
||||||
assert(X == 10) -- local X
|
|
||||||
end
|
|
||||||
assert(_ENV.X == 20) -- global X
|
|
||||||
|
|
||||||
-- '_ENV' cannot be global
|
|
||||||
checkerr("global _ENV, a; a = 10", "variable 'a'")
|
|
||||||
|
|
||||||
-- global declarations inside functions
|
|
||||||
checkerr([[
|
|
||||||
global none
|
|
||||||
local function foo () XXX = 1 end --< ERROR]], "variable 'XXX'")
|
|
||||||
|
|
||||||
if not T then -- when not in "test mode", "global" isn't reserved
|
|
||||||
assert(load("global = 1; return global")() == 1)
|
|
||||||
print " ('global' is not a reserved word)"
|
|
||||||
else
|
|
||||||
-- "global" reserved, cannot be used as a variable
|
|
||||||
assert(not load("global = 1; return global"))
|
|
||||||
end
|
|
||||||
|
|
||||||
local foo = 20
|
|
||||||
do
|
|
||||||
global function foo (x)
|
|
||||||
if x == 0 then return 1 else return 2 * foo(x - 1) end
|
|
||||||
end
|
|
||||||
assert(foo == _ENV.foo and foo(4) == 16)
|
|
||||||
end
|
|
||||||
assert(_ENV.foo(4) == 16)
|
|
||||||
assert(foo == 20) -- local one is in context here
|
|
||||||
|
|
||||||
do
|
|
||||||
global foo;
|
|
||||||
function foo (x) return end -- Ok after declaration
|
|
||||||
end
|
|
||||||
|
|
||||||
checkerr([[
|
|
||||||
global<const> foo;
|
|
||||||
function foo (x) return end -- ERROR: foo is read-only
|
|
||||||
]], "assign to const variable 'foo'")
|
|
||||||
|
|
||||||
checkerr([[
|
|
||||||
global foo <const>;
|
|
||||||
function foo (x) -- ERROR: foo is read-only
|
|
||||||
return
|
|
||||||
end
|
|
||||||
]], "%:2%:") -- correct line in error message
|
|
||||||
|
|
||||||
checkerr([[
|
|
||||||
global<const> *;
|
|
||||||
print(X) -- Ok to use
|
|
||||||
Y = 1 -- ERROR
|
|
||||||
]], "assign to const variable 'Y'")
|
|
||||||
|
|
||||||
checkerr([[
|
|
||||||
global *;
|
|
||||||
Y = X -- Ok to use
|
|
||||||
global<const> *;
|
|
||||||
Y = 1 -- ERROR
|
|
||||||
]], "assign to const variable 'Y'")
|
|
||||||
|
|
||||||
global *
|
|
||||||
Y = 10
|
|
||||||
assert(_ENV.Y == 10)
|
|
||||||
global<const> *
|
|
||||||
local x = Y
|
|
||||||
global *
|
|
||||||
Y = x + Y
|
|
||||||
assert(_ENV.Y == 20)
|
|
||||||
Y = nil
|
|
||||||
end
|
|
||||||
|
|
||||||
|
|
||||||
do -- Ok to declare hundreds of globals
|
|
||||||
global table
|
|
||||||
local code = {}
|
|
||||||
for i = 1, 1000 do
|
|
||||||
code[#code + 1] = ";global x" .. i
|
|
||||||
end
|
|
||||||
code[#code + 1] = "; return x990"
|
|
||||||
code = table.concat(code)
|
|
||||||
_ENV.x990 = 11
|
|
||||||
assert(load(code)() == 11)
|
|
||||||
_ENV.x990 = nil
|
|
||||||
end
|
|
||||||
|
|
||||||
do -- mixing lots of global/local declarations
|
|
||||||
global table
|
|
||||||
local code = {}
|
|
||||||
for i = 1, 200 do
|
|
||||||
code[#code + 1] = ";global x" .. i
|
|
||||||
code[#code + 1] = ";local y" .. i .. "=" .. (2*i)
|
|
||||||
end
|
|
||||||
code[#code + 1] = "; return x200 + y200"
|
|
||||||
code = table.concat(code)
|
|
||||||
_ENV.x200 = 11
|
|
||||||
assert(assert(load(code))() == 2*200 + 11)
|
|
||||||
_ENV.x200 = nil
|
|
||||||
end
|
|
||||||
|
|
||||||
do print "testing initialization in global declarations"
|
|
||||||
global<const> a, b, c = 10, 20, 30
|
|
||||||
assert(_ENV.a == 10 and b == 20 and c == 30)
|
|
||||||
_ENV.a = nil; _ENV.b = nil; _ENV.c = nil;
|
|
||||||
|
|
||||||
global<const> a, b, c = 10
|
|
||||||
assert(_ENV.a == 10 and b == nil and c == nil)
|
|
||||||
_ENV.a = nil; _ENV.b = nil; _ENV.c = nil;
|
|
||||||
|
|
||||||
global table
|
|
||||||
global a, b, c, d = table.unpack{1, 2, 3, 6, 5}
|
|
||||||
assert(_ENV.a == 1 and b == 2 and c == 3 and d == 6)
|
|
||||||
a = nil; b = nil; c = nil; d = nil
|
|
||||||
|
|
||||||
local a, b = 100, 200
|
|
||||||
do
|
|
||||||
global a, b = a, b
|
|
||||||
end
|
|
||||||
assert(_ENV.a == 100 and _ENV.b == 200)
|
|
||||||
_ENV.a = nil; _ENV.b = nil
|
|
||||||
|
|
||||||
|
|
||||||
assert(_ENV.a == nil and _ENV.b == nil and _ENV.c == nil and _ENV.d == nil)
|
|
||||||
end
|
|
||||||
|
|
||||||
do
|
|
||||||
global table, string
|
|
||||||
-- global initialization when names don't fit in K
|
|
||||||
|
|
||||||
-- to fill constant table
|
|
||||||
local code = {}
|
|
||||||
for i = 1, 300 do code[i] = "'" .. i .. "'" end
|
|
||||||
code = table.concat(code, ",")
|
|
||||||
code = string.format([[
|
|
||||||
return function (_ENV)
|
|
||||||
local dummy = {%s} -- fill initial positions in constant table,
|
|
||||||
-- so that initialization must use registers for global names
|
|
||||||
global a, b, c = 10, 20, 30
|
|
||||||
end]], code)
|
|
||||||
|
|
||||||
local fun = assert(load(code))()
|
|
||||||
|
|
||||||
local env = {}
|
|
||||||
fun(env)
|
|
||||||
assert(env.a == 10 and env.b == 20 and env.c == 30)
|
|
||||||
end
|
|
||||||
|
|
||||||
|
|
||||||
do -- testing global redefinitions
|
|
||||||
-- cannot use 'checkerr' as errors are not compile time
|
|
||||||
global pcall
|
|
||||||
local f = assert(load("global print = 10"))
|
|
||||||
local st, msg = pcall(f)
|
|
||||||
assert(string.find(msg, "global 'print' already defined"))
|
|
||||||
|
|
||||||
local f = assert(load("local _ENV = {AA = false}; global AA = 10"))
|
|
||||||
local st, msg = pcall(f)
|
|
||||||
assert(string.find(msg, "global 'AA' already defined"))
|
|
||||||
|
|
||||||
end
|
|
||||||
|
|
||||||
print'OK'
|
print'OK'
|
||||||
|
|
||||||
|
|||||||
+37
-140
@@ -1,34 +1,30 @@
|
|||||||
-- $Id: testes/heavy.lua,v $
|
-- $Id: heavy.lua,v 1.4 2016/11/07 13:11:28 roberto Exp $
|
||||||
-- See Copyright Notice in file lua.h
|
-- See Copyright Notice in file all.lua
|
||||||
|
|
||||||
global <const> *
|
print("creating a string too long")
|
||||||
|
do
|
||||||
local function teststring ()
|
local st, msg = pcall(function ()
|
||||||
print("creating a string too long")
|
|
||||||
do
|
|
||||||
local a = "x"
|
local a = "x"
|
||||||
local st, msg = pcall(function ()
|
while true do
|
||||||
while true do
|
a = a .. a.. a.. a.. a.. a.. a.. a.. a.. a
|
||||||
a = a .. a.. a.. a.. a.. a.. a.. a.. a.. a
|
.. a .. a.. a.. a.. a.. a.. a.. a.. a.. a
|
||||||
.. a .. a.. a.. a.. a.. a.. a.. a.. a.. a
|
.. a .. a.. a.. a.. a.. a.. a.. a.. a.. a
|
||||||
.. a .. a.. a.. a.. a.. a.. a.. a.. a.. a
|
.. a .. a.. a.. a.. a.. a.. a.. a.. a.. a
|
||||||
.. a .. a.. a.. a.. a.. a.. a.. a.. a.. a
|
.. a .. a.. a.. a.. a.. a.. a.. a.. a.. a
|
||||||
.. a .. a.. a.. a.. a.. a.. a.. a.. a.. a
|
.. a .. a.. a.. a.. a.. a.. a.. a.. a.. a
|
||||||
.. a .. a.. a.. a.. a.. a.. a.. a.. a.. a
|
.. a .. a.. a.. a.. a.. a.. a.. a.. a.. a
|
||||||
.. a .. a.. a.. a.. a.. a.. a.. a.. a.. a
|
.. a .. a.. a.. a.. a.. a.. a.. a.. a.. a
|
||||||
.. a .. a.. a.. a.. a.. a.. a.. a.. a.. a
|
.. a .. a.. a.. a.. a.. a.. a.. a.. a.. a
|
||||||
.. a .. a.. a.. a.. a.. a.. a.. a.. a.. a
|
.. a .. a.. a.. a.. a.. a.. a.. a.. a.. a
|
||||||
.. a .. a.. a.. a.. a.. a.. a.. a.. a.. a
|
print(string.format("string with %d bytes", #a))
|
||||||
print(string.format("string with %d bytes", #a))
|
end
|
||||||
end
|
end)
|
||||||
end)
|
assert(not st and
|
||||||
assert(not st and
|
(string.find(msg, "string length overflow") or
|
||||||
(string.find(msg, "string length overflow") or
|
string.find(msg, "not enough memory")))
|
||||||
string.find(msg, "not enough memory")))
|
|
||||||
print("string length overflow with " .. #a * 100)
|
|
||||||
end
|
|
||||||
print('+')
|
|
||||||
end
|
end
|
||||||
|
print('+')
|
||||||
|
|
||||||
|
|
||||||
local function loadrep (x, what)
|
local function loadrep (x, what)
|
||||||
local p = 1<<20
|
local p = 1<<20
|
||||||
@@ -37,139 +33,40 @@ local function loadrep (x, what)
|
|||||||
local function f()
|
local function f()
|
||||||
count = count + p
|
count = count + p
|
||||||
if count % (0x80*p) == 0 then
|
if count % (0x80*p) == 0 then
|
||||||
io.stderr:write("(", count // 2^20, " M)")
|
io.stderr:write("(", string.format("0x%x", count), ")")
|
||||||
end
|
end
|
||||||
return s
|
return s
|
||||||
end
|
end
|
||||||
local st, msg = load(f, "=big")
|
local st, msg = load(f, "=big")
|
||||||
print("\nmemory: ", collectgarbage'count' * 1024)
|
print(string.format("\ntotal: 0x%x %s", count, what))
|
||||||
msg = string.match(msg, "^[^\n]+") -- get only first line
|
|
||||||
print(string.format("total: 0x%x %s ('%s')", count, what, msg))
|
|
||||||
return st, msg
|
return st, msg
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
||||||
local function controlstruct ()
|
print("loading chunk with too many lines")
|
||||||
print("control structure too long")
|
do
|
||||||
local lim = ((1 << 24) - 2) // 4
|
|
||||||
local s = string.rep("a = a + 1\n", lim)
|
|
||||||
s = "while true do " .. s .. "end"
|
|
||||||
assert(load(s))
|
|
||||||
print("ok with " .. lim .. " lines")
|
|
||||||
lim = lim + 3
|
|
||||||
s = string.rep("a = a + 1\n", lim)
|
|
||||||
s = "while true do " .. s .. "end"
|
|
||||||
local st, msg = load(s)
|
|
||||||
assert(not st and string.find(msg, "too long"))
|
|
||||||
print(msg)
|
|
||||||
end
|
|
||||||
|
|
||||||
|
|
||||||
local function manylines ()
|
|
||||||
print("loading chunk with too many lines")
|
|
||||||
local st, msg = loadrep("\n", "lines")
|
local st, msg = loadrep("\n", "lines")
|
||||||
assert(not st and string.find(msg, "too many lines"))
|
assert(not st and string.find(msg, "too many lines"))
|
||||||
print('+')
|
|
||||||
end
|
end
|
||||||
|
print('+')
|
||||||
|
|
||||||
|
|
||||||
local function hugeid ()
|
print("loading chunk with huge identifier")
|
||||||
print("loading chunk with huge identifier")
|
do
|
||||||
local st, msg = loadrep("a", "chars")
|
local st, msg = loadrep("a", "chars")
|
||||||
assert(not st and
|
assert(not st and
|
||||||
(string.find(msg, "lexical element too long") or
|
(string.find(msg, "lexical element too long") or
|
||||||
string.find(msg, "not enough memory")))
|
string.find(msg, "not enough memory")))
|
||||||
print('+')
|
|
||||||
end
|
end
|
||||||
|
print('+')
|
||||||
|
|
||||||
local function toomanyinst ()
|
|
||||||
print("loading chunk with too many instructions")
|
print("loading chunk with too many instructions")
|
||||||
|
do
|
||||||
local st, msg = loadrep("a = 10; ", "instructions")
|
local st, msg = loadrep("a = 10; ", "instructions")
|
||||||
print('+')
|
print(st, msg)
|
||||||
end
|
end
|
||||||
|
print('+')
|
||||||
|
|
||||||
|
|
||||||
local function loadrepfunc (prefix, f)
|
|
||||||
local count = -1
|
|
||||||
local function aux ()
|
|
||||||
count = count + 1
|
|
||||||
if count == 0 then
|
|
||||||
return prefix
|
|
||||||
else
|
|
||||||
if count % (0x100000) == 0 then
|
|
||||||
io.stderr:write("(", count // 2^20, " M)")
|
|
||||||
end
|
|
||||||
return f(count)
|
|
||||||
end
|
|
||||||
end
|
|
||||||
local st, msg = load(aux, "k")
|
|
||||||
print("\nmemory: ", collectgarbage'count' * 1024)
|
|
||||||
msg = string.match(msg, "^[^\n]+") -- get only first line
|
|
||||||
print("expected error: ", msg)
|
|
||||||
end
|
|
||||||
|
|
||||||
|
|
||||||
local function toomanyconst ()
|
|
||||||
print("loading function with too many constants")
|
|
||||||
loadrepfunc("function foo () return {0,",
|
|
||||||
function (n)
|
|
||||||
-- convert 'n' to a string in the format [["...",]],
|
|
||||||
-- where '...' is a kind of number in base 128
|
|
||||||
-- (in a range that does not include either the double quote
|
|
||||||
-- and the escape.)
|
|
||||||
return string.char(34,
|
|
||||||
((n // 128^0) & 127) + 128,
|
|
||||||
((n // 128^1) & 127) + 128,
|
|
||||||
((n // 128^2) & 127) + 128,
|
|
||||||
((n // 128^3) & 127) + 128,
|
|
||||||
((n // 128^4) & 127) + 128,
|
|
||||||
34, 44)
|
|
||||||
end)
|
|
||||||
end
|
|
||||||
|
|
||||||
|
|
||||||
local function toomanystr ()
|
|
||||||
local a = {}
|
|
||||||
local st, msg = pcall(function ()
|
|
||||||
for i = 1, math.huge do
|
|
||||||
if i % (0x100000) == 0 then
|
|
||||||
io.stderr:write("(", i // 2^20, " M)")
|
|
||||||
end
|
|
||||||
a[i] = string.pack("I", i)
|
|
||||||
end
|
|
||||||
end)
|
|
||||||
local size = #a
|
|
||||||
a = collectgarbage'count'
|
|
||||||
print("\nmemory:", a * 1024)
|
|
||||||
print("expected error:", msg)
|
|
||||||
print("size:", size)
|
|
||||||
end
|
|
||||||
|
|
||||||
|
|
||||||
local function toomanyidx ()
|
|
||||||
local a = {}
|
|
||||||
local st, msg = pcall(function ()
|
|
||||||
for i = 1, math.huge do
|
|
||||||
if i % (0x100000) == 0 then
|
|
||||||
io.stderr:write("(", i // 2^20, " M)")
|
|
||||||
end
|
|
||||||
a[i] = i
|
|
||||||
end
|
|
||||||
end)
|
|
||||||
print("\nmemory: ", collectgarbage'count' * 1024)
|
|
||||||
print("expected error: ", msg)
|
|
||||||
print("size:", #a)
|
|
||||||
end
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
-- teststring()
|
|
||||||
-- controlstruct()
|
|
||||||
-- manylines()
|
|
||||||
-- hugeid()
|
|
||||||
-- toomanyinst()
|
|
||||||
-- toomanyconst()
|
|
||||||
-- toomanystr()
|
|
||||||
toomanyidx()
|
|
||||||
|
|
||||||
print "OK"
|
print "OK"
|
||||||
|
|||||||
@@ -1,2 +0,0 @@
|
|||||||
# This is a dummy file just to make git keep the otherwise empty
|
|
||||||
# directory 'P1' in the repository.
|
|
||||||
+1
-1
@@ -1,7 +1,7 @@
|
|||||||
#include "lua.h"
|
#include "lua.h"
|
||||||
|
|
||||||
/* function from lib1.c */
|
/* function from lib1.c */
|
||||||
LUAMOD_API int lib1_export (lua_State *L);
|
int lib1_export (lua_State *L);
|
||||||
|
|
||||||
LUAMOD_API int luaopen_lib11 (lua_State *L) {
|
LUAMOD_API int luaopen_lib11 (lua_State *L) {
|
||||||
return lib1_export(L);
|
return lib1_export(L);
|
||||||
|
|||||||
@@ -1,76 +0,0 @@
|
|||||||
/* implementation for lib2-v2 */
|
|
||||||
|
|
||||||
#include <string.h>
|
|
||||||
|
|
||||||
#include "lua.h"
|
|
||||||
#include "lauxlib.h"
|
|
||||||
|
|
||||||
static int id (lua_State *L) {
|
|
||||||
lua_pushboolean(L, 1);
|
|
||||||
lua_insert(L, 1);
|
|
||||||
return lua_gettop(L);
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
struct STR {
|
|
||||||
void *ud;
|
|
||||||
lua_Alloc allocf;
|
|
||||||
};
|
|
||||||
|
|
||||||
|
|
||||||
static void *t_freestr (void *ud, void *ptr, size_t osize, size_t nsize) {
|
|
||||||
struct STR *blk = (struct STR*)ptr - 1;
|
|
||||||
blk->allocf(blk->ud, blk, sizeof(struct STR) + osize, 0);
|
|
||||||
return NULL;
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
static int newstr (lua_State *L) {
|
|
||||||
size_t len;
|
|
||||||
const char *str = luaL_checklstring(L, 1, &len);
|
|
||||||
void *ud;
|
|
||||||
lua_Alloc allocf = lua_getallocf(L, &ud);
|
|
||||||
struct STR *blk = (struct STR*)allocf(ud, NULL, 0,
|
|
||||||
len + 1 + sizeof(struct STR));
|
|
||||||
if (blk == NULL) { /* allocation error? */
|
|
||||||
lua_pushliteral(L, "not enough memory");
|
|
||||||
lua_error(L); /* raise a memory error */
|
|
||||||
}
|
|
||||||
blk->ud = ud; blk->allocf = allocf;
|
|
||||||
memcpy(blk + 1, str, len + 1);
|
|
||||||
lua_pushexternalstring(L, (char *)(blk + 1), len, t_freestr, L);
|
|
||||||
return 1;
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
/*
|
|
||||||
** Create an external string and keep it in the registry, so that it
|
|
||||||
** will test that the library code is still available (to deallocate
|
|
||||||
** this string) when closing the state.
|
|
||||||
*/
|
|
||||||
static void initstr (lua_State *L) {
|
|
||||||
lua_pushcfunction(L, newstr);
|
|
||||||
lua_pushstring(L,
|
|
||||||
"012345678901234567890123456789012345678901234567890123456789");
|
|
||||||
lua_call(L, 1, 1); /* call newstr("0123...") */
|
|
||||||
luaL_ref(L, LUA_REGISTRYINDEX); /* keep string in the registry */
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
static const struct luaL_Reg funcs[] = {
|
|
||||||
{"id", id},
|
|
||||||
{"newstr", newstr},
|
|
||||||
{NULL, NULL}
|
|
||||||
};
|
|
||||||
|
|
||||||
|
|
||||||
LUAMOD_API int luaopen_lib2 (lua_State *L) {
|
|
||||||
lua_settop(L, 2);
|
|
||||||
lua_setglobal(L, "y"); /* y gets 2nd parameter */
|
|
||||||
lua_setglobal(L, "x"); /* x gets 1st parameter */
|
|
||||||
initstr(L);
|
|
||||||
luaL_newlib(L, funcs);
|
|
||||||
return 1;
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
@@ -5,23 +5,22 @@ LUA_DIR = ../../
|
|||||||
CC = gcc
|
CC = gcc
|
||||||
|
|
||||||
# compilation should generate Dynamic-Link Libraries
|
# compilation should generate Dynamic-Link Libraries
|
||||||
CFLAGS = -Wall -O2 -I$(LUA_DIR) -fPIC -shared
|
CFLAGS = -Wall -std=gnu99 -O2 -I$(LUA_DIR) -fPIC -shared
|
||||||
|
|
||||||
# libraries used by the tests
|
# libraries used by the tests
|
||||||
all: lib1.so lib11.so lib2.so lib21.so lib2-v2.so
|
all: lib1.so lib11.so lib2.so lib21.so lib2-v2.so
|
||||||
touch all
|
|
||||||
|
|
||||||
lib1.so: lib1.c $(LUA_DIR)/luaconf.h $(LUA_DIR)/lua.h
|
lib1.so: lib1.c
|
||||||
$(CC) $(CFLAGS) -o lib1.so lib1.c
|
$(CC) $(CFLAGS) -o lib1.so lib1.c
|
||||||
|
|
||||||
lib11.so: lib11.c $(LUA_DIR)/luaconf.h $(LUA_DIR)/lua.h
|
lib11.so: lib11.c
|
||||||
$(CC) $(CFLAGS) -o lib11.so lib11.c
|
$(CC) $(CFLAGS) -o lib11.so lib11.c
|
||||||
|
|
||||||
lib2.so: lib2.c $(LUA_DIR)/luaconf.h $(LUA_DIR)/lua.h
|
lib2.so: lib2.c
|
||||||
$(CC) $(CFLAGS) -o lib2.so lib2.c
|
$(CC) $(CFLAGS) -o lib2.so lib2.c
|
||||||
|
|
||||||
lib21.so: lib21.c $(LUA_DIR)/luaconf.h $(LUA_DIR)/lua.h
|
lib21.so: lib21.c
|
||||||
$(CC) $(CFLAGS) -o lib21.so lib21.c
|
$(CC) $(CFLAGS) -o lib21.so lib21.c
|
||||||
|
|
||||||
lib2-v2.so: lib21.c $(LUA_DIR)/luaconf.h $(LUA_DIR)/lua.h
|
lib2-v2.so: lib2.so
|
||||||
$(CC) $(CFLAGS) -o lib2-v2.so lib22.c
|
mv lib2.so ./lib2-v2.so
|
||||||
|
|||||||
+24
-67
@@ -1,10 +1,8 @@
|
|||||||
-- $Id: testes/literals.lua $
|
-- $Id: literals.lua,v 1.36 2016/11/07 13:11:28 roberto Exp $
|
||||||
-- See Copyright Notice in file lua.h
|
-- See Copyright Notice in file all.lua
|
||||||
|
|
||||||
print('testing scanner')
|
print('testing scanner')
|
||||||
|
|
||||||
global <const> *
|
|
||||||
|
|
||||||
local debug = require "debug"
|
local debug = require "debug"
|
||||||
|
|
||||||
|
|
||||||
@@ -12,7 +10,6 @@ local function dostring (x) return assert(load(x), "")() end
|
|||||||
|
|
||||||
dostring("x \v\f = \t\r 'a\0a' \v\f\f")
|
dostring("x \v\f = \t\r 'a\0a' \v\f\f")
|
||||||
assert(x == 'a\0a' and string.len(x) == 3)
|
assert(x == 'a\0a' and string.len(x) == 3)
|
||||||
_G.x = nil
|
|
||||||
|
|
||||||
-- escape sequences
|
-- escape sequences
|
||||||
assert('\n\"\'\\' == [[
|
assert('\n\"\'\\' == [[
|
||||||
@@ -59,23 +56,16 @@ assert("abc\z
|
|||||||
assert("\u{0}\u{00000000}\x00\0" == string.char(0, 0, 0, 0))
|
assert("\u{0}\u{00000000}\x00\0" == string.char(0, 0, 0, 0))
|
||||||
|
|
||||||
-- limits for 1-byte sequences
|
-- limits for 1-byte sequences
|
||||||
assert("\u{0}\u{7F}" == "\x00\x7F")
|
assert("\u{0}\u{7F}" == "\x00\z\x7F")
|
||||||
|
|
||||||
-- limits for 2-byte sequences
|
-- limits for 2-byte sequences
|
||||||
assert("\u{80}\u{7FF}" == "\xC2\x80\xDF\xBF")
|
assert("\u{80}\u{7FF}" == "\xC2\x80\z\xDF\xBF")
|
||||||
|
|
||||||
-- limits for 3-byte sequences
|
-- limits for 3-byte sequences
|
||||||
assert("\u{800}\u{FFFF}" == "\xE0\xA0\x80\xEF\xBF\xBF")
|
assert("\u{800}\u{FFFF}" == "\xE0\xA0\x80\z\xEF\xBF\xBF")
|
||||||
|
|
||||||
-- limits for 4-byte sequences
|
-- limits for 4-byte sequences
|
||||||
assert("\u{10000}\u{1FFFFF}" == "\xF0\x90\x80\x80\xF7\xBF\xBF\xBF")
|
assert("\u{10000}\u{10FFFF}" == "\xF0\x90\x80\x80\z\xF4\x8F\xBF\xBF")
|
||||||
|
|
||||||
-- limits for 5-byte sequences
|
|
||||||
assert("\u{200000}\u{3FFFFFF}" == "\xF8\x88\x80\x80\x80\xFB\xBF\xBF\xBF\xBF")
|
|
||||||
|
|
||||||
-- limits for 6-byte sequences
|
|
||||||
assert("\u{4000000}\u{7FFFFFFF}" ==
|
|
||||||
"\xFC\x84\x80\x80\x80\x80\xFD\xBF\xBF\xBF\xBF\xBF")
|
|
||||||
|
|
||||||
|
|
||||||
-- Error in escape sequences
|
-- Error in escape sequences
|
||||||
@@ -104,7 +94,7 @@ lexerror([["xyz\300"]], [[\300"]])
|
|||||||
lexerror([[" \256"]], [[\256"]])
|
lexerror([[" \256"]], [[\256"]])
|
||||||
|
|
||||||
-- errors in UTF-8 sequences
|
-- errors in UTF-8 sequences
|
||||||
lexerror([["abc\u{100000000}"]], [[abc\u{100000000]]) -- too large
|
lexerror([["abc\u{110000}"]], [[abc\u{110000]]) -- too large
|
||||||
lexerror([["abc\u11r"]], [[abc\u1]]) -- missing '{'
|
lexerror([["abc\u11r"]], [[abc\u1]]) -- missing '{'
|
||||||
lexerror([["abc\u"]], [[abc\u"]]) -- missing '{'
|
lexerror([["abc\u"]], [[abc\u"]]) -- missing '{'
|
||||||
lexerror([["abc\u{11r"]], [[abc\u{11r]]) -- missing '}'
|
lexerror([["abc\u{11r"]], [[abc\u{11r]]) -- missing '}'
|
||||||
@@ -132,16 +122,16 @@ end
|
|||||||
|
|
||||||
-- long variable names
|
-- long variable names
|
||||||
|
|
||||||
local var1 = string.rep('a', 15000) .. '1'
|
var1 = string.rep('a', 15000) .. '1'
|
||||||
local var2 = string.rep('a', 15000) .. '2'
|
var2 = string.rep('a', 15000) .. '2'
|
||||||
local prog = string.format([[
|
prog = string.format([[
|
||||||
%s = 5
|
%s = 5
|
||||||
%s = %s + 1
|
%s = %s + 1
|
||||||
return function () return %s - %s end
|
return function () return %s - %s end
|
||||||
]], var1, var2, var1, var1, var2)
|
]], var1, var2, var1, var1, var2)
|
||||||
local f = dostring(prog)
|
local f = dostring(prog)
|
||||||
assert(_G[var1] == 5 and _G[var2] == 6 and f() == -1)
|
assert(_G[var1] == 5 and _G[var2] == 6 and f() == -1)
|
||||||
_G[var1], _G[var2] = nil
|
var1, var2, f = nil
|
||||||
print('+')
|
print('+')
|
||||||
|
|
||||||
-- escapes --
|
-- escapes --
|
||||||
@@ -153,13 +143,13 @@ assert([[
|
|||||||
$debug]] == "\n $debug")
|
$debug]] == "\n $debug")
|
||||||
assert([[ [ ]] ~= [[ ] ]])
|
assert([[ [ ]] ~= [[ ] ]])
|
||||||
-- long strings --
|
-- long strings --
|
||||||
local b = "001234567890123456789012345678901234567891234567890123456789012345678901234567890012345678901234567890123456789012345678912345678901234567890123456789012345678900123456789012345678901234567890123456789123456789012345678901234567890123456789001234567890123456789012345678901234567891234567890123456789012345678901234567890012345678901234567890123456789012345678912345678901234567890123456789012345678900123456789012345678901234567890123456789123456789012345678901234567890123456789001234567890123456789012345678901234567891234567890123456789012345678901234567890012345678901234567890123456789012345678912345678901234567890123456789012345678900123456789012345678901234567890123456789123456789012345678901234567890123456789001234567890123456789012345678901234567891234567890123456789012345678901234567890012345678901234567890123456789012345678912345678901234567890123456789012345678900123456789012345678901234567890123456789123456789012345678901234567890123456789"
|
b = "001234567890123456789012345678901234567891234567890123456789012345678901234567890012345678901234567890123456789012345678912345678901234567890123456789012345678900123456789012345678901234567890123456789123456789012345678901234567890123456789001234567890123456789012345678901234567891234567890123456789012345678901234567890012345678901234567890123456789012345678912345678901234567890123456789012345678900123456789012345678901234567890123456789123456789012345678901234567890123456789001234567890123456789012345678901234567891234567890123456789012345678901234567890012345678901234567890123456789012345678912345678901234567890123456789012345678900123456789012345678901234567890123456789123456789012345678901234567890123456789001234567890123456789012345678901234567891234567890123456789012345678901234567890012345678901234567890123456789012345678912345678901234567890123456789012345678900123456789012345678901234567890123456789123456789012345678901234567890123456789"
|
||||||
assert(string.len(b) == 960)
|
assert(string.len(b) == 960)
|
||||||
prog = [=[
|
prog = [=[
|
||||||
print('+')
|
print('+')
|
||||||
|
|
||||||
local a1 = [["this is a 'string' with several 'quotes'"]]
|
a1 = [["this is a 'string' with several 'quotes'"]]
|
||||||
local a2 = "'quotes'"
|
a2 = "'quotes'"
|
||||||
|
|
||||||
assert(string.find(a1, a2) == 34)
|
assert(string.find(a1, a2) == 34)
|
||||||
print('+')
|
print('+')
|
||||||
@@ -167,13 +157,12 @@ print('+')
|
|||||||
a1 = [==[temp = [[an arbitrary value]]; ]==]
|
a1 = [==[temp = [[an arbitrary value]]; ]==]
|
||||||
assert(load(a1))()
|
assert(load(a1))()
|
||||||
assert(temp == 'an arbitrary value')
|
assert(temp == 'an arbitrary value')
|
||||||
_G.temp = nil
|
|
||||||
-- long strings --
|
-- long strings --
|
||||||
local b = "001234567890123456789012345678901234567891234567890123456789012345678901234567890012345678901234567890123456789012345678912345678901234567890123456789012345678900123456789012345678901234567890123456789123456789012345678901234567890123456789001234567890123456789012345678901234567891234567890123456789012345678901234567890012345678901234567890123456789012345678912345678901234567890123456789012345678900123456789012345678901234567890123456789123456789012345678901234567890123456789001234567890123456789012345678901234567891234567890123456789012345678901234567890012345678901234567890123456789012345678912345678901234567890123456789012345678900123456789012345678901234567890123456789123456789012345678901234567890123456789001234567890123456789012345678901234567891234567890123456789012345678901234567890012345678901234567890123456789012345678912345678901234567890123456789012345678900123456789012345678901234567890123456789123456789012345678901234567890123456789"
|
b = "001234567890123456789012345678901234567891234567890123456789012345678901234567890012345678901234567890123456789012345678912345678901234567890123456789012345678900123456789012345678901234567890123456789123456789012345678901234567890123456789001234567890123456789012345678901234567891234567890123456789012345678901234567890012345678901234567890123456789012345678912345678901234567890123456789012345678900123456789012345678901234567890123456789123456789012345678901234567890123456789001234567890123456789012345678901234567891234567890123456789012345678901234567890012345678901234567890123456789012345678912345678901234567890123456789012345678900123456789012345678901234567890123456789123456789012345678901234567890123456789001234567890123456789012345678901234567891234567890123456789012345678901234567890012345678901234567890123456789012345678912345678901234567890123456789012345678900123456789012345678901234567890123456789123456789012345678901234567890123456789"
|
||||||
assert(string.len(b) == 960)
|
assert(string.len(b) == 960)
|
||||||
print('+')
|
print('+')
|
||||||
|
|
||||||
local a = [[00123456789012345678901234567890123456789123456789012345678901234567890123456789
|
a = [[00123456789012345678901234567890123456789123456789012345678901234567890123456789
|
||||||
00123456789012345678901234567890123456789123456789012345678901234567890123456789
|
00123456789012345678901234567890123456789123456789012345678901234567890123456789
|
||||||
00123456789012345678901234567890123456789123456789012345678901234567890123456789
|
00123456789012345678901234567890123456789123456789012345678901234567890123456789
|
||||||
00123456789012345678901234567890123456789123456789012345678901234567890123456789
|
00123456789012345678901234567890123456789123456789012345678901234567890123456789
|
||||||
@@ -203,41 +192,19 @@ x = 1
|
|||||||
]=]
|
]=]
|
||||||
|
|
||||||
print('+')
|
print('+')
|
||||||
_G.x = nil
|
x = nil
|
||||||
dostring(prog)
|
dostring(prog)
|
||||||
assert(x)
|
assert(x)
|
||||||
_G.x = nil
|
|
||||||
|
|
||||||
|
prog = nil
|
||||||
|
a = nil
|
||||||
do -- reuse of long strings
|
b = nil
|
||||||
|
|
||||||
-- get the address of a string
|
|
||||||
local function getadd (s) return string.format("%p", s) end
|
|
||||||
|
|
||||||
local s1 <const> = "01234567890123456789012345678901234567890123456789"
|
|
||||||
local s2 <const> = "01234567890123456789012345678901234567890123456789"
|
|
||||||
local s3 = "01234567890123456789012345678901234567890123456789"
|
|
||||||
local function foo() return s1 end
|
|
||||||
local function foo1() return s3 end
|
|
||||||
local function foo2()
|
|
||||||
return "01234567890123456789012345678901234567890123456789"
|
|
||||||
end
|
|
||||||
local a1 = getadd(s1)
|
|
||||||
assert(a1 == getadd(s2))
|
|
||||||
assert(a1 == getadd(foo()))
|
|
||||||
assert(a1 == getadd(foo1()))
|
|
||||||
assert(a1 == getadd(foo2()))
|
|
||||||
|
|
||||||
local sd = "0123456789" .. "0123456789012345678901234567890123456789"
|
|
||||||
assert(sd == s1 and getadd(sd) ~= a1)
|
|
||||||
end
|
|
||||||
|
|
||||||
|
|
||||||
-- testing line ends
|
-- testing line ends
|
||||||
prog = [[
|
prog = [[
|
||||||
local a = 1 -- a comment
|
a = 1 -- a comment
|
||||||
local b = 2
|
b = 2
|
||||||
|
|
||||||
|
|
||||||
x = [=[
|
x = [=[
|
||||||
@@ -254,11 +221,10 @@ for _, n in pairs{"\n", "\r", "\n\r", "\r\n"} do
|
|||||||
assert(dostring(prog) == nn)
|
assert(dostring(prog) == nn)
|
||||||
assert(_G.x == "hi\n" and _G.y == "\nhello\r\n\n")
|
assert(_G.x == "hi\n" and _G.y == "\nhello\r\n\n")
|
||||||
end
|
end
|
||||||
_G.x, _G.y = nil
|
|
||||||
|
|
||||||
|
|
||||||
-- testing comments and strings with long brackets
|
-- testing comments and strings with long brackets
|
||||||
local a = [==[]=]==]
|
a = [==[]=]==]
|
||||||
assert(a == "]=")
|
assert(a == "]=")
|
||||||
|
|
||||||
a = [==[[===[[=[]]=][====[]]===]===]==]
|
a = [==[[===[[=[]]=][====[]]===]===]==]
|
||||||
@@ -308,7 +274,7 @@ if os.setlocale("pt_BR") or os.setlocale("ptb") then
|
|||||||
|
|
||||||
assert(" 0x.1 " + " 0x,1" + "-0X.1\t" == 0x0.1)
|
assert(" 0x.1 " + " 0x,1" + "-0X.1\t" == 0x0.1)
|
||||||
|
|
||||||
assert(not tonumber"inf" and not tonumber"NAN")
|
assert(tonumber"inf" == nil and tonumber"NAN" == nil)
|
||||||
|
|
||||||
assert(assert(load(string.format("return %q", 4.51)))() == 4.51)
|
assert(assert(load(string.format("return %q", 4.51)))() == 4.51)
|
||||||
|
|
||||||
@@ -333,13 +299,4 @@ assert(not load"a = 'non-ending string\n'")
|
|||||||
assert(not load"a = '\\345'")
|
assert(not load"a = '\\345'")
|
||||||
assert(not load"a = [=x]")
|
assert(not load"a = [=x]")
|
||||||
|
|
||||||
local function malformednum (n, exp)
|
|
||||||
local s, msg = load("return " .. n)
|
|
||||||
assert(not s and string.find(msg, exp))
|
|
||||||
end
|
|
||||||
|
|
||||||
malformednum("0xe-", "near <eof>")
|
|
||||||
malformednum("0xep-p", "malformed number")
|
|
||||||
malformednum("1print()", "malformed number")
|
|
||||||
|
|
||||||
print('OK')
|
print('OK')
|
||||||
|
|||||||
+14
-1079
File diff suppressed because it is too large
Load Diff
+45
-243
@@ -1,6 +1,6 @@
|
|||||||
# testing special comment on first line
|
# testing special comment on first line
|
||||||
-- $Id: testes/main.lua $
|
-- $Id: main.lua,v 1.65 2016/11/07 13:11:28 roberto Exp $
|
||||||
-- See Copyright Notice in file lua.h
|
-- See Copyright Notice in file all.lua
|
||||||
|
|
||||||
-- most (all?) tests here assume a reasonable "Unix-like" shell
|
-- most (all?) tests here assume a reasonable "Unix-like" shell
|
||||||
if _port then return end
|
if _port then return end
|
||||||
@@ -13,7 +13,7 @@ print ("testing stand-alone interpreter")
|
|||||||
|
|
||||||
assert(os.execute()) -- machine has a system command
|
assert(os.execute()) -- machine has a system command
|
||||||
|
|
||||||
local arg = arg or ARG
|
local arg = arg or _ARG
|
||||||
|
|
||||||
local prog = os.tmpname()
|
local prog = os.tmpname()
|
||||||
local otherprog = os.tmpname()
|
local otherprog = os.tmpname()
|
||||||
@@ -27,26 +27,22 @@ do
|
|||||||
end
|
end
|
||||||
print("progname: "..progname)
|
print("progname: "..progname)
|
||||||
|
|
||||||
|
local prepfile = function (s, p)
|
||||||
local prepfile = function (s, mod, p)
|
p = p or prog
|
||||||
mod = mod and "wb" or "w" -- mod true means binary files
|
io.output(p)
|
||||||
p = p or prog -- file to write the program
|
io.write(s)
|
||||||
local f = io.open(p, mod)
|
assert(io.close())
|
||||||
f:write(s)
|
|
||||||
assert(f:close())
|
|
||||||
end
|
end
|
||||||
|
|
||||||
local function getoutput ()
|
local function getoutput ()
|
||||||
local f = io.open(out)
|
io.input(out)
|
||||||
local t = f:read("a")
|
local t = io.read("a")
|
||||||
f:close()
|
io.input():close()
|
||||||
assert(os.remove(out))
|
assert(os.remove(out))
|
||||||
return t
|
return t
|
||||||
end
|
end
|
||||||
|
|
||||||
local function checkprogout (s)
|
local function checkprogout (s)
|
||||||
-- expected result must end with new line
|
|
||||||
assert(string.sub(s, -1) == "\n")
|
|
||||||
local t = getoutput()
|
local t = getoutput()
|
||||||
for line in string.gmatch(s, ".-\n") do
|
for line in string.gmatch(s, ".-\n") do
|
||||||
assert(string.find(t, line, 1, true))
|
assert(string.find(t, line, 1, true))
|
||||||
@@ -67,11 +63,10 @@ local function RUN (p, ...)
|
|||||||
assert(os.execute(s))
|
assert(os.execute(s))
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
||||||
local function NoRun (msg, p, ...)
|
local function NoRun (msg, p, ...)
|
||||||
p = string.gsub(p, "lua", '"'..progname..'"', 1)
|
p = string.gsub(p, "lua", '"'..progname..'"', 1)
|
||||||
local s = string.format(p, ...)
|
local s = string.format(p, ...)
|
||||||
s = string.format("%s >%s 2>&1", s, out) -- send output and error to 'out'
|
s = string.format("%s 2> %s", s, out) -- will send error to 'out'
|
||||||
assert(not os.execute(s))
|
assert(not os.execute(s))
|
||||||
assert(string.find(getoutput(), msg, 1, true)) -- check error message
|
assert(string.find(getoutput(), msg, 1, true)) -- check error message
|
||||||
end
|
end
|
||||||
@@ -90,40 +85,13 @@ prepfile[[
|
|||||||
1, a
|
1, a
|
||||||
)
|
)
|
||||||
]]
|
]]
|
||||||
RUN('lua - -- < %s > %s', prog, out)
|
RUN('lua - < %s > %s', prog, out)
|
||||||
checkout("1\tnil\n")
|
checkout("1\tnil\n")
|
||||||
|
|
||||||
RUN('echo "print(10)\nprint(2)\n" | lua > %s', out)
|
RUN('echo "print(10)\nprint(2)\n" | lua > %s', out)
|
||||||
checkout("10\n2\n")
|
checkout("10\n2\n")
|
||||||
|
|
||||||
|
|
||||||
-- testing BOM
|
|
||||||
prepfile("\xEF\xBB\xBF")
|
|
||||||
RUN('lua %s > %s', prog, out)
|
|
||||||
checkout("")
|
|
||||||
|
|
||||||
prepfile("\xEF\xBB\xBFprint(3)")
|
|
||||||
RUN('lua %s > %s', prog, out)
|
|
||||||
checkout("3\n")
|
|
||||||
|
|
||||||
prepfile("\xEF\xBB\xBF# comment!!\nprint(3)")
|
|
||||||
RUN('lua %s > %s', prog, out)
|
|
||||||
checkout("3\n")
|
|
||||||
|
|
||||||
-- bad BOMs
|
|
||||||
prepfile("\xEF", true)
|
|
||||||
NoRun("unexpected symbol", 'lua %s', prog)
|
|
||||||
|
|
||||||
prepfile("\xEF\xBB", true)
|
|
||||||
NoRun("unexpected symbol", 'lua %s', prog)
|
|
||||||
|
|
||||||
prepfile("\xEFprint(3)", true)
|
|
||||||
NoRun("unexpected symbol", 'lua %s', prog)
|
|
||||||
|
|
||||||
prepfile("\xEF\xBBprint(3)", true)
|
|
||||||
NoRun("unexpected symbol", 'lua %s', prog)
|
|
||||||
|
|
||||||
|
|
||||||
-- test option '-'
|
-- test option '-'
|
||||||
RUN('echo "print(arg[1])" | lua - -h > %s', out)
|
RUN('echo "print(arg[1])" | lua - -h > %s', out)
|
||||||
checkout("-h\n")
|
checkout("-h\n")
|
||||||
@@ -133,11 +101,11 @@ checkout("-h\n")
|
|||||||
prepfile("print(package.path)")
|
prepfile("print(package.path)")
|
||||||
|
|
||||||
-- test LUA_PATH
|
-- test LUA_PATH
|
||||||
RUN('env LUA_INIT= LUA_PATH=x lua -- %s > %s', prog, out)
|
RUN('env LUA_INIT= LUA_PATH=x lua %s > %s', prog, out)
|
||||||
checkout("x\n")
|
checkout("x\n")
|
||||||
|
|
||||||
-- test LUA_PATH_version
|
-- test LUA_PATH_version
|
||||||
RUN('env LUA_INIT= LUA_PATH_5_5=y LUA_PATH=x lua %s > %s', prog, out)
|
RUN('env LUA_INIT= LUA_PATH_5_3=y LUA_PATH=x lua %s > %s', prog, out)
|
||||||
checkout("y\n")
|
checkout("y\n")
|
||||||
|
|
||||||
-- test LUA_CPATH
|
-- test LUA_CPATH
|
||||||
@@ -146,7 +114,7 @@ RUN('env LUA_INIT= LUA_CPATH=xuxu lua %s > %s', prog, out)
|
|||||||
checkout("xuxu\n")
|
checkout("xuxu\n")
|
||||||
|
|
||||||
-- test LUA_CPATH_version
|
-- test LUA_CPATH_version
|
||||||
RUN('env LUA_INIT= LUA_CPATH_5_5=yacc LUA_CPATH=x lua %s > %s', prog, out)
|
RUN('env LUA_INIT= LUA_CPATH_5_3=yacc LUA_CPATH=x lua %s > %s', prog, out)
|
||||||
checkout("yacc\n")
|
checkout("yacc\n")
|
||||||
|
|
||||||
-- test LUA_INIT (and its access to 'arg' table)
|
-- test LUA_INIT (and its access to 'arg' table)
|
||||||
@@ -156,7 +124,7 @@ checkout("3.2\n")
|
|||||||
|
|
||||||
-- test LUA_INIT_version
|
-- test LUA_INIT_version
|
||||||
prepfile("print(X)")
|
prepfile("print(X)")
|
||||||
RUN('env LUA_INIT_5_5="X=10" LUA_INIT="X=3" lua %s > %s', prog, out)
|
RUN('env LUA_INIT_5_3="X=10" LUA_INIT="X=3" lua %s > %s', prog, out)
|
||||||
checkout("10\n")
|
checkout("10\n")
|
||||||
|
|
||||||
-- test LUA_INIT for files
|
-- test LUA_INIT for files
|
||||||
@@ -174,18 +142,12 @@ do
|
|||||||
prepfile("print(package.path, package.cpath)")
|
prepfile("print(package.path, package.cpath)")
|
||||||
RUN('env LUA_INIT="error(10)" LUA_PATH=xxx LUA_CPATH=xxx lua -E %s > %s',
|
RUN('env LUA_INIT="error(10)" LUA_PATH=xxx LUA_CPATH=xxx lua -E %s > %s',
|
||||||
prog, out)
|
prog, out)
|
||||||
local output = getoutput()
|
|
||||||
defaultpath = string.match(output, "^(.-)\t")
|
|
||||||
defaultCpath = string.match(output, "\t(.-)$")
|
|
||||||
|
|
||||||
-- running with an empty environment
|
|
||||||
RUN('env -i lua %s > %s', prog, out)
|
|
||||||
local out = getoutput()
|
local out = getoutput()
|
||||||
assert(defaultpath == string.match(output, "^(.-)\t"))
|
defaultpath = string.match(out, "^(.-)\t")
|
||||||
assert(defaultCpath == string.match(output, "\t(.-)$"))
|
defaultCpath = string.match(out, "\t(.-)$")
|
||||||
end
|
end
|
||||||
|
|
||||||
-- paths did not change
|
-- paths did not changed
|
||||||
assert(not string.find(defaultpath, "xxx") and
|
assert(not string.find(defaultpath, "xxx") and
|
||||||
string.find(defaultpath, "lua") and
|
string.find(defaultpath, "lua") and
|
||||||
not string.find(defaultCpath, "xxx") and
|
not string.find(defaultCpath, "xxx") and
|
||||||
@@ -198,46 +160,29 @@ local function convert (p)
|
|||||||
RUN('env LUA_PATH="%s" lua %s > %s', p, prog, out)
|
RUN('env LUA_PATH="%s" lua %s > %s', p, prog, out)
|
||||||
local expected = getoutput()
|
local expected = getoutput()
|
||||||
expected = string.sub(expected, 1, -2) -- cut final end of line
|
expected = string.sub(expected, 1, -2) -- cut final end of line
|
||||||
if string.find(p, ";;") then
|
assert(string.gsub(p, ";;", ";"..defaultpath..";") == expected)
|
||||||
p = string.gsub(p, ";;", ";"..defaultpath..";")
|
|
||||||
p = string.gsub(p, "^;", "") -- remove ';' at the beginning
|
|
||||||
p = string.gsub(p, ";$", "") -- remove ';' at the end
|
|
||||||
end
|
|
||||||
assert(p == expected)
|
|
||||||
end
|
end
|
||||||
|
|
||||||
convert(";")
|
convert(";")
|
||||||
convert(";;")
|
convert(";;")
|
||||||
convert("a;;b")
|
convert(";;;")
|
||||||
convert(";;b")
|
convert(";;;;")
|
||||||
convert("a;;")
|
convert(";;;;;")
|
||||||
convert("a;b;;c")
|
convert(";;a;;;bc")
|
||||||
|
|
||||||
|
|
||||||
-- test -l over multiple libraries
|
-- test -l over multiple libraries
|
||||||
prepfile("print(1); a=2; return {x=15}")
|
prepfile("print(1); a=2; return {x=15}")
|
||||||
prepfile(("print(a); print(_G['%s'].x)"):format(prog), false, otherprog)
|
prepfile(("print(a); print(_G['%s'].x)"):format(prog), otherprog)
|
||||||
RUN('env LUA_PATH="?;;" lua -l %s -l%s -lstring -l io %s > %s', prog, otherprog, otherprog, out)
|
RUN('env LUA_PATH="?;;" lua -l %s -l%s -lstring -l io %s > %s', prog, otherprog, otherprog, out)
|
||||||
checkout("1\n2\n15\n2\n15\n")
|
checkout("1\n2\n15\n2\n15\n")
|
||||||
|
|
||||||
-- test explicit global names in -l
|
|
||||||
prepfile("print(str.upper'alo alo', m.max(10, 20))")
|
|
||||||
RUN("lua -l 'str=string' '-lm=math' -e 'print(m.sin(0))' %s > %s", prog, out)
|
|
||||||
checkout("0.0\nALO ALO\t20\n")
|
|
||||||
|
|
||||||
|
|
||||||
-- test module names with version suffix ("libs/lib2-v2")
|
|
||||||
RUN("env LUA_CPATH='./libs/?.so' lua -l lib2-v2 -e 'print(lib2.id())' > %s",
|
|
||||||
out)
|
|
||||||
checkout("true\n")
|
|
||||||
|
|
||||||
|
|
||||||
-- test 'arg' table
|
-- test 'arg' table
|
||||||
local a = [[
|
local a = [[
|
||||||
assert(#arg == 3 and arg[1] == 'a' and
|
assert(#arg == 3 and arg[1] == 'a' and
|
||||||
arg[2] == 'b' and arg[3] == 'c')
|
arg[2] == 'b' and arg[3] == 'c')
|
||||||
assert(arg[-1] == '--' and arg[-2] == "-e " and arg[-3] == '%s')
|
assert(arg[-1] == '--' and arg[-2] == "-e " and arg[-3] == '%s')
|
||||||
assert(arg[4] == undef and arg[-4] == undef)
|
assert(arg[4] == nil and arg[-4] == nil)
|
||||||
local a, b, c = ...
|
local a, b, c = ...
|
||||||
assert(... == 'a' and a == 'a' and b == 'b' and c == 'c')
|
assert(... == 'a' and a == 'a' and b == 'b' and c == 'c')
|
||||||
]]
|
]]
|
||||||
@@ -247,7 +192,7 @@ RUN('lua "-e " -- %s a b c', prog) -- "-e " runs an empty command
|
|||||||
|
|
||||||
-- test 'arg' availability in libraries
|
-- test 'arg' availability in libraries
|
||||||
prepfile"assert(arg)"
|
prepfile"assert(arg)"
|
||||||
prepfile("assert(arg)", false, otherprog)
|
prepfile("assert(arg)", otherprog)
|
||||||
RUN('env LUA_PATH="?;;" lua -l%s - < %s', prog, otherprog)
|
RUN('env LUA_PATH="?;;" lua -l%s - < %s', prog, otherprog)
|
||||||
|
|
||||||
-- test messing up the 'arg' table
|
-- test messing up the 'arg' table
|
||||||
@@ -263,82 +208,6 @@ assert(string.find(getoutput(), "error calling 'print'"))
|
|||||||
RUN('echo "io.stderr:write(1000)\ncont" | lua -e "require\'debug\'.debug()" 2> %s', out)
|
RUN('echo "io.stderr:write(1000)\ncont" | lua -e "require\'debug\'.debug()" 2> %s', out)
|
||||||
checkout("lua_debug> 1000lua_debug> ")
|
checkout("lua_debug> 1000lua_debug> ")
|
||||||
|
|
||||||
do -- test warning for locals
|
|
||||||
RUN('echo " local x" | lua -i > %s 2>&1', out)
|
|
||||||
assert(string.find(getoutput(), "warning: "))
|
|
||||||
|
|
||||||
RUN('echo "local1 = 10\nlocal1 + 3" | lua -i > %s 2>&1', out)
|
|
||||||
local t = getoutput()
|
|
||||||
assert(not string.find(t, "warning"))
|
|
||||||
assert(string.find(t, "13"))
|
|
||||||
end
|
|
||||||
|
|
||||||
print("testing warnings")
|
|
||||||
|
|
||||||
-- no warnings by default
|
|
||||||
RUN('echo "io.stderr:write(1); warn[[XXX]]" | lua 2> %s', out)
|
|
||||||
checkout("1")
|
|
||||||
|
|
||||||
prepfile[[
|
|
||||||
warn("@allow") -- unknown control, ignored
|
|
||||||
warn("@off", "XXX", "@off") -- these are not control messages
|
|
||||||
warn("@off") -- this one is
|
|
||||||
warn("@on", "YYY", "@on") -- not control, but warn is off
|
|
||||||
warn("@off") -- keep it off
|
|
||||||
warn("@on") -- restart warnings
|
|
||||||
warn("", "@on") -- again, no control, real warning
|
|
||||||
warn("@on") -- keep it "started"
|
|
||||||
warn("Z", "Z", "Z") -- common warning
|
|
||||||
]]
|
|
||||||
RUN('lua -W %s 2> %s', prog, out)
|
|
||||||
checkout[[
|
|
||||||
Lua warning: @offXXX@off
|
|
||||||
Lua warning: @on
|
|
||||||
Lua warning: ZZZ
|
|
||||||
]]
|
|
||||||
|
|
||||||
prepfile[[
|
|
||||||
warn("@allow")
|
|
||||||
-- create two objects to be finalized when closing state
|
|
||||||
-- the errors in the finalizers must generate warnings
|
|
||||||
u1 = setmetatable({}, {__gc = function () error("XYZ") end})
|
|
||||||
u2 = setmetatable({}, {__gc = function () error("ZYX") end})
|
|
||||||
]]
|
|
||||||
RUN('lua -W %s 2> %s', prog, out)
|
|
||||||
checkprogout("ZYX)\nXYZ)\n")
|
|
||||||
|
|
||||||
-- bug since 5.2: finalizer called when closing a state could
|
|
||||||
-- subvert finalization order
|
|
||||||
prepfile[[
|
|
||||||
-- ensure tables will be collected only at the end of the program
|
|
||||||
collectgarbage"stop"
|
|
||||||
|
|
||||||
print("creating 1")
|
|
||||||
-- this finalizer should be called last
|
|
||||||
setmetatable({}, {__gc = function () print(1) end})
|
|
||||||
|
|
||||||
print("creating 2")
|
|
||||||
setmetatable({}, {__gc = function ()
|
|
||||||
print("2")
|
|
||||||
print("creating 3")
|
|
||||||
-- this finalizer should not be called, as object will be
|
|
||||||
-- created after 'lua_close' has been called
|
|
||||||
setmetatable({}, {__gc = function () print(3) end})
|
|
||||||
print(collectgarbage() or false) -- cannot call collector here
|
|
||||||
os.exit(0, true)
|
|
||||||
end})
|
|
||||||
]]
|
|
||||||
RUN('lua -W %s > %s', prog, out)
|
|
||||||
checkout[[
|
|
||||||
creating 1
|
|
||||||
creating 2
|
|
||||||
2
|
|
||||||
creating 3
|
|
||||||
false
|
|
||||||
1
|
|
||||||
]]
|
|
||||||
|
|
||||||
|
|
||||||
-- test many arguments
|
-- test many arguments
|
||||||
prepfile[[print(({...})[30])]]
|
prepfile[[print(({...})[30])]]
|
||||||
RUN('lua %s %s > %s', prog, string.rep(" a", 30), out)
|
RUN('lua %s %s > %s', prog, string.rep(" a", 30), out)
|
||||||
@@ -347,7 +216,7 @@ checkout("a\n")
|
|||||||
RUN([[lua "-eprint(1)" -ea=3 -e "print(a)" > %s]], out)
|
RUN([[lua "-eprint(1)" -ea=3 -e "print(a)" > %s]], out)
|
||||||
checkout("1\n3\n")
|
checkout("1\n3\n")
|
||||||
|
|
||||||
-- test interactive mode
|
-- test iteractive mode
|
||||||
prepfile[[
|
prepfile[[
|
||||||
(6*2-6) -- ===
|
(6*2-6) -- ===
|
||||||
a =
|
a =
|
||||||
@@ -357,16 +226,11 @@ a]]
|
|||||||
RUN([[lua -e"_PROMPT='' _PROMPT2=''" -i < %s > %s]], prog, out)
|
RUN([[lua -e"_PROMPT='' _PROMPT2=''" -i < %s > %s]], prog, out)
|
||||||
checkprogout("6\n10\n10\n\n")
|
checkprogout("6\n10\n10\n\n")
|
||||||
|
|
||||||
prepfile("a = [[b\nc\nd\ne]]\na")
|
prepfile("a = [[b\nc\nd\ne]]\n=a")
|
||||||
RUN([[lua -e"_PROMPT='' _PROMPT2=''" -i -- < %s > %s]], prog, out)
|
RUN([[lua -e"_PROMPT='' _PROMPT2=''" -i < %s > %s]], prog, out)
|
||||||
checkprogout("b\nc\nd\ne\n\n")
|
checkprogout("b\nc\nd\ne\n\n")
|
||||||
|
|
||||||
-- input interrupted in continuation line
|
prompt = "alo"
|
||||||
prepfile("a.\n")
|
|
||||||
RUN([[lua -i < %s > /dev/null 2> %s]], prog, out)
|
|
||||||
checkprogout("near <eof>\n")
|
|
||||||
|
|
||||||
local prompt = "alo"
|
|
||||||
prepfile[[ --
|
prepfile[[ --
|
||||||
a = 2
|
a = 2
|
||||||
]]
|
]]
|
||||||
@@ -374,31 +238,6 @@ RUN([[lua "-e_PROMPT='%s'" -i < %s > %s]], prompt, prog, out)
|
|||||||
local t = getoutput()
|
local t = getoutput()
|
||||||
assert(string.find(t, prompt .. ".*" .. prompt .. ".*" .. prompt))
|
assert(string.find(t, prompt .. ".*" .. prompt .. ".*" .. prompt))
|
||||||
|
|
||||||
-- using the prompt default
|
|
||||||
prepfile[[ --
|
|
||||||
a = 2
|
|
||||||
]]
|
|
||||||
RUN([[lua -i < %s > %s]], prog, out)
|
|
||||||
local t = getoutput()
|
|
||||||
prompt = "> " -- the default
|
|
||||||
assert(string.find(t, prompt .. ".*" .. prompt .. ".*" .. prompt))
|
|
||||||
|
|
||||||
|
|
||||||
-- non-string prompt
|
|
||||||
prompt = [[
|
|
||||||
local C = 'X';
|
|
||||||
_PROMPT=setmetatable({},{__tostring = function ()
|
|
||||||
C = C .. 'X'; return C end})
|
|
||||||
]]
|
|
||||||
prepfile[[ --
|
|
||||||
a = 2
|
|
||||||
]]
|
|
||||||
RUN([[lua -e "%s" -i < %s > %s]], prompt, prog, out)
|
|
||||||
local t = getoutput()
|
|
||||||
-- skip version line and then check the presence of the three prompts
|
|
||||||
assert(string.find(t, "^.-\nXX[^\nX]*\n?XXX[^\nX]*\n?XXXX\n?$"))
|
|
||||||
|
|
||||||
|
|
||||||
-- test for error objects
|
-- test for error objects
|
||||||
prepfile[[
|
prepfile[[
|
||||||
debug = require "debug"
|
debug = require "debug"
|
||||||
@@ -415,15 +254,15 @@ NoRun("error object is a table value", [[lua %s]], prog)
|
|||||||
|
|
||||||
|
|
||||||
-- chunk broken in many lines
|
-- chunk broken in many lines
|
||||||
local s = [=[ --
|
s = [=[ --
|
||||||
function f ( x )
|
function f ( x )
|
||||||
local a = [[
|
local a = [[
|
||||||
xuxu
|
xuxu
|
||||||
]]
|
]]
|
||||||
local b = "\
|
local b = "\
|
||||||
xuxu\n"
|
xuxu\n"
|
||||||
if x == 11 then return 1 + 12 , 2 + 20 end --[[ test multiple returns ]]
|
if x == 11 then return 1 + 12 , 2 + 20 end --[[ test multiple returns ]]
|
||||||
return x + 1
|
return x + 1
|
||||||
--\\
|
--\\
|
||||||
end
|
end
|
||||||
return( f( 100 ) )
|
return( f( 100 ) )
|
||||||
@@ -433,14 +272,16 @@ s = string.gsub(s, ' ', '\n\n') -- change all spaces for newlines
|
|||||||
prepfile(s)
|
prepfile(s)
|
||||||
RUN([[lua -e"_PROMPT='' _PROMPT2=''" -i < %s > %s]], prog, out)
|
RUN([[lua -e"_PROMPT='' _PROMPT2=''" -i < %s > %s]], prog, out)
|
||||||
checkprogout("101\n13\t22\n\n")
|
checkprogout("101\n13\t22\n\n")
|
||||||
|
|
||||||
prepfile[[#comment in 1st line without \n at the end]]
|
prepfile[[#comment in 1st line without \n at the end]]
|
||||||
RUN('lua %s', prog)
|
RUN('lua %s', prog)
|
||||||
|
|
||||||
-- first-line comment with binary file
|
prepfile[[#test line number when file starts with comment line
|
||||||
prepfile("#comment\n" .. string.dump(load("print(3)")), true)
|
debug = require"debug"
|
||||||
|
print(debug.getinfo(1).currentline)
|
||||||
|
]]
|
||||||
RUN('lua %s > %s', prog, out)
|
RUN('lua %s > %s', prog, out)
|
||||||
checkout('3\n')
|
checkprogout('3')
|
||||||
|
|
||||||
-- close Lua with an open file
|
-- close Lua with an open file
|
||||||
prepfile(string.format([[io.output(%q); io.write('alo')]], out))
|
prepfile(string.format([[io.output(%q); io.write('alo')]], out))
|
||||||
@@ -465,21 +306,6 @@ NoRun("", "lua %s", prog) -- no message
|
|||||||
prepfile("os.exit(false, true)")
|
prepfile("os.exit(false, true)")
|
||||||
NoRun("", "lua %s", prog) -- no message
|
NoRun("", "lua %s", prog) -- no message
|
||||||
|
|
||||||
|
|
||||||
-- to-be-closed variables in main chunk
|
|
||||||
prepfile[[
|
|
||||||
local x <close> = setmetatable({},
|
|
||||||
{__close = function (self, err)
|
|
||||||
assert(err == nil)
|
|
||||||
print("Ok")
|
|
||||||
end})
|
|
||||||
local e1 <close> = setmetatable({}, {__close = function () print(120) end})
|
|
||||||
os.exit(true, true)
|
|
||||||
]]
|
|
||||||
RUN('lua %s > %s', prog, out)
|
|
||||||
checkprogout("120\nOk\n")
|
|
||||||
|
|
||||||
|
|
||||||
-- remove temporary files
|
-- remove temporary files
|
||||||
assert(os.remove(prog))
|
assert(os.remove(prog))
|
||||||
assert(os.remove(otherprog))
|
assert(os.remove(otherprog))
|
||||||
@@ -488,49 +314,25 @@ assert(not os.remove(out))
|
|||||||
-- invalid options
|
-- invalid options
|
||||||
NoRun("unrecognized option '-h'", "lua -h")
|
NoRun("unrecognized option '-h'", "lua -h")
|
||||||
NoRun("unrecognized option '---'", "lua ---")
|
NoRun("unrecognized option '---'", "lua ---")
|
||||||
NoRun("unrecognized option '-Ex'", "lua -Ex --")
|
NoRun("unrecognized option '-Ex'", "lua -Ex")
|
||||||
NoRun("unrecognized option '-vv'", "lua -vv")
|
NoRun("unrecognized option '-vv'", "lua -vv")
|
||||||
NoRun("unrecognized option '-iv'", "lua -iv")
|
NoRun("unrecognized option '-iv'", "lua -iv")
|
||||||
NoRun("'-e' needs argument", "lua -e")
|
NoRun("'-e' needs argument", "lua -e")
|
||||||
NoRun("syntax error", "lua -e a")
|
NoRun("syntax error", "lua -e a")
|
||||||
NoRun("'-l' needs argument", "lua -l")
|
NoRun("'-l' needs argument", "lua -l")
|
||||||
NoRun("-i", "lua -- -i") -- handles -i as a script name
|
|
||||||
|
|
||||||
|
|
||||||
if T then -- test library?
|
if T then -- auxiliary library?
|
||||||
print("testing 'not enough memory' to create a state")
|
print("testing 'not enough memory' to create a state")
|
||||||
NoRun("not enough memory", "env MEMLIMIT=100 lua")
|
NoRun("not enough memory", "env MEMLIMIT=100 lua")
|
||||||
|
|
||||||
-- testing 'warn'
|
|
||||||
warn("@store")
|
|
||||||
warn("@123", "456", "789")
|
|
||||||
assert(_WARN == "@123456789"); _WARN = false
|
|
||||||
|
|
||||||
warn("zip", "", " ", "zap")
|
|
||||||
assert(_WARN == "zip zap"); _WARN = false
|
|
||||||
warn("ZIP", "", " ", "ZAP")
|
|
||||||
assert(_WARN == "ZIP ZAP"); _WARN = false
|
|
||||||
warn("@normal")
|
|
||||||
end
|
end
|
||||||
|
|
||||||
do
|
|
||||||
-- 'warn' must get at least one argument
|
|
||||||
local st, msg = pcall(warn)
|
|
||||||
assert(string.find(msg, "string expected"))
|
|
||||||
|
|
||||||
-- 'warn' does not leave unfinished warning in case of errors
|
|
||||||
-- (message would appear in next warning)
|
|
||||||
st, msg = pcall(warn, "SHOULD NOT APPEAR", {})
|
|
||||||
assert(string.find(msg, "string expected"))
|
|
||||||
end
|
|
||||||
|
|
||||||
print('+')
|
print('+')
|
||||||
|
|
||||||
print('testing Ctrl C')
|
print('testing Ctrl C')
|
||||||
do
|
do
|
||||||
-- interrupt a script
|
-- interrupt a script
|
||||||
local function kill (pid)
|
local function kill (pid)
|
||||||
return os.execute(string.format('kill -INT %s 2> /dev/null', pid))
|
return os.execute(string.format('kill -INT %d 2> /dev/null', pid))
|
||||||
end
|
end
|
||||||
|
|
||||||
-- function to run a script in background, returning its output file
|
-- function to run a script in background, returning its output file
|
||||||
|
|||||||
+122
-443
@@ -1,19 +1,12 @@
|
|||||||
-- $Id: testes/math.lua $
|
-- $Id: math.lua,v 1.78 2016/11/07 13:11:28 roberto Exp $
|
||||||
-- See Copyright Notice in file lua.h
|
-- See Copyright Notice in file all.lua
|
||||||
|
|
||||||
print("testing numbers and math lib")
|
print("testing numbers and math lib")
|
||||||
|
|
||||||
local math = require "math"
|
local minint = math.mininteger
|
||||||
local string = require "string"
|
local maxint = math.maxinteger
|
||||||
|
|
||||||
global none
|
local intbits = math.floor(math.log(maxint, 2) + 0.5) + 1
|
||||||
|
|
||||||
global<const> print, assert, pcall, type, pairs, load
|
|
||||||
global<const> tonumber, tostring, select
|
|
||||||
|
|
||||||
local<const> minint, maxint = math.mininteger, math.maxinteger
|
|
||||||
|
|
||||||
local intbits <const> = math.floor(math.log(maxint, 2) + 0.5) + 1
|
|
||||||
assert((1 << intbits) == 0)
|
assert((1 << intbits) == 0)
|
||||||
|
|
||||||
assert(minint == 1 << (intbits - 1))
|
assert(minint == 1 << (intbits - 1))
|
||||||
@@ -29,18 +22,6 @@ do
|
|||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
||||||
-- maximum exponent for a floating-point number
|
|
||||||
local maxexp = 0
|
|
||||||
do
|
|
||||||
local p = 2.0
|
|
||||||
while p < math.huge do
|
|
||||||
maxexp = maxexp + 1
|
|
||||||
p = p + p
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
|
|
||||||
local function isNaN (x)
|
local function isNaN (x)
|
||||||
return (x ~= x)
|
return (x ~= x)
|
||||||
end
|
end
|
||||||
@@ -53,12 +34,12 @@ do
|
|||||||
local x = 2.0^floatbits
|
local x = 2.0^floatbits
|
||||||
assert(x > x - 1.0 and x == x + 1.0)
|
assert(x > x - 1.0 and x == x + 1.0)
|
||||||
|
|
||||||
local msg = " %d-bit integers, %d-bit*2^%d floats"
|
print(string.format("%d-bit integers, %d-bit (mantissa) floats",
|
||||||
print(string.format(msg, intbits, floatbits, maxexp))
|
intbits, floatbits))
|
||||||
end
|
end
|
||||||
|
|
||||||
assert(math.type(0) == "integer" and math.type(0.0) == "float"
|
assert(math.type(0) == "integer" and math.type(0.0) == "float"
|
||||||
and not math.type("10"))
|
and math.type("10") == nil)
|
||||||
|
|
||||||
|
|
||||||
local function checkerror (msg, f, ...)
|
local function checkerror (msg, f, ...)
|
||||||
@@ -69,7 +50,7 @@ end
|
|||||||
local msgf2i = "number.* has no integer representation"
|
local msgf2i = "number.* has no integer representation"
|
||||||
|
|
||||||
-- float equality
|
-- float equality
|
||||||
local function eq (a,b,limit)
|
function eq (a,b,limit)
|
||||||
if not limit then
|
if not limit then
|
||||||
if floatbits >= 50 then limit = 1E-11
|
if floatbits >= 50 then limit = 1E-11
|
||||||
else limit = 1E-5
|
else limit = 1E-5
|
||||||
@@ -81,7 +62,7 @@ end
|
|||||||
|
|
||||||
|
|
||||||
-- equality with types
|
-- equality with types
|
||||||
local function eqT (a,b)
|
function eqT (a,b)
|
||||||
return a == b and math.type(a) == math.type(b)
|
return a == b and math.type(a) == math.type(b)
|
||||||
end
|
end
|
||||||
|
|
||||||
@@ -102,7 +83,7 @@ end
|
|||||||
do
|
do
|
||||||
local x = -1
|
local x = -1
|
||||||
local mz = 0/x -- minus zero
|
local mz = 0/x -- minus zero
|
||||||
local t = {[0] = 10, 20, 30, 40, 50}
|
t = {[0] = 10, 20, 30, 40, 50}
|
||||||
assert(t[mz] == t[0] and t[-0] == t[0])
|
assert(t[mz] == t[0] and t[-0] == t[0])
|
||||||
end
|
end
|
||||||
|
|
||||||
@@ -158,17 +139,6 @@ assert(-1 // 0.0 == -1/0)
|
|||||||
assert(eqT(3.5 // 1.5, 2.0))
|
assert(eqT(3.5 // 1.5, 2.0))
|
||||||
assert(eqT(3.5 // -1.5, -3.0))
|
assert(eqT(3.5 // -1.5, -3.0))
|
||||||
|
|
||||||
do -- tests for different kinds of opcodes
|
|
||||||
local x, y
|
|
||||||
x = 1; assert(x // 0.0 == 1/0)
|
|
||||||
x = 1.0; assert(x // 0 == 1/0)
|
|
||||||
x = 3.5; assert(eqT(x // 1, 3.0))
|
|
||||||
assert(eqT(x // -1, -4.0))
|
|
||||||
|
|
||||||
x = 3.5; y = 1.5; assert(eqT(x // y, 2.0))
|
|
||||||
x = 3.5; y = -1.5; assert(eqT(x // y, -3.0))
|
|
||||||
end
|
|
||||||
|
|
||||||
assert(maxint // maxint == 1)
|
assert(maxint // maxint == 1)
|
||||||
assert(maxint // 1 == maxint)
|
assert(maxint // 1 == maxint)
|
||||||
assert((maxint - 1) // maxint == 0)
|
assert((maxint - 1) // maxint == 0)
|
||||||
@@ -191,7 +161,7 @@ do
|
|||||||
for i = -3, 3 do -- variables avoid constant folding
|
for i = -3, 3 do -- variables avoid constant folding
|
||||||
for j = -3, 3 do
|
for j = -3, 3 do
|
||||||
-- domain errors (0^(-n)) are not portable
|
-- domain errors (0^(-n)) are not portable
|
||||||
if not _ENV._port or i ~= 0 or j > 0 then
|
if not _port or i ~= 0 or j > 0 then
|
||||||
assert(eq(i^j, 1 / i^(-j)))
|
assert(eq(i^j, 1 / i^(-j)))
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
@@ -289,7 +259,7 @@ else
|
|||||||
end
|
end
|
||||||
|
|
||||||
do
|
do
|
||||||
local NaN <const> = 0/0
|
local NaN = 0/0
|
||||||
assert(not (NaN < 0))
|
assert(not (NaN < 0))
|
||||||
assert(not (NaN > minint))
|
assert(not (NaN > minint))
|
||||||
assert(not (NaN <= -9))
|
assert(not (NaN <= -9))
|
||||||
@@ -297,8 +267,6 @@ do
|
|||||||
assert(not (NaN < maxint))
|
assert(not (NaN < maxint))
|
||||||
assert(not (minint <= NaN))
|
assert(not (minint <= NaN))
|
||||||
assert(not (minint < NaN))
|
assert(not (minint < NaN))
|
||||||
assert(not (4 <= NaN))
|
|
||||||
assert(not (4 < NaN))
|
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
||||||
@@ -311,7 +279,7 @@ checkcompt(msgf2i, "return 2.3 >> 0")
|
|||||||
checkcompt(msgf2i, ("return 2.0^%d & 1"):format(intbits - 1))
|
checkcompt(msgf2i, ("return 2.0^%d & 1"):format(intbits - 1))
|
||||||
checkcompt("field 'huge'", "return math.huge << 1")
|
checkcompt("field 'huge'", "return math.huge << 1")
|
||||||
checkcompt(msgf2i, ("return 1 | 2.0^%d"):format(intbits - 1))
|
checkcompt(msgf2i, ("return 1 | 2.0^%d"):format(intbits - 1))
|
||||||
checkcompt(msgf2i, "return 2.3 ~ 0.0")
|
checkcompt(msgf2i, "return 2.3 ~ '0.0'")
|
||||||
|
|
||||||
|
|
||||||
-- testing overflow errors when converting from float to integer (runtime)
|
-- testing overflow errors when converting from float to integer (runtime)
|
||||||
@@ -400,17 +368,17 @@ assert(tonumber(1/0) == 1/0)
|
|||||||
|
|
||||||
-- 'tonumber' with strings
|
-- 'tonumber' with strings
|
||||||
assert(tonumber("0") == 0)
|
assert(tonumber("0") == 0)
|
||||||
assert(not tonumber(""))
|
assert(tonumber("") == nil)
|
||||||
assert(not tonumber(" "))
|
assert(tonumber(" ") == nil)
|
||||||
assert(not tonumber("-"))
|
assert(tonumber("-") == nil)
|
||||||
assert(not tonumber(" -0x "))
|
assert(tonumber(" -0x ") == nil)
|
||||||
assert(not tonumber{})
|
assert(tonumber{} == nil)
|
||||||
assert(tonumber'+0.01' == 1/100 and tonumber'+.01' == 0.01 and
|
assert(tonumber'+0.01' == 1/100 and tonumber'+.01' == 0.01 and
|
||||||
tonumber'.01' == 0.01 and tonumber'-1.' == -1 and
|
tonumber'.01' == 0.01 and tonumber'-1.' == -1 and
|
||||||
tonumber'+1.' == 1)
|
tonumber'+1.' == 1)
|
||||||
assert(not tonumber'+ 0.01' and not tonumber'+.e1' and
|
assert(tonumber'+ 0.01' == nil and tonumber'+.e1' == nil and
|
||||||
not tonumber'1e' and not tonumber'1.0e+' and
|
tonumber'1e' == nil and tonumber'1.0e+' == nil and
|
||||||
not tonumber'.')
|
tonumber'.' == nil)
|
||||||
assert(tonumber('-012') == -010-2)
|
assert(tonumber('-012') == -010-2)
|
||||||
assert(tonumber('-1.2e2') == - - -120)
|
assert(tonumber('-1.2e2') == - - -120)
|
||||||
|
|
||||||
@@ -437,7 +405,7 @@ for i = 2,36 do
|
|||||||
assert(tonumber('\t10000000000\t', i) == i10)
|
assert(tonumber('\t10000000000\t', i) == i10)
|
||||||
end
|
end
|
||||||
|
|
||||||
if not _ENV._soft then
|
if not _soft then
|
||||||
-- tests with very long numerals
|
-- tests with very long numerals
|
||||||
assert(tonumber("0x"..string.rep("f", 13)..".0") == 2.0^(4*13) - 1)
|
assert(tonumber("0x"..string.rep("f", 13)..".0") == 2.0^(4*13) - 1)
|
||||||
assert(tonumber("0x"..string.rep("f", 150)..".0") == 2.0^(4*150) - 1)
|
assert(tonumber("0x"..string.rep("f", 150)..".0") == 2.0^(4*150) - 1)
|
||||||
@@ -464,45 +432,45 @@ local function f (...)
|
|||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
assert(not f(tonumber('fFfa', 15)))
|
assert(f(tonumber('fFfa', 15)) == nil)
|
||||||
assert(not f(tonumber('099', 8)))
|
assert(f(tonumber('099', 8)) == nil)
|
||||||
assert(not f(tonumber('1\0', 2)))
|
assert(f(tonumber('1\0', 2)) == nil)
|
||||||
assert(not f(tonumber('', 8)))
|
assert(f(tonumber('', 8)) == nil)
|
||||||
assert(not f(tonumber(' ', 9)))
|
assert(f(tonumber(' ', 9)) == nil)
|
||||||
assert(not f(tonumber(' ', 9)))
|
assert(f(tonumber(' ', 9)) == nil)
|
||||||
assert(not f(tonumber('0xf', 10)))
|
assert(f(tonumber('0xf', 10)) == nil)
|
||||||
|
|
||||||
assert(not f(tonumber('inf')))
|
assert(f(tonumber('inf')) == nil)
|
||||||
assert(not f(tonumber(' INF ')))
|
assert(f(tonumber(' INF ')) == nil)
|
||||||
assert(not f(tonumber('Nan')))
|
assert(f(tonumber('Nan')) == nil)
|
||||||
assert(not f(tonumber('nan')))
|
assert(f(tonumber('nan')) == nil)
|
||||||
|
|
||||||
assert(not f(tonumber(' ')))
|
assert(f(tonumber(' ')) == nil)
|
||||||
assert(not f(tonumber('')))
|
assert(f(tonumber('')) == nil)
|
||||||
assert(not f(tonumber('1 a')))
|
assert(f(tonumber('1 a')) == nil)
|
||||||
assert(not f(tonumber('1 a', 2)))
|
assert(f(tonumber('1 a', 2)) == nil)
|
||||||
assert(not f(tonumber('1\0')))
|
assert(f(tonumber('1\0')) == nil)
|
||||||
assert(not f(tonumber('1 \0')))
|
assert(f(tonumber('1 \0')) == nil)
|
||||||
assert(not f(tonumber('1\0 ')))
|
assert(f(tonumber('1\0 ')) == nil)
|
||||||
assert(not f(tonumber('e1')))
|
assert(f(tonumber('e1')) == nil)
|
||||||
assert(not f(tonumber('e 1')))
|
assert(f(tonumber('e 1')) == nil)
|
||||||
assert(not f(tonumber(' 3.4.5 ')))
|
assert(f(tonumber(' 3.4.5 ')) == nil)
|
||||||
|
|
||||||
|
|
||||||
-- testing 'tonumber' for invalid hexadecimal formats
|
-- testing 'tonumber' for invalid hexadecimal formats
|
||||||
|
|
||||||
assert(not tonumber('0x'))
|
assert(tonumber('0x') == nil)
|
||||||
assert(not tonumber('x'))
|
assert(tonumber('x') == nil)
|
||||||
assert(not tonumber('x3'))
|
assert(tonumber('x3') == nil)
|
||||||
assert(not tonumber('0x3.3.3')) -- two decimal points
|
assert(tonumber('0x3.3.3') == nil) -- two decimal points
|
||||||
assert(not tonumber('00x2'))
|
assert(tonumber('00x2') == nil)
|
||||||
assert(not tonumber('0x 2'))
|
assert(tonumber('0x 2') == nil)
|
||||||
assert(not tonumber('0 x2'))
|
assert(tonumber('0 x2') == nil)
|
||||||
assert(not tonumber('23x'))
|
assert(tonumber('23x') == nil)
|
||||||
assert(not tonumber('- 0xaa'))
|
assert(tonumber('- 0xaa') == nil)
|
||||||
assert(not tonumber('-0xaaP ')) -- no exponent
|
assert(tonumber('-0xaaP ') == nil) -- no exponent
|
||||||
assert(not tonumber('0x0.51p'))
|
assert(tonumber('0x0.51p') == nil)
|
||||||
assert(not tonumber('0x5p+-2'))
|
assert(tonumber('0x5p+-2') == nil)
|
||||||
|
|
||||||
|
|
||||||
-- testing hexadecimal numerals
|
-- testing hexadecimal numerals
|
||||||
@@ -560,73 +528,9 @@ assert(eqT(-4 % 3, 2))
|
|||||||
assert(eqT(4 % -3, -2))
|
assert(eqT(4 % -3, -2))
|
||||||
assert(eqT(-4.0 % 3, 2.0))
|
assert(eqT(-4.0 % 3, 2.0))
|
||||||
assert(eqT(4 % -3.0, -2.0))
|
assert(eqT(4 % -3.0, -2.0))
|
||||||
assert(eqT(4 % -5, -1))
|
|
||||||
assert(eqT(4 % -5.0, -1.0))
|
|
||||||
assert(eqT(4 % 5, 4))
|
|
||||||
assert(eqT(4 % 5.0, 4.0))
|
|
||||||
assert(eqT(-4 % -5, -4))
|
|
||||||
assert(eqT(-4 % -5.0, -4.0))
|
|
||||||
assert(eqT(-4 % 5, 1))
|
|
||||||
assert(eqT(-4 % 5.0, 1.0))
|
|
||||||
assert(eqT(4.25 % 4, 0.25))
|
|
||||||
assert(eqT(10.0 % 2, 0.0))
|
|
||||||
assert(eqT(-10.0 % 2, 0.0))
|
|
||||||
assert(eqT(-10.0 % -2, 0.0))
|
|
||||||
assert(math.pi - math.pi % 1 == 3)
|
assert(math.pi - math.pi % 1 == 3)
|
||||||
assert(math.pi - math.pi % 0.001 == 3.141)
|
assert(math.pi - math.pi % 0.001 == 3.141)
|
||||||
|
|
||||||
do -- very small numbers
|
|
||||||
local i, j = 0, 20000
|
|
||||||
while i < j do
|
|
||||||
local m = (i + j) // 2
|
|
||||||
if 10^-m > 0 then
|
|
||||||
i = m + 1
|
|
||||||
else
|
|
||||||
j = m
|
|
||||||
end
|
|
||||||
end
|
|
||||||
-- 'i' is the smallest possible ten-exponent
|
|
||||||
local b = 10^-(i - (i // 10)) -- a very small number
|
|
||||||
assert(b > 0 and b * b == 0)
|
|
||||||
local delta = b / 1000
|
|
||||||
assert(eq((2.1 * b) % (2 * b), (0.1 * b), delta))
|
|
||||||
assert(eq((-2.1 * b) % (2 * b), (2 * b) - (0.1 * b), delta))
|
|
||||||
assert(eq((2.1 * b) % (-2 * b), (0.1 * b) - (2 * b), delta))
|
|
||||||
assert(eq((-2.1 * b) % (-2 * b), (-0.1 * b), delta))
|
|
||||||
end
|
|
||||||
|
|
||||||
|
|
||||||
-- basic consistency between integer modulo and float modulo
|
|
||||||
for i = -10, 10 do
|
|
||||||
for j = -10, 10 do
|
|
||||||
if j ~= 0 then
|
|
||||||
assert((i + 0.0) % j == i % j)
|
|
||||||
end
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
for i = 0, 10 do
|
|
||||||
for j = -10, 10 do
|
|
||||||
if j ~= 0 then
|
|
||||||
assert((2^i) % j == (1 << i) % j)
|
|
||||||
end
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
do -- precision of module for large numbers
|
|
||||||
local i = 10
|
|
||||||
while (1 << i) > 0 do
|
|
||||||
assert((1 << i) % 3 == i % 2 + 1)
|
|
||||||
i = i + 1
|
|
||||||
end
|
|
||||||
|
|
||||||
i = 10
|
|
||||||
while 2^i < math.huge do
|
|
||||||
assert(2^i % 3 == i % 2 + 1)
|
|
||||||
i = i + 1
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
assert(eqT(minint % minint, 0))
|
assert(eqT(minint % minint, 0))
|
||||||
assert(eqT(maxint % maxint, 0))
|
assert(eqT(maxint % maxint, 0))
|
||||||
assert((minint + 1) % minint == minint + 1)
|
assert((minint + 1) % minint == minint + 1)
|
||||||
@@ -639,7 +543,7 @@ assert(maxint % -2 == -1)
|
|||||||
|
|
||||||
-- non-portable tests because Windows C library cannot compute
|
-- non-portable tests because Windows C library cannot compute
|
||||||
-- fmod(1, huge) correctly
|
-- fmod(1, huge) correctly
|
||||||
if not _ENV._port then
|
if not _port then
|
||||||
local function anan (x) assert(isNaN(x)) end -- assert Not a Number
|
local function anan (x) assert(isNaN(x)) end -- assert Not a Number
|
||||||
anan(0.0 % 0)
|
anan(0.0 % 0)
|
||||||
anan(1.3 % 0)
|
anan(1.3 % 0)
|
||||||
@@ -685,18 +589,6 @@ assert(eq(math.exp(0), 1))
|
|||||||
assert(eq(math.sin(10), math.sin(10%(2*math.pi))))
|
assert(eq(math.sin(10), math.sin(10%(2*math.pi))))
|
||||||
|
|
||||||
|
|
||||||
do print("testing ldexp/frexp")
|
|
||||||
global ipairs
|
|
||||||
for _, x in ipairs{0, 10, 32, -math.pi, 1e10, 1e-10, math.huge, -math.huge} do
|
|
||||||
local m, p = math.frexp(x)
|
|
||||||
assert(math.ldexp(m, p) == x)
|
|
||||||
local am = math.abs(m)
|
|
||||||
assert(m == x or (0.5 <= am and am < 1))
|
|
||||||
end
|
|
||||||
|
|
||||||
end
|
|
||||||
|
|
||||||
|
|
||||||
assert(tonumber(' 1.3e-2 ') == 1.3e-2)
|
assert(tonumber(' 1.3e-2 ') == 1.3e-2)
|
||||||
assert(tonumber(' -1.00000000000001 ') == -1.00000000000001)
|
assert(tonumber(' -1.00000000000001 ') == -1.00000000000001)
|
||||||
|
|
||||||
@@ -736,19 +628,19 @@ do -- testing floor & ceil
|
|||||||
assert(eqT(math.tointeger(maxint), maxint))
|
assert(eqT(math.tointeger(maxint), maxint))
|
||||||
assert(eqT(math.tointeger(maxint .. ""), maxint))
|
assert(eqT(math.tointeger(maxint .. ""), maxint))
|
||||||
assert(eqT(math.tointeger(minint + 0.0), minint))
|
assert(eqT(math.tointeger(minint + 0.0), minint))
|
||||||
assert(not math.tointeger(0.0 - minint))
|
assert(math.tointeger(0.0 - minint) == nil)
|
||||||
assert(not math.tointeger(math.pi))
|
assert(math.tointeger(math.pi) == nil)
|
||||||
assert(not math.tointeger(-math.pi))
|
assert(math.tointeger(-math.pi) == nil)
|
||||||
assert(math.floor(math.huge) == math.huge)
|
assert(math.floor(math.huge) == math.huge)
|
||||||
assert(math.ceil(math.huge) == math.huge)
|
assert(math.ceil(math.huge) == math.huge)
|
||||||
assert(not math.tointeger(math.huge))
|
assert(math.tointeger(math.huge) == nil)
|
||||||
assert(math.floor(-math.huge) == -math.huge)
|
assert(math.floor(-math.huge) == -math.huge)
|
||||||
assert(math.ceil(-math.huge) == -math.huge)
|
assert(math.ceil(-math.huge) == -math.huge)
|
||||||
assert(not math.tointeger(-math.huge))
|
assert(math.tointeger(-math.huge) == nil)
|
||||||
assert(math.tointeger("34.0") == 34)
|
assert(math.tointeger("34.0") == 34)
|
||||||
assert(not math.tointeger("34.3"))
|
assert(math.tointeger("34.3") == nil)
|
||||||
assert(not math.tointeger({}))
|
assert(math.tointeger({}) == nil)
|
||||||
assert(not math.tointeger(0/0)) -- NaN
|
assert(math.tointeger(0/0) == nil) -- NaN
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
||||||
@@ -789,7 +681,7 @@ do -- testing max/min
|
|||||||
assert(eqT(math.min(maxint, maxint - 1), maxint - 1))
|
assert(eqT(math.min(maxint, maxint - 1), maxint - 1))
|
||||||
assert(eqT(math.min(maxint - 2, maxint, maxint - 1), maxint - 2))
|
assert(eqT(math.min(maxint - 2, maxint, maxint - 1), maxint - 2))
|
||||||
end
|
end
|
||||||
-- testing implicit conversions
|
-- testing implicit convertions
|
||||||
|
|
||||||
local a,b = '10', '20'
|
local a,b = '10', '20'
|
||||||
assert(a*b == 200 and a+b == 30 and a-b == -10 and a/b == 0.5 and -b == -20)
|
assert(a*b == 200 and a+b == 30 and a-b == -10 and a/b == 0.5 and -b == -20)
|
||||||
@@ -798,9 +690,7 @@ assert(a == '10' and b == '20')
|
|||||||
|
|
||||||
do
|
do
|
||||||
print("testing -0 and NaN")
|
print("testing -0 and NaN")
|
||||||
global rawset, undef
|
local mz, z = -0.0, 0.0
|
||||||
local mz <const> = -0.0
|
|
||||||
local z <const> = 0.0
|
|
||||||
assert(mz == z)
|
assert(mz == z)
|
||||||
assert(1/mz < 0 and 0 < 1/z)
|
assert(1/mz < 0 and 0 < 1/z)
|
||||||
local a = {[mz] = 1}
|
local a = {[mz] = 1}
|
||||||
@@ -808,25 +698,24 @@ do
|
|||||||
a[z] = 2
|
a[z] = 2
|
||||||
assert(a[z] == 2 and a[mz] == 2)
|
assert(a[z] == 2 and a[mz] == 2)
|
||||||
local inf = math.huge * 2 + 1
|
local inf = math.huge * 2 + 1
|
||||||
local mz <const> = -1/inf
|
mz, z = -1/inf, 1/inf
|
||||||
local z <const> = 1/inf
|
|
||||||
assert(mz == z)
|
assert(mz == z)
|
||||||
assert(1/mz < 0 and 0 < 1/z)
|
assert(1/mz < 0 and 0 < 1/z)
|
||||||
local NaN <const> = inf - inf
|
local NaN = inf - inf
|
||||||
assert(NaN ~= NaN)
|
assert(NaN ~= NaN)
|
||||||
assert(not (NaN < NaN))
|
assert(not (NaN < NaN))
|
||||||
assert(not (NaN <= NaN))
|
assert(not (NaN <= NaN))
|
||||||
assert(not (NaN > NaN))
|
assert(not (NaN > NaN))
|
||||||
assert(not (NaN >= NaN))
|
assert(not (NaN >= NaN))
|
||||||
assert(not (0 < NaN) and not (NaN < 0))
|
assert(not (0 < NaN) and not (NaN < 0))
|
||||||
local NaN1 <const> = 0/0
|
local NaN1 = 0/0
|
||||||
assert(NaN ~= NaN1 and not (NaN <= NaN1) and not (NaN1 <= NaN))
|
assert(NaN ~= NaN1 and not (NaN <= NaN1) and not (NaN1 <= NaN))
|
||||||
local a = {}
|
local a = {}
|
||||||
assert(not pcall(rawset, a, NaN, 1))
|
assert(not pcall(rawset, a, NaN, 1))
|
||||||
assert(a[NaN] == undef)
|
assert(a[NaN] == nil)
|
||||||
a[1] = 1
|
a[1] = 1
|
||||||
assert(not pcall(rawset, a, NaN, 1))
|
assert(not pcall(rawset, a, NaN, 1))
|
||||||
assert(a[NaN] == undef)
|
assert(a[NaN] == nil)
|
||||||
-- strings with same binary representation as 0.0 (might create problems
|
-- strings with same binary representation as 0.0 (might create problems
|
||||||
-- for constant manipulation in the pre-compiler)
|
-- for constant manipulation in the pre-compiler)
|
||||||
local a1, a2, a3, a4, a5 = 0, 0, "\0\0\0\0\0\0\0\0", 0, "\0\0\0\0\0\0\0\0"
|
local a1, a2, a3, a4, a5 = 0, 0, "\0\0\0\0\0\0\0\0", 0, "\0\0\0\0\0\0\0\0"
|
||||||
@@ -835,311 +724,101 @@ do
|
|||||||
end
|
end
|
||||||
|
|
||||||
|
|
||||||
--
|
print("testing 'math.random'")
|
||||||
-- [[==================================================================
|
math.randomseed(0)
|
||||||
print("testing 'math.random'")
|
|
||||||
-- -===================================================================
|
|
||||||
--
|
|
||||||
|
|
||||||
local random, max, min = math.random, math.max, math.min
|
|
||||||
|
|
||||||
local function testnear (val, ref, tol)
|
|
||||||
return (math.abs(val - ref) < ref * tol)
|
|
||||||
end
|
|
||||||
|
|
||||||
|
|
||||||
-- low-level!! For the current implementation of random in Lua,
|
|
||||||
-- the first call after seed 1007 should return 0x7a7040a5a323c9d6
|
|
||||||
do
|
|
||||||
-- all computations should work with 32-bit integers
|
|
||||||
local h <const> = 0x7a7040a5 -- higher half
|
|
||||||
local l <const> = 0xa323c9d6 -- lower half
|
|
||||||
|
|
||||||
math.randomseed(1007)
|
|
||||||
-- get the low 'intbits' of the 64-bit expected result
|
|
||||||
local res = (h << 32 | l) & ~(~0 << intbits)
|
|
||||||
assert(random(0) == res)
|
|
||||||
|
|
||||||
math.randomseed(1007, 0)
|
|
||||||
-- using higher bits to generate random floats; (the '% 2^32' converts
|
|
||||||
-- 32-bit integers to floats as unsigned)
|
|
||||||
local res
|
|
||||||
if floatbits <= 32 then
|
|
||||||
-- get all bits from the higher half
|
|
||||||
res = (h >> (32 - floatbits)) % 2^32
|
|
||||||
else
|
|
||||||
-- get 32 bits from the higher half and the rest from the lower half
|
|
||||||
res = (h % 2^32) * 2^(floatbits - 32) + ((l >> (64 - floatbits)) % 2^32)
|
|
||||||
end
|
|
||||||
local rand = random()
|
|
||||||
assert(eq(rand, 0x0.7a7040a5a323c9d6, 2^-floatbits))
|
|
||||||
assert(rand * 2^floatbits == res)
|
|
||||||
end
|
|
||||||
|
|
||||||
do
|
|
||||||
-- testing return of 'randomseed'
|
|
||||||
local x, y = math.randomseed()
|
|
||||||
local res = math.random(0)
|
|
||||||
x, y = math.randomseed(x, y) -- should repeat the state
|
|
||||||
assert(math.random(0) == res)
|
|
||||||
math.randomseed(x, y) -- again should repeat the state
|
|
||||||
assert(math.random(0) == res)
|
|
||||||
-- keep the random seed for following tests
|
|
||||||
print(string.format("random seeds: %d, %d", x, y))
|
|
||||||
end
|
|
||||||
|
|
||||||
do -- test random for floats
|
do -- test random for floats
|
||||||
local randbits = math.min(floatbits, 64) -- at most 64 random bits
|
local max = -math.huge
|
||||||
local mult = 2^randbits -- to make random float into an integral
|
local min = math.huge
|
||||||
local counts = {} -- counts for bits
|
for i = 0, 20000 do
|
||||||
for i = 1, randbits do counts[i] = 0 end
|
local t = math.random()
|
||||||
local up = -math.huge
|
|
||||||
local low = math.huge
|
|
||||||
local rounds = 100 * randbits -- 100 times for each bit
|
|
||||||
local totalrounds = 0
|
|
||||||
::doagain:: -- will repeat test until we get good statistics
|
|
||||||
for i = 0, rounds do
|
|
||||||
local t = random()
|
|
||||||
assert(0 <= t and t < 1)
|
assert(0 <= t and t < 1)
|
||||||
up = max(up, t)
|
max = math.max(max, t)
|
||||||
low = min(low, t)
|
min = math.min(min, t)
|
||||||
assert(t * mult % 1 == 0) -- no extra bits
|
if eq(max, 1, 0.001) and eq(min, 0, 0.001) then
|
||||||
local bit = i % randbits -- bit to be tested
|
goto ok
|
||||||
if (t * 2^bit) % 1 >= 0.5 then -- is bit set?
|
|
||||||
counts[bit + 1] = counts[bit + 1] + 1 -- increment its count
|
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
totalrounds = totalrounds + rounds
|
-- loop ended without satisfing condition
|
||||||
if not (eq(up, 1, 0.001) and eq(low, 0, 0.001)) then
|
assert(false)
|
||||||
goto doagain
|
::ok::
|
||||||
end
|
|
||||||
-- all bit counts should be near 50%
|
|
||||||
local expected = (totalrounds / randbits / 2)
|
|
||||||
for i = 1, randbits do
|
|
||||||
if not testnear(counts[i], expected, 0.10) then
|
|
||||||
goto doagain
|
|
||||||
end
|
|
||||||
end
|
|
||||||
print(string.format("float random range in %d calls: [%f, %f]",
|
|
||||||
totalrounds, low, up))
|
|
||||||
end
|
|
||||||
|
|
||||||
|
|
||||||
do -- test random for full integers
|
|
||||||
local up = 0
|
|
||||||
local low = 0
|
|
||||||
local counts = {} -- counts for bits
|
|
||||||
for i = 1, intbits do counts[i] = 0 end
|
|
||||||
local rounds = 100 * intbits -- 100 times for each bit
|
|
||||||
local totalrounds = 0
|
|
||||||
::doagain:: -- will repeat test until we get good statistics
|
|
||||||
for i = 0, rounds do
|
|
||||||
local t = random(0)
|
|
||||||
up = max(up, t)
|
|
||||||
low = min(low, t)
|
|
||||||
local bit = i % intbits -- bit to be tested
|
|
||||||
-- increment its count if it is set
|
|
||||||
counts[bit + 1] = counts[bit + 1] + ((t >> bit) & 1)
|
|
||||||
end
|
|
||||||
totalrounds = totalrounds + rounds
|
|
||||||
local lim = maxint >> 10
|
|
||||||
if not (maxint - up < lim and low - minint < lim) then
|
|
||||||
goto doagain
|
|
||||||
end
|
|
||||||
-- all bit counts should be near 50%
|
|
||||||
local expected = (totalrounds / intbits / 2)
|
|
||||||
for i = 1, intbits do
|
|
||||||
if not testnear(counts[i], expected, 0.10) then
|
|
||||||
goto doagain
|
|
||||||
end
|
|
||||||
end
|
|
||||||
print(string.format(
|
|
||||||
"integer random range in %d calls: [minint + %.0fppm, maxint - %.0fppm]",
|
|
||||||
totalrounds, (minint - low) / minint * 1e6,
|
|
||||||
(maxint - up) / maxint * 1e6))
|
|
||||||
end
|
end
|
||||||
|
|
||||||
do
|
do
|
||||||
-- test distribution for a dice
|
local function aux (p, lim) -- test random for small intervals
|
||||||
local count = {0, 0, 0, 0, 0, 0}
|
local x1, x2
|
||||||
local rep = 200
|
if #p == 1 then x1 = 1; x2 = p[1]
|
||||||
local totalrep = 0
|
else x1 = p[1]; x2 = p[2]
|
||||||
::doagain::
|
|
||||||
for i = 1, rep * 6 do
|
|
||||||
local r = random(6)
|
|
||||||
count[r] = count[r] + 1
|
|
||||||
end
|
|
||||||
totalrep = totalrep + rep
|
|
||||||
for i = 1, 6 do
|
|
||||||
if not testnear(count[i], totalrep, 0.05) then
|
|
||||||
goto doagain
|
|
||||||
end
|
end
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
do
|
|
||||||
local function aux (x1, x2) -- test random for small intervals
|
|
||||||
local mark = {}; local count = 0 -- to check that all values appeared
|
local mark = {}; local count = 0 -- to check that all values appeared
|
||||||
while true do
|
for i = 0, lim or 2000 do
|
||||||
local t = random(x1, x2)
|
local t = math.random(table.unpack(p))
|
||||||
assert(x1 <= t and t <= x2)
|
assert(x1 <= t and t <= x2)
|
||||||
if not mark[t] then -- new value
|
if not mark[t] then -- new value
|
||||||
mark[t] = true
|
mark[t] = true
|
||||||
count = count + 1
|
count = count + 1
|
||||||
if count == x2 - x1 + 1 then -- all values appeared; OK
|
end
|
||||||
goto ok
|
if count == x2 - x1 + 1 then -- all values appeared; OK
|
||||||
end
|
goto ok
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
-- loop ended without satisfing condition
|
||||||
|
assert(false)
|
||||||
::ok::
|
::ok::
|
||||||
end
|
end
|
||||||
|
|
||||||
aux(-10,0)
|
aux({-10,0})
|
||||||
aux(1, 6)
|
aux({6})
|
||||||
aux(1, 2)
|
aux({-10, 10})
|
||||||
aux(1, 13)
|
aux({minint, minint})
|
||||||
aux(1, 31)
|
aux({maxint, maxint})
|
||||||
aux(1, 32)
|
aux({minint, minint + 9})
|
||||||
aux(1, 33)
|
aux({maxint - 3, maxint})
|
||||||
aux(-10, 10)
|
|
||||||
aux(-10,-10) -- unit set
|
|
||||||
aux(minint, minint) -- unit set
|
|
||||||
aux(maxint, maxint) -- unit set
|
|
||||||
aux(minint, minint + 9)
|
|
||||||
aux(maxint - 3, maxint)
|
|
||||||
end
|
end
|
||||||
|
|
||||||
do
|
do
|
||||||
local function aux(p1, p2) -- test random for large intervals
|
local function aux(p1, p2) -- test random for large intervals
|
||||||
local max = minint
|
local max = minint
|
||||||
local min = maxint
|
local min = maxint
|
||||||
local n = 100
|
local n = 200
|
||||||
local mark = {}; local count = 0 -- to count how many different values
|
local mark = {}; local count = 0 -- to count how many different values
|
||||||
::doagain::
|
|
||||||
for _ = 1, n do
|
for _ = 1, n do
|
||||||
local t = random(p1, p2)
|
local t = math.random(p1, p2)
|
||||||
|
max = math.max(max, t)
|
||||||
|
min = math.min(min, t)
|
||||||
if not mark[t] then -- new value
|
if not mark[t] then -- new value
|
||||||
assert(p1 <= t and t <= p2)
|
|
||||||
max = math.max(max, t)
|
|
||||||
min = math.min(min, t)
|
|
||||||
mark[t] = true
|
mark[t] = true
|
||||||
count = count + 1
|
count = count + 1
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
-- at least 80% of values are different
|
-- at least 80% of values are different
|
||||||
if not (count >= n * 0.8) then
|
assert(count >= n * 0.8)
|
||||||
goto doagain
|
|
||||||
end
|
|
||||||
-- min and max not too far from formal min and max
|
-- min and max not too far from formal min and max
|
||||||
local diff = (p2 - p1) >> 4
|
local diff = (p2 - p1) // 8
|
||||||
if not (min < p1 + diff and max > p2 - diff) then
|
assert(min < p1 + diff and max > p2 - diff)
|
||||||
goto doagain
|
|
||||||
end
|
|
||||||
end
|
end
|
||||||
aux(0, maxint)
|
aux(0, maxint)
|
||||||
aux(1, maxint)
|
aux(1, maxint)
|
||||||
aux(3, maxint // 3)
|
|
||||||
aux(minint, -1)
|
aux(minint, -1)
|
||||||
aux(minint // 2, maxint // 2)
|
aux(minint // 2, maxint // 2)
|
||||||
aux(minint, maxint)
|
|
||||||
aux(minint + 1, maxint)
|
|
||||||
aux(minint, maxint - 1)
|
|
||||||
aux(0, 1 << (intbits - 5))
|
|
||||||
end
|
end
|
||||||
|
|
||||||
|
for i=1,100 do
|
||||||
|
assert(math.random(maxint) > 0)
|
||||||
|
assert(math.random(minint, -1) < 0)
|
||||||
|
end
|
||||||
|
|
||||||
assert(not pcall(random, 1, 2, 3)) -- too many arguments
|
assert(not pcall(math.random, 1, 2, 3)) -- too many arguments
|
||||||
|
|
||||||
-- empty interval
|
-- empty interval
|
||||||
assert(not pcall(random, minint + 1, minint))
|
assert(not pcall(math.random, minint + 1, minint))
|
||||||
assert(not pcall(random, maxint, maxint - 1))
|
assert(not pcall(math.random, maxint, maxint - 1))
|
||||||
assert(not pcall(random, maxint, minint))
|
assert(not pcall(math.random, maxint, minint))
|
||||||
|
|
||||||
-- ]]==================================================================
|
-- interval too large
|
||||||
|
assert(not pcall(math.random, minint, 0))
|
||||||
|
assert(not pcall(math.random, -1, maxint))
|
||||||
--
|
assert(not pcall(math.random, minint // 2, maxint // 2 + 1))
|
||||||
-- [[==================================================================
|
|
||||||
print("testing precision of 'tostring'")
|
|
||||||
-- -===================================================================
|
|
||||||
--
|
|
||||||
|
|
||||||
-- number of decimal digits supported by float precision
|
|
||||||
local decdig = math.floor(floatbits * math.log(2, 10))
|
|
||||||
print(string.format(" %d-digit float numbers with full precision",
|
|
||||||
decdig))
|
|
||||||
-- number of decimal digits supported by integer precision
|
|
||||||
local Idecdig = math.floor(math.log(maxint, 10))
|
|
||||||
print(string.format(" %d-digit integer numbers with full precision",
|
|
||||||
Idecdig))
|
|
||||||
|
|
||||||
do
|
|
||||||
-- Any number should print so that reading it back gives itself:
|
|
||||||
-- tonumber(tostring(x)) == x
|
|
||||||
|
|
||||||
-- Mersenne fractions
|
|
||||||
local p = 1.0
|
|
||||||
for i = 1, maxexp do
|
|
||||||
p = p + p
|
|
||||||
local x = 1 / (p - 1)
|
|
||||||
assert(x == tonumber(tostring(x)))
|
|
||||||
end
|
|
||||||
|
|
||||||
-- some random numbers in [0,1)
|
|
||||||
for i = 1, 100 do
|
|
||||||
local x = math.random()
|
|
||||||
assert(x == tonumber(tostring(x)))
|
|
||||||
end
|
|
||||||
|
|
||||||
-- different numbers should print differently.
|
|
||||||
-- check pairs of floats with minimum detectable difference
|
|
||||||
local p = floatbits - 1
|
|
||||||
global ipairs
|
|
||||||
for i = 1, maxexp - 1 do
|
|
||||||
for _, i in ipairs{-i, i} do
|
|
||||||
local x = 2^i
|
|
||||||
local diff = 2^(i - p) -- least significant bit for 'x'
|
|
||||||
local y = x + diff
|
|
||||||
local fy = tostring(y)
|
|
||||||
assert(x ~= y and tostring(x) ~= fy)
|
|
||||||
assert(tonumber(fy) == y)
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
|
|
||||||
-- "reasonable" numerals should be printed like themselves
|
|
||||||
|
|
||||||
-- create random float numerals with 5 digits, with a decimal point
|
|
||||||
-- inserted in all places. (With more than 5, things like "0.00001"
|
|
||||||
-- reformats like "1e-5".)
|
|
||||||
for i = 1, 1000 do
|
|
||||||
-- random numeral with 5 digits
|
|
||||||
local x = string.format("%.5d", math.random(0, 99999))
|
|
||||||
for i = 2, #x do
|
|
||||||
-- insert decimal point at position 'i'
|
|
||||||
local y = string.sub(x, 1, i - 1) .. "." .. string.sub(x, i, -1)
|
|
||||||
y = string.gsub(y, "^0*(%d.-%d)0*$", "%1") -- trim extra zeros
|
|
||||||
assert(y == tostring(tonumber(y)))
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
-- all-random floats
|
|
||||||
local Fsz = string.packsize("n") -- size of floats in bytes
|
|
||||||
|
|
||||||
for i = 1, 400 do
|
|
||||||
local s = string.pack("j", math.random(0)) -- a random string of bits
|
|
||||||
while #s < Fsz do -- make 's' long enough
|
|
||||||
s = s .. string.pack("j", math.random(0))
|
|
||||||
end
|
|
||||||
local n = string.unpack("n", s) -- read 's' as a float
|
|
||||||
s = tostring(n)
|
|
||||||
if string.find(s, "^%-?%d") then -- avoid NaN, inf, -inf
|
|
||||||
assert(tonumber(s) == n)
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
end
|
|
||||||
-- ]]==================================================================
|
|
||||||
|
|
||||||
|
|
||||||
print('OK')
|
print('OK')
|
||||||
|
|||||||
@@ -1,290 +0,0 @@
|
|||||||
-- $Id: testes/memerr.lua $
|
|
||||||
-- See Copyright Notice in file lua.h
|
|
||||||
|
|
||||||
|
|
||||||
local function checkerr (msg, f, ...)
|
|
||||||
local stat, err = pcall(f, ...)
|
|
||||||
assert(not stat and string.find(err, msg))
|
|
||||||
end
|
|
||||||
|
|
||||||
if T==nil then
|
|
||||||
(Message or print)
|
|
||||||
('\n >>> testC not active: skipping memory error tests <<<\n')
|
|
||||||
return
|
|
||||||
end
|
|
||||||
|
|
||||||
print("testing memory-allocation errors")
|
|
||||||
|
|
||||||
local debug = require "debug"
|
|
||||||
|
|
||||||
local pack = table.pack
|
|
||||||
|
|
||||||
-- standard error message for memory errors
|
|
||||||
local MEMERRMSG = "not enough memory"
|
|
||||||
|
|
||||||
|
|
||||||
-- memory error in panic function
|
|
||||||
T.totalmem(T.totalmem()+10000) -- set low memory limit (+10k)
|
|
||||||
assert(T.checkpanic("newuserdata 20000") == MEMERRMSG)
|
|
||||||
T.totalmem(0) -- restore high limit
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
-- {==================================================================
|
|
||||||
-- Testing memory limits
|
|
||||||
-- ===================================================================
|
|
||||||
|
|
||||||
checkerr("block too big", T.newuserdata, math.maxinteger)
|
|
||||||
collectgarbage()
|
|
||||||
local f = load"local a={}; for i=1,100000 do a[i]=i end"
|
|
||||||
T.alloccount(10)
|
|
||||||
checkerr(MEMERRMSG, f)
|
|
||||||
T.alloccount() -- remove limit
|
|
||||||
|
|
||||||
|
|
||||||
-- preallocate stack space
|
|
||||||
local function deep (n) if n > 0 then deep(n - 1) end end
|
|
||||||
|
|
||||||
|
|
||||||
-- test memory errors; increase limit for maximum memory by steps,
|
|
||||||
-- so that we get memory errors in all allocations of a given
|
|
||||||
-- task, until there is enough memory to complete the task without
|
|
||||||
-- errors.
|
|
||||||
local function testbytes (s, f)
|
|
||||||
collectgarbage()
|
|
||||||
local M = T.totalmem()
|
|
||||||
local oldM = M
|
|
||||||
local a,b = nil
|
|
||||||
while true do
|
|
||||||
collectgarbage(); collectgarbage()
|
|
||||||
deep(4)
|
|
||||||
T.totalmem(M)
|
|
||||||
a, b = T.testC("pcall 0 1 0; pushstatus; return 2", f)
|
|
||||||
T.totalmem(0) -- remove limit
|
|
||||||
if a and b == "OK" then break end -- stop when no more errors
|
|
||||||
if b ~= "OK" and b ~= MEMERRMSG then -- not a memory error?
|
|
||||||
error(a, 0) -- propagate it
|
|
||||||
end
|
|
||||||
M = M + 7 -- increase memory limit
|
|
||||||
end
|
|
||||||
print(string.format("minimum memory for %s: %d bytes", s, M - oldM))
|
|
||||||
return a
|
|
||||||
end
|
|
||||||
|
|
||||||
-- test memory errors; increase limit for number of allocations one
|
|
||||||
-- by one, so that we get memory errors in all allocations of a given
|
|
||||||
-- task, until there is enough allocations to complete the task without
|
|
||||||
-- errors.
|
|
||||||
|
|
||||||
local function testalloc (s, f)
|
|
||||||
collectgarbage()
|
|
||||||
local M = 0
|
|
||||||
local a,b = nil
|
|
||||||
while true do
|
|
||||||
collectgarbage(); collectgarbage()
|
|
||||||
deep(4)
|
|
||||||
T.alloccount(M)
|
|
||||||
a, b = T.testC("pcall 0 1 0; pushstatus; return 2", f)
|
|
||||||
T.alloccount() -- remove limit
|
|
||||||
if a and b == "OK" then break end -- stop when no more errors
|
|
||||||
if b ~= "OK" and b ~= MEMERRMSG then -- not a memory error?
|
|
||||||
error(a, 0) -- propagate it
|
|
||||||
end
|
|
||||||
M = M + 1 -- increase allocation limit
|
|
||||||
end
|
|
||||||
print(string.format("minimum allocations for %s: %d allocations", s, M))
|
|
||||||
return M
|
|
||||||
end
|
|
||||||
|
|
||||||
|
|
||||||
local function testamem (s, f)
|
|
||||||
local aloc = testalloc(s, f)
|
|
||||||
local res = testbytes(s, f)
|
|
||||||
return {aloc = aloc, res = res}
|
|
||||||
end
|
|
||||||
|
|
||||||
|
|
||||||
local b = testamem("function call", function () return 10 end)
|
|
||||||
assert(b.res == 10 and b.aloc == 0)
|
|
||||||
|
|
||||||
testamem("state creation", function ()
|
|
||||||
local st = T.newstate()
|
|
||||||
if st then T.closestate(st) end -- close new state
|
|
||||||
return st
|
|
||||||
end)
|
|
||||||
|
|
||||||
testamem("empty-table creation", function ()
|
|
||||||
return {}
|
|
||||||
end)
|
|
||||||
|
|
||||||
testamem("string creation", function ()
|
|
||||||
return "XXX" .. "YYY"
|
|
||||||
end)
|
|
||||||
|
|
||||||
testamem("coroutine creation", function()
|
|
||||||
return coroutine.create(print)
|
|
||||||
end)
|
|
||||||
|
|
||||||
do -- vararg tables
|
|
||||||
local function pack (...t) return t end
|
|
||||||
local b = testamem("vararg table", function ()
|
|
||||||
return pack(10, 20, 30, 40, "hello")
|
|
||||||
end)
|
|
||||||
assert(b.aloc == 3) -- new table uses three memory blocks
|
|
||||||
-- table optimized away
|
|
||||||
local function sel (n, ...arg) return arg[n] + arg.n end
|
|
||||||
local b = testamem("optimized vararg table",
|
|
||||||
function () return sel(2.0, 20, 30) end)
|
|
||||||
assert(b.res == 32 and b.aloc == 0) -- no memory needed for this case
|
|
||||||
end
|
|
||||||
|
|
||||||
-- testing to-be-closed variables
|
|
||||||
testamem("to-be-closed variables", function()
|
|
||||||
local flag
|
|
||||||
do
|
|
||||||
local x <close> =
|
|
||||||
setmetatable({}, {__close = function () flag = true end})
|
|
||||||
flag = false
|
|
||||||
local x = {}
|
|
||||||
end
|
|
||||||
return flag
|
|
||||||
end)
|
|
||||||
|
|
||||||
|
|
||||||
-- testing threads
|
|
||||||
|
|
||||||
-- get main thread from registry
|
|
||||||
local mt = T.testC("rawgeti R !M; return 1")
|
|
||||||
assert(type(mt) == "thread" and coroutine.running() == mt)
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
local function expand (n,s)
|
|
||||||
if n==0 then return "" end
|
|
||||||
local e = string.rep("=", n)
|
|
||||||
return string.format("T.doonnewstack([%s[ %s;\n collectgarbage(); %s]%s])\n",
|
|
||||||
e, s, expand(n-1,s), e)
|
|
||||||
end
|
|
||||||
|
|
||||||
G=0; collectgarbage()
|
|
||||||
load(expand(20,"G=G+1"))()
|
|
||||||
assert(G==20); collectgarbage()
|
|
||||||
G = nil
|
|
||||||
|
|
||||||
testamem("running code on new thread", function ()
|
|
||||||
return T.doonnewstack("local x=1") == 0 -- try to create thread
|
|
||||||
end)
|
|
||||||
|
|
||||||
|
|
||||||
do -- external strings
|
|
||||||
local str = string.rep("a", 100)
|
|
||||||
testamem("creating external strings", function ()
|
|
||||||
return T.externstr(str)
|
|
||||||
end)
|
|
||||||
end
|
|
||||||
|
|
||||||
|
|
||||||
-- testing memory x compiler
|
|
||||||
|
|
||||||
testamem("loadstring", function ()
|
|
||||||
return load("x=1") -- try to do load a string
|
|
||||||
end)
|
|
||||||
|
|
||||||
|
|
||||||
local testprog = [[
|
|
||||||
local function foo () return end
|
|
||||||
local t = {"x"}
|
|
||||||
AA = "aaa"
|
|
||||||
for i = 1, #t do AA = AA .. t[i] end
|
|
||||||
return true
|
|
||||||
]]
|
|
||||||
|
|
||||||
-- testing memory x dofile
|
|
||||||
_G.AA = nil
|
|
||||||
local t =os.tmpname()
|
|
||||||
local f = assert(io.open(t, "w"))
|
|
||||||
f:write(testprog)
|
|
||||||
f:close()
|
|
||||||
testamem("dofile", function ()
|
|
||||||
local a = loadfile(t)
|
|
||||||
return a and a()
|
|
||||||
end)
|
|
||||||
assert(os.remove(t))
|
|
||||||
assert(_G.AA == "aaax")
|
|
||||||
|
|
||||||
|
|
||||||
-- other generic tests
|
|
||||||
|
|
||||||
testamem("gsub", function ()
|
|
||||||
local a, b = string.gsub("alo alo", "(a)", function (x) return x..'b' end)
|
|
||||||
return (a == 'ablo ablo')
|
|
||||||
end)
|
|
||||||
|
|
||||||
testamem("dump/undump", function ()
|
|
||||||
local a = load(testprog)
|
|
||||||
local b = a and string.dump(a)
|
|
||||||
a = b and load(b)
|
|
||||||
return a and a()
|
|
||||||
end)
|
|
||||||
|
|
||||||
_G.AA = nil
|
|
||||||
|
|
||||||
local t = os.tmpname()
|
|
||||||
testamem("file creation", function ()
|
|
||||||
local f = assert(io.open(t, 'w'))
|
|
||||||
assert (not io.open"nomenaoexistente")
|
|
||||||
io.close(f);
|
|
||||||
return not loadfile'nomenaoexistente'
|
|
||||||
end)
|
|
||||||
assert(os.remove(t))
|
|
||||||
|
|
||||||
testamem("table creation", function ()
|
|
||||||
local a, lim = {}, 10
|
|
||||||
for i=1,lim do a[i] = i; a[i..'a'] = {} end
|
|
||||||
return (type(a[lim..'a']) == 'table' and a[lim] == lim)
|
|
||||||
end)
|
|
||||||
|
|
||||||
testamem("constructors", function ()
|
|
||||||
local a = {10, 20, 30, 40, 50; a=1, b=2, c=3, d=4, e=5}
|
|
||||||
return (type(a) == 'table' and a.e == 5)
|
|
||||||
end)
|
|
||||||
|
|
||||||
local a = 1
|
|
||||||
local close = nil
|
|
||||||
testamem("closure creation", function ()
|
|
||||||
function close (b)
|
|
||||||
return function (x) return b + x end
|
|
||||||
end
|
|
||||||
return (close(2)(4) == 6)
|
|
||||||
end)
|
|
||||||
|
|
||||||
testamem("using coroutines", function ()
|
|
||||||
local a = coroutine.wrap(function ()
|
|
||||||
coroutine.yield(string.rep("a", 10))
|
|
||||||
return {}
|
|
||||||
end)
|
|
||||||
assert(string.len(a()) == 10)
|
|
||||||
return a()
|
|
||||||
end)
|
|
||||||
|
|
||||||
do -- auxiliary buffer
|
|
||||||
local lim = 100
|
|
||||||
local a = {}; for i = 1, lim do a[i] = "01234567890123456789" end
|
|
||||||
testamem("auxiliary buffer", function ()
|
|
||||||
return (#table.concat(a, ",") == 20*lim + lim - 1)
|
|
||||||
end)
|
|
||||||
end
|
|
||||||
|
|
||||||
testamem("growing stack", function ()
|
|
||||||
local function foo (n)
|
|
||||||
if n == 0 then return 1 else return 1 + foo(n - 1) end
|
|
||||||
end
|
|
||||||
return foo(100)
|
|
||||||
end)
|
|
||||||
|
|
||||||
-- }==================================================================
|
|
||||||
|
|
||||||
|
|
||||||
print "Ok"
|
|
||||||
|
|
||||||
|
|
||||||
Some files were not shown because too many files have changed in this diff Show More
Reference in New Issue
Block a user